blob: 0f61367f2a107545dfb7b9f8d35764897900cfb7 [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
580 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100581mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200583 tmode_T old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000584#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100585 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
588 if (ignoreinput)
589 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100590 // Go to cooked mode without echo, to allow SIGINT interrupting us
591 // here. But we don't want QUIT to kill us (CTRL-\ used in a
592 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200593 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000594 in_mch_delay = TRUE;
Bram Moolenaar26e86442020-05-17 14:06:16 +0200595 old_tmode = mch_cur_tmode;
596 if (mch_cur_tmode == TMODE_RAW && msec > 500)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 settmode(TMODE_SLEEP);
598
599 /*
600 * Everybody sleeps in a different way...
601 * Prefer nanosleep(), some versions of usleep() can only sleep up to
602 * one second.
603 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000604#ifdef FEAT_MZSCHEME
605 do
606 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100607 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000608 if (total > p_mzq)
609 msec = p_mzq;
610 else
611 msec = total;
612 total -= msec;
613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#ifdef HAVE_NANOSLEEP
615 {
616 struct timespec ts;
617
618 ts.tv_sec = msec / 1000;
619 ts.tv_nsec = (msec % 1000) * 1000000;
620 (void)nanosleep(&ts, NULL);
621 }
622#else
623# ifdef HAVE_USLEEP
624 while (msec >= 1000)
625 {
626 usleep((unsigned int)(999 * 1000));
627 msec -= 999;
628 }
629 usleep((unsigned int)(msec * 1000));
630# else
631# ifndef HAVE_SELECT
632 poll(NULL, 0, (int)msec);
633# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 {
635 struct timeval tv;
636
637 tv.tv_sec = msec / 1000;
638 tv.tv_usec = (msec % 1000) * 1000;
639 /*
640 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
641 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
642 */
643 select(0, NULL, NULL, NULL, &tv);
644 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100645# endif // HAVE_SELECT
646# endif // HAVE_NANOSLEEP
647#endif // HAVE_USLEEP
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000648#ifdef FEAT_MZSCHEME
649 }
650 while (total > 0);
651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
Bram Moolenaar26e86442020-05-17 14:06:16 +0200653 if (msec > 500)
654 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000655 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 }
657 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200658 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659}
660
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000661#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
663# define HAVE_CHECK_STACK_GROWTH
664/*
665 * Support for checking for an almost-out-of-stack-space situation.
666 */
667
668/*
669 * Return a pointer to an item on the stack. Used to find out if the stack
670 * grows up or down.
671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672static int stack_grows_downwards;
673
674/*
675 * Find out if the stack grows upwards or downwards.
676 * "p" points to a variable on the stack of the caller.
677 */
678 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100679check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680{
681 int i;
682
683 stack_grows_downwards = (p > (char *)&i);
684}
685#endif
686
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000687#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688static char *stack_limit = NULL;
689
690#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
691# include <pthread.h>
692# include <pthread_np.h>
693#endif
694
695/*
696 * Find out until how var the stack can grow without getting into trouble.
697 * Called when starting up and when switching to the signal stack in
698 * deathtrap().
699 */
700 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100701get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702{
703 struct rlimit rlp;
704 int i;
705 long lim;
706
Bram Moolenaar0f873732019-12-05 20:28:46 +0100707 // Set the stack limit to 15/16 of the allowable size. Skip this when the
708 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 if (getrlimit(RLIMIT_STACK, &rlp) == 0
710 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
711# ifdef RLIM_INFINITY
712 && rlp.rlim_cur != RLIM_INFINITY
713# endif
714 )
715 {
716 lim = (long)rlp.rlim_cur;
717#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
718 {
719 pthread_attr_t attr;
720 size_t size;
721
Bram Moolenaar0f873732019-12-05 20:28:46 +0100722 // On FreeBSD the initial thread always has a fixed stack size, no
723 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 pthread_attr_init(&attr);
725 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
726 {
727 pthread_attr_getstacksize(&attr, &size);
728 if (lim > (long)size)
729 lim = (long)size;
730 }
731 pthread_attr_destroy(&attr);
732 }
733#endif
734 if (stack_grows_downwards)
735 {
736 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
737 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100738 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 stack_limit = (char *)((long)&i / 16L);
740 }
741 else
742 {
743 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
744 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100745 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 }
747 }
748}
749
750/*
751 * Return FAIL when running out of stack space.
752 * "p" must point to any variable local to the caller that's on the stack.
753 */
754 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100755mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756{
757 if (stack_limit != NULL)
758 {
759 if (stack_grows_downwards)
760 {
761 if (p < stack_limit)
762 return FAIL;
763 }
764 else if (p > stack_limit)
765 return FAIL;
766 }
767 return OK;
768}
769#endif
770
771#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
772/*
773 * Support for using the signal stack.
774 * This helps when we run out of stack space, which causes a SIGSEGV. The
775 * signal handler then must run on another stack, since the normal stack is
776 * completely full.
777 */
778
779#ifndef SIGSTKSZ
Bram Moolenaar0f873732019-12-05 20:28:46 +0100780# define SIGSTKSZ 8000 // just a guess of how much stack is needed...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781#endif
782
783# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100784static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100786static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787# endif
788
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789static char *signal_stack;
790
791 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100792init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794 if (signal_stack != NULL)
795 {
796# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797# ifdef HAVE_SS_BASE
798 sigstk.ss_base = signal_stack;
799# else
800 sigstk.ss_sp = signal_stack;
801# endif
802 sigstk.ss_size = SIGSTKSZ;
803 sigstk.ss_flags = 0;
804 (void)sigaltstack(&sigstk, NULL);
805# else
806 sigstk.ss_sp = signal_stack;
807 if (stack_grows_downwards)
808 sigstk.ss_sp += SIGSTKSZ - 1;
809 sigstk.ss_onstack = 0;
810 (void)sigstack(&sigstk, NULL);
811# endif
812 }
813}
814#endif
815
816/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000817 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 * will barf when the second argument to signal() is ``wrong''.
819 * Let me try it with a few tricky defines from my own osdef.h (jw).
820 */
821#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 static RETSIGTYPE
823sig_winch SIGDEFARG(sigarg)
824{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100825 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
827 do_resize = TRUE;
828 SIGRETURN;
829}
830#endif
831
832#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 static RETSIGTYPE
834catch_sigint SIGDEFARG(sigarg)
835{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100836 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
838 got_int = TRUE;
839 SIGRETURN;
840}
841#endif
842
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200843#if defined(SIGUSR1)
844 static RETSIGTYPE
845catch_sigusr1 SIGDEFARG(sigarg)
846{
847 // this is not required on all systems, but it doesn't hurt anybody
848 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
849 got_sigusr1 = TRUE;
850 SIGRETURN;
851}
852#endif
853
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 static RETSIGTYPE
856catch_sigpwr SIGDEFARG(sigarg)
857{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100858 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000859 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 /*
861 * I'm not sure we get the SIGPWR signal when the system is really going
862 * down or when the batteries are almost empty. Just preserve the swap
863 * files and don't exit, that can't do any harm.
864 */
865 ml_sync_all(FALSE, FALSE);
866 SIGRETURN;
867}
868#endif
869
870#ifdef SET_SIG_ALARM
871/*
872 * signal function for alarm().
873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 static RETSIGTYPE
875sig_alarm SIGDEFARG(sigarg)
876{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100877 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 sig_alarm_called = TRUE;
879 SIGRETURN;
880}
881#endif
882
Bram Moolenaaredce7422019-01-20 18:39:30 +0100883#if (defined(HAVE_SETJMP_H) \
884 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
885 || defined(FEAT_LIBCALL))) \
886 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100887# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100888
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100889// argument to SETJMP()
890static JMP_BUF lc_jump_env;
891
892# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100893// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100894// Volatile because it is used in signal handlers.
895static volatile sig_atomic_t lc_signal;
896# endif
897
898// TRUE when lc_jump_env is valid.
899// Volatile because it is used in signal handler deathtrap().
900static volatile sig_atomic_t lc_active INIT(= FALSE);
901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902/*
903 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100904 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100906 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 * Usage:
908 * mch_startjmp();
909 * if (SETJMP(lc_jump_env) != 0)
910 * {
911 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100912 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 * }
914 * else
915 * {
916 * do_the_work;
917 * mch_endjmp();
918 * }
919 * Note: Can't move SETJMP() here, because a function calling setjmp() must
920 * not return before the saved environment is used.
921 * Returns OK for normal return, FAIL when the protected code caused a
922 * problem and LONGJMP() was used.
923 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100924 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100925mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926{
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100927# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 lc_active = TRUE;
931}
932
Bram Moolenaar113e1072019-01-20 15:30:40 +0100933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100934mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
936 lc_active = FALSE;
937}
938
Bram Moolenaar113e1072019-01-20 15:30:40 +0100939 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100940mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941{
942# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100943 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
944 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 init_signal_stack();
946# endif
947}
948#endif
949
950/*
951 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200952 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200954 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
955 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 */
957 static RETSIGTYPE
958deathtrap SIGDEFARG(sigarg)
959{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100960 static int entered = 0; // count the number of times we got here.
961 // Note: when memory has been corrupted
962 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963#ifdef SIGHASARG
964 int i;
965#endif
966
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100967#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 /*
969 * Catch a crash in protected code.
970 * Restores the environment saved in lc_jump_env, which looks like
971 * SETJMP() returns 1.
972 */
973 if (lc_active)
974 {
975# if defined(SIGHASARG)
976 lc_signal = sigarg;
977# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100978 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100980 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 }
982#endif
983
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000984#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000985# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100986 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
987 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
988 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000989 if (in_mch_delay && sigarg == SIGQUIT)
990 SIGRETURN;
991# endif
992
Bram Moolenaar0f873732019-12-05 20:28:46 +0100993 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
994 // here. This avoids that a non-reentrant function is interrupted, e.g.,
995 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000996 if (entered == 0
997 && (0
998# ifdef SIGHUP
999 || sigarg == SIGHUP
1000# endif
1001# ifdef SIGQUIT
1002 || sigarg == SIGQUIT
1003# endif
1004# ifdef SIGTERM
1005 || sigarg == SIGTERM
1006# endif
1007# ifdef SIGPWR
1008 || sigarg == SIGPWR
1009# endif
1010# ifdef SIGUSR1
1011 || sigarg == SIGUSR1
1012# endif
1013# ifdef SIGUSR2
1014 || sigarg == SIGUSR2
1015# endif
1016 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001017 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001018 SIGRETURN;
1019#endif
1020
Bram Moolenaar0f873732019-12-05 20:28:46 +01001021 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 ++entered;
1023
Bram Moolenaar0f873732019-12-05 20:28:46 +01001024 // Executing autocommands is likely to use more stack space than we have
1025 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001026 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001029 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 set_vim_var_nr(VV_DYING, (long)entered);
1031#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001032 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001034#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001035 // Since we are now using the signal stack, need to reset the stack
1036 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 get_stack_limit();
1038#endif
1039
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001040#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001041 // This is for opening gdb the moment Vim crashes.
1042 // You need to manually adjust the file name and Vim executable name.
1043 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001044 {
1045# define VI_GDB_FILE "/tmp/vimgdb"
1046# define VIM_NAME "/usr/bin/vim"
1047 FILE *fp = fopen(VI_GDB_FILE, "w");
1048 if (fp)
1049 {
1050 fprintf(fp,
1051 "file %s\n"
1052 "attach %d\n"
1053 "set height 1000\n"
1054 "bt full\n"
1055 , VIM_NAME, getpid());
1056 fclose(fp);
1057 system("xterm -e gdb -x "VI_GDB_FILE);
1058 unlink(VI_GDB_FILE);
1059 }
1060 }
1061#endif
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001064 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 for (i = 0; signal_info[i].sig != -1; i++)
1066 if (sigarg == signal_info[i].sig)
1067 break;
1068 deadly_signal = sigarg;
1069#endif
1070
Bram Moolenaar0f873732019-12-05 20:28:46 +01001071 full_screen = FALSE; // don't write message to the GUI, it might be
1072 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 /*
1074 * If something goes wrong after entering here, we may get here again.
1075 * When this happens, give a message and try to exit nicely (resetting the
1076 * terminal mode, etc.)
1077 * When this happens twice, just exit, don't even try to give a message,
1078 * stack may be corrupt or something weird.
1079 * When this still happens again (or memory was corrupted in such a way
1080 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1081 */
1082 if (entered >= 3)
1083 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001084 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 may_core_dump();
1086 if (entered >= 4)
1087 _exit(8);
1088 exit(7);
1089 }
1090 if (entered == 2)
1091 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001092 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001093 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 out_flush();
1095 getout(1);
1096 }
1097
Bram Moolenaar0f873732019-12-05 20:28:46 +01001098 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001100 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 signal_info[i].name);
1102#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001103 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001105
Bram Moolenaar0f873732019-12-05 20:28:46 +01001106 // Preserve files and exit. This sets the really_exiting flag to prevent
1107 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001108 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109
Bram Moolenaar0f873732019-12-05 20:28:46 +01001110 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001111
Bram Moolenaar009b2592004-10-24 19:18:58 +00001112#ifdef NBDEBUG
1113 reset_signals();
1114 may_core_dump();
1115 abort();
1116#endif
1117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 SIGRETURN;
1119}
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001122 * Invoked after receiving SIGCONT. We don't know what happened while
1123 * sleeping, deal with part of that.
1124 */
1125 static void
1126after_sigcont(void)
1127{
1128# ifdef FEAT_TITLE
1129 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1130 // again later.
1131 oldtitle_outdated = TRUE;
1132# endif
1133 settmode(TMODE_RAW);
1134 need_check_timestamps = TRUE;
1135 did_check_timestamps = FALSE;
1136}
1137
1138#if defined(SIGCONT)
1139static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001140static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001141
1142/*
1143 * With multi-threading, suspending might not work immediately. Catch the
1144 * SIGCONT signal, which will be used as an indication whether the suspending
1145 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001146 *
1147 * On Linux, signal is not always handled immediately either.
1148 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001149 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001150 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001151 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001153static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001154static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155
1156/*
1157 * signal handler for SIGCONT
1158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 static RETSIGTYPE
1160sigcont_handler SIGDEFARG(sigarg)
1161{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001162 if (in_mch_suspend)
1163 {
1164 sigcont_received = TRUE;
1165 }
1166 else
1167 {
1168 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1169 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1170 // back to a sane mode. We should redraw, but we can't really do that
1171 // in a signal handler, do a redraw later.
1172 after_sigcont();
1173 redraw_later(CLEAR);
1174 cursor_on_force();
1175 out_flush();
1176 }
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 SIGRETURN;
1179}
1180#endif
1181
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001182#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001183# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001184static void *clip_star_save = NULL;
1185static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001186# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001187
1188/*
1189 * Called when Vim is going to sleep or execute a shell command.
1190 * We can't respond to requests for the X selections. Lose them, otherwise
1191 * other applications will hang. But first copy the text to cut buffer 0.
1192 */
1193 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001194loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001195{
1196 if (clip_star.owned || clip_plus.owned)
1197 {
1198 x11_export_final_selection();
1199 if (clip_star.owned)
1200 clip_lose_selection(&clip_star);
1201 if (clip_plus.owned)
1202 clip_lose_selection(&clip_plus);
1203 if (x11_display != NULL)
1204 XFlush(x11_display);
1205 }
1206}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001207
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001208# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001209/*
1210 * Save clipboard text to restore later.
1211 */
1212 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001213save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001214{
1215 if (clip_star.owned)
1216 clip_star_save = get_register('*', TRUE);
1217 if (clip_plus.owned)
1218 clip_plus_save = get_register('+', TRUE);
1219}
1220
1221/*
1222 * Restore clipboard text if no one own the X selection.
1223 */
1224 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001225restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001226{
1227 if (clip_star_save != NULL)
1228 {
1229 if (!clip_gen_owner_exists(&clip_star))
1230 put_register('*', clip_star_save);
1231 else
1232 free_register(clip_star_save);
1233 clip_star_save = NULL;
1234 }
1235 if (clip_plus_save != NULL)
1236 {
1237 if (!clip_gen_owner_exists(&clip_plus))
1238 put_register('+', clip_plus_save);
1239 else
1240 free_register(clip_plus_save);
1241 clip_plus_save = NULL;
1242 }
1243}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001244# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001245#endif
1246
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247/*
1248 * If the machine has job control, use it to suspend the program,
1249 * otherwise fake it by starting a new shell.
1250 */
1251 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001252mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001254 if (ignore_sigtstp)
1255 return;
1256
Bram Moolenaar041c7102020-05-30 18:14:57 +02001257#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001258 in_mch_suspend = TRUE;
1259
Bram Moolenaar0f873732019-12-05 20:28:46 +01001260 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001262 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001265 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001267# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 sigcont_received = FALSE;
1269# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001270
Bram Moolenaar0f873732019-12-05 20:28:46 +01001271 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001272
1273# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001274 /*
1275 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001276 * immediately, but somehow not all the time, probably because it's handled
1277 * in another thread. Do not call pause() because there would be race
1278 * condition which would hang Vim if signal happened in between the test of
1279 * sigcont_received and the call to pause(). If signal is not yet received,
1280 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1281 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001282 */
1283 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001284 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001285
Bram Moolenaar262735e2009-07-14 10:20:22 +00001286 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar262735e2009-07-14 10:20:22 +00001287 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001290 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
Bram Moolenaar2e310482018-08-21 13:09:10 +02001292 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293#else
1294 suspend_shell();
1295#endif
1296}
1297
1298 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001299mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 Columns = 80;
1302 Rows = 24;
1303
1304 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001305
1306#ifdef SIGTSTP
1307 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1308 // that indicates the shell (or program) that launched us does not support
1309 // tty job control and thus we should ignore that signal. If invoked as a
1310 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
1311 ignore_sigtstp = restricted || SIG_IGN == signal(SIGTSTP, SIG_ERR);
1312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001314
Bram Moolenaar56718732006-03-15 22:53:57 +00001315#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001316 mac_conv_init();
1317#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001318#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1319 win_clip_init();
1320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321}
1322
1323 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001324set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325{
1326#if defined(SIGWINCH)
1327 /*
1328 * WINDOW CHANGE signal is handled with sig_winch().
1329 */
1330 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1331#endif
1332
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001334 // See mch_init() for the conditions under which we ignore SIGTSTP.
1335 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001337#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 signal(SIGCONT, sigcont_handler);
1339#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001340#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 /*
1342 * We want to ignore breaking of PIPEs.
1343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 signal(SIGPIPE, SIG_IGN);
1345#endif
1346
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001348 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349#endif
1350
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001351#ifdef SIGUSR1
1352 /*
1353 * Call user's handler on SIGUSR1
1354 */
1355 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
1356#endif
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 /*
1359 * Ignore alarm signals (Perl's alarm() generates it).
1360 */
1361#ifdef SIGALRM
1362 signal(SIGALRM, SIG_IGN);
1363#endif
1364
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001365#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 /*
1367 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1368 * work will be lost.
1369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1371#endif
1372
1373 /*
1374 * Arrange for other signals to gracefully shutdown Vim.
1375 */
1376 catch_signals(deathtrap, SIG_ERR);
1377
1378#if defined(FEAT_GUI) && defined(SIGHUP)
1379 /*
1380 * When the GUI is running, ignore the hangup signal.
1381 */
1382 if (gui.in_use)
1383 signal(SIGHUP, SIG_IGN);
1384#endif
1385}
1386
Bram Moolenaardf177f62005-02-22 08:39:57 +00001387#if defined(SIGINT) || defined(PROTO)
1388/*
1389 * Catch CTRL-C (only works while in Cooked mode).
1390 */
1391 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001392catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001393{
1394 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1395}
1396#endif
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001399reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400{
1401 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001402#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001403 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 signal(SIGCONT, SIG_DFL);
1405#endif
1406}
1407
1408 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001409catch_signals(
1410 RETSIGTYPE (*func_deadly)(),
1411 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
1413 int i;
1414
1415 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (signal_info[i].deadly)
1418 {
1419#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1420 struct sigaction sa;
1421
Bram Moolenaar0f873732019-12-05 20:28:46 +01001422 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 sa.sa_handler = func_deadly;
1424 sigemptyset(&sa.sa_mask);
1425# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001426 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1427 // thread handling in combination with using the alternate stack:
1428 // pthread library functions try to use the stack pointer to
1429 // identify the current thread, causing a SEGV signal, which
1430 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 sa.sa_flags = 0;
1432# else
1433 sa.sa_flags = SA_ONSTACK;
1434# endif
1435 sigaction(signal_info[i].sig, &sa, NULL);
1436#else
1437# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1438 struct sigvec sv;
1439
Bram Moolenaar0f873732019-12-05 20:28:46 +01001440 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 sv.sv_handler = func_deadly;
1442 sv.sv_mask = 0;
1443 sv.sv_flags = SV_ONSTACK;
1444 sigvec(signal_info[i].sig, &sv, NULL);
1445# else
1446 signal(signal_info[i].sig, func_deadly);
1447# endif
1448#endif
1449 }
1450 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001451 {
1452 // Deal with non-deadly signals.
1453#ifdef SIGTSTP
1454 signal(signal_info[i].sig,
1455 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1456 ? SIG_IGN : func_other);
1457#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001459#endif
1460 }
1461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462}
1463
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001464#ifdef HAVE_SIGPROCMASK
1465 static void
1466block_signals(sigset_t *set)
1467{
1468 sigset_t newset;
1469 int i;
1470
1471 sigemptyset(&newset);
1472
1473 for (i = 0; signal_info[i].sig != -1; i++)
1474 sigaddset(&newset, signal_info[i].sig);
1475
Bram Moolenaar2e310482018-08-21 13:09:10 +02001476# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001477 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001478 sigaddset(&newset, SIGCONT);
1479# endif
1480
1481 sigprocmask(SIG_BLOCK, &newset, set);
1482}
1483
1484 static void
1485unblock_signals(sigset_t *set)
1486{
1487 sigprocmask(SIG_SETMASK, set, NULL);
1488}
1489#endif
1490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001492 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001493 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1494 * return TRUE
1495 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1496 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001497 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001498 * Returns TRUE when Vim should exit.
1499 */
1500 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001501vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001502{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001503 static int got_signal = 0;
1504 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001505
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001506 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001507 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001508 case SIGNAL_BLOCK: blocked = TRUE;
1509 break;
1510
1511 case SIGNAL_UNBLOCK: blocked = FALSE;
1512 if (got_signal != 0)
1513 {
1514 kill(getpid(), got_signal);
1515 got_signal = 0;
1516 }
1517 break;
1518
1519 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001520 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001521 got_signal = sig;
1522#ifdef SIGPWR
1523 if (sig != SIGPWR)
1524#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001525 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001526 break;
1527 }
1528 return FALSE;
1529}
1530
1531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 * Check_win checks whether we have an interactive stdout.
1533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001535mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 if (isatty(1))
1538 return OK;
1539 return FAIL;
1540}
1541
1542/*
1543 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1544 */
1545 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001546mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 if (isatty(read_cmd_fd))
1549 return TRUE;
1550 return FALSE;
1551}
1552
1553#ifdef FEAT_X11
1554
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001555# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1557
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558/*
1559 * Give a message about the elapsed time for opening the X window.
1560 */
1561 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001562xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001564 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565}
1566# endif
1567#endif
1568
1569#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1570/*
1571 * A few functions shared by X11 title and clipboard code.
1572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
1574static int got_x_error = FALSE;
1575
1576/*
1577 * X Error handler, otherwise X just exits! (very rude) -- webb
1578 */
1579 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001580x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001582 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 STRCAT(IObuff, _("\nVim: Got X error\n"));
1584
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001585 // In the GUI we cannot print a message and continue, because no X calls
1586 // are allowed here (causes my system to hang). Silently continuing seems
1587 // like the best alternative. Do preserve files, in case we crash.
1588 ml_sync_all(FALSE, FALSE);
1589
1590#ifdef FEAT_GUI
1591 if (!gui.in_use)
1592#endif
1593 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
Bram Moolenaar0f873732019-12-05 20:28:46 +01001595 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596}
1597
1598/*
1599 * Another X Error handler, just used to check for errors.
1600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001602x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604 got_x_error = TRUE;
1605 return 0;
1606}
1607
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001608/*
1609 * Return TRUE when connection to the X server is desired.
1610 */
1611 static int
1612x_connect_to_server(void)
1613{
1614 // No point in connecting if we are exiting or dying.
1615 if (exiting || v_dying)
1616 return FALSE;
1617
1618#if defined(FEAT_CLIENTSERVER)
1619 if (x_force_connect)
1620 return TRUE;
1621#endif
1622 if (x_no_connect)
1623 return FALSE;
1624
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001625 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001626 if (clip_exclude_prog != NULL)
1627 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001628 // Just in case we get called recursively, return FALSE. This could
1629 // happen if vpeekc() is used while executing the prog and it causes a
1630 // related callback to be invoked.
1631 if (regprog_in_use(clip_exclude_prog))
1632 return FALSE;
1633
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001634 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1635 return FALSE;
1636 }
1637 return TRUE;
1638}
1639
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001641# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642/*
1643 * An X IO Error handler, used to catch error while opening the display.
1644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001646x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001648 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001650# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001651 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653}
1654# endif
1655
1656/*
1657 * An X IO Error handler, used to catch terminal errors.
1658 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001659static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001662x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
1664 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001665 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 x11_window = 0;
1667 x11_display = NULL;
1668 xterm_Shell = (Widget)0;
1669
Bram Moolenaar0f873732019-12-05 20:28:46 +01001670 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001672# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001673 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001676
1677/*
1678 * If the X11 connection was lost try to restore it.
1679 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001680 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001681 */
1682 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001683may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001684{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001685 // No point in restoring the connecting if we are exiting or dying.
1686 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001687 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001688 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001689
1690# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001691 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001692 if (app_context != (XtAppContext)NULL)
1693 {
1694 XtDestroyApplicationContext(app_context);
1695 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001696 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001697 }
1698# endif
1699
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001700 setup_term_clip();
1701 get_x11_title(FALSE);
1702 }
1703}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001704
1705 void
1706ex_xrestore(exarg_T *eap)
1707{
1708 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1709 {
1710 if (xterm_display_allocated)
1711 vim_free(xterm_display);
1712 xterm_display = (char *)vim_strsave(eap->arg);
1713 xterm_display_allocated = TRUE;
1714 }
1715 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001716 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001717
1718 clear_xterm_clip();
1719 x11_window = 0;
1720 xterm_dpy_retry_count = 5; // Try reconnecting five times
1721 may_restore_clipboard();
1722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
1724
1725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 * Test if "dpy" and x11_window are valid by getting the window title.
1727 * I don't actually want it yet, so there may be a simpler call to use, but
1728 * this will cause the error handler x_error_check() to be called if anything
1729 * is wrong, such as the window pointer being invalid (as can happen when the
1730 * user changes his DISPLAY, but not his WINDOWID) -- webb
1731 */
1732 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001733test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
1735 int (*old_handler)();
1736 XTextProperty text_prop;
1737
1738 old_handler = XSetErrorHandler(x_error_check);
1739 got_x_error = FALSE;
1740 if (XGetWMName(dpy, x11_window, &text_prop))
1741 XFree((void *)text_prop.value);
1742 XSync(dpy, False);
1743 (void)XSetErrorHandler(old_handler);
1744
1745 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001746 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
1748 return (got_x_error ? FAIL : OK);
1749}
1750#endif
1751
1752#ifdef FEAT_TITLE
1753
1754#ifdef FEAT_X11
1755
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001756static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
1758/*
1759 * try to get x11 window and display
1760 *
1761 * return FAIL for failure, OK otherwise
1762 */
1763 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001764get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765{
1766 char *winid;
1767 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001768#define XD_NONE 0 // x11_display not set here
1769#define XD_HERE 1 // x11_display opened here
1770#define XD_GUI 2 // x11_display used from gui.dpy
1771#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 static int x11_display_from = XD_NONE;
1773 static int did_set_error_handler = FALSE;
1774
1775 if (!did_set_error_handler)
1776 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001777 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 (void)XSetErrorHandler(x_error_handler);
1779 did_set_error_handler = TRUE;
1780 }
1781
Bram Moolenaar9372a112005-12-06 19:59:18 +00001782#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 if (gui.in_use)
1784 {
1785 /*
1786 * If the X11 display was opened here before, for the window where Vim
1787 * was started, close that one now to avoid a memory leak.
1788 */
1789 if (x11_display_from == XD_HERE && x11_display != NULL)
1790 {
1791 XCloseDisplay(x11_display);
1792 x11_display_from = XD_NONE;
1793 }
1794 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1795 {
1796 x11_display_from = XD_GUI;
1797 return OK;
1798 }
1799 x11_display = NULL;
1800 return FAIL;
1801 }
1802 else if (x11_display_from == XD_GUI)
1803 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001804 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 x11_window = 0;
1806 x11_display = NULL;
1807 x11_display_from = XD_NONE;
1808 }
1809#endif
1810
Bram Moolenaar0f873732019-12-05 20:28:46 +01001811 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 if (!x_connect_to_server())
1813 return FAIL;
1814
1815 /*
1816 * If WINDOWID not set, should try another method to find out
1817 * what the current window number is. The only code I know for
1818 * this is very complicated.
1819 * We assume that zero is invalid for WINDOWID.
1820 */
1821 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1822 x11_window = (Window)atol(winid);
1823
1824#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001825 if (xterm_dpy == x11_display)
1826 // x11_display may have been set to xterm_dpy elsewhere
1827 x11_display_from = XD_XTERM;
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 if (xterm_dpy != NULL && x11_window != 0)
1830 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001831 // We may have checked it already, but Gnome terminal can move us to
1832 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001833 if (x11_display_from != XD_XTERM)
1834 {
1835 /*
1836 * If the X11 display was opened here before, for the window where
1837 * Vim was started, close that one now to avoid a memory leak.
1838 */
1839 if (x11_display_from == XD_HERE && x11_display != NULL)
1840 XCloseDisplay(x11_display);
1841 x11_display = xterm_dpy;
1842 x11_display_from = XD_XTERM;
1843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 if (test_x11_window(x11_display) == FAIL)
1845 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001846 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 x11_window = 0;
1848 x11_display = NULL;
1849 x11_display_from = XD_NONE;
1850 return FAIL;
1851 }
1852 return OK;
1853 }
1854#endif
1855
1856 if (x11_window == 0 || x11_display == NULL)
1857 result = -1;
1858
Bram Moolenaar0f873732019-12-05 20:28:46 +01001859 if (result != -1) // Have already been here and set this
1860 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861
1862 if (x11_window != 0 && x11_display == NULL)
1863 {
1864#ifdef SET_SIG_ALARM
1865 RETSIGTYPE (*sig_save)();
1866#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001867#ifdef ELAPSED_FUNC
1868 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
1870 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001871 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872#endif
1873
1874#ifdef SET_SIG_ALARM
1875 /*
1876 * Opening the Display may hang if the DISPLAY setting is wrong, or
1877 * the network connection is bad. Set an alarm timer to get out.
1878 */
1879 sig_alarm_called = FALSE;
1880 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1881 (RETSIGTYPE (*)())sig_alarm);
1882 alarm(2);
1883#endif
1884 x11_display = XOpenDisplay(NULL);
1885
1886#ifdef SET_SIG_ALARM
1887 alarm(0);
1888 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1889 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001890 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891#endif
1892 if (x11_display != NULL)
1893 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001894# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001896 {
1897 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001898 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001899 verbose_leave();
1900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901# endif
1902 if (test_x11_window(x11_display) == FAIL)
1903 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001904 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 x11_window = 0;
1906 XCloseDisplay(x11_display);
1907 x11_display = NULL;
1908 }
1909 else
1910 x11_display_from = XD_HERE;
1911 }
1912 }
1913 if (x11_window == 0 || x11_display == NULL)
1914 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001915
1916# ifdef FEAT_EVAL
1917 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1918# endif
1919
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 return (result = OK);
1921}
1922
1923/*
1924 * Determine original x11 Window Title
1925 */
1926 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001927get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001929 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930}
1931
1932/*
1933 * Determine original x11 Window icon
1934 */
1935 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001936get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937{
1938 int retval = FALSE;
1939
1940 retval = get_x11_thing(FALSE, test_only);
1941
Bram Moolenaar0f873732019-12-05 20:28:46 +01001942 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (oldicon == NULL && !test_only)
1944 {
1945 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001946 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001948 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 }
1950
1951 return retval;
1952}
1953
1954 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001955get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01001956 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01001957 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 XTextProperty text_prop;
1960 int retval = FALSE;
1961 Status status;
1962
1963 if (get_x11_windis() == OK)
1964 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001965 // Get window/icon name if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 if (get_title)
1967 status = XGetWMName(x11_display, x11_window, &text_prop);
1968 else
1969 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1970
1971 /*
1972 * If terminal is xterm, then x11_window may be a child window of the
1973 * outer xterm window that actually contains the window/icon name, so
1974 * keep traversing up the tree until a window with a title/icon is
1975 * found.
1976 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001977 // Previously this was only done for xterm and alike. I don't see a
Bram Moolenaar0f873732019-12-05 20:28:46 +01001978 // reason why it would fail for other terminal emulators.
1979 // if (term_is_xterm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
1981 Window root;
1982 Window parent;
1983 Window win = x11_window;
1984 Window *children;
1985 unsigned int num_children;
1986
1987 while (!status || text_prop.value == NULL)
1988 {
1989 if (!XQueryTree(x11_display, win, &root, &parent, &children,
1990 &num_children))
1991 break;
1992 if (children)
1993 XFree((void *)children);
1994 if (parent == root || parent == 0)
1995 break;
1996
1997 win = parent;
1998 if (get_title)
1999 status = XGetWMName(x11_display, win, &text_prop);
2000 else
2001 status = XGetWMIconName(x11_display, win, &text_prop);
2002 }
2003 }
2004 if (status && text_prop.value != NULL)
2005 {
2006 retval = TRUE;
2007 if (!test_only)
2008 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002009 if (get_title)
2010 vim_free(oldtitle);
2011 else
2012 vim_free(oldicon);
Bram Moolenaara12a1612019-01-24 16:39:02 +01002013 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (get_title)
2016 oldtitle = vim_strsave((char_u *)text_prop.value);
2017 else
2018 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
2020 else
2021 {
2022 char **cl;
2023 Status transform_status;
2024 int n = 0;
2025
2026 transform_status = XmbTextPropertyToTextList(x11_display,
2027 &text_prop,
2028 &cl, &n);
2029 if (transform_status >= Success && n > 0 && cl[0])
2030 {
2031 if (get_title)
2032 oldtitle = vim_strsave((char_u *) cl[0]);
2033 else
2034 oldicon = vim_strsave((char_u *) cl[0]);
2035 XFreeStringList(cl);
2036 }
2037 else
2038 {
2039 if (get_title)
2040 oldtitle = vim_strsave((char_u *)text_prop.value);
2041 else
2042 oldicon = vim_strsave((char_u *)text_prop.value);
2043 }
2044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 }
2046 XFree((void *)text_prop.value);
2047 }
2048 }
2049 return retval;
2050}
2051
Bram Moolenaar0f873732019-12-05 20:28:46 +01002052// Xutf8 functions are not available on older systems. Note that on some
2053// systems X_HAVE_UTF8_STRING may be defined in a header file but
2054// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2055// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002056#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002057# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058# define USE_UTF8_STRING
2059# endif
2060#endif
2061
2062/*
2063 * Set x11 Window Title
2064 *
2065 * get_x11_windis() must be called before this and have returned OK
2066 */
2067 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002068set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002070 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2071 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2072 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073#ifdef USE_UTF8_STRING
2074 if (enc_utf8)
2075 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2076 NULL, NULL, 0, NULL, NULL, NULL);
2077 else
2078#endif
2079 {
2080#if XtSpecificationRelease >= 4
2081# ifdef FEAT_XFONTSET
2082 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2083 NULL, NULL, 0, NULL, NULL, NULL);
2084# else
2085 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002086 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087
Bram Moolenaar0f873732019-12-05 20:28:46 +01002088 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002089 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 XSetWMProperties(x11_display, x11_window, &text_prop,
2091 NULL, NULL, 0, NULL, NULL, NULL);
2092# endif
2093#else
2094 XStoreName(x11_display, x11_window, (char *)title);
2095#endif
2096 }
2097 XFlush(x11_display);
2098}
2099
2100/*
2101 * Set x11 Window icon
2102 *
2103 * get_x11_windis() must be called before this and have returned OK
2104 */
2105 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002106set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002108 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109#ifdef USE_UTF8_STRING
2110 if (enc_utf8)
2111 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2112 NULL, 0, NULL, NULL, NULL);
2113 else
2114#endif
2115 {
2116#if XtSpecificationRelease >= 4
2117# ifdef FEAT_XFONTSET
2118 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2119 NULL, 0, NULL, NULL, NULL);
2120# else
2121 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002122 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002124 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2126 NULL, 0, NULL, NULL, NULL);
2127# endif
2128#else
2129 XSetIconName(x11_display, x11_window, (char *)icon);
2130#endif
2131 }
2132 XFlush(x11_display);
2133}
2134
Bram Moolenaar0f873732019-12-05 20:28:46 +01002135#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002138get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139{
2140 return FALSE;
2141}
2142
2143 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002144get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145{
2146 if (!test_only)
2147 {
2148 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002149 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002151 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
2153 return FALSE;
2154}
2155
Bram Moolenaar0f873732019-12-05 20:28:46 +01002156#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
2158 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002159mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160{
2161 return get_x11_title(TRUE);
2162}
2163
2164 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002165mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
2167 return get_x11_icon(TRUE);
2168}
2169
2170/*
2171 * Set the window title and icon.
2172 */
2173 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002174mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
2176 int type = 0;
2177 static int recursive = 0;
2178
Bram Moolenaar0f873732019-12-05 20:28:46 +01002179 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002181 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 return;
2183
Bram Moolenaar0f873732019-12-05 20:28:46 +01002184 // When one of the X11 functions causes a deadly signal, we get here again
2185 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 if (recursive)
2187 return;
2188 ++recursive;
2189
2190 /*
2191 * if the window ID and the display is known, we may use X11 calls
2192 */
2193#ifdef FEAT_X11
2194 if (get_x11_windis() == OK)
2195 type = 1;
2196#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002197# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002198 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 if (gui.in_use)
2200 type = 1;
2201# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202#endif
2203
2204 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002205 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 * than x11 calls, because the x11 calls don't always work
2207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 if ((type || *T_TS != NUL) && title != NULL)
2209 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002210 if (oldtitle_outdated)
2211 {
2212 oldtitle_outdated = FALSE;
2213 VIM_CLEAR(oldtitle);
2214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 if (oldtitle == NULL
2216#ifdef FEAT_GUI
2217 && !gui.in_use
2218#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002219 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 (void)get_x11_title(FALSE);
2221
Bram Moolenaar0f873732019-12-05 20:28:46 +01002222 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 term_settitle(title);
2224#ifdef FEAT_X11
2225 else
2226# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002227 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002229 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002231#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002232 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 else
2234 gui_mch_settitle(title, icon);
2235#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002236 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 }
2238
2239 if ((type || *T_CIS != NUL) && icon != NULL)
2240 {
2241 if (oldicon == NULL
2242#ifdef FEAT_GUI
2243 && !gui.in_use
2244#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002245 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 get_x11_icon(FALSE);
2247
2248 if (*T_CIS != NUL)
2249 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002250 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002252 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 out_flush();
2254 }
2255#ifdef FEAT_X11
2256 else
2257# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002258 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002260 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261#endif
2262 did_set_icon = TRUE;
2263 }
2264 --recursive;
2265}
2266
2267/*
2268 * Restore the window/icon title.
2269 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002270 * SAVE_RESTORE_TITLE only restore title
2271 * SAVE_RESTORE_ICON only restore icon
2272 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 */
2274 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002275mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
Bram Moolenaardac13472019-09-16 21:06:21 +02002277 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002278
Bram Moolenaar0f873732019-12-05 20:28:46 +01002279 // only restore the title or icon when it has been set
Bram Moolenaardac13472019-09-16 21:06:21 +02002280 mch_settitle(((which & SAVE_RESTORE_TITLE) && unix_did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002282 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2283
Bram Moolenaare5c83282019-05-03 23:15:37 +02002284 if (do_push_pop)
2285 {
2286 // pop and push from/to the stack
2287 term_pop_title(which);
2288 term_push_title(which);
2289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290}
2291
Bram Moolenaar0f873732019-12-05 20:28:46 +01002292#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
2294/*
2295 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002296 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 */
2298 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002299vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300{
2301 if (name == NULL)
2302 return FALSE;
2303 return (STRNICMP(name, "xterm", 5) == 0
2304 || STRNICMP(name, "nxterm", 6) == 0
2305 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002306 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002308 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 || STRCMP(name, "builtin_xterm") == 0);
2310}
2311
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002312#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2313/*
2314 * Return TRUE if "name" appears to be that of a terminal
2315 * known to support the xterm-style mouse protocol.
2316 * Relies on term_is_xterm having been set to its correct value.
2317 */
2318 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002319use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002320{
2321 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002322 && (term_is_xterm
2323 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002324 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002325 || STRICMP(name, "st") == 0
2326 || STRNICMP(name, "st-", 3) == 0
2327 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002328}
2329#endif
2330
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331/*
2332 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2333 * Return 1 for "xterm".
2334 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002335 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002336 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 */
2338 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002339use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002341 if (ttym_flags == TTYM_SGR)
2342 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002343 if (ttym_flags == TTYM_URXVT)
2344 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (ttym_flags == TTYM_XTERM2)
2346 return 2;
2347 if (ttym_flags == TTYM_XTERM)
2348 return 1;
2349 return 0;
2350}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002353vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354{
2355 if (name == NULL)
2356 return FALSE;
2357 return (STRNICMP(name, "iris-ansi", 9) == 0
2358 || STRCMP(name, "builtin_iris-ansi") == 0);
2359}
2360
2361 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002362vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
2364 if (name == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002365 return FALSE; // actually all ANSI comp. terminals should be here
2366 // catch VT100 - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002367 return ((STRNICMP(name, "vt", 2) == 0
2368 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 || STRCMP(name, "builtin_vt320") == 0);
2370}
2371
2372/*
2373 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2374 * This should include all windowed terminal emulators.
2375 */
2376 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002377vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
2379 if (name == NULL)
2380 return FALSE;
2381 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2382 return TRUE;
2383 return ( STRNICMP(name, "hpterm", 6) == 0
2384 || STRNICMP(name, "sun-cmd", 7) == 0
2385 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002386 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 || STRNICMP(name, "dtterm", 6) == 0);
2388}
2389
2390/*
2391 * Insert user name in s[len].
2392 * Return OK if a name found.
2393 */
2394 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002395mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
2397#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002398 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 return OK;
2400#else
2401 return mch_get_uname(getuid(), s, len);
2402#endif
2403}
2404
2405/*
2406 * Insert user name for "uid" in s[len].
2407 * Return OK if a name found.
2408 */
2409 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002410mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
2412#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2413 struct passwd *pw;
2414
2415 if ((pw = getpwuid(uid)) != NULL
2416 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2417 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002418 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 return OK;
2420 }
2421#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002422 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2423 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424}
2425
2426/*
2427 * Insert host name is s[len].
2428 */
2429
2430#ifdef HAVE_SYS_UTSNAME_H
2431 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002432mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
2434 struct utsname vutsname;
2435
2436 if (uname(&vutsname) < 0)
2437 *s = NUL;
2438 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002439 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002441#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
2443# ifdef HAVE_SYS_SYSTEMINFO_H
2444# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2445# endif
2446
2447 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002448mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449{
2450# ifdef VAXC
2451 vaxc$gethostname((char *)s, len);
2452# else
2453 gethostname((char *)s, len);
2454# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002455 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002457#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
2459/*
2460 * return process ID
2461 */
2462 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002463mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465 return (long)getpid();
2466}
2467
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002468/*
2469 * return TRUE if process "pid" is still running
2470 */
2471 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002472mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002473{
2474 // EMX kill() not working correctly, it seems
2475 return kill(pid, 0) == 0;
2476}
2477
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002480strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
2482 extern int sys_nerr;
2483 extern char *sys_errlist[];
2484 static char er[20];
2485
2486 if (err > 0 && err < sys_nerr)
2487 return (sys_errlist[err]);
2488 sprintf(er, "Error %d", err);
2489 return er;
2490}
2491#endif
2492
2493/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002494 * Get name of current directory into buffer "buf" of length "len" bytes.
2495 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 * Return OK for success, FAIL for failure.
2497 */
2498 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002499mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
2501#if defined(USE_GETCWD)
2502 if (getcwd((char *)buf, len) == NULL)
2503 {
2504 STRCPY(buf, strerror(errno));
2505 return FAIL;
2506 }
2507 return OK;
2508#else
2509 return (getwd((char *)buf) != NULL ? OK : FAIL);
2510#endif
2511}
2512
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002514 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 *
2516 * return FAIL for failure, OK for success
2517 */
2518 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002519mch_FullName(
2520 char_u *fname,
2521 char_u *buf,
2522 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002523 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002526#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002528 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 char_u olddir[MAXPATHL];
2531 char_u *p;
2532 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002533#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002534 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2535 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002536#endif
2537
Bram Moolenaar38323e42007-03-06 19:22:53 +00002538#ifdef VMS
2539 fname = vms_fixfilename(fname);
2540#endif
2541
Bram Moolenaara2442432007-04-26 14:26:37 +00002542#ifdef __CYGWIN__
2543 /*
2544 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2545 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002546# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002547 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2548 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002549 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2550 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002551# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002552 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002553# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002554 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002555#endif
2556
Bram Moolenaar0f873732019-12-05 20:28:46 +01002557 // Expand it if forced or not an absolute path.
2558 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002559 if ((force || !mch_isFullName(fname))
2560 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
2562 /*
2563 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002564 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 * This will get the correct path name with "../" things.
2566 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002567 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002569#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 /*
2571 * Use fchdir() if possible, it's said to be faster and more
2572 * reliable. But on SunOS 4 it might not work. Check this by
2573 * doing a fchdir() right now.
2574 */
2575 if (!dont_fchdir)
2576 {
2577 fd = open(".", O_RDONLY | O_EXTRA, 0);
2578 if (fd >= 0 && fchdir(fd) < 0)
2579 {
2580 close(fd);
2581 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002582 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
2584 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
Bram Moolenaar0f873732019-12-05 20:28:46 +01002587 // Only change directory when we are sure we can return to where
2588 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002590#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 (mch_dirname(olddir, MAXPATHL) == FAIL
2594 || mch_chdir((char *)olddir) != 0))
2595 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002596 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 retval = FAIL;
2598 }
2599 else
2600 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002601 // The directory is copied into buf[], to be able to remove
2602 // the file name without changing it (could be a string in
2603 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (p - fname >= len)
2605 retval = FAIL;
2606 else
2607 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002608 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (mch_chdir((char *)buf))
2610 retval = FAIL;
2611 else
2612 fname = p + 1;
2613 *buf = NUL;
2614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 }
2616 }
2617 if (mch_dirname(buf, len) == FAIL)
2618 {
2619 retval = FAIL;
2620 *buf = NUL;
2621 }
2622 if (p != NULL)
2623 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002624#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 if (fd >= 0)
2626 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002627 if (p_verbose >= 5)
2628 {
2629 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002630 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002631 verbose_leave();
2632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 l = fchdir(fd);
2634 close(fd);
2635 }
2636 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 l = mch_chdir((char *)olddir);
2639 if (l != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002640 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 }
2642
2643 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002644 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002645 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002646#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002647 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002649 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002652
Bram Moolenaar0f873732019-12-05 20:28:46 +01002653 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002654 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 return FAIL;
2656
Bram Moolenaar0f873732019-12-05 20:28:46 +01002657 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 if (STRCMP(fname, ".") != 0)
2659 STRCAT(buf, fname);
2660
2661 return OK;
2662}
2663
2664/*
2665 * Return TRUE if "fname" does not depend on the current directory.
2666 */
2667 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002668mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002670#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 return ( fname[0] == '/' || fname[0] == '.' ||
2672 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2673 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2674 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002675#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677#endif
2678}
2679
Bram Moolenaar24552be2005-12-10 20:17:30 +00002680#if defined(USE_FNAME_CASE) || defined(PROTO)
2681/*
2682 * Set the case of the file name, if it already exists. This will cause the
2683 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002684 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002685 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002686 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002687fname_case(
2688 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002689 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002690{
2691 struct stat st;
2692 char_u *slash, *tail;
2693 DIR *dirp;
2694 struct dirent *dp;
2695
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002696 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002697 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002698 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002699 slash = vim_strrchr(name, '/');
2700 if (slash == NULL)
2701 {
2702 dirp = opendir(".");
2703 tail = name;
2704 }
2705 else
2706 {
2707 *slash = NUL;
2708 dirp = opendir((char *)name);
2709 *slash = '/';
2710 tail = slash + 1;
2711 }
2712
2713 if (dirp != NULL)
2714 {
2715 while ((dp = readdir(dirp)) != NULL)
2716 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002717 // Only accept names that differ in case and are the same byte
2718 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002719 if (STRICMP(tail, dp->d_name) == 0
2720 && STRLEN(tail) == STRLEN(dp->d_name))
2721 {
2722 char_u newname[MAXPATHL + 1];
2723 struct stat st2;
2724
Bram Moolenaar0f873732019-12-05 20:28:46 +01002725 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002726 vim_strncpy(newname, name, MAXPATHL);
2727 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2728 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002729 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002730 && st.st_ino == st2.st_ino
2731 && st.st_dev == st2.st_dev)
2732 {
2733 STRCPY(tail, dp->d_name);
2734 break;
2735 }
2736 }
2737 }
2738
2739 closedir(dirp);
2740 }
2741 }
2742}
2743#endif
2744
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745/*
2746 * Get file permissions for 'name'.
2747 * Returns -1 when it doesn't exist.
2748 */
2749 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002750mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
2752 struct stat statb;
2753
Bram Moolenaar0f873732019-12-05 20:28:46 +01002754 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755#ifdef VMS
2756 if (stat((char *)vms_fixfilename(name), &statb))
2757#else
2758 if (stat((char *)name, &statb))
2759#endif
2760 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002761#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002762 // The top bit makes the value negative, which means the file doesn't
2763 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002764 return statb.st_mode & ~S_ADDACE;
2765#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768}
2769
2770/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002771 * Set file permission for "name" to "perm".
2772 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 */
2774 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002775mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776{
2777 return (chmod((char *)
2778#ifdef VMS
2779 vms_fixfilename(name),
2780#else
2781 name,
2782#endif
2783 (mode_t)perm) == 0 ? OK : FAIL);
2784}
2785
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002786#if defined(HAVE_FCHMOD) || defined(PROTO)
2787/*
2788 * Set file permission for open file "fd" to "perm".
2789 * Return FAIL for failure, OK otherwise.
2790 */
2791 int
2792mch_fsetperm(int fd, long perm)
2793{
2794 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2795}
2796#endif
2797
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798#if defined(HAVE_ACL) || defined(PROTO)
2799# ifdef HAVE_SYS_ACL_H
2800# include <sys/acl.h>
2801# endif
2802# ifdef HAVE_SYS_ACCESS_H
2803# include <sys/access.h>
2804# endif
2805
2806# ifdef HAVE_SOLARIS_ACL
2807typedef struct vim_acl_solaris_T {
2808 int acl_cnt;
2809 aclent_t *acl_entry;
2810} vim_acl_solaris_T;
2811# endif
2812
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002813#if defined(HAVE_SELINUX) || defined(PROTO)
2814/*
2815 * Copy security info from "from_file" to "to_file".
2816 */
2817 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002818mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002819{
2820 if (from_file == NULL)
2821 return;
2822
2823 if (selinux_enabled == -1)
2824 selinux_enabled = is_selinux_enabled();
2825
2826 if (selinux_enabled > 0)
2827 {
2828 security_context_t from_context = NULL;
2829 security_context_t to_context = NULL;
2830
2831 if (getfilecon((char *)from_file, &from_context) < 0)
2832 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002833 // If the filesystem doesn't support extended attributes,
2834 // the original had no special security context and the
2835 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002836 if (errno == EOPNOTSUPP)
2837 return;
2838
Bram Moolenaar32526b32019-01-19 17:43:09 +01002839 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002840 msg_outtrans(from_file);
2841 msg_putchar('\n');
2842 return;
2843 }
2844 if (getfilecon((char *)to_file, &to_context) < 0)
2845 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002846 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002847 msg_outtrans(to_file);
2848 msg_putchar('\n');
2849 freecon (from_context);
2850 return ;
2851 }
2852 if (strcmp(from_context, to_context) != 0)
2853 {
2854 if (setfilecon((char *)to_file, from_context) < 0)
2855 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002856 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002857 msg_outtrans(to_file);
2858 msg_putchar('\n');
2859 }
2860 }
2861 freecon(to_context);
2862 freecon(from_context);
2863 }
2864}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002865#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002866
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002867#if defined(HAVE_SMACK) && !defined(PROTO)
2868/*
2869 * Copy security info from "from_file" to "to_file".
2870 */
2871 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002872mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002873{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002874 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002875 {
2876 XATTR_NAME_SMACK,
2877 XATTR_NAME_SMACKEXEC,
2878 XATTR_NAME_SMACKMMAP
2879 };
2880
2881 char buffer[SMACK_LABEL_LEN];
2882 const char *name;
2883 int index;
2884 int ret;
2885 ssize_t size;
2886
2887 if (from_file == NULL)
2888 return;
2889
2890 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2891 / sizeof(smack_copied_attributes)[0]) ; index++)
2892 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002893 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002894 name = smack_copied_attributes[index];
2895
Bram Moolenaar0f873732019-12-05 20:28:46 +01002896 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002897 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2898 if (size >= 0)
2899 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002900 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002901 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2902 if (ret < 0)
2903 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002904 vim_snprintf((char *)IObuff, IOSIZE,
2905 _("Could not set security context %s for %s"),
2906 name, to_file);
2907 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002908 msg_putchar('\n');
2909 }
2910 }
2911 else
2912 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002913 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002914 switch (errno)
2915 {
2916 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002917 // extended attributes aren't supported or enabled
2918 // should a message be echoed? not sure...
2919 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002920
2921 case ERANGE:
2922 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002923 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002924 vim_snprintf((char *)IObuff, IOSIZE,
2925 _("Could not get security context %s for %s. Removing it!"),
2926 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002927 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002928 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01002929 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002930
2931 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002932 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002933 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002934 // Silently ignore errors, apparently this happens when
2935 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002936 break;
2937 }
2938 }
2939 }
2940}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002941#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002942
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943/*
2944 * Return a pointer to the ACL of file "fname" in allocated memory.
2945 * Return NULL if the ACL is not available for whatever reason.
2946 */
2947 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002948mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
2950 vim_acl_T ret = NULL;
2951#ifdef HAVE_POSIX_ACL
2952 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2953#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002954#ifdef HAVE_SOLARIS_ZFS_ACL
2955 acl_t *aclent;
2956
2957 if (acl_get((char *)fname, 0, &aclent) < 0)
2958 return NULL;
2959 ret = (vim_acl_T)aclent;
2960#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961#ifdef HAVE_SOLARIS_ACL
2962 vim_acl_solaris_T *aclent;
2963
2964 aclent = malloc(sizeof(vim_acl_solaris_T));
2965 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2966 {
2967 free(aclent);
2968 return NULL;
2969 }
2970 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2971 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2972 {
2973 free(aclent->acl_entry);
2974 free(aclent);
2975 return NULL;
2976 }
2977 ret = (vim_acl_T)aclent;
2978#else
2979#if defined(HAVE_AIX_ACL)
2980 int aclsize;
2981 struct acl *aclent;
2982
2983 aclsize = sizeof(struct acl);
2984 aclent = malloc(aclsize);
2985 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2986 {
2987 if (errno == ENOSPC)
2988 {
2989 aclsize = aclent->acl_len;
2990 aclent = realloc(aclent, aclsize);
2991 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2992 {
2993 free(aclent);
2994 return NULL;
2995 }
2996 }
2997 else
2998 {
2999 free(aclent);
3000 return NULL;
3001 }
3002 }
3003 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003004#endif // HAVE_AIX_ACL
3005#endif // HAVE_SOLARIS_ACL
3006#endif // HAVE_SOLARIS_ZFS_ACL
3007#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 return ret;
3009}
3010
3011/*
3012 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3013 */
3014 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003015mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
3017 if (aclent == NULL)
3018 return;
3019#ifdef HAVE_POSIX_ACL
3020 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3021#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003022#ifdef HAVE_SOLARIS_ZFS_ACL
3023 acl_set((char *)fname, (acl_t *)aclent);
3024#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025#ifdef HAVE_SOLARIS_ACL
3026 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3027 ((vim_acl_solaris_T *)aclent)->acl_entry);
3028#else
3029#ifdef HAVE_AIX_ACL
3030 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003031#endif // HAVE_AIX_ACL
3032#endif // HAVE_SOLARIS_ACL
3033#endif // HAVE_SOLARIS_ZFS_ACL
3034#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035}
3036
3037 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003038mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
3040 if (aclent == NULL)
3041 return;
3042#ifdef HAVE_POSIX_ACL
3043 acl_free((acl_t)aclent);
3044#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003045#ifdef HAVE_SOLARIS_ZFS_ACL
3046 acl_free((acl_t *)aclent);
3047#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048#ifdef HAVE_SOLARIS_ACL
3049 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3050 free(aclent);
3051#else
3052#ifdef HAVE_AIX_ACL
3053 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003054#endif // HAVE_AIX_ACL
3055#endif // HAVE_SOLARIS_ACL
3056#endif // HAVE_SOLARIS_ZFS_ACL
3057#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058}
3059#endif
3060
3061/*
3062 * Set hidden flag for "name".
3063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003065mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003067 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068}
3069
3070/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003071 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 * return FALSE if "name" is not a directory
3073 * return FALSE for error
3074 */
3075 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003076mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 struct stat statb;
3079
Bram Moolenaar0f873732019-12-05 20:28:46 +01003080 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 return FALSE;
3082 if (stat((char *)name, &statb))
3083 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085}
3086
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003087/*
3088 * return TRUE if "name" is a directory, NOT a symlink to a directory
3089 * return FALSE if "name" is not a directory
3090 * return FALSE for error
3091 */
3092 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003093mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003094{
3095 struct stat statb;
3096
Bram Moolenaar0f873732019-12-05 20:28:46 +01003097 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003098 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003099 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003100 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003101 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003102}
3103
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104/*
3105 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3106 */
3107 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003108executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
3110 struct stat st;
3111
3112 if (stat((char *)name, &st))
3113 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003114#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003115 // Like on Unix system file can have executable rights but not necessarily
3116 // be an executable, but on Unix is not a default for an ordinary file to
3117 // have an executable flag - on VMS it is in most cases.
3118 // Therefore, this check does not have any sense - let keep us to the
3119 // conventions instead:
3120 // *.COM and *.EXE files are the executables - the rest are not. This is
3121 // not ideal but better then it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003122 int vms_executable = 0;
3123 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3124 {
3125 if (strstr(vms_tolower((char*)name),".exe") != NULL
3126 || strstr(vms_tolower((char*)name),".com")!= NULL)
3127 vms_executable = 1;
3128 }
3129 return vms_executable;
3130#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133}
3134
3135/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003136 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003137 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 * Return -1 if unknown.
3139 */
3140 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003141mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 char_u *buf;
3144 char_u *p, *e;
3145 int retval;
3146
Bram Moolenaar0f873732019-12-05 20:28:46 +01003147 // When "use_path" is false and if it's an absolute or relative path don't
3148 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003149 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003150 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003151 // There must be a path separator, files in the current directory
3152 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003153 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003154 {
3155 if (path != NULL)
3156 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003157 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003158 *path = FullName_save(name, TRUE);
3159 else
3160 *path = vim_strsave(name);
3161 }
3162 return TRUE;
3163 }
3164 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166
3167 p = (char_u *)getenv("PATH");
3168 if (p == NULL || *p == NUL)
3169 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003170 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (buf == NULL)
3172 return -1;
3173
3174 /*
3175 * Walk through all entries in $PATH to check if "name" exists there and
3176 * is an executable file.
3177 */
3178 for (;;)
3179 {
3180 e = (char_u *)strchr((char *)p, ':');
3181 if (e == NULL)
3182 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003183 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 STRCPY(buf, "./");
3185 else
3186 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003187 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 add_pathsep(buf);
3189 }
3190 STRCAT(buf, name);
3191 retval = executable_file(buf);
3192 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003193 {
3194 if (path != NULL)
3195 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003196 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003197 *path = FullName_save(buf, TRUE);
3198 else
3199 *path = vim_strsave(buf);
3200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204 if (*e != ':')
3205 break;
3206 p = e + 1;
3207 }
3208
3209 vim_free(buf);
3210 return retval;
3211}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
3213/*
3214 * Check what "name" is:
3215 * NODE_NORMAL: file or directory (or doesn't exist)
3216 * NODE_WRITABLE: writable device, socket, fifo, etc.
3217 * NODE_OTHER: non-writable things
3218 */
3219 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003220mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222 struct stat st;
3223
3224 if (stat((char *)name, &st))
3225 return NODE_NORMAL;
3226 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3227 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003228 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003230 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 return NODE_WRITABLE;
3232}
3233
3234 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003235mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
3237#ifdef HAVE_CHECK_STACK_GROWTH
3238 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 check_stack_growth((char *)&i);
3241
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003242# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 get_stack_limit();
3244# endif
3245
3246#endif
3247
3248 /*
3249 * Setup an alternative stack for signals. Helps to catch signals when
3250 * running out of stack space.
3251 * Use of sigaltstack() is preferred, it's more portable.
3252 * Ignore any errors.
3253 */
3254#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003255 signal_stack = alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 init_signal_stack();
3257#endif
3258}
3259
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003260#if defined(EXITFREE) || defined(PROTO)
3261 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003262mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003263{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003264# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3265 if (clip_star.owned)
3266 clip_lose_selection(&clip_star);
3267 if (clip_plus.owned)
3268 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003269# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003270# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003271 if (xterm_Shell != (Widget)0)
3272 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003273# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003274 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003275 if (xterm_dpy != NULL)
3276 XtCloseDisplay(xterm_dpy);
3277 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003278 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003279 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003280# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003281 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003282# endif
3283 }
3284# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003285# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003286# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003287 if (x11_display != NULL
3288# ifdef FEAT_XCLIPBOARD
3289 && x11_display != xterm_dpy
3290# endif
3291 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003292 XCloseDisplay(x11_display);
3293# endif
3294# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003295 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003296# endif
3297# ifdef FEAT_TITLE
3298 vim_free(oldtitle);
3299 vim_free(oldicon);
3300# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003301}
3302#endif
3303
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304/*
3305 * Output a newline when exiting.
3306 * Make sure the newline goes to the same stream as the text.
3307 */
3308 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003309exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003311 if (silent_mode)
3312 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 if (newline_on_exit || msg_didout)
3314 {
3315 if (msg_use_printf())
3316 {
3317 if (info_message)
3318 mch_msg("\n");
3319 else
3320 mch_errmsg("\r\n");
3321 }
3322 else
3323 out_char('\n');
3324 }
3325 else
3326 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003327 restore_cterm_colors(); // get original colors back
3328 msg_clr_eos_force(); // clear the rest of the display
3329 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 }
3331}
3332
Bram Moolenaarb4151682020-05-11 22:13:28 +02003333#ifdef USE_GCOV_FLUSH
3334extern void __gcov_flush();
3335#endif
3336
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003338mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
3340 exiting = TRUE;
3341
3342#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3343 x11_export_final_selection();
3344#endif
3345
3346#ifdef FEAT_GUI
3347 if (!gui.in_use)
3348#endif
3349 {
3350 settmode(TMODE_COOK);
3351#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003352 // restore xterm title and icon name
3353 mch_restore_title(SAVE_RESTORE_BOTH);
3354 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355#endif
3356 /*
3357 * When t_ti is not empty but it doesn't cause swapping terminal
3358 * pages, need to output a newline when msg_didout is set. But when
3359 * t_ti does swap pages it should not go to the shell page. Do this
3360 * before stoptermcap().
3361 */
3362 if (swapping_screen() && !newline_on_exit)
3363 exit_scroll();
3364
Bram Moolenaar0f873732019-12-05 20:28:46 +01003365 // Stop termcap: May need to check for T_CRV response, which
3366 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 stoptermcap();
3368
3369 /*
3370 * A newline is only required after a message in the alternate screen.
3371 * This is set to TRUE by wait_return().
3372 */
3373 if (!swapping_screen() || newline_on_exit)
3374 exit_scroll();
3375
Bram Moolenaar0f873732019-12-05 20:28:46 +01003376 // Cursor may have been switched off without calling starttermcap()
3377 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 if (full_screen)
3379 cursor_on();
3380 }
3381 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003382 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003383
3384#ifdef USE_GCOV_FLUSH
3385 // Flush coverage info before possibly being killed by a deadly signal.
3386 __gcov_flush();
3387#endif
3388
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 may_core_dump();
3390#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 gui_exit(r);
3393#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003394
Bram Moolenaar56718732006-03-15 22:53:57 +00003395#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003396 mac_conv_cleanup();
3397#endif
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003400 // A core dump won't be created if the signal handler
3401 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (deadly_signal != 0)
3403 return;
3404#endif
3405
Bram Moolenaar009b2592004-10-24 19:18:58 +00003406#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003407 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003408#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003409
3410#ifdef EXITFREE
3411 free_all_mem();
3412#endif
3413
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 exit(r);
3415}
3416
3417 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003418may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
3420 if (deadly_signal != 0)
3421 {
3422 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003423 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 }
3425}
3426
3427#ifndef VMS
3428
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003429/*
3430 * Get the file descriptor to use for tty operations.
3431 */
3432 static int
3433get_tty_fd(int fd)
3434{
3435 int tty_fd = fd;
3436
3437#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3438 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3439 // "fd" when it is a master device.
3440 if (mch_isatty(fd) > 1)
3441 {
3442 char *name;
3443
3444 name = ptsname(fd);
3445 if (name == NULL)
3446 return -1;
3447
3448 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3449 if (tty_fd < 0)
3450 return -1;
3451 }
3452#endif
3453 return tty_fd;
3454}
3455
3456 static int
3457mch_tcgetattr(int fd, void *term)
3458{
3459 int tty_fd;
3460 int retval = -1;
3461
3462 tty_fd = get_tty_fd(fd);
3463 if (tty_fd >= 0)
3464 {
3465#ifdef NEW_TTY_SYSTEM
3466# ifdef HAVE_TERMIOS_H
3467 retval = tcgetattr(tty_fd, (struct termios *)term);
3468# else
3469 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3470# endif
3471#else
3472 // for "old" tty systems
3473 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3474#endif
3475 if (tty_fd != fd)
3476 close(tty_fd);
3477 }
3478 return retval;
3479}
3480
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003482mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
3484 static int first = TRUE;
3485
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003486#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487# ifdef HAVE_TERMIOS_H
3488 static struct termios told;
3489 struct termios tnew;
3490# else
3491 static struct termio told;
3492 struct termio tnew;
3493# endif
3494
3495 if (first)
3496 {
3497 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003498 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 }
3500
3501 tnew = told;
3502 if (tmode == TMODE_RAW)
3503 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003504 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003505 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3506 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003508# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003509 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510# endif
3511 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003512# ifdef ONLCR
3513 // Don't map NL -> CR NL, we do it ourselves.
3514 // Also disable expanding tabs if possible.
3515# ifdef XTABS
3516 tnew.c_oflag &= ~(ONLCR | XTABS);
3517# else
3518# ifdef TAB3
3519 tnew.c_oflag &= ~(ONLCR | TAB3);
3520# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003522# endif
3523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003525 tnew.c_cc[VMIN] = 1; // return after 1 char
3526 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003529 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003530 // Also reset ICANON here, otherwise on Solaris select() won't see
3531 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003532 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003533 tnew.c_cc[VMIN] = 1; // return after 1 char
3534 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
3537# if defined(HAVE_TERMIOS_H)
3538 {
3539 int n = 10;
3540
Bram Moolenaar0f873732019-12-05 20:28:46 +01003541 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3542 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3544 && errno == EINTR && n > 0)
3545 --n;
3546 }
3547# else
3548 ioctl(read_cmd_fd, TCSETA, &tnew);
3549# endif
3550
3551#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 /*
3553 * for "old" tty systems
3554 */
3555# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003556# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557# endif
3558 static struct sgttyb ttybold;
3559 struct sgttyb ttybnew;
3560
3561 if (first)
3562 {
3563 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003564 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
3566
3567 ttybnew = ttybold;
3568 if (tmode == TMODE_RAW)
3569 {
3570 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3571 ttybnew.sg_flags |= RAW;
3572 }
3573 else if (tmode == TMODE_SLEEP)
3574 ttybnew.sg_flags &= ~(ECHO);
3575 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3576#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003577 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578}
3579
3580/*
3581 * Try to get the code for "t_kb" from the stty setting
3582 *
3583 * Even if termcap claims a backspace key, the user's setting *should*
3584 * prevail. stty knows more about reality than termcap does, and if
3585 * somebody's usual erase key is DEL (which, for most BSD users, it will
3586 * be), they're going to get really annoyed if their erase key starts
3587 * doing forward deletes for no reason. (Eric Fischer)
3588 */
3589 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003590get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003592 ttyinfo_T info;
3593 char_u buf[2];
3594 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003596 if (get_tty_info(read_cmd_fd, &info) == OK)
3597 {
3598 intr_char = info.interrupt;
3599 buf[0] = info.backspace;
3600 buf[1] = NUL;
3601 add_termcode((char_u *)"kb", buf, FALSE);
3602
Bram Moolenaar0f873732019-12-05 20:28:46 +01003603 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003604 p = find_termcode((char_u *)"kD");
3605 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3606 do_fixdel(NULL);
3607 }
3608}
3609
3610/*
3611 * Obtain the characters that Backspace and Enter produce on "fd".
3612 * Returns OK or FAIL.
3613 */
3614 int
3615get_tty_info(int fd, ttyinfo_T *info)
3616{
3617#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618# ifdef HAVE_TERMIOS_H
3619 struct termios keys;
3620# else
3621 struct termio keys;
3622# endif
3623
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003624 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003626 info->backspace = keys.c_cc[VERASE];
3627 info->interrupt = keys.c_cc[VINTR];
3628 if (keys.c_iflag & ICRNL)
3629 info->enter = NL;
3630 else
3631 info->enter = CAR;
3632 if (keys.c_oflag & ONLCR)
3633 info->nl_does_cr = TRUE;
3634 else
3635 info->nl_does_cr = FALSE;
3636 return OK;
3637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003639 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 struct sgttyb keys;
3641
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003642 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003644 info->backspace = keys.sg_erase;
3645 info->interrupt = keys.sg_kill;
3646 info->enter = CAR;
3647 info->nl_does_cr = TRUE;
3648 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003651 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652}
3653
Bram Moolenaar0f873732019-12-05 20:28:46 +01003654#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003656static int mouse_ison = FALSE;
3657
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658/*
3659 * Set mouse clicks on or off.
3660 */
3661 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003662mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003664#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003665 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 int xterm_mouse_vers;
3668
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003669#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003670 if (!on)
3671 // Make sure not tracing mouse movements. Important when a button-down
3672 // was received but no release yet.
3673 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003674#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003675
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003676 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003677#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003678 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003679#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003680 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003681 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 return;
3683
3684 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003685
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003686#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003687 if (ttym_flags == TTYM_URXVT)
3688 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003689 out_str_nf((char_u *)
3690 (on
3691 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3692 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003693 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003694 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003695#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003696
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003697 if (ttym_flags == TTYM_SGR)
3698 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003699 // SGR mode supports columns above 223
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003700 out_str_nf((char_u *)
3701 (on
3702 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3703 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003704 mouse_ison = on;
3705 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003706
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003707#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003708 if (bevalterm_ison != (p_bevalterm && on))
3709 {
3710 bevalterm_ison = (p_bevalterm && on);
3711 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003712 // disable mouse movement events, enabling is below
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003713 out_str_nf((char_u *)
3714 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003715 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003716#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 if (xterm_mouse_vers > 0)
3719 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003720 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 out_str_nf((char_u *)
3722 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003723 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003724#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003725 bevalterm_ison
3726 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003727#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003728 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003730 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 out_str_nf((char_u *)
3732 (xterm_mouse_vers > 1
3733 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3734 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003735 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003738#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 else if (ttym_flags == TTYM_DEC)
3740 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003741 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003743 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003745 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003749#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 else
3751 {
3752 if (on)
3753 {
3754 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003755 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
3757 else
3758 {
3759 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003760 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003765#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003766 else
3767 {
3768 if (on)
3769 {
3770 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003771 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003772 }
3773 else
3774 {
3775 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003776 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003777 }
3778 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003779#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003780
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003781#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 else
3783 {
3784 if (on)
3785 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003786 // D - Enable Mouse up/down messages
3787 // L - Enable Left Button Reporting
3788 // M - Enable Middle Button Reporting
3789 // R - Enable Right Button Reporting
3790 // K - Enable SHIFT and CTRL key Reporting
3791 // + - Enable Advanced messaging of mouse moves and up/down messages
3792 // Q - Quiet No Ack
3793 // # - Numeric value of mouse pointer required
3794 // 0 = Multiview 2000 cursor, used as standard
3795 // 1 = Windows Arrow
3796 // 2 = Windows I Beam
3797 // 3 = Windows Hour Glass
3798 // 4 = Windows Cross Hair
3799 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003800# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003801 // Disables full feedback of pointer movements
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3803 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003804# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3806 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003807# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003808 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810 else
3811 {
3812 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3813 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003814 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003817#endif
3818#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 else
3820 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003821 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 if (on)
3823 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3824 else
3825 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003826 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829}
3830
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003831#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003832/*
3833 * Called when 'balloonevalterm' changed.
3834 */
3835 void
3836mch_bevalterm_changed(void)
3837{
3838 mch_setmouse(mouse_ison);
3839}
3840#endif
3841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842/*
3843 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3844 */
3845 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003846check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847{
3848# ifdef FEAT_MOUSE_XTERM
3849 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003850# ifdef FEAT_MOUSE_URXVT
3851 && use_xterm_mouse() != 3
3852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853# ifdef FEAT_GUI
3854 && !gui.in_use
3855# endif
3856 )
3857 {
3858 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003859 ? IF_EB("\233M", CSI_STR "M")
3860 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 if (*p_mouse != NUL)
3862 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003863 // force mouse off and maybe on to send possibly new mouse
3864 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 mch_setmouse(FALSE);
3866 setmouse();
3867 }
3868 }
3869 else
3870 del_mouse_termcode(KS_MOUSE);
3871# endif
3872
3873# ifdef FEAT_MOUSE_GPM
3874 if (!use_xterm_mouse()
3875# ifdef FEAT_GUI
3876 && !gui.in_use
3877# endif
3878 )
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003879 set_mouse_termcode(KS_GPM_MOUSE,
3880 (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3881 else
3882 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883# endif
3884
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003885# ifdef FEAT_SYSMOUSE
3886 if (!use_xterm_mouse()
3887# ifdef FEAT_GUI
3888 && !gui.in_use
3889# endif
3890 )
3891 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3892# endif
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003895 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if (!use_xterm_mouse()
3897# ifdef FEAT_GUI
3898 && !gui.in_use
3899# endif
3900 )
3901 set_mouse_termcode(KS_JSBTERM_MOUSE,
3902 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3903 else
3904 del_mouse_termcode(KS_JSBTERM_MOUSE);
3905# endif
3906
3907# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003908 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3909 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 if (!use_xterm_mouse()
3911# ifdef FEAT_GUI
3912 && !gui.in_use
3913# endif
3914 )
3915 set_mouse_termcode(KS_NETTERM_MOUSE,
3916 (char_u *)IF_EB("\033}", ESC_STR "}"));
3917 else
3918 del_mouse_termcode(KS_NETTERM_MOUSE);
3919# endif
3920
3921# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003922 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003923 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924# ifdef FEAT_GUI
3925 && !gui.in_use
3926# endif
3927 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003928 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3929 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 else
3931 del_mouse_termcode(KS_DEC_MOUSE);
3932# endif
3933# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01003934 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003935 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936# ifdef FEAT_GUI
3937 && !gui.in_use
3938# endif
3939 )
3940 set_mouse_termcode(KS_PTERM_MOUSE,
3941 (char_u *) IF_EB("\033[", ESC_STR "["));
3942 else
3943 del_mouse_termcode(KS_PTERM_MOUSE);
3944# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003945# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003946 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003947# ifdef FEAT_GUI
3948 && !gui.in_use
3949# endif
3950 )
3951 {
3952 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003953 ? IF_EB("\233*M", CSI_STR "*M")
3954 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003955
3956 if (*p_mouse != NUL)
3957 {
3958 mch_setmouse(FALSE);
3959 setmouse();
3960 }
3961 }
3962 else
3963 del_mouse_termcode(KS_URXVT_MOUSE);
3964# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003965 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003966# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003967 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003968# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003969 )
3970 {
3971 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003972 ? IF_EB("\233<*M", CSI_STR "<*M")
3973 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3974
3975 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3976 ? IF_EB("\233<*m", CSI_STR "<*m")
3977 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003978
3979 if (*p_mouse != NUL)
3980 {
3981 mch_setmouse(FALSE);
3982 setmouse();
3983 }
3984 }
3985 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003986 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003987 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003988 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992#ifndef VMS
3993
3994/*
3995 * Try to get the current window size:
3996 * 1. with an ioctl(), most accurate method
3997 * 2. from the environment variables LINES and COLUMNS
3998 * 3. from the termcap
3999 * 4. keep using the old values
4000 * Return OK when size could be determined, FAIL otherwise.
4001 */
4002 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004003mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
4005 long rows = 0;
4006 long columns = 0;
4007 char_u *p;
4008
4009 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 * 1. try using an ioctl. It is the most accurate method.
4011 *
4012 * Try using TIOCGWINSZ first, some systems that have it also define
4013 * TIOCGSIZE but don't have a struct ttysize.
4014 */
4015# ifdef TIOCGWINSZ
4016 {
4017 struct winsize ws;
4018 int fd = 1;
4019
Bram Moolenaar0f873732019-12-05 20:28:46 +01004020 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 if (!isatty(fd) && isatty(read_cmd_fd))
4022 fd = read_cmd_fd;
4023 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4024 {
4025 columns = ws.ws_col;
4026 rows = ws.ws_row;
4027 }
4028 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004029# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030# ifdef TIOCGSIZE
4031 {
4032 struct ttysize ts;
4033 int fd = 1;
4034
Bram Moolenaar0f873732019-12-05 20:28:46 +01004035 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 if (!isatty(fd) && isatty(read_cmd_fd))
4037 fd = read_cmd_fd;
4038 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4039 {
4040 columns = ts.ts_cols;
4041 rows = ts.ts_lines;
4042 }
4043 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004044# endif // TIOCGSIZE
4045# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
4047 /*
4048 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004049 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4050 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004052 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
4054 if ((p = (char_u *)getenv("LINES")))
4055 rows = atoi((char *)p);
4056 if ((p = (char_u *)getenv("COLUMNS")))
4057 columns = atoi((char *)p);
4058 }
4059
4060#ifdef HAVE_TGETENT
4061 /*
4062 * 3. try reading "co" and "li" entries from termcap
4063 */
4064 if (columns == 0 || rows == 0)
4065 getlinecol(&columns, &rows);
4066#endif
4067
4068 /*
4069 * 4. If everything fails, use the old values
4070 */
4071 if (columns <= 0 || rows <= 0)
4072 return FAIL;
4073
4074 Rows = rows;
4075 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004076 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 return OK;
4078}
4079
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004080#if defined(FEAT_TERMINAL) || defined(PROTO)
4081/*
4082 * Report the windows size "rows" and "cols" to tty "fd".
4083 */
4084 int
4085mch_report_winsize(int fd, int rows, int cols)
4086{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004087 int tty_fd;
4088 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004089
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004090 tty_fd = get_tty_fd(fd);
4091 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004092 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004093# if defined(TIOCSWINSZ)
4094 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004095
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004096 ws.ws_col = cols;
4097 ws.ws_row = rows;
4098 ws.ws_xpixel = cols * 5;
4099 ws.ws_ypixel = rows * 10;
4100 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4101 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4102 retval == 0 ? "success" : "failed");
4103# elif defined(TIOCSSIZE)
4104 struct ttysize ts;
4105
4106 ts.ts_cols = cols;
4107 ts.ts_lines = rows;
4108 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4109 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4110 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004111# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004112 if (tty_fd != fd)
4113 close(tty_fd);
4114 }
4115 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004116}
4117#endif
4118
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119/*
4120 * Try to set the window size to Rows and Columns.
4121 */
4122 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004123mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124{
4125 if (*T_CWS)
4126 {
4127 /*
4128 * NOTE: if you get an error here that term_set_winsize() is
4129 * undefined, check the output of configure. It could probably not
4130 * find a ncurses, termcap or termlib library.
4131 */
4132 term_set_winsize((int)Rows, (int)Columns);
4133 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004134 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136}
4137
Bram Moolenaar0f873732019-12-05 20:28:46 +01004138#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
4140/*
4141 * Rows and/or Columns has changed.
4142 */
4143 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004144mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004146 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147}
4148
Bram Moolenaar205b8862011-09-07 15:04:31 +02004149/*
4150 * Wait for process "child" to end.
4151 * Return "child" if it exited properly, <= 0 on error.
4152 */
4153 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004154wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004155{
4156 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004157 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004158
4159 while (wait_pid != child)
4160 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004161 // When compiled with Python threads are probably used, in which case
4162 // wait() sometimes hangs for no obvious reason. Use waitpid()
4163 // instead and loop (like the GUI). Also needed for other interfaces,
4164 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004165# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004166 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004167# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004168 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004169# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004170 if (wait_pid == 0)
4171 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004172 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004173 mch_delay(delay_msec, TRUE);
4174 if (++delay_msec > 10)
4175 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004176 continue;
4177 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004178 if (wait_pid <= 0
4179# ifdef ECHILD
4180 && errno == ECHILD
4181# endif
4182 )
4183 break;
4184 }
4185 return wait_pid;
4186}
4187
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004188#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004189/*
4190 * Set the environment for a child process.
4191 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004192 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004193set_child_environment(
4194 long rows,
4195 long columns,
4196 char *term,
4197 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004198{
4199# ifdef HAVE_SETENV
4200 char envbuf[50];
4201# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004202 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004203 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004204 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004205 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004206 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004207# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004208 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004209# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004210# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004211 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004212# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004213# endif
4214
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004215# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004216 setenv("TERM", term, 1);
4217 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004218 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004219 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004220 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004221 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004222 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004223 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004224 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004225# ifdef FEAT_TERMINAL
4226 if (is_terminal)
4227 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004228 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004229 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4230 }
4231# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004232# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004233 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004234# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004235# else
4236 /*
4237 * Putenv does not copy the string, it has to remain valid.
4238 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004239 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004240 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004241 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4242 putenv(envbuf_Term);
4243 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004244 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004245 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4246 putenv(envbuf_Lines);
4247 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4248 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004249 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004250 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004251 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004252# ifdef FEAT_TERMINAL
4253 if (is_terminal)
4254 {
4255 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004256 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004257 putenv(envbuf_Version);
4258 }
4259# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004260# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004261 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4262 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4263 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004264# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004265# endif
4266}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004267
4268 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004269set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004270{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004271 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004272}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004273#endif
4274
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004275#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004276/*
4277 * Open a PTY, with FD for the master and slave side.
4278 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004279 * When successful both file descriptors are stored and the allocated pty name
4280 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004281 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004282 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004283open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004284{
4285 char *tty_name;
4286
Bram Moolenaar59386482019-02-10 22:43:46 +01004287 if (name1 != NULL)
4288 *name1 = NULL;
4289 if (name2 != NULL)
4290 *name2 = NULL;
4291
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004292 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004293 if (*pty_master_fd >= 0)
4294 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004295 // Leaving out O_NOCTTY may lead to waitpid() always returning
4296 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4297 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004298#ifdef O_NOCTTY
4299 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4300#else
4301 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4302#endif
4303 if (*pty_slave_fd < 0)
4304 {
4305 close(*pty_master_fd);
4306 *pty_master_fd = -1;
4307 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004308 else
4309 {
4310 if (name1 != NULL)
4311 *name1 = vim_strsave((char_u *)tty_name);
4312 if (name2 != NULL)
4313 *name2 = vim_strsave((char_u *)tty_name);
4314 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004315 }
4316}
4317#endif
4318
Bram Moolenaarfae42832017-08-01 22:24:26 +02004319/*
4320 * Send SIGINT to a child process if "c" is an interrupt character.
4321 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004322 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004323may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4324{
4325# ifdef SIGINT
4326 if (c == Ctrl_C || c == intr_char)
4327 {
4328# ifdef HAVE_SETSID
4329 kill(-pid, SIGINT);
4330# else
4331 kill(0, SIGINT);
4332# endif
4333 if (wpid > 0)
4334 kill(wpid, SIGINT);
4335 }
4336# endif
4337}
4338
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004339#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004340
Bram Moolenaar0f873732019-12-05 20:28:46 +01004341/*
4342 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4343 * pointers, the last one is NULL.
4344 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4345 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004346 int
4347unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004348 char_u *cmd,
4349 char ***argvp,
4350 char_u **sh_tofree,
4351 char_u **shcf_tofree)
4352{
4353 char **argv = NULL;
4354 int argc;
4355
4356 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004357 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004358 return FAIL;
4359
4360 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4361 return FAIL;
4362 *argvp = argv;
4363
4364 if (cmd != NULL)
4365 {
4366 char_u *s;
4367 char_u *p;
4368
4369 if (extra_shell_arg != NULL)
4370 argv[argc++] = (char *)extra_shell_arg;
4371
Bram Moolenaar0f873732019-12-05 20:28:46 +01004372 // Break 'shellcmdflag' into white separated parts. This doesn't
4373 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004374 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004375 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004376 return FAIL;
4377 s = *shcf_tofree;
4378 p = p_shcf;
4379 while (*p != NUL)
4380 {
4381 argv[argc++] = (char *)s;
4382 while (*p && *p != ' ' && *p != TAB)
4383 *s++ = *p++;
4384 *s++ = NUL;
4385 p = skipwhite(p);
4386 }
4387
4388 argv[argc++] = (char *)cmd;
4389 }
4390 argv[argc] = NULL;
4391 return OK;
4392}
4393#endif
4394
4395#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4396/*
4397 * Use a terminal window to run a shell command in.
4398 */
4399 static int
4400mch_call_shell_terminal(
4401 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004402 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004403{
4404 jobopt_T opt;
4405 char **argv = NULL;
4406 char_u *tofree1 = NULL;
4407 char_u *tofree2 = NULL;
4408 int retval = -1;
4409 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004410 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004411 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004412 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004413
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004414 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004415 goto theend;
4416
4417 init_job_options(&opt);
4418 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4419 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004420 if (buf == NULL)
4421 goto theend;
4422
4423 job = term_getjob(buf->b_term);
4424 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004425
Bram Moolenaar0f873732019-12-05 20:28:46 +01004426 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004427 aucmd_prepbuf(&aco, buf);
4428
4429 clear_oparg(&oa);
4430 while (term_use_loop())
4431 {
4432 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4433 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004434 // If terminal_loop() returns OK we got a key that is handled
4435 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004436 if (terminal_loop(TRUE) == OK)
4437 normal_cmd(&oa, TRUE);
4438 }
4439 else
4440 normal_cmd(&oa, TRUE);
4441 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004442 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004443 ch_log(NULL, "system command finished");
4444
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004445 job_unref(job);
4446
Bram Moolenaar0f873732019-12-05 20:28:46 +01004447 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004448 aucmd_restbuf(&aco);
4449
4450 wait_return(TRUE);
4451 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4452
4453theend:
4454 vim_free(argv);
4455 vim_free(tofree1);
4456 vim_free(tofree2);
4457 return retval;
4458}
4459#endif
4460
4461#ifdef USE_SYSTEM
4462/*
4463 * Use system() to start the shell: simple but slow.
4464 */
4465 static int
4466mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004467 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004468 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469{
4470#ifdef VMS
4471 char *ifn = NULL;
4472 char *ofn = NULL;
4473#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004474 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004475 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004476 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
4478 out_flush();
4479
4480 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004481 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482
Bram Moolenaar62b42182010-09-21 22:09:37 +02004483# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004484 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004485 loose_clipboard();
4486# endif
4487
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 if (cmd == NULL)
4489 x = system((char *)p_sh);
4490 else
4491 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004492# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 if (ofn = strchr((char *)cmd, '>'))
4494 *ofn++ = '\0';
4495 if (ifn = strchr((char *)cmd, '<'))
4496 {
4497 char *p;
4498
4499 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004500 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (p)
4502 *p = '\0';
4503 }
4504 if (ofn)
4505 x = vms_sys((char *)cmd, ofn, ifn);
4506 else
4507 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004508# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004509 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004511 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 if (newcmd == NULL)
4513 x = 0;
4514 else
4515 {
4516 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4517 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4518 (char *)p_shcf,
4519 (char *)cmd);
4520 x = system((char *)newcmd);
4521 vim_free(newcmd);
4522 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 }
4525# ifdef VMS
4526 x = vms_sys_status(x);
4527# endif
4528 if (emsg_silent)
4529 ;
4530 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004531 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 else if (x && !(options & SHELL_SILENT))
4533 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004534 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 msg_outnum((long)x);
4536 msg_putchar('\n');
4537 }
4538
4539 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004540 {
4541 // The shell may have messed with the mode, always set it.
4542 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004543 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545# ifdef FEAT_TITLE
4546 resettitle();
4547# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004548# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4549 restore_clipboard();
4550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004552}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
Bram Moolenaar0f873732019-12-05 20:28:46 +01004554#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
Bram Moolenaar0f873732019-12-05 20:28:46 +01004556# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4557 // 127, some shells use that already
4558# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
Bram Moolenaar13568252018-03-16 20:46:58 +01004560/*
4561 * Don't use system(), use fork()/exec().
4562 */
4563 static int
4564mch_call_shell_fork(
4565 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004566 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004567{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004568 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004570 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 pid_t wait_pid = 0;
4572# ifdef HAVE_UNION_WAIT
4573 union wait status;
4574# else
4575 int status = -1;
4576# endif
4577 int retval = -1;
4578 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004579 char_u *tofree1 = NULL;
4580 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004582 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004583# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004585# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004586 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 int fd_fromshell[2];
4588 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004589 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590
4591 out_flush();
4592 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004593 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004594 if (tmode == TMODE_RAW)
4595 // The shell may have messed with the mode, always set it later.
4596 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004598 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004599 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004602 * For the GUI, when writing the output into the buffer and when reading
4603 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4604 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004606 if ((options & (SHELL_READ|SHELL_WRITE))
4607# ifdef FEAT_GUI
4608 || (gui.in_use && show_shell_mess)
4609# endif
4610 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004612# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 /*
4614 * Try to open a master pty.
4615 * If this works, open the slave pty.
4616 * If the slave can't be opened, close the master pty.
4617 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004618 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004619 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 /*
4621 * If not opening a pty or it didn't work, try using pipes.
4622 */
4623 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004624# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
4626 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004627 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
4629 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004630 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
4632 close(fd_toshell[0]);
4633 close(fd_toshell[1]);
4634 }
4635 }
4636 if (pipe_error)
4637 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004638 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 out_flush();
4640 }
4641 }
4642 }
4643
Bram Moolenaar0f873732019-12-05 20:28:46 +01004644 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004646 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004647 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004648 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004649 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004651 UNBLOCK_SIGNALS(&curset);
4652
Bram Moolenaar32526b32019-01-19 17:43:09 +01004653 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004654 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004656 || (gui.in_use && show_shell_mess)
4657# endif
4658 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004660# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004661 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 {
4663 close(pty_master_fd);
4664 close(pty_slave_fd);
4665 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004666 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004667# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
4669 close(fd_toshell[0]);
4670 close(fd_toshell[1]);
4671 close(fd_fromshell[0]);
4672 close(fd_fromshell[1]);
4673 }
4674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004676 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004678 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004679 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680
Bram Moolenaar819524702018-02-27 19:10:00 +01004681# ifdef FEAT_JOB_CHANNEL
4682 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01004683 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01004684 ch_logfile((char_u *)"", (char_u *)"");
4685# endif
4686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 if (!show_shell_mess || (options & SHELL_EXPAND))
4688 {
4689 int fd;
4690
4691 /*
4692 * Don't want to show any message from the shell. Can't just
4693 * close stdout and stderr though, because some systems will
4694 * break if you try to write to them after that, so we must
4695 * use dup() to replace them with something else -- webb
4696 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4697 * waiting for input.
4698 */
4699 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4700 fclose(stdin);
4701 fclose(stdout);
4702 fclose(stderr);
4703
4704 /*
4705 * If any of these open()'s and dup()'s fail, we just continue
4706 * anyway. It's not fatal, and on most systems it will make
4707 * no difference at all. On a few it will cause the execvp()
4708 * to exit with a non-zero status even when the completion
4709 * could be done, which is nothing too serious. If the open()
4710 * or dup() failed we'd just do the same thing ourselves
4711 * anyway -- webb
4712 */
4713 if (fd >= 0)
4714 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004715 vim_ignored = dup(fd); // To replace stdin (fd 0)
4716 vim_ignored = dup(fd); // To replace stdout (fd 1)
4717 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718
Bram Moolenaar0f873732019-12-05 20:28:46 +01004719 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 close(fd);
4721 }
4722 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004723 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004725 || gui.in_use
4726# endif
4727 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 {
4729
Bram Moolenaardf177f62005-02-22 08:39:57 +00004730# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004731 // Create our own process group, so that the child and all its
4732 // children can be kill()ed. Don't do this when using pipes,
4733 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004734 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004735 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004736 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004737# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004738 // When doing "!xterm&" and 'shell' is bash: the shell
4739 // will exit and send SIGHUP to all processes in its
4740 // group, killing the just started process. Ignore SIGHUP
4741 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004742 signal(SIGHUP, SIG_IGN);
4743# endif
4744 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004745# endif
4746# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004747 if (pty_slave_fd >= 0)
4748 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004749 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004750 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004751 setup_slavepty(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01004753 // Try to become controlling tty (probably doesn't work,
4754 // unless run by root)
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004755 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004757 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004758# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004759 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
Bram Moolenaara5792f52005-11-23 21:25:05 +00004761 /*
4762 * stderr is only redirected when using the GUI, so that a
4763 * program like gpg can still access the terminal to get a
4764 * passphrase using stderr.
4765 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004766# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (pty_master_fd >= 0)
4768 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004769 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770
Bram Moolenaar0f873732019-12-05 20:28:46 +01004771 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004773 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004775 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004776 if (gui.in_use)
4777 {
4778 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004779 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
Bram Moolenaar0f873732019-12-05 20:28:46 +01004782 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
4784 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004787 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 close(fd_toshell[1]);
4789 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004790 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 close(fd_toshell[0]);
4792
Bram Moolenaar0f873732019-12-05 20:28:46 +01004793 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 close(fd_fromshell[0]);
4795 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004796 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 close(fd_fromshell[1]);
4798
Bram Moolenaara5792f52005-11-23 21:25:05 +00004799# ifdef FEAT_GUI
4800 if (gui.in_use)
4801 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004802 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004803 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004804 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004805 }
4806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
4808 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004809
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 /*
4811 * There is no type cast for the argv, because the type may be
4812 * different on different machines. This may cause a warning
4813 * message with strict compilers, don't worry about it.
4814 * Call _exit() instead of exit() to avoid closing the connection
4815 * to the X server (esp. with GTK, which uses atexit()).
4816 */
4817 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004818 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004820 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 {
4822 /*
4823 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004824 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 */
4826 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004827 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004828 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004829# ifdef FEAT_JOB_CHANNEL
4830 ++dont_check_job_ended;
4831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 /*
4833 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004834 * This is also used to pipe stdin/stdout to/from the external
4835 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004837 if ((options & (SHELL_READ|SHELL_WRITE))
4838# ifdef FEAT_GUI
4839 || (gui.in_use && show_shell_mess)
4840# endif
4841 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004843# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004845 int buffer_off = 0; // valid bytes in buffer[]
4846 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4847 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 int len;
4849 int p_more_save;
4850 int old_State;
4851 int c;
4852 int toshell_fd;
4853 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004854 garray_T ga;
4855 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004856# ifdef ELAPSED_FUNC
4857 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004858# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
Bram Moolenaardf177f62005-02-22 08:39:57 +00004860# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 if (pty_master_fd >= 0)
4862 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 fromshell_fd = pty_master_fd;
4864 toshell_fd = dup(pty_master_fd);
4865 }
4866 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004867# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
4869 close(fd_toshell[0]);
4870 close(fd_fromshell[1]);
4871 toshell_fd = fd_toshell[1];
4872 fromshell_fd = fd_fromshell[0];
4873 }
4874
4875 /*
4876 * Write to the child if there are typed characters.
4877 * Read from the child if there are characters available.
4878 * Repeat the reading a few times if more characters are
4879 * available. Need to check for typed keys now and then, but
4880 * not too often (delays when no chars are available).
4881 * This loop is quit if no characters can be read from the pty
4882 * (WaitForChar detected special condition), or there are no
4883 * characters available and the child has exited.
4884 * Only check if the child has exited when there is no more
4885 * output. The child may exit before all the output has
4886 * been printed.
4887 *
4888 * Currently this busy loops!
4889 * This can probably dead-lock when the write blocks!
4890 */
4891 p_more_save = p_more;
4892 p_more = FALSE;
4893 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004894 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004896 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004897 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004898 // Fork a process that will write the lines to the
4899 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004900 if ((wpid = fork()) == -1)
4901 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004902 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004903 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004904 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004905 {
4906 linenr_T lnum = curbuf->b_op_start.lnum;
4907 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004908 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004909 size_t l;
4910
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004911 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004912 for (;;)
4913 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004914 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004915 if (l == 0)
4916 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004917 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004918 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004919 len = write(toshell_fd, "", (size_t)1);
4920 else
4921 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004922 char_u *s = vim_strchr(lp + written, NL);
4923
Bram Moolenaar89d40322006-08-29 15:30:07 +00004924 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004925 s == NULL ? l
4926 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004927 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004928 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004929 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004930 // Finished a line, add a NL, unless this line
4931 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004932 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004933 || (!curbuf->b_p_bin
4934 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004935 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004936 && (lnum !=
4937 curbuf->b_ml.ml_line_count
4938 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004939 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004940 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004941 ++lnum;
4942 if (lnum > curbuf->b_op_end.lnum)
4943 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004944 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00004945 close(toshell_fd);
4946 toshell_fd = -1;
4947 break;
4948 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004949 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004950 written = 0;
4951 }
4952 else if (len > 0)
4953 written += len;
4954 }
4955 _exit(0);
4956 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004957 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00004958 {
4959 close(toshell_fd);
4960 toshell_fd = -1;
4961 }
4962 }
4963
4964 if (options & SHELL_READ)
4965 ga_init2(&ga, 1, BUFLEN);
4966
4967 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004968# ifdef ELAPSED_FUNC
4969 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004970# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 for (;;)
4972 {
4973 /*
4974 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004975 * if there are any.
4976 * Don't do this if we are expanding wild cards (would eat
4977 * typeahead).
4978 * Don't do this when filtering and terminal is in cooked
4979 * mode, the shell command will handle the I/O. Avoids
4980 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004981 * Don't get characters when the child has already
4982 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004983 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004984 * while (noread_cnt > 4), avoids that ":r !ls" eats
4985 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 */
4987 len = 0;
4988 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004989 && ((options &
4990 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4991 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004992# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004993 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004994# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004995 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004996 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004997 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004999 if (ta_len == 0)
5000 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005001 // Get extra characters when we don't have any.
5002 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005003 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005004# ifdef ELAPSED_FUNC
5005 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005006# endif
5007 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5008 }
5009 if (ta_len > 0 || len > 0)
5010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 /*
5012 * For pipes:
5013 * Check for CTRL-C: send interrupt signal to child.
5014 * Check for CTRL-D: EOF, close pipe to child.
5015 */
5016 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 /*
5019 * Send SIGINT to the child's group or all
5020 * processes in our group.
5021 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005022 may_send_sigint(ta_buf[ta_len], pid, wpid);
5023
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 if (pty_master_fd < 0 && toshell_fd >= 0
5025 && ta_buf[ta_len] == Ctrl_D)
5026 {
5027 close(toshell_fd);
5028 toshell_fd = -1;
5029 }
5030 }
5031
Bram Moolenaarf4140482020-02-15 23:06:45 +01005032 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033
5034 /*
5035 * For pipes: echo the typed characters.
5036 * For a pty this does not seem to work.
5037 */
5038 if (pty_master_fd < 0)
5039 {
5040 for (i = ta_len; i < ta_len + len; ++i)
5041 {
5042 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5043 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 else if (has_mbyte)
5045 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005046 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 msg_outtrans_len(ta_buf + i, l);
5049 i += l - 1;
5050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 else
5052 msg_outtrans_len(ta_buf + i, 1);
5053 }
5054 windgoto(msg_row, msg_col);
5055 out_flush();
5056 }
5057
5058 ta_len += len;
5059
5060 /*
5061 * Write the characters to the child, unless EOF has
5062 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005063 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005064 * When writing buffer lines, drop the typed
5065 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005067 if (options & SHELL_WRITE)
5068 ta_len = 0;
5069 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
5071 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5072 if (len > 0)
5073 {
5074 ta_len -= len;
5075 mch_memmove(ta_buf, ta_buf + len, ta_len);
5076 }
5077 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080
Bram Moolenaardf177f62005-02-22 08:39:57 +00005081 if (got_int)
5082 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005083 // CTRL-C sends a signal to the child, we ignore it
5084 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005085# ifdef HAVE_SETSID
5086 kill(-pid, SIGINT);
5087# else
5088 kill(0, SIGINT);
5089# endif
5090 if (wpid > 0)
5091 kill(wpid, SIGINT);
5092 got_int = FALSE;
5093 }
5094
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 /*
5096 * Check if the child has any characters to be printed.
5097 * Read them and write them to our window. Repeat this as
5098 * long as there is something to do, avoid the 10ms wait
5099 * for mch_inchar(), or sending typeahead characters to
5100 * the external process.
5101 * TODO: This should handle escape sequences, compatible
5102 * to some terminal (vt52?).
5103 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005104 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005105 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005107 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005110 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005112
5113 noread_cnt = 0;
5114 if (options & SHELL_READ)
5115 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005116 // Do NUL -> NL translation, append NL separated
5117 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005118 for (i = 0; i < len; ++i)
5119 {
5120 if (buffer[i] == NL)
5121 append_ga_line(&ga);
5122 else if (buffer[i] == NUL)
5123 ga_append(&ga, NL);
5124 else
5125 ga_append(&ga, buffer[i]);
5126 }
5127 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005128 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
5130 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005131 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
Bram Moolenaardf177f62005-02-22 08:39:57 +00005133 len += buffer_off;
5134 buffer[len] = NUL;
5135
Bram Moolenaar0f873732019-12-05 20:28:46 +01005136 // Check if the last character in buffer[] is
5137 // incomplete, keep these bytes for the next
5138 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 for (p = buffer; p < buffer + len; p += l)
5140 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005141 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005143 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 else if (MB_BYTE2LEN(*p) != l)
5145 break;
5146 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005147 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005149 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 if (len >= 12)
5151 ++p;
5152 else
5153 {
5154 buffer_off = len;
5155 continue;
5156 }
5157 }
5158 c = *p;
5159 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005160 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if (p < buffer + len)
5162 {
5163 *p = c;
5164 buffer_off = (buffer + len) - p;
5165 mch_memmove(buffer, p, buffer_off);
5166 continue;
5167 }
5168 buffer_off = 0;
5169 }
5170 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
5172 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005173 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
5175
5176 windgoto(msg_row, msg_col);
5177 cursor_on();
5178 out_flush();
5179 if (got_int)
5180 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005181
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005182# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005183 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005184 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005185 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005186
Bram Moolenaar0f873732019-12-05 20:28:46 +01005187 // Avoid that we keep looping here without
5188 // checking for a CTRL-C for a long time. Don't
5189 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005190 if (msec > 2000)
5191 {
5192 noread_cnt = 5;
5193 break;
5194 }
5195 }
5196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
5198
Bram Moolenaar0f873732019-12-05 20:28:46 +01005199 // If we already detected the child has finished, continue
5200 // reading output for a short while. Some text may be
5201 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005202 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005203 {
5204 if (noread_cnt < 5)
5205 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005206 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005207 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005208
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 /*
5210 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005211 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005213# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005214 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005215# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5219 || (wait_pid == pid && WIFEXITED(status)))
5220 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005221 // Don't break the loop yet, try reading more
5222 // characters from "fromshell_fd" first. When using
5223 // pipes there might still be something to read and
5224 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005227 else
5228 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005229
Bram Moolenaar95a51352013-03-21 22:53:50 +01005230# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005231 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005232 clip_update();
5233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
5235finished:
5236 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005237 if (options & SHELL_READ)
5238 {
5239 if (ga.ga_len > 0)
5240 {
5241 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005242 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005243 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005244 }
5245 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005246 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005247 ga_clear(&ga);
5248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 /*
5251 * Give all typeahead that wasn't used back to ui_inchar().
5252 */
5253 if (ta_len)
5254 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 State = old_State;
5256 if (toshell_fd >= 0)
5257 close(toshell_fd);
5258 close(fromshell_fd);
5259 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005260# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005261 else
5262 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005263 long delay_msec = 1;
5264
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005265 /*
5266 * Similar to the loop above, but only handle X events, no
5267 * I/O.
5268 */
5269 for (;;)
5270 {
5271 if (got_int)
5272 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005273 // CTRL-C sends a signal to the child, we ignore it
5274 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005275# ifdef HAVE_SETSID
5276 kill(-pid, SIGINT);
5277# else
5278 kill(0, SIGINT);
5279# endif
5280 got_int = FALSE;
5281 }
5282# ifdef __NeXT__
5283 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5284# else
5285 wait_pid = waitpid(pid, &status, WNOHANG);
5286# endif
5287 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5288 || (wait_pid == pid && WIFEXITED(status)))
5289 {
5290 wait_pid = pid;
5291 break;
5292 }
5293
Bram Moolenaar0f873732019-12-05 20:28:46 +01005294 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005295 clip_update();
5296
Bram Moolenaar0f873732019-12-05 20:28:46 +01005297 // Wait for 1 to 10 msec. 1 is faster but gives the child
5298 // less time.
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005299 mch_delay(delay_msec, TRUE);
5300 if (++delay_msec > 10)
5301 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005302 }
5303 }
5304# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
5306 /*
5307 * Wait until our child has exited.
5308 * Ignore wait() returning pids of other children and returning
5309 * because of some signal like SIGWINCH.
5310 * Don't wait if wait_pid was already set above, indicating the
5311 * child already exited.
5312 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005313 if (wait_pid != pid)
5314 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
Bram Moolenaar624891f2010-10-13 16:22:09 +02005316# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005317 // Close slave side of pty. Only do this after the child has
5318 // exited, otherwise the child may hang when it tries to write on
5319 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005320 if (pty_master_fd >= 0)
5321 close(pty_slave_fd);
5322# endif
5323
Bram Moolenaar0f873732019-12-05 20:28:46 +01005324 // Make sure the child that writes to the external program is
5325 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005326 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005327 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005328 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005329 wait4pid(wpid, NULL);
5330 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005331
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005332# ifdef FEAT_JOB_CHANNEL
5333 --dont_check_job_ended;
5334# endif
5335
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 /*
5337 * Set to raw mode right now, otherwise a CTRL-C after
5338 * catch_signals() will kill Vim.
5339 */
5340 if (tmode == TMODE_RAW)
5341 settmode(TMODE_RAW);
5342 did_settmode = TRUE;
5343 set_signals();
5344
5345 if (WIFEXITED(status))
5346 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005347 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005349 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {
5351 if (retval == EXEC_FAILED)
5352 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005353 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 msg_outtrans(p_sh);
5355 msg_putchar('\n');
5356 }
5357 else if (!(options & SHELL_SILENT))
5358 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005359 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 msg_outnum((long)retval);
5361 msg_putchar('\n');
5362 }
5363 }
5364 }
5365 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005366 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 }
5368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
5370error:
5371 if (!did_settmode)
5372 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005373 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374# ifdef FEAT_TITLE
5375 resettitle();
5376# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005377 vim_free(argv);
5378 vim_free(tofree1);
5379 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380
5381 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005382}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005383#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005384
5385 int
5386mch_call_shell(
5387 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005388 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005389{
5390#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5391 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5392 return mch_call_shell_terminal(cmd, options);
5393#endif
5394#ifdef USE_SYSTEM
5395 return mch_call_shell_system(cmd, options);
5396#else
5397 return mch_call_shell_fork(cmd, options);
5398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399}
5400
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005401#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005402 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005403mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005404{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005405 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005406 int fd_in[2] = {-1, -1}; // for stdin
5407 int fd_out[2] = {-1, -1}; // for stdout
5408 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005409 int pty_master_fd = -1;
5410 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005411 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005412 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5413 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5414 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005415 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005416 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5417 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005418 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005419 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005420 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005421
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005422 if (use_out_for_err && use_null_for_out)
5423 use_null_for_err = TRUE;
5424
Bram Moolenaar0f873732019-12-05 20:28:46 +01005425 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005426 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005427
Bram Moolenaarb2412082017-08-20 18:09:14 +02005428 if (options->jo_pty
5429 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005430 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005431 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005432 open_pty(&pty_master_fd, &pty_slave_fd,
5433 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005434
Bram Moolenaar0f873732019-12-05 20:28:46 +01005435 // TODO: without the channel feature connect the child to /dev/null?
5436 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005437 if (use_file_for_in)
5438 {
5439 char_u *fname = options->jo_io_name[PART_IN];
5440
5441 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5442 if (fd_in[0] < 0)
5443 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005444 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005445 goto failed;
5446 }
5447 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005448 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005449 // When writing buffer lines to the input don't use the pty, so that
5450 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005451 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5452 && pipe(fd_in) < 0)
5453 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005454
5455 if (use_file_for_out)
5456 {
5457 char_u *fname = options->jo_io_name[PART_OUT];
5458
5459 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5460 if (fd_out[1] < 0)
5461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005462 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005463 goto failed;
5464 }
5465 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005466 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005467 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005468
5469 if (use_file_for_err)
5470 {
5471 char_u *fname = options->jo_io_name[PART_ERR];
5472
5473 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5474 if (fd_err[1] < 0)
5475 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005476 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005477 goto failed;
5478 }
5479 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005480 else if (!use_out_for_err && !use_null_for_err
5481 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005482 goto failed;
5483
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005484 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5485 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005486 if (options->jo_set & JO_CHANNEL)
5487 {
5488 channel = options->jo_channel;
5489 if (channel != NULL)
5490 ++channel->ch_refcount;
5491 }
5492 else
5493 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005494 if (channel == NULL)
5495 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005496 if (job->jv_tty_out != NULL)
5497 ch_log(channel, "using pty %s on fd %d",
5498 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005499 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005500
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005501 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005502 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005503 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005504 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005505 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005506 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005507 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005508 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005509 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005510 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005511 int null_fd = -1;
5512 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005513
Bram Moolenaar0f873732019-12-05 20:28:46 +01005514 // child
5515 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005516 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005517
Bram Moolenaar819524702018-02-27 19:10:00 +01005518# ifdef FEAT_JOB_CHANNEL
5519 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005520 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005521 ch_logfile((char_u *)"", (char_u *)"");
5522# endif
5523
Bram Moolenaar835dc632016-02-07 14:27:38 +01005524# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005525 // Create our own process group, so that the child and all its
5526 // children can be kill()ed. Don't do this when using pipes,
5527 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005528 (void)setsid();
5529# endif
5530
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005531# ifdef FEAT_TERMINAL
5532 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005533 {
5534 char *term = (char *)T_NAME;
5535
5536#ifdef FEAT_GUI
5537 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005538 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005539 term = getenv("TERM");
5540#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005541 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005542 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005543 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005544 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005545 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005546 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005547 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005548 else if (t_colors > 16)
5549 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005550 else
5551 term = "xterm";
5552 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005553 set_child_environment(
5554 (long)options->jo_term_rows,
5555 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005556 term,
5557 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005558 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005559 else
5560# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005561 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005562
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005563 if (options->jo_env != NULL)
5564 {
5565 dict_T *dict = options->jo_env;
5566 hashitem_T *hi;
5567 int todo = (int)dict->dv_hashtab.ht_used;
5568
5569 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5570 if (!HASHITEM_EMPTY(hi))
5571 {
5572 typval_T *item = &dict_lookup(hi)->di_tv;
5573
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005574 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005575 --todo;
5576 }
5577 }
5578
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005579 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005580 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005581 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005582 if (null_fd < 0)
5583 {
5584 perror("opening /dev/null failed");
5585 _exit(OPEN_NULL_FAILED);
5586 }
5587 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005588
Bram Moolenaar223896d2017-08-02 22:33:28 +02005589 if (pty_slave_fd >= 0)
5590 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005591 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005592 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005593# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005594 // Try to become controlling tty (probably doesn't work,
5595 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005596 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5597# endif
5598 }
5599
Bram Moolenaar0f873732019-12-05 20:28:46 +01005600 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005601 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005602 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005603 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005604 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005605 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005606 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005607 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005608
Bram Moolenaar0f873732019-12-05 20:28:46 +01005609 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005610 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005611 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005612 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005613 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005614 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005615 }
5616 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005617 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005618 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005619 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005620 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005621 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005622
Bram Moolenaar0f873732019-12-05 20:28:46 +01005623 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005624 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005625 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005626 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005627 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005628 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005629 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005630 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005631
5632 if (fd_in[0] >= 0)
5633 close(fd_in[0]);
5634 if (fd_in[1] >= 0)
5635 close(fd_in[1]);
5636 if (fd_out[0] >= 0)
5637 close(fd_out[0]);
5638 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005639 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005640 if (fd_err[0] >= 0)
5641 close(fd_err[0]);
5642 if (fd_err[1] >= 0)
5643 close(fd_err[1]);
5644 if (pty_master_fd >= 0)
5645 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005646 close(pty_master_fd); // not used in the child
5647 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005648 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005649
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005650 if (null_fd >= 0)
5651 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005652
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005653 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5654 _exit(EXEC_FAILED);
5655
Bram Moolenaar0f873732019-12-05 20:28:46 +01005656 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005657 execvp(argv[0], argv);
5658
Bram Moolenaar4694a172016-04-21 14:05:23 +02005659 if (stderr_works)
5660 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005661# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005662 // calling free_all_mem() here causes problems. Ignore valgrind
5663 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005664# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005665 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005666 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005667
Bram Moolenaar0f873732019-12-05 20:28:46 +01005668 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005669 UNBLOCK_SIGNALS(&curset);
5670
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005671 job->jv_pid = pid;
5672 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005673 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005674
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005675 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005676 close(pty_slave_fd); // not used in the parent
5677 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005678 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005679 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005680 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005681 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005682 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005683 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005684 if (channel != NULL)
5685 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005686 int in_fd = INVALID_FD;
5687 int out_fd = INVALID_FD;
5688 int err_fd = INVALID_FD;
5689
5690 if (!(use_file_for_in || use_null_for_in))
5691 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5692
5693 if (!(use_file_for_out || use_null_for_out))
5694 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5695
5696 // When using pty_master_fd only set it for stdout, do not duplicate
5697 // it for stderr, it only needs to be read once.
5698 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5699 {
5700 if (fd_err[0] >= 0)
5701 err_fd = fd_err[0];
5702 else if (out_fd != pty_master_fd)
5703 err_fd = pty_master_fd;
5704 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005705
5706 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005707 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005708 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005709 else
5710 {
5711 if (fd_in[1] >= 0)
5712 close(fd_in[1]);
5713 if (fd_out[0] >= 0)
5714 close(fd_out[0]);
5715 if (fd_err[0] >= 0)
5716 close(fd_err[0]);
5717 if (pty_master_fd >= 0)
5718 close(pty_master_fd);
5719 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005720
Bram Moolenaar0f873732019-12-05 20:28:46 +01005721 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005722 return;
5723
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005724failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005725 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005726 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005727 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005728 if (fd_in[1] >= 0)
5729 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005730 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005731 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005732 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005733 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005734 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005735 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005736 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005737 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005738 if (pty_master_fd >= 0)
5739 close(pty_master_fd);
5740 if (pty_slave_fd >= 0)
5741 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005742}
5743
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005744 static char_u *
5745get_signal_name(int sig)
5746{
5747 int i;
5748 char_u numbuf[NUMBUFLEN];
5749
5750 if (sig == SIGKILL)
5751 return vim_strsave((char_u *)"kill");
5752
5753 for (i = 0; signal_info[i].sig != -1; i++)
5754 if (sig == signal_info[i].sig)
5755 return strlow_save((char_u *)signal_info[i].name);
5756
5757 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5758 return vim_strsave(numbuf);
5759}
5760
Bram Moolenaar835dc632016-02-07 14:27:38 +01005761 char *
5762mch_job_status(job_T *job)
5763{
5764# ifdef HAVE_UNION_WAIT
5765 union wait status;
5766# else
5767 int status = -1;
5768# endif
5769 pid_t wait_pid = 0;
5770
5771# ifdef __NeXT__
5772 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5773# else
5774 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5775# endif
5776 if (wait_pid == -1)
5777 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005778 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005779 if (job->jv_status < JOB_ENDED)
5780 ch_log(job->jv_channel, "Job no longer exists: %s",
5781 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005782 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005783 }
5784 if (wait_pid == 0)
5785 return "run";
5786 if (WIFEXITED(status))
5787 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005788 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005789 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005790 if (job->jv_status < JOB_ENDED)
5791 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005792 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005793 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005794 if (WIFSIGNALED(status))
5795 {
5796 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005797 job->jv_termsig = get_signal_name(WTERMSIG(status));
5798 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5799 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5800 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005801 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005802 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005803 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005804
5805return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005806 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005807 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005808 return "dead";
5809}
5810
5811 job_T *
5812mch_detect_ended_job(job_T *job_list)
5813{
5814# ifdef HAVE_UNION_WAIT
5815 union wait status;
5816# else
5817 int status = -1;
5818# endif
5819 pid_t wait_pid = 0;
5820 job_T *job;
5821
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005822# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005823 // Do not do this when waiting for a shell command to finish, we would get
5824 // the exit value here (and discard it), the exit value obtained there
5825 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005826 if (dont_check_job_ended > 0)
5827 return NULL;
5828# endif
5829
Bram Moolenaar97792de2016-10-15 18:36:49 +02005830# ifdef __NeXT__
5831 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5832# else
5833 wait_pid = waitpid(-1, &status, WNOHANG);
5834# endif
5835 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005836 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005837 return NULL;
5838 for (job = job_list; job != NULL; job = job->jv_next)
5839 {
5840 if (job->jv_pid == wait_pid)
5841 {
5842 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005843 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005844 job->jv_exitval = WEXITSTATUS(status);
5845 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005846 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005847 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005848 job->jv_termsig = get_signal_name(WTERMSIG(status));
5849 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005850 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005851 {
5852 ch_log(job->jv_channel, "Job ended");
5853 job->jv_status = JOB_ENDED;
5854 }
5855 return job;
5856 }
5857 }
5858 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005859}
5860
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005861/*
5862 * Send a (deadly) signal to "job".
5863 * Return FAIL if "how" is not a valid name.
5864 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005865 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005866mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005867{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005868 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005869
Bram Moolenaar923d9262016-02-25 20:56:01 +01005870 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005871 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005872 else if (STRCMP(how, "hup") == 0)
5873 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005874 else if (STRCMP(how, "quit") == 0)
5875 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005876 else if (STRCMP(how, "int") == 0)
5877 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005878 else if (STRCMP(how, "kill") == 0)
5879 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005880#ifdef SIGWINCH
5881 else if (STRCMP(how, "winch") == 0)
5882 sig = SIGWINCH;
5883#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005884 else if (isdigit(*how))
5885 sig = atoi((char *)how);
5886 else
5887 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005888
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005889 // Never kill ourselves!
5890 if (job->jv_pid != 0)
5891 {
5892 // TODO: have an option to only kill the process, not the group?
5893 kill(-job->jv_pid, sig);
5894 kill(job->jv_pid, sig);
5895 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005896
Bram Moolenaar835dc632016-02-07 14:27:38 +01005897 return OK;
5898}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005899
5900/*
5901 * Clear the data related to "job".
5902 */
5903 void
5904mch_clear_job(job_T *job)
5905{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005906 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005907# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005908 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005909# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005910 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005911# endif
5912}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005913#endif
5914
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005915#if defined(FEAT_TERMINAL) || defined(PROTO)
5916 int
5917mch_create_pty_channel(job_T *job, jobopt_T *options)
5918{
5919 int pty_master_fd = -1;
5920 int pty_slave_fd = -1;
5921 channel_T *channel;
5922
Bram Moolenaar59386482019-02-10 22:43:46 +01005923 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02005924 if (pty_master_fd < 0 || pty_slave_fd < 0)
5925 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005926 close(pty_slave_fd);
5927
5928 channel = add_channel();
5929 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005930 {
5931 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005932 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005933 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005934 if (job->jv_tty_out != NULL)
5935 ch_log(channel, "using pty %s on fd %d",
5936 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005937 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005938 channel->ch_keep_open = TRUE;
5939
Bram Moolenaar0f873732019-12-05 20:28:46 +01005940 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5941 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005942 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005943 channel_set_job(channel, job, options);
5944 return OK;
5945}
5946#endif
5947
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948/*
5949 * Check for CTRL-C typed by reading all available characters.
5950 * In cooked mode we should get SIGINT, no need to check.
5951 */
5952 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005953mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
Bram Moolenaar26e86442020-05-17 14:06:16 +02005955 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005956 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 fill_input_buf(FALSE);
5958}
5959
5960/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005961 * Wait "msec" msec until a character is available from the mouse, keyboard,
5962 * from inbuf[].
5963 * "msec" == -1 will block forever.
5964 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005965 * When "ignore_input" is TRUE even check for pending input when input is
5966 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005967 * "interrupted" (if not NULL) is set to TRUE when no character is available
5968 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005969 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005970 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 */
5972 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005973WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005975#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005976 return ui_wait_for_chars_or_timer(
5977 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005978#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005979 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005980#endif
5981}
5982
5983/*
5984 * Wait "msec" msec until a character is available from the mouse or keyboard
5985 * or from inbuf[].
5986 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005987 * for "ignore_input" see WaitForCharOr().
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 Moolenaar943bb2b2016-03-19 14:11:18 +01005990 * When a GUI is being used, this will never get called -- webb
5991 */
5992 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005993WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005994{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995#ifdef FEAT_MOUSE_GPM
5996 int gpm_process_wanted;
5997#endif
5998#ifdef FEAT_XCLIPBOARD
5999 int rest;
6000#endif
6001 int avail;
6002
Bram Moolenaar0f873732019-12-05 20:28:46 +01006003 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 return 1;
6005
6006#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006007 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 if (WantQueryMouse)
6009 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006010 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006011 if (!no_query_mouse_for_testing)
6012 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 }
6014#endif
6015
6016 /*
6017 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6018 * events. This is a bit complicated, because they might both be defined.
6019 */
6020#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6021# ifdef FEAT_XCLIPBOARD
6022 rest = 0;
6023 if (do_xterm_trace())
6024 rest = msec;
6025# endif
6026 do
6027 {
6028# ifdef FEAT_XCLIPBOARD
6029 if (rest != 0)
6030 {
6031 msec = XT_TRACE_DELAY;
6032 if (rest >= 0 && rest < XT_TRACE_DELAY)
6033 msec = rest;
6034 if (rest >= 0)
6035 rest -= msec;
6036 }
6037# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006038# ifdef FEAT_SOUND_CANBERRA
6039 // Invoke any pending sound callbacks.
6040 if (has_sound_callback_in_queue())
6041 invoke_sound_callback();
6042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043# ifdef FEAT_MOUSE_GPM
6044 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006045 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006046 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006047 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006049 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006051# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006053 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 return 1;
6055# ifdef FEAT_XCLIPBOARD
6056 if (rest == 0 || !do_xterm_trace())
6057# endif
6058 break;
6059 }
6060 }
6061 while (FALSE
6062# ifdef FEAT_MOUSE_GPM
6063 || (gpm_process_wanted && mch_gpm_process() == 0)
6064# endif
6065# ifdef FEAT_XCLIPBOARD
6066 || (!avail && rest != 0)
6067# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006068 )
6069 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070
6071#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006072 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073#endif
6074 return avail;
6075}
6076
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006077#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078/*
6079 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006080 * "msec" == 0 will check for characters once.
6081 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006084 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006085 * "interrupted" (if not NULL) is set to TRUE when no character is available
6086 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006088 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006089RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090{
6091 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006092 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006093#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 static int busy = FALSE;
6095
Bram Moolenaar0f873732019-12-05 20:28:46 +01006096 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097# define MAY_LOOP
6098
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006099# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006100 // Remember at what time we started, so that we know how much longer we
6101 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006102 long start_msec = msec;
6103 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006105 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006106 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107# endif
6108
Bram Moolenaar0f873732019-12-05 20:28:46 +01006109 // Handle being called recursively. This may happen for the session
6110 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 if (busy)
6112 return 0;
6113#endif
6114
6115#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006116 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117#endif
6118 {
6119#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006120 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006121# ifdef FEAT_MZSCHEME
6122 int mzquantum_used = FALSE;
6123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124#endif
6125#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006126 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006127 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 int nfd;
6129# ifdef FEAT_XCLIPBOARD
6130 int xterm_idx = -1;
6131# endif
6132# ifdef FEAT_MOUSE_GPM
6133 int gpm_idx = -1;
6134# endif
6135# ifdef USE_XSMP
6136 int xsmp_idx = -1;
6137# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006138 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006140# ifdef FEAT_MZSCHEME
6141 mzvim_check_threads();
6142 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6143 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006144 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006145 mzquantum_used = TRUE;
6146 }
6147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 fds[0].fd = fd;
6149 fds[0].events = POLLIN;
6150 nfd = 1;
6151
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006153 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 if (xterm_Shell != (Widget)0)
6155 {
6156 xterm_idx = nfd;
6157 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6158 fds[nfd].events = POLLIN;
6159 nfd++;
6160 }
6161# endif
6162# ifdef FEAT_MOUSE_GPM
6163 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6164 {
6165 gpm_idx = nfd;
6166 fds[nfd].fd = gpm_fd;
6167 fds[nfd].events = POLLIN;
6168 nfd++;
6169 }
6170# endif
6171# ifdef USE_XSMP
6172 if (xsmp_icefd != -1)
6173 {
6174 xsmp_idx = nfd;
6175 fds[nfd].fd = xsmp_icefd;
6176 fds[nfd].events = POLLIN;
6177 nfd++;
6178 }
6179# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006180#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006181 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006182#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006183 if (interrupted != NULL)
6184 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006186 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006187
6188 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006189 if (result == 0 && interrupted != NULL && ret > 0)
6190 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006191
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006192# ifdef FEAT_MZSCHEME
6193 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006194 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006195 finished = FALSE;
6196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198# ifdef FEAT_XCLIPBOARD
6199 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6200 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006201 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006203 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 finished = FALSE;
6205 }
6206# endif
6207# ifdef FEAT_MOUSE_GPM
6208 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210# endif
6211# ifdef USE_XSMP
6212 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6213 {
6214 if (fds[xsmp_idx].revents & POLLIN)
6215 {
6216 busy = TRUE;
6217 xsmp_handle_requests();
6218 busy = FALSE;
6219 }
6220 else if (fds[xsmp_idx].revents & POLLHUP)
6221 {
6222 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006223 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 xsmp_close();
6225 }
6226 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006227 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 }
6229# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006230#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006231 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006232 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006233 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235
Bram Moolenaar0f873732019-12-05 20:28:46 +01006236#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237
6238 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006239 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006240 // These are static because they can take 8 Kbyte each and cause the
6241 // signal stack to run out with -O3.
6242 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006244 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006246# ifdef FEAT_MZSCHEME
6247 mzvim_check_threads();
6248 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6249 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006250 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006251 mzquantum_used = TRUE;
6252 }
6253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006255 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006257 tv.tv_sec = towait / 1000;
6258 tv.tv_usec = (towait % 1000) * (1000000/1000);
6259 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006261 else
6262 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263
6264 /*
6265 * Select on ready for reading and exceptional condition (end of file).
6266 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006267select_eintr:
6268 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006269 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 FD_ZERO(&efds);
6271 FD_SET(fd, &rfds);
6272# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006273 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 FD_SET(fd, &efds);
6275# endif
6276 maxfd = fd;
6277
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006279 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 if (xterm_Shell != (Widget)0)
6281 {
6282 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6283 if (maxfd < ConnectionNumber(xterm_dpy))
6284 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006285
Bram Moolenaar0f873732019-12-05 20:28:46 +01006286 // An event may have already been read but not handled. In
6287 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006288 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 }
6290# endif
6291# ifdef FEAT_MOUSE_GPM
6292 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6293 {
6294 FD_SET(gpm_fd, &rfds);
6295 FD_SET(gpm_fd, &efds);
6296 if (maxfd < gpm_fd)
6297 maxfd = gpm_fd;
6298 }
6299# endif
6300# ifdef USE_XSMP
6301 if (xsmp_icefd != -1)
6302 {
6303 FD_SET(xsmp_icefd, &rfds);
6304 FD_SET(xsmp_icefd, &efds);
6305 if (maxfd < xsmp_icefd)
6306 maxfd = xsmp_icefd;
6307 }
6308# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006309# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006310 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006311# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006312 if (interrupted != NULL)
6313 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
Bram Moolenaar643b6142018-09-12 20:29:09 +02006315 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6316 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006317 result = ret > 0 && FD_ISSET(fd, &rfds);
6318 if (result)
6319 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006320 else if (interrupted != NULL && ret > 0)
6321 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006322
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006323# ifdef EINTR
6324 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006325 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006326 // Check whether window has been resized, EINTR may be caused by
6327 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006328 if (do_resize)
6329 handle_resize();
6330
Bram Moolenaar0f873732019-12-05 20:28:46 +01006331 // Interrupted by a signal, need to try again. We ignore msec
6332 // here, because we do want to check even after a timeout if
6333 // characters are available. Needed for reading output of an
6334 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006335 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006336 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006337# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006338# ifdef __TANDEM
6339 if (ret == -1 && errno == ENOTSUP)
6340 {
6341 FD_ZERO(&rfds);
6342 FD_ZERO(&efds);
6343 ret = 0;
6344 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006345# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006346# ifdef FEAT_MZSCHEME
6347 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006348 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006349 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350# endif
6351
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352# ifdef FEAT_XCLIPBOARD
6353 if (ret > 0 && xterm_Shell != (Widget)0
6354 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6355 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006356 xterm_update(); // Maybe we should hand out clipboard
6357 // continue looping when we only got the X event and the input
6358 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 if (--ret == 0 && !input_available())
6360 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006361 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 finished = FALSE;
6363 }
6364 }
6365# endif
6366# ifdef FEAT_MOUSE_GPM
6367 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6368 {
6369 if (FD_ISSET(gpm_fd, &efds))
6370 gpm_close();
6371 else if (FD_ISSET(gpm_fd, &rfds))
6372 *check_for_gpm = 1;
6373 }
6374# endif
6375# ifdef USE_XSMP
6376 if (ret > 0 && xsmp_icefd != -1)
6377 {
6378 if (FD_ISSET(xsmp_icefd, &efds))
6379 {
6380 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006381 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 xsmp_close();
6383 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006384 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 }
6386 else if (FD_ISSET(xsmp_icefd, &rfds))
6387 {
6388 busy = TRUE;
6389 xsmp_handle_requests();
6390 busy = FALSE;
6391 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006392 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 }
6394 }
6395# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006396#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006397 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006398 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006399 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
Bram Moolenaar0f873732019-12-05 20:28:46 +01006402#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403
6404#ifdef MAY_LOOP
6405 if (finished || msec == 0)
6406 break;
6407
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006408# ifdef FEAT_CLIENTSERVER
6409 if (server_waiting())
6410 break;
6411# endif
6412
Bram Moolenaar0f873732019-12-05 20:28:46 +01006413 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 if (msec > 0)
6415 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006416# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006417 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006418 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006420 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 msec = msec / 2;
6422# endif
6423 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006424 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 }
6426#endif
6427 }
6428
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006429 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430}
6431
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006433 * Expand a path into all matching files and/or directories. Handles "*",
6434 * "?", "[a-z]", "**", etc.
6435 * "path" has backslashes before chars that are not to be expanded.
6436 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 */
6438 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006439mch_expandpath(
6440 garray_T *gap,
6441 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006442 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
Bram Moolenaar02743632005-07-25 20:42:36 +00006444 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446
6447/*
6448 * mch_expand_wildcards() - this code does wild-card pattern matching using
6449 * the shell
6450 *
6451 * return OK for success, FAIL for error (you may lose some memory) and put
6452 * an error message in *file.
6453 *
6454 * num_pat is number of input patterns
6455 * pat is array of pointers to input patterns
6456 * num_file is pointer to number of matched file names
6457 * file is pointer to array of pointers to matched file names
6458 */
6459
6460#ifndef SEEK_SET
6461# define SEEK_SET 0
6462#endif
6463#ifndef SEEK_END
6464# define SEEK_END 2
6465#endif
6466
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006467#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006468
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006470mch_expand_wildcards(
6471 int num_pat,
6472 char_u **pat,
6473 int *num_file,
6474 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006475 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476{
6477 int i;
6478 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006479 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 char_u *p;
6481 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
Bram Moolenaarc7247912008-01-13 12:54:11 +00006483 /*
6484 * This is the non-OS/2 implementation (really Unix).
6485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 int j;
6487 char_u *tempname;
6488 char_u *command;
6489 FILE *fd;
6490 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006491#define STYLE_ECHO 0 // use "echo", the default
6492#define STYLE_GLOB 1 // use "glob", for csh
6493#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6494#define STYLE_PRINT 3 // use "print -N", for zsh
6495#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6496 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 int shell_style = STYLE_ECHO;
6498 int check_spaces;
6499 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006500 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006501 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006502 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
Bram Moolenaar0f873732019-12-05 20:28:46 +01006504 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 *file = NULL;
6506
6507 /*
6508 * If there are no wildcards, just copy the names to allocated memory.
6509 * Saves a lot of time, because we don't have to start a new shell.
6510 */
6511 if (!have_wildcard(num_pat, pat))
6512 return save_patterns(num_pat, pat, num_file, file);
6513
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006514# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006515 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006516 if (sandbox != 0 && check_secure())
6517 return FAIL;
6518# endif
6519
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 /*
6521 * Don't allow the use of backticks in secure and restricted mode.
6522 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006523 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 for (i = 0; i < num_pat; ++i)
6525 if (vim_strchr(pat[i], '`') != NULL
6526 && (check_restricted() || check_secure()))
6527 return FAIL;
6528
6529 /*
6530 * get a name for the temp file
6531 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006532 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006534 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 return FAIL;
6536 }
6537
6538 /*
6539 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006540 * file.
6541 * STYLE_BT: NL separated
6542 * If expanding `cmd` execute it directly.
6543 * STYLE_GLOB: NUL separated
6544 * If we use *csh, "glob" will work better than "echo".
6545 * STYLE_PRINT: NL or NUL separated
6546 * If we use *zsh, "print -N" will work better than "glob".
6547 * STYLE_VIMGLOB: NL separated
6548 * If we use *sh*, we define "vimglob()".
6549 * STYLE_ECHO: space separated.
6550 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 */
6552 if (num_pat == 1 && *pat[0] == '`'
6553 && (len = STRLEN(pat[0])) > 2
6554 && *(pat[0] + len - 1) == '`')
6555 shell_style = STYLE_BT;
6556 else if ((len = STRLEN(p_sh)) >= 3)
6557 {
6558 if (STRCMP(p_sh + len - 3, "csh") == 0)
6559 shell_style = STYLE_GLOB;
6560 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6561 shell_style = STYLE_PRINT;
6562 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006563 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6564 "sh") != NULL)
6565 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
Bram Moolenaar0f873732019-12-05 20:28:46 +01006567 // Compute the length of the command. We need 2 extra bytes: for the
6568 // optional '&' and for the NUL.
6569 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006571 if (shell_style == STYLE_VIMGLOB)
6572 len += STRLEN(sh_vimglob_func);
6573
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006574 for (i = 0; i < num_pat; ++i)
6575 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006576 // Count the length of the patterns in the same way as they are put in
6577 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006578#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006579 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006580#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006581 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006582 for (j = 0; pat[i][j] != NUL; ++j)
6583 {
6584 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006585 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006586 ++len;
6587 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006588#endif
6589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 command = alloc(len);
6591 if (command == NULL)
6592 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006593 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 vim_free(tempname);
6595 return FAIL;
6596 }
6597
6598 /*
6599 * Build the shell command:
6600 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6601 * recognizes this).
6602 * - Add the shell command to print the expanded names.
6603 * - Add the temp file name.
6604 * - Add the file name patterns.
6605 */
6606 if (shell_style == STYLE_BT)
6607 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006608 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006609 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006610 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006612 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006613 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006615 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006617 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 *p = ' ';
6619 }
6620 STRCAT(command, ">");
6621 }
6622 else
6623 {
6624 if (flags & EW_NOTFOUND)
6625 STRCPY(command, "set nonomatch; ");
6626 else
6627 STRCPY(command, "unset nonomatch; ");
6628 if (shell_style == STYLE_GLOB)
6629 STRCAT(command, "glob >");
6630 else if (shell_style == STYLE_PRINT)
6631 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006632 else if (shell_style == STYLE_VIMGLOB)
6633 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 else
6635 STRCAT(command, "echo >");
6636 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006637
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006639
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 if (shell_style != STYLE_BT)
6641 for (i = 0; i < num_pat; ++i)
6642 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006643 // When using system() always add extra quotes, because the shell
6644 // is started twice. Otherwise put a backslash before special
6645 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646#ifdef USE_SYSTEM
6647 STRCAT(command, " \"");
6648 STRCAT(command, pat[i]);
6649 STRCAT(command, "\"");
6650#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006651 int intick = FALSE;
6652
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 p = command + STRLEN(command);
6654 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006655 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006656 {
6657 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006658 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006659 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6660 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006661 // Remove a backslash, take char literally. But keep
6662 // backslash inside backticks, before a special character
6663 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006664 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006665 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6666 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006667 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006668 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006669 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006670 else if (!intick
6671 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6672 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006673 // Put a backslash before a special character, but not
6674 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6675 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006676 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006677
Bram Moolenaar0f873732019-12-05 20:28:46 +01006678 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006679 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 *p = NUL;
6682#endif
6683 }
6684 if (flags & EW_SILENT)
6685 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006686 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006687 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688
6689 /*
6690 * Using zsh -G: If a pattern has no matches, it is just deleted from
6691 * the argument list, otherwise zsh gives an error message and doesn't
6692 * expand any other pattern.
6693 */
6694 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006695 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
6697 /*
6698 * If we use -f then shell variables set in .cshrc won't get expanded.
6699 * vi can do it, so we will too, but it is only necessary if there is a "$"
6700 * in one of the patterns, otherwise we can still use the fast option.
6701 */
6702 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006703 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704
6705 /*
6706 * execute the shell command
6707 */
6708 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6709
Bram Moolenaar0f873732019-12-05 20:28:46 +01006710 // When running in the background, give it some time to create the temp
6711 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006712 if (ampersand)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 mch_delay(10L, TRUE);
6714
Bram Moolenaar0f873732019-12-05 20:28:46 +01006715 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 show_shell_mess = TRUE;
6717 vim_free(command);
6718
Bram Moolenaar0f873732019-12-05 20:28:46 +01006719 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 {
6721 mch_remove(tempname);
6722 vim_free(tempname);
6723 /*
6724 * With interactive completion, the error message is not printed.
6725 * However with USE_SYSTEM, I don't know how to turn off error messages
6726 * from the shell, so screen may still get messed up -- webb.
6727 */
6728#ifndef USE_SYSTEM
6729 if (!(flags & EW_SILENT))
6730#endif
6731 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006732 redraw_later_clear(); // probably messed up screen
6733 msg_putchar('\n'); // clear bottom line quickly
6734 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735#ifdef USE_SYSTEM
6736 if (!(flags & EW_SILENT))
6737#endif
6738 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006739 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006740 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 }
6742 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006743 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6744 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 if (shell_style == STYLE_BT)
6746 return FAIL;
6747 goto notfound;
6748 }
6749
6750 /*
6751 * read the names from the file into memory
6752 */
6753 fd = fopen((char *)tempname, READBIN);
6754 if (fd == NULL)
6755 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006756 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 if (!(flags & EW_SILENT))
6758 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006759 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006760 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 }
6762 vim_free(tempname);
6763 goto notfound;
6764 }
6765 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006766 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006768 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006769 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006770 buffer = NULL;
6771 else
6772 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 if (buffer == NULL)
6774 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006775 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 mch_remove(tempname);
6777 vim_free(tempname);
6778 fclose(fd);
6779 return FAIL;
6780 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006781 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 i = fread((char *)buffer, 1, len, fd);
6783 fclose(fd);
6784 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006785 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006787 // unexpected read error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006788 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 vim_free(tempname);
6790 vim_free(buffer);
6791 return FAIL;
6792 }
6793 vim_free(tempname);
6794
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006795# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006796 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006798 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6800 *p++ = buffer[i];
6801 len = p - buffer;
6802# endif
6803
6804
Bram Moolenaar0f873732019-12-05 20:28:46 +01006805 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (shell_style == STYLE_ECHO)
6807 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006808 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006810 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {
6812 while (*p != ' ' && *p != '\n')
6813 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006814 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
6816 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006817 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006818 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006820 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006822 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 {
6824 while (*p != '\n' && *p != NUL)
6825 ++p;
6826 if (*p != NUL)
6827 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006828 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 }
6830 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006831 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 else
6833 {
6834 /*
6835 * Some versions of zsh use spaces instead of NULs to separate
6836 * results. Only do this when there is no NUL before the end of the
6837 * buffer, otherwise we would never be able to use file names with
6838 * embedded spaces when zsh does use NULs.
6839 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6840 * don't check for spaces again.
6841 */
6842 check_spaces = FALSE;
6843 if (shell_style == STYLE_PRINT && !did_find_nul)
6844 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006845 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006846 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006847 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 did_find_nul = TRUE;
6849 else
6850 check_spaces = TRUE;
6851 }
6852
6853 /*
6854 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6855 * already is one, for STYLE_GLOB it needs to be added.
6856 */
6857 if (len && buffer[len - 1] == NUL)
6858 --len;
6859 else
6860 buffer[len] = NUL;
6861 i = 0;
6862 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006863 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 {
6865 ++i;
6866 *p = NUL;
6867 }
6868 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006869 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 }
6871 if (i == 0)
6872 {
6873 /*
6874 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6875 * /bin/sh will happily expand it to nothing rather than returning an
6876 * error; and hey, it's good to check anyway -- webb.
6877 */
6878 vim_free(buffer);
6879 goto notfound;
6880 }
6881 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006882 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 if (*file == NULL)
6884 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006885 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 vim_free(buffer);
6887 return FAIL;
6888 }
6889
6890 /*
6891 * Isolate the individual file names.
6892 */
6893 p = buffer;
6894 for (i = 0; i < *num_file; ++i)
6895 {
6896 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006897 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006898 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6899 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006901 while (!(shell_style == STYLE_ECHO && *p == ' ')
6902 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006904 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 *p = NUL;
6906 else
6907 {
6908 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006909 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 }
6911 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006912 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006914 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006916 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 }
6918 }
6919
6920 /*
6921 * Move the file names to allocated memory.
6922 */
6923 for (j = 0, i = 0; i < *num_file; ++i)
6924 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006925 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6927 continue;
6928
Bram Moolenaar0f873732019-12-05 20:28:46 +01006929 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 dir = (mch_isdir((*file)[i]));
6931 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6932 continue;
6933
Bram Moolenaar0f873732019-12-05 20:28:46 +01006934 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01006935 if (!dir && (flags & EW_EXEC)
6936 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006937 continue;
6938
Bram Moolenaar964b3742019-05-24 18:54:09 +02006939 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 if (p)
6941 {
6942 STRCPY(p, (*file)[i]);
6943 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006944 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 (*file)[j++] = p;
6946 }
6947 }
6948 vim_free(buffer);
6949 *num_file = j;
6950
Bram Moolenaar0f873732019-12-05 20:28:46 +01006951 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006953 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 goto notfound;
6955 }
6956
6957 return OK;
6958
6959notfound:
6960 if (flags & EW_NOTFOUND)
6961 return save_patterns(num_pat, pat, num_file, file);
6962 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963}
6964
Bram Moolenaar0f873732019-12-05 20:28:46 +01006965#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006968save_patterns(
6969 int num_pat,
6970 char_u **pat,
6971 int *num_file,
6972 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
6974 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006975 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006977 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 if (*file == NULL)
6979 return FAIL;
6980 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006981 {
6982 s = vim_strsave(pat[i]);
6983 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006984 // Be compatible with expand_filename(): halve the number of
6985 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00006986 backslash_halve(s);
6987 (*file)[i] = s;
6988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 *num_file = num_pat;
6990 return OK;
6991}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993/*
6994 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6995 * expand.
6996 */
6997 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006998mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007000 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 if (*p == '\\' && p[1] != NUL)
7003 ++p;
7004 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 if (vim_strchr((char_u *)
7006#ifdef VMS
7007 "*?%"
7008#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010#endif
7011 , *p) != NULL)
7012 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 }
7014 return FALSE;
7015}
7016
7017/*
7018 * Return TRUE if the string "p" contains a wildcard.
7019 * Don't recognize '~' at the end as a wildcard.
7020 */
7021 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007022mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007024 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 if (*p == '\\' && p[1] != NUL)
7027 ++p;
7028 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 if (vim_strchr((char_u *)
7030#ifdef VMS
7031 "*?%$"
7032#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034#endif
7035 , *p) != NULL
7036 || (*p == '~' && p[1] != NUL))
7037 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 }
7039 return FALSE;
7040}
7041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007043have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
7045 int i;
7046
7047 for (i = 0; i < num; i++)
7048 if (mch_has_wildcard(file[i]))
7049 return 1;
7050 return 0;
7051}
7052
7053 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007054have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
7056 int i;
7057
7058 for (i = 0; i < num; i++)
7059 if (vim_strchr(file[i], '$') != NULL)
7060 return TRUE;
7061 return FALSE;
7062}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007064#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065/*
7066 * Scaled-down version of rename(), which is missing in Xenix.
7067 * This version can only move regular files and will fail if the
7068 * destination exists.
7069 */
7070 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007071mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
7073 struct stat st;
7074
Bram Moolenaar0f873732019-12-05 20:28:46 +01007075 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007077 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007079 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 return 0;
7081 return -1;
7082}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007083#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007085#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086/*
7087 * Initializes connection with gpm (if it isn't already opened)
7088 * Return 1 if succeeded (or connection already opened), 0 if failed
7089 */
7090 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007091gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007093 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094
7095 if (!gpm_flag)
7096 {
7097 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7098 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007099 // Default handling for mouse move
7100 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 gpm_connect.maxMod = 0xffff;
7102 if (Gpm_Open(&gpm_connect, 0) > 0)
7103 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007104 // gpm library tries to handling TSTP causes
7105 // problems. Anyways, we close connection to Gpm whenever
7106 // we are going to suspend or starting an external process
7107 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007108# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007110# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007111 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 }
7113 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007114 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 return 0;
7116 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007117 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118}
7119
7120/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007121 * Returns TRUE if the GPM mouse is enabled.
7122 */
7123 int
7124gpm_enabled(void)
7125{
7126 return gpm_flag && gpm_fd >= 0;
7127}
7128
7129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 */
7132 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007133gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007135 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 Gpm_Close();
7137}
7138
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007139/*
7140 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007142 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 */
7144 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007145mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
7147 int button;
7148 static Gpm_Event gpm_event;
7149 char_u string[6];
7150 int_u vim_modifiers;
7151 int row,col;
7152 unsigned char buttons_mask;
7153 unsigned char gpm_modifiers;
7154 static unsigned char old_buttons = 0;
7155
7156 Gpm_GetEvent(&gpm_event);
7157
7158#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007159 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 if (hold_gui_events)
7161 return 0;
7162#endif
7163
7164 row = gpm_event.y - 1;
7165 col = gpm_event.x - 1;
7166
Bram Moolenaar0f873732019-12-05 20:28:46 +01007167 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 string[1] = 'M';
7169 string[2] = 'G';
7170 switch (GPM_BARE_EVENTS(gpm_event.type))
7171 {
7172 case GPM_DRAG:
7173 string[3] = MOUSE_DRAG;
7174 break;
7175 case GPM_DOWN:
7176 buttons_mask = gpm_event.buttons & ~old_buttons;
7177 old_buttons = gpm_event.buttons;
7178 switch (buttons_mask)
7179 {
7180 case GPM_B_LEFT:
7181 button = MOUSE_LEFT;
7182 break;
7183 case GPM_B_MIDDLE:
7184 button = MOUSE_MIDDLE;
7185 break;
7186 case GPM_B_RIGHT:
7187 button = MOUSE_RIGHT;
7188 break;
7189 default:
7190 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007191 // Don't know what to do. Can more than one button be
7192 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 }
7194 string[3] = (char_u)(button | 0x20);
7195 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7196 break;
7197 case GPM_UP:
7198 string[3] = MOUSE_RELEASE;
7199 old_buttons &= ~gpm_event.buttons;
7200 break;
7201 default:
7202 return 0;
7203 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007204 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 gpm_modifiers = gpm_event.modifiers;
7206 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007207 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7208 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7209 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7211 vim_modifiers |= MOUSE_SHIFT;
7212
7213 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7214 vim_modifiers |= MOUSE_CTRL;
7215 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7216 vim_modifiers |= MOUSE_ALT;
7217 string[3] |= vim_modifiers;
7218 string[4] = (char_u)(col + ' ' + 1);
7219 string[5] = (char_u)(row + ' ' + 1);
7220 add_to_input_buf(string, 6);
7221 return 6;
7222}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007223#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007225#ifdef FEAT_SYSMOUSE
7226/*
7227 * Initialize connection with sysmouse.
7228 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7229 * output, any sysmouse output than will be processed via sig_sysmouse().
7230 * Return OK if succeeded, FAIL if failed.
7231 */
7232 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007233sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007234{
7235 struct mouse_info mouse;
7236
7237 mouse.operation = MOUSE_MODE;
7238 mouse.u.mode.mode = 0;
7239 mouse.u.mode.signal = SIGUSR2;
7240 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7241 {
7242 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7243 mouse.operation = MOUSE_SHOW;
7244 ioctl(1, CONS_MOUSECTL, &mouse);
7245 return OK;
7246 }
7247 return FAIL;
7248}
7249
7250/*
7251 * Stop processing SIGUSR2 signals, and also make sure that
7252 * virtual console do not send us any sysmouse related signal.
7253 */
7254 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007255sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007256{
7257 struct mouse_info mouse;
7258
7259 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7260 mouse.operation = MOUSE_MODE;
7261 mouse.u.mode.mode = 0;
7262 mouse.u.mode.signal = 0;
7263 ioctl(1, CONS_MOUSECTL, &mouse);
7264}
7265
7266/*
7267 * Gets info from sysmouse and adds special keys to input buf.
7268 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007269 static RETSIGTYPE
7270sig_sysmouse SIGDEFARG(sigarg)
7271{
7272 struct mouse_info mouse;
7273 struct video_info video;
7274 char_u string[6];
7275 int row, col;
7276 int button;
7277 int buttons;
7278 static int oldbuttons = 0;
7279
7280#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007281 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007282 if (hold_gui_events)
7283 return;
7284#endif
7285
7286 mouse.operation = MOUSE_GETINFO;
7287 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7288 && ioctl(1, FBIO_MODEINFO, &video) != -1
7289 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7290 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7291 {
7292 row = mouse.u.data.y / video.vi_cheight;
7293 col = mouse.u.data.x / video.vi_cwidth;
7294 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007295 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007296 string[1] = 'M';
7297 string[2] = 'S';
7298 if (oldbuttons == buttons && buttons != 0)
7299 {
7300 button = MOUSE_DRAG;
7301 }
7302 else
7303 {
7304 switch (buttons)
7305 {
7306 case 0:
7307 button = MOUSE_RELEASE;
7308 break;
7309 case 1:
7310 button = MOUSE_LEFT;
7311 break;
7312 case 2:
7313 button = MOUSE_MIDDLE;
7314 break;
7315 case 4:
7316 button = MOUSE_RIGHT;
7317 break;
7318 default:
7319 return;
7320 }
7321 oldbuttons = buttons;
7322 }
7323 string[3] = (char_u)(button);
7324 string[4] = (char_u)(col + ' ' + 1);
7325 string[5] = (char_u)(row + ' ' + 1);
7326 add_to_input_buf(string, 6);
7327 }
7328 return;
7329}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007330#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007331
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007333typedef char_u * (*STRPROCSTR)(char_u *);
7334typedef char_u * (*INTPROCSTR)(int);
7335typedef int (*STRPROCINT)(char_u *);
7336typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337
7338/*
7339 * Call a DLL routine which takes either a string or int param
7340 * and returns an allocated string.
7341 */
7342 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007343mch_libcall(
7344 char_u *libname,
7345 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007346 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007347 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007348 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007349 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350{
7351# if defined(USE_DLOPEN)
7352 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007353 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354# else
7355 shl_t hinstLib;
7356# endif
7357 STRPROCSTR ProcAdd;
7358 INTPROCSTR ProcAddI;
7359 char_u *retval_str = NULL;
7360 int retval_int = 0;
7361 int success = FALSE;
7362
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007363 /*
7364 * Get a handle to the DLL module.
7365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007367 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007368 (void)dlerror();
7369
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 hinstLib = dlopen((char *)libname, RTLD_LAZY
7371# ifdef RTLD_LOCAL
7372 | RTLD_LOCAL
7373# endif
7374 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007375 if (hinstLib == NULL)
7376 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007377 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007378 dlerr = (char *)dlerror();
7379 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007380 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382# else
7383 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7384# endif
7385
Bram Moolenaar0f873732019-12-05 20:28:46 +01007386 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 if (hinstLib != NULL)
7388 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007389# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 /*
7391 * Catch a crash when calling the library function. For example when
7392 * using a number where a string pointer is expected.
7393 */
7394 mch_startjmp();
7395 if (SETJMP(lc_jump_env) != 0)
7396 {
7397 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007398# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007399 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 mch_didjmp();
7402 }
7403 else
7404# endif
7405 {
7406 retval_str = NULL;
7407 retval_int = 0;
7408
7409 if (argstring != NULL)
7410 {
7411# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007412 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007413 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414# else
7415 if (shl_findsym(&hinstLib, (const char *)funcname,
7416 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7417 ProcAdd = NULL;
7418# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007419 if ((success = (ProcAdd != NULL
7420# if defined(USE_DLOPEN)
7421 && dlerr == NULL
7422# endif
7423 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 {
7425 if (string_result == NULL)
7426 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7427 else
7428 retval_str = (ProcAdd)(argstring);
7429 }
7430 }
7431 else
7432 {
7433# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007434 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007435 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436# else
7437 if (shl_findsym(&hinstLib, (const char *)funcname,
7438 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7439 ProcAddI = NULL;
7440# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007441 if ((success = (ProcAddI != NULL
7442# if defined(USE_DLOPEN)
7443 && dlerr == NULL
7444# endif
7445 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 {
7447 if (string_result == NULL)
7448 retval_int = ((INTPROCINT)ProcAddI)(argint);
7449 else
7450 retval_str = (ProcAddI)(argint);
7451 }
7452 }
7453
Bram Moolenaar0f873732019-12-05 20:28:46 +01007454 // Save the string before we free the library.
7455 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 if (string_result == NULL)
7457 *number_result = retval_int;
7458 else if (retval_str != NULL
7459 && retval_str != (char_u *)1
7460 && retval_str != (char_u *)-1)
7461 *string_result = vim_strsave(retval_str);
7462 }
7463
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007464# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 mch_endjmp();
7466# ifdef SIGHASARG
7467 if (lc_signal != 0)
7468 {
7469 int i;
7470
Bram Moolenaar0f873732019-12-05 20:28:46 +01007471 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 for (i = 0; signal_info[i].sig != -1; i++)
7473 if (lc_signal == signal_info[i].sig)
7474 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007475 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 }
7477# endif
7478# endif
7479
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007481 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007482 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007483 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007484
Bram Moolenaar0f873732019-12-05 20:28:46 +01007485 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 (void)dlclose(hinstLib);
7487# else
7488 (void)shl_unload(hinstLib);
7489# endif
7490 }
7491
7492 if (!success)
7493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007494 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 return FAIL;
7496 }
7497
7498 return OK;
7499}
7500#endif
7501
7502#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007503static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504static int xterm_button;
7505
7506/*
7507 * Setup a dummy window for X selections in a terminal.
7508 */
7509 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007510setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511{
7512 int z = 0;
7513 char *strp = "";
7514 Widget AppShell;
7515
7516 if (!x_connect_to_server())
7517 return;
7518
7519 open_app_context();
7520 if (app_context != NULL && xterm_Shell == (Widget)0)
7521 {
7522 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007523# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007525# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007526# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007527 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528
7529 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007530 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531# endif
7532
Bram Moolenaar0f873732019-12-05 20:28:46 +01007533 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 oldhandler = XSetErrorHandler(x_error_check);
7535
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007536# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007537 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7539 mch_startjmp();
7540 if (SETJMP(lc_jump_env) != 0)
7541 {
7542 mch_didjmp();
7543 xterm_dpy = NULL;
7544 }
7545 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007546# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 {
7548 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7549 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007550 if (xterm_dpy != NULL)
7551 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007552# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007554# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 }
7556
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007557# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007558 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007560# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007561 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 (void)XSetErrorHandler(oldhandler);
7563
7564 if (xterm_dpy == NULL)
7565 {
7566 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007567 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 return;
7569 }
7570
Bram Moolenaar0f873732019-12-05 20:28:46 +01007571 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 (void)XSetIOErrorHandler(x_IOerror_handler);
7573
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007574# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007576 {
7577 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007578 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007579 verbose_leave();
7580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581# endif
7582
Bram Moolenaar0f873732019-12-05 20:28:46 +01007583 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7585 applicationShellWidgetClass, xterm_dpy,
7586 NULL);
7587 if (AppShell == (Widget)0)
7588 return;
7589 xterm_Shell = XtVaCreatePopupShell("VIM",
7590 topLevelShellWidgetClass, AppShell,
7591 XtNmappedWhenManaged, 0,
7592 XtNwidth, 1,
7593 XtNheight, 1,
7594 NULL);
7595 if (xterm_Shell == (Widget)0)
7596 return;
7597
7598 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007599 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 if (x11_display == NULL)
7601 x11_display = xterm_dpy;
7602
7603 XtRealizeWidget(xterm_Shell);
7604 XSync(xterm_dpy, False);
7605 xterm_update();
7606 }
7607 if (xterm_Shell != (Widget)0)
7608 {
7609 clip_init(TRUE);
7610 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7611 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007612 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 if (test_x11_window(xterm_dpy) == FAIL)
7614 x11_window = 0;
7615 if (x11_window != 0)
7616 xterm_trace = 0;
7617 }
7618}
7619
7620 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007621start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622{
7623 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7624 return;
7625 xterm_trace = 1;
7626 xterm_button = button;
7627 do_xterm_trace();
7628}
7629
7630
7631 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007632stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
7634 if (xterm_trace < 0)
7635 return;
7636 xterm_trace = 0;
7637}
7638
7639/*
7640 * Query the xterm pointer and generate mouse termcodes if necessary
7641 * return TRUE if dragging is active, else FALSE
7642 */
7643 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007644do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645{
7646 Window root, child;
7647 int root_x, root_y;
7648 int win_x, win_y;
7649 int row, col;
7650 int_u mask_return;
7651 char_u buf[50];
7652 char_u *strp;
7653 long got_hints;
7654 static char_u *mouse_code;
7655 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7656 static int prev_row = 0, prev_col = 0;
7657 static XSizeHints xterm_hints;
7658
7659 if (xterm_trace <= 0)
7660 return FALSE;
7661
7662 if (xterm_trace == 1)
7663 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007664 // Get the hints just before tracking starts. The font size might
7665 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007666 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7667 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 || xterm_hints.width_inc <= 1
7669 || xterm_hints.height_inc <= 1)
7670 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007671 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 return FALSE;
7673 }
7674
Bram Moolenaar0f873732019-12-05 20:28:46 +01007675 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 mouse_code = find_termcode(mouse_name);
7677 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007678 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 xterm_trace = 2;
7680
Bram Moolenaar0f873732019-12-05 20:28:46 +01007681 // Find the offset of the chars, there might be a scrollbar on the
7682 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7684 &win_x, &win_y, &mask_return);
7685 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7686 - (xterm_hints.height_inc / 2);
7687 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7688 xterm_hints.y = 2;
7689 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7690 - (xterm_hints.width_inc / 2);
7691 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7692 xterm_hints.x = 2;
7693 return TRUE;
7694 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007695 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 {
7697 xterm_trace = 0;
7698 return FALSE;
7699 }
7700
7701 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7702 &win_x, &win_y, &mask_return);
7703
7704 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7705 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7706 if (row == prev_row && col == prev_col)
7707 return TRUE;
7708
7709 STRCPY(buf, mouse_code);
7710 strp = buf + STRLEN(buf);
7711 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7712 *strp++ = (char_u)(col + ' ' + 1);
7713 *strp++ = (char_u)(row + ' ' + 1);
7714 *strp = 0;
7715 add_to_input_buf(buf, STRLEN(buf));
7716
7717 prev_row = row;
7718 prev_col = col;
7719 return TRUE;
7720}
7721
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007722# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723/*
7724 * Destroy the display, window and app_context. Required for GTK.
7725 */
7726 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007727clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728{
7729 if (xterm_Shell != (Widget)0)
7730 {
7731 XtDestroyWidget(xterm_Shell);
7732 xterm_Shell = (Widget)0;
7733 }
7734 if (xterm_dpy != NULL)
7735 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007736# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007737 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007739# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 if (x11_display == xterm_dpy)
7741 x11_display = NULL;
7742 xterm_dpy = NULL;
7743 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007744# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 if (app_context != (XtAppContext)NULL)
7746 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007747 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 XtDestroyApplicationContext(app_context);
7749 app_context = (XtAppContext)NULL;
7750 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007751# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752}
7753# endif
7754
7755/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007756 * Catch up with GUI or X events.
7757 */
7758 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007759clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007760{
7761# ifdef FEAT_GUI
7762 if (gui.in_use)
7763 gui_mch_update();
7764 else
7765# endif
7766 if (xterm_Shell != (Widget)0)
7767 xterm_update();
7768}
7769
7770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 * Catch up with any queued X events. This may put keyboard input into the
7772 * input buffer, call resize call-backs, trigger timers etc. If there is
7773 * nothing in the X event queue (& no timers pending), then we return
7774 * immediately.
7775 */
7776 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007777xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
7779 XEvent event;
7780
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007781 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007783 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007785 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007786 break;
7787
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007788 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007789 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007790 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007791 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007792#ifdef FEAT_CLIENTSERVER
7793 {
7794 XPropertyEvent *e = (XPropertyEvent *)&event;
7795
7796 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007798 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007801 XtDispatchEvent(&event);
7802 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007803 else
7804 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007805 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007806 XtAppProcessEvent(app_context, mask);
7807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 }
7809}
7810
7811 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007812clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813{
7814 if (xterm_Shell != (Widget)0)
7815 return clip_x11_own_selection(xterm_Shell, cbd);
7816 return FAIL;
7817}
7818
7819 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007820clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821{
7822 if (xterm_Shell != (Widget)0)
7823 clip_x11_lose_selection(xterm_Shell, cbd);
7824}
7825
7826 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007827clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829 if (xterm_Shell != (Widget)0)
7830 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7831}
7832
7833 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007834clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835{
7836 clip_x11_set_selection(cbd);
7837}
7838#endif
7839
7840
7841#if defined(USE_XSMP) || defined(PROTO)
7842/*
7843 * Code for X Session Management Protocol.
7844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845
7846# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847/*
7848 * This is our chance to ask the user if they want to save,
7849 * or abort the logout
7850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007852xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853{
7854 cmdmod_T save_cmdmod;
7855 int cancel_shutdown = False;
7856
7857 save_cmdmod = cmdmod;
7858 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007859 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007860 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 cancel_shutdown = True;
7862 cmdmod = save_cmdmod;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007863 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 out_flush();
7865
Bram Moolenaar0f873732019-12-05 20:28:46 +01007866 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 SmcInteractDone(smc_conn, cancel_shutdown);
7868
Bram Moolenaar0f873732019-12-05 20:28:46 +01007869 // Finish off
7870 // Only end save-yourself here if we're not cancelling shutdown;
7871 // we'll get a cancelled callback later in which we'll end it.
7872 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 if (!cancel_shutdown)
7874 {
7875 xsmp.save_yourself = False;
7876 SmcSaveYourselfDone(smc_conn, True);
7877 }
7878}
7879# endif
7880
7881/*
7882 * Callback that starts save-yourself.
7883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007885xsmp_handle_save_yourself(
7886 SmcConn smc_conn,
7887 SmPointer client_data UNUSED,
7888 int save_type UNUSED,
7889 Bool shutdown,
7890 int interact_style UNUSED,
7891 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007893 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 if (xsmp.save_yourself)
7895 SmcSaveYourselfDone(smc_conn, True);
7896 xsmp.save_yourself = True;
7897 xsmp.shutdown = shutdown;
7898
Bram Moolenaar0f873732019-12-05 20:28:46 +01007899 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007901 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902
7903 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007904 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905
7906# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007907 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007909 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 SmcInteractRequest(smc_conn, SmDialogError,
7911 xsmp_handle_interaction, client_data);
7912 else
7913# endif
7914 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007915 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 SmcSaveYourselfDone(smc_conn, True);
7917 xsmp.save_yourself = False;
7918 }
7919}
7920
7921
7922/*
7923 * Callback to warn us of imminent death.
7924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007926xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927{
7928 xsmp_close();
7929
Bram Moolenaar0f873732019-12-05 20:28:46 +01007930 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 getout_preserve_modified(0);
7932}
7933
7934
7935/*
7936 * Callback to tell us that save-yourself has completed.
7937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007939xsmp_save_complete(
7940 SmcConn smc_conn UNUSED,
7941 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
7943 xsmp.save_yourself = False;
7944}
7945
7946
7947/*
7948 * Callback to tell us that an instigated shutdown was cancelled
7949 * (maybe even by us)
7950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007952xsmp_shutdown_cancelled(
7953 SmcConn smc_conn,
7954 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955{
7956 if (xsmp.save_yourself)
7957 SmcSaveYourselfDone(smc_conn, True);
7958 xsmp.save_yourself = False;
7959 xsmp.shutdown = False;
7960}
7961
7962
7963/*
7964 * Callback to tell us that a new ICE connection has been established.
7965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007967xsmp_ice_connection(
7968 IceConn iceConn,
7969 IcePointer clientData UNUSED,
7970 Bool opening,
7971 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007973 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 if (opening)
7975 {
7976 xsmp_icefd = IceConnectionNumber(iceConn);
7977 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7978 }
7979}
7980
7981
Bram Moolenaar0f873732019-12-05 20:28:46 +01007982// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007984xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
7986 Bool rep;
7987
7988 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7989 == IceProcessMessagesIOError)
7990 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007991 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007993 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 xsmp_close();
7995 return FAIL;
7996 }
7997 else
7998 return OK;
7999}
8000
8001static int dummy;
8002
Bram Moolenaar0f873732019-12-05 20:28:46 +01008003// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 void
8005xsmp_init(void)
8006{
8007 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 SmcCallbacks smcallbacks;
8009#if 0
8010 SmPropValue smname;
8011 SmProp smnameprop;
8012 SmProp *smprops[1];
8013#endif
8014
8015 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008016 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017
8018 xsmp.save_yourself = xsmp.shutdown = False;
8019
Bram Moolenaar0f873732019-12-05 20:28:46 +01008020 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8022 smcallbacks.save_yourself.client_data = NULL;
8023 smcallbacks.die.callback = xsmp_die;
8024 smcallbacks.die.client_data = NULL;
8025 smcallbacks.save_complete.callback = xsmp_save_complete;
8026 smcallbacks.save_complete.client_data = NULL;
8027 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8028 smcallbacks.shutdown_cancelled.client_data = NULL;
8029
Bram Moolenaar0f873732019-12-05 20:28:46 +01008030 // Set up a watch on ICE connection creations. The "dummy" argument is
8031 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8033 {
8034 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008035 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 return;
8037 }
8038
Bram Moolenaar0f873732019-12-05 20:28:46 +01008039 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 xsmp.smcconn = SmcOpenConnection(
8041 NULL,
8042 NULL,
8043 SmProtoMajor,
8044 SmProtoMinor,
8045 SmcSaveYourselfProcMask | SmcDieProcMask
8046 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8047 &smcallbacks,
8048 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008049 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008050 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 errorstring);
8052 if (xsmp.smcconn == NULL)
8053 {
8054 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008055
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008057 {
8058 vim_snprintf(errorreport, sizeof(errorreport),
8059 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008060 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 return;
8063 }
8064 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8065
8066#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008067 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 smname.value = "vim";
8069 smname.length = 3;
8070 smnameprop.name = "SmProgram";
8071 smnameprop.type = "SmARRAY8";
8072 smnameprop.num_vals = 1;
8073 smnameprop.vals = &smname;
8074
8075 smprops[0] = &smnameprop;
8076 SmcSetProperties(xsmp.smcconn, 1, smprops);
8077#endif
8078}
8079
8080
Bram Moolenaar0f873732019-12-05 20:28:46 +01008081// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008083xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 if (xsmp_icefd != -1)
8086 {
8087 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008088 if (xsmp.clientid != NULL)
8089 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008090 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 xsmp_icefd = -1;
8092 }
8093}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008094#endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095
8096
8097#ifdef EBCDIC
Bram Moolenaar0f873732019-12-05 20:28:46 +01008098// Translate character to its CTRL- value
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099char CtrlTable[] =
8100{
8101/* 00 - 5E */
8102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8103 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8108/* ^ */ 0x1E,
8109/* - */ 0x1F,
8110/* 61 - 6C */
8111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8112/* _ */ 0x1F,
8113/* 6E - 80 */
8114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8115/* a */ 0x01,
8116/* b */ 0x02,
8117/* c */ 0x03,
8118/* d */ 0x37,
8119/* e */ 0x2D,
8120/* f */ 0x2E,
8121/* g */ 0x2F,
8122/* h */ 0x16,
8123/* i */ 0x05,
8124/* 8A - 90 */
8125 0, 0, 0, 0, 0, 0, 0,
8126/* j */ 0x15,
8127/* k */ 0x0B,
8128/* l */ 0x0C,
8129/* m */ 0x0D,
8130/* n */ 0x0E,
8131/* o */ 0x0F,
8132/* p */ 0x10,
8133/* q */ 0x11,
8134/* r */ 0x12,
8135/* 9A - A1 */
8136 0, 0, 0, 0, 0, 0, 0, 0,
8137/* s */ 0x13,
8138/* t */ 0x3C,
8139/* u */ 0x3D,
8140/* v */ 0x32,
8141/* w */ 0x26,
8142/* x */ 0x18,
8143/* y */ 0x19,
8144/* z */ 0x3F,
8145/* AA - AC */
8146 0, 0, 0,
8147/* [ */ 0x27,
8148/* AE - BC */
8149 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8150/* ] */ 0x1D,
8151/* BE - C0 */ 0, 0, 0,
8152/* A */ 0x01,
8153/* B */ 0x02,
8154/* C */ 0x03,
8155/* D */ 0x37,
8156/* E */ 0x2D,
8157/* F */ 0x2E,
8158/* G */ 0x2F,
8159/* H */ 0x16,
8160/* I */ 0x05,
8161/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8162/* J */ 0x15,
8163/* K */ 0x0B,
8164/* L */ 0x0C,
8165/* M */ 0x0D,
8166/* N */ 0x0E,
8167/* O */ 0x0F,
8168/* P */ 0x10,
8169/* Q */ 0x11,
8170/* R */ 0x12,
8171/* DA - DF */ 0, 0, 0, 0, 0, 0,
8172/* \ */ 0x1C,
8173/* E1 */ 0,
8174/* S */ 0x13,
8175/* T */ 0x3C,
8176/* U */ 0x3D,
8177/* V */ 0x32,
8178/* W */ 0x26,
8179/* X */ 0x18,
8180/* Y */ 0x19,
8181/* Z */ 0x3F,
8182/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8183 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8184};
8185
8186char MetaCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008187{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8189 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8190 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8191 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8192};
8193
8194
Bram Moolenaar0f873732019-12-05 20:28:46 +01008195// TODO: Use characters NOT numbers!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196char CtrlCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008197{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8199 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8200 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8201 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8202};
8203
8204
8205#endif