blob: d15514268f0d7ff2ca5299020d0c901f99275954 [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
zeertzjq6de4e582023-09-30 14:19:14 +020039# include <sys/xattr.h>
Christian Brabandte085dfd2023-09-30 12:49:18 +020040#endif
41
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020042#ifdef HAVE_SMACK
zeertzjq6de4e582023-09-30 14:19:14 +020043# include <sys/xattr.h>
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020044# include <linux/xattr.h>
45# ifndef SMACK_LABEL_LEN
46# define SMACK_LABEL_LEN 1024
47# endif
48#endif
49
Bram Moolenaara2442432007-04-26 14:26:37 +000050#ifdef __CYGWIN__
K.Takata972db232022-02-04 10:45:38 +000051# include <cygwin/version.h>
52# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
Bram Moolenaar0f873732019-12-05 20:28:46 +010053 // for cygwin_conv_path()
K.Takata972db232022-02-04 10:45:38 +000054# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
55# define WIN32_LEAN_AND_MEAN
56# include <windows.h>
57# include "winclip.pro"
Bram Moolenaara2442432007-04-26 14:26:37 +000058# endif
59#endif
60
Bram Moolenaar071d4272004-06-13 20:20:40 +000061#ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000062
Bram Moolenaar071d4272004-06-13 20:20:40 +000063# include <gpm.h>
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000064
65# ifdef DYNAMIC_GPM
66# define Gpm_Open (*dll_Gpm_Open)
67# define Gpm_Close (*dll_Gpm_Close)
68# define Gpm_GetEvent (*dll_Gpm_GetEvent)
69# define gpm_flag (dll_gpm_flag != NULL ? *dll_gpm_flag : 0)
70# define gpm_fd (dll_gpm_fd != NULL ? *dll_gpm_fd : -1)
71
72static int (*dll_Gpm_Open) (Gpm_Connect *, int);
73static int (*dll_Gpm_Close) (void);
74static int (*dll_Gpm_GetEvent) (Gpm_Event *);
75static int *dll_gpm_flag;
76static int *dll_gpm_fd;
77
78static void *libgpm_hinst;
79# endif
80
Bram Moolenaar0f873732019-12-05 20:28:46 +010081// <linux/keyboard.h> contains defines conflicting with "keymap.h",
82// I just copied relevant defines here. A cleaner solution would be to put gpm
83// code into separate file and include there linux/keyboard.h
84// #include <linux/keyboard.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000085# define KG_SHIFT 0
86# define KG_CTRL 2
87# define KG_ALT 3
88# define KG_ALTGR 1
89# define KG_SHIFTL 4
90# define KG_SHIFTR 5
91# define KG_CTRLL 6
92# define KG_CTRLR 7
93# define KG_CAPSSHIFT 8
94
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010095static void gpm_close(void);
96static int gpm_open(void);
97static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098#endif
99
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000100#ifdef FEAT_SYSMOUSE
101# include <sys/consio.h>
102# include <sys/fbio.h>
103
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100104static int sysmouse_open(void);
105static void sysmouse_close(void);
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100106static void sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000107#endif
108
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109/*
110 * end of autoconf section. To be extended...
111 */
112
Bram Moolenaar0f873732019-12-05 20:28:46 +0100113// Are the following #ifdefs still required? And why? Is that for X11?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114
115#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
116# ifdef SIGWINCH
117# undef SIGWINCH
118# endif
119# ifdef TIOCGWINSZ
120# undef TIOCGWINSZ
121# endif
122#endif
123
Bram Moolenaar0f873732019-12-05 20:28:46 +0100124#if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125# define SIGWINCH SIGWINDOW
126#endif
127
128#ifdef FEAT_X11
129# include <X11/Xlib.h>
130# include <X11/Xutil.h>
131# include <X11/Xatom.h>
132# ifdef FEAT_XCLIPBOARD
133# include <X11/Intrinsic.h>
134# include <X11/Shell.h>
135# include <X11/StringDefs.h>
136static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void clip_update(void);
138static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139# endif
140
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141Window x11_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143#endif
144
Bram Moolenaar5c3128e2020-05-11 20:54:42 +0200145static int ignore_sigtstp = FALSE;
146
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
149static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200150static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +0200151static int unix_did_set_title = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152static char_u *oldicon = NULL;
153static int did_set_icon = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156
Bram Moolenaar205b8862011-09-07 15:04:31 +0200157#ifdef HAVE_UNION_WAIT
158typedef union wait waitstatus;
159#else
160typedef int waitstatus;
161#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200162static int WaitForChar(long msec, int *interrupted, int ignore_input);
163static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar041c7102020-05-30 18:14:57 +0200164#ifdef VMS
Bram Moolenaarcda77642016-06-04 13:32:35 +0200165int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200167static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169
170#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int do_xterm_trace(void);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100172# define XT_TRACE_DELAY 50 // delay for xterm tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#endif
174
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100175static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
177#if defined(SIGWINCH)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100178static void sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000180#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100181static void sig_tstp SIGPROTOARG;
Bram Moolenaarabd56da2022-06-23 20:46:27 +0100182// volatile because it is used in signal handler sig_tstp() and
183// sigcont_handler().
dbivolaruab16ad32021-12-29 19:41:47 +0000184static volatile sig_atomic_t in_mch_suspend = FALSE;
185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186#if defined(SIGINT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100187static void catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200189#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100190static void catch_sigusr1 SIGPROTOARG;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100193static void catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194#endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000195#if defined(SIGALRM) && defined(FEAT_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196# define SET_SIG_ALARM
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100197static void sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100198// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200199static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200#endif
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100201static void deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100203static void catch_int_signal(void);
204static void set_signals(void);
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100205static void catch_signals(void (*func_deadly)(int), void (*func_other)(int));
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200206#ifdef HAVE_SIGPROCMASK
207# define SIGSET_DECL(set) sigset_t set;
208# define BLOCK_SIGNALS(set) block_signals(set)
209# define UNBLOCK_SIGNALS(set) unblock_signals(set)
210#else
211# define SIGSET_DECL(set)
212# define BLOCK_SIGNALS(set) do { /**/ } while (0)
213# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
214#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100215static int have_wildcard(int, char_u **);
216static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100218static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
220#ifndef SIG_ERR
ichizok378447f2023-05-11 22:25:42 +0100221# define SIG_ERR ((sighandler_T)-1)
222#endif
223#ifndef SIG_HOLD
224# define SIG_HOLD ((sighandler_T)-2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#endif
226
Bram Moolenaar0f873732019-12-05 20:28:46 +0100227// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200228static volatile sig_atomic_t do_resize = FALSE;
dbivolaruab16ad32021-12-29 19:41:47 +0000229// volatile because it is used in signal handler sig_tstp().
230static volatile sig_atomic_t got_tstp = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231static char_u *extra_shell_arg = NULL;
232static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100233// volatile because it is used in signal handler deathtrap().
234static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
235// volatile because it is used in signal handler deathtrap().
236static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100238#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
239static int dont_check_job_ended = 0;
240#endif
241
Bram Moolenaar26e86442020-05-17 14:06:16 +0200242// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
243static tmode_T mch_cur_tmode = TMODE_COOK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244
245#ifdef USE_XSMP
246typedef struct
247{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100248 SmcConn smcconn; // The SM connection ID
249 IceConn iceconn; // The ICE connection ID
250 char *clientid; // The client ID for the current smc session
251 Bool save_yourself; // If we're in the middle of a save_yourself
252 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253} xsmp_config_T;
254
255static xsmp_config_T xsmp;
256#endif
257
258#ifdef SYS_SIGLIST_DECLARED
259/*
260 * I have seen
261 * extern char *_sys_siglist[NSIG];
Yegappan Lakshmananaebc6ef2022-08-27 21:24:26 +0100262 * on Linux, NetBSD and Solaris. It contains a nice list of strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 * that describe the signals. That is nearly what we want here. But
264 * autoconf does only check for sys_siglist (without the underscore), I
265 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100266 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 */
268#endif
269
270static struct signalinfo
271{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100272 int sig; // Signal number, eg. SIGSEGV etc
273 char *name; // Signal name (not char_u!).
274 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275} signal_info[] =
276{
277#ifdef SIGHUP
278 {SIGHUP, "HUP", TRUE},
279#endif
280#ifdef SIGQUIT
281 {SIGQUIT, "QUIT", TRUE},
282#endif
283#ifdef SIGILL
284 {SIGILL, "ILL", TRUE},
285#endif
286#ifdef SIGTRAP
287 {SIGTRAP, "TRAP", TRUE},
288#endif
289#ifdef SIGABRT
290 {SIGABRT, "ABRT", TRUE},
291#endif
292#ifdef SIGEMT
293 {SIGEMT, "EMT", TRUE},
294#endif
295#ifdef SIGFPE
296 {SIGFPE, "FPE", TRUE},
297#endif
298#ifdef SIGBUS
299 {SIGBUS, "BUS", TRUE},
300#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100301#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100302 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 {SIGSEGV, "SEGV", TRUE},
304#endif
305#ifdef SIGSYS
306 {SIGSYS, "SYS", TRUE},
307#endif
308#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100309 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#endif
311#ifdef SIGTERM
312 {SIGTERM, "TERM", TRUE},
313#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100314#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 {SIGVTALRM, "VTALRM", TRUE},
316#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000317#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100318 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
319 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 {SIGPROF, "PROF", TRUE},
321#endif
322#ifdef SIGXCPU
323 {SIGXCPU, "XCPU", TRUE},
324#endif
325#ifdef SIGXFSZ
326 {SIGXFSZ, "XFSZ", TRUE},
327#endif
328#ifdef SIGUSR1
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200329 {SIGUSR1, "USR1", FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000331#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100332 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 {SIGUSR2, "USR2", TRUE},
334#endif
335#ifdef SIGINT
336 {SIGINT, "INT", FALSE},
337#endif
338#ifdef SIGWINCH
339 {SIGWINCH, "WINCH", FALSE},
340#endif
341#ifdef SIGTSTP
342 {SIGTSTP, "TSTP", FALSE},
343#endif
344#ifdef SIGPIPE
345 {SIGPIPE, "PIPE", FALSE},
346#endif
347 {-1, "Unknown!", FALSE}
348};
349
ichizok378447f2023-05-11 22:25:42 +0100350 sighandler_T
351mch_signal(int sig, sighandler_T func)
352{
353#if defined(HAVE_SIGACTION) && defined(HAVE_SIGPROCMASK)
354 // Modern implementation: use sigaction().
355 struct sigaction sa, old;
356 sigset_t curset;
357 int blocked;
358
359 if (sigprocmask(SIG_BLOCK, NULL, &curset) == -1)
360 return SIG_ERR;
361
362 blocked = sigismember(&curset, sig);
363
364 if (func == SIG_HOLD)
365 {
366 if (blocked)
367 return SIG_HOLD;
368
369 sigemptyset(&curset);
370 sigaddset(&curset, sig);
371
372 if (sigaction(sig, NULL, &old) == -1
373 || sigprocmask(SIG_BLOCK, &curset, NULL) == -1)
374 return SIG_ERR;
375 return old.sa_handler;
376 }
377
378 if (blocked)
379 {
380 sigemptyset(&curset);
381 sigaddset(&curset, sig);
382
383 if (sigprocmask(SIG_UNBLOCK, &curset, NULL) == -1)
384 return SIG_ERR;
385 }
386
387 sa.sa_handler = func;
388 sigemptyset(&sa.sa_mask);
389# ifdef SA_RESTART
390 sa.sa_flags = SA_RESTART;
391# else
392 sa.sa_flags = 0;
393# endif
394 if (sigaction(sig, &sa, &old) == -1)
395 return SIG_ERR;
396 return blocked ? SIG_HOLD: old.sa_handler;
397#elif defined(HAVE_SIGSET)
398 // Using sigset() is preferred above signal().
399 return sigset(sig, func);
400#else
401 // Oldest and most compatible solution.
402 return signal(sig, func);
403#endif
404}
405
Bram Moolenaar25724922009-07-14 15:38:41 +0000406 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100407mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000408{
409 if (p_verbose >= 5)
410 {
411 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100412 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000413 verbose_leave();
414 }
ichizok378447f2023-05-11 22:25:42 +0100415#ifdef VMS
Bram Moolenaar25724922009-07-14 15:38:41 +0000416 return chdir(vms_fixfilename(path));
ichizok378447f2023-05-11 22:25:42 +0100417#else
Bram Moolenaar25724922009-07-14 15:38:41 +0000418 return chdir(path);
ichizok378447f2023-05-11 22:25:42 +0100419#endif
Bram Moolenaar25724922009-07-14 15:38:41 +0000420}
421
Bram Moolenaar0f873732019-12-05 20:28:46 +0100422// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100423#if defined(ECHOE) && defined(ICANON) \
424 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
425 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200426# define NEW_TTY_SYSTEM
427#endif
428
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000429/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100430 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100433mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200435 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100436 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100437 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438}
439
440/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100441 * Function passed to inchar_loop() to handle window resizing.
442 * If "check_only" is TRUE: Return whether there was a resize.
443 * If "check_only" is FALSE: Deal with the window resized.
444 */
445 static int
446resize_func(int check_only)
447{
448 if (check_only)
449 return do_resize;
450 while (do_resize)
Bram Moolenaar55f1b822023-06-21 13:42:48 +0100451 {
Bram Moolenaar545c8a52023-06-21 15:51:47 +0100452#ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +0100453 ch_log(NULL, "calling handle_resize() in resize_func()");
Bram Moolenaar545c8a52023-06-21 15:51:47 +0100454#endif
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100455 handle_resize();
Bram Moolenaar55f1b822023-06-21 13:42:48 +0100456 }
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100457 return FALSE;
458}
459
460/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000461 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 * Get a characters from the keyboard.
463 * Return the number of characters that are available.
464 * If wtime == 0 do not wait for characters.
465 * If wtime == n wait a short time for characters.
466 * If wtime == -1 wait forever for characters.
467 */
468 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100469mch_inchar(
470 char_u *buf,
471 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100472 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100473 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100475 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
476 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477}
478
479 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100480handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481{
482 do_resize = FALSE;
483 shell_resized();
484}
485
486/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200487 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 */
489 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100490mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200492 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493}
494
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200495#if defined(FEAT_TERMINAL) || defined(PROTO)
496/*
497 * Check for any pending input or messages.
498 */
499 int
500mch_check_messages(void)
501{
502 return WaitForChar(0L, NULL, TRUE);
503}
504#endif
505
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
507# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000508# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509# endif
510# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
511# include <sys/sysctl.h>
512# endif
513# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
514# include <sys/sysinfo.h>
515# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100516# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100517# include <mach/mach_host.h>
518# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100519# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520
521/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000522 * Return total amount of memory available in Kbyte.
523 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100526mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100529 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530
Bram Moolenaar362dc332018-03-05 21:59:37 +0100531# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100532 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100533 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100534 mach_port_t host = mach_host_self();
535 kern_return_t kret;
536# ifdef HOST_VM_INFO64
537 struct vm_statistics64 vm_stat;
538 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
Bram Moolenaar988615f2018-02-27 17:58:20 +0100540 kret = host_statistics64(host, HOST_VM_INFO64,
541 (host_info64_t)&vm_stat, &count);
542# else
543 struct vm_statistics vm_stat;
544 natural_t count = HOST_VM_INFO_COUNT;
545
546 kret = host_statistics(host, HOST_VM_INFO,
547 (host_info_t)&vm_stat, &count);
548# endif
549 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100550 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100551 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
552 + vm_stat.inactive_count
553# ifdef MAC_OS_X_VERSION_10_9
554 + vm_stat.compressor_page_count
555# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100556 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100557 mach_port_deallocate(mach_task_self(), host);
558 }
559# endif
560
561# ifdef HAVE_SYSCTL
562 if (mem == 0)
563 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100564 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100565 int mib[2];
566 size_t len = sizeof(long_u);
567# ifdef HW_USERMEM64
568 long_u physmem;
569# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100570 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100571 union {
572 int_u u32;
573 long_u u64;
574 } physmem;
575# endif
576
577 mib[0] = CTL_HW;
578# ifdef HW_USERMEM64
579 mib[1] = HW_USERMEM64;
580# else
581 mib[1] = HW_USERMEM;
582# endif
583 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
584 {
585# ifdef HW_USERMEM64
586 mem = (long_u)physmem;
587# else
588 if (len == sizeof(physmem.u64))
589 mem = (long_u)physmem.u64;
590 else
591 mem = (long_u)physmem.u32;
592# endif
593 }
594 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200595# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200597# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (mem == 0)
599 {
600 struct sysinfo sinfo;
601
Bram Moolenaar0f873732019-12-05 20:28:46 +0100602 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000604 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200605# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100606 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000607 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
608 {
609 sinfo.mem_unit = sinfo.mem_unit >> 1;
610 --shiftright;
611 }
612 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200613# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200615# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200620# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 if (mem == 0)
622 {
623 long pagesize, pagecount;
624
Bram Moolenaar0f873732019-12-05 20:28:46 +0100625 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 pagesize = sysconf(_SC_PAGESIZE);
627 pagecount = sysconf(_SC_PHYS_PAGES);
628 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000629 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100630 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000631 while (shiftright > 0 && (pagesize & 1) == 0)
632 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000633 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000634 --shiftright;
635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
Bram Moolenaar0f873732019-12-05 20:28:46 +0100641 // Return the minimum of the physical memory and the user limit, because
642 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200643# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {
645 struct rlimit rlp;
646
647 if (getrlimit(RLIMIT_DATA, &rlp) == 0
648 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200649# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200651# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000652 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000654 {
655 mem = (long_u)rlp.rlim_cur;
656 shiftright = 10;
657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200659# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000662 return mem >> shiftright;
663 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664}
665#endif
666
Bram Moolenaar0981c872020-08-23 14:28:37 +0200667/*
668 * "flags": MCH_DELAY_IGNOREINPUT - don't read input
669 * MCH_DELAY_SETTMODE - use settmode() even for short delays
670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200672mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200674 tmode_T old_tmode;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200675 int call_settmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000676#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100677 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
Bram Moolenaar0981c872020-08-23 14:28:37 +0200680 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100682 // Go to cooked mode without echo, to allow SIGINT interrupting us
683 // here. But we don't want QUIT to kill us (CTRL-\ used in a
684 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200685 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000686 in_mch_delay = TRUE;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200687 call_settmode = mch_cur_tmode == TMODE_RAW
688 && (msec > 500 || (flags & MCH_DELAY_SETTMODE));
689 if (call_settmode)
690 {
691 old_tmode = mch_cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 settmode(TMODE_SLEEP);
Bram Moolenaar80361a52020-10-05 21:39:25 +0200693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
695 /*
696 * Everybody sleeps in a different way...
697 * Prefer nanosleep(), some versions of usleep() can only sleep up to
698 * one second.
699 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000700#ifdef FEAT_MZSCHEME
701 do
702 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100703 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000704 if (total > p_mzq)
705 msec = p_mzq;
706 else
707 msec = total;
708 total -= msec;
709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#ifdef HAVE_NANOSLEEP
711 {
712 struct timespec ts;
713
714 ts.tv_sec = msec / 1000;
715 ts.tv_nsec = (msec % 1000) * 1000000;
716 (void)nanosleep(&ts, NULL);
717 }
718#else
719# ifdef HAVE_USLEEP
720 while (msec >= 1000)
721 {
722 usleep((unsigned int)(999 * 1000));
723 msec -= 999;
724 }
725 usleep((unsigned int)(msec * 1000));
726# else
727# ifndef HAVE_SELECT
728 poll(NULL, 0, (int)msec);
729# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {
731 struct timeval tv;
732
733 tv.tv_sec = msec / 1000;
734 tv.tv_usec = (msec % 1000) * 1000;
Bram Moolenaar0981c872020-08-23 14:28:37 +0200735 // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
736 // a patch from Sun to fix this. Reported by Gunnar Pedersen.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 select(0, NULL, NULL, NULL, &tv);
738 }
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100739# endif
740# endif
741#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000742#ifdef FEAT_MZSCHEME
743 }
744 while (total > 0);
745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746
Bram Moolenaar80361a52020-10-05 21:39:25 +0200747 if (call_settmode)
Bram Moolenaar26e86442020-05-17 14:06:16 +0200748 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000749 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 }
751 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200752 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753}
754
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000755#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
757# define HAVE_CHECK_STACK_GROWTH
758/*
759 * Support for checking for an almost-out-of-stack-space situation.
760 */
761
762/*
763 * Return a pointer to an item on the stack. Used to find out if the stack
764 * grows up or down.
765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766static int stack_grows_downwards;
767
768/*
769 * Find out if the stack grows upwards or downwards.
770 * "p" points to a variable on the stack of the caller.
771 */
772 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100773check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
775 int i;
776
777 stack_grows_downwards = (p > (char *)&i);
778}
779#endif
780
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000781#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782static char *stack_limit = NULL;
783
784#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
785# include <pthread.h>
786# include <pthread_np.h>
787#endif
788
789/*
790 * Find out until how var the stack can grow without getting into trouble.
791 * Called when starting up and when switching to the signal stack in
792 * deathtrap().
793 */
794 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100795get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
797 struct rlimit rlp;
798 int i;
799 long lim;
800
Bram Moolenaar0f873732019-12-05 20:28:46 +0100801 // Set the stack limit to 15/16 of the allowable size. Skip this when the
802 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 if (getrlimit(RLIMIT_STACK, &rlp) == 0
804 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
805# ifdef RLIM_INFINITY
806 && rlp.rlim_cur != RLIM_INFINITY
807# endif
808 )
809 {
810 lim = (long)rlp.rlim_cur;
811#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
812 {
813 pthread_attr_t attr;
814 size_t size;
815
Bram Moolenaar0f873732019-12-05 20:28:46 +0100816 // On FreeBSD the initial thread always has a fixed stack size, no
817 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 pthread_attr_init(&attr);
819 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
820 {
821 pthread_attr_getstacksize(&attr, &size);
822 if (lim > (long)size)
823 lim = (long)size;
824 }
825 pthread_attr_destroy(&attr);
826 }
827#endif
828 if (stack_grows_downwards)
829 {
830 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
831 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100832 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 stack_limit = (char *)((long)&i / 16L);
834 }
835 else
836 {
837 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
838 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100839 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 }
841 }
842}
843
844/*
845 * Return FAIL when running out of stack space.
846 * "p" must point to any variable local to the caller that's on the stack.
847 */
848 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100849mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000851 if (stack_limit == NULL)
852 return OK;
853
854 if (stack_grows_downwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000856 if (p < stack_limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 return FAIL;
858 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000859 else if (p > stack_limit)
860 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 return OK;
862}
863#endif
864
865#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
866/*
867 * Support for using the signal stack.
868 * This helps when we run out of stack space, which causes a SIGSEGV. The
869 * signal handler then must run on another stack, since the normal stack is
870 * completely full.
871 */
872
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100874static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100876static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877# endif
878
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200879/*
880 * Get a size of signal stack.
881 * Preference (if available): sysconf > SIGSTKSZ > guessed size
882 */
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100883static long int get_signal_stack_size(void)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200884{
885# ifdef HAVE_SYSCONF_SIGSTKSZ
886 long int size = -1;
887
888 // return size only if sysconf doesn't return an error
889 if ((size = sysconf(_SC_SIGSTKSZ)) > -1)
890 return size;
891# endif
892
893# ifdef SIGSTKSZ
894 // if sysconf() isn't available or gives error, return SIGSTKSZ
895 // if defined
896 return SIGSTKSZ;
897# endif
898
899 // otherwise guess the size
900 return 8000;
901}
902
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903static char *signal_stack;
904
905 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100906init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000908 if (signal_stack == NULL)
909 return;
910
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912# ifdef HAVE_SS_BASE
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000913 sigstk.ss_base = signal_stack;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000915 sigstk.ss_sp = signal_stack;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000917 sigstk.ss_size = get_signal_stack_size();
918 sigstk.ss_flags = 0;
919 (void)sigaltstack(&sigstk, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000921 sigstk.ss_sp = signal_stack;
922 if (stack_grows_downwards)
923 sigstk.ss_sp += get_signal_stack_size() - 1;
924 sigstk.ss_onstack = 0;
925 (void)sigstack(&sigstk, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927}
928#endif
929
930/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000931 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 * will barf when the second argument to signal() is ``wrong''.
933 * Let me try it with a few tricky defines from my own osdef.h (jw).
934 */
935#if defined(SIGWINCH)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100936 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937sig_winch SIGDEFARG(sigarg)
938{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100939 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100940 mch_signal(SIGWINCH, sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 do_resize = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942}
943#endif
944
dbivolaruab16ad32021-12-29 19:41:47 +0000945#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100946 static void
dbivolaruab16ad32021-12-29 19:41:47 +0000947sig_tstp SIGDEFARG(sigarg)
948{
949 // Second time we get called we actually need to suspend
950 if (in_mch_suspend)
951 {
ichizok378447f2023-05-11 22:25:42 +0100952 mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
dbivolaruab16ad32021-12-29 19:41:47 +0000953 raise(sigarg);
954 }
dbivolaru79a6e252022-01-23 16:41:14 +0000955 else
956 got_tstp = TRUE;
dbivolaruab16ad32021-12-29 19:41:47 +0000957
ichizok5f823d12022-03-13 17:27:38 +0000958#if !defined(__ANDROID__) && !defined(__OpenBSD__) && !defined(__DragonFly__)
959 // This is not required on all systems. On some systems (at least Android,
960 // OpenBSD, and DragonFlyBSD) this breaks suspending with CTRL-Z.
ichizok378447f2023-05-11 22:25:42 +0100961 mch_signal(SIGTSTP, sig_tstp);
xtkobacbef12e2022-02-27 12:31:52 +0000962#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000963}
964#endif
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966#if defined(SIGINT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100967 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968catch_sigint SIGDEFARG(sigarg)
969{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100970 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100971 mch_signal(SIGINT, catch_sigint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 got_int = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974#endif
975
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200976#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100977 static void
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200978catch_sigusr1 SIGDEFARG(sigarg)
979{
980 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100981 mch_signal(SIGUSR1, catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200982 got_sigusr1 = TRUE;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200983}
984#endif
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100987 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988catch_sigpwr SIGDEFARG(sigarg)
989{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100990 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100991 mch_signal(SIGPWR, catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 /*
993 * I'm not sure we get the SIGPWR signal when the system is really going
994 * down or when the batteries are almost empty. Just preserve the swap
995 * files and don't exit, that can't do any harm.
996 */
997 ml_sync_all(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998}
999#endif
1000
1001#ifdef SET_SIG_ALARM
1002/*
1003 * signal function for alarm().
1004 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001005 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006sig_alarm SIGDEFARG(sigarg)
1007{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001008 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 sig_alarm_called = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010}
1011#endif
1012
Bram Moolenaaredce7422019-01-20 18:39:30 +01001013#if (defined(HAVE_SETJMP_H) \
1014 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
1015 || defined(FEAT_LIBCALL))) \
1016 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001017# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +01001018
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001019// argument to SETJMP()
1020static JMP_BUF lc_jump_env;
1021
1022# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001023// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001024// Volatile because it is used in signal handlers.
1025static volatile sig_atomic_t lc_signal;
1026# endif
1027
1028// TRUE when lc_jump_env is valid.
1029// Volatile because it is used in signal handler deathtrap().
zeertzjq9b7d2a92022-08-26 10:33:54 +01001030static volatile sig_atomic_t lc_active = FALSE;
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001031
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032/*
1033 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001034 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001036 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 * Usage:
1038 * mch_startjmp();
1039 * if (SETJMP(lc_jump_env) != 0)
1040 * {
1041 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001042 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 * }
1044 * else
1045 * {
1046 * do_the_work;
1047 * mch_endjmp();
1048 * }
1049 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1050 * not return before the saved environment is used.
1051 * Returns OK for normal return, FAIL when the protected code caused a
1052 * problem and LONGJMP() was used.
1053 */
Bram Moolenaar113e1072019-01-20 15:30:40 +01001054 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001055mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001057# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 lc_active = TRUE;
1061}
1062
Bram Moolenaar113e1072019-01-20 15:30:40 +01001063 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001064mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
1066 lc_active = FALSE;
1067}
1068
Bram Moolenaar113e1072019-01-20 15:30:40 +01001069 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001070mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
1072# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001073 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1074 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 init_signal_stack();
1076# endif
1077}
1078#endif
1079
1080/*
1081 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001082 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001084 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1085 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001087 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088deathtrap SIGDEFARG(sigarg)
1089{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001090 static int entered = 0; // count the number of times we got here.
1091 // Note: when memory has been corrupted
1092 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093#ifdef SIGHASARG
1094 int i;
1095#endif
1096
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001097#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 /*
1099 * Catch a crash in protected code.
1100 * Restores the environment saved in lc_jump_env, which looks like
1101 * SETJMP() returns 1.
1102 */
1103 if (lc_active)
1104 {
1105# if defined(SIGHASARG)
1106 lc_signal = sigarg;
1107# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001108 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001110 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
1112#endif
1113
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001114#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001115# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001116 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1117 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1118 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001119 if (in_mch_delay && sigarg == SIGQUIT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001120 return;
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001121# endif
1122
Bram Moolenaar0f873732019-12-05 20:28:46 +01001123 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1124 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1125 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001126 if (entered == 0
1127 && (0
1128# ifdef SIGHUP
1129 || sigarg == SIGHUP
1130# endif
1131# ifdef SIGQUIT
1132 || sigarg == SIGQUIT
1133# endif
1134# ifdef SIGTERM
1135 || sigarg == SIGTERM
1136# endif
1137# ifdef SIGPWR
1138 || sigarg == SIGPWR
1139# endif
1140# ifdef SIGUSR1
1141 || sigarg == SIGUSR1
1142# endif
1143# ifdef SIGUSR2
1144 || sigarg == SIGUSR2
1145# endif
1146 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001147 && !vim_handle_signal(sigarg))
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001148 return;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001149#endif
1150
Bram Moolenaar0f873732019-12-05 20:28:46 +01001151 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 ++entered;
1153
Bram Moolenaar0f873732019-12-05 20:28:46 +01001154 // Executing autocommands is likely to use more stack space than we have
1155 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001156 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001159 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 set_vim_var_nr(VV_DYING, (long)entered);
1161#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001162 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001164#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001165 // Since we are now using the signal stack, need to reset the stack
1166 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 get_stack_limit();
1168#endif
1169
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001170#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001171 // This is for opening gdb the moment Vim crashes.
1172 // You need to manually adjust the file name and Vim executable name.
1173 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001174 {
1175# define VI_GDB_FILE "/tmp/vimgdb"
1176# define VIM_NAME "/usr/bin/vim"
1177 FILE *fp = fopen(VI_GDB_FILE, "w");
1178 if (fp)
1179 {
1180 fprintf(fp,
1181 "file %s\n"
1182 "attach %d\n"
1183 "set height 1000\n"
1184 "bt full\n"
1185 , VIM_NAME, getpid());
1186 fclose(fp);
1187 system("xterm -e gdb -x "VI_GDB_FILE);
1188 unlink(VI_GDB_FILE);
1189 }
1190 }
1191#endif
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001194 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 for (i = 0; signal_info[i].sig != -1; i++)
1196 if (sigarg == signal_info[i].sig)
1197 break;
1198 deadly_signal = sigarg;
1199#endif
1200
Bram Moolenaar0f873732019-12-05 20:28:46 +01001201 full_screen = FALSE; // don't write message to the GUI, it might be
1202 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 /*
1204 * If something goes wrong after entering here, we may get here again.
1205 * When this happens, give a message and try to exit nicely (resetting the
1206 * terminal mode, etc.)
1207 * When this happens twice, just exit, don't even try to give a message,
1208 * stack may be corrupt or something weird.
1209 * When this still happens again (or memory was corrupted in such a way
1210 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1211 */
1212 if (entered >= 3)
1213 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001214 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 may_core_dump();
1216 if (entered >= 4)
1217 _exit(8);
1218 exit(7);
1219 }
1220 if (entered == 2)
1221 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001222 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001223 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 out_flush();
1225 getout(1);
1226 }
1227
Bram Moolenaar0f873732019-12-05 20:28:46 +01001228 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001230 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 signal_info[i].name);
1232#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001233 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001235
Bram Moolenaar0f873732019-12-05 20:28:46 +01001236 // Preserve files and exit. This sets the really_exiting flag to prevent
1237 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001238 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
Bram Moolenaar0f873732019-12-05 20:28:46 +01001240 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001241
Bram Moolenaar009b2592004-10-24 19:18:58 +00001242#ifdef NBDEBUG
1243 reset_signals();
1244 may_core_dump();
1245 abort();
1246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247}
1248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001250 * Invoked after receiving SIGCONT. We don't know what happened while
1251 * sleeping, deal with part of that.
1252 */
1253 static void
1254after_sigcont(void)
1255{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001256 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1257 // again later.
1258 oldtitle_outdated = TRUE;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001259
Bram Moolenaar2e310482018-08-21 13:09:10 +02001260 settmode(TMODE_RAW);
1261 need_check_timestamps = TRUE;
1262 did_check_timestamps = FALSE;
1263}
1264
1265#if defined(SIGCONT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001266static void sigcont_handler SIGPROTOARG;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001267
1268/*
1269 * With multi-threading, suspending might not work immediately. Catch the
1270 * SIGCONT signal, which will be used as an indication whether the suspending
1271 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001272 *
1273 * On Linux, signal is not always handled immediately either.
1274 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001275 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001276 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001277 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001279static volatile sig_atomic_t sigcont_received;
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001280static void sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281
1282/*
1283 * signal handler for SIGCONT
1284 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001285 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286sigcont_handler SIGDEFARG(sigarg)
1287{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001288 if (in_mch_suspend)
1289 {
1290 sigcont_received = TRUE;
1291 }
1292 else
1293 {
1294 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1295 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1296 // back to a sane mode. We should redraw, but we can't really do that
1297 // in a signal handler, do a redraw later.
1298 after_sigcont();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001299 redraw_later(UPD_CLEAR);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001300 cursor_on_force();
1301 out_flush();
1302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303}
1304#endif
1305
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001306#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001307# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001308static void *clip_star_save = NULL;
1309static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001310# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001311
1312/*
1313 * Called when Vim is going to sleep or execute a shell command.
1314 * We can't respond to requests for the X selections. Lose them, otherwise
1315 * other applications will hang. But first copy the text to cut buffer 0.
1316 */
1317 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001318loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001319{
1320 if (clip_star.owned || clip_plus.owned)
1321 {
1322 x11_export_final_selection();
1323 if (clip_star.owned)
1324 clip_lose_selection(&clip_star);
1325 if (clip_plus.owned)
1326 clip_lose_selection(&clip_plus);
1327 if (x11_display != NULL)
1328 XFlush(x11_display);
1329 }
1330}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001331
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001332# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001333/*
1334 * Save clipboard text to restore later.
1335 */
1336 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001337save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001338{
1339 if (clip_star.owned)
1340 clip_star_save = get_register('*', TRUE);
1341 if (clip_plus.owned)
1342 clip_plus_save = get_register('+', TRUE);
1343}
1344
1345/*
1346 * Restore clipboard text if no one own the X selection.
1347 */
1348 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001349restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001350{
1351 if (clip_star_save != NULL)
1352 {
1353 if (!clip_gen_owner_exists(&clip_star))
1354 put_register('*', clip_star_save);
1355 else
1356 free_register(clip_star_save);
1357 clip_star_save = NULL;
1358 }
1359 if (clip_plus_save != NULL)
1360 {
1361 if (!clip_gen_owner_exists(&clip_plus))
1362 put_register('+', clip_plus_save);
1363 else
1364 free_register(clip_plus_save);
1365 clip_plus_save = NULL;
1366 }
1367}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001368# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001369#endif
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371/*
1372 * If the machine has job control, use it to suspend the program,
1373 * otherwise fake it by starting a new shell.
1374 */
1375 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001376mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001378 if (ignore_sigtstp)
1379 return;
1380
Bram Moolenaar041c7102020-05-30 18:14:57 +02001381#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001382 in_mch_suspend = TRUE;
1383
Bram Moolenaar0f873732019-12-05 20:28:46 +01001384 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001386 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
1388# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001389 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001391# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 sigcont_received = FALSE;
1393# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001394
Bram Moolenaar0f873732019-12-05 20:28:46 +01001395 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001396
1397# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001398 /*
1399 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001400 * immediately, but somehow not all the time, probably because it's handled
1401 * in another thread. Do not call pause() because there would be race
1402 * condition which would hang Vim if signal happened in between the test of
1403 * sigcont_received and the call to pause(). If signal is not yet received,
1404 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1405 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001406 */
1407 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001408 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001409
Bram Moolenaar262735e2009-07-14 10:20:22 +00001410 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001411 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001414 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
Bram Moolenaar2e310482018-08-21 13:09:10 +02001416 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417#else
1418 suspend_shell();
1419#endif
1420}
1421
1422 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001423mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
1425 Columns = 80;
1426 Rows = 24;
1427
1428 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001429
1430#ifdef SIGTSTP
1431 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1432 // that indicates the shell (or program) that launched us does not support
1433 // tty job control and thus we should ignore that signal. If invoked as a
1434 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
ichizok378447f2023-05-11 22:25:42 +01001435 ignore_sigtstp = restricted || SIG_IGN == mch_signal(SIGTSTP, SIG_ERR);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001438
Bram Moolenaar56718732006-03-15 22:53:57 +00001439#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001440 mac_conv_init();
1441#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001442#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1443 win_clip_init();
1444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445}
1446
1447 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001448set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
1450#if defined(SIGWINCH)
1451 /*
1452 * WINDOW CHANGE signal is handled with sig_winch().
1453 */
ichizok378447f2023-05-11 22:25:42 +01001454 mch_signal(SIGWINCH, sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#endif
1456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001458 // See mch_init() for the conditions under which we ignore SIGTSTP.
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001459 // In the GUI default TSTP processing is OK.
1460 // Checking both gui.in_use and gui.starting because gui.in_use is not set
1461 // at this point (set after menus are displayed), but gui.starting is set.
ichizok378447f2023-05-11 22:25:42 +01001462 mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001463# ifdef FEAT_GUI
1464 : gui.in_use || gui.starting ? SIG_DFL
1465# endif
ichizok378447f2023-05-11 22:25:42 +01001466 : sig_tstp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001468#if defined(SIGCONT)
ichizok378447f2023-05-11 22:25:42 +01001469 mch_signal(SIGCONT, sigcont_handler);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001471#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 /*
1473 * We want to ignore breaking of PIPEs.
1474 */
ichizok378447f2023-05-11 22:25:42 +01001475 mch_signal(SIGPIPE, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476#endif
1477
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001479 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480#endif
1481
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001482#ifdef SIGUSR1
1483 /*
1484 * Call user's handler on SIGUSR1
1485 */
ichizok378447f2023-05-11 22:25:42 +01001486 mch_signal(SIGUSR1, catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001487#endif
1488
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 /*
1490 * Ignore alarm signals (Perl's alarm() generates it).
1491 */
1492#ifdef SIGALRM
ichizok378447f2023-05-11 22:25:42 +01001493 mch_signal(SIGALRM, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494#endif
1495
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001496#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 /*
1498 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1499 * work will be lost.
1500 */
ichizok378447f2023-05-11 22:25:42 +01001501 mch_signal(SIGPWR, catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502#endif
1503
1504 /*
1505 * Arrange for other signals to gracefully shutdown Vim.
1506 */
1507 catch_signals(deathtrap, SIG_ERR);
1508
1509#if defined(FEAT_GUI) && defined(SIGHUP)
1510 /*
1511 * When the GUI is running, ignore the hangup signal.
1512 */
1513 if (gui.in_use)
ichizok378447f2023-05-11 22:25:42 +01001514 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515#endif
1516}
1517
Bram Moolenaardf177f62005-02-22 08:39:57 +00001518#if defined(SIGINT) || defined(PROTO)
1519/*
1520 * Catch CTRL-C (only works while in Cooked mode).
1521 */
1522 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001523catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001524{
ichizok378447f2023-05-11 22:25:42 +01001525 mch_signal(SIGINT, catch_sigint);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001526}
1527#endif
1528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001530reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001533#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001534 // SIGCONT isn't in the list, because its default action is ignore
ichizok378447f2023-05-11 22:25:42 +01001535 mch_signal(SIGCONT, SIG_DFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536#endif
1537}
1538
1539 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001540catch_signals(
Michael Jarvisbe9624e2023-04-19 20:28:48 +01001541 void (*func_deadly)(int),
1542 void (*func_other)(int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
1544 int i;
1545
1546 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (signal_info[i].deadly)
1549 {
1550#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1551 struct sigaction sa;
1552
Bram Moolenaar0f873732019-12-05 20:28:46 +01001553 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 sa.sa_handler = func_deadly;
1555 sigemptyset(&sa.sa_mask);
1556# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001557 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1558 // thread handling in combination with using the alternate stack:
1559 // pthread library functions try to use the stack pointer to
1560 // identify the current thread, causing a SEGV signal, which
1561 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 sa.sa_flags = 0;
1563# else
1564 sa.sa_flags = SA_ONSTACK;
1565# endif
1566 sigaction(signal_info[i].sig, &sa, NULL);
1567#else
1568# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1569 struct sigvec sv;
1570
Bram Moolenaar0f873732019-12-05 20:28:46 +01001571 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 sv.sv_handler = func_deadly;
1573 sv.sv_mask = 0;
1574 sv.sv_flags = SV_ONSTACK;
1575 sigvec(signal_info[i].sig, &sv, NULL);
1576# else
ichizok378447f2023-05-11 22:25:42 +01001577 mch_signal(signal_info[i].sig, func_deadly);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578# endif
1579#endif
1580 }
1581 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001582 {
1583 // Deal with non-deadly signals.
1584#ifdef SIGTSTP
ichizok378447f2023-05-11 22:25:42 +01001585 mch_signal(signal_info[i].sig,
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001586 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1587 ? SIG_IGN : func_other);
1588#else
ichizok378447f2023-05-11 22:25:42 +01001589 mch_signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001590#endif
1591 }
1592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593}
1594
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001595#ifdef HAVE_SIGPROCMASK
1596 static void
1597block_signals(sigset_t *set)
1598{
1599 sigset_t newset;
1600 int i;
1601
1602 sigemptyset(&newset);
1603
1604 for (i = 0; signal_info[i].sig != -1; i++)
1605 sigaddset(&newset, signal_info[i].sig);
1606
Bram Moolenaar2e310482018-08-21 13:09:10 +02001607# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001608 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001609 sigaddset(&newset, SIGCONT);
1610# endif
1611
1612 sigprocmask(SIG_BLOCK, &newset, set);
1613}
1614
1615 static void
1616unblock_signals(sigset_t *set)
1617{
1618 sigprocmask(SIG_SETMASK, set, NULL);
1619}
1620#endif
1621
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001623 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001624 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1625 * return TRUE
1626 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1627 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001628 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001629 * Returns TRUE when Vim should exit.
1630 */
1631 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001632vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001633{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001634 static int got_signal = 0;
1635 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001636
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001637 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001638 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001639 case SIGNAL_BLOCK: blocked = TRUE;
1640 break;
1641
1642 case SIGNAL_UNBLOCK: blocked = FALSE;
1643 if (got_signal != 0)
1644 {
1645 kill(getpid(), got_signal);
1646 got_signal = 0;
1647 }
1648 break;
1649
1650 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001651 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001652 got_signal = sig;
1653#ifdef SIGPWR
1654 if (sig != SIGPWR)
1655#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001656 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001657 break;
1658 }
1659 return FALSE;
1660}
1661
1662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 * Check_win checks whether we have an interactive stdout.
1664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001666mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 if (isatty(1))
1669 return OK;
1670 return FAIL;
1671}
1672
1673/*
1674 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1675 */
1676 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001677mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
1679 if (isatty(read_cmd_fd))
1680 return TRUE;
1681 return FALSE;
1682}
1683
1684#ifdef FEAT_X11
1685
Bram Moolenaar651fca82021-11-29 20:39:38 +00001686# if defined(ELAPSED_TIMEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688/*
1689 * Give a message about the elapsed time for opening the X window.
1690 */
1691 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001692xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001694 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695}
1696# endif
1697#endif
1698
Bram Moolenaar651fca82021-11-29 20:39:38 +00001699#if defined(FEAT_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700/*
1701 * A few functions shared by X11 title and clipboard code.
1702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
1704static int got_x_error = FALSE;
1705
1706/*
1707 * X Error handler, otherwise X just exits! (very rude) -- webb
1708 */
1709 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001710x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001712 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 STRCAT(IObuff, _("\nVim: Got X error\n"));
1714
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001715 // In the GUI we cannot print a message and continue, because no X calls
1716 // are allowed here (causes my system to hang). Silently continuing seems
1717 // like the best alternative. Do preserve files, in case we crash.
1718 ml_sync_all(FALSE, FALSE);
1719
1720#ifdef FEAT_GUI
1721 if (!gui.in_use)
1722#endif
1723 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
Bram Moolenaar0f873732019-12-05 20:28:46 +01001725 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726}
1727
1728/*
1729 * Another X Error handler, just used to check for errors.
1730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001732x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
1734 got_x_error = TRUE;
1735 return 0;
1736}
1737
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001738/*
1739 * Return TRUE when connection to the X server is desired.
1740 */
1741 static int
1742x_connect_to_server(void)
1743{
1744 // No point in connecting if we are exiting or dying.
1745 if (exiting || v_dying)
1746 return FALSE;
1747
1748#if defined(FEAT_CLIENTSERVER)
1749 if (x_force_connect)
1750 return TRUE;
1751#endif
1752 if (x_no_connect)
1753 return FALSE;
1754
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001755 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001756 if (clip_exclude_prog != NULL)
1757 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001758 // Just in case we get called recursively, return FALSE. This could
1759 // happen if vpeekc() is used while executing the prog and it causes a
1760 // related callback to be invoked.
1761 if (regprog_in_use(clip_exclude_prog))
1762 return FALSE;
1763
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001764 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1765 return FALSE;
1766 }
1767 return TRUE;
1768}
1769
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001771# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772/*
1773 * An X IO Error handler, used to catch error while opening the display.
1774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001776x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001778 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001780# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001781 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783}
1784# endif
1785
1786/*
1787 * An X IO Error handler, used to catch terminal errors.
1788 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001789static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001792x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
1794 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001795 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 x11_window = 0;
1797 x11_display = NULL;
1798 xterm_Shell = (Widget)0;
1799
Bram Moolenaar0f873732019-12-05 20:28:46 +01001800 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001802# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001803 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001806
1807/*
1808 * If the X11 connection was lost try to restore it.
1809 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001810 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001811 */
1812 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001813may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001814{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001815 // No point in restoring the connecting if we are exiting or dying.
1816 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001817 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001818 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001819
1820# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001821 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001822 if (app_context != (XtAppContext)NULL)
1823 {
1824 XtDestroyApplicationContext(app_context);
1825 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001826 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001827 }
1828# endif
1829
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001830 setup_term_clip();
1831 get_x11_title(FALSE);
1832 }
1833}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001834
1835 void
1836ex_xrestore(exarg_T *eap)
1837{
1838 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1839 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001840 if (xterm_display_allocated)
1841 vim_free(xterm_display);
1842 xterm_display = (char *)vim_strsave(eap->arg);
1843 xterm_display_allocated = TRUE;
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001844 }
1845 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001846 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001847
1848 clear_xterm_clip();
1849 x11_window = 0;
1850 xterm_dpy_retry_count = 5; // Try reconnecting five times
1851 may_restore_clipboard();
1852}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853#endif
1854
1855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 * Test if "dpy" and x11_window are valid by getting the window title.
1857 * I don't actually want it yet, so there may be a simpler call to use, but
1858 * this will cause the error handler x_error_check() to be called if anything
1859 * is wrong, such as the window pointer being invalid (as can happen when the
1860 * user changes his DISPLAY, but not his WINDOWID) -- webb
1861 */
1862 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001863test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864{
Dominique Pellé4927bc72023-09-24 16:12:07 +02001865 int (*old_handler)(Display*, XErrorEvent*);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 XTextProperty text_prop;
1867
1868 old_handler = XSetErrorHandler(x_error_check);
1869 got_x_error = FALSE;
1870 if (XGetWMName(dpy, x11_window, &text_prop))
1871 XFree((void *)text_prop.value);
1872 XSync(dpy, False);
1873 (void)XSetErrorHandler(old_handler);
1874
1875 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001876 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 return (got_x_error ? FAIL : OK);
1879}
1880#endif
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
1883#ifdef FEAT_X11
1884
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001885static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886
1887/*
1888 * try to get x11 window and display
1889 *
1890 * return FAIL for failure, OK otherwise
1891 */
1892 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001893get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 char *winid;
1896 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001897#define XD_NONE 0 // x11_display not set here
1898#define XD_HERE 1 // x11_display opened here
1899#define XD_GUI 2 // x11_display used from gui.dpy
1900#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 static int x11_display_from = XD_NONE;
1902 static int did_set_error_handler = FALSE;
1903
1904 if (!did_set_error_handler)
1905 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001906 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 (void)XSetErrorHandler(x_error_handler);
1908 did_set_error_handler = TRUE;
1909 }
1910
Bram Moolenaar9372a112005-12-06 19:59:18 +00001911#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (gui.in_use)
1913 {
1914 /*
1915 * If the X11 display was opened here before, for the window where Vim
1916 * was started, close that one now to avoid a memory leak.
1917 */
1918 if (x11_display_from == XD_HERE && x11_display != NULL)
1919 {
1920 XCloseDisplay(x11_display);
1921 x11_display_from = XD_NONE;
1922 }
1923 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1924 {
1925 x11_display_from = XD_GUI;
1926 return OK;
1927 }
1928 x11_display = NULL;
1929 return FAIL;
1930 }
1931 else if (x11_display_from == XD_GUI)
1932 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001933 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 x11_window = 0;
1935 x11_display = NULL;
1936 x11_display_from = XD_NONE;
1937 }
1938#endif
1939
Bram Moolenaar0f873732019-12-05 20:28:46 +01001940 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (!x_connect_to_server())
1942 return FAIL;
1943
1944 /*
1945 * If WINDOWID not set, should try another method to find out
1946 * what the current window number is. The only code I know for
1947 * this is very complicated.
1948 * We assume that zero is invalid for WINDOWID.
1949 */
1950 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1951 x11_window = (Window)atol(winid);
1952
1953#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001954 if (xterm_dpy == x11_display)
1955 // x11_display may have been set to xterm_dpy elsewhere
1956 x11_display_from = XD_XTERM;
1957
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 if (xterm_dpy != NULL && x11_window != 0)
1959 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001960 // We may have checked it already, but Gnome terminal can move us to
1961 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001962 if (x11_display_from != XD_XTERM)
1963 {
1964 /*
1965 * If the X11 display was opened here before, for the window where
1966 * Vim was started, close that one now to avoid a memory leak.
1967 */
1968 if (x11_display_from == XD_HERE && x11_display != NULL)
1969 XCloseDisplay(x11_display);
1970 x11_display = xterm_dpy;
1971 x11_display_from = XD_XTERM;
1972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 if (test_x11_window(x11_display) == FAIL)
1974 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001975 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 x11_window = 0;
1977 x11_display = NULL;
1978 x11_display_from = XD_NONE;
1979 return FAIL;
1980 }
1981 return OK;
1982 }
1983#endif
1984
1985 if (x11_window == 0 || x11_display == NULL)
1986 result = -1;
1987
Bram Moolenaar0f873732019-12-05 20:28:46 +01001988 if (result != -1) // Have already been here and set this
1989 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990
1991 if (x11_window != 0 && x11_display == NULL)
1992 {
1993#ifdef SET_SIG_ALARM
ichizok378447f2023-05-11 22:25:42 +01001994 sighandler_T sig_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001996#ifdef ELAPSED_FUNC
1997 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998
1999 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01002000 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001#endif
2002
2003#ifdef SET_SIG_ALARM
2004 /*
2005 * Opening the Display may hang if the DISPLAY setting is wrong, or
2006 * the network connection is bad. Set an alarm timer to get out.
2007 */
2008 sig_alarm_called = FALSE;
ichizok378447f2023-05-11 22:25:42 +01002009 sig_save = mch_signal(SIGALRM, sig_alarm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 alarm(2);
2011#endif
2012 x11_display = XOpenDisplay(NULL);
2013
2014#ifdef SET_SIG_ALARM
2015 alarm(0);
ichizok378447f2023-05-11 22:25:42 +01002016 mch_signal(SIGALRM, sig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01002018 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019#endif
2020 if (x11_display != NULL)
2021 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002022# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00002024 {
2025 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002026 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00002027 verbose_leave();
2028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029# endif
2030 if (test_x11_window(x11_display) == FAIL)
2031 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002032 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 x11_window = 0;
2034 XCloseDisplay(x11_display);
2035 x11_display = NULL;
2036 }
2037 else
2038 x11_display_from = XD_HERE;
2039 }
2040 }
2041 if (x11_window == 0 || x11_display == NULL)
2042 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02002043
2044# ifdef FEAT_EVAL
2045 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
2046# endif
2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 return (result = OK);
2049}
2050
2051/*
2052 * Determine original x11 Window Title
2053 */
2054 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002055get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056{
Bram Moolenaar47136d72004-10-12 20:02:24 +00002057 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058}
2059
2060/*
2061 * Determine original x11 Window icon
2062 */
2063 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002064get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
2066 int retval = FALSE;
2067
2068 retval = get_x11_thing(FALSE, test_only);
2069
Bram Moolenaar0f873732019-12-05 20:28:46 +01002070 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (oldicon == NULL && !test_only)
2072 {
2073 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002074 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002076 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 }
2078
2079 return retval;
2080}
2081
2082 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002083get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002084 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01002085 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086{
2087 XTextProperty text_prop;
2088 int retval = FALSE;
2089 Status status;
2090
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002091 if (get_x11_windis() != OK)
2092 return FALSE;
2093
2094 // Get window/icon name if any
2095 if (get_title)
2096 status = XGetWMName(x11_display, x11_window, &text_prop);
2097 else
2098 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2099
2100 /*
2101 * If terminal is xterm, then x11_window may be a child window of the
2102 * outer xterm window that actually contains the window/icon name, so
2103 * keep traversing up the tree until a window with a title/icon is
2104 * found.
2105 */
2106 // Previously this was only done for xterm and alike. I don't see a
2107 // reason why it would fail for other terminal emulators.
2108 // if (term_is_xterm)
2109 Window root;
2110 Window parent;
2111 Window win = x11_window;
2112 Window *children;
2113 unsigned int num_children;
2114
2115 while (!status || text_prop.value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002117 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2118 &num_children))
2119 break;
2120 if (children)
2121 XFree((void *)children);
2122 if (parent == root || parent == 0)
2123 break;
2124
2125 win = parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002127 status = XGetWMName(x11_display, win, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002129 status = XGetWMIconName(x11_display, win, &text_prop);
2130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002132 if (status && text_prop.value != NULL)
2133 {
2134 retval = TRUE;
2135 if (!test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002137 if (get_title)
2138 vim_free(oldtitle);
2139 else
2140 vim_free(oldicon);
2141 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002144 oldtitle = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002146 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002148 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002150 char **cl;
2151 Status transform_status;
2152 int n = 0;
2153
2154 transform_status = XmbTextPropertyToTextList(x11_display,
2155 &text_prop,
2156 &cl, &n);
2157 if (transform_status >= Success && n > 0 && cl[0])
2158 {
2159 if (get_title)
2160 oldtitle = vim_strsave((char_u *) cl[0]);
2161 else
2162 oldicon = vim_strsave((char_u *) cl[0]);
2163 XFreeStringList(cl);
2164 }
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002165 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 if (get_title)
2168 oldtitle = vim_strsave((char_u *)text_prop.value);
2169 else
2170 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002174 XFree((void *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 }
2176 return retval;
2177}
2178
Bram Moolenaar0f873732019-12-05 20:28:46 +01002179// Xutf8 functions are not available on older systems. Note that on some
2180// systems X_HAVE_UTF8_STRING may be defined in a header file but
2181// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2182// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002183#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002184# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185# define USE_UTF8_STRING
2186# endif
2187#endif
2188
2189/*
2190 * Set x11 Window Title
2191 *
2192 * get_x11_windis() must be called before this and have returned OK
2193 */
2194 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002195set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002197 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2198 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2199 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200#ifdef USE_UTF8_STRING
2201 if (enc_utf8)
2202 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2203 NULL, NULL, 0, NULL, NULL, NULL);
2204 else
2205#endif
2206 {
2207#if XtSpecificationRelease >= 4
2208# ifdef FEAT_XFONTSET
2209 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2210 NULL, NULL, 0, NULL, NULL, NULL);
2211# else
2212 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002213 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214
Bram Moolenaar0f873732019-12-05 20:28:46 +01002215 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002216 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 XSetWMProperties(x11_display, x11_window, &text_prop,
2218 NULL, NULL, 0, NULL, NULL, NULL);
2219# endif
2220#else
2221 XStoreName(x11_display, x11_window, (char *)title);
2222#endif
2223 }
2224 XFlush(x11_display);
2225}
2226
2227/*
2228 * Set x11 Window icon
2229 *
2230 * get_x11_windis() must be called before this and have returned OK
2231 */
2232 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002233set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002235 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236#ifdef USE_UTF8_STRING
2237 if (enc_utf8)
2238 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2239 NULL, 0, NULL, NULL, NULL);
2240 else
2241#endif
2242 {
2243#if XtSpecificationRelease >= 4
2244# ifdef FEAT_XFONTSET
2245 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2246 NULL, 0, NULL, NULL, NULL);
2247# else
2248 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002249 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002251 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2253 NULL, 0, NULL, NULL, NULL);
2254# endif
2255#else
2256 XSetIconName(x11_display, x11_window, (char *)icon);
2257#endif
2258 }
2259 XFlush(x11_display);
2260}
2261
Bram Moolenaar0f873732019-12-05 20:28:46 +01002262#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002265get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 return FALSE;
2268}
2269
2270 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002271get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
2273 if (!test_only)
2274 {
2275 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002276 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002278 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280 return FALSE;
2281}
2282
Bram Moolenaar0f873732019-12-05 20:28:46 +01002283#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
2285 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002286mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288 return get_x11_title(TRUE);
2289}
2290
2291 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002292mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
2294 return get_x11_icon(TRUE);
2295}
2296
2297/*
2298 * Set the window title and icon.
2299 */
2300 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002301mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302{
2303 int type = 0;
2304 static int recursive = 0;
2305
Bram Moolenaar0f873732019-12-05 20:28:46 +01002306 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002308 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 return;
2310
Bram Moolenaar0f873732019-12-05 20:28:46 +01002311 // When one of the X11 functions causes a deadly signal, we get here again
2312 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (recursive)
2314 return;
2315 ++recursive;
2316
2317 /*
2318 * if the window ID and the display is known, we may use X11 calls
2319 */
2320#ifdef FEAT_X11
2321 if (get_x11_windis() == OK)
2322 type = 1;
lilydjwg6e0a18f2024-01-29 20:54:28 +01002323#endif
2324#if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002325 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 if (gui.in_use)
2327 type = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328#endif
2329
2330 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002331 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 * than x11 calls, because the x11 calls don't always work
2333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if ((type || *T_TS != NUL) && title != NULL)
2335 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002336 if (oldtitle_outdated)
2337 {
2338 oldtitle_outdated = FALSE;
2339 VIM_CLEAR(oldtitle);
2340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (oldtitle == NULL
2342#ifdef FEAT_GUI
2343 && !gui.in_use
2344#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002345 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 (void)get_x11_title(FALSE);
2347
Bram Moolenaar0f873732019-12-05 20:28:46 +01002348 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 term_settitle(title);
2350#ifdef FEAT_X11
2351 else
2352# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002353 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002355 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002357#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002358 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 else
2360 gui_mch_settitle(title, icon);
2361#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002362 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
2364
2365 if ((type || *T_CIS != NUL) && icon != NULL)
2366 {
2367 if (oldicon == NULL
2368#ifdef FEAT_GUI
2369 && !gui.in_use
2370#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002371 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 get_x11_icon(FALSE);
2373
2374 if (*T_CIS != NUL)
2375 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002376 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002378 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 out_flush();
2380 }
2381#ifdef FEAT_X11
2382 else
2383# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002384 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002386 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387#endif
2388 did_set_icon = TRUE;
2389 }
2390 --recursive;
2391}
2392
2393/*
2394 * Restore the window/icon title.
2395 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002396 * SAVE_RESTORE_TITLE only restore title
2397 * SAVE_RESTORE_ICON only restore icon
2398 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 */
2400 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002401mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
Bram Moolenaardac13472019-09-16 21:06:21 +02002403 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002404
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002405 // Only restore the title or icon when it has been set.
2406 // When using "oldtitle" make a copy, it might be freed halfway.
2407 char_u *title = ((which & SAVE_RESTORE_TITLE) && unix_did_set_title)
2408 ? (oldtitle ? oldtitle : p_titleold) : NULL;
2409 char_u *tofree = NULL;
2410 if (title == oldtitle && oldtitle != NULL)
2411 {
2412 tofree = vim_strsave(title);
2413 if (tofree != NULL)
2414 title = tofree;
2415 }
2416 mch_settitle(title,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002417 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002418 vim_free(tofree);
Bram Moolenaar40385db2018-08-07 22:31:44 +02002419
Bram Moolenaare5c83282019-05-03 23:15:37 +02002420 if (do_push_pop)
2421 {
2422 // pop and push from/to the stack
2423 term_pop_title(which);
2424 term_push_title(which);
2425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426}
2427
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429/*
2430 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002431 * This matches "xterm.*", thus "xterm-256color", "xterm-kitty", etc.
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002432 * Do not consider "xterm-kitty" an xterm, it is not fully xterm compatible,
2433 * using the "xterm-kitty" terminfo entry should work better.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002434 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 */
2436 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002437vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 if (name == NULL)
2440 return FALSE;
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002441 return ((STRNICMP(name, "xterm", 5) == 0
2442 && STRNICMP(name, "xterm-kitty", 11) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 || STRNICMP(name, "nxterm", 6) == 0
2444 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002445 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002447 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 || STRCMP(name, "builtin_xterm") == 0);
2449}
2450
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002451#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2452/*
2453 * Return TRUE if "name" appears to be that of a terminal
2454 * known to support the xterm-style mouse protocol.
2455 * Relies on term_is_xterm having been set to its correct value.
2456 */
2457 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002458use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002459{
2460 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002461 && (term_is_xterm
2462 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002463 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002464 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002465 || STRICMP(name, "st") == 0
2466 || STRNICMP(name, "st-", 3) == 0
2467 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002468}
2469#endif
2470
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471/*
2472 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2473 * Return 1 for "xterm".
2474 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002475 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002476 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 */
2478 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002479use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002481 if (ttym_flags == TTYM_SGR)
2482 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002483 if (ttym_flags == TTYM_URXVT)
2484 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (ttym_flags == TTYM_XTERM2)
2486 return 2;
2487 if (ttym_flags == TTYM_XTERM)
2488 return 1;
2489 return 0;
2490}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002492/*
2493 * Return TRUE if "name" is an iris-ansi terminal name.
2494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002496vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 if (name == NULL)
2499 return FALSE;
2500 return (STRNICMP(name, "iris-ansi", 9) == 0
2501 || STRCMP(name, "builtin_iris-ansi") == 0);
2502}
2503
Dominique Pellee764d1b2023-03-12 21:20:59 +00002504#if defined(VMS) || defined(PROTO)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002505/*
2506 * Return TRUE if "name" is a vt300-like terminal name.
2507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002509vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
2511 if (name == NULL)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002512 return FALSE;
2513 // Actually all ANSI compatible terminals should be here.
2514 // Catch at least VT1xx - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002515 return ((STRNICMP(name, "vt", 2) == 0
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002516 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 || STRCMP(name, "builtin_vt320") == 0);
2518}
Dominique Pellee764d1b2023-03-12 21:20:59 +00002519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520
2521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 * Insert user name in s[len].
2523 * Return OK if a name found.
2524 */
2525 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002526mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
2528#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002529 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 return OK;
2531#else
2532 return mch_get_uname(getuid(), s, len);
2533#endif
2534}
2535
2536/*
2537 * Insert user name for "uid" in s[len].
2538 * Return OK if a name found.
2539 */
2540 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002541mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542{
2543#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2544 struct passwd *pw;
2545
2546 if ((pw = getpwuid(uid)) != NULL
2547 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2548 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002549 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 return OK;
2551 }
2552#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002553 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2554 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555}
2556
2557/*
2558 * Insert host name is s[len].
2559 */
2560
2561#ifdef HAVE_SYS_UTSNAME_H
2562 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002563mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
2565 struct utsname vutsname;
2566
2567 if (uname(&vutsname) < 0)
2568 *s = NUL;
2569 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002570 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002572#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
2574# ifdef HAVE_SYS_SYSTEMINFO_H
2575# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2576# endif
2577
2578 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002579mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
2581# ifdef VAXC
2582 vaxc$gethostname((char *)s, len);
2583# else
2584 gethostname((char *)s, len);
2585# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002586 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002588#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
2590/*
2591 * return process ID
2592 */
2593 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002594mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
2596 return (long)getpid();
2597}
2598
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002599/*
2600 * return TRUE if process "pid" is still running
2601 */
2602 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002603mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002604{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002605 // If there is no error the process must be running.
2606 if (kill(pid, 0) == 0)
2607 return TRUE;
2608#ifdef ESRCH
2609 // If the error is ESRCH then the process is not running.
2610 if (errno == ESRCH)
2611 return FALSE;
2612#endif
2613 // If the process is running and owned by another user we get EPERM. With
2614 // other errors the process might be running, assuming it is then.
2615 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002616}
2617
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002620strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621{
2622 extern int sys_nerr;
2623 extern char *sys_errlist[];
2624 static char er[20];
2625
2626 if (err > 0 && err < sys_nerr)
2627 return (sys_errlist[err]);
2628 sprintf(er, "Error %d", err);
2629 return er;
2630}
2631#endif
2632
2633/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002634 * Get name of current directory into buffer "buf" of length "len" bytes.
2635 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 * Return OK for success, FAIL for failure.
2637 */
2638 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002639mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
2641#if defined(USE_GETCWD)
2642 if (getcwd((char *)buf, len) == NULL)
2643 {
2644 STRCPY(buf, strerror(errno));
2645 return FAIL;
2646 }
2647 return OK;
2648#else
2649 return (getwd((char *)buf) != NULL ? OK : FAIL);
2650#endif
2651}
2652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002654 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 *
2656 * return FAIL for failure, OK for success
2657 */
2658 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002659mch_FullName(
2660 char_u *fname,
2661 char_u *buf,
2662 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002663 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664{
2665 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002666#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002668 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 char_u olddir[MAXPATHL];
2671 char_u *p;
2672 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002673#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002674 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2675 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002676#endif
2677
Bram Moolenaar38323e42007-03-06 19:22:53 +00002678#ifdef VMS
2679 fname = vms_fixfilename(fname);
2680#endif
2681
Bram Moolenaara2442432007-04-26 14:26:37 +00002682#ifdef __CYGWIN__
2683 /*
2684 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2685 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002686# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002687 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2688 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002689 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2690 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002691# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002692 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002693# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002694 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002695#endif
2696
Bram Moolenaar0f873732019-12-05 20:28:46 +01002697 // Expand it if forced or not an absolute path.
2698 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002699 if ((force || !mch_isFullName(fname))
2700 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
2702 /*
2703 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002704 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 * This will get the correct path name with "../" things.
2706 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002707 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002709 if (STRCMP(p, "/..") == 0)
2710 // for "/path/dir/.." include the "/.."
2711 p += 3;
2712
Bram Moolenaar38323e42007-03-06 19:22:53 +00002713#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 /*
2715 * Use fchdir() if possible, it's said to be faster and more
2716 * reliable. But on SunOS 4 it might not work. Check this by
2717 * doing a fchdir() right now.
2718 */
2719 if (!dont_fchdir)
2720 {
2721 fd = open(".", O_RDONLY | O_EXTRA, 0);
2722 if (fd >= 0 && fchdir(fd) < 0)
2723 {
2724 close(fd);
2725 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002726 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
Bram Moolenaar0f873732019-12-05 20:28:46 +01002731 // Only change directory when we are sure we can return to where
2732 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002734#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 (mch_dirname(olddir, MAXPATHL) == FAIL
2738 || mch_chdir((char *)olddir) != 0))
2739 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002740 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 retval = FAIL;
2742 }
2743 else
2744 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002745 // The directory is copied into buf[], to be able to remove
2746 // the file name without changing it (could be a string in
2747 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 if (p - fname >= len)
2749 retval = FAIL;
2750 else
2751 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002752 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002754 {
2755 // Path does not exist (yet). For a full path fail,
2756 // will use the path as-is. For a relative path use
2757 // the current directory and append the file name.
2758 if (mch_isFullName(fname))
2759 retval = FAIL;
2760 else
2761 p = NULL;
2762 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002763 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002765 else
2766 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 *buf = NUL;
2768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
2770 }
2771 if (mch_dirname(buf, len) == FAIL)
2772 {
2773 retval = FAIL;
2774 *buf = NUL;
2775 }
2776 if (p != NULL)
2777 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002778#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (fd >= 0)
2780 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002781 if (p_verbose >= 5)
2782 {
2783 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002784 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002785 verbose_leave();
2786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
2789 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 l = mch_chdir((char *)olddir);
2792 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002793 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 }
itchyny051a40c2021-10-20 10:00:05 +01002795#ifdef HAVE_FCHDIR
2796 if (fd >= 0)
2797 close(fd);
2798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002801 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002802 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002803#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002804 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002806 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002809
Bram Moolenaar0f873732019-12-05 20:28:46 +01002810 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002811 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 return FAIL;
2813
Bram Moolenaar0f873732019-12-05 20:28:46 +01002814 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 if (STRCMP(fname, ".") != 0)
2816 STRCAT(buf, fname);
2817
2818 return OK;
2819}
2820
2821/*
2822 * Return TRUE if "fname" does not depend on the current directory.
2823 */
2824 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002825mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002827#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 return ( fname[0] == '/' || fname[0] == '.' ||
2829 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2830 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2831 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002832#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834#endif
2835}
2836
Bram Moolenaar24552be2005-12-10 20:17:30 +00002837#if defined(USE_FNAME_CASE) || defined(PROTO)
2838/*
2839 * Set the case of the file name, if it already exists. This will cause the
2840 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002841 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002842 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002843 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002844fname_case(
2845 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002846 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002847{
2848 struct stat st;
2849 char_u *slash, *tail;
2850 DIR *dirp;
2851 struct dirent *dp;
2852
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002853 if (mch_lstat((char *)name, &st) < 0)
2854 return;
2855
2856 // Open the directory where the file is located.
2857 slash = vim_strrchr(name, '/');
2858 if (slash == NULL)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002859 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002860 dirp = opendir(".");
2861 tail = name;
2862 }
2863 else
2864 {
2865 *slash = NUL;
2866 dirp = opendir((char *)name);
2867 *slash = '/';
2868 tail = slash + 1;
2869 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002870
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002871 if (dirp == NULL)
2872 return;
2873
2874 while ((dp = readdir(dirp)) != NULL)
2875 {
2876 // Only accept names that differ in case and are the same byte
2877 // length. TODO: accept different length name.
2878 if (STRICMP(tail, dp->d_name) == 0
2879 && STRLEN(tail) == STRLEN(dp->d_name))
Bram Moolenaar24552be2005-12-10 20:17:30 +00002880 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002881 char_u newname[MAXPATHL + 1];
2882 struct stat st2;
2883
2884 // Verify the inode is equal.
2885 vim_strncpy(newname, name, MAXPATHL);
2886 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2887 MAXPATHL - (tail - name));
2888 if (mch_lstat((char *)newname, &st2) >= 0
2889 && st.st_ino == st2.st_ino
2890 && st.st_dev == st2.st_dev)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002891 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002892 STRCPY(tail, dp->d_name);
2893 break;
Bram Moolenaar24552be2005-12-10 20:17:30 +00002894 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002895 }
2896 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002897
2898 closedir(dirp);
Bram Moolenaar24552be2005-12-10 20:17:30 +00002899}
2900#endif
2901
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902/*
2903 * Get file permissions for 'name'.
2904 * Returns -1 when it doesn't exist.
2905 */
2906 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002907mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908{
2909 struct stat statb;
2910
Bram Moolenaar0f873732019-12-05 20:28:46 +01002911 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912#ifdef VMS
2913 if (stat((char *)vms_fixfilename(name), &statb))
2914#else
2915 if (stat((char *)name, &statb))
2916#endif
2917 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002918#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002919 // The top bit makes the value negative, which means the file doesn't
2920 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002921 return statb.st_mode & ~S_ADDACE;
2922#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925}
2926
2927/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002928 * Set file permission for "name" to "perm".
2929 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 */
2931 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002932mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
2934 return (chmod((char *)
2935#ifdef VMS
2936 vms_fixfilename(name),
2937#else
2938 name,
2939#endif
2940 (mode_t)perm) == 0 ? OK : FAIL);
2941}
2942
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002943#if defined(HAVE_FCHMOD) || defined(PROTO)
2944/*
2945 * Set file permission for open file "fd" to "perm".
2946 * Return FAIL for failure, OK otherwise.
2947 */
2948 int
2949mch_fsetperm(int fd, long perm)
2950{
2951 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2952}
2953#endif
2954
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955#if defined(HAVE_ACL) || defined(PROTO)
2956# ifdef HAVE_SYS_ACL_H
2957# include <sys/acl.h>
2958# endif
2959# ifdef HAVE_SYS_ACCESS_H
2960# include <sys/access.h>
2961# endif
2962
2963# ifdef HAVE_SOLARIS_ACL
2964typedef struct vim_acl_solaris_T {
2965 int acl_cnt;
2966 aclent_t *acl_entry;
2967} vim_acl_solaris_T;
2968# endif
2969
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002970#if defined(HAVE_SELINUX) || defined(PROTO)
2971/*
2972 * Copy security info from "from_file" to "to_file".
2973 */
2974 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002975mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002976{
2977 if (from_file == NULL)
2978 return;
2979
2980 if (selinux_enabled == -1)
2981 selinux_enabled = is_selinux_enabled();
2982
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002983 if (selinux_enabled <= 0)
2984 return;
2985
2986 // Use "char *" instead of "security_context_t" to avoid a deprecation
2987 // warning.
2988 char *from_context = NULL;
2989 char *to_context = NULL;
2990
2991 if (getfilecon((char *)from_file, &from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002992 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002993 // If the filesystem doesn't support extended attributes,
2994 // the original had no special security context and the
2995 // target cannot have one either.
2996 if (errno == EOPNOTSUPP)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002997 return;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002998
2999 msg_puts(_("\nCould not get security context for "));
3000 msg_outtrans(from_file);
3001 msg_putchar('\n');
3002 return;
3003 }
3004 if (getfilecon((char *)to_file, &to_context) < 0)
3005 {
3006 msg_puts(_("\nCould not get security context for "));
3007 msg_outtrans(to_file);
3008 msg_putchar('\n');
3009 freecon (from_context);
3010 return ;
3011 }
3012 if (strcmp(from_context, to_context) != 0)
3013 {
3014 if (setfilecon((char *)to_file, from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003015 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003016 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003017 msg_outtrans(to_file);
3018 msg_putchar('\n');
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003019 }
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003020 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003021 freecon(to_context);
3022 freecon(from_context);
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003023}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003024#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003025
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003026#if defined(HAVE_SMACK) && !defined(PROTO)
3027/*
3028 * Copy security info from "from_file" to "to_file".
3029 */
3030 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01003031mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003032{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02003033 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003034 {
3035 XATTR_NAME_SMACK,
3036 XATTR_NAME_SMACKEXEC,
3037 XATTR_NAME_SMACKMMAP
3038 };
3039
3040 char buffer[SMACK_LABEL_LEN];
3041 const char *name;
3042 int index;
3043 int ret;
3044 ssize_t size;
3045
3046 if (from_file == NULL)
3047 return;
3048
Christian Brabandt5a679b22023-10-16 10:17:13 +02003049 size = listxattr((char *)from_file, NULL, 0);
3050 // not supported or no attributes to copy
Paul R. Tagliamonte14759de2024-03-10 08:35:10 +01003051 if (size <= 0)
Christian Brabandt5a679b22023-10-16 10:17:13 +02003052 return;
3053
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003054 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
3055 / sizeof(smack_copied_attributes)[0]) ; index++)
3056 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003057 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003058 name = smack_copied_attributes[index];
3059
Bram Moolenaar0f873732019-12-05 20:28:46 +01003060 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003061 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
3062 if (size >= 0)
3063 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003064 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003065 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
3066 if (ret < 0)
3067 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003068 vim_snprintf((char *)IObuff, IOSIZE,
3069 _("Could not set security context %s for %s"),
3070 name, to_file);
3071 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003072 msg_putchar('\n');
3073 }
3074 }
3075 else
3076 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003077 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003078 switch (errno)
3079 {
3080 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003081 // extended attributes aren't supported or enabled
3082 // should a message be echoed? not sure...
3083 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003084
3085 case ERANGE:
3086 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003087 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003088 vim_snprintf((char *)IObuff, IOSIZE,
3089 _("Could not get security context %s for %s. Removing it!"),
3090 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003091 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003092 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003093 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003094
3095 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003096 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003097 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003098 // Silently ignore errors, apparently this happens when
3099 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003100 break;
3101 }
3102 }
3103 }
3104}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003105#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003106
Christian Brabandte085dfd2023-09-30 12:49:18 +02003107#ifdef FEAT_XATTR
3108/*
3109 * Copy extended attributes from_file to to_file
3110 */
3111 void
3112mch_copy_xattr(char_u *from_file, char_u *to_file)
3113{
3114 char *xattr_buf;
Paul R. Tagliamonte14759de2024-03-10 08:35:10 +01003115 ssize_t size;
3116 ssize_t tsize;
Christian Brabandte085dfd2023-09-30 12:49:18 +02003117 ssize_t keylen, vallen, max_vallen = 0;
3118 char *key;
3119 char *val = NULL;
3120 char *errmsg = NULL;
3121
3122 if (from_file == NULL)
3123 return;
3124
3125 // get the length of the extended attributes
3126 size = listxattr((char *)from_file, NULL, 0);
3127 // not supported or no attributes to copy
Paul R. Tagliamonte14759de2024-03-10 08:35:10 +01003128 if (size <= 0)
Christian Brabandte085dfd2023-09-30 12:49:18 +02003129 return;
3130 xattr_buf = (char*)alloc(size);
3131 if (xattr_buf == NULL)
3132 return;
3133 size = listxattr((char *)from_file, xattr_buf, size);
3134 tsize = size;
3135
3136 errno = 0;
3137
3138 for (int round = 0; round < 2; round++)
3139 {
3140
3141 key = xattr_buf;
3142 if (round == 1)
3143 size = tsize;
3144
3145 while (size > 0)
3146 {
3147 vallen = getxattr((char *)from_file, key,
3148 val, round ? max_vallen : 0);
3149 // only set the attribute in the second round
3150 if (vallen >= 0 && round &&
3151 setxattr((char *)to_file, key, val, vallen, 0) == 0)
3152 ;
3153 else if (errno)
3154 {
3155 switch (errno)
3156 {
3157 case E2BIG:
3158 errmsg = e_xattr_e2big;
3159 goto error_exit;
3160 case ENOTSUP:
Gene C993b1752023-10-02 22:42:26 +02003161 case EACCES:
3162 case EPERM:
3163 break;
Christian Brabandte085dfd2023-09-30 12:49:18 +02003164 case ERANGE:
3165 errmsg = e_xattr_erange;
3166 goto error_exit;
3167 default:
3168 errmsg = e_xattr_other;
3169 goto error_exit;
3170 }
3171 }
3172
3173 if (round == 0 && vallen > max_vallen)
3174 max_vallen = vallen;
3175
3176 // add one for terminating null
3177 keylen = STRLEN(key) + 1;
3178 size -= keylen;
3179 key += keylen;
3180 }
3181 if (round)
3182 break;
3183
3184 val = (char*)alloc(max_vallen + 1);
3185 if (val == NULL)
3186 goto error_exit;
3187
3188 }
3189error_exit:
3190 vim_free(xattr_buf);
3191 vim_free(val);
3192
3193 if (errmsg != NULL)
zeertzjq7ece0362023-10-01 09:07:14 +02003194 emsg(_(errmsg));
Christian Brabandte085dfd2023-09-30 12:49:18 +02003195}
3196#endif
3197
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198/*
3199 * Return a pointer to the ACL of file "fname" in allocated memory.
3200 * Return NULL if the ACL is not available for whatever reason.
3201 */
3202 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003203mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204{
3205 vim_acl_T ret = NULL;
3206#ifdef HAVE_POSIX_ACL
3207 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3208#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003209#ifdef HAVE_SOLARIS_ZFS_ACL
3210 acl_t *aclent;
3211
3212 if (acl_get((char *)fname, 0, &aclent) < 0)
3213 return NULL;
3214 ret = (vim_acl_T)aclent;
3215#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216#ifdef HAVE_SOLARIS_ACL
3217 vim_acl_solaris_T *aclent;
3218
3219 aclent = malloc(sizeof(vim_acl_solaris_T));
3220 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3221 {
3222 free(aclent);
3223 return NULL;
3224 }
3225 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3226 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3227 {
3228 free(aclent->acl_entry);
3229 free(aclent);
3230 return NULL;
3231 }
3232 ret = (vim_acl_T)aclent;
3233#else
3234#if defined(HAVE_AIX_ACL)
3235 int aclsize;
3236 struct acl *aclent;
3237
3238 aclsize = sizeof(struct acl);
3239 aclent = malloc(aclsize);
3240 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3241 {
3242 if (errno == ENOSPC)
3243 {
3244 aclsize = aclent->acl_len;
3245 aclent = realloc(aclent, aclsize);
3246 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3247 {
3248 free(aclent);
3249 return NULL;
3250 }
3251 }
3252 else
3253 {
3254 free(aclent);
3255 return NULL;
3256 }
3257 }
3258 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003259#endif // HAVE_AIX_ACL
3260#endif // HAVE_SOLARIS_ACL
3261#endif // HAVE_SOLARIS_ZFS_ACL
3262#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 return ret;
3264}
3265
3266/*
3267 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3268 */
3269 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003270mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272 if (aclent == NULL)
3273 return;
3274#ifdef HAVE_POSIX_ACL
3275 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3276#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003277#ifdef HAVE_SOLARIS_ZFS_ACL
3278 acl_set((char *)fname, (acl_t *)aclent);
3279#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280#ifdef HAVE_SOLARIS_ACL
3281 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3282 ((vim_acl_solaris_T *)aclent)->acl_entry);
3283#else
3284#ifdef HAVE_AIX_ACL
3285 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003286#endif // HAVE_AIX_ACL
3287#endif // HAVE_SOLARIS_ACL
3288#endif // HAVE_SOLARIS_ZFS_ACL
3289#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290}
3291
3292 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003293mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 if (aclent == NULL)
3296 return;
3297#ifdef HAVE_POSIX_ACL
3298 acl_free((acl_t)aclent);
3299#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003300#ifdef HAVE_SOLARIS_ZFS_ACL
3301 acl_free((acl_t *)aclent);
3302#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303#ifdef HAVE_SOLARIS_ACL
3304 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3305 free(aclent);
3306#else
3307#ifdef HAVE_AIX_ACL
3308 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003309#endif // HAVE_AIX_ACL
3310#endif // HAVE_SOLARIS_ACL
3311#endif // HAVE_SOLARIS_ZFS_ACL
3312#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313}
3314#endif
3315
3316/*
3317 * Set hidden flag for "name".
3318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003320mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003322 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323}
3324
3325/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003326 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 * return FALSE if "name" is not a directory
3328 * return FALSE for error
3329 */
3330 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003331mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
3333 struct stat statb;
3334
Bram Moolenaar0f873732019-12-05 20:28:46 +01003335 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 return FALSE;
3337 if (stat((char *)name, &statb))
3338 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340}
3341
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003342/*
3343 * return TRUE if "name" is a directory, NOT a symlink to a directory
3344 * return FALSE if "name" is not a directory
3345 * return FALSE for error
3346 */
3347 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003348mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003349{
3350 struct stat statb;
3351
Bram Moolenaar0f873732019-12-05 20:28:46 +01003352 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003353 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003354 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003355 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003356 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003357}
3358
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359/*
3360 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3361 */
3362 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003363executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 struct stat st;
3366
3367 if (stat((char *)name, &st))
3368 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003369#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003370 // Like on Unix system file can have executable rights but not necessarily
3371 // be an executable, but on Unix is not a default for an ordinary file to
3372 // have an executable flag - on VMS it is in most cases.
3373 // Therefore, this check does not have any sense - let keep us to the
3374 // conventions instead:
3375 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003376 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003377 int vms_executable = 0;
3378 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3379 {
3380 if (strstr(vms_tolower((char*)name),".exe") != NULL
3381 || strstr(vms_tolower((char*)name),".com")!= NULL)
3382 vms_executable = 1;
3383 }
3384 return vms_executable;
3385#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388}
3389
3390/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003391 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003392 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 * Return -1 if unknown.
3394 */
3395 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003396mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397{
3398 char_u *buf;
3399 char_u *p, *e;
3400 int retval;
3401
Bram Moolenaar0f873732019-12-05 20:28:46 +01003402 // When "use_path" is false and if it's an absolute or relative path don't
3403 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003404 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003405 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003406 // There must be a path separator, files in the current directory
3407 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003408 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003409 {
3410 if (path != NULL)
3411 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003412 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003413 *path = FullName_save(name, TRUE);
3414 else
3415 *path = vim_strsave(name);
3416 }
3417 return TRUE;
3418 }
3419 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
3422 p = (char_u *)getenv("PATH");
3423 if (p == NULL || *p == NUL)
3424 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003425 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (buf == NULL)
3427 return -1;
3428
3429 /*
3430 * Walk through all entries in $PATH to check if "name" exists there and
3431 * is an executable file.
3432 */
3433 for (;;)
3434 {
3435 e = (char_u *)strchr((char *)p, ':');
3436 if (e == NULL)
3437 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003438 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 STRCPY(buf, "./");
3440 else
3441 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003442 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 add_pathsep(buf);
3444 }
3445 STRCAT(buf, name);
3446 retval = executable_file(buf);
3447 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003448 {
3449 if (path != NULL)
3450 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003451 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003452 *path = FullName_save(buf, TRUE);
3453 else
3454 *path = vim_strsave(buf);
3455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
3459 if (*e != ':')
3460 break;
3461 p = e + 1;
3462 }
3463
3464 vim_free(buf);
3465 return retval;
3466}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
3468/*
3469 * Check what "name" is:
3470 * NODE_NORMAL: file or directory (or doesn't exist)
3471 * NODE_WRITABLE: writable device, socket, fifo, etc.
3472 * NODE_OTHER: non-writable things
3473 */
3474 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003475mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
3477 struct stat st;
3478
3479 if (stat((char *)name, &st))
3480 return NODE_NORMAL;
3481 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3482 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003483 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003485 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 return NODE_WRITABLE;
3487}
3488
3489 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003490mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491{
3492#ifdef HAVE_CHECK_STACK_GROWTH
3493 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 check_stack_growth((char *)&i);
3496
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003497# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 get_stack_limit();
3499# endif
3500
3501#endif
3502
3503 /*
3504 * Setup an alternative stack for signals. Helps to catch signals when
3505 * running out of stack space.
3506 * Use of sigaltstack() is preferred, it's more portable.
3507 * Ignore any errors.
3508 */
3509#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003510 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 init_signal_stack();
3512#endif
3513}
3514
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003515#if defined(EXITFREE) || defined(PROTO)
3516 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003517mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003518{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003519# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3520 if (clip_star.owned)
3521 clip_lose_selection(&clip_star);
3522 if (clip_plus.owned)
3523 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003524# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003525# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003526 if (xterm_Shell != (Widget)0)
3527 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003528# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003529 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003530 if (xterm_dpy != NULL)
3531 XtCloseDisplay(xterm_dpy);
3532 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003533 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003534 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003535# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003536 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003537# endif
3538 }
3539# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003540# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003541# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003542 if (x11_display != NULL
3543# ifdef FEAT_XCLIPBOARD
3544 && x11_display != xterm_dpy
3545# endif
3546 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003547 XCloseDisplay(x11_display);
3548# endif
3549# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003550 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003551# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003552 vim_free(oldtitle);
3553 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003554}
3555#endif
3556
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557/*
3558 * Output a newline when exiting.
3559 * Make sure the newline goes to the same stream as the text.
3560 */
3561 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003562exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003564 if (silent_mode)
3565 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 if (newline_on_exit || msg_didout)
3567 {
3568 if (msg_use_printf())
3569 {
3570 if (info_message)
3571 mch_msg("\n");
3572 else
3573 mch_errmsg("\r\n");
3574 }
3575 else
3576 out_char('\n');
3577 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003578 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003580 restore_cterm_colors(); // get original colors back
3581 msg_clr_eos_force(); // clear the rest of the display
3582 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
3584}
3585
Bram Moolenaarb4151682020-05-11 22:13:28 +02003586#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003587# if (defined(__GNUC__) \
3588 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3589 || (defined(__clang__) && (__clang_major__ >= 12))
3590extern void __gcov_dump(void);
3591extern void __gcov_reset(void);
3592# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3593# else
3594extern void __gcov_flush(void);
3595# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003596#endif
3597
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003599mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600{
3601 exiting = TRUE;
3602
3603#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3604 x11_export_final_selection();
3605#endif
3606
3607#ifdef FEAT_GUI
3608 if (!gui.in_use)
3609#endif
3610 {
3611 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003612 if (!is_not_a_term())
3613 {
3614 // restore xterm title and icon name
3615 mch_restore_title(SAVE_RESTORE_BOTH);
3616 term_pop_title(SAVE_RESTORE_BOTH);
3617 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 /*
3620 * When t_ti is not empty but it doesn't cause swapping terminal
3621 * pages, need to output a newline when msg_didout is set. But when
3622 * t_ti does swap pages it should not go to the shell page. Do this
3623 * before stoptermcap().
3624 */
3625 if (swapping_screen() && !newline_on_exit)
3626 exit_scroll();
3627
Bram Moolenaar0f873732019-12-05 20:28:46 +01003628 // Stop termcap: May need to check for T_CRV response, which
3629 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 stoptermcap();
3631
3632 /*
3633 * A newline is only required after a message in the alternate screen.
3634 * This is set to TRUE by wait_return().
3635 */
3636 if (!swapping_screen() || newline_on_exit)
3637 exit_scroll();
3638
Bram Moolenaar0f873732019-12-05 20:28:46 +01003639 // Cursor may have been switched off without calling starttermcap()
3640 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 if (full_screen)
3642 cursor_on();
3643 }
3644 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003645 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003646
3647#ifdef USE_GCOV_FLUSH
3648 // Flush coverage info before possibly being killed by a deadly signal.
3649 __gcov_flush();
3650#endif
3651
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 may_core_dump();
3653#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 gui_exit(r);
3656#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003657
Bram Moolenaar56718732006-03-15 22:53:57 +00003658#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003659 mac_conv_cleanup();
3660#endif
3661
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003663 // A core dump won't be created if the signal handler
3664 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 if (deadly_signal != 0)
3666 return;
3667#endif
3668
Bram Moolenaar009b2592004-10-24 19:18:58 +00003669#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003670 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003671#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003672
3673#ifdef EXITFREE
3674 free_all_mem();
3675#endif
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 exit(r);
3678}
3679
3680 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003681may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682{
3683 if (deadly_signal != 0)
3684 {
ichizok378447f2023-05-11 22:25:42 +01003685 mch_signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003686 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 }
3688}
3689
3690#ifndef VMS
3691
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003692/*
3693 * Get the file descriptor to use for tty operations.
3694 */
3695 static int
3696get_tty_fd(int fd)
3697{
3698 int tty_fd = fd;
3699
3700#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3701 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3702 // "fd" when it is a master device.
3703 if (mch_isatty(fd) > 1)
3704 {
3705 char *name;
3706
3707 name = ptsname(fd);
3708 if (name == NULL)
3709 return -1;
3710
3711 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3712 if (tty_fd < 0)
3713 return -1;
3714 }
3715#endif
3716 return tty_fd;
3717}
3718
3719 static int
3720mch_tcgetattr(int fd, void *term)
3721{
3722 int tty_fd;
3723 int retval = -1;
3724
3725 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003726 if (tty_fd < 0)
3727 return -1;
3728
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003729#ifdef NEW_TTY_SYSTEM
3730# ifdef HAVE_TERMIOS_H
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003731 retval = tcgetattr(tty_fd, (struct termios *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003732# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003733 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003734# endif
3735#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003736 // for "old" tty systems
3737 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003738#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003739 if (tty_fd != fd)
3740 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003741 return retval;
3742}
3743
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003745mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
3747 static int first = TRUE;
3748
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003749#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750# ifdef HAVE_TERMIOS_H
3751 static struct termios told;
3752 struct termios tnew;
3753# else
3754 static struct termio told;
3755 struct termio tnew;
3756# endif
3757
3758 if (first)
3759 {
3760 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003761 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
3763
3764 tnew = told;
3765 if (tmode == TMODE_RAW)
3766 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003767 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003768 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
Anton Sharonov49528da2024-04-14 20:02:24 +02003769 tnew.c_iflag &= ~(ICRNL |
3770 (T_XON == NULL || *T_XON == NUL ? IXON : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003772# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003773 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774# endif
3775 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003776# ifdef ONLCR
3777 // Don't map NL -> CR NL, we do it ourselves.
3778 // Also disable expanding tabs if possible.
3779# ifdef XTABS
3780 tnew.c_oflag &= ~(ONLCR | XTABS);
3781# else
3782# ifdef TAB3
3783 tnew.c_oflag &= ~(ONLCR | TAB3);
3784# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003786# endif
3787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003789 tnew.c_cc[VMIN] = 1; // return after 1 char
3790 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003793 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003794 // Also reset ICANON here, otherwise on Solaris select() won't see
3795 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003796 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003797 tnew.c_cc[VMIN] = 1; // return after 1 char
3798 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800
3801# if defined(HAVE_TERMIOS_H)
3802 {
3803 int n = 10;
3804
Bram Moolenaar0f873732019-12-05 20:28:46 +01003805 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3806 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3808 && errno == EINTR && n > 0)
3809 --n;
3810 }
3811# else
3812 ioctl(read_cmd_fd, TCSETA, &tnew);
3813# endif
3814
3815#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 /*
3817 * for "old" tty systems
3818 */
3819# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003820# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821# endif
3822 static struct sgttyb ttybold;
3823 struct sgttyb ttybnew;
3824
3825 if (first)
3826 {
3827 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003828 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830
3831 ttybnew = ttybold;
3832 if (tmode == TMODE_RAW)
3833 {
3834 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3835 ttybnew.sg_flags |= RAW;
3836 }
3837 else if (tmode == TMODE_SLEEP)
3838 ttybnew.sg_flags &= ~(ECHO);
3839 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3840#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003841 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842}
3843
3844/*
3845 * Try to get the code for "t_kb" from the stty setting
3846 *
3847 * Even if termcap claims a backspace key, the user's setting *should*
3848 * prevail. stty knows more about reality than termcap does, and if
3849 * somebody's usual erase key is DEL (which, for most BSD users, it will
3850 * be), they're going to get really annoyed if their erase key starts
3851 * doing forward deletes for no reason. (Eric Fischer)
3852 */
3853 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003854get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003856 ttyinfo_T info;
3857 char_u buf[2];
3858 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003860 if (get_tty_info(read_cmd_fd, &info) != OK)
3861 return;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003862
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003863 intr_char = info.interrupt;
3864 buf[0] = info.backspace;
3865 buf[1] = NUL;
3866 add_termcode((char_u *)"kb", buf, FALSE);
3867
3868 // If <BS> and <DEL> are now the same, redefine <DEL>.
3869 p = find_termcode((char_u *)"kD");
3870 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3871 do_fixdel(NULL);
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003872}
3873
3874/*
3875 * Obtain the characters that Backspace and Enter produce on "fd".
3876 * Returns OK or FAIL.
3877 */
3878 int
3879get_tty_info(int fd, ttyinfo_T *info)
3880{
3881#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882# ifdef HAVE_TERMIOS_H
3883 struct termios keys;
3884# else
3885 struct termio keys;
3886# endif
3887
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003888 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003890 info->backspace = keys.c_cc[VERASE];
3891 info->interrupt = keys.c_cc[VINTR];
3892 if (keys.c_iflag & ICRNL)
3893 info->enter = NL;
3894 else
3895 info->enter = CAR;
3896 if (keys.c_oflag & ONLCR)
3897 info->nl_does_cr = TRUE;
3898 else
3899 info->nl_does_cr = FALSE;
3900 return OK;
3901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003903 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 struct sgttyb keys;
3905
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003906 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003908 info->backspace = keys.sg_erase;
3909 info->interrupt = keys.sg_kill;
3910 info->enter = CAR;
3911 info->nl_does_cr = TRUE;
3912 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003915 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916}
3917
Bram Moolenaar0f873732019-12-05 20:28:46 +01003918#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003920static int mouse_ison = FALSE;
3921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922/*
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003923 * Set mouse clicks on or off and possible enable mouse movement events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 */
3925 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003926mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003928#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003929 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 int xterm_mouse_vers;
3932
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003933#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003934 if (!on)
3935 // Make sure not tracing mouse movements. Important when a button-down
3936 // was received but no release yet.
3937 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003938#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003939
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003940 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003941#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003942 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003943#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003944 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003945 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 return;
3947
3948 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003949
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003950#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003951 if (ttym_flags == TTYM_URXVT)
3952 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003953 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003954 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003955 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003956#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003957
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00003958 if (T_CXM != NULL && *T_CXM != NUL)
3959 {
3960 term_enable_mouse(on);
3961 }
3962 else if (ttym_flags == TTYM_SGR)
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003963 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003964 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003965 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003966 mouse_ison = on;
3967 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003968
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003969#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003970 if (bevalterm_ison != (p_bevalterm && on))
3971 {
3972 bevalterm_ison = (p_bevalterm && on);
3973 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003974 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003975 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003976 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003977#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003978
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (xterm_mouse_vers > 0)
3980 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003981 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 out_str_nf((char_u *)
3983 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003984 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003985#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003986 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003987#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003988 "\033[?1002h")
3989 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003990 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003992 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003993 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
3995
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003996#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 else if (ttym_flags == TTYM_DEC)
3998 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003999 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01004001 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004003 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004007#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 else
4009 {
4010 if (on)
4011 {
4012 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004013 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
4015 else
4016 {
4017 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004018 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 }
4020 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004023#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004024 else
4025 {
4026 if (on)
4027 {
4028 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004029 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004030 }
4031 else
4032 {
4033 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004034 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004035 }
4036 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004037#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004038
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004039#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 else
4041 {
4042 if (on)
4043 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004044 // D - Enable Mouse up/down messages
4045 // L - Enable Left Button Reporting
4046 // M - Enable Middle Button Reporting
4047 // R - Enable Right Button Reporting
4048 // K - Enable SHIFT and CTRL key Reporting
4049 // + - Enable Advanced messaging of mouse moves and up/down messages
4050 // Q - Quiet No Ack
4051 // # - Numeric value of mouse pointer required
4052 // 0 = Multiview 2000 cursor, used as standard
4053 // 1 = Windows Arrow
4054 // 2 = Windows I Beam
4055 // 3 = Windows Hour Glass
4056 // 4 = Windows Cross Hair
4057 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004058# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01004059 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004060 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004061# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004062 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004063# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004064 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
4066 else
4067 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004068 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004069 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004072#endif
4073#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 else
4075 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004076 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 if (on)
4078 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
4079 else
4080 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004081 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084}
4085
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004086#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004087/*
4088 * Called when 'balloonevalterm' changed.
4089 */
4090 void
4091mch_bevalterm_changed(void)
4092{
4093 mch_setmouse(mouse_ison);
4094}
4095#endif
4096
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097/*
4098 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
4099 */
4100 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004101check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102{
4103# ifdef FEAT_MOUSE_XTERM
4104 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02004105# ifdef FEAT_MOUSE_URXVT
4106 && use_xterm_mouse() != 3
4107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108# ifdef FEAT_GUI
4109 && !gui.in_use
4110# endif
4111 )
4112 {
4113 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004114 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 if (*p_mouse != NUL)
4116 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004117 // force mouse off and maybe on to send possibly new mouse
4118 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 mch_setmouse(FALSE);
4120 setmouse();
4121 }
4122 }
4123 else
4124 del_mouse_termcode(KS_MOUSE);
4125# endif
4126
4127# ifdef FEAT_MOUSE_GPM
4128 if (!use_xterm_mouse()
4129# ifdef FEAT_GUI
4130 && !gui.in_use
4131# endif
4132 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004133 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02004134 else
4135 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136# endif
4137
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004138# ifdef FEAT_SYSMOUSE
4139 if (!use_xterm_mouse()
4140# ifdef FEAT_GUI
4141 && !gui.in_use
4142# endif
4143 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004144 set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004145# endif
4146
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01004148 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 if (!use_xterm_mouse()
4150# ifdef FEAT_GUI
4151 && !gui.in_use
4152# endif
4153 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004154 set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 else
4156 del_mouse_termcode(KS_JSBTERM_MOUSE);
4157# endif
4158
4159# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01004160 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
4161 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 if (!use_xterm_mouse()
4163# ifdef FEAT_GUI
4164 && !gui.in_use
4165# endif
4166 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004167 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 else
4169 del_mouse_termcode(KS_NETTERM_MOUSE);
4170# endif
4171
4172# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01004173 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004174 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175# ifdef FEAT_GUI
4176 && !gui.in_use
4177# endif
4178 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004179 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004180 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 else
4182 del_mouse_termcode(KS_DEC_MOUSE);
4183# endif
4184# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004185 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004186 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187# ifdef FEAT_GUI
4188 && !gui.in_use
4189# endif
4190 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004191 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 else
4193 del_mouse_termcode(KS_PTERM_MOUSE);
4194# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004195# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004196 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004197# ifdef FEAT_GUI
4198 && !gui.in_use
4199# endif
4200 )
4201 {
4202 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004203 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004204
4205 if (*p_mouse != NUL)
4206 {
4207 mch_setmouse(FALSE);
4208 setmouse();
4209 }
4210 }
4211 else
4212 del_mouse_termcode(KS_URXVT_MOUSE);
4213# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004214 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004215# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004216 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004217# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004218 )
4219 {
4220 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004221 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004222
4223 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004224 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004225
4226 if (*p_mouse != NUL)
4227 {
4228 mch_setmouse(FALSE);
4229 setmouse();
4230 }
4231 }
4232 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004233 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004234 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004235 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239#ifndef VMS
4240
4241/*
4242 * Try to get the current window size:
4243 * 1. with an ioctl(), most accurate method
4244 * 2. from the environment variables LINES and COLUMNS
4245 * 3. from the termcap
4246 * 4. keep using the old values
4247 * Return OK when size could be determined, FAIL otherwise.
4248 */
4249 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004250mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
4252 long rows = 0;
4253 long columns = 0;
4254 char_u *p;
4255
4256 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 * 1. try using an ioctl. It is the most accurate method.
4258 *
4259 * Try using TIOCGWINSZ first, some systems that have it also define
4260 * TIOCGSIZE but don't have a struct ttysize.
4261 */
4262# ifdef TIOCGWINSZ
4263 {
4264 struct winsize ws;
4265 int fd = 1;
4266
Bram Moolenaar0f873732019-12-05 20:28:46 +01004267 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 if (!isatty(fd) && isatty(read_cmd_fd))
4269 fd = read_cmd_fd;
4270 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4271 {
4272 columns = ws.ws_col;
4273 rows = ws.ws_row;
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004274# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004275 ch_log(NULL, "Got size with TIOCGWINSZ: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004279# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280# ifdef TIOCGSIZE
4281 {
4282 struct ttysize ts;
4283 int fd = 1;
4284
Bram Moolenaar0f873732019-12-05 20:28:46 +01004285 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 if (!isatty(fd) && isatty(read_cmd_fd))
4287 fd = read_cmd_fd;
4288 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4289 {
4290 columns = ts.ts_cols;
4291 rows = ts.ts_lines;
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004292# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004293 ch_log(NULL, "Got size with TIOCGSIZE: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004294# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004297# endif // TIOCGSIZE
4298# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299
4300 /*
4301 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004302 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4303 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004305 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
4307 if ((p = (char_u *)getenv("LINES")))
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004308 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 rows = atoi((char *)p);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004310# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004311 ch_log(NULL, "Got 'lines' from $LINES: %ld", rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004312# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 if ((p = (char_u *)getenv("COLUMNS")))
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 columns = atoi((char *)p);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004317# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004318 ch_log(NULL, "Got 'columns' from $COLUMNS: %ld", columns);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004319# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322
4323#ifdef HAVE_TGETENT
4324 /*
4325 * 3. try reading "co" and "li" entries from termcap
4326 */
4327 if (columns == 0 || rows == 0)
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 getlinecol(&columns, &rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004330# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004331 ch_log(NULL, "Got size from termcap: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004332# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334#endif
4335
4336 /*
4337 * 4. If everything fails, use the old values
4338 */
4339 if (columns <= 0 || rows <= 0)
4340 return FAIL;
4341
4342 Rows = rows;
4343 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004344 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 return OK;
4346}
4347
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004348#if defined(FEAT_TERMINAL) || defined(PROTO)
4349/*
4350 * Report the windows size "rows" and "cols" to tty "fd".
4351 */
4352 int
4353mch_report_winsize(int fd, int rows, int cols)
4354{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004355 int tty_fd;
4356 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004357
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004358 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004359 if (tty_fd < 0)
4360 return FAIL;
4361
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004362# if defined(TIOCSWINSZ)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004363 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004364
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004365 ws.ws_col = cols;
4366 ws.ws_row = rows;
4367 ws.ws_xpixel = cols * 5;
4368 ws.ws_ypixel = rows * 10;
4369 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004370 ch_log(NULL, "ioctl(TIOCSWINSZ) %s", retval == 0 ? "success" : "failed");
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004371# elif defined(TIOCSSIZE)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004372 struct ttysize ts;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004373
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004374 ts.ts_cols = cols;
4375 ts.ts_lines = rows;
4376 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004377 ch_log(NULL, "ioctl(TIOCSSIZE) %s", retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004378# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004379 if (tty_fd != fd)
4380 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004381 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004382}
4383#endif
4384
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385/*
4386 * Try to set the window size to Rows and Columns.
4387 */
4388 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004389mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390{
4391 if (*T_CWS)
4392 {
4393 /*
4394 * NOTE: if you get an error here that term_set_winsize() is
4395 * undefined, check the output of configure. It could probably not
4396 * find a ncurses, termcap or termlib library.
4397 */
4398 term_set_winsize((int)Rows, (int)Columns);
4399 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004400 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
4402}
4403
Bram Moolenaar0f873732019-12-05 20:28:46 +01004404#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
4406/*
4407 * Rows and/or Columns has changed.
4408 */
4409 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004410mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004412 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413}
4414
Bram Moolenaar205b8862011-09-07 15:04:31 +02004415/*
4416 * Wait for process "child" to end.
4417 * Return "child" if it exited properly, <= 0 on error.
4418 */
4419 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004420wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004421{
4422 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004423 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004424
4425 while (wait_pid != child)
4426 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004427 // When compiled with Python threads are probably used, in which case
4428 // wait() sometimes hangs for no obvious reason. Use waitpid()
4429 // instead and loop (like the GUI). Also needed for other interfaces,
4430 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004431# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004432 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004433# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004434 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004435# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004436 if (wait_pid == 0)
4437 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004438 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004439 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004440 if (++delay_msec > 10)
4441 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004442 continue;
4443 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004444 if (wait_pid <= 0
4445# ifdef ECHILD
4446 && errno == ECHILD
4447# endif
4448 )
4449 break;
4450 }
4451 return wait_pid;
4452}
4453
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004454#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004455/*
4456 * Set the environment for a child process.
4457 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004458 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004459set_child_environment(
4460 long rows,
4461 long columns,
4462 char *term,
4463 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004464{
4465# ifdef HAVE_SETENV
4466 char envbuf[50];
4467# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004468 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004469 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004470 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004471 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004472 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004473# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004474 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004475# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004476# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004477 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004478# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004479# endif
4480
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004481# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004482 setenv("TERM", term, 1);
4483 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004484 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004485 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004486 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004487 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004488 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004489 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004490 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004491# ifdef FEAT_TERMINAL
4492 if (is_terminal)
4493 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004494 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004495 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4496 }
4497# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004498# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004499 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004500# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004501# else
4502 /*
4503 * Putenv does not copy the string, it has to remain valid.
4504 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004505 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004506 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004507 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4508 putenv(envbuf_Term);
4509 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004510 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004511 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4512 putenv(envbuf_Lines);
4513 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4514 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004515 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004516 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004517 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004518# ifdef FEAT_TERMINAL
4519 if (is_terminal)
4520 {
4521 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004522 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004523 putenv(envbuf_Version);
4524 }
4525# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004526# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004527 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4528 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4529 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004530# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004531# endif
4532}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004533
4534 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004535set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004536{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004537 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004538}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004539#endif
4540
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004541#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004542/*
4543 * Open a PTY, with FD for the master and slave side.
4544 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004545 * When successful both file descriptors are stored and the allocated pty name
4546 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004547 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004548 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004549open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004550{
4551 char *tty_name;
4552
Bram Moolenaar59386482019-02-10 22:43:46 +01004553 if (name1 != NULL)
4554 *name1 = NULL;
4555 if (name2 != NULL)
4556 *name2 = NULL;
4557
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004558 *pty_master_fd = mch_openpty(&tty_name); // open pty
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004559 if (*pty_master_fd < 0)
4560 return;
4561
4562 // Leaving out O_NOCTTY may lead to waitpid() always returning
4563 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4564 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004565#ifdef O_NOCTTY
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004566 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004567#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004568 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004569#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004570 if (*pty_slave_fd < 0)
4571 {
4572 close(*pty_master_fd);
4573 *pty_master_fd = -1;
4574 }
4575 else
4576 {
4577 if (name1 != NULL)
4578 *name1 = vim_strsave((char_u *)tty_name);
4579 if (name2 != NULL)
4580 *name2 = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004581 }
4582}
4583#endif
4584
Bram Moolenaarfae42832017-08-01 22:24:26 +02004585/*
4586 * Send SIGINT to a child process if "c" is an interrupt character.
4587 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004588 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004589may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4590{
4591# ifdef SIGINT
4592 if (c == Ctrl_C || c == intr_char)
4593 {
4594# ifdef HAVE_SETSID
4595 kill(-pid, SIGINT);
4596# else
4597 kill(0, SIGINT);
4598# endif
4599 if (wpid > 0)
4600 kill(wpid, SIGINT);
4601 }
4602# endif
4603}
4604
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004605#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004606
Bram Moolenaar0f873732019-12-05 20:28:46 +01004607/*
4608 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4609 * pointers, the last one is NULL.
4610 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4611 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004612 int
4613unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004614 char_u *cmd,
4615 char ***argvp,
4616 char_u **sh_tofree,
4617 char_u **shcf_tofree)
4618{
4619 char **argv = NULL;
4620 int argc;
4621
4622 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004623 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004624 return FAIL;
4625
4626 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4627 return FAIL;
4628 *argvp = argv;
4629
4630 if (cmd != NULL)
4631 {
4632 char_u *s;
4633 char_u *p;
4634
4635 if (extra_shell_arg != NULL)
4636 argv[argc++] = (char *)extra_shell_arg;
4637
Bram Moolenaar0f873732019-12-05 20:28:46 +01004638 // Break 'shellcmdflag' into white separated parts. This doesn't
4639 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004640 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004641 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004642 return FAIL;
4643 s = *shcf_tofree;
4644 p = p_shcf;
4645 while (*p != NUL)
4646 {
4647 argv[argc++] = (char *)s;
4648 while (*p && *p != ' ' && *p != TAB)
4649 *s++ = *p++;
4650 *s++ = NUL;
4651 p = skipwhite(p);
4652 }
4653
4654 argv[argc++] = (char *)cmd;
4655 }
4656 argv[argc] = NULL;
4657 return OK;
4658}
4659#endif
4660
4661#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4662/*
4663 * Use a terminal window to run a shell command in.
4664 */
4665 static int
4666mch_call_shell_terminal(
4667 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004668 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004669{
4670 jobopt_T opt;
4671 char **argv = NULL;
4672 char_u *tofree1 = NULL;
4673 char_u *tofree2 = NULL;
4674 int retval = -1;
4675 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004676 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004677 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004678 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004679
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004680 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004681 goto theend;
4682
4683 init_job_options(&opt);
4684 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4685 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004686 if (buf == NULL)
4687 goto theend;
4688
4689 job = term_getjob(buf->b_term);
4690 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004691
Bram Moolenaar0f873732019-12-05 20:28:46 +01004692 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004693 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004694 if (curbuf == buf)
Bram Moolenaar13568252018-03-16 20:46:58 +01004695 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004696 // Only when managed to find a window for "buf",
4697 clear_oparg(&oa);
4698 while (term_use_loop())
Bram Moolenaar13568252018-03-16 20:46:58 +01004699 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004700 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4701 {
4702 // If terminal_loop() returns OK we got a key that is handled
4703 // in Normal model. We don't do redrawing anyway.
4704 if (terminal_loop(TRUE) == OK)
4705 normal_cmd(&oa, TRUE);
4706 }
4707 else
Bram Moolenaar13568252018-03-16 20:46:58 +01004708 normal_cmd(&oa, TRUE);
4709 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00004710 retval = job->jv_exitval;
4711 ch_log(NULL, "system command finished");
4712
4713 job_unref(job);
4714
4715 // restore curwin/curbuf and a few other things
4716 aucmd_restbuf(&aco);
Bram Moolenaar13568252018-03-16 20:46:58 +01004717 }
Bram Moolenaar13568252018-03-16 20:46:58 +01004718
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01004719 // Only require pressing Enter when redrawing, to avoid that system() gets
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004720 // the hit-enter prompt even though it didn't output anything.
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004721 if (RedrawingDisabled == 0)
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004722 wait_return(TRUE);
Bram Moolenaar13568252018-03-16 20:46:58 +01004723 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4724
4725theend:
4726 vim_free(argv);
4727 vim_free(tofree1);
4728 vim_free(tofree2);
4729 return retval;
4730}
4731#endif
4732
4733#ifdef USE_SYSTEM
4734/*
4735 * Use system() to start the shell: simple but slow.
4736 */
4737 static int
4738mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004739 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004740 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
4742#ifdef VMS
4743 char *ifn = NULL;
4744 char *ofn = NULL;
4745#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004746 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004747 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004748 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749
4750 out_flush();
4751
4752 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004753 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
Bram Moolenaar62b42182010-09-21 22:09:37 +02004755# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004756 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004757 loose_clipboard();
4758# endif
4759
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 if (cmd == NULL)
4761 x = system((char *)p_sh);
4762 else
4763 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004764# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 if (ofn = strchr((char *)cmd, '>'))
4766 *ofn++ = '\0';
4767 if (ifn = strchr((char *)cmd, '<'))
4768 {
4769 char *p;
4770
4771 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004772 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (p)
4774 *p = '\0';
4775 }
4776 if (ofn)
4777 x = vms_sys((char *)cmd, ofn, ifn);
4778 else
4779 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004780# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004781 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004783 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (newcmd == NULL)
4785 x = 0;
4786 else
4787 {
4788 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4789 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4790 (char *)p_shcf,
4791 (char *)cmd);
4792 x = system((char *)newcmd);
4793 vim_free(newcmd);
4794 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004795# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797# ifdef VMS
4798 x = vms_sys_status(x);
4799# endif
4800 if (emsg_silent)
4801 ;
4802 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004803 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 else if (x && !(options & SHELL_SILENT))
4805 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004806 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 msg_outnum((long)x);
4808 msg_putchar('\n');
4809 }
4810
4811 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004812 {
4813 // The shell may have messed with the mode, always set it.
4814 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004815 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004818# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4819 restore_clipboard();
4820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004822}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823
Bram Moolenaar0f873732019-12-05 20:28:46 +01004824#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825
Bram Moolenaar0f873732019-12-05 20:28:46 +01004826# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4827 // 127, some shells use that already
4828# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829
Bram Moolenaar13568252018-03-16 20:46:58 +01004830/*
4831 * Don't use system(), use fork()/exec().
4832 */
4833 static int
4834mch_call_shell_fork(
4835 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004836 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004837{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004838 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004840 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 pid_t wait_pid = 0;
4842# ifdef HAVE_UNION_WAIT
4843 union wait status;
4844# else
4845 int status = -1;
4846# endif
4847 int retval = -1;
4848 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004849 char_u *tofree1 = NULL;
4850 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004852 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004853# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004855# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004856 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 int fd_fromshell[2];
4858 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004859 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860
4861 out_flush();
4862 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004863 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004864 if (tmode == TMODE_RAW)
4865 // The shell may have messed with the mode, always set it later.
4866 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004868 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004869 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004870
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004872 * For the GUI, when writing the output into the buffer and when reading
4873 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4874 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004876 if ((options & (SHELL_READ|SHELL_WRITE))
4877# ifdef FEAT_GUI
4878 || (gui.in_use && show_shell_mess)
4879# endif
4880 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004882# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 /*
4884 * Try to open a master pty.
4885 * If this works, open the slave pty.
4886 * If the slave can't be opened, close the master pty.
4887 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004888 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004889 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 /*
4891 * If not opening a pty or it didn't work, try using pipes.
4892 */
4893 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004894# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 {
4896 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004897 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 {
4899 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004900 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 {
4902 close(fd_toshell[0]);
4903 close(fd_toshell[1]);
4904 }
4905 }
4906 if (pipe_error)
4907 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004908 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 out_flush();
4910 }
4911 }
4912 }
4913
Bram Moolenaar0f873732019-12-05 20:28:46 +01004914 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004916 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004917 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004918 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004919 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004921 UNBLOCK_SIGNALS(&curset);
4922
Bram Moolenaar32526b32019-01-19 17:43:09 +01004923 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004924 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004926 || (gui.in_use && show_shell_mess)
4927# endif
4928 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004930# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004931 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 {
4933 close(pty_master_fd);
4934 close(pty_slave_fd);
4935 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004936 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
4939 close(fd_toshell[0]);
4940 close(fd_toshell[1]);
4941 close(fd_fromshell[0]);
4942 close(fd_fromshell[1]);
4943 }
4944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004946 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004948 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004949 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00004951# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01004952 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004953 {
4954 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004955 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004956 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004957# endif
4958
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 if (!show_shell_mess || (options & SHELL_EXPAND))
4960 {
4961 int fd;
4962
4963 /*
4964 * Don't want to show any message from the shell. Can't just
4965 * close stdout and stderr though, because some systems will
4966 * break if you try to write to them after that, so we must
4967 * use dup() to replace them with something else -- webb
4968 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4969 * waiting for input.
4970 */
4971 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4972 fclose(stdin);
4973 fclose(stdout);
4974 fclose(stderr);
4975
4976 /*
4977 * If any of these open()'s and dup()'s fail, we just continue
4978 * anyway. It's not fatal, and on most systems it will make
4979 * no difference at all. On a few it will cause the execvp()
4980 * to exit with a non-zero status even when the completion
4981 * could be done, which is nothing too serious. If the open()
4982 * or dup() failed we'd just do the same thing ourselves
4983 * anyway -- webb
4984 */
4985 if (fd >= 0)
4986 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004987 vim_ignored = dup(fd); // To replace stdin (fd 0)
4988 vim_ignored = dup(fd); // To replace stdout (fd 1)
4989 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990
Bram Moolenaar0f873732019-12-05 20:28:46 +01004991 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 close(fd);
4993 }
4994 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004995 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004997 || gui.in_use
4998# endif
4999 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
5001
Bram Moolenaardf177f62005-02-22 08:39:57 +00005002# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005003 // Create our own process group, so that the child and all its
5004 // children can be kill()ed. Don't do this when using pipes,
5005 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005006 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00005007 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005008 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00005009# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005010 // When doing "!xterm&" and 'shell' is bash: the shell
5011 // will exit and send SIGHUP to all processes in its
5012 // group, killing the just started process. Ignore SIGHUP
5013 // to avoid that. (suggested by Simon Schubert)
ichizok378447f2023-05-11 22:25:42 +01005014 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar07256082009-02-04 13:19:42 +00005015# endif
5016 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005017# endif
5018# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005019 if (pty_slave_fd >= 0)
5020 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005021 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005022 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005023 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01005024# ifdef TIOCSCTTY
5025 // Try to become controlling tty (probably doesn't work,
5026 // unless run by root)
5027 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5028# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005029 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005030# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005031 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
Bram Moolenaara5792f52005-11-23 21:25:05 +00005033 /*
5034 * stderr is only redirected when using the GUI, so that a
5035 * program like gpg can still access the terminal to get a
5036 * passphrase using stderr.
5037 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005038# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 if (pty_master_fd >= 0)
5040 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005041 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
Bram Moolenaar0f873732019-12-05 20:28:46 +01005043 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005045 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005047 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00005048 if (gui.in_use)
5049 {
5050 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005051 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00005052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053
Bram Moolenaar0f873732019-12-05 20:28:46 +01005054 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
5056 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00005057# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005059 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 close(fd_toshell[1]);
5061 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005062 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 close(fd_toshell[0]);
5064
Bram Moolenaar0f873732019-12-05 20:28:46 +01005065 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 close(fd_fromshell[0]);
5067 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005068 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 close(fd_fromshell[1]);
5070
Bram Moolenaara5792f52005-11-23 21:25:05 +00005071# ifdef FEAT_GUI
5072 if (gui.in_use)
5073 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005074 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00005075 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005076 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00005077 }
5078# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 /*
5083 * There is no type cast for the argv, because the type may be
5084 * different on different machines. This may cause a warning
5085 * message with strict compilers, don't worry about it.
5086 * Call _exit() instead of exit() to avoid closing the connection
5087 * to the X server (esp. with GTK, which uses atexit()).
5088 */
5089 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005090 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005092 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
5094 /*
5095 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005096 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 */
5098 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005099 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005100 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005101# ifdef FEAT_JOB_CHANNEL
5102 ++dont_check_job_ended;
5103# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 /*
5105 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005106 * This is also used to pipe stdin/stdout to/from the external
5107 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005109 if ((options & (SHELL_READ|SHELL_WRITE))
5110# ifdef FEAT_GUI
5111 || (gui.in_use && show_shell_mess)
5112# endif
5113 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005115# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01005117 int buffer_off = 0; // valid bytes in buffer[]
5118 char_u ta_buf[BUFLEN + 1]; // TypeAHead
5119 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 int len;
5121 int p_more_save;
5122 int old_State;
5123 int c;
5124 int toshell_fd;
5125 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005126 garray_T ga;
5127 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01005128# ifdef ELAPSED_FUNC
5129 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005130# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131
Bram Moolenaardf177f62005-02-22 08:39:57 +00005132# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 if (pty_master_fd >= 0)
5134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 fromshell_fd = pty_master_fd;
5136 toshell_fd = dup(pty_master_fd);
5137 }
5138 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00005139# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
5141 close(fd_toshell[0]);
5142 close(fd_fromshell[1]);
5143 toshell_fd = fd_toshell[1];
5144 fromshell_fd = fd_fromshell[0];
5145 }
5146
5147 /*
5148 * Write to the child if there are typed characters.
5149 * Read from the child if there are characters available.
5150 * Repeat the reading a few times if more characters are
5151 * available. Need to check for typed keys now and then, but
5152 * not too often (delays when no chars are available).
5153 * This loop is quit if no characters can be read from the pty
5154 * (WaitForChar detected special condition), or there are no
5155 * characters available and the child has exited.
5156 * Only check if the child has exited when there is no more
5157 * output. The child may exit before all the output has
5158 * been printed.
5159 *
5160 * Currently this busy loops!
5161 * This can probably dead-lock when the write blocks!
5162 */
5163 p_more_save = p_more;
5164 p_more = FALSE;
5165 old_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01005166 State = MODE_EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005168 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005169 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005170 // Fork a process that will write the lines to the
5171 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005172 if ((wpid = fork()) == -1)
5173 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005174 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005175 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005176 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00005177 {
5178 linenr_T lnum = curbuf->b_op_start.lnum;
5179 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005180 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005181 size_t l;
5182
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005183 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005184 for (;;)
5185 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005186 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005187 if (l == 0)
5188 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005189 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005190 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00005191 len = write(toshell_fd, "", (size_t)1);
5192 else
5193 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005194 char_u *s = vim_strchr(lp + written, NL);
5195
Bram Moolenaar89d40322006-08-29 15:30:07 +00005196 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00005197 s == NULL ? l
5198 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005199 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00005200 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005201 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005202 // Finished a line, add a NL, unless this line
5203 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005204 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005205 || (!curbuf->b_p_bin
5206 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005207 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaar88456cd2022-11-18 22:14:09 +00005208 && (lnum != curbuf->b_ml.ml_line_count
Bram Moolenaardf177f62005-02-22 08:39:57 +00005209 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005210 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005211 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005212 ++lnum;
5213 if (lnum > curbuf->b_op_end.lnum)
5214 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005215 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005216 close(toshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005217 break;
5218 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005219 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005220 written = 0;
5221 }
5222 else if (len > 0)
5223 written += len;
5224 }
5225 _exit(0);
5226 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005227 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005228 {
5229 close(toshell_fd);
5230 toshell_fd = -1;
5231 }
5232 }
5233
5234 if (options & SHELL_READ)
5235 ga_init2(&ga, 1, BUFLEN);
5236
5237 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005238# ifdef ELAPSED_FUNC
5239 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005240# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 for (;;)
5242 {
5243 /*
5244 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005245 * if there are any.
5246 * Don't do this if we are expanding wild cards (would eat
5247 * typeahead).
5248 * Don't do this when filtering and terminal is in cooked
5249 * mode, the shell command will handle the I/O. Avoids
5250 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005251 * Don't get characters when the child has already
5252 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005253 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005254 * while (noread_cnt > 4), avoids that ":r !ls" eats
5255 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 */
5257 len = 0;
5258 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005259 && ((options &
5260 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5261 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005262# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005263 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005264# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005265 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005266 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005267 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005269 if (ta_len == 0)
5270 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005271 // Get extra characters when we don't have any.
5272 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005273 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005274# ifdef ELAPSED_FUNC
5275 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005276# endif
5277 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5278 }
5279 if (ta_len > 0 || len > 0)
5280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 /*
5282 * For pipes:
5283 * Check for CTRL-C: send interrupt signal to child.
5284 * Check for CTRL-D: EOF, close pipe to child.
5285 */
5286 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5287 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 /*
5289 * Send SIGINT to the child's group or all
5290 * processes in our group.
5291 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005292 may_send_sigint(ta_buf[ta_len], pid, wpid);
5293
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 if (pty_master_fd < 0 && toshell_fd >= 0
5295 && ta_buf[ta_len] == Ctrl_D)
5296 {
5297 close(toshell_fd);
5298 toshell_fd = -1;
5299 }
5300 }
5301
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01005302 // Remove Vim-specific codes from the input.
5303 len = term_replace_keycodes(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
5305 /*
5306 * For pipes: echo the typed characters.
5307 * For a pty this does not seem to work.
5308 */
5309 if (pty_master_fd < 0)
5310 {
5311 for (i = ta_len; i < ta_len + len; ++i)
5312 {
5313 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5314 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 else if (has_mbyte)
5316 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005317 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318
5319 msg_outtrans_len(ta_buf + i, l);
5320 i += l - 1;
5321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 else
5323 msg_outtrans_len(ta_buf + i, 1);
5324 }
5325 windgoto(msg_row, msg_col);
5326 out_flush();
5327 }
5328
5329 ta_len += len;
5330
5331 /*
5332 * Write the characters to the child, unless EOF has
5333 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005334 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005335 * When writing buffer lines, drop the typed
5336 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005338 if (options & SHELL_WRITE)
5339 ta_len = 0;
5340 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
5342 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5343 if (len > 0)
5344 {
5345 ta_len -= len;
5346 mch_memmove(ta_buf, ta_buf + len, ta_len);
5347 }
5348 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 }
5351
Bram Moolenaardf177f62005-02-22 08:39:57 +00005352 if (got_int)
5353 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005354 // CTRL-C sends a signal to the child, we ignore it
5355 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005356# ifdef HAVE_SETSID
5357 kill(-pid, SIGINT);
5358# else
5359 kill(0, SIGINT);
5360# endif
5361 if (wpid > 0)
5362 kill(wpid, SIGINT);
5363 got_int = FALSE;
5364 }
5365
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 /*
5367 * Check if the child has any characters to be printed.
5368 * Read them and write them to our window. Repeat this as
5369 * long as there is something to do, avoid the 10ms wait
5370 * for mch_inchar(), or sending typeahead characters to
5371 * the external process.
5372 * TODO: This should handle escape sequences, compatible
5373 * to some terminal (vt52?).
5374 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005375 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005376 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005378 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005381 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005383
5384 noread_cnt = 0;
5385 if (options & SHELL_READ)
5386 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005387 // Do NUL -> NL translation, append NL separated
5388 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005389 for (i = 0; i < len; ++i)
5390 {
5391 if (buffer[i] == NL)
5392 append_ga_line(&ga);
5393 else if (buffer[i] == NUL)
5394 ga_append(&ga, NL);
5395 else
5396 ga_append(&ga, buffer[i]);
5397 }
5398 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005399 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 {
5401 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005402 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403
Bram Moolenaardf177f62005-02-22 08:39:57 +00005404 len += buffer_off;
5405 buffer[len] = NUL;
5406
Bram Moolenaar0f873732019-12-05 20:28:46 +01005407 // Check if the last character in buffer[] is
5408 // incomplete, keep these bytes for the next
5409 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 for (p = buffer; p < buffer + len; p += l)
5411 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005412 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005414 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 else if (MB_BYTE2LEN(*p) != l)
5416 break;
5417 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005418 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005420 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 if (len >= 12)
5422 ++p;
5423 else
5424 {
5425 buffer_off = len;
5426 continue;
5427 }
5428 }
5429 c = *p;
5430 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005431 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 if (p < buffer + len)
5433 {
5434 *p = c;
5435 buffer_off = (buffer + len) - p;
5436 mch_memmove(buffer, p, buffer_off);
5437 continue;
5438 }
5439 buffer_off = 0;
5440 }
5441 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 {
5443 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005444 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 }
5446
5447 windgoto(msg_row, msg_col);
5448 cursor_on();
5449 out_flush();
5450 if (got_int)
5451 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005452
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005453# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005454 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005455 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005456 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005457
Bram Moolenaar0f873732019-12-05 20:28:46 +01005458 // Avoid that we keep looping here without
5459 // checking for a CTRL-C for a long time. Don't
5460 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005461 if (msec > 2000)
5462 {
5463 noread_cnt = 5;
5464 break;
5465 }
5466 }
5467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 }
5469
Bram Moolenaar0f873732019-12-05 20:28:46 +01005470 // If we already detected the child has finished, continue
5471 // reading output for a short while. Some text may be
5472 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005473 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005474 {
5475 if (noread_cnt < 5)
5476 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005477 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005478 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005479
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 /*
5481 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005482 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005484# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005485 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005486# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005488# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5490 || (wait_pid == pid && WIFEXITED(status)))
5491 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005492 // Don't break the loop yet, try reading more
5493 // characters from "fromshell_fd" first. When using
5494 // pipes there might still be something to read and
5495 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005498 else
5499 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005500
Bram Moolenaar95a51352013-03-21 22:53:50 +01005501# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005502 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005503 clip_update();
5504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 }
5506finished:
5507 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005508 if (options & SHELL_READ)
5509 {
5510 if (ga.ga_len > 0)
5511 {
5512 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005513 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005514 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005515 }
5516 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005517 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005518 ga_clear(&ga);
5519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 /*
5522 * Give all typeahead that wasn't used back to ui_inchar().
5523 */
5524 if (ta_len)
5525 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 State = old_State;
5527 if (toshell_fd >= 0)
5528 close(toshell_fd);
5529 close(fromshell_fd);
5530 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005531# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005532 else
5533 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005534 long delay_msec = 1;
5535
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005536 if (tmode == TMODE_RAW)
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005537 // Possibly disables modifyOtherKeys, so that the system
5538 // can recognize CTRL-C.
5539 out_str_t_TE();
Bram Moolenaar0981c872020-08-23 14:28:37 +02005540
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005541 /*
5542 * Similar to the loop above, but only handle X events, no
5543 * I/O.
5544 */
5545 for (;;)
5546 {
5547 if (got_int)
5548 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005549 // CTRL-C sends a signal to the child, we ignore it
5550 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005551# ifdef HAVE_SETSID
5552 kill(-pid, SIGINT);
5553# else
5554 kill(0, SIGINT);
5555# endif
5556 got_int = FALSE;
5557 }
5558# ifdef __NeXT__
5559 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5560# else
5561 wait_pid = waitpid(pid, &status, WNOHANG);
5562# endif
5563 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5564 || (wait_pid == pid && WIFEXITED(status)))
5565 {
5566 wait_pid = pid;
5567 break;
5568 }
5569
Bram Moolenaar0f873732019-12-05 20:28:46 +01005570 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005571 clip_update();
5572
Bram Moolenaar0f873732019-12-05 20:28:46 +01005573 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005574 // less time, gradually wait longer.
5575 mch_delay(delay_msec,
5576 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005577 if (++delay_msec > 10)
5578 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005579 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005580
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005581 if (tmode == TMODE_RAW)
5582 // possibly enables modifyOtherKeys again
Bram Moolenaar733a69b2022-12-01 12:03:47 +00005583 out_str_t_TI();
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005584 }
5585# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
5587 /*
5588 * Wait until our child has exited.
5589 * Ignore wait() returning pids of other children and returning
5590 * because of some signal like SIGWINCH.
5591 * Don't wait if wait_pid was already set above, indicating the
5592 * child already exited.
5593 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005594 if (wait_pid != pid)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01005595 (void)wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
Bram Moolenaar624891f2010-10-13 16:22:09 +02005597# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005598 // Close slave side of pty. Only do this after the child has
5599 // exited, otherwise the child may hang when it tries to write on
5600 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005601 if (pty_master_fd >= 0)
5602 close(pty_slave_fd);
5603# endif
5604
Bram Moolenaar0f873732019-12-05 20:28:46 +01005605 // Make sure the child that writes to the external program is
5606 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005607 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005608 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005609 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005610 wait4pid(wpid, NULL);
5611 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005612
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005613# ifdef FEAT_JOB_CHANNEL
5614 --dont_check_job_ended;
5615# endif
5616
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 /*
5618 * Set to raw mode right now, otherwise a CTRL-C after
5619 * catch_signals() will kill Vim.
5620 */
5621 if (tmode == TMODE_RAW)
5622 settmode(TMODE_RAW);
5623 did_settmode = TRUE;
5624 set_signals();
5625
5626 if (WIFEXITED(status))
5627 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005628 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005630 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
5632 if (retval == EXEC_FAILED)
5633 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005634 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 msg_outtrans(p_sh);
5636 msg_putchar('\n');
5637 }
5638 else if (!(options & SHELL_SILENT))
5639 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005640 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 msg_outnum((long)retval);
5642 msg_putchar('\n');
5643 }
5644 }
5645 }
5646 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005647 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
5649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
5651error:
5652 if (!did_settmode)
5653 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005654 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005656 vim_free(argv);
5657 vim_free(tofree1);
5658 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
5660 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005661}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005662#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005663
5664 int
5665mch_call_shell(
5666 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005667 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005668{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005669#ifdef FEAT_EVAL
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01005670 ch_log(NULL, "executing shell command: %s", cmd);
5671#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005672#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar524c8532022-09-27 15:48:20 +01005673 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL
5674 && (options & SHELL_SILENT) == 0)
Bram Moolenaar13568252018-03-16 20:46:58 +01005675 return mch_call_shell_terminal(cmd, options);
5676#endif
5677#ifdef USE_SYSTEM
5678 return mch_call_shell_system(cmd, options);
5679#else
5680 return mch_call_shell_fork(cmd, options);
5681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682}
5683
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005684#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005685 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005686mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005687{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005688 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005689 int fd_in[2] = {-1, -1}; // for stdin
5690 int fd_out[2] = {-1, -1}; // for stdout
5691 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005692 int pty_master_fd = -1;
5693 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005694 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005695 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5696 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5697 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005698 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005699 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5700 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005701 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005702 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005703 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005704
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005705 if (use_out_for_err && use_null_for_out)
5706 use_null_for_err = TRUE;
5707
Bram Moolenaar0f873732019-12-05 20:28:46 +01005708 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005709 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005710
Bram Moolenaarb2412082017-08-20 18:09:14 +02005711 if (options->jo_pty
5712 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005713 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005714 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005715 open_pty(&pty_master_fd, &pty_slave_fd,
5716 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005717
Bram Moolenaar0f873732019-12-05 20:28:46 +01005718 // TODO: without the channel feature connect the child to /dev/null?
5719 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005720 if (use_file_for_in)
5721 {
5722 char_u *fname = options->jo_io_name[PART_IN];
5723
5724 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5725 if (fd_in[0] < 0)
5726 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005727 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005728 goto failed;
5729 }
5730 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005731 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005732 // When writing buffer lines to the input don't use the pty, so that
5733 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005734 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5735 && pipe(fd_in) < 0)
5736 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005737
5738 if (use_file_for_out)
5739 {
5740 char_u *fname = options->jo_io_name[PART_OUT];
5741
5742 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5743 if (fd_out[1] < 0)
5744 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005745 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005746 goto failed;
5747 }
5748 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005749 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005750 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005751
5752 if (use_file_for_err)
5753 {
5754 char_u *fname = options->jo_io_name[PART_ERR];
5755
5756 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5757 if (fd_err[1] < 0)
5758 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005759 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005760 goto failed;
5761 }
5762 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005763 else if (!use_out_for_err && !use_null_for_err
5764 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005765 goto failed;
5766
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005767 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5768 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005769 if (options->jo_set & JO_CHANNEL)
5770 {
5771 channel = options->jo_channel;
5772 if (channel != NULL)
5773 ++channel->ch_refcount;
5774 }
5775 else
5776 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005777 if (channel == NULL)
5778 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005779 if (job->jv_tty_out != NULL)
5780 ch_log(channel, "using pty %s on fd %d",
5781 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005782 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005783
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005784 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005785 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005786 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005787 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005788 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005789 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005790 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005791 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005792 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005793 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005794 int null_fd = -1;
5795 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005796
Bram Moolenaar0f873732019-12-05 20:28:46 +01005797 // child
5798 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005799 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005800
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005801# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01005802 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005803 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005804 ch_logfile((char_u *)"", (char_u *)"");
5805# endif
5806
Bram Moolenaar835dc632016-02-07 14:27:38 +01005807# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005808 // Create our own process group, so that the child and all its
5809 // children can be kill()ed. Don't do this when using pipes,
5810 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005811 (void)setsid();
5812# endif
5813
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005814# ifdef FEAT_TERMINAL
5815 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005816 {
5817 char *term = (char *)T_NAME;
5818
5819#ifdef FEAT_GUI
5820 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005821 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005822 term = getenv("TERM");
5823#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005824 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005825 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005826 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005827 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005828 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005829 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005830 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005831 else if (t_colors > 16)
5832 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005833 else
5834 term = "xterm";
5835 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005836 set_child_environment(
5837 (long)options->jo_term_rows,
5838 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005839 term,
5840 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005841 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005842 else
5843# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005844 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005845
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005846 if (options->jo_env != NULL)
5847 {
5848 dict_T *dict = options->jo_env;
5849 hashitem_T *hi;
5850 int todo = (int)dict->dv_hashtab.ht_used;
5851
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005852 FOR_ALL_HASHTAB_ITEMS(&dict->dv_hashtab, hi, todo)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005853 if (!HASHITEM_EMPTY(hi))
5854 {
5855 typval_T *item = &dict_lookup(hi)->di_tv;
5856
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005857 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005858 --todo;
5859 }
5860 }
5861
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005862 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005863 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005864 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005865 if (null_fd < 0)
5866 {
5867 perror("opening /dev/null failed");
5868 _exit(OPEN_NULL_FAILED);
5869 }
5870 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005871
Bram Moolenaar223896d2017-08-02 22:33:28 +02005872 if (pty_slave_fd >= 0)
5873 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005874 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005875 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005876# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005877 // Try to become controlling tty (probably doesn't work,
5878 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005879 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5880# endif
5881 }
5882
Bram Moolenaar0f873732019-12-05 20:28:46 +01005883 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005884 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005885 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005886 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005887 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005888 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005889 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005890 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005891
Bram Moolenaar0f873732019-12-05 20:28:46 +01005892 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005893 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005894 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005895 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005896 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005897 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005898 }
5899 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005900 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005901 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005902 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005903 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005904 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005905
Bram Moolenaar0f873732019-12-05 20:28:46 +01005906 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005907 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005908 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005909 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005910 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005911 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005912 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005913 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005914
5915 if (fd_in[0] >= 0)
5916 close(fd_in[0]);
5917 if (fd_in[1] >= 0)
5918 close(fd_in[1]);
5919 if (fd_out[0] >= 0)
5920 close(fd_out[0]);
5921 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005922 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005923 if (fd_err[0] >= 0)
5924 close(fd_err[0]);
5925 if (fd_err[1] >= 0)
5926 close(fd_err[1]);
5927 if (pty_master_fd >= 0)
5928 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005929 close(pty_master_fd); // not used in the child
5930 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005931 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005932
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005933 if (null_fd >= 0)
5934 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005935
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005936 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5937 _exit(EXEC_FAILED);
5938
Bram Moolenaar0f873732019-12-05 20:28:46 +01005939 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005940 execvp(argv[0], argv);
5941
Bram Moolenaar4694a172016-04-21 14:05:23 +02005942 if (stderr_works)
5943 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005944# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005945 // calling free_all_mem() here causes problems. Ignore valgrind
5946 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005947# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005948 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005949 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005950
Bram Moolenaar0f873732019-12-05 20:28:46 +01005951 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005952 UNBLOCK_SIGNALS(&curset);
5953
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005954 job->jv_pid = pid;
5955 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005956 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005957
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005958 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005959 close(pty_slave_fd); // not used in the parent
5960 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005961 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005962 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005963 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005964 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005965 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005966 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005967 if (channel != NULL)
5968 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005969 int in_fd = INVALID_FD;
5970 int out_fd = INVALID_FD;
5971 int err_fd = INVALID_FD;
5972
5973 if (!(use_file_for_in || use_null_for_in))
5974 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5975
5976 if (!(use_file_for_out || use_null_for_out))
5977 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5978
5979 // When using pty_master_fd only set it for stdout, do not duplicate
5980 // it for stderr, it only needs to be read once.
5981 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5982 {
5983 if (fd_err[0] >= 0)
5984 err_fd = fd_err[0];
5985 else if (out_fd != pty_master_fd)
5986 err_fd = pty_master_fd;
5987 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005988
5989 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005990 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005991 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005992 else
5993 {
5994 if (fd_in[1] >= 0)
5995 close(fd_in[1]);
5996 if (fd_out[0] >= 0)
5997 close(fd_out[0]);
5998 if (fd_err[0] >= 0)
5999 close(fd_err[0]);
6000 if (pty_master_fd >= 0)
6001 close(pty_master_fd);
6002 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006003
Bram Moolenaar0f873732019-12-05 20:28:46 +01006004 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006005 return;
6006
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006007failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01006008 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006009 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006010 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01006011 if (fd_in[1] >= 0)
6012 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006013 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006014 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01006015 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006016 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006017 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006018 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01006019 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006020 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02006021 if (pty_master_fd >= 0)
6022 close(pty_master_fd);
6023 if (pty_slave_fd >= 0)
6024 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01006025}
6026
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006027 static char_u *
6028get_signal_name(int sig)
6029{
6030 int i;
6031 char_u numbuf[NUMBUFLEN];
6032
6033 if (sig == SIGKILL)
6034 return vim_strsave((char_u *)"kill");
6035
6036 for (i = 0; signal_info[i].sig != -1; i++)
6037 if (sig == signal_info[i].sig)
6038 return strlow_save((char_u *)signal_info[i].name);
6039
6040 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
6041 return vim_strsave(numbuf);
6042}
6043
Bram Moolenaar835dc632016-02-07 14:27:38 +01006044 char *
6045mch_job_status(job_T *job)
6046{
6047# ifdef HAVE_UNION_WAIT
6048 union wait status;
6049# else
6050 int status = -1;
6051# endif
6052 pid_t wait_pid = 0;
6053
6054# ifdef __NeXT__
6055 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
6056# else
6057 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
6058# endif
6059 if (wait_pid == -1)
6060 {
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00006061 int waitpid_errno = errno;
6062 if (waitpid_errno == ECHILD && mch_process_running(job->jv_pid))
6063 // The process is alive, but it was probably reparented (for
6064 // example by ptrace called by a debugger like lldb or gdb).
6065 // Note: This assumes that process IDs are not reused.
6066 return "run";
6067
Bram Moolenaar0f873732019-12-05 20:28:46 +01006068 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006069 if (job->jv_status < JOB_ENDED)
6070 ch_log(job->jv_channel, "Job no longer exists: %s",
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00006071 strerror(waitpid_errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02006072 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006073 }
6074 if (wait_pid == 0)
6075 return "run";
6076 if (WIFEXITED(status))
6077 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006078 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01006079 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006080 if (job->jv_status < JOB_ENDED)
6081 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02006082 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006083 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01006084 if (WIFSIGNALED(status))
6085 {
6086 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006087 job->jv_termsig = get_signal_name(WTERMSIG(status));
6088 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
6089 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
6090 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02006091 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01006092 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01006093 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02006094
6095return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01006096 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02006097 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02006098 return "dead";
6099}
6100
6101 job_T *
6102mch_detect_ended_job(job_T *job_list)
6103{
6104# ifdef HAVE_UNION_WAIT
6105 union wait status;
6106# else
6107 int status = -1;
6108# endif
6109 pid_t wait_pid = 0;
6110 job_T *job;
6111
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01006112# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006113 // Do not do this when waiting for a shell command to finish, we would get
6114 // the exit value here (and discard it), the exit value obtained there
6115 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01006116 if (dont_check_job_ended > 0)
6117 return NULL;
6118# endif
6119
Bram Moolenaar97792de2016-10-15 18:36:49 +02006120# ifdef __NeXT__
6121 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
6122# else
6123 wait_pid = waitpid(-1, &status, WNOHANG);
6124# endif
6125 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006126 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02006127 return NULL;
6128 for (job = job_list; job != NULL; job = job->jv_next)
6129 {
6130 if (job->jv_pid == wait_pid)
6131 {
6132 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006133 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02006134 job->jv_exitval = WEXITSTATUS(status);
6135 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006136 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02006137 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006138 job->jv_termsig = get_signal_name(WTERMSIG(status));
6139 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01006140 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02006141 {
6142 ch_log(job->jv_channel, "Job ended");
6143 job->jv_status = JOB_ENDED;
6144 }
6145 return job;
6146 }
6147 }
6148 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006149}
6150
Bram Moolenaar3a117e12016-10-30 21:57:52 +01006151/*
6152 * Send a (deadly) signal to "job".
6153 * Return FAIL if "how" is not a valid name.
6154 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01006155 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006156mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01006157{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006158 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006159
Bram Moolenaar923d9262016-02-25 20:56:01 +01006160 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01006161 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01006162 else if (STRCMP(how, "hup") == 0)
6163 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006164 else if (STRCMP(how, "quit") == 0)
6165 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01006166 else if (STRCMP(how, "int") == 0)
6167 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006168 else if (STRCMP(how, "kill") == 0)
6169 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02006170#ifdef SIGWINCH
6171 else if (STRCMP(how, "winch") == 0)
6172 sig = SIGWINCH;
6173#endif
Keith Thompson184f71c2024-01-04 21:19:04 +01006174 else if (SAFE_isdigit(*how))
Bram Moolenaar835dc632016-02-07 14:27:38 +01006175 sig = atoi((char *)how);
6176 else
6177 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01006178
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01006179 // Never kill ourselves!
6180 if (job->jv_pid != 0)
6181 {
6182 // TODO: have an option to only kill the process, not the group?
6183 kill(-job->jv_pid, sig);
6184 kill(job->jv_pid, sig);
6185 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01006186
Bram Moolenaar835dc632016-02-07 14:27:38 +01006187 return OK;
6188}
Bram Moolenaar76467df2016-02-12 19:30:26 +01006189
6190/*
6191 * Clear the data related to "job".
6192 */
6193 void
6194mch_clear_job(job_T *job)
6195{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006196 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01006197# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006198 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006199# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006200 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006201# endif
6202}
Bram Moolenaar835dc632016-02-07 14:27:38 +01006203#endif
6204
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006205#if defined(FEAT_TERMINAL) || defined(PROTO)
6206 int
6207mch_create_pty_channel(job_T *job, jobopt_T *options)
6208{
6209 int pty_master_fd = -1;
6210 int pty_slave_fd = -1;
6211 channel_T *channel;
6212
Bram Moolenaar59386482019-02-10 22:43:46 +01006213 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02006214 if (pty_master_fd < 0 || pty_slave_fd < 0)
6215 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006216 close(pty_slave_fd);
6217
6218 channel = add_channel();
6219 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006220 {
6221 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006222 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006223 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006224 if (job->jv_tty_out != NULL)
6225 ch_log(channel, "using pty %s on fd %d",
6226 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006227 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006228 channel->ch_keep_open = TRUE;
6229
Bram Moolenaar0f873732019-12-05 20:28:46 +01006230 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6231 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006232 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006233 channel_set_job(channel, job, options);
6234 return OK;
6235}
6236#endif
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238/*
6239 * Check for CTRL-C typed by reading all available characters.
6240 * In cooked mode we should get SIGINT, no need to check.
6241 */
6242 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006243mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006245 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006246 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 fill_input_buf(FALSE);
6248}
6249
6250/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006251 * Wait "msec" msec until a character is available from the mouse, keyboard,
6252 * from inbuf[].
6253 * "msec" == -1 will block forever.
6254 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006255 * When "ignore_input" is TRUE even check for pending input when input is
6256 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006257 * "interrupted" (if not NULL) is set to TRUE when no character is available
6258 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006259 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006260 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 */
6262 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006263WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006265#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006266 return ui_wait_for_chars_or_timer(
6267 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006268#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006269 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006270#endif
6271}
6272
6273/*
6274 * Wait "msec" msec until a character is available from the mouse or keyboard
6275 * or from inbuf[].
6276 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006277 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006278 * "interrupted" (if not NULL) is set to TRUE when no character is available
6279 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006280 * When a GUI is being used, this will never get called -- webb
6281 */
6282 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006283WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006284{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285#ifdef FEAT_MOUSE_GPM
6286 int gpm_process_wanted;
6287#endif
6288#ifdef FEAT_XCLIPBOARD
6289 int rest;
6290#endif
6291 int avail;
6292
Bram Moolenaar0f873732019-12-05 20:28:46 +01006293 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 return 1;
6295
6296#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006297 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 if (WantQueryMouse)
6299 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006300 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006301 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006302 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 }
6304#endif
6305
6306 /*
6307 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6308 * events. This is a bit complicated, because they might both be defined.
6309 */
6310#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6311# ifdef FEAT_XCLIPBOARD
6312 rest = 0;
6313 if (do_xterm_trace())
6314 rest = msec;
6315# endif
6316 do
6317 {
6318# ifdef FEAT_XCLIPBOARD
6319 if (rest != 0)
6320 {
6321 msec = XT_TRACE_DELAY;
6322 if (rest >= 0 && rest < XT_TRACE_DELAY)
6323 msec = rest;
6324 if (rest >= 0)
6325 rest -= msec;
6326 }
6327# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01006328# ifdef FEAT_SOUND_MACOSX
6329 // Invoke any pending sound callbacks.
6330 process_cfrunloop();
6331# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006332# ifdef FEAT_SOUND_CANBERRA
6333 // Invoke any pending sound callbacks.
6334 if (has_sound_callback_in_queue())
6335 invoke_sound_callback();
6336# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337# ifdef FEAT_MOUSE_GPM
6338 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006339 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006340 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006341 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006343 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006347 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 return 1;
6349# ifdef FEAT_XCLIPBOARD
6350 if (rest == 0 || !do_xterm_trace())
6351# endif
6352 break;
6353 }
6354 }
6355 while (FALSE
6356# ifdef FEAT_MOUSE_GPM
6357 || (gpm_process_wanted && mch_gpm_process() == 0)
6358# endif
6359# ifdef FEAT_XCLIPBOARD
6360 || (!avail && rest != 0)
6361# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006362 )
6363 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
6365#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006366 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367#endif
6368 return avail;
6369}
6370
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006371#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372/*
6373 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006374 * "msec" == 0 will check for characters once.
6375 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006378 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006379 * "interrupted" (if not NULL) is set to TRUE when no character is available
6380 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006382 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006383RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384{
6385 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006386 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006387#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 static int busy = FALSE;
6389
Bram Moolenaar0f873732019-12-05 20:28:46 +01006390 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391# define MAY_LOOP
6392
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006393# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006394 // Remember at what time we started, so that we know how much longer we
6395 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006396 long start_msec = msec;
6397 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006399 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006400 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401# endif
6402
Bram Moolenaar0f873732019-12-05 20:28:46 +01006403 // Handle being called recursively. This may happen for the session
6404 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 if (busy)
6406 return 0;
6407#endif
6408
6409#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006410 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411#endif
6412 {
6413#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006414 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006415# ifdef FEAT_MZSCHEME
6416 int mzquantum_used = FALSE;
6417# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418#endif
6419#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006420 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006421 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 int nfd;
6423# ifdef FEAT_XCLIPBOARD
6424 int xterm_idx = -1;
6425# endif
6426# ifdef FEAT_MOUSE_GPM
6427 int gpm_idx = -1;
6428# endif
6429# ifdef USE_XSMP
6430 int xsmp_idx = -1;
6431# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006432 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006434# ifdef FEAT_MZSCHEME
6435 mzvim_check_threads();
6436 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6437 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006438 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006439 mzquantum_used = TRUE;
6440 }
6441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 fds[0].fd = fd;
6443 fds[0].events = POLLIN;
6444 nfd = 1;
6445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006447 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 if (xterm_Shell != (Widget)0)
6449 {
6450 xterm_idx = nfd;
6451 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6452 fds[nfd].events = POLLIN;
6453 nfd++;
6454 }
6455# endif
6456# ifdef FEAT_MOUSE_GPM
6457 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6458 {
6459 gpm_idx = nfd;
6460 fds[nfd].fd = gpm_fd;
6461 fds[nfd].events = POLLIN;
6462 nfd++;
6463 }
6464# endif
6465# ifdef USE_XSMP
6466 if (xsmp_icefd != -1)
6467 {
6468 xsmp_idx = nfd;
6469 fds[nfd].fd = xsmp_icefd;
6470 fds[nfd].events = POLLIN;
6471 nfd++;
6472 }
6473# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006474#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006475 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006476#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006477 if (interrupted != NULL)
6478 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006480 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006481
6482 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006483 if (result == 0 && interrupted != NULL && ret > 0)
6484 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006485
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006486# ifdef FEAT_MZSCHEME
6487 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006488 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006489 finished = FALSE;
6490# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492# ifdef FEAT_XCLIPBOARD
6493 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6494 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006495 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006497 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 finished = FALSE;
6499 }
6500# endif
6501# ifdef FEAT_MOUSE_GPM
6502 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504# endif
6505# ifdef USE_XSMP
6506 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6507 {
6508 if (fds[xsmp_idx].revents & POLLIN)
6509 {
6510 busy = TRUE;
6511 xsmp_handle_requests();
6512 busy = FALSE;
6513 }
6514 else if (fds[xsmp_idx].revents & POLLHUP)
6515 {
6516 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006517 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 xsmp_close();
6519 }
6520 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006521 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 }
6523# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006524#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006525 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006526 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006527 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529
Bram Moolenaar0f873732019-12-05 20:28:46 +01006530#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531
6532 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006533 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006534 // These are static because they can take 8 Kbyte each and cause the
6535 // signal stack to run out with -O3.
6536 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006538 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006540# ifdef FEAT_MZSCHEME
6541 mzvim_check_threads();
6542 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6543 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006544 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006545 mzquantum_used = TRUE;
6546 }
6547# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006549 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006551 tv.tv_sec = towait / 1000;
6552 tv.tv_usec = (towait % 1000) * (1000000/1000);
6553 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006555 else
6556 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557
6558 /*
6559 * Select on ready for reading and exceptional condition (end of file).
6560 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006561select_eintr:
6562 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006563 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 FD_ZERO(&efds);
6565 FD_SET(fd, &rfds);
K.Takatab247e062022-02-07 10:45:23 +00006566# ifndef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006567 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 FD_SET(fd, &efds);
6569# endif
6570 maxfd = fd;
6571
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006573 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 if (xterm_Shell != (Widget)0)
6575 {
6576 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6577 if (maxfd < ConnectionNumber(xterm_dpy))
6578 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006579
Bram Moolenaar0f873732019-12-05 20:28:46 +01006580 // An event may have already been read but not handled. In
6581 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006582 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 }
6584# endif
6585# ifdef FEAT_MOUSE_GPM
6586 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6587 {
6588 FD_SET(gpm_fd, &rfds);
6589 FD_SET(gpm_fd, &efds);
6590 if (maxfd < gpm_fd)
6591 maxfd = gpm_fd;
6592 }
6593# endif
6594# ifdef USE_XSMP
6595 if (xsmp_icefd != -1)
6596 {
6597 FD_SET(xsmp_icefd, &rfds);
6598 FD_SET(xsmp_icefd, &efds);
6599 if (maxfd < xsmp_icefd)
6600 maxfd = xsmp_icefd;
6601 }
6602# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006603# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006604 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006605# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006606 if (interrupted != NULL)
6607 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608
Bram Moolenaar643b6142018-09-12 20:29:09 +02006609 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6610 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006611 result = ret > 0 && FD_ISSET(fd, &rfds);
6612 if (result)
6613 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006614 else if (interrupted != NULL && ret > 0)
6615 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006616
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006617# ifdef EINTR
6618 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006619 {
dbivolaruab16ad32021-12-29 19:41:47 +00006620 // Check whether the EINTR is caused by SIGTSTP
6621 if (got_tstp && !in_mch_suspend)
6622 {
6623 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006624
dbivolaruab16ad32021-12-29 19:41:47 +00006625 ea.forceit = TRUE;
6626 ex_stop(&ea);
6627 got_tstp = FALSE;
6628 }
6629
Bram Moolenaar0f873732019-12-05 20:28:46 +01006630 // Check whether window has been resized, EINTR may be caused by
6631 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006632 if (do_resize)
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006633 {
Bram Moolenaar545c8a52023-06-21 15:51:47 +01006634# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006635 ch_log(NULL, "calling handle_resize() in RealWaitForChar()");
Bram Moolenaar545c8a52023-06-21 15:51:47 +01006636# endif
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006637 handle_resize();
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006638 }
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006639
Bram Moolenaar0f873732019-12-05 20:28:46 +01006640 // Interrupted by a signal, need to try again. We ignore msec
6641 // here, because we do want to check even after a timeout if
6642 // characters are available. Needed for reading output of an
6643 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006644 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006645 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006646# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006647# ifdef __TANDEM
6648 if (ret == -1 && errno == ENOTSUP)
6649 {
6650 FD_ZERO(&rfds);
6651 FD_ZERO(&efds);
6652 ret = 0;
6653 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006654# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006655# ifdef FEAT_MZSCHEME
6656 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006657 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006658 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659# endif
6660
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661# ifdef FEAT_XCLIPBOARD
6662 if (ret > 0 && xterm_Shell != (Widget)0
6663 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6664 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006665 xterm_update(); // Maybe we should hand out clipboard
6666 // continue looping when we only got the X event and the input
6667 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 if (--ret == 0 && !input_available())
6669 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006670 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 finished = FALSE;
6672 }
6673 }
6674# endif
6675# ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00006676 if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 {
6678 if (FD_ISSET(gpm_fd, &efds))
6679 gpm_close();
6680 else if (FD_ISSET(gpm_fd, &rfds))
6681 *check_for_gpm = 1;
6682 }
6683# endif
6684# ifdef USE_XSMP
6685 if (ret > 0 && xsmp_icefd != -1)
6686 {
6687 if (FD_ISSET(xsmp_icefd, &efds))
6688 {
6689 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006690 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 xsmp_close();
6692 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006693 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 }
6695 else if (FD_ISSET(xsmp_icefd, &rfds))
6696 {
6697 busy = TRUE;
6698 xsmp_handle_requests();
6699 busy = FALSE;
6700 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006701 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 }
6703 }
6704# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006705#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006706 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006707 if (ret >= 0)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01006708 (void)channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710
Bram Moolenaar0f873732019-12-05 20:28:46 +01006711#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712
6713#ifdef MAY_LOOP
6714 if (finished || msec == 0)
6715 break;
6716
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006717# ifdef FEAT_CLIENTSERVER
6718 if (server_waiting())
6719 break;
6720# endif
6721
Bram Moolenaar0f873732019-12-05 20:28:46 +01006722 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 if (msec > 0)
6724 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006725# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006726 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006727 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006729 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 msec = msec / 2;
6731# endif
6732 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006733 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 }
6735#endif
6736 }
6737
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006738 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739}
6740
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006742 * Expand a path into all matching files and/or directories. Handles "*",
6743 * "?", "[a-z]", "**", etc.
6744 * "path" has backslashes before chars that are not to be expanded.
6745 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 */
6747 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006748mch_expandpath(
6749 garray_T *gap,
6750 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006751 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
Bram Moolenaar02743632005-07-25 20:42:36 +00006753 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
6756/*
6757 * mch_expand_wildcards() - this code does wild-card pattern matching using
6758 * the shell
6759 *
6760 * return OK for success, FAIL for error (you may lose some memory) and put
6761 * an error message in *file.
6762 *
6763 * num_pat is number of input patterns
6764 * pat is array of pointers to input patterns
6765 * num_file is pointer to number of matched file names
6766 * file is pointer to array of pointers to matched file names
6767 */
6768
6769#ifndef SEEK_SET
6770# define SEEK_SET 0
6771#endif
6772#ifndef SEEK_END
6773# define SEEK_END 2
6774#endif
6775
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006776#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006777
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006779mch_expand_wildcards(
6780 int num_pat,
6781 char_u **pat,
6782 int *num_file,
6783 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006784 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785{
6786 int i;
6787 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006788 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 char_u *p;
6790 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791
Bram Moolenaarc7247912008-01-13 12:54:11 +00006792 /*
6793 * This is the non-OS/2 implementation (really Unix).
6794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 int j;
6796 char_u *tempname;
6797 char_u *command;
6798 FILE *fd;
6799 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006800#define STYLE_ECHO 0 // use "echo", the default
6801#define STYLE_GLOB 1 // use "glob", for csh
6802#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6803#define STYLE_PRINT 3 // use "print -N", for zsh
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006804#define STYLE_BT 4 // `cmd` expansion, execute the pattern directly
6805#define STYLE_GLOBSTAR 5 // use extended shell glob for bash (this uses extended
6806 // globbing functionality using globstar, needs bash > 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 int shell_style = STYLE_ECHO;
6808 int check_spaces;
6809 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006810 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006811 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006812 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006813 // vimglob() function with globstar setting enabled, only for bash >= 4.X
6814 static char *sh_globstar_opt = "[[ ${BASH_VERSINFO[0]} -ge 4 ]] && shopt -s globstar; ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815
Bram Moolenaar0f873732019-12-05 20:28:46 +01006816 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 *file = NULL;
6818
6819 /*
6820 * If there are no wildcards, just copy the names to allocated memory.
6821 * Saves a lot of time, because we don't have to start a new shell.
6822 */
6823 if (!have_wildcard(num_pat, pat))
6824 return save_patterns(num_pat, pat, num_file, file);
6825
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006826# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006827 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006828 if (sandbox != 0 && check_secure())
6829 return FAIL;
6830# endif
6831
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 /*
6833 * Don't allow the use of backticks in secure and restricted mode.
6834 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006835 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 for (i = 0; i < num_pat; ++i)
6837 if (vim_strchr(pat[i], '`') != NULL
6838 && (check_restricted() || check_secure()))
6839 return FAIL;
6840
6841 /*
6842 * get a name for the temp file
6843 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006844 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006846 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 return FAIL;
6848 }
6849
6850 /*
6851 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006852 * file.
6853 * STYLE_BT: NL separated
6854 * If expanding `cmd` execute it directly.
6855 * STYLE_GLOB: NUL separated
6856 * If we use *csh, "glob" will work better than "echo".
6857 * STYLE_PRINT: NL or NUL separated
6858 * If we use *zsh, "print -N" will work better than "glob".
6859 * STYLE_VIMGLOB: NL separated
6860 * If we use *sh*, we define "vimglob()".
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006861 * STYLE_GLOBSTAR: NL separated
6862 * If we use *bash*, we define "vimglob() and enable globstar option".
Bram Moolenaarc7247912008-01-13 12:54:11 +00006863 * STYLE_ECHO: space separated.
6864 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 */
6866 if (num_pat == 1 && *pat[0] == '`'
6867 && (len = STRLEN(pat[0])) > 2
6868 && *(pat[0] + len - 1) == '`')
6869 shell_style = STYLE_BT;
6870 else if ((len = STRLEN(p_sh)) >= 3)
6871 {
6872 if (STRCMP(p_sh + len - 3, "csh") == 0)
6873 shell_style = STYLE_GLOB;
6874 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6875 shell_style = STYLE_PRINT;
6876 }
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006877 if (shell_style == STYLE_ECHO)
6878 {
6879 if (strstr((char *)gettail(p_sh), "bash") != NULL)
6880 shell_style = STYLE_GLOBSTAR;
6881 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
6882 shell_style = STYLE_VIMGLOB;
6883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884
Bram Moolenaar0f873732019-12-05 20:28:46 +01006885 // Compute the length of the command. We need 2 extra bytes: for the
6886 // optional '&' and for the NUL.
6887 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006889 if (shell_style == STYLE_VIMGLOB)
6890 len += STRLEN(sh_vimglob_func);
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006891 else if (shell_style == STYLE_GLOBSTAR)
6892 len += STRLEN(sh_vimglob_func)
6893 + STRLEN(sh_globstar_opt);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006894
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006895 for (i = 0; i < num_pat; ++i)
6896 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006897 // Count the length of the patterns in the same way as they are put in
6898 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006899#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006900 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006901#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006902 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006903 for (j = 0; pat[i][j] != NUL; ++j)
6904 {
6905 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006906 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006907 ++len;
6908 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006909#endif
6910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 command = alloc(len);
6912 if (command == NULL)
6913 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006914 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 vim_free(tempname);
6916 return FAIL;
6917 }
6918
6919 /*
6920 * Build the shell command:
6921 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6922 * recognizes this).
6923 * - Add the shell command to print the expanded names.
6924 * - Add the temp file name.
6925 * - Add the file name patterns.
6926 */
6927 if (shell_style == STYLE_BT)
6928 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006929 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006930 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006931 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006933 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006934 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006936 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006938 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 *p = ' ';
6940 }
6941 STRCAT(command, ">");
6942 }
6943 else
6944 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006945 STRCPY(command, "");
6946 if (shell_style == STYLE_GLOB)
6947 {
6948 // Assume the nonomatch option is valid only for csh like shells,
6949 // otherwise, this may set the positional parameters for the shell,
6950 // e.g. "$*".
6951 if (flags & EW_NOTFOUND)
6952 STRCAT(command, "set nonomatch; ");
6953 else
6954 STRCAT(command, "unset nonomatch; ");
6955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 if (shell_style == STYLE_GLOB)
6957 STRCAT(command, "glob >");
6958 else if (shell_style == STYLE_PRINT)
6959 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006960 else if (shell_style == STYLE_VIMGLOB)
6961 STRCAT(command, sh_vimglob_func);
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006962 else if (shell_style == STYLE_GLOBSTAR)
6963 {
6964 STRCAT(command, sh_globstar_opt);
6965 STRCAT(command, sh_vimglob_func);
6966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 else
6968 STRCAT(command, "echo >");
6969 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006970
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006972
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 if (shell_style != STYLE_BT)
6974 for (i = 0; i < num_pat; ++i)
6975 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006976 // When using system() always add extra quotes, because the shell
6977 // is started twice. Otherwise put a backslash before special
6978 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979#ifdef USE_SYSTEM
6980 STRCAT(command, " \"");
6981 STRCAT(command, pat[i]);
6982 STRCAT(command, "\"");
6983#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006984 int intick = FALSE;
6985
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 p = command + STRLEN(command);
6987 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006988 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006989 {
6990 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006991 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006992 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6993 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006994 // Remove a backslash, take char literally. But keep
6995 // backslash inside backticks, before a special character
6996 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006997 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006998 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6999 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007000 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00007001 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00007002 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02007003 else if (!intick
7004 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
7005 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007006 // Put a backslash before a special character, but not
7007 // when inside ``. And not for $var when EW_KEEPDOLLAR is
7008 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00007009 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00007010
Bram Moolenaar0f873732019-12-05 20:28:46 +01007011 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00007012 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00007013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 *p = NUL;
7015#endif
7016 }
7017 if (flags & EW_SILENT)
7018 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01007019 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007020 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021
7022 /*
7023 * Using zsh -G: If a pattern has no matches, it is just deleted from
7024 * the argument list, otherwise zsh gives an error message and doesn't
7025 * expand any other pattern.
7026 */
7027 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007028 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029
7030 /*
7031 * If we use -f then shell variables set in .cshrc won't get expanded.
7032 * vi can do it, so we will too, but it is only necessary if there is a "$"
7033 * in one of the patterns, otherwise we can still use the fast option.
7034 */
7035 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007036 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
7038 /*
7039 * execute the shell command
7040 */
7041 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
7042
Bram Moolenaar0f873732019-12-05 20:28:46 +01007043 // When running in the background, give it some time to create the temp
7044 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01007045 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02007046 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047
Bram Moolenaar0f873732019-12-05 20:28:46 +01007048 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 show_shell_mess = TRUE;
7050 vim_free(command);
7051
Bram Moolenaar0f873732019-12-05 20:28:46 +01007052 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 {
7054 mch_remove(tempname);
7055 vim_free(tempname);
7056 /*
7057 * With interactive completion, the error message is not printed.
7058 * However with USE_SYSTEM, I don't know how to turn off error messages
7059 * from the shell, so screen may still get messed up -- webb.
7060 */
7061#ifndef USE_SYSTEM
7062 if (!(flags & EW_SILENT))
7063#endif
7064 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007065 redraw_later_clear(); // probably messed up screen
7066 msg_putchar('\n'); // clear bottom line quickly
7067 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068#ifdef USE_SYSTEM
7069 if (!(flags & EW_SILENT))
7070#endif
7071 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00007072 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01007073 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 }
7075 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007076 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
7077 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 if (shell_style == STYLE_BT)
7079 return FAIL;
7080 goto notfound;
7081 }
7082
7083 /*
7084 * read the names from the file into memory
7085 */
7086 fd = fopen((char *)tempname, READBIN);
7087 if (fd == NULL)
7088 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007089 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 if (!(flags & EW_SILENT))
7091 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00007092 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01007093 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 }
7095 vim_free(tempname);
7096 goto notfound;
7097 }
7098 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007099 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02007101 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007102 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02007103 buffer = NULL;
7104 else
7105 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 if (buffer == NULL)
7107 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007108 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 mch_remove(tempname);
7110 vim_free(tempname);
7111 fclose(fd);
7112 return FAIL;
7113 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02007114 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 i = fread((char *)buffer, 1, len, fd);
7116 fclose(fd);
7117 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00007118 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007120 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007121 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 vim_free(tempname);
7123 vim_free(buffer);
7124 return FAIL;
7125 }
7126 vim_free(tempname);
7127
Bram Moolenaar1eed5322019-02-26 17:03:54 +01007128# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01007129 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02007131 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
7133 *p++ = buffer[i];
7134 len = p - buffer;
7135# endif
7136
7137
Bram Moolenaar0f873732019-12-05 20:28:46 +01007138 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 if (shell_style == STYLE_ECHO)
7140 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007141 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007143 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 {
7145 while (*p != ' ' && *p != '\n')
7146 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007147 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 }
7149 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007150 // file names are separated with NL
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02007151 else if (shell_style == STYLE_BT ||
7152 shell_style == STYLE_VIMGLOB ||
7153 shell_style == STYLE_GLOBSTAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007155 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007157 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 {
7159 while (*p != '\n' && *p != NUL)
7160 ++p;
7161 if (*p != NUL)
7162 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007163 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 }
7165 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007166 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 else
7168 {
7169 /*
7170 * Some versions of zsh use spaces instead of NULs to separate
7171 * results. Only do this when there is no NUL before the end of the
7172 * buffer, otherwise we would never be able to use file names with
7173 * embedded spaces when zsh does use NULs.
7174 * When we found a NUL once, we know zsh is OK, set did_find_nul and
7175 * don't check for spaces again.
7176 */
7177 check_spaces = FALSE;
7178 if (shell_style == STYLE_PRINT && !did_find_nul)
7179 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007180 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007181 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01007182 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 did_find_nul = TRUE;
7184 else
7185 check_spaces = TRUE;
7186 }
7187
7188 /*
7189 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
7190 * already is one, for STYLE_GLOB it needs to be added.
7191 */
7192 if (len && buffer[len - 1] == NUL)
7193 --len;
7194 else
7195 buffer[len] = NUL;
7196 i = 0;
7197 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007198 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {
7200 ++i;
7201 *p = NUL;
7202 }
7203 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007204 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 }
7206 if (i == 0)
7207 {
7208 /*
7209 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
7210 * /bin/sh will happily expand it to nothing rather than returning an
7211 * error; and hey, it's good to check anyway -- webb.
7212 */
7213 vim_free(buffer);
7214 goto notfound;
7215 }
7216 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007217 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 if (*file == NULL)
7219 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007220 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 vim_free(buffer);
7222 return FAIL;
7223 }
7224
7225 /*
7226 * Isolate the individual file names.
7227 */
7228 p = buffer;
7229 for (i = 0; i < *num_file; ++i)
7230 {
7231 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007232 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00007233 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02007234 || shell_style == STYLE_VIMGLOB || shell_style == STYLE_GLOBSTAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00007236 while (!(shell_style == STYLE_ECHO && *p == ' ')
7237 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007239 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 *p = NUL;
7241 else
7242 {
7243 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007244 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 }
7246 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007247 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007249 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007251 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 }
7253 }
7254
7255 /*
7256 * Move the file names to allocated memory.
7257 */
7258 for (j = 0, i = 0; i < *num_file; ++i)
7259 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007260 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7262 continue;
7263
Bram Moolenaar0f873732019-12-05 20:28:46 +01007264 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 dir = (mch_isdir((*file)[i]));
7266 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7267 continue;
7268
Bram Moolenaar0f873732019-12-05 20:28:46 +01007269 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007270 if (!dir && (flags & EW_EXEC)
7271 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007272 continue;
7273
Bram Moolenaar964b3742019-05-24 18:54:09 +02007274 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 if (p)
7276 {
7277 STRCPY(p, (*file)[i]);
7278 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007279 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 (*file)[j++] = p;
7281 }
7282 }
7283 vim_free(buffer);
7284 *num_file = j;
7285
Bram Moolenaar0f873732019-12-05 20:28:46 +01007286 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007288 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 goto notfound;
7290 }
7291
7292 return OK;
7293
7294notfound:
7295 if (flags & EW_NOTFOUND)
7296 return save_patterns(num_pat, pat, num_file, file);
7297 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298}
7299
Bram Moolenaar0f873732019-12-05 20:28:46 +01007300#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007303save_patterns(
7304 int num_pat,
7305 char_u **pat,
7306 int *num_file,
7307 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308{
7309 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007310 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007312 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 if (*file == NULL)
7314 return FAIL;
7315 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007316 {
7317 s = vim_strsave(pat[i]);
7318 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007319 // Be compatible with expand_filename(): halve the number of
7320 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007321 backslash_halve(s);
7322 (*file)[i] = s;
7323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 *num_file = num_pat;
7325 return OK;
7326}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328/*
7329 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7330 * expand.
7331 */
7332 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007333mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007335 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 if (*p == '\\' && p[1] != NUL)
7338 ++p;
7339 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 if (vim_strchr((char_u *)
7341#ifdef VMS
7342 "*?%"
7343#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345#endif
7346 , *p) != NULL)
7347 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 }
7349 return FALSE;
7350}
7351
7352/*
7353 * Return TRUE if the string "p" contains a wildcard.
7354 * Don't recognize '~' at the end as a wildcard.
7355 */
7356 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007357mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007359 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 if (*p == '\\' && p[1] != NUL)
7362 ++p;
7363 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 if (vim_strchr((char_u *)
7365#ifdef VMS
7366 "*?%$"
7367#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369#endif
7370 , *p) != NULL
7371 || (*p == '~' && p[1] != NUL))
7372 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 }
7374 return FALSE;
7375}
7376
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007378have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379{
7380 int i;
7381
7382 for (i = 0; i < num; i++)
7383 if (mch_has_wildcard(file[i]))
7384 return 1;
7385 return 0;
7386}
7387
7388 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007389have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391 int i;
7392
7393 for (i = 0; i < num; i++)
7394 if (vim_strchr(file[i], '$') != NULL)
7395 return TRUE;
7396 return FALSE;
7397}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007399#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400/*
7401 * Scaled-down version of rename(), which is missing in Xenix.
7402 * This version can only move regular files and will fail if the
7403 * destination exists.
7404 */
7405 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007406mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407{
7408 struct stat st;
7409
Bram Moolenaar0f873732019-12-05 20:28:46 +01007410 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007412 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007414 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 return 0;
7416 return -1;
7417}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007418#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007420#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007421# if defined(DYNAMIC_GPM) || defined(PROTO)
7422/*
7423 * Initialize Gpm's symbols for dynamic linking.
7424 * Must be called only if libgpm_hinst is NULL.
7425 */
7426 static int
7427load_libgpm(void)
7428{
7429 libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL);
7430
7431 if (libgpm_hinst == NULL)
7432 {
7433 if (p_verbose > 0)
7434 smsg_attr(HL_ATTR(HLF_W),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007435 _("Could not load gpm library: %s"), dlerror());
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007436 return FAIL;
7437 }
7438
7439 if (
7440 (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL
7441 || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL
7442 || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL
7443 || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL
7444 || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL
7445 )
7446 {
7447 semsg(_(e_could_not_load_library_str_str), "gpm", dlerror());
7448 dlclose(libgpm_hinst);
7449 libgpm_hinst = NULL;
7450 dll_gpm_flag = NULL;
7451 dll_gpm_fd = NULL;
7452 return FAIL;
7453 }
7454 return OK;
7455}
7456
7457 int
7458gpm_available(void)
7459{
7460 return libgpm_hinst != NULL || load_libgpm() == OK;
7461}
7462# endif // DYNAMIC_GPM
7463
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464/*
7465 * Initializes connection with gpm (if it isn't already opened)
7466 * Return 1 if succeeded (or connection already opened), 0 if failed
7467 */
7468 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007469gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007471 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007473#ifdef DYNAMIC_GPM
7474 if (!gpm_available())
7475 return 0;
7476#endif
7477
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007478 if (gpm_flag)
7479 return 1; // already open
7480
7481 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7482 gpm_connect.defaultMask = ~GPM_HARD;
7483 // Default handling for mouse move
7484 gpm_connect.minMod = 0; // Handle any modifier keys
7485 gpm_connect.maxMod = 0xffff;
7486 if (Gpm_Open(&gpm_connect, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007488 // gpm library tries to handling TSTP causes
7489 // problems. Anyways, we close connection to Gpm whenever
7490 // we are going to suspend or starting an external process
7491 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007492# ifdef SIGTSTP
ichizok378447f2023-05-11 22:25:42 +01007493 mch_signal(SIGTSTP, restricted ? SIG_IGN : sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007494# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007495 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007497 if (gpm_fd == -2)
Julio Bcc59d622024-04-04 21:55:10 +02007498 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007499 Gpm_Close(); // We don't want to talk to xterm via gpm
Julio Bcc59d622024-04-04 21:55:10 +02007500
7501 // Gpm_Close fails to properly restore the WINCH and TSTP handlers,
7502 // leading to Vim ignoring resize signals. We have to re-initialize
7503 // these handlers again here.
7504# ifdef SIGWINCH
7505 mch_signal(SIGWINCH, sig_winch);
7506# endif
7507# ifdef SIGTSTP
7508 mch_signal(SIGTSTP, restricted ? SIG_IGN : sig_tstp);
7509# endif
7510 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007511 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512}
7513
7514/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007515 * Returns TRUE if the GPM mouse is enabled.
7516 */
7517 int
7518gpm_enabled(void)
7519{
7520 return gpm_flag && gpm_fd >= 0;
7521}
7522
7523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 */
7526 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007527gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007529 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 Gpm_Close();
7531}
7532
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007533/*
7534 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007536 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 */
7538 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007539mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540{
7541 int button;
7542 static Gpm_Event gpm_event;
7543 char_u string[6];
7544 int_u vim_modifiers;
7545 int row,col;
7546 unsigned char buttons_mask;
7547 unsigned char gpm_modifiers;
7548 static unsigned char old_buttons = 0;
7549
7550 Gpm_GetEvent(&gpm_event);
7551
7552#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007553 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 if (hold_gui_events)
7555 return 0;
7556#endif
7557
7558 row = gpm_event.y - 1;
7559 col = gpm_event.x - 1;
7560
Bram Moolenaar0f873732019-12-05 20:28:46 +01007561 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 string[1] = 'M';
7563 string[2] = 'G';
7564 switch (GPM_BARE_EVENTS(gpm_event.type))
7565 {
7566 case GPM_DRAG:
7567 string[3] = MOUSE_DRAG;
7568 break;
7569 case GPM_DOWN:
7570 buttons_mask = gpm_event.buttons & ~old_buttons;
7571 old_buttons = gpm_event.buttons;
7572 switch (buttons_mask)
7573 {
7574 case GPM_B_LEFT:
7575 button = MOUSE_LEFT;
7576 break;
7577 case GPM_B_MIDDLE:
7578 button = MOUSE_MIDDLE;
7579 break;
7580 case GPM_B_RIGHT:
7581 button = MOUSE_RIGHT;
7582 break;
7583 default:
7584 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007585 // Don't know what to do. Can more than one button be
7586 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 }
7588 string[3] = (char_u)(button | 0x20);
7589 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7590 break;
7591 case GPM_UP:
7592 string[3] = MOUSE_RELEASE;
7593 old_buttons &= ~gpm_event.buttons;
7594 break;
7595 default:
7596 return 0;
7597 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007598 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 gpm_modifiers = gpm_event.modifiers;
7600 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007601 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7602 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7603 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7605 vim_modifiers |= MOUSE_SHIFT;
7606
7607 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7608 vim_modifiers |= MOUSE_CTRL;
7609 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7610 vim_modifiers |= MOUSE_ALT;
7611 string[3] |= vim_modifiers;
7612 string[4] = (char_u)(col + ' ' + 1);
7613 string[5] = (char_u)(row + ' ' + 1);
7614 add_to_input_buf(string, 6);
7615 return 6;
7616}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007617#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007619#ifdef FEAT_SYSMOUSE
7620/*
7621 * Initialize connection with sysmouse.
7622 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7623 * output, any sysmouse output than will be processed via sig_sysmouse().
7624 * Return OK if succeeded, FAIL if failed.
7625 */
7626 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007627sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007628{
7629 struct mouse_info mouse;
7630
7631 mouse.operation = MOUSE_MODE;
7632 mouse.u.mode.mode = 0;
7633 mouse.u.mode.signal = SIGUSR2;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007634 if (ioctl(1, CONS_MOUSECTL, &mouse) == -1)
7635 return FAIL;
7636
ichizok378447f2023-05-11 22:25:42 +01007637 mch_signal(SIGUSR2, sig_sysmouse);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007638 mouse.operation = MOUSE_SHOW;
7639 ioctl(1, CONS_MOUSECTL, &mouse);
7640 return OK;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007641}
7642
7643/*
7644 * Stop processing SIGUSR2 signals, and also make sure that
7645 * virtual console do not send us any sysmouse related signal.
7646 */
7647 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007648sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007649{
7650 struct mouse_info mouse;
7651
ichizok378447f2023-05-11 22:25:42 +01007652 mch_signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007653 mouse.operation = MOUSE_MODE;
7654 mouse.u.mode.mode = 0;
7655 mouse.u.mode.signal = 0;
7656 ioctl(1, CONS_MOUSECTL, &mouse);
7657}
7658
7659/*
7660 * Gets info from sysmouse and adds special keys to input buf.
7661 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007662 static void
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007663sig_sysmouse SIGDEFARG(sigarg)
7664{
7665 struct mouse_info mouse;
7666 struct video_info video;
7667 char_u string[6];
7668 int row, col;
7669 int button;
7670 int buttons;
7671 static int oldbuttons = 0;
7672
7673#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007674 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007675 if (hold_gui_events)
7676 return;
7677#endif
7678
7679 mouse.operation = MOUSE_GETINFO;
7680 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7681 && ioctl(1, FBIO_MODEINFO, &video) != -1
7682 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7683 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7684 {
7685 row = mouse.u.data.y / video.vi_cheight;
7686 col = mouse.u.data.x / video.vi_cwidth;
7687 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007688 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007689 string[1] = 'M';
7690 string[2] = 'S';
7691 if (oldbuttons == buttons && buttons != 0)
7692 {
7693 button = MOUSE_DRAG;
7694 }
7695 else
7696 {
7697 switch (buttons)
7698 {
7699 case 0:
7700 button = MOUSE_RELEASE;
7701 break;
7702 case 1:
7703 button = MOUSE_LEFT;
7704 break;
7705 case 2:
7706 button = MOUSE_MIDDLE;
7707 break;
7708 case 4:
7709 button = MOUSE_RIGHT;
7710 break;
7711 default:
7712 return;
7713 }
7714 oldbuttons = buttons;
7715 }
7716 string[3] = (char_u)(button);
7717 string[4] = (char_u)(col + ' ' + 1);
7718 string[5] = (char_u)(row + ' ' + 1);
7719 add_to_input_buf(string, 6);
7720 }
7721 return;
7722}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007723#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007724
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007726typedef char_u * (*STRPROCSTR)(char_u *);
7727typedef char_u * (*INTPROCSTR)(int);
7728typedef int (*STRPROCINT)(char_u *);
7729typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730
7731/*
7732 * Call a DLL routine which takes either a string or int param
7733 * and returns an allocated string.
7734 */
7735 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007736mch_libcall(
7737 char_u *libname,
7738 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007739 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007740 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007741 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007742 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743{
7744# if defined(USE_DLOPEN)
7745 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007746 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747# else
7748 shl_t hinstLib;
7749# endif
7750 STRPROCSTR ProcAdd;
7751 INTPROCSTR ProcAddI;
7752 char_u *retval_str = NULL;
7753 int retval_int = 0;
7754 int success = FALSE;
7755
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007756 /*
7757 * Get a handle to the DLL module.
7758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007760 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007761 (void)dlerror();
7762
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 hinstLib = dlopen((char *)libname, RTLD_LAZY
7764# ifdef RTLD_LOCAL
7765 | RTLD_LOCAL
7766# endif
7767 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007768 if (hinstLib == NULL)
7769 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007770 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007771 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007772 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007773 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775# else
7776 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7777# endif
7778
Bram Moolenaar0f873732019-12-05 20:28:46 +01007779 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 if (hinstLib != NULL)
7781 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007782# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 /*
7784 * Catch a crash when calling the library function. For example when
7785 * using a number where a string pointer is expected.
7786 */
7787 mch_startjmp();
7788 if (SETJMP(lc_jump_env) != 0)
7789 {
7790 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007791# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007792 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007793# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 mch_didjmp();
7795 }
7796 else
7797# endif
7798 {
7799 retval_str = NULL;
7800 retval_int = 0;
7801
7802 if (argstring != NULL)
7803 {
7804# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007805 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007806 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807# else
7808 if (shl_findsym(&hinstLib, (const char *)funcname,
7809 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7810 ProcAdd = NULL;
7811# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007812 if ((success = (ProcAdd != NULL
7813# if defined(USE_DLOPEN)
7814 && dlerr == NULL
7815# endif
7816 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {
7818 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007819 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 else
7821 retval_str = (ProcAdd)(argstring);
7822 }
7823 }
7824 else
7825 {
7826# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007827 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007828 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829# else
7830 if (shl_findsym(&hinstLib, (const char *)funcname,
7831 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7832 ProcAddI = NULL;
7833# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007834 if ((success = (ProcAddI != NULL
7835# if defined(USE_DLOPEN)
7836 && dlerr == NULL
7837# endif
7838 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {
7840 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007841 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 else
7843 retval_str = (ProcAddI)(argint);
7844 }
7845 }
7846
Bram Moolenaar0f873732019-12-05 20:28:46 +01007847 // Save the string before we free the library.
7848 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 if (string_result == NULL)
7850 *number_result = retval_int;
7851 else if (retval_str != NULL
7852 && retval_str != (char_u *)1
7853 && retval_str != (char_u *)-1)
7854 *string_result = vim_strsave(retval_str);
7855 }
7856
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007857# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 mch_endjmp();
7859# ifdef SIGHASARG
7860 if (lc_signal != 0)
7861 {
7862 int i;
7863
Bram Moolenaar0f873732019-12-05 20:28:46 +01007864 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 for (i = 0; signal_info[i].sig != -1; i++)
7866 if (lc_signal == signal_info[i].sig)
7867 break;
Bram Moolenaar50809a42023-05-20 16:39:07 +01007868 semsg(_(e_got_sig_str_in_libcall), signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 }
7870# endif
7871# endif
7872
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007874 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007875 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007876 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007877
Bram Moolenaar0f873732019-12-05 20:28:46 +01007878 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 (void)dlclose(hinstLib);
7880# else
7881 (void)shl_unload(hinstLib);
7882# endif
7883 }
7884
7885 if (!success)
7886 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007887 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 return FAIL;
7889 }
7890
7891 return OK;
7892}
7893#endif
7894
7895#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007896static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897static int xterm_button;
7898
7899/*
7900 * Setup a dummy window for X selections in a terminal.
7901 */
7902 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007903setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904{
7905 int z = 0;
7906 char *strp = "";
7907 Widget AppShell;
7908
7909 if (!x_connect_to_server())
7910 return;
7911
7912 open_app_context();
7913 if (app_context != NULL && xterm_Shell == (Widget)0)
7914 {
Dominique Pellé4927bc72023-09-24 16:12:07 +02007915 int (*oldhandler)(Display*, XErrorEvent*);
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007916# if defined(USING_SETJMP)
Dominique Pellé4927bc72023-09-24 16:12:07 +02007917 int (*oldIOhandler)(Display*);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007918# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007919# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007920 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921
7922 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007923 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924# endif
7925
Bram Moolenaar0f873732019-12-05 20:28:46 +01007926 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 oldhandler = XSetErrorHandler(x_error_check);
7928
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007929# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007930 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7932 mch_startjmp();
7933 if (SETJMP(lc_jump_env) != 0)
7934 {
7935 mch_didjmp();
7936 xterm_dpy = NULL;
7937 }
7938 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {
7941 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7942 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007943 if (xterm_dpy != NULL)
7944 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007945# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007947# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 }
7949
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007950# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007951 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007953# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007954 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 (void)XSetErrorHandler(oldhandler);
7956
7957 if (xterm_dpy == NULL)
7958 {
7959 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007960 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 return;
7962 }
7963
Bram Moolenaar0f873732019-12-05 20:28:46 +01007964 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 (void)XSetIOErrorHandler(x_IOerror_handler);
7966
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007967# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007969 {
7970 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007971 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007972 verbose_leave();
7973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974# endif
7975
Bram Moolenaar0f873732019-12-05 20:28:46 +01007976 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7978 applicationShellWidgetClass, xterm_dpy,
7979 NULL);
7980 if (AppShell == (Widget)0)
7981 return;
7982 xterm_Shell = XtVaCreatePopupShell("VIM",
7983 topLevelShellWidgetClass, AppShell,
7984 XtNmappedWhenManaged, 0,
7985 XtNwidth, 1,
7986 XtNheight, 1,
7987 NULL);
7988 if (xterm_Shell == (Widget)0)
7989 return;
7990
7991 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007992 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (x11_display == NULL)
7994 x11_display = xterm_dpy;
7995
7996 XtRealizeWidget(xterm_Shell);
7997 XSync(xterm_dpy, False);
7998 xterm_update();
7999 }
8000 if (xterm_Shell != (Widget)0)
8001 {
8002 clip_init(TRUE);
8003 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
8004 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01008005 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 if (test_x11_window(xterm_dpy) == FAIL)
8007 x11_window = 0;
8008 if (x11_window != 0)
8009 xterm_trace = 0;
8010 }
8011}
8012
8013 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008014start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015{
8016 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
8017 return;
8018 xterm_trace = 1;
8019 xterm_button = button;
8020 do_xterm_trace();
8021}
8022
8023
8024 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008025stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026{
8027 if (xterm_trace < 0)
8028 return;
8029 xterm_trace = 0;
8030}
8031
8032/*
8033 * Query the xterm pointer and generate mouse termcodes if necessary
8034 * return TRUE if dragging is active, else FALSE
8035 */
8036 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008037do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038{
8039 Window root, child;
8040 int root_x, root_y;
8041 int win_x, win_y;
8042 int row, col;
8043 int_u mask_return;
8044 char_u buf[50];
8045 char_u *strp;
8046 long got_hints;
8047 static char_u *mouse_code;
8048 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
8049 static int prev_row = 0, prev_col = 0;
8050 static XSizeHints xterm_hints;
8051
8052 if (xterm_trace <= 0)
8053 return FALSE;
8054
8055 if (xterm_trace == 1)
8056 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008057 // Get the hints just before tracking starts. The font size might
8058 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00008059 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
8060 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 || xterm_hints.width_inc <= 1
8062 || xterm_hints.height_inc <= 1)
8063 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008064 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 return FALSE;
8066 }
8067
Bram Moolenaar0f873732019-12-05 20:28:46 +01008068 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 mouse_code = find_termcode(mouse_name);
8070 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02008071 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 xterm_trace = 2;
8073
Bram Moolenaar0f873732019-12-05 20:28:46 +01008074 // Find the offset of the chars, there might be a scrollbar on the
8075 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
8077 &win_x, &win_y, &mask_return);
8078 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
8079 - (xterm_hints.height_inc / 2);
8080 if (xterm_hints.y <= xterm_hints.height_inc / 2)
8081 xterm_hints.y = 2;
8082 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
8083 - (xterm_hints.width_inc / 2);
8084 if (xterm_hints.x <= xterm_hints.width_inc / 2)
8085 xterm_hints.x = 2;
8086 return TRUE;
8087 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02008088 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {
8090 xterm_trace = 0;
8091 return FALSE;
8092 }
8093
8094 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
8095 &win_x, &win_y, &mask_return);
8096
8097 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
8098 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
8099 if (row == prev_row && col == prev_col)
8100 return TRUE;
8101
8102 STRCPY(buf, mouse_code);
8103 strp = buf + STRLEN(buf);
8104 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
8105 *strp++ = (char_u)(col + ' ' + 1);
8106 *strp++ = (char_u)(row + ' ' + 1);
8107 *strp = 0;
8108 add_to_input_buf(buf, STRLEN(buf));
8109
8110 prev_row = row;
8111 prev_col = col;
8112 return TRUE;
8113}
8114
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02008115# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116/*
8117 * Destroy the display, window and app_context. Required for GTK.
8118 */
8119 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008120clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121{
8122 if (xterm_Shell != (Widget)0)
8123 {
8124 XtDestroyWidget(xterm_Shell);
8125 xterm_Shell = (Widget)0;
8126 }
8127 if (xterm_dpy != NULL)
8128 {
Bram Moolenaare8208012008-06-20 09:59:25 +00008129# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008130 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00008132# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 if (x11_display == xterm_dpy)
8134 x11_display = NULL;
8135 xterm_dpy = NULL;
8136 }
Bram Moolenaare8208012008-06-20 09:59:25 +00008137# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 if (app_context != (XtAppContext)NULL)
8139 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008140 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 XtDestroyApplicationContext(app_context);
8142 app_context = (XtAppContext)NULL;
8143 }
Bram Moolenaare8208012008-06-20 09:59:25 +00008144# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145}
8146# endif
8147
8148/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01008149 * Catch up with GUI or X events.
8150 */
8151 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008152clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01008153{
8154# ifdef FEAT_GUI
8155 if (gui.in_use)
8156 gui_mch_update();
8157 else
8158# endif
8159 if (xterm_Shell != (Widget)0)
8160 xterm_update();
8161}
8162
8163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 * Catch up with any queued X events. This may put keyboard input into the
8165 * input buffer, call resize call-backs, trigger timers etc. If there is
8166 * nothing in the X event queue (& no timers pending), then we return
8167 * immediately.
8168 */
8169 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008170xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171{
8172 XEvent event;
8173
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008174 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008176 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008178 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008179 break;
8180
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008181 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008182 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008183 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008184 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008185#ifdef FEAT_CLIENTSERVER
8186 {
8187 XPropertyEvent *e = (XPropertyEvent *)&event;
8188
8189 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008191 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008194 XtDispatchEvent(&event);
8195 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008196 else
8197 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008198 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008199 XtAppProcessEvent(app_context, mask);
8200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 }
8202}
8203
8204 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008205clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
8207 if (xterm_Shell != (Widget)0)
8208 return clip_x11_own_selection(xterm_Shell, cbd);
8209 return FAIL;
8210}
8211
8212 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008213clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214{
8215 if (xterm_Shell != (Widget)0)
8216 clip_x11_lose_selection(xterm_Shell, cbd);
8217}
8218
8219 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008220clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221{
8222 if (xterm_Shell != (Widget)0)
8223 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
8224}
8225
8226 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008227clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228{
8229 clip_x11_set_selection(cbd);
8230}
8231#endif
8232
8233
8234#if defined(USE_XSMP) || defined(PROTO)
8235/*
8236 * Code for X Session Management Protocol.
8237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238
8239# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240/*
8241 * This is our chance to ask the user if they want to save,
8242 * or abort the logout
8243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008245xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246{
Bram Moolenaare1004402020-10-24 20:49:43 +02008247 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 int cancel_shutdown = False;
8249
Bram Moolenaare1004402020-10-24 20:49:43 +02008250 save_cmod_flags = cmdmod.cmod_flags;
8251 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01008252 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01008253 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02008255 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01008256 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 out_flush();
8258
Bram Moolenaar0f873732019-12-05 20:28:46 +01008259 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 SmcInteractDone(smc_conn, cancel_shutdown);
8261
Bram Moolenaar0f873732019-12-05 20:28:46 +01008262 // Finish off
8263 // Only end save-yourself here if we're not cancelling shutdown;
8264 // we'll get a cancelled callback later in which we'll end it.
8265 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 if (!cancel_shutdown)
8267 {
8268 xsmp.save_yourself = False;
8269 SmcSaveYourselfDone(smc_conn, True);
8270 }
8271}
8272# endif
8273
8274/*
8275 * Callback that starts save-yourself.
8276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008278xsmp_handle_save_yourself(
8279 SmcConn smc_conn,
8280 SmPointer client_data UNUSED,
8281 int save_type UNUSED,
8282 Bool shutdown,
8283 int interact_style UNUSED,
8284 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008286 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 if (xsmp.save_yourself)
8288 SmcSaveYourselfDone(smc_conn, True);
8289 xsmp.save_yourself = True;
8290 xsmp.shutdown = shutdown;
8291
Bram Moolenaar0f873732019-12-05 20:28:46 +01008292 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01008294 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295
8296 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008297 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298
8299# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008300 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008302 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 SmcInteractRequest(smc_conn, SmDialogError,
8304 xsmp_handle_interaction, client_data);
8305 else
8306# endif
8307 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008308 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 SmcSaveYourselfDone(smc_conn, True);
8310 xsmp.save_yourself = False;
8311 }
8312}
8313
8314
8315/*
8316 * Callback to warn us of imminent death.
8317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008319xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
8321 xsmp_close();
8322
Bram Moolenaar0f873732019-12-05 20:28:46 +01008323 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 getout_preserve_modified(0);
8325}
8326
8327
8328/*
8329 * Callback to tell us that save-yourself has completed.
8330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008332xsmp_save_complete(
8333 SmcConn smc_conn UNUSED,
8334 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
8336 xsmp.save_yourself = False;
8337}
8338
8339
8340/*
8341 * Callback to tell us that an instigated shutdown was cancelled
8342 * (maybe even by us)
8343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008345xsmp_shutdown_cancelled(
8346 SmcConn smc_conn,
8347 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348{
8349 if (xsmp.save_yourself)
8350 SmcSaveYourselfDone(smc_conn, True);
8351 xsmp.save_yourself = False;
8352 xsmp.shutdown = False;
8353}
8354
8355
8356/*
8357 * Callback to tell us that a new ICE connection has been established.
8358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008360xsmp_ice_connection(
8361 IceConn iceConn,
8362 IcePointer clientData UNUSED,
8363 Bool opening,
8364 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008366 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 if (opening)
8368 {
8369 xsmp_icefd = IceConnectionNumber(iceConn);
8370 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8371 }
8372}
8373
8374
Bram Moolenaar0f873732019-12-05 20:28:46 +01008375// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008377xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378{
8379 Bool rep;
8380
8381 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8382 == IceProcessMessagesIOError)
8383 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008384 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008386 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 xsmp_close();
8388 return FAIL;
8389 }
8390 else
8391 return OK;
8392}
8393
8394static int dummy;
8395
Bram Moolenaar0f873732019-12-05 20:28:46 +01008396// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 void
8398xsmp_init(void)
8399{
8400 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 SmcCallbacks smcallbacks;
8402#if 0
8403 SmPropValue smname;
8404 SmProp smnameprop;
8405 SmProp *smprops[1];
8406#endif
8407
8408 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008409 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410
8411 xsmp.save_yourself = xsmp.shutdown = False;
8412
Bram Moolenaar0f873732019-12-05 20:28:46 +01008413 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8415 smcallbacks.save_yourself.client_data = NULL;
8416 smcallbacks.die.callback = xsmp_die;
8417 smcallbacks.die.client_data = NULL;
8418 smcallbacks.save_complete.callback = xsmp_save_complete;
8419 smcallbacks.save_complete.client_data = NULL;
8420 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8421 smcallbacks.shutdown_cancelled.client_data = NULL;
8422
Bram Moolenaar0f873732019-12-05 20:28:46 +01008423 // Set up a watch on ICE connection creations. The "dummy" argument is
8424 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8426 {
8427 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008428 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 return;
8430 }
8431
Bram Moolenaar0f873732019-12-05 20:28:46 +01008432 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 xsmp.smcconn = SmcOpenConnection(
8434 NULL,
8435 NULL,
8436 SmProtoMajor,
8437 SmProtoMinor,
8438 SmcSaveYourselfProcMask | SmcDieProcMask
8439 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8440 &smcallbacks,
8441 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008442 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008443 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 errorstring);
8445 if (xsmp.smcconn == NULL)
8446 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008448 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008449 char errorreport[132];
8450
8451 // If the message is too long it might not be NUL terminated. Add
8452 // a NUL at the end to make sure we don't go over the end.
8453 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008454 vim_snprintf(errorreport, sizeof(errorreport),
8455 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008456 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 return;
8459 }
8460 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8461
8462#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008463 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 smname.value = "vim";
8465 smname.length = 3;
8466 smnameprop.name = "SmProgram";
8467 smnameprop.type = "SmARRAY8";
8468 smnameprop.num_vals = 1;
8469 smnameprop.vals = &smname;
8470
8471 smprops[0] = &smnameprop;
8472 SmcSetProperties(xsmp.smcconn, 1, smprops);
8473#endif
8474}
8475
8476
Bram Moolenaar0f873732019-12-05 20:28:46 +01008477// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008479xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008481 if (xsmp_icefd == -1)
8482 return;
8483
8484 SmcCloseConnection(xsmp.smcconn, 0, NULL);
8485 if (xsmp.clientid != NULL)
8486 free(xsmp.clientid);
8487 xsmp.clientid = NULL;
8488 xsmp_icefd = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008490#endif // USE_XSMP
Paul Ollis65745772022-06-05 16:55:54 +01008491
8492#if defined(FEAT_RELTIME) || defined(PROTO)
Bram Moolenaar08210f82023-04-13 19:15:54 +01008493# if defined(PROF_NSEC) || defined(PROTO)
Paul Ollis65745772022-06-05 16:55:54 +01008494/*
8495 * Implement timeout with timer_create() and timer_settime().
8496 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008497static volatile sig_atomic_t timeout_flag = FALSE;
8498static timer_t timer_id;
8499static int timer_created = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008500
8501/*
8502 * Callback for when the timer expires.
8503 */
8504 static void
8505set_flag(union sigval _unused UNUSED)
8506{
8507 timeout_flag = TRUE;
8508}
8509
8510/*
8511 * Stop any active timeout.
8512 */
8513 void
8514stop_timeout(void)
8515{
8516 static struct itimerspec disarm = {{0, 0}, {0, 0}};
8517
8518 if (timer_created)
8519 {
8520 int ret = timer_settime(timer_id, 0, &disarm, NULL);
8521
8522 if (ret < 0)
8523 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8524 }
8525
8526 // Clear the current timeout flag; any previous timeout should be
8527 // considered _not_ triggered.
8528 timeout_flag = FALSE;
8529}
8530
8531/*
8532 * Start the timeout timer.
8533 *
8534 * The return value is a pointer to a flag that is initialised to FALSE. If the
8535 * timeout expires, the flag is set to TRUE. This will only return pointers to
8536 * static memory; i.e. any pointer returned by this function may always be
8537 * safely dereferenced.
8538 *
8539 * This function is not expected to fail, but if it does it will still return a
8540 * valid flag pointer; the flag will remain stuck as FALSE .
8541 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008542 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008543start_timeout(long msec)
8544{
8545 struct itimerspec interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008546 {0, 0}, // Do not repeat.
8547 {msec / 1000, (msec % 1000) * 1000000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008548 int ret;
8549
8550 // This is really the caller's responsibility, but let's make sure the
8551 // previous timer has been stopped.
8552 stop_timeout();
Paul Ollis65745772022-06-05 16:55:54 +01008553
8554 if (!timer_created)
8555 {
8556 struct sigevent action = {0};
8557
8558 action.sigev_notify = SIGEV_THREAD;
8559 action.sigev_notify_function = set_flag;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008560 ret = timer_create(CLOCK_MONOTONIC, &action, &timer_id);
8561 if (ret < 0)
Paul Ollis65745772022-06-05 16:55:54 +01008562 {
8563 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8564 return &timeout_flag;
8565 }
8566 timer_created = TRUE;
8567 }
8568
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00008569# ifdef FEAT_EVAL
Bram Moolenaar616592e2022-06-17 15:17:10 +01008570 ch_log(NULL, "setting timeout timer to %d sec %ld nsec",
8571 (int)interval.it_value.tv_sec, (long)interval.it_value.tv_nsec);
Bram Moolenaar509ce032022-06-20 11:23:01 +01008572# endif
Paul Ollis65745772022-06-05 16:55:54 +01008573 ret = timer_settime(timer_id, 0, &interval, NULL);
8574 if (ret < 0)
8575 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8576
8577 return &timeout_flag;
8578}
8579
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008580/*
8581 * To be used before fork/exec: delete any created timer.
8582 */
8583 void
8584delete_timer(void)
8585{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008586 if (!timer_created)
8587 return;
8588
8589 timer_delete(timer_id);
8590 timer_created = FALSE;
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008591}
8592
Bram Moolenaar08210f82023-04-13 19:15:54 +01008593# else // PROF_NSEC
Paul Ollis65745772022-06-05 16:55:54 +01008594
8595/*
8596 * Implement timeout with setitimer()
8597 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008598static struct sigaction prev_sigaction;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008599static volatile sig_atomic_t timeout_flag = FALSE;
8600static int timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008601static int timer_handler_active = FALSE;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008602static volatile sig_atomic_t alarm_pending = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008603
8604/*
8605 * Handle SIGALRM for a timeout.
8606 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01008607 static void
Paul Ollis65745772022-06-05 16:55:54 +01008608set_flag SIGDEFARG(sigarg)
8609{
8610 if (alarm_pending)
8611 alarm_pending = FALSE;
8612 else
8613 timeout_flag = TRUE;
8614}
8615
8616/*
8617 * Stop any active timeout.
8618 */
8619 void
8620stop_timeout(void)
8621{
8622 static struct itimerval disarm = {{0, 0}, {0, 0}};
8623 int ret;
8624
8625 if (timer_active)
8626 {
8627 timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008628 ret = setitimer(ITIMER_REAL, &disarm, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008629 if (ret < 0)
8630 // Should only get here as a result of coding errors.
8631 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8632 }
8633
8634 if (timer_handler_active)
8635 {
8636 timer_handler_active = FALSE;
8637 ret = sigaction(SIGALRM, &prev_sigaction, NULL);
8638 if (ret < 0)
8639 // Should only get here as a result of coding errors.
8640 semsg(_(e_could_not_reset_handler_for_timeout_str),
8641 strerror(errno));
8642 }
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008643 timeout_flag = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008644}
8645
8646/*
8647 * Start the timeout timer.
8648 *
8649 * The return value is a pointer to a flag that is initialised to FALSE. If the
8650 * timeout expires, the flag is set to TRUE. This will only return pointers to
8651 * static memory; i.e. any pointer returned by this function may always be
8652 * safely dereferenced.
8653 *
8654 * This function is not expected to fail, but if it does it will still return a
8655 * valid flag pointer; the flag will remain stuck as FALSE .
8656 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008657 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008658start_timeout(long msec)
8659{
8660 struct itimerval interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008661 {0, 0}, // Do not repeat.
8662 {msec / 1000, (msec % 1000) * 1000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008663 struct sigaction handle_alarm;
8664 int ret;
8665 sigset_t sigs;
8666 sigset_t saved_sigs;
8667
8668 // This is really the caller's responsibility, but let's make sure the
8669 // previous timer has been stopped.
8670 stop_timeout();
8671
8672 // There is a small chance that SIGALRM is pending and so the handler must
8673 // ignore it on the first call.
8674 alarm_pending = FALSE;
8675 ret = sigemptyset(&sigs);
8676 ret = ret == 0 ? sigaddset(&sigs, SIGALRM) : ret;
8677 ret = ret == 0 ? sigprocmask(SIG_BLOCK, &sigs, &saved_sigs) : ret;
8678 timeout_flag = FALSE;
8679 ret = ret == 0 ? sigpending(&sigs) : ret;
8680 if (ret == 0)
8681 {
8682 alarm_pending = sigismember(&sigs, SIGALRM);
Bram Moolenaar1f89abf2022-06-06 10:07:01 +01008683 ret = sigprocmask(SIG_SETMASK, &saved_sigs, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008684 }
8685 if (unlikely(ret != 0 || alarm_pending < 0))
8686 {
8687 // Just catching coding errors. Write an error message, but carry on.
8688 semsg(_(e_could_not_check_for_pending_sigalrm_str), strerror(errno));
8689 alarm_pending = FALSE;
8690 }
8691
8692 // Set up the alarm handler first.
8693 ret = sigemptyset(&handle_alarm.sa_mask);
8694 handle_alarm.sa_handler = set_flag;
8695 handle_alarm.sa_flags = 0;
8696 ret = ret == 0 ? sigaction(SIGALRM, &handle_alarm, &prev_sigaction) : ret;
8697 if (ret < 0)
8698 {
8699 // Should only get here as a result of coding errors.
8700 semsg(_(e_could_not_set_handler_for_timeout_str), strerror(errno));
8701 return &timeout_flag;
8702 }
8703 timer_handler_active = TRUE;
8704
8705 // Set up the interval timer once the alarm handler is in place.
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008706 ret = setitimer(ITIMER_REAL, &interval, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008707 if (ret < 0)
8708 {
8709 // Should only get here as a result of coding errors.
8710 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8711 stop_timeout();
8712 return &timeout_flag;
8713 }
8714
8715 timer_active = TRUE;
8716 return &timeout_flag;
8717}
Bram Moolenaar08210f82023-04-13 19:15:54 +01008718# endif // PROF_NSEC
Paul Ollis65745772022-06-05 16:55:54 +01008719#endif // FEAT_RELTIME