blob: 1edc7e66ba671d085f0f28e61b8c33a87f4e25ab [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!!!
Bram Moolenaar041c7102020-05-30 18:14:57 +020015 * Also for Atari MiNT.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016 *
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 Moolenaara2442432007-04-26 14:26:37 +000046#ifdef __CYGWIN__
Bram Moolenaar4f974752019-02-17 17:44:42 +010047# ifndef MSWIN
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000048# include <cygwin/version.h>
Bram Moolenaar0f873732019-12-05 20:28:46 +010049# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
50 // for cygwin_conv_path()
Bram Moolenaar693e40c2013-02-26 14:56:42 +010051# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
52# define WIN32_LEAN_AND_MEAN
53# include <windows.h>
54# include "winclip.pro"
55# endif
Bram Moolenaara2442432007-04-26 14:26:37 +000056# endif
57#endif
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#ifdef FEAT_MOUSE_GPM
60# include <gpm.h>
Bram Moolenaar0f873732019-12-05 20:28:46 +010061// <linux/keyboard.h> contains defines conflicting with "keymap.h",
62// I just copied relevant defines here. A cleaner solution would be to put gpm
63// code into separate file and include there linux/keyboard.h
64// #include <linux/keyboard.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000065# define KG_SHIFT 0
66# define KG_CTRL 2
67# define KG_ALT 3
68# define KG_ALTGR 1
69# define KG_SHIFTL 4
70# define KG_SHIFTR 5
71# define KG_CTRLL 6
72# define KG_CTRLR 7
73# define KG_CAPSSHIFT 8
74
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010075static void gpm_close(void);
76static int gpm_open(void);
77static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000078#endif
79
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000080#ifdef FEAT_SYSMOUSE
81# include <sys/consio.h>
82# include <sys/fbio.h>
83
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010084static int sysmouse_open(void);
85static void sysmouse_close(void);
Bram Moolenaard99df422016-01-29 23:20:40 +010086static RETSIGTYPE sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000087#endif
88
Bram Moolenaar071d4272004-06-13 20:20:40 +000089/*
90 * end of autoconf section. To be extended...
91 */
92
Bram Moolenaar0f873732019-12-05 20:28:46 +010093// Are the following #ifdefs still required? And why? Is that for X11?
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
95#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
96# ifdef SIGWINCH
97# undef SIGWINCH
98# endif
99# ifdef TIOCGWINSZ
100# undef TIOCGWINSZ
101# endif
102#endif
103
Bram Moolenaar0f873732019-12-05 20:28:46 +0100104#if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105# define SIGWINCH SIGWINDOW
106#endif
107
108#ifdef FEAT_X11
109# include <X11/Xlib.h>
110# include <X11/Xutil.h>
111# include <X11/Xatom.h>
112# ifdef FEAT_XCLIPBOARD
113# include <X11/Intrinsic.h>
114# include <X11/Shell.h>
115# include <X11/StringDefs.h>
116static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static void clip_update(void);
118static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119# endif
120
121# if defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE)
122Window x11_window = 0;
123# endif
124Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
126
Bram Moolenaar5c3128e2020-05-11 20:54:42 +0200127static int ignore_sigtstp = FALSE;
128
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifdef FEAT_TITLE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
132static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200133static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +0200134static int unix_did_set_title = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135static char_u *oldicon = NULL;
136static int did_set_icon = FALSE;
137#endif
138
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140
Bram Moolenaar205b8862011-09-07 15:04:31 +0200141#ifdef HAVE_UNION_WAIT
142typedef union wait waitstatus;
143#else
144typedef int waitstatus;
145#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200146static int WaitForChar(long msec, int *interrupted, int ignore_input);
147static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar041c7102020-05-30 18:14:57 +0200148#ifdef VMS
Bram Moolenaarcda77642016-06-04 13:32:35 +0200149int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200151static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#endif
153
154#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static int do_xterm_trace(void);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100156# define XT_TRACE_DELAY 50 // delay for xterm tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160
161#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100162static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#endif
164#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100165static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200167#if defined(SIGUSR1)
168static RETSIGTYPE catch_sigusr1 SIGPROTOARG;
169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100171static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#if defined(SIGALRM) && defined(FEAT_X11) \
174 && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
175# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100176static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100177// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200178static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100180static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100182static void catch_int_signal(void);
183static void set_signals(void);
184static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200185#ifdef HAVE_SIGPROCMASK
186# define SIGSET_DECL(set) sigset_t set;
187# define BLOCK_SIGNALS(set) block_signals(set)
188# define UNBLOCK_SIGNALS(set) unblock_signals(set)
189#else
190# define SIGSET_DECL(set)
191# define BLOCK_SIGNALS(set) do { /**/ } while (0)
192# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
193#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100194static int have_wildcard(int, char_u **);
195static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100197static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000200# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#endif
202
Bram Moolenaar0f873732019-12-05 20:28:46 +0100203// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200204static volatile sig_atomic_t do_resize = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205static char_u *extra_shell_arg = NULL;
206static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100207// volatile because it is used in signal handler deathtrap().
208static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
209// volatile because it is used in signal handler deathtrap().
210static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100212#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
213static int dont_check_job_ended = 0;
214#endif
215
Bram Moolenaar26e86442020-05-17 14:06:16 +0200216// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
217static tmode_T mch_cur_tmode = TMODE_COOK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
219#ifdef USE_XSMP
220typedef struct
221{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100222 SmcConn smcconn; // The SM connection ID
223 IceConn iceconn; // The ICE connection ID
224 char *clientid; // The client ID for the current smc session
225 Bool save_yourself; // If we're in the middle of a save_yourself
226 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227} xsmp_config_T;
228
229static xsmp_config_T xsmp;
230#endif
231
232#ifdef SYS_SIGLIST_DECLARED
233/*
234 * I have seen
235 * extern char *_sys_siglist[NSIG];
236 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
237 * that describe the signals. That is nearly what we want here. But
238 * autoconf does only check for sys_siglist (without the underscore), I
239 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100240 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 */
242#endif
243
244static struct signalinfo
245{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100246 int sig; // Signal number, eg. SIGSEGV etc
247 char *name; // Signal name (not char_u!).
248 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249} signal_info[] =
250{
251#ifdef SIGHUP
252 {SIGHUP, "HUP", TRUE},
253#endif
254#ifdef SIGQUIT
255 {SIGQUIT, "QUIT", TRUE},
256#endif
257#ifdef SIGILL
258 {SIGILL, "ILL", TRUE},
259#endif
260#ifdef SIGTRAP
261 {SIGTRAP, "TRAP", TRUE},
262#endif
263#ifdef SIGABRT
264 {SIGABRT, "ABRT", TRUE},
265#endif
266#ifdef SIGEMT
267 {SIGEMT, "EMT", TRUE},
268#endif
269#ifdef SIGFPE
270 {SIGFPE, "FPE", TRUE},
271#endif
272#ifdef SIGBUS
273 {SIGBUS, "BUS", TRUE},
274#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100275#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100276 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {SIGSEGV, "SEGV", TRUE},
278#endif
279#ifdef SIGSYS
280 {SIGSYS, "SYS", TRUE},
281#endif
282#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100283 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284#endif
285#ifdef SIGTERM
286 {SIGTERM, "TERM", TRUE},
287#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100288#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 {SIGVTALRM, "VTALRM", TRUE},
290#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000291#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100292 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
293 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 {SIGPROF, "PROF", TRUE},
295#endif
296#ifdef SIGXCPU
297 {SIGXCPU, "XCPU", TRUE},
298#endif
299#ifdef SIGXFSZ
300 {SIGXFSZ, "XFSZ", TRUE},
301#endif
302#ifdef SIGUSR1
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200303 {SIGUSR1, "USR1", FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000305#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100306 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {SIGUSR2, "USR2", TRUE},
308#endif
309#ifdef SIGINT
310 {SIGINT, "INT", FALSE},
311#endif
312#ifdef SIGWINCH
313 {SIGWINCH, "WINCH", FALSE},
314#endif
315#ifdef SIGTSTP
316 {SIGTSTP, "TSTP", FALSE},
317#endif
318#ifdef SIGPIPE
319 {SIGPIPE, "PIPE", FALSE},
320#endif
321 {-1, "Unknown!", FALSE}
322};
323
Bram Moolenaar25724922009-07-14 15:38:41 +0000324 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100325mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000326{
327 if (p_verbose >= 5)
328 {
329 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100330 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000331 verbose_leave();
332 }
333# ifdef VMS
334 return chdir(vms_fixfilename(path));
335# else
336 return chdir(path);
337# endif
338}
339
Bram Moolenaar0f873732019-12-05 20:28:46 +0100340// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100341#if defined(ECHOE) && defined(ICANON) \
342 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
343 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200344# define NEW_TTY_SYSTEM
345#endif
346
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000347/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100348 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100351mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200353 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100354 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100355 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356}
357
358/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100359 * Function passed to inchar_loop() to handle window resizing.
360 * If "check_only" is TRUE: Return whether there was a resize.
361 * If "check_only" is FALSE: Deal with the window resized.
362 */
363 static int
364resize_func(int check_only)
365{
366 if (check_only)
367 return do_resize;
368 while (do_resize)
369 handle_resize();
370 return FALSE;
371}
372
373/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000374 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 * Get a characters from the keyboard.
376 * Return the number of characters that are available.
377 * If wtime == 0 do not wait for characters.
378 * If wtime == n wait a short time for characters.
379 * If wtime == -1 wait forever for characters.
380 */
381 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100382mch_inchar(
383 char_u *buf,
384 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100385 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100386 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100388 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
389 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390}
391
392 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100393handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394{
395 do_resize = FALSE;
396 shell_resized();
397}
398
399/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200400 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 */
402 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100403mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200405 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406}
407
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200408#if defined(FEAT_TERMINAL) || defined(PROTO)
409/*
410 * Check for any pending input or messages.
411 */
412 int
413mch_check_messages(void)
414{
415 return WaitForChar(0L, NULL, TRUE);
416}
417#endif
418
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
420# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000421# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422# endif
423# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
424# include <sys/sysctl.h>
425# endif
426# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
427# include <sys/sysinfo.h>
428# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100429# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100430# include <mach/mach_host.h>
431# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433
434/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000435 * Return total amount of memory available in Kbyte.
436 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100439mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100442 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
Bram Moolenaar362dc332018-03-05 21:59:37 +0100444# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100445 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100446 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100447 mach_port_t host = mach_host_self();
448 kern_return_t kret;
449# ifdef HOST_VM_INFO64
450 struct vm_statistics64 vm_stat;
451 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
Bram Moolenaar988615f2018-02-27 17:58:20 +0100453 kret = host_statistics64(host, HOST_VM_INFO64,
454 (host_info64_t)&vm_stat, &count);
455# else
456 struct vm_statistics vm_stat;
457 natural_t count = HOST_VM_INFO_COUNT;
458
459 kret = host_statistics(host, HOST_VM_INFO,
460 (host_info_t)&vm_stat, &count);
461# endif
462 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100463 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100464 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
465 + vm_stat.inactive_count
466# ifdef MAC_OS_X_VERSION_10_9
467 + vm_stat.compressor_page_count
468# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100469 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100470 mach_port_deallocate(mach_task_self(), host);
471 }
472# endif
473
474# ifdef HAVE_SYSCTL
475 if (mem == 0)
476 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100477 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100478 int mib[2];
479 size_t len = sizeof(long_u);
480# ifdef HW_USERMEM64
481 long_u physmem;
482# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100483 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100484 union {
485 int_u u32;
486 long_u u64;
487 } physmem;
488# endif
489
490 mib[0] = CTL_HW;
491# ifdef HW_USERMEM64
492 mib[1] = HW_USERMEM64;
493# else
494 mib[1] = HW_USERMEM;
495# endif
496 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
497 {
498# ifdef HW_USERMEM64
499 mem = (long_u)physmem;
500# else
501 if (len == sizeof(physmem.u64))
502 mem = (long_u)physmem.u64;
503 else
504 mem = (long_u)physmem.u32;
505# endif
506 }
507 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200510# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (mem == 0)
512 {
513 struct sysinfo sinfo;
514
Bram Moolenaar0f873732019-12-05 20:28:46 +0100515 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000517 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200518# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100519 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000520 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
521 {
522 sinfo.mem_unit = sinfo.mem_unit >> 1;
523 --shiftright;
524 }
525 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200526# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200528# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200531# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200533# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (mem == 0)
535 {
536 long pagesize, pagecount;
537
Bram Moolenaar0f873732019-12-05 20:28:46 +0100538 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 pagesize = sysconf(_SC_PAGESIZE);
540 pagecount = sysconf(_SC_PHYS_PAGES);
541 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000542 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100543 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000544 while (shiftright > 0 && (pagesize & 1) == 0)
545 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000546 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000547 --shiftright;
548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200552# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
Bram Moolenaar0f873732019-12-05 20:28:46 +0100554 // Return the minimum of the physical memory and the user limit, because
555 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200556# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 {
558 struct rlimit rlp;
559
560 if (getrlimit(RLIMIT_DATA, &rlp) == 0
561 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200562# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200564# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000565 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000567 {
568 mem = (long_u)rlp.rlim_cur;
569 shiftright = 10;
570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000575 return mem >> shiftright;
576 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577}
578#endif
579
Bram Moolenaar0981c872020-08-23 14:28:37 +0200580/*
581 * "flags": MCH_DELAY_IGNOREINPUT - don't read input
582 * MCH_DELAY_SETTMODE - use settmode() even for short delays
583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200585mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200587 tmode_T old_tmode;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200588 int call_settmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000589#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100590 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
Bram Moolenaar0981c872020-08-23 14:28:37 +0200593 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100595 // Go to cooked mode without echo, to allow SIGINT interrupting us
596 // here. But we don't want QUIT to kill us (CTRL-\ used in a
597 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200598 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000599 in_mch_delay = TRUE;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200600 call_settmode = mch_cur_tmode == TMODE_RAW
601 && (msec > 500 || (flags & MCH_DELAY_SETTMODE));
602 if (call_settmode)
603 {
604 old_tmode = mch_cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 settmode(TMODE_SLEEP);
Bram Moolenaar80361a52020-10-05 21:39:25 +0200606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608 /*
609 * Everybody sleeps in a different way...
610 * Prefer nanosleep(), some versions of usleep() can only sleep up to
611 * one second.
612 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000613#ifdef FEAT_MZSCHEME
614 do
615 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100616 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000617 if (total > p_mzq)
618 msec = p_mzq;
619 else
620 msec = total;
621 total -= msec;
622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623#ifdef HAVE_NANOSLEEP
624 {
625 struct timespec ts;
626
627 ts.tv_sec = msec / 1000;
628 ts.tv_nsec = (msec % 1000) * 1000000;
629 (void)nanosleep(&ts, NULL);
630 }
631#else
632# ifdef HAVE_USLEEP
633 while (msec >= 1000)
634 {
635 usleep((unsigned int)(999 * 1000));
636 msec -= 999;
637 }
638 usleep((unsigned int)(msec * 1000));
639# else
640# ifndef HAVE_SELECT
641 poll(NULL, 0, (int)msec);
642# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {
644 struct timeval tv;
645
646 tv.tv_sec = msec / 1000;
647 tv.tv_usec = (msec % 1000) * 1000;
Bram Moolenaar0981c872020-08-23 14:28:37 +0200648 // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
649 // a patch from Sun to fix this. Reported by Gunnar Pedersen.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 select(0, NULL, NULL, NULL, &tv);
651 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100652# endif // HAVE_SELECT
653# endif // HAVE_NANOSLEEP
654#endif // HAVE_USLEEP
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000655#ifdef FEAT_MZSCHEME
656 }
657 while (total > 0);
658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659
Bram Moolenaar80361a52020-10-05 21:39:25 +0200660 if (call_settmode)
Bram Moolenaar26e86442020-05-17 14:06:16 +0200661 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000662 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200665 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666}
667
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000668#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
670# define HAVE_CHECK_STACK_GROWTH
671/*
672 * Support for checking for an almost-out-of-stack-space situation.
673 */
674
675/*
676 * Return a pointer to an item on the stack. Used to find out if the stack
677 * grows up or down.
678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679static int stack_grows_downwards;
680
681/*
682 * Find out if the stack grows upwards or downwards.
683 * "p" points to a variable on the stack of the caller.
684 */
685 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100686check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
688 int i;
689
690 stack_grows_downwards = (p > (char *)&i);
691}
692#endif
693
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000694#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695static char *stack_limit = NULL;
696
697#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
698# include <pthread.h>
699# include <pthread_np.h>
700#endif
701
702/*
703 * Find out until how var the stack can grow without getting into trouble.
704 * Called when starting up and when switching to the signal stack in
705 * deathtrap().
706 */
707 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100708get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709{
710 struct rlimit rlp;
711 int i;
712 long lim;
713
Bram Moolenaar0f873732019-12-05 20:28:46 +0100714 // Set the stack limit to 15/16 of the allowable size. Skip this when the
715 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 if (getrlimit(RLIMIT_STACK, &rlp) == 0
717 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
718# ifdef RLIM_INFINITY
719 && rlp.rlim_cur != RLIM_INFINITY
720# endif
721 )
722 {
723 lim = (long)rlp.rlim_cur;
724#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
725 {
726 pthread_attr_t attr;
727 size_t size;
728
Bram Moolenaar0f873732019-12-05 20:28:46 +0100729 // On FreeBSD the initial thread always has a fixed stack size, no
730 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 pthread_attr_init(&attr);
732 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
733 {
734 pthread_attr_getstacksize(&attr, &size);
735 if (lim > (long)size)
736 lim = (long)size;
737 }
738 pthread_attr_destroy(&attr);
739 }
740#endif
741 if (stack_grows_downwards)
742 {
743 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
744 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100745 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 stack_limit = (char *)((long)&i / 16L);
747 }
748 else
749 {
750 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
751 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100752 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 }
754 }
755}
756
757/*
758 * Return FAIL when running out of stack space.
759 * "p" must point to any variable local to the caller that's on the stack.
760 */
761 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100762mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
764 if (stack_limit != NULL)
765 {
766 if (stack_grows_downwards)
767 {
768 if (p < stack_limit)
769 return FAIL;
770 }
771 else if (p > stack_limit)
772 return FAIL;
773 }
774 return OK;
775}
776#endif
777
778#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
779/*
780 * Support for using the signal stack.
781 * This helps when we run out of stack space, which causes a SIGSEGV. The
782 * signal handler then must run on another stack, since the normal stack is
783 * completely full.
784 */
785
786#ifndef SIGSTKSZ
Bram Moolenaar0f873732019-12-05 20:28:46 +0100787# define SIGSTKSZ 8000 // just a guess of how much stack is needed...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#endif
789
790# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100791static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100793static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794# endif
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796static char *signal_stack;
797
798 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100799init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 if (signal_stack != NULL)
802 {
803# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804# ifdef HAVE_SS_BASE
805 sigstk.ss_base = signal_stack;
806# else
807 sigstk.ss_sp = signal_stack;
808# endif
809 sigstk.ss_size = SIGSTKSZ;
810 sigstk.ss_flags = 0;
811 (void)sigaltstack(&sigstk, NULL);
812# else
813 sigstk.ss_sp = signal_stack;
814 if (stack_grows_downwards)
815 sigstk.ss_sp += SIGSTKSZ - 1;
816 sigstk.ss_onstack = 0;
817 (void)sigstack(&sigstk, NULL);
818# endif
819 }
820}
821#endif
822
823/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000824 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 * will barf when the second argument to signal() is ``wrong''.
826 * Let me try it with a few tricky defines from my own osdef.h (jw).
827 */
828#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 static RETSIGTYPE
830sig_winch SIGDEFARG(sigarg)
831{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100832 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
834 do_resize = TRUE;
835 SIGRETURN;
836}
837#endif
838
839#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 static RETSIGTYPE
841catch_sigint SIGDEFARG(sigarg)
842{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100843 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
845 got_int = TRUE;
846 SIGRETURN;
847}
848#endif
849
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200850#if defined(SIGUSR1)
851 static RETSIGTYPE
852catch_sigusr1 SIGDEFARG(sigarg)
853{
854 // this is not required on all systems, but it doesn't hurt anybody
855 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
856 got_sigusr1 = TRUE;
857 SIGRETURN;
858}
859#endif
860
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 static RETSIGTYPE
863catch_sigpwr SIGDEFARG(sigarg)
864{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100865 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000866 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 /*
868 * I'm not sure we get the SIGPWR signal when the system is really going
869 * down or when the batteries are almost empty. Just preserve the swap
870 * files and don't exit, that can't do any harm.
871 */
872 ml_sync_all(FALSE, FALSE);
873 SIGRETURN;
874}
875#endif
876
877#ifdef SET_SIG_ALARM
878/*
879 * signal function for alarm().
880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 static RETSIGTYPE
882sig_alarm SIGDEFARG(sigarg)
883{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100884 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 sig_alarm_called = TRUE;
886 SIGRETURN;
887}
888#endif
889
Bram Moolenaaredce7422019-01-20 18:39:30 +0100890#if (defined(HAVE_SETJMP_H) \
891 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
892 || defined(FEAT_LIBCALL))) \
893 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100894# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100895
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100896// argument to SETJMP()
897static JMP_BUF lc_jump_env;
898
899# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100900// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100901// Volatile because it is used in signal handlers.
902static volatile sig_atomic_t lc_signal;
903# endif
904
905// TRUE when lc_jump_env is valid.
906// Volatile because it is used in signal handler deathtrap().
907static volatile sig_atomic_t lc_active INIT(= FALSE);
908
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909/*
910 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100911 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100913 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 * Usage:
915 * mch_startjmp();
916 * if (SETJMP(lc_jump_env) != 0)
917 * {
918 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100919 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 * }
921 * else
922 * {
923 * do_the_work;
924 * mch_endjmp();
925 * }
926 * Note: Can't move SETJMP() here, because a function calling setjmp() must
927 * not return before the saved environment is used.
928 * Returns OK for normal return, FAIL when the protected code caused a
929 * problem and LONGJMP() was used.
930 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100931 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100932mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100934# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 lc_active = TRUE;
938}
939
Bram Moolenaar113e1072019-01-20 15:30:40 +0100940 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100941mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942{
943 lc_active = FALSE;
944}
945
Bram Moolenaar113e1072019-01-20 15:30:40 +0100946 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100947mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948{
949# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100950 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
951 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 init_signal_stack();
953# endif
954}
955#endif
956
957/*
958 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200959 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200961 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
962 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 */
964 static RETSIGTYPE
965deathtrap SIGDEFARG(sigarg)
966{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100967 static int entered = 0; // count the number of times we got here.
968 // Note: when memory has been corrupted
969 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970#ifdef SIGHASARG
971 int i;
972#endif
973
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100974#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 /*
976 * Catch a crash in protected code.
977 * Restores the environment saved in lc_jump_env, which looks like
978 * SETJMP() returns 1.
979 */
980 if (lc_active)
981 {
982# if defined(SIGHASARG)
983 lc_signal = sigarg;
984# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100985 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100987 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
989#endif
990
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000991#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000992# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100993 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
994 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
995 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000996 if (in_mch_delay && sigarg == SIGQUIT)
997 SIGRETURN;
998# endif
999
Bram Moolenaar0f873732019-12-05 20:28:46 +01001000 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1001 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1002 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001003 if (entered == 0
1004 && (0
1005# ifdef SIGHUP
1006 || sigarg == SIGHUP
1007# endif
1008# ifdef SIGQUIT
1009 || sigarg == SIGQUIT
1010# endif
1011# ifdef SIGTERM
1012 || sigarg == SIGTERM
1013# endif
1014# ifdef SIGPWR
1015 || sigarg == SIGPWR
1016# endif
1017# ifdef SIGUSR1
1018 || sigarg == SIGUSR1
1019# endif
1020# ifdef SIGUSR2
1021 || sigarg == SIGUSR2
1022# endif
1023 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001024 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001025 SIGRETURN;
1026#endif
1027
Bram Moolenaar0f873732019-12-05 20:28:46 +01001028 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 ++entered;
1030
Bram Moolenaar0f873732019-12-05 20:28:46 +01001031 // Executing autocommands is likely to use more stack space than we have
1032 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001033 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001036 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 set_vim_var_nr(VV_DYING, (long)entered);
1038#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001039 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001041#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001042 // Since we are now using the signal stack, need to reset the stack
1043 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 get_stack_limit();
1045#endif
1046
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001047#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001048 // This is for opening gdb the moment Vim crashes.
1049 // You need to manually adjust the file name and Vim executable name.
1050 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001051 {
1052# define VI_GDB_FILE "/tmp/vimgdb"
1053# define VIM_NAME "/usr/bin/vim"
1054 FILE *fp = fopen(VI_GDB_FILE, "w");
1055 if (fp)
1056 {
1057 fprintf(fp,
1058 "file %s\n"
1059 "attach %d\n"
1060 "set height 1000\n"
1061 "bt full\n"
1062 , VIM_NAME, getpid());
1063 fclose(fp);
1064 system("xterm -e gdb -x "VI_GDB_FILE);
1065 unlink(VI_GDB_FILE);
1066 }
1067 }
1068#endif
1069
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001071 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 for (i = 0; signal_info[i].sig != -1; i++)
1073 if (sigarg == signal_info[i].sig)
1074 break;
1075 deadly_signal = sigarg;
1076#endif
1077
Bram Moolenaar0f873732019-12-05 20:28:46 +01001078 full_screen = FALSE; // don't write message to the GUI, it might be
1079 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 /*
1081 * If something goes wrong after entering here, we may get here again.
1082 * When this happens, give a message and try to exit nicely (resetting the
1083 * terminal mode, etc.)
1084 * When this happens twice, just exit, don't even try to give a message,
1085 * stack may be corrupt or something weird.
1086 * When this still happens again (or memory was corrupted in such a way
1087 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1088 */
1089 if (entered >= 3)
1090 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001091 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 may_core_dump();
1093 if (entered >= 4)
1094 _exit(8);
1095 exit(7);
1096 }
1097 if (entered == 2)
1098 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001099 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001100 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 out_flush();
1102 getout(1);
1103 }
1104
Bram Moolenaar0f873732019-12-05 20:28:46 +01001105 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001107 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 signal_info[i].name);
1109#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001110 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001112
Bram Moolenaar0f873732019-12-05 20:28:46 +01001113 // Preserve files and exit. This sets the really_exiting flag to prevent
1114 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001115 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116
Bram Moolenaar0f873732019-12-05 20:28:46 +01001117 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001118
Bram Moolenaar009b2592004-10-24 19:18:58 +00001119#ifdef NBDEBUG
1120 reset_signals();
1121 may_core_dump();
1122 abort();
1123#endif
1124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 SIGRETURN;
1126}
1127
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001129 * Invoked after receiving SIGCONT. We don't know what happened while
1130 * sleeping, deal with part of that.
1131 */
1132 static void
1133after_sigcont(void)
1134{
1135# ifdef FEAT_TITLE
1136 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1137 // again later.
1138 oldtitle_outdated = TRUE;
1139# endif
1140 settmode(TMODE_RAW);
1141 need_check_timestamps = TRUE;
1142 did_check_timestamps = FALSE;
1143}
1144
1145#if defined(SIGCONT)
1146static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001147static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001148
1149/*
1150 * With multi-threading, suspending might not work immediately. Catch the
1151 * SIGCONT signal, which will be used as an indication whether the suspending
1152 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001153 *
1154 * On Linux, signal is not always handled immediately either.
1155 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001156 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001157 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001158 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001160static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001161static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162
1163/*
1164 * signal handler for SIGCONT
1165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 static RETSIGTYPE
1167sigcont_handler SIGDEFARG(sigarg)
1168{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001169 if (in_mch_suspend)
1170 {
1171 sigcont_received = TRUE;
1172 }
1173 else
1174 {
1175 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1176 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1177 // back to a sane mode. We should redraw, but we can't really do that
1178 // in a signal handler, do a redraw later.
1179 after_sigcont();
1180 redraw_later(CLEAR);
1181 cursor_on_force();
1182 out_flush();
1183 }
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 SIGRETURN;
1186}
1187#endif
1188
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001189#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001190# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001191static void *clip_star_save = NULL;
1192static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001193# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001194
1195/*
1196 * Called when Vim is going to sleep or execute a shell command.
1197 * We can't respond to requests for the X selections. Lose them, otherwise
1198 * other applications will hang. But first copy the text to cut buffer 0.
1199 */
1200 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001201loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001202{
1203 if (clip_star.owned || clip_plus.owned)
1204 {
1205 x11_export_final_selection();
1206 if (clip_star.owned)
1207 clip_lose_selection(&clip_star);
1208 if (clip_plus.owned)
1209 clip_lose_selection(&clip_plus);
1210 if (x11_display != NULL)
1211 XFlush(x11_display);
1212 }
1213}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001214
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001215# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001216/*
1217 * Save clipboard text to restore later.
1218 */
1219 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001220save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001221{
1222 if (clip_star.owned)
1223 clip_star_save = get_register('*', TRUE);
1224 if (clip_plus.owned)
1225 clip_plus_save = get_register('+', TRUE);
1226}
1227
1228/*
1229 * Restore clipboard text if no one own the X selection.
1230 */
1231 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001232restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001233{
1234 if (clip_star_save != NULL)
1235 {
1236 if (!clip_gen_owner_exists(&clip_star))
1237 put_register('*', clip_star_save);
1238 else
1239 free_register(clip_star_save);
1240 clip_star_save = NULL;
1241 }
1242 if (clip_plus_save != NULL)
1243 {
1244 if (!clip_gen_owner_exists(&clip_plus))
1245 put_register('+', clip_plus_save);
1246 else
1247 free_register(clip_plus_save);
1248 clip_plus_save = NULL;
1249 }
1250}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001251# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001252#endif
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254/*
1255 * If the machine has job control, use it to suspend the program,
1256 * otherwise fake it by starting a new shell.
1257 */
1258 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001259mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001261 if (ignore_sigtstp)
1262 return;
1263
Bram Moolenaar041c7102020-05-30 18:14:57 +02001264#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001265 in_mch_suspend = TRUE;
1266
Bram Moolenaar0f873732019-12-05 20:28:46 +01001267 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001269 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
1271# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001272 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001274# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 sigcont_received = FALSE;
1276# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001277
Bram Moolenaar0f873732019-12-05 20:28:46 +01001278 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001279
1280# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001281 /*
1282 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001283 * immediately, but somehow not all the time, probably because it's handled
1284 * in another thread. Do not call pause() because there would be race
1285 * condition which would hang Vim if signal happened in between the test of
1286 * sigcont_received and the call to pause(). If signal is not yet received,
1287 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1288 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001289 */
1290 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001291 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001292
Bram Moolenaar262735e2009-07-14 10:20:22 +00001293 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001294 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001297 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298
Bram Moolenaar2e310482018-08-21 13:09:10 +02001299 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#else
1301 suspend_shell();
1302#endif
1303}
1304
1305 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001306mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307{
1308 Columns = 80;
1309 Rows = 24;
1310
1311 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001312
1313#ifdef SIGTSTP
1314 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1315 // that indicates the shell (or program) that launched us does not support
1316 // tty job control and thus we should ignore that signal. If invoked as a
1317 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
1318 ignore_sigtstp = restricted || SIG_IGN == signal(SIGTSTP, SIG_ERR);
1319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001321
Bram Moolenaar56718732006-03-15 22:53:57 +00001322#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001323 mac_conv_init();
1324#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001325#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1326 win_clip_init();
1327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328}
1329
1330 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001331set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
1333#if defined(SIGWINCH)
1334 /*
1335 * WINDOW CHANGE signal is handled with sig_winch().
1336 */
1337 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1338#endif
1339
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001341 // See mch_init() for the conditions under which we ignore SIGTSTP.
1342 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001344#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 signal(SIGCONT, sigcont_handler);
1346#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001347#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 /*
1349 * We want to ignore breaking of PIPEs.
1350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 signal(SIGPIPE, SIG_IGN);
1352#endif
1353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001355 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356#endif
1357
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001358#ifdef SIGUSR1
1359 /*
1360 * Call user's handler on SIGUSR1
1361 */
1362 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
1363#endif
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 /*
1366 * Ignore alarm signals (Perl's alarm() generates it).
1367 */
1368#ifdef SIGALRM
1369 signal(SIGALRM, SIG_IGN);
1370#endif
1371
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001372#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 /*
1374 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1375 * work will be lost.
1376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1378#endif
1379
1380 /*
1381 * Arrange for other signals to gracefully shutdown Vim.
1382 */
1383 catch_signals(deathtrap, SIG_ERR);
1384
1385#if defined(FEAT_GUI) && defined(SIGHUP)
1386 /*
1387 * When the GUI is running, ignore the hangup signal.
1388 */
1389 if (gui.in_use)
1390 signal(SIGHUP, SIG_IGN);
1391#endif
1392}
1393
Bram Moolenaardf177f62005-02-22 08:39:57 +00001394#if defined(SIGINT) || defined(PROTO)
1395/*
1396 * Catch CTRL-C (only works while in Cooked mode).
1397 */
1398 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001399catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001400{
1401 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1402}
1403#endif
1404
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001406reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
1408 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001409#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001410 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 signal(SIGCONT, SIG_DFL);
1412#endif
1413}
1414
1415 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001416catch_signals(
1417 RETSIGTYPE (*func_deadly)(),
1418 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
1420 int i;
1421
1422 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (signal_info[i].deadly)
1425 {
1426#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1427 struct sigaction sa;
1428
Bram Moolenaar0f873732019-12-05 20:28:46 +01001429 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 sa.sa_handler = func_deadly;
1431 sigemptyset(&sa.sa_mask);
1432# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001433 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1434 // thread handling in combination with using the alternate stack:
1435 // pthread library functions try to use the stack pointer to
1436 // identify the current thread, causing a SEGV signal, which
1437 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 sa.sa_flags = 0;
1439# else
1440 sa.sa_flags = SA_ONSTACK;
1441# endif
1442 sigaction(signal_info[i].sig, &sa, NULL);
1443#else
1444# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1445 struct sigvec sv;
1446
Bram Moolenaar0f873732019-12-05 20:28:46 +01001447 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 sv.sv_handler = func_deadly;
1449 sv.sv_mask = 0;
1450 sv.sv_flags = SV_ONSTACK;
1451 sigvec(signal_info[i].sig, &sv, NULL);
1452# else
1453 signal(signal_info[i].sig, func_deadly);
1454# endif
1455#endif
1456 }
1457 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001458 {
1459 // Deal with non-deadly signals.
1460#ifdef SIGTSTP
1461 signal(signal_info[i].sig,
1462 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1463 ? SIG_IGN : func_other);
1464#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001466#endif
1467 }
1468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469}
1470
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001471#ifdef HAVE_SIGPROCMASK
1472 static void
1473block_signals(sigset_t *set)
1474{
1475 sigset_t newset;
1476 int i;
1477
1478 sigemptyset(&newset);
1479
1480 for (i = 0; signal_info[i].sig != -1; i++)
1481 sigaddset(&newset, signal_info[i].sig);
1482
Bram Moolenaar2e310482018-08-21 13:09:10 +02001483# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001484 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001485 sigaddset(&newset, SIGCONT);
1486# endif
1487
1488 sigprocmask(SIG_BLOCK, &newset, set);
1489}
1490
1491 static void
1492unblock_signals(sigset_t *set)
1493{
1494 sigprocmask(SIG_SETMASK, set, NULL);
1495}
1496#endif
1497
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001499 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001500 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1501 * return TRUE
1502 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1503 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001504 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001505 * Returns TRUE when Vim should exit.
1506 */
1507 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001508vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001509{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001510 static int got_signal = 0;
1511 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001512
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001513 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001514 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001515 case SIGNAL_BLOCK: blocked = TRUE;
1516 break;
1517
1518 case SIGNAL_UNBLOCK: blocked = FALSE;
1519 if (got_signal != 0)
1520 {
1521 kill(getpid(), got_signal);
1522 got_signal = 0;
1523 }
1524 break;
1525
1526 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001527 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001528 got_signal = sig;
1529#ifdef SIGPWR
1530 if (sig != SIGPWR)
1531#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001532 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001533 break;
1534 }
1535 return FALSE;
1536}
1537
1538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 * Check_win checks whether we have an interactive stdout.
1540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001542mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if (isatty(1))
1545 return OK;
1546 return FAIL;
1547}
1548
1549/*
1550 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1551 */
1552 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001553mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 if (isatty(read_cmd_fd))
1556 return TRUE;
1557 return FALSE;
1558}
1559
1560#ifdef FEAT_X11
1561
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001562# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1564
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565/*
1566 * Give a message about the elapsed time for opening the X window.
1567 */
1568 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001569xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001571 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572}
1573# endif
1574#endif
1575
1576#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1577/*
1578 * A few functions shared by X11 title and clipboard code.
1579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581static int got_x_error = FALSE;
1582
1583/*
1584 * X Error handler, otherwise X just exits! (very rude) -- webb
1585 */
1586 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001587x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001589 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 STRCAT(IObuff, _("\nVim: Got X error\n"));
1591
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001592 // In the GUI we cannot print a message and continue, because no X calls
1593 // are allowed here (causes my system to hang). Silently continuing seems
1594 // like the best alternative. Do preserve files, in case we crash.
1595 ml_sync_all(FALSE, FALSE);
1596
1597#ifdef FEAT_GUI
1598 if (!gui.in_use)
1599#endif
1600 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
Bram Moolenaar0f873732019-12-05 20:28:46 +01001602 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603}
1604
1605/*
1606 * Another X Error handler, just used to check for errors.
1607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001609x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610{
1611 got_x_error = TRUE;
1612 return 0;
1613}
1614
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001615/*
1616 * Return TRUE when connection to the X server is desired.
1617 */
1618 static int
1619x_connect_to_server(void)
1620{
1621 // No point in connecting if we are exiting or dying.
1622 if (exiting || v_dying)
1623 return FALSE;
1624
1625#if defined(FEAT_CLIENTSERVER)
1626 if (x_force_connect)
1627 return TRUE;
1628#endif
1629 if (x_no_connect)
1630 return FALSE;
1631
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001632 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001633 if (clip_exclude_prog != NULL)
1634 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001635 // Just in case we get called recursively, return FALSE. This could
1636 // happen if vpeekc() is used while executing the prog and it causes a
1637 // related callback to be invoked.
1638 if (regprog_in_use(clip_exclude_prog))
1639 return FALSE;
1640
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001641 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1642 return FALSE;
1643 }
1644 return TRUE;
1645}
1646
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001648# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649/*
1650 * An X IO Error handler, used to catch error while opening the display.
1651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001653x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001655 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001657# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001658 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001659# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660}
1661# endif
1662
1663/*
1664 * An X IO Error handler, used to catch terminal errors.
1665 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001666static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001669x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670{
1671 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001672 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 x11_window = 0;
1674 x11_display = NULL;
1675 xterm_Shell = (Widget)0;
1676
Bram Moolenaar0f873732019-12-05 20:28:46 +01001677 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001679# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001680 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001681# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001683
1684/*
1685 * If the X11 connection was lost try to restore it.
1686 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001687 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001688 */
1689 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001690may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001691{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001692 // No point in restoring the connecting if we are exiting or dying.
1693 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001694 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001695 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001696
1697# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001698 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001699 if (app_context != (XtAppContext)NULL)
1700 {
1701 XtDestroyApplicationContext(app_context);
1702 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001703 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001704 }
1705# endif
1706
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001707 setup_term_clip();
1708 get_x11_title(FALSE);
1709 }
1710}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001711
1712 void
1713ex_xrestore(exarg_T *eap)
1714{
1715 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1716 {
1717 if (xterm_display_allocated)
1718 vim_free(xterm_display);
1719 xterm_display = (char *)vim_strsave(eap->arg);
1720 xterm_display_allocated = TRUE;
1721 }
1722 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001723 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001724
1725 clear_xterm_clip();
1726 x11_window = 0;
1727 xterm_dpy_retry_count = 5; // Try reconnecting five times
1728 may_restore_clipboard();
1729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731
1732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 * Test if "dpy" and x11_window are valid by getting the window title.
1734 * I don't actually want it yet, so there may be a simpler call to use, but
1735 * this will cause the error handler x_error_check() to be called if anything
1736 * is wrong, such as the window pointer being invalid (as can happen when the
1737 * user changes his DISPLAY, but not his WINDOWID) -- webb
1738 */
1739 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001740test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
1742 int (*old_handler)();
1743 XTextProperty text_prop;
1744
1745 old_handler = XSetErrorHandler(x_error_check);
1746 got_x_error = FALSE;
1747 if (XGetWMName(dpy, x11_window, &text_prop))
1748 XFree((void *)text_prop.value);
1749 XSync(dpy, False);
1750 (void)XSetErrorHandler(old_handler);
1751
1752 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001753 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
1755 return (got_x_error ? FAIL : OK);
1756}
1757#endif
1758
1759#ifdef FEAT_TITLE
1760
1761#ifdef FEAT_X11
1762
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001763static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
1765/*
1766 * try to get x11 window and display
1767 *
1768 * return FAIL for failure, OK otherwise
1769 */
1770 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001771get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772{
1773 char *winid;
1774 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001775#define XD_NONE 0 // x11_display not set here
1776#define XD_HERE 1 // x11_display opened here
1777#define XD_GUI 2 // x11_display used from gui.dpy
1778#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 static int x11_display_from = XD_NONE;
1780 static int did_set_error_handler = FALSE;
1781
1782 if (!did_set_error_handler)
1783 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001784 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 (void)XSetErrorHandler(x_error_handler);
1786 did_set_error_handler = TRUE;
1787 }
1788
Bram Moolenaar9372a112005-12-06 19:59:18 +00001789#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 if (gui.in_use)
1791 {
1792 /*
1793 * If the X11 display was opened here before, for the window where Vim
1794 * was started, close that one now to avoid a memory leak.
1795 */
1796 if (x11_display_from == XD_HERE && x11_display != NULL)
1797 {
1798 XCloseDisplay(x11_display);
1799 x11_display_from = XD_NONE;
1800 }
1801 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1802 {
1803 x11_display_from = XD_GUI;
1804 return OK;
1805 }
1806 x11_display = NULL;
1807 return FAIL;
1808 }
1809 else if (x11_display_from == XD_GUI)
1810 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001811 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 x11_window = 0;
1813 x11_display = NULL;
1814 x11_display_from = XD_NONE;
1815 }
1816#endif
1817
Bram Moolenaar0f873732019-12-05 20:28:46 +01001818 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (!x_connect_to_server())
1820 return FAIL;
1821
1822 /*
1823 * If WINDOWID not set, should try another method to find out
1824 * what the current window number is. The only code I know for
1825 * this is very complicated.
1826 * We assume that zero is invalid for WINDOWID.
1827 */
1828 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1829 x11_window = (Window)atol(winid);
1830
1831#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001832 if (xterm_dpy == x11_display)
1833 // x11_display may have been set to xterm_dpy elsewhere
1834 x11_display_from = XD_XTERM;
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 if (xterm_dpy != NULL && x11_window != 0)
1837 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001838 // We may have checked it already, but Gnome terminal can move us to
1839 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001840 if (x11_display_from != XD_XTERM)
1841 {
1842 /*
1843 * If the X11 display was opened here before, for the window where
1844 * Vim was started, close that one now to avoid a memory leak.
1845 */
1846 if (x11_display_from == XD_HERE && x11_display != NULL)
1847 XCloseDisplay(x11_display);
1848 x11_display = xterm_dpy;
1849 x11_display_from = XD_XTERM;
1850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (test_x11_window(x11_display) == FAIL)
1852 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001853 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 x11_window = 0;
1855 x11_display = NULL;
1856 x11_display_from = XD_NONE;
1857 return FAIL;
1858 }
1859 return OK;
1860 }
1861#endif
1862
1863 if (x11_window == 0 || x11_display == NULL)
1864 result = -1;
1865
Bram Moolenaar0f873732019-12-05 20:28:46 +01001866 if (result != -1) // Have already been here and set this
1867 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
1869 if (x11_window != 0 && x11_display == NULL)
1870 {
1871#ifdef SET_SIG_ALARM
1872 RETSIGTYPE (*sig_save)();
1873#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001874#ifdef ELAPSED_FUNC
1875 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
1877 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001878 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879#endif
1880
1881#ifdef SET_SIG_ALARM
1882 /*
1883 * Opening the Display may hang if the DISPLAY setting is wrong, or
1884 * the network connection is bad. Set an alarm timer to get out.
1885 */
1886 sig_alarm_called = FALSE;
1887 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1888 (RETSIGTYPE (*)())sig_alarm);
1889 alarm(2);
1890#endif
1891 x11_display = XOpenDisplay(NULL);
1892
1893#ifdef SET_SIG_ALARM
1894 alarm(0);
1895 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1896 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001897 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898#endif
1899 if (x11_display != NULL)
1900 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001901# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001903 {
1904 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001905 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001906 verbose_leave();
1907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908# endif
1909 if (test_x11_window(x11_display) == FAIL)
1910 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001911 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 x11_window = 0;
1913 XCloseDisplay(x11_display);
1914 x11_display = NULL;
1915 }
1916 else
1917 x11_display_from = XD_HERE;
1918 }
1919 }
1920 if (x11_window == 0 || x11_display == NULL)
1921 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001922
1923# ifdef FEAT_EVAL
1924 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1925# endif
1926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 return (result = OK);
1928}
1929
1930/*
1931 * Determine original x11 Window Title
1932 */
1933 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001934get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001936 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937}
1938
1939/*
1940 * Determine original x11 Window icon
1941 */
1942 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001943get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944{
1945 int retval = FALSE;
1946
1947 retval = get_x11_thing(FALSE, test_only);
1948
Bram Moolenaar0f873732019-12-05 20:28:46 +01001949 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (oldicon == NULL && !test_only)
1951 {
1952 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001953 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001955 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957
1958 return retval;
1959}
1960
1961 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001962get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01001963 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01001964 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965{
1966 XTextProperty text_prop;
1967 int retval = FALSE;
1968 Status status;
1969
1970 if (get_x11_windis() == OK)
1971 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001972 // Get window/icon name if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 if (get_title)
1974 status = XGetWMName(x11_display, x11_window, &text_prop);
1975 else
1976 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1977
1978 /*
1979 * If terminal is xterm, then x11_window may be a child window of the
1980 * outer xterm window that actually contains the window/icon name, so
1981 * keep traversing up the tree until a window with a title/icon is
1982 * found.
1983 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001984 // Previously this was only done for xterm and alike. I don't see a
Bram Moolenaar0f873732019-12-05 20:28:46 +01001985 // reason why it would fail for other terminal emulators.
1986 // if (term_is_xterm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
1988 Window root;
1989 Window parent;
1990 Window win = x11_window;
1991 Window *children;
1992 unsigned int num_children;
1993
1994 while (!status || text_prop.value == NULL)
1995 {
1996 if (!XQueryTree(x11_display, win, &root, &parent, &children,
1997 &num_children))
1998 break;
1999 if (children)
2000 XFree((void *)children);
2001 if (parent == root || parent == 0)
2002 break;
2003
2004 win = parent;
2005 if (get_title)
2006 status = XGetWMName(x11_display, win, &text_prop);
2007 else
2008 status = XGetWMIconName(x11_display, win, &text_prop);
2009 }
2010 }
2011 if (status && text_prop.value != NULL)
2012 {
2013 retval = TRUE;
2014 if (!test_only)
2015 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002016 if (get_title)
2017 vim_free(oldtitle);
2018 else
2019 vim_free(oldicon);
Bram Moolenaara12a1612019-01-24 16:39:02 +01002020 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 if (get_title)
2023 oldtitle = vim_strsave((char_u *)text_prop.value);
2024 else
2025 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 }
2027 else
2028 {
2029 char **cl;
2030 Status transform_status;
2031 int n = 0;
2032
2033 transform_status = XmbTextPropertyToTextList(x11_display,
2034 &text_prop,
2035 &cl, &n);
2036 if (transform_status >= Success && n > 0 && cl[0])
2037 {
2038 if (get_title)
2039 oldtitle = vim_strsave((char_u *) cl[0]);
2040 else
2041 oldicon = vim_strsave((char_u *) cl[0]);
2042 XFreeStringList(cl);
2043 }
2044 else
2045 {
2046 if (get_title)
2047 oldtitle = vim_strsave((char_u *)text_prop.value);
2048 else
2049 oldicon = vim_strsave((char_u *)text_prop.value);
2050 }
2051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
2053 XFree((void *)text_prop.value);
2054 }
2055 }
2056 return retval;
2057}
2058
Bram Moolenaar0f873732019-12-05 20:28:46 +01002059// Xutf8 functions are not available on older systems. Note that on some
2060// systems X_HAVE_UTF8_STRING may be defined in a header file but
2061// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2062// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002063#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002064# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065# define USE_UTF8_STRING
2066# endif
2067#endif
2068
2069/*
2070 * Set x11 Window Title
2071 *
2072 * get_x11_windis() must be called before this and have returned OK
2073 */
2074 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002075set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002077 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2078 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2079 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080#ifdef USE_UTF8_STRING
2081 if (enc_utf8)
2082 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2083 NULL, NULL, 0, NULL, NULL, NULL);
2084 else
2085#endif
2086 {
2087#if XtSpecificationRelease >= 4
2088# ifdef FEAT_XFONTSET
2089 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2090 NULL, NULL, 0, NULL, NULL, NULL);
2091# else
2092 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002093 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094
Bram Moolenaar0f873732019-12-05 20:28:46 +01002095 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002096 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 XSetWMProperties(x11_display, x11_window, &text_prop,
2098 NULL, NULL, 0, NULL, NULL, NULL);
2099# endif
2100#else
2101 XStoreName(x11_display, x11_window, (char *)title);
2102#endif
2103 }
2104 XFlush(x11_display);
2105}
2106
2107/*
2108 * Set x11 Window icon
2109 *
2110 * get_x11_windis() must be called before this and have returned OK
2111 */
2112 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002113set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002115 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116#ifdef USE_UTF8_STRING
2117 if (enc_utf8)
2118 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2119 NULL, 0, NULL, NULL, NULL);
2120 else
2121#endif
2122 {
2123#if XtSpecificationRelease >= 4
2124# ifdef FEAT_XFONTSET
2125 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2126 NULL, 0, NULL, NULL, NULL);
2127# else
2128 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002129 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002131 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2133 NULL, 0, NULL, NULL, NULL);
2134# endif
2135#else
2136 XSetIconName(x11_display, x11_window, (char *)icon);
2137#endif
2138 }
2139 XFlush(x11_display);
2140}
2141
Bram Moolenaar0f873732019-12-05 20:28:46 +01002142#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002145get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146{
2147 return FALSE;
2148}
2149
2150 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002151get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152{
2153 if (!test_only)
2154 {
2155 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002156 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002158 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 }
2160 return FALSE;
2161}
2162
Bram Moolenaar0f873732019-12-05 20:28:46 +01002163#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002166mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167{
2168 return get_x11_title(TRUE);
2169}
2170
2171 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002172mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
2174 return get_x11_icon(TRUE);
2175}
2176
2177/*
2178 * Set the window title and icon.
2179 */
2180 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002181mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183 int type = 0;
2184 static int recursive = 0;
2185
Bram Moolenaar0f873732019-12-05 20:28:46 +01002186 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002188 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 return;
2190
Bram Moolenaar0f873732019-12-05 20:28:46 +01002191 // When one of the X11 functions causes a deadly signal, we get here again
2192 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 if (recursive)
2194 return;
2195 ++recursive;
2196
2197 /*
2198 * if the window ID and the display is known, we may use X11 calls
2199 */
2200#ifdef FEAT_X11
2201 if (get_x11_windis() == OK)
2202 type = 1;
2203#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002204# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002205 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 if (gui.in_use)
2207 type = 1;
2208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209#endif
2210
2211 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002212 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 * than x11 calls, because the x11 calls don't always work
2214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 if ((type || *T_TS != NUL) && title != NULL)
2216 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002217 if (oldtitle_outdated)
2218 {
2219 oldtitle_outdated = FALSE;
2220 VIM_CLEAR(oldtitle);
2221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 if (oldtitle == NULL
2223#ifdef FEAT_GUI
2224 && !gui.in_use
2225#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002226 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 (void)get_x11_title(FALSE);
2228
Bram Moolenaar0f873732019-12-05 20:28:46 +01002229 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 term_settitle(title);
2231#ifdef FEAT_X11
2232 else
2233# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002234 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002236 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002238#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002239 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 else
2241 gui_mch_settitle(title, icon);
2242#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002243 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
2245
2246 if ((type || *T_CIS != NUL) && icon != NULL)
2247 {
2248 if (oldicon == NULL
2249#ifdef FEAT_GUI
2250 && !gui.in_use
2251#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002252 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 get_x11_icon(FALSE);
2254
2255 if (*T_CIS != NUL)
2256 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002257 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002259 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 out_flush();
2261 }
2262#ifdef FEAT_X11
2263 else
2264# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002265 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002267 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#endif
2269 did_set_icon = TRUE;
2270 }
2271 --recursive;
2272}
2273
2274/*
2275 * Restore the window/icon title.
2276 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002277 * SAVE_RESTORE_TITLE only restore title
2278 * SAVE_RESTORE_ICON only restore icon
2279 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 */
2281 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002282mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283{
Bram Moolenaardac13472019-09-16 21:06:21 +02002284 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002285
Bram Moolenaar0f873732019-12-05 20:28:46 +01002286 // only restore the title or icon when it has been set
Bram Moolenaardac13472019-09-16 21:06:21 +02002287 mch_settitle(((which & SAVE_RESTORE_TITLE) && unix_did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002289 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2290
Bram Moolenaare5c83282019-05-03 23:15:37 +02002291 if (do_push_pop)
2292 {
2293 // pop and push from/to the stack
2294 term_pop_title(which);
2295 term_push_title(which);
2296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297}
2298
Bram Moolenaar0f873732019-12-05 20:28:46 +01002299#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300
2301/*
2302 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002303 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 */
2305 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002306vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
2308 if (name == NULL)
2309 return FALSE;
2310 return (STRNICMP(name, "xterm", 5) == 0
2311 || STRNICMP(name, "nxterm", 6) == 0
2312 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002313 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002315 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 || STRCMP(name, "builtin_xterm") == 0);
2317}
2318
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002319#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2320/*
2321 * Return TRUE if "name" appears to be that of a terminal
2322 * known to support the xterm-style mouse protocol.
2323 * Relies on term_is_xterm having been set to its correct value.
2324 */
2325 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002326use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002327{
2328 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002329 && (term_is_xterm
2330 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002331 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002332 || STRICMP(name, "st") == 0
2333 || STRNICMP(name, "st-", 3) == 0
2334 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002335}
2336#endif
2337
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338/*
2339 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2340 * Return 1 for "xterm".
2341 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002342 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002343 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 */
2345 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002346use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002348 if (ttym_flags == TTYM_SGR)
2349 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002350 if (ttym_flags == TTYM_URXVT)
2351 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (ttym_flags == TTYM_XTERM2)
2353 return 2;
2354 if (ttym_flags == TTYM_XTERM)
2355 return 1;
2356 return 0;
2357}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
2359 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002360vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361{
2362 if (name == NULL)
2363 return FALSE;
2364 return (STRNICMP(name, "iris-ansi", 9) == 0
2365 || STRCMP(name, "builtin_iris-ansi") == 0);
2366}
2367
2368 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002369vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370{
2371 if (name == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002372 return FALSE; // actually all ANSI comp. terminals should be here
2373 // catch VT100 - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002374 return ((STRNICMP(name, "vt", 2) == 0
2375 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 || STRCMP(name, "builtin_vt320") == 0);
2377}
2378
2379/*
2380 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2381 * This should include all windowed terminal emulators.
2382 */
2383 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002384vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385{
2386 if (name == NULL)
2387 return FALSE;
2388 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2389 return TRUE;
2390 return ( STRNICMP(name, "hpterm", 6) == 0
2391 || STRNICMP(name, "sun-cmd", 7) == 0
2392 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002393 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 || STRNICMP(name, "dtterm", 6) == 0);
2395}
2396
2397/*
2398 * Insert user name in s[len].
2399 * Return OK if a name found.
2400 */
2401 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002402mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403{
2404#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002405 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 return OK;
2407#else
2408 return mch_get_uname(getuid(), s, len);
2409#endif
2410}
2411
2412/*
2413 * Insert user name for "uid" in s[len].
2414 * Return OK if a name found.
2415 */
2416 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002417mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2420 struct passwd *pw;
2421
2422 if ((pw = getpwuid(uid)) != NULL
2423 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2424 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002425 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 return OK;
2427 }
2428#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002429 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2430 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431}
2432
2433/*
2434 * Insert host name is s[len].
2435 */
2436
2437#ifdef HAVE_SYS_UTSNAME_H
2438 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002439mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
2441 struct utsname vutsname;
2442
2443 if (uname(&vutsname) < 0)
2444 *s = NUL;
2445 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002446 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002448#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
2450# ifdef HAVE_SYS_SYSTEMINFO_H
2451# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2452# endif
2453
2454 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002455mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457# ifdef VAXC
2458 vaxc$gethostname((char *)s, len);
2459# else
2460 gethostname((char *)s, len);
2461# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002462 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002464#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
2466/*
2467 * return process ID
2468 */
2469 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002470mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472 return (long)getpid();
2473}
2474
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002475/*
2476 * return TRUE if process "pid" is still running
2477 */
2478 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002479mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002480{
2481 // EMX kill() not working correctly, it seems
2482 return kill(pid, 0) == 0;
2483}
2484
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002487strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
2489 extern int sys_nerr;
2490 extern char *sys_errlist[];
2491 static char er[20];
2492
2493 if (err > 0 && err < sys_nerr)
2494 return (sys_errlist[err]);
2495 sprintf(er, "Error %d", err);
2496 return er;
2497}
2498#endif
2499
2500/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002501 * Get name of current directory into buffer "buf" of length "len" bytes.
2502 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * Return OK for success, FAIL for failure.
2504 */
2505 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002506mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
2508#if defined(USE_GETCWD)
2509 if (getcwd((char *)buf, len) == NULL)
2510 {
2511 STRCPY(buf, strerror(errno));
2512 return FAIL;
2513 }
2514 return OK;
2515#else
2516 return (getwd((char *)buf) != NULL ? OK : FAIL);
2517#endif
2518}
2519
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002521 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 *
2523 * return FAIL for failure, OK for success
2524 */
2525 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002526mch_FullName(
2527 char_u *fname,
2528 char_u *buf,
2529 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002530 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
2532 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002533#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002535 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 char_u olddir[MAXPATHL];
2538 char_u *p;
2539 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002540#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002541 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2542 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002543#endif
2544
Bram Moolenaar38323e42007-03-06 19:22:53 +00002545#ifdef VMS
2546 fname = vms_fixfilename(fname);
2547#endif
2548
Bram Moolenaara2442432007-04-26 14:26:37 +00002549#ifdef __CYGWIN__
2550 /*
2551 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2552 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002553# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002554 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2555 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002556 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2557 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002558# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002559 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002560# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002561 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002562#endif
2563
Bram Moolenaar0f873732019-12-05 20:28:46 +01002564 // Expand it if forced or not an absolute path.
2565 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002566 if ((force || !mch_isFullName(fname))
2567 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 {
2569 /*
2570 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002571 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 * This will get the correct path name with "../" things.
2573 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002574 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002576#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 /*
2578 * Use fchdir() if possible, it's said to be faster and more
2579 * reliable. But on SunOS 4 it might not work. Check this by
2580 * doing a fchdir() right now.
2581 */
2582 if (!dont_fchdir)
2583 {
2584 fd = open(".", O_RDONLY | O_EXTRA, 0);
2585 if (fd >= 0 && fchdir(fd) < 0)
2586 {
2587 close(fd);
2588 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002589 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
2591 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
Bram Moolenaar0f873732019-12-05 20:28:46 +01002594 // Only change directory when we are sure we can return to where
2595 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002597#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 (mch_dirname(olddir, MAXPATHL) == FAIL
2601 || mch_chdir((char *)olddir) != 0))
2602 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002603 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 retval = FAIL;
2605 }
2606 else
2607 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002608 // The directory is copied into buf[], to be able to remove
2609 // the file name without changing it (could be a string in
2610 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 if (p - fname >= len)
2612 retval = FAIL;
2613 else
2614 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002615 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 if (mch_chdir((char *)buf))
2617 retval = FAIL;
2618 else
2619 fname = p + 1;
2620 *buf = NUL;
2621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
2623 }
2624 if (mch_dirname(buf, len) == FAIL)
2625 {
2626 retval = FAIL;
2627 *buf = NUL;
2628 }
2629 if (p != NULL)
2630 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002631#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 if (fd >= 0)
2633 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002634 if (p_verbose >= 5)
2635 {
2636 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002637 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002638 verbose_leave();
2639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 l = fchdir(fd);
2641 close(fd);
2642 }
2643 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 l = mch_chdir((char *)olddir);
2646 if (l != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002647 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649
2650 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002651 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002652 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002653#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002654 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002656 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002659
Bram Moolenaar0f873732019-12-05 20:28:46 +01002660 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002661 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 return FAIL;
2663
Bram Moolenaar0f873732019-12-05 20:28:46 +01002664 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (STRCMP(fname, ".") != 0)
2666 STRCAT(buf, fname);
2667
2668 return OK;
2669}
2670
2671/*
2672 * Return TRUE if "fname" does not depend on the current directory.
2673 */
2674 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002675mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002677#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 return ( fname[0] == '/' || fname[0] == '.' ||
2679 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2680 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2681 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002682#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684#endif
2685}
2686
Bram Moolenaar24552be2005-12-10 20:17:30 +00002687#if defined(USE_FNAME_CASE) || defined(PROTO)
2688/*
2689 * Set the case of the file name, if it already exists. This will cause the
2690 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002691 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002692 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002693 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002694fname_case(
2695 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002696 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002697{
2698 struct stat st;
2699 char_u *slash, *tail;
2700 DIR *dirp;
2701 struct dirent *dp;
2702
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002703 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002704 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002705 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002706 slash = vim_strrchr(name, '/');
2707 if (slash == NULL)
2708 {
2709 dirp = opendir(".");
2710 tail = name;
2711 }
2712 else
2713 {
2714 *slash = NUL;
2715 dirp = opendir((char *)name);
2716 *slash = '/';
2717 tail = slash + 1;
2718 }
2719
2720 if (dirp != NULL)
2721 {
2722 while ((dp = readdir(dirp)) != NULL)
2723 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002724 // Only accept names that differ in case and are the same byte
2725 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002726 if (STRICMP(tail, dp->d_name) == 0
2727 && STRLEN(tail) == STRLEN(dp->d_name))
2728 {
2729 char_u newname[MAXPATHL + 1];
2730 struct stat st2;
2731
Bram Moolenaar0f873732019-12-05 20:28:46 +01002732 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002733 vim_strncpy(newname, name, MAXPATHL);
2734 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2735 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002736 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002737 && st.st_ino == st2.st_ino
2738 && st.st_dev == st2.st_dev)
2739 {
2740 STRCPY(tail, dp->d_name);
2741 break;
2742 }
2743 }
2744 }
2745
2746 closedir(dirp);
2747 }
2748 }
2749}
2750#endif
2751
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752/*
2753 * Get file permissions for 'name'.
2754 * Returns -1 when it doesn't exist.
2755 */
2756 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002757mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758{
2759 struct stat statb;
2760
Bram Moolenaar0f873732019-12-05 20:28:46 +01002761 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762#ifdef VMS
2763 if (stat((char *)vms_fixfilename(name), &statb))
2764#else
2765 if (stat((char *)name, &statb))
2766#endif
2767 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002768#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002769 // The top bit makes the value negative, which means the file doesn't
2770 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002771 return statb.st_mode & ~S_ADDACE;
2772#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775}
2776
2777/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002778 * Set file permission for "name" to "perm".
2779 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 */
2781 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002782mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
2784 return (chmod((char *)
2785#ifdef VMS
2786 vms_fixfilename(name),
2787#else
2788 name,
2789#endif
2790 (mode_t)perm) == 0 ? OK : FAIL);
2791}
2792
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002793#if defined(HAVE_FCHMOD) || defined(PROTO)
2794/*
2795 * Set file permission for open file "fd" to "perm".
2796 * Return FAIL for failure, OK otherwise.
2797 */
2798 int
2799mch_fsetperm(int fd, long perm)
2800{
2801 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2802}
2803#endif
2804
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805#if defined(HAVE_ACL) || defined(PROTO)
2806# ifdef HAVE_SYS_ACL_H
2807# include <sys/acl.h>
2808# endif
2809# ifdef HAVE_SYS_ACCESS_H
2810# include <sys/access.h>
2811# endif
2812
2813# ifdef HAVE_SOLARIS_ACL
2814typedef struct vim_acl_solaris_T {
2815 int acl_cnt;
2816 aclent_t *acl_entry;
2817} vim_acl_solaris_T;
2818# endif
2819
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002820#if defined(HAVE_SELINUX) || defined(PROTO)
2821/*
2822 * Copy security info from "from_file" to "to_file".
2823 */
2824 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002825mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002826{
2827 if (from_file == NULL)
2828 return;
2829
2830 if (selinux_enabled == -1)
2831 selinux_enabled = is_selinux_enabled();
2832
2833 if (selinux_enabled > 0)
2834 {
Bram Moolenaar89560232020-10-09 23:04:47 +02002835 // Use "char *" instead of "security_context_t" to avoid a deprecation
2836 // warning.
2837 char *from_context = NULL;
2838 char *to_context = NULL;
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002839
2840 if (getfilecon((char *)from_file, &from_context) < 0)
2841 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002842 // If the filesystem doesn't support extended attributes,
2843 // the original had no special security context and the
2844 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002845 if (errno == EOPNOTSUPP)
2846 return;
2847
Bram Moolenaar32526b32019-01-19 17:43:09 +01002848 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002849 msg_outtrans(from_file);
2850 msg_putchar('\n');
2851 return;
2852 }
2853 if (getfilecon((char *)to_file, &to_context) < 0)
2854 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002855 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002856 msg_outtrans(to_file);
2857 msg_putchar('\n');
2858 freecon (from_context);
2859 return ;
2860 }
2861 if (strcmp(from_context, to_context) != 0)
2862 {
2863 if (setfilecon((char *)to_file, from_context) < 0)
2864 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002865 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002866 msg_outtrans(to_file);
2867 msg_putchar('\n');
2868 }
2869 }
2870 freecon(to_context);
2871 freecon(from_context);
2872 }
2873}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002874#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002875
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002876#if defined(HAVE_SMACK) && !defined(PROTO)
2877/*
2878 * Copy security info from "from_file" to "to_file".
2879 */
2880 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002881mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002882{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002883 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002884 {
2885 XATTR_NAME_SMACK,
2886 XATTR_NAME_SMACKEXEC,
2887 XATTR_NAME_SMACKMMAP
2888 };
2889
2890 char buffer[SMACK_LABEL_LEN];
2891 const char *name;
2892 int index;
2893 int ret;
2894 ssize_t size;
2895
2896 if (from_file == NULL)
2897 return;
2898
2899 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2900 / sizeof(smack_copied_attributes)[0]) ; index++)
2901 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002902 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002903 name = smack_copied_attributes[index];
2904
Bram Moolenaar0f873732019-12-05 20:28:46 +01002905 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002906 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2907 if (size >= 0)
2908 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002909 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002910 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2911 if (ret < 0)
2912 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002913 vim_snprintf((char *)IObuff, IOSIZE,
2914 _("Could not set security context %s for %s"),
2915 name, to_file);
2916 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002917 msg_putchar('\n');
2918 }
2919 }
2920 else
2921 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002922 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002923 switch (errno)
2924 {
2925 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002926 // extended attributes aren't supported or enabled
2927 // should a message be echoed? not sure...
2928 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002929
2930 case ERANGE:
2931 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002932 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002933 vim_snprintf((char *)IObuff, IOSIZE,
2934 _("Could not get security context %s for %s. Removing it!"),
2935 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002936 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002937 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01002938 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002939
2940 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002941 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002942 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002943 // Silently ignore errors, apparently this happens when
2944 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002945 break;
2946 }
2947 }
2948 }
2949}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002950#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952/*
2953 * Return a pointer to the ACL of file "fname" in allocated memory.
2954 * Return NULL if the ACL is not available for whatever reason.
2955 */
2956 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002957mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958{
2959 vim_acl_T ret = NULL;
2960#ifdef HAVE_POSIX_ACL
2961 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2962#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002963#ifdef HAVE_SOLARIS_ZFS_ACL
2964 acl_t *aclent;
2965
2966 if (acl_get((char *)fname, 0, &aclent) < 0)
2967 return NULL;
2968 ret = (vim_acl_T)aclent;
2969#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970#ifdef HAVE_SOLARIS_ACL
2971 vim_acl_solaris_T *aclent;
2972
2973 aclent = malloc(sizeof(vim_acl_solaris_T));
2974 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2975 {
2976 free(aclent);
2977 return NULL;
2978 }
2979 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2980 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2981 {
2982 free(aclent->acl_entry);
2983 free(aclent);
2984 return NULL;
2985 }
2986 ret = (vim_acl_T)aclent;
2987#else
2988#if defined(HAVE_AIX_ACL)
2989 int aclsize;
2990 struct acl *aclent;
2991
2992 aclsize = sizeof(struct acl);
2993 aclent = malloc(aclsize);
2994 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2995 {
2996 if (errno == ENOSPC)
2997 {
2998 aclsize = aclent->acl_len;
2999 aclent = realloc(aclent, aclsize);
3000 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3001 {
3002 free(aclent);
3003 return NULL;
3004 }
3005 }
3006 else
3007 {
3008 free(aclent);
3009 return NULL;
3010 }
3011 }
3012 ret = (vim_acl_T)aclent;
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 return ret;
3018}
3019
3020/*
3021 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3022 */
3023 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003024mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
3026 if (aclent == NULL)
3027 return;
3028#ifdef HAVE_POSIX_ACL
3029 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3030#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003031#ifdef HAVE_SOLARIS_ZFS_ACL
3032 acl_set((char *)fname, (acl_t *)aclent);
3033#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034#ifdef HAVE_SOLARIS_ACL
3035 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3036 ((vim_acl_solaris_T *)aclent)->acl_entry);
3037#else
3038#ifdef HAVE_AIX_ACL
3039 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003040#endif // HAVE_AIX_ACL
3041#endif // HAVE_SOLARIS_ACL
3042#endif // HAVE_SOLARIS_ZFS_ACL
3043#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044}
3045
3046 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003047mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
3049 if (aclent == NULL)
3050 return;
3051#ifdef HAVE_POSIX_ACL
3052 acl_free((acl_t)aclent);
3053#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003054#ifdef HAVE_SOLARIS_ZFS_ACL
3055 acl_free((acl_t *)aclent);
3056#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057#ifdef HAVE_SOLARIS_ACL
3058 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3059 free(aclent);
3060#else
3061#ifdef HAVE_AIX_ACL
3062 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003063#endif // HAVE_AIX_ACL
3064#endif // HAVE_SOLARIS_ACL
3065#endif // HAVE_SOLARIS_ZFS_ACL
3066#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067}
3068#endif
3069
3070/*
3071 * Set hidden flag for "name".
3072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003074mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003076 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077}
3078
3079/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003080 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 * return FALSE if "name" is not a directory
3082 * return FALSE for error
3083 */
3084 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003085mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
3087 struct stat statb;
3088
Bram Moolenaar0f873732019-12-05 20:28:46 +01003089 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 return FALSE;
3091 if (stat((char *)name, &statb))
3092 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094}
3095
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003096/*
3097 * return TRUE if "name" is a directory, NOT a symlink to a directory
3098 * return FALSE if "name" is not a directory
3099 * return FALSE for error
3100 */
3101 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003102mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003103{
3104 struct stat statb;
3105
Bram Moolenaar0f873732019-12-05 20:28:46 +01003106 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003107 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003108 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003109 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003110 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003111}
3112
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113/*
3114 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3115 */
3116 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003117executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
3119 struct stat st;
3120
3121 if (stat((char *)name, &st))
3122 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003123#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003124 // Like on Unix system file can have executable rights but not necessarily
3125 // be an executable, but on Unix is not a default for an ordinary file to
3126 // have an executable flag - on VMS it is in most cases.
3127 // Therefore, this check does not have any sense - let keep us to the
3128 // conventions instead:
3129 // *.COM and *.EXE files are the executables - the rest are not. This is
3130 // not ideal but better then it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003131 int vms_executable = 0;
3132 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3133 {
3134 if (strstr(vms_tolower((char*)name),".exe") != NULL
3135 || strstr(vms_tolower((char*)name),".com")!= NULL)
3136 vms_executable = 1;
3137 }
3138 return vms_executable;
3139#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142}
3143
3144/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003145 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003146 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 * Return -1 if unknown.
3148 */
3149 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003150mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
3152 char_u *buf;
3153 char_u *p, *e;
3154 int retval;
3155
Bram Moolenaar0f873732019-12-05 20:28:46 +01003156 // When "use_path" is false and if it's an absolute or relative path don't
3157 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003158 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003159 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003160 // There must be a path separator, files in the current directory
3161 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003162 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003163 {
3164 if (path != NULL)
3165 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003166 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003167 *path = FullName_save(name, TRUE);
3168 else
3169 *path = vim_strsave(name);
3170 }
3171 return TRUE;
3172 }
3173 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 p = (char_u *)getenv("PATH");
3177 if (p == NULL || *p == NUL)
3178 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003179 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 if (buf == NULL)
3181 return -1;
3182
3183 /*
3184 * Walk through all entries in $PATH to check if "name" exists there and
3185 * is an executable file.
3186 */
3187 for (;;)
3188 {
3189 e = (char_u *)strchr((char *)p, ':');
3190 if (e == NULL)
3191 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003192 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 STRCPY(buf, "./");
3194 else
3195 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003196 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 add_pathsep(buf);
3198 }
3199 STRCAT(buf, name);
3200 retval = executable_file(buf);
3201 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003202 {
3203 if (path != NULL)
3204 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003205 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003206 *path = FullName_save(buf, TRUE);
3207 else
3208 *path = vim_strsave(buf);
3209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
3213 if (*e != ':')
3214 break;
3215 p = e + 1;
3216 }
3217
3218 vim_free(buf);
3219 return retval;
3220}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
3222/*
3223 * Check what "name" is:
3224 * NODE_NORMAL: file or directory (or doesn't exist)
3225 * NODE_WRITABLE: writable device, socket, fifo, etc.
3226 * NODE_OTHER: non-writable things
3227 */
3228 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003229mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
3231 struct stat st;
3232
3233 if (stat((char *)name, &st))
3234 return NODE_NORMAL;
3235 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3236 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003237 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003239 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 return NODE_WRITABLE;
3241}
3242
3243 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003244mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
3246#ifdef HAVE_CHECK_STACK_GROWTH
3247 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 check_stack_growth((char *)&i);
3250
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003251# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 get_stack_limit();
3253# endif
3254
3255#endif
3256
3257 /*
3258 * Setup an alternative stack for signals. Helps to catch signals when
3259 * running out of stack space.
3260 * Use of sigaltstack() is preferred, it's more portable.
3261 * Ignore any errors.
3262 */
3263#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003264 signal_stack = alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 init_signal_stack();
3266#endif
3267}
3268
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003269#if defined(EXITFREE) || defined(PROTO)
3270 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003271mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003272{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003273# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3274 if (clip_star.owned)
3275 clip_lose_selection(&clip_star);
3276 if (clip_plus.owned)
3277 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003278# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003279# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003280 if (xterm_Shell != (Widget)0)
3281 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003282# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003283 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003284 if (xterm_dpy != NULL)
3285 XtCloseDisplay(xterm_dpy);
3286 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003287 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003288 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003289# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003290 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003291# endif
3292 }
3293# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003294# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003295# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003296 if (x11_display != NULL
3297# ifdef FEAT_XCLIPBOARD
3298 && x11_display != xterm_dpy
3299# endif
3300 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003301 XCloseDisplay(x11_display);
3302# endif
3303# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003304 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003305# endif
3306# ifdef FEAT_TITLE
3307 vim_free(oldtitle);
3308 vim_free(oldicon);
3309# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003310}
3311#endif
3312
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313/*
3314 * Output a newline when exiting.
3315 * Make sure the newline goes to the same stream as the text.
3316 */
3317 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003318exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003320 if (silent_mode)
3321 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 if (newline_on_exit || msg_didout)
3323 {
3324 if (msg_use_printf())
3325 {
3326 if (info_message)
3327 mch_msg("\n");
3328 else
3329 mch_errmsg("\r\n");
3330 }
3331 else
3332 out_char('\n');
3333 }
3334 else
3335 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003336 restore_cterm_colors(); // get original colors back
3337 msg_clr_eos_force(); // clear the rest of the display
3338 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 }
3340}
3341
Bram Moolenaarb4151682020-05-11 22:13:28 +02003342#ifdef USE_GCOV_FLUSH
3343extern void __gcov_flush();
3344#endif
3345
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003347mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
3349 exiting = TRUE;
3350
3351#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3352 x11_export_final_selection();
3353#endif
3354
3355#ifdef FEAT_GUI
3356 if (!gui.in_use)
3357#endif
3358 {
3359 settmode(TMODE_COOK);
3360#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003361 // restore xterm title and icon name
3362 mch_restore_title(SAVE_RESTORE_BOTH);
3363 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364#endif
3365 /*
3366 * When t_ti is not empty but it doesn't cause swapping terminal
3367 * pages, need to output a newline when msg_didout is set. But when
3368 * t_ti does swap pages it should not go to the shell page. Do this
3369 * before stoptermcap().
3370 */
3371 if (swapping_screen() && !newline_on_exit)
3372 exit_scroll();
3373
Bram Moolenaar0f873732019-12-05 20:28:46 +01003374 // Stop termcap: May need to check for T_CRV response, which
3375 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 stoptermcap();
3377
3378 /*
3379 * A newline is only required after a message in the alternate screen.
3380 * This is set to TRUE by wait_return().
3381 */
3382 if (!swapping_screen() || newline_on_exit)
3383 exit_scroll();
3384
Bram Moolenaar0f873732019-12-05 20:28:46 +01003385 // Cursor may have been switched off without calling starttermcap()
3386 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 if (full_screen)
3388 cursor_on();
3389 }
3390 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003391 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003392
3393#ifdef USE_GCOV_FLUSH
3394 // Flush coverage info before possibly being killed by a deadly signal.
3395 __gcov_flush();
3396#endif
3397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 may_core_dump();
3399#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 gui_exit(r);
3402#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003403
Bram Moolenaar56718732006-03-15 22:53:57 +00003404#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003405 mac_conv_cleanup();
3406#endif
3407
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003409 // A core dump won't be created if the signal handler
3410 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 if (deadly_signal != 0)
3412 return;
3413#endif
3414
Bram Moolenaar009b2592004-10-24 19:18:58 +00003415#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003416 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003417#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003418
3419#ifdef EXITFREE
3420 free_all_mem();
3421#endif
3422
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 exit(r);
3424}
3425
3426 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003427may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
3429 if (deadly_signal != 0)
3430 {
3431 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003432 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
3434}
3435
3436#ifndef VMS
3437
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003438/*
3439 * Get the file descriptor to use for tty operations.
3440 */
3441 static int
3442get_tty_fd(int fd)
3443{
3444 int tty_fd = fd;
3445
3446#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3447 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3448 // "fd" when it is a master device.
3449 if (mch_isatty(fd) > 1)
3450 {
3451 char *name;
3452
3453 name = ptsname(fd);
3454 if (name == NULL)
3455 return -1;
3456
3457 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3458 if (tty_fd < 0)
3459 return -1;
3460 }
3461#endif
3462 return tty_fd;
3463}
3464
3465 static int
3466mch_tcgetattr(int fd, void *term)
3467{
3468 int tty_fd;
3469 int retval = -1;
3470
3471 tty_fd = get_tty_fd(fd);
3472 if (tty_fd >= 0)
3473 {
3474#ifdef NEW_TTY_SYSTEM
3475# ifdef HAVE_TERMIOS_H
3476 retval = tcgetattr(tty_fd, (struct termios *)term);
3477# else
3478 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3479# endif
3480#else
3481 // for "old" tty systems
3482 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3483#endif
3484 if (tty_fd != fd)
3485 close(tty_fd);
3486 }
3487 return retval;
3488}
3489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003491mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492{
3493 static int first = TRUE;
3494
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003495#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496# ifdef HAVE_TERMIOS_H
3497 static struct termios told;
3498 struct termios tnew;
3499# else
3500 static struct termio told;
3501 struct termio tnew;
3502# endif
3503
3504 if (first)
3505 {
3506 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003507 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
3509
3510 tnew = told;
3511 if (tmode == TMODE_RAW)
3512 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003513 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003514 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3515 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003517# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003518 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519# endif
3520 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003521# ifdef ONLCR
3522 // Don't map NL -> CR NL, we do it ourselves.
3523 // Also disable expanding tabs if possible.
3524# ifdef XTABS
3525 tnew.c_oflag &= ~(ONLCR | XTABS);
3526# else
3527# ifdef TAB3
3528 tnew.c_oflag &= ~(ONLCR | TAB3);
3529# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003531# endif
3532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003534 tnew.c_cc[VMIN] = 1; // return after 1 char
3535 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
3537 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003538 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003539 // Also reset ICANON here, otherwise on Solaris select() won't see
3540 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003541 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003542 tnew.c_cc[VMIN] = 1; // return after 1 char
3543 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
3546# if defined(HAVE_TERMIOS_H)
3547 {
3548 int n = 10;
3549
Bram Moolenaar0f873732019-12-05 20:28:46 +01003550 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3551 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3553 && errno == EINTR && n > 0)
3554 --n;
3555 }
3556# else
3557 ioctl(read_cmd_fd, TCSETA, &tnew);
3558# endif
3559
3560#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 /*
3562 * for "old" tty systems
3563 */
3564# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003565# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566# endif
3567 static struct sgttyb ttybold;
3568 struct sgttyb ttybnew;
3569
3570 if (first)
3571 {
3572 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003573 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575
3576 ttybnew = ttybold;
3577 if (tmode == TMODE_RAW)
3578 {
3579 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3580 ttybnew.sg_flags |= RAW;
3581 }
3582 else if (tmode == TMODE_SLEEP)
3583 ttybnew.sg_flags &= ~(ECHO);
3584 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3585#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003586 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587}
3588
3589/*
3590 * Try to get the code for "t_kb" from the stty setting
3591 *
3592 * Even if termcap claims a backspace key, the user's setting *should*
3593 * prevail. stty knows more about reality than termcap does, and if
3594 * somebody's usual erase key is DEL (which, for most BSD users, it will
3595 * be), they're going to get really annoyed if their erase key starts
3596 * doing forward deletes for no reason. (Eric Fischer)
3597 */
3598 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003599get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003601 ttyinfo_T info;
3602 char_u buf[2];
3603 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003605 if (get_tty_info(read_cmd_fd, &info) == OK)
3606 {
3607 intr_char = info.interrupt;
3608 buf[0] = info.backspace;
3609 buf[1] = NUL;
3610 add_termcode((char_u *)"kb", buf, FALSE);
3611
Bram Moolenaar0f873732019-12-05 20:28:46 +01003612 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003613 p = find_termcode((char_u *)"kD");
3614 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3615 do_fixdel(NULL);
3616 }
3617}
3618
3619/*
3620 * Obtain the characters that Backspace and Enter produce on "fd".
3621 * Returns OK or FAIL.
3622 */
3623 int
3624get_tty_info(int fd, ttyinfo_T *info)
3625{
3626#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627# ifdef HAVE_TERMIOS_H
3628 struct termios keys;
3629# else
3630 struct termio keys;
3631# endif
3632
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003633 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003635 info->backspace = keys.c_cc[VERASE];
3636 info->interrupt = keys.c_cc[VINTR];
3637 if (keys.c_iflag & ICRNL)
3638 info->enter = NL;
3639 else
3640 info->enter = CAR;
3641 if (keys.c_oflag & ONLCR)
3642 info->nl_does_cr = TRUE;
3643 else
3644 info->nl_does_cr = FALSE;
3645 return OK;
3646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003648 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 struct sgttyb keys;
3650
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003651 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003653 info->backspace = keys.sg_erase;
3654 info->interrupt = keys.sg_kill;
3655 info->enter = CAR;
3656 info->nl_does_cr = TRUE;
3657 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003660 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661}
3662
Bram Moolenaar0f873732019-12-05 20:28:46 +01003663#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003665static int mouse_ison = FALSE;
3666
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667/*
3668 * Set mouse clicks on or off.
3669 */
3670 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003671mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003673#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003674 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 int xterm_mouse_vers;
3677
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003678#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003679 if (!on)
3680 // Make sure not tracing mouse movements. Important when a button-down
3681 // was received but no release yet.
3682 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003683#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003684
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003685 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003686#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003687 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003688#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003689 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003690 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 return;
3692
3693 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003694
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003695#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003696 if (ttym_flags == TTYM_URXVT)
3697 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003698 out_str_nf((char_u *)
3699 (on
3700 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3701 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003702 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003703 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003704#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003705
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003706 if (ttym_flags == TTYM_SGR)
3707 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003708 // SGR mode supports columns above 223
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003709 out_str_nf((char_u *)
3710 (on
3711 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3712 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003713 mouse_ison = on;
3714 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003715
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003716#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003717 if (bevalterm_ison != (p_bevalterm && on))
3718 {
3719 bevalterm_ison = (p_bevalterm && on);
3720 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003721 // disable mouse movement events, enabling is below
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003722 out_str_nf((char_u *)
3723 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003724 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003725#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003726
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 if (xterm_mouse_vers > 0)
3728 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003729 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 out_str_nf((char_u *)
3731 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003732 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003733#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003734 bevalterm_ison
3735 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003736#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003737 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003739 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 out_str_nf((char_u *)
3741 (xterm_mouse_vers > 1
3742 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3743 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003744 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
3746
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003747#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 else if (ttym_flags == TTYM_DEC)
3749 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003750 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003752 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003754 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003758#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 else
3760 {
3761 if (on)
3762 {
3763 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003764 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766 else
3767 {
3768 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003769 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
3771 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003774#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003775 else
3776 {
3777 if (on)
3778 {
3779 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003780 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003781 }
3782 else
3783 {
3784 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003785 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003786 }
3787 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003788#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003789
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003790#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 else
3792 {
3793 if (on)
3794 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003795 // D - Enable Mouse up/down messages
3796 // L - Enable Left Button Reporting
3797 // M - Enable Middle Button Reporting
3798 // R - Enable Right Button Reporting
3799 // K - Enable SHIFT and CTRL key Reporting
3800 // + - Enable Advanced messaging of mouse moves and up/down messages
3801 // Q - Quiet No Ack
3802 // # - Numeric value of mouse pointer required
3803 // 0 = Multiview 2000 cursor, used as standard
3804 // 1 = Windows Arrow
3805 // 2 = Windows I Beam
3806 // 3 = Windows Hour Glass
3807 // 4 = Windows Cross Hair
3808 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003809# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003810 // Disables full feedback of pointer movements
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3812 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003813# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3815 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003816# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003817 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819 else
3820 {
3821 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3822 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003823 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
3825 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003826#endif
3827#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 else
3829 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003830 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 if (on)
3832 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3833 else
3834 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003835 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838}
3839
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003840#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003841/*
3842 * Called when 'balloonevalterm' changed.
3843 */
3844 void
3845mch_bevalterm_changed(void)
3846{
3847 mch_setmouse(mouse_ison);
3848}
3849#endif
3850
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851/*
3852 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3853 */
3854 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003855check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856{
3857# ifdef FEAT_MOUSE_XTERM
3858 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003859# ifdef FEAT_MOUSE_URXVT
3860 && use_xterm_mouse() != 3
3861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862# ifdef FEAT_GUI
3863 && !gui.in_use
3864# endif
3865 )
3866 {
3867 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003868 ? IF_EB("\233M", CSI_STR "M")
3869 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 if (*p_mouse != NUL)
3871 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003872 // force mouse off and maybe on to send possibly new mouse
3873 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 mch_setmouse(FALSE);
3875 setmouse();
3876 }
3877 }
3878 else
3879 del_mouse_termcode(KS_MOUSE);
3880# endif
3881
3882# ifdef FEAT_MOUSE_GPM
3883 if (!use_xterm_mouse()
3884# ifdef FEAT_GUI
3885 && !gui.in_use
3886# endif
3887 )
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003888 set_mouse_termcode(KS_GPM_MOUSE,
3889 (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3890 else
3891 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892# endif
3893
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003894# ifdef FEAT_SYSMOUSE
3895 if (!use_xterm_mouse()
3896# ifdef FEAT_GUI
3897 && !gui.in_use
3898# endif
3899 )
3900 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3901# endif
3902
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003904 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 if (!use_xterm_mouse()
3906# ifdef FEAT_GUI
3907 && !gui.in_use
3908# endif
3909 )
3910 set_mouse_termcode(KS_JSBTERM_MOUSE,
3911 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3912 else
3913 del_mouse_termcode(KS_JSBTERM_MOUSE);
3914# endif
3915
3916# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003917 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3918 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 if (!use_xterm_mouse()
3920# ifdef FEAT_GUI
3921 && !gui.in_use
3922# endif
3923 )
3924 set_mouse_termcode(KS_NETTERM_MOUSE,
3925 (char_u *)IF_EB("\033}", ESC_STR "}"));
3926 else
3927 del_mouse_termcode(KS_NETTERM_MOUSE);
3928# endif
3929
3930# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003931 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003932 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933# ifdef FEAT_GUI
3934 && !gui.in_use
3935# endif
3936 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003937 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3938 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 else
3940 del_mouse_termcode(KS_DEC_MOUSE);
3941# endif
3942# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01003943 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003944 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945# ifdef FEAT_GUI
3946 && !gui.in_use
3947# endif
3948 )
3949 set_mouse_termcode(KS_PTERM_MOUSE,
3950 (char_u *) IF_EB("\033[", ESC_STR "["));
3951 else
3952 del_mouse_termcode(KS_PTERM_MOUSE);
3953# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003954# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003955 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003956# ifdef FEAT_GUI
3957 && !gui.in_use
3958# endif
3959 )
3960 {
3961 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003962 ? IF_EB("\233*M", CSI_STR "*M")
3963 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003964
3965 if (*p_mouse != NUL)
3966 {
3967 mch_setmouse(FALSE);
3968 setmouse();
3969 }
3970 }
3971 else
3972 del_mouse_termcode(KS_URXVT_MOUSE);
3973# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003974 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003975# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003976 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003977# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003978 )
3979 {
3980 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003981 ? IF_EB("\233<*M", CSI_STR "<*M")
3982 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3983
3984 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3985 ? IF_EB("\233<*m", CSI_STR "<*m")
3986 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003987
3988 if (*p_mouse != NUL)
3989 {
3990 mch_setmouse(FALSE);
3991 setmouse();
3992 }
3993 }
3994 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003995 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003996 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003997 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001#ifndef VMS
4002
4003/*
4004 * Try to get the current window size:
4005 * 1. with an ioctl(), most accurate method
4006 * 2. from the environment variables LINES and COLUMNS
4007 * 3. from the termcap
4008 * 4. keep using the old values
4009 * Return OK when size could be determined, FAIL otherwise.
4010 */
4011 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004012mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013{
4014 long rows = 0;
4015 long columns = 0;
4016 char_u *p;
4017
4018 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 * 1. try using an ioctl. It is the most accurate method.
4020 *
4021 * Try using TIOCGWINSZ first, some systems that have it also define
4022 * TIOCGSIZE but don't have a struct ttysize.
4023 */
4024# ifdef TIOCGWINSZ
4025 {
4026 struct winsize ws;
4027 int fd = 1;
4028
Bram Moolenaar0f873732019-12-05 20:28:46 +01004029 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 if (!isatty(fd) && isatty(read_cmd_fd))
4031 fd = read_cmd_fd;
4032 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4033 {
4034 columns = ws.ws_col;
4035 rows = ws.ws_row;
4036 }
4037 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004038# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039# ifdef TIOCGSIZE
4040 {
4041 struct ttysize ts;
4042 int fd = 1;
4043
Bram Moolenaar0f873732019-12-05 20:28:46 +01004044 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 if (!isatty(fd) && isatty(read_cmd_fd))
4046 fd = read_cmd_fd;
4047 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4048 {
4049 columns = ts.ts_cols;
4050 rows = ts.ts_lines;
4051 }
4052 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004053# endif // TIOCGSIZE
4054# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
4056 /*
4057 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004058 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4059 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004061 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
4063 if ((p = (char_u *)getenv("LINES")))
4064 rows = atoi((char *)p);
4065 if ((p = (char_u *)getenv("COLUMNS")))
4066 columns = atoi((char *)p);
4067 }
4068
4069#ifdef HAVE_TGETENT
4070 /*
4071 * 3. try reading "co" and "li" entries from termcap
4072 */
4073 if (columns == 0 || rows == 0)
4074 getlinecol(&columns, &rows);
4075#endif
4076
4077 /*
4078 * 4. If everything fails, use the old values
4079 */
4080 if (columns <= 0 || rows <= 0)
4081 return FAIL;
4082
4083 Rows = rows;
4084 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004085 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 return OK;
4087}
4088
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004089#if defined(FEAT_TERMINAL) || defined(PROTO)
4090/*
4091 * Report the windows size "rows" and "cols" to tty "fd".
4092 */
4093 int
4094mch_report_winsize(int fd, int rows, int cols)
4095{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004096 int tty_fd;
4097 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004098
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004099 tty_fd = get_tty_fd(fd);
4100 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004101 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004102# if defined(TIOCSWINSZ)
4103 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004104
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004105 ws.ws_col = cols;
4106 ws.ws_row = rows;
4107 ws.ws_xpixel = cols * 5;
4108 ws.ws_ypixel = rows * 10;
4109 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4110 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4111 retval == 0 ? "success" : "failed");
4112# elif defined(TIOCSSIZE)
4113 struct ttysize ts;
4114
4115 ts.ts_cols = cols;
4116 ts.ts_lines = rows;
4117 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4118 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4119 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004120# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004121 if (tty_fd != fd)
4122 close(tty_fd);
4123 }
4124 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004125}
4126#endif
4127
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128/*
4129 * Try to set the window size to Rows and Columns.
4130 */
4131 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004132mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133{
4134 if (*T_CWS)
4135 {
4136 /*
4137 * NOTE: if you get an error here that term_set_winsize() is
4138 * undefined, check the output of configure. It could probably not
4139 * find a ncurses, termcap or termlib library.
4140 */
4141 term_set_winsize((int)Rows, (int)Columns);
4142 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004143 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
4145}
4146
Bram Moolenaar0f873732019-12-05 20:28:46 +01004147#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149/*
4150 * Rows and/or Columns has changed.
4151 */
4152 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004153mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004155 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156}
4157
Bram Moolenaar205b8862011-09-07 15:04:31 +02004158/*
4159 * Wait for process "child" to end.
4160 * Return "child" if it exited properly, <= 0 on error.
4161 */
4162 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004163wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004164{
4165 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004166 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004167
4168 while (wait_pid != child)
4169 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004170 // When compiled with Python threads are probably used, in which case
4171 // wait() sometimes hangs for no obvious reason. Use waitpid()
4172 // instead and loop (like the GUI). Also needed for other interfaces,
4173 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004174# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004175 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004176# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004177 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004178# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004179 if (wait_pid == 0)
4180 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004181 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004182 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004183 if (++delay_msec > 10)
4184 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004185 continue;
4186 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004187 if (wait_pid <= 0
4188# ifdef ECHILD
4189 && errno == ECHILD
4190# endif
4191 )
4192 break;
4193 }
4194 return wait_pid;
4195}
4196
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004197#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004198/*
4199 * Set the environment for a child process.
4200 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004201 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004202set_child_environment(
4203 long rows,
4204 long columns,
4205 char *term,
4206 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004207{
4208# ifdef HAVE_SETENV
4209 char envbuf[50];
4210# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004211 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004212 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004213 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004214 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004215 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004216# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004217 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004218# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004219# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004220 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004221# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004222# endif
4223
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004224# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004225 setenv("TERM", term, 1);
4226 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004227 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004228 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004229 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004230 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004231 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004232 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004233 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004234# ifdef FEAT_TERMINAL
4235 if (is_terminal)
4236 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004237 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004238 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4239 }
4240# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004241# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004242 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004243# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004244# else
4245 /*
4246 * Putenv does not copy the string, it has to remain valid.
4247 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004248 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004249 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004250 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4251 putenv(envbuf_Term);
4252 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004253 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004254 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4255 putenv(envbuf_Lines);
4256 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4257 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004258 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004259 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004260 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004261# ifdef FEAT_TERMINAL
4262 if (is_terminal)
4263 {
4264 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004265 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004266 putenv(envbuf_Version);
4267 }
4268# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004269# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004270 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4271 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4272 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004273# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004274# endif
4275}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004276
4277 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004278set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004279{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004280 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004281}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004282#endif
4283
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004284#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004285/*
4286 * Open a PTY, with FD for the master and slave side.
4287 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004288 * When successful both file descriptors are stored and the allocated pty name
4289 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004290 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004291 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004292open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004293{
4294 char *tty_name;
4295
Bram Moolenaar59386482019-02-10 22:43:46 +01004296 if (name1 != NULL)
4297 *name1 = NULL;
4298 if (name2 != NULL)
4299 *name2 = NULL;
4300
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004301 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004302 if (*pty_master_fd >= 0)
4303 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004304 // Leaving out O_NOCTTY may lead to waitpid() always returning
4305 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4306 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004307#ifdef O_NOCTTY
4308 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4309#else
4310 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4311#endif
4312 if (*pty_slave_fd < 0)
4313 {
4314 close(*pty_master_fd);
4315 *pty_master_fd = -1;
4316 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004317 else
4318 {
4319 if (name1 != NULL)
4320 *name1 = vim_strsave((char_u *)tty_name);
4321 if (name2 != NULL)
4322 *name2 = vim_strsave((char_u *)tty_name);
4323 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004324 }
4325}
4326#endif
4327
Bram Moolenaarfae42832017-08-01 22:24:26 +02004328/*
4329 * Send SIGINT to a child process if "c" is an interrupt character.
4330 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004331 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004332may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4333{
4334# ifdef SIGINT
4335 if (c == Ctrl_C || c == intr_char)
4336 {
4337# ifdef HAVE_SETSID
4338 kill(-pid, SIGINT);
4339# else
4340 kill(0, SIGINT);
4341# endif
4342 if (wpid > 0)
4343 kill(wpid, SIGINT);
4344 }
4345# endif
4346}
4347
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004348#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004349
Bram Moolenaar0f873732019-12-05 20:28:46 +01004350/*
4351 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4352 * pointers, the last one is NULL.
4353 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4354 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004355 int
4356unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004357 char_u *cmd,
4358 char ***argvp,
4359 char_u **sh_tofree,
4360 char_u **shcf_tofree)
4361{
4362 char **argv = NULL;
4363 int argc;
4364
4365 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004366 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004367 return FAIL;
4368
4369 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4370 return FAIL;
4371 *argvp = argv;
4372
4373 if (cmd != NULL)
4374 {
4375 char_u *s;
4376 char_u *p;
4377
4378 if (extra_shell_arg != NULL)
4379 argv[argc++] = (char *)extra_shell_arg;
4380
Bram Moolenaar0f873732019-12-05 20:28:46 +01004381 // Break 'shellcmdflag' into white separated parts. This doesn't
4382 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004383 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004384 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004385 return FAIL;
4386 s = *shcf_tofree;
4387 p = p_shcf;
4388 while (*p != NUL)
4389 {
4390 argv[argc++] = (char *)s;
4391 while (*p && *p != ' ' && *p != TAB)
4392 *s++ = *p++;
4393 *s++ = NUL;
4394 p = skipwhite(p);
4395 }
4396
4397 argv[argc++] = (char *)cmd;
4398 }
4399 argv[argc] = NULL;
4400 return OK;
4401}
4402#endif
4403
4404#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4405/*
4406 * Use a terminal window to run a shell command in.
4407 */
4408 static int
4409mch_call_shell_terminal(
4410 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004411 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004412{
4413 jobopt_T opt;
4414 char **argv = NULL;
4415 char_u *tofree1 = NULL;
4416 char_u *tofree2 = NULL;
4417 int retval = -1;
4418 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004419 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004420 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004421 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004422
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004423 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004424 goto theend;
4425
4426 init_job_options(&opt);
4427 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4428 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004429 if (buf == NULL)
4430 goto theend;
4431
4432 job = term_getjob(buf->b_term);
4433 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004434
Bram Moolenaar0f873732019-12-05 20:28:46 +01004435 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004436 aucmd_prepbuf(&aco, buf);
4437
4438 clear_oparg(&oa);
4439 while (term_use_loop())
4440 {
4441 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4442 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004443 // If terminal_loop() returns OK we got a key that is handled
4444 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004445 if (terminal_loop(TRUE) == OK)
4446 normal_cmd(&oa, TRUE);
4447 }
4448 else
4449 normal_cmd(&oa, TRUE);
4450 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004451 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004452 ch_log(NULL, "system command finished");
4453
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004454 job_unref(job);
4455
Bram Moolenaar0f873732019-12-05 20:28:46 +01004456 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004457 aucmd_restbuf(&aco);
4458
4459 wait_return(TRUE);
4460 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4461
4462theend:
4463 vim_free(argv);
4464 vim_free(tofree1);
4465 vim_free(tofree2);
4466 return retval;
4467}
4468#endif
4469
4470#ifdef USE_SYSTEM
4471/*
4472 * Use system() to start the shell: simple but slow.
4473 */
4474 static int
4475mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004476 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004477 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478{
4479#ifdef VMS
4480 char *ifn = NULL;
4481 char *ofn = NULL;
4482#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004483 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004484 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004485 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
4487 out_flush();
4488
4489 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004490 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
Bram Moolenaar62b42182010-09-21 22:09:37 +02004492# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004493 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004494 loose_clipboard();
4495# endif
4496
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 if (cmd == NULL)
4498 x = system((char *)p_sh);
4499 else
4500 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004501# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 if (ofn = strchr((char *)cmd, '>'))
4503 *ofn++ = '\0';
4504 if (ifn = strchr((char *)cmd, '<'))
4505 {
4506 char *p;
4507
4508 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004509 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 if (p)
4511 *p = '\0';
4512 }
4513 if (ofn)
4514 x = vms_sys((char *)cmd, ofn, ifn);
4515 else
4516 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004517# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004518 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004520 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 if (newcmd == NULL)
4522 x = 0;
4523 else
4524 {
4525 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4526 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4527 (char *)p_shcf,
4528 (char *)cmd);
4529 x = system((char *)newcmd);
4530 vim_free(newcmd);
4531 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534# ifdef VMS
4535 x = vms_sys_status(x);
4536# endif
4537 if (emsg_silent)
4538 ;
4539 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004540 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 else if (x && !(options & SHELL_SILENT))
4542 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004543 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 msg_outnum((long)x);
4545 msg_putchar('\n');
4546 }
4547
4548 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004549 {
4550 // The shell may have messed with the mode, always set it.
4551 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004552 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554# ifdef FEAT_TITLE
4555 resettitle();
4556# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004557# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4558 restore_clipboard();
4559# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004561}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
Bram Moolenaar0f873732019-12-05 20:28:46 +01004563#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
Bram Moolenaar0f873732019-12-05 20:28:46 +01004565# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4566 // 127, some shells use that already
4567# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
Bram Moolenaar13568252018-03-16 20:46:58 +01004569/*
4570 * Don't use system(), use fork()/exec().
4571 */
4572 static int
4573mch_call_shell_fork(
4574 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004575 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004576{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004577 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004579 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 pid_t wait_pid = 0;
4581# ifdef HAVE_UNION_WAIT
4582 union wait status;
4583# else
4584 int status = -1;
4585# endif
4586 int retval = -1;
4587 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004588 char_u *tofree1 = NULL;
4589 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004591 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004592# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004594# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004595 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 int fd_fromshell[2];
4597 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004598 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599
4600 out_flush();
4601 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004602 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004603 if (tmode == TMODE_RAW)
4604 // The shell may have messed with the mode, always set it later.
4605 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004607 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004608 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004609
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004611 * For the GUI, when writing the output into the buffer and when reading
4612 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4613 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004615 if ((options & (SHELL_READ|SHELL_WRITE))
4616# ifdef FEAT_GUI
4617 || (gui.in_use && show_shell_mess)
4618# endif
4619 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004621# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 /*
4623 * Try to open a master pty.
4624 * If this works, open the slave pty.
4625 * If the slave can't be opened, close the master pty.
4626 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004627 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004628 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 /*
4630 * If not opening a pty or it didn't work, try using pipes.
4631 */
4632 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
4635 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004636 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
4638 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004639 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
4641 close(fd_toshell[0]);
4642 close(fd_toshell[1]);
4643 }
4644 }
4645 if (pipe_error)
4646 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004647 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 out_flush();
4649 }
4650 }
4651 }
4652
Bram Moolenaar0f873732019-12-05 20:28:46 +01004653 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004655 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004656 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004657 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004658 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004660 UNBLOCK_SIGNALS(&curset);
4661
Bram Moolenaar32526b32019-01-19 17:43:09 +01004662 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004663 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004665 || (gui.in_use && show_shell_mess)
4666# endif
4667 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004669# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004670 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 {
4672 close(pty_master_fd);
4673 close(pty_slave_fd);
4674 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004675 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
4678 close(fd_toshell[0]);
4679 close(fd_toshell[1]);
4680 close(fd_fromshell[0]);
4681 close(fd_fromshell[1]);
4682 }
4683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004685 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004687 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004688 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
Bram Moolenaar819524702018-02-27 19:10:00 +01004690# ifdef FEAT_JOB_CHANNEL
4691 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004692 {
4693 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004694 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004695 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004696# endif
4697
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 if (!show_shell_mess || (options & SHELL_EXPAND))
4699 {
4700 int fd;
4701
4702 /*
4703 * Don't want to show any message from the shell. Can't just
4704 * close stdout and stderr though, because some systems will
4705 * break if you try to write to them after that, so we must
4706 * use dup() to replace them with something else -- webb
4707 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4708 * waiting for input.
4709 */
4710 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4711 fclose(stdin);
4712 fclose(stdout);
4713 fclose(stderr);
4714
4715 /*
4716 * If any of these open()'s and dup()'s fail, we just continue
4717 * anyway. It's not fatal, and on most systems it will make
4718 * no difference at all. On a few it will cause the execvp()
4719 * to exit with a non-zero status even when the completion
4720 * could be done, which is nothing too serious. If the open()
4721 * or dup() failed we'd just do the same thing ourselves
4722 * anyway -- webb
4723 */
4724 if (fd >= 0)
4725 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004726 vim_ignored = dup(fd); // To replace stdin (fd 0)
4727 vim_ignored = dup(fd); // To replace stdout (fd 1)
4728 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
Bram Moolenaar0f873732019-12-05 20:28:46 +01004730 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 close(fd);
4732 }
4733 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004734 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004736 || gui.in_use
4737# endif
4738 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
4740
Bram Moolenaardf177f62005-02-22 08:39:57 +00004741# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004742 // Create our own process group, so that the child and all its
4743 // children can be kill()ed. Don't do this when using pipes,
4744 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004745 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004746 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004747 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004748# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004749 // When doing "!xterm&" and 'shell' is bash: the shell
4750 // will exit and send SIGHUP to all processes in its
4751 // group, killing the just started process. Ignore SIGHUP
4752 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004753 signal(SIGHUP, SIG_IGN);
4754# endif
4755 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004756# endif
4757# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004758 if (pty_slave_fd >= 0)
4759 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004760 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004761 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004762 setup_slavepty(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01004764 // Try to become controlling tty (probably doesn't work,
4765 // unless run by root)
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004766 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004768 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004769# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004770 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771
Bram Moolenaara5792f52005-11-23 21:25:05 +00004772 /*
4773 * stderr is only redirected when using the GUI, so that a
4774 * program like gpg can still access the terminal to get a
4775 * passphrase using stderr.
4776 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004777# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 if (pty_master_fd >= 0)
4779 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004780 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
Bram Moolenaar0f873732019-12-05 20:28:46 +01004782 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004784 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004786 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004787 if (gui.in_use)
4788 {
4789 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004790 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792
Bram Moolenaar0f873732019-12-05 20:28:46 +01004793 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
4795 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004798 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 close(fd_toshell[1]);
4800 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004801 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 close(fd_toshell[0]);
4803
Bram Moolenaar0f873732019-12-05 20:28:46 +01004804 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 close(fd_fromshell[0]);
4806 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004807 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 close(fd_fromshell[1]);
4809
Bram Moolenaara5792f52005-11-23 21:25:05 +00004810# ifdef FEAT_GUI
4811 if (gui.in_use)
4812 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004813 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004814 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004815 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004816 }
4817# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
4819 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004820
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 /*
4822 * There is no type cast for the argv, because the type may be
4823 * different on different machines. This may cause a warning
4824 * message with strict compilers, don't worry about it.
4825 * Call _exit() instead of exit() to avoid closing the connection
4826 * to the X server (esp. with GTK, which uses atexit()).
4827 */
4828 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004829 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004831 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 {
4833 /*
4834 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004835 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 */
4837 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004838 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004839 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004840# ifdef FEAT_JOB_CHANNEL
4841 ++dont_check_job_ended;
4842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 /*
4844 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004845 * This is also used to pipe stdin/stdout to/from the external
4846 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004848 if ((options & (SHELL_READ|SHELL_WRITE))
4849# ifdef FEAT_GUI
4850 || (gui.in_use && show_shell_mess)
4851# endif
4852 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004854# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004856 int buffer_off = 0; // valid bytes in buffer[]
4857 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4858 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 int len;
4860 int p_more_save;
4861 int old_State;
4862 int c;
4863 int toshell_fd;
4864 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004865 garray_T ga;
4866 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004867# ifdef ELAPSED_FUNC
4868 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004869# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
Bram Moolenaardf177f62005-02-22 08:39:57 +00004871# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 if (pty_master_fd >= 0)
4873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 fromshell_fd = pty_master_fd;
4875 toshell_fd = dup(pty_master_fd);
4876 }
4877 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004878# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
4880 close(fd_toshell[0]);
4881 close(fd_fromshell[1]);
4882 toshell_fd = fd_toshell[1];
4883 fromshell_fd = fd_fromshell[0];
4884 }
4885
4886 /*
4887 * Write to the child if there are typed characters.
4888 * Read from the child if there are characters available.
4889 * Repeat the reading a few times if more characters are
4890 * available. Need to check for typed keys now and then, but
4891 * not too often (delays when no chars are available).
4892 * This loop is quit if no characters can be read from the pty
4893 * (WaitForChar detected special condition), or there are no
4894 * characters available and the child has exited.
4895 * Only check if the child has exited when there is no more
4896 * output. The child may exit before all the output has
4897 * been printed.
4898 *
4899 * Currently this busy loops!
4900 * This can probably dead-lock when the write blocks!
4901 */
4902 p_more_save = p_more;
4903 p_more = FALSE;
4904 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004905 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004907 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004908 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004909 // Fork a process that will write the lines to the
4910 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004911 if ((wpid = fork()) == -1)
4912 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004913 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004914 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004915 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004916 {
4917 linenr_T lnum = curbuf->b_op_start.lnum;
4918 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004919 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004920 size_t l;
4921
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004922 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004923 for (;;)
4924 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004925 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004926 if (l == 0)
4927 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004928 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004929 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004930 len = write(toshell_fd, "", (size_t)1);
4931 else
4932 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004933 char_u *s = vim_strchr(lp + written, NL);
4934
Bram Moolenaar89d40322006-08-29 15:30:07 +00004935 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004936 s == NULL ? l
4937 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004938 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004939 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004940 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004941 // Finished a line, add a NL, unless this line
4942 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004943 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004944 || (!curbuf->b_p_bin
4945 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004946 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004947 && (lnum !=
4948 curbuf->b_ml.ml_line_count
4949 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004950 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004951 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004952 ++lnum;
4953 if (lnum > curbuf->b_op_end.lnum)
4954 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004955 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00004956 close(toshell_fd);
4957 toshell_fd = -1;
4958 break;
4959 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004960 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004961 written = 0;
4962 }
4963 else if (len > 0)
4964 written += len;
4965 }
4966 _exit(0);
4967 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004968 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00004969 {
4970 close(toshell_fd);
4971 toshell_fd = -1;
4972 }
4973 }
4974
4975 if (options & SHELL_READ)
4976 ga_init2(&ga, 1, BUFLEN);
4977
4978 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004979# ifdef ELAPSED_FUNC
4980 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004981# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 for (;;)
4983 {
4984 /*
4985 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004986 * if there are any.
4987 * Don't do this if we are expanding wild cards (would eat
4988 * typeahead).
4989 * Don't do this when filtering and terminal is in cooked
4990 * mode, the shell command will handle the I/O. Avoids
4991 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004992 * Don't get characters when the child has already
4993 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004994 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004995 * while (noread_cnt > 4), avoids that ":r !ls" eats
4996 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 */
4998 len = 0;
4999 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005000 && ((options &
5001 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5002 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005003# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005004 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005005# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005006 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005007 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005008 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005010 if (ta_len == 0)
5011 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005012 // Get extra characters when we don't have any.
5013 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005014 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005015# ifdef ELAPSED_FUNC
5016 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005017# endif
5018 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5019 }
5020 if (ta_len > 0 || len > 0)
5021 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 /*
5023 * For pipes:
5024 * Check for CTRL-C: send interrupt signal to child.
5025 * Check for CTRL-D: EOF, close pipe to child.
5026 */
5027 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 /*
5030 * Send SIGINT to the child's group or all
5031 * processes in our group.
5032 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005033 may_send_sigint(ta_buf[ta_len], pid, wpid);
5034
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 if (pty_master_fd < 0 && toshell_fd >= 0
5036 && ta_buf[ta_len] == Ctrl_D)
5037 {
5038 close(toshell_fd);
5039 toshell_fd = -1;
5040 }
5041 }
5042
Bram Moolenaarf4140482020-02-15 23:06:45 +01005043 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044
5045 /*
5046 * For pipes: echo the typed characters.
5047 * For a pty this does not seem to work.
5048 */
5049 if (pty_master_fd < 0)
5050 {
5051 for (i = ta_len; i < ta_len + len; ++i)
5052 {
5053 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5054 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 else if (has_mbyte)
5056 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005057 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058
5059 msg_outtrans_len(ta_buf + i, l);
5060 i += l - 1;
5061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 else
5063 msg_outtrans_len(ta_buf + i, 1);
5064 }
5065 windgoto(msg_row, msg_col);
5066 out_flush();
5067 }
5068
5069 ta_len += len;
5070
5071 /*
5072 * Write the characters to the child, unless EOF has
5073 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005074 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005075 * When writing buffer lines, drop the typed
5076 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005078 if (options & SHELL_WRITE)
5079 ta_len = 0;
5080 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
5082 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5083 if (len > 0)
5084 {
5085 ta_len -= len;
5086 mch_memmove(ta_buf, ta_buf + len, ta_len);
5087 }
5088 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
5091
Bram Moolenaardf177f62005-02-22 08:39:57 +00005092 if (got_int)
5093 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005094 // CTRL-C sends a signal to the child, we ignore it
5095 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005096# ifdef HAVE_SETSID
5097 kill(-pid, SIGINT);
5098# else
5099 kill(0, SIGINT);
5100# endif
5101 if (wpid > 0)
5102 kill(wpid, SIGINT);
5103 got_int = FALSE;
5104 }
5105
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 /*
5107 * Check if the child has any characters to be printed.
5108 * Read them and write them to our window. Repeat this as
5109 * long as there is something to do, avoid the 10ms wait
5110 * for mch_inchar(), or sending typeahead characters to
5111 * the external process.
5112 * TODO: This should handle escape sequences, compatible
5113 * to some terminal (vt52?).
5114 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005115 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005116 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005118 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005121 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005123
5124 noread_cnt = 0;
5125 if (options & SHELL_READ)
5126 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005127 // Do NUL -> NL translation, append NL separated
5128 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005129 for (i = 0; i < len; ++i)
5130 {
5131 if (buffer[i] == NL)
5132 append_ga_line(&ga);
5133 else if (buffer[i] == NUL)
5134 ga_append(&ga, NL);
5135 else
5136 ga_append(&ga, buffer[i]);
5137 }
5138 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005139 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
5141 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005142 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
Bram Moolenaardf177f62005-02-22 08:39:57 +00005144 len += buffer_off;
5145 buffer[len] = NUL;
5146
Bram Moolenaar0f873732019-12-05 20:28:46 +01005147 // Check if the last character in buffer[] is
5148 // incomplete, keep these bytes for the next
5149 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 for (p = buffer; p < buffer + len; p += l)
5151 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005152 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005154 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 else if (MB_BYTE2LEN(*p) != l)
5156 break;
5157 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005158 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005160 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if (len >= 12)
5162 ++p;
5163 else
5164 {
5165 buffer_off = len;
5166 continue;
5167 }
5168 }
5169 c = *p;
5170 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005171 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if (p < buffer + len)
5173 {
5174 *p = c;
5175 buffer_off = (buffer + len) - p;
5176 mch_memmove(buffer, p, buffer_off);
5177 continue;
5178 }
5179 buffer_off = 0;
5180 }
5181 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 {
5183 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005184 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186
5187 windgoto(msg_row, msg_col);
5188 cursor_on();
5189 out_flush();
5190 if (got_int)
5191 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005192
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005193# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005194 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005195 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005196 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005197
Bram Moolenaar0f873732019-12-05 20:28:46 +01005198 // Avoid that we keep looping here without
5199 // checking for a CTRL-C for a long time. Don't
5200 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005201 if (msec > 2000)
5202 {
5203 noread_cnt = 5;
5204 break;
5205 }
5206 }
5207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 }
5209
Bram Moolenaar0f873732019-12-05 20:28:46 +01005210 // If we already detected the child has finished, continue
5211 // reading output for a short while. Some text may be
5212 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005213 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005214 {
5215 if (noread_cnt < 5)
5216 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005217 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005218 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005219
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 /*
5221 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005222 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005224# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005225 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005226# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5230 || (wait_pid == pid && WIFEXITED(status)))
5231 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005232 // Don't break the loop yet, try reading more
5233 // characters from "fromshell_fd" first. When using
5234 // pipes there might still be something to read and
5235 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005238 else
5239 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005240
Bram Moolenaar95a51352013-03-21 22:53:50 +01005241# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005242 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005243 clip_update();
5244# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 }
5246finished:
5247 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005248 if (options & SHELL_READ)
5249 {
5250 if (ga.ga_len > 0)
5251 {
5252 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005253 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005254 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005255 }
5256 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005257 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005258 ga_clear(&ga);
5259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 /*
5262 * Give all typeahead that wasn't used back to ui_inchar().
5263 */
5264 if (ta_len)
5265 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 State = old_State;
5267 if (toshell_fd >= 0)
5268 close(toshell_fd);
5269 close(fromshell_fd);
5270 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005271# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005272 else
5273 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005274 long delay_msec = 1;
5275
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005276 if (tmode == TMODE_RAW)
5277 // possibly disables modifyOtherKeys, so that the system
5278 // can recognize CTRL-C
5279 out_str(T_CTE);
Bram Moolenaar0981c872020-08-23 14:28:37 +02005280
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005281 /*
5282 * Similar to the loop above, but only handle X events, no
5283 * I/O.
5284 */
5285 for (;;)
5286 {
5287 if (got_int)
5288 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005289 // CTRL-C sends a signal to the child, we ignore it
5290 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005291# ifdef HAVE_SETSID
5292 kill(-pid, SIGINT);
5293# else
5294 kill(0, SIGINT);
5295# endif
5296 got_int = FALSE;
5297 }
5298# ifdef __NeXT__
5299 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5300# else
5301 wait_pid = waitpid(pid, &status, WNOHANG);
5302# endif
5303 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5304 || (wait_pid == pid && WIFEXITED(status)))
5305 {
5306 wait_pid = pid;
5307 break;
5308 }
5309
Bram Moolenaar0f873732019-12-05 20:28:46 +01005310 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005311 clip_update();
5312
Bram Moolenaar0f873732019-12-05 20:28:46 +01005313 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005314 // less time, gradually wait longer.
5315 mch_delay(delay_msec,
5316 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005317 if (++delay_msec > 10)
5318 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005319 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005320
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005321 if (tmode == TMODE_RAW)
5322 // possibly enables modifyOtherKeys again
5323 out_str(T_CTI);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005324 }
5325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326
5327 /*
5328 * Wait until our child has exited.
5329 * Ignore wait() returning pids of other children and returning
5330 * because of some signal like SIGWINCH.
5331 * Don't wait if wait_pid was already set above, indicating the
5332 * child already exited.
5333 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005334 if (wait_pid != pid)
5335 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
Bram Moolenaar624891f2010-10-13 16:22:09 +02005337# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005338 // Close slave side of pty. Only do this after the child has
5339 // exited, otherwise the child may hang when it tries to write on
5340 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005341 if (pty_master_fd >= 0)
5342 close(pty_slave_fd);
5343# endif
5344
Bram Moolenaar0f873732019-12-05 20:28:46 +01005345 // Make sure the child that writes to the external program is
5346 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005347 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005348 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005349 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005350 wait4pid(wpid, NULL);
5351 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005352
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005353# ifdef FEAT_JOB_CHANNEL
5354 --dont_check_job_ended;
5355# endif
5356
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 /*
5358 * Set to raw mode right now, otherwise a CTRL-C after
5359 * catch_signals() will kill Vim.
5360 */
5361 if (tmode == TMODE_RAW)
5362 settmode(TMODE_RAW);
5363 did_settmode = TRUE;
5364 set_signals();
5365
5366 if (WIFEXITED(status))
5367 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005368 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005370 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 {
5372 if (retval == EXEC_FAILED)
5373 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005374 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 msg_outtrans(p_sh);
5376 msg_putchar('\n');
5377 }
5378 else if (!(options & SHELL_SILENT))
5379 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005380 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 msg_outnum((long)retval);
5382 msg_putchar('\n');
5383 }
5384 }
5385 }
5386 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005387 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
5391error:
5392 if (!did_settmode)
5393 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005394 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395# ifdef FEAT_TITLE
5396 resettitle();
5397# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005398 vim_free(argv);
5399 vim_free(tofree1);
5400 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401
5402 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005403}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005404#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005405
5406 int
5407mch_call_shell(
5408 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005409 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005410{
5411#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5412 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5413 return mch_call_shell_terminal(cmd, options);
5414#endif
5415#ifdef USE_SYSTEM
5416 return mch_call_shell_system(cmd, options);
5417#else
5418 return mch_call_shell_fork(cmd, options);
5419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420}
5421
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005422#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005423 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005424mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005425{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005426 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005427 int fd_in[2] = {-1, -1}; // for stdin
5428 int fd_out[2] = {-1, -1}; // for stdout
5429 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005430 int pty_master_fd = -1;
5431 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005432 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005433 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5434 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5435 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005436 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005437 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5438 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005439 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005440 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005441 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005442
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005443 if (use_out_for_err && use_null_for_out)
5444 use_null_for_err = TRUE;
5445
Bram Moolenaar0f873732019-12-05 20:28:46 +01005446 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005447 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005448
Bram Moolenaarb2412082017-08-20 18:09:14 +02005449 if (options->jo_pty
5450 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005451 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005452 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005453 open_pty(&pty_master_fd, &pty_slave_fd,
5454 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005455
Bram Moolenaar0f873732019-12-05 20:28:46 +01005456 // TODO: without the channel feature connect the child to /dev/null?
5457 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005458 if (use_file_for_in)
5459 {
5460 char_u *fname = options->jo_io_name[PART_IN];
5461
5462 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5463 if (fd_in[0] < 0)
5464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005465 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005466 goto failed;
5467 }
5468 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005469 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005470 // When writing buffer lines to the input don't use the pty, so that
5471 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005472 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5473 && pipe(fd_in) < 0)
5474 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005475
5476 if (use_file_for_out)
5477 {
5478 char_u *fname = options->jo_io_name[PART_OUT];
5479
5480 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5481 if (fd_out[1] < 0)
5482 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005483 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005484 goto failed;
5485 }
5486 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005487 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005488 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005489
5490 if (use_file_for_err)
5491 {
5492 char_u *fname = options->jo_io_name[PART_ERR];
5493
5494 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5495 if (fd_err[1] < 0)
5496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005497 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005498 goto failed;
5499 }
5500 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005501 else if (!use_out_for_err && !use_null_for_err
5502 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005503 goto failed;
5504
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005505 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5506 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005507 if (options->jo_set & JO_CHANNEL)
5508 {
5509 channel = options->jo_channel;
5510 if (channel != NULL)
5511 ++channel->ch_refcount;
5512 }
5513 else
5514 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005515 if (channel == NULL)
5516 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005517 if (job->jv_tty_out != NULL)
5518 ch_log(channel, "using pty %s on fd %d",
5519 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005520 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005521
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005522 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005523 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005524 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005525 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005526 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005527 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005528 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005529 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005530 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005531 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005532 int null_fd = -1;
5533 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005534
Bram Moolenaar0f873732019-12-05 20:28:46 +01005535 // child
5536 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005537 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005538
Bram Moolenaar819524702018-02-27 19:10:00 +01005539# ifdef FEAT_JOB_CHANNEL
5540 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005541 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005542 ch_logfile((char_u *)"", (char_u *)"");
5543# endif
5544
Bram Moolenaar835dc632016-02-07 14:27:38 +01005545# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005546 // Create our own process group, so that the child and all its
5547 // children can be kill()ed. Don't do this when using pipes,
5548 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005549 (void)setsid();
5550# endif
5551
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005552# ifdef FEAT_TERMINAL
5553 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005554 {
5555 char *term = (char *)T_NAME;
5556
5557#ifdef FEAT_GUI
5558 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005559 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005560 term = getenv("TERM");
5561#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005562 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005563 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005564 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005565 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005566 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005567 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005568 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005569 else if (t_colors > 16)
5570 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005571 else
5572 term = "xterm";
5573 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005574 set_child_environment(
5575 (long)options->jo_term_rows,
5576 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005577 term,
5578 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005579 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005580 else
5581# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005582 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005583
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005584 if (options->jo_env != NULL)
5585 {
5586 dict_T *dict = options->jo_env;
5587 hashitem_T *hi;
5588 int todo = (int)dict->dv_hashtab.ht_used;
5589
5590 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5591 if (!HASHITEM_EMPTY(hi))
5592 {
5593 typval_T *item = &dict_lookup(hi)->di_tv;
5594
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005595 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005596 --todo;
5597 }
5598 }
5599
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005600 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005601 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005602 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005603 if (null_fd < 0)
5604 {
5605 perror("opening /dev/null failed");
5606 _exit(OPEN_NULL_FAILED);
5607 }
5608 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005609
Bram Moolenaar223896d2017-08-02 22:33:28 +02005610 if (pty_slave_fd >= 0)
5611 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005612 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005613 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005614# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005615 // Try to become controlling tty (probably doesn't work,
5616 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005617 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5618# endif
5619 }
5620
Bram Moolenaar0f873732019-12-05 20:28:46 +01005621 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005622 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005623 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005624 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005625 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005626 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005627 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005628 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005629
Bram Moolenaar0f873732019-12-05 20:28:46 +01005630 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005631 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005632 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005633 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005634 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005635 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005636 }
5637 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005638 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005639 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005640 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005641 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005642 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005643
Bram Moolenaar0f873732019-12-05 20:28:46 +01005644 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005645 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005646 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005647 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005648 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005649 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005650 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005651 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005652
5653 if (fd_in[0] >= 0)
5654 close(fd_in[0]);
5655 if (fd_in[1] >= 0)
5656 close(fd_in[1]);
5657 if (fd_out[0] >= 0)
5658 close(fd_out[0]);
5659 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005660 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005661 if (fd_err[0] >= 0)
5662 close(fd_err[0]);
5663 if (fd_err[1] >= 0)
5664 close(fd_err[1]);
5665 if (pty_master_fd >= 0)
5666 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005667 close(pty_master_fd); // not used in the child
5668 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005669 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005670
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005671 if (null_fd >= 0)
5672 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005673
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005674 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5675 _exit(EXEC_FAILED);
5676
Bram Moolenaar0f873732019-12-05 20:28:46 +01005677 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005678 execvp(argv[0], argv);
5679
Bram Moolenaar4694a172016-04-21 14:05:23 +02005680 if (stderr_works)
5681 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005682# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005683 // calling free_all_mem() here causes problems. Ignore valgrind
5684 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005685# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005686 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005687 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005688
Bram Moolenaar0f873732019-12-05 20:28:46 +01005689 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005690 UNBLOCK_SIGNALS(&curset);
5691
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005692 job->jv_pid = pid;
5693 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005694 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005695
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005696 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005697 close(pty_slave_fd); // not used in the parent
5698 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005699 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005700 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005701 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005702 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005703 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005704 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005705 if (channel != NULL)
5706 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005707 int in_fd = INVALID_FD;
5708 int out_fd = INVALID_FD;
5709 int err_fd = INVALID_FD;
5710
5711 if (!(use_file_for_in || use_null_for_in))
5712 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5713
5714 if (!(use_file_for_out || use_null_for_out))
5715 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5716
5717 // When using pty_master_fd only set it for stdout, do not duplicate
5718 // it for stderr, it only needs to be read once.
5719 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5720 {
5721 if (fd_err[0] >= 0)
5722 err_fd = fd_err[0];
5723 else if (out_fd != pty_master_fd)
5724 err_fd = pty_master_fd;
5725 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005726
5727 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005728 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005729 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005730 else
5731 {
5732 if (fd_in[1] >= 0)
5733 close(fd_in[1]);
5734 if (fd_out[0] >= 0)
5735 close(fd_out[0]);
5736 if (fd_err[0] >= 0)
5737 close(fd_err[0]);
5738 if (pty_master_fd >= 0)
5739 close(pty_master_fd);
5740 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005741
Bram Moolenaar0f873732019-12-05 20:28:46 +01005742 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005743 return;
5744
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005745failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005746 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005747 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005748 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005749 if (fd_in[1] >= 0)
5750 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005751 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005752 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005753 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005754 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005755 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005756 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005757 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005758 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005759 if (pty_master_fd >= 0)
5760 close(pty_master_fd);
5761 if (pty_slave_fd >= 0)
5762 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005763}
5764
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005765 static char_u *
5766get_signal_name(int sig)
5767{
5768 int i;
5769 char_u numbuf[NUMBUFLEN];
5770
5771 if (sig == SIGKILL)
5772 return vim_strsave((char_u *)"kill");
5773
5774 for (i = 0; signal_info[i].sig != -1; i++)
5775 if (sig == signal_info[i].sig)
5776 return strlow_save((char_u *)signal_info[i].name);
5777
5778 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5779 return vim_strsave(numbuf);
5780}
5781
Bram Moolenaar835dc632016-02-07 14:27:38 +01005782 char *
5783mch_job_status(job_T *job)
5784{
5785# ifdef HAVE_UNION_WAIT
5786 union wait status;
5787# else
5788 int status = -1;
5789# endif
5790 pid_t wait_pid = 0;
5791
5792# ifdef __NeXT__
5793 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5794# else
5795 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5796# endif
5797 if (wait_pid == -1)
5798 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005799 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005800 if (job->jv_status < JOB_ENDED)
5801 ch_log(job->jv_channel, "Job no longer exists: %s",
5802 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005803 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005804 }
5805 if (wait_pid == 0)
5806 return "run";
5807 if (WIFEXITED(status))
5808 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005809 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005810 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005811 if (job->jv_status < JOB_ENDED)
5812 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005813 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005814 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005815 if (WIFSIGNALED(status))
5816 {
5817 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005818 job->jv_termsig = get_signal_name(WTERMSIG(status));
5819 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5820 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5821 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005822 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005823 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005824 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005825
5826return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005827 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005828 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005829 return "dead";
5830}
5831
5832 job_T *
5833mch_detect_ended_job(job_T *job_list)
5834{
5835# ifdef HAVE_UNION_WAIT
5836 union wait status;
5837# else
5838 int status = -1;
5839# endif
5840 pid_t wait_pid = 0;
5841 job_T *job;
5842
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005843# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005844 // Do not do this when waiting for a shell command to finish, we would get
5845 // the exit value here (and discard it), the exit value obtained there
5846 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005847 if (dont_check_job_ended > 0)
5848 return NULL;
5849# endif
5850
Bram Moolenaar97792de2016-10-15 18:36:49 +02005851# ifdef __NeXT__
5852 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5853# else
5854 wait_pid = waitpid(-1, &status, WNOHANG);
5855# endif
5856 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005857 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005858 return NULL;
5859 for (job = job_list; job != NULL; job = job->jv_next)
5860 {
5861 if (job->jv_pid == wait_pid)
5862 {
5863 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005864 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005865 job->jv_exitval = WEXITSTATUS(status);
5866 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005867 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005868 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005869 job->jv_termsig = get_signal_name(WTERMSIG(status));
5870 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005871 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005872 {
5873 ch_log(job->jv_channel, "Job ended");
5874 job->jv_status = JOB_ENDED;
5875 }
5876 return job;
5877 }
5878 }
5879 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005880}
5881
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005882/*
5883 * Send a (deadly) signal to "job".
5884 * Return FAIL if "how" is not a valid name.
5885 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005886 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005887mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005888{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005889 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005890
Bram Moolenaar923d9262016-02-25 20:56:01 +01005891 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005892 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005893 else if (STRCMP(how, "hup") == 0)
5894 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005895 else if (STRCMP(how, "quit") == 0)
5896 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005897 else if (STRCMP(how, "int") == 0)
5898 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005899 else if (STRCMP(how, "kill") == 0)
5900 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005901#ifdef SIGWINCH
5902 else if (STRCMP(how, "winch") == 0)
5903 sig = SIGWINCH;
5904#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005905 else if (isdigit(*how))
5906 sig = atoi((char *)how);
5907 else
5908 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005909
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005910 // Never kill ourselves!
5911 if (job->jv_pid != 0)
5912 {
5913 // TODO: have an option to only kill the process, not the group?
5914 kill(-job->jv_pid, sig);
5915 kill(job->jv_pid, sig);
5916 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005917
Bram Moolenaar835dc632016-02-07 14:27:38 +01005918 return OK;
5919}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005920
5921/*
5922 * Clear the data related to "job".
5923 */
5924 void
5925mch_clear_job(job_T *job)
5926{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005927 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005928# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005929 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005930# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005931 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005932# endif
5933}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005934#endif
5935
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005936#if defined(FEAT_TERMINAL) || defined(PROTO)
5937 int
5938mch_create_pty_channel(job_T *job, jobopt_T *options)
5939{
5940 int pty_master_fd = -1;
5941 int pty_slave_fd = -1;
5942 channel_T *channel;
5943
Bram Moolenaar59386482019-02-10 22:43:46 +01005944 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02005945 if (pty_master_fd < 0 || pty_slave_fd < 0)
5946 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005947 close(pty_slave_fd);
5948
5949 channel = add_channel();
5950 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005951 {
5952 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005953 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005954 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005955 if (job->jv_tty_out != NULL)
5956 ch_log(channel, "using pty %s on fd %d",
5957 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005958 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005959 channel->ch_keep_open = TRUE;
5960
Bram Moolenaar0f873732019-12-05 20:28:46 +01005961 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5962 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005963 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005964 channel_set_job(channel, job, options);
5965 return OK;
5966}
5967#endif
5968
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969/*
5970 * Check for CTRL-C typed by reading all available characters.
5971 * In cooked mode we should get SIGINT, no need to check.
5972 */
5973 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005974mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975{
Bram Moolenaar26e86442020-05-17 14:06:16 +02005976 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005977 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 fill_input_buf(FALSE);
5979}
5980
5981/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005982 * Wait "msec" msec until a character is available from the mouse, keyboard,
5983 * from inbuf[].
5984 * "msec" == -1 will block forever.
5985 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005986 * When "ignore_input" is TRUE even check for pending input when input is
5987 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005988 * "interrupted" (if not NULL) is set to TRUE when no character is available
5989 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005990 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005991 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 */
5993 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005994WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005996#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005997 return ui_wait_for_chars_or_timer(
5998 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005999#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006000 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006001#endif
6002}
6003
6004/*
6005 * Wait "msec" msec until a character is available from the mouse or keyboard
6006 * or from inbuf[].
6007 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006008 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006009 * "interrupted" (if not NULL) is set to TRUE when no character is available
6010 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006011 * When a GUI is being used, this will never get called -- webb
6012 */
6013 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006014WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006015{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016#ifdef FEAT_MOUSE_GPM
6017 int gpm_process_wanted;
6018#endif
6019#ifdef FEAT_XCLIPBOARD
6020 int rest;
6021#endif
6022 int avail;
6023
Bram Moolenaar0f873732019-12-05 20:28:46 +01006024 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 return 1;
6026
6027#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006028 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 if (WantQueryMouse)
6030 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006031 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006032 if (!no_query_mouse_for_testing)
6033 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 }
6035#endif
6036
6037 /*
6038 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6039 * events. This is a bit complicated, because they might both be defined.
6040 */
6041#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6042# ifdef FEAT_XCLIPBOARD
6043 rest = 0;
6044 if (do_xterm_trace())
6045 rest = msec;
6046# endif
6047 do
6048 {
6049# ifdef FEAT_XCLIPBOARD
6050 if (rest != 0)
6051 {
6052 msec = XT_TRACE_DELAY;
6053 if (rest >= 0 && rest < XT_TRACE_DELAY)
6054 msec = rest;
6055 if (rest >= 0)
6056 rest -= msec;
6057 }
6058# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006059# ifdef FEAT_SOUND_CANBERRA
6060 // Invoke any pending sound callbacks.
6061 if (has_sound_callback_in_queue())
6062 invoke_sound_callback();
6063# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064# ifdef FEAT_MOUSE_GPM
6065 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006066 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006067 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006068 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006070 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006072# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006074 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 return 1;
6076# ifdef FEAT_XCLIPBOARD
6077 if (rest == 0 || !do_xterm_trace())
6078# endif
6079 break;
6080 }
6081 }
6082 while (FALSE
6083# ifdef FEAT_MOUSE_GPM
6084 || (gpm_process_wanted && mch_gpm_process() == 0)
6085# endif
6086# ifdef FEAT_XCLIPBOARD
6087 || (!avail && rest != 0)
6088# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006089 )
6090 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
6092#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006093 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094#endif
6095 return avail;
6096}
6097
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006098#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099/*
6100 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006101 * "msec" == 0 will check for characters once.
6102 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006105 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006106 * "interrupted" (if not NULL) is set to TRUE when no character is available
6107 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006109 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006110RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111{
6112 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006113 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006114#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 static int busy = FALSE;
6116
Bram Moolenaar0f873732019-12-05 20:28:46 +01006117 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118# define MAY_LOOP
6119
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006120# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006121 // Remember at what time we started, so that we know how much longer we
6122 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006123 long start_msec = msec;
6124 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006126 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006127 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128# endif
6129
Bram Moolenaar0f873732019-12-05 20:28:46 +01006130 // Handle being called recursively. This may happen for the session
6131 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 if (busy)
6133 return 0;
6134#endif
6135
6136#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006137 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138#endif
6139 {
6140#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006141 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006142# ifdef FEAT_MZSCHEME
6143 int mzquantum_used = FALSE;
6144# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145#endif
6146#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006147 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006148 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 int nfd;
6150# ifdef FEAT_XCLIPBOARD
6151 int xterm_idx = -1;
6152# endif
6153# ifdef FEAT_MOUSE_GPM
6154 int gpm_idx = -1;
6155# endif
6156# ifdef USE_XSMP
6157 int xsmp_idx = -1;
6158# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006159 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006161# ifdef FEAT_MZSCHEME
6162 mzvim_check_threads();
6163 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6164 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006165 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006166 mzquantum_used = TRUE;
6167 }
6168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 fds[0].fd = fd;
6170 fds[0].events = POLLIN;
6171 nfd = 1;
6172
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006174 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 if (xterm_Shell != (Widget)0)
6176 {
6177 xterm_idx = nfd;
6178 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6179 fds[nfd].events = POLLIN;
6180 nfd++;
6181 }
6182# endif
6183# ifdef FEAT_MOUSE_GPM
6184 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6185 {
6186 gpm_idx = nfd;
6187 fds[nfd].fd = gpm_fd;
6188 fds[nfd].events = POLLIN;
6189 nfd++;
6190 }
6191# endif
6192# ifdef USE_XSMP
6193 if (xsmp_icefd != -1)
6194 {
6195 xsmp_idx = nfd;
6196 fds[nfd].fd = xsmp_icefd;
6197 fds[nfd].events = POLLIN;
6198 nfd++;
6199 }
6200# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006201#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006202 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006203#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006204 if (interrupted != NULL)
6205 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006207 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006208
6209 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006210 if (result == 0 && interrupted != NULL && ret > 0)
6211 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006212
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006213# ifdef FEAT_MZSCHEME
6214 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006215 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006216 finished = FALSE;
6217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219# ifdef FEAT_XCLIPBOARD
6220 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6221 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006222 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006224 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 finished = FALSE;
6226 }
6227# endif
6228# ifdef FEAT_MOUSE_GPM
6229 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231# endif
6232# ifdef USE_XSMP
6233 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6234 {
6235 if (fds[xsmp_idx].revents & POLLIN)
6236 {
6237 busy = TRUE;
6238 xsmp_handle_requests();
6239 busy = FALSE;
6240 }
6241 else if (fds[xsmp_idx].revents & POLLHUP)
6242 {
6243 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006244 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 xsmp_close();
6246 }
6247 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006248 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 }
6250# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006251#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006252 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006253 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006254 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256
Bram Moolenaar0f873732019-12-05 20:28:46 +01006257#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258
6259 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006260 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006261 // These are static because they can take 8 Kbyte each and cause the
6262 // signal stack to run out with -O3.
6263 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006265 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006267# ifdef FEAT_MZSCHEME
6268 mzvim_check_threads();
6269 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6270 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006271 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006272 mzquantum_used = TRUE;
6273 }
6274# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006276 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006278 tv.tv_sec = towait / 1000;
6279 tv.tv_usec = (towait % 1000) * (1000000/1000);
6280 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006282 else
6283 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284
6285 /*
6286 * Select on ready for reading and exceptional condition (end of file).
6287 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006288select_eintr:
6289 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006290 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 FD_ZERO(&efds);
6292 FD_SET(fd, &rfds);
6293# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006294 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 FD_SET(fd, &efds);
6296# endif
6297 maxfd = fd;
6298
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006300 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 if (xterm_Shell != (Widget)0)
6302 {
6303 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6304 if (maxfd < ConnectionNumber(xterm_dpy))
6305 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006306
Bram Moolenaar0f873732019-12-05 20:28:46 +01006307 // An event may have already been read but not handled. In
6308 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006309 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 }
6311# endif
6312# ifdef FEAT_MOUSE_GPM
6313 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6314 {
6315 FD_SET(gpm_fd, &rfds);
6316 FD_SET(gpm_fd, &efds);
6317 if (maxfd < gpm_fd)
6318 maxfd = gpm_fd;
6319 }
6320# endif
6321# ifdef USE_XSMP
6322 if (xsmp_icefd != -1)
6323 {
6324 FD_SET(xsmp_icefd, &rfds);
6325 FD_SET(xsmp_icefd, &efds);
6326 if (maxfd < xsmp_icefd)
6327 maxfd = xsmp_icefd;
6328 }
6329# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006330# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006331 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006332# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006333 if (interrupted != NULL)
6334 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335
Bram Moolenaar643b6142018-09-12 20:29:09 +02006336 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6337 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006338 result = ret > 0 && FD_ISSET(fd, &rfds);
6339 if (result)
6340 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006341 else if (interrupted != NULL && ret > 0)
6342 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006343
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006344# ifdef EINTR
6345 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006346 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006347 // Check whether window has been resized, EINTR may be caused by
6348 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006349 if (do_resize)
6350 handle_resize();
6351
Bram Moolenaar0f873732019-12-05 20:28:46 +01006352 // Interrupted by a signal, need to try again. We ignore msec
6353 // here, because we do want to check even after a timeout if
6354 // characters are available. Needed for reading output of an
6355 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006356 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006357 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006358# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006359# ifdef __TANDEM
6360 if (ret == -1 && errno == ENOTSUP)
6361 {
6362 FD_ZERO(&rfds);
6363 FD_ZERO(&efds);
6364 ret = 0;
6365 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006366# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006367# ifdef FEAT_MZSCHEME
6368 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006369 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006370 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371# endif
6372
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373# ifdef FEAT_XCLIPBOARD
6374 if (ret > 0 && xterm_Shell != (Widget)0
6375 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6376 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006377 xterm_update(); // Maybe we should hand out clipboard
6378 // continue looping when we only got the X event and the input
6379 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 if (--ret == 0 && !input_available())
6381 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006382 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 finished = FALSE;
6384 }
6385 }
6386# endif
6387# ifdef FEAT_MOUSE_GPM
6388 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6389 {
6390 if (FD_ISSET(gpm_fd, &efds))
6391 gpm_close();
6392 else if (FD_ISSET(gpm_fd, &rfds))
6393 *check_for_gpm = 1;
6394 }
6395# endif
6396# ifdef USE_XSMP
6397 if (ret > 0 && xsmp_icefd != -1)
6398 {
6399 if (FD_ISSET(xsmp_icefd, &efds))
6400 {
6401 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006402 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 xsmp_close();
6404 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006405 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 }
6407 else if (FD_ISSET(xsmp_icefd, &rfds))
6408 {
6409 busy = TRUE;
6410 xsmp_handle_requests();
6411 busy = FALSE;
6412 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006413 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 }
6415 }
6416# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006417#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006418 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006419 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006420 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422
Bram Moolenaar0f873732019-12-05 20:28:46 +01006423#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424
6425#ifdef MAY_LOOP
6426 if (finished || msec == 0)
6427 break;
6428
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006429# ifdef FEAT_CLIENTSERVER
6430 if (server_waiting())
6431 break;
6432# endif
6433
Bram Moolenaar0f873732019-12-05 20:28:46 +01006434 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 if (msec > 0)
6436 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006437# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006438 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006439 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006441 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 msec = msec / 2;
6443# endif
6444 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006445 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 }
6447#endif
6448 }
6449
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006450 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451}
6452
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006454 * Expand a path into all matching files and/or directories. Handles "*",
6455 * "?", "[a-z]", "**", etc.
6456 * "path" has backslashes before chars that are not to be expanded.
6457 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 */
6459 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006460mch_expandpath(
6461 garray_T *gap,
6462 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006463 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
Bram Moolenaar02743632005-07-25 20:42:36 +00006465 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467
6468/*
6469 * mch_expand_wildcards() - this code does wild-card pattern matching using
6470 * the shell
6471 *
6472 * return OK for success, FAIL for error (you may lose some memory) and put
6473 * an error message in *file.
6474 *
6475 * num_pat is number of input patterns
6476 * pat is array of pointers to input patterns
6477 * num_file is pointer to number of matched file names
6478 * file is pointer to array of pointers to matched file names
6479 */
6480
6481#ifndef SEEK_SET
6482# define SEEK_SET 0
6483#endif
6484#ifndef SEEK_END
6485# define SEEK_END 2
6486#endif
6487
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006488#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006489
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006491mch_expand_wildcards(
6492 int num_pat,
6493 char_u **pat,
6494 int *num_file,
6495 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006496 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497{
6498 int i;
6499 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006500 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 char_u *p;
6502 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
Bram Moolenaarc7247912008-01-13 12:54:11 +00006504 /*
6505 * This is the non-OS/2 implementation (really Unix).
6506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 int j;
6508 char_u *tempname;
6509 char_u *command;
6510 FILE *fd;
6511 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006512#define STYLE_ECHO 0 // use "echo", the default
6513#define STYLE_GLOB 1 // use "glob", for csh
6514#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6515#define STYLE_PRINT 3 // use "print -N", for zsh
6516#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6517 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 int shell_style = STYLE_ECHO;
6519 int check_spaces;
6520 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006521 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006522 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006523 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524
Bram Moolenaar0f873732019-12-05 20:28:46 +01006525 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 *file = NULL;
6527
6528 /*
6529 * If there are no wildcards, just copy the names to allocated memory.
6530 * Saves a lot of time, because we don't have to start a new shell.
6531 */
6532 if (!have_wildcard(num_pat, pat))
6533 return save_patterns(num_pat, pat, num_file, file);
6534
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006535# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006536 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006537 if (sandbox != 0 && check_secure())
6538 return FAIL;
6539# endif
6540
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 /*
6542 * Don't allow the use of backticks in secure and restricted mode.
6543 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006544 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 for (i = 0; i < num_pat; ++i)
6546 if (vim_strchr(pat[i], '`') != NULL
6547 && (check_restricted() || check_secure()))
6548 return FAIL;
6549
6550 /*
6551 * get a name for the temp file
6552 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006553 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006555 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 return FAIL;
6557 }
6558
6559 /*
6560 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006561 * file.
6562 * STYLE_BT: NL separated
6563 * If expanding `cmd` execute it directly.
6564 * STYLE_GLOB: NUL separated
6565 * If we use *csh, "glob" will work better than "echo".
6566 * STYLE_PRINT: NL or NUL separated
6567 * If we use *zsh, "print -N" will work better than "glob".
6568 * STYLE_VIMGLOB: NL separated
6569 * If we use *sh*, we define "vimglob()".
6570 * STYLE_ECHO: space separated.
6571 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 */
6573 if (num_pat == 1 && *pat[0] == '`'
6574 && (len = STRLEN(pat[0])) > 2
6575 && *(pat[0] + len - 1) == '`')
6576 shell_style = STYLE_BT;
6577 else if ((len = STRLEN(p_sh)) >= 3)
6578 {
6579 if (STRCMP(p_sh + len - 3, "csh") == 0)
6580 shell_style = STYLE_GLOB;
6581 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6582 shell_style = STYLE_PRINT;
6583 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006584 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6585 "sh") != NULL)
6586 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587
Bram Moolenaar0f873732019-12-05 20:28:46 +01006588 // Compute the length of the command. We need 2 extra bytes: for the
6589 // optional '&' and for the NUL.
6590 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006592 if (shell_style == STYLE_VIMGLOB)
6593 len += STRLEN(sh_vimglob_func);
6594
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006595 for (i = 0; i < num_pat; ++i)
6596 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006597 // Count the length of the patterns in the same way as they are put in
6598 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006599#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006600 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006601#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006602 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006603 for (j = 0; pat[i][j] != NUL; ++j)
6604 {
6605 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006606 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006607 ++len;
6608 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006609#endif
6610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 command = alloc(len);
6612 if (command == NULL)
6613 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006614 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 vim_free(tempname);
6616 return FAIL;
6617 }
6618
6619 /*
6620 * Build the shell command:
6621 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6622 * recognizes this).
6623 * - Add the shell command to print the expanded names.
6624 * - Add the temp file name.
6625 * - Add the file name patterns.
6626 */
6627 if (shell_style == STYLE_BT)
6628 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006629 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006630 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006631 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006633 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006634 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006636 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006638 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 *p = ' ';
6640 }
6641 STRCAT(command, ">");
6642 }
6643 else
6644 {
6645 if (flags & EW_NOTFOUND)
6646 STRCPY(command, "set nonomatch; ");
6647 else
6648 STRCPY(command, "unset nonomatch; ");
6649 if (shell_style == STYLE_GLOB)
6650 STRCAT(command, "glob >");
6651 else if (shell_style == STYLE_PRINT)
6652 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006653 else if (shell_style == STYLE_VIMGLOB)
6654 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 else
6656 STRCAT(command, "echo >");
6657 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006658
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006660
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 if (shell_style != STYLE_BT)
6662 for (i = 0; i < num_pat; ++i)
6663 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006664 // When using system() always add extra quotes, because the shell
6665 // is started twice. Otherwise put a backslash before special
6666 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667#ifdef USE_SYSTEM
6668 STRCAT(command, " \"");
6669 STRCAT(command, pat[i]);
6670 STRCAT(command, "\"");
6671#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006672 int intick = FALSE;
6673
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 p = command + STRLEN(command);
6675 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006676 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006677 {
6678 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006679 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006680 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6681 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006682 // Remove a backslash, take char literally. But keep
6683 // backslash inside backticks, before a special character
6684 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006685 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006686 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6687 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006688 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006689 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006690 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006691 else if (!intick
6692 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6693 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006694 // Put a backslash before a special character, but not
6695 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6696 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006697 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006698
Bram Moolenaar0f873732019-12-05 20:28:46 +01006699 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006700 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 *p = NUL;
6703#endif
6704 }
6705 if (flags & EW_SILENT)
6706 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006707 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006708 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
6710 /*
6711 * Using zsh -G: If a pattern has no matches, it is just deleted from
6712 * the argument list, otherwise zsh gives an error message and doesn't
6713 * expand any other pattern.
6714 */
6715 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006716 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717
6718 /*
6719 * If we use -f then shell variables set in .cshrc won't get expanded.
6720 * vi can do it, so we will too, but it is only necessary if there is a "$"
6721 * in one of the patterns, otherwise we can still use the fast option.
6722 */
6723 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006724 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725
6726 /*
6727 * execute the shell command
6728 */
6729 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6730
Bram Moolenaar0f873732019-12-05 20:28:46 +01006731 // When running in the background, give it some time to create the temp
6732 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006733 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006734 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
Bram Moolenaar0f873732019-12-05 20:28:46 +01006736 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 show_shell_mess = TRUE;
6738 vim_free(command);
6739
Bram Moolenaar0f873732019-12-05 20:28:46 +01006740 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 {
6742 mch_remove(tempname);
6743 vim_free(tempname);
6744 /*
6745 * With interactive completion, the error message is not printed.
6746 * However with USE_SYSTEM, I don't know how to turn off error messages
6747 * from the shell, so screen may still get messed up -- webb.
6748 */
6749#ifndef USE_SYSTEM
6750 if (!(flags & EW_SILENT))
6751#endif
6752 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006753 redraw_later_clear(); // probably messed up screen
6754 msg_putchar('\n'); // clear bottom line quickly
6755 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756#ifdef USE_SYSTEM
6757 if (!(flags & EW_SILENT))
6758#endif
6759 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006760 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006761 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 }
6763 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006764 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6765 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 if (shell_style == STYLE_BT)
6767 return FAIL;
6768 goto notfound;
6769 }
6770
6771 /*
6772 * read the names from the file into memory
6773 */
6774 fd = fopen((char *)tempname, READBIN);
6775 if (fd == NULL)
6776 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006777 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 if (!(flags & EW_SILENT))
6779 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006780 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006781 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 }
6783 vim_free(tempname);
6784 goto notfound;
6785 }
6786 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006787 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006789 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006790 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006791 buffer = NULL;
6792 else
6793 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 if (buffer == NULL)
6795 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006796 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 mch_remove(tempname);
6798 vim_free(tempname);
6799 fclose(fd);
6800 return FAIL;
6801 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006802 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 i = fread((char *)buffer, 1, len, fd);
6804 fclose(fd);
6805 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006806 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006808 // unexpected read error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006809 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 vim_free(tempname);
6811 vim_free(buffer);
6812 return FAIL;
6813 }
6814 vim_free(tempname);
6815
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006816# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006817 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006819 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6821 *p++ = buffer[i];
6822 len = p - buffer;
6823# endif
6824
6825
Bram Moolenaar0f873732019-12-05 20:28:46 +01006826 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 if (shell_style == STYLE_ECHO)
6828 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006829 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006831 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 {
6833 while (*p != ' ' && *p != '\n')
6834 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006835 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 }
6837 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006838 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006839 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006841 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006843 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 {
6845 while (*p != '\n' && *p != NUL)
6846 ++p;
6847 if (*p != NUL)
6848 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006849 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 }
6851 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006852 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 else
6854 {
6855 /*
6856 * Some versions of zsh use spaces instead of NULs to separate
6857 * results. Only do this when there is no NUL before the end of the
6858 * buffer, otherwise we would never be able to use file names with
6859 * embedded spaces when zsh does use NULs.
6860 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6861 * don't check for spaces again.
6862 */
6863 check_spaces = FALSE;
6864 if (shell_style == STYLE_PRINT && !did_find_nul)
6865 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006866 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006867 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006868 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 did_find_nul = TRUE;
6870 else
6871 check_spaces = TRUE;
6872 }
6873
6874 /*
6875 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6876 * already is one, for STYLE_GLOB it needs to be added.
6877 */
6878 if (len && buffer[len - 1] == NUL)
6879 --len;
6880 else
6881 buffer[len] = NUL;
6882 i = 0;
6883 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006884 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {
6886 ++i;
6887 *p = NUL;
6888 }
6889 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006890 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
6892 if (i == 0)
6893 {
6894 /*
6895 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6896 * /bin/sh will happily expand it to nothing rather than returning an
6897 * error; and hey, it's good to check anyway -- webb.
6898 */
6899 vim_free(buffer);
6900 goto notfound;
6901 }
6902 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006903 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 if (*file == NULL)
6905 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006906 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 vim_free(buffer);
6908 return FAIL;
6909 }
6910
6911 /*
6912 * Isolate the individual file names.
6913 */
6914 p = buffer;
6915 for (i = 0; i < *num_file; ++i)
6916 {
6917 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006918 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006919 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6920 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006922 while (!(shell_style == STYLE_ECHO && *p == ' ')
6923 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006925 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 *p = NUL;
6927 else
6928 {
6929 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006930 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 }
6932 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006933 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006935 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006937 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 }
6939 }
6940
6941 /*
6942 * Move the file names to allocated memory.
6943 */
6944 for (j = 0, i = 0; i < *num_file; ++i)
6945 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006946 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6948 continue;
6949
Bram Moolenaar0f873732019-12-05 20:28:46 +01006950 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 dir = (mch_isdir((*file)[i]));
6952 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6953 continue;
6954
Bram Moolenaar0f873732019-12-05 20:28:46 +01006955 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01006956 if (!dir && (flags & EW_EXEC)
6957 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006958 continue;
6959
Bram Moolenaar964b3742019-05-24 18:54:09 +02006960 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 if (p)
6962 {
6963 STRCPY(p, (*file)[i]);
6964 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006965 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 (*file)[j++] = p;
6967 }
6968 }
6969 vim_free(buffer);
6970 *num_file = j;
6971
Bram Moolenaar0f873732019-12-05 20:28:46 +01006972 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006974 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 goto notfound;
6976 }
6977
6978 return OK;
6979
6980notfound:
6981 if (flags & EW_NOTFOUND)
6982 return save_patterns(num_pat, pat, num_file, file);
6983 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984}
6985
Bram Moolenaar0f873732019-12-05 20:28:46 +01006986#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006989save_patterns(
6990 int num_pat,
6991 char_u **pat,
6992 int *num_file,
6993 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994{
6995 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006996 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006998 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 if (*file == NULL)
7000 return FAIL;
7001 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007002 {
7003 s = vim_strsave(pat[i]);
7004 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007005 // Be compatible with expand_filename(): halve the number of
7006 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007007 backslash_halve(s);
7008 (*file)[i] = s;
7009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 *num_file = num_pat;
7011 return OK;
7012}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014/*
7015 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7016 * expand.
7017 */
7018 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007019mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007021 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 if (*p == '\\' && p[1] != NUL)
7024 ++p;
7025 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 if (vim_strchr((char_u *)
7027#ifdef VMS
7028 "*?%"
7029#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031#endif
7032 , *p) != NULL)
7033 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 }
7035 return FALSE;
7036}
7037
7038/*
7039 * Return TRUE if the string "p" contains a wildcard.
7040 * Don't recognize '~' at the end as a wildcard.
7041 */
7042 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007043mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007045 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 if (*p == '\\' && p[1] != NUL)
7048 ++p;
7049 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 if (vim_strchr((char_u *)
7051#ifdef VMS
7052 "*?%$"
7053#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055#endif
7056 , *p) != NULL
7057 || (*p == '~' && p[1] != NUL))
7058 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 }
7060 return FALSE;
7061}
7062
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007064have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065{
7066 int i;
7067
7068 for (i = 0; i < num; i++)
7069 if (mch_has_wildcard(file[i]))
7070 return 1;
7071 return 0;
7072}
7073
7074 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007075have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
7077 int i;
7078
7079 for (i = 0; i < num; i++)
7080 if (vim_strchr(file[i], '$') != NULL)
7081 return TRUE;
7082 return FALSE;
7083}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007085#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086/*
7087 * Scaled-down version of rename(), which is missing in Xenix.
7088 * This version can only move regular files and will fail if the
7089 * destination exists.
7090 */
7091 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007092mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093{
7094 struct stat st;
7095
Bram Moolenaar0f873732019-12-05 20:28:46 +01007096 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007098 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007100 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 return 0;
7102 return -1;
7103}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007104#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007106#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107/*
7108 * Initializes connection with gpm (if it isn't already opened)
7109 * Return 1 if succeeded (or connection already opened), 0 if failed
7110 */
7111 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007112gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007114 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
7116 if (!gpm_flag)
7117 {
7118 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7119 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007120 // Default handling for mouse move
7121 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 gpm_connect.maxMod = 0xffff;
7123 if (Gpm_Open(&gpm_connect, 0) > 0)
7124 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007125 // gpm library tries to handling TSTP causes
7126 // problems. Anyways, we close connection to Gpm whenever
7127 // we are going to suspend or starting an external process
7128 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007129# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007131# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007132 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 }
7134 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007135 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 return 0;
7137 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007138 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139}
7140
7141/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007142 * Returns TRUE if the GPM mouse is enabled.
7143 */
7144 int
7145gpm_enabled(void)
7146{
7147 return gpm_flag && gpm_fd >= 0;
7148}
7149
7150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 */
7153 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007154gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007156 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 Gpm_Close();
7158}
7159
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007160/*
7161 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007163 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 */
7165 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007166mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167{
7168 int button;
7169 static Gpm_Event gpm_event;
7170 char_u string[6];
7171 int_u vim_modifiers;
7172 int row,col;
7173 unsigned char buttons_mask;
7174 unsigned char gpm_modifiers;
7175 static unsigned char old_buttons = 0;
7176
7177 Gpm_GetEvent(&gpm_event);
7178
7179#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007180 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 if (hold_gui_events)
7182 return 0;
7183#endif
7184
7185 row = gpm_event.y - 1;
7186 col = gpm_event.x - 1;
7187
Bram Moolenaar0f873732019-12-05 20:28:46 +01007188 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 string[1] = 'M';
7190 string[2] = 'G';
7191 switch (GPM_BARE_EVENTS(gpm_event.type))
7192 {
7193 case GPM_DRAG:
7194 string[3] = MOUSE_DRAG;
7195 break;
7196 case GPM_DOWN:
7197 buttons_mask = gpm_event.buttons & ~old_buttons;
7198 old_buttons = gpm_event.buttons;
7199 switch (buttons_mask)
7200 {
7201 case GPM_B_LEFT:
7202 button = MOUSE_LEFT;
7203 break;
7204 case GPM_B_MIDDLE:
7205 button = MOUSE_MIDDLE;
7206 break;
7207 case GPM_B_RIGHT:
7208 button = MOUSE_RIGHT;
7209 break;
7210 default:
7211 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007212 // Don't know what to do. Can more than one button be
7213 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 }
7215 string[3] = (char_u)(button | 0x20);
7216 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7217 break;
7218 case GPM_UP:
7219 string[3] = MOUSE_RELEASE;
7220 old_buttons &= ~gpm_event.buttons;
7221 break;
7222 default:
7223 return 0;
7224 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007225 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 gpm_modifiers = gpm_event.modifiers;
7227 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007228 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7229 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7230 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7232 vim_modifiers |= MOUSE_SHIFT;
7233
7234 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7235 vim_modifiers |= MOUSE_CTRL;
7236 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7237 vim_modifiers |= MOUSE_ALT;
7238 string[3] |= vim_modifiers;
7239 string[4] = (char_u)(col + ' ' + 1);
7240 string[5] = (char_u)(row + ' ' + 1);
7241 add_to_input_buf(string, 6);
7242 return 6;
7243}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007244#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007246#ifdef FEAT_SYSMOUSE
7247/*
7248 * Initialize connection with sysmouse.
7249 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7250 * output, any sysmouse output than will be processed via sig_sysmouse().
7251 * Return OK if succeeded, FAIL if failed.
7252 */
7253 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007254sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007255{
7256 struct mouse_info mouse;
7257
7258 mouse.operation = MOUSE_MODE;
7259 mouse.u.mode.mode = 0;
7260 mouse.u.mode.signal = SIGUSR2;
7261 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7262 {
7263 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7264 mouse.operation = MOUSE_SHOW;
7265 ioctl(1, CONS_MOUSECTL, &mouse);
7266 return OK;
7267 }
7268 return FAIL;
7269}
7270
7271/*
7272 * Stop processing SIGUSR2 signals, and also make sure that
7273 * virtual console do not send us any sysmouse related signal.
7274 */
7275 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007276sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007277{
7278 struct mouse_info mouse;
7279
7280 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7281 mouse.operation = MOUSE_MODE;
7282 mouse.u.mode.mode = 0;
7283 mouse.u.mode.signal = 0;
7284 ioctl(1, CONS_MOUSECTL, &mouse);
7285}
7286
7287/*
7288 * Gets info from sysmouse and adds special keys to input buf.
7289 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007290 static RETSIGTYPE
7291sig_sysmouse SIGDEFARG(sigarg)
7292{
7293 struct mouse_info mouse;
7294 struct video_info video;
7295 char_u string[6];
7296 int row, col;
7297 int button;
7298 int buttons;
7299 static int oldbuttons = 0;
7300
7301#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007302 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007303 if (hold_gui_events)
7304 return;
7305#endif
7306
7307 mouse.operation = MOUSE_GETINFO;
7308 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7309 && ioctl(1, FBIO_MODEINFO, &video) != -1
7310 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7311 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7312 {
7313 row = mouse.u.data.y / video.vi_cheight;
7314 col = mouse.u.data.x / video.vi_cwidth;
7315 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007316 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007317 string[1] = 'M';
7318 string[2] = 'S';
7319 if (oldbuttons == buttons && buttons != 0)
7320 {
7321 button = MOUSE_DRAG;
7322 }
7323 else
7324 {
7325 switch (buttons)
7326 {
7327 case 0:
7328 button = MOUSE_RELEASE;
7329 break;
7330 case 1:
7331 button = MOUSE_LEFT;
7332 break;
7333 case 2:
7334 button = MOUSE_MIDDLE;
7335 break;
7336 case 4:
7337 button = MOUSE_RIGHT;
7338 break;
7339 default:
7340 return;
7341 }
7342 oldbuttons = buttons;
7343 }
7344 string[3] = (char_u)(button);
7345 string[4] = (char_u)(col + ' ' + 1);
7346 string[5] = (char_u)(row + ' ' + 1);
7347 add_to_input_buf(string, 6);
7348 }
7349 return;
7350}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007351#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007352
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007354typedef char_u * (*STRPROCSTR)(char_u *);
7355typedef char_u * (*INTPROCSTR)(int);
7356typedef int (*STRPROCINT)(char_u *);
7357typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358
7359/*
7360 * Call a DLL routine which takes either a string or int param
7361 * and returns an allocated string.
7362 */
7363 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007364mch_libcall(
7365 char_u *libname,
7366 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007367 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007368 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007369 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007370 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371{
7372# if defined(USE_DLOPEN)
7373 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007374 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375# else
7376 shl_t hinstLib;
7377# endif
7378 STRPROCSTR ProcAdd;
7379 INTPROCSTR ProcAddI;
7380 char_u *retval_str = NULL;
7381 int retval_int = 0;
7382 int success = FALSE;
7383
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007384 /*
7385 * Get a handle to the DLL module.
7386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007388 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007389 (void)dlerror();
7390
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 hinstLib = dlopen((char *)libname, RTLD_LAZY
7392# ifdef RTLD_LOCAL
7393 | RTLD_LOCAL
7394# endif
7395 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007396 if (hinstLib == NULL)
7397 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007398 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007399 dlerr = (char *)dlerror();
7400 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007401 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403# else
7404 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7405# endif
7406
Bram Moolenaar0f873732019-12-05 20:28:46 +01007407 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 if (hinstLib != NULL)
7409 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007410# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 /*
7412 * Catch a crash when calling the library function. For example when
7413 * using a number where a string pointer is expected.
7414 */
7415 mch_startjmp();
7416 if (SETJMP(lc_jump_env) != 0)
7417 {
7418 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007419# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007420 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 mch_didjmp();
7423 }
7424 else
7425# endif
7426 {
7427 retval_str = NULL;
7428 retval_int = 0;
7429
7430 if (argstring != NULL)
7431 {
7432# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007433 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007434 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435# else
7436 if (shl_findsym(&hinstLib, (const char *)funcname,
7437 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7438 ProcAdd = NULL;
7439# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007440 if ((success = (ProcAdd != NULL
7441# if defined(USE_DLOPEN)
7442 && dlerr == NULL
7443# endif
7444 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 {
7446 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007447 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 else
7449 retval_str = (ProcAdd)(argstring);
7450 }
7451 }
7452 else
7453 {
7454# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007455 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007456 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457# else
7458 if (shl_findsym(&hinstLib, (const char *)funcname,
7459 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7460 ProcAddI = NULL;
7461# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007462 if ((success = (ProcAddI != NULL
7463# if defined(USE_DLOPEN)
7464 && dlerr == NULL
7465# endif
7466 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 {
7468 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007469 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 else
7471 retval_str = (ProcAddI)(argint);
7472 }
7473 }
7474
Bram Moolenaar0f873732019-12-05 20:28:46 +01007475 // Save the string before we free the library.
7476 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 if (string_result == NULL)
7478 *number_result = retval_int;
7479 else if (retval_str != NULL
7480 && retval_str != (char_u *)1
7481 && retval_str != (char_u *)-1)
7482 *string_result = vim_strsave(retval_str);
7483 }
7484
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007485# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 mch_endjmp();
7487# ifdef SIGHASARG
7488 if (lc_signal != 0)
7489 {
7490 int i;
7491
Bram Moolenaar0f873732019-12-05 20:28:46 +01007492 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 for (i = 0; signal_info[i].sig != -1; i++)
7494 if (lc_signal == signal_info[i].sig)
7495 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007496 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 }
7498# endif
7499# endif
7500
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007502 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007503 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007504 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007505
Bram Moolenaar0f873732019-12-05 20:28:46 +01007506 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 (void)dlclose(hinstLib);
7508# else
7509 (void)shl_unload(hinstLib);
7510# endif
7511 }
7512
7513 if (!success)
7514 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007515 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 return FAIL;
7517 }
7518
7519 return OK;
7520}
7521#endif
7522
7523#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007524static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525static int xterm_button;
7526
7527/*
7528 * Setup a dummy window for X selections in a terminal.
7529 */
7530 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007531setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532{
7533 int z = 0;
7534 char *strp = "";
7535 Widget AppShell;
7536
7537 if (!x_connect_to_server())
7538 return;
7539
7540 open_app_context();
7541 if (app_context != NULL && xterm_Shell == (Widget)0)
7542 {
7543 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007544# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007546# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007547# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007548 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549
7550 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007551 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552# endif
7553
Bram Moolenaar0f873732019-12-05 20:28:46 +01007554 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 oldhandler = XSetErrorHandler(x_error_check);
7556
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007557# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007558 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7560 mch_startjmp();
7561 if (SETJMP(lc_jump_env) != 0)
7562 {
7563 mch_didjmp();
7564 xterm_dpy = NULL;
7565 }
7566 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 {
7569 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7570 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007571 if (xterm_dpy != NULL)
7572 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007573# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007575# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 }
7577
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007578# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007579 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007581# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007582 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 (void)XSetErrorHandler(oldhandler);
7584
7585 if (xterm_dpy == NULL)
7586 {
7587 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007588 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 return;
7590 }
7591
Bram Moolenaar0f873732019-12-05 20:28:46 +01007592 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 (void)XSetIOErrorHandler(x_IOerror_handler);
7594
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007595# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007597 {
7598 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007599 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007600 verbose_leave();
7601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602# endif
7603
Bram Moolenaar0f873732019-12-05 20:28:46 +01007604 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7606 applicationShellWidgetClass, xterm_dpy,
7607 NULL);
7608 if (AppShell == (Widget)0)
7609 return;
7610 xterm_Shell = XtVaCreatePopupShell("VIM",
7611 topLevelShellWidgetClass, AppShell,
7612 XtNmappedWhenManaged, 0,
7613 XtNwidth, 1,
7614 XtNheight, 1,
7615 NULL);
7616 if (xterm_Shell == (Widget)0)
7617 return;
7618
7619 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007620 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 if (x11_display == NULL)
7622 x11_display = xterm_dpy;
7623
7624 XtRealizeWidget(xterm_Shell);
7625 XSync(xterm_dpy, False);
7626 xterm_update();
7627 }
7628 if (xterm_Shell != (Widget)0)
7629 {
7630 clip_init(TRUE);
7631 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7632 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007633 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 if (test_x11_window(xterm_dpy) == FAIL)
7635 x11_window = 0;
7636 if (x11_window != 0)
7637 xterm_trace = 0;
7638 }
7639}
7640
7641 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007642start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
7644 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7645 return;
7646 xterm_trace = 1;
7647 xterm_button = button;
7648 do_xterm_trace();
7649}
7650
7651
7652 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007653stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654{
7655 if (xterm_trace < 0)
7656 return;
7657 xterm_trace = 0;
7658}
7659
7660/*
7661 * Query the xterm pointer and generate mouse termcodes if necessary
7662 * return TRUE if dragging is active, else FALSE
7663 */
7664 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007665do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666{
7667 Window root, child;
7668 int root_x, root_y;
7669 int win_x, win_y;
7670 int row, col;
7671 int_u mask_return;
7672 char_u buf[50];
7673 char_u *strp;
7674 long got_hints;
7675 static char_u *mouse_code;
7676 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7677 static int prev_row = 0, prev_col = 0;
7678 static XSizeHints xterm_hints;
7679
7680 if (xterm_trace <= 0)
7681 return FALSE;
7682
7683 if (xterm_trace == 1)
7684 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007685 // Get the hints just before tracking starts. The font size might
7686 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007687 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7688 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 || xterm_hints.width_inc <= 1
7690 || xterm_hints.height_inc <= 1)
7691 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007692 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 return FALSE;
7694 }
7695
Bram Moolenaar0f873732019-12-05 20:28:46 +01007696 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 mouse_code = find_termcode(mouse_name);
7698 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007699 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 xterm_trace = 2;
7701
Bram Moolenaar0f873732019-12-05 20:28:46 +01007702 // Find the offset of the chars, there might be a scrollbar on the
7703 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7705 &win_x, &win_y, &mask_return);
7706 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7707 - (xterm_hints.height_inc / 2);
7708 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7709 xterm_hints.y = 2;
7710 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7711 - (xterm_hints.width_inc / 2);
7712 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7713 xterm_hints.x = 2;
7714 return TRUE;
7715 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007716 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 {
7718 xterm_trace = 0;
7719 return FALSE;
7720 }
7721
7722 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7723 &win_x, &win_y, &mask_return);
7724
7725 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7726 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7727 if (row == prev_row && col == prev_col)
7728 return TRUE;
7729
7730 STRCPY(buf, mouse_code);
7731 strp = buf + STRLEN(buf);
7732 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7733 *strp++ = (char_u)(col + ' ' + 1);
7734 *strp++ = (char_u)(row + ' ' + 1);
7735 *strp = 0;
7736 add_to_input_buf(buf, STRLEN(buf));
7737
7738 prev_row = row;
7739 prev_col = col;
7740 return TRUE;
7741}
7742
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007743# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744/*
7745 * Destroy the display, window and app_context. Required for GTK.
7746 */
7747 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007748clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749{
7750 if (xterm_Shell != (Widget)0)
7751 {
7752 XtDestroyWidget(xterm_Shell);
7753 xterm_Shell = (Widget)0;
7754 }
7755 if (xterm_dpy != NULL)
7756 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007757# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007758 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007760# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 if (x11_display == xterm_dpy)
7762 x11_display = NULL;
7763 xterm_dpy = NULL;
7764 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007765# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 if (app_context != (XtAppContext)NULL)
7767 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007768 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 XtDestroyApplicationContext(app_context);
7770 app_context = (XtAppContext)NULL;
7771 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007772# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773}
7774# endif
7775
7776/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007777 * Catch up with GUI or X events.
7778 */
7779 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007780clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007781{
7782# ifdef FEAT_GUI
7783 if (gui.in_use)
7784 gui_mch_update();
7785 else
7786# endif
7787 if (xterm_Shell != (Widget)0)
7788 xterm_update();
7789}
7790
7791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 * Catch up with any queued X events. This may put keyboard input into the
7793 * input buffer, call resize call-backs, trigger timers etc. If there is
7794 * nothing in the X event queue (& no timers pending), then we return
7795 * immediately.
7796 */
7797 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007798xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799{
7800 XEvent event;
7801
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007802 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007804 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007806 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007807 break;
7808
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007809 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007810 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007811 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007812 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007813#ifdef FEAT_CLIENTSERVER
7814 {
7815 XPropertyEvent *e = (XPropertyEvent *)&event;
7816
7817 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007819 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007822 XtDispatchEvent(&event);
7823 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007824 else
7825 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007826 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007827 XtAppProcessEvent(app_context, mask);
7828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
7830}
7831
7832 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007833clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834{
7835 if (xterm_Shell != (Widget)0)
7836 return clip_x11_own_selection(xterm_Shell, cbd);
7837 return FAIL;
7838}
7839
7840 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007841clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
7843 if (xterm_Shell != (Widget)0)
7844 clip_x11_lose_selection(xterm_Shell, cbd);
7845}
7846
7847 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007848clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
7850 if (xterm_Shell != (Widget)0)
7851 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7852}
7853
7854 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007855clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
7857 clip_x11_set_selection(cbd);
7858}
7859#endif
7860
7861
7862#if defined(USE_XSMP) || defined(PROTO)
7863/*
7864 * Code for X Session Management Protocol.
7865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866
7867# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868/*
7869 * This is our chance to ask the user if they want to save,
7870 * or abort the logout
7871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007873xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
Bram Moolenaare1004402020-10-24 20:49:43 +02007875 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 int cancel_shutdown = False;
7877
Bram Moolenaare1004402020-10-24 20:49:43 +02007878 save_cmod_flags = cmdmod.cmod_flags;
7879 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007880 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007881 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02007883 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007884 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 out_flush();
7886
Bram Moolenaar0f873732019-12-05 20:28:46 +01007887 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 SmcInteractDone(smc_conn, cancel_shutdown);
7889
Bram Moolenaar0f873732019-12-05 20:28:46 +01007890 // Finish off
7891 // Only end save-yourself here if we're not cancelling shutdown;
7892 // we'll get a cancelled callback later in which we'll end it.
7893 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 if (!cancel_shutdown)
7895 {
7896 xsmp.save_yourself = False;
7897 SmcSaveYourselfDone(smc_conn, True);
7898 }
7899}
7900# endif
7901
7902/*
7903 * Callback that starts save-yourself.
7904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007906xsmp_handle_save_yourself(
7907 SmcConn smc_conn,
7908 SmPointer client_data UNUSED,
7909 int save_type UNUSED,
7910 Bool shutdown,
7911 int interact_style UNUSED,
7912 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007914 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 if (xsmp.save_yourself)
7916 SmcSaveYourselfDone(smc_conn, True);
7917 xsmp.save_yourself = True;
7918 xsmp.shutdown = shutdown;
7919
Bram Moolenaar0f873732019-12-05 20:28:46 +01007920 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007922 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
7924 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007925 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926
7927# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007928 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007930 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 SmcInteractRequest(smc_conn, SmDialogError,
7932 xsmp_handle_interaction, client_data);
7933 else
7934# endif
7935 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007936 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 SmcSaveYourselfDone(smc_conn, True);
7938 xsmp.save_yourself = False;
7939 }
7940}
7941
7942
7943/*
7944 * Callback to warn us of imminent death.
7945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007947xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948{
7949 xsmp_close();
7950
Bram Moolenaar0f873732019-12-05 20:28:46 +01007951 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 getout_preserve_modified(0);
7953}
7954
7955
7956/*
7957 * Callback to tell us that save-yourself has completed.
7958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007960xsmp_save_complete(
7961 SmcConn smc_conn UNUSED,
7962 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963{
7964 xsmp.save_yourself = False;
7965}
7966
7967
7968/*
7969 * Callback to tell us that an instigated shutdown was cancelled
7970 * (maybe even by us)
7971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007973xsmp_shutdown_cancelled(
7974 SmcConn smc_conn,
7975 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 if (xsmp.save_yourself)
7978 SmcSaveYourselfDone(smc_conn, True);
7979 xsmp.save_yourself = False;
7980 xsmp.shutdown = False;
7981}
7982
7983
7984/*
7985 * Callback to tell us that a new ICE connection has been established.
7986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007988xsmp_ice_connection(
7989 IceConn iceConn,
7990 IcePointer clientData UNUSED,
7991 Bool opening,
7992 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007994 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 if (opening)
7996 {
7997 xsmp_icefd = IceConnectionNumber(iceConn);
7998 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7999 }
8000}
8001
8002
Bram Moolenaar0f873732019-12-05 20:28:46 +01008003// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008005xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006{
8007 Bool rep;
8008
8009 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8010 == IceProcessMessagesIOError)
8011 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008012 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008014 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 xsmp_close();
8016 return FAIL;
8017 }
8018 else
8019 return OK;
8020}
8021
8022static int dummy;
8023
Bram Moolenaar0f873732019-12-05 20:28:46 +01008024// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 void
8026xsmp_init(void)
8027{
8028 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 SmcCallbacks smcallbacks;
8030#if 0
8031 SmPropValue smname;
8032 SmProp smnameprop;
8033 SmProp *smprops[1];
8034#endif
8035
8036 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008037 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038
8039 xsmp.save_yourself = xsmp.shutdown = False;
8040
Bram Moolenaar0f873732019-12-05 20:28:46 +01008041 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8043 smcallbacks.save_yourself.client_data = NULL;
8044 smcallbacks.die.callback = xsmp_die;
8045 smcallbacks.die.client_data = NULL;
8046 smcallbacks.save_complete.callback = xsmp_save_complete;
8047 smcallbacks.save_complete.client_data = NULL;
8048 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8049 smcallbacks.shutdown_cancelled.client_data = NULL;
8050
Bram Moolenaar0f873732019-12-05 20:28:46 +01008051 // Set up a watch on ICE connection creations. The "dummy" argument is
8052 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8054 {
8055 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008056 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 return;
8058 }
8059
Bram Moolenaar0f873732019-12-05 20:28:46 +01008060 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 xsmp.smcconn = SmcOpenConnection(
8062 NULL,
8063 NULL,
8064 SmProtoMajor,
8065 SmProtoMinor,
8066 SmcSaveYourselfProcMask | SmcDieProcMask
8067 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8068 &smcallbacks,
8069 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008070 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008071 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 errorstring);
8073 if (xsmp.smcconn == NULL)
8074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008076 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008077 char errorreport[132];
8078
8079 // If the message is too long it might not be NUL terminated. Add
8080 // a NUL at the end to make sure we don't go over the end.
8081 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008082 vim_snprintf(errorreport, sizeof(errorreport),
8083 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008084 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 return;
8087 }
8088 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8089
8090#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008091 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 smname.value = "vim";
8093 smname.length = 3;
8094 smnameprop.name = "SmProgram";
8095 smnameprop.type = "SmARRAY8";
8096 smnameprop.num_vals = 1;
8097 smnameprop.vals = &smname;
8098
8099 smprops[0] = &smnameprop;
8100 SmcSetProperties(xsmp.smcconn, 1, smprops);
8101#endif
8102}
8103
8104
Bram Moolenaar0f873732019-12-05 20:28:46 +01008105// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008107xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108{
8109 if (xsmp_icefd != -1)
8110 {
8111 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008112 if (xsmp.clientid != NULL)
8113 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008114 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 xsmp_icefd = -1;
8116 }
8117}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008118#endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119
8120
8121#ifdef EBCDIC
Bram Moolenaar0f873732019-12-05 20:28:46 +01008122// Translate character to its CTRL- value
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123char CtrlTable[] =
8124{
8125/* 00 - 5E */
8126 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8130 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8131 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8132/* ^ */ 0x1E,
8133/* - */ 0x1F,
8134/* 61 - 6C */
8135 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8136/* _ */ 0x1F,
8137/* 6E - 80 */
8138 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8139/* a */ 0x01,
8140/* b */ 0x02,
8141/* c */ 0x03,
8142/* d */ 0x37,
8143/* e */ 0x2D,
8144/* f */ 0x2E,
8145/* g */ 0x2F,
8146/* h */ 0x16,
8147/* i */ 0x05,
8148/* 8A - 90 */
8149 0, 0, 0, 0, 0, 0, 0,
8150/* j */ 0x15,
8151/* k */ 0x0B,
8152/* l */ 0x0C,
8153/* m */ 0x0D,
8154/* n */ 0x0E,
8155/* o */ 0x0F,
8156/* p */ 0x10,
8157/* q */ 0x11,
8158/* r */ 0x12,
8159/* 9A - A1 */
8160 0, 0, 0, 0, 0, 0, 0, 0,
8161/* s */ 0x13,
8162/* t */ 0x3C,
8163/* u */ 0x3D,
8164/* v */ 0x32,
8165/* w */ 0x26,
8166/* x */ 0x18,
8167/* y */ 0x19,
8168/* z */ 0x3F,
8169/* AA - AC */
8170 0, 0, 0,
8171/* [ */ 0x27,
8172/* AE - BC */
8173 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8174/* ] */ 0x1D,
8175/* BE - C0 */ 0, 0, 0,
8176/* A */ 0x01,
8177/* B */ 0x02,
8178/* C */ 0x03,
8179/* D */ 0x37,
8180/* E */ 0x2D,
8181/* F */ 0x2E,
8182/* G */ 0x2F,
8183/* H */ 0x16,
8184/* I */ 0x05,
8185/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8186/* J */ 0x15,
8187/* K */ 0x0B,
8188/* L */ 0x0C,
8189/* M */ 0x0D,
8190/* N */ 0x0E,
8191/* O */ 0x0F,
8192/* P */ 0x10,
8193/* Q */ 0x11,
8194/* R */ 0x12,
8195/* DA - DF */ 0, 0, 0, 0, 0, 0,
8196/* \ */ 0x1C,
8197/* E1 */ 0,
8198/* S */ 0x13,
8199/* T */ 0x3C,
8200/* U */ 0x3D,
8201/* V */ 0x32,
8202/* W */ 0x26,
8203/* X */ 0x18,
8204/* Y */ 0x19,
8205/* Z */ 0x3F,
8206/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8207 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8208};
8209
8210char MetaCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008211{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8213 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8214 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8215 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8216};
8217
8218
Bram Moolenaar0f873732019-12-05 20:28:46 +01008219// TODO: Use characters NOT numbers!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220char CtrlCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008221{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8223 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8224 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8225 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8226};
8227
8228
8229#endif