blob: 674dd96666e32c8309374837e3dc40d3889527a3 [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
Christian Brabandte085dfd2023-09-30 12:49:18 +020038#ifdef FEAT_XATTR
39# include <attr/xattr.h>
40# define XATTR_VAL_LEN 1024
41#endif
42
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020043#ifdef HAVE_SMACK
44# include <attr/xattr.h>
45# include <linux/xattr.h>
46# ifndef SMACK_LABEL_LEN
47# define SMACK_LABEL_LEN 1024
48# endif
49#endif
50
Bram Moolenaara2442432007-04-26 14:26:37 +000051#ifdef __CYGWIN__
K.Takata972db232022-02-04 10:45:38 +000052# include <cygwin/version.h>
53# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
Bram Moolenaar0f873732019-12-05 20:28:46 +010054 // for cygwin_conv_path()
K.Takata972db232022-02-04 10:45:38 +000055# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
56# define WIN32_LEAN_AND_MEAN
57# include <windows.h>
58# include "winclip.pro"
Bram Moolenaara2442432007-04-26 14:26:37 +000059# endif
60#endif
61
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000063
Bram Moolenaar071d4272004-06-13 20:20:40 +000064# include <gpm.h>
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000065
66# ifdef DYNAMIC_GPM
67# define Gpm_Open (*dll_Gpm_Open)
68# define Gpm_Close (*dll_Gpm_Close)
69# define Gpm_GetEvent (*dll_Gpm_GetEvent)
70# define gpm_flag (dll_gpm_flag != NULL ? *dll_gpm_flag : 0)
71# define gpm_fd (dll_gpm_fd != NULL ? *dll_gpm_fd : -1)
72
73static int (*dll_Gpm_Open) (Gpm_Connect *, int);
74static int (*dll_Gpm_Close) (void);
75static int (*dll_Gpm_GetEvent) (Gpm_Event *);
76static int *dll_gpm_flag;
77static int *dll_gpm_fd;
78
79static void *libgpm_hinst;
80# endif
81
Bram Moolenaar0f873732019-12-05 20:28:46 +010082// <linux/keyboard.h> contains defines conflicting with "keymap.h",
83// I just copied relevant defines here. A cleaner solution would be to put gpm
84// code into separate file and include there linux/keyboard.h
85// #include <linux/keyboard.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000086# define KG_SHIFT 0
87# define KG_CTRL 2
88# define KG_ALT 3
89# define KG_ALTGR 1
90# define KG_SHIFTL 4
91# define KG_SHIFTR 5
92# define KG_CTRLL 6
93# define KG_CTRLR 7
94# define KG_CAPSSHIFT 8
95
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010096static void gpm_close(void);
97static int gpm_open(void);
98static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
100
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000101#ifdef FEAT_SYSMOUSE
102# include <sys/consio.h>
103# include <sys/fbio.h>
104
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100105static int sysmouse_open(void);
106static void sysmouse_close(void);
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100107static void sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000108#endif
109
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110/*
111 * end of autoconf section. To be extended...
112 */
113
Bram Moolenaar0f873732019-12-05 20:28:46 +0100114// Are the following #ifdefs still required? And why? Is that for X11?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115
116#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
117# ifdef SIGWINCH
118# undef SIGWINCH
119# endif
120# ifdef TIOCGWINSZ
121# undef TIOCGWINSZ
122# endif
123#endif
124
Bram Moolenaar0f873732019-12-05 20:28:46 +0100125#if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126# define SIGWINCH SIGWINDOW
127#endif
128
129#ifdef FEAT_X11
130# include <X11/Xlib.h>
131# include <X11/Xutil.h>
132# include <X11/Xatom.h>
133# ifdef FEAT_XCLIPBOARD
134# include <X11/Intrinsic.h>
135# include <X11/Shell.h>
136# include <X11/StringDefs.h>
137static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void clip_update(void);
139static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140# endif
141
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142Window x11_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#endif
145
Bram Moolenaar5c3128e2020-05-11 20:54:42 +0200146static int ignore_sigtstp = FALSE;
147
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149
150static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200151static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +0200152static int unix_did_set_title = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static char_u *oldicon = NULL;
154static int did_set_icon = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaar205b8862011-09-07 15:04:31 +0200158#ifdef HAVE_UNION_WAIT
159typedef union wait waitstatus;
160#else
161typedef int waitstatus;
162#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200163static int WaitForChar(long msec, int *interrupted, int ignore_input);
164static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar041c7102020-05-30 18:14:57 +0200165#ifdef VMS
Bram Moolenaarcda77642016-06-04 13:32:35 +0200166int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200168static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
170
171#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100172static int do_xterm_trace(void);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100173# define XT_TRACE_DELAY 50 // delay for xterm tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174#endif
175
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
178#if defined(SIGWINCH)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100179static void sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000181#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100182static void sig_tstp SIGPROTOARG;
Bram Moolenaarabd56da2022-06-23 20:46:27 +0100183// volatile because it is used in signal handler sig_tstp() and
184// sigcont_handler().
dbivolaruab16ad32021-12-29 19:41:47 +0000185static volatile sig_atomic_t in_mch_suspend = FALSE;
186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#if defined(SIGINT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100188static void catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200190#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100191static void catch_sigusr1 SIGPROTOARG;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100194static void catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000196#if defined(SIGALRM) && defined(FEAT_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197# define SET_SIG_ALARM
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100198static void sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100199// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200200static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#endif
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100202static void deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100204static void catch_int_signal(void);
205static void set_signals(void);
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100206static void catch_signals(void (*func_deadly)(int), void (*func_other)(int));
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200207#ifdef HAVE_SIGPROCMASK
208# define SIGSET_DECL(set) sigset_t set;
209# define BLOCK_SIGNALS(set) block_signals(set)
210# define UNBLOCK_SIGNALS(set) unblock_signals(set)
211#else
212# define SIGSET_DECL(set)
213# define BLOCK_SIGNALS(set) do { /**/ } while (0)
214# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
215#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100216static int have_wildcard(int, char_u **);
217static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100219static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220
221#ifndef SIG_ERR
ichizok378447f2023-05-11 22:25:42 +0100222# define SIG_ERR ((sighandler_T)-1)
223#endif
224#ifndef SIG_HOLD
225# define SIG_HOLD ((sighandler_T)-2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
227
Bram Moolenaar0f873732019-12-05 20:28:46 +0100228// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200229static volatile sig_atomic_t do_resize = FALSE;
dbivolaruab16ad32021-12-29 19:41:47 +0000230// volatile because it is used in signal handler sig_tstp().
231static volatile sig_atomic_t got_tstp = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232static char_u *extra_shell_arg = NULL;
233static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100234// volatile because it is used in signal handler deathtrap().
235static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
236// volatile because it is used in signal handler deathtrap().
237static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100239#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
240static int dont_check_job_ended = 0;
241#endif
242
Bram Moolenaar26e86442020-05-17 14:06:16 +0200243// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
244static tmode_T mch_cur_tmode = TMODE_COOK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245
246#ifdef USE_XSMP
247typedef struct
248{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100249 SmcConn smcconn; // The SM connection ID
250 IceConn iceconn; // The ICE connection ID
251 char *clientid; // The client ID for the current smc session
252 Bool save_yourself; // If we're in the middle of a save_yourself
253 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254} xsmp_config_T;
255
256static xsmp_config_T xsmp;
257#endif
258
259#ifdef SYS_SIGLIST_DECLARED
260/*
261 * I have seen
262 * extern char *_sys_siglist[NSIG];
Yegappan Lakshmananaebc6ef2022-08-27 21:24:26 +0100263 * on Linux, NetBSD and Solaris. It contains a nice list of strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 * that describe the signals. That is nearly what we want here. But
265 * autoconf does only check for sys_siglist (without the underscore), I
266 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100267 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 */
269#endif
270
271static struct signalinfo
272{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100273 int sig; // Signal number, eg. SIGSEGV etc
274 char *name; // Signal name (not char_u!).
275 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276} signal_info[] =
277{
278#ifdef SIGHUP
279 {SIGHUP, "HUP", TRUE},
280#endif
281#ifdef SIGQUIT
282 {SIGQUIT, "QUIT", TRUE},
283#endif
284#ifdef SIGILL
285 {SIGILL, "ILL", TRUE},
286#endif
287#ifdef SIGTRAP
288 {SIGTRAP, "TRAP", TRUE},
289#endif
290#ifdef SIGABRT
291 {SIGABRT, "ABRT", TRUE},
292#endif
293#ifdef SIGEMT
294 {SIGEMT, "EMT", TRUE},
295#endif
296#ifdef SIGFPE
297 {SIGFPE, "FPE", TRUE},
298#endif
299#ifdef SIGBUS
300 {SIGBUS, "BUS", TRUE},
301#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100302#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100303 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 {SIGSEGV, "SEGV", TRUE},
305#endif
306#ifdef SIGSYS
307 {SIGSYS, "SYS", TRUE},
308#endif
309#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100310 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
312#ifdef SIGTERM
313 {SIGTERM, "TERM", TRUE},
314#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100315#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 {SIGVTALRM, "VTALRM", TRUE},
317#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000318#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100319 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
320 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 {SIGPROF, "PROF", TRUE},
322#endif
323#ifdef SIGXCPU
324 {SIGXCPU, "XCPU", TRUE},
325#endif
326#ifdef SIGXFSZ
327 {SIGXFSZ, "XFSZ", TRUE},
328#endif
329#ifdef SIGUSR1
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200330 {SIGUSR1, "USR1", FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000332#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100333 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 {SIGUSR2, "USR2", TRUE},
335#endif
336#ifdef SIGINT
337 {SIGINT, "INT", FALSE},
338#endif
339#ifdef SIGWINCH
340 {SIGWINCH, "WINCH", FALSE},
341#endif
342#ifdef SIGTSTP
343 {SIGTSTP, "TSTP", FALSE},
344#endif
345#ifdef SIGPIPE
346 {SIGPIPE, "PIPE", FALSE},
347#endif
348 {-1, "Unknown!", FALSE}
349};
350
ichizok378447f2023-05-11 22:25:42 +0100351 sighandler_T
352mch_signal(int sig, sighandler_T func)
353{
354#if defined(HAVE_SIGACTION) && defined(HAVE_SIGPROCMASK)
355 // Modern implementation: use sigaction().
356 struct sigaction sa, old;
357 sigset_t curset;
358 int blocked;
359
360 if (sigprocmask(SIG_BLOCK, NULL, &curset) == -1)
361 return SIG_ERR;
362
363 blocked = sigismember(&curset, sig);
364
365 if (func == SIG_HOLD)
366 {
367 if (blocked)
368 return SIG_HOLD;
369
370 sigemptyset(&curset);
371 sigaddset(&curset, sig);
372
373 if (sigaction(sig, NULL, &old) == -1
374 || sigprocmask(SIG_BLOCK, &curset, NULL) == -1)
375 return SIG_ERR;
376 return old.sa_handler;
377 }
378
379 if (blocked)
380 {
381 sigemptyset(&curset);
382 sigaddset(&curset, sig);
383
384 if (sigprocmask(SIG_UNBLOCK, &curset, NULL) == -1)
385 return SIG_ERR;
386 }
387
388 sa.sa_handler = func;
389 sigemptyset(&sa.sa_mask);
390# ifdef SA_RESTART
391 sa.sa_flags = SA_RESTART;
392# else
393 sa.sa_flags = 0;
394# endif
395 if (sigaction(sig, &sa, &old) == -1)
396 return SIG_ERR;
397 return blocked ? SIG_HOLD: old.sa_handler;
398#elif defined(HAVE_SIGSET)
399 // Using sigset() is preferred above signal().
400 return sigset(sig, func);
401#else
402 // Oldest and most compatible solution.
403 return signal(sig, func);
404#endif
405}
406
Bram Moolenaar25724922009-07-14 15:38:41 +0000407 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100408mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000409{
410 if (p_verbose >= 5)
411 {
412 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100413 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000414 verbose_leave();
415 }
ichizok378447f2023-05-11 22:25:42 +0100416#ifdef VMS
Bram Moolenaar25724922009-07-14 15:38:41 +0000417 return chdir(vms_fixfilename(path));
ichizok378447f2023-05-11 22:25:42 +0100418#else
Bram Moolenaar25724922009-07-14 15:38:41 +0000419 return chdir(path);
ichizok378447f2023-05-11 22:25:42 +0100420#endif
Bram Moolenaar25724922009-07-14 15:38:41 +0000421}
422
Bram Moolenaar0f873732019-12-05 20:28:46 +0100423// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100424#if defined(ECHOE) && defined(ICANON) \
425 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
426 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200427# define NEW_TTY_SYSTEM
428#endif
429
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000430/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100431 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100434mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200436 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100437 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100438 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439}
440
441/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100442 * Function passed to inchar_loop() to handle window resizing.
443 * If "check_only" is TRUE: Return whether there was a resize.
444 * If "check_only" is FALSE: Deal with the window resized.
445 */
446 static int
447resize_func(int check_only)
448{
449 if (check_only)
450 return do_resize;
451 while (do_resize)
Bram Moolenaar55f1b822023-06-21 13:42:48 +0100452 {
Bram Moolenaar545c8a52023-06-21 15:51:47 +0100453#ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +0100454 ch_log(NULL, "calling handle_resize() in resize_func()");
Bram Moolenaar545c8a52023-06-21 15:51:47 +0100455#endif
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100456 handle_resize();
Bram Moolenaar55f1b822023-06-21 13:42:48 +0100457 }
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100458 return FALSE;
459}
460
461/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000462 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 * Get a characters from the keyboard.
464 * Return the number of characters that are available.
465 * If wtime == 0 do not wait for characters.
466 * If wtime == n wait a short time for characters.
467 * If wtime == -1 wait forever for characters.
468 */
469 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100470mch_inchar(
471 char_u *buf,
472 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100473 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100474 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100476 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
477 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478}
479
480 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100481handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482{
483 do_resize = FALSE;
484 shell_resized();
485}
486
487/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200488 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 */
490 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100491mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200493 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494}
495
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200496#if defined(FEAT_TERMINAL) || defined(PROTO)
497/*
498 * Check for any pending input or messages.
499 */
500 int
501mch_check_messages(void)
502{
503 return WaitForChar(0L, NULL, TRUE);
504}
505#endif
506
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
508# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000509# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510# endif
511# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
512# include <sys/sysctl.h>
513# endif
514# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
515# include <sys/sysinfo.h>
516# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100517# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100518# include <mach/mach_host.h>
519# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521
522/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000523 * Return total amount of memory available in Kbyte.
524 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100527mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100530 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
Bram Moolenaar362dc332018-03-05 21:59:37 +0100532# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100533 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100534 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100535 mach_port_t host = mach_host_self();
536 kern_return_t kret;
537# ifdef HOST_VM_INFO64
538 struct vm_statistics64 vm_stat;
539 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
Bram Moolenaar988615f2018-02-27 17:58:20 +0100541 kret = host_statistics64(host, HOST_VM_INFO64,
542 (host_info64_t)&vm_stat, &count);
543# else
544 struct vm_statistics vm_stat;
545 natural_t count = HOST_VM_INFO_COUNT;
546
547 kret = host_statistics(host, HOST_VM_INFO,
548 (host_info_t)&vm_stat, &count);
549# endif
550 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100551 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100552 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
553 + vm_stat.inactive_count
554# ifdef MAC_OS_X_VERSION_10_9
555 + vm_stat.compressor_page_count
556# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100557 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100558 mach_port_deallocate(mach_task_self(), host);
559 }
560# endif
561
562# ifdef HAVE_SYSCTL
563 if (mem == 0)
564 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100565 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100566 int mib[2];
567 size_t len = sizeof(long_u);
568# ifdef HW_USERMEM64
569 long_u physmem;
570# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100571 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100572 union {
573 int_u u32;
574 long_u u64;
575 } physmem;
576# endif
577
578 mib[0] = CTL_HW;
579# ifdef HW_USERMEM64
580 mib[1] = HW_USERMEM64;
581# else
582 mib[1] = HW_USERMEM;
583# endif
584 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
585 {
586# ifdef HW_USERMEM64
587 mem = (long_u)physmem;
588# else
589 if (len == sizeof(physmem.u64))
590 mem = (long_u)physmem.u64;
591 else
592 mem = (long_u)physmem.u32;
593# endif
594 }
595 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200596# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200598# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (mem == 0)
600 {
601 struct sysinfo sinfo;
602
Bram Moolenaar0f873732019-12-05 20:28:46 +0100603 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000605 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200606# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100607 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000608 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
609 {
610 sinfo.mem_unit = sinfo.mem_unit >> 1;
611 --shiftright;
612 }
613 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200614# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200616# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200621# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 if (mem == 0)
623 {
624 long pagesize, pagecount;
625
Bram Moolenaar0f873732019-12-05 20:28:46 +0100626 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 pagesize = sysconf(_SC_PAGESIZE);
628 pagecount = sysconf(_SC_PHYS_PAGES);
629 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000630 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100631 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000632 while (shiftright > 0 && (pagesize & 1) == 0)
633 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000634 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000635 --shiftright;
636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200640# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
Bram Moolenaar0f873732019-12-05 20:28:46 +0100642 // Return the minimum of the physical memory and the user limit, because
643 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200644# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 {
646 struct rlimit rlp;
647
648 if (getrlimit(RLIMIT_DATA, &rlp) == 0
649 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200650# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200652# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000653 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000655 {
656 mem = (long_u)rlp.rlim_cur;
657 shiftright = 10;
658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200660# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661
662 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000663 return mem >> shiftright;
664 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665}
666#endif
667
Bram Moolenaar0981c872020-08-23 14:28:37 +0200668/*
669 * "flags": MCH_DELAY_IGNOREINPUT - don't read input
670 * MCH_DELAY_SETTMODE - use settmode() even for short delays
671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200673mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200675 tmode_T old_tmode;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200676 int call_settmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000677#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100678 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
Bram Moolenaar0981c872020-08-23 14:28:37 +0200681 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100683 // Go to cooked mode without echo, to allow SIGINT interrupting us
684 // here. But we don't want QUIT to kill us (CTRL-\ used in a
685 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200686 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000687 in_mch_delay = TRUE;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200688 call_settmode = mch_cur_tmode == TMODE_RAW
689 && (msec > 500 || (flags & MCH_DELAY_SETTMODE));
690 if (call_settmode)
691 {
692 old_tmode = mch_cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 settmode(TMODE_SLEEP);
Bram Moolenaar80361a52020-10-05 21:39:25 +0200694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
696 /*
697 * Everybody sleeps in a different way...
698 * Prefer nanosleep(), some versions of usleep() can only sleep up to
699 * one second.
700 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000701#ifdef FEAT_MZSCHEME
702 do
703 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100704 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000705 if (total > p_mzq)
706 msec = p_mzq;
707 else
708 msec = total;
709 total -= msec;
710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711#ifdef HAVE_NANOSLEEP
712 {
713 struct timespec ts;
714
715 ts.tv_sec = msec / 1000;
716 ts.tv_nsec = (msec % 1000) * 1000000;
717 (void)nanosleep(&ts, NULL);
718 }
719#else
720# ifdef HAVE_USLEEP
721 while (msec >= 1000)
722 {
723 usleep((unsigned int)(999 * 1000));
724 msec -= 999;
725 }
726 usleep((unsigned int)(msec * 1000));
727# else
728# ifndef HAVE_SELECT
729 poll(NULL, 0, (int)msec);
730# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 {
732 struct timeval tv;
733
734 tv.tv_sec = msec / 1000;
735 tv.tv_usec = (msec % 1000) * 1000;
Bram Moolenaar0981c872020-08-23 14:28:37 +0200736 // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
737 // a patch from Sun to fix this. Reported by Gunnar Pedersen.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 select(0, NULL, NULL, NULL, &tv);
739 }
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100740# endif
741# endif
742#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000743#ifdef FEAT_MZSCHEME
744 }
745 while (total > 0);
746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
Bram Moolenaar80361a52020-10-05 21:39:25 +0200748 if (call_settmode)
Bram Moolenaar26e86442020-05-17 14:06:16 +0200749 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000750 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 }
752 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200753 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754}
755
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000756#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
758# define HAVE_CHECK_STACK_GROWTH
759/*
760 * Support for checking for an almost-out-of-stack-space situation.
761 */
762
763/*
764 * Return a pointer to an item on the stack. Used to find out if the stack
765 * grows up or down.
766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767static int stack_grows_downwards;
768
769/*
770 * Find out if the stack grows upwards or downwards.
771 * "p" points to a variable on the stack of the caller.
772 */
773 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100774check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775{
776 int i;
777
778 stack_grows_downwards = (p > (char *)&i);
779}
780#endif
781
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000782#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783static char *stack_limit = NULL;
784
785#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
786# include <pthread.h>
787# include <pthread_np.h>
788#endif
789
790/*
791 * Find out until how var the stack can grow without getting into trouble.
792 * Called when starting up and when switching to the signal stack in
793 * deathtrap().
794 */
795 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100796get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
798 struct rlimit rlp;
799 int i;
800 long lim;
801
Bram Moolenaar0f873732019-12-05 20:28:46 +0100802 // Set the stack limit to 15/16 of the allowable size. Skip this when the
803 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 if (getrlimit(RLIMIT_STACK, &rlp) == 0
805 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
806# ifdef RLIM_INFINITY
807 && rlp.rlim_cur != RLIM_INFINITY
808# endif
809 )
810 {
811 lim = (long)rlp.rlim_cur;
812#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
813 {
814 pthread_attr_t attr;
815 size_t size;
816
Bram Moolenaar0f873732019-12-05 20:28:46 +0100817 // On FreeBSD the initial thread always has a fixed stack size, no
818 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 pthread_attr_init(&attr);
820 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
821 {
822 pthread_attr_getstacksize(&attr, &size);
823 if (lim > (long)size)
824 lim = (long)size;
825 }
826 pthread_attr_destroy(&attr);
827 }
828#endif
829 if (stack_grows_downwards)
830 {
831 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
832 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100833 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 stack_limit = (char *)((long)&i / 16L);
835 }
836 else
837 {
838 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
839 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100840 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
842 }
843}
844
845/*
846 * Return FAIL when running out of stack space.
847 * "p" must point to any variable local to the caller that's on the stack.
848 */
849 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100850mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000852 if (stack_limit == NULL)
853 return OK;
854
855 if (stack_grows_downwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000857 if (p < stack_limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 return FAIL;
859 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000860 else if (p > stack_limit)
861 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 return OK;
863}
864#endif
865
866#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
867/*
868 * Support for using the signal stack.
869 * This helps when we run out of stack space, which causes a SIGSEGV. The
870 * signal handler then must run on another stack, since the normal stack is
871 * completely full.
872 */
873
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100875static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100877static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878# endif
879
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200880/*
881 * Get a size of signal stack.
882 * Preference (if available): sysconf > SIGSTKSZ > guessed size
883 */
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100884static long int get_signal_stack_size(void)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200885{
886# ifdef HAVE_SYSCONF_SIGSTKSZ
887 long int size = -1;
888
889 // return size only if sysconf doesn't return an error
890 if ((size = sysconf(_SC_SIGSTKSZ)) > -1)
891 return size;
892# endif
893
894# ifdef SIGSTKSZ
895 // if sysconf() isn't available or gives error, return SIGSTKSZ
896 // if defined
897 return SIGSTKSZ;
898# endif
899
900 // otherwise guess the size
901 return 8000;
902}
903
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904static char *signal_stack;
905
906 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100907init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000909 if (signal_stack == NULL)
910 return;
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913# ifdef HAVE_SS_BASE
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000914 sigstk.ss_base = signal_stack;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000916 sigstk.ss_sp = signal_stack;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000918 sigstk.ss_size = get_signal_stack_size();
919 sigstk.ss_flags = 0;
920 (void)sigaltstack(&sigstk, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000922 sigstk.ss_sp = signal_stack;
923 if (stack_grows_downwards)
924 sigstk.ss_sp += get_signal_stack_size() - 1;
925 sigstk.ss_onstack = 0;
926 (void)sigstack(&sigstk, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928}
929#endif
930
931/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000932 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 * will barf when the second argument to signal() is ``wrong''.
934 * Let me try it with a few tricky defines from my own osdef.h (jw).
935 */
936#if defined(SIGWINCH)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100937 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938sig_winch SIGDEFARG(sigarg)
939{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100940 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100941 mch_signal(SIGWINCH, sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 do_resize = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943}
944#endif
945
dbivolaruab16ad32021-12-29 19:41:47 +0000946#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100947 static void
dbivolaruab16ad32021-12-29 19:41:47 +0000948sig_tstp SIGDEFARG(sigarg)
949{
950 // Second time we get called we actually need to suspend
951 if (in_mch_suspend)
952 {
ichizok378447f2023-05-11 22:25:42 +0100953 mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
dbivolaruab16ad32021-12-29 19:41:47 +0000954 raise(sigarg);
955 }
dbivolaru79a6e252022-01-23 16:41:14 +0000956 else
957 got_tstp = TRUE;
dbivolaruab16ad32021-12-29 19:41:47 +0000958
ichizok5f823d12022-03-13 17:27:38 +0000959#if !defined(__ANDROID__) && !defined(__OpenBSD__) && !defined(__DragonFly__)
960 // This is not required on all systems. On some systems (at least Android,
961 // OpenBSD, and DragonFlyBSD) this breaks suspending with CTRL-Z.
ichizok378447f2023-05-11 22:25:42 +0100962 mch_signal(SIGTSTP, sig_tstp);
xtkobacbef12e2022-02-27 12:31:52 +0000963#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000964}
965#endif
966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#if defined(SIGINT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100968 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969catch_sigint SIGDEFARG(sigarg)
970{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100971 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100972 mch_signal(SIGINT, catch_sigint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 got_int = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974}
975#endif
976
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200977#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100978 static void
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200979catch_sigusr1 SIGDEFARG(sigarg)
980{
981 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100982 mch_signal(SIGUSR1, catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200983 got_sigusr1 = TRUE;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200984}
985#endif
986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100988 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989catch_sigpwr SIGDEFARG(sigarg)
990{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100991 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100992 mch_signal(SIGPWR, catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 /*
994 * I'm not sure we get the SIGPWR signal when the system is really going
995 * down or when the batteries are almost empty. Just preserve the swap
996 * files and don't exit, that can't do any harm.
997 */
998 ml_sync_all(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999}
1000#endif
1001
1002#ifdef SET_SIG_ALARM
1003/*
1004 * signal function for alarm().
1005 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001006 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007sig_alarm SIGDEFARG(sigarg)
1008{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001009 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 sig_alarm_called = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011}
1012#endif
1013
Bram Moolenaaredce7422019-01-20 18:39:30 +01001014#if (defined(HAVE_SETJMP_H) \
1015 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
1016 || defined(FEAT_LIBCALL))) \
1017 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001018# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +01001019
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001020// argument to SETJMP()
1021static JMP_BUF lc_jump_env;
1022
1023# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001024// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001025// Volatile because it is used in signal handlers.
1026static volatile sig_atomic_t lc_signal;
1027# endif
1028
1029// TRUE when lc_jump_env is valid.
1030// Volatile because it is used in signal handler deathtrap().
zeertzjq9b7d2a92022-08-26 10:33:54 +01001031static volatile sig_atomic_t lc_active = FALSE;
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001032
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033/*
1034 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001035 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001037 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 * Usage:
1039 * mch_startjmp();
1040 * if (SETJMP(lc_jump_env) != 0)
1041 * {
1042 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001043 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 * }
1045 * else
1046 * {
1047 * do_the_work;
1048 * mch_endjmp();
1049 * }
1050 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1051 * not return before the saved environment is used.
1052 * Returns OK for normal return, FAIL when the protected code caused a
1053 * problem and LONGJMP() was used.
1054 */
Bram Moolenaar113e1072019-01-20 15:30:40 +01001055 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001056mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057{
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001058# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 lc_active = TRUE;
1062}
1063
Bram Moolenaar113e1072019-01-20 15:30:40 +01001064 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001065mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066{
1067 lc_active = FALSE;
1068}
1069
Bram Moolenaar113e1072019-01-20 15:30:40 +01001070 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001071mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072{
1073# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001074 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1075 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 init_signal_stack();
1077# endif
1078}
1079#endif
1080
1081/*
1082 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001083 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001085 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1086 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001088 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089deathtrap SIGDEFARG(sigarg)
1090{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001091 static int entered = 0; // count the number of times we got here.
1092 // Note: when memory has been corrupted
1093 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094#ifdef SIGHASARG
1095 int i;
1096#endif
1097
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001098#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 /*
1100 * Catch a crash in protected code.
1101 * Restores the environment saved in lc_jump_env, which looks like
1102 * SETJMP() returns 1.
1103 */
1104 if (lc_active)
1105 {
1106# if defined(SIGHASARG)
1107 lc_signal = sigarg;
1108# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001109 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001111 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 }
1113#endif
1114
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001115#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001116# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001117 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1118 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1119 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001120 if (in_mch_delay && sigarg == SIGQUIT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001121 return;
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001122# endif
1123
Bram Moolenaar0f873732019-12-05 20:28:46 +01001124 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1125 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1126 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001127 if (entered == 0
1128 && (0
1129# ifdef SIGHUP
1130 || sigarg == SIGHUP
1131# endif
1132# ifdef SIGQUIT
1133 || sigarg == SIGQUIT
1134# endif
1135# ifdef SIGTERM
1136 || sigarg == SIGTERM
1137# endif
1138# ifdef SIGPWR
1139 || sigarg == SIGPWR
1140# endif
1141# ifdef SIGUSR1
1142 || sigarg == SIGUSR1
1143# endif
1144# ifdef SIGUSR2
1145 || sigarg == SIGUSR2
1146# endif
1147 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001148 && !vim_handle_signal(sigarg))
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001149 return;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001150#endif
1151
Bram Moolenaar0f873732019-12-05 20:28:46 +01001152 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 ++entered;
1154
Bram Moolenaar0f873732019-12-05 20:28:46 +01001155 // Executing autocommands is likely to use more stack space than we have
1156 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001157 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001158
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001160 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 set_vim_var_nr(VV_DYING, (long)entered);
1162#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001163 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001165#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001166 // Since we are now using the signal stack, need to reset the stack
1167 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 get_stack_limit();
1169#endif
1170
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001171#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001172 // This is for opening gdb the moment Vim crashes.
1173 // You need to manually adjust the file name and Vim executable name.
1174 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001175 {
1176# define VI_GDB_FILE "/tmp/vimgdb"
1177# define VIM_NAME "/usr/bin/vim"
1178 FILE *fp = fopen(VI_GDB_FILE, "w");
1179 if (fp)
1180 {
1181 fprintf(fp,
1182 "file %s\n"
1183 "attach %d\n"
1184 "set height 1000\n"
1185 "bt full\n"
1186 , VIM_NAME, getpid());
1187 fclose(fp);
1188 system("xterm -e gdb -x "VI_GDB_FILE);
1189 unlink(VI_GDB_FILE);
1190 }
1191 }
1192#endif
1193
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001195 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 for (i = 0; signal_info[i].sig != -1; i++)
1197 if (sigarg == signal_info[i].sig)
1198 break;
1199 deadly_signal = sigarg;
1200#endif
1201
Bram Moolenaar0f873732019-12-05 20:28:46 +01001202 full_screen = FALSE; // don't write message to the GUI, it might be
1203 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 /*
1205 * If something goes wrong after entering here, we may get here again.
1206 * When this happens, give a message and try to exit nicely (resetting the
1207 * terminal mode, etc.)
1208 * When this happens twice, just exit, don't even try to give a message,
1209 * stack may be corrupt or something weird.
1210 * When this still happens again (or memory was corrupted in such a way
1211 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1212 */
1213 if (entered >= 3)
1214 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001215 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 may_core_dump();
1217 if (entered >= 4)
1218 _exit(8);
1219 exit(7);
1220 }
1221 if (entered == 2)
1222 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001223 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001224 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 out_flush();
1226 getout(1);
1227 }
1228
Bram Moolenaar0f873732019-12-05 20:28:46 +01001229 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001231 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 signal_info[i].name);
1233#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001234 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001236
Bram Moolenaar0f873732019-12-05 20:28:46 +01001237 // Preserve files and exit. This sets the really_exiting flag to prevent
1238 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001239 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
Bram Moolenaar0f873732019-12-05 20:28:46 +01001241 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001242
Bram Moolenaar009b2592004-10-24 19:18:58 +00001243#ifdef NBDEBUG
1244 reset_signals();
1245 may_core_dump();
1246 abort();
1247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248}
1249
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001251 * Invoked after receiving SIGCONT. We don't know what happened while
1252 * sleeping, deal with part of that.
1253 */
1254 static void
1255after_sigcont(void)
1256{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001257 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1258 // again later.
1259 oldtitle_outdated = TRUE;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001260
Bram Moolenaar2e310482018-08-21 13:09:10 +02001261 settmode(TMODE_RAW);
1262 need_check_timestamps = TRUE;
1263 did_check_timestamps = FALSE;
1264}
1265
1266#if defined(SIGCONT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001267static void sigcont_handler SIGPROTOARG;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001268
1269/*
1270 * With multi-threading, suspending might not work immediately. Catch the
1271 * SIGCONT signal, which will be used as an indication whether the suspending
1272 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001273 *
1274 * On Linux, signal is not always handled immediately either.
1275 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001276 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001277 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001278 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001280static volatile sig_atomic_t sigcont_received;
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001281static void sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
1283/*
1284 * signal handler for SIGCONT
1285 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001286 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287sigcont_handler SIGDEFARG(sigarg)
1288{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001289 if (in_mch_suspend)
1290 {
1291 sigcont_received = TRUE;
1292 }
1293 else
1294 {
1295 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1296 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1297 // back to a sane mode. We should redraw, but we can't really do that
1298 // in a signal handler, do a redraw later.
1299 after_sigcont();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001300 redraw_later(UPD_CLEAR);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001301 cursor_on_force();
1302 out_flush();
1303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304}
1305#endif
1306
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001307#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001308# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001309static void *clip_star_save = NULL;
1310static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001311# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001312
1313/*
1314 * Called when Vim is going to sleep or execute a shell command.
1315 * We can't respond to requests for the X selections. Lose them, otherwise
1316 * other applications will hang. But first copy the text to cut buffer 0.
1317 */
1318 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001319loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001320{
1321 if (clip_star.owned || clip_plus.owned)
1322 {
1323 x11_export_final_selection();
1324 if (clip_star.owned)
1325 clip_lose_selection(&clip_star);
1326 if (clip_plus.owned)
1327 clip_lose_selection(&clip_plus);
1328 if (x11_display != NULL)
1329 XFlush(x11_display);
1330 }
1331}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001332
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001333# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001334/*
1335 * Save clipboard text to restore later.
1336 */
1337 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001338save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001339{
1340 if (clip_star.owned)
1341 clip_star_save = get_register('*', TRUE);
1342 if (clip_plus.owned)
1343 clip_plus_save = get_register('+', TRUE);
1344}
1345
1346/*
1347 * Restore clipboard text if no one own the X selection.
1348 */
1349 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001350restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001351{
1352 if (clip_star_save != NULL)
1353 {
1354 if (!clip_gen_owner_exists(&clip_star))
1355 put_register('*', clip_star_save);
1356 else
1357 free_register(clip_star_save);
1358 clip_star_save = NULL;
1359 }
1360 if (clip_plus_save != NULL)
1361 {
1362 if (!clip_gen_owner_exists(&clip_plus))
1363 put_register('+', clip_plus_save);
1364 else
1365 free_register(clip_plus_save);
1366 clip_plus_save = NULL;
1367 }
1368}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001369# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001370#endif
1371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372/*
1373 * If the machine has job control, use it to suspend the program,
1374 * otherwise fake it by starting a new shell.
1375 */
1376 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001377mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001379 if (ignore_sigtstp)
1380 return;
1381
Bram Moolenaar041c7102020-05-30 18:14:57 +02001382#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001383 in_mch_suspend = TRUE;
1384
Bram Moolenaar0f873732019-12-05 20:28:46 +01001385 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001387 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
1389# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001390 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001392# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 sigcont_received = FALSE;
1394# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001395
Bram Moolenaar0f873732019-12-05 20:28:46 +01001396 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001397
1398# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001399 /*
1400 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001401 * immediately, but somehow not all the time, probably because it's handled
1402 * in another thread. Do not call pause() because there would be race
1403 * condition which would hang Vim if signal happened in between the test of
1404 * sigcont_received and the call to pause(). If signal is not yet received,
1405 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1406 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001407 */
1408 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001409 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001410
Bram Moolenaar262735e2009-07-14 10:20:22 +00001411 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001412 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001415 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
Bram Moolenaar2e310482018-08-21 13:09:10 +02001417 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418#else
1419 suspend_shell();
1420#endif
1421}
1422
1423 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001424mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
1426 Columns = 80;
1427 Rows = 24;
1428
1429 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001430
1431#ifdef SIGTSTP
1432 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1433 // that indicates the shell (or program) that launched us does not support
1434 // tty job control and thus we should ignore that signal. If invoked as a
1435 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
ichizok378447f2023-05-11 22:25:42 +01001436 ignore_sigtstp = restricted || SIG_IGN == mch_signal(SIGTSTP, SIG_ERR);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001439
Bram Moolenaar56718732006-03-15 22:53:57 +00001440#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001441 mac_conv_init();
1442#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001443#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1444 win_clip_init();
1445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446}
1447
1448 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001449set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
1451#if defined(SIGWINCH)
1452 /*
1453 * WINDOW CHANGE signal is handled with sig_winch().
1454 */
ichizok378447f2023-05-11 22:25:42 +01001455 mch_signal(SIGWINCH, sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456#endif
1457
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001459 // See mch_init() for the conditions under which we ignore SIGTSTP.
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001460 // In the GUI default TSTP processing is OK.
1461 // Checking both gui.in_use and gui.starting because gui.in_use is not set
1462 // at this point (set after menus are displayed), but gui.starting is set.
ichizok378447f2023-05-11 22:25:42 +01001463 mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001464# ifdef FEAT_GUI
1465 : gui.in_use || gui.starting ? SIG_DFL
1466# endif
ichizok378447f2023-05-11 22:25:42 +01001467 : sig_tstp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001469#if defined(SIGCONT)
ichizok378447f2023-05-11 22:25:42 +01001470 mch_signal(SIGCONT, sigcont_handler);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001472#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 /*
1474 * We want to ignore breaking of PIPEs.
1475 */
ichizok378447f2023-05-11 22:25:42 +01001476 mch_signal(SIGPIPE, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477#endif
1478
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001480 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481#endif
1482
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001483#ifdef SIGUSR1
1484 /*
1485 * Call user's handler on SIGUSR1
1486 */
ichizok378447f2023-05-11 22:25:42 +01001487 mch_signal(SIGUSR1, catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001488#endif
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 /*
1491 * Ignore alarm signals (Perl's alarm() generates it).
1492 */
1493#ifdef SIGALRM
ichizok378447f2023-05-11 22:25:42 +01001494 mch_signal(SIGALRM, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495#endif
1496
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001497#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 /*
1499 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1500 * work will be lost.
1501 */
ichizok378447f2023-05-11 22:25:42 +01001502 mch_signal(SIGPWR, catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503#endif
1504
1505 /*
1506 * Arrange for other signals to gracefully shutdown Vim.
1507 */
1508 catch_signals(deathtrap, SIG_ERR);
1509
1510#if defined(FEAT_GUI) && defined(SIGHUP)
1511 /*
1512 * When the GUI is running, ignore the hangup signal.
1513 */
1514 if (gui.in_use)
ichizok378447f2023-05-11 22:25:42 +01001515 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516#endif
1517}
1518
Bram Moolenaardf177f62005-02-22 08:39:57 +00001519#if defined(SIGINT) || defined(PROTO)
1520/*
1521 * Catch CTRL-C (only works while in Cooked mode).
1522 */
1523 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001524catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001525{
ichizok378447f2023-05-11 22:25:42 +01001526 mch_signal(SIGINT, catch_sigint);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001527}
1528#endif
1529
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001531reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001534#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001535 // SIGCONT isn't in the list, because its default action is ignore
ichizok378447f2023-05-11 22:25:42 +01001536 mch_signal(SIGCONT, SIG_DFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537#endif
1538}
1539
1540 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001541catch_signals(
Michael Jarvisbe9624e2023-04-19 20:28:48 +01001542 void (*func_deadly)(int),
1543 void (*func_other)(int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
1545 int i;
1546
1547 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (signal_info[i].deadly)
1550 {
1551#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1552 struct sigaction sa;
1553
Bram Moolenaar0f873732019-12-05 20:28:46 +01001554 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 sa.sa_handler = func_deadly;
1556 sigemptyset(&sa.sa_mask);
1557# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001558 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1559 // thread handling in combination with using the alternate stack:
1560 // pthread library functions try to use the stack pointer to
1561 // identify the current thread, causing a SEGV signal, which
1562 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 sa.sa_flags = 0;
1564# else
1565 sa.sa_flags = SA_ONSTACK;
1566# endif
1567 sigaction(signal_info[i].sig, &sa, NULL);
1568#else
1569# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1570 struct sigvec sv;
1571
Bram Moolenaar0f873732019-12-05 20:28:46 +01001572 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 sv.sv_handler = func_deadly;
1574 sv.sv_mask = 0;
1575 sv.sv_flags = SV_ONSTACK;
1576 sigvec(signal_info[i].sig, &sv, NULL);
1577# else
ichizok378447f2023-05-11 22:25:42 +01001578 mch_signal(signal_info[i].sig, func_deadly);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579# endif
1580#endif
1581 }
1582 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001583 {
1584 // Deal with non-deadly signals.
1585#ifdef SIGTSTP
ichizok378447f2023-05-11 22:25:42 +01001586 mch_signal(signal_info[i].sig,
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001587 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1588 ? SIG_IGN : func_other);
1589#else
ichizok378447f2023-05-11 22:25:42 +01001590 mch_signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001591#endif
1592 }
1593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594}
1595
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001596#ifdef HAVE_SIGPROCMASK
1597 static void
1598block_signals(sigset_t *set)
1599{
1600 sigset_t newset;
1601 int i;
1602
1603 sigemptyset(&newset);
1604
1605 for (i = 0; signal_info[i].sig != -1; i++)
1606 sigaddset(&newset, signal_info[i].sig);
1607
Bram Moolenaar2e310482018-08-21 13:09:10 +02001608# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001609 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001610 sigaddset(&newset, SIGCONT);
1611# endif
1612
1613 sigprocmask(SIG_BLOCK, &newset, set);
1614}
1615
1616 static void
1617unblock_signals(sigset_t *set)
1618{
1619 sigprocmask(SIG_SETMASK, set, NULL);
1620}
1621#endif
1622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001624 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001625 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1626 * return TRUE
1627 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1628 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001629 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001630 * Returns TRUE when Vim should exit.
1631 */
1632 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001633vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001634{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001635 static int got_signal = 0;
1636 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001637
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001638 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001639 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001640 case SIGNAL_BLOCK: blocked = TRUE;
1641 break;
1642
1643 case SIGNAL_UNBLOCK: blocked = FALSE;
1644 if (got_signal != 0)
1645 {
1646 kill(getpid(), got_signal);
1647 got_signal = 0;
1648 }
1649 break;
1650
1651 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001652 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001653 got_signal = sig;
1654#ifdef SIGPWR
1655 if (sig != SIGPWR)
1656#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001657 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001658 break;
1659 }
1660 return FALSE;
1661}
1662
1663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 * Check_win checks whether we have an interactive stdout.
1665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001667mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 if (isatty(1))
1670 return OK;
1671 return FAIL;
1672}
1673
1674/*
1675 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1676 */
1677 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001678mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
1680 if (isatty(read_cmd_fd))
1681 return TRUE;
1682 return FALSE;
1683}
1684
1685#ifdef FEAT_X11
1686
Bram Moolenaar651fca82021-11-29 20:39:38 +00001687# if defined(ELAPSED_TIMEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689/*
1690 * Give a message about the elapsed time for opening the X window.
1691 */
1692 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001693xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001695 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696}
1697# endif
1698#endif
1699
Bram Moolenaar651fca82021-11-29 20:39:38 +00001700#if defined(FEAT_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701/*
1702 * A few functions shared by X11 title and clipboard code.
1703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
1705static int got_x_error = FALSE;
1706
1707/*
1708 * X Error handler, otherwise X just exits! (very rude) -- webb
1709 */
1710 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001711x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001713 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 STRCAT(IObuff, _("\nVim: Got X error\n"));
1715
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001716 // In the GUI we cannot print a message and continue, because no X calls
1717 // are allowed here (causes my system to hang). Silently continuing seems
1718 // like the best alternative. Do preserve files, in case we crash.
1719 ml_sync_all(FALSE, FALSE);
1720
1721#ifdef FEAT_GUI
1722 if (!gui.in_use)
1723#endif
1724 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
Bram Moolenaar0f873732019-12-05 20:28:46 +01001726 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727}
1728
1729/*
1730 * Another X Error handler, just used to check for errors.
1731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001733x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
1735 got_x_error = TRUE;
1736 return 0;
1737}
1738
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001739/*
1740 * Return TRUE when connection to the X server is desired.
1741 */
1742 static int
1743x_connect_to_server(void)
1744{
1745 // No point in connecting if we are exiting or dying.
1746 if (exiting || v_dying)
1747 return FALSE;
1748
1749#if defined(FEAT_CLIENTSERVER)
1750 if (x_force_connect)
1751 return TRUE;
1752#endif
1753 if (x_no_connect)
1754 return FALSE;
1755
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001756 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001757 if (clip_exclude_prog != NULL)
1758 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001759 // Just in case we get called recursively, return FALSE. This could
1760 // happen if vpeekc() is used while executing the prog and it causes a
1761 // related callback to be invoked.
1762 if (regprog_in_use(clip_exclude_prog))
1763 return FALSE;
1764
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001765 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1766 return FALSE;
1767 }
1768 return TRUE;
1769}
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001772# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773/*
1774 * An X IO Error handler, used to catch error while opening the display.
1775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001777x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001779 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001781# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001782 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784}
1785# endif
1786
1787/*
1788 * An X IO Error handler, used to catch terminal errors.
1789 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001790static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001793x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794{
1795 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001796 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 x11_window = 0;
1798 x11_display = NULL;
1799 xterm_Shell = (Widget)0;
1800
Bram Moolenaar0f873732019-12-05 20:28:46 +01001801 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001803# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001804 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001805# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001807
1808/*
1809 * If the X11 connection was lost try to restore it.
1810 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001811 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001812 */
1813 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001814may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001815{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001816 // No point in restoring the connecting if we are exiting or dying.
1817 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001818 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001819 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001820
1821# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001822 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001823 if (app_context != (XtAppContext)NULL)
1824 {
1825 XtDestroyApplicationContext(app_context);
1826 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001827 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001828 }
1829# endif
1830
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001831 setup_term_clip();
1832 get_x11_title(FALSE);
1833 }
1834}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001835
1836 void
1837ex_xrestore(exarg_T *eap)
1838{
1839 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1840 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001841 if (xterm_display_allocated)
1842 vim_free(xterm_display);
1843 xterm_display = (char *)vim_strsave(eap->arg);
1844 xterm_display_allocated = TRUE;
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001845 }
1846 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001847 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001848
1849 clear_xterm_clip();
1850 x11_window = 0;
1851 xterm_dpy_retry_count = 5; // Try reconnecting five times
1852 may_restore_clipboard();
1853}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854#endif
1855
1856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 * Test if "dpy" and x11_window are valid by getting the window title.
1858 * I don't actually want it yet, so there may be a simpler call to use, but
1859 * this will cause the error handler x_error_check() to be called if anything
1860 * is wrong, such as the window pointer being invalid (as can happen when the
1861 * user changes his DISPLAY, but not his WINDOWID) -- webb
1862 */
1863 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001864test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865{
Dominique Pellé4927bc72023-09-24 16:12:07 +02001866 int (*old_handler)(Display*, XErrorEvent*);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 XTextProperty text_prop;
1868
1869 old_handler = XSetErrorHandler(x_error_check);
1870 got_x_error = FALSE;
1871 if (XGetWMName(dpy, x11_window, &text_prop))
1872 XFree((void *)text_prop.value);
1873 XSync(dpy, False);
1874 (void)XSetErrorHandler(old_handler);
1875
1876 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001877 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
1879 return (got_x_error ? FAIL : OK);
1880}
1881#endif
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884#ifdef FEAT_X11
1885
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001886static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
1888/*
1889 * try to get x11 window and display
1890 *
1891 * return FAIL for failure, OK otherwise
1892 */
1893 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001894get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895{
1896 char *winid;
1897 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001898#define XD_NONE 0 // x11_display not set here
1899#define XD_HERE 1 // x11_display opened here
1900#define XD_GUI 2 // x11_display used from gui.dpy
1901#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 static int x11_display_from = XD_NONE;
1903 static int did_set_error_handler = FALSE;
1904
1905 if (!did_set_error_handler)
1906 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001907 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 (void)XSetErrorHandler(x_error_handler);
1909 did_set_error_handler = TRUE;
1910 }
1911
Bram Moolenaar9372a112005-12-06 19:59:18 +00001912#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (gui.in_use)
1914 {
1915 /*
1916 * If the X11 display was opened here before, for the window where Vim
1917 * was started, close that one now to avoid a memory leak.
1918 */
1919 if (x11_display_from == XD_HERE && x11_display != NULL)
1920 {
1921 XCloseDisplay(x11_display);
1922 x11_display_from = XD_NONE;
1923 }
1924 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1925 {
1926 x11_display_from = XD_GUI;
1927 return OK;
1928 }
1929 x11_display = NULL;
1930 return FAIL;
1931 }
1932 else if (x11_display_from == XD_GUI)
1933 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001934 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 x11_window = 0;
1936 x11_display = NULL;
1937 x11_display_from = XD_NONE;
1938 }
1939#endif
1940
Bram Moolenaar0f873732019-12-05 20:28:46 +01001941 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 if (!x_connect_to_server())
1943 return FAIL;
1944
1945 /*
1946 * If WINDOWID not set, should try another method to find out
1947 * what the current window number is. The only code I know for
1948 * this is very complicated.
1949 * We assume that zero is invalid for WINDOWID.
1950 */
1951 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1952 x11_window = (Window)atol(winid);
1953
1954#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001955 if (xterm_dpy == x11_display)
1956 // x11_display may have been set to xterm_dpy elsewhere
1957 x11_display_from = XD_XTERM;
1958
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 if (xterm_dpy != NULL && x11_window != 0)
1960 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001961 // We may have checked it already, but Gnome terminal can move us to
1962 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001963 if (x11_display_from != XD_XTERM)
1964 {
1965 /*
1966 * If the X11 display was opened here before, for the window where
1967 * Vim was started, close that one now to avoid a memory leak.
1968 */
1969 if (x11_display_from == XD_HERE && x11_display != NULL)
1970 XCloseDisplay(x11_display);
1971 x11_display = xterm_dpy;
1972 x11_display_from = XD_XTERM;
1973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 if (test_x11_window(x11_display) == FAIL)
1975 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001976 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 x11_window = 0;
1978 x11_display = NULL;
1979 x11_display_from = XD_NONE;
1980 return FAIL;
1981 }
1982 return OK;
1983 }
1984#endif
1985
1986 if (x11_window == 0 || x11_display == NULL)
1987 result = -1;
1988
Bram Moolenaar0f873732019-12-05 20:28:46 +01001989 if (result != -1) // Have already been here and set this
1990 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
1992 if (x11_window != 0 && x11_display == NULL)
1993 {
1994#ifdef SET_SIG_ALARM
ichizok378447f2023-05-11 22:25:42 +01001995 sighandler_T sig_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001997#ifdef ELAPSED_FUNC
1998 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01002001 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002#endif
2003
2004#ifdef SET_SIG_ALARM
2005 /*
2006 * Opening the Display may hang if the DISPLAY setting is wrong, or
2007 * the network connection is bad. Set an alarm timer to get out.
2008 */
2009 sig_alarm_called = FALSE;
ichizok378447f2023-05-11 22:25:42 +01002010 sig_save = mch_signal(SIGALRM, sig_alarm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 alarm(2);
2012#endif
2013 x11_display = XOpenDisplay(NULL);
2014
2015#ifdef SET_SIG_ALARM
2016 alarm(0);
ichizok378447f2023-05-11 22:25:42 +01002017 mch_signal(SIGALRM, sig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01002019 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020#endif
2021 if (x11_display != NULL)
2022 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002023# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00002025 {
2026 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002027 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00002028 verbose_leave();
2029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030# endif
2031 if (test_x11_window(x11_display) == FAIL)
2032 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002033 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 x11_window = 0;
2035 XCloseDisplay(x11_display);
2036 x11_display = NULL;
2037 }
2038 else
2039 x11_display_from = XD_HERE;
2040 }
2041 }
2042 if (x11_window == 0 || x11_display == NULL)
2043 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02002044
2045# ifdef FEAT_EVAL
2046 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
2047# endif
2048
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 return (result = OK);
2050}
2051
2052/*
2053 * Determine original x11 Window Title
2054 */
2055 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002056get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057{
Bram Moolenaar47136d72004-10-12 20:02:24 +00002058 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059}
2060
2061/*
2062 * Determine original x11 Window icon
2063 */
2064 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002065get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066{
2067 int retval = FALSE;
2068
2069 retval = get_x11_thing(FALSE, test_only);
2070
Bram Moolenaar0f873732019-12-05 20:28:46 +01002071 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 if (oldicon == NULL && !test_only)
2073 {
2074 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002075 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002077 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
2079
2080 return retval;
2081}
2082
2083 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002084get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002085 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01002086 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087{
2088 XTextProperty text_prop;
2089 int retval = FALSE;
2090 Status status;
2091
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002092 if (get_x11_windis() != OK)
2093 return FALSE;
2094
2095 // Get window/icon name if any
2096 if (get_title)
2097 status = XGetWMName(x11_display, x11_window, &text_prop);
2098 else
2099 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2100
2101 /*
2102 * If terminal is xterm, then x11_window may be a child window of the
2103 * outer xterm window that actually contains the window/icon name, so
2104 * keep traversing up the tree until a window with a title/icon is
2105 * found.
2106 */
2107 // Previously this was only done for xterm and alike. I don't see a
2108 // reason why it would fail for other terminal emulators.
2109 // if (term_is_xterm)
2110 Window root;
2111 Window parent;
2112 Window win = x11_window;
2113 Window *children;
2114 unsigned int num_children;
2115
2116 while (!status || text_prop.value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002118 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2119 &num_children))
2120 break;
2121 if (children)
2122 XFree((void *)children);
2123 if (parent == root || parent == 0)
2124 break;
2125
2126 win = parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002128 status = XGetWMName(x11_display, win, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002130 status = XGetWMIconName(x11_display, win, &text_prop);
2131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002133 if (status && text_prop.value != NULL)
2134 {
2135 retval = TRUE;
2136 if (!test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002138 if (get_title)
2139 vim_free(oldtitle);
2140 else
2141 vim_free(oldicon);
2142 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002145 oldtitle = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002147 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002149 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002151 char **cl;
2152 Status transform_status;
2153 int n = 0;
2154
2155 transform_status = XmbTextPropertyToTextList(x11_display,
2156 &text_prop,
2157 &cl, &n);
2158 if (transform_status >= Success && n > 0 && cl[0])
2159 {
2160 if (get_title)
2161 oldtitle = vim_strsave((char_u *) cl[0]);
2162 else
2163 oldicon = vim_strsave((char_u *) cl[0]);
2164 XFreeStringList(cl);
2165 }
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002166 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 if (get_title)
2169 oldtitle = vim_strsave((char_u *)text_prop.value);
2170 else
2171 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002175 XFree((void *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 }
2177 return retval;
2178}
2179
Bram Moolenaar0f873732019-12-05 20:28:46 +01002180// Xutf8 functions are not available on older systems. Note that on some
2181// systems X_HAVE_UTF8_STRING may be defined in a header file but
2182// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2183// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002184#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002185# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186# define USE_UTF8_STRING
2187# endif
2188#endif
2189
2190/*
2191 * Set x11 Window Title
2192 *
2193 * get_x11_windis() must be called before this and have returned OK
2194 */
2195 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002196set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002198 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2199 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2200 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201#ifdef USE_UTF8_STRING
2202 if (enc_utf8)
2203 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2204 NULL, NULL, 0, NULL, NULL, NULL);
2205 else
2206#endif
2207 {
2208#if XtSpecificationRelease >= 4
2209# ifdef FEAT_XFONTSET
2210 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2211 NULL, NULL, 0, NULL, NULL, NULL);
2212# else
2213 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002214 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
Bram Moolenaar0f873732019-12-05 20:28:46 +01002216 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002217 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 XSetWMProperties(x11_display, x11_window, &text_prop,
2219 NULL, NULL, 0, NULL, NULL, NULL);
2220# endif
2221#else
2222 XStoreName(x11_display, x11_window, (char *)title);
2223#endif
2224 }
2225 XFlush(x11_display);
2226}
2227
2228/*
2229 * Set x11 Window icon
2230 *
2231 * get_x11_windis() must be called before this and have returned OK
2232 */
2233 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002234set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002236 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#ifdef USE_UTF8_STRING
2238 if (enc_utf8)
2239 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2240 NULL, 0, NULL, NULL, NULL);
2241 else
2242#endif
2243 {
2244#if XtSpecificationRelease >= 4
2245# ifdef FEAT_XFONTSET
2246 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2247 NULL, 0, NULL, NULL, NULL);
2248# else
2249 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002250 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002252 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2254 NULL, 0, NULL, NULL, NULL);
2255# endif
2256#else
2257 XSetIconName(x11_display, x11_window, (char *)icon);
2258#endif
2259 }
2260 XFlush(x11_display);
2261}
2262
Bram Moolenaar0f873732019-12-05 20:28:46 +01002263#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002266get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267{
2268 return FALSE;
2269}
2270
2271 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002272get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273{
2274 if (!test_only)
2275 {
2276 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002277 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002279 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281 return FALSE;
2282}
2283
Bram Moolenaar0f873732019-12-05 20:28:46 +01002284#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
2286 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002287mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
2289 return get_x11_title(TRUE);
2290}
2291
2292 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002293mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294{
2295 return get_x11_icon(TRUE);
2296}
2297
2298/*
2299 * Set the window title and icon.
2300 */
2301 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002302mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303{
2304 int type = 0;
2305 static int recursive = 0;
2306
Bram Moolenaar0f873732019-12-05 20:28:46 +01002307 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002309 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 return;
2311
Bram Moolenaar0f873732019-12-05 20:28:46 +01002312 // When one of the X11 functions causes a deadly signal, we get here again
2313 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (recursive)
2315 return;
2316 ++recursive;
2317
2318 /*
2319 * if the window ID and the display is known, we may use X11 calls
2320 */
2321#ifdef FEAT_X11
2322 if (get_x11_windis() == OK)
2323 type = 1;
2324#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002325# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002326 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 if (gui.in_use)
2328 type = 1;
2329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#endif
2331
2332 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002333 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 * than x11 calls, because the x11 calls don't always work
2335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 if ((type || *T_TS != NUL) && title != NULL)
2337 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002338 if (oldtitle_outdated)
2339 {
2340 oldtitle_outdated = FALSE;
2341 VIM_CLEAR(oldtitle);
2342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (oldtitle == NULL
2344#ifdef FEAT_GUI
2345 && !gui.in_use
2346#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002347 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 (void)get_x11_title(FALSE);
2349
Bram Moolenaar0f873732019-12-05 20:28:46 +01002350 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 term_settitle(title);
2352#ifdef FEAT_X11
2353 else
2354# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002355 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002357 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002359#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002360 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 else
2362 gui_mch_settitle(title, icon);
2363#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002364 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366
2367 if ((type || *T_CIS != NUL) && icon != NULL)
2368 {
2369 if (oldicon == NULL
2370#ifdef FEAT_GUI
2371 && !gui.in_use
2372#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002373 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 get_x11_icon(FALSE);
2375
2376 if (*T_CIS != NUL)
2377 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002378 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002380 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 out_flush();
2382 }
2383#ifdef FEAT_X11
2384 else
2385# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002386 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002388 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389#endif
2390 did_set_icon = TRUE;
2391 }
2392 --recursive;
2393}
2394
2395/*
2396 * Restore the window/icon title.
2397 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002398 * SAVE_RESTORE_TITLE only restore title
2399 * SAVE_RESTORE_ICON only restore icon
2400 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 */
2402 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002403mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404{
Bram Moolenaardac13472019-09-16 21:06:21 +02002405 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002406
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002407 // Only restore the title or icon when it has been set.
2408 // When using "oldtitle" make a copy, it might be freed halfway.
2409 char_u *title = ((which & SAVE_RESTORE_TITLE) && unix_did_set_title)
2410 ? (oldtitle ? oldtitle : p_titleold) : NULL;
2411 char_u *tofree = NULL;
2412 if (title == oldtitle && oldtitle != NULL)
2413 {
2414 tofree = vim_strsave(title);
2415 if (tofree != NULL)
2416 title = tofree;
2417 }
2418 mch_settitle(title,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002419 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002420 vim_free(tofree);
Bram Moolenaar40385db2018-08-07 22:31:44 +02002421
Bram Moolenaare5c83282019-05-03 23:15:37 +02002422 if (do_push_pop)
2423 {
2424 // pop and push from/to the stack
2425 term_pop_title(which);
2426 term_push_title(which);
2427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428}
2429
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431/*
2432 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002433 * This matches "xterm.*", thus "xterm-256color", "xterm-kitty", etc.
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002434 * Do not consider "xterm-kitty" an xterm, it is not fully xterm compatible,
2435 * using the "xterm-kitty" terminfo entry should work better.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002436 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 */
2438 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002439vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
2441 if (name == NULL)
2442 return FALSE;
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002443 return ((STRNICMP(name, "xterm", 5) == 0
2444 && STRNICMP(name, "xterm-kitty", 11) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 || STRNICMP(name, "nxterm", 6) == 0
2446 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002447 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002449 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 || STRCMP(name, "builtin_xterm") == 0);
2451}
2452
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002453#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2454/*
2455 * Return TRUE if "name" appears to be that of a terminal
2456 * known to support the xterm-style mouse protocol.
2457 * Relies on term_is_xterm having been set to its correct value.
2458 */
2459 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002460use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002461{
2462 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002463 && (term_is_xterm
2464 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002465 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002466 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002467 || STRICMP(name, "st") == 0
2468 || STRNICMP(name, "st-", 3) == 0
2469 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002470}
2471#endif
2472
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473/*
2474 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2475 * Return 1 for "xterm".
2476 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002477 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002478 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 */
2480 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002481use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002483 if (ttym_flags == TTYM_SGR)
2484 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002485 if (ttym_flags == TTYM_URXVT)
2486 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 if (ttym_flags == TTYM_XTERM2)
2488 return 2;
2489 if (ttym_flags == TTYM_XTERM)
2490 return 1;
2491 return 0;
2492}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002494/*
2495 * Return TRUE if "name" is an iris-ansi terminal name.
2496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002498vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
2500 if (name == NULL)
2501 return FALSE;
2502 return (STRNICMP(name, "iris-ansi", 9) == 0
2503 || STRCMP(name, "builtin_iris-ansi") == 0);
2504}
2505
Dominique Pellee764d1b2023-03-12 21:20:59 +00002506#if defined(VMS) || defined(PROTO)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002507/*
2508 * Return TRUE if "name" is a vt300-like terminal name.
2509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002511vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513 if (name == NULL)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002514 return FALSE;
2515 // Actually all ANSI compatible terminals should be here.
2516 // Catch at least VT1xx - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002517 return ((STRNICMP(name, "vt", 2) == 0
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002518 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 || STRCMP(name, "builtin_vt320") == 0);
2520}
Dominique Pellee764d1b2023-03-12 21:20:59 +00002521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
2523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 * Insert user name in s[len].
2525 * Return OK if a name found.
2526 */
2527 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002528mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
2530#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002531 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 return OK;
2533#else
2534 return mch_get_uname(getuid(), s, len);
2535#endif
2536}
2537
2538/*
2539 * Insert user name for "uid" in s[len].
2540 * Return OK if a name found.
2541 */
2542 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002543mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2546 struct passwd *pw;
2547
2548 if ((pw = getpwuid(uid)) != NULL
2549 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2550 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002551 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 return OK;
2553 }
2554#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002555 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2556 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557}
2558
2559/*
2560 * Insert host name is s[len].
2561 */
2562
2563#ifdef HAVE_SYS_UTSNAME_H
2564 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002565mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566{
2567 struct utsname vutsname;
2568
2569 if (uname(&vutsname) < 0)
2570 *s = NUL;
2571 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002572 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002574#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
2576# ifdef HAVE_SYS_SYSTEMINFO_H
2577# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2578# endif
2579
2580 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002581mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
2583# ifdef VAXC
2584 vaxc$gethostname((char *)s, len);
2585# else
2586 gethostname((char *)s, len);
2587# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002588 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002590#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
2592/*
2593 * return process ID
2594 */
2595 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002596mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597{
2598 return (long)getpid();
2599}
2600
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002601/*
2602 * return TRUE if process "pid" is still running
2603 */
2604 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002605mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002606{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002607 // If there is no error the process must be running.
2608 if (kill(pid, 0) == 0)
2609 return TRUE;
2610#ifdef ESRCH
2611 // If the error is ESRCH then the process is not running.
2612 if (errno == ESRCH)
2613 return FALSE;
2614#endif
2615 // If the process is running and owned by another user we get EPERM. With
2616 // other errors the process might be running, assuming it is then.
2617 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002618}
2619
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002622strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623{
2624 extern int sys_nerr;
2625 extern char *sys_errlist[];
2626 static char er[20];
2627
2628 if (err > 0 && err < sys_nerr)
2629 return (sys_errlist[err]);
2630 sprintf(er, "Error %d", err);
2631 return er;
2632}
2633#endif
2634
2635/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002636 * Get name of current directory into buffer "buf" of length "len" bytes.
2637 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 * Return OK for success, FAIL for failure.
2639 */
2640 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002641mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642{
2643#if defined(USE_GETCWD)
2644 if (getcwd((char *)buf, len) == NULL)
2645 {
2646 STRCPY(buf, strerror(errno));
2647 return FAIL;
2648 }
2649 return OK;
2650#else
2651 return (getwd((char *)buf) != NULL ? OK : FAIL);
2652#endif
2653}
2654
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002656 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 *
2658 * return FAIL for failure, OK for success
2659 */
2660 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002661mch_FullName(
2662 char_u *fname,
2663 char_u *buf,
2664 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002665 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
2667 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002668#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002670 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 char_u olddir[MAXPATHL];
2673 char_u *p;
2674 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002675#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002676 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2677 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002678#endif
2679
Bram Moolenaar38323e42007-03-06 19:22:53 +00002680#ifdef VMS
2681 fname = vms_fixfilename(fname);
2682#endif
2683
Bram Moolenaara2442432007-04-26 14:26:37 +00002684#ifdef __CYGWIN__
2685 /*
2686 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2687 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002688# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002689 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2690 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002691 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2692 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002693# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002694 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002695# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002696 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002697#endif
2698
Bram Moolenaar0f873732019-12-05 20:28:46 +01002699 // Expand it if forced or not an absolute path.
2700 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002701 if ((force || !mch_isFullName(fname))
2702 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
2704 /*
2705 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002706 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 * This will get the correct path name with "../" things.
2708 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002709 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002711 if (STRCMP(p, "/..") == 0)
2712 // for "/path/dir/.." include the "/.."
2713 p += 3;
2714
Bram Moolenaar38323e42007-03-06 19:22:53 +00002715#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 /*
2717 * Use fchdir() if possible, it's said to be faster and more
2718 * reliable. But on SunOS 4 it might not work. Check this by
2719 * doing a fchdir() right now.
2720 */
2721 if (!dont_fchdir)
2722 {
2723 fd = open(".", O_RDONLY | O_EXTRA, 0);
2724 if (fd >= 0 && fchdir(fd) < 0)
2725 {
2726 close(fd);
2727 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002728 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
2730 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
Bram Moolenaar0f873732019-12-05 20:28:46 +01002733 // Only change directory when we are sure we can return to where
2734 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002736#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 (mch_dirname(olddir, MAXPATHL) == FAIL
2740 || mch_chdir((char *)olddir) != 0))
2741 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002742 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 retval = FAIL;
2744 }
2745 else
2746 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002747 // The directory is copied into buf[], to be able to remove
2748 // the file name without changing it (could be a string in
2749 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 if (p - fname >= len)
2751 retval = FAIL;
2752 else
2753 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002754 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002756 {
2757 // Path does not exist (yet). For a full path fail,
2758 // will use the path as-is. For a relative path use
2759 // the current directory and append the file name.
2760 if (mch_isFullName(fname))
2761 retval = FAIL;
2762 else
2763 p = NULL;
2764 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002765 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002767 else
2768 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 *buf = NUL;
2770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
2772 }
2773 if (mch_dirname(buf, len) == FAIL)
2774 {
2775 retval = FAIL;
2776 *buf = NUL;
2777 }
2778 if (p != NULL)
2779 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002780#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 if (fd >= 0)
2782 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002783 if (p_verbose >= 5)
2784 {
2785 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002786 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002787 verbose_leave();
2788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
2791 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 l = mch_chdir((char *)olddir);
2794 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002795 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
itchyny051a40c2021-10-20 10:00:05 +01002797#ifdef HAVE_FCHDIR
2798 if (fd >= 0)
2799 close(fd);
2800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801
2802 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002803 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002804 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002805#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002806 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002808 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002811
Bram Moolenaar0f873732019-12-05 20:28:46 +01002812 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002813 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 return FAIL;
2815
Bram Moolenaar0f873732019-12-05 20:28:46 +01002816 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 if (STRCMP(fname, ".") != 0)
2818 STRCAT(buf, fname);
2819
2820 return OK;
2821}
2822
2823/*
2824 * Return TRUE if "fname" does not depend on the current directory.
2825 */
2826 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002827mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002829#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 return ( fname[0] == '/' || fname[0] == '.' ||
2831 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2832 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2833 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002834#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836#endif
2837}
2838
Bram Moolenaar24552be2005-12-10 20:17:30 +00002839#if defined(USE_FNAME_CASE) || defined(PROTO)
2840/*
2841 * Set the case of the file name, if it already exists. This will cause the
2842 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002843 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002844 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002845 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002846fname_case(
2847 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002848 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002849{
2850 struct stat st;
2851 char_u *slash, *tail;
2852 DIR *dirp;
2853 struct dirent *dp;
2854
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002855 if (mch_lstat((char *)name, &st) < 0)
2856 return;
2857
2858 // Open the directory where the file is located.
2859 slash = vim_strrchr(name, '/');
2860 if (slash == NULL)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002861 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002862 dirp = opendir(".");
2863 tail = name;
2864 }
2865 else
2866 {
2867 *slash = NUL;
2868 dirp = opendir((char *)name);
2869 *slash = '/';
2870 tail = slash + 1;
2871 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002872
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002873 if (dirp == NULL)
2874 return;
2875
2876 while ((dp = readdir(dirp)) != NULL)
2877 {
2878 // Only accept names that differ in case and are the same byte
2879 // length. TODO: accept different length name.
2880 if (STRICMP(tail, dp->d_name) == 0
2881 && STRLEN(tail) == STRLEN(dp->d_name))
Bram Moolenaar24552be2005-12-10 20:17:30 +00002882 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002883 char_u newname[MAXPATHL + 1];
2884 struct stat st2;
2885
2886 // Verify the inode is equal.
2887 vim_strncpy(newname, name, MAXPATHL);
2888 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2889 MAXPATHL - (tail - name));
2890 if (mch_lstat((char *)newname, &st2) >= 0
2891 && st.st_ino == st2.st_ino
2892 && st.st_dev == st2.st_dev)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002893 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002894 STRCPY(tail, dp->d_name);
2895 break;
Bram Moolenaar24552be2005-12-10 20:17:30 +00002896 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002897 }
2898 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002899
2900 closedir(dirp);
Bram Moolenaar24552be2005-12-10 20:17:30 +00002901}
2902#endif
2903
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904/*
2905 * Get file permissions for 'name'.
2906 * Returns -1 when it doesn't exist.
2907 */
2908 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002909mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910{
2911 struct stat statb;
2912
Bram Moolenaar0f873732019-12-05 20:28:46 +01002913 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914#ifdef VMS
2915 if (stat((char *)vms_fixfilename(name), &statb))
2916#else
2917 if (stat((char *)name, &statb))
2918#endif
2919 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002920#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002921 // The top bit makes the value negative, which means the file doesn't
2922 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002923 return statb.st_mode & ~S_ADDACE;
2924#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927}
2928
2929/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002930 * Set file permission for "name" to "perm".
2931 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 */
2933 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002934mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935{
2936 return (chmod((char *)
2937#ifdef VMS
2938 vms_fixfilename(name),
2939#else
2940 name,
2941#endif
2942 (mode_t)perm) == 0 ? OK : FAIL);
2943}
2944
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002945#if defined(HAVE_FCHMOD) || defined(PROTO)
2946/*
2947 * Set file permission for open file "fd" to "perm".
2948 * Return FAIL for failure, OK otherwise.
2949 */
2950 int
2951mch_fsetperm(int fd, long perm)
2952{
2953 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2954}
2955#endif
2956
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957#if defined(HAVE_ACL) || defined(PROTO)
2958# ifdef HAVE_SYS_ACL_H
2959# include <sys/acl.h>
2960# endif
2961# ifdef HAVE_SYS_ACCESS_H
2962# include <sys/access.h>
2963# endif
2964
2965# ifdef HAVE_SOLARIS_ACL
2966typedef struct vim_acl_solaris_T {
2967 int acl_cnt;
2968 aclent_t *acl_entry;
2969} vim_acl_solaris_T;
2970# endif
2971
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002972#if defined(HAVE_SELINUX) || defined(PROTO)
2973/*
2974 * Copy security info from "from_file" to "to_file".
2975 */
2976 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002977mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002978{
2979 if (from_file == NULL)
2980 return;
2981
2982 if (selinux_enabled == -1)
2983 selinux_enabled = is_selinux_enabled();
2984
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002985 if (selinux_enabled <= 0)
2986 return;
2987
2988 // Use "char *" instead of "security_context_t" to avoid a deprecation
2989 // warning.
2990 char *from_context = NULL;
2991 char *to_context = NULL;
2992
2993 if (getfilecon((char *)from_file, &from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002994 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002995 // If the filesystem doesn't support extended attributes,
2996 // the original had no special security context and the
2997 // target cannot have one either.
2998 if (errno == EOPNOTSUPP)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002999 return;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003000
3001 msg_puts(_("\nCould not get security context for "));
3002 msg_outtrans(from_file);
3003 msg_putchar('\n');
3004 return;
3005 }
3006 if (getfilecon((char *)to_file, &to_context) < 0)
3007 {
3008 msg_puts(_("\nCould not get security context for "));
3009 msg_outtrans(to_file);
3010 msg_putchar('\n');
3011 freecon (from_context);
3012 return ;
3013 }
3014 if (strcmp(from_context, to_context) != 0)
3015 {
3016 if (setfilecon((char *)to_file, from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003017 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003018 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003019 msg_outtrans(to_file);
3020 msg_putchar('\n');
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003021 }
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003022 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003023 freecon(to_context);
3024 freecon(from_context);
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003025}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003026#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003027
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003028#if defined(HAVE_SMACK) && !defined(PROTO)
3029/*
3030 * Copy security info from "from_file" to "to_file".
3031 */
3032 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01003033mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003034{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02003035 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003036 {
3037 XATTR_NAME_SMACK,
3038 XATTR_NAME_SMACKEXEC,
3039 XATTR_NAME_SMACKMMAP
3040 };
3041
3042 char buffer[SMACK_LABEL_LEN];
3043 const char *name;
3044 int index;
3045 int ret;
3046 ssize_t size;
3047
3048 if (from_file == NULL)
3049 return;
3050
3051 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
3052 / sizeof(smack_copied_attributes)[0]) ; index++)
3053 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003054 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003055 name = smack_copied_attributes[index];
3056
Bram Moolenaar0f873732019-12-05 20:28:46 +01003057 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003058 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
3059 if (size >= 0)
3060 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003061 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003062 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
3063 if (ret < 0)
3064 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003065 vim_snprintf((char *)IObuff, IOSIZE,
3066 _("Could not set security context %s for %s"),
3067 name, to_file);
3068 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003069 msg_putchar('\n');
3070 }
3071 }
3072 else
3073 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003074 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003075 switch (errno)
3076 {
3077 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003078 // extended attributes aren't supported or enabled
3079 // should a message be echoed? not sure...
3080 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003081
3082 case ERANGE:
3083 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003084 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003085 vim_snprintf((char *)IObuff, IOSIZE,
3086 _("Could not get security context %s for %s. Removing it!"),
3087 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003088 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003089 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003090 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003091
3092 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003093 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003094 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003095 // Silently ignore errors, apparently this happens when
3096 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003097 break;
3098 }
3099 }
3100 }
3101}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003102#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003103
Christian Brabandte085dfd2023-09-30 12:49:18 +02003104#ifdef FEAT_XATTR
3105/*
3106 * Copy extended attributes from_file to to_file
3107 */
3108 void
3109mch_copy_xattr(char_u *from_file, char_u *to_file)
3110{
3111 char *xattr_buf;
3112 size_t size;
3113 size_t tsize;
3114 ssize_t keylen, vallen, max_vallen = 0;
3115 char *key;
3116 char *val = NULL;
3117 char *errmsg = NULL;
3118
3119 if (from_file == NULL)
3120 return;
3121
3122 // get the length of the extended attributes
3123 size = listxattr((char *)from_file, NULL, 0);
3124 // not supported or no attributes to copy
3125 if (errno == ENOTSUP || size == 0)
3126 return;
3127 xattr_buf = (char*)alloc(size);
3128 if (xattr_buf == NULL)
3129 return;
3130 size = listxattr((char *)from_file, xattr_buf, size);
3131 tsize = size;
3132
3133 errno = 0;
3134
3135 for (int round = 0; round < 2; round++)
3136 {
3137
3138 key = xattr_buf;
3139 if (round == 1)
3140 size = tsize;
3141
3142 while (size > 0)
3143 {
3144 vallen = getxattr((char *)from_file, key,
3145 val, round ? max_vallen : 0);
3146 // only set the attribute in the second round
3147 if (vallen >= 0 && round &&
3148 setxattr((char *)to_file, key, val, vallen, 0) == 0)
3149 ;
3150 else if (errno)
3151 {
3152 switch (errno)
3153 {
3154 case E2BIG:
3155 errmsg = e_xattr_e2big;
3156 goto error_exit;
3157 case ENOTSUP:
3158 errmsg = e_xattr_enotsup;
3159 goto error_exit;
3160 case ERANGE:
3161 errmsg = e_xattr_erange;
3162 goto error_exit;
3163 default:
3164 errmsg = e_xattr_other;
3165 goto error_exit;
3166 }
3167 }
3168
3169 if (round == 0 && vallen > max_vallen)
3170 max_vallen = vallen;
3171
3172 // add one for terminating null
3173 keylen = STRLEN(key) + 1;
3174 size -= keylen;
3175 key += keylen;
3176 }
3177 if (round)
3178 break;
3179
3180 val = (char*)alloc(max_vallen + 1);
3181 if (val == NULL)
3182 goto error_exit;
3183
3184 }
3185error_exit:
3186 vim_free(xattr_buf);
3187 vim_free(val);
3188
3189 if (errmsg != NULL)
3190 emsg((char *)errmsg);
3191}
3192#endif
3193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194/*
3195 * Return a pointer to the ACL of file "fname" in allocated memory.
3196 * Return NULL if the ACL is not available for whatever reason.
3197 */
3198 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003199mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
3201 vim_acl_T ret = NULL;
3202#ifdef HAVE_POSIX_ACL
3203 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3204#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003205#ifdef HAVE_SOLARIS_ZFS_ACL
3206 acl_t *aclent;
3207
3208 if (acl_get((char *)fname, 0, &aclent) < 0)
3209 return NULL;
3210 ret = (vim_acl_T)aclent;
3211#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212#ifdef HAVE_SOLARIS_ACL
3213 vim_acl_solaris_T *aclent;
3214
3215 aclent = malloc(sizeof(vim_acl_solaris_T));
3216 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3217 {
3218 free(aclent);
3219 return NULL;
3220 }
3221 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3222 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3223 {
3224 free(aclent->acl_entry);
3225 free(aclent);
3226 return NULL;
3227 }
3228 ret = (vim_acl_T)aclent;
3229#else
3230#if defined(HAVE_AIX_ACL)
3231 int aclsize;
3232 struct acl *aclent;
3233
3234 aclsize = sizeof(struct acl);
3235 aclent = malloc(aclsize);
3236 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3237 {
3238 if (errno == ENOSPC)
3239 {
3240 aclsize = aclent->acl_len;
3241 aclent = realloc(aclent, aclsize);
3242 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3243 {
3244 free(aclent);
3245 return NULL;
3246 }
3247 }
3248 else
3249 {
3250 free(aclent);
3251 return NULL;
3252 }
3253 }
3254 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003255#endif // HAVE_AIX_ACL
3256#endif // HAVE_SOLARIS_ACL
3257#endif // HAVE_SOLARIS_ZFS_ACL
3258#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 return ret;
3260}
3261
3262/*
3263 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3264 */
3265 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003266mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267{
3268 if (aclent == NULL)
3269 return;
3270#ifdef HAVE_POSIX_ACL
3271 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3272#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003273#ifdef HAVE_SOLARIS_ZFS_ACL
3274 acl_set((char *)fname, (acl_t *)aclent);
3275#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276#ifdef HAVE_SOLARIS_ACL
3277 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3278 ((vim_acl_solaris_T *)aclent)->acl_entry);
3279#else
3280#ifdef HAVE_AIX_ACL
3281 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003282#endif // HAVE_AIX_ACL
3283#endif // HAVE_SOLARIS_ACL
3284#endif // HAVE_SOLARIS_ZFS_ACL
3285#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286}
3287
3288 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003289mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
3291 if (aclent == NULL)
3292 return;
3293#ifdef HAVE_POSIX_ACL
3294 acl_free((acl_t)aclent);
3295#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003296#ifdef HAVE_SOLARIS_ZFS_ACL
3297 acl_free((acl_t *)aclent);
3298#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299#ifdef HAVE_SOLARIS_ACL
3300 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3301 free(aclent);
3302#else
3303#ifdef HAVE_AIX_ACL
3304 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003305#endif // HAVE_AIX_ACL
3306#endif // HAVE_SOLARIS_ACL
3307#endif // HAVE_SOLARIS_ZFS_ACL
3308#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309}
3310#endif
3311
3312/*
3313 * Set hidden flag for "name".
3314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003316mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003318 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319}
3320
3321/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003322 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 * return FALSE if "name" is not a directory
3324 * return FALSE for error
3325 */
3326 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003327mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 struct stat statb;
3330
Bram Moolenaar0f873732019-12-05 20:28:46 +01003331 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 return FALSE;
3333 if (stat((char *)name, &statb))
3334 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336}
3337
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003338/*
3339 * return TRUE if "name" is a directory, NOT a symlink to a directory
3340 * return FALSE if "name" is not a directory
3341 * return FALSE for error
3342 */
3343 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003344mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003345{
3346 struct stat statb;
3347
Bram Moolenaar0f873732019-12-05 20:28:46 +01003348 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003349 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003350 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003351 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003352 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003353}
3354
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355/*
3356 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3357 */
3358 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003359executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 struct stat st;
3362
3363 if (stat((char *)name, &st))
3364 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003365#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003366 // Like on Unix system file can have executable rights but not necessarily
3367 // be an executable, but on Unix is not a default for an ordinary file to
3368 // have an executable flag - on VMS it is in most cases.
3369 // Therefore, this check does not have any sense - let keep us to the
3370 // conventions instead:
3371 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003372 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003373 int vms_executable = 0;
3374 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3375 {
3376 if (strstr(vms_tolower((char*)name),".exe") != NULL
3377 || strstr(vms_tolower((char*)name),".com")!= NULL)
3378 vms_executable = 1;
3379 }
3380 return vms_executable;
3381#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384}
3385
3386/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003387 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003388 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 * Return -1 if unknown.
3390 */
3391 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003392mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
3394 char_u *buf;
3395 char_u *p, *e;
3396 int retval;
3397
Bram Moolenaar0f873732019-12-05 20:28:46 +01003398 // When "use_path" is false and if it's an absolute or relative path don't
3399 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003400 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003401 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003402 // There must be a path separator, files in the current directory
3403 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003404 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003405 {
3406 if (path != NULL)
3407 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003408 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003409 *path = FullName_save(name, TRUE);
3410 else
3411 *path = vim_strsave(name);
3412 }
3413 return TRUE;
3414 }
3415 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417
3418 p = (char_u *)getenv("PATH");
3419 if (p == NULL || *p == NUL)
3420 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003421 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 if (buf == NULL)
3423 return -1;
3424
3425 /*
3426 * Walk through all entries in $PATH to check if "name" exists there and
3427 * is an executable file.
3428 */
3429 for (;;)
3430 {
3431 e = (char_u *)strchr((char *)p, ':');
3432 if (e == NULL)
3433 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003434 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 STRCPY(buf, "./");
3436 else
3437 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003438 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 add_pathsep(buf);
3440 }
3441 STRCAT(buf, name);
3442 retval = executable_file(buf);
3443 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003444 {
3445 if (path != NULL)
3446 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003447 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003448 *path = FullName_save(buf, TRUE);
3449 else
3450 *path = vim_strsave(buf);
3451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
3455 if (*e != ':')
3456 break;
3457 p = e + 1;
3458 }
3459
3460 vim_free(buf);
3461 return retval;
3462}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464/*
3465 * Check what "name" is:
3466 * NODE_NORMAL: file or directory (or doesn't exist)
3467 * NODE_WRITABLE: writable device, socket, fifo, etc.
3468 * NODE_OTHER: non-writable things
3469 */
3470 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003471mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
3473 struct stat st;
3474
3475 if (stat((char *)name, &st))
3476 return NODE_NORMAL;
3477 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3478 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003479 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003481 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 return NODE_WRITABLE;
3483}
3484
3485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003486mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
3488#ifdef HAVE_CHECK_STACK_GROWTH
3489 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 check_stack_growth((char *)&i);
3492
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003493# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 get_stack_limit();
3495# endif
3496
3497#endif
3498
3499 /*
3500 * Setup an alternative stack for signals. Helps to catch signals when
3501 * running out of stack space.
3502 * Use of sigaltstack() is preferred, it's more portable.
3503 * Ignore any errors.
3504 */
3505#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003506 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 init_signal_stack();
3508#endif
3509}
3510
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003511#if defined(EXITFREE) || defined(PROTO)
3512 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003513mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003514{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003515# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3516 if (clip_star.owned)
3517 clip_lose_selection(&clip_star);
3518 if (clip_plus.owned)
3519 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003520# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003521# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003522 if (xterm_Shell != (Widget)0)
3523 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003524# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003525 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003526 if (xterm_dpy != NULL)
3527 XtCloseDisplay(xterm_dpy);
3528 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003529 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003530 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003531# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003532 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003533# endif
3534 }
3535# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003536# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003537# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003538 if (x11_display != NULL
3539# ifdef FEAT_XCLIPBOARD
3540 && x11_display != xterm_dpy
3541# endif
3542 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003543 XCloseDisplay(x11_display);
3544# endif
3545# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003546 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003547# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003548 vim_free(oldtitle);
3549 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003550}
3551#endif
3552
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553/*
3554 * Output a newline when exiting.
3555 * Make sure the newline goes to the same stream as the text.
3556 */
3557 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003558exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003560 if (silent_mode)
3561 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 if (newline_on_exit || msg_didout)
3563 {
3564 if (msg_use_printf())
3565 {
3566 if (info_message)
3567 mch_msg("\n");
3568 else
3569 mch_errmsg("\r\n");
3570 }
3571 else
3572 out_char('\n');
3573 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003574 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003576 restore_cterm_colors(); // get original colors back
3577 msg_clr_eos_force(); // clear the rest of the display
3578 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
3580}
3581
Bram Moolenaarb4151682020-05-11 22:13:28 +02003582#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003583# if (defined(__GNUC__) \
3584 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3585 || (defined(__clang__) && (__clang_major__ >= 12))
3586extern void __gcov_dump(void);
3587extern void __gcov_reset(void);
3588# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3589# else
3590extern void __gcov_flush(void);
3591# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003592#endif
3593
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003595mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596{
3597 exiting = TRUE;
3598
3599#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3600 x11_export_final_selection();
3601#endif
3602
3603#ifdef FEAT_GUI
3604 if (!gui.in_use)
3605#endif
3606 {
3607 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003608 if (!is_not_a_term())
3609 {
3610 // restore xterm title and icon name
3611 mch_restore_title(SAVE_RESTORE_BOTH);
3612 term_pop_title(SAVE_RESTORE_BOTH);
3613 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003614
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 /*
3616 * When t_ti is not empty but it doesn't cause swapping terminal
3617 * pages, need to output a newline when msg_didout is set. But when
3618 * t_ti does swap pages it should not go to the shell page. Do this
3619 * before stoptermcap().
3620 */
3621 if (swapping_screen() && !newline_on_exit)
3622 exit_scroll();
3623
Bram Moolenaar0f873732019-12-05 20:28:46 +01003624 // Stop termcap: May need to check for T_CRV response, which
3625 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 stoptermcap();
3627
3628 /*
3629 * A newline is only required after a message in the alternate screen.
3630 * This is set to TRUE by wait_return().
3631 */
3632 if (!swapping_screen() || newline_on_exit)
3633 exit_scroll();
3634
Bram Moolenaar0f873732019-12-05 20:28:46 +01003635 // Cursor may have been switched off without calling starttermcap()
3636 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 if (full_screen)
3638 cursor_on();
3639 }
3640 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003641 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003642
3643#ifdef USE_GCOV_FLUSH
3644 // Flush coverage info before possibly being killed by a deadly signal.
3645 __gcov_flush();
3646#endif
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 may_core_dump();
3649#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 gui_exit(r);
3652#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003653
Bram Moolenaar56718732006-03-15 22:53:57 +00003654#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003655 mac_conv_cleanup();
3656#endif
3657
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003659 // A core dump won't be created if the signal handler
3660 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 if (deadly_signal != 0)
3662 return;
3663#endif
3664
Bram Moolenaar009b2592004-10-24 19:18:58 +00003665#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003666 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003667#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003668
3669#ifdef EXITFREE
3670 free_all_mem();
3671#endif
3672
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 exit(r);
3674}
3675
3676 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003677may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678{
3679 if (deadly_signal != 0)
3680 {
ichizok378447f2023-05-11 22:25:42 +01003681 mch_signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003682 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684}
3685
3686#ifndef VMS
3687
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003688/*
3689 * Get the file descriptor to use for tty operations.
3690 */
3691 static int
3692get_tty_fd(int fd)
3693{
3694 int tty_fd = fd;
3695
3696#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3697 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3698 // "fd" when it is a master device.
3699 if (mch_isatty(fd) > 1)
3700 {
3701 char *name;
3702
3703 name = ptsname(fd);
3704 if (name == NULL)
3705 return -1;
3706
3707 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3708 if (tty_fd < 0)
3709 return -1;
3710 }
3711#endif
3712 return tty_fd;
3713}
3714
3715 static int
3716mch_tcgetattr(int fd, void *term)
3717{
3718 int tty_fd;
3719 int retval = -1;
3720
3721 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003722 if (tty_fd < 0)
3723 return -1;
3724
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003725#ifdef NEW_TTY_SYSTEM
3726# ifdef HAVE_TERMIOS_H
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003727 retval = tcgetattr(tty_fd, (struct termios *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003728# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003729 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003730# endif
3731#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003732 // for "old" tty systems
3733 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003734#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003735 if (tty_fd != fd)
3736 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003737 return retval;
3738}
3739
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003741mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
3743 static int first = TRUE;
3744
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003745#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746# ifdef HAVE_TERMIOS_H
3747 static struct termios told;
3748 struct termios tnew;
3749# else
3750 static struct termio told;
3751 struct termio tnew;
3752# endif
3753
3754 if (first)
3755 {
3756 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003757 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 }
3759
3760 tnew = told;
3761 if (tmode == TMODE_RAW)
3762 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003763 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003764 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3765 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003767# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003768 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769# endif
3770 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003771# ifdef ONLCR
3772 // Don't map NL -> CR NL, we do it ourselves.
3773 // Also disable expanding tabs if possible.
3774# ifdef XTABS
3775 tnew.c_oflag &= ~(ONLCR | XTABS);
3776# else
3777# ifdef TAB3
3778 tnew.c_oflag &= ~(ONLCR | TAB3);
3779# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003781# endif
3782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003784 tnew.c_cc[VMIN] = 1; // return after 1 char
3785 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
3787 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003788 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003789 // Also reset ICANON here, otherwise on Solaris select() won't see
3790 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003791 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003792 tnew.c_cc[VMIN] = 1; // return after 1 char
3793 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
3796# if defined(HAVE_TERMIOS_H)
3797 {
3798 int n = 10;
3799
Bram Moolenaar0f873732019-12-05 20:28:46 +01003800 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3801 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3803 && errno == EINTR && n > 0)
3804 --n;
3805 }
3806# else
3807 ioctl(read_cmd_fd, TCSETA, &tnew);
3808# endif
3809
3810#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 /*
3812 * for "old" tty systems
3813 */
3814# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003815# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816# endif
3817 static struct sgttyb ttybold;
3818 struct sgttyb ttybnew;
3819
3820 if (first)
3821 {
3822 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003823 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
3825
3826 ttybnew = ttybold;
3827 if (tmode == TMODE_RAW)
3828 {
3829 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3830 ttybnew.sg_flags |= RAW;
3831 }
3832 else if (tmode == TMODE_SLEEP)
3833 ttybnew.sg_flags &= ~(ECHO);
3834 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3835#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003836 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837}
3838
3839/*
3840 * Try to get the code for "t_kb" from the stty setting
3841 *
3842 * Even if termcap claims a backspace key, the user's setting *should*
3843 * prevail. stty knows more about reality than termcap does, and if
3844 * somebody's usual erase key is DEL (which, for most BSD users, it will
3845 * be), they're going to get really annoyed if their erase key starts
3846 * doing forward deletes for no reason. (Eric Fischer)
3847 */
3848 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003849get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003851 ttyinfo_T info;
3852 char_u buf[2];
3853 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003855 if (get_tty_info(read_cmd_fd, &info) != OK)
3856 return;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003857
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003858 intr_char = info.interrupt;
3859 buf[0] = info.backspace;
3860 buf[1] = NUL;
3861 add_termcode((char_u *)"kb", buf, FALSE);
3862
3863 // If <BS> and <DEL> are now the same, redefine <DEL>.
3864 p = find_termcode((char_u *)"kD");
3865 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3866 do_fixdel(NULL);
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003867}
3868
3869/*
3870 * Obtain the characters that Backspace and Enter produce on "fd".
3871 * Returns OK or FAIL.
3872 */
3873 int
3874get_tty_info(int fd, ttyinfo_T *info)
3875{
3876#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877# ifdef HAVE_TERMIOS_H
3878 struct termios keys;
3879# else
3880 struct termio keys;
3881# endif
3882
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003883 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003885 info->backspace = keys.c_cc[VERASE];
3886 info->interrupt = keys.c_cc[VINTR];
3887 if (keys.c_iflag & ICRNL)
3888 info->enter = NL;
3889 else
3890 info->enter = CAR;
3891 if (keys.c_oflag & ONLCR)
3892 info->nl_does_cr = TRUE;
3893 else
3894 info->nl_does_cr = FALSE;
3895 return OK;
3896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003898 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 struct sgttyb keys;
3900
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003901 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003903 info->backspace = keys.sg_erase;
3904 info->interrupt = keys.sg_kill;
3905 info->enter = CAR;
3906 info->nl_does_cr = TRUE;
3907 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003910 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911}
3912
Bram Moolenaar0f873732019-12-05 20:28:46 +01003913#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003915static int mouse_ison = FALSE;
3916
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917/*
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003918 * Set mouse clicks on or off and possible enable mouse movement events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 */
3920 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003921mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003923#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003924 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 int xterm_mouse_vers;
3927
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003928#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003929 if (!on)
3930 // Make sure not tracing mouse movements. Important when a button-down
3931 // was received but no release yet.
3932 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003933#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003934
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003935 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003936#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003937 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003938#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003939 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003940 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 return;
3942
3943 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003944
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003945#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003946 if (ttym_flags == TTYM_URXVT)
3947 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003948 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003949 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003950 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003951#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003952
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00003953 if (T_CXM != NULL && *T_CXM != NUL)
3954 {
3955 term_enable_mouse(on);
3956 }
3957 else if (ttym_flags == TTYM_SGR)
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003958 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003959 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003960 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003961 mouse_ison = on;
3962 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003963
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003964#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003965 if (bevalterm_ison != (p_bevalterm && on))
3966 {
3967 bevalterm_ison = (p_bevalterm && on);
3968 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003969 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003970 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003971 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003972#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003973
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 if (xterm_mouse_vers > 0)
3975 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003976 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 out_str_nf((char_u *)
3978 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003979 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003980#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003981 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003982#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003983 "\033[?1002h")
3984 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003985 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003987 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003988 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
3990
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003991#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 else if (ttym_flags == TTYM_DEC)
3993 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003994 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003996 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003998 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004002#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 else
4004 {
4005 if (on)
4006 {
4007 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004008 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
4010 else
4011 {
4012 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004013 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
4015 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004018#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004019 else
4020 {
4021 if (on)
4022 {
4023 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004024 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004025 }
4026 else
4027 {
4028 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004029 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004030 }
4031 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004032#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004033
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004034#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 else
4036 {
4037 if (on)
4038 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004039 // D - Enable Mouse up/down messages
4040 // L - Enable Left Button Reporting
4041 // M - Enable Middle Button Reporting
4042 // R - Enable Right Button Reporting
4043 // K - Enable SHIFT and CTRL key Reporting
4044 // + - Enable Advanced messaging of mouse moves and up/down messages
4045 // Q - Quiet No Ack
4046 // # - Numeric value of mouse pointer required
4047 // 0 = Multiview 2000 cursor, used as standard
4048 // 1 = Windows Arrow
4049 // 2 = Windows I Beam
4050 // 3 = Windows Hour Glass
4051 // 4 = Windows Cross Hair
4052 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004053# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01004054 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004055 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004056# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004057 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004058# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004059 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
4061 else
4062 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004063 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004064 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
4066 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004067#endif
4068#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 else
4070 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004071 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 if (on)
4073 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
4074 else
4075 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004076 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079}
4080
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004081#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004082/*
4083 * Called when 'balloonevalterm' changed.
4084 */
4085 void
4086mch_bevalterm_changed(void)
4087{
4088 mch_setmouse(mouse_ison);
4089}
4090#endif
4091
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092/*
4093 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
4094 */
4095 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004096check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097{
4098# ifdef FEAT_MOUSE_XTERM
4099 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02004100# ifdef FEAT_MOUSE_URXVT
4101 && use_xterm_mouse() != 3
4102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103# ifdef FEAT_GUI
4104 && !gui.in_use
4105# endif
4106 )
4107 {
4108 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004109 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 if (*p_mouse != NUL)
4111 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004112 // force mouse off and maybe on to send possibly new mouse
4113 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 mch_setmouse(FALSE);
4115 setmouse();
4116 }
4117 }
4118 else
4119 del_mouse_termcode(KS_MOUSE);
4120# endif
4121
4122# ifdef FEAT_MOUSE_GPM
4123 if (!use_xterm_mouse()
4124# ifdef FEAT_GUI
4125 && !gui.in_use
4126# endif
4127 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004128 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02004129 else
4130 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131# endif
4132
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004133# ifdef FEAT_SYSMOUSE
4134 if (!use_xterm_mouse()
4135# ifdef FEAT_GUI
4136 && !gui.in_use
4137# endif
4138 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004139 set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004140# endif
4141
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01004143 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 if (!use_xterm_mouse()
4145# ifdef FEAT_GUI
4146 && !gui.in_use
4147# endif
4148 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004149 set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 else
4151 del_mouse_termcode(KS_JSBTERM_MOUSE);
4152# endif
4153
4154# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01004155 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
4156 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 if (!use_xterm_mouse()
4158# ifdef FEAT_GUI
4159 && !gui.in_use
4160# endif
4161 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004162 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 else
4164 del_mouse_termcode(KS_NETTERM_MOUSE);
4165# endif
4166
4167# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01004168 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004169 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170# ifdef FEAT_GUI
4171 && !gui.in_use
4172# endif
4173 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004174 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004175 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 else
4177 del_mouse_termcode(KS_DEC_MOUSE);
4178# endif
4179# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004180 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004181 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182# ifdef FEAT_GUI
4183 && !gui.in_use
4184# endif
4185 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004186 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 else
4188 del_mouse_termcode(KS_PTERM_MOUSE);
4189# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004190# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004191 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004192# ifdef FEAT_GUI
4193 && !gui.in_use
4194# endif
4195 )
4196 {
4197 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004198 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004199
4200 if (*p_mouse != NUL)
4201 {
4202 mch_setmouse(FALSE);
4203 setmouse();
4204 }
4205 }
4206 else
4207 del_mouse_termcode(KS_URXVT_MOUSE);
4208# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004209 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004210# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004211 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004212# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004213 )
4214 {
4215 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004216 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004217
4218 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004219 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004220
4221 if (*p_mouse != NUL)
4222 {
4223 mch_setmouse(FALSE);
4224 setmouse();
4225 }
4226 }
4227 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004228 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004229 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004230 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234#ifndef VMS
4235
4236/*
4237 * Try to get the current window size:
4238 * 1. with an ioctl(), most accurate method
4239 * 2. from the environment variables LINES and COLUMNS
4240 * 3. from the termcap
4241 * 4. keep using the old values
4242 * Return OK when size could be determined, FAIL otherwise.
4243 */
4244 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004245mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246{
4247 long rows = 0;
4248 long columns = 0;
4249 char_u *p;
4250
4251 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 * 1. try using an ioctl. It is the most accurate method.
4253 *
4254 * Try using TIOCGWINSZ first, some systems that have it also define
4255 * TIOCGSIZE but don't have a struct ttysize.
4256 */
4257# ifdef TIOCGWINSZ
4258 {
4259 struct winsize ws;
4260 int fd = 1;
4261
Bram Moolenaar0f873732019-12-05 20:28:46 +01004262 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (!isatty(fd) && isatty(read_cmd_fd))
4264 fd = read_cmd_fd;
4265 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4266 {
4267 columns = ws.ws_col;
4268 rows = ws.ws_row;
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004269# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004270 ch_log(NULL, "Got size with TIOCGWINSZ: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
4273 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004274# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275# ifdef TIOCGSIZE
4276 {
4277 struct ttysize ts;
4278 int fd = 1;
4279
Bram Moolenaar0f873732019-12-05 20:28:46 +01004280 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 if (!isatty(fd) && isatty(read_cmd_fd))
4282 fd = read_cmd_fd;
4283 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4284 {
4285 columns = ts.ts_cols;
4286 rows = ts.ts_lines;
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004287# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004288 ch_log(NULL, "Got size with TIOCGSIZE: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004292# endif // TIOCGSIZE
4293# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294
4295 /*
4296 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004297 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4298 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004300 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 {
4302 if ((p = (char_u *)getenv("LINES")))
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 rows = atoi((char *)p);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004305# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004306 ch_log(NULL, "Got 'lines' from $LINES: %ld", rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004307# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 if ((p = (char_u *)getenv("COLUMNS")))
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 columns = atoi((char *)p);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004312# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004313 ch_log(NULL, "Got 'columns' from $COLUMNS: %ld", columns);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004314# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317
4318#ifdef HAVE_TGETENT
4319 /*
4320 * 3. try reading "co" and "li" entries from termcap
4321 */
4322 if (columns == 0 || rows == 0)
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 getlinecol(&columns, &rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004325# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004326 ch_log(NULL, "Got size from termcap: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004327# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329#endif
4330
4331 /*
4332 * 4. If everything fails, use the old values
4333 */
4334 if (columns <= 0 || rows <= 0)
4335 return FAIL;
4336
4337 Rows = rows;
4338 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004339 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 return OK;
4341}
4342
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004343#if defined(FEAT_TERMINAL) || defined(PROTO)
4344/*
4345 * Report the windows size "rows" and "cols" to tty "fd".
4346 */
4347 int
4348mch_report_winsize(int fd, int rows, int cols)
4349{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004350 int tty_fd;
4351 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004352
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004353 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004354 if (tty_fd < 0)
4355 return FAIL;
4356
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004357# if defined(TIOCSWINSZ)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004358 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004359
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004360 ws.ws_col = cols;
4361 ws.ws_row = rows;
4362 ws.ws_xpixel = cols * 5;
4363 ws.ws_ypixel = rows * 10;
4364 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004365 ch_log(NULL, "ioctl(TIOCSWINSZ) %s", retval == 0 ? "success" : "failed");
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004366# elif defined(TIOCSSIZE)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004367 struct ttysize ts;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004368
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004369 ts.ts_cols = cols;
4370 ts.ts_lines = rows;
4371 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004372 ch_log(NULL, "ioctl(TIOCSSIZE) %s", retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004373# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004374 if (tty_fd != fd)
4375 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004376 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004377}
4378#endif
4379
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380/*
4381 * Try to set the window size to Rows and Columns.
4382 */
4383 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004384mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385{
4386 if (*T_CWS)
4387 {
4388 /*
4389 * NOTE: if you get an error here that term_set_winsize() is
4390 * undefined, check the output of configure. It could probably not
4391 * find a ncurses, termcap or termlib library.
4392 */
4393 term_set_winsize((int)Rows, (int)Columns);
4394 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004395 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
4397}
4398
Bram Moolenaar0f873732019-12-05 20:28:46 +01004399#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
4401/*
4402 * Rows and/or Columns has changed.
4403 */
4404 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004405mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004407 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408}
4409
Bram Moolenaar205b8862011-09-07 15:04:31 +02004410/*
4411 * Wait for process "child" to end.
4412 * Return "child" if it exited properly, <= 0 on error.
4413 */
4414 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004415wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004416{
4417 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004418 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004419
4420 while (wait_pid != child)
4421 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004422 // When compiled with Python threads are probably used, in which case
4423 // wait() sometimes hangs for no obvious reason. Use waitpid()
4424 // instead and loop (like the GUI). Also needed for other interfaces,
4425 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004426# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004427 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004428# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004429 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004430# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004431 if (wait_pid == 0)
4432 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004433 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004434 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004435 if (++delay_msec > 10)
4436 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004437 continue;
4438 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004439 if (wait_pid <= 0
4440# ifdef ECHILD
4441 && errno == ECHILD
4442# endif
4443 )
4444 break;
4445 }
4446 return wait_pid;
4447}
4448
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004449#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004450/*
4451 * Set the environment for a child process.
4452 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004453 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004454set_child_environment(
4455 long rows,
4456 long columns,
4457 char *term,
4458 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004459{
4460# ifdef HAVE_SETENV
4461 char envbuf[50];
4462# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004463 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004464 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004465 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004466 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004467 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004468# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004469 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004470# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004471# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004472 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004473# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004474# endif
4475
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004476# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004477 setenv("TERM", term, 1);
4478 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004479 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004480 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004481 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004482 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004483 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004484 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004485 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004486# ifdef FEAT_TERMINAL
4487 if (is_terminal)
4488 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004489 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004490 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4491 }
4492# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004493# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004494 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004495# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004496# else
4497 /*
4498 * Putenv does not copy the string, it has to remain valid.
4499 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004500 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004501 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004502 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4503 putenv(envbuf_Term);
4504 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004505 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004506 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4507 putenv(envbuf_Lines);
4508 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4509 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004510 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004511 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004512 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004513# ifdef FEAT_TERMINAL
4514 if (is_terminal)
4515 {
4516 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004517 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004518 putenv(envbuf_Version);
4519 }
4520# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004521# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004522 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4523 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4524 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004525# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004526# endif
4527}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004528
4529 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004530set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004531{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004532 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004533}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004534#endif
4535
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004536#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004537/*
4538 * Open a PTY, with FD for the master and slave side.
4539 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004540 * When successful both file descriptors are stored and the allocated pty name
4541 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004542 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004543 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004544open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004545{
4546 char *tty_name;
4547
Bram Moolenaar59386482019-02-10 22:43:46 +01004548 if (name1 != NULL)
4549 *name1 = NULL;
4550 if (name2 != NULL)
4551 *name2 = NULL;
4552
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004553 *pty_master_fd = mch_openpty(&tty_name); // open pty
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004554 if (*pty_master_fd < 0)
4555 return;
4556
4557 // Leaving out O_NOCTTY may lead to waitpid() always returning
4558 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4559 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004560#ifdef O_NOCTTY
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004561 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004562#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004563 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004564#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004565 if (*pty_slave_fd < 0)
4566 {
4567 close(*pty_master_fd);
4568 *pty_master_fd = -1;
4569 }
4570 else
4571 {
4572 if (name1 != NULL)
4573 *name1 = vim_strsave((char_u *)tty_name);
4574 if (name2 != NULL)
4575 *name2 = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004576 }
4577}
4578#endif
4579
Bram Moolenaarfae42832017-08-01 22:24:26 +02004580/*
4581 * Send SIGINT to a child process if "c" is an interrupt character.
4582 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004583 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004584may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4585{
4586# ifdef SIGINT
4587 if (c == Ctrl_C || c == intr_char)
4588 {
4589# ifdef HAVE_SETSID
4590 kill(-pid, SIGINT);
4591# else
4592 kill(0, SIGINT);
4593# endif
4594 if (wpid > 0)
4595 kill(wpid, SIGINT);
4596 }
4597# endif
4598}
4599
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004600#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004601
Bram Moolenaar0f873732019-12-05 20:28:46 +01004602/*
4603 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4604 * pointers, the last one is NULL.
4605 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4606 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004607 int
4608unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004609 char_u *cmd,
4610 char ***argvp,
4611 char_u **sh_tofree,
4612 char_u **shcf_tofree)
4613{
4614 char **argv = NULL;
4615 int argc;
4616
4617 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004618 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004619 return FAIL;
4620
4621 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4622 return FAIL;
4623 *argvp = argv;
4624
4625 if (cmd != NULL)
4626 {
4627 char_u *s;
4628 char_u *p;
4629
4630 if (extra_shell_arg != NULL)
4631 argv[argc++] = (char *)extra_shell_arg;
4632
Bram Moolenaar0f873732019-12-05 20:28:46 +01004633 // Break 'shellcmdflag' into white separated parts. This doesn't
4634 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004635 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004636 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004637 return FAIL;
4638 s = *shcf_tofree;
4639 p = p_shcf;
4640 while (*p != NUL)
4641 {
4642 argv[argc++] = (char *)s;
4643 while (*p && *p != ' ' && *p != TAB)
4644 *s++ = *p++;
4645 *s++ = NUL;
4646 p = skipwhite(p);
4647 }
4648
4649 argv[argc++] = (char *)cmd;
4650 }
4651 argv[argc] = NULL;
4652 return OK;
4653}
4654#endif
4655
4656#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4657/*
4658 * Use a terminal window to run a shell command in.
4659 */
4660 static int
4661mch_call_shell_terminal(
4662 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004663 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004664{
4665 jobopt_T opt;
4666 char **argv = NULL;
4667 char_u *tofree1 = NULL;
4668 char_u *tofree2 = NULL;
4669 int retval = -1;
4670 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004671 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004672 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004673 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004674
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004675 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004676 goto theend;
4677
4678 init_job_options(&opt);
4679 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4680 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004681 if (buf == NULL)
4682 goto theend;
4683
4684 job = term_getjob(buf->b_term);
4685 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004686
Bram Moolenaar0f873732019-12-05 20:28:46 +01004687 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004688 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004689 if (curbuf == buf)
Bram Moolenaar13568252018-03-16 20:46:58 +01004690 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004691 // Only when managed to find a window for "buf",
4692 clear_oparg(&oa);
4693 while (term_use_loop())
Bram Moolenaar13568252018-03-16 20:46:58 +01004694 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004695 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4696 {
4697 // If terminal_loop() returns OK we got a key that is handled
4698 // in Normal model. We don't do redrawing anyway.
4699 if (terminal_loop(TRUE) == OK)
4700 normal_cmd(&oa, TRUE);
4701 }
4702 else
Bram Moolenaar13568252018-03-16 20:46:58 +01004703 normal_cmd(&oa, TRUE);
4704 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00004705 retval = job->jv_exitval;
4706 ch_log(NULL, "system command finished");
4707
4708 job_unref(job);
4709
4710 // restore curwin/curbuf and a few other things
4711 aucmd_restbuf(&aco);
Bram Moolenaar13568252018-03-16 20:46:58 +01004712 }
Bram Moolenaar13568252018-03-16 20:46:58 +01004713
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01004714 // Only require pressing Enter when redrawing, to avoid that system() gets
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004715 // the hit-enter prompt even though it didn't output anything.
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004716 if (RedrawingDisabled == 0)
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004717 wait_return(TRUE);
Bram Moolenaar13568252018-03-16 20:46:58 +01004718 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4719
4720theend:
4721 vim_free(argv);
4722 vim_free(tofree1);
4723 vim_free(tofree2);
4724 return retval;
4725}
4726#endif
4727
4728#ifdef USE_SYSTEM
4729/*
4730 * Use system() to start the shell: simple but slow.
4731 */
4732 static int
4733mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004734 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004735 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
4737#ifdef VMS
4738 char *ifn = NULL;
4739 char *ofn = NULL;
4740#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004741 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004742 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004743 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744
4745 out_flush();
4746
4747 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004748 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749
Bram Moolenaar62b42182010-09-21 22:09:37 +02004750# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004751 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004752 loose_clipboard();
4753# endif
4754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 if (cmd == NULL)
4756 x = system((char *)p_sh);
4757 else
4758 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004759# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 if (ofn = strchr((char *)cmd, '>'))
4761 *ofn++ = '\0';
4762 if (ifn = strchr((char *)cmd, '<'))
4763 {
4764 char *p;
4765
4766 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004767 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 if (p)
4769 *p = '\0';
4770 }
4771 if (ofn)
4772 x = vms_sys((char *)cmd, ofn, ifn);
4773 else
4774 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004775# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004776 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004778 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 if (newcmd == NULL)
4780 x = 0;
4781 else
4782 {
4783 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4784 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4785 (char *)p_shcf,
4786 (char *)cmd);
4787 x = system((char *)newcmd);
4788 vim_free(newcmd);
4789 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792# ifdef VMS
4793 x = vms_sys_status(x);
4794# endif
4795 if (emsg_silent)
4796 ;
4797 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004798 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 else if (x && !(options & SHELL_SILENT))
4800 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004801 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 msg_outnum((long)x);
4803 msg_putchar('\n');
4804 }
4805
4806 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004807 {
4808 // The shell may have messed with the mode, always set it.
4809 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004810 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004813# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4814 restore_clipboard();
4815# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004817}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818
Bram Moolenaar0f873732019-12-05 20:28:46 +01004819#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
Bram Moolenaar0f873732019-12-05 20:28:46 +01004821# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4822 // 127, some shells use that already
4823# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824
Bram Moolenaar13568252018-03-16 20:46:58 +01004825/*
4826 * Don't use system(), use fork()/exec().
4827 */
4828 static int
4829mch_call_shell_fork(
4830 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004831 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004832{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004833 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004835 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 pid_t wait_pid = 0;
4837# ifdef HAVE_UNION_WAIT
4838 union wait status;
4839# else
4840 int status = -1;
4841# endif
4842 int retval = -1;
4843 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004844 char_u *tofree1 = NULL;
4845 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004847 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004848# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004850# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004851 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 int fd_fromshell[2];
4853 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004854 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
4856 out_flush();
4857 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004858 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004859 if (tmode == TMODE_RAW)
4860 // The shell may have messed with the mode, always set it later.
4861 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004863 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004864 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004865
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004867 * For the GUI, when writing the output into the buffer and when reading
4868 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4869 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004871 if ((options & (SHELL_READ|SHELL_WRITE))
4872# ifdef FEAT_GUI
4873 || (gui.in_use && show_shell_mess)
4874# endif
4875 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004877# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 /*
4879 * Try to open a master pty.
4880 * If this works, open the slave pty.
4881 * If the slave can't be opened, close the master pty.
4882 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004883 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004884 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 /*
4886 * If not opening a pty or it didn't work, try using pipes.
4887 */
4888 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 {
4891 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004892 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
4894 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004895 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
4897 close(fd_toshell[0]);
4898 close(fd_toshell[1]);
4899 }
4900 }
4901 if (pipe_error)
4902 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004903 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 out_flush();
4905 }
4906 }
4907 }
4908
Bram Moolenaar0f873732019-12-05 20:28:46 +01004909 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004911 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004912 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004913 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004914 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004916 UNBLOCK_SIGNALS(&curset);
4917
Bram Moolenaar32526b32019-01-19 17:43:09 +01004918 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004919 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004921 || (gui.in_use && show_shell_mess)
4922# endif
4923 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004925# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004926 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
4928 close(pty_master_fd);
4929 close(pty_slave_fd);
4930 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004931 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 {
4934 close(fd_toshell[0]);
4935 close(fd_toshell[1]);
4936 close(fd_fromshell[0]);
4937 close(fd_fromshell[1]);
4938 }
4939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004941 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004943 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004944 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00004946# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01004947 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004948 {
4949 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004950 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004951 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004952# endif
4953
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (!show_shell_mess || (options & SHELL_EXPAND))
4955 {
4956 int fd;
4957
4958 /*
4959 * Don't want to show any message from the shell. Can't just
4960 * close stdout and stderr though, because some systems will
4961 * break if you try to write to them after that, so we must
4962 * use dup() to replace them with something else -- webb
4963 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4964 * waiting for input.
4965 */
4966 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4967 fclose(stdin);
4968 fclose(stdout);
4969 fclose(stderr);
4970
4971 /*
4972 * If any of these open()'s and dup()'s fail, we just continue
4973 * anyway. It's not fatal, and on most systems it will make
4974 * no difference at all. On a few it will cause the execvp()
4975 * to exit with a non-zero status even when the completion
4976 * could be done, which is nothing too serious. If the open()
4977 * or dup() failed we'd just do the same thing ourselves
4978 * anyway -- webb
4979 */
4980 if (fd >= 0)
4981 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004982 vim_ignored = dup(fd); // To replace stdin (fd 0)
4983 vim_ignored = dup(fd); // To replace stdout (fd 1)
4984 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985
Bram Moolenaar0f873732019-12-05 20:28:46 +01004986 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 close(fd);
4988 }
4989 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004990 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004992 || gui.in_use
4993# endif
4994 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 {
4996
Bram Moolenaardf177f62005-02-22 08:39:57 +00004997# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004998 // Create our own process group, so that the child and all its
4999 // children can be kill()ed. Don't do this when using pipes,
5000 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005001 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00005002 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005003 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00005004# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005005 // When doing "!xterm&" and 'shell' is bash: the shell
5006 // will exit and send SIGHUP to all processes in its
5007 // group, killing the just started process. Ignore SIGHUP
5008 // to avoid that. (suggested by Simon Schubert)
ichizok378447f2023-05-11 22:25:42 +01005009 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar07256082009-02-04 13:19:42 +00005010# endif
5011 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005012# endif
5013# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005014 if (pty_slave_fd >= 0)
5015 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005016 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005017 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005018 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01005019# ifdef TIOCSCTTY
5020 // Try to become controlling tty (probably doesn't work,
5021 // unless run by root)
5022 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5023# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005024 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005025# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005026 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027
Bram Moolenaara5792f52005-11-23 21:25:05 +00005028 /*
5029 * stderr is only redirected when using the GUI, so that a
5030 * program like gpg can still access the terminal to get a
5031 * passphrase using stderr.
5032 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005033# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 if (pty_master_fd >= 0)
5035 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005036 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
Bram Moolenaar0f873732019-12-05 20:28:46 +01005038 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005040 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005042 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00005043 if (gui.in_use)
5044 {
5045 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005046 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00005047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
Bram Moolenaar0f873732019-12-05 20:28:46 +01005049 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
5051 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00005052# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005054 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 close(fd_toshell[1]);
5056 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005057 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 close(fd_toshell[0]);
5059
Bram Moolenaar0f873732019-12-05 20:28:46 +01005060 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 close(fd_fromshell[0]);
5062 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005063 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 close(fd_fromshell[1]);
5065
Bram Moolenaara5792f52005-11-23 21:25:05 +00005066# ifdef FEAT_GUI
5067 if (gui.in_use)
5068 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005069 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00005070 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005071 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00005072 }
5073# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
5075 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 /*
5078 * There is no type cast for the argv, because the type may be
5079 * different on different machines. This may cause a warning
5080 * message with strict compilers, don't worry about it.
5081 * Call _exit() instead of exit() to avoid closing the connection
5082 * to the X server (esp. with GTK, which uses atexit()).
5083 */
5084 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005085 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005087 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
5089 /*
5090 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005091 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 */
5093 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005094 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005095 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005096# ifdef FEAT_JOB_CHANNEL
5097 ++dont_check_job_ended;
5098# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 /*
5100 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005101 * This is also used to pipe stdin/stdout to/from the external
5102 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005104 if ((options & (SHELL_READ|SHELL_WRITE))
5105# ifdef FEAT_GUI
5106 || (gui.in_use && show_shell_mess)
5107# endif
5108 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005110# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01005112 int buffer_off = 0; // valid bytes in buffer[]
5113 char_u ta_buf[BUFLEN + 1]; // TypeAHead
5114 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 int len;
5116 int p_more_save;
5117 int old_State;
5118 int c;
5119 int toshell_fd;
5120 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005121 garray_T ga;
5122 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01005123# ifdef ELAPSED_FUNC
5124 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126
Bram Moolenaardf177f62005-02-22 08:39:57 +00005127# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (pty_master_fd >= 0)
5129 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 fromshell_fd = pty_master_fd;
5131 toshell_fd = dup(pty_master_fd);
5132 }
5133 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00005134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
5136 close(fd_toshell[0]);
5137 close(fd_fromshell[1]);
5138 toshell_fd = fd_toshell[1];
5139 fromshell_fd = fd_fromshell[0];
5140 }
5141
5142 /*
5143 * Write to the child if there are typed characters.
5144 * Read from the child if there are characters available.
5145 * Repeat the reading a few times if more characters are
5146 * available. Need to check for typed keys now and then, but
5147 * not too often (delays when no chars are available).
5148 * This loop is quit if no characters can be read from the pty
5149 * (WaitForChar detected special condition), or there are no
5150 * characters available and the child has exited.
5151 * Only check if the child has exited when there is no more
5152 * output. The child may exit before all the output has
5153 * been printed.
5154 *
5155 * Currently this busy loops!
5156 * This can probably dead-lock when the write blocks!
5157 */
5158 p_more_save = p_more;
5159 p_more = FALSE;
5160 old_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01005161 State = MODE_EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005163 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005164 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005165 // Fork a process that will write the lines to the
5166 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005167 if ((wpid = fork()) == -1)
5168 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005169 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005170 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005171 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00005172 {
5173 linenr_T lnum = curbuf->b_op_start.lnum;
5174 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005175 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005176 size_t l;
5177
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005178 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005179 for (;;)
5180 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005181 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005182 if (l == 0)
5183 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005184 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005185 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00005186 len = write(toshell_fd, "", (size_t)1);
5187 else
5188 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005189 char_u *s = vim_strchr(lp + written, NL);
5190
Bram Moolenaar89d40322006-08-29 15:30:07 +00005191 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00005192 s == NULL ? l
5193 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005194 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00005195 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005196 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005197 // Finished a line, add a NL, unless this line
5198 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005199 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005200 || (!curbuf->b_p_bin
5201 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005202 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaar88456cd2022-11-18 22:14:09 +00005203 && (lnum != curbuf->b_ml.ml_line_count
Bram Moolenaardf177f62005-02-22 08:39:57 +00005204 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005205 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005206 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005207 ++lnum;
5208 if (lnum > curbuf->b_op_end.lnum)
5209 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005210 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005211 close(toshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005212 break;
5213 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005214 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005215 written = 0;
5216 }
5217 else if (len > 0)
5218 written += len;
5219 }
5220 _exit(0);
5221 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005222 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005223 {
5224 close(toshell_fd);
5225 toshell_fd = -1;
5226 }
5227 }
5228
5229 if (options & SHELL_READ)
5230 ga_init2(&ga, 1, BUFLEN);
5231
5232 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005233# ifdef ELAPSED_FUNC
5234 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005235# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 for (;;)
5237 {
5238 /*
5239 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005240 * if there are any.
5241 * Don't do this if we are expanding wild cards (would eat
5242 * typeahead).
5243 * Don't do this when filtering and terminal is in cooked
5244 * mode, the shell command will handle the I/O. Avoids
5245 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005246 * Don't get characters when the child has already
5247 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005248 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005249 * while (noread_cnt > 4), avoids that ":r !ls" eats
5250 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 */
5252 len = 0;
5253 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005254 && ((options &
5255 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5256 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005257# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005258 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005259# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005260 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005261 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005262 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005264 if (ta_len == 0)
5265 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005266 // Get extra characters when we don't have any.
5267 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005268 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005269# ifdef ELAPSED_FUNC
5270 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005271# endif
5272 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5273 }
5274 if (ta_len > 0 || len > 0)
5275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 /*
5277 * For pipes:
5278 * Check for CTRL-C: send interrupt signal to child.
5279 * Check for CTRL-D: EOF, close pipe to child.
5280 */
5281 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 /*
5284 * Send SIGINT to the child's group or all
5285 * processes in our group.
5286 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005287 may_send_sigint(ta_buf[ta_len], pid, wpid);
5288
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 if (pty_master_fd < 0 && toshell_fd >= 0
5290 && ta_buf[ta_len] == Ctrl_D)
5291 {
5292 close(toshell_fd);
5293 toshell_fd = -1;
5294 }
5295 }
5296
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01005297 // Remove Vim-specific codes from the input.
5298 len = term_replace_keycodes(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
5300 /*
5301 * For pipes: echo the typed characters.
5302 * For a pty this does not seem to work.
5303 */
5304 if (pty_master_fd < 0)
5305 {
5306 for (i = ta_len; i < ta_len + len; ++i)
5307 {
5308 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5309 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 else if (has_mbyte)
5311 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005312 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313
5314 msg_outtrans_len(ta_buf + i, l);
5315 i += l - 1;
5316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 else
5318 msg_outtrans_len(ta_buf + i, 1);
5319 }
5320 windgoto(msg_row, msg_col);
5321 out_flush();
5322 }
5323
5324 ta_len += len;
5325
5326 /*
5327 * Write the characters to the child, unless EOF has
5328 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005329 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005330 * When writing buffer lines, drop the typed
5331 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005333 if (options & SHELL_WRITE)
5334 ta_len = 0;
5335 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
5337 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5338 if (len > 0)
5339 {
5340 ta_len -= len;
5341 mch_memmove(ta_buf, ta_buf + len, ta_len);
5342 }
5343 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
5346
Bram Moolenaardf177f62005-02-22 08:39:57 +00005347 if (got_int)
5348 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005349 // CTRL-C sends a signal to the child, we ignore it
5350 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005351# ifdef HAVE_SETSID
5352 kill(-pid, SIGINT);
5353# else
5354 kill(0, SIGINT);
5355# endif
5356 if (wpid > 0)
5357 kill(wpid, SIGINT);
5358 got_int = FALSE;
5359 }
5360
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 /*
5362 * Check if the child has any characters to be printed.
5363 * Read them and write them to our window. Repeat this as
5364 * long as there is something to do, avoid the 10ms wait
5365 * for mch_inchar(), or sending typeahead characters to
5366 * the external process.
5367 * TODO: This should handle escape sequences, compatible
5368 * to some terminal (vt52?).
5369 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005370 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005371 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005373 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005376 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005378
5379 noread_cnt = 0;
5380 if (options & SHELL_READ)
5381 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005382 // Do NUL -> NL translation, append NL separated
5383 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005384 for (i = 0; i < len; ++i)
5385 {
5386 if (buffer[i] == NL)
5387 append_ga_line(&ga);
5388 else if (buffer[i] == NUL)
5389 ga_append(&ga, NL);
5390 else
5391 ga_append(&ga, buffer[i]);
5392 }
5393 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005394 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
5396 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005397 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398
Bram Moolenaardf177f62005-02-22 08:39:57 +00005399 len += buffer_off;
5400 buffer[len] = NUL;
5401
Bram Moolenaar0f873732019-12-05 20:28:46 +01005402 // Check if the last character in buffer[] is
5403 // incomplete, keep these bytes for the next
5404 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 for (p = buffer; p < buffer + len; p += l)
5406 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005407 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005409 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 else if (MB_BYTE2LEN(*p) != l)
5411 break;
5412 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005413 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005415 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 if (len >= 12)
5417 ++p;
5418 else
5419 {
5420 buffer_off = len;
5421 continue;
5422 }
5423 }
5424 c = *p;
5425 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005426 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 if (p < buffer + len)
5428 {
5429 *p = c;
5430 buffer_off = (buffer + len) - p;
5431 mch_memmove(buffer, p, buffer_off);
5432 continue;
5433 }
5434 buffer_off = 0;
5435 }
5436 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 {
5438 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005439 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 }
5441
5442 windgoto(msg_row, msg_col);
5443 cursor_on();
5444 out_flush();
5445 if (got_int)
5446 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005447
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005448# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005449 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005450 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005451 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005452
Bram Moolenaar0f873732019-12-05 20:28:46 +01005453 // Avoid that we keep looping here without
5454 // checking for a CTRL-C for a long time. Don't
5455 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005456 if (msec > 2000)
5457 {
5458 noread_cnt = 5;
5459 break;
5460 }
5461 }
5462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 }
5464
Bram Moolenaar0f873732019-12-05 20:28:46 +01005465 // If we already detected the child has finished, continue
5466 // reading output for a short while. Some text may be
5467 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005468 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005469 {
5470 if (noread_cnt < 5)
5471 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005472 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005473 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005474
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 /*
5476 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005477 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005479# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005480 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005481# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005483# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5485 || (wait_pid == pid && WIFEXITED(status)))
5486 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005487 // Don't break the loop yet, try reading more
5488 // characters from "fromshell_fd" first. When using
5489 // pipes there might still be something to read and
5490 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005493 else
5494 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005495
Bram Moolenaar95a51352013-03-21 22:53:50 +01005496# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005497 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005498 clip_update();
5499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 }
5501finished:
5502 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005503 if (options & SHELL_READ)
5504 {
5505 if (ga.ga_len > 0)
5506 {
5507 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005508 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005509 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005510 }
5511 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005512 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005513 ga_clear(&ga);
5514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 /*
5517 * Give all typeahead that wasn't used back to ui_inchar().
5518 */
5519 if (ta_len)
5520 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 State = old_State;
5522 if (toshell_fd >= 0)
5523 close(toshell_fd);
5524 close(fromshell_fd);
5525 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005526# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005527 else
5528 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005529 long delay_msec = 1;
5530
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005531 if (tmode == TMODE_RAW)
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005532 // Possibly disables modifyOtherKeys, so that the system
5533 // can recognize CTRL-C.
5534 out_str_t_TE();
Bram Moolenaar0981c872020-08-23 14:28:37 +02005535
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005536 /*
5537 * Similar to the loop above, but only handle X events, no
5538 * I/O.
5539 */
5540 for (;;)
5541 {
5542 if (got_int)
5543 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005544 // CTRL-C sends a signal to the child, we ignore it
5545 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005546# ifdef HAVE_SETSID
5547 kill(-pid, SIGINT);
5548# else
5549 kill(0, SIGINT);
5550# endif
5551 got_int = FALSE;
5552 }
5553# ifdef __NeXT__
5554 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5555# else
5556 wait_pid = waitpid(pid, &status, WNOHANG);
5557# endif
5558 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5559 || (wait_pid == pid && WIFEXITED(status)))
5560 {
5561 wait_pid = pid;
5562 break;
5563 }
5564
Bram Moolenaar0f873732019-12-05 20:28:46 +01005565 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005566 clip_update();
5567
Bram Moolenaar0f873732019-12-05 20:28:46 +01005568 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005569 // less time, gradually wait longer.
5570 mch_delay(delay_msec,
5571 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005572 if (++delay_msec > 10)
5573 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005574 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005575
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005576 if (tmode == TMODE_RAW)
5577 // possibly enables modifyOtherKeys again
Bram Moolenaar733a69b2022-12-01 12:03:47 +00005578 out_str_t_TI();
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005579 }
5580# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581
5582 /*
5583 * Wait until our child has exited.
5584 * Ignore wait() returning pids of other children and returning
5585 * because of some signal like SIGWINCH.
5586 * Don't wait if wait_pid was already set above, indicating the
5587 * child already exited.
5588 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005589 if (wait_pid != pid)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01005590 (void)wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591
Bram Moolenaar624891f2010-10-13 16:22:09 +02005592# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005593 // Close slave side of pty. Only do this after the child has
5594 // exited, otherwise the child may hang when it tries to write on
5595 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005596 if (pty_master_fd >= 0)
5597 close(pty_slave_fd);
5598# endif
5599
Bram Moolenaar0f873732019-12-05 20:28:46 +01005600 // Make sure the child that writes to the external program is
5601 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005602 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005603 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005604 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005605 wait4pid(wpid, NULL);
5606 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005607
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005608# ifdef FEAT_JOB_CHANNEL
5609 --dont_check_job_ended;
5610# endif
5611
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 /*
5613 * Set to raw mode right now, otherwise a CTRL-C after
5614 * catch_signals() will kill Vim.
5615 */
5616 if (tmode == TMODE_RAW)
5617 settmode(TMODE_RAW);
5618 did_settmode = TRUE;
5619 set_signals();
5620
5621 if (WIFEXITED(status))
5622 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005623 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005625 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 {
5627 if (retval == EXEC_FAILED)
5628 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005629 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 msg_outtrans(p_sh);
5631 msg_putchar('\n');
5632 }
5633 else if (!(options & SHELL_SILENT))
5634 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005635 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 msg_outnum((long)retval);
5637 msg_putchar('\n');
5638 }
5639 }
5640 }
5641 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005642 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 }
5644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645
5646error:
5647 if (!did_settmode)
5648 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005649 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005651 vim_free(argv);
5652 vim_free(tofree1);
5653 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
5655 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005656}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005657#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005658
5659 int
5660mch_call_shell(
5661 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005662 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005663{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005664#ifdef FEAT_EVAL
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01005665 ch_log(NULL, "executing shell command: %s", cmd);
5666#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005667#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar524c8532022-09-27 15:48:20 +01005668 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL
5669 && (options & SHELL_SILENT) == 0)
Bram Moolenaar13568252018-03-16 20:46:58 +01005670 return mch_call_shell_terminal(cmd, options);
5671#endif
5672#ifdef USE_SYSTEM
5673 return mch_call_shell_system(cmd, options);
5674#else
5675 return mch_call_shell_fork(cmd, options);
5676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677}
5678
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005679#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005680 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005681mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005682{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005683 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005684 int fd_in[2] = {-1, -1}; // for stdin
5685 int fd_out[2] = {-1, -1}; // for stdout
5686 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005687 int pty_master_fd = -1;
5688 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005689 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005690 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5691 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5692 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005693 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005694 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5695 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005696 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005697 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005698 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005699
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005700 if (use_out_for_err && use_null_for_out)
5701 use_null_for_err = TRUE;
5702
Bram Moolenaar0f873732019-12-05 20:28:46 +01005703 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005704 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005705
Bram Moolenaarb2412082017-08-20 18:09:14 +02005706 if (options->jo_pty
5707 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005708 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005709 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005710 open_pty(&pty_master_fd, &pty_slave_fd,
5711 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005712
Bram Moolenaar0f873732019-12-05 20:28:46 +01005713 // TODO: without the channel feature connect the child to /dev/null?
5714 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005715 if (use_file_for_in)
5716 {
5717 char_u *fname = options->jo_io_name[PART_IN];
5718
5719 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5720 if (fd_in[0] < 0)
5721 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005722 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005723 goto failed;
5724 }
5725 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005726 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005727 // When writing buffer lines to the input don't use the pty, so that
5728 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005729 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5730 && pipe(fd_in) < 0)
5731 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005732
5733 if (use_file_for_out)
5734 {
5735 char_u *fname = options->jo_io_name[PART_OUT];
5736
5737 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5738 if (fd_out[1] < 0)
5739 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005740 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005741 goto failed;
5742 }
5743 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005744 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005745 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005746
5747 if (use_file_for_err)
5748 {
5749 char_u *fname = options->jo_io_name[PART_ERR];
5750
5751 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5752 if (fd_err[1] < 0)
5753 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005754 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005755 goto failed;
5756 }
5757 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005758 else if (!use_out_for_err && !use_null_for_err
5759 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005760 goto failed;
5761
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005762 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5763 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005764 if (options->jo_set & JO_CHANNEL)
5765 {
5766 channel = options->jo_channel;
5767 if (channel != NULL)
5768 ++channel->ch_refcount;
5769 }
5770 else
5771 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005772 if (channel == NULL)
5773 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005774 if (job->jv_tty_out != NULL)
5775 ch_log(channel, "using pty %s on fd %d",
5776 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005777 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005778
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005779 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005780 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005781 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005782 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005783 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005784 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005785 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005786 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005787 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005788 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005789 int null_fd = -1;
5790 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005791
Bram Moolenaar0f873732019-12-05 20:28:46 +01005792 // child
5793 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005794 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005795
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005796# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01005797 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005798 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005799 ch_logfile((char_u *)"", (char_u *)"");
5800# endif
5801
Bram Moolenaar835dc632016-02-07 14:27:38 +01005802# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005803 // Create our own process group, so that the child and all its
5804 // children can be kill()ed. Don't do this when using pipes,
5805 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005806 (void)setsid();
5807# endif
5808
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005809# ifdef FEAT_TERMINAL
5810 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005811 {
5812 char *term = (char *)T_NAME;
5813
5814#ifdef FEAT_GUI
5815 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005816 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005817 term = getenv("TERM");
5818#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005819 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005820 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005821 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005822 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005823 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005824 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005825 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005826 else if (t_colors > 16)
5827 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005828 else
5829 term = "xterm";
5830 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005831 set_child_environment(
5832 (long)options->jo_term_rows,
5833 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005834 term,
5835 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005836 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005837 else
5838# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005839 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005840
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005841 if (options->jo_env != NULL)
5842 {
5843 dict_T *dict = options->jo_env;
5844 hashitem_T *hi;
5845 int todo = (int)dict->dv_hashtab.ht_used;
5846
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005847 FOR_ALL_HASHTAB_ITEMS(&dict->dv_hashtab, hi, todo)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005848 if (!HASHITEM_EMPTY(hi))
5849 {
5850 typval_T *item = &dict_lookup(hi)->di_tv;
5851
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005852 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005853 --todo;
5854 }
5855 }
5856
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005857 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005858 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005859 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005860 if (null_fd < 0)
5861 {
5862 perror("opening /dev/null failed");
5863 _exit(OPEN_NULL_FAILED);
5864 }
5865 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005866
Bram Moolenaar223896d2017-08-02 22:33:28 +02005867 if (pty_slave_fd >= 0)
5868 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005869 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005870 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005871# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005872 // Try to become controlling tty (probably doesn't work,
5873 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005874 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5875# endif
5876 }
5877
Bram Moolenaar0f873732019-12-05 20:28:46 +01005878 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005879 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005880 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005881 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005882 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005883 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005884 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005885 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005886
Bram Moolenaar0f873732019-12-05 20:28:46 +01005887 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005888 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005889 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005890 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005891 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005892 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005893 }
5894 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005895 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005896 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005897 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005898 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005899 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005900
Bram Moolenaar0f873732019-12-05 20:28:46 +01005901 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005902 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005903 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005904 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005905 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005906 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005907 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005908 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005909
5910 if (fd_in[0] >= 0)
5911 close(fd_in[0]);
5912 if (fd_in[1] >= 0)
5913 close(fd_in[1]);
5914 if (fd_out[0] >= 0)
5915 close(fd_out[0]);
5916 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005917 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005918 if (fd_err[0] >= 0)
5919 close(fd_err[0]);
5920 if (fd_err[1] >= 0)
5921 close(fd_err[1]);
5922 if (pty_master_fd >= 0)
5923 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005924 close(pty_master_fd); // not used in the child
5925 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005926 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005927
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005928 if (null_fd >= 0)
5929 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005930
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005931 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5932 _exit(EXEC_FAILED);
5933
Bram Moolenaar0f873732019-12-05 20:28:46 +01005934 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005935 execvp(argv[0], argv);
5936
Bram Moolenaar4694a172016-04-21 14:05:23 +02005937 if (stderr_works)
5938 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005939# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005940 // calling free_all_mem() here causes problems. Ignore valgrind
5941 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005942# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005943 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005944 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005945
Bram Moolenaar0f873732019-12-05 20:28:46 +01005946 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005947 UNBLOCK_SIGNALS(&curset);
5948
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005949 job->jv_pid = pid;
5950 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005951 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005952
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005953 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005954 close(pty_slave_fd); // not used in the parent
5955 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005956 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005957 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005958 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005959 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005960 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005961 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005962 if (channel != NULL)
5963 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005964 int in_fd = INVALID_FD;
5965 int out_fd = INVALID_FD;
5966 int err_fd = INVALID_FD;
5967
5968 if (!(use_file_for_in || use_null_for_in))
5969 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5970
5971 if (!(use_file_for_out || use_null_for_out))
5972 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5973
5974 // When using pty_master_fd only set it for stdout, do not duplicate
5975 // it for stderr, it only needs to be read once.
5976 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5977 {
5978 if (fd_err[0] >= 0)
5979 err_fd = fd_err[0];
5980 else if (out_fd != pty_master_fd)
5981 err_fd = pty_master_fd;
5982 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005983
5984 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005985 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005986 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005987 else
5988 {
5989 if (fd_in[1] >= 0)
5990 close(fd_in[1]);
5991 if (fd_out[0] >= 0)
5992 close(fd_out[0]);
5993 if (fd_err[0] >= 0)
5994 close(fd_err[0]);
5995 if (pty_master_fd >= 0)
5996 close(pty_master_fd);
5997 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005998
Bram Moolenaar0f873732019-12-05 20:28:46 +01005999 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006000 return;
6001
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006002failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01006003 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006004 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006005 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01006006 if (fd_in[1] >= 0)
6007 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006008 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006009 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01006010 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006011 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006012 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006013 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01006014 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006015 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02006016 if (pty_master_fd >= 0)
6017 close(pty_master_fd);
6018 if (pty_slave_fd >= 0)
6019 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01006020}
6021
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006022 static char_u *
6023get_signal_name(int sig)
6024{
6025 int i;
6026 char_u numbuf[NUMBUFLEN];
6027
6028 if (sig == SIGKILL)
6029 return vim_strsave((char_u *)"kill");
6030
6031 for (i = 0; signal_info[i].sig != -1; i++)
6032 if (sig == signal_info[i].sig)
6033 return strlow_save((char_u *)signal_info[i].name);
6034
6035 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
6036 return vim_strsave(numbuf);
6037}
6038
Bram Moolenaar835dc632016-02-07 14:27:38 +01006039 char *
6040mch_job_status(job_T *job)
6041{
6042# ifdef HAVE_UNION_WAIT
6043 union wait status;
6044# else
6045 int status = -1;
6046# endif
6047 pid_t wait_pid = 0;
6048
6049# ifdef __NeXT__
6050 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
6051# else
6052 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
6053# endif
6054 if (wait_pid == -1)
6055 {
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00006056 int waitpid_errno = errno;
6057 if (waitpid_errno == ECHILD && mch_process_running(job->jv_pid))
6058 // The process is alive, but it was probably reparented (for
6059 // example by ptrace called by a debugger like lldb or gdb).
6060 // Note: This assumes that process IDs are not reused.
6061 return "run";
6062
Bram Moolenaar0f873732019-12-05 20:28:46 +01006063 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006064 if (job->jv_status < JOB_ENDED)
6065 ch_log(job->jv_channel, "Job no longer exists: %s",
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00006066 strerror(waitpid_errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02006067 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006068 }
6069 if (wait_pid == 0)
6070 return "run";
6071 if (WIFEXITED(status))
6072 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006073 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01006074 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006075 if (job->jv_status < JOB_ENDED)
6076 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02006077 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006078 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01006079 if (WIFSIGNALED(status))
6080 {
6081 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006082 job->jv_termsig = get_signal_name(WTERMSIG(status));
6083 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
6084 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
6085 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02006086 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01006087 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01006088 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02006089
6090return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01006091 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02006092 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02006093 return "dead";
6094}
6095
6096 job_T *
6097mch_detect_ended_job(job_T *job_list)
6098{
6099# ifdef HAVE_UNION_WAIT
6100 union wait status;
6101# else
6102 int status = -1;
6103# endif
6104 pid_t wait_pid = 0;
6105 job_T *job;
6106
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01006107# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006108 // Do not do this when waiting for a shell command to finish, we would get
6109 // the exit value here (and discard it), the exit value obtained there
6110 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01006111 if (dont_check_job_ended > 0)
6112 return NULL;
6113# endif
6114
Bram Moolenaar97792de2016-10-15 18:36:49 +02006115# ifdef __NeXT__
6116 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
6117# else
6118 wait_pid = waitpid(-1, &status, WNOHANG);
6119# endif
6120 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006121 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02006122 return NULL;
6123 for (job = job_list; job != NULL; job = job->jv_next)
6124 {
6125 if (job->jv_pid == wait_pid)
6126 {
6127 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006128 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02006129 job->jv_exitval = WEXITSTATUS(status);
6130 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006131 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02006132 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006133 job->jv_termsig = get_signal_name(WTERMSIG(status));
6134 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01006135 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02006136 {
6137 ch_log(job->jv_channel, "Job ended");
6138 job->jv_status = JOB_ENDED;
6139 }
6140 return job;
6141 }
6142 }
6143 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006144}
6145
Bram Moolenaar3a117e12016-10-30 21:57:52 +01006146/*
6147 * Send a (deadly) signal to "job".
6148 * Return FAIL if "how" is not a valid name.
6149 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01006150 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006151mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01006152{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006153 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006154
Bram Moolenaar923d9262016-02-25 20:56:01 +01006155 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01006156 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01006157 else if (STRCMP(how, "hup") == 0)
6158 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006159 else if (STRCMP(how, "quit") == 0)
6160 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01006161 else if (STRCMP(how, "int") == 0)
6162 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006163 else if (STRCMP(how, "kill") == 0)
6164 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02006165#ifdef SIGWINCH
6166 else if (STRCMP(how, "winch") == 0)
6167 sig = SIGWINCH;
6168#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006169 else if (isdigit(*how))
6170 sig = atoi((char *)how);
6171 else
6172 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01006173
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01006174 // Never kill ourselves!
6175 if (job->jv_pid != 0)
6176 {
6177 // TODO: have an option to only kill the process, not the group?
6178 kill(-job->jv_pid, sig);
6179 kill(job->jv_pid, sig);
6180 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01006181
Bram Moolenaar835dc632016-02-07 14:27:38 +01006182 return OK;
6183}
Bram Moolenaar76467df2016-02-12 19:30:26 +01006184
6185/*
6186 * Clear the data related to "job".
6187 */
6188 void
6189mch_clear_job(job_T *job)
6190{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006191 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01006192# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006193 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006194# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006195 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006196# endif
6197}
Bram Moolenaar835dc632016-02-07 14:27:38 +01006198#endif
6199
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006200#if defined(FEAT_TERMINAL) || defined(PROTO)
6201 int
6202mch_create_pty_channel(job_T *job, jobopt_T *options)
6203{
6204 int pty_master_fd = -1;
6205 int pty_slave_fd = -1;
6206 channel_T *channel;
6207
Bram Moolenaar59386482019-02-10 22:43:46 +01006208 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02006209 if (pty_master_fd < 0 || pty_slave_fd < 0)
6210 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006211 close(pty_slave_fd);
6212
6213 channel = add_channel();
6214 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006215 {
6216 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006217 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006218 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006219 if (job->jv_tty_out != NULL)
6220 ch_log(channel, "using pty %s on fd %d",
6221 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006222 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006223 channel->ch_keep_open = TRUE;
6224
Bram Moolenaar0f873732019-12-05 20:28:46 +01006225 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6226 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006227 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006228 channel_set_job(channel, job, options);
6229 return OK;
6230}
6231#endif
6232
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233/*
6234 * Check for CTRL-C typed by reading all available characters.
6235 * In cooked mode we should get SIGINT, no need to check.
6236 */
6237 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006238mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006240 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006241 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 fill_input_buf(FALSE);
6243}
6244
6245/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006246 * Wait "msec" msec until a character is available from the mouse, keyboard,
6247 * from inbuf[].
6248 * "msec" == -1 will block forever.
6249 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006250 * When "ignore_input" is TRUE even check for pending input when input is
6251 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006252 * "interrupted" (if not NULL) is set to TRUE when no character is available
6253 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006254 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006255 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 */
6257 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006258WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006260#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006261 return ui_wait_for_chars_or_timer(
6262 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006263#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006264 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006265#endif
6266}
6267
6268/*
6269 * Wait "msec" msec until a character is available from the mouse or keyboard
6270 * or from inbuf[].
6271 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006272 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006273 * "interrupted" (if not NULL) is set to TRUE when no character is available
6274 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006275 * When a GUI is being used, this will never get called -- webb
6276 */
6277 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006278WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006279{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280#ifdef FEAT_MOUSE_GPM
6281 int gpm_process_wanted;
6282#endif
6283#ifdef FEAT_XCLIPBOARD
6284 int rest;
6285#endif
6286 int avail;
6287
Bram Moolenaar0f873732019-12-05 20:28:46 +01006288 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 return 1;
6290
6291#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006292 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 if (WantQueryMouse)
6294 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006295 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006296 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006297 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 }
6299#endif
6300
6301 /*
6302 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6303 * events. This is a bit complicated, because they might both be defined.
6304 */
6305#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6306# ifdef FEAT_XCLIPBOARD
6307 rest = 0;
6308 if (do_xterm_trace())
6309 rest = msec;
6310# endif
6311 do
6312 {
6313# ifdef FEAT_XCLIPBOARD
6314 if (rest != 0)
6315 {
6316 msec = XT_TRACE_DELAY;
6317 if (rest >= 0 && rest < XT_TRACE_DELAY)
6318 msec = rest;
6319 if (rest >= 0)
6320 rest -= msec;
6321 }
6322# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01006323# ifdef FEAT_SOUND_MACOSX
6324 // Invoke any pending sound callbacks.
6325 process_cfrunloop();
6326# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006327# ifdef FEAT_SOUND_CANBERRA
6328 // Invoke any pending sound callbacks.
6329 if (has_sound_callback_in_queue())
6330 invoke_sound_callback();
6331# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332# ifdef FEAT_MOUSE_GPM
6333 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006334 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006335 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006336 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006338 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006342 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 return 1;
6344# ifdef FEAT_XCLIPBOARD
6345 if (rest == 0 || !do_xterm_trace())
6346# endif
6347 break;
6348 }
6349 }
6350 while (FALSE
6351# ifdef FEAT_MOUSE_GPM
6352 || (gpm_process_wanted && mch_gpm_process() == 0)
6353# endif
6354# ifdef FEAT_XCLIPBOARD
6355 || (!avail && rest != 0)
6356# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006357 )
6358 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359
6360#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006361 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362#endif
6363 return avail;
6364}
6365
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006366#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367/*
6368 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006369 * "msec" == 0 will check for characters once.
6370 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006373 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006374 * "interrupted" (if not NULL) is set to TRUE when no character is available
6375 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006377 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006378RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379{
6380 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006381 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006382#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 static int busy = FALSE;
6384
Bram Moolenaar0f873732019-12-05 20:28:46 +01006385 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386# define MAY_LOOP
6387
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006388# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006389 // Remember at what time we started, so that we know how much longer we
6390 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006391 long start_msec = msec;
6392 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006394 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006395 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396# endif
6397
Bram Moolenaar0f873732019-12-05 20:28:46 +01006398 // Handle being called recursively. This may happen for the session
6399 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 if (busy)
6401 return 0;
6402#endif
6403
6404#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006405 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406#endif
6407 {
6408#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006409 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006410# ifdef FEAT_MZSCHEME
6411 int mzquantum_used = FALSE;
6412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413#endif
6414#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006415 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006416 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 int nfd;
6418# ifdef FEAT_XCLIPBOARD
6419 int xterm_idx = -1;
6420# endif
6421# ifdef FEAT_MOUSE_GPM
6422 int gpm_idx = -1;
6423# endif
6424# ifdef USE_XSMP
6425 int xsmp_idx = -1;
6426# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006427 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006429# ifdef FEAT_MZSCHEME
6430 mzvim_check_threads();
6431 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6432 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006433 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006434 mzquantum_used = TRUE;
6435 }
6436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 fds[0].fd = fd;
6438 fds[0].events = POLLIN;
6439 nfd = 1;
6440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006442 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 if (xterm_Shell != (Widget)0)
6444 {
6445 xterm_idx = nfd;
6446 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6447 fds[nfd].events = POLLIN;
6448 nfd++;
6449 }
6450# endif
6451# ifdef FEAT_MOUSE_GPM
6452 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6453 {
6454 gpm_idx = nfd;
6455 fds[nfd].fd = gpm_fd;
6456 fds[nfd].events = POLLIN;
6457 nfd++;
6458 }
6459# endif
6460# ifdef USE_XSMP
6461 if (xsmp_icefd != -1)
6462 {
6463 xsmp_idx = nfd;
6464 fds[nfd].fd = xsmp_icefd;
6465 fds[nfd].events = POLLIN;
6466 nfd++;
6467 }
6468# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006469#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006470 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006471#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006472 if (interrupted != NULL)
6473 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006475 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006476
6477 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006478 if (result == 0 && interrupted != NULL && ret > 0)
6479 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006480
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006481# ifdef FEAT_MZSCHEME
6482 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006483 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006484 finished = FALSE;
6485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487# ifdef FEAT_XCLIPBOARD
6488 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6489 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006490 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006492 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 finished = FALSE;
6494 }
6495# endif
6496# ifdef FEAT_MOUSE_GPM
6497 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499# endif
6500# ifdef USE_XSMP
6501 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6502 {
6503 if (fds[xsmp_idx].revents & POLLIN)
6504 {
6505 busy = TRUE;
6506 xsmp_handle_requests();
6507 busy = FALSE;
6508 }
6509 else if (fds[xsmp_idx].revents & POLLHUP)
6510 {
6511 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006512 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 xsmp_close();
6514 }
6515 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006516 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 }
6518# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006519#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006520 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006521 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006522 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524
Bram Moolenaar0f873732019-12-05 20:28:46 +01006525#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526
6527 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006528 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006529 // These are static because they can take 8 Kbyte each and cause the
6530 // signal stack to run out with -O3.
6531 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006533 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006535# ifdef FEAT_MZSCHEME
6536 mzvim_check_threads();
6537 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6538 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006539 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006540 mzquantum_used = TRUE;
6541 }
6542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006544 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006546 tv.tv_sec = towait / 1000;
6547 tv.tv_usec = (towait % 1000) * (1000000/1000);
6548 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006550 else
6551 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552
6553 /*
6554 * Select on ready for reading and exceptional condition (end of file).
6555 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006556select_eintr:
6557 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006558 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 FD_ZERO(&efds);
6560 FD_SET(fd, &rfds);
K.Takatab247e062022-02-07 10:45:23 +00006561# ifndef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006562 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 FD_SET(fd, &efds);
6564# endif
6565 maxfd = fd;
6566
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006568 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 if (xterm_Shell != (Widget)0)
6570 {
6571 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6572 if (maxfd < ConnectionNumber(xterm_dpy))
6573 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006574
Bram Moolenaar0f873732019-12-05 20:28:46 +01006575 // An event may have already been read but not handled. In
6576 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006577 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 }
6579# endif
6580# ifdef FEAT_MOUSE_GPM
6581 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6582 {
6583 FD_SET(gpm_fd, &rfds);
6584 FD_SET(gpm_fd, &efds);
6585 if (maxfd < gpm_fd)
6586 maxfd = gpm_fd;
6587 }
6588# endif
6589# ifdef USE_XSMP
6590 if (xsmp_icefd != -1)
6591 {
6592 FD_SET(xsmp_icefd, &rfds);
6593 FD_SET(xsmp_icefd, &efds);
6594 if (maxfd < xsmp_icefd)
6595 maxfd = xsmp_icefd;
6596 }
6597# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006598# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006599 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006600# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006601 if (interrupted != NULL)
6602 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603
Bram Moolenaar643b6142018-09-12 20:29:09 +02006604 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6605 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006606 result = ret > 0 && FD_ISSET(fd, &rfds);
6607 if (result)
6608 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006609 else if (interrupted != NULL && ret > 0)
6610 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006611
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006612# ifdef EINTR
6613 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006614 {
dbivolaruab16ad32021-12-29 19:41:47 +00006615 // Check whether the EINTR is caused by SIGTSTP
6616 if (got_tstp && !in_mch_suspend)
6617 {
6618 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006619
dbivolaruab16ad32021-12-29 19:41:47 +00006620 ea.forceit = TRUE;
6621 ex_stop(&ea);
6622 got_tstp = FALSE;
6623 }
6624
Bram Moolenaar0f873732019-12-05 20:28:46 +01006625 // Check whether window has been resized, EINTR may be caused by
6626 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006627 if (do_resize)
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006628 {
Bram Moolenaar545c8a52023-06-21 15:51:47 +01006629# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006630 ch_log(NULL, "calling handle_resize() in RealWaitForChar()");
Bram Moolenaar545c8a52023-06-21 15:51:47 +01006631# endif
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006632 handle_resize();
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006633 }
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006634
Bram Moolenaar0f873732019-12-05 20:28:46 +01006635 // Interrupted by a signal, need to try again. We ignore msec
6636 // here, because we do want to check even after a timeout if
6637 // characters are available. Needed for reading output of an
6638 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006639 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006640 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006641# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006642# ifdef __TANDEM
6643 if (ret == -1 && errno == ENOTSUP)
6644 {
6645 FD_ZERO(&rfds);
6646 FD_ZERO(&efds);
6647 ret = 0;
6648 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006649# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006650# ifdef FEAT_MZSCHEME
6651 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006652 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006653 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654# endif
6655
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656# ifdef FEAT_XCLIPBOARD
6657 if (ret > 0 && xterm_Shell != (Widget)0
6658 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6659 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006660 xterm_update(); // Maybe we should hand out clipboard
6661 // continue looping when we only got the X event and the input
6662 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 if (--ret == 0 && !input_available())
6664 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006665 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 finished = FALSE;
6667 }
6668 }
6669# endif
6670# ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00006671 if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 {
6673 if (FD_ISSET(gpm_fd, &efds))
6674 gpm_close();
6675 else if (FD_ISSET(gpm_fd, &rfds))
6676 *check_for_gpm = 1;
6677 }
6678# endif
6679# ifdef USE_XSMP
6680 if (ret > 0 && xsmp_icefd != -1)
6681 {
6682 if (FD_ISSET(xsmp_icefd, &efds))
6683 {
6684 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006685 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 xsmp_close();
6687 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006688 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 }
6690 else if (FD_ISSET(xsmp_icefd, &rfds))
6691 {
6692 busy = TRUE;
6693 xsmp_handle_requests();
6694 busy = FALSE;
6695 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006696 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 }
6698 }
6699# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006700#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006701 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006702 if (ret >= 0)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01006703 (void)channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
Bram Moolenaar0f873732019-12-05 20:28:46 +01006706#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707
6708#ifdef MAY_LOOP
6709 if (finished || msec == 0)
6710 break;
6711
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006712# ifdef FEAT_CLIENTSERVER
6713 if (server_waiting())
6714 break;
6715# endif
6716
Bram Moolenaar0f873732019-12-05 20:28:46 +01006717 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 if (msec > 0)
6719 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006720# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006721 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006722 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006724 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 msec = msec / 2;
6726# endif
6727 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006728 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 }
6730#endif
6731 }
6732
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006733 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734}
6735
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006737 * Expand a path into all matching files and/or directories. Handles "*",
6738 * "?", "[a-z]", "**", etc.
6739 * "path" has backslashes before chars that are not to be expanded.
6740 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 */
6742 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006743mch_expandpath(
6744 garray_T *gap,
6745 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006746 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747{
Bram Moolenaar02743632005-07-25 20:42:36 +00006748 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
6751/*
6752 * mch_expand_wildcards() - this code does wild-card pattern matching using
6753 * the shell
6754 *
6755 * return OK for success, FAIL for error (you may lose some memory) and put
6756 * an error message in *file.
6757 *
6758 * num_pat is number of input patterns
6759 * pat is array of pointers to input patterns
6760 * num_file is pointer to number of matched file names
6761 * file is pointer to array of pointers to matched file names
6762 */
6763
6764#ifndef SEEK_SET
6765# define SEEK_SET 0
6766#endif
6767#ifndef SEEK_END
6768# define SEEK_END 2
6769#endif
6770
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006771#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006772
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006774mch_expand_wildcards(
6775 int num_pat,
6776 char_u **pat,
6777 int *num_file,
6778 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006779 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780{
6781 int i;
6782 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006783 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 char_u *p;
6785 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
Bram Moolenaarc7247912008-01-13 12:54:11 +00006787 /*
6788 * This is the non-OS/2 implementation (really Unix).
6789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 int j;
6791 char_u *tempname;
6792 char_u *command;
6793 FILE *fd;
6794 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006795#define STYLE_ECHO 0 // use "echo", the default
6796#define STYLE_GLOB 1 // use "glob", for csh
6797#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6798#define STYLE_PRINT 3 // use "print -N", for zsh
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006799#define STYLE_BT 4 // `cmd` expansion, execute the pattern directly
6800#define STYLE_GLOBSTAR 5 // use extended shell glob for bash (this uses extended
6801 // globbing functionality using globstar, needs bash > 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 int shell_style = STYLE_ECHO;
6803 int check_spaces;
6804 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006805 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006806 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006807 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006808 // vimglob() function with globstar setting enabled, only for bash >= 4.X
6809 static char *sh_globstar_opt = "[[ ${BASH_VERSINFO[0]} -ge 4 ]] && shopt -s globstar; ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810
Bram Moolenaar0f873732019-12-05 20:28:46 +01006811 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 *file = NULL;
6813
6814 /*
6815 * If there are no wildcards, just copy the names to allocated memory.
6816 * Saves a lot of time, because we don't have to start a new shell.
6817 */
6818 if (!have_wildcard(num_pat, pat))
6819 return save_patterns(num_pat, pat, num_file, file);
6820
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006821# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006822 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006823 if (sandbox != 0 && check_secure())
6824 return FAIL;
6825# endif
6826
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 /*
6828 * Don't allow the use of backticks in secure and restricted mode.
6829 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006830 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 for (i = 0; i < num_pat; ++i)
6832 if (vim_strchr(pat[i], '`') != NULL
6833 && (check_restricted() || check_secure()))
6834 return FAIL;
6835
6836 /*
6837 * get a name for the temp file
6838 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006839 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006841 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 return FAIL;
6843 }
6844
6845 /*
6846 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006847 * file.
6848 * STYLE_BT: NL separated
6849 * If expanding `cmd` execute it directly.
6850 * STYLE_GLOB: NUL separated
6851 * If we use *csh, "glob" will work better than "echo".
6852 * STYLE_PRINT: NL or NUL separated
6853 * If we use *zsh, "print -N" will work better than "glob".
6854 * STYLE_VIMGLOB: NL separated
6855 * If we use *sh*, we define "vimglob()".
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006856 * STYLE_GLOBSTAR: NL separated
6857 * If we use *bash*, we define "vimglob() and enable globstar option".
Bram Moolenaarc7247912008-01-13 12:54:11 +00006858 * STYLE_ECHO: space separated.
6859 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 */
6861 if (num_pat == 1 && *pat[0] == '`'
6862 && (len = STRLEN(pat[0])) > 2
6863 && *(pat[0] + len - 1) == '`')
6864 shell_style = STYLE_BT;
6865 else if ((len = STRLEN(p_sh)) >= 3)
6866 {
6867 if (STRCMP(p_sh + len - 3, "csh") == 0)
6868 shell_style = STYLE_GLOB;
6869 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6870 shell_style = STYLE_PRINT;
6871 }
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006872 if (shell_style == STYLE_ECHO)
6873 {
6874 if (strstr((char *)gettail(p_sh), "bash") != NULL)
6875 shell_style = STYLE_GLOBSTAR;
6876 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
6877 shell_style = STYLE_VIMGLOB;
6878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879
Bram Moolenaar0f873732019-12-05 20:28:46 +01006880 // Compute the length of the command. We need 2 extra bytes: for the
6881 // optional '&' and for the NUL.
6882 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006884 if (shell_style == STYLE_VIMGLOB)
6885 len += STRLEN(sh_vimglob_func);
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006886 else if (shell_style == STYLE_GLOBSTAR)
6887 len += STRLEN(sh_vimglob_func)
6888 + STRLEN(sh_globstar_opt);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006889
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006890 for (i = 0; i < num_pat; ++i)
6891 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006892 // Count the length of the patterns in the same way as they are put in
6893 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006894#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006895 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006896#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006897 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006898 for (j = 0; pat[i][j] != NUL; ++j)
6899 {
6900 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006901 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006902 ++len;
6903 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006904#endif
6905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 command = alloc(len);
6907 if (command == NULL)
6908 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006909 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 vim_free(tempname);
6911 return FAIL;
6912 }
6913
6914 /*
6915 * Build the shell command:
6916 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6917 * recognizes this).
6918 * - Add the shell command to print the expanded names.
6919 * - Add the temp file name.
6920 * - Add the file name patterns.
6921 */
6922 if (shell_style == STYLE_BT)
6923 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006924 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006925 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006926 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006928 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006929 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006931 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006933 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 *p = ' ';
6935 }
6936 STRCAT(command, ">");
6937 }
6938 else
6939 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006940 STRCPY(command, "");
6941 if (shell_style == STYLE_GLOB)
6942 {
6943 // Assume the nonomatch option is valid only for csh like shells,
6944 // otherwise, this may set the positional parameters for the shell,
6945 // e.g. "$*".
6946 if (flags & EW_NOTFOUND)
6947 STRCAT(command, "set nonomatch; ");
6948 else
6949 STRCAT(command, "unset nonomatch; ");
6950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 if (shell_style == STYLE_GLOB)
6952 STRCAT(command, "glob >");
6953 else if (shell_style == STYLE_PRINT)
6954 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006955 else if (shell_style == STYLE_VIMGLOB)
6956 STRCAT(command, sh_vimglob_func);
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006957 else if (shell_style == STYLE_GLOBSTAR)
6958 {
6959 STRCAT(command, sh_globstar_opt);
6960 STRCAT(command, sh_vimglob_func);
6961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 else
6963 STRCAT(command, "echo >");
6964 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006965
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 if (shell_style != STYLE_BT)
6969 for (i = 0; i < num_pat; ++i)
6970 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006971 // When using system() always add extra quotes, because the shell
6972 // is started twice. Otherwise put a backslash before special
6973 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974#ifdef USE_SYSTEM
6975 STRCAT(command, " \"");
6976 STRCAT(command, pat[i]);
6977 STRCAT(command, "\"");
6978#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006979 int intick = FALSE;
6980
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 p = command + STRLEN(command);
6982 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006983 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006984 {
6985 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006986 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006987 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6988 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006989 // Remove a backslash, take char literally. But keep
6990 // backslash inside backticks, before a special character
6991 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006992 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006993 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6994 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006995 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006996 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006997 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006998 else if (!intick
6999 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
7000 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007001 // Put a backslash before a special character, but not
7002 // when inside ``. And not for $var when EW_KEEPDOLLAR is
7003 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00007004 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00007005
Bram Moolenaar0f873732019-12-05 20:28:46 +01007006 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00007007 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00007008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 *p = NUL;
7010#endif
7011 }
7012 if (flags & EW_SILENT)
7013 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01007014 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007015 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016
7017 /*
7018 * Using zsh -G: If a pattern has no matches, it is just deleted from
7019 * the argument list, otherwise zsh gives an error message and doesn't
7020 * expand any other pattern.
7021 */
7022 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007023 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024
7025 /*
7026 * If we use -f then shell variables set in .cshrc won't get expanded.
7027 * vi can do it, so we will too, but it is only necessary if there is a "$"
7028 * in one of the patterns, otherwise we can still use the fast option.
7029 */
7030 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007031 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032
7033 /*
7034 * execute the shell command
7035 */
7036 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
7037
Bram Moolenaar0f873732019-12-05 20:28:46 +01007038 // When running in the background, give it some time to create the temp
7039 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01007040 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02007041 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042
Bram Moolenaar0f873732019-12-05 20:28:46 +01007043 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 show_shell_mess = TRUE;
7045 vim_free(command);
7046
Bram Moolenaar0f873732019-12-05 20:28:46 +01007047 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 {
7049 mch_remove(tempname);
7050 vim_free(tempname);
7051 /*
7052 * With interactive completion, the error message is not printed.
7053 * However with USE_SYSTEM, I don't know how to turn off error messages
7054 * from the shell, so screen may still get messed up -- webb.
7055 */
7056#ifndef USE_SYSTEM
7057 if (!(flags & EW_SILENT))
7058#endif
7059 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007060 redraw_later_clear(); // probably messed up screen
7061 msg_putchar('\n'); // clear bottom line quickly
7062 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063#ifdef USE_SYSTEM
7064 if (!(flags & EW_SILENT))
7065#endif
7066 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00007067 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01007068 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 }
7070 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007071 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
7072 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 if (shell_style == STYLE_BT)
7074 return FAIL;
7075 goto notfound;
7076 }
7077
7078 /*
7079 * read the names from the file into memory
7080 */
7081 fd = fopen((char *)tempname, READBIN);
7082 if (fd == NULL)
7083 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007084 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 if (!(flags & EW_SILENT))
7086 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00007087 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01007088 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 }
7090 vim_free(tempname);
7091 goto notfound;
7092 }
7093 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007094 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02007096 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007097 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02007098 buffer = NULL;
7099 else
7100 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 if (buffer == NULL)
7102 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007103 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 mch_remove(tempname);
7105 vim_free(tempname);
7106 fclose(fd);
7107 return FAIL;
7108 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02007109 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 i = fread((char *)buffer, 1, len, fd);
7111 fclose(fd);
7112 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00007113 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007115 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007116 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 vim_free(tempname);
7118 vim_free(buffer);
7119 return FAIL;
7120 }
7121 vim_free(tempname);
7122
Bram Moolenaar1eed5322019-02-26 17:03:54 +01007123# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01007124 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02007126 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
7128 *p++ = buffer[i];
7129 len = p - buffer;
7130# endif
7131
7132
Bram Moolenaar0f873732019-12-05 20:28:46 +01007133 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 if (shell_style == STYLE_ECHO)
7135 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007136 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007138 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {
7140 while (*p != ' ' && *p != '\n')
7141 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007142 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 }
7144 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007145 // file names are separated with NL
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02007146 else if (shell_style == STYLE_BT ||
7147 shell_style == STYLE_VIMGLOB ||
7148 shell_style == STYLE_GLOBSTAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007150 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007152 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 {
7154 while (*p != '\n' && *p != NUL)
7155 ++p;
7156 if (*p != NUL)
7157 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007158 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 }
7160 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007161 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 else
7163 {
7164 /*
7165 * Some versions of zsh use spaces instead of NULs to separate
7166 * results. Only do this when there is no NUL before the end of the
7167 * buffer, otherwise we would never be able to use file names with
7168 * embedded spaces when zsh does use NULs.
7169 * When we found a NUL once, we know zsh is OK, set did_find_nul and
7170 * don't check for spaces again.
7171 */
7172 check_spaces = FALSE;
7173 if (shell_style == STYLE_PRINT && !did_find_nul)
7174 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007175 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007176 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01007177 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 did_find_nul = TRUE;
7179 else
7180 check_spaces = TRUE;
7181 }
7182
7183 /*
7184 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
7185 * already is one, for STYLE_GLOB it needs to be added.
7186 */
7187 if (len && buffer[len - 1] == NUL)
7188 --len;
7189 else
7190 buffer[len] = NUL;
7191 i = 0;
7192 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007193 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {
7195 ++i;
7196 *p = NUL;
7197 }
7198 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007199 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 }
7201 if (i == 0)
7202 {
7203 /*
7204 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
7205 * /bin/sh will happily expand it to nothing rather than returning an
7206 * error; and hey, it's good to check anyway -- webb.
7207 */
7208 vim_free(buffer);
7209 goto notfound;
7210 }
7211 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007212 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 if (*file == NULL)
7214 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007215 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 vim_free(buffer);
7217 return FAIL;
7218 }
7219
7220 /*
7221 * Isolate the individual file names.
7222 */
7223 p = buffer;
7224 for (i = 0; i < *num_file; ++i)
7225 {
7226 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007227 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00007228 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02007229 || shell_style == STYLE_VIMGLOB || shell_style == STYLE_GLOBSTAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00007231 while (!(shell_style == STYLE_ECHO && *p == ' ')
7232 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007234 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 *p = NUL;
7236 else
7237 {
7238 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007239 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 }
7241 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007242 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007244 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007246 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 }
7248 }
7249
7250 /*
7251 * Move the file names to allocated memory.
7252 */
7253 for (j = 0, i = 0; i < *num_file; ++i)
7254 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007255 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7257 continue;
7258
Bram Moolenaar0f873732019-12-05 20:28:46 +01007259 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 dir = (mch_isdir((*file)[i]));
7261 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7262 continue;
7263
Bram Moolenaar0f873732019-12-05 20:28:46 +01007264 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007265 if (!dir && (flags & EW_EXEC)
7266 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007267 continue;
7268
Bram Moolenaar964b3742019-05-24 18:54:09 +02007269 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 if (p)
7271 {
7272 STRCPY(p, (*file)[i]);
7273 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007274 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 (*file)[j++] = p;
7276 }
7277 }
7278 vim_free(buffer);
7279 *num_file = j;
7280
Bram Moolenaar0f873732019-12-05 20:28:46 +01007281 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007283 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 goto notfound;
7285 }
7286
7287 return OK;
7288
7289notfound:
7290 if (flags & EW_NOTFOUND)
7291 return save_patterns(num_pat, pat, num_file, file);
7292 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293}
7294
Bram Moolenaar0f873732019-12-05 20:28:46 +01007295#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007298save_patterns(
7299 int num_pat,
7300 char_u **pat,
7301 int *num_file,
7302 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
7304 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007305 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007307 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 if (*file == NULL)
7309 return FAIL;
7310 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007311 {
7312 s = vim_strsave(pat[i]);
7313 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007314 // Be compatible with expand_filename(): halve the number of
7315 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007316 backslash_halve(s);
7317 (*file)[i] = s;
7318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 *num_file = num_pat;
7320 return OK;
7321}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323/*
7324 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7325 * expand.
7326 */
7327 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007328mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007330 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 if (*p == '\\' && p[1] != NUL)
7333 ++p;
7334 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 if (vim_strchr((char_u *)
7336#ifdef VMS
7337 "*?%"
7338#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340#endif
7341 , *p) != NULL)
7342 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 }
7344 return FALSE;
7345}
7346
7347/*
7348 * Return TRUE if the string "p" contains a wildcard.
7349 * Don't recognize '~' at the end as a wildcard.
7350 */
7351 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007352mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007354 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 if (*p == '\\' && p[1] != NUL)
7357 ++p;
7358 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 if (vim_strchr((char_u *)
7360#ifdef VMS
7361 "*?%$"
7362#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364#endif
7365 , *p) != NULL
7366 || (*p == '~' && p[1] != NUL))
7367 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 }
7369 return FALSE;
7370}
7371
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007373have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374{
7375 int i;
7376
7377 for (i = 0; i < num; i++)
7378 if (mch_has_wildcard(file[i]))
7379 return 1;
7380 return 0;
7381}
7382
7383 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007384have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385{
7386 int i;
7387
7388 for (i = 0; i < num; i++)
7389 if (vim_strchr(file[i], '$') != NULL)
7390 return TRUE;
7391 return FALSE;
7392}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007394#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395/*
7396 * Scaled-down version of rename(), which is missing in Xenix.
7397 * This version can only move regular files and will fail if the
7398 * destination exists.
7399 */
7400 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007401mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
7403 struct stat st;
7404
Bram Moolenaar0f873732019-12-05 20:28:46 +01007405 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007407 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007409 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 return 0;
7411 return -1;
7412}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007413#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007415#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007416# if defined(DYNAMIC_GPM) || defined(PROTO)
7417/*
7418 * Initialize Gpm's symbols for dynamic linking.
7419 * Must be called only if libgpm_hinst is NULL.
7420 */
7421 static int
7422load_libgpm(void)
7423{
7424 libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL);
7425
7426 if (libgpm_hinst == NULL)
7427 {
7428 if (p_verbose > 0)
7429 smsg_attr(HL_ATTR(HLF_W),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007430 _("Could not load gpm library: %s"), dlerror());
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007431 return FAIL;
7432 }
7433
7434 if (
7435 (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL
7436 || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL
7437 || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL
7438 || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL
7439 || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL
7440 )
7441 {
7442 semsg(_(e_could_not_load_library_str_str), "gpm", dlerror());
7443 dlclose(libgpm_hinst);
7444 libgpm_hinst = NULL;
7445 dll_gpm_flag = NULL;
7446 dll_gpm_fd = NULL;
7447 return FAIL;
7448 }
7449 return OK;
7450}
7451
7452 int
7453gpm_available(void)
7454{
7455 return libgpm_hinst != NULL || load_libgpm() == OK;
7456}
7457# endif // DYNAMIC_GPM
7458
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459/*
7460 * Initializes connection with gpm (if it isn't already opened)
7461 * Return 1 if succeeded (or connection already opened), 0 if failed
7462 */
7463 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007464gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007466 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007468#ifdef DYNAMIC_GPM
7469 if (!gpm_available())
7470 return 0;
7471#endif
7472
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007473 if (gpm_flag)
7474 return 1; // already open
7475
7476 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7477 gpm_connect.defaultMask = ~GPM_HARD;
7478 // Default handling for mouse move
7479 gpm_connect.minMod = 0; // Handle any modifier keys
7480 gpm_connect.maxMod = 0xffff;
7481 if (Gpm_Open(&gpm_connect, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007483 // gpm library tries to handling TSTP causes
7484 // problems. Anyways, we close connection to Gpm whenever
7485 // we are going to suspend or starting an external process
7486 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007487# ifdef SIGTSTP
ichizok378447f2023-05-11 22:25:42 +01007488 mch_signal(SIGTSTP, restricted ? SIG_IGN : sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007489# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007490 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007492 if (gpm_fd == -2)
7493 Gpm_Close(); // We don't want to talk to xterm via gpm
7494 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495}
7496
7497/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007498 * Returns TRUE if the GPM mouse is enabled.
7499 */
7500 int
7501gpm_enabled(void)
7502{
7503 return gpm_flag && gpm_fd >= 0;
7504}
7505
7506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 */
7509 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007510gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007512 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 Gpm_Close();
7514}
7515
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007516/*
7517 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007519 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 */
7521 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007522mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523{
7524 int button;
7525 static Gpm_Event gpm_event;
7526 char_u string[6];
7527 int_u vim_modifiers;
7528 int row,col;
7529 unsigned char buttons_mask;
7530 unsigned char gpm_modifiers;
7531 static unsigned char old_buttons = 0;
7532
7533 Gpm_GetEvent(&gpm_event);
7534
7535#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007536 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 if (hold_gui_events)
7538 return 0;
7539#endif
7540
7541 row = gpm_event.y - 1;
7542 col = gpm_event.x - 1;
7543
Bram Moolenaar0f873732019-12-05 20:28:46 +01007544 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 string[1] = 'M';
7546 string[2] = 'G';
7547 switch (GPM_BARE_EVENTS(gpm_event.type))
7548 {
7549 case GPM_DRAG:
7550 string[3] = MOUSE_DRAG;
7551 break;
7552 case GPM_DOWN:
7553 buttons_mask = gpm_event.buttons & ~old_buttons;
7554 old_buttons = gpm_event.buttons;
7555 switch (buttons_mask)
7556 {
7557 case GPM_B_LEFT:
7558 button = MOUSE_LEFT;
7559 break;
7560 case GPM_B_MIDDLE:
7561 button = MOUSE_MIDDLE;
7562 break;
7563 case GPM_B_RIGHT:
7564 button = MOUSE_RIGHT;
7565 break;
7566 default:
7567 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007568 // Don't know what to do. Can more than one button be
7569 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 }
7571 string[3] = (char_u)(button | 0x20);
7572 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7573 break;
7574 case GPM_UP:
7575 string[3] = MOUSE_RELEASE;
7576 old_buttons &= ~gpm_event.buttons;
7577 break;
7578 default:
7579 return 0;
7580 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007581 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 gpm_modifiers = gpm_event.modifiers;
7583 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007584 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7585 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7586 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7588 vim_modifiers |= MOUSE_SHIFT;
7589
7590 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7591 vim_modifiers |= MOUSE_CTRL;
7592 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7593 vim_modifiers |= MOUSE_ALT;
7594 string[3] |= vim_modifiers;
7595 string[4] = (char_u)(col + ' ' + 1);
7596 string[5] = (char_u)(row + ' ' + 1);
7597 add_to_input_buf(string, 6);
7598 return 6;
7599}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007600#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007602#ifdef FEAT_SYSMOUSE
7603/*
7604 * Initialize connection with sysmouse.
7605 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7606 * output, any sysmouse output than will be processed via sig_sysmouse().
7607 * Return OK if succeeded, FAIL if failed.
7608 */
7609 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007610sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007611{
7612 struct mouse_info mouse;
7613
7614 mouse.operation = MOUSE_MODE;
7615 mouse.u.mode.mode = 0;
7616 mouse.u.mode.signal = SIGUSR2;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007617 if (ioctl(1, CONS_MOUSECTL, &mouse) == -1)
7618 return FAIL;
7619
ichizok378447f2023-05-11 22:25:42 +01007620 mch_signal(SIGUSR2, sig_sysmouse);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007621 mouse.operation = MOUSE_SHOW;
7622 ioctl(1, CONS_MOUSECTL, &mouse);
7623 return OK;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007624}
7625
7626/*
7627 * Stop processing SIGUSR2 signals, and also make sure that
7628 * virtual console do not send us any sysmouse related signal.
7629 */
7630 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007631sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007632{
7633 struct mouse_info mouse;
7634
ichizok378447f2023-05-11 22:25:42 +01007635 mch_signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007636 mouse.operation = MOUSE_MODE;
7637 mouse.u.mode.mode = 0;
7638 mouse.u.mode.signal = 0;
7639 ioctl(1, CONS_MOUSECTL, &mouse);
7640}
7641
7642/*
7643 * Gets info from sysmouse and adds special keys to input buf.
7644 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007645 static void
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007646sig_sysmouse SIGDEFARG(sigarg)
7647{
7648 struct mouse_info mouse;
7649 struct video_info video;
7650 char_u string[6];
7651 int row, col;
7652 int button;
7653 int buttons;
7654 static int oldbuttons = 0;
7655
7656#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007657 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007658 if (hold_gui_events)
7659 return;
7660#endif
7661
7662 mouse.operation = MOUSE_GETINFO;
7663 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7664 && ioctl(1, FBIO_MODEINFO, &video) != -1
7665 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7666 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7667 {
7668 row = mouse.u.data.y / video.vi_cheight;
7669 col = mouse.u.data.x / video.vi_cwidth;
7670 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007671 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007672 string[1] = 'M';
7673 string[2] = 'S';
7674 if (oldbuttons == buttons && buttons != 0)
7675 {
7676 button = MOUSE_DRAG;
7677 }
7678 else
7679 {
7680 switch (buttons)
7681 {
7682 case 0:
7683 button = MOUSE_RELEASE;
7684 break;
7685 case 1:
7686 button = MOUSE_LEFT;
7687 break;
7688 case 2:
7689 button = MOUSE_MIDDLE;
7690 break;
7691 case 4:
7692 button = MOUSE_RIGHT;
7693 break;
7694 default:
7695 return;
7696 }
7697 oldbuttons = buttons;
7698 }
7699 string[3] = (char_u)(button);
7700 string[4] = (char_u)(col + ' ' + 1);
7701 string[5] = (char_u)(row + ' ' + 1);
7702 add_to_input_buf(string, 6);
7703 }
7704 return;
7705}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007706#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007707
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007709typedef char_u * (*STRPROCSTR)(char_u *);
7710typedef char_u * (*INTPROCSTR)(int);
7711typedef int (*STRPROCINT)(char_u *);
7712typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713
7714/*
7715 * Call a DLL routine which takes either a string or int param
7716 * and returns an allocated string.
7717 */
7718 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007719mch_libcall(
7720 char_u *libname,
7721 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007722 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007723 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007724 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007725 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726{
7727# if defined(USE_DLOPEN)
7728 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007729 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730# else
7731 shl_t hinstLib;
7732# endif
7733 STRPROCSTR ProcAdd;
7734 INTPROCSTR ProcAddI;
7735 char_u *retval_str = NULL;
7736 int retval_int = 0;
7737 int success = FALSE;
7738
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007739 /*
7740 * Get a handle to the DLL module.
7741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007743 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007744 (void)dlerror();
7745
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 hinstLib = dlopen((char *)libname, RTLD_LAZY
7747# ifdef RTLD_LOCAL
7748 | RTLD_LOCAL
7749# endif
7750 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007751 if (hinstLib == NULL)
7752 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007753 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007754 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007755 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007756 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758# else
7759 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7760# endif
7761
Bram Moolenaar0f873732019-12-05 20:28:46 +01007762 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 if (hinstLib != NULL)
7764 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007765# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 /*
7767 * Catch a crash when calling the library function. For example when
7768 * using a number where a string pointer is expected.
7769 */
7770 mch_startjmp();
7771 if (SETJMP(lc_jump_env) != 0)
7772 {
7773 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007774# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007775 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 mch_didjmp();
7778 }
7779 else
7780# endif
7781 {
7782 retval_str = NULL;
7783 retval_int = 0;
7784
7785 if (argstring != NULL)
7786 {
7787# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007788 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007789 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790# else
7791 if (shl_findsym(&hinstLib, (const char *)funcname,
7792 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7793 ProcAdd = NULL;
7794# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007795 if ((success = (ProcAdd != NULL
7796# if defined(USE_DLOPEN)
7797 && dlerr == NULL
7798# endif
7799 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 {
7801 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007802 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 else
7804 retval_str = (ProcAdd)(argstring);
7805 }
7806 }
7807 else
7808 {
7809# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007810 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007811 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812# else
7813 if (shl_findsym(&hinstLib, (const char *)funcname,
7814 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7815 ProcAddI = NULL;
7816# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007817 if ((success = (ProcAddI != NULL
7818# if defined(USE_DLOPEN)
7819 && dlerr == NULL
7820# endif
7821 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {
7823 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007824 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 else
7826 retval_str = (ProcAddI)(argint);
7827 }
7828 }
7829
Bram Moolenaar0f873732019-12-05 20:28:46 +01007830 // Save the string before we free the library.
7831 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 if (string_result == NULL)
7833 *number_result = retval_int;
7834 else if (retval_str != NULL
7835 && retval_str != (char_u *)1
7836 && retval_str != (char_u *)-1)
7837 *string_result = vim_strsave(retval_str);
7838 }
7839
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007840# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 mch_endjmp();
7842# ifdef SIGHASARG
7843 if (lc_signal != 0)
7844 {
7845 int i;
7846
Bram Moolenaar0f873732019-12-05 20:28:46 +01007847 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 for (i = 0; signal_info[i].sig != -1; i++)
7849 if (lc_signal == signal_info[i].sig)
7850 break;
Bram Moolenaar50809a42023-05-20 16:39:07 +01007851 semsg(_(e_got_sig_str_in_libcall), signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 }
7853# endif
7854# endif
7855
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007857 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007858 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007859 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007860
Bram Moolenaar0f873732019-12-05 20:28:46 +01007861 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 (void)dlclose(hinstLib);
7863# else
7864 (void)shl_unload(hinstLib);
7865# endif
7866 }
7867
7868 if (!success)
7869 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007870 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 return FAIL;
7872 }
7873
7874 return OK;
7875}
7876#endif
7877
7878#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007879static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880static int xterm_button;
7881
7882/*
7883 * Setup a dummy window for X selections in a terminal.
7884 */
7885 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007886setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
7888 int z = 0;
7889 char *strp = "";
7890 Widget AppShell;
7891
7892 if (!x_connect_to_server())
7893 return;
7894
7895 open_app_context();
7896 if (app_context != NULL && xterm_Shell == (Widget)0)
7897 {
Dominique Pellé4927bc72023-09-24 16:12:07 +02007898 int (*oldhandler)(Display*, XErrorEvent*);
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007899# if defined(USING_SETJMP)
Dominique Pellé4927bc72023-09-24 16:12:07 +02007900 int (*oldIOhandler)(Display*);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007901# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007902# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007903 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904
7905 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007906 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907# endif
7908
Bram Moolenaar0f873732019-12-05 20:28:46 +01007909 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 oldhandler = XSetErrorHandler(x_error_check);
7911
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007912# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007913 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7915 mch_startjmp();
7916 if (SETJMP(lc_jump_env) != 0)
7917 {
7918 mch_didjmp();
7919 xterm_dpy = NULL;
7920 }
7921 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 {
7924 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7925 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007926 if (xterm_dpy != NULL)
7927 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007928# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007930# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 }
7932
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007933# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007934 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007936# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007937 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 (void)XSetErrorHandler(oldhandler);
7939
7940 if (xterm_dpy == NULL)
7941 {
7942 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007943 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 return;
7945 }
7946
Bram Moolenaar0f873732019-12-05 20:28:46 +01007947 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 (void)XSetIOErrorHandler(x_IOerror_handler);
7949
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007950# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007952 {
7953 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007954 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007955 verbose_leave();
7956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957# endif
7958
Bram Moolenaar0f873732019-12-05 20:28:46 +01007959 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7961 applicationShellWidgetClass, xterm_dpy,
7962 NULL);
7963 if (AppShell == (Widget)0)
7964 return;
7965 xterm_Shell = XtVaCreatePopupShell("VIM",
7966 topLevelShellWidgetClass, AppShell,
7967 XtNmappedWhenManaged, 0,
7968 XtNwidth, 1,
7969 XtNheight, 1,
7970 NULL);
7971 if (xterm_Shell == (Widget)0)
7972 return;
7973
7974 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007975 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 if (x11_display == NULL)
7977 x11_display = xterm_dpy;
7978
7979 XtRealizeWidget(xterm_Shell);
7980 XSync(xterm_dpy, False);
7981 xterm_update();
7982 }
7983 if (xterm_Shell != (Widget)0)
7984 {
7985 clip_init(TRUE);
7986 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7987 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007988 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 if (test_x11_window(xterm_dpy) == FAIL)
7990 x11_window = 0;
7991 if (x11_window != 0)
7992 xterm_trace = 0;
7993 }
7994}
7995
7996 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007997start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998{
7999 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
8000 return;
8001 xterm_trace = 1;
8002 xterm_button = button;
8003 do_xterm_trace();
8004}
8005
8006
8007 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008008stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009{
8010 if (xterm_trace < 0)
8011 return;
8012 xterm_trace = 0;
8013}
8014
8015/*
8016 * Query the xterm pointer and generate mouse termcodes if necessary
8017 * return TRUE if dragging is active, else FALSE
8018 */
8019 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008020do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021{
8022 Window root, child;
8023 int root_x, root_y;
8024 int win_x, win_y;
8025 int row, col;
8026 int_u mask_return;
8027 char_u buf[50];
8028 char_u *strp;
8029 long got_hints;
8030 static char_u *mouse_code;
8031 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
8032 static int prev_row = 0, prev_col = 0;
8033 static XSizeHints xterm_hints;
8034
8035 if (xterm_trace <= 0)
8036 return FALSE;
8037
8038 if (xterm_trace == 1)
8039 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008040 // Get the hints just before tracking starts. The font size might
8041 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00008042 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
8043 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 || xterm_hints.width_inc <= 1
8045 || xterm_hints.height_inc <= 1)
8046 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008047 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 return FALSE;
8049 }
8050
Bram Moolenaar0f873732019-12-05 20:28:46 +01008051 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 mouse_code = find_termcode(mouse_name);
8053 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02008054 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 xterm_trace = 2;
8056
Bram Moolenaar0f873732019-12-05 20:28:46 +01008057 // Find the offset of the chars, there might be a scrollbar on the
8058 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
8060 &win_x, &win_y, &mask_return);
8061 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
8062 - (xterm_hints.height_inc / 2);
8063 if (xterm_hints.y <= xterm_hints.height_inc / 2)
8064 xterm_hints.y = 2;
8065 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
8066 - (xterm_hints.width_inc / 2);
8067 if (xterm_hints.x <= xterm_hints.width_inc / 2)
8068 xterm_hints.x = 2;
8069 return TRUE;
8070 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02008071 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {
8073 xterm_trace = 0;
8074 return FALSE;
8075 }
8076
8077 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
8078 &win_x, &win_y, &mask_return);
8079
8080 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
8081 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
8082 if (row == prev_row && col == prev_col)
8083 return TRUE;
8084
8085 STRCPY(buf, mouse_code);
8086 strp = buf + STRLEN(buf);
8087 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
8088 *strp++ = (char_u)(col + ' ' + 1);
8089 *strp++ = (char_u)(row + ' ' + 1);
8090 *strp = 0;
8091 add_to_input_buf(buf, STRLEN(buf));
8092
8093 prev_row = row;
8094 prev_col = col;
8095 return TRUE;
8096}
8097
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02008098# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099/*
8100 * Destroy the display, window and app_context. Required for GTK.
8101 */
8102 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008103clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104{
8105 if (xterm_Shell != (Widget)0)
8106 {
8107 XtDestroyWidget(xterm_Shell);
8108 xterm_Shell = (Widget)0;
8109 }
8110 if (xterm_dpy != NULL)
8111 {
Bram Moolenaare8208012008-06-20 09:59:25 +00008112# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008113 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00008115# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 if (x11_display == xterm_dpy)
8117 x11_display = NULL;
8118 xterm_dpy = NULL;
8119 }
Bram Moolenaare8208012008-06-20 09:59:25 +00008120# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 if (app_context != (XtAppContext)NULL)
8122 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008123 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 XtDestroyApplicationContext(app_context);
8125 app_context = (XtAppContext)NULL;
8126 }
Bram Moolenaare8208012008-06-20 09:59:25 +00008127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128}
8129# endif
8130
8131/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01008132 * Catch up with GUI or X events.
8133 */
8134 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008135clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01008136{
8137# ifdef FEAT_GUI
8138 if (gui.in_use)
8139 gui_mch_update();
8140 else
8141# endif
8142 if (xterm_Shell != (Widget)0)
8143 xterm_update();
8144}
8145
8146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 * Catch up with any queued X events. This may put keyboard input into the
8148 * input buffer, call resize call-backs, trigger timers etc. If there is
8149 * nothing in the X event queue (& no timers pending), then we return
8150 * immediately.
8151 */
8152 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008153xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154{
8155 XEvent event;
8156
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008157 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008159 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008161 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008162 break;
8163
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008164 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008165 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008166 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008167 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008168#ifdef FEAT_CLIENTSERVER
8169 {
8170 XPropertyEvent *e = (XPropertyEvent *)&event;
8171
8172 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008174 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008177 XtDispatchEvent(&event);
8178 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008179 else
8180 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008181 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008182 XtAppProcessEvent(app_context, mask);
8183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 }
8185}
8186
8187 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008188clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189{
8190 if (xterm_Shell != (Widget)0)
8191 return clip_x11_own_selection(xterm_Shell, cbd);
8192 return FAIL;
8193}
8194
8195 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008196clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197{
8198 if (xterm_Shell != (Widget)0)
8199 clip_x11_lose_selection(xterm_Shell, cbd);
8200}
8201
8202 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008203clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204{
8205 if (xterm_Shell != (Widget)0)
8206 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
8207}
8208
8209 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008210clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211{
8212 clip_x11_set_selection(cbd);
8213}
8214#endif
8215
8216
8217#if defined(USE_XSMP) || defined(PROTO)
8218/*
8219 * Code for X Session Management Protocol.
8220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221
8222# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223/*
8224 * This is our chance to ask the user if they want to save,
8225 * or abort the logout
8226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008228xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
Bram Moolenaare1004402020-10-24 20:49:43 +02008230 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 int cancel_shutdown = False;
8232
Bram Moolenaare1004402020-10-24 20:49:43 +02008233 save_cmod_flags = cmdmod.cmod_flags;
8234 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01008235 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01008236 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02008238 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01008239 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 out_flush();
8241
Bram Moolenaar0f873732019-12-05 20:28:46 +01008242 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 SmcInteractDone(smc_conn, cancel_shutdown);
8244
Bram Moolenaar0f873732019-12-05 20:28:46 +01008245 // Finish off
8246 // Only end save-yourself here if we're not cancelling shutdown;
8247 // we'll get a cancelled callback later in which we'll end it.
8248 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 if (!cancel_shutdown)
8250 {
8251 xsmp.save_yourself = False;
8252 SmcSaveYourselfDone(smc_conn, True);
8253 }
8254}
8255# endif
8256
8257/*
8258 * Callback that starts save-yourself.
8259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008261xsmp_handle_save_yourself(
8262 SmcConn smc_conn,
8263 SmPointer client_data UNUSED,
8264 int save_type UNUSED,
8265 Bool shutdown,
8266 int interact_style UNUSED,
8267 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008269 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 if (xsmp.save_yourself)
8271 SmcSaveYourselfDone(smc_conn, True);
8272 xsmp.save_yourself = True;
8273 xsmp.shutdown = shutdown;
8274
Bram Moolenaar0f873732019-12-05 20:28:46 +01008275 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01008277 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278
8279 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008280 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281
8282# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008283 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008285 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 SmcInteractRequest(smc_conn, SmDialogError,
8287 xsmp_handle_interaction, client_data);
8288 else
8289# endif
8290 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008291 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 SmcSaveYourselfDone(smc_conn, True);
8293 xsmp.save_yourself = False;
8294 }
8295}
8296
8297
8298/*
8299 * Callback to warn us of imminent death.
8300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008302xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303{
8304 xsmp_close();
8305
Bram Moolenaar0f873732019-12-05 20:28:46 +01008306 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 getout_preserve_modified(0);
8308}
8309
8310
8311/*
8312 * Callback to tell us that save-yourself has completed.
8313 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008315xsmp_save_complete(
8316 SmcConn smc_conn UNUSED,
8317 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318{
8319 xsmp.save_yourself = False;
8320}
8321
8322
8323/*
8324 * Callback to tell us that an instigated shutdown was cancelled
8325 * (maybe even by us)
8326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008328xsmp_shutdown_cancelled(
8329 SmcConn smc_conn,
8330 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331{
8332 if (xsmp.save_yourself)
8333 SmcSaveYourselfDone(smc_conn, True);
8334 xsmp.save_yourself = False;
8335 xsmp.shutdown = False;
8336}
8337
8338
8339/*
8340 * Callback to tell us that a new ICE connection has been established.
8341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008343xsmp_ice_connection(
8344 IceConn iceConn,
8345 IcePointer clientData UNUSED,
8346 Bool opening,
8347 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008349 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 if (opening)
8351 {
8352 xsmp_icefd = IceConnectionNumber(iceConn);
8353 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8354 }
8355}
8356
8357
Bram Moolenaar0f873732019-12-05 20:28:46 +01008358// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008360xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361{
8362 Bool rep;
8363
8364 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8365 == IceProcessMessagesIOError)
8366 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008367 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008369 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 xsmp_close();
8371 return FAIL;
8372 }
8373 else
8374 return OK;
8375}
8376
8377static int dummy;
8378
Bram Moolenaar0f873732019-12-05 20:28:46 +01008379// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 void
8381xsmp_init(void)
8382{
8383 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 SmcCallbacks smcallbacks;
8385#if 0
8386 SmPropValue smname;
8387 SmProp smnameprop;
8388 SmProp *smprops[1];
8389#endif
8390
8391 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008392 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393
8394 xsmp.save_yourself = xsmp.shutdown = False;
8395
Bram Moolenaar0f873732019-12-05 20:28:46 +01008396 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8398 smcallbacks.save_yourself.client_data = NULL;
8399 smcallbacks.die.callback = xsmp_die;
8400 smcallbacks.die.client_data = NULL;
8401 smcallbacks.save_complete.callback = xsmp_save_complete;
8402 smcallbacks.save_complete.client_data = NULL;
8403 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8404 smcallbacks.shutdown_cancelled.client_data = NULL;
8405
Bram Moolenaar0f873732019-12-05 20:28:46 +01008406 // Set up a watch on ICE connection creations. The "dummy" argument is
8407 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8409 {
8410 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008411 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 return;
8413 }
8414
Bram Moolenaar0f873732019-12-05 20:28:46 +01008415 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 xsmp.smcconn = SmcOpenConnection(
8417 NULL,
8418 NULL,
8419 SmProtoMajor,
8420 SmProtoMinor,
8421 SmcSaveYourselfProcMask | SmcDieProcMask
8422 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8423 &smcallbacks,
8424 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008425 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008426 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 errorstring);
8428 if (xsmp.smcconn == NULL)
8429 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008431 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008432 char errorreport[132];
8433
8434 // If the message is too long it might not be NUL terminated. Add
8435 // a NUL at the end to make sure we don't go over the end.
8436 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008437 vim_snprintf(errorreport, sizeof(errorreport),
8438 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008439 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 return;
8442 }
8443 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8444
8445#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008446 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 smname.value = "vim";
8448 smname.length = 3;
8449 smnameprop.name = "SmProgram";
8450 smnameprop.type = "SmARRAY8";
8451 smnameprop.num_vals = 1;
8452 smnameprop.vals = &smname;
8453
8454 smprops[0] = &smnameprop;
8455 SmcSetProperties(xsmp.smcconn, 1, smprops);
8456#endif
8457}
8458
8459
Bram Moolenaar0f873732019-12-05 20:28:46 +01008460// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008462xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008464 if (xsmp_icefd == -1)
8465 return;
8466
8467 SmcCloseConnection(xsmp.smcconn, 0, NULL);
8468 if (xsmp.clientid != NULL)
8469 free(xsmp.clientid);
8470 xsmp.clientid = NULL;
8471 xsmp_icefd = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008473#endif // USE_XSMP
Paul Ollis65745772022-06-05 16:55:54 +01008474
8475#if defined(FEAT_RELTIME) || defined(PROTO)
Bram Moolenaar08210f82023-04-13 19:15:54 +01008476# if defined(PROF_NSEC) || defined(PROTO)
Paul Ollis65745772022-06-05 16:55:54 +01008477/*
8478 * Implement timeout with timer_create() and timer_settime().
8479 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008480static volatile sig_atomic_t timeout_flag = FALSE;
8481static timer_t timer_id;
8482static int timer_created = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008483
8484/*
8485 * Callback for when the timer expires.
8486 */
8487 static void
8488set_flag(union sigval _unused UNUSED)
8489{
8490 timeout_flag = TRUE;
8491}
8492
8493/*
8494 * Stop any active timeout.
8495 */
8496 void
8497stop_timeout(void)
8498{
8499 static struct itimerspec disarm = {{0, 0}, {0, 0}};
8500
8501 if (timer_created)
8502 {
8503 int ret = timer_settime(timer_id, 0, &disarm, NULL);
8504
8505 if (ret < 0)
8506 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8507 }
8508
8509 // Clear the current timeout flag; any previous timeout should be
8510 // considered _not_ triggered.
8511 timeout_flag = FALSE;
8512}
8513
8514/*
8515 * Start the timeout timer.
8516 *
8517 * The return value is a pointer to a flag that is initialised to FALSE. If the
8518 * timeout expires, the flag is set to TRUE. This will only return pointers to
8519 * static memory; i.e. any pointer returned by this function may always be
8520 * safely dereferenced.
8521 *
8522 * This function is not expected to fail, but if it does it will still return a
8523 * valid flag pointer; the flag will remain stuck as FALSE .
8524 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008525 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008526start_timeout(long msec)
8527{
8528 struct itimerspec interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008529 {0, 0}, // Do not repeat.
8530 {msec / 1000, (msec % 1000) * 1000000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008531 int ret;
8532
8533 // This is really the caller's responsibility, but let's make sure the
8534 // previous timer has been stopped.
8535 stop_timeout();
Paul Ollis65745772022-06-05 16:55:54 +01008536
8537 if (!timer_created)
8538 {
8539 struct sigevent action = {0};
8540
8541 action.sigev_notify = SIGEV_THREAD;
8542 action.sigev_notify_function = set_flag;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008543 ret = timer_create(CLOCK_MONOTONIC, &action, &timer_id);
8544 if (ret < 0)
Paul Ollis65745772022-06-05 16:55:54 +01008545 {
8546 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8547 return &timeout_flag;
8548 }
8549 timer_created = TRUE;
8550 }
8551
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00008552# ifdef FEAT_EVAL
Bram Moolenaar616592e2022-06-17 15:17:10 +01008553 ch_log(NULL, "setting timeout timer to %d sec %ld nsec",
8554 (int)interval.it_value.tv_sec, (long)interval.it_value.tv_nsec);
Bram Moolenaar509ce032022-06-20 11:23:01 +01008555# endif
Paul Ollis65745772022-06-05 16:55:54 +01008556 ret = timer_settime(timer_id, 0, &interval, NULL);
8557 if (ret < 0)
8558 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8559
8560 return &timeout_flag;
8561}
8562
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008563/*
8564 * To be used before fork/exec: delete any created timer.
8565 */
8566 void
8567delete_timer(void)
8568{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008569 if (!timer_created)
8570 return;
8571
8572 timer_delete(timer_id);
8573 timer_created = FALSE;
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008574}
8575
Bram Moolenaar08210f82023-04-13 19:15:54 +01008576# else // PROF_NSEC
Paul Ollis65745772022-06-05 16:55:54 +01008577
8578/*
8579 * Implement timeout with setitimer()
8580 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008581static struct sigaction prev_sigaction;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008582static volatile sig_atomic_t timeout_flag = FALSE;
8583static int timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008584static int timer_handler_active = FALSE;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008585static volatile sig_atomic_t alarm_pending = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008586
8587/*
8588 * Handle SIGALRM for a timeout.
8589 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01008590 static void
Paul Ollis65745772022-06-05 16:55:54 +01008591set_flag SIGDEFARG(sigarg)
8592{
8593 if (alarm_pending)
8594 alarm_pending = FALSE;
8595 else
8596 timeout_flag = TRUE;
8597}
8598
8599/*
8600 * Stop any active timeout.
8601 */
8602 void
8603stop_timeout(void)
8604{
8605 static struct itimerval disarm = {{0, 0}, {0, 0}};
8606 int ret;
8607
8608 if (timer_active)
8609 {
8610 timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008611 ret = setitimer(ITIMER_REAL, &disarm, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008612 if (ret < 0)
8613 // Should only get here as a result of coding errors.
8614 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8615 }
8616
8617 if (timer_handler_active)
8618 {
8619 timer_handler_active = FALSE;
8620 ret = sigaction(SIGALRM, &prev_sigaction, NULL);
8621 if (ret < 0)
8622 // Should only get here as a result of coding errors.
8623 semsg(_(e_could_not_reset_handler_for_timeout_str),
8624 strerror(errno));
8625 }
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008626 timeout_flag = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008627}
8628
8629/*
8630 * Start the timeout timer.
8631 *
8632 * The return value is a pointer to a flag that is initialised to FALSE. If the
8633 * timeout expires, the flag is set to TRUE. This will only return pointers to
8634 * static memory; i.e. any pointer returned by this function may always be
8635 * safely dereferenced.
8636 *
8637 * This function is not expected to fail, but if it does it will still return a
8638 * valid flag pointer; the flag will remain stuck as FALSE .
8639 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008640 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008641start_timeout(long msec)
8642{
8643 struct itimerval interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008644 {0, 0}, // Do not repeat.
8645 {msec / 1000, (msec % 1000) * 1000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008646 struct sigaction handle_alarm;
8647 int ret;
8648 sigset_t sigs;
8649 sigset_t saved_sigs;
8650
8651 // This is really the caller's responsibility, but let's make sure the
8652 // previous timer has been stopped.
8653 stop_timeout();
8654
8655 // There is a small chance that SIGALRM is pending and so the handler must
8656 // ignore it on the first call.
8657 alarm_pending = FALSE;
8658 ret = sigemptyset(&sigs);
8659 ret = ret == 0 ? sigaddset(&sigs, SIGALRM) : ret;
8660 ret = ret == 0 ? sigprocmask(SIG_BLOCK, &sigs, &saved_sigs) : ret;
8661 timeout_flag = FALSE;
8662 ret = ret == 0 ? sigpending(&sigs) : ret;
8663 if (ret == 0)
8664 {
8665 alarm_pending = sigismember(&sigs, SIGALRM);
Bram Moolenaar1f89abf2022-06-06 10:07:01 +01008666 ret = sigprocmask(SIG_SETMASK, &saved_sigs, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008667 }
8668 if (unlikely(ret != 0 || alarm_pending < 0))
8669 {
8670 // Just catching coding errors. Write an error message, but carry on.
8671 semsg(_(e_could_not_check_for_pending_sigalrm_str), strerror(errno));
8672 alarm_pending = FALSE;
8673 }
8674
8675 // Set up the alarm handler first.
8676 ret = sigemptyset(&handle_alarm.sa_mask);
8677 handle_alarm.sa_handler = set_flag;
8678 handle_alarm.sa_flags = 0;
8679 ret = ret == 0 ? sigaction(SIGALRM, &handle_alarm, &prev_sigaction) : ret;
8680 if (ret < 0)
8681 {
8682 // Should only get here as a result of coding errors.
8683 semsg(_(e_could_not_set_handler_for_timeout_str), strerror(errno));
8684 return &timeout_flag;
8685 }
8686 timer_handler_active = TRUE;
8687
8688 // Set up the interval timer once the alarm handler is in place.
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008689 ret = setitimer(ITIMER_REAL, &interval, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008690 if (ret < 0)
8691 {
8692 // Should only get here as a result of coding errors.
8693 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8694 stop_timeout();
8695 return &timeout_flag;
8696 }
8697
8698 timer_active = TRUE;
8699 return &timeout_flag;
8700}
Bram Moolenaar08210f82023-04-13 19:15:54 +01008701# endif // PROF_NSEC
Paul Ollis65745772022-06-05 16:55:54 +01008702#endif // FEAT_RELTIME