blob: f5dea37f9908b58b1b9a6172f69f93aa45c62923 [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{
John Marriottefc41a52025-01-23 20:05:29 +01001838 size_t arglen;
1839
1840 if (eap->arg != NULL && (arglen = STRLEN(eap->arg)) > 0)
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001841 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001842 if (xterm_display_allocated)
1843 vim_free(xterm_display);
John Marriottefc41a52025-01-23 20:05:29 +01001844 xterm_display = (char *)vim_strnsave(eap->arg, arglen);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001845 xterm_display_allocated = TRUE;
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001846 }
1847 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001848 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001849
1850 clear_xterm_clip();
1851 x11_window = 0;
1852 xterm_dpy_retry_count = 5; // Try reconnecting five times
1853 may_restore_clipboard();
1854}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855#endif
1856
1857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 * Test if "dpy" and x11_window are valid by getting the window title.
1859 * I don't actually want it yet, so there may be a simpler call to use, but
1860 * this will cause the error handler x_error_check() to be called if anything
1861 * is wrong, such as the window pointer being invalid (as can happen when the
1862 * user changes his DISPLAY, but not his WINDOWID) -- webb
1863 */
1864 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001865test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
Dominique Pellé4927bc72023-09-24 16:12:07 +02001867 int (*old_handler)(Display*, XErrorEvent*);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 XTextProperty text_prop;
1869
1870 old_handler = XSetErrorHandler(x_error_check);
1871 got_x_error = FALSE;
1872 if (XGetWMName(dpy, x11_window, &text_prop))
1873 XFree((void *)text_prop.value);
1874 XSync(dpy, False);
1875 (void)XSetErrorHandler(old_handler);
1876
1877 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001878 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
1880 return (got_x_error ? FAIL : OK);
1881}
1882#endif
1883
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884
1885#ifdef FEAT_X11
1886
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001887static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888
1889/*
1890 * try to get x11 window and display
1891 *
1892 * return FAIL for failure, OK otherwise
1893 */
1894 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001895get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 char *winid;
1898 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001899#define XD_NONE 0 // x11_display not set here
1900#define XD_HERE 1 // x11_display opened here
1901#define XD_GUI 2 // x11_display used from gui.dpy
1902#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 static int x11_display_from = XD_NONE;
1904 static int did_set_error_handler = FALSE;
1905
1906 if (!did_set_error_handler)
1907 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001908 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 (void)XSetErrorHandler(x_error_handler);
1910 did_set_error_handler = TRUE;
1911 }
1912
Bram Moolenaar9372a112005-12-06 19:59:18 +00001913#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (gui.in_use)
1915 {
1916 /*
1917 * If the X11 display was opened here before, for the window where Vim
1918 * was started, close that one now to avoid a memory leak.
1919 */
1920 if (x11_display_from == XD_HERE && x11_display != NULL)
1921 {
1922 XCloseDisplay(x11_display);
1923 x11_display_from = XD_NONE;
1924 }
1925 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1926 {
1927 x11_display_from = XD_GUI;
1928 return OK;
1929 }
1930 x11_display = NULL;
1931 return FAIL;
1932 }
1933 else if (x11_display_from == XD_GUI)
1934 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001935 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 x11_window = 0;
1937 x11_display = NULL;
1938 x11_display_from = XD_NONE;
1939 }
1940#endif
1941
Bram Moolenaar0f873732019-12-05 20:28:46 +01001942 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (!x_connect_to_server())
1944 return FAIL;
1945
1946 /*
1947 * If WINDOWID not set, should try another method to find out
1948 * what the current window number is. The only code I know for
1949 * this is very complicated.
1950 * We assume that zero is invalid for WINDOWID.
1951 */
1952 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1953 x11_window = (Window)atol(winid);
1954
1955#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001956 if (xterm_dpy == x11_display)
1957 // x11_display may have been set to xterm_dpy elsewhere
1958 x11_display_from = XD_XTERM;
1959
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 if (xterm_dpy != NULL && x11_window != 0)
1961 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001962 // We may have checked it already, but Gnome terminal can move us to
1963 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001964 if (x11_display_from != XD_XTERM)
1965 {
1966 /*
1967 * If the X11 display was opened here before, for the window where
1968 * Vim was started, close that one now to avoid a memory leak.
1969 */
1970 if (x11_display_from == XD_HERE && x11_display != NULL)
1971 XCloseDisplay(x11_display);
1972 x11_display = xterm_dpy;
1973 x11_display_from = XD_XTERM;
1974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 if (test_x11_window(x11_display) == FAIL)
1976 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001977 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 x11_window = 0;
1979 x11_display = NULL;
1980 x11_display_from = XD_NONE;
1981 return FAIL;
1982 }
1983 return OK;
1984 }
1985#endif
1986
1987 if (x11_window == 0 || x11_display == NULL)
1988 result = -1;
1989
Bram Moolenaar0f873732019-12-05 20:28:46 +01001990 if (result != -1) // Have already been here and set this
1991 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992
1993 if (x11_window != 0 && x11_display == NULL)
1994 {
1995#ifdef SET_SIG_ALARM
ichizok378447f2023-05-11 22:25:42 +01001996 sighandler_T sig_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001998#ifdef ELAPSED_FUNC
1999 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
2001 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01002002 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003#endif
2004
2005#ifdef SET_SIG_ALARM
2006 /*
2007 * Opening the Display may hang if the DISPLAY setting is wrong, or
2008 * the network connection is bad. Set an alarm timer to get out.
2009 */
2010 sig_alarm_called = FALSE;
ichizok378447f2023-05-11 22:25:42 +01002011 sig_save = mch_signal(SIGALRM, sig_alarm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 alarm(2);
2013#endif
2014 x11_display = XOpenDisplay(NULL);
2015
2016#ifdef SET_SIG_ALARM
2017 alarm(0);
ichizok378447f2023-05-11 22:25:42 +01002018 mch_signal(SIGALRM, sig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01002020 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021#endif
2022 if (x11_display != NULL)
2023 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002024# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00002026 {
2027 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002028 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00002029 verbose_leave();
2030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031# endif
2032 if (test_x11_window(x11_display) == FAIL)
2033 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002034 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 x11_window = 0;
2036 XCloseDisplay(x11_display);
2037 x11_display = NULL;
2038 }
2039 else
2040 x11_display_from = XD_HERE;
2041 }
2042 }
2043 if (x11_window == 0 || x11_display == NULL)
2044 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02002045
2046# ifdef FEAT_EVAL
2047 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
2048# endif
2049
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 return (result = OK);
2051}
2052
2053/*
2054 * Determine original x11 Window Title
2055 */
2056 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002057get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058{
Bram Moolenaar47136d72004-10-12 20:02:24 +00002059 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060}
2061
2062/*
2063 * Determine original x11 Window icon
2064 */
2065 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002066get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067{
2068 int retval = FALSE;
2069
2070 retval = get_x11_thing(FALSE, test_only);
2071
Bram Moolenaar0f873732019-12-05 20:28:46 +01002072 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 if (oldicon == NULL && !test_only)
2074 {
2075 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002076 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002078 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
2080
2081 return retval;
2082}
2083
2084 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002085get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002086 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01002087 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088{
2089 XTextProperty text_prop;
2090 int retval = FALSE;
2091 Status status;
2092
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002093 if (get_x11_windis() != OK)
2094 return FALSE;
2095
2096 // Get window/icon name if any
2097 if (get_title)
2098 status = XGetWMName(x11_display, x11_window, &text_prop);
2099 else
2100 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2101
2102 /*
2103 * If terminal is xterm, then x11_window may be a child window of the
2104 * outer xterm window that actually contains the window/icon name, so
2105 * keep traversing up the tree until a window with a title/icon is
2106 * found.
2107 */
2108 // Previously this was only done for xterm and alike. I don't see a
2109 // reason why it would fail for other terminal emulators.
2110 // if (term_is_xterm)
2111 Window root;
2112 Window parent;
2113 Window win = x11_window;
2114 Window *children;
2115 unsigned int num_children;
2116
2117 while (!status || text_prop.value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002119 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2120 &num_children))
2121 break;
2122 if (children)
2123 XFree((void *)children);
2124 if (parent == root || parent == 0)
2125 break;
2126
2127 win = parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002129 status = XGetWMName(x11_display, win, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002131 status = XGetWMIconName(x11_display, win, &text_prop);
2132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002134 if (status && text_prop.value != NULL)
2135 {
2136 retval = TRUE;
2137 if (!test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002139 if (get_title)
2140 vim_free(oldtitle);
2141 else
2142 vim_free(oldicon);
2143 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002146 oldtitle = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002148 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002150 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002152 char **cl;
2153 Status transform_status;
2154 int n = 0;
2155
2156 transform_status = XmbTextPropertyToTextList(x11_display,
2157 &text_prop,
2158 &cl, &n);
2159 if (transform_status >= Success && n > 0 && cl[0])
2160 {
2161 if (get_title)
2162 oldtitle = vim_strsave((char_u *) cl[0]);
2163 else
2164 oldicon = vim_strsave((char_u *) cl[0]);
2165 XFreeStringList(cl);
2166 }
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002167 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 if (get_title)
2170 oldtitle = vim_strsave((char_u *)text_prop.value);
2171 else
2172 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002176 XFree((void *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 }
2178 return retval;
2179}
2180
Bram Moolenaar0f873732019-12-05 20:28:46 +01002181// Xutf8 functions are not available on older systems. Note that on some
2182// systems X_HAVE_UTF8_STRING may be defined in a header file but
2183// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2184// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002185#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002186# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187# define USE_UTF8_STRING
2188# endif
2189#endif
2190
2191/*
2192 * Set x11 Window Title
2193 *
2194 * get_x11_windis() must be called before this and have returned OK
2195 */
2196 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002197set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002199 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2200 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2201 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202#ifdef USE_UTF8_STRING
2203 if (enc_utf8)
2204 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2205 NULL, NULL, 0, NULL, NULL, NULL);
2206 else
2207#endif
2208 {
2209#if XtSpecificationRelease >= 4
2210# ifdef FEAT_XFONTSET
2211 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2212 NULL, NULL, 0, NULL, NULL, NULL);
2213# else
2214 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002215 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
Bram Moolenaar0f873732019-12-05 20:28:46 +01002217 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002218 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 XSetWMProperties(x11_display, x11_window, &text_prop,
2220 NULL, NULL, 0, NULL, NULL, NULL);
2221# endif
2222#else
2223 XStoreName(x11_display, x11_window, (char *)title);
2224#endif
2225 }
2226 XFlush(x11_display);
2227}
2228
2229/*
2230 * Set x11 Window icon
2231 *
2232 * get_x11_windis() must be called before this and have returned OK
2233 */
2234 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002235set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002237 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238#ifdef USE_UTF8_STRING
2239 if (enc_utf8)
2240 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2241 NULL, 0, NULL, NULL, NULL);
2242 else
2243#endif
2244 {
2245#if XtSpecificationRelease >= 4
2246# ifdef FEAT_XFONTSET
2247 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2248 NULL, 0, NULL, NULL, NULL);
2249# else
2250 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002251 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002253 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2255 NULL, 0, NULL, NULL, NULL);
2256# endif
2257#else
2258 XSetIconName(x11_display, x11_window, (char *)icon);
2259#endif
2260 }
2261 XFlush(x11_display);
2262}
2263
Bram Moolenaar0f873732019-12-05 20:28:46 +01002264#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002267get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
2269 return FALSE;
2270}
2271
2272 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002273get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 if (!test_only)
2276 {
2277 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002278 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002280 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
2282 return FALSE;
2283}
2284
Bram Moolenaar0f873732019-12-05 20:28:46 +01002285#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002288mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
2290 return get_x11_title(TRUE);
2291}
2292
2293 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002294mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
2296 return get_x11_icon(TRUE);
2297}
2298
2299/*
2300 * Set the window title and icon.
2301 */
2302 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002303mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304{
2305 int type = 0;
2306 static int recursive = 0;
2307
Bram Moolenaar0f873732019-12-05 20:28:46 +01002308 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002310 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 return;
2312
Bram Moolenaar0f873732019-12-05 20:28:46 +01002313 // When one of the X11 functions causes a deadly signal, we get here again
2314 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (recursive)
2316 return;
2317 ++recursive;
2318
2319 /*
2320 * if the window ID and the display is known, we may use X11 calls
2321 */
2322#ifdef FEAT_X11
2323 if (get_x11_windis() == OK)
2324 type = 1;
lilydjwg6e0a18f2024-01-29 20:54:28 +01002325#endif
2326#if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002327 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 if (gui.in_use)
2329 type = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#endif
2331
2332 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002333 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 * than x11 calls, because the x11 calls don't always work
2335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 if ((type || *T_TS != NUL) && title != NULL)
2337 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002338 if (oldtitle_outdated)
2339 {
2340 oldtitle_outdated = FALSE;
2341 VIM_CLEAR(oldtitle);
2342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (oldtitle == NULL
2344#ifdef FEAT_GUI
2345 && !gui.in_use
2346#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002347 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 (void)get_x11_title(FALSE);
2349
Bram Moolenaar0f873732019-12-05 20:28:46 +01002350 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 term_settitle(title);
2352#ifdef FEAT_X11
2353 else
2354# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002355 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002357 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002359#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002360 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 else
2362 gui_mch_settitle(title, icon);
2363#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002364 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366
2367 if ((type || *T_CIS != NUL) && icon != NULL)
2368 {
2369 if (oldicon == NULL
2370#ifdef FEAT_GUI
2371 && !gui.in_use
2372#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002373 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 get_x11_icon(FALSE);
2375
2376 if (*T_CIS != NUL)
2377 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002378 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002380 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 out_flush();
2382 }
2383#ifdef FEAT_X11
2384 else
2385# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002386 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002388 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389#endif
2390 did_set_icon = TRUE;
2391 }
2392 --recursive;
2393}
2394
2395/*
2396 * Restore the window/icon title.
2397 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002398 * SAVE_RESTORE_TITLE only restore title
2399 * SAVE_RESTORE_ICON only restore icon
2400 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 */
2402 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002403mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404{
Bram Moolenaardac13472019-09-16 21:06:21 +02002405 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002406
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002407 // Only restore the title or icon when it has been set.
2408 // When using "oldtitle" make a copy, it might be freed halfway.
2409 char_u *title = ((which & SAVE_RESTORE_TITLE) && unix_did_set_title)
2410 ? (oldtitle ? oldtitle : p_titleold) : NULL;
2411 char_u *tofree = NULL;
2412 if (title == oldtitle && oldtitle != NULL)
2413 {
2414 tofree = vim_strsave(title);
2415 if (tofree != NULL)
2416 title = tofree;
2417 }
2418 mch_settitle(title,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002419 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002420 vim_free(tofree);
Bram Moolenaar40385db2018-08-07 22:31:44 +02002421
Bram Moolenaare5c83282019-05-03 23:15:37 +02002422 if (do_push_pop)
2423 {
2424 // pop and push from/to the stack
2425 term_pop_title(which);
2426 term_push_title(which);
2427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428}
2429
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431/*
2432 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002433 * This matches "xterm.*", thus "xterm-256color", "xterm-kitty", etc.
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002434 * Do not consider "xterm-kitty" an xterm, it is not fully xterm compatible,
2435 * using the "xterm-kitty" terminfo entry should work better.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002436 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 */
2438 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002439vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
2441 if (name == NULL)
2442 return FALSE;
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002443 return ((STRNICMP(name, "xterm", 5) == 0
2444 && STRNICMP(name, "xterm-kitty", 11) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 || STRNICMP(name, "nxterm", 6) == 0
2446 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002447 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002449 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 || STRCMP(name, "builtin_xterm") == 0);
2451}
2452
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002453#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2454/*
2455 * Return TRUE if "name" appears to be that of a terminal
2456 * known to support the xterm-style mouse protocol.
2457 * Relies on term_is_xterm having been set to its correct value.
2458 */
2459 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002460use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002461{
2462 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002463 && (term_is_xterm
2464 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002465 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002466 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002467 || STRICMP(name, "st") == 0
2468 || STRNICMP(name, "st-", 3) == 0
2469 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002470}
2471#endif
2472
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473/*
2474 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2475 * Return 1 for "xterm".
2476 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002477 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002478 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 */
2480 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002481use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002483 if (ttym_flags == TTYM_SGR)
2484 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002485 if (ttym_flags == TTYM_URXVT)
2486 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 if (ttym_flags == TTYM_XTERM2)
2488 return 2;
2489 if (ttym_flags == TTYM_XTERM)
2490 return 1;
2491 return 0;
2492}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002494/*
2495 * Return TRUE if "name" is an iris-ansi terminal name.
2496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002498vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
2500 if (name == NULL)
2501 return FALSE;
2502 return (STRNICMP(name, "iris-ansi", 9) == 0
2503 || STRCMP(name, "builtin_iris-ansi") == 0);
2504}
2505
Dominique Pellee764d1b2023-03-12 21:20:59 +00002506#if defined(VMS) || defined(PROTO)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002507/*
2508 * Return TRUE if "name" is a vt300-like terminal name.
2509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002511vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513 if (name == NULL)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002514 return FALSE;
2515 // Actually all ANSI compatible terminals should be here.
2516 // Catch at least VT1xx - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002517 return ((STRNICMP(name, "vt", 2) == 0
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002518 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 || STRCMP(name, "builtin_vt320") == 0);
2520}
Dominique Pellee764d1b2023-03-12 21:20:59 +00002521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
2523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 * Insert user name in s[len].
2525 * Return OK if a name found.
2526 */
2527 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002528mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
2530#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002531 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 return OK;
2533#else
2534 return mch_get_uname(getuid(), s, len);
2535#endif
2536}
2537
2538/*
2539 * Insert user name for "uid" in s[len].
2540 * Return OK if a name found.
2541 */
2542 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002543mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2546 struct passwd *pw;
2547
2548 if ((pw = getpwuid(uid)) != NULL
2549 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2550 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002551 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 return OK;
2553 }
2554#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002555 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2556 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557}
2558
2559/*
2560 * Insert host name is s[len].
2561 */
2562
2563#ifdef HAVE_SYS_UTSNAME_H
2564 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002565mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566{
2567 struct utsname vutsname;
2568
2569 if (uname(&vutsname) < 0)
2570 *s = NUL;
2571 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002572 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002574#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
2576# ifdef HAVE_SYS_SYSTEMINFO_H
2577# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2578# endif
2579
2580 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002581mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
2583# ifdef VAXC
2584 vaxc$gethostname((char *)s, len);
2585# else
2586 gethostname((char *)s, len);
2587# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002588 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002590#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
2592/*
2593 * return process ID
2594 */
2595 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002596mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597{
2598 return (long)getpid();
2599}
2600
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002601/*
2602 * return TRUE if process "pid" is still running
2603 */
2604 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002605mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002606{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002607 // If there is no error the process must be running.
2608 if (kill(pid, 0) == 0)
2609 return TRUE;
2610#ifdef ESRCH
2611 // If the error is ESRCH then the process is not running.
2612 if (errno == ESRCH)
2613 return FALSE;
2614#endif
2615 // If the process is running and owned by another user we get EPERM. With
2616 // other errors the process might be running, assuming it is then.
2617 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002618}
2619
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002622strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623{
2624 extern int sys_nerr;
2625 extern char *sys_errlist[];
2626 static char er[20];
2627
2628 if (err > 0 && err < sys_nerr)
2629 return (sys_errlist[err]);
John Marriottefc41a52025-01-23 20:05:29 +01002630 snprintf(er, sizeof(er), "Error %d", err);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 return er;
2632}
2633#endif
2634
2635/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002636 * Get name of current directory into buffer "buf" of length "len" bytes.
2637 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 * Return OK for success, FAIL for failure.
2639 */
2640 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002641mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642{
2643#if defined(USE_GETCWD)
2644 if (getcwd((char *)buf, len) == NULL)
2645 {
2646 STRCPY(buf, strerror(errno));
2647 return FAIL;
2648 }
2649 return OK;
2650#else
2651 return (getwd((char *)buf) != NULL ? OK : FAIL);
2652#endif
2653}
2654
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002656 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 *
2658 * return FAIL for failure, OK for success
2659 */
2660 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002661mch_FullName(
2662 char_u *fname,
2663 char_u *buf,
2664 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002665 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
John Marriottefc41a52025-01-23 20:05:29 +01002667 int buflen = 0;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002668#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002670 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 char_u olddir[MAXPATHL];
2673 char_u *p;
2674 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002675#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002676 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2677 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002678#endif
2679
Bram Moolenaar38323e42007-03-06 19:22:53 +00002680#ifdef VMS
2681 fname = vms_fixfilename(fname);
2682#endif
2683
Bram Moolenaara2442432007-04-26 14:26:37 +00002684#ifdef __CYGWIN__
2685 /*
2686 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2687 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002688# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002689 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2690 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002691 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2692 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002693# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002694 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002695# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002696 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002697#endif
2698
Bram Moolenaar0f873732019-12-05 20:28:46 +01002699 // Expand it if forced or not an absolute path.
2700 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002701 if ((force || !mch_isFullName(fname))
2702 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
zeertzjq1ee74202024-07-12 07:29:14 +02002704 if (p == NULL && STRCMP(fname, "..") == 0)
2705 // Handle ".." without path separators.
2706 p = fname + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 /*
2708 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002709 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 * This will get the correct path name with "../" things.
2711 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002712 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002714 if (STRCMP(p, "/..") == 0)
zeertzjq1ee74202024-07-12 07:29:14 +02002715 // For "/path/dir/.." include the "/..".
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002716 p += 3;
2717
Bram Moolenaar38323e42007-03-06 19:22:53 +00002718#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 /*
2720 * Use fchdir() if possible, it's said to be faster and more
2721 * reliable. But on SunOS 4 it might not work. Check this by
2722 * doing a fchdir() right now.
2723 */
2724 if (!dont_fchdir)
2725 {
2726 fd = open(".", O_RDONLY | O_EXTRA, 0);
2727 if (fd >= 0 && fchdir(fd) < 0)
2728 {
2729 close(fd);
2730 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002731 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
2733 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
Bram Moolenaar0f873732019-12-05 20:28:46 +01002736 // Only change directory when we are sure we can return to where
2737 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002739#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 (mch_dirname(olddir, MAXPATHL) == FAIL
2743 || mch_chdir((char *)olddir) != 0))
2744 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002745 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 retval = FAIL;
2747 }
2748 else
2749 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002750 // The directory is copied into buf[], to be able to remove
2751 // the file name without changing it (could be a string in
2752 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 if (p - fname >= len)
2754 retval = FAIL;
2755 else
2756 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002757 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002759 {
2760 // Path does not exist (yet). For a full path fail,
2761 // will use the path as-is. For a relative path use
2762 // the current directory and append the file name.
2763 if (mch_isFullName(fname))
2764 retval = FAIL;
2765 else
2766 p = NULL;
2767 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002768 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002770 else
2771 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 *buf = NUL;
2773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775 }
2776 if (mch_dirname(buf, len) == FAIL)
2777 {
2778 retval = FAIL;
2779 *buf = NUL;
2780 }
2781 if (p != NULL)
2782 {
John Marriottefc41a52025-01-23 20:05:29 +01002783 int l;
2784
Bram Moolenaar38323e42007-03-06 19:22:53 +00002785#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 if (fd >= 0)
2787 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002788 if (p_verbose >= 5)
2789 {
2790 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002791 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002792 verbose_leave();
2793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
2796 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 l = mch_chdir((char *)olddir);
2799 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002800 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
itchyny051a40c2021-10-20 10:00:05 +01002802#ifdef HAVE_FCHDIR
2803 if (fd >= 0)
2804 close(fd);
2805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806
John Marriottefc41a52025-01-23 20:05:29 +01002807 buflen = (int)STRLEN(buf);
2808 if (buflen >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002809 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002810#ifndef VMS
John Marriottefc41a52025-01-23 20:05:29 +01002811 else if (buflen > 0 && buf[buflen - 1] != PATHSEP && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 && STRCMP(fname, ".") != 0)
John Marriottefc41a52025-01-23 20:05:29 +01002813 {
2814 STRCPY(buf + buflen, PATHSEPSTR);
2815 buflen += STRLEN_LITERAL(PATHSEPSTR);
2816 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002819
John Marriottefc41a52025-01-23 20:05:29 +01002820 if (buflen == 0)
2821 buflen = (int)STRLEN(buf);
2822
Bram Moolenaar0f873732019-12-05 20:28:46 +01002823 // Catch file names which are too long.
John Marriottefc41a52025-01-23 20:05:29 +01002824 if (retval == FAIL || (int)(buflen + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 return FAIL;
2826
Bram Moolenaar0f873732019-12-05 20:28:46 +01002827 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (STRCMP(fname, ".") != 0)
John Marriottefc41a52025-01-23 20:05:29 +01002829 STRCPY(buf + buflen, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
2831 return OK;
2832}
2833
2834/*
2835 * Return TRUE if "fname" does not depend on the current directory.
2836 */
2837 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002838mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002840#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 return ( fname[0] == '/' || fname[0] == '.' ||
2842 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2843 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2844 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002845#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847#endif
2848}
2849
Bram Moolenaar24552be2005-12-10 20:17:30 +00002850#if defined(USE_FNAME_CASE) || defined(PROTO)
2851/*
2852 * Set the case of the file name, if it already exists. This will cause the
2853 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002854 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002855 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002856 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002857fname_case(
2858 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002859 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002860{
2861 struct stat st;
2862 char_u *slash, *tail;
John Marriottefc41a52025-01-23 20:05:29 +01002863 size_t taillen;
Bram Moolenaar24552be2005-12-10 20:17:30 +00002864 DIR *dirp;
2865 struct dirent *dp;
2866
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002867 if (mch_lstat((char *)name, &st) < 0)
2868 return;
2869
2870 // Open the directory where the file is located.
2871 slash = vim_strrchr(name, '/');
2872 if (slash == NULL)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002873 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002874 dirp = opendir(".");
2875 tail = name;
2876 }
2877 else
2878 {
2879 *slash = NUL;
2880 dirp = opendir((char *)name);
2881 *slash = '/';
2882 tail = slash + 1;
2883 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002884
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002885 if (dirp == NULL)
2886 return;
2887
John Marriottefc41a52025-01-23 20:05:29 +01002888 taillen = STRLEN(tail);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002889 while ((dp = readdir(dirp)) != NULL)
2890 {
2891 // Only accept names that differ in case and are the same byte
2892 // length. TODO: accept different length name.
2893 if (STRICMP(tail, dp->d_name) == 0
John Marriottefc41a52025-01-23 20:05:29 +01002894 && taillen == STRLEN(dp->d_name))
Bram Moolenaar24552be2005-12-10 20:17:30 +00002895 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002896 char_u newname[MAXPATHL + 1];
2897 struct stat st2;
2898
2899 // Verify the inode is equal.
2900 vim_strncpy(newname, name, MAXPATHL);
2901 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2902 MAXPATHL - (tail - name));
2903 if (mch_lstat((char *)newname, &st2) >= 0
2904 && st.st_ino == st2.st_ino
2905 && st.st_dev == st2.st_dev)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002906 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002907 STRCPY(tail, dp->d_name);
2908 break;
Bram Moolenaar24552be2005-12-10 20:17:30 +00002909 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002910 }
2911 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002912
2913 closedir(dirp);
Bram Moolenaar24552be2005-12-10 20:17:30 +00002914}
2915#endif
2916
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917/*
2918 * Get file permissions for 'name'.
2919 * Returns -1 when it doesn't exist.
2920 */
2921 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002922mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
2924 struct stat statb;
2925
Bram Moolenaar0f873732019-12-05 20:28:46 +01002926 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927#ifdef VMS
2928 if (stat((char *)vms_fixfilename(name), &statb))
2929#else
2930 if (stat((char *)name, &statb))
2931#endif
2932 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002933#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002934 // The top bit makes the value negative, which means the file doesn't
2935 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002936 return statb.st_mode & ~S_ADDACE;
2937#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940}
2941
2942/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002943 * Set file permission for "name" to "perm".
2944 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 */
2946 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002947mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948{
2949 return (chmod((char *)
2950#ifdef VMS
2951 vms_fixfilename(name),
2952#else
2953 name,
2954#endif
2955 (mode_t)perm) == 0 ? OK : FAIL);
2956}
2957
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002958#if defined(HAVE_FCHMOD) || defined(PROTO)
2959/*
2960 * Set file permission for open file "fd" to "perm".
2961 * Return FAIL for failure, OK otherwise.
2962 */
2963 int
2964mch_fsetperm(int fd, long perm)
2965{
2966 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2967}
2968#endif
2969
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970#if defined(HAVE_ACL) || defined(PROTO)
2971# ifdef HAVE_SYS_ACL_H
2972# include <sys/acl.h>
2973# endif
2974# ifdef HAVE_SYS_ACCESS_H
2975# include <sys/access.h>
2976# endif
2977
2978# ifdef HAVE_SOLARIS_ACL
2979typedef struct vim_acl_solaris_T {
2980 int acl_cnt;
2981 aclent_t *acl_entry;
2982} vim_acl_solaris_T;
2983# endif
2984
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002985#if defined(HAVE_SELINUX) || defined(PROTO)
2986/*
2987 * Copy security info from "from_file" to "to_file".
2988 */
2989 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002990mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002991{
2992 if (from_file == NULL)
2993 return;
2994
2995 if (selinux_enabled == -1)
2996 selinux_enabled = is_selinux_enabled();
2997
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002998 if (selinux_enabled <= 0)
2999 return;
3000
3001 // Use "char *" instead of "security_context_t" to avoid a deprecation
3002 // warning.
3003 char *from_context = NULL;
3004 char *to_context = NULL;
3005
3006 if (getfilecon((char *)from_file, &from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003007 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003008 // If the filesystem doesn't support extended attributes,
3009 // the original had no special security context and the
3010 // target cannot have one either.
3011 if (errno == EOPNOTSUPP)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003012 return;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003013
3014 msg_puts(_("\nCould not get security context for "));
3015 msg_outtrans(from_file);
3016 msg_putchar('\n');
3017 return;
3018 }
3019 if (getfilecon((char *)to_file, &to_context) < 0)
3020 {
3021 msg_puts(_("\nCould not get security context for "));
3022 msg_outtrans(to_file);
3023 msg_putchar('\n');
3024 freecon (from_context);
3025 return ;
3026 }
3027 if (strcmp(from_context, to_context) != 0)
3028 {
3029 if (setfilecon((char *)to_file, from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003030 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003031 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003032 msg_outtrans(to_file);
3033 msg_putchar('\n');
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003034 }
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003035 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003036 freecon(to_context);
3037 freecon(from_context);
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003038}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003039#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003040
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003041#if defined(HAVE_SMACK) && !defined(PROTO)
3042/*
3043 * Copy security info from "from_file" to "to_file".
3044 */
3045 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01003046mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003047{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02003048 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003049 {
3050 XATTR_NAME_SMACK,
3051 XATTR_NAME_SMACKEXEC,
3052 XATTR_NAME_SMACKMMAP
3053 };
3054
3055 char buffer[SMACK_LABEL_LEN];
3056 const char *name;
3057 int index;
3058 int ret;
3059 ssize_t size;
3060
3061 if (from_file == NULL)
3062 return;
3063
Christian Brabandt5a679b22023-10-16 10:17:13 +02003064 size = listxattr((char *)from_file, NULL, 0);
3065 // not supported or no attributes to copy
Paul R. Tagliamonte14759de2024-03-10 08:35:10 +01003066 if (size <= 0)
Christian Brabandt5a679b22023-10-16 10:17:13 +02003067 return;
3068
John Marriottefc41a52025-01-23 20:05:29 +01003069 for (index = 0 ; index < (int)ARRAY_LENGTH(smack_copied_attributes); index++)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003070 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003071 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003072 name = smack_copied_attributes[index];
3073
Bram Moolenaar0f873732019-12-05 20:28:46 +01003074 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003075 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
3076 if (size >= 0)
3077 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003078 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003079 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
3080 if (ret < 0)
3081 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003082 vim_snprintf((char *)IObuff, IOSIZE,
3083 _("Could not set security context %s for %s"),
3084 name, to_file);
3085 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003086 msg_putchar('\n');
3087 }
3088 }
3089 else
3090 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003091 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003092 switch (errno)
3093 {
3094 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003095 // extended attributes aren't supported or enabled
3096 // should a message be echoed? not sure...
3097 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003098
3099 case ERANGE:
3100 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003101 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003102 vim_snprintf((char *)IObuff, IOSIZE,
3103 _("Could not get security context %s for %s. Removing it!"),
3104 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003105 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003106 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003107 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003108
3109 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003110 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003111 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003112 // Silently ignore errors, apparently this happens when
3113 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003114 break;
3115 }
3116 }
3117 }
3118}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003119#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003120
Christian Brabandte085dfd2023-09-30 12:49:18 +02003121#ifdef FEAT_XATTR
3122/*
3123 * Copy extended attributes from_file to to_file
3124 */
3125 void
3126mch_copy_xattr(char_u *from_file, char_u *to_file)
3127{
3128 char *xattr_buf;
Paul R. Tagliamonte14759de2024-03-10 08:35:10 +01003129 ssize_t size;
3130 ssize_t tsize;
Christian Brabandte085dfd2023-09-30 12:49:18 +02003131 ssize_t keylen, vallen, max_vallen = 0;
3132 char *key;
3133 char *val = NULL;
3134 char *errmsg = NULL;
3135
3136 if (from_file == NULL)
3137 return;
3138
3139 // get the length of the extended attributes
3140 size = listxattr((char *)from_file, NULL, 0);
3141 // not supported or no attributes to copy
Paul R. Tagliamonte14759de2024-03-10 08:35:10 +01003142 if (size <= 0)
Christian Brabandte085dfd2023-09-30 12:49:18 +02003143 return;
3144 xattr_buf = (char*)alloc(size);
3145 if (xattr_buf == NULL)
3146 return;
3147 size = listxattr((char *)from_file, xattr_buf, size);
3148 tsize = size;
3149
3150 errno = 0;
3151
3152 for (int round = 0; round < 2; round++)
3153 {
3154
3155 key = xattr_buf;
3156 if (round == 1)
3157 size = tsize;
3158
3159 while (size > 0)
3160 {
3161 vallen = getxattr((char *)from_file, key,
3162 val, round ? max_vallen : 0);
3163 // only set the attribute in the second round
3164 if (vallen >= 0 && round &&
3165 setxattr((char *)to_file, key, val, vallen, 0) == 0)
3166 ;
3167 else if (errno)
3168 {
3169 switch (errno)
3170 {
3171 case E2BIG:
3172 errmsg = e_xattr_e2big;
3173 goto error_exit;
3174 case ENOTSUP:
Gene C993b1752023-10-02 22:42:26 +02003175 case EACCES:
3176 case EPERM:
3177 break;
Christian Brabandte085dfd2023-09-30 12:49:18 +02003178 case ERANGE:
3179 errmsg = e_xattr_erange;
3180 goto error_exit;
3181 default:
3182 errmsg = e_xattr_other;
3183 goto error_exit;
3184 }
3185 }
3186
3187 if (round == 0 && vallen > max_vallen)
3188 max_vallen = vallen;
3189
3190 // add one for terminating null
3191 keylen = STRLEN(key) + 1;
3192 size -= keylen;
3193 key += keylen;
3194 }
3195 if (round)
3196 break;
3197
3198 val = (char*)alloc(max_vallen + 1);
3199 if (val == NULL)
3200 goto error_exit;
3201
3202 }
3203error_exit:
3204 vim_free(xattr_buf);
3205 vim_free(val);
3206
3207 if (errmsg != NULL)
zeertzjq7ece0362023-10-01 09:07:14 +02003208 emsg(_(errmsg));
Christian Brabandte085dfd2023-09-30 12:49:18 +02003209}
3210#endif
3211
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212/*
3213 * Return a pointer to the ACL of file "fname" in allocated memory.
3214 * Return NULL if the ACL is not available for whatever reason.
3215 */
3216 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003217mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218{
3219 vim_acl_T ret = NULL;
3220#ifdef HAVE_POSIX_ACL
3221 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3222#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003223#ifdef HAVE_SOLARIS_ZFS_ACL
3224 acl_t *aclent;
3225
3226 if (acl_get((char *)fname, 0, &aclent) < 0)
3227 return NULL;
3228 ret = (vim_acl_T)aclent;
3229#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230#ifdef HAVE_SOLARIS_ACL
3231 vim_acl_solaris_T *aclent;
3232
3233 aclent = malloc(sizeof(vim_acl_solaris_T));
John Marriottefc41a52025-01-23 20:05:29 +01003234 if (aclent == NULL)
3235 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3237 {
3238 free(aclent);
3239 return NULL;
3240 }
3241 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
John Marriottefc41a52025-01-23 20:05:29 +01003242 if (aclent->acl_entry == NULL)
3243 {
3244 free(aclent);
3245 return NULL;
3246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3248 {
3249 free(aclent->acl_entry);
3250 free(aclent);
3251 return NULL;
3252 }
3253 ret = (vim_acl_T)aclent;
3254#else
3255#if defined(HAVE_AIX_ACL)
3256 int aclsize;
3257 struct acl *aclent;
3258
3259 aclsize = sizeof(struct acl);
3260 aclent = malloc(aclsize);
John Marriottefc41a52025-01-23 20:05:29 +01003261 if (aclent == NULL)
3262 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3264 {
3265 if (errno == ENOSPC)
3266 {
3267 aclsize = aclent->acl_len;
3268 aclent = realloc(aclent, aclsize);
John Marriottefc41a52025-01-23 20:05:29 +01003269 if (aclent == NULL || statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
3271 free(aclent);
3272 return NULL;
3273 }
3274 }
3275 else
3276 {
3277 free(aclent);
3278 return NULL;
3279 }
3280 }
3281 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003282#endif // HAVE_AIX_ACL
3283#endif // HAVE_SOLARIS_ACL
3284#endif // HAVE_SOLARIS_ZFS_ACL
3285#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 return ret;
3287}
3288
3289/*
3290 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3291 */
3292 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003293mch_set_acl(char_u *fname UNUSED, 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_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3299#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003300#ifdef HAVE_SOLARIS_ZFS_ACL
3301 acl_set((char *)fname, (acl_t *)aclent);
3302#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303#ifdef HAVE_SOLARIS_ACL
3304 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3305 ((vim_acl_solaris_T *)aclent)->acl_entry);
3306#else
3307#ifdef HAVE_AIX_ACL
3308 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
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
3315 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003316mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 if (aclent == NULL)
3319 return;
3320#ifdef HAVE_POSIX_ACL
3321 acl_free((acl_t)aclent);
3322#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003323#ifdef HAVE_SOLARIS_ZFS_ACL
3324 acl_free((acl_t *)aclent);
3325#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326#ifdef HAVE_SOLARIS_ACL
3327 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3328 free(aclent);
3329#else
3330#ifdef HAVE_AIX_ACL
3331 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003332#endif // HAVE_AIX_ACL
3333#endif // HAVE_SOLARIS_ACL
3334#endif // HAVE_SOLARIS_ZFS_ACL
3335#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336}
3337#endif
3338
3339/*
3340 * Set hidden flag for "name".
3341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003343mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003345 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346}
3347
3348/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003349 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 * return FALSE if "name" is not a directory
3351 * return FALSE for error
3352 */
3353 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003354mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
3356 struct stat statb;
3357
Bram Moolenaar0f873732019-12-05 20:28:46 +01003358 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 return FALSE;
3360 if (stat((char *)name, &statb))
3361 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363}
3364
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003365/*
3366 * return TRUE if "name" is a directory, NOT a symlink to a directory
3367 * return FALSE if "name" is not a directory
3368 * return FALSE for error
3369 */
3370 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003371mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003372{
3373 struct stat statb;
3374
Bram Moolenaar0f873732019-12-05 20:28:46 +01003375 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003376 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003377 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003378 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003379 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003380}
3381
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382/*
3383 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3384 */
3385 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003386executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
3388 struct stat st;
3389
3390 if (stat((char *)name, &st))
3391 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003392#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003393 // Like on Unix system file can have executable rights but not necessarily
3394 // be an executable, but on Unix is not a default for an ordinary file to
3395 // have an executable flag - on VMS it is in most cases.
3396 // Therefore, this check does not have any sense - let keep us to the
3397 // conventions instead:
3398 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003399 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003400 int vms_executable = 0;
3401 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3402 {
3403 if (strstr(vms_tolower((char*)name),".exe") != NULL
3404 || strstr(vms_tolower((char*)name),".com")!= NULL)
3405 vms_executable = 1;
3406 }
3407 return vms_executable;
3408#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411}
3412
3413/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003414 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003415 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 * Return -1 if unknown.
3417 */
3418 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003419mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
3421 char_u *buf;
John Marriottefc41a52025-01-23 20:05:29 +01003422 size_t bufsize;
3423 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 char_u *p, *e;
John Marriottefc41a52025-01-23 20:05:29 +01003425 char_u *p_end;
3426 size_t elen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 int retval;
3428
Bram Moolenaar0f873732019-12-05 20:28:46 +01003429 // When "use_path" is false and if it's an absolute or relative path don't
3430 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003431 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003432 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003433 // There must be a path separator, files in the current directory
3434 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003435 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003436 {
3437 if (path != NULL)
3438 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003439 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003440 *path = FullName_save(name, TRUE);
3441 else
3442 *path = vim_strsave(name);
3443 }
3444 return TRUE;
3445 }
3446 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
3449 p = (char_u *)getenv("PATH");
3450 if (p == NULL || *p == NUL)
3451 return -1;
John Marriottefc41a52025-01-23 20:05:29 +01003452 p_end = p + STRLEN(p);
3453 bufsize = STRLEN(name) + (size_t)(p_end - p) + 2;
3454 buf = alloc(bufsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 if (buf == NULL)
3456 return -1;
3457
3458 /*
3459 * Walk through all entries in $PATH to check if "name" exists there and
3460 * is an executable file.
3461 */
3462 for (;;)
3463 {
3464 e = (char_u *)strchr((char *)p, ':');
3465 if (e == NULL)
John Marriottefc41a52025-01-23 20:05:29 +01003466 e = p_end;
3467 elen = (size_t)(e - p);
3468 if (elen <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
John Marriottefc41a52025-01-23 20:05:29 +01003470 p = (char_u *)"./";
3471 elen = STRLEN_LITERAL("./");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
John Marriottefc41a52025-01-23 20:05:29 +01003473 buflen = vim_snprintf((char *)buf, bufsize, "%.*s%s%s",
3474 (int)elen,
3475 p,
3476 (after_pathsep(p, p + elen)) ? "" : PATHSEPSTR,
3477 name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 retval = executable_file(buf);
3479 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003480 {
3481 if (path != NULL)
3482 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003483 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003484 *path = FullName_save(buf, TRUE);
3485 else
John Marriottefc41a52025-01-23 20:05:29 +01003486 *path = vim_strnsave(buf, buflen);
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
3491 if (*e != ':')
3492 break;
3493 p = e + 1;
3494 }
3495
3496 vim_free(buf);
3497 return retval;
3498}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
3500/*
3501 * Check what "name" is:
3502 * NODE_NORMAL: file or directory (or doesn't exist)
3503 * NODE_WRITABLE: writable device, socket, fifo, etc.
3504 * NODE_OTHER: non-writable things
3505 */
3506 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003507mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508{
3509 struct stat st;
3510
3511 if (stat((char *)name, &st))
3512 return NODE_NORMAL;
3513 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3514 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003515 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003517 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 return NODE_WRITABLE;
3519}
3520
3521 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003522mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523{
3524#ifdef HAVE_CHECK_STACK_GROWTH
3525 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 check_stack_growth((char *)&i);
3528
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003529# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 get_stack_limit();
3531# endif
3532
3533#endif
3534
3535 /*
3536 * Setup an alternative stack for signals. Helps to catch signals when
3537 * running out of stack space.
3538 * Use of sigaltstack() is preferred, it's more portable.
3539 * Ignore any errors.
3540 */
3541#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003542 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 init_signal_stack();
3544#endif
3545}
3546
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003547#if defined(EXITFREE) || defined(PROTO)
3548 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003549mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003550{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003551# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3552 if (clip_star.owned)
3553 clip_lose_selection(&clip_star);
3554 if (clip_plus.owned)
3555 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003556# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003557# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003558 if (xterm_Shell != (Widget)0)
3559 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003560# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003561 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003562 if (xterm_dpy != NULL)
3563 XtCloseDisplay(xterm_dpy);
3564 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003565 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003566 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003567# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003568 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003569# endif
3570 }
3571# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003572# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003573# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003574 if (x11_display != NULL
3575# ifdef FEAT_XCLIPBOARD
3576 && x11_display != xterm_dpy
3577# endif
3578 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003579 XCloseDisplay(x11_display);
3580# endif
3581# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003582 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003583# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003584 vim_free(oldtitle);
3585 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003586}
3587#endif
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589/*
3590 * Output a newline when exiting.
3591 * Make sure the newline goes to the same stream as the text.
3592 */
3593 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003594exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003596 if (silent_mode)
3597 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 if (newline_on_exit || msg_didout)
3599 {
3600 if (msg_use_printf())
3601 {
3602 if (info_message)
3603 mch_msg("\n");
3604 else
3605 mch_errmsg("\r\n");
3606 }
3607 else
3608 out_char('\n');
3609 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003610 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003612 restore_cterm_colors(); // get original colors back
3613 msg_clr_eos_force(); // clear the rest of the display
3614 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
3616}
3617
Bram Moolenaarb4151682020-05-11 22:13:28 +02003618#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003619# if (defined(__GNUC__) \
3620 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3621 || (defined(__clang__) && (__clang_major__ >= 12))
3622extern void __gcov_dump(void);
3623extern void __gcov_reset(void);
3624# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3625# else
3626extern void __gcov_flush(void);
3627# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003628#endif
3629
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003631mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632{
3633 exiting = TRUE;
3634
3635#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3636 x11_export_final_selection();
3637#endif
3638
3639#ifdef FEAT_GUI
3640 if (!gui.in_use)
3641#endif
3642 {
3643 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003644 if (!is_not_a_term())
3645 {
3646 // restore xterm title and icon name
3647 mch_restore_title(SAVE_RESTORE_BOTH);
3648 term_pop_title(SAVE_RESTORE_BOTH);
3649 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 /*
3652 * When t_ti is not empty but it doesn't cause swapping terminal
3653 * pages, need to output a newline when msg_didout is set. But when
3654 * t_ti does swap pages it should not go to the shell page. Do this
3655 * before stoptermcap().
3656 */
3657 if (swapping_screen() && !newline_on_exit)
3658 exit_scroll();
3659
Bram Moolenaar0f873732019-12-05 20:28:46 +01003660 // Stop termcap: May need to check for T_CRV response, which
3661 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 stoptermcap();
3663
3664 /*
3665 * A newline is only required after a message in the alternate screen.
3666 * This is set to TRUE by wait_return().
3667 */
3668 if (!swapping_screen() || newline_on_exit)
3669 exit_scroll();
3670
Bram Moolenaar0f873732019-12-05 20:28:46 +01003671 // Cursor may have been switched off without calling starttermcap()
3672 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 if (full_screen)
3674 cursor_on();
3675 }
3676 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003677 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003678
3679#ifdef USE_GCOV_FLUSH
3680 // Flush coverage info before possibly being killed by a deadly signal.
3681 __gcov_flush();
3682#endif
3683
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 may_core_dump();
3685#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 gui_exit(r);
3688#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003689
Bram Moolenaar56718732006-03-15 22:53:57 +00003690#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003691 mac_conv_cleanup();
3692#endif
3693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003695 // A core dump won't be created if the signal handler
3696 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (deadly_signal != 0)
3698 return;
3699#endif
3700
Bram Moolenaar009b2592004-10-24 19:18:58 +00003701#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003702 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003703#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003704
3705#ifdef EXITFREE
3706 free_all_mem();
3707#endif
3708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 exit(r);
3710}
3711
3712 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003713may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714{
3715 if (deadly_signal != 0)
3716 {
ichizok378447f2023-05-11 22:25:42 +01003717 mch_signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003718 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720}
3721
3722#ifndef VMS
3723
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003724/*
3725 * Get the file descriptor to use for tty operations.
3726 */
3727 static int
3728get_tty_fd(int fd)
3729{
3730 int tty_fd = fd;
3731
3732#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3733 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3734 // "fd" when it is a master device.
3735 if (mch_isatty(fd) > 1)
3736 {
3737 char *name;
3738
3739 name = ptsname(fd);
3740 if (name == NULL)
3741 return -1;
3742
3743 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3744 if (tty_fd < 0)
3745 return -1;
3746 }
3747#endif
3748 return tty_fd;
3749}
3750
3751 static int
3752mch_tcgetattr(int fd, void *term)
3753{
3754 int tty_fd;
3755 int retval = -1;
3756
3757 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003758 if (tty_fd < 0)
3759 return -1;
3760
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003761#ifdef NEW_TTY_SYSTEM
3762# ifdef HAVE_TERMIOS_H
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003763 retval = tcgetattr(tty_fd, (struct termios *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003764# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003765 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003766# endif
3767#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003768 // for "old" tty systems
3769 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003770#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003771 if (tty_fd != fd)
3772 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003773 return retval;
3774}
3775
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003777mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
3779 static int first = TRUE;
3780
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003781#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782# ifdef HAVE_TERMIOS_H
3783 static struct termios told;
3784 struct termios tnew;
3785# else
3786 static struct termio told;
3787 struct termio tnew;
3788# endif
3789
3790 if (first)
3791 {
3792 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003793 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795
3796 tnew = told;
3797 if (tmode == TMODE_RAW)
3798 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003799 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003800 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
Anton Sharonov49528da2024-04-14 20:02:24 +02003801 tnew.c_iflag &= ~(ICRNL |
3802 (T_XON == NULL || *T_XON == NUL ? IXON : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003804# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003805 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806# endif
3807 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003808# ifdef ONLCR
3809 // Don't map NL -> CR NL, we do it ourselves.
3810 // Also disable expanding tabs if possible.
3811# ifdef XTABS
3812 tnew.c_oflag &= ~(ONLCR | XTABS);
3813# else
3814# ifdef TAB3
3815 tnew.c_oflag &= ~(ONLCR | TAB3);
3816# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003818# endif
3819# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003821 tnew.c_cc[VMIN] = 1; // return after 1 char
3822 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
3824 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003825 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003826 // Also reset ICANON here, otherwise on Solaris select() won't see
3827 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003828 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003829 tnew.c_cc[VMIN] = 1; // return after 1 char
3830 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
3833# if defined(HAVE_TERMIOS_H)
3834 {
3835 int n = 10;
3836
Bram Moolenaar0f873732019-12-05 20:28:46 +01003837 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3838 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3840 && errno == EINTR && n > 0)
3841 --n;
3842 }
3843# else
3844 ioctl(read_cmd_fd, TCSETA, &tnew);
3845# endif
3846
3847#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 /*
3849 * for "old" tty systems
3850 */
3851# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003852# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853# endif
3854 static struct sgttyb ttybold;
3855 struct sgttyb ttybnew;
3856
3857 if (first)
3858 {
3859 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003860 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 }
3862
3863 ttybnew = ttybold;
3864 if (tmode == TMODE_RAW)
3865 {
3866 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3867 ttybnew.sg_flags |= RAW;
3868 }
3869 else if (tmode == TMODE_SLEEP)
3870 ttybnew.sg_flags &= ~(ECHO);
3871 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3872#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003873 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874}
3875
3876/*
3877 * Try to get the code for "t_kb" from the stty setting
3878 *
3879 * Even if termcap claims a backspace key, the user's setting *should*
3880 * prevail. stty knows more about reality than termcap does, and if
3881 * somebody's usual erase key is DEL (which, for most BSD users, it will
3882 * be), they're going to get really annoyed if their erase key starts
3883 * doing forward deletes for no reason. (Eric Fischer)
3884 */
3885 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003886get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003888 ttyinfo_T info;
3889 char_u buf[2];
3890 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003892 if (get_tty_info(read_cmd_fd, &info) != OK)
3893 return;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003894
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003895 intr_char = info.interrupt;
3896 buf[0] = info.backspace;
3897 buf[1] = NUL;
3898 add_termcode((char_u *)"kb", buf, FALSE);
3899
3900 // If <BS> and <DEL> are now the same, redefine <DEL>.
3901 p = find_termcode((char_u *)"kD");
3902 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3903 do_fixdel(NULL);
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003904}
3905
3906/*
3907 * Obtain the characters that Backspace and Enter produce on "fd".
3908 * Returns OK or FAIL.
3909 */
3910 int
3911get_tty_info(int fd, ttyinfo_T *info)
3912{
3913#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914# ifdef HAVE_TERMIOS_H
3915 struct termios keys;
3916# else
3917 struct termio keys;
3918# endif
3919
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003920 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003922 info->backspace = keys.c_cc[VERASE];
3923 info->interrupt = keys.c_cc[VINTR];
3924 if (keys.c_iflag & ICRNL)
3925 info->enter = NL;
3926 else
3927 info->enter = CAR;
3928 if (keys.c_oflag & ONLCR)
3929 info->nl_does_cr = TRUE;
3930 else
3931 info->nl_does_cr = FALSE;
3932 return OK;
3933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003935 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 struct sgttyb keys;
3937
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003938 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003940 info->backspace = keys.sg_erase;
3941 info->interrupt = keys.sg_kill;
3942 info->enter = CAR;
3943 info->nl_does_cr = TRUE;
3944 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003947 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948}
3949
Bram Moolenaar0f873732019-12-05 20:28:46 +01003950#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003952static int mouse_ison = FALSE;
3953
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954/*
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003955 * Set mouse clicks on or off and possible enable mouse movement events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 */
3957 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003958mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003960#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003961 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 int xterm_mouse_vers;
3964
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003965#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003966 if (!on)
3967 // Make sure not tracing mouse movements. Important when a button-down
3968 // was received but no release yet.
3969 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003970#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003971
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003972 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003973#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003974 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003975#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003976 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003977 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 return;
3979
3980 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003981
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003982#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003983 if (ttym_flags == TTYM_URXVT)
3984 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003985 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003986 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003987 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003988#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003989
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00003990 if (T_CXM != NULL && *T_CXM != NUL)
3991 {
3992 term_enable_mouse(on);
3993 }
3994 else if (ttym_flags == TTYM_SGR)
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003995 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003996 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003997 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003998 mouse_ison = on;
3999 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004000
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004001#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004002 if (bevalterm_ison != (p_bevalterm && on))
4003 {
4004 bevalterm_ison = (p_bevalterm && on);
4005 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004006 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004007 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004008 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004009#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004010
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 if (xterm_mouse_vers > 0)
4012 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004013 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 out_str_nf((char_u *)
4015 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004016 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004017#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004018 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004019#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004020 "\033[?1002h")
4021 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01004022 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004024 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004025 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
4027
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004028#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 else if (ttym_flags == TTYM_DEC)
4030 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004031 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01004033 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004035 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004039#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 else
4041 {
4042 if (on)
4043 {
4044 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004045 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
4047 else
4048 {
4049 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004050 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 }
4052 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004055#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004056 else
4057 {
4058 if (on)
4059 {
4060 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004061 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004062 }
4063 else
4064 {
4065 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004066 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004067 }
4068 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004069#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004070
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004071#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 else
4073 {
4074 if (on)
4075 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004076 // D - Enable Mouse up/down messages
4077 // L - Enable Left Button Reporting
4078 // M - Enable Middle Button Reporting
4079 // R - Enable Right Button Reporting
4080 // K - Enable SHIFT and CTRL key Reporting
4081 // + - Enable Advanced messaging of mouse moves and up/down messages
4082 // Q - Quiet No Ack
4083 // # - Numeric value of mouse pointer required
4084 // 0 = Multiview 2000 cursor, used as standard
4085 // 1 = Windows Arrow
4086 // 2 = Windows I Beam
4087 // 3 = Windows Hour Glass
4088 // 4 = Windows Cross Hair
4089 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004090# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01004091 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004092 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004093# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004094 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004095# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004096 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
4098 else
4099 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004100 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004101 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004104#endif
4105#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 else
4107 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004108 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 if (on)
4110 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
4111 else
4112 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004113 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116}
4117
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004118#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004119/*
4120 * Called when 'balloonevalterm' changed.
4121 */
4122 void
4123mch_bevalterm_changed(void)
4124{
4125 mch_setmouse(mouse_ison);
4126}
4127#endif
4128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129/*
4130 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
4131 */
4132 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004133check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
4135# ifdef FEAT_MOUSE_XTERM
4136 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02004137# ifdef FEAT_MOUSE_URXVT
4138 && use_xterm_mouse() != 3
4139# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140# ifdef FEAT_GUI
4141 && !gui.in_use
4142# endif
4143 )
4144 {
4145 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004146 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 if (*p_mouse != NUL)
4148 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004149 // force mouse off and maybe on to send possibly new mouse
4150 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 mch_setmouse(FALSE);
4152 setmouse();
4153 }
4154 }
4155 else
4156 del_mouse_termcode(KS_MOUSE);
4157# endif
4158
4159# ifdef FEAT_MOUSE_GPM
4160 if (!use_xterm_mouse()
4161# ifdef FEAT_GUI
4162 && !gui.in_use
4163# endif
4164 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004165 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02004166 else
4167 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168# endif
4169
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004170# ifdef FEAT_SYSMOUSE
4171 if (!use_xterm_mouse()
4172# ifdef FEAT_GUI
4173 && !gui.in_use
4174# endif
4175 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004176 set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004177# endif
4178
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01004180 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 if (!use_xterm_mouse()
4182# ifdef FEAT_GUI
4183 && !gui.in_use
4184# endif
4185 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004186 set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 else
4188 del_mouse_termcode(KS_JSBTERM_MOUSE);
4189# endif
4190
4191# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01004192 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
4193 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 if (!use_xterm_mouse()
4195# ifdef FEAT_GUI
4196 && !gui.in_use
4197# endif
4198 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004199 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 else
4201 del_mouse_termcode(KS_NETTERM_MOUSE);
4202# endif
4203
4204# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01004205 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004206 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207# ifdef FEAT_GUI
4208 && !gui.in_use
4209# endif
4210 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004211 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004212 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 else
4214 del_mouse_termcode(KS_DEC_MOUSE);
4215# endif
4216# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004217 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004218 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219# ifdef FEAT_GUI
4220 && !gui.in_use
4221# endif
4222 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004223 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 else
4225 del_mouse_termcode(KS_PTERM_MOUSE);
4226# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004227# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004228 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004229# ifdef FEAT_GUI
4230 && !gui.in_use
4231# endif
4232 )
4233 {
4234 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004235 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004236
4237 if (*p_mouse != NUL)
4238 {
4239 mch_setmouse(FALSE);
4240 setmouse();
4241 }
4242 }
4243 else
4244 del_mouse_termcode(KS_URXVT_MOUSE);
4245# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004246 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004247# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004248 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004249# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004250 )
4251 {
4252 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004253 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004254
4255 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004256 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004257
4258 if (*p_mouse != NUL)
4259 {
4260 mch_setmouse(FALSE);
4261 setmouse();
4262 }
4263 }
4264 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004265 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004266 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004267 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271#ifndef VMS
4272
4273/*
4274 * Try to get the current window size:
4275 * 1. with an ioctl(), most accurate method
4276 * 2. from the environment variables LINES and COLUMNS
4277 * 3. from the termcap
4278 * 4. keep using the old values
4279 * Return OK when size could be determined, FAIL otherwise.
4280 */
4281 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004282mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283{
4284 long rows = 0;
4285 long columns = 0;
4286 char_u *p;
4287
4288 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 * 1. try using an ioctl. It is the most accurate method.
4290 *
4291 * Try using TIOCGWINSZ first, some systems that have it also define
4292 * TIOCGSIZE but don't have a struct ttysize.
4293 */
4294# ifdef TIOCGWINSZ
4295 {
4296 struct winsize ws;
4297 int fd = 1;
4298
Bram Moolenaar0f873732019-12-05 20:28:46 +01004299 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 if (!isatty(fd) && isatty(read_cmd_fd))
4301 fd = read_cmd_fd;
4302 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4303 {
4304 columns = ws.ws_col;
4305 rows = ws.ws_row;
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004306# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004307 ch_log(NULL, "Got size with TIOCGWINSZ: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004308# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
4310 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004311# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312# ifdef TIOCGSIZE
4313 {
4314 struct ttysize ts;
4315 int fd = 1;
4316
Bram Moolenaar0f873732019-12-05 20:28:46 +01004317 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 if (!isatty(fd) && isatty(read_cmd_fd))
4319 fd = read_cmd_fd;
4320 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4321 {
4322 columns = ts.ts_cols;
4323 rows = ts.ts_lines;
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004324# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004325 ch_log(NULL, "Got size with TIOCGSIZE: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004326# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004329# endif // TIOCGSIZE
4330# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
4332 /*
4333 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004334 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4335 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004337 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 {
4339 if ((p = (char_u *)getenv("LINES")))
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004340 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 rows = atoi((char *)p);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004342# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004343 ch_log(NULL, "Got 'lines' from $LINES: %ld", rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004344# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 if ((p = (char_u *)getenv("COLUMNS")))
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 columns = atoi((char *)p);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004349# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004350 ch_log(NULL, "Got 'columns' from $COLUMNS: %ld", columns);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004351# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354
4355#ifdef HAVE_TGETENT
4356 /*
4357 * 3. try reading "co" and "li" entries from termcap
4358 */
4359 if (columns == 0 || rows == 0)
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 getlinecol(&columns, &rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004362# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004363 ch_log(NULL, "Got size from termcap: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004364# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366#endif
4367
4368 /*
4369 * 4. If everything fails, use the old values
4370 */
4371 if (columns <= 0 || rows <= 0)
4372 return FAIL;
4373
4374 Rows = rows;
4375 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004376 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 return OK;
4378}
4379
mikoto20001083cae2024-11-11 21:24:14 +01004380/*
4381 * Try to get the current terminal cell size.
mikoto2000de094dc2024-11-14 22:13:48 +01004382 * On failure, returns -1x-1
mikoto20001083cae2024-11-11 21:24:14 +01004383 */
4384 void
4385mch_calc_cell_size(struct cellsize *cs_out)
4386{
mikoto2000de094dc2024-11-14 22:13:48 +01004387 // get current tty size.
4388 struct winsize ws;
4389 int fd = 1;
4390 int retval = -1;
4391 retval = ioctl(fd, TIOCGWINSZ, &ws);
4392
4393#ifdef FEAT_EVAL
4394 ch_log(NULL, "ioctl(TIOCGWINSZ) %s", retval == 0 ? "success" : "failed");
mikoto20001083cae2024-11-11 21:24:14 +01004395#endif
mikoto20001083cae2024-11-11 21:24:14 +01004396
mikoto2000a73dfc22024-11-18 21:12:21 +01004397 if (retval == -1 || ws.ws_col == 0 || ws.ws_row == 0)
mikoto2000de094dc2024-11-14 22:13:48 +01004398 {
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02004399 cs_out->cs_xpixel = -1;
4400 cs_out->cs_ypixel = -1;
4401 return;
mikoto2000de094dc2024-11-14 22:13:48 +01004402 }
mikoto20001083cae2024-11-11 21:24:14 +01004403
mikoto2000de094dc2024-11-14 22:13:48 +01004404 // calculate parent tty's pixel per cell.
4405 int x_cell_size = ws.ws_xpixel / ws.ws_col;
4406 int y_cell_size = ws.ws_ypixel / ws.ws_row;
mikoto20001083cae2024-11-11 21:24:14 +01004407
mikoto2000de094dc2024-11-14 22:13:48 +01004408 // calculate current tty's pixel
4409 cs_out->cs_xpixel = x_cell_size;
4410 cs_out->cs_ypixel = y_cell_size;
4411
4412#ifdef FEAT_EVAL
4413 ch_log(NULL, "Got cell pixel size with TIOCGWINSZ: %d x %d", x_cell_size, y_cell_size);
mikoto20001083cae2024-11-11 21:24:14 +01004414#endif
4415}
4416
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004417#if defined(FEAT_TERMINAL) || defined(PROTO)
4418/*
4419 * Report the windows size "rows" and "cols" to tty "fd".
4420 */
4421 int
4422mch_report_winsize(int fd, int rows, int cols)
4423{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004424 int tty_fd;
4425 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004426
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004427 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004428 if (tty_fd < 0)
4429 return FAIL;
4430
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004431# if defined(TIOCSWINSZ)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004432 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004433
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004434 ws.ws_col = cols;
4435 ws.ws_row = rows;
mikoto20001083cae2024-11-11 21:24:14 +01004436
zeertzjq98800972025-04-18 10:57:33 +02004437 // calculate and set tty pixel size
mikoto20001083cae2024-11-11 21:24:14 +01004438 struct cellsize cs;
4439 mch_calc_cell_size(&cs);
mikoto2000de094dc2024-11-14 22:13:48 +01004440
4441 if (cs.cs_xpixel == -1)
4442 {
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +02004443 // failed get pixel size.
4444 ws.ws_xpixel = 0;
4445 ws.ws_ypixel = 0;
mikoto2000de094dc2024-11-14 22:13:48 +01004446 }
4447 else
4448 {
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +02004449 ws.ws_xpixel = cols * cs.cs_xpixel;
4450 ws.ws_ypixel = rows * cs.cs_ypixel;
mikoto2000de094dc2024-11-14 22:13:48 +01004451 }
mikoto20001083cae2024-11-11 21:24:14 +01004452
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004453 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004454 ch_log(NULL, "ioctl(TIOCSWINSZ) %s", retval == 0 ? "success" : "failed");
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004455# elif defined(TIOCSSIZE)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004456 struct ttysize ts;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004457
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004458 ts.ts_cols = cols;
4459 ts.ts_lines = rows;
4460 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004461 ch_log(NULL, "ioctl(TIOCSSIZE) %s", retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004462# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004463 if (tty_fd != fd)
4464 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004465 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004466}
4467#endif
4468
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469/*
4470 * Try to set the window size to Rows and Columns.
4471 */
4472 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004473mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474{
4475 if (*T_CWS)
4476 {
4477 /*
4478 * NOTE: if you get an error here that term_set_winsize() is
4479 * undefined, check the output of configure. It could probably not
4480 * find a ncurses, termcap or termlib library.
4481 */
4482 term_set_winsize((int)Rows, (int)Columns);
4483 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004484 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486}
4487
Bram Moolenaar0f873732019-12-05 20:28:46 +01004488#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490/*
4491 * Rows and/or Columns has changed.
4492 */
4493 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004494mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004496 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497}
4498
Bram Moolenaar205b8862011-09-07 15:04:31 +02004499/*
4500 * Wait for process "child" to end.
4501 * Return "child" if it exited properly, <= 0 on error.
4502 */
4503 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004504wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004505{
4506 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004507 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004508
4509 while (wait_pid != child)
4510 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004511 // When compiled with Python threads are probably used, in which case
4512 // wait() sometimes hangs for no obvious reason. Use waitpid()
4513 // instead and loop (like the GUI). Also needed for other interfaces,
4514 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004515# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004516 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004517# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004518 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004519# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004520 if (wait_pid == 0)
4521 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004522 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004523 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004524 if (++delay_msec > 10)
4525 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004526 continue;
4527 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004528 if (wait_pid <= 0
4529# ifdef ECHILD
4530 && errno == ECHILD
4531# endif
4532 )
4533 break;
4534 }
4535 return wait_pid;
4536}
4537
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004538#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004539/*
4540 * Set the environment for a child process.
4541 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004542 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004543set_child_environment(
4544 long rows,
4545 long columns,
4546 char *term,
4547 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004548{
4549# ifdef HAVE_SETENV
4550 char envbuf[50];
4551# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004552 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004553 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004554 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004555 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004556 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004557# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004558 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004559# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004560# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004561 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004562# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004563# endif
4564
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004565# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004566 setenv("TERM", term, 1);
4567 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004568 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004569 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004570 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004571 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004572 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004573 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004574 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004575# ifdef FEAT_TERMINAL
4576 if (is_terminal)
4577 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004578 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004579 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4580 }
4581# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004582# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004583 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004584# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004585# else
4586 /*
4587 * Putenv does not copy the string, it has to remain valid.
4588 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004589 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004590 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004591 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4592 putenv(envbuf_Term);
4593 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004594 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004595 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4596 putenv(envbuf_Lines);
4597 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4598 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004599 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004600 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004601 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004602# ifdef FEAT_TERMINAL
4603 if (is_terminal)
4604 {
4605 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004606 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004607 putenv(envbuf_Version);
4608 }
4609# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004610# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004611 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4612 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4613 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004614# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004615# endif
4616}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004617
4618 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004619set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004620{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004621 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004622}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004623#endif
4624
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004625#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004626/*
4627 * Open a PTY, with FD for the master and slave side.
4628 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004629 * When successful both file descriptors are stored and the allocated pty name
4630 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004631 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004632 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004633open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004634{
4635 char *tty_name;
4636
Bram Moolenaar59386482019-02-10 22:43:46 +01004637 if (name1 != NULL)
4638 *name1 = NULL;
4639 if (name2 != NULL)
4640 *name2 = NULL;
4641
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004642 *pty_master_fd = mch_openpty(&tty_name); // open pty
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004643 if (*pty_master_fd < 0)
4644 return;
4645
4646 // Leaving out O_NOCTTY may lead to waitpid() always returning
4647 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4648 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004649#ifdef O_NOCTTY
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004650 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004651#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004652 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004653#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004654 if (*pty_slave_fd < 0)
4655 {
4656 close(*pty_master_fd);
4657 *pty_master_fd = -1;
4658 }
4659 else
4660 {
4661 if (name1 != NULL)
4662 *name1 = vim_strsave((char_u *)tty_name);
4663 if (name2 != NULL)
4664 *name2 = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004665 }
4666}
4667#endif
4668
Bram Moolenaarfae42832017-08-01 22:24:26 +02004669/*
4670 * Send SIGINT to a child process if "c" is an interrupt character.
4671 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004672 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004673may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4674{
4675# ifdef SIGINT
4676 if (c == Ctrl_C || c == intr_char)
4677 {
4678# ifdef HAVE_SETSID
4679 kill(-pid, SIGINT);
4680# else
4681 kill(0, SIGINT);
4682# endif
4683 if (wpid > 0)
4684 kill(wpid, SIGINT);
4685 }
4686# endif
4687}
4688
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004689#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004690
Bram Moolenaar0f873732019-12-05 20:28:46 +01004691/*
4692 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4693 * pointers, the last one is NULL.
4694 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4695 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004696 int
4697unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004698 char_u *cmd,
4699 char ***argvp,
4700 char_u **sh_tofree,
4701 char_u **shcf_tofree)
4702{
4703 char **argv = NULL;
4704 int argc;
4705
4706 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004707 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004708 return FAIL;
4709
4710 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4711 return FAIL;
4712 *argvp = argv;
4713
4714 if (cmd != NULL)
4715 {
4716 char_u *s;
4717 char_u *p;
4718
4719 if (extra_shell_arg != NULL)
4720 argv[argc++] = (char *)extra_shell_arg;
4721
Bram Moolenaar0f873732019-12-05 20:28:46 +01004722 // Break 'shellcmdflag' into white separated parts. This doesn't
4723 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004724 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004725 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004726 return FAIL;
4727 s = *shcf_tofree;
4728 p = p_shcf;
4729 while (*p != NUL)
4730 {
4731 argv[argc++] = (char *)s;
4732 while (*p && *p != ' ' && *p != TAB)
4733 *s++ = *p++;
4734 *s++ = NUL;
4735 p = skipwhite(p);
4736 }
4737
4738 argv[argc++] = (char *)cmd;
4739 }
4740 argv[argc] = NULL;
4741 return OK;
4742}
4743#endif
4744
4745#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4746/*
4747 * Use a terminal window to run a shell command in.
4748 */
4749 static int
4750mch_call_shell_terminal(
4751 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004752 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004753{
4754 jobopt_T opt;
4755 char **argv = NULL;
4756 char_u *tofree1 = NULL;
4757 char_u *tofree2 = NULL;
4758 int retval = -1;
4759 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004760 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004761 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004762 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004763
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004764 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004765 goto theend;
4766
4767 init_job_options(&opt);
4768 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4769 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004770 if (buf == NULL)
4771 goto theend;
4772
4773 job = term_getjob(buf->b_term);
4774 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004775
Bram Moolenaar0f873732019-12-05 20:28:46 +01004776 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004777 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004778 if (curbuf == buf)
Bram Moolenaar13568252018-03-16 20:46:58 +01004779 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004780 // Only when managed to find a window for "buf",
4781 clear_oparg(&oa);
4782 while (term_use_loop())
Bram Moolenaar13568252018-03-16 20:46:58 +01004783 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004784 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4785 {
4786 // If terminal_loop() returns OK we got a key that is handled
4787 // in Normal model. We don't do redrawing anyway.
4788 if (terminal_loop(TRUE) == OK)
4789 normal_cmd(&oa, TRUE);
4790 }
4791 else
Bram Moolenaar13568252018-03-16 20:46:58 +01004792 normal_cmd(&oa, TRUE);
4793 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00004794 retval = job->jv_exitval;
4795 ch_log(NULL, "system command finished");
4796
4797 job_unref(job);
4798
4799 // restore curwin/curbuf and a few other things
4800 aucmd_restbuf(&aco);
Bram Moolenaar13568252018-03-16 20:46:58 +01004801 }
Bram Moolenaar13568252018-03-16 20:46:58 +01004802
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01004803 // Only require pressing Enter when redrawing, to avoid that system() gets
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004804 // the hit-enter prompt even though it didn't output anything.
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004805 if (RedrawingDisabled == 0)
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004806 wait_return(TRUE);
Bram Moolenaar13568252018-03-16 20:46:58 +01004807 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4808
4809theend:
4810 vim_free(argv);
4811 vim_free(tofree1);
4812 vim_free(tofree2);
4813 return retval;
4814}
4815#endif
4816
4817#ifdef USE_SYSTEM
4818/*
4819 * Use system() to start the shell: simple but slow.
4820 */
4821 static int
4822mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004823 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004824 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825{
4826#ifdef VMS
4827 char *ifn = NULL;
4828 char *ofn = NULL;
4829#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004830 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004831 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004832 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833
4834 out_flush();
4835
4836 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004837 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838
Bram Moolenaar62b42182010-09-21 22:09:37 +02004839# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004840 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004841 loose_clipboard();
4842# endif
4843
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (cmd == NULL)
4845 x = system((char *)p_sh);
4846 else
4847 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004848# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 if (ofn = strchr((char *)cmd, '>'))
4850 *ofn++ = '\0';
4851 if (ifn = strchr((char *)cmd, '<'))
4852 {
4853 char *p;
4854
4855 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004856 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 if (p)
4858 *p = '\0';
4859 }
4860 if (ofn)
4861 x = vms_sys((char *)cmd, ofn, ifn);
4862 else
4863 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004864# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004865 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004867 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 if (newcmd == NULL)
4869 x = 0;
4870 else
4871 {
4872 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4873 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4874 (char *)p_shcf,
4875 (char *)cmd);
4876 x = system((char *)newcmd);
4877 vim_free(newcmd);
4878 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881# ifdef VMS
4882 x = vms_sys_status(x);
4883# endif
4884 if (emsg_silent)
4885 ;
4886 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004887 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 else if (x && !(options & SHELL_SILENT))
4889 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004890 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 msg_outnum((long)x);
4892 msg_putchar('\n');
4893 }
4894
4895 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004896 {
4897 // The shell may have messed with the mode, always set it.
4898 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004899 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004902# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4903 restore_clipboard();
4904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004906}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907
Bram Moolenaar0f873732019-12-05 20:28:46 +01004908#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
Bram Moolenaar0f873732019-12-05 20:28:46 +01004910# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4911 // 127, some shells use that already
4912# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
Bram Moolenaar13568252018-03-16 20:46:58 +01004914/*
4915 * Don't use system(), use fork()/exec().
4916 */
4917 static int
4918mch_call_shell_fork(
4919 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004920 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004921{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004922 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004924 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 pid_t wait_pid = 0;
4926# ifdef HAVE_UNION_WAIT
4927 union wait status;
4928# else
4929 int status = -1;
4930# endif
4931 int retval = -1;
4932 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004933 char_u *tofree1 = NULL;
4934 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004936 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004937# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004939# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004940 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 int fd_fromshell[2];
4942 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004943 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
4945 out_flush();
4946 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004947 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004948 if (tmode == TMODE_RAW)
4949 // The shell may have messed with the mode, always set it later.
4950 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004952 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004953 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004954
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004956 * For the GUI, when writing the output into the buffer and when reading
4957 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4958 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004960 if ((options & (SHELL_READ|SHELL_WRITE))
4961# ifdef FEAT_GUI
4962 || (gui.in_use && show_shell_mess)
4963# endif
4964 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004966# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 /*
4968 * Try to open a master pty.
4969 * If this works, open the slave pty.
4970 * If the slave can't be opened, close the master pty.
4971 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004972 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004973 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 /*
4975 * If not opening a pty or it didn't work, try using pipes.
4976 */
4977 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004978# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
4980 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004981 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
4983 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004984 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 close(fd_toshell[0]);
4987 close(fd_toshell[1]);
4988 }
4989 }
4990 if (pipe_error)
4991 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004992 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 out_flush();
4994 }
4995 }
4996 }
4997
Bram Moolenaar0f873732019-12-05 20:28:46 +01004998 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005000 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005001 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005002 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005003 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005005 UNBLOCK_SIGNALS(&curset);
5006
Bram Moolenaar32526b32019-01-19 17:43:09 +01005007 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005008 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00005010 || (gui.in_use && show_shell_mess)
5011# endif
5012 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005014# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005015 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 close(pty_master_fd);
5018 close(pty_slave_fd);
5019 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005020 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00005021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 {
5023 close(fd_toshell[0]);
5024 close(fd_toshell[1]);
5025 close(fd_fromshell[0]);
5026 close(fd_fromshell[1]);
5027 }
5028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005030 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005032 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005033 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005035# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01005036 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02005037 {
5038 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01005039 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02005040 }
Bram Moolenaar819524702018-02-27 19:10:00 +01005041# endif
5042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 if (!show_shell_mess || (options & SHELL_EXPAND))
5044 {
5045 int fd;
5046
5047 /*
5048 * Don't want to show any message from the shell. Can't just
5049 * close stdout and stderr though, because some systems will
5050 * break if you try to write to them after that, so we must
5051 * use dup() to replace them with something else -- webb
5052 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
5053 * waiting for input.
5054 */
5055 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
5056 fclose(stdin);
5057 fclose(stdout);
5058 fclose(stderr);
5059
5060 /*
5061 * If any of these open()'s and dup()'s fail, we just continue
5062 * anyway. It's not fatal, and on most systems it will make
5063 * no difference at all. On a few it will cause the execvp()
5064 * to exit with a non-zero status even when the completion
5065 * could be done, which is nothing too serious. If the open()
5066 * or dup() failed we'd just do the same thing ourselves
5067 * anyway -- webb
5068 */
5069 if (fd >= 0)
5070 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005071 vim_ignored = dup(fd); // To replace stdin (fd 0)
5072 vim_ignored = dup(fd); // To replace stdout (fd 1)
5073 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
Bram Moolenaar0f873732019-12-05 20:28:46 +01005075 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 close(fd);
5077 }
5078 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005079 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00005081 || gui.in_use
5082# endif
5083 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
5085
Bram Moolenaardf177f62005-02-22 08:39:57 +00005086# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005087 // Create our own process group, so that the child and all its
5088 // children can be kill()ed. Don't do this when using pipes,
5089 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005090 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00005091 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005092 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00005093# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005094 // When doing "!xterm&" and 'shell' is bash: the shell
5095 // will exit and send SIGHUP to all processes in its
5096 // group, killing the just started process. Ignore SIGHUP
5097 // to avoid that. (suggested by Simon Schubert)
ichizok378447f2023-05-11 22:25:42 +01005098 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar07256082009-02-04 13:19:42 +00005099# endif
5100 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005101# endif
5102# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005103 if (pty_slave_fd >= 0)
5104 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005105 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005106 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005107 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01005108# ifdef TIOCSCTTY
5109 // Try to become controlling tty (probably doesn't work,
5110 // unless run by root)
5111 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5112# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005113 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005114# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005115 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116
Bram Moolenaara5792f52005-11-23 21:25:05 +00005117 /*
5118 * stderr is only redirected when using the GUI, so that a
5119 * program like gpg can still access the terminal to get a
5120 * passphrase using stderr.
5121 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005122# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 if (pty_master_fd >= 0)
5124 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005125 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126
Bram Moolenaar0f873732019-12-05 20:28:46 +01005127 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005129 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005131 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00005132 if (gui.in_use)
5133 {
5134 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005135 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00005136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137
Bram Moolenaar0f873732019-12-05 20:28:46 +01005138 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00005141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005143 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 close(fd_toshell[1]);
5145 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005146 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 close(fd_toshell[0]);
5148
Bram Moolenaar0f873732019-12-05 20:28:46 +01005149 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 close(fd_fromshell[0]);
5151 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005152 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 close(fd_fromshell[1]);
5154
Bram Moolenaara5792f52005-11-23 21:25:05 +00005155# ifdef FEAT_GUI
5156 if (gui.in_use)
5157 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005158 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00005159 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005160 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00005161 }
5162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
5164 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005165
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 /*
5167 * There is no type cast for the argv, because the type may be
5168 * different on different machines. This may cause a warning
5169 * message with strict compilers, don't worry about it.
5170 * Call _exit() instead of exit() to avoid closing the connection
5171 * to the X server (esp. with GTK, which uses atexit()).
5172 */
5173 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005174 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005176 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
5178 /*
5179 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005180 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 */
5182 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005183 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005184 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005185# ifdef FEAT_JOB_CHANNEL
5186 ++dont_check_job_ended;
5187# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 /*
5189 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005190 * This is also used to pipe stdin/stdout to/from the external
5191 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005193 if ((options & (SHELL_READ|SHELL_WRITE))
5194# ifdef FEAT_GUI
5195 || (gui.in_use && show_shell_mess)
5196# endif
5197 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005199# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01005201 int buffer_off = 0; // valid bytes in buffer[]
5202 char_u ta_buf[BUFLEN + 1]; // TypeAHead
5203 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 int len;
5205 int p_more_save;
5206 int old_State;
5207 int c;
5208 int toshell_fd;
5209 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005210 garray_T ga;
5211 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01005212# ifdef ELAPSED_FUNC
5213 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215
Bram Moolenaardf177f62005-02-22 08:39:57 +00005216# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (pty_master_fd >= 0)
5218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 fromshell_fd = pty_master_fd;
5220 toshell_fd = dup(pty_master_fd);
5221 }
5222 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00005223# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 {
5225 close(fd_toshell[0]);
5226 close(fd_fromshell[1]);
5227 toshell_fd = fd_toshell[1];
5228 fromshell_fd = fd_fromshell[0];
5229 }
5230
5231 /*
5232 * Write to the child if there are typed characters.
5233 * Read from the child if there are characters available.
5234 * Repeat the reading a few times if more characters are
5235 * available. Need to check for typed keys now and then, but
5236 * not too often (delays when no chars are available).
5237 * This loop is quit if no characters can be read from the pty
5238 * (WaitForChar detected special condition), or there are no
5239 * characters available and the child has exited.
5240 * Only check if the child has exited when there is no more
5241 * output. The child may exit before all the output has
5242 * been printed.
5243 *
5244 * Currently this busy loops!
5245 * This can probably dead-lock when the write blocks!
5246 */
5247 p_more_save = p_more;
5248 p_more = FALSE;
5249 old_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01005250 State = MODE_EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005252 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005253 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005254 // Fork a process that will write the lines to the
5255 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005256 if ((wpid = fork()) == -1)
5257 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005258 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005259 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005260 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00005261 {
5262 linenr_T lnum = curbuf->b_op_start.lnum;
zeertzjq53fed232025-03-30 15:01:56 +02005263 size_t written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005264 char_u *lp = ml_get(lnum);
John Marriottefc41a52025-01-23 20:05:29 +01005265 size_t lplen = (size_t)ml_get_len(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005266
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005267 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005268 for (;;)
5269 {
John Marriottefc41a52025-01-23 20:05:29 +01005270 if (lplen == 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005271 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005272 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005273 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00005274 len = write(toshell_fd, "", (size_t)1);
5275 else
5276 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005277 char_u *s = vim_strchr(lp + written, NL);
5278
Bram Moolenaar89d40322006-08-29 15:30:07 +00005279 len = write(toshell_fd, (char *)lp + written,
zeertzjq53fed232025-03-30 15:01:56 +02005280 s == NULL ? lplen - written
Bram Moolenaar78a15312009-05-15 19:33:18 +00005281 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005282 }
zeertzjq53fed232025-03-30 15:01:56 +02005283 if (len == (int)(lplen - written))
Bram Moolenaardf177f62005-02-22 08:39:57 +00005284 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005285 // Finished a line, add a NL, unless this line
5286 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005287 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005288 || (!curbuf->b_p_bin
5289 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005290 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaar88456cd2022-11-18 22:14:09 +00005291 && (lnum != curbuf->b_ml.ml_line_count
Bram Moolenaardf177f62005-02-22 08:39:57 +00005292 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005293 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005294 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005295 ++lnum;
5296 if (lnum > curbuf->b_op_end.lnum)
5297 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005298 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005299 close(toshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005300 break;
5301 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005302 lp = ml_get(lnum);
John Marriottefc41a52025-01-23 20:05:29 +01005303 lplen = ml_get_len(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005304 written = 0;
5305 }
5306 else if (len > 0)
zeertzjq53fed232025-03-30 15:01:56 +02005307 written += (size_t)len;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005308 }
5309 _exit(0);
5310 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005311 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005312 {
5313 close(toshell_fd);
5314 toshell_fd = -1;
5315 }
5316 }
5317
5318 if (options & SHELL_READ)
5319 ga_init2(&ga, 1, BUFLEN);
5320
5321 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005322# ifdef ELAPSED_FUNC
5323 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 for (;;)
5326 {
5327 /*
5328 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005329 * if there are any.
5330 * Don't do this if we are expanding wild cards (would eat
5331 * typeahead).
5332 * Don't do this when filtering and terminal is in cooked
5333 * mode, the shell command will handle the I/O. Avoids
5334 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005335 * Don't get characters when the child has already
5336 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005337 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005338 * while (noread_cnt > 4), avoids that ":r !ls" eats
5339 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 */
5341 len = 0;
5342 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005343 && ((options &
5344 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5345 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005346# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005347 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005348# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005349 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005350 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005351 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005353 if (ta_len == 0)
5354 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005355 // Get extra characters when we don't have any.
5356 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005357 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005358# ifdef ELAPSED_FUNC
5359 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005360# endif
5361 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5362 }
5363 if (ta_len > 0 || len > 0)
5364 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 /*
5366 * For pipes:
5367 * Check for CTRL-C: send interrupt signal to child.
5368 * Check for CTRL-D: EOF, close pipe to child.
5369 */
5370 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5371 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 /*
5373 * Send SIGINT to the child's group or all
5374 * processes in our group.
5375 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005376 may_send_sigint(ta_buf[ta_len], pid, wpid);
5377
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 if (pty_master_fd < 0 && toshell_fd >= 0
5379 && ta_buf[ta_len] == Ctrl_D)
5380 {
5381 close(toshell_fd);
5382 toshell_fd = -1;
5383 }
5384 }
5385
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01005386 // Remove Vim-specific codes from the input.
5387 len = term_replace_keycodes(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
5389 /*
5390 * For pipes: echo the typed characters.
5391 * For a pty this does not seem to work.
5392 */
5393 if (pty_master_fd < 0)
5394 {
5395 for (i = ta_len; i < ta_len + len; ++i)
5396 {
5397 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5398 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 else if (has_mbyte)
5400 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005401 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
5403 msg_outtrans_len(ta_buf + i, l);
5404 i += l - 1;
5405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 else
5407 msg_outtrans_len(ta_buf + i, 1);
5408 }
5409 windgoto(msg_row, msg_col);
5410 out_flush();
5411 }
5412
5413 ta_len += len;
5414
5415 /*
5416 * Write the characters to the child, unless EOF has
5417 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005418 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005419 * When writing buffer lines, drop the typed
5420 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005422 if (options & SHELL_WRITE)
5423 ta_len = 0;
5424 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {
5426 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5427 if (len > 0)
5428 {
5429 ta_len -= len;
5430 mch_memmove(ta_buf, ta_buf + len, ta_len);
5431 }
5432 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
5435
Bram Moolenaardf177f62005-02-22 08:39:57 +00005436 if (got_int)
5437 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005438 // CTRL-C sends a signal to the child, we ignore it
5439 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005440# ifdef HAVE_SETSID
5441 kill(-pid, SIGINT);
5442# else
5443 kill(0, SIGINT);
5444# endif
5445 if (wpid > 0)
5446 kill(wpid, SIGINT);
5447 got_int = FALSE;
5448 }
5449
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 /*
5451 * Check if the child has any characters to be printed.
5452 * Read them and write them to our window. Repeat this as
5453 * long as there is something to do, avoid the 10ms wait
5454 * for mch_inchar(), or sending typeahead characters to
5455 * the external process.
5456 * TODO: This should handle escape sequences, compatible
5457 * to some terminal (vt52?).
5458 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005459 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005460 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005462 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005465 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005467
5468 noread_cnt = 0;
5469 if (options & SHELL_READ)
5470 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005471 // Do NUL -> NL translation, append NL separated
5472 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005473 for (i = 0; i < len; ++i)
5474 {
5475 if (buffer[i] == NL)
5476 append_ga_line(&ga);
5477 else if (buffer[i] == NUL)
5478 ga_append(&ga, NL);
5479 else
5480 ga_append(&ga, buffer[i]);
5481 }
5482 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005483 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 {
5485 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005486 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
Bram Moolenaardf177f62005-02-22 08:39:57 +00005488 len += buffer_off;
5489 buffer[len] = NUL;
5490
Bram Moolenaar0f873732019-12-05 20:28:46 +01005491 // Check if the last character in buffer[] is
5492 // incomplete, keep these bytes for the next
5493 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 for (p = buffer; p < buffer + len; p += l)
5495 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005496 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005498 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 else if (MB_BYTE2LEN(*p) != l)
5500 break;
5501 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005502 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005504 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 if (len >= 12)
5506 ++p;
5507 else
5508 {
5509 buffer_off = len;
5510 continue;
5511 }
5512 }
5513 c = *p;
5514 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005515 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 if (p < buffer + len)
5517 {
5518 *p = c;
5519 buffer_off = (buffer + len) - p;
5520 mch_memmove(buffer, p, buffer_off);
5521 continue;
5522 }
5523 buffer_off = 0;
5524 }
5525 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 {
5527 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005528 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 }
5530
5531 windgoto(msg_row, msg_col);
5532 cursor_on();
5533 out_flush();
5534 if (got_int)
5535 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005536
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005537# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005538 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005539 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005540 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005541
Bram Moolenaar0f873732019-12-05 20:28:46 +01005542 // Avoid that we keep looping here without
5543 // checking for a CTRL-C for a long time. Don't
5544 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005545 if (msec > 2000)
5546 {
5547 noread_cnt = 5;
5548 break;
5549 }
5550 }
5551# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 }
5553
Bram Moolenaar0f873732019-12-05 20:28:46 +01005554 // If we already detected the child has finished, continue
5555 // reading output for a short while. Some text may be
5556 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005557 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005558 {
5559 if (noread_cnt < 5)
5560 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005561 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005562 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005563
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 /*
5565 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005566 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005568# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005569 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005570# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5574 || (wait_pid == pid && WIFEXITED(status)))
5575 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005576 // Don't break the loop yet, try reading more
5577 // characters from "fromshell_fd" first. When using
5578 // pipes there might still be something to read and
5579 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005582 else
5583 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005584
Bram Moolenaar95a51352013-03-21 22:53:50 +01005585# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005586 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005587 clip_update();
5588# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 }
5590finished:
5591 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005592 if (options & SHELL_READ)
5593 {
5594 if (ga.ga_len > 0)
5595 {
5596 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005597 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005598 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005599 }
5600 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005601 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005602 ga_clear(&ga);
5603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 /*
5606 * Give all typeahead that wasn't used back to ui_inchar().
5607 */
5608 if (ta_len)
5609 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 State = old_State;
5611 if (toshell_fd >= 0)
5612 close(toshell_fd);
5613 close(fromshell_fd);
5614 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005615# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005616 else
5617 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005618 long delay_msec = 1;
5619
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005620 if (tmode == TMODE_RAW)
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005621 // Possibly disables modifyOtherKeys, so that the system
5622 // can recognize CTRL-C.
5623 out_str_t_TE();
Bram Moolenaar0981c872020-08-23 14:28:37 +02005624
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005625 /*
5626 * Similar to the loop above, but only handle X events, no
5627 * I/O.
5628 */
5629 for (;;)
5630 {
5631 if (got_int)
5632 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005633 // CTRL-C sends a signal to the child, we ignore it
5634 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005635# ifdef HAVE_SETSID
5636 kill(-pid, SIGINT);
5637# else
5638 kill(0, SIGINT);
5639# endif
5640 got_int = FALSE;
5641 }
5642# ifdef __NeXT__
5643 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5644# else
5645 wait_pid = waitpid(pid, &status, WNOHANG);
5646# endif
5647 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5648 || (wait_pid == pid && WIFEXITED(status)))
5649 {
5650 wait_pid = pid;
5651 break;
5652 }
5653
Bram Moolenaar0f873732019-12-05 20:28:46 +01005654 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005655 clip_update();
5656
Bram Moolenaar0f873732019-12-05 20:28:46 +01005657 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005658 // less time, gradually wait longer.
5659 mch_delay(delay_msec,
5660 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005661 if (++delay_msec > 10)
5662 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005663 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005664
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005665 if (tmode == TMODE_RAW)
5666 // possibly enables modifyOtherKeys again
Bram Moolenaar733a69b2022-12-01 12:03:47 +00005667 out_str_t_TI();
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005668 }
5669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
5671 /*
5672 * Wait until our child has exited.
5673 * Ignore wait() returning pids of other children and returning
5674 * because of some signal like SIGWINCH.
5675 * Don't wait if wait_pid was already set above, indicating the
5676 * child already exited.
5677 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005678 if (wait_pid != pid)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01005679 (void)wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680
Bram Moolenaar624891f2010-10-13 16:22:09 +02005681# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005682 // Close slave side of pty. Only do this after the child has
5683 // exited, otherwise the child may hang when it tries to write on
5684 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005685 if (pty_master_fd >= 0)
5686 close(pty_slave_fd);
5687# endif
5688
Bram Moolenaar0f873732019-12-05 20:28:46 +01005689 // Make sure the child that writes to the external program is
5690 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005691 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005692 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005693 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005694 wait4pid(wpid, NULL);
5695 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005696
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005697# ifdef FEAT_JOB_CHANNEL
5698 --dont_check_job_ended;
5699# endif
5700
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 /*
5702 * Set to raw mode right now, otherwise a CTRL-C after
5703 * catch_signals() will kill Vim.
5704 */
5705 if (tmode == TMODE_RAW)
5706 settmode(TMODE_RAW);
5707 did_settmode = TRUE;
5708 set_signals();
5709
5710 if (WIFEXITED(status))
5711 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005712 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005714 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 {
5716 if (retval == EXEC_FAILED)
5717 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005718 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 msg_outtrans(p_sh);
5720 msg_putchar('\n');
5721 }
5722 else if (!(options & SHELL_SILENT))
5723 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005724 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 msg_outnum((long)retval);
5726 msg_putchar('\n');
5727 }
5728 }
5729 }
5730 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005731 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
5733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
5735error:
5736 if (!did_settmode)
5737 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005738 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005740 vim_free(argv);
5741 vim_free(tofree1);
5742 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
5744 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005745}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005746#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005747
5748 int
5749mch_call_shell(
5750 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005751 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005752{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005753#ifdef FEAT_EVAL
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01005754 ch_log(NULL, "executing shell command: %s", cmd);
5755#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005756#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar524c8532022-09-27 15:48:20 +01005757 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL
5758 && (options & SHELL_SILENT) == 0)
Bram Moolenaar13568252018-03-16 20:46:58 +01005759 return mch_call_shell_terminal(cmd, options);
5760#endif
5761#ifdef USE_SYSTEM
5762 return mch_call_shell_system(cmd, options);
5763#else
5764 return mch_call_shell_fork(cmd, options);
5765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766}
5767
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005768#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005769 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005770mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005771{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005772 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005773 int fd_in[2] = {-1, -1}; // for stdin
5774 int fd_out[2] = {-1, -1}; // for stdout
5775 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005776 int pty_master_fd = -1;
5777 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005778 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005779 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5780 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5781 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005782 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005783 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5784 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005785 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005786 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005787 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005788
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005789 if (use_out_for_err && use_null_for_out)
5790 use_null_for_err = TRUE;
5791
Bram Moolenaar0f873732019-12-05 20:28:46 +01005792 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005793 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005794
Bram Moolenaarb2412082017-08-20 18:09:14 +02005795 if (options->jo_pty
5796 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005797 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005798 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005799 open_pty(&pty_master_fd, &pty_slave_fd,
5800 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005801
Bram Moolenaar0f873732019-12-05 20:28:46 +01005802 // TODO: without the channel feature connect the child to /dev/null?
5803 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005804 if (use_file_for_in)
5805 {
5806 char_u *fname = options->jo_io_name[PART_IN];
5807
5808 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5809 if (fd_in[0] < 0)
5810 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005811 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005812 goto failed;
5813 }
5814 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005815 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005816 // When writing buffer lines to the input don't use the pty, so that
5817 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005818 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5819 && pipe(fd_in) < 0)
5820 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005821
5822 if (use_file_for_out)
5823 {
5824 char_u *fname = options->jo_io_name[PART_OUT];
5825
5826 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5827 if (fd_out[1] < 0)
5828 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005829 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005830 goto failed;
5831 }
5832 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005833 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005834 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005835
5836 if (use_file_for_err)
5837 {
5838 char_u *fname = options->jo_io_name[PART_ERR];
5839
5840 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5841 if (fd_err[1] < 0)
5842 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005843 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005844 goto failed;
5845 }
5846 }
Christian Brabandtb50bc9a2024-09-30 21:29:43 +02005847 // only create a pipe for the error fd, when either a callback has been setup
5848 // or pty is not used (e.g. terminal uses pty by default)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005849 else if (!use_out_for_err && !use_null_for_err
Christian Brabandtb50bc9a2024-09-30 21:29:43 +02005850 && (pty_master_fd < 0 || (options->jo_set & JO_ERR_CALLBACK))
5851 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005852 goto failed;
5853
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005854 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5855 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005856 if (options->jo_set & JO_CHANNEL)
5857 {
5858 channel = options->jo_channel;
5859 if (channel != NULL)
5860 ++channel->ch_refcount;
5861 }
5862 else
5863 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005864 if (channel == NULL)
5865 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005866 if (job->jv_tty_out != NULL)
5867 ch_log(channel, "using pty %s on fd %d",
5868 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005869 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005870
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005871 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005872 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005873 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005874 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005875 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005876 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005877 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005878 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005879 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005880 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005881 int null_fd = -1;
5882 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005883
Bram Moolenaar0f873732019-12-05 20:28:46 +01005884 // child
5885 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005886 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005887
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005888# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01005889 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005890 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005891 ch_logfile((char_u *)"", (char_u *)"");
5892# endif
5893
Bram Moolenaar835dc632016-02-07 14:27:38 +01005894# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005895 // Create our own process group, so that the child and all its
5896 // children can be kill()ed. Don't do this when using pipes,
5897 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005898 (void)setsid();
5899# endif
5900
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005901# ifdef FEAT_TERMINAL
5902 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005903 {
5904 char *term = (char *)T_NAME;
5905
5906#ifdef FEAT_GUI
5907 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005908 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005909 term = getenv("TERM");
5910#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005911 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005912 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005913 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005914 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005915 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005916 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005917 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005918 else if (t_colors > 16)
5919 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005920 else
5921 term = "xterm";
5922 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005923 set_child_environment(
5924 (long)options->jo_term_rows,
5925 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005926 term,
5927 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005928 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005929 else
5930# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005931 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005932
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005933 if (options->jo_env != NULL)
5934 {
5935 dict_T *dict = options->jo_env;
5936 hashitem_T *hi;
5937 int todo = (int)dict->dv_hashtab.ht_used;
5938
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005939 FOR_ALL_HASHTAB_ITEMS(&dict->dv_hashtab, hi, todo)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005940 if (!HASHITEM_EMPTY(hi))
5941 {
5942 typval_T *item = &dict_lookup(hi)->di_tv;
5943
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005944 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005945 --todo;
5946 }
5947 }
5948
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005949 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005950 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005951 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005952 if (null_fd < 0)
5953 {
5954 perror("opening /dev/null failed");
5955 _exit(OPEN_NULL_FAILED);
5956 }
5957 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005958
Bram Moolenaar223896d2017-08-02 22:33:28 +02005959 if (pty_slave_fd >= 0)
5960 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005961 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005962 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005963# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005964 // Try to become controlling tty (probably doesn't work,
5965 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005966 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5967# endif
5968 }
5969
Bram Moolenaar0f873732019-12-05 20:28:46 +01005970 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005971 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005972 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005973 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005974 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005975 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005976 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005977 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005978
Bram Moolenaar0f873732019-12-05 20:28:46 +01005979 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005980 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005981 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005982 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005983 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005984 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005985 }
5986 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005987 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005988 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005989 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005990 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005991 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005992
Bram Moolenaar0f873732019-12-05 20:28:46 +01005993 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005994 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005995 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005996 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005997 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005998 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005999 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02006000 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02006001
6002 if (fd_in[0] >= 0)
6003 close(fd_in[0]);
6004 if (fd_in[1] >= 0)
6005 close(fd_in[1]);
6006 if (fd_out[0] >= 0)
6007 close(fd_out[0]);
6008 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01006009 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02006010 if (fd_err[0] >= 0)
6011 close(fd_err[0]);
6012 if (fd_err[1] >= 0)
6013 close(fd_err[1]);
6014 if (pty_master_fd >= 0)
6015 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006016 close(pty_master_fd); // not used in the child
6017 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01006018 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02006019
Bram Moolenaarf65333c2016-03-08 18:27:21 +01006020 if (null_fd >= 0)
6021 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006022
Bram Moolenaar05aafed2017-08-11 19:12:11 +02006023 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
6024 _exit(EXEC_FAILED);
6025
Bram Moolenaar0f873732019-12-05 20:28:46 +01006026 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01006027 execvp(argv[0], argv);
6028
Bram Moolenaar4694a172016-04-21 14:05:23 +02006029 if (stderr_works)
6030 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02006031# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01006032 // calling free_all_mem() here causes problems. Ignore valgrind
6033 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02006034# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01006035 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01006036 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006037
Bram Moolenaar0f873732019-12-05 20:28:46 +01006038 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02006039 UNBLOCK_SIGNALS(&curset);
6040
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006041 job->jv_pid = pid;
6042 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006043 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006044
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02006045 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006046 close(pty_slave_fd); // not used in the parent
6047 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01006048 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01006049 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01006050 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01006051 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01006052 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01006053 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01006054 if (channel != NULL)
6055 {
Bram Moolenaar652de232019-04-04 20:13:09 +02006056 int in_fd = INVALID_FD;
6057 int out_fd = INVALID_FD;
6058 int err_fd = INVALID_FD;
6059
6060 if (!(use_file_for_in || use_null_for_in))
6061 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
6062
6063 if (!(use_file_for_out || use_null_for_out))
6064 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
6065
6066 // When using pty_master_fd only set it for stdout, do not duplicate
6067 // it for stderr, it only needs to be read once.
6068 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
6069 {
6070 if (fd_err[0] >= 0)
6071 err_fd = fd_err[0];
6072 else if (out_fd != pty_master_fd)
6073 err_fd = pty_master_fd;
6074 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02006075
6076 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01006077 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01006078 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02006079 else
6080 {
6081 if (fd_in[1] >= 0)
6082 close(fd_in[1]);
6083 if (fd_out[0] >= 0)
6084 close(fd_out[0]);
6085 if (fd_err[0] >= 0)
6086 close(fd_err[0]);
6087 if (pty_master_fd >= 0)
6088 close(pty_master_fd);
6089 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006090
Bram Moolenaar0f873732019-12-05 20:28:46 +01006091 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006092 return;
6093
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006094failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01006095 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006096 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006097 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01006098 if (fd_in[1] >= 0)
6099 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006100 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006101 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01006102 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006103 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006104 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006105 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01006106 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006107 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02006108 if (pty_master_fd >= 0)
6109 close(pty_master_fd);
6110 if (pty_slave_fd >= 0)
6111 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01006112}
6113
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006114 static char_u *
6115get_signal_name(int sig)
6116{
6117 int i;
6118 char_u numbuf[NUMBUFLEN];
6119
6120 if (sig == SIGKILL)
John Marriottefc41a52025-01-23 20:05:29 +01006121 return vim_strnsave((char_u *)"kill", STRLEN_LITERAL("kill"));
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006122
6123 for (i = 0; signal_info[i].sig != -1; i++)
6124 if (sig == signal_info[i].sig)
6125 return strlow_save((char_u *)signal_info[i].name);
6126
John Marriottefc41a52025-01-23 20:05:29 +01006127 i = vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
6128 return vim_strnsave(numbuf, i);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006129}
6130
Bram Moolenaar835dc632016-02-07 14:27:38 +01006131 char *
6132mch_job_status(job_T *job)
6133{
6134# ifdef HAVE_UNION_WAIT
6135 union wait status;
6136# else
6137 int status = -1;
6138# endif
6139 pid_t wait_pid = 0;
6140
6141# ifdef __NeXT__
6142 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
6143# else
6144 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
6145# endif
6146 if (wait_pid == -1)
6147 {
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00006148 int waitpid_errno = errno;
6149 if (waitpid_errno == ECHILD && mch_process_running(job->jv_pid))
6150 // The process is alive, but it was probably reparented (for
6151 // example by ptrace called by a debugger like lldb or gdb).
6152 // Note: This assumes that process IDs are not reused.
6153 return "run";
6154
Bram Moolenaar0f873732019-12-05 20:28:46 +01006155 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006156 if (job->jv_status < JOB_ENDED)
6157 ch_log(job->jv_channel, "Job no longer exists: %s",
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00006158 strerror(waitpid_errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02006159 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006160 }
6161 if (wait_pid == 0)
6162 return "run";
6163 if (WIFEXITED(status))
6164 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006165 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01006166 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006167 if (job->jv_status < JOB_ENDED)
6168 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02006169 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006170 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01006171 if (WIFSIGNALED(status))
6172 {
6173 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006174 job->jv_termsig = get_signal_name(WTERMSIG(status));
6175 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
6176 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
6177 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02006178 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01006179 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01006180 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02006181
6182return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01006183 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02006184 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02006185 return "dead";
6186}
6187
6188 job_T *
6189mch_detect_ended_job(job_T *job_list)
6190{
6191# ifdef HAVE_UNION_WAIT
6192 union wait status;
6193# else
6194 int status = -1;
6195# endif
6196 pid_t wait_pid = 0;
6197 job_T *job;
6198
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01006199# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006200 // Do not do this when waiting for a shell command to finish, we would get
6201 // the exit value here (and discard it), the exit value obtained there
6202 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01006203 if (dont_check_job_ended > 0)
6204 return NULL;
6205# endif
6206
Bram Moolenaar97792de2016-10-15 18:36:49 +02006207# ifdef __NeXT__
6208 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
6209# else
6210 wait_pid = waitpid(-1, &status, WNOHANG);
6211# endif
6212 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006213 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02006214 return NULL;
6215 for (job = job_list; job != NULL; job = job->jv_next)
6216 {
6217 if (job->jv_pid == wait_pid)
6218 {
6219 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006220 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02006221 job->jv_exitval = WEXITSTATUS(status);
6222 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006223 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02006224 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006225 job->jv_termsig = get_signal_name(WTERMSIG(status));
6226 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01006227 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02006228 {
6229 ch_log(job->jv_channel, "Job ended");
6230 job->jv_status = JOB_ENDED;
6231 }
6232 return job;
6233 }
6234 }
6235 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006236}
6237
Bram Moolenaar3a117e12016-10-30 21:57:52 +01006238/*
6239 * Send a (deadly) signal to "job".
6240 * Return FAIL if "how" is not a valid name.
6241 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01006242 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006243mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01006244{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006245 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006246
Bram Moolenaar923d9262016-02-25 20:56:01 +01006247 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01006248 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01006249 else if (STRCMP(how, "hup") == 0)
6250 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006251 else if (STRCMP(how, "quit") == 0)
6252 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01006253 else if (STRCMP(how, "int") == 0)
6254 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006255 else if (STRCMP(how, "kill") == 0)
6256 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02006257#ifdef SIGWINCH
6258 else if (STRCMP(how, "winch") == 0)
6259 sig = SIGWINCH;
6260#endif
Keith Thompson184f71c2024-01-04 21:19:04 +01006261 else if (SAFE_isdigit(*how))
Bram Moolenaar835dc632016-02-07 14:27:38 +01006262 sig = atoi((char *)how);
6263 else
6264 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01006265
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01006266 // Never kill ourselves!
6267 if (job->jv_pid != 0)
6268 {
6269 // TODO: have an option to only kill the process, not the group?
6270 kill(-job->jv_pid, sig);
6271 kill(job->jv_pid, sig);
6272 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01006273
Bram Moolenaar835dc632016-02-07 14:27:38 +01006274 return OK;
6275}
Bram Moolenaar76467df2016-02-12 19:30:26 +01006276
6277/*
6278 * Clear the data related to "job".
6279 */
6280 void
6281mch_clear_job(job_T *job)
6282{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006283 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01006284# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006285 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006286# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006287 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006288# endif
6289}
Bram Moolenaar835dc632016-02-07 14:27:38 +01006290#endif
6291
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006292#if defined(FEAT_TERMINAL) || defined(PROTO)
6293 int
6294mch_create_pty_channel(job_T *job, jobopt_T *options)
6295{
6296 int pty_master_fd = -1;
6297 int pty_slave_fd = -1;
6298 channel_T *channel;
6299
Bram Moolenaar59386482019-02-10 22:43:46 +01006300 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02006301 if (pty_master_fd < 0 || pty_slave_fd < 0)
6302 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006303 close(pty_slave_fd);
6304
6305 channel = add_channel();
6306 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006307 {
6308 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006309 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006310 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006311 if (job->jv_tty_out != NULL)
6312 ch_log(channel, "using pty %s on fd %d",
6313 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006314 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006315 channel->ch_keep_open = TRUE;
6316
Bram Moolenaar0f873732019-12-05 20:28:46 +01006317 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6318 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006319 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006320 channel_set_job(channel, job, options);
6321 return OK;
6322}
6323#endif
6324
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325/*
6326 * Check for CTRL-C typed by reading all available characters.
6327 * In cooked mode we should get SIGINT, no need to check.
6328 */
6329 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006330mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006332 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006333 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 fill_input_buf(FALSE);
6335}
6336
6337/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006338 * Wait "msec" msec until a character is available from the mouse, keyboard,
6339 * from inbuf[].
6340 * "msec" == -1 will block forever.
6341 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006342 * When "ignore_input" is TRUE even check for pending input when input is
6343 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006344 * "interrupted" (if not NULL) is set to TRUE when no character is available
6345 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006346 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006347 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 */
6349 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006350WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006352#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006353 return ui_wait_for_chars_or_timer(
6354 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006355#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006356 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006357#endif
6358}
6359
6360/*
6361 * Wait "msec" msec until a character is available from the mouse or keyboard
6362 * or from inbuf[].
6363 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006364 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006365 * "interrupted" (if not NULL) is set to TRUE when no character is available
6366 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006367 * When a GUI is being used, this will never get called -- webb
6368 */
6369 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006370WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006371{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372#ifdef FEAT_MOUSE_GPM
6373 int gpm_process_wanted;
6374#endif
6375#ifdef FEAT_XCLIPBOARD
6376 int rest;
6377#endif
6378 int avail;
6379
Bram Moolenaar0f873732019-12-05 20:28:46 +01006380 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 return 1;
6382
6383#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006384 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 if (WantQueryMouse)
6386 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006387 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006388 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006389 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 }
6391#endif
6392
6393 /*
6394 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6395 * events. This is a bit complicated, because they might both be defined.
6396 */
6397#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6398# ifdef FEAT_XCLIPBOARD
6399 rest = 0;
6400 if (do_xterm_trace())
6401 rest = msec;
6402# endif
6403 do
6404 {
6405# ifdef FEAT_XCLIPBOARD
6406 if (rest != 0)
6407 {
6408 msec = XT_TRACE_DELAY;
6409 if (rest >= 0 && rest < XT_TRACE_DELAY)
6410 msec = rest;
6411 if (rest >= 0)
6412 rest -= msec;
6413 }
6414# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01006415# ifdef FEAT_SOUND_MACOSX
6416 // Invoke any pending sound callbacks.
6417 process_cfrunloop();
6418# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006419# ifdef FEAT_SOUND_CANBERRA
6420 // Invoke any pending sound callbacks.
6421 if (has_sound_callback_in_queue())
6422 invoke_sound_callback();
6423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424# ifdef FEAT_MOUSE_GPM
6425 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006426 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006427 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006428 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006430 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006434 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 return 1;
6436# ifdef FEAT_XCLIPBOARD
6437 if (rest == 0 || !do_xterm_trace())
6438# endif
6439 break;
6440 }
6441 }
6442 while (FALSE
6443# ifdef FEAT_MOUSE_GPM
6444 || (gpm_process_wanted && mch_gpm_process() == 0)
6445# endif
6446# ifdef FEAT_XCLIPBOARD
6447 || (!avail && rest != 0)
6448# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006449 )
6450 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451
6452#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006453 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454#endif
6455 return avail;
6456}
6457
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006458#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459/*
6460 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006461 * "msec" == 0 will check for characters once.
6462 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006465 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006466 * "interrupted" (if not NULL) is set to TRUE when no character is available
6467 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006469 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006470RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006473 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006474#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 static int busy = FALSE;
6476
Bram Moolenaar0f873732019-12-05 20:28:46 +01006477 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478# define MAY_LOOP
6479
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006480# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006481 // Remember at what time we started, so that we know how much longer we
6482 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006483 long start_msec = msec;
6484 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006486 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006487 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488# endif
6489
Bram Moolenaar0f873732019-12-05 20:28:46 +01006490 // Handle being called recursively. This may happen for the session
6491 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 if (busy)
6493 return 0;
6494#endif
6495
6496#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006497 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498#endif
6499 {
6500#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006501 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006502# ifdef FEAT_MZSCHEME
6503 int mzquantum_used = FALSE;
6504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505#endif
6506#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006507 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006508 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 int nfd;
6510# ifdef FEAT_XCLIPBOARD
6511 int xterm_idx = -1;
6512# endif
6513# ifdef FEAT_MOUSE_GPM
6514 int gpm_idx = -1;
6515# endif
6516# ifdef USE_XSMP
6517 int xsmp_idx = -1;
6518# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006519 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006521# ifdef FEAT_MZSCHEME
6522 mzvim_check_threads();
6523 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6524 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006525 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006526 mzquantum_used = TRUE;
6527 }
6528# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 fds[0].fd = fd;
6530 fds[0].events = POLLIN;
6531 nfd = 1;
6532
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006534 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 if (xterm_Shell != (Widget)0)
6536 {
6537 xterm_idx = nfd;
6538 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6539 fds[nfd].events = POLLIN;
6540 nfd++;
6541 }
6542# endif
6543# ifdef FEAT_MOUSE_GPM
6544 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6545 {
6546 gpm_idx = nfd;
6547 fds[nfd].fd = gpm_fd;
6548 fds[nfd].events = POLLIN;
6549 nfd++;
6550 }
6551# endif
6552# ifdef USE_XSMP
6553 if (xsmp_icefd != -1)
6554 {
6555 xsmp_idx = nfd;
6556 fds[nfd].fd = xsmp_icefd;
6557 fds[nfd].events = POLLIN;
6558 nfd++;
6559 }
6560# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006561#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006562 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006563#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006564 if (interrupted != NULL)
6565 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006567 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006568
6569 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006570 if (result == 0 && interrupted != NULL && ret > 0)
6571 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006572
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006573# ifdef FEAT_MZSCHEME
6574 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006575 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006576 finished = FALSE;
6577# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579# ifdef FEAT_XCLIPBOARD
6580 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6581 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006582 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006584 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 finished = FALSE;
6586 }
6587# endif
6588# ifdef FEAT_MOUSE_GPM
6589 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591# endif
6592# ifdef USE_XSMP
6593 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6594 {
6595 if (fds[xsmp_idx].revents & POLLIN)
6596 {
6597 busy = TRUE;
6598 xsmp_handle_requests();
6599 busy = FALSE;
6600 }
6601 else if (fds[xsmp_idx].revents & POLLHUP)
6602 {
6603 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006604 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 xsmp_close();
6606 }
6607 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006608 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 }
6610# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006611#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006612 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006613 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006614 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616
Bram Moolenaar0f873732019-12-05 20:28:46 +01006617#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618
6619 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006620 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006621 // These are static because they can take 8 Kbyte each and cause the
6622 // signal stack to run out with -O3.
6623 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006625 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006627# ifdef FEAT_MZSCHEME
6628 mzvim_check_threads();
6629 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6630 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006631 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006632 mzquantum_used = TRUE;
6633 }
6634# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006636 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006638 tv.tv_sec = towait / 1000;
6639 tv.tv_usec = (towait % 1000) * (1000000/1000);
6640 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006642 else
6643 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644
6645 /*
6646 * Select on ready for reading and exceptional condition (end of file).
6647 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006648select_eintr:
6649 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006650 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 FD_ZERO(&efds);
6652 FD_SET(fd, &rfds);
K.Takatab247e062022-02-07 10:45:23 +00006653# ifndef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006654 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 FD_SET(fd, &efds);
6656# endif
6657 maxfd = fd;
6658
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006660 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 if (xterm_Shell != (Widget)0)
6662 {
6663 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6664 if (maxfd < ConnectionNumber(xterm_dpy))
6665 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006666
Bram Moolenaar0f873732019-12-05 20:28:46 +01006667 // An event may have already been read but not handled. In
6668 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006669 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 }
6671# endif
6672# ifdef FEAT_MOUSE_GPM
6673 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6674 {
6675 FD_SET(gpm_fd, &rfds);
6676 FD_SET(gpm_fd, &efds);
6677 if (maxfd < gpm_fd)
6678 maxfd = gpm_fd;
6679 }
6680# endif
6681# ifdef USE_XSMP
6682 if (xsmp_icefd != -1)
6683 {
6684 FD_SET(xsmp_icefd, &rfds);
6685 FD_SET(xsmp_icefd, &efds);
6686 if (maxfd < xsmp_icefd)
6687 maxfd = xsmp_icefd;
6688 }
6689# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006690# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006691 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006692# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006693 if (interrupted != NULL)
6694 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
Bram Moolenaar643b6142018-09-12 20:29:09 +02006696 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6697 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006698 result = ret > 0 && FD_ISSET(fd, &rfds);
6699 if (result)
6700 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006701 else if (interrupted != NULL && ret > 0)
6702 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006703
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006704# ifdef EINTR
6705 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006706 {
dbivolaruab16ad32021-12-29 19:41:47 +00006707 // Check whether the EINTR is caused by SIGTSTP
6708 if (got_tstp && !in_mch_suspend)
6709 {
6710 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006711
dbivolaruab16ad32021-12-29 19:41:47 +00006712 ea.forceit = TRUE;
6713 ex_stop(&ea);
6714 got_tstp = FALSE;
6715 }
6716
Bram Moolenaar0f873732019-12-05 20:28:46 +01006717 // Check whether window has been resized, EINTR may be caused by
6718 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006719 if (do_resize)
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006720 {
Bram Moolenaar545c8a52023-06-21 15:51:47 +01006721# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006722 ch_log(NULL, "calling handle_resize() in RealWaitForChar()");
Bram Moolenaar545c8a52023-06-21 15:51:47 +01006723# endif
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006724 handle_resize();
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006725 }
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006726
Bram Moolenaar0f873732019-12-05 20:28:46 +01006727 // Interrupted by a signal, need to try again. We ignore msec
6728 // here, because we do want to check even after a timeout if
6729 // characters are available. Needed for reading output of an
6730 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006731 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006732 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006733# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006734# ifdef __TANDEM
6735 if (ret == -1 && errno == ENOTSUP)
6736 {
6737 FD_ZERO(&rfds);
6738 FD_ZERO(&efds);
6739 ret = 0;
6740 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006741# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006742# ifdef FEAT_MZSCHEME
6743 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006744 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006745 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746# endif
6747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748# ifdef FEAT_XCLIPBOARD
6749 if (ret > 0 && xterm_Shell != (Widget)0
6750 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6751 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006752 xterm_update(); // Maybe we should hand out clipboard
6753 // continue looping when we only got the X event and the input
6754 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 if (--ret == 0 && !input_available())
6756 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006757 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 finished = FALSE;
6759 }
6760 }
6761# endif
6762# ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00006763 if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 {
6765 if (FD_ISSET(gpm_fd, &efds))
6766 gpm_close();
6767 else if (FD_ISSET(gpm_fd, &rfds))
6768 *check_for_gpm = 1;
6769 }
6770# endif
6771# ifdef USE_XSMP
6772 if (ret > 0 && xsmp_icefd != -1)
6773 {
6774 if (FD_ISSET(xsmp_icefd, &efds))
6775 {
6776 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006777 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 xsmp_close();
6779 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006780 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 }
6782 else if (FD_ISSET(xsmp_icefd, &rfds))
6783 {
6784 busy = TRUE;
6785 xsmp_handle_requests();
6786 busy = FALSE;
6787 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006788 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 }
6790 }
6791# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006792#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006793 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006794 if (ret >= 0)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01006795 (void)channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
Bram Moolenaar0f873732019-12-05 20:28:46 +01006798#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799
6800#ifdef MAY_LOOP
6801 if (finished || msec == 0)
6802 break;
6803
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006804# ifdef FEAT_CLIENTSERVER
6805 if (server_waiting())
6806 break;
6807# endif
6808
Bram Moolenaar0f873732019-12-05 20:28:46 +01006809 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 if (msec > 0)
6811 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006812# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006813 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006814 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006816 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 msec = msec / 2;
6818# endif
6819 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006820 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 }
6822#endif
6823 }
6824
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006825 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826}
6827
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828/*
Christian Brabandt6cc30272024-12-13 17:54:33 +01006829 * Expand a path into all matching files and/or directories. Handles "*",
6830 * "?", "[a-z]", "**", etc.
6831 * "path" has backslashes before chars that are not to be expanded.
6832 * Returns the number of matches found.
6833 */
6834 int
6835mch_expandpath(
6836 garray_T *gap,
6837 char_u *path,
6838 int flags) // EW_* flags
6839{
6840 return unix_expandpath(gap, path, 0, flags, FALSE);
6841}
6842
6843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 * mch_expand_wildcards() - this code does wild-card pattern matching using
6845 * the shell
6846 *
6847 * return OK for success, FAIL for error (you may lose some memory) and put
6848 * an error message in *file.
6849 *
6850 * num_pat is number of input patterns
6851 * pat is array of pointers to input patterns
6852 * num_file is pointer to number of matched file names
6853 * file is pointer to array of pointers to matched file names
6854 */
6855
6856#ifndef SEEK_SET
6857# define SEEK_SET 0
6858#endif
6859#ifndef SEEK_END
6860# define SEEK_END 2
6861#endif
6862
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006863#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006864
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006866mch_expand_wildcards(
6867 int num_pat,
6868 char_u **pat,
6869 int *num_file,
6870 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006871 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872{
6873 int i;
6874 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006875 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 char_u *p;
6877 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878
Bram Moolenaarc7247912008-01-13 12:54:11 +00006879 /*
6880 * This is the non-OS/2 implementation (really Unix).
6881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 int j;
6883 char_u *tempname;
John Marriottefc41a52025-01-23 20:05:29 +01006884 size_t tempnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 char_u *command;
John Marriottefc41a52025-01-23 20:05:29 +01006886 size_t commandlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 FILE *fd;
6888 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006889#define STYLE_ECHO 0 // use "echo", the default
6890#define STYLE_GLOB 1 // use "glob", for csh
6891#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6892#define STYLE_PRINT 3 // use "print -N", for zsh
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006893#define STYLE_BT 4 // `cmd` expansion, execute the pattern directly
6894#define STYLE_GLOBSTAR 5 // use extended shell glob for bash (this uses extended
6895 // globbing functionality using globstar, needs bash > 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 int shell_style = STYLE_ECHO;
6897 int check_spaces;
6898 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006899 int ampersand = FALSE;
John Marriottefc41a52025-01-23 20:05:29 +01006900#define STRING_INIT(s) \
6901 {(char_u *)(s), STRLEN_LITERAL(s)}
6902 // vimglob() function to define for Posix shell
6903 static string_T sh_vimglob_func = STRING_INIT("vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >");
6904 // vimglob() function with globstar setting enabled, only for bash >= 4.X
6905 static string_T sh_globstar_opt = STRING_INIT("[[ ${BASH_VERSINFO[0]} -ge 4 ]] && shopt -s globstar; ");
6906#undef STRING_INIT
6907
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908
Bram Moolenaar0f873732019-12-05 20:28:46 +01006909 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 *file = NULL;
6911
6912 /*
6913 * If there are no wildcards, just copy the names to allocated memory.
6914 * Saves a lot of time, because we don't have to start a new shell.
6915 */
6916 if (!have_wildcard(num_pat, pat))
6917 return save_patterns(num_pat, pat, num_file, file);
6918
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006919# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006920 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006921 if (sandbox != 0 && check_secure())
6922 return FAIL;
6923# endif
6924
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 /*
6926 * Don't allow the use of backticks in secure and restricted mode.
6927 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006928 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 for (i = 0; i < num_pat; ++i)
6930 if (vim_strchr(pat[i], '`') != NULL
6931 && (check_restricted() || check_secure()))
6932 return FAIL;
6933
6934 /*
6935 * get a name for the temp file
6936 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006937 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006939 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 return FAIL;
6941 }
6942
6943 /*
6944 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006945 * file.
6946 * STYLE_BT: NL separated
6947 * If expanding `cmd` execute it directly.
6948 * STYLE_GLOB: NUL separated
6949 * If we use *csh, "glob" will work better than "echo".
6950 * STYLE_PRINT: NL or NUL separated
6951 * If we use *zsh, "print -N" will work better than "glob".
6952 * STYLE_VIMGLOB: NL separated
6953 * If we use *sh*, we define "vimglob()".
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006954 * STYLE_GLOBSTAR: NL separated
6955 * If we use *bash*, we define "vimglob() and enable globstar option".
Bram Moolenaarc7247912008-01-13 12:54:11 +00006956 * STYLE_ECHO: space separated.
6957 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 */
6959 if (num_pat == 1 && *pat[0] == '`'
6960 && (len = STRLEN(pat[0])) > 2
6961 && *(pat[0] + len - 1) == '`')
6962 shell_style = STYLE_BT;
6963 else if ((len = STRLEN(p_sh)) >= 3)
6964 {
6965 if (STRCMP(p_sh + len - 3, "csh") == 0)
6966 shell_style = STYLE_GLOB;
6967 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6968 shell_style = STYLE_PRINT;
6969 }
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006970 if (shell_style == STYLE_ECHO)
6971 {
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02006972 if (strstr((char *)gettail(p_sh), "bash") != NULL)
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006973 shell_style = STYLE_GLOBSTAR;
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02006974 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006975 shell_style = STYLE_VIMGLOB;
6976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
Bram Moolenaar0f873732019-12-05 20:28:46 +01006978 // Compute the length of the command. We need 2 extra bytes: for the
6979 // optional '&' and for the NUL.
6980 // Worst case: "unset nonomatch; print -N >" plus two is 29
John Marriottefc41a52025-01-23 20:05:29 +01006981 tempnamelen = STRLEN(tempname);
6982 len = tempnamelen + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006983 if (shell_style == STYLE_VIMGLOB)
John Marriottefc41a52025-01-23 20:05:29 +01006984 len += sh_vimglob_func.length;
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006985 else if (shell_style == STYLE_GLOBSTAR)
John Marriottefc41a52025-01-23 20:05:29 +01006986 len += sh_vimglob_func.length + sh_globstar_opt.length;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006987
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006988 for (i = 0; i < num_pat; ++i)
6989 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006990 // Count the length of the patterns in the same way as they are put in
6991 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006992#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006993 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006994#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006995 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006996 for (j = 0; pat[i][j] != NUL; ++j)
6997 {
6998 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006999 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00007000 ++len;
7001 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00007002#endif
7003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 command = alloc(len);
7005 if (command == NULL)
7006 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007007 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 vim_free(tempname);
7009 return FAIL;
7010 }
7011
7012 /*
7013 * Build the shell command:
7014 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
7015 * recognizes this).
7016 * - Add the shell command to print the expanded names.
7017 * - Add the temp file name.
7018 * - Add the file name patterns.
7019 */
7020 if (shell_style == STYLE_BT)
7021 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007022 // change `command; command& ` to (command; command )
John Marriottefc41a52025-01-23 20:05:29 +01007023 commandlen = vim_snprintf((char *)command, len, "(%s)>%s", pat[0] + 1, tempname); // +1 to exclude first backtick
7024
7025 p = (char_u *)strstr((char *)command, ")>");
7026 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {
John Marriottefc41a52025-01-23 20:05:29 +01007028 ; // can't happen ;-)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 }
John Marriottefc41a52025-01-23 20:05:29 +01007030 else
7031 {
7032 --p;
7033 while (p > command && VIM_ISWHITE(*p))
7034 --p;
7035 if (*p == '`') // remove backtick
7036 *p = ' ';
7037
7038 --p;
7039 while (p > command && VIM_ISWHITE(*p))
7040 --p;
7041 if (*p == '&') // remove ampersand
7042 {
7043 ampersand = TRUE;
7044 *p = ' ';
7045 }
7046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 }
7048 else
7049 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00007050 if (shell_style == STYLE_GLOB)
7051 {
7052 // Assume the nonomatch option is valid only for csh like shells,
7053 // otherwise, this may set the positional parameters for the shell,
7054 // e.g. "$*".
John Marriottefc41a52025-01-23 20:05:29 +01007055 commandlen = vim_snprintf((char *)command, len, "%sset nonomatch; glob >%s",
7056 (flags & EW_NOTFOUND) ? "" : "un", tempname);
Christian Brabandt8b8d8292021-11-19 12:37:36 +00007057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 else if (shell_style == STYLE_PRINT)
John Marriottefc41a52025-01-23 20:05:29 +01007059 commandlen = vim_snprintf((char *)command, len, "print -N >%s", tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00007060 else if (shell_style == STYLE_VIMGLOB)
John Marriottefc41a52025-01-23 20:05:29 +01007061 commandlen = vim_snprintf((char *)command, len, "%s%s", sh_vimglob_func.string, tempname);
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02007062 else if (shell_style == STYLE_GLOBSTAR)
John Marriottefc41a52025-01-23 20:05:29 +01007063 commandlen = vim_snprintf((char *)command, len, "%s%s%s", sh_globstar_opt.string,
7064 sh_vimglob_func.string, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 else
John Marriottefc41a52025-01-23 20:05:29 +01007066 commandlen = vim_snprintf((char *)command, len, "echo >%s", tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00007067
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 for (i = 0; i < num_pat; ++i)
7069 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007070 // When using system() always add extra quotes, because the shell
7071 // is started twice. Otherwise put a backslash before special
7072 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073#ifdef USE_SYSTEM
John Marriottefc41a52025-01-23 20:05:29 +01007074 commandlen += vim_snprintf((char *)command + commandlen, len, " \"%s\"", pat[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00007076 int intick = FALSE;
7077
John Marriottefc41a52025-01-23 20:05:29 +01007078 p = command + commandlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00007080 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00007081 {
7082 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00007083 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00007084 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
7085 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007086 // Remove a backslash, take char literally. But keep
7087 // backslash inside backticks, before a special character
7088 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007089 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00007090 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
7091 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007092 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00007093 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00007094 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02007095 else if (!intick
7096 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
7097 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007098 // Put a backslash before a special character, but not
7099 // when inside ``. And not for $var when EW_KEEPDOLLAR is
7100 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00007101 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00007102
Bram Moolenaar0f873732019-12-05 20:28:46 +01007103 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00007104 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00007105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 *p = NUL;
John Marriottefc41a52025-01-23 20:05:29 +01007107 commandlen = (size_t)(p - command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108#endif
7109 }
John Marriottefc41a52025-01-23 20:05:29 +01007110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 if (flags & EW_SILENT)
7112 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01007113 if (ampersand)
John Marriottefc41a52025-01-23 20:05:29 +01007114 STRCPY(command + commandlen, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
7116 /*
7117 * Using zsh -G: If a pattern has no matches, it is just deleted from
7118 * the argument list, otherwise zsh gives an error message and doesn't
7119 * expand any other pattern.
7120 */
7121 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007122 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123
7124 /*
7125 * If we use -f then shell variables set in .cshrc won't get expanded.
7126 * vi can do it, so we will too, but it is only necessary if there is a "$"
7127 * in one of the patterns, otherwise we can still use the fast option.
7128 */
7129 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007130 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131
7132 /*
7133 * execute the shell command
7134 */
7135 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
7136
Bram Moolenaar0f873732019-12-05 20:28:46 +01007137 // When running in the background, give it some time to create the temp
7138 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01007139 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02007140 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141
Bram Moolenaar0f873732019-12-05 20:28:46 +01007142 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 show_shell_mess = TRUE;
7144 vim_free(command);
7145
Bram Moolenaar0f873732019-12-05 20:28:46 +01007146 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 {
7148 mch_remove(tempname);
7149 vim_free(tempname);
7150 /*
7151 * With interactive completion, the error message is not printed.
7152 * However with USE_SYSTEM, I don't know how to turn off error messages
7153 * from the shell, so screen may still get messed up -- webb.
7154 */
7155#ifndef USE_SYSTEM
7156 if (!(flags & EW_SILENT))
7157#endif
7158 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007159 redraw_later_clear(); // probably messed up screen
7160 msg_putchar('\n'); // clear bottom line quickly
7161 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162#ifdef USE_SYSTEM
7163 if (!(flags & EW_SILENT))
7164#endif
7165 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00007166 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01007167 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 }
7169 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007170 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
7171 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 if (shell_style == STYLE_BT)
7173 return FAIL;
7174 goto notfound;
7175 }
7176
7177 /*
7178 * read the names from the file into memory
7179 */
7180 fd = fopen((char *)tempname, READBIN);
7181 if (fd == NULL)
7182 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007183 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if (!(flags & EW_SILENT))
7185 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00007186 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01007187 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 }
7189 vim_free(tempname);
7190 goto notfound;
7191 }
7192 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007193 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02007195 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007196 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02007197 buffer = NULL;
7198 else
7199 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 if (buffer == NULL)
7201 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007202 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 mch_remove(tempname);
7204 vim_free(tempname);
7205 fclose(fd);
7206 return FAIL;
7207 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02007208 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 i = fread((char *)buffer, 1, len, fd);
7210 fclose(fd);
7211 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00007212 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007214 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007215 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 vim_free(tempname);
7217 vim_free(buffer);
7218 return FAIL;
7219 }
7220 vim_free(tempname);
7221
Bram Moolenaar1eed5322019-02-26 17:03:54 +01007222# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01007223 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02007225 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
7227 *p++ = buffer[i];
7228 len = p - buffer;
7229# endif
7230
7231
Bram Moolenaar0f873732019-12-05 20:28:46 +01007232 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 if (shell_style == STYLE_ECHO)
7234 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007235 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007237 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 {
7239 while (*p != ' ' && *p != '\n')
7240 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007241 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 }
7243 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007244 // file names are separated with NL
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02007245 else if (shell_style == STYLE_BT ||
7246 shell_style == STYLE_VIMGLOB ||
7247 shell_style == STYLE_GLOBSTAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007249 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007251 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 {
7253 while (*p != '\n' && *p != NUL)
7254 ++p;
7255 if (*p != NUL)
7256 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007257 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 }
7259 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007260 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 else
7262 {
7263 /*
7264 * Some versions of zsh use spaces instead of NULs to separate
7265 * results. Only do this when there is no NUL before the end of the
7266 * buffer, otherwise we would never be able to use file names with
7267 * embedded spaces when zsh does use NULs.
7268 * When we found a NUL once, we know zsh is OK, set did_find_nul and
7269 * don't check for spaces again.
7270 */
7271 check_spaces = FALSE;
7272 if (shell_style == STYLE_PRINT && !did_find_nul)
7273 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007274 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007275 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01007276 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 did_find_nul = TRUE;
7278 else
7279 check_spaces = TRUE;
7280 }
7281
7282 /*
7283 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
7284 * already is one, for STYLE_GLOB it needs to be added.
7285 */
7286 if (len && buffer[len - 1] == NUL)
7287 --len;
7288 else
7289 buffer[len] = NUL;
7290 i = 0;
7291 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007292 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 {
7294 ++i;
7295 *p = NUL;
7296 }
7297 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007298 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 }
7300 if (i == 0)
7301 {
7302 /*
7303 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
7304 * /bin/sh will happily expand it to nothing rather than returning an
7305 * error; and hey, it's good to check anyway -- webb.
7306 */
7307 vim_free(buffer);
7308 goto notfound;
7309 }
7310 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007311 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 if (*file == NULL)
7313 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007314 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 vim_free(buffer);
7316 return FAIL;
7317 }
7318
7319 /*
7320 * Isolate the individual file names.
7321 */
7322 p = buffer;
7323 for (i = 0; i < *num_file; ++i)
7324 {
7325 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007326 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00007327 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02007328 || shell_style == STYLE_VIMGLOB || shell_style == STYLE_GLOBSTAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00007330 while (!(shell_style == STYLE_ECHO && *p == ' ')
7331 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007333 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 *p = NUL;
7335 else
7336 {
7337 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007338 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 }
7340 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007341 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007343 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007345 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 }
7347 }
7348
7349 /*
7350 * Move the file names to allocated memory.
7351 */
7352 for (j = 0, i = 0; i < *num_file; ++i)
7353 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007354 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7356 continue;
7357
Bram Moolenaar0f873732019-12-05 20:28:46 +01007358 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 dir = (mch_isdir((*file)[i]));
7360 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7361 continue;
7362
Bram Moolenaar0f873732019-12-05 20:28:46 +01007363 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007364 if (!dir && (flags & EW_EXEC)
7365 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007366 continue;
7367
Bram Moolenaar964b3742019-05-24 18:54:09 +02007368 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 if (p)
7370 {
7371 STRCPY(p, (*file)[i]);
7372 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007373 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 (*file)[j++] = p;
7375 }
7376 }
7377 vim_free(buffer);
7378 *num_file = j;
7379
Bram Moolenaar0f873732019-12-05 20:28:46 +01007380 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007382 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 goto notfound;
7384 }
7385
7386 return OK;
7387
7388notfound:
7389 if (flags & EW_NOTFOUND)
7390 return save_patterns(num_pat, pat, num_file, file);
7391 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392}
7393
Bram Moolenaar0f873732019-12-05 20:28:46 +01007394#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007397save_patterns(
7398 int num_pat,
7399 char_u **pat,
7400 int *num_file,
7401 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
7403 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007404 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007406 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 if (*file == NULL)
7408 return FAIL;
7409 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007410 {
7411 s = vim_strsave(pat[i]);
7412 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007413 // Be compatible with expand_filename(): halve the number of
7414 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007415 backslash_halve(s);
7416 (*file)[i] = s;
7417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 *num_file = num_pat;
7419 return OK;
7420}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422/*
7423 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7424 * expand.
7425 */
7426 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007427mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007429 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 if (*p == '\\' && p[1] != NUL)
7432 ++p;
7433 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 if (vim_strchr((char_u *)
7435#ifdef VMS
7436 "*?%"
7437#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439#endif
7440 , *p) != NULL)
7441 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 }
7443 return FALSE;
7444}
7445
7446/*
7447 * Return TRUE if the string "p" contains a wildcard.
7448 * Don't recognize '~' at the end as a wildcard.
7449 */
7450 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007451mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007453 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 if (*p == '\\' && p[1] != NUL)
7456 ++p;
7457 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 if (vim_strchr((char_u *)
7459#ifdef VMS
7460 "*?%$"
7461#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463#endif
7464 , *p) != NULL
7465 || (*p == '~' && p[1] != NUL))
7466 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 }
7468 return FALSE;
7469}
7470
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007472have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473{
7474 int i;
7475
7476 for (i = 0; i < num; i++)
7477 if (mch_has_wildcard(file[i]))
7478 return 1;
7479 return 0;
7480}
7481
7482 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007483have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
7485 int i;
7486
7487 for (i = 0; i < num; i++)
7488 if (vim_strchr(file[i], '$') != NULL)
7489 return TRUE;
7490 return FALSE;
7491}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007493#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494/*
7495 * Scaled-down version of rename(), which is missing in Xenix.
7496 * This version can only move regular files and will fail if the
7497 * destination exists.
7498 */
7499 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007500mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501{
7502 struct stat st;
7503
Bram Moolenaar0f873732019-12-05 20:28:46 +01007504 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007506 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007508 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 return 0;
7510 return -1;
7511}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007512#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007514#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007515# if defined(DYNAMIC_GPM) || defined(PROTO)
7516/*
7517 * Initialize Gpm's symbols for dynamic linking.
7518 * Must be called only if libgpm_hinst is NULL.
7519 */
7520 static int
7521load_libgpm(void)
7522{
7523 libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL);
7524
7525 if (libgpm_hinst == NULL)
7526 {
7527 if (p_verbose > 0)
7528 smsg_attr(HL_ATTR(HLF_W),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007529 _("Could not load gpm library: %s"), dlerror());
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007530 return FAIL;
7531 }
7532
7533 if (
7534 (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL
7535 || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL
7536 || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL
7537 || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL
7538 || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL
7539 )
7540 {
7541 semsg(_(e_could_not_load_library_str_str), "gpm", dlerror());
7542 dlclose(libgpm_hinst);
7543 libgpm_hinst = NULL;
7544 dll_gpm_flag = NULL;
7545 dll_gpm_fd = NULL;
7546 return FAIL;
7547 }
7548 return OK;
7549}
7550
7551 int
7552gpm_available(void)
7553{
7554 return libgpm_hinst != NULL || load_libgpm() == OK;
7555}
7556# endif // DYNAMIC_GPM
7557
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558/*
7559 * Initializes connection with gpm (if it isn't already opened)
7560 * Return 1 if succeeded (or connection already opened), 0 if failed
7561 */
7562 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007563gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007565 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007567#ifdef DYNAMIC_GPM
7568 if (!gpm_available())
7569 return 0;
7570#endif
7571
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007572 if (gpm_flag)
7573 return 1; // already open
7574
7575 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7576 gpm_connect.defaultMask = ~GPM_HARD;
7577 // Default handling for mouse move
7578 gpm_connect.minMod = 0; // Handle any modifier keys
7579 gpm_connect.maxMod = 0xffff;
7580 if (Gpm_Open(&gpm_connect, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007582 // gpm library tries to handling TSTP causes
7583 // problems. Anyways, we close connection to Gpm whenever
7584 // we are going to suspend or starting an external process
7585 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007586# ifdef SIGTSTP
ichizok378447f2023-05-11 22:25:42 +01007587 mch_signal(SIGTSTP, restricted ? SIG_IGN : sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007588# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007589 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007591 if (gpm_fd == -2)
Julio Bcc59d622024-04-04 21:55:10 +02007592 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007593 Gpm_Close(); // We don't want to talk to xterm via gpm
Julio Bcc59d622024-04-04 21:55:10 +02007594
zeertzjqd9be94c2024-07-14 10:20:20 +02007595 // Gpm_Close fails to properly restore the WINCH and TSTP handlers,
7596 // leading to Vim ignoring resize signals. We have to re-initialize
7597 // these handlers again here.
Julio Bcc59d622024-04-04 21:55:10 +02007598# ifdef SIGWINCH
7599 mch_signal(SIGWINCH, sig_winch);
7600# endif
7601# ifdef SIGTSTP
7602 mch_signal(SIGTSTP, restricted ? SIG_IGN : sig_tstp);
7603# endif
7604 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007605 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606}
7607
7608/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007609 * Returns TRUE if the GPM mouse is enabled.
7610 */
7611 int
7612gpm_enabled(void)
7613{
7614 return gpm_flag && gpm_fd >= 0;
7615}
7616
7617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 */
7620 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007621gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007623 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 Gpm_Close();
7625}
7626
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007627/*
7628 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007630 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 */
7632 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007633mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634{
7635 int button;
7636 static Gpm_Event gpm_event;
7637 char_u string[6];
7638 int_u vim_modifiers;
7639 int row,col;
7640 unsigned char buttons_mask;
7641 unsigned char gpm_modifiers;
7642 static unsigned char old_buttons = 0;
7643
7644 Gpm_GetEvent(&gpm_event);
7645
7646#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007647 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 if (hold_gui_events)
7649 return 0;
7650#endif
7651
7652 row = gpm_event.y - 1;
7653 col = gpm_event.x - 1;
7654
Bram Moolenaar0f873732019-12-05 20:28:46 +01007655 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 string[1] = 'M';
7657 string[2] = 'G';
7658 switch (GPM_BARE_EVENTS(gpm_event.type))
7659 {
7660 case GPM_DRAG:
7661 string[3] = MOUSE_DRAG;
7662 break;
7663 case GPM_DOWN:
7664 buttons_mask = gpm_event.buttons & ~old_buttons;
7665 old_buttons = gpm_event.buttons;
7666 switch (buttons_mask)
7667 {
7668 case GPM_B_LEFT:
7669 button = MOUSE_LEFT;
7670 break;
7671 case GPM_B_MIDDLE:
7672 button = MOUSE_MIDDLE;
7673 break;
7674 case GPM_B_RIGHT:
7675 button = MOUSE_RIGHT;
7676 break;
7677 default:
7678 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007679 // Don't know what to do. Can more than one button be
7680 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 }
7682 string[3] = (char_u)(button | 0x20);
7683 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7684 break;
7685 case GPM_UP:
7686 string[3] = MOUSE_RELEASE;
7687 old_buttons &= ~gpm_event.buttons;
7688 break;
7689 default:
7690 return 0;
7691 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007692 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 gpm_modifiers = gpm_event.modifiers;
7694 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007695 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7696 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7697 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7699 vim_modifiers |= MOUSE_SHIFT;
7700
7701 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7702 vim_modifiers |= MOUSE_CTRL;
7703 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7704 vim_modifiers |= MOUSE_ALT;
7705 string[3] |= vim_modifiers;
7706 string[4] = (char_u)(col + ' ' + 1);
7707 string[5] = (char_u)(row + ' ' + 1);
7708 add_to_input_buf(string, 6);
7709 return 6;
7710}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007711#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007713#ifdef FEAT_SYSMOUSE
7714/*
7715 * Initialize connection with sysmouse.
7716 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7717 * output, any sysmouse output than will be processed via sig_sysmouse().
7718 * Return OK if succeeded, FAIL if failed.
7719 */
7720 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007721sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007722{
7723 struct mouse_info mouse;
7724
7725 mouse.operation = MOUSE_MODE;
7726 mouse.u.mode.mode = 0;
7727 mouse.u.mode.signal = SIGUSR2;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007728 if (ioctl(1, CONS_MOUSECTL, &mouse) == -1)
7729 return FAIL;
7730
ichizok378447f2023-05-11 22:25:42 +01007731 mch_signal(SIGUSR2, sig_sysmouse);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007732 mouse.operation = MOUSE_SHOW;
7733 ioctl(1, CONS_MOUSECTL, &mouse);
7734 return OK;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007735}
7736
7737/*
7738 * Stop processing SIGUSR2 signals, and also make sure that
7739 * virtual console do not send us any sysmouse related signal.
7740 */
7741 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007742sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007743{
7744 struct mouse_info mouse;
7745
ichizok378447f2023-05-11 22:25:42 +01007746 mch_signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007747 mouse.operation = MOUSE_MODE;
7748 mouse.u.mode.mode = 0;
7749 mouse.u.mode.signal = 0;
7750 ioctl(1, CONS_MOUSECTL, &mouse);
7751}
7752
7753/*
7754 * Gets info from sysmouse and adds special keys to input buf.
7755 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007756 static void
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007757sig_sysmouse SIGDEFARG(sigarg)
7758{
7759 struct mouse_info mouse;
7760 struct video_info video;
7761 char_u string[6];
7762 int row, col;
7763 int button;
7764 int buttons;
7765 static int oldbuttons = 0;
7766
7767#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007768 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007769 if (hold_gui_events)
7770 return;
7771#endif
7772
7773 mouse.operation = MOUSE_GETINFO;
7774 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7775 && ioctl(1, FBIO_MODEINFO, &video) != -1
7776 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7777 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7778 {
7779 row = mouse.u.data.y / video.vi_cheight;
7780 col = mouse.u.data.x / video.vi_cwidth;
7781 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007782 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007783 string[1] = 'M';
7784 string[2] = 'S';
7785 if (oldbuttons == buttons && buttons != 0)
7786 {
7787 button = MOUSE_DRAG;
7788 }
7789 else
7790 {
7791 switch (buttons)
7792 {
7793 case 0:
7794 button = MOUSE_RELEASE;
7795 break;
7796 case 1:
7797 button = MOUSE_LEFT;
7798 break;
7799 case 2:
7800 button = MOUSE_MIDDLE;
7801 break;
7802 case 4:
7803 button = MOUSE_RIGHT;
7804 break;
7805 default:
7806 return;
7807 }
7808 oldbuttons = buttons;
7809 }
7810 string[3] = (char_u)(button);
7811 string[4] = (char_u)(col + ' ' + 1);
7812 string[5] = (char_u)(row + ' ' + 1);
7813 add_to_input_buf(string, 6);
7814 }
7815 return;
7816}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007817#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007818
LemonBoy9987fe82024-07-04 13:20:49 +02007819/*
7820 * Fill the buffer 'buf' with 'len' random bytes.
7821 * Returns FAIL if the OS PRNG is not available or something went wrong.
7822 */
7823 int
7824mch_get_random(char_u *buf, int len)
7825{
7826 static int dev_urandom_state = NOTDONE;
7827
7828 if (dev_urandom_state == FAIL)
7829 return FAIL;
7830
7831 int fd = open("/dev/urandom", O_RDONLY);
7832
7833 // Attempt reading /dev/urandom.
7834 if (fd == -1)
7835 dev_urandom_state = FAIL;
7836 else if (read(fd, buf, len) == len)
Christian Brabandt93a3d2b2024-07-05 09:54:30 +02007837 {
LemonBoy9987fe82024-07-04 13:20:49 +02007838 dev_urandom_state = OK;
Christian Brabandt93a3d2b2024-07-05 09:54:30 +02007839 close(fd);
7840 }
LemonBoy9987fe82024-07-04 13:20:49 +02007841 else
7842 {
7843 dev_urandom_state = FAIL;
7844 close(fd);
7845 }
7846
7847 return dev_urandom_state;
7848}
7849
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007851typedef char_u * (*STRPROCSTR)(char_u *);
7852typedef char_u * (*INTPROCSTR)(int);
7853typedef int (*STRPROCINT)(char_u *);
7854typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855
7856/*
7857 * Call a DLL routine which takes either a string or int param
7858 * and returns an allocated string.
7859 */
7860 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007861mch_libcall(
7862 char_u *libname,
7863 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007864 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007865 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007866 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007867 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869# if defined(USE_DLOPEN)
7870 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007871 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872# else
7873 shl_t hinstLib;
7874# endif
7875 STRPROCSTR ProcAdd;
7876 INTPROCSTR ProcAddI;
7877 char_u *retval_str = NULL;
7878 int retval_int = 0;
7879 int success = FALSE;
7880
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007881 /*
7882 * Get a handle to the DLL module.
7883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007885 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007886 (void)dlerror();
7887
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 hinstLib = dlopen((char *)libname, RTLD_LAZY
7889# ifdef RTLD_LOCAL
7890 | RTLD_LOCAL
7891# endif
7892 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007893 if (hinstLib == NULL)
7894 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007895 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007896 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007897 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007898 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900# else
7901 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7902# endif
7903
Bram Moolenaar0f873732019-12-05 20:28:46 +01007904 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 if (hinstLib != NULL)
7906 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007907# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 /*
7909 * Catch a crash when calling the library function. For example when
7910 * using a number where a string pointer is expected.
7911 */
7912 mch_startjmp();
7913 if (SETJMP(lc_jump_env) != 0)
7914 {
7915 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007916# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007917 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 mch_didjmp();
7920 }
7921 else
7922# endif
7923 {
7924 retval_str = NULL;
7925 retval_int = 0;
7926
7927 if (argstring != NULL)
7928 {
7929# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007930 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007931 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932# else
7933 if (shl_findsym(&hinstLib, (const char *)funcname,
7934 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7935 ProcAdd = NULL;
7936# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007937 if ((success = (ProcAdd != NULL
7938# if defined(USE_DLOPEN)
7939 && dlerr == NULL
7940# endif
7941 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {
7943 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007944 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 else
7946 retval_str = (ProcAdd)(argstring);
7947 }
7948 }
7949 else
7950 {
7951# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007952 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007953 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954# else
7955 if (shl_findsym(&hinstLib, (const char *)funcname,
7956 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7957 ProcAddI = NULL;
7958# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007959 if ((success = (ProcAddI != NULL
7960# if defined(USE_DLOPEN)
7961 && dlerr == NULL
7962# endif
7963 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 {
7965 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007966 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 else
7968 retval_str = (ProcAddI)(argint);
7969 }
7970 }
7971
Bram Moolenaar0f873732019-12-05 20:28:46 +01007972 // Save the string before we free the library.
7973 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 if (string_result == NULL)
7975 *number_result = retval_int;
7976 else if (retval_str != NULL
7977 && retval_str != (char_u *)1
7978 && retval_str != (char_u *)-1)
7979 *string_result = vim_strsave(retval_str);
7980 }
7981
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007982# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 mch_endjmp();
7984# ifdef SIGHASARG
7985 if (lc_signal != 0)
7986 {
7987 int i;
7988
Bram Moolenaar0f873732019-12-05 20:28:46 +01007989 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 for (i = 0; signal_info[i].sig != -1; i++)
7991 if (lc_signal == signal_info[i].sig)
7992 break;
Bram Moolenaar50809a42023-05-20 16:39:07 +01007993 semsg(_(e_got_sig_str_in_libcall), signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 }
7995# endif
7996# endif
7997
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007999 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00008000 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008001 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00008002
Bram Moolenaar0f873732019-12-05 20:28:46 +01008003 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 (void)dlclose(hinstLib);
8005# else
8006 (void)shl_unload(hinstLib);
8007# endif
8008 }
8009
8010 if (!success)
8011 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008012 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 return FAIL;
8014 }
8015
8016 return OK;
8017}
8018#endif
8019
8020#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008021static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022static int xterm_button;
8023
8024/*
8025 * Setup a dummy window for X selections in a terminal.
8026 */
8027 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008028setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029{
8030 int z = 0;
8031 char *strp = "";
8032 Widget AppShell;
8033
8034 if (!x_connect_to_server())
8035 return;
8036
8037 open_app_context();
8038 if (app_context != NULL && xterm_Shell == (Widget)0)
8039 {
Dominique Pellé4927bc72023-09-24 16:12:07 +02008040 int (*oldhandler)(Display*, XErrorEvent*);
Bram Moolenaarb2148f52019-01-20 23:43:57 +01008041# if defined(USING_SETJMP)
Dominique Pellé4927bc72023-09-24 16:12:07 +02008042 int (*oldIOhandler)(Display*);
Bram Moolenaaredce7422019-01-20 18:39:30 +01008043# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01008044# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01008045 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046
8047 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01008048 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049# endif
8050
Bram Moolenaar0f873732019-12-05 20:28:46 +01008051 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 oldhandler = XSetErrorHandler(x_error_check);
8053
Bram Moolenaarb2148f52019-01-20 23:43:57 +01008054# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008055 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
8057 mch_startjmp();
8058 if (SETJMP(lc_jump_env) != 0)
8059 {
8060 mch_didjmp();
8061 xterm_dpy = NULL;
8062 }
8063 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01008064# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 {
8066 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
8067 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008068 if (xterm_dpy != NULL)
8069 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01008070# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01008072# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 }
8074
Bram Moolenaarb2148f52019-01-20 23:43:57 +01008075# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008076 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01008078# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01008079 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 (void)XSetErrorHandler(oldhandler);
8081
8082 if (xterm_dpy == NULL)
8083 {
8084 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008085 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 return;
8087 }
8088
Bram Moolenaar0f873732019-12-05 20:28:46 +01008089 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 (void)XSetIOErrorHandler(x_IOerror_handler);
8091
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01008092# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008094 {
8095 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01008096 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008097 verbose_leave();
8098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099# endif
8100
Bram Moolenaar0f873732019-12-05 20:28:46 +01008101 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
8103 applicationShellWidgetClass, xterm_dpy,
8104 NULL);
8105 if (AppShell == (Widget)0)
8106 return;
8107 xterm_Shell = XtVaCreatePopupShell("VIM",
8108 topLevelShellWidgetClass, AppShell,
8109 XtNmappedWhenManaged, 0,
8110 XtNwidth, 1,
8111 XtNheight, 1,
8112 NULL);
8113 if (xterm_Shell == (Widget)0)
8114 return;
8115
8116 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02008117 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 if (x11_display == NULL)
8119 x11_display = xterm_dpy;
8120
8121 XtRealizeWidget(xterm_Shell);
8122 XSync(xterm_dpy, False);
8123 xterm_update();
8124 }
8125 if (xterm_Shell != (Widget)0)
8126 {
8127 clip_init(TRUE);
8128 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
8129 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01008130 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 if (test_x11_window(xterm_dpy) == FAIL)
8132 x11_window = 0;
8133 if (x11_window != 0)
8134 xterm_trace = 0;
8135 }
8136}
8137
8138 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008139start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140{
8141 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
8142 return;
8143 xterm_trace = 1;
8144 xterm_button = button;
8145 do_xterm_trace();
8146}
8147
8148
8149 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008150stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151{
8152 if (xterm_trace < 0)
8153 return;
8154 xterm_trace = 0;
8155}
8156
8157/*
8158 * Query the xterm pointer and generate mouse termcodes if necessary
8159 * return TRUE if dragging is active, else FALSE
8160 */
8161 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008162do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163{
8164 Window root, child;
8165 int root_x, root_y;
8166 int win_x, win_y;
8167 int row, col;
8168 int_u mask_return;
8169 char_u buf[50];
8170 char_u *strp;
8171 long got_hints;
John Marriottefc41a52025-01-23 20:05:29 +01008172 static char_u *mouse_code = NULL;
8173 static size_t mouse_codelen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
8175 static int prev_row = 0, prev_col = 0;
8176 static XSizeHints xterm_hints;
8177
8178 if (xterm_trace <= 0)
8179 return FALSE;
8180
8181 if (xterm_trace == 1)
8182 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008183 // Get the hints just before tracking starts. The font size might
8184 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00008185 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
8186 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 || xterm_hints.width_inc <= 1
8188 || xterm_hints.height_inc <= 1)
8189 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008190 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 return FALSE;
8192 }
8193
Bram Moolenaar0f873732019-12-05 20:28:46 +01008194 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 mouse_code = find_termcode(mouse_name);
John Marriottefc41a52025-01-23 20:05:29 +01008196 if (mouse_code != NULL)
8197 mouse_codelen = STRLEN(mouse_code);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02008199 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 xterm_trace = 2;
8201
Bram Moolenaar0f873732019-12-05 20:28:46 +01008202 // Find the offset of the chars, there might be a scrollbar on the
8203 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
8205 &win_x, &win_y, &mask_return);
8206 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
8207 - (xterm_hints.height_inc / 2);
8208 if (xterm_hints.y <= xterm_hints.height_inc / 2)
8209 xterm_hints.y = 2;
8210 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
8211 - (xterm_hints.width_inc / 2);
8212 if (xterm_hints.x <= xterm_hints.width_inc / 2)
8213 xterm_hints.x = 2;
8214 return TRUE;
8215 }
John Marriottefc41a52025-01-23 20:05:29 +01008216
8217 if (mouse_code == NULL || mouse_codelen > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
8219 xterm_trace = 0;
8220 return FALSE;
8221 }
8222
8223 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
8224 &win_x, &win_y, &mask_return);
8225
8226 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
8227 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
8228 if (row == prev_row && col == prev_col)
8229 return TRUE;
8230
8231 STRCPY(buf, mouse_code);
John Marriottefc41a52025-01-23 20:05:29 +01008232 strp = buf + mouse_codelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
8234 *strp++ = (char_u)(col + ' ' + 1);
8235 *strp++ = (char_u)(row + ' ' + 1);
John Marriottefc41a52025-01-23 20:05:29 +01008236 *strp = NUL;
8237 add_to_input_buf(buf, strp - buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238
8239 prev_row = row;
8240 prev_col = col;
8241 return TRUE;
8242}
8243
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02008244# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245/*
8246 * Destroy the display, window and app_context. Required for GTK.
8247 */
8248 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008249clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250{
8251 if (xterm_Shell != (Widget)0)
8252 {
8253 XtDestroyWidget(xterm_Shell);
8254 xterm_Shell = (Widget)0;
8255 }
8256 if (xterm_dpy != NULL)
8257 {
Bram Moolenaare8208012008-06-20 09:59:25 +00008258# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008259 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00008261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 if (x11_display == xterm_dpy)
8263 x11_display = NULL;
8264 xterm_dpy = NULL;
8265 }
Bram Moolenaare8208012008-06-20 09:59:25 +00008266# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 if (app_context != (XtAppContext)NULL)
8268 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008269 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 XtDestroyApplicationContext(app_context);
8271 app_context = (XtAppContext)NULL;
8272 }
Bram Moolenaare8208012008-06-20 09:59:25 +00008273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274}
8275# endif
8276
8277/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01008278 * Catch up with GUI or X events.
8279 */
8280 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008281clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01008282{
8283# ifdef FEAT_GUI
8284 if (gui.in_use)
8285 gui_mch_update();
8286 else
8287# endif
8288 if (xterm_Shell != (Widget)0)
8289 xterm_update();
8290}
8291
8292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 * Catch up with any queued X events. This may put keyboard input into the
8294 * input buffer, call resize call-backs, trigger timers etc. If there is
8295 * nothing in the X event queue (& no timers pending), then we return
8296 * immediately.
8297 */
8298 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008299xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 XEvent event;
8302
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008303 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008305 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008307 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008308 break;
8309
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008310 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008311 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008312 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008313 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008314#ifdef FEAT_CLIENTSERVER
8315 {
8316 XPropertyEvent *e = (XPropertyEvent *)&event;
8317
8318 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008320 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008323 XtDispatchEvent(&event);
8324 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008325 else
8326 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008327 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008328 XtAppProcessEvent(app_context, mask);
8329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 }
8331}
8332
8333 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008334clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
8336 if (xterm_Shell != (Widget)0)
8337 return clip_x11_own_selection(xterm_Shell, cbd);
8338 return FAIL;
8339}
8340
8341 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008342clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343{
8344 if (xterm_Shell != (Widget)0)
8345 clip_x11_lose_selection(xterm_Shell, cbd);
8346}
8347
8348 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008349clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350{
8351 if (xterm_Shell != (Widget)0)
8352 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
8353}
8354
8355 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008356clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357{
8358 clip_x11_set_selection(cbd);
8359}
8360#endif
8361
8362
8363#if defined(USE_XSMP) || defined(PROTO)
8364/*
8365 * Code for X Session Management Protocol.
8366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367
8368# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369/*
8370 * This is our chance to ask the user if they want to save,
8371 * or abort the logout
8372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008374xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
Bram Moolenaare1004402020-10-24 20:49:43 +02008376 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 int cancel_shutdown = False;
8378
Bram Moolenaare1004402020-10-24 20:49:43 +02008379 save_cmod_flags = cmdmod.cmod_flags;
8380 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01008381 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01008382 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02008384 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01008385 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 out_flush();
8387
Bram Moolenaar0f873732019-12-05 20:28:46 +01008388 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 SmcInteractDone(smc_conn, cancel_shutdown);
8390
Bram Moolenaar0f873732019-12-05 20:28:46 +01008391 // Finish off
8392 // Only end save-yourself here if we're not cancelling shutdown;
8393 // we'll get a cancelled callback later in which we'll end it.
8394 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 if (!cancel_shutdown)
8396 {
8397 xsmp.save_yourself = False;
8398 SmcSaveYourselfDone(smc_conn, True);
8399 }
8400}
8401# endif
8402
8403/*
8404 * Callback that starts save-yourself.
8405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008407xsmp_handle_save_yourself(
8408 SmcConn smc_conn,
8409 SmPointer client_data UNUSED,
8410 int save_type UNUSED,
8411 Bool shutdown,
8412 int interact_style UNUSED,
8413 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008415 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 if (xsmp.save_yourself)
8417 SmcSaveYourselfDone(smc_conn, True);
8418 xsmp.save_yourself = True;
8419 xsmp.shutdown = shutdown;
8420
Bram Moolenaar0f873732019-12-05 20:28:46 +01008421 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01008423 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424
8425 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008426 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427
8428# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008429 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008431 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 SmcInteractRequest(smc_conn, SmDialogError,
8433 xsmp_handle_interaction, client_data);
8434 else
8435# endif
8436 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008437 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 SmcSaveYourselfDone(smc_conn, True);
8439 xsmp.save_yourself = False;
8440 }
8441}
8442
8443
8444/*
8445 * Callback to warn us of imminent death.
8446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008448xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449{
8450 xsmp_close();
8451
Bram Moolenaar0f873732019-12-05 20:28:46 +01008452 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 getout_preserve_modified(0);
8454}
8455
8456
8457/*
8458 * Callback to tell us that save-yourself has completed.
8459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008461xsmp_save_complete(
8462 SmcConn smc_conn UNUSED,
8463 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
8465 xsmp.save_yourself = False;
8466}
8467
8468
8469/*
8470 * Callback to tell us that an instigated shutdown was cancelled
8471 * (maybe even by us)
8472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008474xsmp_shutdown_cancelled(
8475 SmcConn smc_conn,
8476 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477{
8478 if (xsmp.save_yourself)
8479 SmcSaveYourselfDone(smc_conn, True);
8480 xsmp.save_yourself = False;
8481 xsmp.shutdown = False;
8482}
8483
8484
8485/*
8486 * Callback to tell us that a new ICE connection has been established.
8487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008489xsmp_ice_connection(
8490 IceConn iceConn,
8491 IcePointer clientData UNUSED,
8492 Bool opening,
8493 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008495 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 if (opening)
8497 {
8498 xsmp_icefd = IceConnectionNumber(iceConn);
8499 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8500 }
8501}
8502
8503
Bram Moolenaar0f873732019-12-05 20:28:46 +01008504// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008506xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507{
8508 Bool rep;
8509
8510 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8511 == IceProcessMessagesIOError)
8512 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008513 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008515 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 xsmp_close();
8517 return FAIL;
8518 }
8519 else
8520 return OK;
8521}
8522
8523static int dummy;
8524
Bram Moolenaar0f873732019-12-05 20:28:46 +01008525// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 void
8527xsmp_init(void)
8528{
8529 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 SmcCallbacks smcallbacks;
8531#if 0
8532 SmPropValue smname;
8533 SmProp smnameprop;
8534 SmProp *smprops[1];
8535#endif
8536
8537 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008538 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539
8540 xsmp.save_yourself = xsmp.shutdown = False;
8541
Bram Moolenaar0f873732019-12-05 20:28:46 +01008542 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8544 smcallbacks.save_yourself.client_data = NULL;
8545 smcallbacks.die.callback = xsmp_die;
8546 smcallbacks.die.client_data = NULL;
8547 smcallbacks.save_complete.callback = xsmp_save_complete;
8548 smcallbacks.save_complete.client_data = NULL;
8549 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8550 smcallbacks.shutdown_cancelled.client_data = NULL;
8551
Bram Moolenaar0f873732019-12-05 20:28:46 +01008552 // Set up a watch on ICE connection creations. The "dummy" argument is
8553 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8555 {
8556 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008557 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 return;
8559 }
8560
Bram Moolenaar0f873732019-12-05 20:28:46 +01008561 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 xsmp.smcconn = SmcOpenConnection(
8563 NULL,
8564 NULL,
8565 SmProtoMajor,
8566 SmProtoMinor,
8567 SmcSaveYourselfProcMask | SmcDieProcMask
8568 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8569 &smcallbacks,
8570 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008571 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008572 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 errorstring);
8574 if (xsmp.smcconn == NULL)
8575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008577 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008578 char errorreport[132];
8579
8580 // If the message is too long it might not be NUL terminated. Add
8581 // a NUL at the end to make sure we don't go over the end.
8582 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008583 vim_snprintf(errorreport, sizeof(errorreport),
8584 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008585 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 return;
8588 }
8589 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8590
8591#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008592 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 smname.value = "vim";
8594 smname.length = 3;
8595 smnameprop.name = "SmProgram";
8596 smnameprop.type = "SmARRAY8";
8597 smnameprop.num_vals = 1;
8598 smnameprop.vals = &smname;
8599
8600 smprops[0] = &smnameprop;
8601 SmcSetProperties(xsmp.smcconn, 1, smprops);
8602#endif
8603}
8604
8605
Bram Moolenaar0f873732019-12-05 20:28:46 +01008606// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008608xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008610 if (xsmp_icefd == -1)
8611 return;
8612
8613 SmcCloseConnection(xsmp.smcconn, 0, NULL);
8614 if (xsmp.clientid != NULL)
8615 free(xsmp.clientid);
8616 xsmp.clientid = NULL;
8617 xsmp_icefd = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008619#endif // USE_XSMP
Paul Ollis65745772022-06-05 16:55:54 +01008620
8621#if defined(FEAT_RELTIME) || defined(PROTO)
Bram Moolenaar08210f82023-04-13 19:15:54 +01008622# if defined(PROF_NSEC) || defined(PROTO)
Paul Ollis65745772022-06-05 16:55:54 +01008623/*
8624 * Implement timeout with timer_create() and timer_settime().
8625 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008626static volatile sig_atomic_t timeout_flag = FALSE;
8627static timer_t timer_id;
8628static int timer_created = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008629
8630/*
8631 * Callback for when the timer expires.
8632 */
8633 static void
8634set_flag(union sigval _unused UNUSED)
8635{
8636 timeout_flag = TRUE;
8637}
8638
8639/*
8640 * Stop any active timeout.
8641 */
8642 void
8643stop_timeout(void)
8644{
8645 static struct itimerspec disarm = {{0, 0}, {0, 0}};
8646
8647 if (timer_created)
8648 {
8649 int ret = timer_settime(timer_id, 0, &disarm, NULL);
8650
8651 if (ret < 0)
8652 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8653 }
8654
8655 // Clear the current timeout flag; any previous timeout should be
8656 // considered _not_ triggered.
8657 timeout_flag = FALSE;
8658}
8659
8660/*
8661 * Start the timeout timer.
8662 *
8663 * The return value is a pointer to a flag that is initialised to FALSE. If the
8664 * timeout expires, the flag is set to TRUE. This will only return pointers to
8665 * static memory; i.e. any pointer returned by this function may always be
8666 * safely dereferenced.
8667 *
8668 * This function is not expected to fail, but if it does it will still return a
8669 * valid flag pointer; the flag will remain stuck as FALSE .
8670 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008671 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008672start_timeout(long msec)
8673{
8674 struct itimerspec interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008675 {0, 0}, // Do not repeat.
8676 {msec / 1000, (msec % 1000) * 1000000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008677 int ret;
8678
8679 // This is really the caller's responsibility, but let's make sure the
8680 // previous timer has been stopped.
8681 stop_timeout();
Paul Ollis65745772022-06-05 16:55:54 +01008682
8683 if (!timer_created)
8684 {
8685 struct sigevent action = {0};
8686
8687 action.sigev_notify = SIGEV_THREAD;
8688 action.sigev_notify_function = set_flag;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008689 ret = timer_create(CLOCK_MONOTONIC, &action, &timer_id);
8690 if (ret < 0)
Paul Ollis65745772022-06-05 16:55:54 +01008691 {
8692 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8693 return &timeout_flag;
8694 }
8695 timer_created = TRUE;
8696 }
8697
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00008698# ifdef FEAT_EVAL
Bram Moolenaar616592e2022-06-17 15:17:10 +01008699 ch_log(NULL, "setting timeout timer to %d sec %ld nsec",
8700 (int)interval.it_value.tv_sec, (long)interval.it_value.tv_nsec);
Bram Moolenaar509ce032022-06-20 11:23:01 +01008701# endif
Paul Ollis65745772022-06-05 16:55:54 +01008702 ret = timer_settime(timer_id, 0, &interval, NULL);
8703 if (ret < 0)
8704 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8705
8706 return &timeout_flag;
8707}
8708
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008709/*
8710 * To be used before fork/exec: delete any created timer.
8711 */
8712 void
8713delete_timer(void)
8714{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008715 if (!timer_created)
8716 return;
8717
8718 timer_delete(timer_id);
8719 timer_created = FALSE;
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008720}
8721
Bram Moolenaar08210f82023-04-13 19:15:54 +01008722# else // PROF_NSEC
Paul Ollis65745772022-06-05 16:55:54 +01008723
8724/*
8725 * Implement timeout with setitimer()
8726 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008727static struct sigaction prev_sigaction;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008728static volatile sig_atomic_t timeout_flag = FALSE;
8729static int timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008730static int timer_handler_active = FALSE;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008731static volatile sig_atomic_t alarm_pending = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008732
8733/*
8734 * Handle SIGALRM for a timeout.
8735 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01008736 static void
Paul Ollis65745772022-06-05 16:55:54 +01008737set_flag SIGDEFARG(sigarg)
8738{
8739 if (alarm_pending)
8740 alarm_pending = FALSE;
8741 else
8742 timeout_flag = TRUE;
8743}
8744
8745/*
8746 * Stop any active timeout.
8747 */
8748 void
8749stop_timeout(void)
8750{
8751 static struct itimerval disarm = {{0, 0}, {0, 0}};
8752 int ret;
8753
8754 if (timer_active)
8755 {
8756 timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008757 ret = setitimer(ITIMER_REAL, &disarm, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008758 if (ret < 0)
8759 // Should only get here as a result of coding errors.
8760 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8761 }
8762
8763 if (timer_handler_active)
8764 {
8765 timer_handler_active = FALSE;
8766 ret = sigaction(SIGALRM, &prev_sigaction, NULL);
8767 if (ret < 0)
8768 // Should only get here as a result of coding errors.
8769 semsg(_(e_could_not_reset_handler_for_timeout_str),
8770 strerror(errno));
8771 }
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008772 timeout_flag = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008773}
8774
8775/*
8776 * Start the timeout timer.
8777 *
8778 * The return value is a pointer to a flag that is initialised to FALSE. If the
8779 * timeout expires, the flag is set to TRUE. This will only return pointers to
8780 * static memory; i.e. any pointer returned by this function may always be
8781 * safely dereferenced.
8782 *
8783 * This function is not expected to fail, but if it does it will still return a
8784 * valid flag pointer; the flag will remain stuck as FALSE .
8785 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008786 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008787start_timeout(long msec)
8788{
8789 struct itimerval interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008790 {0, 0}, // Do not repeat.
8791 {msec / 1000, (msec % 1000) * 1000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008792 struct sigaction handle_alarm;
8793 int ret;
8794 sigset_t sigs;
8795 sigset_t saved_sigs;
8796
8797 // This is really the caller's responsibility, but let's make sure the
8798 // previous timer has been stopped.
8799 stop_timeout();
8800
8801 // There is a small chance that SIGALRM is pending and so the handler must
8802 // ignore it on the first call.
8803 alarm_pending = FALSE;
8804 ret = sigemptyset(&sigs);
8805 ret = ret == 0 ? sigaddset(&sigs, SIGALRM) : ret;
8806 ret = ret == 0 ? sigprocmask(SIG_BLOCK, &sigs, &saved_sigs) : ret;
8807 timeout_flag = FALSE;
8808 ret = ret == 0 ? sigpending(&sigs) : ret;
8809 if (ret == 0)
8810 {
8811 alarm_pending = sigismember(&sigs, SIGALRM);
Bram Moolenaar1f89abf2022-06-06 10:07:01 +01008812 ret = sigprocmask(SIG_SETMASK, &saved_sigs, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008813 }
8814 if (unlikely(ret != 0 || alarm_pending < 0))
8815 {
8816 // Just catching coding errors. Write an error message, but carry on.
8817 semsg(_(e_could_not_check_for_pending_sigalrm_str), strerror(errno));
8818 alarm_pending = FALSE;
8819 }
8820
8821 // Set up the alarm handler first.
8822 ret = sigemptyset(&handle_alarm.sa_mask);
8823 handle_alarm.sa_handler = set_flag;
8824 handle_alarm.sa_flags = 0;
8825 ret = ret == 0 ? sigaction(SIGALRM, &handle_alarm, &prev_sigaction) : ret;
8826 if (ret < 0)
8827 {
8828 // Should only get here as a result of coding errors.
8829 semsg(_(e_could_not_set_handler_for_timeout_str), strerror(errno));
8830 return &timeout_flag;
8831 }
8832 timer_handler_active = TRUE;
8833
8834 // Set up the interval timer once the alarm handler is in place.
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008835 ret = setitimer(ITIMER_REAL, &interval, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008836 if (ret < 0)
8837 {
8838 // Should only get here as a result of coding errors.
8839 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8840 stop_timeout();
8841 return &timeout_flag;
8842 }
8843
8844 timer_active = TRUE;
8845 return &timeout_flag;
8846}
Bram Moolenaar08210f82023-04-13 19:15:54 +01008847# endif // PROF_NSEC
Paul Ollis65745772022-06-05 16:55:54 +01008848#endif // FEAT_RELTIME