blob: 194e4d31342fd75e3c596ad216bec6c62f041a9a [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)
447 handle_resize();
448 return FALSE;
449}
450
451/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000452 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 * Get a characters from the keyboard.
454 * Return the number of characters that are available.
455 * If wtime == 0 do not wait for characters.
456 * If wtime == n wait a short time for characters.
457 * If wtime == -1 wait forever for characters.
458 */
459 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100460mch_inchar(
461 char_u *buf,
462 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100463 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100464 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100466 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
467 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468}
469
470 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100471handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472{
473 do_resize = FALSE;
474 shell_resized();
475}
476
477/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200478 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 */
480 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100481mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200483 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484}
485
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200486#if defined(FEAT_TERMINAL) || defined(PROTO)
487/*
488 * Check for any pending input or messages.
489 */
490 int
491mch_check_messages(void)
492{
493 return WaitForChar(0L, NULL, TRUE);
494}
495#endif
496
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
498# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000499# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500# endif
501# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
502# include <sys/sysctl.h>
503# endif
504# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
505# include <sys/sysinfo.h>
506# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100507# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100508# include <mach/mach_host.h>
509# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511
512/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000513 * Return total amount of memory available in Kbyte.
514 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100517mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100520 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521
Bram Moolenaar362dc332018-03-05 21:59:37 +0100522# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100523 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100524 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100525 mach_port_t host = mach_host_self();
526 kern_return_t kret;
527# ifdef HOST_VM_INFO64
528 struct vm_statistics64 vm_stat;
529 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530
Bram Moolenaar988615f2018-02-27 17:58:20 +0100531 kret = host_statistics64(host, HOST_VM_INFO64,
532 (host_info64_t)&vm_stat, &count);
533# else
534 struct vm_statistics vm_stat;
535 natural_t count = HOST_VM_INFO_COUNT;
536
537 kret = host_statistics(host, HOST_VM_INFO,
538 (host_info_t)&vm_stat, &count);
539# endif
540 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100541 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100542 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
543 + vm_stat.inactive_count
544# ifdef MAC_OS_X_VERSION_10_9
545 + vm_stat.compressor_page_count
546# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100547 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100548 mach_port_deallocate(mach_task_self(), host);
549 }
550# endif
551
552# ifdef HAVE_SYSCTL
553 if (mem == 0)
554 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100555 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100556 int mib[2];
557 size_t len = sizeof(long_u);
558# ifdef HW_USERMEM64
559 long_u physmem;
560# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100561 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100562 union {
563 int_u u32;
564 long_u u64;
565 } physmem;
566# endif
567
568 mib[0] = CTL_HW;
569# ifdef HW_USERMEM64
570 mib[1] = HW_USERMEM64;
571# else
572 mib[1] = HW_USERMEM;
573# endif
574 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
575 {
576# ifdef HW_USERMEM64
577 mem = (long_u)physmem;
578# else
579 if (len == sizeof(physmem.u64))
580 mem = (long_u)physmem.u64;
581 else
582 mem = (long_u)physmem.u32;
583# endif
584 }
585 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200586# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200588# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 if (mem == 0)
590 {
591 struct sysinfo sinfo;
592
Bram Moolenaar0f873732019-12-05 20:28:46 +0100593 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000595 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200596# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100597 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000598 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
599 {
600 sinfo.mem_unit = sinfo.mem_unit >> 1;
601 --shiftright;
602 }
603 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200604# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200606# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200611# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 if (mem == 0)
613 {
614 long pagesize, pagecount;
615
Bram Moolenaar0f873732019-12-05 20:28:46 +0100616 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 pagesize = sysconf(_SC_PAGESIZE);
618 pagecount = sysconf(_SC_PHYS_PAGES);
619 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000620 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100621 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000622 while (shiftright > 0 && (pagesize & 1) == 0)
623 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000624 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000625 --shiftright;
626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
Bram Moolenaar0f873732019-12-05 20:28:46 +0100632 // Return the minimum of the physical memory and the user limit, because
633 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200634# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 {
636 struct rlimit rlp;
637
638 if (getrlimit(RLIMIT_DATA, &rlp) == 0
639 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200640# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200642# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000643 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000645 {
646 mem = (long_u)rlp.rlim_cur;
647 shiftright = 10;
648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200650# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651
652 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000653 return mem >> shiftright;
654 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655}
656#endif
657
Bram Moolenaar0981c872020-08-23 14:28:37 +0200658/*
659 * "flags": MCH_DELAY_IGNOREINPUT - don't read input
660 * MCH_DELAY_SETTMODE - use settmode() even for short delays
661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200663mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200665 tmode_T old_tmode;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200666 int call_settmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000667#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100668 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
Bram Moolenaar0981c872020-08-23 14:28:37 +0200671 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100673 // Go to cooked mode without echo, to allow SIGINT interrupting us
674 // here. But we don't want QUIT to kill us (CTRL-\ used in a
675 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200676 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000677 in_mch_delay = TRUE;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200678 call_settmode = mch_cur_tmode == TMODE_RAW
679 && (msec > 500 || (flags & MCH_DELAY_SETTMODE));
680 if (call_settmode)
681 {
682 old_tmode = mch_cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 settmode(TMODE_SLEEP);
Bram Moolenaar80361a52020-10-05 21:39:25 +0200684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
686 /*
687 * Everybody sleeps in a different way...
688 * Prefer nanosleep(), some versions of usleep() can only sleep up to
689 * one second.
690 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000691#ifdef FEAT_MZSCHEME
692 do
693 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100694 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000695 if (total > p_mzq)
696 msec = p_mzq;
697 else
698 msec = total;
699 total -= msec;
700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701#ifdef HAVE_NANOSLEEP
702 {
703 struct timespec ts;
704
705 ts.tv_sec = msec / 1000;
706 ts.tv_nsec = (msec % 1000) * 1000000;
707 (void)nanosleep(&ts, NULL);
708 }
709#else
710# ifdef HAVE_USLEEP
711 while (msec >= 1000)
712 {
713 usleep((unsigned int)(999 * 1000));
714 msec -= 999;
715 }
716 usleep((unsigned int)(msec * 1000));
717# else
718# ifndef HAVE_SELECT
719 poll(NULL, 0, (int)msec);
720# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {
722 struct timeval tv;
723
724 tv.tv_sec = msec / 1000;
725 tv.tv_usec = (msec % 1000) * 1000;
Bram Moolenaar0981c872020-08-23 14:28:37 +0200726 // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
727 // a patch from Sun to fix this. Reported by Gunnar Pedersen.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 select(0, NULL, NULL, NULL, &tv);
729 }
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100730# endif
731# endif
732#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000733#ifdef FEAT_MZSCHEME
734 }
735 while (total > 0);
736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
Bram Moolenaar80361a52020-10-05 21:39:25 +0200738 if (call_settmode)
Bram Moolenaar26e86442020-05-17 14:06:16 +0200739 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000740 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 }
742 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200743 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744}
745
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000746#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
748# define HAVE_CHECK_STACK_GROWTH
749/*
750 * Support for checking for an almost-out-of-stack-space situation.
751 */
752
753/*
754 * Return a pointer to an item on the stack. Used to find out if the stack
755 * grows up or down.
756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757static int stack_grows_downwards;
758
759/*
760 * Find out if the stack grows upwards or downwards.
761 * "p" points to a variable on the stack of the caller.
762 */
763 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100764check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
766 int i;
767
768 stack_grows_downwards = (p > (char *)&i);
769}
770#endif
771
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000772#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773static char *stack_limit = NULL;
774
775#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
776# include <pthread.h>
777# include <pthread_np.h>
778#endif
779
780/*
781 * Find out until how var the stack can grow without getting into trouble.
782 * Called when starting up and when switching to the signal stack in
783 * deathtrap().
784 */
785 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100786get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
788 struct rlimit rlp;
789 int i;
790 long lim;
791
Bram Moolenaar0f873732019-12-05 20:28:46 +0100792 // Set the stack limit to 15/16 of the allowable size. Skip this when the
793 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 if (getrlimit(RLIMIT_STACK, &rlp) == 0
795 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
796# ifdef RLIM_INFINITY
797 && rlp.rlim_cur != RLIM_INFINITY
798# endif
799 )
800 {
801 lim = (long)rlp.rlim_cur;
802#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
803 {
804 pthread_attr_t attr;
805 size_t size;
806
Bram Moolenaar0f873732019-12-05 20:28:46 +0100807 // On FreeBSD the initial thread always has a fixed stack size, no
808 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 pthread_attr_init(&attr);
810 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
811 {
812 pthread_attr_getstacksize(&attr, &size);
813 if (lim > (long)size)
814 lim = (long)size;
815 }
816 pthread_attr_destroy(&attr);
817 }
818#endif
819 if (stack_grows_downwards)
820 {
821 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
822 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100823 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 stack_limit = (char *)((long)&i / 16L);
825 }
826 else
827 {
828 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
829 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100830 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
832 }
833}
834
835/*
836 * Return FAIL when running out of stack space.
837 * "p" must point to any variable local to the caller that's on the stack.
838 */
839 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100840mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000842 if (stack_limit == NULL)
843 return OK;
844
845 if (stack_grows_downwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000847 if (p < stack_limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 return FAIL;
849 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000850 else if (p > stack_limit)
851 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 return OK;
853}
854#endif
855
856#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
857/*
858 * Support for using the signal stack.
859 * This helps when we run out of stack space, which causes a SIGSEGV. The
860 * signal handler then must run on another stack, since the normal stack is
861 * completely full.
862 */
863
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100865static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100867static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868# endif
869
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200870/*
871 * Get a size of signal stack.
872 * Preference (if available): sysconf > SIGSTKSZ > guessed size
873 */
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100874static long int get_signal_stack_size(void)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200875{
876# ifdef HAVE_SYSCONF_SIGSTKSZ
877 long int size = -1;
878
879 // return size only if sysconf doesn't return an error
880 if ((size = sysconf(_SC_SIGSTKSZ)) > -1)
881 return size;
882# endif
883
884# ifdef SIGSTKSZ
885 // if sysconf() isn't available or gives error, return SIGSTKSZ
886 // if defined
887 return SIGSTKSZ;
888# endif
889
890 // otherwise guess the size
891 return 8000;
892}
893
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894static char *signal_stack;
895
896 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100897init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000899 if (signal_stack == NULL)
900 return;
901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903# ifdef HAVE_SS_BASE
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000904 sigstk.ss_base = signal_stack;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000906 sigstk.ss_sp = signal_stack;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000908 sigstk.ss_size = get_signal_stack_size();
909 sigstk.ss_flags = 0;
910 (void)sigaltstack(&sigstk, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000912 sigstk.ss_sp = signal_stack;
913 if (stack_grows_downwards)
914 sigstk.ss_sp += get_signal_stack_size() - 1;
915 sigstk.ss_onstack = 0;
916 (void)sigstack(&sigstk, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918}
919#endif
920
921/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000922 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 * will barf when the second argument to signal() is ``wrong''.
924 * Let me try it with a few tricky defines from my own osdef.h (jw).
925 */
926#if defined(SIGWINCH)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100927 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928sig_winch SIGDEFARG(sigarg)
929{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100930 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100931 mch_signal(SIGWINCH, sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 do_resize = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933}
934#endif
935
dbivolaruab16ad32021-12-29 19:41:47 +0000936#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100937 static void
dbivolaruab16ad32021-12-29 19:41:47 +0000938sig_tstp SIGDEFARG(sigarg)
939{
940 // Second time we get called we actually need to suspend
941 if (in_mch_suspend)
942 {
ichizok378447f2023-05-11 22:25:42 +0100943 mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
dbivolaruab16ad32021-12-29 19:41:47 +0000944 raise(sigarg);
945 }
dbivolaru79a6e252022-01-23 16:41:14 +0000946 else
947 got_tstp = TRUE;
dbivolaruab16ad32021-12-29 19:41:47 +0000948
ichizok5f823d12022-03-13 17:27:38 +0000949#if !defined(__ANDROID__) && !defined(__OpenBSD__) && !defined(__DragonFly__)
950 // This is not required on all systems. On some systems (at least Android,
951 // OpenBSD, and DragonFlyBSD) this breaks suspending with CTRL-Z.
ichizok378447f2023-05-11 22:25:42 +0100952 mch_signal(SIGTSTP, sig_tstp);
xtkobacbef12e2022-02-27 12:31:52 +0000953#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000954}
955#endif
956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957#if defined(SIGINT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100958 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959catch_sigint SIGDEFARG(sigarg)
960{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100961 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100962 mch_signal(SIGINT, catch_sigint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 got_int = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964}
965#endif
966
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200967#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100968 static void
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200969catch_sigusr1 SIGDEFARG(sigarg)
970{
971 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100972 mch_signal(SIGUSR1, catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200973 got_sigusr1 = TRUE;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200974}
975#endif
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100978 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979catch_sigpwr SIGDEFARG(sigarg)
980{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100981 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100982 mch_signal(SIGPWR, catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 /*
984 * I'm not sure we get the SIGPWR signal when the system is really going
985 * down or when the batteries are almost empty. Just preserve the swap
986 * files and don't exit, that can't do any harm.
987 */
988 ml_sync_all(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990#endif
991
992#ifdef SET_SIG_ALARM
993/*
994 * signal function for alarm().
995 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100996 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997sig_alarm SIGDEFARG(sigarg)
998{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100999 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 sig_alarm_called = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001}
1002#endif
1003
Bram Moolenaaredce7422019-01-20 18:39:30 +01001004#if (defined(HAVE_SETJMP_H) \
1005 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
1006 || defined(FEAT_LIBCALL))) \
1007 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001008# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +01001009
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001010// argument to SETJMP()
1011static JMP_BUF lc_jump_env;
1012
1013# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001014// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001015// Volatile because it is used in signal handlers.
1016static volatile sig_atomic_t lc_signal;
1017# endif
1018
1019// TRUE when lc_jump_env is valid.
1020// Volatile because it is used in signal handler deathtrap().
zeertzjq9b7d2a92022-08-26 10:33:54 +01001021static volatile sig_atomic_t lc_active = FALSE;
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023/*
1024 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001025 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001027 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 * Usage:
1029 * mch_startjmp();
1030 * if (SETJMP(lc_jump_env) != 0)
1031 * {
1032 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001033 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 * }
1035 * else
1036 * {
1037 * do_the_work;
1038 * mch_endjmp();
1039 * }
1040 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1041 * not return before the saved environment is used.
1042 * Returns OK for normal return, FAIL when the protected code caused a
1043 * problem and LONGJMP() was used.
1044 */
Bram Moolenaar113e1072019-01-20 15:30:40 +01001045 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001046mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001048# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001050# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 lc_active = TRUE;
1052}
1053
Bram Moolenaar113e1072019-01-20 15:30:40 +01001054 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001055mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
1057 lc_active = FALSE;
1058}
1059
Bram Moolenaar113e1072019-01-20 15:30:40 +01001060 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001061mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062{
1063# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001064 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1065 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 init_signal_stack();
1067# endif
1068}
1069#endif
1070
1071/*
1072 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001073 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001075 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1076 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001078 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079deathtrap SIGDEFARG(sigarg)
1080{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001081 static int entered = 0; // count the number of times we got here.
1082 // Note: when memory has been corrupted
1083 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084#ifdef SIGHASARG
1085 int i;
1086#endif
1087
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001088#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 /*
1090 * Catch a crash in protected code.
1091 * Restores the environment saved in lc_jump_env, which looks like
1092 * SETJMP() returns 1.
1093 */
1094 if (lc_active)
1095 {
1096# if defined(SIGHASARG)
1097 lc_signal = sigarg;
1098# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001099 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001101 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 }
1103#endif
1104
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001105#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001106# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001107 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1108 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1109 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001110 if (in_mch_delay && sigarg == SIGQUIT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001111 return;
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001112# endif
1113
Bram Moolenaar0f873732019-12-05 20:28:46 +01001114 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1115 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1116 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001117 if (entered == 0
1118 && (0
1119# ifdef SIGHUP
1120 || sigarg == SIGHUP
1121# endif
1122# ifdef SIGQUIT
1123 || sigarg == SIGQUIT
1124# endif
1125# ifdef SIGTERM
1126 || sigarg == SIGTERM
1127# endif
1128# ifdef SIGPWR
1129 || sigarg == SIGPWR
1130# endif
1131# ifdef SIGUSR1
1132 || sigarg == SIGUSR1
1133# endif
1134# ifdef SIGUSR2
1135 || sigarg == SIGUSR2
1136# endif
1137 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001138 && !vim_handle_signal(sigarg))
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001139 return;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001140#endif
1141
Bram Moolenaar0f873732019-12-05 20:28:46 +01001142 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 ++entered;
1144
Bram Moolenaar0f873732019-12-05 20:28:46 +01001145 // Executing autocommands is likely to use more stack space than we have
1146 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001147 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001150 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 set_vim_var_nr(VV_DYING, (long)entered);
1152#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001153 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001155#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001156 // Since we are now using the signal stack, need to reset the stack
1157 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 get_stack_limit();
1159#endif
1160
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001161#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001162 // This is for opening gdb the moment Vim crashes.
1163 // You need to manually adjust the file name and Vim executable name.
1164 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001165 {
1166# define VI_GDB_FILE "/tmp/vimgdb"
1167# define VIM_NAME "/usr/bin/vim"
1168 FILE *fp = fopen(VI_GDB_FILE, "w");
1169 if (fp)
1170 {
1171 fprintf(fp,
1172 "file %s\n"
1173 "attach %d\n"
1174 "set height 1000\n"
1175 "bt full\n"
1176 , VIM_NAME, getpid());
1177 fclose(fp);
1178 system("xterm -e gdb -x "VI_GDB_FILE);
1179 unlink(VI_GDB_FILE);
1180 }
1181 }
1182#endif
1183
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001185 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 for (i = 0; signal_info[i].sig != -1; i++)
1187 if (sigarg == signal_info[i].sig)
1188 break;
1189 deadly_signal = sigarg;
1190#endif
1191
Bram Moolenaar0f873732019-12-05 20:28:46 +01001192 full_screen = FALSE; // don't write message to the GUI, it might be
1193 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 /*
1195 * If something goes wrong after entering here, we may get here again.
1196 * When this happens, give a message and try to exit nicely (resetting the
1197 * terminal mode, etc.)
1198 * When this happens twice, just exit, don't even try to give a message,
1199 * stack may be corrupt or something weird.
1200 * When this still happens again (or memory was corrupted in such a way
1201 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1202 */
1203 if (entered >= 3)
1204 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001205 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 may_core_dump();
1207 if (entered >= 4)
1208 _exit(8);
1209 exit(7);
1210 }
1211 if (entered == 2)
1212 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001213 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001214 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 out_flush();
1216 getout(1);
1217 }
1218
Bram Moolenaar0f873732019-12-05 20:28:46 +01001219 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001221 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 signal_info[i].name);
1223#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001224 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001226
Bram Moolenaar0f873732019-12-05 20:28:46 +01001227 // Preserve files and exit. This sets the really_exiting flag to prevent
1228 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001229 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
Bram Moolenaar0f873732019-12-05 20:28:46 +01001231 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001232
Bram Moolenaar009b2592004-10-24 19:18:58 +00001233#ifdef NBDEBUG
1234 reset_signals();
1235 may_core_dump();
1236 abort();
1237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238}
1239
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001241 * Invoked after receiving SIGCONT. We don't know what happened while
1242 * sleeping, deal with part of that.
1243 */
1244 static void
1245after_sigcont(void)
1246{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001247 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1248 // again later.
1249 oldtitle_outdated = TRUE;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001250
Bram Moolenaar2e310482018-08-21 13:09:10 +02001251 settmode(TMODE_RAW);
1252 need_check_timestamps = TRUE;
1253 did_check_timestamps = FALSE;
1254}
1255
1256#if defined(SIGCONT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001257static void sigcont_handler SIGPROTOARG;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001258
1259/*
1260 * With multi-threading, suspending might not work immediately. Catch the
1261 * SIGCONT signal, which will be used as an indication whether the suspending
1262 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001263 *
1264 * On Linux, signal is not always handled immediately either.
1265 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001266 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001267 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001268 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001270static volatile sig_atomic_t sigcont_received;
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001271static void sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272
1273/*
1274 * signal handler for SIGCONT
1275 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001276 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277sigcont_handler SIGDEFARG(sigarg)
1278{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001279 if (in_mch_suspend)
1280 {
1281 sigcont_received = TRUE;
1282 }
1283 else
1284 {
1285 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1286 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1287 // back to a sane mode. We should redraw, but we can't really do that
1288 // in a signal handler, do a redraw later.
1289 after_sigcont();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001290 redraw_later(UPD_CLEAR);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001291 cursor_on_force();
1292 out_flush();
1293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294}
1295#endif
1296
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001297#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001298# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001299static void *clip_star_save = NULL;
1300static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001301# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001302
1303/*
1304 * Called when Vim is going to sleep or execute a shell command.
1305 * We can't respond to requests for the X selections. Lose them, otherwise
1306 * other applications will hang. But first copy the text to cut buffer 0.
1307 */
1308 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001309loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001310{
1311 if (clip_star.owned || clip_plus.owned)
1312 {
1313 x11_export_final_selection();
1314 if (clip_star.owned)
1315 clip_lose_selection(&clip_star);
1316 if (clip_plus.owned)
1317 clip_lose_selection(&clip_plus);
1318 if (x11_display != NULL)
1319 XFlush(x11_display);
1320 }
1321}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001322
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001323# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001324/*
1325 * Save clipboard text to restore later.
1326 */
1327 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001328save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001329{
1330 if (clip_star.owned)
1331 clip_star_save = get_register('*', TRUE);
1332 if (clip_plus.owned)
1333 clip_plus_save = get_register('+', TRUE);
1334}
1335
1336/*
1337 * Restore clipboard text if no one own the X selection.
1338 */
1339 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001340restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001341{
1342 if (clip_star_save != NULL)
1343 {
1344 if (!clip_gen_owner_exists(&clip_star))
1345 put_register('*', clip_star_save);
1346 else
1347 free_register(clip_star_save);
1348 clip_star_save = NULL;
1349 }
1350 if (clip_plus_save != NULL)
1351 {
1352 if (!clip_gen_owner_exists(&clip_plus))
1353 put_register('+', clip_plus_save);
1354 else
1355 free_register(clip_plus_save);
1356 clip_plus_save = NULL;
1357 }
1358}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001359# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001360#endif
1361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362/*
1363 * If the machine has job control, use it to suspend the program,
1364 * otherwise fake it by starting a new shell.
1365 */
1366 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001367mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001369 if (ignore_sigtstp)
1370 return;
1371
Bram Moolenaar041c7102020-05-30 18:14:57 +02001372#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001373 in_mch_suspend = TRUE;
1374
Bram Moolenaar0f873732019-12-05 20:28:46 +01001375 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001377 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
1379# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001380 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001382# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 sigcont_received = FALSE;
1384# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001385
Bram Moolenaar0f873732019-12-05 20:28:46 +01001386 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001387
1388# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001389 /*
1390 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001391 * immediately, but somehow not all the time, probably because it's handled
1392 * in another thread. Do not call pause() because there would be race
1393 * condition which would hang Vim if signal happened in between the test of
1394 * sigcont_received and the call to pause(). If signal is not yet received,
1395 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1396 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001397 */
1398 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001399 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001400
Bram Moolenaar262735e2009-07-14 10:20:22 +00001401 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001402 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001405 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
Bram Moolenaar2e310482018-08-21 13:09:10 +02001407 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408#else
1409 suspend_shell();
1410#endif
1411}
1412
1413 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001414mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
1416 Columns = 80;
1417 Rows = 24;
1418
1419 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001420
1421#ifdef SIGTSTP
1422 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1423 // that indicates the shell (or program) that launched us does not support
1424 // tty job control and thus we should ignore that signal. If invoked as a
1425 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
ichizok378447f2023-05-11 22:25:42 +01001426 ignore_sigtstp = restricted || SIG_IGN == mch_signal(SIGTSTP, SIG_ERR);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001429
Bram Moolenaar56718732006-03-15 22:53:57 +00001430#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001431 mac_conv_init();
1432#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001433#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1434 win_clip_init();
1435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436}
1437
1438 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001439set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
1441#if defined(SIGWINCH)
1442 /*
1443 * WINDOW CHANGE signal is handled with sig_winch().
1444 */
ichizok378447f2023-05-11 22:25:42 +01001445 mch_signal(SIGWINCH, sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446#endif
1447
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001449 // See mch_init() for the conditions under which we ignore SIGTSTP.
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001450 // In the GUI default TSTP processing is OK.
1451 // Checking both gui.in_use and gui.starting because gui.in_use is not set
1452 // at this point (set after menus are displayed), but gui.starting is set.
ichizok378447f2023-05-11 22:25:42 +01001453 mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001454# ifdef FEAT_GUI
1455 : gui.in_use || gui.starting ? SIG_DFL
1456# endif
ichizok378447f2023-05-11 22:25:42 +01001457 : sig_tstp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001459#if defined(SIGCONT)
ichizok378447f2023-05-11 22:25:42 +01001460 mch_signal(SIGCONT, sigcont_handler);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001462#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 /*
1464 * We want to ignore breaking of PIPEs.
1465 */
ichizok378447f2023-05-11 22:25:42 +01001466 mch_signal(SIGPIPE, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467#endif
1468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001470 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471#endif
1472
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001473#ifdef SIGUSR1
1474 /*
1475 * Call user's handler on SIGUSR1
1476 */
ichizok378447f2023-05-11 22:25:42 +01001477 mch_signal(SIGUSR1, catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001478#endif
1479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 /*
1481 * Ignore alarm signals (Perl's alarm() generates it).
1482 */
1483#ifdef SIGALRM
ichizok378447f2023-05-11 22:25:42 +01001484 mch_signal(SIGALRM, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485#endif
1486
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001487#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 /*
1489 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1490 * work will be lost.
1491 */
ichizok378447f2023-05-11 22:25:42 +01001492 mch_signal(SIGPWR, catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493#endif
1494
1495 /*
1496 * Arrange for other signals to gracefully shutdown Vim.
1497 */
1498 catch_signals(deathtrap, SIG_ERR);
1499
1500#if defined(FEAT_GUI) && defined(SIGHUP)
1501 /*
1502 * When the GUI is running, ignore the hangup signal.
1503 */
1504 if (gui.in_use)
ichizok378447f2023-05-11 22:25:42 +01001505 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506#endif
1507}
1508
Bram Moolenaardf177f62005-02-22 08:39:57 +00001509#if defined(SIGINT) || defined(PROTO)
1510/*
1511 * Catch CTRL-C (only works while in Cooked mode).
1512 */
1513 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001514catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001515{
ichizok378447f2023-05-11 22:25:42 +01001516 mch_signal(SIGINT, catch_sigint);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001517}
1518#endif
1519
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001521reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001524#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001525 // SIGCONT isn't in the list, because its default action is ignore
ichizok378447f2023-05-11 22:25:42 +01001526 mch_signal(SIGCONT, SIG_DFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527#endif
1528}
1529
1530 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001531catch_signals(
Michael Jarvisbe9624e2023-04-19 20:28:48 +01001532 void (*func_deadly)(int),
1533 void (*func_other)(int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534{
1535 int i;
1536
1537 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001538 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 if (signal_info[i].deadly)
1540 {
1541#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1542 struct sigaction sa;
1543
Bram Moolenaar0f873732019-12-05 20:28:46 +01001544 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 sa.sa_handler = func_deadly;
1546 sigemptyset(&sa.sa_mask);
1547# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001548 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1549 // thread handling in combination with using the alternate stack:
1550 // pthread library functions try to use the stack pointer to
1551 // identify the current thread, causing a SEGV signal, which
1552 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 sa.sa_flags = 0;
1554# else
1555 sa.sa_flags = SA_ONSTACK;
1556# endif
1557 sigaction(signal_info[i].sig, &sa, NULL);
1558#else
1559# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1560 struct sigvec sv;
1561
Bram Moolenaar0f873732019-12-05 20:28:46 +01001562 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 sv.sv_handler = func_deadly;
1564 sv.sv_mask = 0;
1565 sv.sv_flags = SV_ONSTACK;
1566 sigvec(signal_info[i].sig, &sv, NULL);
1567# else
ichizok378447f2023-05-11 22:25:42 +01001568 mch_signal(signal_info[i].sig, func_deadly);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569# endif
1570#endif
1571 }
1572 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001573 {
1574 // Deal with non-deadly signals.
1575#ifdef SIGTSTP
ichizok378447f2023-05-11 22:25:42 +01001576 mch_signal(signal_info[i].sig,
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001577 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1578 ? SIG_IGN : func_other);
1579#else
ichizok378447f2023-05-11 22:25:42 +01001580 mch_signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001581#endif
1582 }
1583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584}
1585
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001586#ifdef HAVE_SIGPROCMASK
1587 static void
1588block_signals(sigset_t *set)
1589{
1590 sigset_t newset;
1591 int i;
1592
1593 sigemptyset(&newset);
1594
1595 for (i = 0; signal_info[i].sig != -1; i++)
1596 sigaddset(&newset, signal_info[i].sig);
1597
Bram Moolenaar2e310482018-08-21 13:09:10 +02001598# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001599 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001600 sigaddset(&newset, SIGCONT);
1601# endif
1602
1603 sigprocmask(SIG_BLOCK, &newset, set);
1604}
1605
1606 static void
1607unblock_signals(sigset_t *set)
1608{
1609 sigprocmask(SIG_SETMASK, set, NULL);
1610}
1611#endif
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001614 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001615 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1616 * return TRUE
1617 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1618 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001619 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001620 * Returns TRUE when Vim should exit.
1621 */
1622 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001623vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001624{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001625 static int got_signal = 0;
1626 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001627
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001628 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001629 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001630 case SIGNAL_BLOCK: blocked = TRUE;
1631 break;
1632
1633 case SIGNAL_UNBLOCK: blocked = FALSE;
1634 if (got_signal != 0)
1635 {
1636 kill(getpid(), got_signal);
1637 got_signal = 0;
1638 }
1639 break;
1640
1641 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001642 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001643 got_signal = sig;
1644#ifdef SIGPWR
1645 if (sig != SIGPWR)
1646#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001647 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001648 break;
1649 }
1650 return FALSE;
1651}
1652
1653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 * Check_win checks whether we have an interactive stdout.
1655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001657mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if (isatty(1))
1660 return OK;
1661 return FAIL;
1662}
1663
1664/*
1665 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1666 */
1667 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001668mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
1670 if (isatty(read_cmd_fd))
1671 return TRUE;
1672 return FALSE;
1673}
1674
1675#ifdef FEAT_X11
1676
Bram Moolenaar651fca82021-11-29 20:39:38 +00001677# if defined(ELAPSED_TIMEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679/*
1680 * Give a message about the elapsed time for opening the X window.
1681 */
1682 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001683xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001685 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686}
1687# endif
1688#endif
1689
Bram Moolenaar651fca82021-11-29 20:39:38 +00001690#if defined(FEAT_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691/*
1692 * A few functions shared by X11 title and clipboard code.
1693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
1695static int got_x_error = FALSE;
1696
1697/*
1698 * X Error handler, otherwise X just exits! (very rude) -- webb
1699 */
1700 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001701x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001703 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 STRCAT(IObuff, _("\nVim: Got X error\n"));
1705
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001706 // In the GUI we cannot print a message and continue, because no X calls
1707 // are allowed here (causes my system to hang). Silently continuing seems
1708 // like the best alternative. Do preserve files, in case we crash.
1709 ml_sync_all(FALSE, FALSE);
1710
1711#ifdef FEAT_GUI
1712 if (!gui.in_use)
1713#endif
1714 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
Bram Moolenaar0f873732019-12-05 20:28:46 +01001716 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717}
1718
1719/*
1720 * Another X Error handler, just used to check for errors.
1721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001723x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 got_x_error = TRUE;
1726 return 0;
1727}
1728
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001729/*
1730 * Return TRUE when connection to the X server is desired.
1731 */
1732 static int
1733x_connect_to_server(void)
1734{
1735 // No point in connecting if we are exiting or dying.
1736 if (exiting || v_dying)
1737 return FALSE;
1738
1739#if defined(FEAT_CLIENTSERVER)
1740 if (x_force_connect)
1741 return TRUE;
1742#endif
1743 if (x_no_connect)
1744 return FALSE;
1745
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001746 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001747 if (clip_exclude_prog != NULL)
1748 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001749 // Just in case we get called recursively, return FALSE. This could
1750 // happen if vpeekc() is used while executing the prog and it causes a
1751 // related callback to be invoked.
1752 if (regprog_in_use(clip_exclude_prog))
1753 return FALSE;
1754
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001755 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1756 return FALSE;
1757 }
1758 return TRUE;
1759}
1760
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001762# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763/*
1764 * An X IO Error handler, used to catch error while opening the display.
1765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001767x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001769 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001771# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001772 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001773# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774}
1775# endif
1776
1777/*
1778 * An X IO Error handler, used to catch terminal errors.
1779 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001780static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001783x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
1785 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001786 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 x11_window = 0;
1788 x11_display = NULL;
1789 xterm_Shell = (Widget)0;
1790
Bram Moolenaar0f873732019-12-05 20:28:46 +01001791 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001793# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001794 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001795# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001797
1798/*
1799 * If the X11 connection was lost try to restore it.
1800 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001801 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001802 */
1803 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001804may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001805{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001806 // No point in restoring the connecting if we are exiting or dying.
1807 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001808 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001809 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001810
1811# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001812 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001813 if (app_context != (XtAppContext)NULL)
1814 {
1815 XtDestroyApplicationContext(app_context);
1816 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001817 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001818 }
1819# endif
1820
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001821 setup_term_clip();
1822 get_x11_title(FALSE);
1823 }
1824}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001825
1826 void
1827ex_xrestore(exarg_T *eap)
1828{
1829 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1830 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001831 if (xterm_display_allocated)
1832 vim_free(xterm_display);
1833 xterm_display = (char *)vim_strsave(eap->arg);
1834 xterm_display_allocated = TRUE;
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001835 }
1836 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001837 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001838
1839 clear_xterm_clip();
1840 x11_window = 0;
1841 xterm_dpy_retry_count = 5; // Try reconnecting five times
1842 may_restore_clipboard();
1843}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844#endif
1845
1846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 * Test if "dpy" and x11_window are valid by getting the window title.
1848 * I don't actually want it yet, so there may be a simpler call to use, but
1849 * this will cause the error handler x_error_check() to be called if anything
1850 * is wrong, such as the window pointer being invalid (as can happen when the
1851 * user changes his DISPLAY, but not his WINDOWID) -- webb
1852 */
1853 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001854test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855{
1856 int (*old_handler)();
1857 XTextProperty text_prop;
1858
1859 old_handler = XSetErrorHandler(x_error_check);
1860 got_x_error = FALSE;
1861 if (XGetWMName(dpy, x11_window, &text_prop))
1862 XFree((void *)text_prop.value);
1863 XSync(dpy, False);
1864 (void)XSetErrorHandler(old_handler);
1865
1866 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001867 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
1869 return (got_x_error ? FAIL : OK);
1870}
1871#endif
1872
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
1874#ifdef FEAT_X11
1875
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001876static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878/*
1879 * try to get x11 window and display
1880 *
1881 * return FAIL for failure, OK otherwise
1882 */
1883 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001884get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885{
1886 char *winid;
1887 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001888#define XD_NONE 0 // x11_display not set here
1889#define XD_HERE 1 // x11_display opened here
1890#define XD_GUI 2 // x11_display used from gui.dpy
1891#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 static int x11_display_from = XD_NONE;
1893 static int did_set_error_handler = FALSE;
1894
1895 if (!did_set_error_handler)
1896 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001897 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 (void)XSetErrorHandler(x_error_handler);
1899 did_set_error_handler = TRUE;
1900 }
1901
Bram Moolenaar9372a112005-12-06 19:59:18 +00001902#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (gui.in_use)
1904 {
1905 /*
1906 * If the X11 display was opened here before, for the window where Vim
1907 * was started, close that one now to avoid a memory leak.
1908 */
1909 if (x11_display_from == XD_HERE && x11_display != NULL)
1910 {
1911 XCloseDisplay(x11_display);
1912 x11_display_from = XD_NONE;
1913 }
1914 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1915 {
1916 x11_display_from = XD_GUI;
1917 return OK;
1918 }
1919 x11_display = NULL;
1920 return FAIL;
1921 }
1922 else if (x11_display_from == XD_GUI)
1923 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001924 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 x11_window = 0;
1926 x11_display = NULL;
1927 x11_display_from = XD_NONE;
1928 }
1929#endif
1930
Bram Moolenaar0f873732019-12-05 20:28:46 +01001931 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 if (!x_connect_to_server())
1933 return FAIL;
1934
1935 /*
1936 * If WINDOWID not set, should try another method to find out
1937 * what the current window number is. The only code I know for
1938 * this is very complicated.
1939 * We assume that zero is invalid for WINDOWID.
1940 */
1941 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1942 x11_window = (Window)atol(winid);
1943
1944#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001945 if (xterm_dpy == x11_display)
1946 // x11_display may have been set to xterm_dpy elsewhere
1947 x11_display_from = XD_XTERM;
1948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (xterm_dpy != NULL && x11_window != 0)
1950 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001951 // We may have checked it already, but Gnome terminal can move us to
1952 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001953 if (x11_display_from != XD_XTERM)
1954 {
1955 /*
1956 * If the X11 display was opened here before, for the window where
1957 * Vim was started, close that one now to avoid a memory leak.
1958 */
1959 if (x11_display_from == XD_HERE && x11_display != NULL)
1960 XCloseDisplay(x11_display);
1961 x11_display = xterm_dpy;
1962 x11_display_from = XD_XTERM;
1963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (test_x11_window(x11_display) == FAIL)
1965 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001966 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 x11_window = 0;
1968 x11_display = NULL;
1969 x11_display_from = XD_NONE;
1970 return FAIL;
1971 }
1972 return OK;
1973 }
1974#endif
1975
1976 if (x11_window == 0 || x11_display == NULL)
1977 result = -1;
1978
Bram Moolenaar0f873732019-12-05 20:28:46 +01001979 if (result != -1) // Have already been here and set this
1980 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981
1982 if (x11_window != 0 && x11_display == NULL)
1983 {
1984#ifdef SET_SIG_ALARM
ichizok378447f2023-05-11 22:25:42 +01001985 sighandler_T sig_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001987#ifdef ELAPSED_FUNC
1988 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
1990 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001991 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992#endif
1993
1994#ifdef SET_SIG_ALARM
1995 /*
1996 * Opening the Display may hang if the DISPLAY setting is wrong, or
1997 * the network connection is bad. Set an alarm timer to get out.
1998 */
1999 sig_alarm_called = FALSE;
ichizok378447f2023-05-11 22:25:42 +01002000 sig_save = mch_signal(SIGALRM, sig_alarm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 alarm(2);
2002#endif
2003 x11_display = XOpenDisplay(NULL);
2004
2005#ifdef SET_SIG_ALARM
2006 alarm(0);
ichizok378447f2023-05-11 22:25:42 +01002007 mch_signal(SIGALRM, sig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01002009 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010#endif
2011 if (x11_display != NULL)
2012 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002013# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00002015 {
2016 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002017 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00002018 verbose_leave();
2019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020# endif
2021 if (test_x11_window(x11_display) == FAIL)
2022 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002023 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 x11_window = 0;
2025 XCloseDisplay(x11_display);
2026 x11_display = NULL;
2027 }
2028 else
2029 x11_display_from = XD_HERE;
2030 }
2031 }
2032 if (x11_window == 0 || x11_display == NULL)
2033 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02002034
2035# ifdef FEAT_EVAL
2036 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
2037# endif
2038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 return (result = OK);
2040}
2041
2042/*
2043 * Determine original x11 Window Title
2044 */
2045 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002046get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047{
Bram Moolenaar47136d72004-10-12 20:02:24 +00002048 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049}
2050
2051/*
2052 * Determine original x11 Window icon
2053 */
2054 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002055get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056{
2057 int retval = FALSE;
2058
2059 retval = get_x11_thing(FALSE, test_only);
2060
Bram Moolenaar0f873732019-12-05 20:28:46 +01002061 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (oldicon == NULL && !test_only)
2063 {
2064 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002065 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002067 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 }
2069
2070 return retval;
2071}
2072
2073 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002074get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002075 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01002076 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077{
2078 XTextProperty text_prop;
2079 int retval = FALSE;
2080 Status status;
2081
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002082 if (get_x11_windis() != OK)
2083 return FALSE;
2084
2085 // Get window/icon name if any
2086 if (get_title)
2087 status = XGetWMName(x11_display, x11_window, &text_prop);
2088 else
2089 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2090
2091 /*
2092 * If terminal is xterm, then x11_window may be a child window of the
2093 * outer xterm window that actually contains the window/icon name, so
2094 * keep traversing up the tree until a window with a title/icon is
2095 * found.
2096 */
2097 // Previously this was only done for xterm and alike. I don't see a
2098 // reason why it would fail for other terminal emulators.
2099 // if (term_is_xterm)
2100 Window root;
2101 Window parent;
2102 Window win = x11_window;
2103 Window *children;
2104 unsigned int num_children;
2105
2106 while (!status || text_prop.value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002108 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2109 &num_children))
2110 break;
2111 if (children)
2112 XFree((void *)children);
2113 if (parent == root || parent == 0)
2114 break;
2115
2116 win = parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002118 status = XGetWMName(x11_display, win, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002120 status = XGetWMIconName(x11_display, win, &text_prop);
2121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002123 if (status && text_prop.value != NULL)
2124 {
2125 retval = TRUE;
2126 if (!test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002128 if (get_title)
2129 vim_free(oldtitle);
2130 else
2131 vim_free(oldicon);
2132 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002135 oldtitle = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002137 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002139 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002141 char **cl;
2142 Status transform_status;
2143 int n = 0;
2144
2145 transform_status = XmbTextPropertyToTextList(x11_display,
2146 &text_prop,
2147 &cl, &n);
2148 if (transform_status >= Success && n > 0 && cl[0])
2149 {
2150 if (get_title)
2151 oldtitle = vim_strsave((char_u *) cl[0]);
2152 else
2153 oldicon = vim_strsave((char_u *) cl[0]);
2154 XFreeStringList(cl);
2155 }
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002156 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (get_title)
2159 oldtitle = vim_strsave((char_u *)text_prop.value);
2160 else
2161 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002165 XFree((void *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
2167 return retval;
2168}
2169
Bram Moolenaar0f873732019-12-05 20:28:46 +01002170// Xutf8 functions are not available on older systems. Note that on some
2171// systems X_HAVE_UTF8_STRING may be defined in a header file but
2172// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2173// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002174#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002175# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176# define USE_UTF8_STRING
2177# endif
2178#endif
2179
2180/*
2181 * Set x11 Window Title
2182 *
2183 * get_x11_windis() must be called before this and have returned OK
2184 */
2185 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002186set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002188 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2189 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2190 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191#ifdef USE_UTF8_STRING
2192 if (enc_utf8)
2193 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2194 NULL, NULL, 0, NULL, NULL, NULL);
2195 else
2196#endif
2197 {
2198#if XtSpecificationRelease >= 4
2199# ifdef FEAT_XFONTSET
2200 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2201 NULL, NULL, 0, NULL, NULL, NULL);
2202# else
2203 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002204 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205
Bram Moolenaar0f873732019-12-05 20:28:46 +01002206 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002207 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 XSetWMProperties(x11_display, x11_window, &text_prop,
2209 NULL, NULL, 0, NULL, NULL, NULL);
2210# endif
2211#else
2212 XStoreName(x11_display, x11_window, (char *)title);
2213#endif
2214 }
2215 XFlush(x11_display);
2216}
2217
2218/*
2219 * Set x11 Window icon
2220 *
2221 * get_x11_windis() must be called before this and have returned OK
2222 */
2223 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002224set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002226 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227#ifdef USE_UTF8_STRING
2228 if (enc_utf8)
2229 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2230 NULL, 0, NULL, NULL, NULL);
2231 else
2232#endif
2233 {
2234#if XtSpecificationRelease >= 4
2235# ifdef FEAT_XFONTSET
2236 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2237 NULL, 0, NULL, NULL, NULL);
2238# else
2239 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002240 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002242 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2244 NULL, 0, NULL, NULL, NULL);
2245# endif
2246#else
2247 XSetIconName(x11_display, x11_window, (char *)icon);
2248#endif
2249 }
2250 XFlush(x11_display);
2251}
2252
Bram Moolenaar0f873732019-12-05 20:28:46 +01002253#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002256get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257{
2258 return FALSE;
2259}
2260
2261 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002262get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263{
2264 if (!test_only)
2265 {
2266 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002267 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002269 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271 return FALSE;
2272}
2273
Bram Moolenaar0f873732019-12-05 20:28:46 +01002274#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275
2276 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002277mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
2279 return get_x11_title(TRUE);
2280}
2281
2282 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002283mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
2285 return get_x11_icon(TRUE);
2286}
2287
2288/*
2289 * Set the window title and icon.
2290 */
2291 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002292mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
2294 int type = 0;
2295 static int recursive = 0;
2296
Bram Moolenaar0f873732019-12-05 20:28:46 +01002297 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002299 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 return;
2301
Bram Moolenaar0f873732019-12-05 20:28:46 +01002302 // When one of the X11 functions causes a deadly signal, we get here again
2303 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (recursive)
2305 return;
2306 ++recursive;
2307
2308 /*
2309 * if the window ID and the display is known, we may use X11 calls
2310 */
2311#ifdef FEAT_X11
2312 if (get_x11_windis() == OK)
2313 type = 1;
2314#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002315# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002316 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 if (gui.in_use)
2318 type = 1;
2319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320#endif
2321
2322 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002323 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 * than x11 calls, because the x11 calls don't always work
2325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 if ((type || *T_TS != NUL) && title != NULL)
2327 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002328 if (oldtitle_outdated)
2329 {
2330 oldtitle_outdated = FALSE;
2331 VIM_CLEAR(oldtitle);
2332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (oldtitle == NULL
2334#ifdef FEAT_GUI
2335 && !gui.in_use
2336#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002337 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 (void)get_x11_title(FALSE);
2339
Bram Moolenaar0f873732019-12-05 20:28:46 +01002340 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 term_settitle(title);
2342#ifdef FEAT_X11
2343 else
2344# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002345 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002347 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002349#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002350 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 else
2352 gui_mch_settitle(title, icon);
2353#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002354 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356
2357 if ((type || *T_CIS != NUL) && icon != NULL)
2358 {
2359 if (oldicon == NULL
2360#ifdef FEAT_GUI
2361 && !gui.in_use
2362#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002363 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 get_x11_icon(FALSE);
2365
2366 if (*T_CIS != NUL)
2367 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002368 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002370 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 out_flush();
2372 }
2373#ifdef FEAT_X11
2374 else
2375# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002376 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002378 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379#endif
2380 did_set_icon = TRUE;
2381 }
2382 --recursive;
2383}
2384
2385/*
2386 * Restore the window/icon title.
2387 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002388 * SAVE_RESTORE_TITLE only restore title
2389 * SAVE_RESTORE_ICON only restore icon
2390 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 */
2392 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002393mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394{
Bram Moolenaardac13472019-09-16 21:06:21 +02002395 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002396
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002397 // Only restore the title or icon when it has been set.
2398 // When using "oldtitle" make a copy, it might be freed halfway.
2399 char_u *title = ((which & SAVE_RESTORE_TITLE) && unix_did_set_title)
2400 ? (oldtitle ? oldtitle : p_titleold) : NULL;
2401 char_u *tofree = NULL;
2402 if (title == oldtitle && oldtitle != NULL)
2403 {
2404 tofree = vim_strsave(title);
2405 if (tofree != NULL)
2406 title = tofree;
2407 }
2408 mch_settitle(title,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002409 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002410 vim_free(tofree);
Bram Moolenaar40385db2018-08-07 22:31:44 +02002411
Bram Moolenaare5c83282019-05-03 23:15:37 +02002412 if (do_push_pop)
2413 {
2414 // pop and push from/to the stack
2415 term_pop_title(which);
2416 term_push_title(which);
2417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418}
2419
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
2421/*
2422 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002423 * This matches "xterm.*", thus "xterm-256color", "xterm-kitty", etc.
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002424 * Do not consider "xterm-kitty" an xterm, it is not fully xterm compatible,
2425 * using the "xterm-kitty" terminfo entry should work better.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002426 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 */
2428 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002429vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
2431 if (name == NULL)
2432 return FALSE;
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002433 return ((STRNICMP(name, "xterm", 5) == 0
2434 && STRNICMP(name, "xterm-kitty", 11) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 || STRNICMP(name, "nxterm", 6) == 0
2436 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002437 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002439 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 || STRCMP(name, "builtin_xterm") == 0);
2441}
2442
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002443#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2444/*
2445 * Return TRUE if "name" appears to be that of a terminal
2446 * known to support the xterm-style mouse protocol.
2447 * Relies on term_is_xterm having been set to its correct value.
2448 */
2449 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002450use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002451{
2452 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002453 && (term_is_xterm
2454 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002455 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002456 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002457 || STRICMP(name, "st") == 0
2458 || STRNICMP(name, "st-", 3) == 0
2459 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002460}
2461#endif
2462
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463/*
2464 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2465 * Return 1 for "xterm".
2466 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002467 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002468 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 */
2470 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002471use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002473 if (ttym_flags == TTYM_SGR)
2474 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002475 if (ttym_flags == TTYM_URXVT)
2476 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 if (ttym_flags == TTYM_XTERM2)
2478 return 2;
2479 if (ttym_flags == TTYM_XTERM)
2480 return 1;
2481 return 0;
2482}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002484/*
2485 * Return TRUE if "name" is an iris-ansi terminal name.
2486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002488vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489{
2490 if (name == NULL)
2491 return FALSE;
2492 return (STRNICMP(name, "iris-ansi", 9) == 0
2493 || STRCMP(name, "builtin_iris-ansi") == 0);
2494}
2495
Dominique Pellee764d1b2023-03-12 21:20:59 +00002496#if defined(VMS) || defined(PROTO)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002497/*
2498 * Return TRUE if "name" is a vt300-like terminal name.
2499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002501vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
2503 if (name == NULL)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002504 return FALSE;
2505 // Actually all ANSI compatible terminals should be here.
2506 // Catch at least VT1xx - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002507 return ((STRNICMP(name, "vt", 2) == 0
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002508 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 || STRCMP(name, "builtin_vt320") == 0);
2510}
Dominique Pellee764d1b2023-03-12 21:20:59 +00002511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 * Insert user name in s[len].
2515 * Return OK if a name found.
2516 */
2517 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002518mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519{
2520#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002521 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 return OK;
2523#else
2524 return mch_get_uname(getuid(), s, len);
2525#endif
2526}
2527
2528/*
2529 * Insert user name for "uid" in s[len].
2530 * Return OK if a name found.
2531 */
2532 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002533mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
2535#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2536 struct passwd *pw;
2537
2538 if ((pw = getpwuid(uid)) != NULL
2539 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2540 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002541 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 return OK;
2543 }
2544#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002545 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2546 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547}
2548
2549/*
2550 * Insert host name is s[len].
2551 */
2552
2553#ifdef HAVE_SYS_UTSNAME_H
2554 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002555mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556{
2557 struct utsname vutsname;
2558
2559 if (uname(&vutsname) < 0)
2560 *s = NUL;
2561 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002562 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002564#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
2566# ifdef HAVE_SYS_SYSTEMINFO_H
2567# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2568# endif
2569
2570 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002571mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
2573# ifdef VAXC
2574 vaxc$gethostname((char *)s, len);
2575# else
2576 gethostname((char *)s, len);
2577# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002578 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002580#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
2582/*
2583 * return process ID
2584 */
2585 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002586mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587{
2588 return (long)getpid();
2589}
2590
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002591/*
2592 * return TRUE if process "pid" is still running
2593 */
2594 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002595mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002596{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002597 // If there is no error the process must be running.
2598 if (kill(pid, 0) == 0)
2599 return TRUE;
2600#ifdef ESRCH
2601 // If the error is ESRCH then the process is not running.
2602 if (errno == ESRCH)
2603 return FALSE;
2604#endif
2605 // If the process is running and owned by another user we get EPERM. With
2606 // other errors the process might be running, assuming it is then.
2607 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002608}
2609
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002612strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613{
2614 extern int sys_nerr;
2615 extern char *sys_errlist[];
2616 static char er[20];
2617
2618 if (err > 0 && err < sys_nerr)
2619 return (sys_errlist[err]);
2620 sprintf(er, "Error %d", err);
2621 return er;
2622}
2623#endif
2624
2625/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002626 * Get name of current directory into buffer "buf" of length "len" bytes.
2627 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 * Return OK for success, FAIL for failure.
2629 */
2630 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002631mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632{
2633#if defined(USE_GETCWD)
2634 if (getcwd((char *)buf, len) == NULL)
2635 {
2636 STRCPY(buf, strerror(errno));
2637 return FAIL;
2638 }
2639 return OK;
2640#else
2641 return (getwd((char *)buf) != NULL ? OK : FAIL);
2642#endif
2643}
2644
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002646 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 *
2648 * return FAIL for failure, OK for success
2649 */
2650 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002651mch_FullName(
2652 char_u *fname,
2653 char_u *buf,
2654 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002655 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
2657 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002658#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002660 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 char_u olddir[MAXPATHL];
2663 char_u *p;
2664 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002665#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002666 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2667 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002668#endif
2669
Bram Moolenaar38323e42007-03-06 19:22:53 +00002670#ifdef VMS
2671 fname = vms_fixfilename(fname);
2672#endif
2673
Bram Moolenaara2442432007-04-26 14:26:37 +00002674#ifdef __CYGWIN__
2675 /*
2676 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2677 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002678# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002679 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2680 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002681 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2682 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002683# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002684 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002685# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002686 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002687#endif
2688
Bram Moolenaar0f873732019-12-05 20:28:46 +01002689 // Expand it if forced or not an absolute path.
2690 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002691 if ((force || !mch_isFullName(fname))
2692 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 {
2694 /*
2695 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002696 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 * This will get the correct path name with "../" things.
2698 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002699 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002701 if (STRCMP(p, "/..") == 0)
2702 // for "/path/dir/.." include the "/.."
2703 p += 3;
2704
Bram Moolenaar38323e42007-03-06 19:22:53 +00002705#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 /*
2707 * Use fchdir() if possible, it's said to be faster and more
2708 * reliable. But on SunOS 4 it might not work. Check this by
2709 * doing a fchdir() right now.
2710 */
2711 if (!dont_fchdir)
2712 {
2713 fd = open(".", O_RDONLY | O_EXTRA, 0);
2714 if (fd >= 0 && fchdir(fd) < 0)
2715 {
2716 close(fd);
2717 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002718 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 }
2720 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
Bram Moolenaar0f873732019-12-05 20:28:46 +01002723 // Only change directory when we are sure we can return to where
2724 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002726#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 (mch_dirname(olddir, MAXPATHL) == FAIL
2730 || mch_chdir((char *)olddir) != 0))
2731 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002732 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 retval = FAIL;
2734 }
2735 else
2736 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002737 // The directory is copied into buf[], to be able to remove
2738 // the file name without changing it (could be a string in
2739 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 if (p - fname >= len)
2741 retval = FAIL;
2742 else
2743 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002744 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002746 {
2747 // Path does not exist (yet). For a full path fail,
2748 // will use the path as-is. For a relative path use
2749 // the current directory and append the file name.
2750 if (mch_isFullName(fname))
2751 retval = FAIL;
2752 else
2753 p = NULL;
2754 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002755 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002757 else
2758 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 *buf = NUL;
2760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
2762 }
2763 if (mch_dirname(buf, len) == FAIL)
2764 {
2765 retval = FAIL;
2766 *buf = NUL;
2767 }
2768 if (p != NULL)
2769 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002770#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 if (fd >= 0)
2772 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002773 if (p_verbose >= 5)
2774 {
2775 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002776 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002777 verbose_leave();
2778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 }
2781 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 l = mch_chdir((char *)olddir);
2784 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002785 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
itchyny051a40c2021-10-20 10:00:05 +01002787#ifdef HAVE_FCHDIR
2788 if (fd >= 0)
2789 close(fd);
2790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791
2792 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002793 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002794 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002795#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002796 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002798 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002801
Bram Moolenaar0f873732019-12-05 20:28:46 +01002802 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002803 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 return FAIL;
2805
Bram Moolenaar0f873732019-12-05 20:28:46 +01002806 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 if (STRCMP(fname, ".") != 0)
2808 STRCAT(buf, fname);
2809
2810 return OK;
2811}
2812
2813/*
2814 * Return TRUE if "fname" does not depend on the current directory.
2815 */
2816 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002817mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002819#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 return ( fname[0] == '/' || fname[0] == '.' ||
2821 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2822 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2823 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002824#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826#endif
2827}
2828
Bram Moolenaar24552be2005-12-10 20:17:30 +00002829#if defined(USE_FNAME_CASE) || defined(PROTO)
2830/*
2831 * Set the case of the file name, if it already exists. This will cause the
2832 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002833 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002834 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002835 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002836fname_case(
2837 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002838 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002839{
2840 struct stat st;
2841 char_u *slash, *tail;
2842 DIR *dirp;
2843 struct dirent *dp;
2844
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002845 if (mch_lstat((char *)name, &st) < 0)
2846 return;
2847
2848 // Open the directory where the file is located.
2849 slash = vim_strrchr(name, '/');
2850 if (slash == NULL)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002851 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002852 dirp = opendir(".");
2853 tail = name;
2854 }
2855 else
2856 {
2857 *slash = NUL;
2858 dirp = opendir((char *)name);
2859 *slash = '/';
2860 tail = slash + 1;
2861 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002862
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002863 if (dirp == NULL)
2864 return;
2865
2866 while ((dp = readdir(dirp)) != NULL)
2867 {
2868 // Only accept names that differ in case and are the same byte
2869 // length. TODO: accept different length name.
2870 if (STRICMP(tail, dp->d_name) == 0
2871 && STRLEN(tail) == STRLEN(dp->d_name))
Bram Moolenaar24552be2005-12-10 20:17:30 +00002872 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002873 char_u newname[MAXPATHL + 1];
2874 struct stat st2;
2875
2876 // Verify the inode is equal.
2877 vim_strncpy(newname, name, MAXPATHL);
2878 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2879 MAXPATHL - (tail - name));
2880 if (mch_lstat((char *)newname, &st2) >= 0
2881 && st.st_ino == st2.st_ino
2882 && st.st_dev == st2.st_dev)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002883 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002884 STRCPY(tail, dp->d_name);
2885 break;
Bram Moolenaar24552be2005-12-10 20:17:30 +00002886 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002887 }
2888 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002889
2890 closedir(dirp);
Bram Moolenaar24552be2005-12-10 20:17:30 +00002891}
2892#endif
2893
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894/*
2895 * Get file permissions for 'name'.
2896 * Returns -1 when it doesn't exist.
2897 */
2898 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002899mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
2901 struct stat statb;
2902
Bram Moolenaar0f873732019-12-05 20:28:46 +01002903 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904#ifdef VMS
2905 if (stat((char *)vms_fixfilename(name), &statb))
2906#else
2907 if (stat((char *)name, &statb))
2908#endif
2909 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002910#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002911 // The top bit makes the value negative, which means the file doesn't
2912 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002913 return statb.st_mode & ~S_ADDACE;
2914#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917}
2918
2919/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002920 * Set file permission for "name" to "perm".
2921 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 */
2923 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002924mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925{
2926 return (chmod((char *)
2927#ifdef VMS
2928 vms_fixfilename(name),
2929#else
2930 name,
2931#endif
2932 (mode_t)perm) == 0 ? OK : FAIL);
2933}
2934
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002935#if defined(HAVE_FCHMOD) || defined(PROTO)
2936/*
2937 * Set file permission for open file "fd" to "perm".
2938 * Return FAIL for failure, OK otherwise.
2939 */
2940 int
2941mch_fsetperm(int fd, long perm)
2942{
2943 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2944}
2945#endif
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947#if defined(HAVE_ACL) || defined(PROTO)
2948# ifdef HAVE_SYS_ACL_H
2949# include <sys/acl.h>
2950# endif
2951# ifdef HAVE_SYS_ACCESS_H
2952# include <sys/access.h>
2953# endif
2954
2955# ifdef HAVE_SOLARIS_ACL
2956typedef struct vim_acl_solaris_T {
2957 int acl_cnt;
2958 aclent_t *acl_entry;
2959} vim_acl_solaris_T;
2960# endif
2961
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002962#if defined(HAVE_SELINUX) || defined(PROTO)
2963/*
2964 * Copy security info from "from_file" to "to_file".
2965 */
2966 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002967mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002968{
2969 if (from_file == NULL)
2970 return;
2971
2972 if (selinux_enabled == -1)
2973 selinux_enabled = is_selinux_enabled();
2974
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002975 if (selinux_enabled <= 0)
2976 return;
2977
2978 // Use "char *" instead of "security_context_t" to avoid a deprecation
2979 // warning.
2980 char *from_context = NULL;
2981 char *to_context = NULL;
2982
2983 if (getfilecon((char *)from_file, &from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002984 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002985 // If the filesystem doesn't support extended attributes,
2986 // the original had no special security context and the
2987 // target cannot have one either.
2988 if (errno == EOPNOTSUPP)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002989 return;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002990
2991 msg_puts(_("\nCould not get security context for "));
2992 msg_outtrans(from_file);
2993 msg_putchar('\n');
2994 return;
2995 }
2996 if (getfilecon((char *)to_file, &to_context) < 0)
2997 {
2998 msg_puts(_("\nCould not get security context for "));
2999 msg_outtrans(to_file);
3000 msg_putchar('\n');
3001 freecon (from_context);
3002 return ;
3003 }
3004 if (strcmp(from_context, to_context) != 0)
3005 {
3006 if (setfilecon((char *)to_file, from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003007 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003008 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003009 msg_outtrans(to_file);
3010 msg_putchar('\n');
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003011 }
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003012 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003013 freecon(to_context);
3014 freecon(from_context);
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003015}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003016#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003017
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003018#if defined(HAVE_SMACK) && !defined(PROTO)
3019/*
3020 * Copy security info from "from_file" to "to_file".
3021 */
3022 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01003023mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003024{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02003025 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003026 {
3027 XATTR_NAME_SMACK,
3028 XATTR_NAME_SMACKEXEC,
3029 XATTR_NAME_SMACKMMAP
3030 };
3031
3032 char buffer[SMACK_LABEL_LEN];
3033 const char *name;
3034 int index;
3035 int ret;
3036 ssize_t size;
3037
3038 if (from_file == NULL)
3039 return;
3040
3041 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
3042 / sizeof(smack_copied_attributes)[0]) ; index++)
3043 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003044 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003045 name = smack_copied_attributes[index];
3046
Bram Moolenaar0f873732019-12-05 20:28:46 +01003047 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003048 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
3049 if (size >= 0)
3050 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003051 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003052 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
3053 if (ret < 0)
3054 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003055 vim_snprintf((char *)IObuff, IOSIZE,
3056 _("Could not set security context %s for %s"),
3057 name, to_file);
3058 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003059 msg_putchar('\n');
3060 }
3061 }
3062 else
3063 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003064 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003065 switch (errno)
3066 {
3067 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003068 // extended attributes aren't supported or enabled
3069 // should a message be echoed? not sure...
3070 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003071
3072 case ERANGE:
3073 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003074 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003075 vim_snprintf((char *)IObuff, IOSIZE,
3076 _("Could not get security context %s for %s. Removing it!"),
3077 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003078 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003079 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003080 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003081
3082 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003083 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003084 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003085 // Silently ignore errors, apparently this happens when
3086 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003087 break;
3088 }
3089 }
3090 }
3091}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003092#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003093
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094/*
3095 * Return a pointer to the ACL of file "fname" in allocated memory.
3096 * Return NULL if the ACL is not available for whatever reason.
3097 */
3098 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003099mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100{
3101 vim_acl_T ret = NULL;
3102#ifdef HAVE_POSIX_ACL
3103 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3104#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003105#ifdef HAVE_SOLARIS_ZFS_ACL
3106 acl_t *aclent;
3107
3108 if (acl_get((char *)fname, 0, &aclent) < 0)
3109 return NULL;
3110 ret = (vim_acl_T)aclent;
3111#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112#ifdef HAVE_SOLARIS_ACL
3113 vim_acl_solaris_T *aclent;
3114
3115 aclent = malloc(sizeof(vim_acl_solaris_T));
3116 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3117 {
3118 free(aclent);
3119 return NULL;
3120 }
3121 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3122 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3123 {
3124 free(aclent->acl_entry);
3125 free(aclent);
3126 return NULL;
3127 }
3128 ret = (vim_acl_T)aclent;
3129#else
3130#if defined(HAVE_AIX_ACL)
3131 int aclsize;
3132 struct acl *aclent;
3133
3134 aclsize = sizeof(struct acl);
3135 aclent = malloc(aclsize);
3136 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3137 {
3138 if (errno == ENOSPC)
3139 {
3140 aclsize = aclent->acl_len;
3141 aclent = realloc(aclent, aclsize);
3142 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3143 {
3144 free(aclent);
3145 return NULL;
3146 }
3147 }
3148 else
3149 {
3150 free(aclent);
3151 return NULL;
3152 }
3153 }
3154 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003155#endif // HAVE_AIX_ACL
3156#endif // HAVE_SOLARIS_ACL
3157#endif // HAVE_SOLARIS_ZFS_ACL
3158#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 return ret;
3160}
3161
3162/*
3163 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3164 */
3165 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003166mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167{
3168 if (aclent == NULL)
3169 return;
3170#ifdef HAVE_POSIX_ACL
3171 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3172#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003173#ifdef HAVE_SOLARIS_ZFS_ACL
3174 acl_set((char *)fname, (acl_t *)aclent);
3175#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176#ifdef HAVE_SOLARIS_ACL
3177 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3178 ((vim_acl_solaris_T *)aclent)->acl_entry);
3179#else
3180#ifdef HAVE_AIX_ACL
3181 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003182#endif // HAVE_AIX_ACL
3183#endif // HAVE_SOLARIS_ACL
3184#endif // HAVE_SOLARIS_ZFS_ACL
3185#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186}
3187
3188 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003189mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190{
3191 if (aclent == NULL)
3192 return;
3193#ifdef HAVE_POSIX_ACL
3194 acl_free((acl_t)aclent);
3195#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003196#ifdef HAVE_SOLARIS_ZFS_ACL
3197 acl_free((acl_t *)aclent);
3198#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199#ifdef HAVE_SOLARIS_ACL
3200 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3201 free(aclent);
3202#else
3203#ifdef HAVE_AIX_ACL
3204 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003205#endif // HAVE_AIX_ACL
3206#endif // HAVE_SOLARIS_ACL
3207#endif // HAVE_SOLARIS_ZFS_ACL
3208#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209}
3210#endif
3211
3212/*
3213 * Set hidden flag for "name".
3214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003216mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003218 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219}
3220
3221/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003222 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 * return FALSE if "name" is not a directory
3224 * return FALSE for error
3225 */
3226 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003227mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228{
3229 struct stat statb;
3230
Bram Moolenaar0f873732019-12-05 20:28:46 +01003231 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 return FALSE;
3233 if (stat((char *)name, &statb))
3234 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236}
3237
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003238/*
3239 * return TRUE if "name" is a directory, NOT a symlink to a directory
3240 * return FALSE if "name" is not a directory
3241 * return FALSE for error
3242 */
3243 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003244mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003245{
3246 struct stat statb;
3247
Bram Moolenaar0f873732019-12-05 20:28:46 +01003248 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003249 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003250 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003251 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003252 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003253}
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255/*
3256 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3257 */
3258 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003259executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261 struct stat st;
3262
3263 if (stat((char *)name, &st))
3264 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003265#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003266 // Like on Unix system file can have executable rights but not necessarily
3267 // be an executable, but on Unix is not a default for an ordinary file to
3268 // have an executable flag - on VMS it is in most cases.
3269 // Therefore, this check does not have any sense - let keep us to the
3270 // conventions instead:
3271 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003272 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003273 int vms_executable = 0;
3274 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3275 {
3276 if (strstr(vms_tolower((char*)name),".exe") != NULL
3277 || strstr(vms_tolower((char*)name),".com")!= NULL)
3278 vms_executable = 1;
3279 }
3280 return vms_executable;
3281#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284}
3285
3286/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003287 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003288 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 * Return -1 if unknown.
3290 */
3291 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003292mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
3294 char_u *buf;
3295 char_u *p, *e;
3296 int retval;
3297
Bram Moolenaar0f873732019-12-05 20:28:46 +01003298 // When "use_path" is false and if it's an absolute or relative path don't
3299 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003300 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003301 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003302 // There must be a path separator, files in the current directory
3303 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003304 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003305 {
3306 if (path != NULL)
3307 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003308 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003309 *path = FullName_save(name, TRUE);
3310 else
3311 *path = vim_strsave(name);
3312 }
3313 return TRUE;
3314 }
3315 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317
3318 p = (char_u *)getenv("PATH");
3319 if (p == NULL || *p == NUL)
3320 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003321 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 if (buf == NULL)
3323 return -1;
3324
3325 /*
3326 * Walk through all entries in $PATH to check if "name" exists there and
3327 * is an executable file.
3328 */
3329 for (;;)
3330 {
3331 e = (char_u *)strchr((char *)p, ':');
3332 if (e == NULL)
3333 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003334 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 STRCPY(buf, "./");
3336 else
3337 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003338 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 add_pathsep(buf);
3340 }
3341 STRCAT(buf, name);
3342 retval = executable_file(buf);
3343 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003344 {
3345 if (path != NULL)
3346 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003347 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003348 *path = FullName_save(buf, TRUE);
3349 else
3350 *path = vim_strsave(buf);
3351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
3355 if (*e != ':')
3356 break;
3357 p = e + 1;
3358 }
3359
3360 vim_free(buf);
3361 return retval;
3362}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
3364/*
3365 * Check what "name" is:
3366 * NODE_NORMAL: file or directory (or doesn't exist)
3367 * NODE_WRITABLE: writable device, socket, fifo, etc.
3368 * NODE_OTHER: non-writable things
3369 */
3370 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003371mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
3373 struct stat st;
3374
3375 if (stat((char *)name, &st))
3376 return NODE_NORMAL;
3377 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3378 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003379 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003381 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 return NODE_WRITABLE;
3383}
3384
3385 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003386mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
3388#ifdef HAVE_CHECK_STACK_GROWTH
3389 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 check_stack_growth((char *)&i);
3392
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003393# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 get_stack_limit();
3395# endif
3396
3397#endif
3398
3399 /*
3400 * Setup an alternative stack for signals. Helps to catch signals when
3401 * running out of stack space.
3402 * Use of sigaltstack() is preferred, it's more portable.
3403 * Ignore any errors.
3404 */
3405#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003406 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 init_signal_stack();
3408#endif
3409}
3410
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003411#if defined(EXITFREE) || defined(PROTO)
3412 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003413mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003414{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003415# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3416 if (clip_star.owned)
3417 clip_lose_selection(&clip_star);
3418 if (clip_plus.owned)
3419 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003420# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003421# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003422 if (xterm_Shell != (Widget)0)
3423 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003424# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003425 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003426 if (xterm_dpy != NULL)
3427 XtCloseDisplay(xterm_dpy);
3428 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003429 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003430 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003431# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003432 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003433# endif
3434 }
3435# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003436# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003437# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003438 if (x11_display != NULL
3439# ifdef FEAT_XCLIPBOARD
3440 && x11_display != xterm_dpy
3441# endif
3442 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003443 XCloseDisplay(x11_display);
3444# endif
3445# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003446 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003447# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003448 vim_free(oldtitle);
3449 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003450}
3451#endif
3452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453/*
3454 * Output a newline when exiting.
3455 * Make sure the newline goes to the same stream as the text.
3456 */
3457 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003458exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003460 if (silent_mode)
3461 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 if (newline_on_exit || msg_didout)
3463 {
3464 if (msg_use_printf())
3465 {
3466 if (info_message)
3467 mch_msg("\n");
3468 else
3469 mch_errmsg("\r\n");
3470 }
3471 else
3472 out_char('\n');
3473 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003474 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003476 restore_cterm_colors(); // get original colors back
3477 msg_clr_eos_force(); // clear the rest of the display
3478 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480}
3481
Bram Moolenaarb4151682020-05-11 22:13:28 +02003482#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003483# if (defined(__GNUC__) \
3484 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3485 || (defined(__clang__) && (__clang_major__ >= 12))
3486extern void __gcov_dump(void);
3487extern void __gcov_reset(void);
3488# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3489# else
3490extern void __gcov_flush(void);
3491# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003492#endif
3493
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003495mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496{
3497 exiting = TRUE;
3498
3499#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3500 x11_export_final_selection();
3501#endif
3502
3503#ifdef FEAT_GUI
3504 if (!gui.in_use)
3505#endif
3506 {
3507 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003508 if (!is_not_a_term())
3509 {
3510 // restore xterm title and icon name
3511 mch_restore_title(SAVE_RESTORE_BOTH);
3512 term_pop_title(SAVE_RESTORE_BOTH);
3513 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003514
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 /*
3516 * When t_ti is not empty but it doesn't cause swapping terminal
3517 * pages, need to output a newline when msg_didout is set. But when
3518 * t_ti does swap pages it should not go to the shell page. Do this
3519 * before stoptermcap().
3520 */
3521 if (swapping_screen() && !newline_on_exit)
3522 exit_scroll();
3523
Bram Moolenaar0f873732019-12-05 20:28:46 +01003524 // Stop termcap: May need to check for T_CRV response, which
3525 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 stoptermcap();
3527
3528 /*
3529 * A newline is only required after a message in the alternate screen.
3530 * This is set to TRUE by wait_return().
3531 */
3532 if (!swapping_screen() || newline_on_exit)
3533 exit_scroll();
3534
Bram Moolenaar0f873732019-12-05 20:28:46 +01003535 // Cursor may have been switched off without calling starttermcap()
3536 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 if (full_screen)
3538 cursor_on();
3539 }
3540 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003541 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003542
3543#ifdef USE_GCOV_FLUSH
3544 // Flush coverage info before possibly being killed by a deadly signal.
3545 __gcov_flush();
3546#endif
3547
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 may_core_dump();
3549#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 gui_exit(r);
3552#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003553
Bram Moolenaar56718732006-03-15 22:53:57 +00003554#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003555 mac_conv_cleanup();
3556#endif
3557
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003559 // A core dump won't be created if the signal handler
3560 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (deadly_signal != 0)
3562 return;
3563#endif
3564
Bram Moolenaar009b2592004-10-24 19:18:58 +00003565#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003566 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003567#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003568
3569#ifdef EXITFREE
3570 free_all_mem();
3571#endif
3572
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 exit(r);
3574}
3575
3576 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003577may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
3579 if (deadly_signal != 0)
3580 {
ichizok378447f2023-05-11 22:25:42 +01003581 mch_signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003582 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
3584}
3585
3586#ifndef VMS
3587
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003588/*
3589 * Get the file descriptor to use for tty operations.
3590 */
3591 static int
3592get_tty_fd(int fd)
3593{
3594 int tty_fd = fd;
3595
3596#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3597 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3598 // "fd" when it is a master device.
3599 if (mch_isatty(fd) > 1)
3600 {
3601 char *name;
3602
3603 name = ptsname(fd);
3604 if (name == NULL)
3605 return -1;
3606
3607 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3608 if (tty_fd < 0)
3609 return -1;
3610 }
3611#endif
3612 return tty_fd;
3613}
3614
3615 static int
3616mch_tcgetattr(int fd, void *term)
3617{
3618 int tty_fd;
3619 int retval = -1;
3620
3621 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003622 if (tty_fd < 0)
3623 return -1;
3624
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003625#ifdef NEW_TTY_SYSTEM
3626# ifdef HAVE_TERMIOS_H
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003627 retval = tcgetattr(tty_fd, (struct termios *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003628# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003629 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003630# endif
3631#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003632 // for "old" tty systems
3633 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003634#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003635 if (tty_fd != fd)
3636 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003637 return retval;
3638}
3639
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003641mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
3643 static int first = TRUE;
3644
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003645#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646# ifdef HAVE_TERMIOS_H
3647 static struct termios told;
3648 struct termios tnew;
3649# else
3650 static struct termio told;
3651 struct termio tnew;
3652# endif
3653
3654 if (first)
3655 {
3656 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003657 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659
3660 tnew = told;
3661 if (tmode == TMODE_RAW)
3662 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003663 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003664 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3665 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003667# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003668 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669# endif
3670 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003671# ifdef ONLCR
3672 // Don't map NL -> CR NL, we do it ourselves.
3673 // Also disable expanding tabs if possible.
3674# ifdef XTABS
3675 tnew.c_oflag &= ~(ONLCR | XTABS);
3676# else
3677# ifdef TAB3
3678 tnew.c_oflag &= ~(ONLCR | TAB3);
3679# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003681# endif
3682# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003684 tnew.c_cc[VMIN] = 1; // return after 1 char
3685 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
3687 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003688 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003689 // Also reset ICANON here, otherwise on Solaris select() won't see
3690 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003691 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003692 tnew.c_cc[VMIN] = 1; // return after 1 char
3693 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695
3696# if defined(HAVE_TERMIOS_H)
3697 {
3698 int n = 10;
3699
Bram Moolenaar0f873732019-12-05 20:28:46 +01003700 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3701 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3703 && errno == EINTR && n > 0)
3704 --n;
3705 }
3706# else
3707 ioctl(read_cmd_fd, TCSETA, &tnew);
3708# endif
3709
3710#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 /*
3712 * for "old" tty systems
3713 */
3714# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003715# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716# endif
3717 static struct sgttyb ttybold;
3718 struct sgttyb ttybnew;
3719
3720 if (first)
3721 {
3722 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003723 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
3725
3726 ttybnew = ttybold;
3727 if (tmode == TMODE_RAW)
3728 {
3729 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3730 ttybnew.sg_flags |= RAW;
3731 }
3732 else if (tmode == TMODE_SLEEP)
3733 ttybnew.sg_flags &= ~(ECHO);
3734 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3735#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003736 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737}
3738
3739/*
3740 * Try to get the code for "t_kb" from the stty setting
3741 *
3742 * Even if termcap claims a backspace key, the user's setting *should*
3743 * prevail. stty knows more about reality than termcap does, and if
3744 * somebody's usual erase key is DEL (which, for most BSD users, it will
3745 * be), they're going to get really annoyed if their erase key starts
3746 * doing forward deletes for no reason. (Eric Fischer)
3747 */
3748 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003749get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003751 ttyinfo_T info;
3752 char_u buf[2];
3753 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003755 if (get_tty_info(read_cmd_fd, &info) != OK)
3756 return;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003757
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003758 intr_char = info.interrupt;
3759 buf[0] = info.backspace;
3760 buf[1] = NUL;
3761 add_termcode((char_u *)"kb", buf, FALSE);
3762
3763 // If <BS> and <DEL> are now the same, redefine <DEL>.
3764 p = find_termcode((char_u *)"kD");
3765 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3766 do_fixdel(NULL);
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003767}
3768
3769/*
3770 * Obtain the characters that Backspace and Enter produce on "fd".
3771 * Returns OK or FAIL.
3772 */
3773 int
3774get_tty_info(int fd, ttyinfo_T *info)
3775{
3776#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777# ifdef HAVE_TERMIOS_H
3778 struct termios keys;
3779# else
3780 struct termio keys;
3781# endif
3782
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003783 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003785 info->backspace = keys.c_cc[VERASE];
3786 info->interrupt = keys.c_cc[VINTR];
3787 if (keys.c_iflag & ICRNL)
3788 info->enter = NL;
3789 else
3790 info->enter = CAR;
3791 if (keys.c_oflag & ONLCR)
3792 info->nl_does_cr = TRUE;
3793 else
3794 info->nl_does_cr = FALSE;
3795 return OK;
3796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003798 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 struct sgttyb keys;
3800
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003801 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003803 info->backspace = keys.sg_erase;
3804 info->interrupt = keys.sg_kill;
3805 info->enter = CAR;
3806 info->nl_does_cr = TRUE;
3807 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003810 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811}
3812
Bram Moolenaar0f873732019-12-05 20:28:46 +01003813#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003815static int mouse_ison = FALSE;
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817/*
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003818 * Set mouse clicks on or off and possible enable mouse movement events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 */
3820 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003821mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003823#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003824 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 int xterm_mouse_vers;
3827
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003828#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003829 if (!on)
3830 // Make sure not tracing mouse movements. Important when a button-down
3831 // was received but no release yet.
3832 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003833#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003834
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003835 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003836#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003837 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003838#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003839 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003840 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 return;
3842
3843 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003844
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003845#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003846 if (ttym_flags == TTYM_URXVT)
3847 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003848 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003849 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003850 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003851#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003852
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00003853 if (T_CXM != NULL && *T_CXM != NUL)
3854 {
3855 term_enable_mouse(on);
3856 }
3857 else if (ttym_flags == TTYM_SGR)
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003858 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003859 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003860 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003861 mouse_ison = on;
3862 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003863
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003864#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003865 if (bevalterm_ison != (p_bevalterm && on))
3866 {
3867 bevalterm_ison = (p_bevalterm && on);
3868 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003869 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003870 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003871 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003872#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 if (xterm_mouse_vers > 0)
3875 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003876 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 out_str_nf((char_u *)
3878 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003879 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003880#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003881 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003882#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003883 "\033[?1002h")
3884 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003885 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003887 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003888 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 }
3890
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003891#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 else if (ttym_flags == TTYM_DEC)
3893 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003894 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003896 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003898 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003902#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 else
3904 {
3905 if (on)
3906 {
3907 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003908 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
3910 else
3911 {
3912 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003913 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 }
3915 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003918#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003919 else
3920 {
3921 if (on)
3922 {
3923 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003924 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003925 }
3926 else
3927 {
3928 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003929 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003930 }
3931 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003932#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003933
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003934#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 else
3936 {
3937 if (on)
3938 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003939 // D - Enable Mouse up/down messages
3940 // L - Enable Left Button Reporting
3941 // M - Enable Middle Button Reporting
3942 // R - Enable Right Button Reporting
3943 // K - Enable SHIFT and CTRL key Reporting
3944 // + - Enable Advanced messaging of mouse moves and up/down messages
3945 // Q - Quiet No Ack
3946 // # - Numeric value of mouse pointer required
3947 // 0 = Multiview 2000 cursor, used as standard
3948 // 1 = Windows Arrow
3949 // 2 = Windows I Beam
3950 // 3 = Windows Hour Glass
3951 // 4 = Windows Cross Hair
3952 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003953# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003954 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003955 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003956# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003957 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003958# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003959 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961 else
3962 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003963 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003964 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
3966 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003967#endif
3968#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 else
3970 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003971 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 if (on)
3973 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3974 else
3975 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003976 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979}
3980
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003981#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003982/*
3983 * Called when 'balloonevalterm' changed.
3984 */
3985 void
3986mch_bevalterm_changed(void)
3987{
3988 mch_setmouse(mouse_ison);
3989}
3990#endif
3991
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992/*
3993 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3994 */
3995 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003996check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997{
3998# ifdef FEAT_MOUSE_XTERM
3999 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02004000# ifdef FEAT_MOUSE_URXVT
4001 && use_xterm_mouse() != 3
4002# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003# ifdef FEAT_GUI
4004 && !gui.in_use
4005# endif
4006 )
4007 {
4008 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004009 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (*p_mouse != NUL)
4011 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004012 // force mouse off and maybe on to send possibly new mouse
4013 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 mch_setmouse(FALSE);
4015 setmouse();
4016 }
4017 }
4018 else
4019 del_mouse_termcode(KS_MOUSE);
4020# endif
4021
4022# ifdef FEAT_MOUSE_GPM
4023 if (!use_xterm_mouse()
4024# ifdef FEAT_GUI
4025 && !gui.in_use
4026# endif
4027 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004028 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02004029 else
4030 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031# endif
4032
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004033# ifdef FEAT_SYSMOUSE
4034 if (!use_xterm_mouse()
4035# ifdef FEAT_GUI
4036 && !gui.in_use
4037# endif
4038 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004039 set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004040# endif
4041
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01004043 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 if (!use_xterm_mouse()
4045# ifdef FEAT_GUI
4046 && !gui.in_use
4047# endif
4048 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004049 set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 else
4051 del_mouse_termcode(KS_JSBTERM_MOUSE);
4052# endif
4053
4054# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01004055 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
4056 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 if (!use_xterm_mouse()
4058# ifdef FEAT_GUI
4059 && !gui.in_use
4060# endif
4061 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004062 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 else
4064 del_mouse_termcode(KS_NETTERM_MOUSE);
4065# endif
4066
4067# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01004068 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004069 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070# ifdef FEAT_GUI
4071 && !gui.in_use
4072# endif
4073 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004074 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004075 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 else
4077 del_mouse_termcode(KS_DEC_MOUSE);
4078# endif
4079# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004080 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004081 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082# ifdef FEAT_GUI
4083 && !gui.in_use
4084# endif
4085 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004086 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 else
4088 del_mouse_termcode(KS_PTERM_MOUSE);
4089# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004090# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004091 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004092# ifdef FEAT_GUI
4093 && !gui.in_use
4094# endif
4095 )
4096 {
4097 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004098 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004099
4100 if (*p_mouse != NUL)
4101 {
4102 mch_setmouse(FALSE);
4103 setmouse();
4104 }
4105 }
4106 else
4107 del_mouse_termcode(KS_URXVT_MOUSE);
4108# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004109 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004110# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004111 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004112# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004113 )
4114 {
4115 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004116 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004117
4118 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004119 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004120
4121 if (*p_mouse != NUL)
4122 {
4123 mch_setmouse(FALSE);
4124 setmouse();
4125 }
4126 }
4127 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004128 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004129 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004130 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134#ifndef VMS
4135
4136/*
4137 * Try to get the current window size:
4138 * 1. with an ioctl(), most accurate method
4139 * 2. from the environment variables LINES and COLUMNS
4140 * 3. from the termcap
4141 * 4. keep using the old values
4142 * Return OK when size could be determined, FAIL otherwise.
4143 */
4144 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004145mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146{
4147 long rows = 0;
4148 long columns = 0;
4149 char_u *p;
4150
4151 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 * 1. try using an ioctl. It is the most accurate method.
4153 *
4154 * Try using TIOCGWINSZ first, some systems that have it also define
4155 * TIOCGSIZE but don't have a struct ttysize.
4156 */
4157# ifdef TIOCGWINSZ
4158 {
4159 struct winsize ws;
4160 int fd = 1;
4161
Bram Moolenaar0f873732019-12-05 20:28:46 +01004162 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 if (!isatty(fd) && isatty(read_cmd_fd))
4164 fd = read_cmd_fd;
4165 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4166 {
4167 columns = ws.ws_col;
4168 rows = ws.ws_row;
4169 }
4170 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004171# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172# ifdef TIOCGSIZE
4173 {
4174 struct ttysize ts;
4175 int fd = 1;
4176
Bram Moolenaar0f873732019-12-05 20:28:46 +01004177 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 if (!isatty(fd) && isatty(read_cmd_fd))
4179 fd = read_cmd_fd;
4180 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4181 {
4182 columns = ts.ts_cols;
4183 rows = ts.ts_lines;
4184 }
4185 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004186# endif // TIOCGSIZE
4187# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
4189 /*
4190 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004191 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4192 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004194 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 {
4196 if ((p = (char_u *)getenv("LINES")))
4197 rows = atoi((char *)p);
4198 if ((p = (char_u *)getenv("COLUMNS")))
4199 columns = atoi((char *)p);
4200 }
4201
4202#ifdef HAVE_TGETENT
4203 /*
4204 * 3. try reading "co" and "li" entries from termcap
4205 */
4206 if (columns == 0 || rows == 0)
4207 getlinecol(&columns, &rows);
4208#endif
4209
4210 /*
4211 * 4. If everything fails, use the old values
4212 */
4213 if (columns <= 0 || rows <= 0)
4214 return FAIL;
4215
4216 Rows = rows;
4217 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004218 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 return OK;
4220}
4221
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004222#if defined(FEAT_TERMINAL) || defined(PROTO)
4223/*
4224 * Report the windows size "rows" and "cols" to tty "fd".
4225 */
4226 int
4227mch_report_winsize(int fd, int rows, int cols)
4228{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004229 int tty_fd;
4230 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004231
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004232 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004233 if (tty_fd < 0)
4234 return FAIL;
4235
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004236# if defined(TIOCSWINSZ)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004237 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004238
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004239 ws.ws_col = cols;
4240 ws.ws_row = rows;
4241 ws.ws_xpixel = cols * 5;
4242 ws.ws_ypixel = rows * 10;
4243 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4244 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4245 retval == 0 ? "success" : "failed");
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004246# elif defined(TIOCSSIZE)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004247 struct ttysize ts;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004248
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004249 ts.ts_cols = cols;
4250 ts.ts_lines = rows;
4251 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4252 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4253 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004254# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004255 if (tty_fd != fd)
4256 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004257 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004258}
4259#endif
4260
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261/*
4262 * Try to set the window size to Rows and Columns.
4263 */
4264 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004265mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266{
4267 if (*T_CWS)
4268 {
4269 /*
4270 * NOTE: if you get an error here that term_set_winsize() is
4271 * undefined, check the output of configure. It could probably not
4272 * find a ncurses, termcap or termlib library.
4273 */
4274 term_set_winsize((int)Rows, (int)Columns);
4275 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004276 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278}
4279
Bram Moolenaar0f873732019-12-05 20:28:46 +01004280#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
4282/*
4283 * Rows and/or Columns has changed.
4284 */
4285 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004286mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004288 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289}
4290
Bram Moolenaar205b8862011-09-07 15:04:31 +02004291/*
4292 * Wait for process "child" to end.
4293 * Return "child" if it exited properly, <= 0 on error.
4294 */
4295 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004296wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004297{
4298 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004299 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004300
4301 while (wait_pid != child)
4302 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004303 // When compiled with Python threads are probably used, in which case
4304 // wait() sometimes hangs for no obvious reason. Use waitpid()
4305 // instead and loop (like the GUI). Also needed for other interfaces,
4306 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004307# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004308 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004309# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004310 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004311# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004312 if (wait_pid == 0)
4313 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004314 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004315 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004316 if (++delay_msec > 10)
4317 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004318 continue;
4319 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004320 if (wait_pid <= 0
4321# ifdef ECHILD
4322 && errno == ECHILD
4323# endif
4324 )
4325 break;
4326 }
4327 return wait_pid;
4328}
4329
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004330#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004331/*
4332 * Set the environment for a child process.
4333 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004334 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004335set_child_environment(
4336 long rows,
4337 long columns,
4338 char *term,
4339 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004340{
4341# ifdef HAVE_SETENV
4342 char envbuf[50];
4343# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004344 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004345 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004346 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004347 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004348 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004349# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004350 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004351# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004352# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004353 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004354# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004355# endif
4356
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004357# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004358 setenv("TERM", term, 1);
4359 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004360 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004361 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004362 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004363 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004364 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004365 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004366 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004367# ifdef FEAT_TERMINAL
4368 if (is_terminal)
4369 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004370 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004371 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4372 }
4373# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004374# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004375 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004376# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004377# else
4378 /*
4379 * Putenv does not copy the string, it has to remain valid.
4380 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004381 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004382 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004383 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4384 putenv(envbuf_Term);
4385 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004386 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004387 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4388 putenv(envbuf_Lines);
4389 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4390 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004391 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004392 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004393 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004394# ifdef FEAT_TERMINAL
4395 if (is_terminal)
4396 {
4397 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004398 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004399 putenv(envbuf_Version);
4400 }
4401# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004402# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004403 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4404 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4405 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004406# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004407# endif
4408}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004409
4410 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004411set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004412{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004413 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004414}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004415#endif
4416
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004417#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004418/*
4419 * Open a PTY, with FD for the master and slave side.
4420 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004421 * When successful both file descriptors are stored and the allocated pty name
4422 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004423 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004424 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004425open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004426{
4427 char *tty_name;
4428
Bram Moolenaar59386482019-02-10 22:43:46 +01004429 if (name1 != NULL)
4430 *name1 = NULL;
4431 if (name2 != NULL)
4432 *name2 = NULL;
4433
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004434 *pty_master_fd = mch_openpty(&tty_name); // open pty
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004435 if (*pty_master_fd < 0)
4436 return;
4437
4438 // Leaving out O_NOCTTY may lead to waitpid() always returning
4439 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4440 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004441#ifdef O_NOCTTY
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004442 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004443#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004444 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004445#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004446 if (*pty_slave_fd < 0)
4447 {
4448 close(*pty_master_fd);
4449 *pty_master_fd = -1;
4450 }
4451 else
4452 {
4453 if (name1 != NULL)
4454 *name1 = vim_strsave((char_u *)tty_name);
4455 if (name2 != NULL)
4456 *name2 = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004457 }
4458}
4459#endif
4460
Bram Moolenaarfae42832017-08-01 22:24:26 +02004461/*
4462 * Send SIGINT to a child process if "c" is an interrupt character.
4463 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004464 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004465may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4466{
4467# ifdef SIGINT
4468 if (c == Ctrl_C || c == intr_char)
4469 {
4470# ifdef HAVE_SETSID
4471 kill(-pid, SIGINT);
4472# else
4473 kill(0, SIGINT);
4474# endif
4475 if (wpid > 0)
4476 kill(wpid, SIGINT);
4477 }
4478# endif
4479}
4480
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004481#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004482
Bram Moolenaar0f873732019-12-05 20:28:46 +01004483/*
4484 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4485 * pointers, the last one is NULL.
4486 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4487 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004488 int
4489unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004490 char_u *cmd,
4491 char ***argvp,
4492 char_u **sh_tofree,
4493 char_u **shcf_tofree)
4494{
4495 char **argv = NULL;
4496 int argc;
4497
4498 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004499 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004500 return FAIL;
4501
4502 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4503 return FAIL;
4504 *argvp = argv;
4505
4506 if (cmd != NULL)
4507 {
4508 char_u *s;
4509 char_u *p;
4510
4511 if (extra_shell_arg != NULL)
4512 argv[argc++] = (char *)extra_shell_arg;
4513
Bram Moolenaar0f873732019-12-05 20:28:46 +01004514 // Break 'shellcmdflag' into white separated parts. This doesn't
4515 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004516 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004517 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004518 return FAIL;
4519 s = *shcf_tofree;
4520 p = p_shcf;
4521 while (*p != NUL)
4522 {
4523 argv[argc++] = (char *)s;
4524 while (*p && *p != ' ' && *p != TAB)
4525 *s++ = *p++;
4526 *s++ = NUL;
4527 p = skipwhite(p);
4528 }
4529
4530 argv[argc++] = (char *)cmd;
4531 }
4532 argv[argc] = NULL;
4533 return OK;
4534}
4535#endif
4536
4537#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4538/*
4539 * Use a terminal window to run a shell command in.
4540 */
4541 static int
4542mch_call_shell_terminal(
4543 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004544 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004545{
4546 jobopt_T opt;
4547 char **argv = NULL;
4548 char_u *tofree1 = NULL;
4549 char_u *tofree2 = NULL;
4550 int retval = -1;
4551 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004552 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004553 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004554 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004555
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004556 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004557 goto theend;
4558
4559 init_job_options(&opt);
4560 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4561 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004562 if (buf == NULL)
4563 goto theend;
4564
4565 job = term_getjob(buf->b_term);
4566 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004567
Bram Moolenaar0f873732019-12-05 20:28:46 +01004568 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004569 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004570 if (curbuf == buf)
Bram Moolenaar13568252018-03-16 20:46:58 +01004571 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004572 // Only when managed to find a window for "buf",
4573 clear_oparg(&oa);
4574 while (term_use_loop())
Bram Moolenaar13568252018-03-16 20:46:58 +01004575 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004576 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4577 {
4578 // If terminal_loop() returns OK we got a key that is handled
4579 // in Normal model. We don't do redrawing anyway.
4580 if (terminal_loop(TRUE) == OK)
4581 normal_cmd(&oa, TRUE);
4582 }
4583 else
Bram Moolenaar13568252018-03-16 20:46:58 +01004584 normal_cmd(&oa, TRUE);
4585 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00004586 retval = job->jv_exitval;
4587 ch_log(NULL, "system command finished");
4588
4589 job_unref(job);
4590
4591 // restore curwin/curbuf and a few other things
4592 aucmd_restbuf(&aco);
Bram Moolenaar13568252018-03-16 20:46:58 +01004593 }
Bram Moolenaar13568252018-03-16 20:46:58 +01004594
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01004595 // Only require pressing Enter when redrawing, to avoid that system() gets
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004596 // the hit-enter prompt even though it didn't output anything.
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004597 if (RedrawingDisabled == 0)
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004598 wait_return(TRUE);
Bram Moolenaar13568252018-03-16 20:46:58 +01004599 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4600
4601theend:
4602 vim_free(argv);
4603 vim_free(tofree1);
4604 vim_free(tofree2);
4605 return retval;
4606}
4607#endif
4608
4609#ifdef USE_SYSTEM
4610/*
4611 * Use system() to start the shell: simple but slow.
4612 */
4613 static int
4614mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004615 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004616 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
4618#ifdef VMS
4619 char *ifn = NULL;
4620 char *ofn = NULL;
4621#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004622 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004623 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004624 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
4626 out_flush();
4627
4628 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004629 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
Bram Moolenaar62b42182010-09-21 22:09:37 +02004631# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004632 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004633 loose_clipboard();
4634# endif
4635
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 if (cmd == NULL)
4637 x = system((char *)p_sh);
4638 else
4639 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004640# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 if (ofn = strchr((char *)cmd, '>'))
4642 *ofn++ = '\0';
4643 if (ifn = strchr((char *)cmd, '<'))
4644 {
4645 char *p;
4646
4647 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004648 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 if (p)
4650 *p = '\0';
4651 }
4652 if (ofn)
4653 x = vms_sys((char *)cmd, ofn, ifn);
4654 else
4655 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004656# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004657 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004659 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (newcmd == NULL)
4661 x = 0;
4662 else
4663 {
4664 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4665 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4666 (char *)p_shcf,
4667 (char *)cmd);
4668 x = system((char *)newcmd);
4669 vim_free(newcmd);
4670 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004671# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673# ifdef VMS
4674 x = vms_sys_status(x);
4675# endif
4676 if (emsg_silent)
4677 ;
4678 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004679 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 else if (x && !(options & SHELL_SILENT))
4681 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004682 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 msg_outnum((long)x);
4684 msg_putchar('\n');
4685 }
4686
4687 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004688 {
4689 // The shell may have messed with the mode, always set it.
4690 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004691 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004694# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4695 restore_clipboard();
4696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004698}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
Bram Moolenaar0f873732019-12-05 20:28:46 +01004700#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701
Bram Moolenaar0f873732019-12-05 20:28:46 +01004702# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4703 // 127, some shells use that already
4704# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705
Bram Moolenaar13568252018-03-16 20:46:58 +01004706/*
4707 * Don't use system(), use fork()/exec().
4708 */
4709 static int
4710mch_call_shell_fork(
4711 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004712 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004713{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004714 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004716 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 pid_t wait_pid = 0;
4718# ifdef HAVE_UNION_WAIT
4719 union wait status;
4720# else
4721 int status = -1;
4722# endif
4723 int retval = -1;
4724 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004725 char_u *tofree1 = NULL;
4726 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004728 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004729# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004731# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004732 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 int fd_fromshell[2];
4734 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004735 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
4737 out_flush();
4738 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004739 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004740 if (tmode == TMODE_RAW)
4741 // The shell may have messed with the mode, always set it later.
4742 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004744 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004745 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004746
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004748 * For the GUI, when writing the output into the buffer and when reading
4749 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4750 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004752 if ((options & (SHELL_READ|SHELL_WRITE))
4753# ifdef FEAT_GUI
4754 || (gui.in_use && show_shell_mess)
4755# endif
4756 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004758# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 /*
4760 * Try to open a master pty.
4761 * If this works, open the slave pty.
4762 * If the slave can't be opened, close the master pty.
4763 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004764 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004765 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 /*
4767 * If not opening a pty or it didn't work, try using pipes.
4768 */
4769 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 {
4772 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004773 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
4775 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004776 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
4778 close(fd_toshell[0]);
4779 close(fd_toshell[1]);
4780 }
4781 }
4782 if (pipe_error)
4783 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004784 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 out_flush();
4786 }
4787 }
4788 }
4789
Bram Moolenaar0f873732019-12-05 20:28:46 +01004790 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004792 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004793 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004794 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004795 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004797 UNBLOCK_SIGNALS(&curset);
4798
Bram Moolenaar32526b32019-01-19 17:43:09 +01004799 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004800 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004802 || (gui.in_use && show_shell_mess)
4803# endif
4804 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004806# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004807 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 {
4809 close(pty_master_fd);
4810 close(pty_slave_fd);
4811 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004812 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004813# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
4815 close(fd_toshell[0]);
4816 close(fd_toshell[1]);
4817 close(fd_fromshell[0]);
4818 close(fd_fromshell[1]);
4819 }
4820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004822 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004824 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004825 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00004827# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01004828 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004829 {
4830 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004831 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004832 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004833# endif
4834
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 if (!show_shell_mess || (options & SHELL_EXPAND))
4836 {
4837 int fd;
4838
4839 /*
4840 * Don't want to show any message from the shell. Can't just
4841 * close stdout and stderr though, because some systems will
4842 * break if you try to write to them after that, so we must
4843 * use dup() to replace them with something else -- webb
4844 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4845 * waiting for input.
4846 */
4847 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4848 fclose(stdin);
4849 fclose(stdout);
4850 fclose(stderr);
4851
4852 /*
4853 * If any of these open()'s and dup()'s fail, we just continue
4854 * anyway. It's not fatal, and on most systems it will make
4855 * no difference at all. On a few it will cause the execvp()
4856 * to exit with a non-zero status even when the completion
4857 * could be done, which is nothing too serious. If the open()
4858 * or dup() failed we'd just do the same thing ourselves
4859 * anyway -- webb
4860 */
4861 if (fd >= 0)
4862 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004863 vim_ignored = dup(fd); // To replace stdin (fd 0)
4864 vim_ignored = dup(fd); // To replace stdout (fd 1)
4865 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
Bram Moolenaar0f873732019-12-05 20:28:46 +01004867 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 close(fd);
4869 }
4870 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004871 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004873 || gui.in_use
4874# endif
4875 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
4877
Bram Moolenaardf177f62005-02-22 08:39:57 +00004878# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004879 // Create our own process group, so that the child and all its
4880 // children can be kill()ed. Don't do this when using pipes,
4881 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004882 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004883 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004884 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004885# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004886 // When doing "!xterm&" and 'shell' is bash: the shell
4887 // will exit and send SIGHUP to all processes in its
4888 // group, killing the just started process. Ignore SIGHUP
4889 // to avoid that. (suggested by Simon Schubert)
ichizok378447f2023-05-11 22:25:42 +01004890 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar07256082009-02-04 13:19:42 +00004891# endif
4892 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004893# endif
4894# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004895 if (pty_slave_fd >= 0)
4896 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004897 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004898 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004899 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004900# ifdef TIOCSCTTY
4901 // Try to become controlling tty (probably doesn't work,
4902 // unless run by root)
4903 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4904# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004905 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004906# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004907 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908
Bram Moolenaara5792f52005-11-23 21:25:05 +00004909 /*
4910 * stderr is only redirected when using the GUI, so that a
4911 * program like gpg can still access the terminal to get a
4912 * passphrase using stderr.
4913 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004914# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 if (pty_master_fd >= 0)
4916 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004917 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
Bram Moolenaar0f873732019-12-05 20:28:46 +01004919 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004921 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004923 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004924 if (gui.in_use)
4925 {
4926 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004927 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929
Bram Moolenaar0f873732019-12-05 20:28:46 +01004930 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004933# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004935 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 close(fd_toshell[1]);
4937 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004938 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 close(fd_toshell[0]);
4940
Bram Moolenaar0f873732019-12-05 20:28:46 +01004941 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 close(fd_fromshell[0]);
4943 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004944 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 close(fd_fromshell[1]);
4946
Bram Moolenaara5792f52005-11-23 21:25:05 +00004947# ifdef FEAT_GUI
4948 if (gui.in_use)
4949 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004950 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004951 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004952 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004953 }
4954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004957
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 /*
4959 * There is no type cast for the argv, because the type may be
4960 * different on different machines. This may cause a warning
4961 * message with strict compilers, don't worry about it.
4962 * Call _exit() instead of exit() to avoid closing the connection
4963 * to the X server (esp. with GTK, which uses atexit()).
4964 */
4965 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004966 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004968 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
4970 /*
4971 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004972 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 */
4974 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004975 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004976 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004977# ifdef FEAT_JOB_CHANNEL
4978 ++dont_check_job_ended;
4979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 /*
4981 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004982 * This is also used to pipe stdin/stdout to/from the external
4983 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004985 if ((options & (SHELL_READ|SHELL_WRITE))
4986# ifdef FEAT_GUI
4987 || (gui.in_use && show_shell_mess)
4988# endif
4989 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004991# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004993 int buffer_off = 0; // valid bytes in buffer[]
4994 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4995 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 int len;
4997 int p_more_save;
4998 int old_State;
4999 int c;
5000 int toshell_fd;
5001 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005002 garray_T ga;
5003 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01005004# ifdef ELAPSED_FUNC
5005 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005006# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
Bram Moolenaardf177f62005-02-22 08:39:57 +00005008# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 if (pty_master_fd >= 0)
5010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 fromshell_fd = pty_master_fd;
5012 toshell_fd = dup(pty_master_fd);
5013 }
5014 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00005015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 close(fd_toshell[0]);
5018 close(fd_fromshell[1]);
5019 toshell_fd = fd_toshell[1];
5020 fromshell_fd = fd_fromshell[0];
5021 }
5022
5023 /*
5024 * Write to the child if there are typed characters.
5025 * Read from the child if there are characters available.
5026 * Repeat the reading a few times if more characters are
5027 * available. Need to check for typed keys now and then, but
5028 * not too often (delays when no chars are available).
5029 * This loop is quit if no characters can be read from the pty
5030 * (WaitForChar detected special condition), or there are no
5031 * characters available and the child has exited.
5032 * Only check if the child has exited when there is no more
5033 * output. The child may exit before all the output has
5034 * been printed.
5035 *
5036 * Currently this busy loops!
5037 * This can probably dead-lock when the write blocks!
5038 */
5039 p_more_save = p_more;
5040 p_more = FALSE;
5041 old_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01005042 State = MODE_EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005044 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005045 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005046 // Fork a process that will write the lines to the
5047 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005048 if ((wpid = fork()) == -1)
5049 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005050 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005051 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005052 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00005053 {
5054 linenr_T lnum = curbuf->b_op_start.lnum;
5055 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005056 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005057 size_t l;
5058
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005059 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005060 for (;;)
5061 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005062 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005063 if (l == 0)
5064 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005065 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005066 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00005067 len = write(toshell_fd, "", (size_t)1);
5068 else
5069 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005070 char_u *s = vim_strchr(lp + written, NL);
5071
Bram Moolenaar89d40322006-08-29 15:30:07 +00005072 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00005073 s == NULL ? l
5074 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005075 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00005076 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005077 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005078 // Finished a line, add a NL, unless this line
5079 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005080 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005081 || (!curbuf->b_p_bin
5082 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005083 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaar88456cd2022-11-18 22:14:09 +00005084 && (lnum != curbuf->b_ml.ml_line_count
Bram Moolenaardf177f62005-02-22 08:39:57 +00005085 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005086 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005087 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005088 ++lnum;
5089 if (lnum > curbuf->b_op_end.lnum)
5090 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005091 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005092 close(toshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005093 break;
5094 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005095 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005096 written = 0;
5097 }
5098 else if (len > 0)
5099 written += len;
5100 }
5101 _exit(0);
5102 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005103 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005104 {
5105 close(toshell_fd);
5106 toshell_fd = -1;
5107 }
5108 }
5109
5110 if (options & SHELL_READ)
5111 ga_init2(&ga, 1, BUFLEN);
5112
5113 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005114# ifdef ELAPSED_FUNC
5115 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 for (;;)
5118 {
5119 /*
5120 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005121 * if there are any.
5122 * Don't do this if we are expanding wild cards (would eat
5123 * typeahead).
5124 * Don't do this when filtering and terminal is in cooked
5125 * mode, the shell command will handle the I/O. Avoids
5126 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005127 * Don't get characters when the child has already
5128 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005129 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005130 * while (noread_cnt > 4), avoids that ":r !ls" eats
5131 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 */
5133 len = 0;
5134 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005135 && ((options &
5136 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5137 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005138# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005139 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005140# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005141 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005142 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005143 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005145 if (ta_len == 0)
5146 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005147 // Get extra characters when we don't have any.
5148 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005149 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005150# ifdef ELAPSED_FUNC
5151 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005152# endif
5153 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5154 }
5155 if (ta_len > 0 || len > 0)
5156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 /*
5158 * For pipes:
5159 * Check for CTRL-C: send interrupt signal to child.
5160 * Check for CTRL-D: EOF, close pipe to child.
5161 */
5162 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 /*
5165 * Send SIGINT to the child's group or all
5166 * processes in our group.
5167 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005168 may_send_sigint(ta_buf[ta_len], pid, wpid);
5169
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 if (pty_master_fd < 0 && toshell_fd >= 0
5171 && ta_buf[ta_len] == Ctrl_D)
5172 {
5173 close(toshell_fd);
5174 toshell_fd = -1;
5175 }
5176 }
5177
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01005178 // Remove Vim-specific codes from the input.
5179 len = term_replace_keycodes(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180
5181 /*
5182 * For pipes: echo the typed characters.
5183 * For a pty this does not seem to work.
5184 */
5185 if (pty_master_fd < 0)
5186 {
5187 for (i = ta_len; i < ta_len + len; ++i)
5188 {
5189 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5190 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 else if (has_mbyte)
5192 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005193 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
5195 msg_outtrans_len(ta_buf + i, l);
5196 i += l - 1;
5197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 else
5199 msg_outtrans_len(ta_buf + i, 1);
5200 }
5201 windgoto(msg_row, msg_col);
5202 out_flush();
5203 }
5204
5205 ta_len += len;
5206
5207 /*
5208 * Write the characters to the child, unless EOF has
5209 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005210 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005211 * When writing buffer lines, drop the typed
5212 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005214 if (options & SHELL_WRITE)
5215 ta_len = 0;
5216 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
5218 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5219 if (len > 0)
5220 {
5221 ta_len -= len;
5222 mch_memmove(ta_buf, ta_buf + len, ta_len);
5223 }
5224 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
5227
Bram Moolenaardf177f62005-02-22 08:39:57 +00005228 if (got_int)
5229 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005230 // CTRL-C sends a signal to the child, we ignore it
5231 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005232# ifdef HAVE_SETSID
5233 kill(-pid, SIGINT);
5234# else
5235 kill(0, SIGINT);
5236# endif
5237 if (wpid > 0)
5238 kill(wpid, SIGINT);
5239 got_int = FALSE;
5240 }
5241
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 /*
5243 * Check if the child has any characters to be printed.
5244 * Read them and write them to our window. Repeat this as
5245 * long as there is something to do, avoid the 10ms wait
5246 * for mch_inchar(), or sending typeahead characters to
5247 * the external process.
5248 * TODO: This should handle escape sequences, compatible
5249 * to some terminal (vt52?).
5250 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005251 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005252 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005254 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005257 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005259
5260 noread_cnt = 0;
5261 if (options & SHELL_READ)
5262 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005263 // Do NUL -> NL translation, append NL separated
5264 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005265 for (i = 0; i < len; ++i)
5266 {
5267 if (buffer[i] == NL)
5268 append_ga_line(&ga);
5269 else if (buffer[i] == NUL)
5270 ga_append(&ga, NL);
5271 else
5272 ga_append(&ga, buffer[i]);
5273 }
5274 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005275 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 {
5277 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005278 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
Bram Moolenaardf177f62005-02-22 08:39:57 +00005280 len += buffer_off;
5281 buffer[len] = NUL;
5282
Bram Moolenaar0f873732019-12-05 20:28:46 +01005283 // Check if the last character in buffer[] is
5284 // incomplete, keep these bytes for the next
5285 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 for (p = buffer; p < buffer + len; p += l)
5287 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005288 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005290 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 else if (MB_BYTE2LEN(*p) != l)
5292 break;
5293 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005294 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005296 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 if (len >= 12)
5298 ++p;
5299 else
5300 {
5301 buffer_off = len;
5302 continue;
5303 }
5304 }
5305 c = *p;
5306 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005307 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 if (p < buffer + len)
5309 {
5310 *p = c;
5311 buffer_off = (buffer + len) - p;
5312 mch_memmove(buffer, p, buffer_off);
5313 continue;
5314 }
5315 buffer_off = 0;
5316 }
5317 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
5319 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005320 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 }
5322
5323 windgoto(msg_row, msg_col);
5324 cursor_on();
5325 out_flush();
5326 if (got_int)
5327 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005328
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005329# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005330 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005331 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005332 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005333
Bram Moolenaar0f873732019-12-05 20:28:46 +01005334 // Avoid that we keep looping here without
5335 // checking for a CTRL-C for a long time. Don't
5336 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005337 if (msec > 2000)
5338 {
5339 noread_cnt = 5;
5340 break;
5341 }
5342 }
5343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 }
5345
Bram Moolenaar0f873732019-12-05 20:28:46 +01005346 // If we already detected the child has finished, continue
5347 // reading output for a short while. Some text may be
5348 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005349 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005350 {
5351 if (noread_cnt < 5)
5352 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005353 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005354 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005355
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 /*
5357 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005358 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005360# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005361 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005362# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5366 || (wait_pid == pid && WIFEXITED(status)))
5367 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005368 // Don't break the loop yet, try reading more
5369 // characters from "fromshell_fd" first. When using
5370 // pipes there might still be something to read and
5371 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005374 else
5375 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005376
Bram Moolenaar95a51352013-03-21 22:53:50 +01005377# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005378 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005379 clip_update();
5380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 }
5382finished:
5383 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005384 if (options & SHELL_READ)
5385 {
5386 if (ga.ga_len > 0)
5387 {
5388 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005389 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005390 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005391 }
5392 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005393 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005394 ga_clear(&ga);
5395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 /*
5398 * Give all typeahead that wasn't used back to ui_inchar().
5399 */
5400 if (ta_len)
5401 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 State = old_State;
5403 if (toshell_fd >= 0)
5404 close(toshell_fd);
5405 close(fromshell_fd);
5406 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005407# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005408 else
5409 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005410 long delay_msec = 1;
5411
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005412 if (tmode == TMODE_RAW)
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005413 // Possibly disables modifyOtherKeys, so that the system
5414 // can recognize CTRL-C.
5415 out_str_t_TE();
Bram Moolenaar0981c872020-08-23 14:28:37 +02005416
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005417 /*
5418 * Similar to the loop above, but only handle X events, no
5419 * I/O.
5420 */
5421 for (;;)
5422 {
5423 if (got_int)
5424 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005425 // CTRL-C sends a signal to the child, we ignore it
5426 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005427# ifdef HAVE_SETSID
5428 kill(-pid, SIGINT);
5429# else
5430 kill(0, SIGINT);
5431# endif
5432 got_int = FALSE;
5433 }
5434# ifdef __NeXT__
5435 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5436# else
5437 wait_pid = waitpid(pid, &status, WNOHANG);
5438# endif
5439 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5440 || (wait_pid == pid && WIFEXITED(status)))
5441 {
5442 wait_pid = pid;
5443 break;
5444 }
5445
Bram Moolenaar0f873732019-12-05 20:28:46 +01005446 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005447 clip_update();
5448
Bram Moolenaar0f873732019-12-05 20:28:46 +01005449 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005450 // less time, gradually wait longer.
5451 mch_delay(delay_msec,
5452 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005453 if (++delay_msec > 10)
5454 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005455 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005456
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005457 if (tmode == TMODE_RAW)
5458 // possibly enables modifyOtherKeys again
Bram Moolenaar733a69b2022-12-01 12:03:47 +00005459 out_str_t_TI();
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005460 }
5461# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463 /*
5464 * Wait until our child has exited.
5465 * Ignore wait() returning pids of other children and returning
5466 * because of some signal like SIGWINCH.
5467 * Don't wait if wait_pid was already set above, indicating the
5468 * child already exited.
5469 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005470 if (wait_pid != pid)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01005471 (void)wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
Bram Moolenaar624891f2010-10-13 16:22:09 +02005473# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005474 // Close slave side of pty. Only do this after the child has
5475 // exited, otherwise the child may hang when it tries to write on
5476 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005477 if (pty_master_fd >= 0)
5478 close(pty_slave_fd);
5479# endif
5480
Bram Moolenaar0f873732019-12-05 20:28:46 +01005481 // Make sure the child that writes to the external program is
5482 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005483 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005484 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005485 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005486 wait4pid(wpid, NULL);
5487 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005488
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005489# ifdef FEAT_JOB_CHANNEL
5490 --dont_check_job_ended;
5491# endif
5492
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 /*
5494 * Set to raw mode right now, otherwise a CTRL-C after
5495 * catch_signals() will kill Vim.
5496 */
5497 if (tmode == TMODE_RAW)
5498 settmode(TMODE_RAW);
5499 did_settmode = TRUE;
5500 set_signals();
5501
5502 if (WIFEXITED(status))
5503 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005504 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005506 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 {
5508 if (retval == EXEC_FAILED)
5509 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005510 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 msg_outtrans(p_sh);
5512 msg_putchar('\n');
5513 }
5514 else if (!(options & SHELL_SILENT))
5515 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005516 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 msg_outnum((long)retval);
5518 msg_putchar('\n');
5519 }
5520 }
5521 }
5522 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005523 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 }
5525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
5527error:
5528 if (!did_settmode)
5529 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005530 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005532 vim_free(argv);
5533 vim_free(tofree1);
5534 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
5536 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005537}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005538#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005539
5540 int
5541mch_call_shell(
5542 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005543 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005544{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005545#ifdef FEAT_EVAL
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01005546 ch_log(NULL, "executing shell command: %s", cmd);
5547#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005548#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar524c8532022-09-27 15:48:20 +01005549 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL
5550 && (options & SHELL_SILENT) == 0)
Bram Moolenaar13568252018-03-16 20:46:58 +01005551 return mch_call_shell_terminal(cmd, options);
5552#endif
5553#ifdef USE_SYSTEM
5554 return mch_call_shell_system(cmd, options);
5555#else
5556 return mch_call_shell_fork(cmd, options);
5557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558}
5559
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005560#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005561 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005562mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005563{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005564 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005565 int fd_in[2] = {-1, -1}; // for stdin
5566 int fd_out[2] = {-1, -1}; // for stdout
5567 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005568 int pty_master_fd = -1;
5569 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005570 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005571 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5572 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5573 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005574 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005575 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5576 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005577 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005578 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005579 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005580
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005581 if (use_out_for_err && use_null_for_out)
5582 use_null_for_err = TRUE;
5583
Bram Moolenaar0f873732019-12-05 20:28:46 +01005584 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005585 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005586
Bram Moolenaarb2412082017-08-20 18:09:14 +02005587 if (options->jo_pty
5588 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005589 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005590 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005591 open_pty(&pty_master_fd, &pty_slave_fd,
5592 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005593
Bram Moolenaar0f873732019-12-05 20:28:46 +01005594 // TODO: without the channel feature connect the child to /dev/null?
5595 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005596 if (use_file_for_in)
5597 {
5598 char_u *fname = options->jo_io_name[PART_IN];
5599
5600 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5601 if (fd_in[0] < 0)
5602 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005603 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005604 goto failed;
5605 }
5606 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005607 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005608 // When writing buffer lines to the input don't use the pty, so that
5609 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005610 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5611 && pipe(fd_in) < 0)
5612 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005613
5614 if (use_file_for_out)
5615 {
5616 char_u *fname = options->jo_io_name[PART_OUT];
5617
5618 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5619 if (fd_out[1] < 0)
5620 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005621 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005622 goto failed;
5623 }
5624 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005625 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005626 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005627
5628 if (use_file_for_err)
5629 {
5630 char_u *fname = options->jo_io_name[PART_ERR];
5631
5632 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5633 if (fd_err[1] < 0)
5634 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005635 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005636 goto failed;
5637 }
5638 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005639 else if (!use_out_for_err && !use_null_for_err
5640 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005641 goto failed;
5642
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005643 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5644 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005645 if (options->jo_set & JO_CHANNEL)
5646 {
5647 channel = options->jo_channel;
5648 if (channel != NULL)
5649 ++channel->ch_refcount;
5650 }
5651 else
5652 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005653 if (channel == NULL)
5654 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005655 if (job->jv_tty_out != NULL)
5656 ch_log(channel, "using pty %s on fd %d",
5657 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005658 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005659
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005660 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005661 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005662 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005663 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005664 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005665 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005666 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005667 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005668 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005669 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005670 int null_fd = -1;
5671 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005672
Bram Moolenaar0f873732019-12-05 20:28:46 +01005673 // child
5674 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005675 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005676
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005677# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01005678 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005679 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005680 ch_logfile((char_u *)"", (char_u *)"");
5681# endif
5682
Bram Moolenaar835dc632016-02-07 14:27:38 +01005683# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005684 // Create our own process group, so that the child and all its
5685 // children can be kill()ed. Don't do this when using pipes,
5686 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005687 (void)setsid();
5688# endif
5689
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005690# ifdef FEAT_TERMINAL
5691 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005692 {
5693 char *term = (char *)T_NAME;
5694
5695#ifdef FEAT_GUI
5696 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005697 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005698 term = getenv("TERM");
5699#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005700 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005701 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005702 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005703 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005704 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005705 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005706 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005707 else if (t_colors > 16)
5708 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005709 else
5710 term = "xterm";
5711 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005712 set_child_environment(
5713 (long)options->jo_term_rows,
5714 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005715 term,
5716 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005717 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005718 else
5719# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005720 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005721
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005722 if (options->jo_env != NULL)
5723 {
5724 dict_T *dict = options->jo_env;
5725 hashitem_T *hi;
5726 int todo = (int)dict->dv_hashtab.ht_used;
5727
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005728 FOR_ALL_HASHTAB_ITEMS(&dict->dv_hashtab, hi, todo)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005729 if (!HASHITEM_EMPTY(hi))
5730 {
5731 typval_T *item = &dict_lookup(hi)->di_tv;
5732
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005733 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005734 --todo;
5735 }
5736 }
5737
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005738 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005739 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005740 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005741 if (null_fd < 0)
5742 {
5743 perror("opening /dev/null failed");
5744 _exit(OPEN_NULL_FAILED);
5745 }
5746 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005747
Bram Moolenaar223896d2017-08-02 22:33:28 +02005748 if (pty_slave_fd >= 0)
5749 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005750 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005751 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005752# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005753 // Try to become controlling tty (probably doesn't work,
5754 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005755 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5756# endif
5757 }
5758
Bram Moolenaar0f873732019-12-05 20:28:46 +01005759 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005760 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005761 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005762 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005763 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005764 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005765 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005766 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005767
Bram Moolenaar0f873732019-12-05 20:28:46 +01005768 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005769 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005770 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005771 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005772 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005773 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005774 }
5775 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005776 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005777 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005778 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005779 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005780 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005781
Bram Moolenaar0f873732019-12-05 20:28:46 +01005782 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005783 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005784 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005785 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005786 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005787 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005788 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005789 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005790
5791 if (fd_in[0] >= 0)
5792 close(fd_in[0]);
5793 if (fd_in[1] >= 0)
5794 close(fd_in[1]);
5795 if (fd_out[0] >= 0)
5796 close(fd_out[0]);
5797 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005798 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005799 if (fd_err[0] >= 0)
5800 close(fd_err[0]);
5801 if (fd_err[1] >= 0)
5802 close(fd_err[1]);
5803 if (pty_master_fd >= 0)
5804 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005805 close(pty_master_fd); // not used in the child
5806 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005807 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005808
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005809 if (null_fd >= 0)
5810 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005811
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005812 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5813 _exit(EXEC_FAILED);
5814
Bram Moolenaar0f873732019-12-05 20:28:46 +01005815 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005816 execvp(argv[0], argv);
5817
Bram Moolenaar4694a172016-04-21 14:05:23 +02005818 if (stderr_works)
5819 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005820# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005821 // calling free_all_mem() here causes problems. Ignore valgrind
5822 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005823# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005824 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005825 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005826
Bram Moolenaar0f873732019-12-05 20:28:46 +01005827 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005828 UNBLOCK_SIGNALS(&curset);
5829
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005830 job->jv_pid = pid;
5831 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005832 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005833
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005834 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005835 close(pty_slave_fd); // not used in the parent
5836 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005837 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005838 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005839 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005840 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005841 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005842 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005843 if (channel != NULL)
5844 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005845 int in_fd = INVALID_FD;
5846 int out_fd = INVALID_FD;
5847 int err_fd = INVALID_FD;
5848
5849 if (!(use_file_for_in || use_null_for_in))
5850 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5851
5852 if (!(use_file_for_out || use_null_for_out))
5853 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5854
5855 // When using pty_master_fd only set it for stdout, do not duplicate
5856 // it for stderr, it only needs to be read once.
5857 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5858 {
5859 if (fd_err[0] >= 0)
5860 err_fd = fd_err[0];
5861 else if (out_fd != pty_master_fd)
5862 err_fd = pty_master_fd;
5863 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005864
5865 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005866 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005867 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005868 else
5869 {
5870 if (fd_in[1] >= 0)
5871 close(fd_in[1]);
5872 if (fd_out[0] >= 0)
5873 close(fd_out[0]);
5874 if (fd_err[0] >= 0)
5875 close(fd_err[0]);
5876 if (pty_master_fd >= 0)
5877 close(pty_master_fd);
5878 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005879
Bram Moolenaar0f873732019-12-05 20:28:46 +01005880 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005881 return;
5882
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005883failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005884 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005885 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005886 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005887 if (fd_in[1] >= 0)
5888 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005889 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005890 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005891 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005892 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005893 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005894 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005895 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005896 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005897 if (pty_master_fd >= 0)
5898 close(pty_master_fd);
5899 if (pty_slave_fd >= 0)
5900 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005901}
5902
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005903 static char_u *
5904get_signal_name(int sig)
5905{
5906 int i;
5907 char_u numbuf[NUMBUFLEN];
5908
5909 if (sig == SIGKILL)
5910 return vim_strsave((char_u *)"kill");
5911
5912 for (i = 0; signal_info[i].sig != -1; i++)
5913 if (sig == signal_info[i].sig)
5914 return strlow_save((char_u *)signal_info[i].name);
5915
5916 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5917 return vim_strsave(numbuf);
5918}
5919
Bram Moolenaar835dc632016-02-07 14:27:38 +01005920 char *
5921mch_job_status(job_T *job)
5922{
5923# ifdef HAVE_UNION_WAIT
5924 union wait status;
5925# else
5926 int status = -1;
5927# endif
5928 pid_t wait_pid = 0;
5929
5930# ifdef __NeXT__
5931 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5932# else
5933 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5934# endif
5935 if (wait_pid == -1)
5936 {
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00005937 int waitpid_errno = errno;
5938 if (waitpid_errno == ECHILD && mch_process_running(job->jv_pid))
5939 // The process is alive, but it was probably reparented (for
5940 // example by ptrace called by a debugger like lldb or gdb).
5941 // Note: This assumes that process IDs are not reused.
5942 return "run";
5943
Bram Moolenaar0f873732019-12-05 20:28:46 +01005944 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005945 if (job->jv_status < JOB_ENDED)
5946 ch_log(job->jv_channel, "Job no longer exists: %s",
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00005947 strerror(waitpid_errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005948 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005949 }
5950 if (wait_pid == 0)
5951 return "run";
5952 if (WIFEXITED(status))
5953 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005954 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005955 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005956 if (job->jv_status < JOB_ENDED)
5957 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005958 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005959 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005960 if (WIFSIGNALED(status))
5961 {
5962 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005963 job->jv_termsig = get_signal_name(WTERMSIG(status));
5964 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5965 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5966 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005967 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005968 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005969 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005970
5971return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005972 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005973 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005974 return "dead";
5975}
5976
5977 job_T *
5978mch_detect_ended_job(job_T *job_list)
5979{
5980# ifdef HAVE_UNION_WAIT
5981 union wait status;
5982# else
5983 int status = -1;
5984# endif
5985 pid_t wait_pid = 0;
5986 job_T *job;
5987
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005988# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005989 // Do not do this when waiting for a shell command to finish, we would get
5990 // the exit value here (and discard it), the exit value obtained there
5991 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005992 if (dont_check_job_ended > 0)
5993 return NULL;
5994# endif
5995
Bram Moolenaar97792de2016-10-15 18:36:49 +02005996# ifdef __NeXT__
5997 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5998# else
5999 wait_pid = waitpid(-1, &status, WNOHANG);
6000# endif
6001 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006002 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02006003 return NULL;
6004 for (job = job_list; job != NULL; job = job->jv_next)
6005 {
6006 if (job->jv_pid == wait_pid)
6007 {
6008 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006009 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02006010 job->jv_exitval = WEXITSTATUS(status);
6011 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006012 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02006013 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006014 job->jv_termsig = get_signal_name(WTERMSIG(status));
6015 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01006016 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02006017 {
6018 ch_log(job->jv_channel, "Job ended");
6019 job->jv_status = JOB_ENDED;
6020 }
6021 return job;
6022 }
6023 }
6024 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006025}
6026
Bram Moolenaar3a117e12016-10-30 21:57:52 +01006027/*
6028 * Send a (deadly) signal to "job".
6029 * Return FAIL if "how" is not a valid name.
6030 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01006031 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006032mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01006033{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006034 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006035
Bram Moolenaar923d9262016-02-25 20:56:01 +01006036 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01006037 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01006038 else if (STRCMP(how, "hup") == 0)
6039 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006040 else if (STRCMP(how, "quit") == 0)
6041 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01006042 else if (STRCMP(how, "int") == 0)
6043 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006044 else if (STRCMP(how, "kill") == 0)
6045 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02006046#ifdef SIGWINCH
6047 else if (STRCMP(how, "winch") == 0)
6048 sig = SIGWINCH;
6049#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006050 else if (isdigit(*how))
6051 sig = atoi((char *)how);
6052 else
6053 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01006054
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01006055 // Never kill ourselves!
6056 if (job->jv_pid != 0)
6057 {
6058 // TODO: have an option to only kill the process, not the group?
6059 kill(-job->jv_pid, sig);
6060 kill(job->jv_pid, sig);
6061 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01006062
Bram Moolenaar835dc632016-02-07 14:27:38 +01006063 return OK;
6064}
Bram Moolenaar76467df2016-02-12 19:30:26 +01006065
6066/*
6067 * Clear the data related to "job".
6068 */
6069 void
6070mch_clear_job(job_T *job)
6071{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006072 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01006073# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006074 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006075# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006076 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006077# endif
6078}
Bram Moolenaar835dc632016-02-07 14:27:38 +01006079#endif
6080
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006081#if defined(FEAT_TERMINAL) || defined(PROTO)
6082 int
6083mch_create_pty_channel(job_T *job, jobopt_T *options)
6084{
6085 int pty_master_fd = -1;
6086 int pty_slave_fd = -1;
6087 channel_T *channel;
6088
Bram Moolenaar59386482019-02-10 22:43:46 +01006089 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02006090 if (pty_master_fd < 0 || pty_slave_fd < 0)
6091 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006092 close(pty_slave_fd);
6093
6094 channel = add_channel();
6095 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006096 {
6097 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006098 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006099 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006100 if (job->jv_tty_out != NULL)
6101 ch_log(channel, "using pty %s on fd %d",
6102 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006103 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006104 channel->ch_keep_open = TRUE;
6105
Bram Moolenaar0f873732019-12-05 20:28:46 +01006106 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6107 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006108 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006109 channel_set_job(channel, job, options);
6110 return OK;
6111}
6112#endif
6113
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114/*
6115 * Check for CTRL-C typed by reading all available characters.
6116 * In cooked mode we should get SIGINT, no need to check.
6117 */
6118 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006119mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006121 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006122 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 fill_input_buf(FALSE);
6124}
6125
6126/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006127 * Wait "msec" msec until a character is available from the mouse, keyboard,
6128 * from inbuf[].
6129 * "msec" == -1 will block forever.
6130 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006131 * When "ignore_input" is TRUE even check for pending input when input is
6132 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006133 * "interrupted" (if not NULL) is set to TRUE when no character is available
6134 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006135 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006136 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 */
6138 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006139WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006141#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006142 return ui_wait_for_chars_or_timer(
6143 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006144#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006145 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006146#endif
6147}
6148
6149/*
6150 * Wait "msec" msec until a character is available from the mouse or keyboard
6151 * or from inbuf[].
6152 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006153 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006154 * "interrupted" (if not NULL) is set to TRUE when no character is available
6155 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006156 * When a GUI is being used, this will never get called -- webb
6157 */
6158 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006159WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006160{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161#ifdef FEAT_MOUSE_GPM
6162 int gpm_process_wanted;
6163#endif
6164#ifdef FEAT_XCLIPBOARD
6165 int rest;
6166#endif
6167 int avail;
6168
Bram Moolenaar0f873732019-12-05 20:28:46 +01006169 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 return 1;
6171
6172#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006173 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 if (WantQueryMouse)
6175 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006176 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006177 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006178 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 }
6180#endif
6181
6182 /*
6183 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6184 * events. This is a bit complicated, because they might both be defined.
6185 */
6186#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6187# ifdef FEAT_XCLIPBOARD
6188 rest = 0;
6189 if (do_xterm_trace())
6190 rest = msec;
6191# endif
6192 do
6193 {
6194# ifdef FEAT_XCLIPBOARD
6195 if (rest != 0)
6196 {
6197 msec = XT_TRACE_DELAY;
6198 if (rest >= 0 && rest < XT_TRACE_DELAY)
6199 msec = rest;
6200 if (rest >= 0)
6201 rest -= msec;
6202 }
6203# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01006204# ifdef FEAT_SOUND_MACOSX
6205 // Invoke any pending sound callbacks.
6206 process_cfrunloop();
6207# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006208# ifdef FEAT_SOUND_CANBERRA
6209 // Invoke any pending sound callbacks.
6210 if (has_sound_callback_in_queue())
6211 invoke_sound_callback();
6212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213# ifdef FEAT_MOUSE_GPM
6214 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006215 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006216 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006217 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006219 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006221# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006223 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 return 1;
6225# ifdef FEAT_XCLIPBOARD
6226 if (rest == 0 || !do_xterm_trace())
6227# endif
6228 break;
6229 }
6230 }
6231 while (FALSE
6232# ifdef FEAT_MOUSE_GPM
6233 || (gpm_process_wanted && mch_gpm_process() == 0)
6234# endif
6235# ifdef FEAT_XCLIPBOARD
6236 || (!avail && rest != 0)
6237# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006238 )
6239 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240
6241#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006242 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243#endif
6244 return avail;
6245}
6246
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006247#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248/*
6249 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006250 * "msec" == 0 will check for characters once.
6251 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006254 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006255 * "interrupted" (if not NULL) is set to TRUE when no character is available
6256 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006258 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006259RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260{
6261 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006262 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006263#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 static int busy = FALSE;
6265
Bram Moolenaar0f873732019-12-05 20:28:46 +01006266 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267# define MAY_LOOP
6268
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006269# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006270 // Remember at what time we started, so that we know how much longer we
6271 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006272 long start_msec = msec;
6273 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006275 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006276 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277# endif
6278
Bram Moolenaar0f873732019-12-05 20:28:46 +01006279 // Handle being called recursively. This may happen for the session
6280 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 if (busy)
6282 return 0;
6283#endif
6284
6285#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006286 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287#endif
6288 {
6289#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006290 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006291# ifdef FEAT_MZSCHEME
6292 int mzquantum_used = FALSE;
6293# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294#endif
6295#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006296 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006297 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 int nfd;
6299# ifdef FEAT_XCLIPBOARD
6300 int xterm_idx = -1;
6301# endif
6302# ifdef FEAT_MOUSE_GPM
6303 int gpm_idx = -1;
6304# endif
6305# ifdef USE_XSMP
6306 int xsmp_idx = -1;
6307# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006308 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006310# ifdef FEAT_MZSCHEME
6311 mzvim_check_threads();
6312 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6313 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006314 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006315 mzquantum_used = TRUE;
6316 }
6317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 fds[0].fd = fd;
6319 fds[0].events = POLLIN;
6320 nfd = 1;
6321
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006323 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 if (xterm_Shell != (Widget)0)
6325 {
6326 xterm_idx = nfd;
6327 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6328 fds[nfd].events = POLLIN;
6329 nfd++;
6330 }
6331# endif
6332# ifdef FEAT_MOUSE_GPM
6333 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6334 {
6335 gpm_idx = nfd;
6336 fds[nfd].fd = gpm_fd;
6337 fds[nfd].events = POLLIN;
6338 nfd++;
6339 }
6340# endif
6341# ifdef USE_XSMP
6342 if (xsmp_icefd != -1)
6343 {
6344 xsmp_idx = nfd;
6345 fds[nfd].fd = xsmp_icefd;
6346 fds[nfd].events = POLLIN;
6347 nfd++;
6348 }
6349# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006350#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006351 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006352#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006353 if (interrupted != NULL)
6354 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006356 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006357
6358 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006359 if (result == 0 && interrupted != NULL && ret > 0)
6360 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006361
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006362# ifdef FEAT_MZSCHEME
6363 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006364 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006365 finished = FALSE;
6366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368# ifdef FEAT_XCLIPBOARD
6369 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6370 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006371 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006373 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 finished = FALSE;
6375 }
6376# endif
6377# ifdef FEAT_MOUSE_GPM
6378 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380# endif
6381# ifdef USE_XSMP
6382 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6383 {
6384 if (fds[xsmp_idx].revents & POLLIN)
6385 {
6386 busy = TRUE;
6387 xsmp_handle_requests();
6388 busy = FALSE;
6389 }
6390 else if (fds[xsmp_idx].revents & POLLHUP)
6391 {
6392 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006393 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 xsmp_close();
6395 }
6396 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006397 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006400#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006401 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006402 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006403 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405
Bram Moolenaar0f873732019-12-05 20:28:46 +01006406#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407
6408 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006409 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006410 // These are static because they can take 8 Kbyte each and cause the
6411 // signal stack to run out with -O3.
6412 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006414 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006416# ifdef FEAT_MZSCHEME
6417 mzvim_check_threads();
6418 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6419 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006420 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006421 mzquantum_used = TRUE;
6422 }
6423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006425 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006427 tv.tv_sec = towait / 1000;
6428 tv.tv_usec = (towait % 1000) * (1000000/1000);
6429 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006431 else
6432 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433
6434 /*
6435 * Select on ready for reading and exceptional condition (end of file).
6436 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006437select_eintr:
6438 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006439 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 FD_ZERO(&efds);
6441 FD_SET(fd, &rfds);
K.Takatab247e062022-02-07 10:45:23 +00006442# ifndef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006443 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 FD_SET(fd, &efds);
6445# endif
6446 maxfd = fd;
6447
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006449 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 if (xterm_Shell != (Widget)0)
6451 {
6452 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6453 if (maxfd < ConnectionNumber(xterm_dpy))
6454 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006455
Bram Moolenaar0f873732019-12-05 20:28:46 +01006456 // An event may have already been read but not handled. In
6457 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006458 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 }
6460# endif
6461# ifdef FEAT_MOUSE_GPM
6462 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6463 {
6464 FD_SET(gpm_fd, &rfds);
6465 FD_SET(gpm_fd, &efds);
6466 if (maxfd < gpm_fd)
6467 maxfd = gpm_fd;
6468 }
6469# endif
6470# ifdef USE_XSMP
6471 if (xsmp_icefd != -1)
6472 {
6473 FD_SET(xsmp_icefd, &rfds);
6474 FD_SET(xsmp_icefd, &efds);
6475 if (maxfd < xsmp_icefd)
6476 maxfd = xsmp_icefd;
6477 }
6478# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006479# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006480 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006481# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006482 if (interrupted != NULL)
6483 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
Bram Moolenaar643b6142018-09-12 20:29:09 +02006485 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6486 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006487 result = ret > 0 && FD_ISSET(fd, &rfds);
6488 if (result)
6489 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006490 else if (interrupted != NULL && ret > 0)
6491 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006492
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006493# ifdef EINTR
6494 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006495 {
dbivolaruab16ad32021-12-29 19:41:47 +00006496 // Check whether the EINTR is caused by SIGTSTP
6497 if (got_tstp && !in_mch_suspend)
6498 {
6499 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006500
dbivolaruab16ad32021-12-29 19:41:47 +00006501 ea.forceit = TRUE;
6502 ex_stop(&ea);
6503 got_tstp = FALSE;
6504 }
6505
Bram Moolenaar0f873732019-12-05 20:28:46 +01006506 // Check whether window has been resized, EINTR may be caused by
6507 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006508 if (do_resize)
6509 handle_resize();
6510
Bram Moolenaar0f873732019-12-05 20:28:46 +01006511 // Interrupted by a signal, need to try again. We ignore msec
6512 // here, because we do want to check even after a timeout if
6513 // characters are available. Needed for reading output of an
6514 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006515 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006516 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006517# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006518# ifdef __TANDEM
6519 if (ret == -1 && errno == ENOTSUP)
6520 {
6521 FD_ZERO(&rfds);
6522 FD_ZERO(&efds);
6523 ret = 0;
6524 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006525# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006526# ifdef FEAT_MZSCHEME
6527 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006528 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006529 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530# endif
6531
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532# ifdef FEAT_XCLIPBOARD
6533 if (ret > 0 && xterm_Shell != (Widget)0
6534 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6535 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006536 xterm_update(); // Maybe we should hand out clipboard
6537 // continue looping when we only got the X event and the input
6538 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (--ret == 0 && !input_available())
6540 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006541 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 finished = FALSE;
6543 }
6544 }
6545# endif
6546# ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00006547 if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 {
6549 if (FD_ISSET(gpm_fd, &efds))
6550 gpm_close();
6551 else if (FD_ISSET(gpm_fd, &rfds))
6552 *check_for_gpm = 1;
6553 }
6554# endif
6555# ifdef USE_XSMP
6556 if (ret > 0 && xsmp_icefd != -1)
6557 {
6558 if (FD_ISSET(xsmp_icefd, &efds))
6559 {
6560 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006561 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 xsmp_close();
6563 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006564 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 }
6566 else if (FD_ISSET(xsmp_icefd, &rfds))
6567 {
6568 busy = TRUE;
6569 xsmp_handle_requests();
6570 busy = FALSE;
6571 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006572 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 }
6574 }
6575# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006576#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006577 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006578 if (ret >= 0)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01006579 (void)channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581
Bram Moolenaar0f873732019-12-05 20:28:46 +01006582#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583
6584#ifdef MAY_LOOP
6585 if (finished || msec == 0)
6586 break;
6587
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006588# ifdef FEAT_CLIENTSERVER
6589 if (server_waiting())
6590 break;
6591# endif
6592
Bram Moolenaar0f873732019-12-05 20:28:46 +01006593 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 if (msec > 0)
6595 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006596# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006597 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006598 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006600 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 msec = msec / 2;
6602# endif
6603 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006604 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 }
6606#endif
6607 }
6608
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006609 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610}
6611
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006613 * Expand a path into all matching files and/or directories. Handles "*",
6614 * "?", "[a-z]", "**", etc.
6615 * "path" has backslashes before chars that are not to be expanded.
6616 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 */
6618 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006619mch_expandpath(
6620 garray_T *gap,
6621 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006622 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623{
Bram Moolenaar02743632005-07-25 20:42:36 +00006624 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
6627/*
6628 * mch_expand_wildcards() - this code does wild-card pattern matching using
6629 * the shell
6630 *
6631 * return OK for success, FAIL for error (you may lose some memory) and put
6632 * an error message in *file.
6633 *
6634 * num_pat is number of input patterns
6635 * pat is array of pointers to input patterns
6636 * num_file is pointer to number of matched file names
6637 * file is pointer to array of pointers to matched file names
6638 */
6639
6640#ifndef SEEK_SET
6641# define SEEK_SET 0
6642#endif
6643#ifndef SEEK_END
6644# define SEEK_END 2
6645#endif
6646
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006647#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006648
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006650mch_expand_wildcards(
6651 int num_pat,
6652 char_u **pat,
6653 int *num_file,
6654 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006655 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
6657 int i;
6658 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006659 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 char_u *p;
6661 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
Bram Moolenaarc7247912008-01-13 12:54:11 +00006663 /*
6664 * This is the non-OS/2 implementation (really Unix).
6665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 int j;
6667 char_u *tempname;
6668 char_u *command;
6669 FILE *fd;
6670 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006671#define STYLE_ECHO 0 // use "echo", the default
6672#define STYLE_GLOB 1 // use "glob", for csh
6673#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6674#define STYLE_PRINT 3 // use "print -N", for zsh
6675#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6676 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 int shell_style = STYLE_ECHO;
6678 int check_spaces;
6679 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006680 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006681 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006682 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
Bram Moolenaar0f873732019-12-05 20:28:46 +01006684 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 *file = NULL;
6686
6687 /*
6688 * If there are no wildcards, just copy the names to allocated memory.
6689 * Saves a lot of time, because we don't have to start a new shell.
6690 */
6691 if (!have_wildcard(num_pat, pat))
6692 return save_patterns(num_pat, pat, num_file, file);
6693
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006694# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006695 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006696 if (sandbox != 0 && check_secure())
6697 return FAIL;
6698# endif
6699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 /*
6701 * Don't allow the use of backticks in secure and restricted mode.
6702 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006703 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 for (i = 0; i < num_pat; ++i)
6705 if (vim_strchr(pat[i], '`') != NULL
6706 && (check_restricted() || check_secure()))
6707 return FAIL;
6708
6709 /*
6710 * get a name for the temp file
6711 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006712 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006714 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 return FAIL;
6716 }
6717
6718 /*
6719 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006720 * file.
6721 * STYLE_BT: NL separated
6722 * If expanding `cmd` execute it directly.
6723 * STYLE_GLOB: NUL separated
6724 * If we use *csh, "glob" will work better than "echo".
6725 * STYLE_PRINT: NL or NUL separated
6726 * If we use *zsh, "print -N" will work better than "glob".
6727 * STYLE_VIMGLOB: NL separated
6728 * If we use *sh*, we define "vimglob()".
6729 * STYLE_ECHO: space separated.
6730 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 */
6732 if (num_pat == 1 && *pat[0] == '`'
6733 && (len = STRLEN(pat[0])) > 2
6734 && *(pat[0] + len - 1) == '`')
6735 shell_style = STYLE_BT;
6736 else if ((len = STRLEN(p_sh)) >= 3)
6737 {
6738 if (STRCMP(p_sh + len - 3, "csh") == 0)
6739 shell_style = STYLE_GLOB;
6740 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6741 shell_style = STYLE_PRINT;
6742 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006743 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6744 "sh") != NULL)
6745 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746
Bram Moolenaar0f873732019-12-05 20:28:46 +01006747 // Compute the length of the command. We need 2 extra bytes: for the
6748 // optional '&' and for the NUL.
6749 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006751 if (shell_style == STYLE_VIMGLOB)
6752 len += STRLEN(sh_vimglob_func);
6753
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006754 for (i = 0; i < num_pat; ++i)
6755 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006756 // Count the length of the patterns in the same way as they are put in
6757 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006758#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006759 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006760#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006761 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006762 for (j = 0; pat[i][j] != NUL; ++j)
6763 {
6764 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006765 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006766 ++len;
6767 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006768#endif
6769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 command = alloc(len);
6771 if (command == NULL)
6772 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006773 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 vim_free(tempname);
6775 return FAIL;
6776 }
6777
6778 /*
6779 * Build the shell command:
6780 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6781 * recognizes this).
6782 * - Add the shell command to print the expanded names.
6783 * - Add the temp file name.
6784 * - Add the file name patterns.
6785 */
6786 if (shell_style == STYLE_BT)
6787 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006788 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006789 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006790 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006792 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006793 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006795 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006797 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 *p = ' ';
6799 }
6800 STRCAT(command, ">");
6801 }
6802 else
6803 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006804 STRCPY(command, "");
6805 if (shell_style == STYLE_GLOB)
6806 {
6807 // Assume the nonomatch option is valid only for csh like shells,
6808 // otherwise, this may set the positional parameters for the shell,
6809 // e.g. "$*".
6810 if (flags & EW_NOTFOUND)
6811 STRCAT(command, "set nonomatch; ");
6812 else
6813 STRCAT(command, "unset nonomatch; ");
6814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (shell_style == STYLE_GLOB)
6816 STRCAT(command, "glob >");
6817 else if (shell_style == STYLE_PRINT)
6818 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006819 else if (shell_style == STYLE_VIMGLOB)
6820 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 else
6822 STRCAT(command, "echo >");
6823 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006824
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006826
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 if (shell_style != STYLE_BT)
6828 for (i = 0; i < num_pat; ++i)
6829 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006830 // When using system() always add extra quotes, because the shell
6831 // is started twice. Otherwise put a backslash before special
6832 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833#ifdef USE_SYSTEM
6834 STRCAT(command, " \"");
6835 STRCAT(command, pat[i]);
6836 STRCAT(command, "\"");
6837#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006838 int intick = FALSE;
6839
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 p = command + STRLEN(command);
6841 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006842 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006843 {
6844 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006845 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006846 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6847 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006848 // Remove a backslash, take char literally. But keep
6849 // backslash inside backticks, before a special character
6850 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006851 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006852 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6853 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006854 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006855 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006856 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006857 else if (!intick
6858 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6859 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006860 // Put a backslash before a special character, but not
6861 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6862 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006863 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006864
Bram Moolenaar0f873732019-12-05 20:28:46 +01006865 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006866 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 *p = NUL;
6869#endif
6870 }
6871 if (flags & EW_SILENT)
6872 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006873 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006874 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875
6876 /*
6877 * Using zsh -G: If a pattern has no matches, it is just deleted from
6878 * the argument list, otherwise zsh gives an error message and doesn't
6879 * expand any other pattern.
6880 */
6881 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006882 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883
6884 /*
6885 * If we use -f then shell variables set in .cshrc won't get expanded.
6886 * vi can do it, so we will too, but it is only necessary if there is a "$"
6887 * in one of the patterns, otherwise we can still use the fast option.
6888 */
6889 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006890 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891
6892 /*
6893 * execute the shell command
6894 */
6895 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6896
Bram Moolenaar0f873732019-12-05 20:28:46 +01006897 // When running in the background, give it some time to create the temp
6898 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006899 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006900 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901
Bram Moolenaar0f873732019-12-05 20:28:46 +01006902 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 show_shell_mess = TRUE;
6904 vim_free(command);
6905
Bram Moolenaar0f873732019-12-05 20:28:46 +01006906 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 {
6908 mch_remove(tempname);
6909 vim_free(tempname);
6910 /*
6911 * With interactive completion, the error message is not printed.
6912 * However with USE_SYSTEM, I don't know how to turn off error messages
6913 * from the shell, so screen may still get messed up -- webb.
6914 */
6915#ifndef USE_SYSTEM
6916 if (!(flags & EW_SILENT))
6917#endif
6918 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006919 redraw_later_clear(); // probably messed up screen
6920 msg_putchar('\n'); // clear bottom line quickly
6921 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922#ifdef USE_SYSTEM
6923 if (!(flags & EW_SILENT))
6924#endif
6925 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006926 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006927 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 }
6929 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006930 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6931 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 if (shell_style == STYLE_BT)
6933 return FAIL;
6934 goto notfound;
6935 }
6936
6937 /*
6938 * read the names from the file into memory
6939 */
6940 fd = fopen((char *)tempname, READBIN);
6941 if (fd == NULL)
6942 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006943 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 if (!(flags & EW_SILENT))
6945 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006946 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006947 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 }
6949 vim_free(tempname);
6950 goto notfound;
6951 }
6952 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006953 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006955 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006956 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006957 buffer = NULL;
6958 else
6959 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 if (buffer == NULL)
6961 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006962 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 mch_remove(tempname);
6964 vim_free(tempname);
6965 fclose(fd);
6966 return FAIL;
6967 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006968 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 i = fread((char *)buffer, 1, len, fd);
6970 fclose(fd);
6971 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006972 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006974 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006975 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 vim_free(tempname);
6977 vim_free(buffer);
6978 return FAIL;
6979 }
6980 vim_free(tempname);
6981
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006982# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006983 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006985 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6987 *p++ = buffer[i];
6988 len = p - buffer;
6989# endif
6990
6991
Bram Moolenaar0f873732019-12-05 20:28:46 +01006992 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (shell_style == STYLE_ECHO)
6994 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006995 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006997 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 {
6999 while (*p != ' ' && *p != '\n')
7000 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007001 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 }
7003 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007004 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00007005 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007007 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007009 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
7011 while (*p != '\n' && *p != NUL)
7012 ++p;
7013 if (*p != NUL)
7014 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007015 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 }
7017 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007018 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 else
7020 {
7021 /*
7022 * Some versions of zsh use spaces instead of NULs to separate
7023 * results. Only do this when there is no NUL before the end of the
7024 * buffer, otherwise we would never be able to use file names with
7025 * embedded spaces when zsh does use NULs.
7026 * When we found a NUL once, we know zsh is OK, set did_find_nul and
7027 * don't check for spaces again.
7028 */
7029 check_spaces = FALSE;
7030 if (shell_style == STYLE_PRINT && !did_find_nul)
7031 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007032 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007033 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01007034 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 did_find_nul = TRUE;
7036 else
7037 check_spaces = TRUE;
7038 }
7039
7040 /*
7041 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
7042 * already is one, for STYLE_GLOB it needs to be added.
7043 */
7044 if (len && buffer[len - 1] == NUL)
7045 --len;
7046 else
7047 buffer[len] = NUL;
7048 i = 0;
7049 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007050 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {
7052 ++i;
7053 *p = NUL;
7054 }
7055 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007056 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 }
7058 if (i == 0)
7059 {
7060 /*
7061 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
7062 * /bin/sh will happily expand it to nothing rather than returning an
7063 * error; and hey, it's good to check anyway -- webb.
7064 */
7065 vim_free(buffer);
7066 goto notfound;
7067 }
7068 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007069 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 if (*file == NULL)
7071 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007072 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 vim_free(buffer);
7074 return FAIL;
7075 }
7076
7077 /*
7078 * Isolate the individual file names.
7079 */
7080 p = buffer;
7081 for (i = 0; i < *num_file; ++i)
7082 {
7083 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007084 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00007085 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
7086 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00007088 while (!(shell_style == STYLE_ECHO && *p == ' ')
7089 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007091 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 *p = NUL;
7093 else
7094 {
7095 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007096 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 }
7098 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007099 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007101 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007103 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 }
7105 }
7106
7107 /*
7108 * Move the file names to allocated memory.
7109 */
7110 for (j = 0, i = 0; i < *num_file; ++i)
7111 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007112 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7114 continue;
7115
Bram Moolenaar0f873732019-12-05 20:28:46 +01007116 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 dir = (mch_isdir((*file)[i]));
7118 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7119 continue;
7120
Bram Moolenaar0f873732019-12-05 20:28:46 +01007121 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007122 if (!dir && (flags & EW_EXEC)
7123 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007124 continue;
7125
Bram Moolenaar964b3742019-05-24 18:54:09 +02007126 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 if (p)
7128 {
7129 STRCPY(p, (*file)[i]);
7130 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007131 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 (*file)[j++] = p;
7133 }
7134 }
7135 vim_free(buffer);
7136 *num_file = j;
7137
Bram Moolenaar0f873732019-12-05 20:28:46 +01007138 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007140 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 goto notfound;
7142 }
7143
7144 return OK;
7145
7146notfound:
7147 if (flags & EW_NOTFOUND)
7148 return save_patterns(num_pat, pat, num_file, file);
7149 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150}
7151
Bram Moolenaar0f873732019-12-05 20:28:46 +01007152#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007155save_patterns(
7156 int num_pat,
7157 char_u **pat,
7158 int *num_file,
7159 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160{
7161 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007162 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007164 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 if (*file == NULL)
7166 return FAIL;
7167 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007168 {
7169 s = vim_strsave(pat[i]);
7170 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007171 // Be compatible with expand_filename(): halve the number of
7172 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007173 backslash_halve(s);
7174 (*file)[i] = s;
7175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 *num_file = num_pat;
7177 return OK;
7178}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180/*
7181 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7182 * expand.
7183 */
7184 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007185mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007187 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 if (*p == '\\' && p[1] != NUL)
7190 ++p;
7191 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 if (vim_strchr((char_u *)
7193#ifdef VMS
7194 "*?%"
7195#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197#endif
7198 , *p) != NULL)
7199 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 }
7201 return FALSE;
7202}
7203
7204/*
7205 * Return TRUE if the string "p" contains a wildcard.
7206 * Don't recognize '~' at the end as a wildcard.
7207 */
7208 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007209mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007211 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 if (*p == '\\' && p[1] != NUL)
7214 ++p;
7215 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 if (vim_strchr((char_u *)
7217#ifdef VMS
7218 "*?%$"
7219#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221#endif
7222 , *p) != NULL
7223 || (*p == '~' && p[1] != NUL))
7224 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 }
7226 return FALSE;
7227}
7228
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007230have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231{
7232 int i;
7233
7234 for (i = 0; i < num; i++)
7235 if (mch_has_wildcard(file[i]))
7236 return 1;
7237 return 0;
7238}
7239
7240 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007241have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
7243 int i;
7244
7245 for (i = 0; i < num; i++)
7246 if (vim_strchr(file[i], '$') != NULL)
7247 return TRUE;
7248 return FALSE;
7249}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007251#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252/*
7253 * Scaled-down version of rename(), which is missing in Xenix.
7254 * This version can only move regular files and will fail if the
7255 * destination exists.
7256 */
7257 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007258mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259{
7260 struct stat st;
7261
Bram Moolenaar0f873732019-12-05 20:28:46 +01007262 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007264 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007266 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 return 0;
7268 return -1;
7269}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007270#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007272#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007273# if defined(DYNAMIC_GPM) || defined(PROTO)
7274/*
7275 * Initialize Gpm's symbols for dynamic linking.
7276 * Must be called only if libgpm_hinst is NULL.
7277 */
7278 static int
7279load_libgpm(void)
7280{
7281 libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL);
7282
7283 if (libgpm_hinst == NULL)
7284 {
7285 if (p_verbose > 0)
7286 smsg_attr(HL_ATTR(HLF_W),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007287 _("Could not load gpm library: %s"), dlerror());
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007288 return FAIL;
7289 }
7290
7291 if (
7292 (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL
7293 || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL
7294 || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL
7295 || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL
7296 || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL
7297 )
7298 {
7299 semsg(_(e_could_not_load_library_str_str), "gpm", dlerror());
7300 dlclose(libgpm_hinst);
7301 libgpm_hinst = NULL;
7302 dll_gpm_flag = NULL;
7303 dll_gpm_fd = NULL;
7304 return FAIL;
7305 }
7306 return OK;
7307}
7308
7309 int
7310gpm_available(void)
7311{
7312 return libgpm_hinst != NULL || load_libgpm() == OK;
7313}
7314# endif // DYNAMIC_GPM
7315
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316/*
7317 * Initializes connection with gpm (if it isn't already opened)
7318 * Return 1 if succeeded (or connection already opened), 0 if failed
7319 */
7320 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007321gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007323 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007325#ifdef DYNAMIC_GPM
7326 if (!gpm_available())
7327 return 0;
7328#endif
7329
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007330 if (gpm_flag)
7331 return 1; // already open
7332
7333 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7334 gpm_connect.defaultMask = ~GPM_HARD;
7335 // Default handling for mouse move
7336 gpm_connect.minMod = 0; // Handle any modifier keys
7337 gpm_connect.maxMod = 0xffff;
7338 if (Gpm_Open(&gpm_connect, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007340 // gpm library tries to handling TSTP causes
7341 // problems. Anyways, we close connection to Gpm whenever
7342 // we are going to suspend or starting an external process
7343 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007344# ifdef SIGTSTP
ichizok378447f2023-05-11 22:25:42 +01007345 mch_signal(SIGTSTP, restricted ? SIG_IGN : sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007346# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007347 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007349 if (gpm_fd == -2)
7350 Gpm_Close(); // We don't want to talk to xterm via gpm
7351 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352}
7353
7354/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007355 * Returns TRUE if the GPM mouse is enabled.
7356 */
7357 int
7358gpm_enabled(void)
7359{
7360 return gpm_flag && gpm_fd >= 0;
7361}
7362
7363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 */
7366 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007367gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007369 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 Gpm_Close();
7371}
7372
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007373/*
7374 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007376 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 */
7378 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007379mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380{
7381 int button;
7382 static Gpm_Event gpm_event;
7383 char_u string[6];
7384 int_u vim_modifiers;
7385 int row,col;
7386 unsigned char buttons_mask;
7387 unsigned char gpm_modifiers;
7388 static unsigned char old_buttons = 0;
7389
7390 Gpm_GetEvent(&gpm_event);
7391
7392#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007393 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 if (hold_gui_events)
7395 return 0;
7396#endif
7397
7398 row = gpm_event.y - 1;
7399 col = gpm_event.x - 1;
7400
Bram Moolenaar0f873732019-12-05 20:28:46 +01007401 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 string[1] = 'M';
7403 string[2] = 'G';
7404 switch (GPM_BARE_EVENTS(gpm_event.type))
7405 {
7406 case GPM_DRAG:
7407 string[3] = MOUSE_DRAG;
7408 break;
7409 case GPM_DOWN:
7410 buttons_mask = gpm_event.buttons & ~old_buttons;
7411 old_buttons = gpm_event.buttons;
7412 switch (buttons_mask)
7413 {
7414 case GPM_B_LEFT:
7415 button = MOUSE_LEFT;
7416 break;
7417 case GPM_B_MIDDLE:
7418 button = MOUSE_MIDDLE;
7419 break;
7420 case GPM_B_RIGHT:
7421 button = MOUSE_RIGHT;
7422 break;
7423 default:
7424 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007425 // Don't know what to do. Can more than one button be
7426 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
7428 string[3] = (char_u)(button | 0x20);
7429 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7430 break;
7431 case GPM_UP:
7432 string[3] = MOUSE_RELEASE;
7433 old_buttons &= ~gpm_event.buttons;
7434 break;
7435 default:
7436 return 0;
7437 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007438 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 gpm_modifiers = gpm_event.modifiers;
7440 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007441 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7442 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7443 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7445 vim_modifiers |= MOUSE_SHIFT;
7446
7447 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7448 vim_modifiers |= MOUSE_CTRL;
7449 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7450 vim_modifiers |= MOUSE_ALT;
7451 string[3] |= vim_modifiers;
7452 string[4] = (char_u)(col + ' ' + 1);
7453 string[5] = (char_u)(row + ' ' + 1);
7454 add_to_input_buf(string, 6);
7455 return 6;
7456}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007457#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007459#ifdef FEAT_SYSMOUSE
7460/*
7461 * Initialize connection with sysmouse.
7462 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7463 * output, any sysmouse output than will be processed via sig_sysmouse().
7464 * Return OK if succeeded, FAIL if failed.
7465 */
7466 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007467sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007468{
7469 struct mouse_info mouse;
7470
7471 mouse.operation = MOUSE_MODE;
7472 mouse.u.mode.mode = 0;
7473 mouse.u.mode.signal = SIGUSR2;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007474 if (ioctl(1, CONS_MOUSECTL, &mouse) == -1)
7475 return FAIL;
7476
ichizok378447f2023-05-11 22:25:42 +01007477 mch_signal(SIGUSR2, sig_sysmouse);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007478 mouse.operation = MOUSE_SHOW;
7479 ioctl(1, CONS_MOUSECTL, &mouse);
7480 return OK;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007481}
7482
7483/*
7484 * Stop processing SIGUSR2 signals, and also make sure that
7485 * virtual console do not send us any sysmouse related signal.
7486 */
7487 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007488sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007489{
7490 struct mouse_info mouse;
7491
ichizok378447f2023-05-11 22:25:42 +01007492 mch_signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007493 mouse.operation = MOUSE_MODE;
7494 mouse.u.mode.mode = 0;
7495 mouse.u.mode.signal = 0;
7496 ioctl(1, CONS_MOUSECTL, &mouse);
7497}
7498
7499/*
7500 * Gets info from sysmouse and adds special keys to input buf.
7501 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007502 static void
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007503sig_sysmouse SIGDEFARG(sigarg)
7504{
7505 struct mouse_info mouse;
7506 struct video_info video;
7507 char_u string[6];
7508 int row, col;
7509 int button;
7510 int buttons;
7511 static int oldbuttons = 0;
7512
7513#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007514 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007515 if (hold_gui_events)
7516 return;
7517#endif
7518
7519 mouse.operation = MOUSE_GETINFO;
7520 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7521 && ioctl(1, FBIO_MODEINFO, &video) != -1
7522 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7523 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7524 {
7525 row = mouse.u.data.y / video.vi_cheight;
7526 col = mouse.u.data.x / video.vi_cwidth;
7527 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007528 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007529 string[1] = 'M';
7530 string[2] = 'S';
7531 if (oldbuttons == buttons && buttons != 0)
7532 {
7533 button = MOUSE_DRAG;
7534 }
7535 else
7536 {
7537 switch (buttons)
7538 {
7539 case 0:
7540 button = MOUSE_RELEASE;
7541 break;
7542 case 1:
7543 button = MOUSE_LEFT;
7544 break;
7545 case 2:
7546 button = MOUSE_MIDDLE;
7547 break;
7548 case 4:
7549 button = MOUSE_RIGHT;
7550 break;
7551 default:
7552 return;
7553 }
7554 oldbuttons = buttons;
7555 }
7556 string[3] = (char_u)(button);
7557 string[4] = (char_u)(col + ' ' + 1);
7558 string[5] = (char_u)(row + ' ' + 1);
7559 add_to_input_buf(string, 6);
7560 }
7561 return;
7562}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007563#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007564
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007566typedef char_u * (*STRPROCSTR)(char_u *);
7567typedef char_u * (*INTPROCSTR)(int);
7568typedef int (*STRPROCINT)(char_u *);
7569typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570
7571/*
7572 * Call a DLL routine which takes either a string or int param
7573 * and returns an allocated string.
7574 */
7575 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007576mch_libcall(
7577 char_u *libname,
7578 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007579 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007580 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007581 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007582 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583{
7584# if defined(USE_DLOPEN)
7585 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007586 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587# else
7588 shl_t hinstLib;
7589# endif
7590 STRPROCSTR ProcAdd;
7591 INTPROCSTR ProcAddI;
7592 char_u *retval_str = NULL;
7593 int retval_int = 0;
7594 int success = FALSE;
7595
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007596 /*
7597 * Get a handle to the DLL module.
7598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007600 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007601 (void)dlerror();
7602
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 hinstLib = dlopen((char *)libname, RTLD_LAZY
7604# ifdef RTLD_LOCAL
7605 | RTLD_LOCAL
7606# endif
7607 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007608 if (hinstLib == NULL)
7609 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007610 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007611 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007612 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007613 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615# else
7616 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7617# endif
7618
Bram Moolenaar0f873732019-12-05 20:28:46 +01007619 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 if (hinstLib != NULL)
7621 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007622# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 /*
7624 * Catch a crash when calling the library function. For example when
7625 * using a number where a string pointer is expected.
7626 */
7627 mch_startjmp();
7628 if (SETJMP(lc_jump_env) != 0)
7629 {
7630 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007631# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007632 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 mch_didjmp();
7635 }
7636 else
7637# endif
7638 {
7639 retval_str = NULL;
7640 retval_int = 0;
7641
7642 if (argstring != NULL)
7643 {
7644# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007645 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007646 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647# else
7648 if (shl_findsym(&hinstLib, (const char *)funcname,
7649 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7650 ProcAdd = NULL;
7651# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007652 if ((success = (ProcAdd != NULL
7653# if defined(USE_DLOPEN)
7654 && dlerr == NULL
7655# endif
7656 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {
7658 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007659 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 else
7661 retval_str = (ProcAdd)(argstring);
7662 }
7663 }
7664 else
7665 {
7666# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007667 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007668 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669# else
7670 if (shl_findsym(&hinstLib, (const char *)funcname,
7671 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7672 ProcAddI = NULL;
7673# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007674 if ((success = (ProcAddI != NULL
7675# if defined(USE_DLOPEN)
7676 && dlerr == NULL
7677# endif
7678 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 {
7680 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007681 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 else
7683 retval_str = (ProcAddI)(argint);
7684 }
7685 }
7686
Bram Moolenaar0f873732019-12-05 20:28:46 +01007687 // Save the string before we free the library.
7688 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 if (string_result == NULL)
7690 *number_result = retval_int;
7691 else if (retval_str != NULL
7692 && retval_str != (char_u *)1
7693 && retval_str != (char_u *)-1)
7694 *string_result = vim_strsave(retval_str);
7695 }
7696
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007697# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 mch_endjmp();
7699# ifdef SIGHASARG
7700 if (lc_signal != 0)
7701 {
7702 int i;
7703
Bram Moolenaar0f873732019-12-05 20:28:46 +01007704 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 for (i = 0; signal_info[i].sig != -1; i++)
7706 if (lc_signal == signal_info[i].sig)
7707 break;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00007708 semsg(e_got_sig_str_in_libcall, signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 }
7710# endif
7711# endif
7712
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007714 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007715 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007716 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007717
Bram Moolenaar0f873732019-12-05 20:28:46 +01007718 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 (void)dlclose(hinstLib);
7720# else
7721 (void)shl_unload(hinstLib);
7722# endif
7723 }
7724
7725 if (!success)
7726 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007727 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 return FAIL;
7729 }
7730
7731 return OK;
7732}
7733#endif
7734
7735#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007736static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737static int xterm_button;
7738
7739/*
7740 * Setup a dummy window for X selections in a terminal.
7741 */
7742 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007743setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744{
7745 int z = 0;
7746 char *strp = "";
7747 Widget AppShell;
7748
7749 if (!x_connect_to_server())
7750 return;
7751
7752 open_app_context();
7753 if (app_context != NULL && xterm_Shell == (Widget)0)
7754 {
7755 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007756# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007758# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007759# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007760 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761
7762 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007763 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764# endif
7765
Bram Moolenaar0f873732019-12-05 20:28:46 +01007766 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 oldhandler = XSetErrorHandler(x_error_check);
7768
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007769# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007770 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7772 mch_startjmp();
7773 if (SETJMP(lc_jump_env) != 0)
7774 {
7775 mch_didjmp();
7776 xterm_dpy = NULL;
7777 }
7778 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007779# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 {
7781 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7782 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007783 if (xterm_dpy != NULL)
7784 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007785# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 }
7789
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007790# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007791 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007793# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007794 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 (void)XSetErrorHandler(oldhandler);
7796
7797 if (xterm_dpy == NULL)
7798 {
7799 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007800 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 return;
7802 }
7803
Bram Moolenaar0f873732019-12-05 20:28:46 +01007804 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 (void)XSetIOErrorHandler(x_IOerror_handler);
7806
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007807# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007809 {
7810 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007811 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007812 verbose_leave();
7813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814# endif
7815
Bram Moolenaar0f873732019-12-05 20:28:46 +01007816 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7818 applicationShellWidgetClass, xterm_dpy,
7819 NULL);
7820 if (AppShell == (Widget)0)
7821 return;
7822 xterm_Shell = XtVaCreatePopupShell("VIM",
7823 topLevelShellWidgetClass, AppShell,
7824 XtNmappedWhenManaged, 0,
7825 XtNwidth, 1,
7826 XtNheight, 1,
7827 NULL);
7828 if (xterm_Shell == (Widget)0)
7829 return;
7830
7831 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007832 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 if (x11_display == NULL)
7834 x11_display = xterm_dpy;
7835
7836 XtRealizeWidget(xterm_Shell);
7837 XSync(xterm_dpy, False);
7838 xterm_update();
7839 }
7840 if (xterm_Shell != (Widget)0)
7841 {
7842 clip_init(TRUE);
7843 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7844 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007845 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 if (test_x11_window(xterm_dpy) == FAIL)
7847 x11_window = 0;
7848 if (x11_window != 0)
7849 xterm_trace = 0;
7850 }
7851}
7852
7853 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007854start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
7856 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7857 return;
7858 xterm_trace = 1;
7859 xterm_button = button;
7860 do_xterm_trace();
7861}
7862
7863
7864 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007865stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866{
7867 if (xterm_trace < 0)
7868 return;
7869 xterm_trace = 0;
7870}
7871
7872/*
7873 * Query the xterm pointer and generate mouse termcodes if necessary
7874 * return TRUE if dragging is active, else FALSE
7875 */
7876 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007877do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878{
7879 Window root, child;
7880 int root_x, root_y;
7881 int win_x, win_y;
7882 int row, col;
7883 int_u mask_return;
7884 char_u buf[50];
7885 char_u *strp;
7886 long got_hints;
7887 static char_u *mouse_code;
7888 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7889 static int prev_row = 0, prev_col = 0;
7890 static XSizeHints xterm_hints;
7891
7892 if (xterm_trace <= 0)
7893 return FALSE;
7894
7895 if (xterm_trace == 1)
7896 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007897 // Get the hints just before tracking starts. The font size might
7898 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007899 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7900 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 || xterm_hints.width_inc <= 1
7902 || xterm_hints.height_inc <= 1)
7903 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007904 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 return FALSE;
7906 }
7907
Bram Moolenaar0f873732019-12-05 20:28:46 +01007908 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 mouse_code = find_termcode(mouse_name);
7910 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007911 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 xterm_trace = 2;
7913
Bram Moolenaar0f873732019-12-05 20:28:46 +01007914 // Find the offset of the chars, there might be a scrollbar on the
7915 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7917 &win_x, &win_y, &mask_return);
7918 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7919 - (xterm_hints.height_inc / 2);
7920 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7921 xterm_hints.y = 2;
7922 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7923 - (xterm_hints.width_inc / 2);
7924 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7925 xterm_hints.x = 2;
7926 return TRUE;
7927 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007928 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {
7930 xterm_trace = 0;
7931 return FALSE;
7932 }
7933
7934 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7935 &win_x, &win_y, &mask_return);
7936
7937 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7938 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7939 if (row == prev_row && col == prev_col)
7940 return TRUE;
7941
7942 STRCPY(buf, mouse_code);
7943 strp = buf + STRLEN(buf);
7944 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7945 *strp++ = (char_u)(col + ' ' + 1);
7946 *strp++ = (char_u)(row + ' ' + 1);
7947 *strp = 0;
7948 add_to_input_buf(buf, STRLEN(buf));
7949
7950 prev_row = row;
7951 prev_col = col;
7952 return TRUE;
7953}
7954
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007955# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956/*
7957 * Destroy the display, window and app_context. Required for GTK.
7958 */
7959 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007960clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961{
7962 if (xterm_Shell != (Widget)0)
7963 {
7964 XtDestroyWidget(xterm_Shell);
7965 xterm_Shell = (Widget)0;
7966 }
7967 if (xterm_dpy != NULL)
7968 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007969# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007970 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 if (x11_display == xterm_dpy)
7974 x11_display = NULL;
7975 xterm_dpy = NULL;
7976 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007977# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 if (app_context != (XtAppContext)NULL)
7979 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007980 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 XtDestroyApplicationContext(app_context);
7982 app_context = (XtAppContext)NULL;
7983 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985}
7986# endif
7987
7988/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007989 * Catch up with GUI or X events.
7990 */
7991 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007992clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007993{
7994# ifdef FEAT_GUI
7995 if (gui.in_use)
7996 gui_mch_update();
7997 else
7998# endif
7999 if (xterm_Shell != (Widget)0)
8000 xterm_update();
8001}
8002
8003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 * Catch up with any queued X events. This may put keyboard input into the
8005 * input buffer, call resize call-backs, trigger timers etc. If there is
8006 * nothing in the X event queue (& no timers pending), then we return
8007 * immediately.
8008 */
8009 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008010xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011{
8012 XEvent event;
8013
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008014 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008016 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008018 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008019 break;
8020
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008021 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008022 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008023 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008024 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008025#ifdef FEAT_CLIENTSERVER
8026 {
8027 XPropertyEvent *e = (XPropertyEvent *)&event;
8028
8029 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008031 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008034 XtDispatchEvent(&event);
8035 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008036 else
8037 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008038 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008039 XtAppProcessEvent(app_context, mask);
8040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 }
8042}
8043
8044 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008045clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046{
8047 if (xterm_Shell != (Widget)0)
8048 return clip_x11_own_selection(xterm_Shell, cbd);
8049 return FAIL;
8050}
8051
8052 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008053clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054{
8055 if (xterm_Shell != (Widget)0)
8056 clip_x11_lose_selection(xterm_Shell, cbd);
8057}
8058
8059 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008060clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061{
8062 if (xterm_Shell != (Widget)0)
8063 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
8064}
8065
8066 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008067clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068{
8069 clip_x11_set_selection(cbd);
8070}
8071#endif
8072
8073
8074#if defined(USE_XSMP) || defined(PROTO)
8075/*
8076 * Code for X Session Management Protocol.
8077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078
8079# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080/*
8081 * This is our chance to ask the user if they want to save,
8082 * or abort the logout
8083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008085xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086{
Bram Moolenaare1004402020-10-24 20:49:43 +02008087 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 int cancel_shutdown = False;
8089
Bram Moolenaare1004402020-10-24 20:49:43 +02008090 save_cmod_flags = cmdmod.cmod_flags;
8091 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01008092 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01008093 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02008095 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01008096 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 out_flush();
8098
Bram Moolenaar0f873732019-12-05 20:28:46 +01008099 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 SmcInteractDone(smc_conn, cancel_shutdown);
8101
Bram Moolenaar0f873732019-12-05 20:28:46 +01008102 // Finish off
8103 // Only end save-yourself here if we're not cancelling shutdown;
8104 // we'll get a cancelled callback later in which we'll end it.
8105 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 if (!cancel_shutdown)
8107 {
8108 xsmp.save_yourself = False;
8109 SmcSaveYourselfDone(smc_conn, True);
8110 }
8111}
8112# endif
8113
8114/*
8115 * Callback that starts save-yourself.
8116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008118xsmp_handle_save_yourself(
8119 SmcConn smc_conn,
8120 SmPointer client_data UNUSED,
8121 int save_type UNUSED,
8122 Bool shutdown,
8123 int interact_style UNUSED,
8124 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008126 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 if (xsmp.save_yourself)
8128 SmcSaveYourselfDone(smc_conn, True);
8129 xsmp.save_yourself = True;
8130 xsmp.shutdown = shutdown;
8131
Bram Moolenaar0f873732019-12-05 20:28:46 +01008132 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01008134 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135
8136 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008137 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138
8139# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008140 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008142 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 SmcInteractRequest(smc_conn, SmDialogError,
8144 xsmp_handle_interaction, client_data);
8145 else
8146# endif
8147 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008148 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 SmcSaveYourselfDone(smc_conn, True);
8150 xsmp.save_yourself = False;
8151 }
8152}
8153
8154
8155/*
8156 * Callback to warn us of imminent death.
8157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008159xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160{
8161 xsmp_close();
8162
Bram Moolenaar0f873732019-12-05 20:28:46 +01008163 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 getout_preserve_modified(0);
8165}
8166
8167
8168/*
8169 * Callback to tell us that save-yourself has completed.
8170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008172xsmp_save_complete(
8173 SmcConn smc_conn UNUSED,
8174 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175{
8176 xsmp.save_yourself = False;
8177}
8178
8179
8180/*
8181 * Callback to tell us that an instigated shutdown was cancelled
8182 * (maybe even by us)
8183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008185xsmp_shutdown_cancelled(
8186 SmcConn smc_conn,
8187 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188{
8189 if (xsmp.save_yourself)
8190 SmcSaveYourselfDone(smc_conn, True);
8191 xsmp.save_yourself = False;
8192 xsmp.shutdown = False;
8193}
8194
8195
8196/*
8197 * Callback to tell us that a new ICE connection has been established.
8198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008200xsmp_ice_connection(
8201 IceConn iceConn,
8202 IcePointer clientData UNUSED,
8203 Bool opening,
8204 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008206 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 if (opening)
8208 {
8209 xsmp_icefd = IceConnectionNumber(iceConn);
8210 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8211 }
8212}
8213
8214
Bram Moolenaar0f873732019-12-05 20:28:46 +01008215// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008217xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218{
8219 Bool rep;
8220
8221 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8222 == IceProcessMessagesIOError)
8223 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008224 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008226 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 xsmp_close();
8228 return FAIL;
8229 }
8230 else
8231 return OK;
8232}
8233
8234static int dummy;
8235
Bram Moolenaar0f873732019-12-05 20:28:46 +01008236// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 void
8238xsmp_init(void)
8239{
8240 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 SmcCallbacks smcallbacks;
8242#if 0
8243 SmPropValue smname;
8244 SmProp smnameprop;
8245 SmProp *smprops[1];
8246#endif
8247
8248 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008249 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250
8251 xsmp.save_yourself = xsmp.shutdown = False;
8252
Bram Moolenaar0f873732019-12-05 20:28:46 +01008253 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8255 smcallbacks.save_yourself.client_data = NULL;
8256 smcallbacks.die.callback = xsmp_die;
8257 smcallbacks.die.client_data = NULL;
8258 smcallbacks.save_complete.callback = xsmp_save_complete;
8259 smcallbacks.save_complete.client_data = NULL;
8260 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8261 smcallbacks.shutdown_cancelled.client_data = NULL;
8262
Bram Moolenaar0f873732019-12-05 20:28:46 +01008263 // Set up a watch on ICE connection creations. The "dummy" argument is
8264 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8266 {
8267 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008268 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 return;
8270 }
8271
Bram Moolenaar0f873732019-12-05 20:28:46 +01008272 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 xsmp.smcconn = SmcOpenConnection(
8274 NULL,
8275 NULL,
8276 SmProtoMajor,
8277 SmProtoMinor,
8278 SmcSaveYourselfProcMask | SmcDieProcMask
8279 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8280 &smcallbacks,
8281 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008282 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008283 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 errorstring);
8285 if (xsmp.smcconn == NULL)
8286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008288 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008289 char errorreport[132];
8290
8291 // If the message is too long it might not be NUL terminated. Add
8292 // a NUL at the end to make sure we don't go over the end.
8293 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008294 vim_snprintf(errorreport, sizeof(errorreport),
8295 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008296 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 return;
8299 }
8300 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8301
8302#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008303 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 smname.value = "vim";
8305 smname.length = 3;
8306 smnameprop.name = "SmProgram";
8307 smnameprop.type = "SmARRAY8";
8308 smnameprop.num_vals = 1;
8309 smnameprop.vals = &smname;
8310
8311 smprops[0] = &smnameprop;
8312 SmcSetProperties(xsmp.smcconn, 1, smprops);
8313#endif
8314}
8315
8316
Bram Moolenaar0f873732019-12-05 20:28:46 +01008317// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008319xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008321 if (xsmp_icefd == -1)
8322 return;
8323
8324 SmcCloseConnection(xsmp.smcconn, 0, NULL);
8325 if (xsmp.clientid != NULL)
8326 free(xsmp.clientid);
8327 xsmp.clientid = NULL;
8328 xsmp_icefd = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008330#endif // USE_XSMP
Paul Ollis65745772022-06-05 16:55:54 +01008331
8332#if defined(FEAT_RELTIME) || defined(PROTO)
Bram Moolenaar08210f82023-04-13 19:15:54 +01008333# if defined(PROF_NSEC) || defined(PROTO)
Paul Ollis65745772022-06-05 16:55:54 +01008334/*
8335 * Implement timeout with timer_create() and timer_settime().
8336 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008337static volatile sig_atomic_t timeout_flag = FALSE;
8338static timer_t timer_id;
8339static int timer_created = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008340
8341/*
8342 * Callback for when the timer expires.
8343 */
8344 static void
8345set_flag(union sigval _unused UNUSED)
8346{
8347 timeout_flag = TRUE;
8348}
8349
8350/*
8351 * Stop any active timeout.
8352 */
8353 void
8354stop_timeout(void)
8355{
8356 static struct itimerspec disarm = {{0, 0}, {0, 0}};
8357
8358 if (timer_created)
8359 {
8360 int ret = timer_settime(timer_id, 0, &disarm, NULL);
8361
8362 if (ret < 0)
8363 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8364 }
8365
8366 // Clear the current timeout flag; any previous timeout should be
8367 // considered _not_ triggered.
8368 timeout_flag = FALSE;
8369}
8370
8371/*
8372 * Start the timeout timer.
8373 *
8374 * The return value is a pointer to a flag that is initialised to FALSE. If the
8375 * timeout expires, the flag is set to TRUE. This will only return pointers to
8376 * static memory; i.e. any pointer returned by this function may always be
8377 * safely dereferenced.
8378 *
8379 * This function is not expected to fail, but if it does it will still return a
8380 * valid flag pointer; the flag will remain stuck as FALSE .
8381 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008382 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008383start_timeout(long msec)
8384{
8385 struct itimerspec interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008386 {0, 0}, // Do not repeat.
8387 {msec / 1000, (msec % 1000) * 1000000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008388 int ret;
8389
8390 // This is really the caller's responsibility, but let's make sure the
8391 // previous timer has been stopped.
8392 stop_timeout();
Paul Ollis65745772022-06-05 16:55:54 +01008393
8394 if (!timer_created)
8395 {
8396 struct sigevent action = {0};
8397
8398 action.sigev_notify = SIGEV_THREAD;
8399 action.sigev_notify_function = set_flag;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008400 ret = timer_create(CLOCK_MONOTONIC, &action, &timer_id);
8401 if (ret < 0)
Paul Ollis65745772022-06-05 16:55:54 +01008402 {
8403 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8404 return &timeout_flag;
8405 }
8406 timer_created = TRUE;
8407 }
8408
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00008409# ifdef FEAT_EVAL
Bram Moolenaar616592e2022-06-17 15:17:10 +01008410 ch_log(NULL, "setting timeout timer to %d sec %ld nsec",
8411 (int)interval.it_value.tv_sec, (long)interval.it_value.tv_nsec);
Bram Moolenaar509ce032022-06-20 11:23:01 +01008412# endif
Paul Ollis65745772022-06-05 16:55:54 +01008413 ret = timer_settime(timer_id, 0, &interval, NULL);
8414 if (ret < 0)
8415 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8416
8417 return &timeout_flag;
8418}
8419
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008420/*
8421 * To be used before fork/exec: delete any created timer.
8422 */
8423 void
8424delete_timer(void)
8425{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008426 if (!timer_created)
8427 return;
8428
8429 timer_delete(timer_id);
8430 timer_created = FALSE;
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008431}
8432
Bram Moolenaar08210f82023-04-13 19:15:54 +01008433# else // PROF_NSEC
Paul Ollis65745772022-06-05 16:55:54 +01008434
8435/*
8436 * Implement timeout with setitimer()
8437 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008438static struct sigaction prev_sigaction;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008439static volatile sig_atomic_t timeout_flag = FALSE;
8440static int timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008441static int timer_handler_active = FALSE;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008442static volatile sig_atomic_t alarm_pending = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008443
8444/*
8445 * Handle SIGALRM for a timeout.
8446 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01008447 static void
Paul Ollis65745772022-06-05 16:55:54 +01008448set_flag SIGDEFARG(sigarg)
8449{
8450 if (alarm_pending)
8451 alarm_pending = FALSE;
8452 else
8453 timeout_flag = TRUE;
8454}
8455
8456/*
8457 * Stop any active timeout.
8458 */
8459 void
8460stop_timeout(void)
8461{
8462 static struct itimerval disarm = {{0, 0}, {0, 0}};
8463 int ret;
8464
8465 if (timer_active)
8466 {
8467 timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008468 ret = setitimer(ITIMER_REAL, &disarm, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008469 if (ret < 0)
8470 // Should only get here as a result of coding errors.
8471 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8472 }
8473
8474 if (timer_handler_active)
8475 {
8476 timer_handler_active = FALSE;
8477 ret = sigaction(SIGALRM, &prev_sigaction, NULL);
8478 if (ret < 0)
8479 // Should only get here as a result of coding errors.
8480 semsg(_(e_could_not_reset_handler_for_timeout_str),
8481 strerror(errno));
8482 }
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008483 timeout_flag = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008484}
8485
8486/*
8487 * Start the timeout timer.
8488 *
8489 * The return value is a pointer to a flag that is initialised to FALSE. If the
8490 * timeout expires, the flag is set to TRUE. This will only return pointers to
8491 * static memory; i.e. any pointer returned by this function may always be
8492 * safely dereferenced.
8493 *
8494 * This function is not expected to fail, but if it does it will still return a
8495 * valid flag pointer; the flag will remain stuck as FALSE .
8496 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008497 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008498start_timeout(long msec)
8499{
8500 struct itimerval interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008501 {0, 0}, // Do not repeat.
8502 {msec / 1000, (msec % 1000) * 1000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008503 struct sigaction handle_alarm;
8504 int ret;
8505 sigset_t sigs;
8506 sigset_t saved_sigs;
8507
8508 // This is really the caller's responsibility, but let's make sure the
8509 // previous timer has been stopped.
8510 stop_timeout();
8511
8512 // There is a small chance that SIGALRM is pending and so the handler must
8513 // ignore it on the first call.
8514 alarm_pending = FALSE;
8515 ret = sigemptyset(&sigs);
8516 ret = ret == 0 ? sigaddset(&sigs, SIGALRM) : ret;
8517 ret = ret == 0 ? sigprocmask(SIG_BLOCK, &sigs, &saved_sigs) : ret;
8518 timeout_flag = FALSE;
8519 ret = ret == 0 ? sigpending(&sigs) : ret;
8520 if (ret == 0)
8521 {
8522 alarm_pending = sigismember(&sigs, SIGALRM);
Bram Moolenaar1f89abf2022-06-06 10:07:01 +01008523 ret = sigprocmask(SIG_SETMASK, &saved_sigs, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008524 }
8525 if (unlikely(ret != 0 || alarm_pending < 0))
8526 {
8527 // Just catching coding errors. Write an error message, but carry on.
8528 semsg(_(e_could_not_check_for_pending_sigalrm_str), strerror(errno));
8529 alarm_pending = FALSE;
8530 }
8531
8532 // Set up the alarm handler first.
8533 ret = sigemptyset(&handle_alarm.sa_mask);
8534 handle_alarm.sa_handler = set_flag;
8535 handle_alarm.sa_flags = 0;
8536 ret = ret == 0 ? sigaction(SIGALRM, &handle_alarm, &prev_sigaction) : ret;
8537 if (ret < 0)
8538 {
8539 // Should only get here as a result of coding errors.
8540 semsg(_(e_could_not_set_handler_for_timeout_str), strerror(errno));
8541 return &timeout_flag;
8542 }
8543 timer_handler_active = TRUE;
8544
8545 // Set up the interval timer once the alarm handler is in place.
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008546 ret = setitimer(ITIMER_REAL, &interval, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008547 if (ret < 0)
8548 {
8549 // Should only get here as a result of coding errors.
8550 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8551 stop_timeout();
8552 return &timeout_flag;
8553 }
8554
8555 timer_active = TRUE;
8556 return &timeout_flag;
8557}
Bram Moolenaar08210f82023-04-13 19:15:54 +01008558# endif // PROF_NSEC
Paul Ollis65745772022-06-05 16:55:54 +01008559#endif // FEAT_RELTIME