blob: 861ff6c33611d858a8a49ae0859d3a3b54229113 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * OS/2 port by Paul Slootman
5 * VMS merge by Zoltan Arpadffy
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code.
10 */
11
12/*
13 * os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
14 * Also for OS/2, using the excellent EMX package!!!
Bram Moolenaar041c7102020-05-30 18:14:57 +020015 * Also for Atari MiNT.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016 *
17 * A lot of this file was originally written by Juergen Weigert and later
18 * changed beyond recognition.
19 */
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#include "vim.h"
22
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023#ifdef FEAT_MZSCHEME
24# include "if_mzsch.h"
25#endif
26
Bram Moolenaar0f873732019-12-05 20:28:46 +010027#include "os_unixx.h" // unix includes for os_unix.c only
Bram Moolenaar071d4272004-06-13 20:20:40 +000028
29#ifdef USE_XSMP
30# include <X11/SM/SMlib.h>
31#endif
32
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000033#ifdef HAVE_SELINUX
34# include <selinux/selinux.h>
35static int selinux_enabled = -1;
36#endif
37
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020038#ifdef HAVE_SMACK
39# include <attr/xattr.h>
40# include <linux/xattr.h>
41# ifndef SMACK_LABEL_LEN
42# define SMACK_LABEL_LEN 1024
43# endif
44#endif
45
Bram Moolenaara2442432007-04-26 14:26:37 +000046#ifdef __CYGWIN__
K.Takata972db232022-02-04 10:45:38 +000047# include <cygwin/version.h>
48# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
Bram Moolenaar0f873732019-12-05 20:28:46 +010049 // for cygwin_conv_path()
K.Takata972db232022-02-04 10:45:38 +000050# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
51# define WIN32_LEAN_AND_MEAN
52# include <windows.h>
53# include "winclip.pro"
Bram Moolenaara2442432007-04-26 14:26:37 +000054# endif
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000058
Bram Moolenaar071d4272004-06-13 20:20:40 +000059# include <gpm.h>
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000060
61# ifdef DYNAMIC_GPM
62# define Gpm_Open (*dll_Gpm_Open)
63# define Gpm_Close (*dll_Gpm_Close)
64# define Gpm_GetEvent (*dll_Gpm_GetEvent)
65# define gpm_flag (dll_gpm_flag != NULL ? *dll_gpm_flag : 0)
66# define gpm_fd (dll_gpm_fd != NULL ? *dll_gpm_fd : -1)
67
68static int (*dll_Gpm_Open) (Gpm_Connect *, int);
69static int (*dll_Gpm_Close) (void);
70static int (*dll_Gpm_GetEvent) (Gpm_Event *);
71static int *dll_gpm_flag;
72static int *dll_gpm_fd;
73
74static void *libgpm_hinst;
75# endif
76
Bram Moolenaar0f873732019-12-05 20:28:46 +010077// <linux/keyboard.h> contains defines conflicting with "keymap.h",
78// I just copied relevant defines here. A cleaner solution would be to put gpm
79// code into separate file and include there linux/keyboard.h
80// #include <linux/keyboard.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000081# define KG_SHIFT 0
82# define KG_CTRL 2
83# define KG_ALT 3
84# define KG_ALTGR 1
85# define KG_SHIFTL 4
86# define KG_SHIFTR 5
87# define KG_CTRLL 6
88# define KG_CTRLR 7
89# define KG_CAPSSHIFT 8
90
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010091static void gpm_close(void);
92static int gpm_open(void);
93static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#endif
95
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000096#ifdef FEAT_SYSMOUSE
97# include <sys/consio.h>
98# include <sys/fbio.h>
99
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100100static int sysmouse_open(void);
101static void sysmouse_close(void);
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100102static void sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000103#endif
104
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105/*
106 * end of autoconf section. To be extended...
107 */
108
Bram Moolenaar0f873732019-12-05 20:28:46 +0100109// Are the following #ifdefs still required? And why? Is that for X11?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
111#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
112# ifdef SIGWINCH
113# undef SIGWINCH
114# endif
115# ifdef TIOCGWINSZ
116# undef TIOCGWINSZ
117# endif
118#endif
119
Bram Moolenaar0f873732019-12-05 20:28:46 +0100120#if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121# define SIGWINCH SIGWINDOW
122#endif
123
124#ifdef FEAT_X11
125# include <X11/Xlib.h>
126# include <X11/Xutil.h>
127# include <X11/Xatom.h>
128# ifdef FEAT_XCLIPBOARD
129# include <X11/Intrinsic.h>
130# include <X11/Shell.h>
131# include <X11/StringDefs.h>
132static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void clip_update(void);
134static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135# endif
136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137Window x11_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
140
Bram Moolenaar5c3128e2020-05-11 20:54:42 +0200141static int ignore_sigtstp = FALSE;
142
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200146static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +0200147static int unix_did_set_title = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static char_u *oldicon = NULL;
149static int did_set_icon = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
Bram Moolenaar205b8862011-09-07 15:04:31 +0200153#ifdef HAVE_UNION_WAIT
154typedef union wait waitstatus;
155#else
156typedef int waitstatus;
157#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200158static int WaitForChar(long msec, int *interrupted, int ignore_input);
159static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar041c7102020-05-30 18:14:57 +0200160#ifdef VMS
Bram Moolenaarcda77642016-06-04 13:32:35 +0200161int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200163static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#endif
165
166#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100167static int do_xterm_trace(void);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100168# define XT_TRACE_DELAY 50 // delay for xterm tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
170
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
173#if defined(SIGWINCH)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100174static void sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000176#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100177static void sig_tstp SIGPROTOARG;
Bram Moolenaarabd56da2022-06-23 20:46:27 +0100178// volatile because it is used in signal handler sig_tstp() and
179// sigcont_handler().
dbivolaruab16ad32021-12-29 19:41:47 +0000180static volatile sig_atomic_t in_mch_suspend = FALSE;
181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#if defined(SIGINT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100183static void catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200185#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100186static void catch_sigusr1 SIGPROTOARG;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100189static void catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000191#if defined(SIGALRM) && defined(FEAT_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define SET_SIG_ALARM
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100193static void sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100194// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200195static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196#endif
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100197static void deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100199static void catch_int_signal(void);
200static void set_signals(void);
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100201static void catch_signals(void (*func_deadly)(int), void (*func_other)(int));
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200202#ifdef HAVE_SIGPROCMASK
203# define SIGSET_DECL(set) sigset_t set;
204# define BLOCK_SIGNALS(set) block_signals(set)
205# define UNBLOCK_SIGNALS(set) unblock_signals(set)
206#else
207# define SIGSET_DECL(set)
208# define BLOCK_SIGNALS(set) do { /**/ } while (0)
209# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
210#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100211static int have_wildcard(int, char_u **);
212static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100214static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215
216#ifndef SIG_ERR
ichizok378447f2023-05-11 22:25:42 +0100217# define SIG_ERR ((sighandler_T)-1)
218#endif
219#ifndef SIG_HOLD
220# define SIG_HOLD ((sighandler_T)-2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#endif
222
Bram Moolenaar0f873732019-12-05 20:28:46 +0100223// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200224static volatile sig_atomic_t do_resize = FALSE;
dbivolaruab16ad32021-12-29 19:41:47 +0000225// volatile because it is used in signal handler sig_tstp().
226static volatile sig_atomic_t got_tstp = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227static char_u *extra_shell_arg = NULL;
228static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100229// volatile because it is used in signal handler deathtrap().
230static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
231// volatile because it is used in signal handler deathtrap().
232static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100234#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
235static int dont_check_job_ended = 0;
236#endif
237
Bram Moolenaar26e86442020-05-17 14:06:16 +0200238// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
239static tmode_T mch_cur_tmode = TMODE_COOK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
241#ifdef USE_XSMP
242typedef struct
243{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100244 SmcConn smcconn; // The SM connection ID
245 IceConn iceconn; // The ICE connection ID
246 char *clientid; // The client ID for the current smc session
247 Bool save_yourself; // If we're in the middle of a save_yourself
248 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249} xsmp_config_T;
250
251static xsmp_config_T xsmp;
252#endif
253
254#ifdef SYS_SIGLIST_DECLARED
255/*
256 * I have seen
257 * extern char *_sys_siglist[NSIG];
Yegappan Lakshmananaebc6ef2022-08-27 21:24:26 +0100258 * on Linux, NetBSD and Solaris. It contains a nice list of strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 * that describe the signals. That is nearly what we want here. But
260 * autoconf does only check for sys_siglist (without the underscore), I
261 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100262 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 */
264#endif
265
266static struct signalinfo
267{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100268 int sig; // Signal number, eg. SIGSEGV etc
269 char *name; // Signal name (not char_u!).
270 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271} signal_info[] =
272{
273#ifdef SIGHUP
274 {SIGHUP, "HUP", TRUE},
275#endif
276#ifdef SIGQUIT
277 {SIGQUIT, "QUIT", TRUE},
278#endif
279#ifdef SIGILL
280 {SIGILL, "ILL", TRUE},
281#endif
282#ifdef SIGTRAP
283 {SIGTRAP, "TRAP", TRUE},
284#endif
285#ifdef SIGABRT
286 {SIGABRT, "ABRT", TRUE},
287#endif
288#ifdef SIGEMT
289 {SIGEMT, "EMT", TRUE},
290#endif
291#ifdef SIGFPE
292 {SIGFPE, "FPE", TRUE},
293#endif
294#ifdef SIGBUS
295 {SIGBUS, "BUS", TRUE},
296#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100297#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100298 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 {SIGSEGV, "SEGV", TRUE},
300#endif
301#ifdef SIGSYS
302 {SIGSYS, "SYS", TRUE},
303#endif
304#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100305 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
307#ifdef SIGTERM
308 {SIGTERM, "TERM", TRUE},
309#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100310#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 {SIGVTALRM, "VTALRM", TRUE},
312#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000313#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100314 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
315 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 {SIGPROF, "PROF", TRUE},
317#endif
318#ifdef SIGXCPU
319 {SIGXCPU, "XCPU", TRUE},
320#endif
321#ifdef SIGXFSZ
322 {SIGXFSZ, "XFSZ", TRUE},
323#endif
324#ifdef SIGUSR1
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200325 {SIGUSR1, "USR1", FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000327#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100328 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 {SIGUSR2, "USR2", TRUE},
330#endif
331#ifdef SIGINT
332 {SIGINT, "INT", FALSE},
333#endif
334#ifdef SIGWINCH
335 {SIGWINCH, "WINCH", FALSE},
336#endif
337#ifdef SIGTSTP
338 {SIGTSTP, "TSTP", FALSE},
339#endif
340#ifdef SIGPIPE
341 {SIGPIPE, "PIPE", FALSE},
342#endif
343 {-1, "Unknown!", FALSE}
344};
345
ichizok378447f2023-05-11 22:25:42 +0100346 sighandler_T
347mch_signal(int sig, sighandler_T func)
348{
349#if defined(HAVE_SIGACTION) && defined(HAVE_SIGPROCMASK)
350 // Modern implementation: use sigaction().
351 struct sigaction sa, old;
352 sigset_t curset;
353 int blocked;
354
355 if (sigprocmask(SIG_BLOCK, NULL, &curset) == -1)
356 return SIG_ERR;
357
358 blocked = sigismember(&curset, sig);
359
360 if (func == SIG_HOLD)
361 {
362 if (blocked)
363 return SIG_HOLD;
364
365 sigemptyset(&curset);
366 sigaddset(&curset, sig);
367
368 if (sigaction(sig, NULL, &old) == -1
369 || sigprocmask(SIG_BLOCK, &curset, NULL) == -1)
370 return SIG_ERR;
371 return old.sa_handler;
372 }
373
374 if (blocked)
375 {
376 sigemptyset(&curset);
377 sigaddset(&curset, sig);
378
379 if (sigprocmask(SIG_UNBLOCK, &curset, NULL) == -1)
380 return SIG_ERR;
381 }
382
383 sa.sa_handler = func;
384 sigemptyset(&sa.sa_mask);
385# ifdef SA_RESTART
386 sa.sa_flags = SA_RESTART;
387# else
388 sa.sa_flags = 0;
389# endif
390 if (sigaction(sig, &sa, &old) == -1)
391 return SIG_ERR;
392 return blocked ? SIG_HOLD: old.sa_handler;
393#elif defined(HAVE_SIGSET)
394 // Using sigset() is preferred above signal().
395 return sigset(sig, func);
396#else
397 // Oldest and most compatible solution.
398 return signal(sig, func);
399#endif
400}
401
Bram Moolenaar25724922009-07-14 15:38:41 +0000402 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100403mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000404{
405 if (p_verbose >= 5)
406 {
407 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100408 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000409 verbose_leave();
410 }
ichizok378447f2023-05-11 22:25:42 +0100411#ifdef VMS
Bram Moolenaar25724922009-07-14 15:38:41 +0000412 return chdir(vms_fixfilename(path));
ichizok378447f2023-05-11 22:25:42 +0100413#else
Bram Moolenaar25724922009-07-14 15:38:41 +0000414 return chdir(path);
ichizok378447f2023-05-11 22:25:42 +0100415#endif
Bram Moolenaar25724922009-07-14 15:38:41 +0000416}
417
Bram Moolenaar0f873732019-12-05 20:28:46 +0100418// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100419#if defined(ECHOE) && defined(ICANON) \
420 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
421 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200422# define NEW_TTY_SYSTEM
423#endif
424
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000425/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100426 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100429mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200431 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100432 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100433 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434}
435
436/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100437 * Function passed to inchar_loop() to handle window resizing.
438 * If "check_only" is TRUE: Return whether there was a resize.
439 * If "check_only" is FALSE: Deal with the window resized.
440 */
441 static int
442resize_func(int check_only)
443{
444 if (check_only)
445 return do_resize;
446 while (do_resize)
Bram Moolenaar55f1b822023-06-21 13:42:48 +0100447 {
448 ch_log(NULL, "calling handle_resize() in resize_func()");
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100449 handle_resize();
Bram Moolenaar55f1b822023-06-21 13:42:48 +0100450 }
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100451 return FALSE;
452}
453
454/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000455 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 * Get a characters from the keyboard.
457 * Return the number of characters that are available.
458 * If wtime == 0 do not wait for characters.
459 * If wtime == n wait a short time for characters.
460 * If wtime == -1 wait forever for characters.
461 */
462 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100463mch_inchar(
464 char_u *buf,
465 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100466 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100467 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100469 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
470 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471}
472
473 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100474handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
476 do_resize = FALSE;
477 shell_resized();
478}
479
480/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200481 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 */
483 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100484mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200486 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487}
488
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200489#if defined(FEAT_TERMINAL) || defined(PROTO)
490/*
491 * Check for any pending input or messages.
492 */
493 int
494mch_check_messages(void)
495{
496 return WaitForChar(0L, NULL, TRUE);
497}
498#endif
499
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
501# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000502# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503# endif
504# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
505# include <sys/sysctl.h>
506# endif
507# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
508# include <sys/sysinfo.h>
509# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100510# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100511# include <mach/mach_host.h>
512# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100513# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514
515/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000516 * Return total amount of memory available in Kbyte.
517 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100520mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100523 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524
Bram Moolenaar362dc332018-03-05 21:59:37 +0100525# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100526 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100527 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100528 mach_port_t host = mach_host_self();
529 kern_return_t kret;
530# ifdef HOST_VM_INFO64
531 struct vm_statistics64 vm_stat;
532 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533
Bram Moolenaar988615f2018-02-27 17:58:20 +0100534 kret = host_statistics64(host, HOST_VM_INFO64,
535 (host_info64_t)&vm_stat, &count);
536# else
537 struct vm_statistics vm_stat;
538 natural_t count = HOST_VM_INFO_COUNT;
539
540 kret = host_statistics(host, HOST_VM_INFO,
541 (host_info_t)&vm_stat, &count);
542# endif
543 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100544 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100545 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
546 + vm_stat.inactive_count
547# ifdef MAC_OS_X_VERSION_10_9
548 + vm_stat.compressor_page_count
549# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100550 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100551 mach_port_deallocate(mach_task_self(), host);
552 }
553# endif
554
555# ifdef HAVE_SYSCTL
556 if (mem == 0)
557 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100558 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100559 int mib[2];
560 size_t len = sizeof(long_u);
561# ifdef HW_USERMEM64
562 long_u physmem;
563# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100564 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100565 union {
566 int_u u32;
567 long_u u64;
568 } physmem;
569# endif
570
571 mib[0] = CTL_HW;
572# ifdef HW_USERMEM64
573 mib[1] = HW_USERMEM64;
574# else
575 mib[1] = HW_USERMEM;
576# endif
577 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
578 {
579# ifdef HW_USERMEM64
580 mem = (long_u)physmem;
581# else
582 if (len == sizeof(physmem.u64))
583 mem = (long_u)physmem.u64;
584 else
585 mem = (long_u)physmem.u32;
586# endif
587 }
588 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200589# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200591# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 if (mem == 0)
593 {
594 struct sysinfo sinfo;
595
Bram Moolenaar0f873732019-12-05 20:28:46 +0100596 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000598 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200599# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100600 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000601 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
602 {
603 sinfo.mem_unit = sinfo.mem_unit >> 1;
604 --shiftright;
605 }
606 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200607# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200609# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200614# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 if (mem == 0)
616 {
617 long pagesize, pagecount;
618
Bram Moolenaar0f873732019-12-05 20:28:46 +0100619 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 pagesize = sysconf(_SC_PAGESIZE);
621 pagecount = sysconf(_SC_PHYS_PAGES);
622 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000623 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100624 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000625 while (shiftright > 0 && (pagesize & 1) == 0)
626 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000627 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000628 --shiftright;
629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
Bram Moolenaar0f873732019-12-05 20:28:46 +0100635 // Return the minimum of the physical memory and the user limit, because
636 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200637# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 {
639 struct rlimit rlp;
640
641 if (getrlimit(RLIMIT_DATA, &rlp) == 0
642 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200643# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200645# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000646 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000648 {
649 mem = (long_u)rlp.rlim_cur;
650 shiftright = 10;
651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200653# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
655 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000656 return mem >> shiftright;
657 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658}
659#endif
660
Bram Moolenaar0981c872020-08-23 14:28:37 +0200661/*
662 * "flags": MCH_DELAY_IGNOREINPUT - don't read input
663 * MCH_DELAY_SETTMODE - use settmode() even for short delays
664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200666mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200668 tmode_T old_tmode;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200669 int call_settmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000670#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100671 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673
Bram Moolenaar0981c872020-08-23 14:28:37 +0200674 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100676 // Go to cooked mode without echo, to allow SIGINT interrupting us
677 // here. But we don't want QUIT to kill us (CTRL-\ used in a
678 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200679 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000680 in_mch_delay = TRUE;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200681 call_settmode = mch_cur_tmode == TMODE_RAW
682 && (msec > 500 || (flags & MCH_DELAY_SETTMODE));
683 if (call_settmode)
684 {
685 old_tmode = mch_cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 settmode(TMODE_SLEEP);
Bram Moolenaar80361a52020-10-05 21:39:25 +0200687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
689 /*
690 * Everybody sleeps in a different way...
691 * Prefer nanosleep(), some versions of usleep() can only sleep up to
692 * one second.
693 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000694#ifdef FEAT_MZSCHEME
695 do
696 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100697 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000698 if (total > p_mzq)
699 msec = p_mzq;
700 else
701 msec = total;
702 total -= msec;
703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#ifdef HAVE_NANOSLEEP
705 {
706 struct timespec ts;
707
708 ts.tv_sec = msec / 1000;
709 ts.tv_nsec = (msec % 1000) * 1000000;
710 (void)nanosleep(&ts, NULL);
711 }
712#else
713# ifdef HAVE_USLEEP
714 while (msec >= 1000)
715 {
716 usleep((unsigned int)(999 * 1000));
717 msec -= 999;
718 }
719 usleep((unsigned int)(msec * 1000));
720# else
721# ifndef HAVE_SELECT
722 poll(NULL, 0, (int)msec);
723# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {
725 struct timeval tv;
726
727 tv.tv_sec = msec / 1000;
728 tv.tv_usec = (msec % 1000) * 1000;
Bram Moolenaar0981c872020-08-23 14:28:37 +0200729 // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
730 // a patch from Sun to fix this. Reported by Gunnar Pedersen.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 select(0, NULL, NULL, NULL, &tv);
732 }
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100733# endif
734# endif
735#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000736#ifdef FEAT_MZSCHEME
737 }
738 while (total > 0);
739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaar80361a52020-10-05 21:39:25 +0200741 if (call_settmode)
Bram Moolenaar26e86442020-05-17 14:06:16 +0200742 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000743 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 }
745 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200746 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747}
748
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000749#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
751# define HAVE_CHECK_STACK_GROWTH
752/*
753 * Support for checking for an almost-out-of-stack-space situation.
754 */
755
756/*
757 * Return a pointer to an item on the stack. Used to find out if the stack
758 * grows up or down.
759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760static int stack_grows_downwards;
761
762/*
763 * Find out if the stack grows upwards or downwards.
764 * "p" points to a variable on the stack of the caller.
765 */
766 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100767check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768{
769 int i;
770
771 stack_grows_downwards = (p > (char *)&i);
772}
773#endif
774
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000775#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776static char *stack_limit = NULL;
777
778#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
779# include <pthread.h>
780# include <pthread_np.h>
781#endif
782
783/*
784 * Find out until how var the stack can grow without getting into trouble.
785 * Called when starting up and when switching to the signal stack in
786 * deathtrap().
787 */
788 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100789get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790{
791 struct rlimit rlp;
792 int i;
793 long lim;
794
Bram Moolenaar0f873732019-12-05 20:28:46 +0100795 // Set the stack limit to 15/16 of the allowable size. Skip this when the
796 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 if (getrlimit(RLIMIT_STACK, &rlp) == 0
798 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
799# ifdef RLIM_INFINITY
800 && rlp.rlim_cur != RLIM_INFINITY
801# endif
802 )
803 {
804 lim = (long)rlp.rlim_cur;
805#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
806 {
807 pthread_attr_t attr;
808 size_t size;
809
Bram Moolenaar0f873732019-12-05 20:28:46 +0100810 // On FreeBSD the initial thread always has a fixed stack size, no
811 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 pthread_attr_init(&attr);
813 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
814 {
815 pthread_attr_getstacksize(&attr, &size);
816 if (lim > (long)size)
817 lim = (long)size;
818 }
819 pthread_attr_destroy(&attr);
820 }
821#endif
822 if (stack_grows_downwards)
823 {
824 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
825 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100826 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 stack_limit = (char *)((long)&i / 16L);
828 }
829 else
830 {
831 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
832 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100833 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 }
835 }
836}
837
838/*
839 * Return FAIL when running out of stack space.
840 * "p" must point to any variable local to the caller that's on the stack.
841 */
842 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100843mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000845 if (stack_limit == NULL)
846 return OK;
847
848 if (stack_grows_downwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000850 if (p < stack_limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 return FAIL;
852 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000853 else if (p > stack_limit)
854 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 return OK;
856}
857#endif
858
859#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
860/*
861 * Support for using the signal stack.
862 * This helps when we run out of stack space, which causes a SIGSEGV. The
863 * signal handler then must run on another stack, since the normal stack is
864 * completely full.
865 */
866
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100868static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100870static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871# endif
872
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200873/*
874 * Get a size of signal stack.
875 * Preference (if available): sysconf > SIGSTKSZ > guessed size
876 */
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100877static long int get_signal_stack_size(void)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200878{
879# ifdef HAVE_SYSCONF_SIGSTKSZ
880 long int size = -1;
881
882 // return size only if sysconf doesn't return an error
883 if ((size = sysconf(_SC_SIGSTKSZ)) > -1)
884 return size;
885# endif
886
887# ifdef SIGSTKSZ
888 // if sysconf() isn't available or gives error, return SIGSTKSZ
889 // if defined
890 return SIGSTKSZ;
891# endif
892
893 // otherwise guess the size
894 return 8000;
895}
896
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897static char *signal_stack;
898
899 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100900init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000902 if (signal_stack == NULL)
903 return;
904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906# ifdef HAVE_SS_BASE
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000907 sigstk.ss_base = signal_stack;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000909 sigstk.ss_sp = signal_stack;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000911 sigstk.ss_size = get_signal_stack_size();
912 sigstk.ss_flags = 0;
913 (void)sigaltstack(&sigstk, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000915 sigstk.ss_sp = signal_stack;
916 if (stack_grows_downwards)
917 sigstk.ss_sp += get_signal_stack_size() - 1;
918 sigstk.ss_onstack = 0;
919 (void)sigstack(&sigstk, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921}
922#endif
923
924/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000925 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 * will barf when the second argument to signal() is ``wrong''.
927 * Let me try it with a few tricky defines from my own osdef.h (jw).
928 */
929#if defined(SIGWINCH)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100930 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931sig_winch SIGDEFARG(sigarg)
932{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100933 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100934 mch_signal(SIGWINCH, sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 do_resize = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936}
937#endif
938
dbivolaruab16ad32021-12-29 19:41:47 +0000939#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100940 static void
dbivolaruab16ad32021-12-29 19:41:47 +0000941sig_tstp SIGDEFARG(sigarg)
942{
943 // Second time we get called we actually need to suspend
944 if (in_mch_suspend)
945 {
ichizok378447f2023-05-11 22:25:42 +0100946 mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
dbivolaruab16ad32021-12-29 19:41:47 +0000947 raise(sigarg);
948 }
dbivolaru79a6e252022-01-23 16:41:14 +0000949 else
950 got_tstp = TRUE;
dbivolaruab16ad32021-12-29 19:41:47 +0000951
ichizok5f823d12022-03-13 17:27:38 +0000952#if !defined(__ANDROID__) && !defined(__OpenBSD__) && !defined(__DragonFly__)
953 // This is not required on all systems. On some systems (at least Android,
954 // OpenBSD, and DragonFlyBSD) this breaks suspending with CTRL-Z.
ichizok378447f2023-05-11 22:25:42 +0100955 mch_signal(SIGTSTP, sig_tstp);
xtkobacbef12e2022-02-27 12:31:52 +0000956#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000957}
958#endif
959
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#if defined(SIGINT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100961 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962catch_sigint SIGDEFARG(sigarg)
963{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100964 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100965 mch_signal(SIGINT, catch_sigint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 got_int = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968#endif
969
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200970#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100971 static void
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200972catch_sigusr1 SIGDEFARG(sigarg)
973{
974 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100975 mch_signal(SIGUSR1, catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200976 got_sigusr1 = TRUE;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200977}
978#endif
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100981 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982catch_sigpwr SIGDEFARG(sigarg)
983{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100984 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100985 mch_signal(SIGPWR, catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 /*
987 * I'm not sure we get the SIGPWR signal when the system is really going
988 * down or when the batteries are almost empty. Just preserve the swap
989 * files and don't exit, that can't do any harm.
990 */
991 ml_sync_all(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992}
993#endif
994
995#ifdef SET_SIG_ALARM
996/*
997 * signal function for alarm().
998 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100999 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000sig_alarm SIGDEFARG(sigarg)
1001{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001002 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 sig_alarm_called = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004}
1005#endif
1006
Bram Moolenaaredce7422019-01-20 18:39:30 +01001007#if (defined(HAVE_SETJMP_H) \
1008 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
1009 || defined(FEAT_LIBCALL))) \
1010 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001011# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +01001012
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001013// argument to SETJMP()
1014static JMP_BUF lc_jump_env;
1015
1016# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001017// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001018// Volatile because it is used in signal handlers.
1019static volatile sig_atomic_t lc_signal;
1020# endif
1021
1022// TRUE when lc_jump_env is valid.
1023// Volatile because it is used in signal handler deathtrap().
zeertzjq9b7d2a92022-08-26 10:33:54 +01001024static volatile sig_atomic_t lc_active = FALSE;
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026/*
1027 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001028 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001030 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 * Usage:
1032 * mch_startjmp();
1033 * if (SETJMP(lc_jump_env) != 0)
1034 * {
1035 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001036 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 * }
1038 * else
1039 * {
1040 * do_the_work;
1041 * mch_endjmp();
1042 * }
1043 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1044 * not return before the saved environment is used.
1045 * Returns OK for normal return, FAIL when the protected code caused a
1046 * problem and LONGJMP() was used.
1047 */
Bram Moolenaar113e1072019-01-20 15:30:40 +01001048 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001049mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001051# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001053# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 lc_active = TRUE;
1055}
1056
Bram Moolenaar113e1072019-01-20 15:30:40 +01001057 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001058mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059{
1060 lc_active = FALSE;
1061}
1062
Bram Moolenaar113e1072019-01-20 15:30:40 +01001063 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001064mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
1066# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001067 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1068 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 init_signal_stack();
1070# endif
1071}
1072#endif
1073
1074/*
1075 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001076 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001078 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1079 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001081 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082deathtrap SIGDEFARG(sigarg)
1083{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001084 static int entered = 0; // count the number of times we got here.
1085 // Note: when memory has been corrupted
1086 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087#ifdef SIGHASARG
1088 int i;
1089#endif
1090
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001091#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 /*
1093 * Catch a crash in protected code.
1094 * Restores the environment saved in lc_jump_env, which looks like
1095 * SETJMP() returns 1.
1096 */
1097 if (lc_active)
1098 {
1099# if defined(SIGHASARG)
1100 lc_signal = sigarg;
1101# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001102 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001104 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 }
1106#endif
1107
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001108#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001109# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001110 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1111 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1112 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001113 if (in_mch_delay && sigarg == SIGQUIT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001114 return;
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001115# endif
1116
Bram Moolenaar0f873732019-12-05 20:28:46 +01001117 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1118 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1119 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001120 if (entered == 0
1121 && (0
1122# ifdef SIGHUP
1123 || sigarg == SIGHUP
1124# endif
1125# ifdef SIGQUIT
1126 || sigarg == SIGQUIT
1127# endif
1128# ifdef SIGTERM
1129 || sigarg == SIGTERM
1130# endif
1131# ifdef SIGPWR
1132 || sigarg == SIGPWR
1133# endif
1134# ifdef SIGUSR1
1135 || sigarg == SIGUSR1
1136# endif
1137# ifdef SIGUSR2
1138 || sigarg == SIGUSR2
1139# endif
1140 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001141 && !vim_handle_signal(sigarg))
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001142 return;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001143#endif
1144
Bram Moolenaar0f873732019-12-05 20:28:46 +01001145 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 ++entered;
1147
Bram Moolenaar0f873732019-12-05 20:28:46 +01001148 // Executing autocommands is likely to use more stack space than we have
1149 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001150 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001151
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001153 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 set_vim_var_nr(VV_DYING, (long)entered);
1155#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001156 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001158#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001159 // Since we are now using the signal stack, need to reset the stack
1160 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 get_stack_limit();
1162#endif
1163
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001164#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001165 // This is for opening gdb the moment Vim crashes.
1166 // You need to manually adjust the file name and Vim executable name.
1167 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001168 {
1169# define VI_GDB_FILE "/tmp/vimgdb"
1170# define VIM_NAME "/usr/bin/vim"
1171 FILE *fp = fopen(VI_GDB_FILE, "w");
1172 if (fp)
1173 {
1174 fprintf(fp,
1175 "file %s\n"
1176 "attach %d\n"
1177 "set height 1000\n"
1178 "bt full\n"
1179 , VIM_NAME, getpid());
1180 fclose(fp);
1181 system("xterm -e gdb -x "VI_GDB_FILE);
1182 unlink(VI_GDB_FILE);
1183 }
1184 }
1185#endif
1186
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001188 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 for (i = 0; signal_info[i].sig != -1; i++)
1190 if (sigarg == signal_info[i].sig)
1191 break;
1192 deadly_signal = sigarg;
1193#endif
1194
Bram Moolenaar0f873732019-12-05 20:28:46 +01001195 full_screen = FALSE; // don't write message to the GUI, it might be
1196 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 /*
1198 * If something goes wrong after entering here, we may get here again.
1199 * When this happens, give a message and try to exit nicely (resetting the
1200 * terminal mode, etc.)
1201 * When this happens twice, just exit, don't even try to give a message,
1202 * stack may be corrupt or something weird.
1203 * When this still happens again (or memory was corrupted in such a way
1204 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1205 */
1206 if (entered >= 3)
1207 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001208 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 may_core_dump();
1210 if (entered >= 4)
1211 _exit(8);
1212 exit(7);
1213 }
1214 if (entered == 2)
1215 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001216 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001217 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 out_flush();
1219 getout(1);
1220 }
1221
Bram Moolenaar0f873732019-12-05 20:28:46 +01001222 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001224 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 signal_info[i].name);
1226#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001227 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001229
Bram Moolenaar0f873732019-12-05 20:28:46 +01001230 // Preserve files and exit. This sets the really_exiting flag to prevent
1231 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001232 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
Bram Moolenaar0f873732019-12-05 20:28:46 +01001234 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001235
Bram Moolenaar009b2592004-10-24 19:18:58 +00001236#ifdef NBDEBUG
1237 reset_signals();
1238 may_core_dump();
1239 abort();
1240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241}
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001244 * Invoked after receiving SIGCONT. We don't know what happened while
1245 * sleeping, deal with part of that.
1246 */
1247 static void
1248after_sigcont(void)
1249{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001250 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1251 // again later.
1252 oldtitle_outdated = TRUE;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001253
Bram Moolenaar2e310482018-08-21 13:09:10 +02001254 settmode(TMODE_RAW);
1255 need_check_timestamps = TRUE;
1256 did_check_timestamps = FALSE;
1257}
1258
1259#if defined(SIGCONT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001260static void sigcont_handler SIGPROTOARG;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001261
1262/*
1263 * With multi-threading, suspending might not work immediately. Catch the
1264 * SIGCONT signal, which will be used as an indication whether the suspending
1265 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001266 *
1267 * On Linux, signal is not always handled immediately either.
1268 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001269 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001270 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001271 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001273static volatile sig_atomic_t sigcont_received;
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001274static void sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
1276/*
1277 * signal handler for SIGCONT
1278 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001279 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280sigcont_handler SIGDEFARG(sigarg)
1281{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001282 if (in_mch_suspend)
1283 {
1284 sigcont_received = TRUE;
1285 }
1286 else
1287 {
1288 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1289 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1290 // back to a sane mode. We should redraw, but we can't really do that
1291 // in a signal handler, do a redraw later.
1292 after_sigcont();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001293 redraw_later(UPD_CLEAR);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001294 cursor_on_force();
1295 out_flush();
1296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297}
1298#endif
1299
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001300#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001301# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001302static void *clip_star_save = NULL;
1303static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001304# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001305
1306/*
1307 * Called when Vim is going to sleep or execute a shell command.
1308 * We can't respond to requests for the X selections. Lose them, otherwise
1309 * other applications will hang. But first copy the text to cut buffer 0.
1310 */
1311 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001312loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001313{
1314 if (clip_star.owned || clip_plus.owned)
1315 {
1316 x11_export_final_selection();
1317 if (clip_star.owned)
1318 clip_lose_selection(&clip_star);
1319 if (clip_plus.owned)
1320 clip_lose_selection(&clip_plus);
1321 if (x11_display != NULL)
1322 XFlush(x11_display);
1323 }
1324}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001325
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001326# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001327/*
1328 * Save clipboard text to restore later.
1329 */
1330 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001331save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001332{
1333 if (clip_star.owned)
1334 clip_star_save = get_register('*', TRUE);
1335 if (clip_plus.owned)
1336 clip_plus_save = get_register('+', TRUE);
1337}
1338
1339/*
1340 * Restore clipboard text if no one own the X selection.
1341 */
1342 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001343restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001344{
1345 if (clip_star_save != NULL)
1346 {
1347 if (!clip_gen_owner_exists(&clip_star))
1348 put_register('*', clip_star_save);
1349 else
1350 free_register(clip_star_save);
1351 clip_star_save = NULL;
1352 }
1353 if (clip_plus_save != NULL)
1354 {
1355 if (!clip_gen_owner_exists(&clip_plus))
1356 put_register('+', clip_plus_save);
1357 else
1358 free_register(clip_plus_save);
1359 clip_plus_save = NULL;
1360 }
1361}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001362# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001363#endif
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365/*
1366 * If the machine has job control, use it to suspend the program,
1367 * otherwise fake it by starting a new shell.
1368 */
1369 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001370mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001372 if (ignore_sigtstp)
1373 return;
1374
Bram Moolenaar041c7102020-05-30 18:14:57 +02001375#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001376 in_mch_suspend = TRUE;
1377
Bram Moolenaar0f873732019-12-05 20:28:46 +01001378 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001380 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
1382# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001383 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001385# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 sigcont_received = FALSE;
1387# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001388
Bram Moolenaar0f873732019-12-05 20:28:46 +01001389 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001390
1391# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001392 /*
1393 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001394 * immediately, but somehow not all the time, probably because it's handled
1395 * in another thread. Do not call pause() because there would be race
1396 * condition which would hang Vim if signal happened in between the test of
1397 * sigcont_received and the call to pause(). If signal is not yet received,
1398 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1399 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001400 */
1401 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001402 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001403
Bram Moolenaar262735e2009-07-14 10:20:22 +00001404 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001405 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001408 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
Bram Moolenaar2e310482018-08-21 13:09:10 +02001410 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411#else
1412 suspend_shell();
1413#endif
1414}
1415
1416 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001417mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
1419 Columns = 80;
1420 Rows = 24;
1421
1422 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001423
1424#ifdef SIGTSTP
1425 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1426 // that indicates the shell (or program) that launched us does not support
1427 // tty job control and thus we should ignore that signal. If invoked as a
1428 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
ichizok378447f2023-05-11 22:25:42 +01001429 ignore_sigtstp = restricted || SIG_IGN == mch_signal(SIGTSTP, SIG_ERR);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001432
Bram Moolenaar56718732006-03-15 22:53:57 +00001433#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001434 mac_conv_init();
1435#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001436#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1437 win_clip_init();
1438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439}
1440
1441 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001442set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443{
1444#if defined(SIGWINCH)
1445 /*
1446 * WINDOW CHANGE signal is handled with sig_winch().
1447 */
ichizok378447f2023-05-11 22:25:42 +01001448 mch_signal(SIGWINCH, sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449#endif
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001452 // See mch_init() for the conditions under which we ignore SIGTSTP.
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001453 // In the GUI default TSTP processing is OK.
1454 // Checking both gui.in_use and gui.starting because gui.in_use is not set
1455 // at this point (set after menus are displayed), but gui.starting is set.
ichizok378447f2023-05-11 22:25:42 +01001456 mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001457# ifdef FEAT_GUI
1458 : gui.in_use || gui.starting ? SIG_DFL
1459# endif
ichizok378447f2023-05-11 22:25:42 +01001460 : sig_tstp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001462#if defined(SIGCONT)
ichizok378447f2023-05-11 22:25:42 +01001463 mch_signal(SIGCONT, sigcont_handler);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001465#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 /*
1467 * We want to ignore breaking of PIPEs.
1468 */
ichizok378447f2023-05-11 22:25:42 +01001469 mch_signal(SIGPIPE, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470#endif
1471
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001473 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474#endif
1475
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001476#ifdef SIGUSR1
1477 /*
1478 * Call user's handler on SIGUSR1
1479 */
ichizok378447f2023-05-11 22:25:42 +01001480 mch_signal(SIGUSR1, catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001481#endif
1482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 /*
1484 * Ignore alarm signals (Perl's alarm() generates it).
1485 */
1486#ifdef SIGALRM
ichizok378447f2023-05-11 22:25:42 +01001487 mch_signal(SIGALRM, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488#endif
1489
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001490#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 /*
1492 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1493 * work will be lost.
1494 */
ichizok378447f2023-05-11 22:25:42 +01001495 mch_signal(SIGPWR, catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496#endif
1497
1498 /*
1499 * Arrange for other signals to gracefully shutdown Vim.
1500 */
1501 catch_signals(deathtrap, SIG_ERR);
1502
1503#if defined(FEAT_GUI) && defined(SIGHUP)
1504 /*
1505 * When the GUI is running, ignore the hangup signal.
1506 */
1507 if (gui.in_use)
ichizok378447f2023-05-11 22:25:42 +01001508 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509#endif
1510}
1511
Bram Moolenaardf177f62005-02-22 08:39:57 +00001512#if defined(SIGINT) || defined(PROTO)
1513/*
1514 * Catch CTRL-C (only works while in Cooked mode).
1515 */
1516 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001517catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001518{
ichizok378447f2023-05-11 22:25:42 +01001519 mch_signal(SIGINT, catch_sigint);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001520}
1521#endif
1522
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001524reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001527#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001528 // SIGCONT isn't in the list, because its default action is ignore
ichizok378447f2023-05-11 22:25:42 +01001529 mch_signal(SIGCONT, SIG_DFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530#endif
1531}
1532
1533 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001534catch_signals(
Michael Jarvisbe9624e2023-04-19 20:28:48 +01001535 void (*func_deadly)(int),
1536 void (*func_other)(int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
1538 int i;
1539
1540 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001541 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (signal_info[i].deadly)
1543 {
1544#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1545 struct sigaction sa;
1546
Bram Moolenaar0f873732019-12-05 20:28:46 +01001547 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 sa.sa_handler = func_deadly;
1549 sigemptyset(&sa.sa_mask);
1550# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001551 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1552 // thread handling in combination with using the alternate stack:
1553 // pthread library functions try to use the stack pointer to
1554 // identify the current thread, causing a SEGV signal, which
1555 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 sa.sa_flags = 0;
1557# else
1558 sa.sa_flags = SA_ONSTACK;
1559# endif
1560 sigaction(signal_info[i].sig, &sa, NULL);
1561#else
1562# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1563 struct sigvec sv;
1564
Bram Moolenaar0f873732019-12-05 20:28:46 +01001565 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 sv.sv_handler = func_deadly;
1567 sv.sv_mask = 0;
1568 sv.sv_flags = SV_ONSTACK;
1569 sigvec(signal_info[i].sig, &sv, NULL);
1570# else
ichizok378447f2023-05-11 22:25:42 +01001571 mch_signal(signal_info[i].sig, func_deadly);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572# endif
1573#endif
1574 }
1575 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001576 {
1577 // Deal with non-deadly signals.
1578#ifdef SIGTSTP
ichizok378447f2023-05-11 22:25:42 +01001579 mch_signal(signal_info[i].sig,
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001580 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1581 ? SIG_IGN : func_other);
1582#else
ichizok378447f2023-05-11 22:25:42 +01001583 mch_signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001584#endif
1585 }
1586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587}
1588
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001589#ifdef HAVE_SIGPROCMASK
1590 static void
1591block_signals(sigset_t *set)
1592{
1593 sigset_t newset;
1594 int i;
1595
1596 sigemptyset(&newset);
1597
1598 for (i = 0; signal_info[i].sig != -1; i++)
1599 sigaddset(&newset, signal_info[i].sig);
1600
Bram Moolenaar2e310482018-08-21 13:09:10 +02001601# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001602 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001603 sigaddset(&newset, SIGCONT);
1604# endif
1605
1606 sigprocmask(SIG_BLOCK, &newset, set);
1607}
1608
1609 static void
1610unblock_signals(sigset_t *set)
1611{
1612 sigprocmask(SIG_SETMASK, set, NULL);
1613}
1614#endif
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001617 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001618 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1619 * return TRUE
1620 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1621 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001622 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001623 * Returns TRUE when Vim should exit.
1624 */
1625 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001626vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001627{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001628 static int got_signal = 0;
1629 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001630
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001631 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001632 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001633 case SIGNAL_BLOCK: blocked = TRUE;
1634 break;
1635
1636 case SIGNAL_UNBLOCK: blocked = FALSE;
1637 if (got_signal != 0)
1638 {
1639 kill(getpid(), got_signal);
1640 got_signal = 0;
1641 }
1642 break;
1643
1644 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001645 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001646 got_signal = sig;
1647#ifdef SIGPWR
1648 if (sig != SIGPWR)
1649#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001650 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001651 break;
1652 }
1653 return FALSE;
1654}
1655
1656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 * Check_win checks whether we have an interactive stdout.
1658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001660mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (isatty(1))
1663 return OK;
1664 return FAIL;
1665}
1666
1667/*
1668 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1669 */
1670 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001671mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
1673 if (isatty(read_cmd_fd))
1674 return TRUE;
1675 return FALSE;
1676}
1677
1678#ifdef FEAT_X11
1679
Bram Moolenaar651fca82021-11-29 20:39:38 +00001680# if defined(ELAPSED_TIMEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682/*
1683 * Give a message about the elapsed time for opening the X window.
1684 */
1685 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001686xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001688 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689}
1690# endif
1691#endif
1692
Bram Moolenaar651fca82021-11-29 20:39:38 +00001693#if defined(FEAT_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694/*
1695 * A few functions shared by X11 title and clipboard code.
1696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
1698static int got_x_error = FALSE;
1699
1700/*
1701 * X Error handler, otherwise X just exits! (very rude) -- webb
1702 */
1703 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001704x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001706 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 STRCAT(IObuff, _("\nVim: Got X error\n"));
1708
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001709 // In the GUI we cannot print a message and continue, because no X calls
1710 // are allowed here (causes my system to hang). Silently continuing seems
1711 // like the best alternative. Do preserve files, in case we crash.
1712 ml_sync_all(FALSE, FALSE);
1713
1714#ifdef FEAT_GUI
1715 if (!gui.in_use)
1716#endif
1717 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
Bram Moolenaar0f873732019-12-05 20:28:46 +01001719 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720}
1721
1722/*
1723 * Another X Error handler, just used to check for errors.
1724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001726x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
1728 got_x_error = TRUE;
1729 return 0;
1730}
1731
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001732/*
1733 * Return TRUE when connection to the X server is desired.
1734 */
1735 static int
1736x_connect_to_server(void)
1737{
1738 // No point in connecting if we are exiting or dying.
1739 if (exiting || v_dying)
1740 return FALSE;
1741
1742#if defined(FEAT_CLIENTSERVER)
1743 if (x_force_connect)
1744 return TRUE;
1745#endif
1746 if (x_no_connect)
1747 return FALSE;
1748
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001749 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001750 if (clip_exclude_prog != NULL)
1751 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001752 // Just in case we get called recursively, return FALSE. This could
1753 // happen if vpeekc() is used while executing the prog and it causes a
1754 // related callback to be invoked.
1755 if (regprog_in_use(clip_exclude_prog))
1756 return FALSE;
1757
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001758 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1759 return FALSE;
1760 }
1761 return TRUE;
1762}
1763
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001765# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766/*
1767 * An X IO Error handler, used to catch error while opening the display.
1768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001770x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001772 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001774# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001775 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777}
1778# endif
1779
1780/*
1781 * An X IO Error handler, used to catch terminal errors.
1782 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001783static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001786x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001789 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 x11_window = 0;
1791 x11_display = NULL;
1792 xterm_Shell = (Widget)0;
1793
Bram Moolenaar0f873732019-12-05 20:28:46 +01001794 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001796# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001797 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001798# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001800
1801/*
1802 * If the X11 connection was lost try to restore it.
1803 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001804 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001805 */
1806 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001807may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001808{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001809 // No point in restoring the connecting if we are exiting or dying.
1810 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001811 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001812 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001813
1814# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001815 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001816 if (app_context != (XtAppContext)NULL)
1817 {
1818 XtDestroyApplicationContext(app_context);
1819 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001820 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001821 }
1822# endif
1823
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001824 setup_term_clip();
1825 get_x11_title(FALSE);
1826 }
1827}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001828
1829 void
1830ex_xrestore(exarg_T *eap)
1831{
1832 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1833 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001834 if (xterm_display_allocated)
1835 vim_free(xterm_display);
1836 xterm_display = (char *)vim_strsave(eap->arg);
1837 xterm_display_allocated = TRUE;
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001838 }
1839 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001840 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001841
1842 clear_xterm_clip();
1843 x11_window = 0;
1844 xterm_dpy_retry_count = 5; // Try reconnecting five times
1845 may_restore_clipboard();
1846}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847#endif
1848
1849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 * Test if "dpy" and x11_window are valid by getting the window title.
1851 * I don't actually want it yet, so there may be a simpler call to use, but
1852 * this will cause the error handler x_error_check() to be called if anything
1853 * is wrong, such as the window pointer being invalid (as can happen when the
1854 * user changes his DISPLAY, but not his WINDOWID) -- webb
1855 */
1856 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001857test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858{
1859 int (*old_handler)();
1860 XTextProperty text_prop;
1861
1862 old_handler = XSetErrorHandler(x_error_check);
1863 got_x_error = FALSE;
1864 if (XGetWMName(dpy, x11_window, &text_prop))
1865 XFree((void *)text_prop.value);
1866 XSync(dpy, False);
1867 (void)XSetErrorHandler(old_handler);
1868
1869 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001870 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
1872 return (got_x_error ? FAIL : OK);
1873}
1874#endif
1875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
1877#ifdef FEAT_X11
1878
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001879static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880
1881/*
1882 * try to get x11 window and display
1883 *
1884 * return FAIL for failure, OK otherwise
1885 */
1886 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001887get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888{
1889 char *winid;
1890 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001891#define XD_NONE 0 // x11_display not set here
1892#define XD_HERE 1 // x11_display opened here
1893#define XD_GUI 2 // x11_display used from gui.dpy
1894#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 static int x11_display_from = XD_NONE;
1896 static int did_set_error_handler = FALSE;
1897
1898 if (!did_set_error_handler)
1899 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001900 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 (void)XSetErrorHandler(x_error_handler);
1902 did_set_error_handler = TRUE;
1903 }
1904
Bram Moolenaar9372a112005-12-06 19:59:18 +00001905#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 if (gui.in_use)
1907 {
1908 /*
1909 * If the X11 display was opened here before, for the window where Vim
1910 * was started, close that one now to avoid a memory leak.
1911 */
1912 if (x11_display_from == XD_HERE && x11_display != NULL)
1913 {
1914 XCloseDisplay(x11_display);
1915 x11_display_from = XD_NONE;
1916 }
1917 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1918 {
1919 x11_display_from = XD_GUI;
1920 return OK;
1921 }
1922 x11_display = NULL;
1923 return FAIL;
1924 }
1925 else if (x11_display_from == XD_GUI)
1926 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001927 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 x11_window = 0;
1929 x11_display = NULL;
1930 x11_display_from = XD_NONE;
1931 }
1932#endif
1933
Bram Moolenaar0f873732019-12-05 20:28:46 +01001934 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 if (!x_connect_to_server())
1936 return FAIL;
1937
1938 /*
1939 * If WINDOWID not set, should try another method to find out
1940 * what the current window number is. The only code I know for
1941 * this is very complicated.
1942 * We assume that zero is invalid for WINDOWID.
1943 */
1944 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1945 x11_window = (Window)atol(winid);
1946
1947#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001948 if (xterm_dpy == x11_display)
1949 // x11_display may have been set to xterm_dpy elsewhere
1950 x11_display_from = XD_XTERM;
1951
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (xterm_dpy != NULL && x11_window != 0)
1953 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001954 // We may have checked it already, but Gnome terminal can move us to
1955 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001956 if (x11_display_from != XD_XTERM)
1957 {
1958 /*
1959 * If the X11 display was opened here before, for the window where
1960 * Vim was started, close that one now to avoid a memory leak.
1961 */
1962 if (x11_display_from == XD_HERE && x11_display != NULL)
1963 XCloseDisplay(x11_display);
1964 x11_display = xterm_dpy;
1965 x11_display_from = XD_XTERM;
1966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 if (test_x11_window(x11_display) == FAIL)
1968 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001969 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 x11_window = 0;
1971 x11_display = NULL;
1972 x11_display_from = XD_NONE;
1973 return FAIL;
1974 }
1975 return OK;
1976 }
1977#endif
1978
1979 if (x11_window == 0 || x11_display == NULL)
1980 result = -1;
1981
Bram Moolenaar0f873732019-12-05 20:28:46 +01001982 if (result != -1) // Have already been here and set this
1983 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
1985 if (x11_window != 0 && x11_display == NULL)
1986 {
1987#ifdef SET_SIG_ALARM
ichizok378447f2023-05-11 22:25:42 +01001988 sighandler_T sig_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001990#ifdef ELAPSED_FUNC
1991 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992
1993 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001994 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995#endif
1996
1997#ifdef SET_SIG_ALARM
1998 /*
1999 * Opening the Display may hang if the DISPLAY setting is wrong, or
2000 * the network connection is bad. Set an alarm timer to get out.
2001 */
2002 sig_alarm_called = FALSE;
ichizok378447f2023-05-11 22:25:42 +01002003 sig_save = mch_signal(SIGALRM, sig_alarm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 alarm(2);
2005#endif
2006 x11_display = XOpenDisplay(NULL);
2007
2008#ifdef SET_SIG_ALARM
2009 alarm(0);
ichizok378447f2023-05-11 22:25:42 +01002010 mch_signal(SIGALRM, sig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01002012 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013#endif
2014 if (x11_display != NULL)
2015 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002016# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00002018 {
2019 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002020 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00002021 verbose_leave();
2022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023# endif
2024 if (test_x11_window(x11_display) == FAIL)
2025 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002026 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 x11_window = 0;
2028 XCloseDisplay(x11_display);
2029 x11_display = NULL;
2030 }
2031 else
2032 x11_display_from = XD_HERE;
2033 }
2034 }
2035 if (x11_window == 0 || x11_display == NULL)
2036 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02002037
2038# ifdef FEAT_EVAL
2039 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
2040# endif
2041
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 return (result = OK);
2043}
2044
2045/*
2046 * Determine original x11 Window Title
2047 */
2048 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002049get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050{
Bram Moolenaar47136d72004-10-12 20:02:24 +00002051 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052}
2053
2054/*
2055 * Determine original x11 Window icon
2056 */
2057 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002058get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059{
2060 int retval = FALSE;
2061
2062 retval = get_x11_thing(FALSE, test_only);
2063
Bram Moolenaar0f873732019-12-05 20:28:46 +01002064 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 if (oldicon == NULL && !test_only)
2066 {
2067 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002068 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002070 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 }
2072
2073 return retval;
2074}
2075
2076 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002077get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002078 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01002079 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
2081 XTextProperty text_prop;
2082 int retval = FALSE;
2083 Status status;
2084
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002085 if (get_x11_windis() != OK)
2086 return FALSE;
2087
2088 // Get window/icon name if any
2089 if (get_title)
2090 status = XGetWMName(x11_display, x11_window, &text_prop);
2091 else
2092 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2093
2094 /*
2095 * If terminal is xterm, then x11_window may be a child window of the
2096 * outer xterm window that actually contains the window/icon name, so
2097 * keep traversing up the tree until a window with a title/icon is
2098 * found.
2099 */
2100 // Previously this was only done for xterm and alike. I don't see a
2101 // reason why it would fail for other terminal emulators.
2102 // if (term_is_xterm)
2103 Window root;
2104 Window parent;
2105 Window win = x11_window;
2106 Window *children;
2107 unsigned int num_children;
2108
2109 while (!status || text_prop.value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002111 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2112 &num_children))
2113 break;
2114 if (children)
2115 XFree((void *)children);
2116 if (parent == root || parent == 0)
2117 break;
2118
2119 win = parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002121 status = XGetWMName(x11_display, win, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002123 status = XGetWMIconName(x11_display, win, &text_prop);
2124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002126 if (status && text_prop.value != NULL)
2127 {
2128 retval = TRUE;
2129 if (!test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002131 if (get_title)
2132 vim_free(oldtitle);
2133 else
2134 vim_free(oldicon);
2135 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002138 oldtitle = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002140 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002142 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002144 char **cl;
2145 Status transform_status;
2146 int n = 0;
2147
2148 transform_status = XmbTextPropertyToTextList(x11_display,
2149 &text_prop,
2150 &cl, &n);
2151 if (transform_status >= Success && n > 0 && cl[0])
2152 {
2153 if (get_title)
2154 oldtitle = vim_strsave((char_u *) cl[0]);
2155 else
2156 oldicon = vim_strsave((char_u *) cl[0]);
2157 XFreeStringList(cl);
2158 }
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002159 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 if (get_title)
2162 oldtitle = vim_strsave((char_u *)text_prop.value);
2163 else
2164 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002168 XFree((void *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
2170 return retval;
2171}
2172
Bram Moolenaar0f873732019-12-05 20:28:46 +01002173// Xutf8 functions are not available on older systems. Note that on some
2174// systems X_HAVE_UTF8_STRING may be defined in a header file but
2175// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2176// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002177#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002178# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179# define USE_UTF8_STRING
2180# endif
2181#endif
2182
2183/*
2184 * Set x11 Window Title
2185 *
2186 * get_x11_windis() must be called before this and have returned OK
2187 */
2188 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002189set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002191 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2192 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2193 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194#ifdef USE_UTF8_STRING
2195 if (enc_utf8)
2196 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2197 NULL, NULL, 0, NULL, NULL, NULL);
2198 else
2199#endif
2200 {
2201#if XtSpecificationRelease >= 4
2202# ifdef FEAT_XFONTSET
2203 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2204 NULL, NULL, 0, NULL, NULL, NULL);
2205# else
2206 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002207 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208
Bram Moolenaar0f873732019-12-05 20:28:46 +01002209 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002210 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 XSetWMProperties(x11_display, x11_window, &text_prop,
2212 NULL, NULL, 0, NULL, NULL, NULL);
2213# endif
2214#else
2215 XStoreName(x11_display, x11_window, (char *)title);
2216#endif
2217 }
2218 XFlush(x11_display);
2219}
2220
2221/*
2222 * Set x11 Window icon
2223 *
2224 * get_x11_windis() must be called before this and have returned OK
2225 */
2226 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002227set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002229 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230#ifdef USE_UTF8_STRING
2231 if (enc_utf8)
2232 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2233 NULL, 0, NULL, NULL, NULL);
2234 else
2235#endif
2236 {
2237#if XtSpecificationRelease >= 4
2238# ifdef FEAT_XFONTSET
2239 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2240 NULL, 0, NULL, NULL, NULL);
2241# else
2242 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002243 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002245 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2247 NULL, 0, NULL, NULL, NULL);
2248# endif
2249#else
2250 XSetIconName(x11_display, x11_window, (char *)icon);
2251#endif
2252 }
2253 XFlush(x11_display);
2254}
2255
Bram Moolenaar0f873732019-12-05 20:28:46 +01002256#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002259get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260{
2261 return FALSE;
2262}
2263
2264 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002265get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 if (!test_only)
2268 {
2269 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002270 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002272 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 }
2274 return FALSE;
2275}
2276
Bram Moolenaar0f873732019-12-05 20:28:46 +01002277#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
2279 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002280mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281{
2282 return get_x11_title(TRUE);
2283}
2284
2285 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002286mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288 return get_x11_icon(TRUE);
2289}
2290
2291/*
2292 * Set the window title and icon.
2293 */
2294 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002295mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296{
2297 int type = 0;
2298 static int recursive = 0;
2299
Bram Moolenaar0f873732019-12-05 20:28:46 +01002300 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002302 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 return;
2304
Bram Moolenaar0f873732019-12-05 20:28:46 +01002305 // When one of the X11 functions causes a deadly signal, we get here again
2306 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 if (recursive)
2308 return;
2309 ++recursive;
2310
2311 /*
2312 * if the window ID and the display is known, we may use X11 calls
2313 */
2314#ifdef FEAT_X11
2315 if (get_x11_windis() == OK)
2316 type = 1;
2317#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002318# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002319 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (gui.in_use)
2321 type = 1;
2322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323#endif
2324
2325 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002326 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 * than x11 calls, because the x11 calls don't always work
2328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 if ((type || *T_TS != NUL) && title != NULL)
2330 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002331 if (oldtitle_outdated)
2332 {
2333 oldtitle_outdated = FALSE;
2334 VIM_CLEAR(oldtitle);
2335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 if (oldtitle == NULL
2337#ifdef FEAT_GUI
2338 && !gui.in_use
2339#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002340 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 (void)get_x11_title(FALSE);
2342
Bram Moolenaar0f873732019-12-05 20:28:46 +01002343 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 term_settitle(title);
2345#ifdef FEAT_X11
2346 else
2347# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002348 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002350 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002352#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002353 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 else
2355 gui_mch_settitle(title, icon);
2356#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002357 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
2359
2360 if ((type || *T_CIS != NUL) && icon != NULL)
2361 {
2362 if (oldicon == NULL
2363#ifdef FEAT_GUI
2364 && !gui.in_use
2365#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002366 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 get_x11_icon(FALSE);
2368
2369 if (*T_CIS != NUL)
2370 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002371 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002373 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 out_flush();
2375 }
2376#ifdef FEAT_X11
2377 else
2378# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002379 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002381 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382#endif
2383 did_set_icon = TRUE;
2384 }
2385 --recursive;
2386}
2387
2388/*
2389 * Restore the window/icon title.
2390 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002391 * SAVE_RESTORE_TITLE only restore title
2392 * SAVE_RESTORE_ICON only restore icon
2393 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 */
2395 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002396mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
Bram Moolenaardac13472019-09-16 21:06:21 +02002398 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002399
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002400 // Only restore the title or icon when it has been set.
2401 // When using "oldtitle" make a copy, it might be freed halfway.
2402 char_u *title = ((which & SAVE_RESTORE_TITLE) && unix_did_set_title)
2403 ? (oldtitle ? oldtitle : p_titleold) : NULL;
2404 char_u *tofree = NULL;
2405 if (title == oldtitle && oldtitle != NULL)
2406 {
2407 tofree = vim_strsave(title);
2408 if (tofree != NULL)
2409 title = tofree;
2410 }
2411 mch_settitle(title,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002412 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002413 vim_free(tofree);
Bram Moolenaar40385db2018-08-07 22:31:44 +02002414
Bram Moolenaare5c83282019-05-03 23:15:37 +02002415 if (do_push_pop)
2416 {
2417 // pop and push from/to the stack
2418 term_pop_title(which);
2419 term_push_title(which);
2420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421}
2422
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
2424/*
2425 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002426 * This matches "xterm.*", thus "xterm-256color", "xterm-kitty", etc.
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002427 * Do not consider "xterm-kitty" an xterm, it is not fully xterm compatible,
2428 * using the "xterm-kitty" terminfo entry should work better.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002429 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 */
2431 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002432vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
2434 if (name == NULL)
2435 return FALSE;
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002436 return ((STRNICMP(name, "xterm", 5) == 0
2437 && STRNICMP(name, "xterm-kitty", 11) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 || STRNICMP(name, "nxterm", 6) == 0
2439 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002440 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002442 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 || STRCMP(name, "builtin_xterm") == 0);
2444}
2445
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002446#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2447/*
2448 * Return TRUE if "name" appears to be that of a terminal
2449 * known to support the xterm-style mouse protocol.
2450 * Relies on term_is_xterm having been set to its correct value.
2451 */
2452 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002453use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002454{
2455 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002456 && (term_is_xterm
2457 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002458 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002459 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002460 || STRICMP(name, "st") == 0
2461 || STRNICMP(name, "st-", 3) == 0
2462 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002463}
2464#endif
2465
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466/*
2467 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2468 * Return 1 for "xterm".
2469 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002470 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002471 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 */
2473 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002474use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002476 if (ttym_flags == TTYM_SGR)
2477 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002478 if (ttym_flags == TTYM_URXVT)
2479 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 if (ttym_flags == TTYM_XTERM2)
2481 return 2;
2482 if (ttym_flags == TTYM_XTERM)
2483 return 1;
2484 return 0;
2485}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002487/*
2488 * Return TRUE if "name" is an iris-ansi terminal name.
2489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002491vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
2493 if (name == NULL)
2494 return FALSE;
2495 return (STRNICMP(name, "iris-ansi", 9) == 0
2496 || STRCMP(name, "builtin_iris-ansi") == 0);
2497}
2498
Dominique Pellee764d1b2023-03-12 21:20:59 +00002499#if defined(VMS) || defined(PROTO)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002500/*
2501 * Return TRUE if "name" is a vt300-like terminal name.
2502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002504vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505{
2506 if (name == NULL)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002507 return FALSE;
2508 // Actually all ANSI compatible terminals should be here.
2509 // Catch at least VT1xx - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002510 return ((STRNICMP(name, "vt", 2) == 0
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002511 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 || STRCMP(name, "builtin_vt320") == 0);
2513}
Dominique Pellee764d1b2023-03-12 21:20:59 +00002514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
2516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 * Insert user name in s[len].
2518 * Return OK if a name found.
2519 */
2520 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002521mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522{
2523#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002524 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 return OK;
2526#else
2527 return mch_get_uname(getuid(), s, len);
2528#endif
2529}
2530
2531/*
2532 * Insert user name for "uid" in s[len].
2533 * Return OK if a name found.
2534 */
2535 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002536mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
2538#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2539 struct passwd *pw;
2540
2541 if ((pw = getpwuid(uid)) != NULL
2542 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2543 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002544 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 return OK;
2546 }
2547#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002548 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2549 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550}
2551
2552/*
2553 * Insert host name is s[len].
2554 */
2555
2556#ifdef HAVE_SYS_UTSNAME_H
2557 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002558mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559{
2560 struct utsname vutsname;
2561
2562 if (uname(&vutsname) < 0)
2563 *s = NUL;
2564 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002565 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002567#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
2569# ifdef HAVE_SYS_SYSTEMINFO_H
2570# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2571# endif
2572
2573 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002574mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576# ifdef VAXC
2577 vaxc$gethostname((char *)s, len);
2578# else
2579 gethostname((char *)s, len);
2580# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002581 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002583#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585/*
2586 * return process ID
2587 */
2588 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002589mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
2591 return (long)getpid();
2592}
2593
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002594/*
2595 * return TRUE if process "pid" is still running
2596 */
2597 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002598mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002599{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002600 // If there is no error the process must be running.
2601 if (kill(pid, 0) == 0)
2602 return TRUE;
2603#ifdef ESRCH
2604 // If the error is ESRCH then the process is not running.
2605 if (errno == ESRCH)
2606 return FALSE;
2607#endif
2608 // If the process is running and owned by another user we get EPERM. With
2609 // other errors the process might be running, assuming it is then.
2610 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002611}
2612
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002615strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616{
2617 extern int sys_nerr;
2618 extern char *sys_errlist[];
2619 static char er[20];
2620
2621 if (err > 0 && err < sys_nerr)
2622 return (sys_errlist[err]);
2623 sprintf(er, "Error %d", err);
2624 return er;
2625}
2626#endif
2627
2628/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002629 * Get name of current directory into buffer "buf" of length "len" bytes.
2630 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 * Return OK for success, FAIL for failure.
2632 */
2633 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002634mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635{
2636#if defined(USE_GETCWD)
2637 if (getcwd((char *)buf, len) == NULL)
2638 {
2639 STRCPY(buf, strerror(errno));
2640 return FAIL;
2641 }
2642 return OK;
2643#else
2644 return (getwd((char *)buf) != NULL ? OK : FAIL);
2645#endif
2646}
2647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002649 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 *
2651 * return FAIL for failure, OK for success
2652 */
2653 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002654mch_FullName(
2655 char_u *fname,
2656 char_u *buf,
2657 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002658 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
2660 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002661#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002663 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 char_u olddir[MAXPATHL];
2666 char_u *p;
2667 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002668#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002669 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2670 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002671#endif
2672
Bram Moolenaar38323e42007-03-06 19:22:53 +00002673#ifdef VMS
2674 fname = vms_fixfilename(fname);
2675#endif
2676
Bram Moolenaara2442432007-04-26 14:26:37 +00002677#ifdef __CYGWIN__
2678 /*
2679 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2680 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002681# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002682 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2683 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002684 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2685 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002686# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002687 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002688# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002689 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002690#endif
2691
Bram Moolenaar0f873732019-12-05 20:28:46 +01002692 // Expand it if forced or not an absolute path.
2693 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002694 if ((force || !mch_isFullName(fname))
2695 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
2697 /*
2698 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002699 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 * This will get the correct path name with "../" things.
2701 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002702 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002704 if (STRCMP(p, "/..") == 0)
2705 // for "/path/dir/.." include the "/.."
2706 p += 3;
2707
Bram Moolenaar38323e42007-03-06 19:22:53 +00002708#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 /*
2710 * Use fchdir() if possible, it's said to be faster and more
2711 * reliable. But on SunOS 4 it might not work. Check this by
2712 * doing a fchdir() right now.
2713 */
2714 if (!dont_fchdir)
2715 {
2716 fd = open(".", O_RDONLY | O_EXTRA, 0);
2717 if (fd >= 0 && fchdir(fd) < 0)
2718 {
2719 close(fd);
2720 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002721 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
2723 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
Bram Moolenaar0f873732019-12-05 20:28:46 +01002726 // Only change directory when we are sure we can return to where
2727 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002729#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 (mch_dirname(olddir, MAXPATHL) == FAIL
2733 || mch_chdir((char *)olddir) != 0))
2734 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002735 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 retval = FAIL;
2737 }
2738 else
2739 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002740 // The directory is copied into buf[], to be able to remove
2741 // the file name without changing it (could be a string in
2742 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 if (p - fname >= len)
2744 retval = FAIL;
2745 else
2746 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002747 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002749 {
2750 // Path does not exist (yet). For a full path fail,
2751 // will use the path as-is. For a relative path use
2752 // the current directory and append the file name.
2753 if (mch_isFullName(fname))
2754 retval = FAIL;
2755 else
2756 p = NULL;
2757 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002758 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002760 else
2761 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 *buf = NUL;
2763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 }
2765 }
2766 if (mch_dirname(buf, len) == FAIL)
2767 {
2768 retval = FAIL;
2769 *buf = NUL;
2770 }
2771 if (p != NULL)
2772 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002773#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (fd >= 0)
2775 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002776 if (p_verbose >= 5)
2777 {
2778 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002779 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002780 verbose_leave();
2781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 }
2784 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 l = mch_chdir((char *)olddir);
2787 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002788 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
itchyny051a40c2021-10-20 10:00:05 +01002790#ifdef HAVE_FCHDIR
2791 if (fd >= 0)
2792 close(fd);
2793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
2795 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002796 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002797 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002798#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002799 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002801 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002804
Bram Moolenaar0f873732019-12-05 20:28:46 +01002805 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002806 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 return FAIL;
2808
Bram Moolenaar0f873732019-12-05 20:28:46 +01002809 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 if (STRCMP(fname, ".") != 0)
2811 STRCAT(buf, fname);
2812
2813 return OK;
2814}
2815
2816/*
2817 * Return TRUE if "fname" does not depend on the current directory.
2818 */
2819 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002820mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002822#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 return ( fname[0] == '/' || fname[0] == '.' ||
2824 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2825 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2826 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002827#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829#endif
2830}
2831
Bram Moolenaar24552be2005-12-10 20:17:30 +00002832#if defined(USE_FNAME_CASE) || defined(PROTO)
2833/*
2834 * Set the case of the file name, if it already exists. This will cause the
2835 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002836 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002837 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002838 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002839fname_case(
2840 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002841 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002842{
2843 struct stat st;
2844 char_u *slash, *tail;
2845 DIR *dirp;
2846 struct dirent *dp;
2847
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002848 if (mch_lstat((char *)name, &st) < 0)
2849 return;
2850
2851 // Open the directory where the file is located.
2852 slash = vim_strrchr(name, '/');
2853 if (slash == NULL)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002854 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002855 dirp = opendir(".");
2856 tail = name;
2857 }
2858 else
2859 {
2860 *slash = NUL;
2861 dirp = opendir((char *)name);
2862 *slash = '/';
2863 tail = slash + 1;
2864 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002865
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002866 if (dirp == NULL)
2867 return;
2868
2869 while ((dp = readdir(dirp)) != NULL)
2870 {
2871 // Only accept names that differ in case and are the same byte
2872 // length. TODO: accept different length name.
2873 if (STRICMP(tail, dp->d_name) == 0
2874 && STRLEN(tail) == STRLEN(dp->d_name))
Bram Moolenaar24552be2005-12-10 20:17:30 +00002875 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002876 char_u newname[MAXPATHL + 1];
2877 struct stat st2;
2878
2879 // Verify the inode is equal.
2880 vim_strncpy(newname, name, MAXPATHL);
2881 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2882 MAXPATHL - (tail - name));
2883 if (mch_lstat((char *)newname, &st2) >= 0
2884 && st.st_ino == st2.st_ino
2885 && st.st_dev == st2.st_dev)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002886 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002887 STRCPY(tail, dp->d_name);
2888 break;
Bram Moolenaar24552be2005-12-10 20:17:30 +00002889 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002890 }
2891 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002892
2893 closedir(dirp);
Bram Moolenaar24552be2005-12-10 20:17:30 +00002894}
2895#endif
2896
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897/*
2898 * Get file permissions for 'name'.
2899 * Returns -1 when it doesn't exist.
2900 */
2901 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002902mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
2904 struct stat statb;
2905
Bram Moolenaar0f873732019-12-05 20:28:46 +01002906 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907#ifdef VMS
2908 if (stat((char *)vms_fixfilename(name), &statb))
2909#else
2910 if (stat((char *)name, &statb))
2911#endif
2912 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002913#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002914 // The top bit makes the value negative, which means the file doesn't
2915 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002916 return statb.st_mode & ~S_ADDACE;
2917#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920}
2921
2922/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002923 * Set file permission for "name" to "perm".
2924 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 */
2926 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002927mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
2929 return (chmod((char *)
2930#ifdef VMS
2931 vms_fixfilename(name),
2932#else
2933 name,
2934#endif
2935 (mode_t)perm) == 0 ? OK : FAIL);
2936}
2937
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002938#if defined(HAVE_FCHMOD) || defined(PROTO)
2939/*
2940 * Set file permission for open file "fd" to "perm".
2941 * Return FAIL for failure, OK otherwise.
2942 */
2943 int
2944mch_fsetperm(int fd, long perm)
2945{
2946 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2947}
2948#endif
2949
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950#if defined(HAVE_ACL) || defined(PROTO)
2951# ifdef HAVE_SYS_ACL_H
2952# include <sys/acl.h>
2953# endif
2954# ifdef HAVE_SYS_ACCESS_H
2955# include <sys/access.h>
2956# endif
2957
2958# ifdef HAVE_SOLARIS_ACL
2959typedef struct vim_acl_solaris_T {
2960 int acl_cnt;
2961 aclent_t *acl_entry;
2962} vim_acl_solaris_T;
2963# endif
2964
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002965#if defined(HAVE_SELINUX) || defined(PROTO)
2966/*
2967 * Copy security info from "from_file" to "to_file".
2968 */
2969 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002970mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002971{
2972 if (from_file == NULL)
2973 return;
2974
2975 if (selinux_enabled == -1)
2976 selinux_enabled = is_selinux_enabled();
2977
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002978 if (selinux_enabled <= 0)
2979 return;
2980
2981 // Use "char *" instead of "security_context_t" to avoid a deprecation
2982 // warning.
2983 char *from_context = NULL;
2984 char *to_context = NULL;
2985
2986 if (getfilecon((char *)from_file, &from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002987 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002988 // If the filesystem doesn't support extended attributes,
2989 // the original had no special security context and the
2990 // target cannot have one either.
2991 if (errno == EOPNOTSUPP)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002992 return;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002993
2994 msg_puts(_("\nCould not get security context for "));
2995 msg_outtrans(from_file);
2996 msg_putchar('\n');
2997 return;
2998 }
2999 if (getfilecon((char *)to_file, &to_context) < 0)
3000 {
3001 msg_puts(_("\nCould not get security context for "));
3002 msg_outtrans(to_file);
3003 msg_putchar('\n');
3004 freecon (from_context);
3005 return ;
3006 }
3007 if (strcmp(from_context, to_context) != 0)
3008 {
3009 if (setfilecon((char *)to_file, from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003010 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003011 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003012 msg_outtrans(to_file);
3013 msg_putchar('\n');
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003014 }
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003015 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003016 freecon(to_context);
3017 freecon(from_context);
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003018}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003019#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003020
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003021#if defined(HAVE_SMACK) && !defined(PROTO)
3022/*
3023 * Copy security info from "from_file" to "to_file".
3024 */
3025 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01003026mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003027{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02003028 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003029 {
3030 XATTR_NAME_SMACK,
3031 XATTR_NAME_SMACKEXEC,
3032 XATTR_NAME_SMACKMMAP
3033 };
3034
3035 char buffer[SMACK_LABEL_LEN];
3036 const char *name;
3037 int index;
3038 int ret;
3039 ssize_t size;
3040
3041 if (from_file == NULL)
3042 return;
3043
3044 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
3045 / sizeof(smack_copied_attributes)[0]) ; index++)
3046 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003047 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003048 name = smack_copied_attributes[index];
3049
Bram Moolenaar0f873732019-12-05 20:28:46 +01003050 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003051 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
3052 if (size >= 0)
3053 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003054 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003055 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
3056 if (ret < 0)
3057 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003058 vim_snprintf((char *)IObuff, IOSIZE,
3059 _("Could not set security context %s for %s"),
3060 name, to_file);
3061 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003062 msg_putchar('\n');
3063 }
3064 }
3065 else
3066 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003067 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003068 switch (errno)
3069 {
3070 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003071 // extended attributes aren't supported or enabled
3072 // should a message be echoed? not sure...
3073 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003074
3075 case ERANGE:
3076 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003077 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003078 vim_snprintf((char *)IObuff, IOSIZE,
3079 _("Could not get security context %s for %s. Removing it!"),
3080 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003081 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003082 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003083 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003084
3085 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003086 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003087 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003088 // Silently ignore errors, apparently this happens when
3089 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003090 break;
3091 }
3092 }
3093 }
3094}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003095#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003096
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097/*
3098 * Return a pointer to the ACL of file "fname" in allocated memory.
3099 * Return NULL if the ACL is not available for whatever reason.
3100 */
3101 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003102mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103{
3104 vim_acl_T ret = NULL;
3105#ifdef HAVE_POSIX_ACL
3106 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3107#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003108#ifdef HAVE_SOLARIS_ZFS_ACL
3109 acl_t *aclent;
3110
3111 if (acl_get((char *)fname, 0, &aclent) < 0)
3112 return NULL;
3113 ret = (vim_acl_T)aclent;
3114#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115#ifdef HAVE_SOLARIS_ACL
3116 vim_acl_solaris_T *aclent;
3117
3118 aclent = malloc(sizeof(vim_acl_solaris_T));
3119 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3120 {
3121 free(aclent);
3122 return NULL;
3123 }
3124 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3125 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3126 {
3127 free(aclent->acl_entry);
3128 free(aclent);
3129 return NULL;
3130 }
3131 ret = (vim_acl_T)aclent;
3132#else
3133#if defined(HAVE_AIX_ACL)
3134 int aclsize;
3135 struct acl *aclent;
3136
3137 aclsize = sizeof(struct acl);
3138 aclent = malloc(aclsize);
3139 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3140 {
3141 if (errno == ENOSPC)
3142 {
3143 aclsize = aclent->acl_len;
3144 aclent = realloc(aclent, aclsize);
3145 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3146 {
3147 free(aclent);
3148 return NULL;
3149 }
3150 }
3151 else
3152 {
3153 free(aclent);
3154 return NULL;
3155 }
3156 }
3157 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003158#endif // HAVE_AIX_ACL
3159#endif // HAVE_SOLARIS_ACL
3160#endif // HAVE_SOLARIS_ZFS_ACL
3161#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 return ret;
3163}
3164
3165/*
3166 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3167 */
3168 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003169mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170{
3171 if (aclent == NULL)
3172 return;
3173#ifdef HAVE_POSIX_ACL
3174 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3175#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003176#ifdef HAVE_SOLARIS_ZFS_ACL
3177 acl_set((char *)fname, (acl_t *)aclent);
3178#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179#ifdef HAVE_SOLARIS_ACL
3180 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3181 ((vim_acl_solaris_T *)aclent)->acl_entry);
3182#else
3183#ifdef HAVE_AIX_ACL
3184 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003185#endif // HAVE_AIX_ACL
3186#endif // HAVE_SOLARIS_ACL
3187#endif // HAVE_SOLARIS_ZFS_ACL
3188#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189}
3190
3191 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003192mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193{
3194 if (aclent == NULL)
3195 return;
3196#ifdef HAVE_POSIX_ACL
3197 acl_free((acl_t)aclent);
3198#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003199#ifdef HAVE_SOLARIS_ZFS_ACL
3200 acl_free((acl_t *)aclent);
3201#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202#ifdef HAVE_SOLARIS_ACL
3203 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3204 free(aclent);
3205#else
3206#ifdef HAVE_AIX_ACL
3207 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003208#endif // HAVE_AIX_ACL
3209#endif // HAVE_SOLARIS_ACL
3210#endif // HAVE_SOLARIS_ZFS_ACL
3211#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212}
3213#endif
3214
3215/*
3216 * Set hidden flag for "name".
3217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003219mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003221 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222}
3223
3224/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003225 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 * return FALSE if "name" is not a directory
3227 * return FALSE for error
3228 */
3229 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003230mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231{
3232 struct stat statb;
3233
Bram Moolenaar0f873732019-12-05 20:28:46 +01003234 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 return FALSE;
3236 if (stat((char *)name, &statb))
3237 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239}
3240
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003241/*
3242 * return TRUE if "name" is a directory, NOT a symlink to a directory
3243 * return FALSE if "name" is not a directory
3244 * return FALSE for error
3245 */
3246 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003247mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003248{
3249 struct stat statb;
3250
Bram Moolenaar0f873732019-12-05 20:28:46 +01003251 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003252 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003253 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003254 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003255 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003256}
3257
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258/*
3259 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3260 */
3261 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003262executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 struct stat st;
3265
3266 if (stat((char *)name, &st))
3267 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003268#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003269 // Like on Unix system file can have executable rights but not necessarily
3270 // be an executable, but on Unix is not a default for an ordinary file to
3271 // have an executable flag - on VMS it is in most cases.
3272 // Therefore, this check does not have any sense - let keep us to the
3273 // conventions instead:
3274 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003275 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003276 int vms_executable = 0;
3277 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3278 {
3279 if (strstr(vms_tolower((char*)name),".exe") != NULL
3280 || strstr(vms_tolower((char*)name),".com")!= NULL)
3281 vms_executable = 1;
3282 }
3283 return vms_executable;
3284#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287}
3288
3289/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003290 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003291 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 * Return -1 if unknown.
3293 */
3294 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003295mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
3297 char_u *buf;
3298 char_u *p, *e;
3299 int retval;
3300
Bram Moolenaar0f873732019-12-05 20:28:46 +01003301 // When "use_path" is false and if it's an absolute or relative path don't
3302 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003303 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003304 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003305 // There must be a path separator, files in the current directory
3306 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003307 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003308 {
3309 if (path != NULL)
3310 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003311 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003312 *path = FullName_save(name, TRUE);
3313 else
3314 *path = vim_strsave(name);
3315 }
3316 return TRUE;
3317 }
3318 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
3321 p = (char_u *)getenv("PATH");
3322 if (p == NULL || *p == NUL)
3323 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003324 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 if (buf == NULL)
3326 return -1;
3327
3328 /*
3329 * Walk through all entries in $PATH to check if "name" exists there and
3330 * is an executable file.
3331 */
3332 for (;;)
3333 {
3334 e = (char_u *)strchr((char *)p, ':');
3335 if (e == NULL)
3336 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003337 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 STRCPY(buf, "./");
3339 else
3340 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003341 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 add_pathsep(buf);
3343 }
3344 STRCAT(buf, name);
3345 retval = executable_file(buf);
3346 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003347 {
3348 if (path != NULL)
3349 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003350 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003351 *path = FullName_save(buf, TRUE);
3352 else
3353 *path = vim_strsave(buf);
3354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
3358 if (*e != ':')
3359 break;
3360 p = e + 1;
3361 }
3362
3363 vim_free(buf);
3364 return retval;
3365}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
3367/*
3368 * Check what "name" is:
3369 * NODE_NORMAL: file or directory (or doesn't exist)
3370 * NODE_WRITABLE: writable device, socket, fifo, etc.
3371 * NODE_OTHER: non-writable things
3372 */
3373 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003374mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 struct stat st;
3377
3378 if (stat((char *)name, &st))
3379 return NODE_NORMAL;
3380 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3381 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003382 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003384 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 return NODE_WRITABLE;
3386}
3387
3388 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003389mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
3391#ifdef HAVE_CHECK_STACK_GROWTH
3392 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 check_stack_growth((char *)&i);
3395
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003396# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 get_stack_limit();
3398# endif
3399
3400#endif
3401
3402 /*
3403 * Setup an alternative stack for signals. Helps to catch signals when
3404 * running out of stack space.
3405 * Use of sigaltstack() is preferred, it's more portable.
3406 * Ignore any errors.
3407 */
3408#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003409 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 init_signal_stack();
3411#endif
3412}
3413
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003414#if defined(EXITFREE) || defined(PROTO)
3415 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003416mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003417{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003418# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3419 if (clip_star.owned)
3420 clip_lose_selection(&clip_star);
3421 if (clip_plus.owned)
3422 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003423# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003424# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003425 if (xterm_Shell != (Widget)0)
3426 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003427# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003428 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003429 if (xterm_dpy != NULL)
3430 XtCloseDisplay(xterm_dpy);
3431 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003432 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003433 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003434# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003435 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003436# endif
3437 }
3438# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003439# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003440# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003441 if (x11_display != NULL
3442# ifdef FEAT_XCLIPBOARD
3443 && x11_display != xterm_dpy
3444# endif
3445 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003446 XCloseDisplay(x11_display);
3447# endif
3448# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003449 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003450# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003451 vim_free(oldtitle);
3452 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003453}
3454#endif
3455
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456/*
3457 * Output a newline when exiting.
3458 * Make sure the newline goes to the same stream as the text.
3459 */
3460 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003461exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003463 if (silent_mode)
3464 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 if (newline_on_exit || msg_didout)
3466 {
3467 if (msg_use_printf())
3468 {
3469 if (info_message)
3470 mch_msg("\n");
3471 else
3472 mch_errmsg("\r\n");
3473 }
3474 else
3475 out_char('\n');
3476 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003477 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003479 restore_cterm_colors(); // get original colors back
3480 msg_clr_eos_force(); // clear the rest of the display
3481 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 }
3483}
3484
Bram Moolenaarb4151682020-05-11 22:13:28 +02003485#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003486# if (defined(__GNUC__) \
3487 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3488 || (defined(__clang__) && (__clang_major__ >= 12))
3489extern void __gcov_dump(void);
3490extern void __gcov_reset(void);
3491# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3492# else
3493extern void __gcov_flush(void);
3494# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003495#endif
3496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003498mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499{
3500 exiting = TRUE;
3501
3502#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3503 x11_export_final_selection();
3504#endif
3505
3506#ifdef FEAT_GUI
3507 if (!gui.in_use)
3508#endif
3509 {
3510 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003511 if (!is_not_a_term())
3512 {
3513 // restore xterm title and icon name
3514 mch_restore_title(SAVE_RESTORE_BOTH);
3515 term_pop_title(SAVE_RESTORE_BOTH);
3516 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003517
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 /*
3519 * When t_ti is not empty but it doesn't cause swapping terminal
3520 * pages, need to output a newline when msg_didout is set. But when
3521 * t_ti does swap pages it should not go to the shell page. Do this
3522 * before stoptermcap().
3523 */
3524 if (swapping_screen() && !newline_on_exit)
3525 exit_scroll();
3526
Bram Moolenaar0f873732019-12-05 20:28:46 +01003527 // Stop termcap: May need to check for T_CRV response, which
3528 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 stoptermcap();
3530
3531 /*
3532 * A newline is only required after a message in the alternate screen.
3533 * This is set to TRUE by wait_return().
3534 */
3535 if (!swapping_screen() || newline_on_exit)
3536 exit_scroll();
3537
Bram Moolenaar0f873732019-12-05 20:28:46 +01003538 // Cursor may have been switched off without calling starttermcap()
3539 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (full_screen)
3541 cursor_on();
3542 }
3543 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003544 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003545
3546#ifdef USE_GCOV_FLUSH
3547 // Flush coverage info before possibly being killed by a deadly signal.
3548 __gcov_flush();
3549#endif
3550
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 may_core_dump();
3552#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 gui_exit(r);
3555#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003556
Bram Moolenaar56718732006-03-15 22:53:57 +00003557#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003558 mac_conv_cleanup();
3559#endif
3560
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003562 // A core dump won't be created if the signal handler
3563 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 if (deadly_signal != 0)
3565 return;
3566#endif
3567
Bram Moolenaar009b2592004-10-24 19:18:58 +00003568#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003569 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003570#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003571
3572#ifdef EXITFREE
3573 free_all_mem();
3574#endif
3575
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 exit(r);
3577}
3578
3579 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003580may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581{
3582 if (deadly_signal != 0)
3583 {
ichizok378447f2023-05-11 22:25:42 +01003584 mch_signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003585 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
3587}
3588
3589#ifndef VMS
3590
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003591/*
3592 * Get the file descriptor to use for tty operations.
3593 */
3594 static int
3595get_tty_fd(int fd)
3596{
3597 int tty_fd = fd;
3598
3599#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3600 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3601 // "fd" when it is a master device.
3602 if (mch_isatty(fd) > 1)
3603 {
3604 char *name;
3605
3606 name = ptsname(fd);
3607 if (name == NULL)
3608 return -1;
3609
3610 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3611 if (tty_fd < 0)
3612 return -1;
3613 }
3614#endif
3615 return tty_fd;
3616}
3617
3618 static int
3619mch_tcgetattr(int fd, void *term)
3620{
3621 int tty_fd;
3622 int retval = -1;
3623
3624 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003625 if (tty_fd < 0)
3626 return -1;
3627
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003628#ifdef NEW_TTY_SYSTEM
3629# ifdef HAVE_TERMIOS_H
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003630 retval = tcgetattr(tty_fd, (struct termios *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003631# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003632 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003633# endif
3634#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003635 // for "old" tty systems
3636 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003637#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003638 if (tty_fd != fd)
3639 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003640 return retval;
3641}
3642
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003644mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
3646 static int first = TRUE;
3647
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003648#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649# ifdef HAVE_TERMIOS_H
3650 static struct termios told;
3651 struct termios tnew;
3652# else
3653 static struct termio told;
3654 struct termio tnew;
3655# endif
3656
3657 if (first)
3658 {
3659 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003660 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
3662
3663 tnew = told;
3664 if (tmode == TMODE_RAW)
3665 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003666 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003667 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3668 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003670# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003671 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672# endif
3673 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003674# ifdef ONLCR
3675 // Don't map NL -> CR NL, we do it ourselves.
3676 // Also disable expanding tabs if possible.
3677# ifdef XTABS
3678 tnew.c_oflag &= ~(ONLCR | XTABS);
3679# else
3680# ifdef TAB3
3681 tnew.c_oflag &= ~(ONLCR | TAB3);
3682# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003684# endif
3685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003687 tnew.c_cc[VMIN] = 1; // return after 1 char
3688 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003691 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003692 // Also reset ICANON here, otherwise on Solaris select() won't see
3693 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003694 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003695 tnew.c_cc[VMIN] = 1; // return after 1 char
3696 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698
3699# if defined(HAVE_TERMIOS_H)
3700 {
3701 int n = 10;
3702
Bram Moolenaar0f873732019-12-05 20:28:46 +01003703 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3704 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3706 && errno == EINTR && n > 0)
3707 --n;
3708 }
3709# else
3710 ioctl(read_cmd_fd, TCSETA, &tnew);
3711# endif
3712
3713#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 /*
3715 * for "old" tty systems
3716 */
3717# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003718# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719# endif
3720 static struct sgttyb ttybold;
3721 struct sgttyb ttybnew;
3722
3723 if (first)
3724 {
3725 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003726 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
3728
3729 ttybnew = ttybold;
3730 if (tmode == TMODE_RAW)
3731 {
3732 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3733 ttybnew.sg_flags |= RAW;
3734 }
3735 else if (tmode == TMODE_SLEEP)
3736 ttybnew.sg_flags &= ~(ECHO);
3737 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3738#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003739 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740}
3741
3742/*
3743 * Try to get the code for "t_kb" from the stty setting
3744 *
3745 * Even if termcap claims a backspace key, the user's setting *should*
3746 * prevail. stty knows more about reality than termcap does, and if
3747 * somebody's usual erase key is DEL (which, for most BSD users, it will
3748 * be), they're going to get really annoyed if their erase key starts
3749 * doing forward deletes for no reason. (Eric Fischer)
3750 */
3751 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003752get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003754 ttyinfo_T info;
3755 char_u buf[2];
3756 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003758 if (get_tty_info(read_cmd_fd, &info) != OK)
3759 return;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003760
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003761 intr_char = info.interrupt;
3762 buf[0] = info.backspace;
3763 buf[1] = NUL;
3764 add_termcode((char_u *)"kb", buf, FALSE);
3765
3766 // If <BS> and <DEL> are now the same, redefine <DEL>.
3767 p = find_termcode((char_u *)"kD");
3768 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3769 do_fixdel(NULL);
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003770}
3771
3772/*
3773 * Obtain the characters that Backspace and Enter produce on "fd".
3774 * Returns OK or FAIL.
3775 */
3776 int
3777get_tty_info(int fd, ttyinfo_T *info)
3778{
3779#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780# ifdef HAVE_TERMIOS_H
3781 struct termios keys;
3782# else
3783 struct termio keys;
3784# endif
3785
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003786 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003788 info->backspace = keys.c_cc[VERASE];
3789 info->interrupt = keys.c_cc[VINTR];
3790 if (keys.c_iflag & ICRNL)
3791 info->enter = NL;
3792 else
3793 info->enter = CAR;
3794 if (keys.c_oflag & ONLCR)
3795 info->nl_does_cr = TRUE;
3796 else
3797 info->nl_does_cr = FALSE;
3798 return OK;
3799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003801 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 struct sgttyb keys;
3803
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003804 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003806 info->backspace = keys.sg_erase;
3807 info->interrupt = keys.sg_kill;
3808 info->enter = CAR;
3809 info->nl_does_cr = TRUE;
3810 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003813 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814}
3815
Bram Moolenaar0f873732019-12-05 20:28:46 +01003816#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003818static int mouse_ison = FALSE;
3819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820/*
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003821 * Set mouse clicks on or off and possible enable mouse movement events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 */
3823 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003824mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003826#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003827 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 int xterm_mouse_vers;
3830
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003831#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003832 if (!on)
3833 // Make sure not tracing mouse movements. Important when a button-down
3834 // was received but no release yet.
3835 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003836#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003837
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003838 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003839#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003840 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003841#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003842 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003843 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 return;
3845
3846 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003847
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003848#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003849 if (ttym_flags == TTYM_URXVT)
3850 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003851 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003852 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003853 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003854#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003855
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00003856 if (T_CXM != NULL && *T_CXM != NUL)
3857 {
3858 term_enable_mouse(on);
3859 }
3860 else if (ttym_flags == TTYM_SGR)
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003861 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003862 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003863 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003864 mouse_ison = on;
3865 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003866
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003867#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003868 if (bevalterm_ison != (p_bevalterm && on))
3869 {
3870 bevalterm_ison = (p_bevalterm && on);
3871 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003872 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003873 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003874 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003875#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003876
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 if (xterm_mouse_vers > 0)
3878 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003879 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 out_str_nf((char_u *)
3881 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003882 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003883#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003884 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003885#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003886 "\033[?1002h")
3887 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003888 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003890 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003891 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003894#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 else if (ttym_flags == TTYM_DEC)
3896 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003897 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003899 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003901 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003905#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 else
3907 {
3908 if (on)
3909 {
3910 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003911 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
3913 else
3914 {
3915 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003916 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 }
3918 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003921#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003922 else
3923 {
3924 if (on)
3925 {
3926 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003927 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003928 }
3929 else
3930 {
3931 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003932 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003933 }
3934 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003935#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003936
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003937#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 else
3939 {
3940 if (on)
3941 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003942 // D - Enable Mouse up/down messages
3943 // L - Enable Left Button Reporting
3944 // M - Enable Middle Button Reporting
3945 // R - Enable Right Button Reporting
3946 // K - Enable SHIFT and CTRL key Reporting
3947 // + - Enable Advanced messaging of mouse moves and up/down messages
3948 // Q - Quiet No Ack
3949 // # - Numeric value of mouse pointer required
3950 // 0 = Multiview 2000 cursor, used as standard
3951 // 1 = Windows Arrow
3952 // 2 = Windows I Beam
3953 // 3 = Windows Hour Glass
3954 // 4 = Windows Cross Hair
3955 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003956# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003957 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003958 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003959# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003960 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003961# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003962 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964 else
3965 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003966 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003967 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003970#endif
3971#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 else
3973 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003974 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 if (on)
3976 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3977 else
3978 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003979 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982}
3983
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003984#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003985/*
3986 * Called when 'balloonevalterm' changed.
3987 */
3988 void
3989mch_bevalterm_changed(void)
3990{
3991 mch_setmouse(mouse_ison);
3992}
3993#endif
3994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995/*
3996 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3997 */
3998 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003999check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
4001# ifdef FEAT_MOUSE_XTERM
4002 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02004003# ifdef FEAT_MOUSE_URXVT
4004 && use_xterm_mouse() != 3
4005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006# ifdef FEAT_GUI
4007 && !gui.in_use
4008# endif
4009 )
4010 {
4011 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004012 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 if (*p_mouse != NUL)
4014 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004015 // force mouse off and maybe on to send possibly new mouse
4016 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 mch_setmouse(FALSE);
4018 setmouse();
4019 }
4020 }
4021 else
4022 del_mouse_termcode(KS_MOUSE);
4023# endif
4024
4025# ifdef FEAT_MOUSE_GPM
4026 if (!use_xterm_mouse()
4027# ifdef FEAT_GUI
4028 && !gui.in_use
4029# endif
4030 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004031 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02004032 else
4033 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034# endif
4035
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004036# ifdef FEAT_SYSMOUSE
4037 if (!use_xterm_mouse()
4038# ifdef FEAT_GUI
4039 && !gui.in_use
4040# endif
4041 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004042 set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004043# endif
4044
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01004046 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 if (!use_xterm_mouse()
4048# ifdef FEAT_GUI
4049 && !gui.in_use
4050# endif
4051 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004052 set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 else
4054 del_mouse_termcode(KS_JSBTERM_MOUSE);
4055# endif
4056
4057# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01004058 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
4059 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 if (!use_xterm_mouse()
4061# ifdef FEAT_GUI
4062 && !gui.in_use
4063# endif
4064 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004065 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 else
4067 del_mouse_termcode(KS_NETTERM_MOUSE);
4068# endif
4069
4070# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01004071 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004072 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073# ifdef FEAT_GUI
4074 && !gui.in_use
4075# endif
4076 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004077 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004078 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 else
4080 del_mouse_termcode(KS_DEC_MOUSE);
4081# endif
4082# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004083 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004084 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085# ifdef FEAT_GUI
4086 && !gui.in_use
4087# endif
4088 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004089 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 else
4091 del_mouse_termcode(KS_PTERM_MOUSE);
4092# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004093# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004094 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004095# ifdef FEAT_GUI
4096 && !gui.in_use
4097# endif
4098 )
4099 {
4100 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004101 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004102
4103 if (*p_mouse != NUL)
4104 {
4105 mch_setmouse(FALSE);
4106 setmouse();
4107 }
4108 }
4109 else
4110 del_mouse_termcode(KS_URXVT_MOUSE);
4111# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004112 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004113# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004114 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004115# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004116 )
4117 {
4118 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004119 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004120
4121 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004122 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004123
4124 if (*p_mouse != NUL)
4125 {
4126 mch_setmouse(FALSE);
4127 setmouse();
4128 }
4129 }
4130 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004131 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004132 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004133 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137#ifndef VMS
4138
4139/*
4140 * Try to get the current window size:
4141 * 1. with an ioctl(), most accurate method
4142 * 2. from the environment variables LINES and COLUMNS
4143 * 3. from the termcap
4144 * 4. keep using the old values
4145 * Return OK when size could be determined, FAIL otherwise.
4146 */
4147 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004148mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149{
4150 long rows = 0;
4151 long columns = 0;
4152 char_u *p;
4153
4154 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 * 1. try using an ioctl. It is the most accurate method.
4156 *
4157 * Try using TIOCGWINSZ first, some systems that have it also define
4158 * TIOCGSIZE but don't have a struct ttysize.
4159 */
4160# ifdef TIOCGWINSZ
4161 {
4162 struct winsize ws;
4163 int fd = 1;
4164
Bram Moolenaar0f873732019-12-05 20:28:46 +01004165 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 if (!isatty(fd) && isatty(read_cmd_fd))
4167 fd = read_cmd_fd;
4168 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4169 {
4170 columns = ws.ws_col;
4171 rows = ws.ws_row;
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004172 ch_log(NULL, "Got size with TIOCGWINSZ: %ld x %ld", columns, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 }
4174 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004175# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176# ifdef TIOCGSIZE
4177 {
4178 struct ttysize ts;
4179 int fd = 1;
4180
Bram Moolenaar0f873732019-12-05 20:28:46 +01004181 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 if (!isatty(fd) && isatty(read_cmd_fd))
4183 fd = read_cmd_fd;
4184 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4185 {
4186 columns = ts.ts_cols;
4187 rows = ts.ts_lines;
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004188 ch_log(NULL, "Got size with TIOCGSIZE: %ld x %ld", columns, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004191# endif // TIOCGSIZE
4192# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
4194 /*
4195 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004196 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4197 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004199 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 {
4201 if ((p = (char_u *)getenv("LINES")))
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 rows = atoi((char *)p);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004204 ch_log(NULL, "Got 'lines' from $LINES: %ld", rows);
4205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 if ((p = (char_u *)getenv("COLUMNS")))
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 columns = atoi((char *)p);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004209 ch_log(NULL, "Got 'columns' from $COLUMNS: %ld", columns);
4210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 }
4212
4213#ifdef HAVE_TGETENT
4214 /*
4215 * 3. try reading "co" and "li" entries from termcap
4216 */
4217 if (columns == 0 || rows == 0)
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 getlinecol(&columns, &rows);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004220 ch_log(NULL, "Got size from termcap: %ld x %ld", columns, rows);
4221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222#endif
4223
4224 /*
4225 * 4. If everything fails, use the old values
4226 */
4227 if (columns <= 0 || rows <= 0)
4228 return FAIL;
4229
4230 Rows = rows;
4231 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004232 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 return OK;
4234}
4235
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004236#if defined(FEAT_TERMINAL) || defined(PROTO)
4237/*
4238 * Report the windows size "rows" and "cols" to tty "fd".
4239 */
4240 int
4241mch_report_winsize(int fd, int rows, int cols)
4242{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004243 int tty_fd;
4244 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004245
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004246 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004247 if (tty_fd < 0)
4248 return FAIL;
4249
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004250# if defined(TIOCSWINSZ)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004251 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004252
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004253 ws.ws_col = cols;
4254 ws.ws_row = rows;
4255 ws.ws_xpixel = cols * 5;
4256 ws.ws_ypixel = rows * 10;
4257 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004258 ch_log(NULL, "ioctl(TIOCSWINSZ) %s", retval == 0 ? "success" : "failed");
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004259# elif defined(TIOCSSIZE)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004260 struct ttysize ts;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004261
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004262 ts.ts_cols = cols;
4263 ts.ts_lines = rows;
4264 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004265 ch_log(NULL, "ioctl(TIOCSSIZE) %s", retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004266# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004267 if (tty_fd != fd)
4268 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004269 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004270}
4271#endif
4272
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273/*
4274 * Try to set the window size to Rows and Columns.
4275 */
4276 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004277mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278{
4279 if (*T_CWS)
4280 {
4281 /*
4282 * NOTE: if you get an error here that term_set_winsize() is
4283 * undefined, check the output of configure. It could probably not
4284 * find a ncurses, termcap or termlib library.
4285 */
4286 term_set_winsize((int)Rows, (int)Columns);
4287 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004288 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290}
4291
Bram Moolenaar0f873732019-12-05 20:28:46 +01004292#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
4294/*
4295 * Rows and/or Columns has changed.
4296 */
4297 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004298mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004300 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301}
4302
Bram Moolenaar205b8862011-09-07 15:04:31 +02004303/*
4304 * Wait for process "child" to end.
4305 * Return "child" if it exited properly, <= 0 on error.
4306 */
4307 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004308wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004309{
4310 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004311 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004312
4313 while (wait_pid != child)
4314 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004315 // When compiled with Python threads are probably used, in which case
4316 // wait() sometimes hangs for no obvious reason. Use waitpid()
4317 // instead and loop (like the GUI). Also needed for other interfaces,
4318 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004319# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004320 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004321# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004322 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004323# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004324 if (wait_pid == 0)
4325 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004326 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004327 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004328 if (++delay_msec > 10)
4329 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004330 continue;
4331 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004332 if (wait_pid <= 0
4333# ifdef ECHILD
4334 && errno == ECHILD
4335# endif
4336 )
4337 break;
4338 }
4339 return wait_pid;
4340}
4341
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004342#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004343/*
4344 * Set the environment for a child process.
4345 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004346 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004347set_child_environment(
4348 long rows,
4349 long columns,
4350 char *term,
4351 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004352{
4353# ifdef HAVE_SETENV
4354 char envbuf[50];
4355# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004356 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004357 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004358 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004359 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004360 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004361# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004362 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004363# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004364# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004365 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004366# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004367# endif
4368
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004369# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004370 setenv("TERM", term, 1);
4371 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004372 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004373 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004374 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004375 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004376 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004377 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004378 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004379# ifdef FEAT_TERMINAL
4380 if (is_terminal)
4381 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004382 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004383 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4384 }
4385# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004386# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004387 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004388# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004389# else
4390 /*
4391 * Putenv does not copy the string, it has to remain valid.
4392 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004393 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004394 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004395 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4396 putenv(envbuf_Term);
4397 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004398 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004399 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4400 putenv(envbuf_Lines);
4401 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4402 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004403 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004404 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004405 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004406# ifdef FEAT_TERMINAL
4407 if (is_terminal)
4408 {
4409 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004410 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004411 putenv(envbuf_Version);
4412 }
4413# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004414# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004415 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4416 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4417 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004418# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004419# endif
4420}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004421
4422 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004423set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004424{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004425 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004426}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004427#endif
4428
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004429#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004430/*
4431 * Open a PTY, with FD for the master and slave side.
4432 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004433 * When successful both file descriptors are stored and the allocated pty name
4434 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004435 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004436 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004437open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004438{
4439 char *tty_name;
4440
Bram Moolenaar59386482019-02-10 22:43:46 +01004441 if (name1 != NULL)
4442 *name1 = NULL;
4443 if (name2 != NULL)
4444 *name2 = NULL;
4445
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004446 *pty_master_fd = mch_openpty(&tty_name); // open pty
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004447 if (*pty_master_fd < 0)
4448 return;
4449
4450 // Leaving out O_NOCTTY may lead to waitpid() always returning
4451 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4452 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004453#ifdef O_NOCTTY
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004454 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004455#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004456 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004457#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004458 if (*pty_slave_fd < 0)
4459 {
4460 close(*pty_master_fd);
4461 *pty_master_fd = -1;
4462 }
4463 else
4464 {
4465 if (name1 != NULL)
4466 *name1 = vim_strsave((char_u *)tty_name);
4467 if (name2 != NULL)
4468 *name2 = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004469 }
4470}
4471#endif
4472
Bram Moolenaarfae42832017-08-01 22:24:26 +02004473/*
4474 * Send SIGINT to a child process if "c" is an interrupt character.
4475 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004476 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004477may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4478{
4479# ifdef SIGINT
4480 if (c == Ctrl_C || c == intr_char)
4481 {
4482# ifdef HAVE_SETSID
4483 kill(-pid, SIGINT);
4484# else
4485 kill(0, SIGINT);
4486# endif
4487 if (wpid > 0)
4488 kill(wpid, SIGINT);
4489 }
4490# endif
4491}
4492
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004493#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004494
Bram Moolenaar0f873732019-12-05 20:28:46 +01004495/*
4496 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4497 * pointers, the last one is NULL.
4498 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4499 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004500 int
4501unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004502 char_u *cmd,
4503 char ***argvp,
4504 char_u **sh_tofree,
4505 char_u **shcf_tofree)
4506{
4507 char **argv = NULL;
4508 int argc;
4509
4510 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004511 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004512 return FAIL;
4513
4514 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4515 return FAIL;
4516 *argvp = argv;
4517
4518 if (cmd != NULL)
4519 {
4520 char_u *s;
4521 char_u *p;
4522
4523 if (extra_shell_arg != NULL)
4524 argv[argc++] = (char *)extra_shell_arg;
4525
Bram Moolenaar0f873732019-12-05 20:28:46 +01004526 // Break 'shellcmdflag' into white separated parts. This doesn't
4527 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004528 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004529 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004530 return FAIL;
4531 s = *shcf_tofree;
4532 p = p_shcf;
4533 while (*p != NUL)
4534 {
4535 argv[argc++] = (char *)s;
4536 while (*p && *p != ' ' && *p != TAB)
4537 *s++ = *p++;
4538 *s++ = NUL;
4539 p = skipwhite(p);
4540 }
4541
4542 argv[argc++] = (char *)cmd;
4543 }
4544 argv[argc] = NULL;
4545 return OK;
4546}
4547#endif
4548
4549#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4550/*
4551 * Use a terminal window to run a shell command in.
4552 */
4553 static int
4554mch_call_shell_terminal(
4555 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004556 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004557{
4558 jobopt_T opt;
4559 char **argv = NULL;
4560 char_u *tofree1 = NULL;
4561 char_u *tofree2 = NULL;
4562 int retval = -1;
4563 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004564 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004565 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004566 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004567
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004568 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004569 goto theend;
4570
4571 init_job_options(&opt);
4572 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4573 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004574 if (buf == NULL)
4575 goto theend;
4576
4577 job = term_getjob(buf->b_term);
4578 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004579
Bram Moolenaar0f873732019-12-05 20:28:46 +01004580 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004581 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004582 if (curbuf == buf)
Bram Moolenaar13568252018-03-16 20:46:58 +01004583 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004584 // Only when managed to find a window for "buf",
4585 clear_oparg(&oa);
4586 while (term_use_loop())
Bram Moolenaar13568252018-03-16 20:46:58 +01004587 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004588 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4589 {
4590 // If terminal_loop() returns OK we got a key that is handled
4591 // in Normal model. We don't do redrawing anyway.
4592 if (terminal_loop(TRUE) == OK)
4593 normal_cmd(&oa, TRUE);
4594 }
4595 else
Bram Moolenaar13568252018-03-16 20:46:58 +01004596 normal_cmd(&oa, TRUE);
4597 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00004598 retval = job->jv_exitval;
4599 ch_log(NULL, "system command finished");
4600
4601 job_unref(job);
4602
4603 // restore curwin/curbuf and a few other things
4604 aucmd_restbuf(&aco);
Bram Moolenaar13568252018-03-16 20:46:58 +01004605 }
Bram Moolenaar13568252018-03-16 20:46:58 +01004606
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01004607 // Only require pressing Enter when redrawing, to avoid that system() gets
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004608 // the hit-enter prompt even though it didn't output anything.
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004609 if (RedrawingDisabled == 0)
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004610 wait_return(TRUE);
Bram Moolenaar13568252018-03-16 20:46:58 +01004611 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4612
4613theend:
4614 vim_free(argv);
4615 vim_free(tofree1);
4616 vim_free(tofree2);
4617 return retval;
4618}
4619#endif
4620
4621#ifdef USE_SYSTEM
4622/*
4623 * Use system() to start the shell: simple but slow.
4624 */
4625 static int
4626mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004627 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004628 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629{
4630#ifdef VMS
4631 char *ifn = NULL;
4632 char *ofn = NULL;
4633#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004634 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004635 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004636 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637
4638 out_flush();
4639
4640 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004641 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
Bram Moolenaar62b42182010-09-21 22:09:37 +02004643# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004644 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004645 loose_clipboard();
4646# endif
4647
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (cmd == NULL)
4649 x = system((char *)p_sh);
4650 else
4651 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004652# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 if (ofn = strchr((char *)cmd, '>'))
4654 *ofn++ = '\0';
4655 if (ifn = strchr((char *)cmd, '<'))
4656 {
4657 char *p;
4658
4659 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004660 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (p)
4662 *p = '\0';
4663 }
4664 if (ofn)
4665 x = vms_sys((char *)cmd, ofn, ifn);
4666 else
4667 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004668# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004669 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004671 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (newcmd == NULL)
4673 x = 0;
4674 else
4675 {
4676 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4677 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4678 (char *)p_shcf,
4679 (char *)cmd);
4680 x = system((char *)newcmd);
4681 vim_free(newcmd);
4682 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004683# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 }
4685# ifdef VMS
4686 x = vms_sys_status(x);
4687# endif
4688 if (emsg_silent)
4689 ;
4690 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004691 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 else if (x && !(options & SHELL_SILENT))
4693 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004694 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 msg_outnum((long)x);
4696 msg_putchar('\n');
4697 }
4698
4699 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004700 {
4701 // The shell may have messed with the mode, always set it.
4702 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004703 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004706# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4707 restore_clipboard();
4708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004710}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711
Bram Moolenaar0f873732019-12-05 20:28:46 +01004712#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713
Bram Moolenaar0f873732019-12-05 20:28:46 +01004714# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4715 // 127, some shells use that already
4716# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717
Bram Moolenaar13568252018-03-16 20:46:58 +01004718/*
4719 * Don't use system(), use fork()/exec().
4720 */
4721 static int
4722mch_call_shell_fork(
4723 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004724 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004725{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004726 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004728 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 pid_t wait_pid = 0;
4730# ifdef HAVE_UNION_WAIT
4731 union wait status;
4732# else
4733 int status = -1;
4734# endif
4735 int retval = -1;
4736 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004737 char_u *tofree1 = NULL;
4738 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004740 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004741# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004743# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004744 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 int fd_fromshell[2];
4746 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004747 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
4749 out_flush();
4750 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004751 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004752 if (tmode == TMODE_RAW)
4753 // The shell may have messed with the mode, always set it later.
4754 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004756 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004757 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004758
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004760 * For the GUI, when writing the output into the buffer and when reading
4761 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4762 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004764 if ((options & (SHELL_READ|SHELL_WRITE))
4765# ifdef FEAT_GUI
4766 || (gui.in_use && show_shell_mess)
4767# endif
4768 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004770# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 /*
4772 * Try to open a master pty.
4773 * If this works, open the slave pty.
4774 * If the slave can't be opened, close the master pty.
4775 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004776 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004777 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 /*
4779 * If not opening a pty or it didn't work, try using pipes.
4780 */
4781 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 {
4784 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004785 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 {
4787 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004788 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 {
4790 close(fd_toshell[0]);
4791 close(fd_toshell[1]);
4792 }
4793 }
4794 if (pipe_error)
4795 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004796 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 out_flush();
4798 }
4799 }
4800 }
4801
Bram Moolenaar0f873732019-12-05 20:28:46 +01004802 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004804 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004805 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004806 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004807 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004809 UNBLOCK_SIGNALS(&curset);
4810
Bram Moolenaar32526b32019-01-19 17:43:09 +01004811 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004812 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004814 || (gui.in_use && show_shell_mess)
4815# endif
4816 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004818# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004819 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
4821 close(pty_master_fd);
4822 close(pty_slave_fd);
4823 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004824 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004825# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 {
4827 close(fd_toshell[0]);
4828 close(fd_toshell[1]);
4829 close(fd_fromshell[0]);
4830 close(fd_fromshell[1]);
4831 }
4832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004834 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004836 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004837 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00004839# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01004840 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004841 {
4842 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004843 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004844 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004845# endif
4846
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 if (!show_shell_mess || (options & SHELL_EXPAND))
4848 {
4849 int fd;
4850
4851 /*
4852 * Don't want to show any message from the shell. Can't just
4853 * close stdout and stderr though, because some systems will
4854 * break if you try to write to them after that, so we must
4855 * use dup() to replace them with something else -- webb
4856 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4857 * waiting for input.
4858 */
4859 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4860 fclose(stdin);
4861 fclose(stdout);
4862 fclose(stderr);
4863
4864 /*
4865 * If any of these open()'s and dup()'s fail, we just continue
4866 * anyway. It's not fatal, and on most systems it will make
4867 * no difference at all. On a few it will cause the execvp()
4868 * to exit with a non-zero status even when the completion
4869 * could be done, which is nothing too serious. If the open()
4870 * or dup() failed we'd just do the same thing ourselves
4871 * anyway -- webb
4872 */
4873 if (fd >= 0)
4874 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004875 vim_ignored = dup(fd); // To replace stdin (fd 0)
4876 vim_ignored = dup(fd); // To replace stdout (fd 1)
4877 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
Bram Moolenaar0f873732019-12-05 20:28:46 +01004879 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 close(fd);
4881 }
4882 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004883 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004885 || gui.in_use
4886# endif
4887 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
4889
Bram Moolenaardf177f62005-02-22 08:39:57 +00004890# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004891 // Create our own process group, so that the child and all its
4892 // children can be kill()ed. Don't do this when using pipes,
4893 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004894 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004895 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004896 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004897# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004898 // When doing "!xterm&" and 'shell' is bash: the shell
4899 // will exit and send SIGHUP to all processes in its
4900 // group, killing the just started process. Ignore SIGHUP
4901 // to avoid that. (suggested by Simon Schubert)
ichizok378447f2023-05-11 22:25:42 +01004902 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar07256082009-02-04 13:19:42 +00004903# endif
4904 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004905# endif
4906# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004907 if (pty_slave_fd >= 0)
4908 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004909 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004910 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004911 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004912# ifdef TIOCSCTTY
4913 // Try to become controlling tty (probably doesn't work,
4914 // unless run by root)
4915 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4916# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004917 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004918# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004919 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
Bram Moolenaara5792f52005-11-23 21:25:05 +00004921 /*
4922 * stderr is only redirected when using the GUI, so that a
4923 * program like gpg can still access the terminal to get a
4924 * passphrase using stderr.
4925 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004926# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (pty_master_fd >= 0)
4928 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004929 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
Bram Moolenaar0f873732019-12-05 20:28:46 +01004931 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004933 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004935 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004936 if (gui.in_use)
4937 {
4938 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004939 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941
Bram Moolenaar0f873732019-12-05 20:28:46 +01004942 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004947 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 close(fd_toshell[1]);
4949 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004950 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 close(fd_toshell[0]);
4952
Bram Moolenaar0f873732019-12-05 20:28:46 +01004953 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 close(fd_fromshell[0]);
4955 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004956 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 close(fd_fromshell[1]);
4958
Bram Moolenaara5792f52005-11-23 21:25:05 +00004959# ifdef FEAT_GUI
4960 if (gui.in_use)
4961 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004962 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004963 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004964 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004965 }
4966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004969
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 /*
4971 * There is no type cast for the argv, because the type may be
4972 * different on different machines. This may cause a warning
4973 * message with strict compilers, don't worry about it.
4974 * Call _exit() instead of exit() to avoid closing the connection
4975 * to the X server (esp. with GTK, which uses atexit()).
4976 */
4977 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004978 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004980 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
4982 /*
4983 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004984 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 */
4986 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004987 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004988 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004989# ifdef FEAT_JOB_CHANNEL
4990 ++dont_check_job_ended;
4991# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 /*
4993 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004994 * This is also used to pipe stdin/stdout to/from the external
4995 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004997 if ((options & (SHELL_READ|SHELL_WRITE))
4998# ifdef FEAT_GUI
4999 || (gui.in_use && show_shell_mess)
5000# endif
5001 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005003# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01005005 int buffer_off = 0; // valid bytes in buffer[]
5006 char_u ta_buf[BUFLEN + 1]; // TypeAHead
5007 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 int len;
5009 int p_more_save;
5010 int old_State;
5011 int c;
5012 int toshell_fd;
5013 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005014 garray_T ga;
5015 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01005016# ifdef ELAPSED_FUNC
5017 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019
Bram Moolenaardf177f62005-02-22 08:39:57 +00005020# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 if (pty_master_fd >= 0)
5022 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 fromshell_fd = pty_master_fd;
5024 toshell_fd = dup(pty_master_fd);
5025 }
5026 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00005027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
5029 close(fd_toshell[0]);
5030 close(fd_fromshell[1]);
5031 toshell_fd = fd_toshell[1];
5032 fromshell_fd = fd_fromshell[0];
5033 }
5034
5035 /*
5036 * Write to the child if there are typed characters.
5037 * Read from the child if there are characters available.
5038 * Repeat the reading a few times if more characters are
5039 * available. Need to check for typed keys now and then, but
5040 * not too often (delays when no chars are available).
5041 * This loop is quit if no characters can be read from the pty
5042 * (WaitForChar detected special condition), or there are no
5043 * characters available and the child has exited.
5044 * Only check if the child has exited when there is no more
5045 * output. The child may exit before all the output has
5046 * been printed.
5047 *
5048 * Currently this busy loops!
5049 * This can probably dead-lock when the write blocks!
5050 */
5051 p_more_save = p_more;
5052 p_more = FALSE;
5053 old_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01005054 State = MODE_EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005056 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005057 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005058 // Fork a process that will write the lines to the
5059 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005060 if ((wpid = fork()) == -1)
5061 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005062 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005063 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005064 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00005065 {
5066 linenr_T lnum = curbuf->b_op_start.lnum;
5067 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005068 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005069 size_t l;
5070
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005071 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005072 for (;;)
5073 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005074 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005075 if (l == 0)
5076 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005077 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005078 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00005079 len = write(toshell_fd, "", (size_t)1);
5080 else
5081 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005082 char_u *s = vim_strchr(lp + written, NL);
5083
Bram Moolenaar89d40322006-08-29 15:30:07 +00005084 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00005085 s == NULL ? l
5086 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005087 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00005088 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005089 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005090 // Finished a line, add a NL, unless this line
5091 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005092 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005093 || (!curbuf->b_p_bin
5094 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005095 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaar88456cd2022-11-18 22:14:09 +00005096 && (lnum != curbuf->b_ml.ml_line_count
Bram Moolenaardf177f62005-02-22 08:39:57 +00005097 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005098 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005099 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005100 ++lnum;
5101 if (lnum > curbuf->b_op_end.lnum)
5102 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005103 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005104 close(toshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005105 break;
5106 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005107 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005108 written = 0;
5109 }
5110 else if (len > 0)
5111 written += len;
5112 }
5113 _exit(0);
5114 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005115 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005116 {
5117 close(toshell_fd);
5118 toshell_fd = -1;
5119 }
5120 }
5121
5122 if (options & SHELL_READ)
5123 ga_init2(&ga, 1, BUFLEN);
5124
5125 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005126# ifdef ELAPSED_FUNC
5127 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 for (;;)
5130 {
5131 /*
5132 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005133 * if there are any.
5134 * Don't do this if we are expanding wild cards (would eat
5135 * typeahead).
5136 * Don't do this when filtering and terminal is in cooked
5137 * mode, the shell command will handle the I/O. Avoids
5138 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005139 * Don't get characters when the child has already
5140 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005141 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005142 * while (noread_cnt > 4), avoids that ":r !ls" eats
5143 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 */
5145 len = 0;
5146 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005147 && ((options &
5148 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5149 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005150# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005151 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005152# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005153 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005154 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005155 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005157 if (ta_len == 0)
5158 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005159 // Get extra characters when we don't have any.
5160 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005161 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005162# ifdef ELAPSED_FUNC
5163 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005164# endif
5165 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5166 }
5167 if (ta_len > 0 || len > 0)
5168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 /*
5170 * For pipes:
5171 * Check for CTRL-C: send interrupt signal to child.
5172 * Check for CTRL-D: EOF, close pipe to child.
5173 */
5174 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 /*
5177 * Send SIGINT to the child's group or all
5178 * processes in our group.
5179 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005180 may_send_sigint(ta_buf[ta_len], pid, wpid);
5181
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 if (pty_master_fd < 0 && toshell_fd >= 0
5183 && ta_buf[ta_len] == Ctrl_D)
5184 {
5185 close(toshell_fd);
5186 toshell_fd = -1;
5187 }
5188 }
5189
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01005190 // Remove Vim-specific codes from the input.
5191 len = term_replace_keycodes(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192
5193 /*
5194 * For pipes: echo the typed characters.
5195 * For a pty this does not seem to work.
5196 */
5197 if (pty_master_fd < 0)
5198 {
5199 for (i = ta_len; i < ta_len + len; ++i)
5200 {
5201 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5202 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 else if (has_mbyte)
5204 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005205 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
5207 msg_outtrans_len(ta_buf + i, l);
5208 i += l - 1;
5209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 else
5211 msg_outtrans_len(ta_buf + i, 1);
5212 }
5213 windgoto(msg_row, msg_col);
5214 out_flush();
5215 }
5216
5217 ta_len += len;
5218
5219 /*
5220 * Write the characters to the child, unless EOF has
5221 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005222 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005223 * When writing buffer lines, drop the typed
5224 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005226 if (options & SHELL_WRITE)
5227 ta_len = 0;
5228 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
5230 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5231 if (len > 0)
5232 {
5233 ta_len -= len;
5234 mch_memmove(ta_buf, ta_buf + len, ta_len);
5235 }
5236 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
5239
Bram Moolenaardf177f62005-02-22 08:39:57 +00005240 if (got_int)
5241 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005242 // CTRL-C sends a signal to the child, we ignore it
5243 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005244# ifdef HAVE_SETSID
5245 kill(-pid, SIGINT);
5246# else
5247 kill(0, SIGINT);
5248# endif
5249 if (wpid > 0)
5250 kill(wpid, SIGINT);
5251 got_int = FALSE;
5252 }
5253
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 /*
5255 * Check if the child has any characters to be printed.
5256 * Read them and write them to our window. Repeat this as
5257 * long as there is something to do, avoid the 10ms wait
5258 * for mch_inchar(), or sending typeahead characters to
5259 * the external process.
5260 * TODO: This should handle escape sequences, compatible
5261 * to some terminal (vt52?).
5262 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005263 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005264 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005266 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005269 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005271
5272 noread_cnt = 0;
5273 if (options & SHELL_READ)
5274 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005275 // Do NUL -> NL translation, append NL separated
5276 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005277 for (i = 0; i < len; ++i)
5278 {
5279 if (buffer[i] == NL)
5280 append_ga_line(&ga);
5281 else if (buffer[i] == NUL)
5282 ga_append(&ga, NL);
5283 else
5284 ga_append(&ga, buffer[i]);
5285 }
5286 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005287 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 {
5289 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005290 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
Bram Moolenaardf177f62005-02-22 08:39:57 +00005292 len += buffer_off;
5293 buffer[len] = NUL;
5294
Bram Moolenaar0f873732019-12-05 20:28:46 +01005295 // Check if the last character in buffer[] is
5296 // incomplete, keep these bytes for the next
5297 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 for (p = buffer; p < buffer + len; p += l)
5299 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005300 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005302 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 else if (MB_BYTE2LEN(*p) != l)
5304 break;
5305 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005306 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005308 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 if (len >= 12)
5310 ++p;
5311 else
5312 {
5313 buffer_off = len;
5314 continue;
5315 }
5316 }
5317 c = *p;
5318 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005319 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 if (p < buffer + len)
5321 {
5322 *p = c;
5323 buffer_off = (buffer + len) - p;
5324 mch_memmove(buffer, p, buffer_off);
5325 continue;
5326 }
5327 buffer_off = 0;
5328 }
5329 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
5331 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005332 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 }
5334
5335 windgoto(msg_row, msg_col);
5336 cursor_on();
5337 out_flush();
5338 if (got_int)
5339 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005340
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005341# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005342 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005343 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005344 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005345
Bram Moolenaar0f873732019-12-05 20:28:46 +01005346 // Avoid that we keep looping here without
5347 // checking for a CTRL-C for a long time. Don't
5348 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005349 if (msec > 2000)
5350 {
5351 noread_cnt = 5;
5352 break;
5353 }
5354 }
5355# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
5357
Bram Moolenaar0f873732019-12-05 20:28:46 +01005358 // If we already detected the child has finished, continue
5359 // reading output for a short while. Some text may be
5360 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005361 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005362 {
5363 if (noread_cnt < 5)
5364 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005365 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005366 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005367
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 /*
5369 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005370 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005372# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005373 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005374# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5378 || (wait_pid == pid && WIFEXITED(status)))
5379 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005380 // Don't break the loop yet, try reading more
5381 // characters from "fromshell_fd" first. When using
5382 // pipes there might still be something to read and
5383 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005386 else
5387 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005388
Bram Moolenaar95a51352013-03-21 22:53:50 +01005389# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005390 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005391 clip_update();
5392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
5394finished:
5395 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005396 if (options & SHELL_READ)
5397 {
5398 if (ga.ga_len > 0)
5399 {
5400 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005401 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005402 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005403 }
5404 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005405 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005406 ga_clear(&ga);
5407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 /*
5410 * Give all typeahead that wasn't used back to ui_inchar().
5411 */
5412 if (ta_len)
5413 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 State = old_State;
5415 if (toshell_fd >= 0)
5416 close(toshell_fd);
5417 close(fromshell_fd);
5418 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005419# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005420 else
5421 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005422 long delay_msec = 1;
5423
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005424 if (tmode == TMODE_RAW)
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005425 // Possibly disables modifyOtherKeys, so that the system
5426 // can recognize CTRL-C.
5427 out_str_t_TE();
Bram Moolenaar0981c872020-08-23 14:28:37 +02005428
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005429 /*
5430 * Similar to the loop above, but only handle X events, no
5431 * I/O.
5432 */
5433 for (;;)
5434 {
5435 if (got_int)
5436 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005437 // CTRL-C sends a signal to the child, we ignore it
5438 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005439# ifdef HAVE_SETSID
5440 kill(-pid, SIGINT);
5441# else
5442 kill(0, SIGINT);
5443# endif
5444 got_int = FALSE;
5445 }
5446# ifdef __NeXT__
5447 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5448# else
5449 wait_pid = waitpid(pid, &status, WNOHANG);
5450# endif
5451 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5452 || (wait_pid == pid && WIFEXITED(status)))
5453 {
5454 wait_pid = pid;
5455 break;
5456 }
5457
Bram Moolenaar0f873732019-12-05 20:28:46 +01005458 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005459 clip_update();
5460
Bram Moolenaar0f873732019-12-05 20:28:46 +01005461 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005462 // less time, gradually wait longer.
5463 mch_delay(delay_msec,
5464 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005465 if (++delay_msec > 10)
5466 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005467 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005468
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005469 if (tmode == TMODE_RAW)
5470 // possibly enables modifyOtherKeys again
Bram Moolenaar733a69b2022-12-01 12:03:47 +00005471 out_str_t_TI();
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005472 }
5473# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
5475 /*
5476 * Wait until our child has exited.
5477 * Ignore wait() returning pids of other children and returning
5478 * because of some signal like SIGWINCH.
5479 * Don't wait if wait_pid was already set above, indicating the
5480 * child already exited.
5481 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005482 if (wait_pid != pid)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01005483 (void)wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
Bram Moolenaar624891f2010-10-13 16:22:09 +02005485# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005486 // Close slave side of pty. Only do this after the child has
5487 // exited, otherwise the child may hang when it tries to write on
5488 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005489 if (pty_master_fd >= 0)
5490 close(pty_slave_fd);
5491# endif
5492
Bram Moolenaar0f873732019-12-05 20:28:46 +01005493 // Make sure the child that writes to the external program is
5494 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005495 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005496 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005497 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005498 wait4pid(wpid, NULL);
5499 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005500
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005501# ifdef FEAT_JOB_CHANNEL
5502 --dont_check_job_ended;
5503# endif
5504
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 /*
5506 * Set to raw mode right now, otherwise a CTRL-C after
5507 * catch_signals() will kill Vim.
5508 */
5509 if (tmode == TMODE_RAW)
5510 settmode(TMODE_RAW);
5511 did_settmode = TRUE;
5512 set_signals();
5513
5514 if (WIFEXITED(status))
5515 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005516 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005518 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 {
5520 if (retval == EXEC_FAILED)
5521 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005522 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 msg_outtrans(p_sh);
5524 msg_putchar('\n');
5525 }
5526 else if (!(options & SHELL_SILENT))
5527 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005528 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 msg_outnum((long)retval);
5530 msg_putchar('\n');
5531 }
5532 }
5533 }
5534 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005535 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 }
5537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538
5539error:
5540 if (!did_settmode)
5541 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005542 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005544 vim_free(argv);
5545 vim_free(tofree1);
5546 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
5548 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005549}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005550#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005551
5552 int
5553mch_call_shell(
5554 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005555 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005556{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005557#ifdef FEAT_EVAL
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01005558 ch_log(NULL, "executing shell command: %s", cmd);
5559#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005560#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar524c8532022-09-27 15:48:20 +01005561 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL
5562 && (options & SHELL_SILENT) == 0)
Bram Moolenaar13568252018-03-16 20:46:58 +01005563 return mch_call_shell_terminal(cmd, options);
5564#endif
5565#ifdef USE_SYSTEM
5566 return mch_call_shell_system(cmd, options);
5567#else
5568 return mch_call_shell_fork(cmd, options);
5569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570}
5571
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005572#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005573 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005574mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005575{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005576 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005577 int fd_in[2] = {-1, -1}; // for stdin
5578 int fd_out[2] = {-1, -1}; // for stdout
5579 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005580 int pty_master_fd = -1;
5581 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005582 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005583 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5584 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5585 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005586 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005587 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5588 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005589 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005590 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005591 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005592
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005593 if (use_out_for_err && use_null_for_out)
5594 use_null_for_err = TRUE;
5595
Bram Moolenaar0f873732019-12-05 20:28:46 +01005596 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005597 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005598
Bram Moolenaarb2412082017-08-20 18:09:14 +02005599 if (options->jo_pty
5600 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005601 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005602 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005603 open_pty(&pty_master_fd, &pty_slave_fd,
5604 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005605
Bram Moolenaar0f873732019-12-05 20:28:46 +01005606 // TODO: without the channel feature connect the child to /dev/null?
5607 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005608 if (use_file_for_in)
5609 {
5610 char_u *fname = options->jo_io_name[PART_IN];
5611
5612 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5613 if (fd_in[0] < 0)
5614 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005615 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005616 goto failed;
5617 }
5618 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005619 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005620 // When writing buffer lines to the input don't use the pty, so that
5621 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005622 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5623 && pipe(fd_in) < 0)
5624 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005625
5626 if (use_file_for_out)
5627 {
5628 char_u *fname = options->jo_io_name[PART_OUT];
5629
5630 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5631 if (fd_out[1] < 0)
5632 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005633 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005634 goto failed;
5635 }
5636 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005637 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005638 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005639
5640 if (use_file_for_err)
5641 {
5642 char_u *fname = options->jo_io_name[PART_ERR];
5643
5644 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5645 if (fd_err[1] < 0)
5646 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005647 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005648 goto failed;
5649 }
5650 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005651 else if (!use_out_for_err && !use_null_for_err
5652 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005653 goto failed;
5654
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005655 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5656 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005657 if (options->jo_set & JO_CHANNEL)
5658 {
5659 channel = options->jo_channel;
5660 if (channel != NULL)
5661 ++channel->ch_refcount;
5662 }
5663 else
5664 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005665 if (channel == NULL)
5666 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005667 if (job->jv_tty_out != NULL)
5668 ch_log(channel, "using pty %s on fd %d",
5669 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005670 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005671
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005672 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005673 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005674 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005675 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005676 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005677 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005678 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005679 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005680 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005681 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005682 int null_fd = -1;
5683 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005684
Bram Moolenaar0f873732019-12-05 20:28:46 +01005685 // child
5686 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005687 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005688
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005689# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01005690 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005691 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005692 ch_logfile((char_u *)"", (char_u *)"");
5693# endif
5694
Bram Moolenaar835dc632016-02-07 14:27:38 +01005695# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005696 // Create our own process group, so that the child and all its
5697 // children can be kill()ed. Don't do this when using pipes,
5698 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005699 (void)setsid();
5700# endif
5701
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005702# ifdef FEAT_TERMINAL
5703 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005704 {
5705 char *term = (char *)T_NAME;
5706
5707#ifdef FEAT_GUI
5708 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005709 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005710 term = getenv("TERM");
5711#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005712 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005713 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005714 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005715 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005716 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005717 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005718 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005719 else if (t_colors > 16)
5720 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005721 else
5722 term = "xterm";
5723 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005724 set_child_environment(
5725 (long)options->jo_term_rows,
5726 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005727 term,
5728 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005729 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005730 else
5731# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005732 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005733
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005734 if (options->jo_env != NULL)
5735 {
5736 dict_T *dict = options->jo_env;
5737 hashitem_T *hi;
5738 int todo = (int)dict->dv_hashtab.ht_used;
5739
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005740 FOR_ALL_HASHTAB_ITEMS(&dict->dv_hashtab, hi, todo)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005741 if (!HASHITEM_EMPTY(hi))
5742 {
5743 typval_T *item = &dict_lookup(hi)->di_tv;
5744
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005745 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005746 --todo;
5747 }
5748 }
5749
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005750 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005751 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005752 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005753 if (null_fd < 0)
5754 {
5755 perror("opening /dev/null failed");
5756 _exit(OPEN_NULL_FAILED);
5757 }
5758 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005759
Bram Moolenaar223896d2017-08-02 22:33:28 +02005760 if (pty_slave_fd >= 0)
5761 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005762 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005763 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005764# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005765 // Try to become controlling tty (probably doesn't work,
5766 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005767 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5768# endif
5769 }
5770
Bram Moolenaar0f873732019-12-05 20:28:46 +01005771 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005772 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005773 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005774 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005775 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005776 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005777 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005778 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005779
Bram Moolenaar0f873732019-12-05 20:28:46 +01005780 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005781 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005782 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005783 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005784 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005785 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005786 }
5787 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005788 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005789 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005790 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005791 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005792 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005793
Bram Moolenaar0f873732019-12-05 20:28:46 +01005794 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005795 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005796 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005797 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005798 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005799 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005800 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005801 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005802
5803 if (fd_in[0] >= 0)
5804 close(fd_in[0]);
5805 if (fd_in[1] >= 0)
5806 close(fd_in[1]);
5807 if (fd_out[0] >= 0)
5808 close(fd_out[0]);
5809 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005810 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005811 if (fd_err[0] >= 0)
5812 close(fd_err[0]);
5813 if (fd_err[1] >= 0)
5814 close(fd_err[1]);
5815 if (pty_master_fd >= 0)
5816 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005817 close(pty_master_fd); // not used in the child
5818 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005819 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005820
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005821 if (null_fd >= 0)
5822 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005823
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005824 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5825 _exit(EXEC_FAILED);
5826
Bram Moolenaar0f873732019-12-05 20:28:46 +01005827 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005828 execvp(argv[0], argv);
5829
Bram Moolenaar4694a172016-04-21 14:05:23 +02005830 if (stderr_works)
5831 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005832# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005833 // calling free_all_mem() here causes problems. Ignore valgrind
5834 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005835# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005836 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005837 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005838
Bram Moolenaar0f873732019-12-05 20:28:46 +01005839 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005840 UNBLOCK_SIGNALS(&curset);
5841
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005842 job->jv_pid = pid;
5843 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005844 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005845
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005846 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005847 close(pty_slave_fd); // not used in the parent
5848 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005849 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005850 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005851 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005852 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005853 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005854 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005855 if (channel != NULL)
5856 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005857 int in_fd = INVALID_FD;
5858 int out_fd = INVALID_FD;
5859 int err_fd = INVALID_FD;
5860
5861 if (!(use_file_for_in || use_null_for_in))
5862 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5863
5864 if (!(use_file_for_out || use_null_for_out))
5865 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5866
5867 // When using pty_master_fd only set it for stdout, do not duplicate
5868 // it for stderr, it only needs to be read once.
5869 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5870 {
5871 if (fd_err[0] >= 0)
5872 err_fd = fd_err[0];
5873 else if (out_fd != pty_master_fd)
5874 err_fd = pty_master_fd;
5875 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005876
5877 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005878 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005879 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005880 else
5881 {
5882 if (fd_in[1] >= 0)
5883 close(fd_in[1]);
5884 if (fd_out[0] >= 0)
5885 close(fd_out[0]);
5886 if (fd_err[0] >= 0)
5887 close(fd_err[0]);
5888 if (pty_master_fd >= 0)
5889 close(pty_master_fd);
5890 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005891
Bram Moolenaar0f873732019-12-05 20:28:46 +01005892 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005893 return;
5894
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005895failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005896 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005897 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005898 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005899 if (fd_in[1] >= 0)
5900 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005901 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005902 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005903 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005904 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005905 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005906 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005907 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005908 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005909 if (pty_master_fd >= 0)
5910 close(pty_master_fd);
5911 if (pty_slave_fd >= 0)
5912 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005913}
5914
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005915 static char_u *
5916get_signal_name(int sig)
5917{
5918 int i;
5919 char_u numbuf[NUMBUFLEN];
5920
5921 if (sig == SIGKILL)
5922 return vim_strsave((char_u *)"kill");
5923
5924 for (i = 0; signal_info[i].sig != -1; i++)
5925 if (sig == signal_info[i].sig)
5926 return strlow_save((char_u *)signal_info[i].name);
5927
5928 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5929 return vim_strsave(numbuf);
5930}
5931
Bram Moolenaar835dc632016-02-07 14:27:38 +01005932 char *
5933mch_job_status(job_T *job)
5934{
5935# ifdef HAVE_UNION_WAIT
5936 union wait status;
5937# else
5938 int status = -1;
5939# endif
5940 pid_t wait_pid = 0;
5941
5942# ifdef __NeXT__
5943 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5944# else
5945 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5946# endif
5947 if (wait_pid == -1)
5948 {
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00005949 int waitpid_errno = errno;
5950 if (waitpid_errno == ECHILD && mch_process_running(job->jv_pid))
5951 // The process is alive, but it was probably reparented (for
5952 // example by ptrace called by a debugger like lldb or gdb).
5953 // Note: This assumes that process IDs are not reused.
5954 return "run";
5955
Bram Moolenaar0f873732019-12-05 20:28:46 +01005956 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005957 if (job->jv_status < JOB_ENDED)
5958 ch_log(job->jv_channel, "Job no longer exists: %s",
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00005959 strerror(waitpid_errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005960 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005961 }
5962 if (wait_pid == 0)
5963 return "run";
5964 if (WIFEXITED(status))
5965 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005966 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005967 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005968 if (job->jv_status < JOB_ENDED)
5969 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005970 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005971 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005972 if (WIFSIGNALED(status))
5973 {
5974 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005975 job->jv_termsig = get_signal_name(WTERMSIG(status));
5976 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5977 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5978 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005979 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005980 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005981 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005982
5983return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005984 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005985 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005986 return "dead";
5987}
5988
5989 job_T *
5990mch_detect_ended_job(job_T *job_list)
5991{
5992# ifdef HAVE_UNION_WAIT
5993 union wait status;
5994# else
5995 int status = -1;
5996# endif
5997 pid_t wait_pid = 0;
5998 job_T *job;
5999
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01006000# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006001 // Do not do this when waiting for a shell command to finish, we would get
6002 // the exit value here (and discard it), the exit value obtained there
6003 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01006004 if (dont_check_job_ended > 0)
6005 return NULL;
6006# endif
6007
Bram Moolenaar97792de2016-10-15 18:36:49 +02006008# ifdef __NeXT__
6009 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
6010# else
6011 wait_pid = waitpid(-1, &status, WNOHANG);
6012# endif
6013 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006014 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02006015 return NULL;
6016 for (job = job_list; job != NULL; job = job->jv_next)
6017 {
6018 if (job->jv_pid == wait_pid)
6019 {
6020 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006021 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02006022 job->jv_exitval = WEXITSTATUS(status);
6023 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006024 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02006025 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006026 job->jv_termsig = get_signal_name(WTERMSIG(status));
6027 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01006028 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02006029 {
6030 ch_log(job->jv_channel, "Job ended");
6031 job->jv_status = JOB_ENDED;
6032 }
6033 return job;
6034 }
6035 }
6036 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006037}
6038
Bram Moolenaar3a117e12016-10-30 21:57:52 +01006039/*
6040 * Send a (deadly) signal to "job".
6041 * Return FAIL if "how" is not a valid name.
6042 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01006043 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006044mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01006045{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006046 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006047
Bram Moolenaar923d9262016-02-25 20:56:01 +01006048 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01006049 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01006050 else if (STRCMP(how, "hup") == 0)
6051 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006052 else if (STRCMP(how, "quit") == 0)
6053 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01006054 else if (STRCMP(how, "int") == 0)
6055 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006056 else if (STRCMP(how, "kill") == 0)
6057 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02006058#ifdef SIGWINCH
6059 else if (STRCMP(how, "winch") == 0)
6060 sig = SIGWINCH;
6061#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006062 else if (isdigit(*how))
6063 sig = atoi((char *)how);
6064 else
6065 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01006066
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01006067 // Never kill ourselves!
6068 if (job->jv_pid != 0)
6069 {
6070 // TODO: have an option to only kill the process, not the group?
6071 kill(-job->jv_pid, sig);
6072 kill(job->jv_pid, sig);
6073 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01006074
Bram Moolenaar835dc632016-02-07 14:27:38 +01006075 return OK;
6076}
Bram Moolenaar76467df2016-02-12 19:30:26 +01006077
6078/*
6079 * Clear the data related to "job".
6080 */
6081 void
6082mch_clear_job(job_T *job)
6083{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006084 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01006085# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006086 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006087# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006088 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006089# endif
6090}
Bram Moolenaar835dc632016-02-07 14:27:38 +01006091#endif
6092
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006093#if defined(FEAT_TERMINAL) || defined(PROTO)
6094 int
6095mch_create_pty_channel(job_T *job, jobopt_T *options)
6096{
6097 int pty_master_fd = -1;
6098 int pty_slave_fd = -1;
6099 channel_T *channel;
6100
Bram Moolenaar59386482019-02-10 22:43:46 +01006101 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02006102 if (pty_master_fd < 0 || pty_slave_fd < 0)
6103 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006104 close(pty_slave_fd);
6105
6106 channel = add_channel();
6107 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006108 {
6109 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006110 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006111 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006112 if (job->jv_tty_out != NULL)
6113 ch_log(channel, "using pty %s on fd %d",
6114 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006115 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006116 channel->ch_keep_open = TRUE;
6117
Bram Moolenaar0f873732019-12-05 20:28:46 +01006118 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6119 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006120 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006121 channel_set_job(channel, job, options);
6122 return OK;
6123}
6124#endif
6125
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126/*
6127 * Check for CTRL-C typed by reading all available characters.
6128 * In cooked mode we should get SIGINT, no need to check.
6129 */
6130 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006131mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006133 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006134 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 fill_input_buf(FALSE);
6136}
6137
6138/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006139 * Wait "msec" msec until a character is available from the mouse, keyboard,
6140 * from inbuf[].
6141 * "msec" == -1 will block forever.
6142 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006143 * When "ignore_input" is TRUE even check for pending input when input is
6144 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006145 * "interrupted" (if not NULL) is set to TRUE when no character is available
6146 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006147 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006148 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 */
6150 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006151WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006153#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006154 return ui_wait_for_chars_or_timer(
6155 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006156#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006157 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006158#endif
6159}
6160
6161/*
6162 * Wait "msec" msec until a character is available from the mouse or keyboard
6163 * or from inbuf[].
6164 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006165 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006166 * "interrupted" (if not NULL) is set to TRUE when no character is available
6167 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006168 * When a GUI is being used, this will never get called -- webb
6169 */
6170 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006171WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006172{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173#ifdef FEAT_MOUSE_GPM
6174 int gpm_process_wanted;
6175#endif
6176#ifdef FEAT_XCLIPBOARD
6177 int rest;
6178#endif
6179 int avail;
6180
Bram Moolenaar0f873732019-12-05 20:28:46 +01006181 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 return 1;
6183
6184#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006185 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 if (WantQueryMouse)
6187 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006188 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006189 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006190 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 }
6192#endif
6193
6194 /*
6195 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6196 * events. This is a bit complicated, because they might both be defined.
6197 */
6198#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6199# ifdef FEAT_XCLIPBOARD
6200 rest = 0;
6201 if (do_xterm_trace())
6202 rest = msec;
6203# endif
6204 do
6205 {
6206# ifdef FEAT_XCLIPBOARD
6207 if (rest != 0)
6208 {
6209 msec = XT_TRACE_DELAY;
6210 if (rest >= 0 && rest < XT_TRACE_DELAY)
6211 msec = rest;
6212 if (rest >= 0)
6213 rest -= msec;
6214 }
6215# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01006216# ifdef FEAT_SOUND_MACOSX
6217 // Invoke any pending sound callbacks.
6218 process_cfrunloop();
6219# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006220# ifdef FEAT_SOUND_CANBERRA
6221 // Invoke any pending sound callbacks.
6222 if (has_sound_callback_in_queue())
6223 invoke_sound_callback();
6224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225# ifdef FEAT_MOUSE_GPM
6226 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006227 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006228 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006229 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006231 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006235 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 return 1;
6237# ifdef FEAT_XCLIPBOARD
6238 if (rest == 0 || !do_xterm_trace())
6239# endif
6240 break;
6241 }
6242 }
6243 while (FALSE
6244# ifdef FEAT_MOUSE_GPM
6245 || (gpm_process_wanted && mch_gpm_process() == 0)
6246# endif
6247# ifdef FEAT_XCLIPBOARD
6248 || (!avail && rest != 0)
6249# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006250 )
6251 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
6253#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006254 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255#endif
6256 return avail;
6257}
6258
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006259#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260/*
6261 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006262 * "msec" == 0 will check for characters once.
6263 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006266 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006267 * "interrupted" (if not NULL) is set to TRUE when no character is available
6268 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006270 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006271RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272{
6273 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006274 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006275#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 static int busy = FALSE;
6277
Bram Moolenaar0f873732019-12-05 20:28:46 +01006278 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279# define MAY_LOOP
6280
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006281# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006282 // Remember at what time we started, so that we know how much longer we
6283 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006284 long start_msec = msec;
6285 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006287 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006288 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289# endif
6290
Bram Moolenaar0f873732019-12-05 20:28:46 +01006291 // Handle being called recursively. This may happen for the session
6292 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 if (busy)
6294 return 0;
6295#endif
6296
6297#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006298 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299#endif
6300 {
6301#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006302 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006303# ifdef FEAT_MZSCHEME
6304 int mzquantum_used = FALSE;
6305# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306#endif
6307#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006308 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006309 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 int nfd;
6311# ifdef FEAT_XCLIPBOARD
6312 int xterm_idx = -1;
6313# endif
6314# ifdef FEAT_MOUSE_GPM
6315 int gpm_idx = -1;
6316# endif
6317# ifdef USE_XSMP
6318 int xsmp_idx = -1;
6319# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006320 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006322# ifdef FEAT_MZSCHEME
6323 mzvim_check_threads();
6324 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6325 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006326 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006327 mzquantum_used = TRUE;
6328 }
6329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 fds[0].fd = fd;
6331 fds[0].events = POLLIN;
6332 nfd = 1;
6333
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006335 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 if (xterm_Shell != (Widget)0)
6337 {
6338 xterm_idx = nfd;
6339 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6340 fds[nfd].events = POLLIN;
6341 nfd++;
6342 }
6343# endif
6344# ifdef FEAT_MOUSE_GPM
6345 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6346 {
6347 gpm_idx = nfd;
6348 fds[nfd].fd = gpm_fd;
6349 fds[nfd].events = POLLIN;
6350 nfd++;
6351 }
6352# endif
6353# ifdef USE_XSMP
6354 if (xsmp_icefd != -1)
6355 {
6356 xsmp_idx = nfd;
6357 fds[nfd].fd = xsmp_icefd;
6358 fds[nfd].events = POLLIN;
6359 nfd++;
6360 }
6361# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006362#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006363 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006364#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006365 if (interrupted != NULL)
6366 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006368 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006369
6370 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006371 if (result == 0 && interrupted != NULL && ret > 0)
6372 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006373
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006374# ifdef FEAT_MZSCHEME
6375 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006376 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006377 finished = FALSE;
6378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380# ifdef FEAT_XCLIPBOARD
6381 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6382 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006383 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006385 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 finished = FALSE;
6387 }
6388# endif
6389# ifdef FEAT_MOUSE_GPM
6390 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392# endif
6393# ifdef USE_XSMP
6394 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6395 {
6396 if (fds[xsmp_idx].revents & POLLIN)
6397 {
6398 busy = TRUE;
6399 xsmp_handle_requests();
6400 busy = FALSE;
6401 }
6402 else if (fds[xsmp_idx].revents & POLLHUP)
6403 {
6404 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006405 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 xsmp_close();
6407 }
6408 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006409 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 }
6411# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006412#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006413 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006414 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006415 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417
Bram Moolenaar0f873732019-12-05 20:28:46 +01006418#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
6420 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006421 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006422 // These are static because they can take 8 Kbyte each and cause the
6423 // signal stack to run out with -O3.
6424 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006426 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006428# ifdef FEAT_MZSCHEME
6429 mzvim_check_threads();
6430 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6431 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006432 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006433 mzquantum_used = TRUE;
6434 }
6435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006437 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006439 tv.tv_sec = towait / 1000;
6440 tv.tv_usec = (towait % 1000) * (1000000/1000);
6441 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006443 else
6444 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445
6446 /*
6447 * Select on ready for reading and exceptional condition (end of file).
6448 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006449select_eintr:
6450 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006451 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 FD_ZERO(&efds);
6453 FD_SET(fd, &rfds);
K.Takatab247e062022-02-07 10:45:23 +00006454# ifndef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006455 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 FD_SET(fd, &efds);
6457# endif
6458 maxfd = fd;
6459
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006461 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 if (xterm_Shell != (Widget)0)
6463 {
6464 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6465 if (maxfd < ConnectionNumber(xterm_dpy))
6466 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006467
Bram Moolenaar0f873732019-12-05 20:28:46 +01006468 // An event may have already been read but not handled. In
6469 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006470 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 }
6472# endif
6473# ifdef FEAT_MOUSE_GPM
6474 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6475 {
6476 FD_SET(gpm_fd, &rfds);
6477 FD_SET(gpm_fd, &efds);
6478 if (maxfd < gpm_fd)
6479 maxfd = gpm_fd;
6480 }
6481# endif
6482# ifdef USE_XSMP
6483 if (xsmp_icefd != -1)
6484 {
6485 FD_SET(xsmp_icefd, &rfds);
6486 FD_SET(xsmp_icefd, &efds);
6487 if (maxfd < xsmp_icefd)
6488 maxfd = xsmp_icefd;
6489 }
6490# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006491# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006492 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006493# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006494 if (interrupted != NULL)
6495 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496
Bram Moolenaar643b6142018-09-12 20:29:09 +02006497 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6498 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006499 result = ret > 0 && FD_ISSET(fd, &rfds);
6500 if (result)
6501 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006502 else if (interrupted != NULL && ret > 0)
6503 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006504
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006505# ifdef EINTR
6506 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006507 {
dbivolaruab16ad32021-12-29 19:41:47 +00006508 // Check whether the EINTR is caused by SIGTSTP
6509 if (got_tstp && !in_mch_suspend)
6510 {
6511 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006512
dbivolaruab16ad32021-12-29 19:41:47 +00006513 ea.forceit = TRUE;
6514 ex_stop(&ea);
6515 got_tstp = FALSE;
6516 }
6517
Bram Moolenaar0f873732019-12-05 20:28:46 +01006518 // Check whether window has been resized, EINTR may be caused by
6519 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006520 if (do_resize)
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006521 {
6522 ch_log(NULL, "calling handle_resize() in RealWaitForChar()");
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006523 handle_resize();
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006524 }
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006525
Bram Moolenaar0f873732019-12-05 20:28:46 +01006526 // Interrupted by a signal, need to try again. We ignore msec
6527 // here, because we do want to check even after a timeout if
6528 // characters are available. Needed for reading output of an
6529 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006530 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006531 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006532# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006533# ifdef __TANDEM
6534 if (ret == -1 && errno == ENOTSUP)
6535 {
6536 FD_ZERO(&rfds);
6537 FD_ZERO(&efds);
6538 ret = 0;
6539 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006540# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006541# ifdef FEAT_MZSCHEME
6542 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006543 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006544 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545# endif
6546
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547# ifdef FEAT_XCLIPBOARD
6548 if (ret > 0 && xterm_Shell != (Widget)0
6549 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6550 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006551 xterm_update(); // Maybe we should hand out clipboard
6552 // continue looping when we only got the X event and the input
6553 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 if (--ret == 0 && !input_available())
6555 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006556 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 finished = FALSE;
6558 }
6559 }
6560# endif
6561# ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00006562 if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 {
6564 if (FD_ISSET(gpm_fd, &efds))
6565 gpm_close();
6566 else if (FD_ISSET(gpm_fd, &rfds))
6567 *check_for_gpm = 1;
6568 }
6569# endif
6570# ifdef USE_XSMP
6571 if (ret > 0 && xsmp_icefd != -1)
6572 {
6573 if (FD_ISSET(xsmp_icefd, &efds))
6574 {
6575 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006576 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 xsmp_close();
6578 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006579 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 }
6581 else if (FD_ISSET(xsmp_icefd, &rfds))
6582 {
6583 busy = TRUE;
6584 xsmp_handle_requests();
6585 busy = FALSE;
6586 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006587 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 }
6589 }
6590# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006591#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006592 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006593 if (ret >= 0)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01006594 (void)channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596
Bram Moolenaar0f873732019-12-05 20:28:46 +01006597#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598
6599#ifdef MAY_LOOP
6600 if (finished || msec == 0)
6601 break;
6602
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006603# ifdef FEAT_CLIENTSERVER
6604 if (server_waiting())
6605 break;
6606# endif
6607
Bram Moolenaar0f873732019-12-05 20:28:46 +01006608 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 if (msec > 0)
6610 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006611# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006612 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006613 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006615 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 msec = msec / 2;
6617# endif
6618 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006619 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 }
6621#endif
6622 }
6623
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006624 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625}
6626
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006628 * Expand a path into all matching files and/or directories. Handles "*",
6629 * "?", "[a-z]", "**", etc.
6630 * "path" has backslashes before chars that are not to be expanded.
6631 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 */
6633 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006634mch_expandpath(
6635 garray_T *gap,
6636 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006637 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638{
Bram Moolenaar02743632005-07-25 20:42:36 +00006639 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641
6642/*
6643 * mch_expand_wildcards() - this code does wild-card pattern matching using
6644 * the shell
6645 *
6646 * return OK for success, FAIL for error (you may lose some memory) and put
6647 * an error message in *file.
6648 *
6649 * num_pat is number of input patterns
6650 * pat is array of pointers to input patterns
6651 * num_file is pointer to number of matched file names
6652 * file is pointer to array of pointers to matched file names
6653 */
6654
6655#ifndef SEEK_SET
6656# define SEEK_SET 0
6657#endif
6658#ifndef SEEK_END
6659# define SEEK_END 2
6660#endif
6661
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006662#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006663
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006665mch_expand_wildcards(
6666 int num_pat,
6667 char_u **pat,
6668 int *num_file,
6669 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006670 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 int i;
6673 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006674 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 char_u *p;
6676 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677
Bram Moolenaarc7247912008-01-13 12:54:11 +00006678 /*
6679 * This is the non-OS/2 implementation (really Unix).
6680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 int j;
6682 char_u *tempname;
6683 char_u *command;
6684 FILE *fd;
6685 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006686#define STYLE_ECHO 0 // use "echo", the default
6687#define STYLE_GLOB 1 // use "glob", for csh
6688#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6689#define STYLE_PRINT 3 // use "print -N", for zsh
6690#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6691 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 int shell_style = STYLE_ECHO;
6693 int check_spaces;
6694 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006695 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006696 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006697 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698
Bram Moolenaar0f873732019-12-05 20:28:46 +01006699 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 *file = NULL;
6701
6702 /*
6703 * If there are no wildcards, just copy the names to allocated memory.
6704 * Saves a lot of time, because we don't have to start a new shell.
6705 */
6706 if (!have_wildcard(num_pat, pat))
6707 return save_patterns(num_pat, pat, num_file, file);
6708
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006709# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006710 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006711 if (sandbox != 0 && check_secure())
6712 return FAIL;
6713# endif
6714
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 /*
6716 * Don't allow the use of backticks in secure and restricted mode.
6717 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006718 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 for (i = 0; i < num_pat; ++i)
6720 if (vim_strchr(pat[i], '`') != NULL
6721 && (check_restricted() || check_secure()))
6722 return FAIL;
6723
6724 /*
6725 * get a name for the temp file
6726 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006727 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006729 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 return FAIL;
6731 }
6732
6733 /*
6734 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006735 * file.
6736 * STYLE_BT: NL separated
6737 * If expanding `cmd` execute it directly.
6738 * STYLE_GLOB: NUL separated
6739 * If we use *csh, "glob" will work better than "echo".
6740 * STYLE_PRINT: NL or NUL separated
6741 * If we use *zsh, "print -N" will work better than "glob".
6742 * STYLE_VIMGLOB: NL separated
6743 * If we use *sh*, we define "vimglob()".
6744 * STYLE_ECHO: space separated.
6745 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 */
6747 if (num_pat == 1 && *pat[0] == '`'
6748 && (len = STRLEN(pat[0])) > 2
6749 && *(pat[0] + len - 1) == '`')
6750 shell_style = STYLE_BT;
6751 else if ((len = STRLEN(p_sh)) >= 3)
6752 {
6753 if (STRCMP(p_sh + len - 3, "csh") == 0)
6754 shell_style = STYLE_GLOB;
6755 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6756 shell_style = STYLE_PRINT;
6757 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006758 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6759 "sh") != NULL)
6760 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761
Bram Moolenaar0f873732019-12-05 20:28:46 +01006762 // Compute the length of the command. We need 2 extra bytes: for the
6763 // optional '&' and for the NUL.
6764 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006766 if (shell_style == STYLE_VIMGLOB)
6767 len += STRLEN(sh_vimglob_func);
6768
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006769 for (i = 0; i < num_pat; ++i)
6770 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006771 // Count the length of the patterns in the same way as they are put in
6772 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006773#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006774 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006775#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006776 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006777 for (j = 0; pat[i][j] != NUL; ++j)
6778 {
6779 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006780 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006781 ++len;
6782 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006783#endif
6784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 command = alloc(len);
6786 if (command == NULL)
6787 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006788 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 vim_free(tempname);
6790 return FAIL;
6791 }
6792
6793 /*
6794 * Build the shell command:
6795 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6796 * recognizes this).
6797 * - Add the shell command to print the expanded names.
6798 * - Add the temp file name.
6799 * - Add the file name patterns.
6800 */
6801 if (shell_style == STYLE_BT)
6802 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006803 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006804 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006805 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006807 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006808 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006810 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006812 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 *p = ' ';
6814 }
6815 STRCAT(command, ">");
6816 }
6817 else
6818 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006819 STRCPY(command, "");
6820 if (shell_style == STYLE_GLOB)
6821 {
6822 // Assume the nonomatch option is valid only for csh like shells,
6823 // otherwise, this may set the positional parameters for the shell,
6824 // e.g. "$*".
6825 if (flags & EW_NOTFOUND)
6826 STRCAT(command, "set nonomatch; ");
6827 else
6828 STRCAT(command, "unset nonomatch; ");
6829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 if (shell_style == STYLE_GLOB)
6831 STRCAT(command, "glob >");
6832 else if (shell_style == STYLE_PRINT)
6833 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006834 else if (shell_style == STYLE_VIMGLOB)
6835 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 else
6837 STRCAT(command, "echo >");
6838 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006839
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006841
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 if (shell_style != STYLE_BT)
6843 for (i = 0; i < num_pat; ++i)
6844 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006845 // When using system() always add extra quotes, because the shell
6846 // is started twice. Otherwise put a backslash before special
6847 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848#ifdef USE_SYSTEM
6849 STRCAT(command, " \"");
6850 STRCAT(command, pat[i]);
6851 STRCAT(command, "\"");
6852#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006853 int intick = FALSE;
6854
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 p = command + STRLEN(command);
6856 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006857 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006858 {
6859 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006860 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006861 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6862 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006863 // Remove a backslash, take char literally. But keep
6864 // backslash inside backticks, before a special character
6865 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006866 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006867 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6868 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006869 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006870 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006871 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006872 else if (!intick
6873 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6874 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006875 // Put a backslash before a special character, but not
6876 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6877 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006878 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006879
Bram Moolenaar0f873732019-12-05 20:28:46 +01006880 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006881 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 *p = NUL;
6884#endif
6885 }
6886 if (flags & EW_SILENT)
6887 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006888 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006889 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890
6891 /*
6892 * Using zsh -G: If a pattern has no matches, it is just deleted from
6893 * the argument list, otherwise zsh gives an error message and doesn't
6894 * expand any other pattern.
6895 */
6896 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006897 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898
6899 /*
6900 * If we use -f then shell variables set in .cshrc won't get expanded.
6901 * vi can do it, so we will too, but it is only necessary if there is a "$"
6902 * in one of the patterns, otherwise we can still use the fast option.
6903 */
6904 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006905 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906
6907 /*
6908 * execute the shell command
6909 */
6910 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6911
Bram Moolenaar0f873732019-12-05 20:28:46 +01006912 // When running in the background, give it some time to create the temp
6913 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006914 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006915 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916
Bram Moolenaar0f873732019-12-05 20:28:46 +01006917 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 show_shell_mess = TRUE;
6919 vim_free(command);
6920
Bram Moolenaar0f873732019-12-05 20:28:46 +01006921 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 {
6923 mch_remove(tempname);
6924 vim_free(tempname);
6925 /*
6926 * With interactive completion, the error message is not printed.
6927 * However with USE_SYSTEM, I don't know how to turn off error messages
6928 * from the shell, so screen may still get messed up -- webb.
6929 */
6930#ifndef USE_SYSTEM
6931 if (!(flags & EW_SILENT))
6932#endif
6933 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006934 redraw_later_clear(); // probably messed up screen
6935 msg_putchar('\n'); // clear bottom line quickly
6936 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937#ifdef USE_SYSTEM
6938 if (!(flags & EW_SILENT))
6939#endif
6940 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006941 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006942 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
6944 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006945 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6946 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 if (shell_style == STYLE_BT)
6948 return FAIL;
6949 goto notfound;
6950 }
6951
6952 /*
6953 * read the names from the file into memory
6954 */
6955 fd = fopen((char *)tempname, READBIN);
6956 if (fd == NULL)
6957 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006958 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 if (!(flags & EW_SILENT))
6960 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006961 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006962 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 }
6964 vim_free(tempname);
6965 goto notfound;
6966 }
6967 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006968 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006970 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006971 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006972 buffer = NULL;
6973 else
6974 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 if (buffer == NULL)
6976 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006977 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 mch_remove(tempname);
6979 vim_free(tempname);
6980 fclose(fd);
6981 return FAIL;
6982 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006983 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 i = fread((char *)buffer, 1, len, fd);
6985 fclose(fd);
6986 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006987 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006989 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006990 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 vim_free(tempname);
6992 vim_free(buffer);
6993 return FAIL;
6994 }
6995 vim_free(tempname);
6996
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006997# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006998 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02007000 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
7002 *p++ = buffer[i];
7003 len = p - buffer;
7004# endif
7005
7006
Bram Moolenaar0f873732019-12-05 20:28:46 +01007007 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 if (shell_style == STYLE_ECHO)
7009 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007010 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007012 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {
7014 while (*p != ' ' && *p != '\n')
7015 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007016 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 }
7018 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007019 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00007020 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007022 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007024 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {
7026 while (*p != '\n' && *p != NUL)
7027 ++p;
7028 if (*p != NUL)
7029 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007030 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 }
7032 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007033 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 else
7035 {
7036 /*
7037 * Some versions of zsh use spaces instead of NULs to separate
7038 * results. Only do this when there is no NUL before the end of the
7039 * buffer, otherwise we would never be able to use file names with
7040 * embedded spaces when zsh does use NULs.
7041 * When we found a NUL once, we know zsh is OK, set did_find_nul and
7042 * don't check for spaces again.
7043 */
7044 check_spaces = FALSE;
7045 if (shell_style == STYLE_PRINT && !did_find_nul)
7046 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007047 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007048 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01007049 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 did_find_nul = TRUE;
7051 else
7052 check_spaces = TRUE;
7053 }
7054
7055 /*
7056 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
7057 * already is one, for STYLE_GLOB it needs to be added.
7058 */
7059 if (len && buffer[len - 1] == NUL)
7060 --len;
7061 else
7062 buffer[len] = NUL;
7063 i = 0;
7064 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007065 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 {
7067 ++i;
7068 *p = NUL;
7069 }
7070 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007071 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 }
7073 if (i == 0)
7074 {
7075 /*
7076 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
7077 * /bin/sh will happily expand it to nothing rather than returning an
7078 * error; and hey, it's good to check anyway -- webb.
7079 */
7080 vim_free(buffer);
7081 goto notfound;
7082 }
7083 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007084 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 if (*file == NULL)
7086 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007087 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 vim_free(buffer);
7089 return FAIL;
7090 }
7091
7092 /*
7093 * Isolate the individual file names.
7094 */
7095 p = buffer;
7096 for (i = 0; i < *num_file; ++i)
7097 {
7098 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007099 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00007100 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
7101 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00007103 while (!(shell_style == STYLE_ECHO && *p == ' ')
7104 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007106 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 *p = NUL;
7108 else
7109 {
7110 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007111 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 }
7113 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007114 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007116 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007118 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 }
7120 }
7121
7122 /*
7123 * Move the file names to allocated memory.
7124 */
7125 for (j = 0, i = 0; i < *num_file; ++i)
7126 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007127 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7129 continue;
7130
Bram Moolenaar0f873732019-12-05 20:28:46 +01007131 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 dir = (mch_isdir((*file)[i]));
7133 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7134 continue;
7135
Bram Moolenaar0f873732019-12-05 20:28:46 +01007136 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007137 if (!dir && (flags & EW_EXEC)
7138 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007139 continue;
7140
Bram Moolenaar964b3742019-05-24 18:54:09 +02007141 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 if (p)
7143 {
7144 STRCPY(p, (*file)[i]);
7145 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007146 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 (*file)[j++] = p;
7148 }
7149 }
7150 vim_free(buffer);
7151 *num_file = j;
7152
Bram Moolenaar0f873732019-12-05 20:28:46 +01007153 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007155 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 goto notfound;
7157 }
7158
7159 return OK;
7160
7161notfound:
7162 if (flags & EW_NOTFOUND)
7163 return save_patterns(num_pat, pat, num_file, file);
7164 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165}
7166
Bram Moolenaar0f873732019-12-05 20:28:46 +01007167#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007170save_patterns(
7171 int num_pat,
7172 char_u **pat,
7173 int *num_file,
7174 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
7176 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007177 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007179 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 if (*file == NULL)
7181 return FAIL;
7182 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007183 {
7184 s = vim_strsave(pat[i]);
7185 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007186 // Be compatible with expand_filename(): halve the number of
7187 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007188 backslash_halve(s);
7189 (*file)[i] = s;
7190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 *num_file = num_pat;
7192 return OK;
7193}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195/*
7196 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7197 * expand.
7198 */
7199 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007200mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007202 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 if (*p == '\\' && p[1] != NUL)
7205 ++p;
7206 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 if (vim_strchr((char_u *)
7208#ifdef VMS
7209 "*?%"
7210#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212#endif
7213 , *p) != NULL)
7214 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 }
7216 return FALSE;
7217}
7218
7219/*
7220 * Return TRUE if the string "p" contains a wildcard.
7221 * Don't recognize '~' at the end as a wildcard.
7222 */
7223 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007224mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007226 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 if (*p == '\\' && p[1] != NUL)
7229 ++p;
7230 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 if (vim_strchr((char_u *)
7232#ifdef VMS
7233 "*?%$"
7234#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236#endif
7237 , *p) != NULL
7238 || (*p == '~' && p[1] != NUL))
7239 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 }
7241 return FALSE;
7242}
7243
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007245have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246{
7247 int i;
7248
7249 for (i = 0; i < num; i++)
7250 if (mch_has_wildcard(file[i]))
7251 return 1;
7252 return 0;
7253}
7254
7255 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007256have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257{
7258 int i;
7259
7260 for (i = 0; i < num; i++)
7261 if (vim_strchr(file[i], '$') != NULL)
7262 return TRUE;
7263 return FALSE;
7264}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007266#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267/*
7268 * Scaled-down version of rename(), which is missing in Xenix.
7269 * This version can only move regular files and will fail if the
7270 * destination exists.
7271 */
7272 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007273mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274{
7275 struct stat st;
7276
Bram Moolenaar0f873732019-12-05 20:28:46 +01007277 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007279 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007281 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 return 0;
7283 return -1;
7284}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007285#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007287#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007288# if defined(DYNAMIC_GPM) || defined(PROTO)
7289/*
7290 * Initialize Gpm's symbols for dynamic linking.
7291 * Must be called only if libgpm_hinst is NULL.
7292 */
7293 static int
7294load_libgpm(void)
7295{
7296 libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL);
7297
7298 if (libgpm_hinst == NULL)
7299 {
7300 if (p_verbose > 0)
7301 smsg_attr(HL_ATTR(HLF_W),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007302 _("Could not load gpm library: %s"), dlerror());
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007303 return FAIL;
7304 }
7305
7306 if (
7307 (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL
7308 || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL
7309 || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL
7310 || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL
7311 || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL
7312 )
7313 {
7314 semsg(_(e_could_not_load_library_str_str), "gpm", dlerror());
7315 dlclose(libgpm_hinst);
7316 libgpm_hinst = NULL;
7317 dll_gpm_flag = NULL;
7318 dll_gpm_fd = NULL;
7319 return FAIL;
7320 }
7321 return OK;
7322}
7323
7324 int
7325gpm_available(void)
7326{
7327 return libgpm_hinst != NULL || load_libgpm() == OK;
7328}
7329# endif // DYNAMIC_GPM
7330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331/*
7332 * Initializes connection with gpm (if it isn't already opened)
7333 * Return 1 if succeeded (or connection already opened), 0 if failed
7334 */
7335 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007336gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007338 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007340#ifdef DYNAMIC_GPM
7341 if (!gpm_available())
7342 return 0;
7343#endif
7344
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007345 if (gpm_flag)
7346 return 1; // already open
7347
7348 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7349 gpm_connect.defaultMask = ~GPM_HARD;
7350 // Default handling for mouse move
7351 gpm_connect.minMod = 0; // Handle any modifier keys
7352 gpm_connect.maxMod = 0xffff;
7353 if (Gpm_Open(&gpm_connect, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007355 // gpm library tries to handling TSTP causes
7356 // problems. Anyways, we close connection to Gpm whenever
7357 // we are going to suspend or starting an external process
7358 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007359# ifdef SIGTSTP
ichizok378447f2023-05-11 22:25:42 +01007360 mch_signal(SIGTSTP, restricted ? SIG_IGN : sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007361# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007362 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007364 if (gpm_fd == -2)
7365 Gpm_Close(); // We don't want to talk to xterm via gpm
7366 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367}
7368
7369/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007370 * Returns TRUE if the GPM mouse is enabled.
7371 */
7372 int
7373gpm_enabled(void)
7374{
7375 return gpm_flag && gpm_fd >= 0;
7376}
7377
7378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 */
7381 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007382gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007384 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 Gpm_Close();
7386}
7387
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007388/*
7389 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007391 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 */
7393 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007394mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396 int button;
7397 static Gpm_Event gpm_event;
7398 char_u string[6];
7399 int_u vim_modifiers;
7400 int row,col;
7401 unsigned char buttons_mask;
7402 unsigned char gpm_modifiers;
7403 static unsigned char old_buttons = 0;
7404
7405 Gpm_GetEvent(&gpm_event);
7406
7407#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007408 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 if (hold_gui_events)
7410 return 0;
7411#endif
7412
7413 row = gpm_event.y - 1;
7414 col = gpm_event.x - 1;
7415
Bram Moolenaar0f873732019-12-05 20:28:46 +01007416 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 string[1] = 'M';
7418 string[2] = 'G';
7419 switch (GPM_BARE_EVENTS(gpm_event.type))
7420 {
7421 case GPM_DRAG:
7422 string[3] = MOUSE_DRAG;
7423 break;
7424 case GPM_DOWN:
7425 buttons_mask = gpm_event.buttons & ~old_buttons;
7426 old_buttons = gpm_event.buttons;
7427 switch (buttons_mask)
7428 {
7429 case GPM_B_LEFT:
7430 button = MOUSE_LEFT;
7431 break;
7432 case GPM_B_MIDDLE:
7433 button = MOUSE_MIDDLE;
7434 break;
7435 case GPM_B_RIGHT:
7436 button = MOUSE_RIGHT;
7437 break;
7438 default:
7439 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007440 // Don't know what to do. Can more than one button be
7441 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 }
7443 string[3] = (char_u)(button | 0x20);
7444 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7445 break;
7446 case GPM_UP:
7447 string[3] = MOUSE_RELEASE;
7448 old_buttons &= ~gpm_event.buttons;
7449 break;
7450 default:
7451 return 0;
7452 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007453 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 gpm_modifiers = gpm_event.modifiers;
7455 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007456 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7457 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7458 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7460 vim_modifiers |= MOUSE_SHIFT;
7461
7462 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7463 vim_modifiers |= MOUSE_CTRL;
7464 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7465 vim_modifiers |= MOUSE_ALT;
7466 string[3] |= vim_modifiers;
7467 string[4] = (char_u)(col + ' ' + 1);
7468 string[5] = (char_u)(row + ' ' + 1);
7469 add_to_input_buf(string, 6);
7470 return 6;
7471}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007472#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007474#ifdef FEAT_SYSMOUSE
7475/*
7476 * Initialize connection with sysmouse.
7477 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7478 * output, any sysmouse output than will be processed via sig_sysmouse().
7479 * Return OK if succeeded, FAIL if failed.
7480 */
7481 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007482sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007483{
7484 struct mouse_info mouse;
7485
7486 mouse.operation = MOUSE_MODE;
7487 mouse.u.mode.mode = 0;
7488 mouse.u.mode.signal = SIGUSR2;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007489 if (ioctl(1, CONS_MOUSECTL, &mouse) == -1)
7490 return FAIL;
7491
ichizok378447f2023-05-11 22:25:42 +01007492 mch_signal(SIGUSR2, sig_sysmouse);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007493 mouse.operation = MOUSE_SHOW;
7494 ioctl(1, CONS_MOUSECTL, &mouse);
7495 return OK;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007496}
7497
7498/*
7499 * Stop processing SIGUSR2 signals, and also make sure that
7500 * virtual console do not send us any sysmouse related signal.
7501 */
7502 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007503sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007504{
7505 struct mouse_info mouse;
7506
ichizok378447f2023-05-11 22:25:42 +01007507 mch_signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007508 mouse.operation = MOUSE_MODE;
7509 mouse.u.mode.mode = 0;
7510 mouse.u.mode.signal = 0;
7511 ioctl(1, CONS_MOUSECTL, &mouse);
7512}
7513
7514/*
7515 * Gets info from sysmouse and adds special keys to input buf.
7516 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007517 static void
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007518sig_sysmouse SIGDEFARG(sigarg)
7519{
7520 struct mouse_info mouse;
7521 struct video_info video;
7522 char_u string[6];
7523 int row, col;
7524 int button;
7525 int buttons;
7526 static int oldbuttons = 0;
7527
7528#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007529 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007530 if (hold_gui_events)
7531 return;
7532#endif
7533
7534 mouse.operation = MOUSE_GETINFO;
7535 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7536 && ioctl(1, FBIO_MODEINFO, &video) != -1
7537 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7538 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7539 {
7540 row = mouse.u.data.y / video.vi_cheight;
7541 col = mouse.u.data.x / video.vi_cwidth;
7542 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007543 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007544 string[1] = 'M';
7545 string[2] = 'S';
7546 if (oldbuttons == buttons && buttons != 0)
7547 {
7548 button = MOUSE_DRAG;
7549 }
7550 else
7551 {
7552 switch (buttons)
7553 {
7554 case 0:
7555 button = MOUSE_RELEASE;
7556 break;
7557 case 1:
7558 button = MOUSE_LEFT;
7559 break;
7560 case 2:
7561 button = MOUSE_MIDDLE;
7562 break;
7563 case 4:
7564 button = MOUSE_RIGHT;
7565 break;
7566 default:
7567 return;
7568 }
7569 oldbuttons = buttons;
7570 }
7571 string[3] = (char_u)(button);
7572 string[4] = (char_u)(col + ' ' + 1);
7573 string[5] = (char_u)(row + ' ' + 1);
7574 add_to_input_buf(string, 6);
7575 }
7576 return;
7577}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007578#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007579
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007581typedef char_u * (*STRPROCSTR)(char_u *);
7582typedef char_u * (*INTPROCSTR)(int);
7583typedef int (*STRPROCINT)(char_u *);
7584typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585
7586/*
7587 * Call a DLL routine which takes either a string or int param
7588 * and returns an allocated string.
7589 */
7590 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007591mch_libcall(
7592 char_u *libname,
7593 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007594 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007595 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007596 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007597 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598{
7599# if defined(USE_DLOPEN)
7600 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007601 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602# else
7603 shl_t hinstLib;
7604# endif
7605 STRPROCSTR ProcAdd;
7606 INTPROCSTR ProcAddI;
7607 char_u *retval_str = NULL;
7608 int retval_int = 0;
7609 int success = FALSE;
7610
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007611 /*
7612 * Get a handle to the DLL module.
7613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007615 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007616 (void)dlerror();
7617
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 hinstLib = dlopen((char *)libname, RTLD_LAZY
7619# ifdef RTLD_LOCAL
7620 | RTLD_LOCAL
7621# endif
7622 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007623 if (hinstLib == NULL)
7624 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007625 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007626 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007627 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007628 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630# else
7631 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7632# endif
7633
Bram Moolenaar0f873732019-12-05 20:28:46 +01007634 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 if (hinstLib != NULL)
7636 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007637# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 /*
7639 * Catch a crash when calling the library function. For example when
7640 * using a number where a string pointer is expected.
7641 */
7642 mch_startjmp();
7643 if (SETJMP(lc_jump_env) != 0)
7644 {
7645 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007646# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007647 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007648# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 mch_didjmp();
7650 }
7651 else
7652# endif
7653 {
7654 retval_str = NULL;
7655 retval_int = 0;
7656
7657 if (argstring != NULL)
7658 {
7659# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007660 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007661 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662# else
7663 if (shl_findsym(&hinstLib, (const char *)funcname,
7664 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7665 ProcAdd = NULL;
7666# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007667 if ((success = (ProcAdd != NULL
7668# if defined(USE_DLOPEN)
7669 && dlerr == NULL
7670# endif
7671 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 {
7673 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007674 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 else
7676 retval_str = (ProcAdd)(argstring);
7677 }
7678 }
7679 else
7680 {
7681# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007682 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007683 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684# else
7685 if (shl_findsym(&hinstLib, (const char *)funcname,
7686 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7687 ProcAddI = NULL;
7688# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007689 if ((success = (ProcAddI != NULL
7690# if defined(USE_DLOPEN)
7691 && dlerr == NULL
7692# endif
7693 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {
7695 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007696 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 else
7698 retval_str = (ProcAddI)(argint);
7699 }
7700 }
7701
Bram Moolenaar0f873732019-12-05 20:28:46 +01007702 // Save the string before we free the library.
7703 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 if (string_result == NULL)
7705 *number_result = retval_int;
7706 else if (retval_str != NULL
7707 && retval_str != (char_u *)1
7708 && retval_str != (char_u *)-1)
7709 *string_result = vim_strsave(retval_str);
7710 }
7711
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007712# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 mch_endjmp();
7714# ifdef SIGHASARG
7715 if (lc_signal != 0)
7716 {
7717 int i;
7718
Bram Moolenaar0f873732019-12-05 20:28:46 +01007719 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 for (i = 0; signal_info[i].sig != -1; i++)
7721 if (lc_signal == signal_info[i].sig)
7722 break;
Bram Moolenaar50809a42023-05-20 16:39:07 +01007723 semsg(_(e_got_sig_str_in_libcall), signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 }
7725# endif
7726# endif
7727
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007729 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007730 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007731 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007732
Bram Moolenaar0f873732019-12-05 20:28:46 +01007733 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 (void)dlclose(hinstLib);
7735# else
7736 (void)shl_unload(hinstLib);
7737# endif
7738 }
7739
7740 if (!success)
7741 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007742 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 return FAIL;
7744 }
7745
7746 return OK;
7747}
7748#endif
7749
7750#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007751static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752static int xterm_button;
7753
7754/*
7755 * Setup a dummy window for X selections in a terminal.
7756 */
7757 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007758setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759{
7760 int z = 0;
7761 char *strp = "";
7762 Widget AppShell;
7763
7764 if (!x_connect_to_server())
7765 return;
7766
7767 open_app_context();
7768 if (app_context != NULL && xterm_Shell == (Widget)0)
7769 {
7770 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007771# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007773# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007774# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007775 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776
7777 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007778 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779# endif
7780
Bram Moolenaar0f873732019-12-05 20:28:46 +01007781 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 oldhandler = XSetErrorHandler(x_error_check);
7783
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007784# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007785 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7787 mch_startjmp();
7788 if (SETJMP(lc_jump_env) != 0)
7789 {
7790 mch_didjmp();
7791 xterm_dpy = NULL;
7792 }
7793 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007794# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 {
7796 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7797 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007798 if (xterm_dpy != NULL)
7799 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007800# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 }
7804
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007805# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007806 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007808# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007809 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 (void)XSetErrorHandler(oldhandler);
7811
7812 if (xterm_dpy == NULL)
7813 {
7814 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007815 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 return;
7817 }
7818
Bram Moolenaar0f873732019-12-05 20:28:46 +01007819 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 (void)XSetIOErrorHandler(x_IOerror_handler);
7821
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007822# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007824 {
7825 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007826 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007827 verbose_leave();
7828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829# endif
7830
Bram Moolenaar0f873732019-12-05 20:28:46 +01007831 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7833 applicationShellWidgetClass, xterm_dpy,
7834 NULL);
7835 if (AppShell == (Widget)0)
7836 return;
7837 xterm_Shell = XtVaCreatePopupShell("VIM",
7838 topLevelShellWidgetClass, AppShell,
7839 XtNmappedWhenManaged, 0,
7840 XtNwidth, 1,
7841 XtNheight, 1,
7842 NULL);
7843 if (xterm_Shell == (Widget)0)
7844 return;
7845
7846 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007847 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 if (x11_display == NULL)
7849 x11_display = xterm_dpy;
7850
7851 XtRealizeWidget(xterm_Shell);
7852 XSync(xterm_dpy, False);
7853 xterm_update();
7854 }
7855 if (xterm_Shell != (Widget)0)
7856 {
7857 clip_init(TRUE);
7858 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7859 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007860 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 if (test_x11_window(xterm_dpy) == FAIL)
7862 x11_window = 0;
7863 if (x11_window != 0)
7864 xterm_trace = 0;
7865 }
7866}
7867
7868 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007869start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870{
7871 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7872 return;
7873 xterm_trace = 1;
7874 xterm_button = button;
7875 do_xterm_trace();
7876}
7877
7878
7879 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007880stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881{
7882 if (xterm_trace < 0)
7883 return;
7884 xterm_trace = 0;
7885}
7886
7887/*
7888 * Query the xterm pointer and generate mouse termcodes if necessary
7889 * return TRUE if dragging is active, else FALSE
7890 */
7891 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007892do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893{
7894 Window root, child;
7895 int root_x, root_y;
7896 int win_x, win_y;
7897 int row, col;
7898 int_u mask_return;
7899 char_u buf[50];
7900 char_u *strp;
7901 long got_hints;
7902 static char_u *mouse_code;
7903 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7904 static int prev_row = 0, prev_col = 0;
7905 static XSizeHints xterm_hints;
7906
7907 if (xterm_trace <= 0)
7908 return FALSE;
7909
7910 if (xterm_trace == 1)
7911 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007912 // Get the hints just before tracking starts. The font size might
7913 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007914 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7915 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 || xterm_hints.width_inc <= 1
7917 || xterm_hints.height_inc <= 1)
7918 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007919 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 return FALSE;
7921 }
7922
Bram Moolenaar0f873732019-12-05 20:28:46 +01007923 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 mouse_code = find_termcode(mouse_name);
7925 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007926 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 xterm_trace = 2;
7928
Bram Moolenaar0f873732019-12-05 20:28:46 +01007929 // Find the offset of the chars, there might be a scrollbar on the
7930 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7932 &win_x, &win_y, &mask_return);
7933 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7934 - (xterm_hints.height_inc / 2);
7935 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7936 xterm_hints.y = 2;
7937 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7938 - (xterm_hints.width_inc / 2);
7939 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7940 xterm_hints.x = 2;
7941 return TRUE;
7942 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007943 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 {
7945 xterm_trace = 0;
7946 return FALSE;
7947 }
7948
7949 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7950 &win_x, &win_y, &mask_return);
7951
7952 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7953 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7954 if (row == prev_row && col == prev_col)
7955 return TRUE;
7956
7957 STRCPY(buf, mouse_code);
7958 strp = buf + STRLEN(buf);
7959 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7960 *strp++ = (char_u)(col + ' ' + 1);
7961 *strp++ = (char_u)(row + ' ' + 1);
7962 *strp = 0;
7963 add_to_input_buf(buf, STRLEN(buf));
7964
7965 prev_row = row;
7966 prev_col = col;
7967 return TRUE;
7968}
7969
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007970# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971/*
7972 * Destroy the display, window and app_context. Required for GTK.
7973 */
7974 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007975clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 if (xterm_Shell != (Widget)0)
7978 {
7979 XtDestroyWidget(xterm_Shell);
7980 xterm_Shell = (Widget)0;
7981 }
7982 if (xterm_dpy != NULL)
7983 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007984# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007985 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007987# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 if (x11_display == xterm_dpy)
7989 x11_display = NULL;
7990 xterm_dpy = NULL;
7991 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007992# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (app_context != (XtAppContext)NULL)
7994 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007995 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 XtDestroyApplicationContext(app_context);
7997 app_context = (XtAppContext)NULL;
7998 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000}
8001# endif
8002
8003/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01008004 * Catch up with GUI or X events.
8005 */
8006 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008007clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01008008{
8009# ifdef FEAT_GUI
8010 if (gui.in_use)
8011 gui_mch_update();
8012 else
8013# endif
8014 if (xterm_Shell != (Widget)0)
8015 xterm_update();
8016}
8017
8018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 * Catch up with any queued X events. This may put keyboard input into the
8020 * input buffer, call resize call-backs, trigger timers etc. If there is
8021 * nothing in the X event queue (& no timers pending), then we return
8022 * immediately.
8023 */
8024 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008025xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026{
8027 XEvent event;
8028
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008029 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008031 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008033 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008034 break;
8035
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008036 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008037 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008038 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008039 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008040#ifdef FEAT_CLIENTSERVER
8041 {
8042 XPropertyEvent *e = (XPropertyEvent *)&event;
8043
8044 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008046 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008049 XtDispatchEvent(&event);
8050 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008051 else
8052 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008053 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008054 XtAppProcessEvent(app_context, mask);
8055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 }
8057}
8058
8059 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008060clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061{
8062 if (xterm_Shell != (Widget)0)
8063 return clip_x11_own_selection(xterm_Shell, cbd);
8064 return FAIL;
8065}
8066
8067 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008068clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069{
8070 if (xterm_Shell != (Widget)0)
8071 clip_x11_lose_selection(xterm_Shell, cbd);
8072}
8073
8074 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008075clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
8077 if (xterm_Shell != (Widget)0)
8078 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
8079}
8080
8081 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008082clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
8084 clip_x11_set_selection(cbd);
8085}
8086#endif
8087
8088
8089#if defined(USE_XSMP) || defined(PROTO)
8090/*
8091 * Code for X Session Management Protocol.
8092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093
8094# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095/*
8096 * This is our chance to ask the user if they want to save,
8097 * or abort the logout
8098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008100xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101{
Bram Moolenaare1004402020-10-24 20:49:43 +02008102 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 int cancel_shutdown = False;
8104
Bram Moolenaare1004402020-10-24 20:49:43 +02008105 save_cmod_flags = cmdmod.cmod_flags;
8106 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01008107 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01008108 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02008110 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01008111 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 out_flush();
8113
Bram Moolenaar0f873732019-12-05 20:28:46 +01008114 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 SmcInteractDone(smc_conn, cancel_shutdown);
8116
Bram Moolenaar0f873732019-12-05 20:28:46 +01008117 // Finish off
8118 // Only end save-yourself here if we're not cancelling shutdown;
8119 // we'll get a cancelled callback later in which we'll end it.
8120 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 if (!cancel_shutdown)
8122 {
8123 xsmp.save_yourself = False;
8124 SmcSaveYourselfDone(smc_conn, True);
8125 }
8126}
8127# endif
8128
8129/*
8130 * Callback that starts save-yourself.
8131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008133xsmp_handle_save_yourself(
8134 SmcConn smc_conn,
8135 SmPointer client_data UNUSED,
8136 int save_type UNUSED,
8137 Bool shutdown,
8138 int interact_style UNUSED,
8139 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008141 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 if (xsmp.save_yourself)
8143 SmcSaveYourselfDone(smc_conn, True);
8144 xsmp.save_yourself = True;
8145 xsmp.shutdown = shutdown;
8146
Bram Moolenaar0f873732019-12-05 20:28:46 +01008147 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01008149 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150
8151 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008152 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153
8154# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008155 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008157 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 SmcInteractRequest(smc_conn, SmDialogError,
8159 xsmp_handle_interaction, client_data);
8160 else
8161# endif
8162 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008163 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 SmcSaveYourselfDone(smc_conn, True);
8165 xsmp.save_yourself = False;
8166 }
8167}
8168
8169
8170/*
8171 * Callback to warn us of imminent death.
8172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008174xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175{
8176 xsmp_close();
8177
Bram Moolenaar0f873732019-12-05 20:28:46 +01008178 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 getout_preserve_modified(0);
8180}
8181
8182
8183/*
8184 * Callback to tell us that save-yourself has completed.
8185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008187xsmp_save_complete(
8188 SmcConn smc_conn UNUSED,
8189 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190{
8191 xsmp.save_yourself = False;
8192}
8193
8194
8195/*
8196 * Callback to tell us that an instigated shutdown was cancelled
8197 * (maybe even by us)
8198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008200xsmp_shutdown_cancelled(
8201 SmcConn smc_conn,
8202 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203{
8204 if (xsmp.save_yourself)
8205 SmcSaveYourselfDone(smc_conn, True);
8206 xsmp.save_yourself = False;
8207 xsmp.shutdown = False;
8208}
8209
8210
8211/*
8212 * Callback to tell us that a new ICE connection has been established.
8213 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008215xsmp_ice_connection(
8216 IceConn iceConn,
8217 IcePointer clientData UNUSED,
8218 Bool opening,
8219 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008221 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 if (opening)
8223 {
8224 xsmp_icefd = IceConnectionNumber(iceConn);
8225 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8226 }
8227}
8228
8229
Bram Moolenaar0f873732019-12-05 20:28:46 +01008230// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008232xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233{
8234 Bool rep;
8235
8236 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8237 == IceProcessMessagesIOError)
8238 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008239 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008241 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 xsmp_close();
8243 return FAIL;
8244 }
8245 else
8246 return OK;
8247}
8248
8249static int dummy;
8250
Bram Moolenaar0f873732019-12-05 20:28:46 +01008251// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 void
8253xsmp_init(void)
8254{
8255 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 SmcCallbacks smcallbacks;
8257#if 0
8258 SmPropValue smname;
8259 SmProp smnameprop;
8260 SmProp *smprops[1];
8261#endif
8262
8263 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008264 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265
8266 xsmp.save_yourself = xsmp.shutdown = False;
8267
Bram Moolenaar0f873732019-12-05 20:28:46 +01008268 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8270 smcallbacks.save_yourself.client_data = NULL;
8271 smcallbacks.die.callback = xsmp_die;
8272 smcallbacks.die.client_data = NULL;
8273 smcallbacks.save_complete.callback = xsmp_save_complete;
8274 smcallbacks.save_complete.client_data = NULL;
8275 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8276 smcallbacks.shutdown_cancelled.client_data = NULL;
8277
Bram Moolenaar0f873732019-12-05 20:28:46 +01008278 // Set up a watch on ICE connection creations. The "dummy" argument is
8279 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8281 {
8282 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008283 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 return;
8285 }
8286
Bram Moolenaar0f873732019-12-05 20:28:46 +01008287 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 xsmp.smcconn = SmcOpenConnection(
8289 NULL,
8290 NULL,
8291 SmProtoMajor,
8292 SmProtoMinor,
8293 SmcSaveYourselfProcMask | SmcDieProcMask
8294 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8295 &smcallbacks,
8296 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008297 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008298 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 errorstring);
8300 if (xsmp.smcconn == NULL)
8301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008303 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008304 char errorreport[132];
8305
8306 // If the message is too long it might not be NUL terminated. Add
8307 // a NUL at the end to make sure we don't go over the end.
8308 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008309 vim_snprintf(errorreport, sizeof(errorreport),
8310 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008311 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 return;
8314 }
8315 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8316
8317#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008318 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 smname.value = "vim";
8320 smname.length = 3;
8321 smnameprop.name = "SmProgram";
8322 smnameprop.type = "SmARRAY8";
8323 smnameprop.num_vals = 1;
8324 smnameprop.vals = &smname;
8325
8326 smprops[0] = &smnameprop;
8327 SmcSetProperties(xsmp.smcconn, 1, smprops);
8328#endif
8329}
8330
8331
Bram Moolenaar0f873732019-12-05 20:28:46 +01008332// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008334xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008336 if (xsmp_icefd == -1)
8337 return;
8338
8339 SmcCloseConnection(xsmp.smcconn, 0, NULL);
8340 if (xsmp.clientid != NULL)
8341 free(xsmp.clientid);
8342 xsmp.clientid = NULL;
8343 xsmp_icefd = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008345#endif // USE_XSMP
Paul Ollis65745772022-06-05 16:55:54 +01008346
8347#if defined(FEAT_RELTIME) || defined(PROTO)
Bram Moolenaar08210f82023-04-13 19:15:54 +01008348# if defined(PROF_NSEC) || defined(PROTO)
Paul Ollis65745772022-06-05 16:55:54 +01008349/*
8350 * Implement timeout with timer_create() and timer_settime().
8351 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008352static volatile sig_atomic_t timeout_flag = FALSE;
8353static timer_t timer_id;
8354static int timer_created = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008355
8356/*
8357 * Callback for when the timer expires.
8358 */
8359 static void
8360set_flag(union sigval _unused UNUSED)
8361{
8362 timeout_flag = TRUE;
8363}
8364
8365/*
8366 * Stop any active timeout.
8367 */
8368 void
8369stop_timeout(void)
8370{
8371 static struct itimerspec disarm = {{0, 0}, {0, 0}};
8372
8373 if (timer_created)
8374 {
8375 int ret = timer_settime(timer_id, 0, &disarm, NULL);
8376
8377 if (ret < 0)
8378 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8379 }
8380
8381 // Clear the current timeout flag; any previous timeout should be
8382 // considered _not_ triggered.
8383 timeout_flag = FALSE;
8384}
8385
8386/*
8387 * Start the timeout timer.
8388 *
8389 * The return value is a pointer to a flag that is initialised to FALSE. If the
8390 * timeout expires, the flag is set to TRUE. This will only return pointers to
8391 * static memory; i.e. any pointer returned by this function may always be
8392 * safely dereferenced.
8393 *
8394 * This function is not expected to fail, but if it does it will still return a
8395 * valid flag pointer; the flag will remain stuck as FALSE .
8396 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008397 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008398start_timeout(long msec)
8399{
8400 struct itimerspec interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008401 {0, 0}, // Do not repeat.
8402 {msec / 1000, (msec % 1000) * 1000000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008403 int ret;
8404
8405 // This is really the caller's responsibility, but let's make sure the
8406 // previous timer has been stopped.
8407 stop_timeout();
Paul Ollis65745772022-06-05 16:55:54 +01008408
8409 if (!timer_created)
8410 {
8411 struct sigevent action = {0};
8412
8413 action.sigev_notify = SIGEV_THREAD;
8414 action.sigev_notify_function = set_flag;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008415 ret = timer_create(CLOCK_MONOTONIC, &action, &timer_id);
8416 if (ret < 0)
Paul Ollis65745772022-06-05 16:55:54 +01008417 {
8418 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8419 return &timeout_flag;
8420 }
8421 timer_created = TRUE;
8422 }
8423
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00008424# ifdef FEAT_EVAL
Bram Moolenaar616592e2022-06-17 15:17:10 +01008425 ch_log(NULL, "setting timeout timer to %d sec %ld nsec",
8426 (int)interval.it_value.tv_sec, (long)interval.it_value.tv_nsec);
Bram Moolenaar509ce032022-06-20 11:23:01 +01008427# endif
Paul Ollis65745772022-06-05 16:55:54 +01008428 ret = timer_settime(timer_id, 0, &interval, NULL);
8429 if (ret < 0)
8430 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8431
8432 return &timeout_flag;
8433}
8434
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008435/*
8436 * To be used before fork/exec: delete any created timer.
8437 */
8438 void
8439delete_timer(void)
8440{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008441 if (!timer_created)
8442 return;
8443
8444 timer_delete(timer_id);
8445 timer_created = FALSE;
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008446}
8447
Bram Moolenaar08210f82023-04-13 19:15:54 +01008448# else // PROF_NSEC
Paul Ollis65745772022-06-05 16:55:54 +01008449
8450/*
8451 * Implement timeout with setitimer()
8452 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008453static struct sigaction prev_sigaction;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008454static volatile sig_atomic_t timeout_flag = FALSE;
8455static int timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008456static int timer_handler_active = FALSE;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008457static volatile sig_atomic_t alarm_pending = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008458
8459/*
8460 * Handle SIGALRM for a timeout.
8461 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01008462 static void
Paul Ollis65745772022-06-05 16:55:54 +01008463set_flag SIGDEFARG(sigarg)
8464{
8465 if (alarm_pending)
8466 alarm_pending = FALSE;
8467 else
8468 timeout_flag = TRUE;
8469}
8470
8471/*
8472 * Stop any active timeout.
8473 */
8474 void
8475stop_timeout(void)
8476{
8477 static struct itimerval disarm = {{0, 0}, {0, 0}};
8478 int ret;
8479
8480 if (timer_active)
8481 {
8482 timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008483 ret = setitimer(ITIMER_REAL, &disarm, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008484 if (ret < 0)
8485 // Should only get here as a result of coding errors.
8486 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8487 }
8488
8489 if (timer_handler_active)
8490 {
8491 timer_handler_active = FALSE;
8492 ret = sigaction(SIGALRM, &prev_sigaction, NULL);
8493 if (ret < 0)
8494 // Should only get here as a result of coding errors.
8495 semsg(_(e_could_not_reset_handler_for_timeout_str),
8496 strerror(errno));
8497 }
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008498 timeout_flag = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008499}
8500
8501/*
8502 * Start the timeout timer.
8503 *
8504 * The return value is a pointer to a flag that is initialised to FALSE. If the
8505 * timeout expires, the flag is set to TRUE. This will only return pointers to
8506 * static memory; i.e. any pointer returned by this function may always be
8507 * safely dereferenced.
8508 *
8509 * This function is not expected to fail, but if it does it will still return a
8510 * valid flag pointer; the flag will remain stuck as FALSE .
8511 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008512 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008513start_timeout(long msec)
8514{
8515 struct itimerval interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008516 {0, 0}, // Do not repeat.
8517 {msec / 1000, (msec % 1000) * 1000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008518 struct sigaction handle_alarm;
8519 int ret;
8520 sigset_t sigs;
8521 sigset_t saved_sigs;
8522
8523 // This is really the caller's responsibility, but let's make sure the
8524 // previous timer has been stopped.
8525 stop_timeout();
8526
8527 // There is a small chance that SIGALRM is pending and so the handler must
8528 // ignore it on the first call.
8529 alarm_pending = FALSE;
8530 ret = sigemptyset(&sigs);
8531 ret = ret == 0 ? sigaddset(&sigs, SIGALRM) : ret;
8532 ret = ret == 0 ? sigprocmask(SIG_BLOCK, &sigs, &saved_sigs) : ret;
8533 timeout_flag = FALSE;
8534 ret = ret == 0 ? sigpending(&sigs) : ret;
8535 if (ret == 0)
8536 {
8537 alarm_pending = sigismember(&sigs, SIGALRM);
Bram Moolenaar1f89abf2022-06-06 10:07:01 +01008538 ret = sigprocmask(SIG_SETMASK, &saved_sigs, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008539 }
8540 if (unlikely(ret != 0 || alarm_pending < 0))
8541 {
8542 // Just catching coding errors. Write an error message, but carry on.
8543 semsg(_(e_could_not_check_for_pending_sigalrm_str), strerror(errno));
8544 alarm_pending = FALSE;
8545 }
8546
8547 // Set up the alarm handler first.
8548 ret = sigemptyset(&handle_alarm.sa_mask);
8549 handle_alarm.sa_handler = set_flag;
8550 handle_alarm.sa_flags = 0;
8551 ret = ret == 0 ? sigaction(SIGALRM, &handle_alarm, &prev_sigaction) : ret;
8552 if (ret < 0)
8553 {
8554 // Should only get here as a result of coding errors.
8555 semsg(_(e_could_not_set_handler_for_timeout_str), strerror(errno));
8556 return &timeout_flag;
8557 }
8558 timer_handler_active = TRUE;
8559
8560 // Set up the interval timer once the alarm handler is in place.
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008561 ret = setitimer(ITIMER_REAL, &interval, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008562 if (ret < 0)
8563 {
8564 // Should only get here as a result of coding errors.
8565 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8566 stop_timeout();
8567 return &timeout_flag;
8568 }
8569
8570 timer_active = TRUE;
8571 return &timeout_flag;
8572}
Bram Moolenaar08210f82023-04-13 19:15:54 +01008573# endif // PROF_NSEC
Paul Ollis65745772022-06-05 16:55:54 +01008574#endif // FEAT_RELTIME