blob: 31f66b1371c2c7584c6f5ef85979b52b77fd7b6f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * OS/2 port by Paul Slootman
5 * VMS merge by Zoltan Arpadffy
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code.
10 */
11
12/*
13 * os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
14 * Also for OS/2, using the excellent EMX package!!!
Bram Moolenaar041c7102020-05-30 18:14:57 +020015 * Also for Atari MiNT.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016 *
17 * A lot of this file was originally written by Juergen Weigert and later
18 * changed beyond recognition.
19 */
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#include "vim.h"
22
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023#ifdef FEAT_MZSCHEME
24# include "if_mzsch.h"
25#endif
26
Bram Moolenaar0f873732019-12-05 20:28:46 +010027#include "os_unixx.h" // unix includes for os_unix.c only
Bram Moolenaar071d4272004-06-13 20:20:40 +000028
29#ifdef USE_XSMP
30# include <X11/SM/SMlib.h>
31#endif
32
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000033#ifdef HAVE_SELINUX
34# include <selinux/selinux.h>
35static int selinux_enabled = -1;
36#endif
37
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020038#ifdef HAVE_SMACK
39# include <attr/xattr.h>
40# include <linux/xattr.h>
41# ifndef SMACK_LABEL_LEN
42# define SMACK_LABEL_LEN 1024
43# endif
44#endif
45
Bram Moolenaara2442432007-04-26 14:26:37 +000046#ifdef __CYGWIN__
K.Takata972db232022-02-04 10:45:38 +000047# include <cygwin/version.h>
48# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
Bram Moolenaar0f873732019-12-05 20:28:46 +010049 // for cygwin_conv_path()
K.Takata972db232022-02-04 10:45:38 +000050# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
51# define WIN32_LEAN_AND_MEAN
52# include <windows.h>
53# include "winclip.pro"
Bram Moolenaara2442432007-04-26 14:26:37 +000054# endif
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000058
Bram Moolenaar071d4272004-06-13 20:20:40 +000059# include <gpm.h>
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000060
61# ifdef DYNAMIC_GPM
62# define Gpm_Open (*dll_Gpm_Open)
63# define Gpm_Close (*dll_Gpm_Close)
64# define Gpm_GetEvent (*dll_Gpm_GetEvent)
65# define gpm_flag (dll_gpm_flag != NULL ? *dll_gpm_flag : 0)
66# define gpm_fd (dll_gpm_fd != NULL ? *dll_gpm_fd : -1)
67
68static int (*dll_Gpm_Open) (Gpm_Connect *, int);
69static int (*dll_Gpm_Close) (void);
70static int (*dll_Gpm_GetEvent) (Gpm_Event *);
71static int *dll_gpm_flag;
72static int *dll_gpm_fd;
73
74static void *libgpm_hinst;
75# endif
76
Bram Moolenaar0f873732019-12-05 20:28:46 +010077// <linux/keyboard.h> contains defines conflicting with "keymap.h",
78// I just copied relevant defines here. A cleaner solution would be to put gpm
79// code into separate file and include there linux/keyboard.h
80// #include <linux/keyboard.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000081# define KG_SHIFT 0
82# define KG_CTRL 2
83# define KG_ALT 3
84# define KG_ALTGR 1
85# define KG_SHIFTL 4
86# define KG_SHIFTR 5
87# define KG_CTRLL 6
88# define KG_CTRLR 7
89# define KG_CAPSSHIFT 8
90
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010091static void gpm_close(void);
92static int gpm_open(void);
93static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#endif
95
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000096#ifdef FEAT_SYSMOUSE
97# include <sys/consio.h>
98# include <sys/fbio.h>
99
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100100static int sysmouse_open(void);
101static void sysmouse_close(void);
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100102static void sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000103#endif
104
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105/*
106 * end of autoconf section. To be extended...
107 */
108
Bram Moolenaar0f873732019-12-05 20:28:46 +0100109// Are the following #ifdefs still required? And why? Is that for X11?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
111#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
112# ifdef SIGWINCH
113# undef SIGWINCH
114# endif
115# ifdef TIOCGWINSZ
116# undef TIOCGWINSZ
117# endif
118#endif
119
Bram Moolenaar0f873732019-12-05 20:28:46 +0100120#if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121# define SIGWINCH SIGWINDOW
122#endif
123
124#ifdef FEAT_X11
125# include <X11/Xlib.h>
126# include <X11/Xutil.h>
127# include <X11/Xatom.h>
128# ifdef FEAT_XCLIPBOARD
129# include <X11/Intrinsic.h>
130# include <X11/Shell.h>
131# include <X11/StringDefs.h>
132static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void clip_update(void);
134static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135# endif
136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137Window x11_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
140
Bram Moolenaar5c3128e2020-05-11 20:54:42 +0200141static int ignore_sigtstp = FALSE;
142
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200146static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +0200147static int unix_did_set_title = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static char_u *oldicon = NULL;
149static int did_set_icon = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
Bram Moolenaar205b8862011-09-07 15:04:31 +0200153#ifdef HAVE_UNION_WAIT
154typedef union wait waitstatus;
155#else
156typedef int waitstatus;
157#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200158static int WaitForChar(long msec, int *interrupted, int ignore_input);
159static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar041c7102020-05-30 18:14:57 +0200160#ifdef VMS
Bram Moolenaarcda77642016-06-04 13:32:35 +0200161int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200163static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#endif
165
166#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100167static int do_xterm_trace(void);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100168# define XT_TRACE_DELAY 50 // delay for xterm tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
170
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
173#if defined(SIGWINCH)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100174static void sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000176#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100177static void sig_tstp SIGPROTOARG;
Bram Moolenaarabd56da2022-06-23 20:46:27 +0100178// volatile because it is used in signal handler sig_tstp() and
179// sigcont_handler().
dbivolaruab16ad32021-12-29 19:41:47 +0000180static volatile sig_atomic_t in_mch_suspend = FALSE;
181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#if defined(SIGINT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100183static void catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200185#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100186static void catch_sigusr1 SIGPROTOARG;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100189static void catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000191#if defined(SIGALRM) && defined(FEAT_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define SET_SIG_ALARM
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100193static void sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100194// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200195static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196#endif
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100197static void deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100199static void catch_int_signal(void);
200static void set_signals(void);
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100201static void catch_signals(void (*func_deadly)(int), void (*func_other)(int));
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200202#ifdef HAVE_SIGPROCMASK
203# define SIGSET_DECL(set) sigset_t set;
204# define BLOCK_SIGNALS(set) block_signals(set)
205# define UNBLOCK_SIGNALS(set) unblock_signals(set)
206#else
207# define SIGSET_DECL(set)
208# define BLOCK_SIGNALS(set) do { /**/ } while (0)
209# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
210#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100211static int have_wildcard(int, char_u **);
212static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100214static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215
216#ifndef SIG_ERR
ichizok378447f2023-05-11 22:25:42 +0100217# define SIG_ERR ((sighandler_T)-1)
218#endif
219#ifndef SIG_HOLD
220# define SIG_HOLD ((sighandler_T)-2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#endif
222
Bram Moolenaar0f873732019-12-05 20:28:46 +0100223// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200224static volatile sig_atomic_t do_resize = FALSE;
dbivolaruab16ad32021-12-29 19:41:47 +0000225// volatile because it is used in signal handler sig_tstp().
226static volatile sig_atomic_t got_tstp = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227static char_u *extra_shell_arg = NULL;
228static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100229// volatile because it is used in signal handler deathtrap().
230static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
231// volatile because it is used in signal handler deathtrap().
232static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100234#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
235static int dont_check_job_ended = 0;
236#endif
237
Bram Moolenaar26e86442020-05-17 14:06:16 +0200238// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
239static tmode_T mch_cur_tmode = TMODE_COOK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
241#ifdef USE_XSMP
242typedef struct
243{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100244 SmcConn smcconn; // The SM connection ID
245 IceConn iceconn; // The ICE connection ID
246 char *clientid; // The client ID for the current smc session
247 Bool save_yourself; // If we're in the middle of a save_yourself
248 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249} xsmp_config_T;
250
251static xsmp_config_T xsmp;
252#endif
253
254#ifdef SYS_SIGLIST_DECLARED
255/*
256 * I have seen
257 * extern char *_sys_siglist[NSIG];
Yegappan Lakshmananaebc6ef2022-08-27 21:24:26 +0100258 * on Linux, NetBSD and Solaris. It contains a nice list of strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 * that describe the signals. That is nearly what we want here. But
260 * autoconf does only check for sys_siglist (without the underscore), I
261 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100262 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 */
264#endif
265
266static struct signalinfo
267{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100268 int sig; // Signal number, eg. SIGSEGV etc
269 char *name; // Signal name (not char_u!).
270 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271} signal_info[] =
272{
273#ifdef SIGHUP
274 {SIGHUP, "HUP", TRUE},
275#endif
276#ifdef SIGQUIT
277 {SIGQUIT, "QUIT", TRUE},
278#endif
279#ifdef SIGILL
280 {SIGILL, "ILL", TRUE},
281#endif
282#ifdef SIGTRAP
283 {SIGTRAP, "TRAP", TRUE},
284#endif
285#ifdef SIGABRT
286 {SIGABRT, "ABRT", TRUE},
287#endif
288#ifdef SIGEMT
289 {SIGEMT, "EMT", TRUE},
290#endif
291#ifdef SIGFPE
292 {SIGFPE, "FPE", TRUE},
293#endif
294#ifdef SIGBUS
295 {SIGBUS, "BUS", TRUE},
296#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100297#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100298 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 {SIGSEGV, "SEGV", TRUE},
300#endif
301#ifdef SIGSYS
302 {SIGSYS, "SYS", TRUE},
303#endif
304#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100305 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
307#ifdef SIGTERM
308 {SIGTERM, "TERM", TRUE},
309#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100310#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 {SIGVTALRM, "VTALRM", TRUE},
312#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000313#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100314 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
315 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 {SIGPROF, "PROF", TRUE},
317#endif
318#ifdef SIGXCPU
319 {SIGXCPU, "XCPU", TRUE},
320#endif
321#ifdef SIGXFSZ
322 {SIGXFSZ, "XFSZ", TRUE},
323#endif
324#ifdef SIGUSR1
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200325 {SIGUSR1, "USR1", FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000327#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100328 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 {SIGUSR2, "USR2", TRUE},
330#endif
331#ifdef SIGINT
332 {SIGINT, "INT", FALSE},
333#endif
334#ifdef SIGWINCH
335 {SIGWINCH, "WINCH", FALSE},
336#endif
337#ifdef SIGTSTP
338 {SIGTSTP, "TSTP", FALSE},
339#endif
340#ifdef SIGPIPE
341 {SIGPIPE, "PIPE", FALSE},
342#endif
343 {-1, "Unknown!", FALSE}
344};
345
ichizok378447f2023-05-11 22:25:42 +0100346 sighandler_T
347mch_signal(int sig, sighandler_T func)
348{
349#if defined(HAVE_SIGACTION) && defined(HAVE_SIGPROCMASK)
350 // Modern implementation: use sigaction().
351 struct sigaction sa, old;
352 sigset_t curset;
353 int blocked;
354
355 if (sigprocmask(SIG_BLOCK, NULL, &curset) == -1)
356 return SIG_ERR;
357
358 blocked = sigismember(&curset, sig);
359
360 if (func == SIG_HOLD)
361 {
362 if (blocked)
363 return SIG_HOLD;
364
365 sigemptyset(&curset);
366 sigaddset(&curset, sig);
367
368 if (sigaction(sig, NULL, &old) == -1
369 || sigprocmask(SIG_BLOCK, &curset, NULL) == -1)
370 return SIG_ERR;
371 return old.sa_handler;
372 }
373
374 if (blocked)
375 {
376 sigemptyset(&curset);
377 sigaddset(&curset, sig);
378
379 if (sigprocmask(SIG_UNBLOCK, &curset, NULL) == -1)
380 return SIG_ERR;
381 }
382
383 sa.sa_handler = func;
384 sigemptyset(&sa.sa_mask);
385# ifdef SA_RESTART
386 sa.sa_flags = SA_RESTART;
387# else
388 sa.sa_flags = 0;
389# endif
390 if (sigaction(sig, &sa, &old) == -1)
391 return SIG_ERR;
392 return blocked ? SIG_HOLD: old.sa_handler;
393#elif defined(HAVE_SIGSET)
394 // Using sigset() is preferred above signal().
395 return sigset(sig, func);
396#else
397 // Oldest and most compatible solution.
398 return signal(sig, func);
399#endif
400}
401
Bram Moolenaar25724922009-07-14 15:38:41 +0000402 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100403mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000404{
405 if (p_verbose >= 5)
406 {
407 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100408 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000409 verbose_leave();
410 }
ichizok378447f2023-05-11 22:25:42 +0100411#ifdef VMS
Bram Moolenaar25724922009-07-14 15:38:41 +0000412 return chdir(vms_fixfilename(path));
ichizok378447f2023-05-11 22:25:42 +0100413#else
Bram Moolenaar25724922009-07-14 15:38:41 +0000414 return chdir(path);
ichizok378447f2023-05-11 22:25:42 +0100415#endif
Bram Moolenaar25724922009-07-14 15:38:41 +0000416}
417
Bram Moolenaar0f873732019-12-05 20:28:46 +0100418// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100419#if defined(ECHOE) && defined(ICANON) \
420 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
421 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200422# define NEW_TTY_SYSTEM
423#endif
424
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000425/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100426 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100429mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200431 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100432 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100433 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434}
435
436/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100437 * Function passed to inchar_loop() to handle window resizing.
438 * If "check_only" is TRUE: Return whether there was a resize.
439 * If "check_only" is FALSE: Deal with the window resized.
440 */
441 static int
442resize_func(int check_only)
443{
444 if (check_only)
445 return do_resize;
446 while (do_resize)
Bram Moolenaar55f1b822023-06-21 13:42:48 +0100447 {
Bram Moolenaar545c8a52023-06-21 15:51:47 +0100448#ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +0100449 ch_log(NULL, "calling handle_resize() in resize_func()");
Bram Moolenaar545c8a52023-06-21 15:51:47 +0100450#endif
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100451 handle_resize();
Bram Moolenaar55f1b822023-06-21 13:42:48 +0100452 }
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100453 return FALSE;
454}
455
456/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000457 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 * Get a characters from the keyboard.
459 * Return the number of characters that are available.
460 * If wtime == 0 do not wait for characters.
461 * If wtime == n wait a short time for characters.
462 * If wtime == -1 wait forever for characters.
463 */
464 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100465mch_inchar(
466 char_u *buf,
467 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100468 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100469 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100471 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
472 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473}
474
475 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100476handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477{
478 do_resize = FALSE;
479 shell_resized();
480}
481
482/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200483 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 */
485 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100486mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200488 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489}
490
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200491#if defined(FEAT_TERMINAL) || defined(PROTO)
492/*
493 * Check for any pending input or messages.
494 */
495 int
496mch_check_messages(void)
497{
498 return WaitForChar(0L, NULL, TRUE);
499}
500#endif
501
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
503# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000504# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505# endif
506# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
507# include <sys/sysctl.h>
508# endif
509# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
510# include <sys/sysinfo.h>
511# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100512# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100513# include <mach/mach_host.h>
514# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516
517/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000518 * Return total amount of memory available in Kbyte.
519 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100522mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100525 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526
Bram Moolenaar362dc332018-03-05 21:59:37 +0100527# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100528 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100529 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100530 mach_port_t host = mach_host_self();
531 kern_return_t kret;
532# ifdef HOST_VM_INFO64
533 struct vm_statistics64 vm_stat;
534 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535
Bram Moolenaar988615f2018-02-27 17:58:20 +0100536 kret = host_statistics64(host, HOST_VM_INFO64,
537 (host_info64_t)&vm_stat, &count);
538# else
539 struct vm_statistics vm_stat;
540 natural_t count = HOST_VM_INFO_COUNT;
541
542 kret = host_statistics(host, HOST_VM_INFO,
543 (host_info_t)&vm_stat, &count);
544# endif
545 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100546 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100547 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
548 + vm_stat.inactive_count
549# ifdef MAC_OS_X_VERSION_10_9
550 + vm_stat.compressor_page_count
551# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100552 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100553 mach_port_deallocate(mach_task_self(), host);
554 }
555# endif
556
557# ifdef HAVE_SYSCTL
558 if (mem == 0)
559 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100560 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100561 int mib[2];
562 size_t len = sizeof(long_u);
563# ifdef HW_USERMEM64
564 long_u physmem;
565# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100566 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100567 union {
568 int_u u32;
569 long_u u64;
570 } physmem;
571# endif
572
573 mib[0] = CTL_HW;
574# ifdef HW_USERMEM64
575 mib[1] = HW_USERMEM64;
576# else
577 mib[1] = HW_USERMEM;
578# endif
579 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
580 {
581# ifdef HW_USERMEM64
582 mem = (long_u)physmem;
583# else
584 if (len == sizeof(physmem.u64))
585 mem = (long_u)physmem.u64;
586 else
587 mem = (long_u)physmem.u32;
588# endif
589 }
590 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200593# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 if (mem == 0)
595 {
596 struct sysinfo sinfo;
597
Bram Moolenaar0f873732019-12-05 20:28:46 +0100598 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000600 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200601# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100602 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000603 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
604 {
605 sinfo.mem_unit = sinfo.mem_unit >> 1;
606 --shiftright;
607 }
608 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200609# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200611# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200616# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 if (mem == 0)
618 {
619 long pagesize, pagecount;
620
Bram Moolenaar0f873732019-12-05 20:28:46 +0100621 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 pagesize = sysconf(_SC_PAGESIZE);
623 pagecount = sysconf(_SC_PHYS_PAGES);
624 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000625 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100626 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000627 while (shiftright > 0 && (pagesize & 1) == 0)
628 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000629 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000630 --shiftright;
631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200635# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
Bram Moolenaar0f873732019-12-05 20:28:46 +0100637 // Return the minimum of the physical memory and the user limit, because
638 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200639# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 {
641 struct rlimit rlp;
642
643 if (getrlimit(RLIMIT_DATA, &rlp) == 0
644 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200645# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200647# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000648 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000650 {
651 mem = (long_u)rlp.rlim_cur;
652 shiftright = 10;
653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200655# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
657 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000658 return mem >> shiftright;
659 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660}
661#endif
662
Bram Moolenaar0981c872020-08-23 14:28:37 +0200663/*
664 * "flags": MCH_DELAY_IGNOREINPUT - don't read input
665 * MCH_DELAY_SETTMODE - use settmode() even for short delays
666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200668mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200670 tmode_T old_tmode;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200671 int call_settmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000672#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100673 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
Bram Moolenaar0981c872020-08-23 14:28:37 +0200676 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100678 // Go to cooked mode without echo, to allow SIGINT interrupting us
679 // here. But we don't want QUIT to kill us (CTRL-\ used in a
680 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200681 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000682 in_mch_delay = TRUE;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200683 call_settmode = mch_cur_tmode == TMODE_RAW
684 && (msec > 500 || (flags & MCH_DELAY_SETTMODE));
685 if (call_settmode)
686 {
687 old_tmode = mch_cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 settmode(TMODE_SLEEP);
Bram Moolenaar80361a52020-10-05 21:39:25 +0200689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
691 /*
692 * Everybody sleeps in a different way...
693 * Prefer nanosleep(), some versions of usleep() can only sleep up to
694 * one second.
695 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000696#ifdef FEAT_MZSCHEME
697 do
698 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100699 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000700 if (total > p_mzq)
701 msec = p_mzq;
702 else
703 msec = total;
704 total -= msec;
705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#ifdef HAVE_NANOSLEEP
707 {
708 struct timespec ts;
709
710 ts.tv_sec = msec / 1000;
711 ts.tv_nsec = (msec % 1000) * 1000000;
712 (void)nanosleep(&ts, NULL);
713 }
714#else
715# ifdef HAVE_USLEEP
716 while (msec >= 1000)
717 {
718 usleep((unsigned int)(999 * 1000));
719 msec -= 999;
720 }
721 usleep((unsigned int)(msec * 1000));
722# else
723# ifndef HAVE_SELECT
724 poll(NULL, 0, (int)msec);
725# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 {
727 struct timeval tv;
728
729 tv.tv_sec = msec / 1000;
730 tv.tv_usec = (msec % 1000) * 1000;
Bram Moolenaar0981c872020-08-23 14:28:37 +0200731 // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
732 // a patch from Sun to fix this. Reported by Gunnar Pedersen.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 select(0, NULL, NULL, NULL, &tv);
734 }
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100735# endif
736# endif
737#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000738#ifdef FEAT_MZSCHEME
739 }
740 while (total > 0);
741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742
Bram Moolenaar80361a52020-10-05 21:39:25 +0200743 if (call_settmode)
Bram Moolenaar26e86442020-05-17 14:06:16 +0200744 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000745 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 }
747 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200748 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749}
750
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000751#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
753# define HAVE_CHECK_STACK_GROWTH
754/*
755 * Support for checking for an almost-out-of-stack-space situation.
756 */
757
758/*
759 * Return a pointer to an item on the stack. Used to find out if the stack
760 * grows up or down.
761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762static int stack_grows_downwards;
763
764/*
765 * Find out if the stack grows upwards or downwards.
766 * "p" points to a variable on the stack of the caller.
767 */
768 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100769check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
771 int i;
772
773 stack_grows_downwards = (p > (char *)&i);
774}
775#endif
776
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000777#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778static char *stack_limit = NULL;
779
780#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
781# include <pthread.h>
782# include <pthread_np.h>
783#endif
784
785/*
786 * Find out until how var the stack can grow without getting into trouble.
787 * Called when starting up and when switching to the signal stack in
788 * deathtrap().
789 */
790 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100791get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792{
793 struct rlimit rlp;
794 int i;
795 long lim;
796
Bram Moolenaar0f873732019-12-05 20:28:46 +0100797 // Set the stack limit to 15/16 of the allowable size. Skip this when the
798 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 if (getrlimit(RLIMIT_STACK, &rlp) == 0
800 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
801# ifdef RLIM_INFINITY
802 && rlp.rlim_cur != RLIM_INFINITY
803# endif
804 )
805 {
806 lim = (long)rlp.rlim_cur;
807#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
808 {
809 pthread_attr_t attr;
810 size_t size;
811
Bram Moolenaar0f873732019-12-05 20:28:46 +0100812 // On FreeBSD the initial thread always has a fixed stack size, no
813 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 pthread_attr_init(&attr);
815 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
816 {
817 pthread_attr_getstacksize(&attr, &size);
818 if (lim > (long)size)
819 lim = (long)size;
820 }
821 pthread_attr_destroy(&attr);
822 }
823#endif
824 if (stack_grows_downwards)
825 {
826 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
827 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100828 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 stack_limit = (char *)((long)&i / 16L);
830 }
831 else
832 {
833 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
834 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100835 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 }
837 }
838}
839
840/*
841 * Return FAIL when running out of stack space.
842 * "p" must point to any variable local to the caller that's on the stack.
843 */
844 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100845mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000847 if (stack_limit == NULL)
848 return OK;
849
850 if (stack_grows_downwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000852 if (p < stack_limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 return FAIL;
854 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000855 else if (p > stack_limit)
856 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 return OK;
858}
859#endif
860
861#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
862/*
863 * Support for using the signal stack.
864 * This helps when we run out of stack space, which causes a SIGSEGV. The
865 * signal handler then must run on another stack, since the normal stack is
866 * completely full.
867 */
868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100870static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100872static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873# endif
874
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200875/*
876 * Get a size of signal stack.
877 * Preference (if available): sysconf > SIGSTKSZ > guessed size
878 */
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100879static long int get_signal_stack_size(void)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200880{
881# ifdef HAVE_SYSCONF_SIGSTKSZ
882 long int size = -1;
883
884 // return size only if sysconf doesn't return an error
885 if ((size = sysconf(_SC_SIGSTKSZ)) > -1)
886 return size;
887# endif
888
889# ifdef SIGSTKSZ
890 // if sysconf() isn't available or gives error, return SIGSTKSZ
891 // if defined
892 return SIGSTKSZ;
893# endif
894
895 // otherwise guess the size
896 return 8000;
897}
898
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899static char *signal_stack;
900
901 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100902init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000904 if (signal_stack == NULL)
905 return;
906
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908# ifdef HAVE_SS_BASE
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000909 sigstk.ss_base = signal_stack;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000911 sigstk.ss_sp = signal_stack;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000913 sigstk.ss_size = get_signal_stack_size();
914 sigstk.ss_flags = 0;
915 (void)sigaltstack(&sigstk, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000917 sigstk.ss_sp = signal_stack;
918 if (stack_grows_downwards)
919 sigstk.ss_sp += get_signal_stack_size() - 1;
920 sigstk.ss_onstack = 0;
921 (void)sigstack(&sigstk, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923}
924#endif
925
926/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000927 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 * will barf when the second argument to signal() is ``wrong''.
929 * Let me try it with a few tricky defines from my own osdef.h (jw).
930 */
931#if defined(SIGWINCH)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100932 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933sig_winch SIGDEFARG(sigarg)
934{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100935 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100936 mch_signal(SIGWINCH, sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 do_resize = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938}
939#endif
940
dbivolaruab16ad32021-12-29 19:41:47 +0000941#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100942 static void
dbivolaruab16ad32021-12-29 19:41:47 +0000943sig_tstp SIGDEFARG(sigarg)
944{
945 // Second time we get called we actually need to suspend
946 if (in_mch_suspend)
947 {
ichizok378447f2023-05-11 22:25:42 +0100948 mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
dbivolaruab16ad32021-12-29 19:41:47 +0000949 raise(sigarg);
950 }
dbivolaru79a6e252022-01-23 16:41:14 +0000951 else
952 got_tstp = TRUE;
dbivolaruab16ad32021-12-29 19:41:47 +0000953
ichizok5f823d12022-03-13 17:27:38 +0000954#if !defined(__ANDROID__) && !defined(__OpenBSD__) && !defined(__DragonFly__)
955 // This is not required on all systems. On some systems (at least Android,
956 // OpenBSD, and DragonFlyBSD) this breaks suspending with CTRL-Z.
ichizok378447f2023-05-11 22:25:42 +0100957 mch_signal(SIGTSTP, sig_tstp);
xtkobacbef12e2022-02-27 12:31:52 +0000958#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000959}
960#endif
961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962#if defined(SIGINT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100963 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964catch_sigint SIGDEFARG(sigarg)
965{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100966 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100967 mch_signal(SIGINT, catch_sigint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 got_int = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969}
970#endif
971
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200972#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100973 static void
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200974catch_sigusr1 SIGDEFARG(sigarg)
975{
976 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100977 mch_signal(SIGUSR1, catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200978 got_sigusr1 = TRUE;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200979}
980#endif
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100983 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984catch_sigpwr SIGDEFARG(sigarg)
985{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100986 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100987 mch_signal(SIGPWR, catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 /*
989 * I'm not sure we get the SIGPWR signal when the system is really going
990 * down or when the batteries are almost empty. Just preserve the swap
991 * files and don't exit, that can't do any harm.
992 */
993 ml_sync_all(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994}
995#endif
996
997#ifdef SET_SIG_ALARM
998/*
999 * signal function for alarm().
1000 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001001 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002sig_alarm SIGDEFARG(sigarg)
1003{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001004 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 sig_alarm_called = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006}
1007#endif
1008
Bram Moolenaaredce7422019-01-20 18:39:30 +01001009#if (defined(HAVE_SETJMP_H) \
1010 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
1011 || defined(FEAT_LIBCALL))) \
1012 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001013# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +01001014
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001015// argument to SETJMP()
1016static JMP_BUF lc_jump_env;
1017
1018# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001019// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001020// Volatile because it is used in signal handlers.
1021static volatile sig_atomic_t lc_signal;
1022# endif
1023
1024// TRUE when lc_jump_env is valid.
1025// Volatile because it is used in signal handler deathtrap().
zeertzjq9b7d2a92022-08-26 10:33:54 +01001026static volatile sig_atomic_t lc_active = FALSE;
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028/*
1029 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001030 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001032 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 * Usage:
1034 * mch_startjmp();
1035 * if (SETJMP(lc_jump_env) != 0)
1036 * {
1037 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001038 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 * }
1040 * else
1041 * {
1042 * do_the_work;
1043 * mch_endjmp();
1044 * }
1045 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1046 * not return before the saved environment is used.
1047 * Returns OK for normal return, FAIL when the protected code caused a
1048 * problem and LONGJMP() was used.
1049 */
Bram Moolenaar113e1072019-01-20 15:30:40 +01001050 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001051mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001053# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 lc_active = TRUE;
1057}
1058
Bram Moolenaar113e1072019-01-20 15:30:40 +01001059 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001060mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061{
1062 lc_active = FALSE;
1063}
1064
Bram Moolenaar113e1072019-01-20 15:30:40 +01001065 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001066mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
1068# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001069 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1070 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 init_signal_stack();
1072# endif
1073}
1074#endif
1075
1076/*
1077 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001078 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001080 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1081 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001083 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084deathtrap SIGDEFARG(sigarg)
1085{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001086 static int entered = 0; // count the number of times we got here.
1087 // Note: when memory has been corrupted
1088 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089#ifdef SIGHASARG
1090 int i;
1091#endif
1092
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001093#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 /*
1095 * Catch a crash in protected code.
1096 * Restores the environment saved in lc_jump_env, which looks like
1097 * SETJMP() returns 1.
1098 */
1099 if (lc_active)
1100 {
1101# if defined(SIGHASARG)
1102 lc_signal = sigarg;
1103# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001104 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001106 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 }
1108#endif
1109
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001110#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001111# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001112 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1113 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1114 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001115 if (in_mch_delay && sigarg == SIGQUIT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001116 return;
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001117# endif
1118
Bram Moolenaar0f873732019-12-05 20:28:46 +01001119 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1120 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1121 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001122 if (entered == 0
1123 && (0
1124# ifdef SIGHUP
1125 || sigarg == SIGHUP
1126# endif
1127# ifdef SIGQUIT
1128 || sigarg == SIGQUIT
1129# endif
1130# ifdef SIGTERM
1131 || sigarg == SIGTERM
1132# endif
1133# ifdef SIGPWR
1134 || sigarg == SIGPWR
1135# endif
1136# ifdef SIGUSR1
1137 || sigarg == SIGUSR1
1138# endif
1139# ifdef SIGUSR2
1140 || sigarg == SIGUSR2
1141# endif
1142 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001143 && !vim_handle_signal(sigarg))
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001144 return;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001145#endif
1146
Bram Moolenaar0f873732019-12-05 20:28:46 +01001147 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 ++entered;
1149
Bram Moolenaar0f873732019-12-05 20:28:46 +01001150 // Executing autocommands is likely to use more stack space than we have
1151 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001152 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001155 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 set_vim_var_nr(VV_DYING, (long)entered);
1157#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001158 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001160#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001161 // Since we are now using the signal stack, need to reset the stack
1162 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 get_stack_limit();
1164#endif
1165
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001166#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001167 // This is for opening gdb the moment Vim crashes.
1168 // You need to manually adjust the file name and Vim executable name.
1169 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001170 {
1171# define VI_GDB_FILE "/tmp/vimgdb"
1172# define VIM_NAME "/usr/bin/vim"
1173 FILE *fp = fopen(VI_GDB_FILE, "w");
1174 if (fp)
1175 {
1176 fprintf(fp,
1177 "file %s\n"
1178 "attach %d\n"
1179 "set height 1000\n"
1180 "bt full\n"
1181 , VIM_NAME, getpid());
1182 fclose(fp);
1183 system("xterm -e gdb -x "VI_GDB_FILE);
1184 unlink(VI_GDB_FILE);
1185 }
1186 }
1187#endif
1188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001190 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 for (i = 0; signal_info[i].sig != -1; i++)
1192 if (sigarg == signal_info[i].sig)
1193 break;
1194 deadly_signal = sigarg;
1195#endif
1196
Bram Moolenaar0f873732019-12-05 20:28:46 +01001197 full_screen = FALSE; // don't write message to the GUI, it might be
1198 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 /*
1200 * If something goes wrong after entering here, we may get here again.
1201 * When this happens, give a message and try to exit nicely (resetting the
1202 * terminal mode, etc.)
1203 * When this happens twice, just exit, don't even try to give a message,
1204 * stack may be corrupt or something weird.
1205 * When this still happens again (or memory was corrupted in such a way
1206 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1207 */
1208 if (entered >= 3)
1209 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001210 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 may_core_dump();
1212 if (entered >= 4)
1213 _exit(8);
1214 exit(7);
1215 }
1216 if (entered == 2)
1217 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001218 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001219 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 out_flush();
1221 getout(1);
1222 }
1223
Bram Moolenaar0f873732019-12-05 20:28:46 +01001224 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001226 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 signal_info[i].name);
1228#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001229 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001231
Bram Moolenaar0f873732019-12-05 20:28:46 +01001232 // Preserve files and exit. This sets the really_exiting flag to prevent
1233 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001234 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235
Bram Moolenaar0f873732019-12-05 20:28:46 +01001236 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001237
Bram Moolenaar009b2592004-10-24 19:18:58 +00001238#ifdef NBDEBUG
1239 reset_signals();
1240 may_core_dump();
1241 abort();
1242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243}
1244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001246 * Invoked after receiving SIGCONT. We don't know what happened while
1247 * sleeping, deal with part of that.
1248 */
1249 static void
1250after_sigcont(void)
1251{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001252 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1253 // again later.
1254 oldtitle_outdated = TRUE;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001255
Bram Moolenaar2e310482018-08-21 13:09:10 +02001256 settmode(TMODE_RAW);
1257 need_check_timestamps = TRUE;
1258 did_check_timestamps = FALSE;
1259}
1260
1261#if defined(SIGCONT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001262static void sigcont_handler SIGPROTOARG;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001263
1264/*
1265 * With multi-threading, suspending might not work immediately. Catch the
1266 * SIGCONT signal, which will be used as an indication whether the suspending
1267 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001268 *
1269 * On Linux, signal is not always handled immediately either.
1270 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001271 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001272 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001273 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001275static volatile sig_atomic_t sigcont_received;
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001276static void sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277
1278/*
1279 * signal handler for SIGCONT
1280 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001281 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282sigcont_handler SIGDEFARG(sigarg)
1283{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001284 if (in_mch_suspend)
1285 {
1286 sigcont_received = TRUE;
1287 }
1288 else
1289 {
1290 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1291 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1292 // back to a sane mode. We should redraw, but we can't really do that
1293 // in a signal handler, do a redraw later.
1294 after_sigcont();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001295 redraw_later(UPD_CLEAR);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001296 cursor_on_force();
1297 out_flush();
1298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299}
1300#endif
1301
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001302#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001303# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001304static void *clip_star_save = NULL;
1305static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001306# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001307
1308/*
1309 * Called when Vim is going to sleep or execute a shell command.
1310 * We can't respond to requests for the X selections. Lose them, otherwise
1311 * other applications will hang. But first copy the text to cut buffer 0.
1312 */
1313 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001314loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001315{
1316 if (clip_star.owned || clip_plus.owned)
1317 {
1318 x11_export_final_selection();
1319 if (clip_star.owned)
1320 clip_lose_selection(&clip_star);
1321 if (clip_plus.owned)
1322 clip_lose_selection(&clip_plus);
1323 if (x11_display != NULL)
1324 XFlush(x11_display);
1325 }
1326}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001327
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001328# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001329/*
1330 * Save clipboard text to restore later.
1331 */
1332 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001333save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001334{
1335 if (clip_star.owned)
1336 clip_star_save = get_register('*', TRUE);
1337 if (clip_plus.owned)
1338 clip_plus_save = get_register('+', TRUE);
1339}
1340
1341/*
1342 * Restore clipboard text if no one own the X selection.
1343 */
1344 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001345restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001346{
1347 if (clip_star_save != NULL)
1348 {
1349 if (!clip_gen_owner_exists(&clip_star))
1350 put_register('*', clip_star_save);
1351 else
1352 free_register(clip_star_save);
1353 clip_star_save = NULL;
1354 }
1355 if (clip_plus_save != NULL)
1356 {
1357 if (!clip_gen_owner_exists(&clip_plus))
1358 put_register('+', clip_plus_save);
1359 else
1360 free_register(clip_plus_save);
1361 clip_plus_save = NULL;
1362 }
1363}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001364# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001365#endif
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367/*
1368 * If the machine has job control, use it to suspend the program,
1369 * otherwise fake it by starting a new shell.
1370 */
1371 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001372mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001374 if (ignore_sigtstp)
1375 return;
1376
Bram Moolenaar041c7102020-05-30 18:14:57 +02001377#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001378 in_mch_suspend = TRUE;
1379
Bram Moolenaar0f873732019-12-05 20:28:46 +01001380 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001382 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
1384# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001385 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001387# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 sigcont_received = FALSE;
1389# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001390
Bram Moolenaar0f873732019-12-05 20:28:46 +01001391 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001392
1393# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001394 /*
1395 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001396 * immediately, but somehow not all the time, probably because it's handled
1397 * in another thread. Do not call pause() because there would be race
1398 * condition which would hang Vim if signal happened in between the test of
1399 * sigcont_received and the call to pause(). If signal is not yet received,
1400 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1401 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001402 */
1403 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001404 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001405
Bram Moolenaar262735e2009-07-14 10:20:22 +00001406 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001407 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001410 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
Bram Moolenaar2e310482018-08-21 13:09:10 +02001412 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413#else
1414 suspend_shell();
1415#endif
1416}
1417
1418 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001419mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
1421 Columns = 80;
1422 Rows = 24;
1423
1424 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001425
1426#ifdef SIGTSTP
1427 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1428 // that indicates the shell (or program) that launched us does not support
1429 // tty job control and thus we should ignore that signal. If invoked as a
1430 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
ichizok378447f2023-05-11 22:25:42 +01001431 ignore_sigtstp = restricted || SIG_IGN == mch_signal(SIGTSTP, SIG_ERR);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001434
Bram Moolenaar56718732006-03-15 22:53:57 +00001435#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001436 mac_conv_init();
1437#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001438#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1439 win_clip_init();
1440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441}
1442
1443 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001444set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445{
1446#if defined(SIGWINCH)
1447 /*
1448 * WINDOW CHANGE signal is handled with sig_winch().
1449 */
ichizok378447f2023-05-11 22:25:42 +01001450 mch_signal(SIGWINCH, sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451#endif
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001454 // See mch_init() for the conditions under which we ignore SIGTSTP.
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001455 // In the GUI default TSTP processing is OK.
1456 // Checking both gui.in_use and gui.starting because gui.in_use is not set
1457 // at this point (set after menus are displayed), but gui.starting is set.
ichizok378447f2023-05-11 22:25:42 +01001458 mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001459# ifdef FEAT_GUI
1460 : gui.in_use || gui.starting ? SIG_DFL
1461# endif
ichizok378447f2023-05-11 22:25:42 +01001462 : sig_tstp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001464#if defined(SIGCONT)
ichizok378447f2023-05-11 22:25:42 +01001465 mch_signal(SIGCONT, sigcont_handler);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001467#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 /*
1469 * We want to ignore breaking of PIPEs.
1470 */
ichizok378447f2023-05-11 22:25:42 +01001471 mch_signal(SIGPIPE, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472#endif
1473
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001475 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476#endif
1477
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001478#ifdef SIGUSR1
1479 /*
1480 * Call user's handler on SIGUSR1
1481 */
ichizok378447f2023-05-11 22:25:42 +01001482 mch_signal(SIGUSR1, catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001483#endif
1484
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 /*
1486 * Ignore alarm signals (Perl's alarm() generates it).
1487 */
1488#ifdef SIGALRM
ichizok378447f2023-05-11 22:25:42 +01001489 mch_signal(SIGALRM, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490#endif
1491
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001492#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 /*
1494 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1495 * work will be lost.
1496 */
ichizok378447f2023-05-11 22:25:42 +01001497 mch_signal(SIGPWR, catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498#endif
1499
1500 /*
1501 * Arrange for other signals to gracefully shutdown Vim.
1502 */
1503 catch_signals(deathtrap, SIG_ERR);
1504
1505#if defined(FEAT_GUI) && defined(SIGHUP)
1506 /*
1507 * When the GUI is running, ignore the hangup signal.
1508 */
1509 if (gui.in_use)
ichizok378447f2023-05-11 22:25:42 +01001510 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511#endif
1512}
1513
Bram Moolenaardf177f62005-02-22 08:39:57 +00001514#if defined(SIGINT) || defined(PROTO)
1515/*
1516 * Catch CTRL-C (only works while in Cooked mode).
1517 */
1518 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001519catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001520{
ichizok378447f2023-05-11 22:25:42 +01001521 mch_signal(SIGINT, catch_sigint);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001522}
1523#endif
1524
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001526reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527{
1528 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001529#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001530 // SIGCONT isn't in the list, because its default action is ignore
ichizok378447f2023-05-11 22:25:42 +01001531 mch_signal(SIGCONT, SIG_DFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532#endif
1533}
1534
1535 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001536catch_signals(
Michael Jarvisbe9624e2023-04-19 20:28:48 +01001537 void (*func_deadly)(int),
1538 void (*func_other)(int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
1540 int i;
1541
1542 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if (signal_info[i].deadly)
1545 {
1546#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1547 struct sigaction sa;
1548
Bram Moolenaar0f873732019-12-05 20:28:46 +01001549 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 sa.sa_handler = func_deadly;
1551 sigemptyset(&sa.sa_mask);
1552# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001553 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1554 // thread handling in combination with using the alternate stack:
1555 // pthread library functions try to use the stack pointer to
1556 // identify the current thread, causing a SEGV signal, which
1557 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 sa.sa_flags = 0;
1559# else
1560 sa.sa_flags = SA_ONSTACK;
1561# endif
1562 sigaction(signal_info[i].sig, &sa, NULL);
1563#else
1564# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1565 struct sigvec sv;
1566
Bram Moolenaar0f873732019-12-05 20:28:46 +01001567 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 sv.sv_handler = func_deadly;
1569 sv.sv_mask = 0;
1570 sv.sv_flags = SV_ONSTACK;
1571 sigvec(signal_info[i].sig, &sv, NULL);
1572# else
ichizok378447f2023-05-11 22:25:42 +01001573 mch_signal(signal_info[i].sig, func_deadly);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574# endif
1575#endif
1576 }
1577 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001578 {
1579 // Deal with non-deadly signals.
1580#ifdef SIGTSTP
ichizok378447f2023-05-11 22:25:42 +01001581 mch_signal(signal_info[i].sig,
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001582 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1583 ? SIG_IGN : func_other);
1584#else
ichizok378447f2023-05-11 22:25:42 +01001585 mch_signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001586#endif
1587 }
1588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589}
1590
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001591#ifdef HAVE_SIGPROCMASK
1592 static void
1593block_signals(sigset_t *set)
1594{
1595 sigset_t newset;
1596 int i;
1597
1598 sigemptyset(&newset);
1599
1600 for (i = 0; signal_info[i].sig != -1; i++)
1601 sigaddset(&newset, signal_info[i].sig);
1602
Bram Moolenaar2e310482018-08-21 13:09:10 +02001603# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001604 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001605 sigaddset(&newset, SIGCONT);
1606# endif
1607
1608 sigprocmask(SIG_BLOCK, &newset, set);
1609}
1610
1611 static void
1612unblock_signals(sigset_t *set)
1613{
1614 sigprocmask(SIG_SETMASK, set, NULL);
1615}
1616#endif
1617
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001619 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001620 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1621 * return TRUE
1622 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1623 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001624 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001625 * Returns TRUE when Vim should exit.
1626 */
1627 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001628vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001629{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001630 static int got_signal = 0;
1631 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001632
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001633 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001634 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001635 case SIGNAL_BLOCK: blocked = TRUE;
1636 break;
1637
1638 case SIGNAL_UNBLOCK: blocked = FALSE;
1639 if (got_signal != 0)
1640 {
1641 kill(getpid(), got_signal);
1642 got_signal = 0;
1643 }
1644 break;
1645
1646 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001647 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001648 got_signal = sig;
1649#ifdef SIGPWR
1650 if (sig != SIGPWR)
1651#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001652 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001653 break;
1654 }
1655 return FALSE;
1656}
1657
1658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 * Check_win checks whether we have an interactive stdout.
1660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001662mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if (isatty(1))
1665 return OK;
1666 return FAIL;
1667}
1668
1669/*
1670 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1671 */
1672 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001673mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674{
1675 if (isatty(read_cmd_fd))
1676 return TRUE;
1677 return FALSE;
1678}
1679
1680#ifdef FEAT_X11
1681
Bram Moolenaar651fca82021-11-29 20:39:38 +00001682# if defined(ELAPSED_TIMEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684/*
1685 * Give a message about the elapsed time for opening the X window.
1686 */
1687 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001688xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001690 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691}
1692# endif
1693#endif
1694
Bram Moolenaar651fca82021-11-29 20:39:38 +00001695#if defined(FEAT_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696/*
1697 * A few functions shared by X11 title and clipboard code.
1698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
1700static int got_x_error = FALSE;
1701
1702/*
1703 * X Error handler, otherwise X just exits! (very rude) -- webb
1704 */
1705 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001706x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001708 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 STRCAT(IObuff, _("\nVim: Got X error\n"));
1710
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001711 // In the GUI we cannot print a message and continue, because no X calls
1712 // are allowed here (causes my system to hang). Silently continuing seems
1713 // like the best alternative. Do preserve files, in case we crash.
1714 ml_sync_all(FALSE, FALSE);
1715
1716#ifdef FEAT_GUI
1717 if (!gui.in_use)
1718#endif
1719 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar0f873732019-12-05 20:28:46 +01001721 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722}
1723
1724/*
1725 * Another X Error handler, just used to check for errors.
1726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001728x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
1730 got_x_error = TRUE;
1731 return 0;
1732}
1733
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001734/*
1735 * Return TRUE when connection to the X server is desired.
1736 */
1737 static int
1738x_connect_to_server(void)
1739{
1740 // No point in connecting if we are exiting or dying.
1741 if (exiting || v_dying)
1742 return FALSE;
1743
1744#if defined(FEAT_CLIENTSERVER)
1745 if (x_force_connect)
1746 return TRUE;
1747#endif
1748 if (x_no_connect)
1749 return FALSE;
1750
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001751 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001752 if (clip_exclude_prog != NULL)
1753 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001754 // Just in case we get called recursively, return FALSE. This could
1755 // happen if vpeekc() is used while executing the prog and it causes a
1756 // related callback to be invoked.
1757 if (regprog_in_use(clip_exclude_prog))
1758 return FALSE;
1759
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001760 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1761 return FALSE;
1762 }
1763 return TRUE;
1764}
1765
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001767# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768/*
1769 * An X IO Error handler, used to catch error while opening the display.
1770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001772x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001774 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001776# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001777 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779}
1780# endif
1781
1782/*
1783 * An X IO Error handler, used to catch terminal errors.
1784 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001785static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001788x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001791 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 x11_window = 0;
1793 x11_display = NULL;
1794 xterm_Shell = (Widget)0;
1795
Bram Moolenaar0f873732019-12-05 20:28:46 +01001796 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001798# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001799 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001802
1803/*
1804 * If the X11 connection was lost try to restore it.
1805 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001806 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001807 */
1808 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001809may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001810{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001811 // No point in restoring the connecting if we are exiting or dying.
1812 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001813 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001814 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001815
1816# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001817 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001818 if (app_context != (XtAppContext)NULL)
1819 {
1820 XtDestroyApplicationContext(app_context);
1821 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001822 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001823 }
1824# endif
1825
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001826 setup_term_clip();
1827 get_x11_title(FALSE);
1828 }
1829}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001830
1831 void
1832ex_xrestore(exarg_T *eap)
1833{
1834 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1835 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001836 if (xterm_display_allocated)
1837 vim_free(xterm_display);
1838 xterm_display = (char *)vim_strsave(eap->arg);
1839 xterm_display_allocated = TRUE;
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001840 }
1841 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001842 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001843
1844 clear_xterm_clip();
1845 x11_window = 0;
1846 xterm_dpy_retry_count = 5; // Try reconnecting five times
1847 may_restore_clipboard();
1848}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849#endif
1850
1851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 * Test if "dpy" and x11_window are valid by getting the window title.
1853 * I don't actually want it yet, so there may be a simpler call to use, but
1854 * this will cause the error handler x_error_check() to be called if anything
1855 * is wrong, such as the window pointer being invalid (as can happen when the
1856 * user changes his DISPLAY, but not his WINDOWID) -- webb
1857 */
1858 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001859test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860{
1861 int (*old_handler)();
1862 XTextProperty text_prop;
1863
1864 old_handler = XSetErrorHandler(x_error_check);
1865 got_x_error = FALSE;
1866 if (XGetWMName(dpy, x11_window, &text_prop))
1867 XFree((void *)text_prop.value);
1868 XSync(dpy, False);
1869 (void)XSetErrorHandler(old_handler);
1870
1871 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001872 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
1874 return (got_x_error ? FAIL : OK);
1875}
1876#endif
1877
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
1879#ifdef FEAT_X11
1880
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001881static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
1883/*
1884 * try to get x11 window and display
1885 *
1886 * return FAIL for failure, OK otherwise
1887 */
1888 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001889get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890{
1891 char *winid;
1892 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001893#define XD_NONE 0 // x11_display not set here
1894#define XD_HERE 1 // x11_display opened here
1895#define XD_GUI 2 // x11_display used from gui.dpy
1896#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 static int x11_display_from = XD_NONE;
1898 static int did_set_error_handler = FALSE;
1899
1900 if (!did_set_error_handler)
1901 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001902 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 (void)XSetErrorHandler(x_error_handler);
1904 did_set_error_handler = TRUE;
1905 }
1906
Bram Moolenaar9372a112005-12-06 19:59:18 +00001907#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (gui.in_use)
1909 {
1910 /*
1911 * If the X11 display was opened here before, for the window where Vim
1912 * was started, close that one now to avoid a memory leak.
1913 */
1914 if (x11_display_from == XD_HERE && x11_display != NULL)
1915 {
1916 XCloseDisplay(x11_display);
1917 x11_display_from = XD_NONE;
1918 }
1919 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1920 {
1921 x11_display_from = XD_GUI;
1922 return OK;
1923 }
1924 x11_display = NULL;
1925 return FAIL;
1926 }
1927 else if (x11_display_from == XD_GUI)
1928 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001929 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 x11_window = 0;
1931 x11_display = NULL;
1932 x11_display_from = XD_NONE;
1933 }
1934#endif
1935
Bram Moolenaar0f873732019-12-05 20:28:46 +01001936 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 if (!x_connect_to_server())
1938 return FAIL;
1939
1940 /*
1941 * If WINDOWID not set, should try another method to find out
1942 * what the current window number is. The only code I know for
1943 * this is very complicated.
1944 * We assume that zero is invalid for WINDOWID.
1945 */
1946 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1947 x11_window = (Window)atol(winid);
1948
1949#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001950 if (xterm_dpy == x11_display)
1951 // x11_display may have been set to xterm_dpy elsewhere
1952 x11_display_from = XD_XTERM;
1953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (xterm_dpy != NULL && x11_window != 0)
1955 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001956 // We may have checked it already, but Gnome terminal can move us to
1957 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001958 if (x11_display_from != XD_XTERM)
1959 {
1960 /*
1961 * If the X11 display was opened here before, for the window where
1962 * Vim was started, close that one now to avoid a memory leak.
1963 */
1964 if (x11_display_from == XD_HERE && x11_display != NULL)
1965 XCloseDisplay(x11_display);
1966 x11_display = xterm_dpy;
1967 x11_display_from = XD_XTERM;
1968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (test_x11_window(x11_display) == FAIL)
1970 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001971 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 x11_window = 0;
1973 x11_display = NULL;
1974 x11_display_from = XD_NONE;
1975 return FAIL;
1976 }
1977 return OK;
1978 }
1979#endif
1980
1981 if (x11_window == 0 || x11_display == NULL)
1982 result = -1;
1983
Bram Moolenaar0f873732019-12-05 20:28:46 +01001984 if (result != -1) // Have already been here and set this
1985 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986
1987 if (x11_window != 0 && x11_display == NULL)
1988 {
1989#ifdef SET_SIG_ALARM
ichizok378447f2023-05-11 22:25:42 +01001990 sighandler_T sig_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001992#ifdef ELAPSED_FUNC
1993 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
1995 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001996 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997#endif
1998
1999#ifdef SET_SIG_ALARM
2000 /*
2001 * Opening the Display may hang if the DISPLAY setting is wrong, or
2002 * the network connection is bad. Set an alarm timer to get out.
2003 */
2004 sig_alarm_called = FALSE;
ichizok378447f2023-05-11 22:25:42 +01002005 sig_save = mch_signal(SIGALRM, sig_alarm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 alarm(2);
2007#endif
2008 x11_display = XOpenDisplay(NULL);
2009
2010#ifdef SET_SIG_ALARM
2011 alarm(0);
ichizok378447f2023-05-11 22:25:42 +01002012 mch_signal(SIGALRM, sig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01002014 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015#endif
2016 if (x11_display != NULL)
2017 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002018# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00002020 {
2021 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002022 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00002023 verbose_leave();
2024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025# endif
2026 if (test_x11_window(x11_display) == FAIL)
2027 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002028 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 x11_window = 0;
2030 XCloseDisplay(x11_display);
2031 x11_display = NULL;
2032 }
2033 else
2034 x11_display_from = XD_HERE;
2035 }
2036 }
2037 if (x11_window == 0 || x11_display == NULL)
2038 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02002039
2040# ifdef FEAT_EVAL
2041 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
2042# endif
2043
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 return (result = OK);
2045}
2046
2047/*
2048 * Determine original x11 Window Title
2049 */
2050 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002051get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052{
Bram Moolenaar47136d72004-10-12 20:02:24 +00002053 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054}
2055
2056/*
2057 * Determine original x11 Window icon
2058 */
2059 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002060get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061{
2062 int retval = FALSE;
2063
2064 retval = get_x11_thing(FALSE, test_only);
2065
Bram Moolenaar0f873732019-12-05 20:28:46 +01002066 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 if (oldicon == NULL && !test_only)
2068 {
2069 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002070 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002072 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 }
2074
2075 return retval;
2076}
2077
2078 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002079get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002080 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01002081 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
2083 XTextProperty text_prop;
2084 int retval = FALSE;
2085 Status status;
2086
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002087 if (get_x11_windis() != OK)
2088 return FALSE;
2089
2090 // Get window/icon name if any
2091 if (get_title)
2092 status = XGetWMName(x11_display, x11_window, &text_prop);
2093 else
2094 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2095
2096 /*
2097 * If terminal is xterm, then x11_window may be a child window of the
2098 * outer xterm window that actually contains the window/icon name, so
2099 * keep traversing up the tree until a window with a title/icon is
2100 * found.
2101 */
2102 // Previously this was only done for xterm and alike. I don't see a
2103 // reason why it would fail for other terminal emulators.
2104 // if (term_is_xterm)
2105 Window root;
2106 Window parent;
2107 Window win = x11_window;
2108 Window *children;
2109 unsigned int num_children;
2110
2111 while (!status || text_prop.value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002113 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2114 &num_children))
2115 break;
2116 if (children)
2117 XFree((void *)children);
2118 if (parent == root || parent == 0)
2119 break;
2120
2121 win = parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002123 status = XGetWMName(x11_display, win, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002125 status = XGetWMIconName(x11_display, win, &text_prop);
2126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002128 if (status && text_prop.value != NULL)
2129 {
2130 retval = TRUE;
2131 if (!test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002133 if (get_title)
2134 vim_free(oldtitle);
2135 else
2136 vim_free(oldicon);
2137 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002140 oldtitle = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002142 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002144 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002146 char **cl;
2147 Status transform_status;
2148 int n = 0;
2149
2150 transform_status = XmbTextPropertyToTextList(x11_display,
2151 &text_prop,
2152 &cl, &n);
2153 if (transform_status >= Success && n > 0 && cl[0])
2154 {
2155 if (get_title)
2156 oldtitle = vim_strsave((char_u *) cl[0]);
2157 else
2158 oldicon = vim_strsave((char_u *) cl[0]);
2159 XFreeStringList(cl);
2160 }
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002161 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (get_title)
2164 oldtitle = vim_strsave((char_u *)text_prop.value);
2165 else
2166 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002170 XFree((void *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
2172 return retval;
2173}
2174
Bram Moolenaar0f873732019-12-05 20:28:46 +01002175// Xutf8 functions are not available on older systems. Note that on some
2176// systems X_HAVE_UTF8_STRING may be defined in a header file but
2177// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2178// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002179#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002180# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181# define USE_UTF8_STRING
2182# endif
2183#endif
2184
2185/*
2186 * Set x11 Window Title
2187 *
2188 * get_x11_windis() must be called before this and have returned OK
2189 */
2190 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002191set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002193 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2194 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2195 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196#ifdef USE_UTF8_STRING
2197 if (enc_utf8)
2198 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2199 NULL, NULL, 0, NULL, NULL, NULL);
2200 else
2201#endif
2202 {
2203#if XtSpecificationRelease >= 4
2204# ifdef FEAT_XFONTSET
2205 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2206 NULL, NULL, 0, NULL, NULL, NULL);
2207# else
2208 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002209 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210
Bram Moolenaar0f873732019-12-05 20:28:46 +01002211 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002212 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 XSetWMProperties(x11_display, x11_window, &text_prop,
2214 NULL, NULL, 0, NULL, NULL, NULL);
2215# endif
2216#else
2217 XStoreName(x11_display, x11_window, (char *)title);
2218#endif
2219 }
2220 XFlush(x11_display);
2221}
2222
2223/*
2224 * Set x11 Window icon
2225 *
2226 * get_x11_windis() must be called before this and have returned OK
2227 */
2228 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002229set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002231 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#ifdef USE_UTF8_STRING
2233 if (enc_utf8)
2234 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2235 NULL, 0, NULL, NULL, NULL);
2236 else
2237#endif
2238 {
2239#if XtSpecificationRelease >= 4
2240# ifdef FEAT_XFONTSET
2241 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2242 NULL, 0, NULL, NULL, NULL);
2243# else
2244 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002245 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002247 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2249 NULL, 0, NULL, NULL, NULL);
2250# endif
2251#else
2252 XSetIconName(x11_display, x11_window, (char *)icon);
2253#endif
2254 }
2255 XFlush(x11_display);
2256}
2257
Bram Moolenaar0f873732019-12-05 20:28:46 +01002258#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002261get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262{
2263 return FALSE;
2264}
2265
2266 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002267get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
2269 if (!test_only)
2270 {
2271 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002272 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002274 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276 return FALSE;
2277}
2278
Bram Moolenaar0f873732019-12-05 20:28:46 +01002279#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280
2281 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002282mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283{
2284 return get_x11_title(TRUE);
2285}
2286
2287 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002288mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
2290 return get_x11_icon(TRUE);
2291}
2292
2293/*
2294 * Set the window title and icon.
2295 */
2296 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002297mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
2299 int type = 0;
2300 static int recursive = 0;
2301
Bram Moolenaar0f873732019-12-05 20:28:46 +01002302 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002304 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 return;
2306
Bram Moolenaar0f873732019-12-05 20:28:46 +01002307 // When one of the X11 functions causes a deadly signal, we get here again
2308 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (recursive)
2310 return;
2311 ++recursive;
2312
2313 /*
2314 * if the window ID and the display is known, we may use X11 calls
2315 */
2316#ifdef FEAT_X11
2317 if (get_x11_windis() == OK)
2318 type = 1;
2319#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002320# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002321 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (gui.in_use)
2323 type = 1;
2324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325#endif
2326
2327 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002328 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 * than x11 calls, because the x11 calls don't always work
2330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 if ((type || *T_TS != NUL) && title != NULL)
2332 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002333 if (oldtitle_outdated)
2334 {
2335 oldtitle_outdated = FALSE;
2336 VIM_CLEAR(oldtitle);
2337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 if (oldtitle == NULL
2339#ifdef FEAT_GUI
2340 && !gui.in_use
2341#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002342 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 (void)get_x11_title(FALSE);
2344
Bram Moolenaar0f873732019-12-05 20:28:46 +01002345 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 term_settitle(title);
2347#ifdef FEAT_X11
2348 else
2349# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002350 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002352 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002354#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002355 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 else
2357 gui_mch_settitle(title, icon);
2358#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002359 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 }
2361
2362 if ((type || *T_CIS != NUL) && icon != NULL)
2363 {
2364 if (oldicon == NULL
2365#ifdef FEAT_GUI
2366 && !gui.in_use
2367#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002368 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 get_x11_icon(FALSE);
2370
2371 if (*T_CIS != NUL)
2372 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002373 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002375 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 out_flush();
2377 }
2378#ifdef FEAT_X11
2379 else
2380# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002381 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002383 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384#endif
2385 did_set_icon = TRUE;
2386 }
2387 --recursive;
2388}
2389
2390/*
2391 * Restore the window/icon title.
2392 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002393 * SAVE_RESTORE_TITLE only restore title
2394 * SAVE_RESTORE_ICON only restore icon
2395 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
2397 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002398mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
Bram Moolenaardac13472019-09-16 21:06:21 +02002400 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002401
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002402 // Only restore the title or icon when it has been set.
2403 // When using "oldtitle" make a copy, it might be freed halfway.
2404 char_u *title = ((which & SAVE_RESTORE_TITLE) && unix_did_set_title)
2405 ? (oldtitle ? oldtitle : p_titleold) : NULL;
2406 char_u *tofree = NULL;
2407 if (title == oldtitle && oldtitle != NULL)
2408 {
2409 tofree = vim_strsave(title);
2410 if (tofree != NULL)
2411 title = tofree;
2412 }
2413 mch_settitle(title,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002414 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002415 vim_free(tofree);
Bram Moolenaar40385db2018-08-07 22:31:44 +02002416
Bram Moolenaare5c83282019-05-03 23:15:37 +02002417 if (do_push_pop)
2418 {
2419 // pop and push from/to the stack
2420 term_pop_title(which);
2421 term_push_title(which);
2422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423}
2424
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426/*
2427 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002428 * This matches "xterm.*", thus "xterm-256color", "xterm-kitty", etc.
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002429 * Do not consider "xterm-kitty" an xterm, it is not fully xterm compatible,
2430 * using the "xterm-kitty" terminfo entry should work better.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002431 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 */
2433 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002434vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435{
2436 if (name == NULL)
2437 return FALSE;
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002438 return ((STRNICMP(name, "xterm", 5) == 0
2439 && STRNICMP(name, "xterm-kitty", 11) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 || STRNICMP(name, "nxterm", 6) == 0
2441 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002442 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002444 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 || STRCMP(name, "builtin_xterm") == 0);
2446}
2447
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002448#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2449/*
2450 * Return TRUE if "name" appears to be that of a terminal
2451 * known to support the xterm-style mouse protocol.
2452 * Relies on term_is_xterm having been set to its correct value.
2453 */
2454 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002455use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002456{
2457 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002458 && (term_is_xterm
2459 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002460 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002461 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002462 || STRICMP(name, "st") == 0
2463 || STRNICMP(name, "st-", 3) == 0
2464 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002465}
2466#endif
2467
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468/*
2469 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2470 * Return 1 for "xterm".
2471 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002472 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002473 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 */
2475 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002476use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002478 if (ttym_flags == TTYM_SGR)
2479 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002480 if (ttym_flags == TTYM_URXVT)
2481 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 if (ttym_flags == TTYM_XTERM2)
2483 return 2;
2484 if (ttym_flags == TTYM_XTERM)
2485 return 1;
2486 return 0;
2487}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002489/*
2490 * Return TRUE if "name" is an iris-ansi terminal name.
2491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002493vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494{
2495 if (name == NULL)
2496 return FALSE;
2497 return (STRNICMP(name, "iris-ansi", 9) == 0
2498 || STRCMP(name, "builtin_iris-ansi") == 0);
2499}
2500
Dominique Pellee764d1b2023-03-12 21:20:59 +00002501#if defined(VMS) || defined(PROTO)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002502/*
2503 * Return TRUE if "name" is a vt300-like terminal name.
2504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002506vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
2508 if (name == NULL)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002509 return FALSE;
2510 // Actually all ANSI compatible terminals should be here.
2511 // Catch at least VT1xx - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002512 return ((STRNICMP(name, "vt", 2) == 0
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002513 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 || STRCMP(name, "builtin_vt320") == 0);
2515}
Dominique Pellee764d1b2023-03-12 21:20:59 +00002516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
2518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 * Insert user name in s[len].
2520 * Return OK if a name found.
2521 */
2522 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002523mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002526 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 return OK;
2528#else
2529 return mch_get_uname(getuid(), s, len);
2530#endif
2531}
2532
2533/*
2534 * Insert user name for "uid" in s[len].
2535 * Return OK if a name found.
2536 */
2537 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002538mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
2540#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2541 struct passwd *pw;
2542
2543 if ((pw = getpwuid(uid)) != NULL
2544 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2545 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002546 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 return OK;
2548 }
2549#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002550 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2551 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552}
2553
2554/*
2555 * Insert host name is s[len].
2556 */
2557
2558#ifdef HAVE_SYS_UTSNAME_H
2559 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002560mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561{
2562 struct utsname vutsname;
2563
2564 if (uname(&vutsname) < 0)
2565 *s = NUL;
2566 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002567 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002569#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
2571# ifdef HAVE_SYS_SYSTEMINFO_H
2572# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2573# endif
2574
2575 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002576mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
2578# ifdef VAXC
2579 vaxc$gethostname((char *)s, len);
2580# else
2581 gethostname((char *)s, len);
2582# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002583 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002585#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
2587/*
2588 * return process ID
2589 */
2590 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002591mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
2593 return (long)getpid();
2594}
2595
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002596/*
2597 * return TRUE if process "pid" is still running
2598 */
2599 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002600mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002601{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002602 // If there is no error the process must be running.
2603 if (kill(pid, 0) == 0)
2604 return TRUE;
2605#ifdef ESRCH
2606 // If the error is ESRCH then the process is not running.
2607 if (errno == ESRCH)
2608 return FALSE;
2609#endif
2610 // If the process is running and owned by another user we get EPERM. With
2611 // other errors the process might be running, assuming it is then.
2612 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002613}
2614
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002617strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
2619 extern int sys_nerr;
2620 extern char *sys_errlist[];
2621 static char er[20];
2622
2623 if (err > 0 && err < sys_nerr)
2624 return (sys_errlist[err]);
2625 sprintf(er, "Error %d", err);
2626 return er;
2627}
2628#endif
2629
2630/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002631 * Get name of current directory into buffer "buf" of length "len" bytes.
2632 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 * Return OK for success, FAIL for failure.
2634 */
2635 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002636mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
2638#if defined(USE_GETCWD)
2639 if (getcwd((char *)buf, len) == NULL)
2640 {
2641 STRCPY(buf, strerror(errno));
2642 return FAIL;
2643 }
2644 return OK;
2645#else
2646 return (getwd((char *)buf) != NULL ? OK : FAIL);
2647#endif
2648}
2649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002651 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 *
2653 * return FAIL for failure, OK for success
2654 */
2655 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002656mch_FullName(
2657 char_u *fname,
2658 char_u *buf,
2659 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002660 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
2662 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002663#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002665 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 char_u olddir[MAXPATHL];
2668 char_u *p;
2669 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002670#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002671 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2672 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002673#endif
2674
Bram Moolenaar38323e42007-03-06 19:22:53 +00002675#ifdef VMS
2676 fname = vms_fixfilename(fname);
2677#endif
2678
Bram Moolenaara2442432007-04-26 14:26:37 +00002679#ifdef __CYGWIN__
2680 /*
2681 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2682 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002683# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002684 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2685 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002686 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2687 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002688# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002689 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002690# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002691 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002692#endif
2693
Bram Moolenaar0f873732019-12-05 20:28:46 +01002694 // Expand it if forced or not an absolute path.
2695 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002696 if ((force || !mch_isFullName(fname))
2697 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 {
2699 /*
2700 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002701 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 * This will get the correct path name with "../" things.
2703 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002704 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002706 if (STRCMP(p, "/..") == 0)
2707 // for "/path/dir/.." include the "/.."
2708 p += 3;
2709
Bram Moolenaar38323e42007-03-06 19:22:53 +00002710#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 /*
2712 * Use fchdir() if possible, it's said to be faster and more
2713 * reliable. But on SunOS 4 it might not work. Check this by
2714 * doing a fchdir() right now.
2715 */
2716 if (!dont_fchdir)
2717 {
2718 fd = open(".", O_RDONLY | O_EXTRA, 0);
2719 if (fd >= 0 && fchdir(fd) < 0)
2720 {
2721 close(fd);
2722 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002723 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
Bram Moolenaar0f873732019-12-05 20:28:46 +01002728 // Only change directory when we are sure we can return to where
2729 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002731#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 (mch_dirname(olddir, MAXPATHL) == FAIL
2735 || mch_chdir((char *)olddir) != 0))
2736 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002737 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 retval = FAIL;
2739 }
2740 else
2741 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002742 // The directory is copied into buf[], to be able to remove
2743 // the file name without changing it (could be a string in
2744 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if (p - fname >= len)
2746 retval = FAIL;
2747 else
2748 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002749 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002751 {
2752 // Path does not exist (yet). For a full path fail,
2753 // will use the path as-is. For a relative path use
2754 // the current directory and append the file name.
2755 if (mch_isFullName(fname))
2756 retval = FAIL;
2757 else
2758 p = NULL;
2759 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002760 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002762 else
2763 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 *buf = NUL;
2765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767 }
2768 if (mch_dirname(buf, len) == FAIL)
2769 {
2770 retval = FAIL;
2771 *buf = NUL;
2772 }
2773 if (p != NULL)
2774 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002775#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 if (fd >= 0)
2777 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002778 if (p_verbose >= 5)
2779 {
2780 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002781 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002782 verbose_leave();
2783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
2786 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 l = mch_chdir((char *)olddir);
2789 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002790 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
itchyny051a40c2021-10-20 10:00:05 +01002792#ifdef HAVE_FCHDIR
2793 if (fd >= 0)
2794 close(fd);
2795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
2797 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002798 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002799 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002800#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002801 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002803 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002806
Bram Moolenaar0f873732019-12-05 20:28:46 +01002807 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002808 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 return FAIL;
2810
Bram Moolenaar0f873732019-12-05 20:28:46 +01002811 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 if (STRCMP(fname, ".") != 0)
2813 STRCAT(buf, fname);
2814
2815 return OK;
2816}
2817
2818/*
2819 * Return TRUE if "fname" does not depend on the current directory.
2820 */
2821 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002822mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002824#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 return ( fname[0] == '/' || fname[0] == '.' ||
2826 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2827 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2828 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002829#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831#endif
2832}
2833
Bram Moolenaar24552be2005-12-10 20:17:30 +00002834#if defined(USE_FNAME_CASE) || defined(PROTO)
2835/*
2836 * Set the case of the file name, if it already exists. This will cause the
2837 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002838 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002839 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002840 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002841fname_case(
2842 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002843 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002844{
2845 struct stat st;
2846 char_u *slash, *tail;
2847 DIR *dirp;
2848 struct dirent *dp;
2849
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002850 if (mch_lstat((char *)name, &st) < 0)
2851 return;
2852
2853 // Open the directory where the file is located.
2854 slash = vim_strrchr(name, '/');
2855 if (slash == NULL)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002856 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002857 dirp = opendir(".");
2858 tail = name;
2859 }
2860 else
2861 {
2862 *slash = NUL;
2863 dirp = opendir((char *)name);
2864 *slash = '/';
2865 tail = slash + 1;
2866 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002867
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002868 if (dirp == NULL)
2869 return;
2870
2871 while ((dp = readdir(dirp)) != NULL)
2872 {
2873 // Only accept names that differ in case and are the same byte
2874 // length. TODO: accept different length name.
2875 if (STRICMP(tail, dp->d_name) == 0
2876 && STRLEN(tail) == STRLEN(dp->d_name))
Bram Moolenaar24552be2005-12-10 20:17:30 +00002877 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002878 char_u newname[MAXPATHL + 1];
2879 struct stat st2;
2880
2881 // Verify the inode is equal.
2882 vim_strncpy(newname, name, MAXPATHL);
2883 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2884 MAXPATHL - (tail - name));
2885 if (mch_lstat((char *)newname, &st2) >= 0
2886 && st.st_ino == st2.st_ino
2887 && st.st_dev == st2.st_dev)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002888 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002889 STRCPY(tail, dp->d_name);
2890 break;
Bram Moolenaar24552be2005-12-10 20:17:30 +00002891 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002892 }
2893 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002894
2895 closedir(dirp);
Bram Moolenaar24552be2005-12-10 20:17:30 +00002896}
2897#endif
2898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899/*
2900 * Get file permissions for 'name'.
2901 * Returns -1 when it doesn't exist.
2902 */
2903 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002904mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 struct stat statb;
2907
Bram Moolenaar0f873732019-12-05 20:28:46 +01002908 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909#ifdef VMS
2910 if (stat((char *)vms_fixfilename(name), &statb))
2911#else
2912 if (stat((char *)name, &statb))
2913#endif
2914 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002915#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002916 // The top bit makes the value negative, which means the file doesn't
2917 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002918 return statb.st_mode & ~S_ADDACE;
2919#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922}
2923
2924/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002925 * Set file permission for "name" to "perm".
2926 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 */
2928 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002929mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930{
2931 return (chmod((char *)
2932#ifdef VMS
2933 vms_fixfilename(name),
2934#else
2935 name,
2936#endif
2937 (mode_t)perm) == 0 ? OK : FAIL);
2938}
2939
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002940#if defined(HAVE_FCHMOD) || defined(PROTO)
2941/*
2942 * Set file permission for open file "fd" to "perm".
2943 * Return FAIL for failure, OK otherwise.
2944 */
2945 int
2946mch_fsetperm(int fd, long perm)
2947{
2948 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2949}
2950#endif
2951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952#if defined(HAVE_ACL) || defined(PROTO)
2953# ifdef HAVE_SYS_ACL_H
2954# include <sys/acl.h>
2955# endif
2956# ifdef HAVE_SYS_ACCESS_H
2957# include <sys/access.h>
2958# endif
2959
2960# ifdef HAVE_SOLARIS_ACL
2961typedef struct vim_acl_solaris_T {
2962 int acl_cnt;
2963 aclent_t *acl_entry;
2964} vim_acl_solaris_T;
2965# endif
2966
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002967#if defined(HAVE_SELINUX) || defined(PROTO)
2968/*
2969 * Copy security info from "from_file" to "to_file".
2970 */
2971 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002972mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002973{
2974 if (from_file == NULL)
2975 return;
2976
2977 if (selinux_enabled == -1)
2978 selinux_enabled = is_selinux_enabled();
2979
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002980 if (selinux_enabled <= 0)
2981 return;
2982
2983 // Use "char *" instead of "security_context_t" to avoid a deprecation
2984 // warning.
2985 char *from_context = NULL;
2986 char *to_context = NULL;
2987
2988 if (getfilecon((char *)from_file, &from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002989 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002990 // If the filesystem doesn't support extended attributes,
2991 // the original had no special security context and the
2992 // target cannot have one either.
2993 if (errno == EOPNOTSUPP)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002994 return;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002995
2996 msg_puts(_("\nCould not get security context for "));
2997 msg_outtrans(from_file);
2998 msg_putchar('\n');
2999 return;
3000 }
3001 if (getfilecon((char *)to_file, &to_context) < 0)
3002 {
3003 msg_puts(_("\nCould not get security context for "));
3004 msg_outtrans(to_file);
3005 msg_putchar('\n');
3006 freecon (from_context);
3007 return ;
3008 }
3009 if (strcmp(from_context, to_context) != 0)
3010 {
3011 if (setfilecon((char *)to_file, from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003012 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003013 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003014 msg_outtrans(to_file);
3015 msg_putchar('\n');
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003016 }
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003017 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003018 freecon(to_context);
3019 freecon(from_context);
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003020}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003021#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003022
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003023#if defined(HAVE_SMACK) && !defined(PROTO)
3024/*
3025 * Copy security info from "from_file" to "to_file".
3026 */
3027 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01003028mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003029{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02003030 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003031 {
3032 XATTR_NAME_SMACK,
3033 XATTR_NAME_SMACKEXEC,
3034 XATTR_NAME_SMACKMMAP
3035 };
3036
3037 char buffer[SMACK_LABEL_LEN];
3038 const char *name;
3039 int index;
3040 int ret;
3041 ssize_t size;
3042
3043 if (from_file == NULL)
3044 return;
3045
3046 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
3047 / sizeof(smack_copied_attributes)[0]) ; index++)
3048 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003049 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003050 name = smack_copied_attributes[index];
3051
Bram Moolenaar0f873732019-12-05 20:28:46 +01003052 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003053 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
3054 if (size >= 0)
3055 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003056 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003057 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
3058 if (ret < 0)
3059 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003060 vim_snprintf((char *)IObuff, IOSIZE,
3061 _("Could not set security context %s for %s"),
3062 name, to_file);
3063 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003064 msg_putchar('\n');
3065 }
3066 }
3067 else
3068 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003069 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003070 switch (errno)
3071 {
3072 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003073 // extended attributes aren't supported or enabled
3074 // should a message be echoed? not sure...
3075 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003076
3077 case ERANGE:
3078 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003079 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003080 vim_snprintf((char *)IObuff, IOSIZE,
3081 _("Could not get security context %s for %s. Removing it!"),
3082 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003083 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003084 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003085 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003086
3087 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003088 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003089 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003090 // Silently ignore errors, apparently this happens when
3091 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003092 break;
3093 }
3094 }
3095 }
3096}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003097#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003098
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099/*
3100 * Return a pointer to the ACL of file "fname" in allocated memory.
3101 * Return NULL if the ACL is not available for whatever reason.
3102 */
3103 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003104mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 vim_acl_T ret = NULL;
3107#ifdef HAVE_POSIX_ACL
3108 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3109#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003110#ifdef HAVE_SOLARIS_ZFS_ACL
3111 acl_t *aclent;
3112
3113 if (acl_get((char *)fname, 0, &aclent) < 0)
3114 return NULL;
3115 ret = (vim_acl_T)aclent;
3116#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117#ifdef HAVE_SOLARIS_ACL
3118 vim_acl_solaris_T *aclent;
3119
3120 aclent = malloc(sizeof(vim_acl_solaris_T));
3121 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3122 {
3123 free(aclent);
3124 return NULL;
3125 }
3126 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3127 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3128 {
3129 free(aclent->acl_entry);
3130 free(aclent);
3131 return NULL;
3132 }
3133 ret = (vim_acl_T)aclent;
3134#else
3135#if defined(HAVE_AIX_ACL)
3136 int aclsize;
3137 struct acl *aclent;
3138
3139 aclsize = sizeof(struct acl);
3140 aclent = malloc(aclsize);
3141 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3142 {
3143 if (errno == ENOSPC)
3144 {
3145 aclsize = aclent->acl_len;
3146 aclent = realloc(aclent, aclsize);
3147 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3148 {
3149 free(aclent);
3150 return NULL;
3151 }
3152 }
3153 else
3154 {
3155 free(aclent);
3156 return NULL;
3157 }
3158 }
3159 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003160#endif // HAVE_AIX_ACL
3161#endif // HAVE_SOLARIS_ACL
3162#endif // HAVE_SOLARIS_ZFS_ACL
3163#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 return ret;
3165}
3166
3167/*
3168 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3169 */
3170 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003171mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
3173 if (aclent == NULL)
3174 return;
3175#ifdef HAVE_POSIX_ACL
3176 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3177#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003178#ifdef HAVE_SOLARIS_ZFS_ACL
3179 acl_set((char *)fname, (acl_t *)aclent);
3180#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181#ifdef HAVE_SOLARIS_ACL
3182 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3183 ((vim_acl_solaris_T *)aclent)->acl_entry);
3184#else
3185#ifdef HAVE_AIX_ACL
3186 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003187#endif // HAVE_AIX_ACL
3188#endif // HAVE_SOLARIS_ACL
3189#endif // HAVE_SOLARIS_ZFS_ACL
3190#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191}
3192
3193 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003194mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195{
3196 if (aclent == NULL)
3197 return;
3198#ifdef HAVE_POSIX_ACL
3199 acl_free((acl_t)aclent);
3200#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003201#ifdef HAVE_SOLARIS_ZFS_ACL
3202 acl_free((acl_t *)aclent);
3203#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204#ifdef HAVE_SOLARIS_ACL
3205 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3206 free(aclent);
3207#else
3208#ifdef HAVE_AIX_ACL
3209 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003210#endif // HAVE_AIX_ACL
3211#endif // HAVE_SOLARIS_ACL
3212#endif // HAVE_SOLARIS_ZFS_ACL
3213#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214}
3215#endif
3216
3217/*
3218 * Set hidden flag for "name".
3219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003221mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003223 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224}
3225
3226/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003227 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 * return FALSE if "name" is not a directory
3229 * return FALSE for error
3230 */
3231 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003232mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233{
3234 struct stat statb;
3235
Bram Moolenaar0f873732019-12-05 20:28:46 +01003236 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 return FALSE;
3238 if (stat((char *)name, &statb))
3239 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241}
3242
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003243/*
3244 * return TRUE if "name" is a directory, NOT a symlink to a directory
3245 * return FALSE if "name" is not a directory
3246 * return FALSE for error
3247 */
3248 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003249mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003250{
3251 struct stat statb;
3252
Bram Moolenaar0f873732019-12-05 20:28:46 +01003253 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003254 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003255 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003256 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003257 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003258}
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260/*
3261 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3262 */
3263 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003264executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
3266 struct stat st;
3267
3268 if (stat((char *)name, &st))
3269 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003270#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003271 // Like on Unix system file can have executable rights but not necessarily
3272 // be an executable, but on Unix is not a default for an ordinary file to
3273 // have an executable flag - on VMS it is in most cases.
3274 // Therefore, this check does not have any sense - let keep us to the
3275 // conventions instead:
3276 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003277 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003278 int vms_executable = 0;
3279 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3280 {
3281 if (strstr(vms_tolower((char*)name),".exe") != NULL
3282 || strstr(vms_tolower((char*)name),".com")!= NULL)
3283 vms_executable = 1;
3284 }
3285 return vms_executable;
3286#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289}
3290
3291/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003292 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003293 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 * Return -1 if unknown.
3295 */
3296 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003297mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
3299 char_u *buf;
3300 char_u *p, *e;
3301 int retval;
3302
Bram Moolenaar0f873732019-12-05 20:28:46 +01003303 // When "use_path" is false and if it's an absolute or relative path don't
3304 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003305 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003306 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003307 // There must be a path separator, files in the current directory
3308 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003309 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003310 {
3311 if (path != NULL)
3312 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003313 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003314 *path = FullName_save(name, TRUE);
3315 else
3316 *path = vim_strsave(name);
3317 }
3318 return TRUE;
3319 }
3320 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
3323 p = (char_u *)getenv("PATH");
3324 if (p == NULL || *p == NUL)
3325 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003326 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 if (buf == NULL)
3328 return -1;
3329
3330 /*
3331 * Walk through all entries in $PATH to check if "name" exists there and
3332 * is an executable file.
3333 */
3334 for (;;)
3335 {
3336 e = (char_u *)strchr((char *)p, ':');
3337 if (e == NULL)
3338 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003339 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 STRCPY(buf, "./");
3341 else
3342 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003343 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 add_pathsep(buf);
3345 }
3346 STRCAT(buf, name);
3347 retval = executable_file(buf);
3348 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003349 {
3350 if (path != NULL)
3351 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003352 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003353 *path = FullName_save(buf, TRUE);
3354 else
3355 *path = vim_strsave(buf);
3356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
3360 if (*e != ':')
3361 break;
3362 p = e + 1;
3363 }
3364
3365 vim_free(buf);
3366 return retval;
3367}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
3369/*
3370 * Check what "name" is:
3371 * NODE_NORMAL: file or directory (or doesn't exist)
3372 * NODE_WRITABLE: writable device, socket, fifo, etc.
3373 * NODE_OTHER: non-writable things
3374 */
3375 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003376mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
3378 struct stat st;
3379
3380 if (stat((char *)name, &st))
3381 return NODE_NORMAL;
3382 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3383 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003384 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003386 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 return NODE_WRITABLE;
3388}
3389
3390 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003391mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
3393#ifdef HAVE_CHECK_STACK_GROWTH
3394 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 check_stack_growth((char *)&i);
3397
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003398# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 get_stack_limit();
3400# endif
3401
3402#endif
3403
3404 /*
3405 * Setup an alternative stack for signals. Helps to catch signals when
3406 * running out of stack space.
3407 * Use of sigaltstack() is preferred, it's more portable.
3408 * Ignore any errors.
3409 */
3410#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003411 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 init_signal_stack();
3413#endif
3414}
3415
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003416#if defined(EXITFREE) || defined(PROTO)
3417 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003418mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003419{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003420# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3421 if (clip_star.owned)
3422 clip_lose_selection(&clip_star);
3423 if (clip_plus.owned)
3424 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003425# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003426# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003427 if (xterm_Shell != (Widget)0)
3428 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003429# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003430 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003431 if (xterm_dpy != NULL)
3432 XtCloseDisplay(xterm_dpy);
3433 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003434 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003435 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003436# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003437 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003438# endif
3439 }
3440# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003441# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003442# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003443 if (x11_display != NULL
3444# ifdef FEAT_XCLIPBOARD
3445 && x11_display != xterm_dpy
3446# endif
3447 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003448 XCloseDisplay(x11_display);
3449# endif
3450# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003451 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003452# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003453 vim_free(oldtitle);
3454 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003455}
3456#endif
3457
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458/*
3459 * Output a newline when exiting.
3460 * Make sure the newline goes to the same stream as the text.
3461 */
3462 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003463exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003465 if (silent_mode)
3466 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 if (newline_on_exit || msg_didout)
3468 {
3469 if (msg_use_printf())
3470 {
3471 if (info_message)
3472 mch_msg("\n");
3473 else
3474 mch_errmsg("\r\n");
3475 }
3476 else
3477 out_char('\n');
3478 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003479 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003481 restore_cterm_colors(); // get original colors back
3482 msg_clr_eos_force(); // clear the rest of the display
3483 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
3485}
3486
Bram Moolenaarb4151682020-05-11 22:13:28 +02003487#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003488# if (defined(__GNUC__) \
3489 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3490 || (defined(__clang__) && (__clang_major__ >= 12))
3491extern void __gcov_dump(void);
3492extern void __gcov_reset(void);
3493# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3494# else
3495extern void __gcov_flush(void);
3496# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003497#endif
3498
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003500mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501{
3502 exiting = TRUE;
3503
3504#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3505 x11_export_final_selection();
3506#endif
3507
3508#ifdef FEAT_GUI
3509 if (!gui.in_use)
3510#endif
3511 {
3512 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003513 if (!is_not_a_term())
3514 {
3515 // restore xterm title and icon name
3516 mch_restore_title(SAVE_RESTORE_BOTH);
3517 term_pop_title(SAVE_RESTORE_BOTH);
3518 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 /*
3521 * When t_ti is not empty but it doesn't cause swapping terminal
3522 * pages, need to output a newline when msg_didout is set. But when
3523 * t_ti does swap pages it should not go to the shell page. Do this
3524 * before stoptermcap().
3525 */
3526 if (swapping_screen() && !newline_on_exit)
3527 exit_scroll();
3528
Bram Moolenaar0f873732019-12-05 20:28:46 +01003529 // Stop termcap: May need to check for T_CRV response, which
3530 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 stoptermcap();
3532
3533 /*
3534 * A newline is only required after a message in the alternate screen.
3535 * This is set to TRUE by wait_return().
3536 */
3537 if (!swapping_screen() || newline_on_exit)
3538 exit_scroll();
3539
Bram Moolenaar0f873732019-12-05 20:28:46 +01003540 // Cursor may have been switched off without calling starttermcap()
3541 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (full_screen)
3543 cursor_on();
3544 }
3545 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003546 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003547
3548#ifdef USE_GCOV_FLUSH
3549 // Flush coverage info before possibly being killed by a deadly signal.
3550 __gcov_flush();
3551#endif
3552
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 may_core_dump();
3554#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 gui_exit(r);
3557#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003558
Bram Moolenaar56718732006-03-15 22:53:57 +00003559#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003560 mac_conv_cleanup();
3561#endif
3562
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003564 // A core dump won't be created if the signal handler
3565 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 if (deadly_signal != 0)
3567 return;
3568#endif
3569
Bram Moolenaar009b2592004-10-24 19:18:58 +00003570#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003571 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003572#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003573
3574#ifdef EXITFREE
3575 free_all_mem();
3576#endif
3577
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 exit(r);
3579}
3580
3581 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003582may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583{
3584 if (deadly_signal != 0)
3585 {
ichizok378447f2023-05-11 22:25:42 +01003586 mch_signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003587 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
3589}
3590
3591#ifndef VMS
3592
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003593/*
3594 * Get the file descriptor to use for tty operations.
3595 */
3596 static int
3597get_tty_fd(int fd)
3598{
3599 int tty_fd = fd;
3600
3601#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3602 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3603 // "fd" when it is a master device.
3604 if (mch_isatty(fd) > 1)
3605 {
3606 char *name;
3607
3608 name = ptsname(fd);
3609 if (name == NULL)
3610 return -1;
3611
3612 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3613 if (tty_fd < 0)
3614 return -1;
3615 }
3616#endif
3617 return tty_fd;
3618}
3619
3620 static int
3621mch_tcgetattr(int fd, void *term)
3622{
3623 int tty_fd;
3624 int retval = -1;
3625
3626 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003627 if (tty_fd < 0)
3628 return -1;
3629
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003630#ifdef NEW_TTY_SYSTEM
3631# ifdef HAVE_TERMIOS_H
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003632 retval = tcgetattr(tty_fd, (struct termios *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003633# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003634 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003635# endif
3636#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003637 // for "old" tty systems
3638 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003639#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003640 if (tty_fd != fd)
3641 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003642 return retval;
3643}
3644
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003646mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
3648 static int first = TRUE;
3649
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003650#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651# ifdef HAVE_TERMIOS_H
3652 static struct termios told;
3653 struct termios tnew;
3654# else
3655 static struct termio told;
3656 struct termio tnew;
3657# endif
3658
3659 if (first)
3660 {
3661 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003662 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
3664
3665 tnew = told;
3666 if (tmode == TMODE_RAW)
3667 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003668 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003669 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3670 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003672# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003673 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674# endif
3675 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003676# ifdef ONLCR
3677 // Don't map NL -> CR NL, we do it ourselves.
3678 // Also disable expanding tabs if possible.
3679# ifdef XTABS
3680 tnew.c_oflag &= ~(ONLCR | XTABS);
3681# else
3682# ifdef TAB3
3683 tnew.c_oflag &= ~(ONLCR | TAB3);
3684# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003686# endif
3687# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003689 tnew.c_cc[VMIN] = 1; // return after 1 char
3690 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003693 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003694 // Also reset ICANON here, otherwise on Solaris select() won't see
3695 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003696 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003697 tnew.c_cc[VMIN] = 1; // return after 1 char
3698 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
3701# if defined(HAVE_TERMIOS_H)
3702 {
3703 int n = 10;
3704
Bram Moolenaar0f873732019-12-05 20:28:46 +01003705 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3706 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3708 && errno == EINTR && n > 0)
3709 --n;
3710 }
3711# else
3712 ioctl(read_cmd_fd, TCSETA, &tnew);
3713# endif
3714
3715#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 /*
3717 * for "old" tty systems
3718 */
3719# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003720# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721# endif
3722 static struct sgttyb ttybold;
3723 struct sgttyb ttybnew;
3724
3725 if (first)
3726 {
3727 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003728 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 }
3730
3731 ttybnew = ttybold;
3732 if (tmode == TMODE_RAW)
3733 {
3734 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3735 ttybnew.sg_flags |= RAW;
3736 }
3737 else if (tmode == TMODE_SLEEP)
3738 ttybnew.sg_flags &= ~(ECHO);
3739 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3740#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003741 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742}
3743
3744/*
3745 * Try to get the code for "t_kb" from the stty setting
3746 *
3747 * Even if termcap claims a backspace key, the user's setting *should*
3748 * prevail. stty knows more about reality than termcap does, and if
3749 * somebody's usual erase key is DEL (which, for most BSD users, it will
3750 * be), they're going to get really annoyed if their erase key starts
3751 * doing forward deletes for no reason. (Eric Fischer)
3752 */
3753 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003754get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003756 ttyinfo_T info;
3757 char_u buf[2];
3758 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003760 if (get_tty_info(read_cmd_fd, &info) != OK)
3761 return;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003762
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003763 intr_char = info.interrupt;
3764 buf[0] = info.backspace;
3765 buf[1] = NUL;
3766 add_termcode((char_u *)"kb", buf, FALSE);
3767
3768 // If <BS> and <DEL> are now the same, redefine <DEL>.
3769 p = find_termcode((char_u *)"kD");
3770 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3771 do_fixdel(NULL);
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003772}
3773
3774/*
3775 * Obtain the characters that Backspace and Enter produce on "fd".
3776 * Returns OK or FAIL.
3777 */
3778 int
3779get_tty_info(int fd, ttyinfo_T *info)
3780{
3781#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782# ifdef HAVE_TERMIOS_H
3783 struct termios keys;
3784# else
3785 struct termio keys;
3786# endif
3787
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003788 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003790 info->backspace = keys.c_cc[VERASE];
3791 info->interrupt = keys.c_cc[VINTR];
3792 if (keys.c_iflag & ICRNL)
3793 info->enter = NL;
3794 else
3795 info->enter = CAR;
3796 if (keys.c_oflag & ONLCR)
3797 info->nl_does_cr = TRUE;
3798 else
3799 info->nl_does_cr = FALSE;
3800 return OK;
3801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003803 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 struct sgttyb keys;
3805
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003806 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003808 info->backspace = keys.sg_erase;
3809 info->interrupt = keys.sg_kill;
3810 info->enter = CAR;
3811 info->nl_does_cr = TRUE;
3812 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003815 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816}
3817
Bram Moolenaar0f873732019-12-05 20:28:46 +01003818#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003820static int mouse_ison = FALSE;
3821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822/*
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003823 * Set mouse clicks on or off and possible enable mouse movement events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 */
3825 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003826mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003828#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003829 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 int xterm_mouse_vers;
3832
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003833#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003834 if (!on)
3835 // Make sure not tracing mouse movements. Important when a button-down
3836 // was received but no release yet.
3837 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003838#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003839
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003840 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003841#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003842 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003843#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003844 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003845 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 return;
3847
3848 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003849
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003850#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003851 if (ttym_flags == TTYM_URXVT)
3852 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003853 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003854 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003855 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003856#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003857
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00003858 if (T_CXM != NULL && *T_CXM != NUL)
3859 {
3860 term_enable_mouse(on);
3861 }
3862 else if (ttym_flags == TTYM_SGR)
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003863 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003864 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003865 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003866 mouse_ison = on;
3867 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003868
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003869#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003870 if (bevalterm_ison != (p_bevalterm && on))
3871 {
3872 bevalterm_ison = (p_bevalterm && on);
3873 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003874 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003875 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003876 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003877#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003878
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 if (xterm_mouse_vers > 0)
3880 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003881 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 out_str_nf((char_u *)
3883 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003884 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003885#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003886 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003887#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003888 "\033[?1002h")
3889 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003890 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003892 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003893 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
3895
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003896#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 else if (ttym_flags == TTYM_DEC)
3898 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003899 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003901 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003903 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003907#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 else
3909 {
3910 if (on)
3911 {
3912 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003913 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 }
3915 else
3916 {
3917 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003918 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 }
3920 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003923#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003924 else
3925 {
3926 if (on)
3927 {
3928 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003929 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003930 }
3931 else
3932 {
3933 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003934 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003935 }
3936 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003937#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003938
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003939#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 else
3941 {
3942 if (on)
3943 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003944 // D - Enable Mouse up/down messages
3945 // L - Enable Left Button Reporting
3946 // M - Enable Middle Button Reporting
3947 // R - Enable Right Button Reporting
3948 // K - Enable SHIFT and CTRL key Reporting
3949 // + - Enable Advanced messaging of mouse moves and up/down messages
3950 // Q - Quiet No Ack
3951 // # - Numeric value of mouse pointer required
3952 // 0 = Multiview 2000 cursor, used as standard
3953 // 1 = Windows Arrow
3954 // 2 = Windows I Beam
3955 // 3 = Windows Hour Glass
3956 // 4 = Windows Cross Hair
3957 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003958# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003959 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003960 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003961# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003962 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003963# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003964 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
3966 else
3967 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003968 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003969 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003972#endif
3973#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 else
3975 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003976 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 if (on)
3978 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3979 else
3980 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003981 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984}
3985
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003986#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003987/*
3988 * Called when 'balloonevalterm' changed.
3989 */
3990 void
3991mch_bevalterm_changed(void)
3992{
3993 mch_setmouse(mouse_ison);
3994}
3995#endif
3996
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997/*
3998 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3999 */
4000 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004001check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
4003# ifdef FEAT_MOUSE_XTERM
4004 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02004005# ifdef FEAT_MOUSE_URXVT
4006 && use_xterm_mouse() != 3
4007# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008# ifdef FEAT_GUI
4009 && !gui.in_use
4010# endif
4011 )
4012 {
4013 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004014 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 if (*p_mouse != NUL)
4016 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004017 // force mouse off and maybe on to send possibly new mouse
4018 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 mch_setmouse(FALSE);
4020 setmouse();
4021 }
4022 }
4023 else
4024 del_mouse_termcode(KS_MOUSE);
4025# endif
4026
4027# ifdef FEAT_MOUSE_GPM
4028 if (!use_xterm_mouse()
4029# ifdef FEAT_GUI
4030 && !gui.in_use
4031# endif
4032 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004033 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02004034 else
4035 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036# endif
4037
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004038# ifdef FEAT_SYSMOUSE
4039 if (!use_xterm_mouse()
4040# ifdef FEAT_GUI
4041 && !gui.in_use
4042# endif
4043 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004044 set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004045# endif
4046
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01004048 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 if (!use_xterm_mouse()
4050# ifdef FEAT_GUI
4051 && !gui.in_use
4052# endif
4053 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004054 set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 else
4056 del_mouse_termcode(KS_JSBTERM_MOUSE);
4057# endif
4058
4059# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01004060 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
4061 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 if (!use_xterm_mouse()
4063# ifdef FEAT_GUI
4064 && !gui.in_use
4065# endif
4066 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004067 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 else
4069 del_mouse_termcode(KS_NETTERM_MOUSE);
4070# endif
4071
4072# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01004073 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004074 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075# ifdef FEAT_GUI
4076 && !gui.in_use
4077# endif
4078 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004079 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004080 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 else
4082 del_mouse_termcode(KS_DEC_MOUSE);
4083# endif
4084# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004085 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004086 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087# ifdef FEAT_GUI
4088 && !gui.in_use
4089# endif
4090 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004091 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 else
4093 del_mouse_termcode(KS_PTERM_MOUSE);
4094# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004095# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004096 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004097# ifdef FEAT_GUI
4098 && !gui.in_use
4099# endif
4100 )
4101 {
4102 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004103 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004104
4105 if (*p_mouse != NUL)
4106 {
4107 mch_setmouse(FALSE);
4108 setmouse();
4109 }
4110 }
4111 else
4112 del_mouse_termcode(KS_URXVT_MOUSE);
4113# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004114 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004115# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004116 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004117# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004118 )
4119 {
4120 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004121 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004122
4123 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004124 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004125
4126 if (*p_mouse != NUL)
4127 {
4128 mch_setmouse(FALSE);
4129 setmouse();
4130 }
4131 }
4132 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004133 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004134 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004135 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139#ifndef VMS
4140
4141/*
4142 * Try to get the current window size:
4143 * 1. with an ioctl(), most accurate method
4144 * 2. from the environment variables LINES and COLUMNS
4145 * 3. from the termcap
4146 * 4. keep using the old values
4147 * Return OK when size could be determined, FAIL otherwise.
4148 */
4149 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004150mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151{
4152 long rows = 0;
4153 long columns = 0;
4154 char_u *p;
4155
4156 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 * 1. try using an ioctl. It is the most accurate method.
4158 *
4159 * Try using TIOCGWINSZ first, some systems that have it also define
4160 * TIOCGSIZE but don't have a struct ttysize.
4161 */
4162# ifdef TIOCGWINSZ
4163 {
4164 struct winsize ws;
4165 int fd = 1;
4166
Bram Moolenaar0f873732019-12-05 20:28:46 +01004167 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 if (!isatty(fd) && isatty(read_cmd_fd))
4169 fd = read_cmd_fd;
4170 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4171 {
4172 columns = ws.ws_col;
4173 rows = ws.ws_row;
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004174# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004175 ch_log(NULL, "Got size with TIOCGWINSZ: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004176# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
4178 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004179# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180# ifdef TIOCGSIZE
4181 {
4182 struct ttysize ts;
4183 int fd = 1;
4184
Bram Moolenaar0f873732019-12-05 20:28:46 +01004185 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 if (!isatty(fd) && isatty(read_cmd_fd))
4187 fd = read_cmd_fd;
4188 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4189 {
4190 columns = ts.ts_cols;
4191 rows = ts.ts_lines;
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004192# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004193 ch_log(NULL, "Got size with TIOCGSIZE: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004197# endif // TIOCGSIZE
4198# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
4200 /*
4201 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004202 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4203 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004205 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
4207 if ((p = (char_u *)getenv("LINES")))
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004208 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 rows = atoi((char *)p);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004210# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004211 ch_log(NULL, "Got 'lines' from $LINES: %ld", rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004212# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 if ((p = (char_u *)getenv("COLUMNS")))
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 columns = atoi((char *)p);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004217# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004218 ch_log(NULL, "Got 'columns' from $COLUMNS: %ld", columns);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004219# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222
4223#ifdef HAVE_TGETENT
4224 /*
4225 * 3. try reading "co" and "li" entries from termcap
4226 */
4227 if (columns == 0 || rows == 0)
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004228 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 getlinecol(&columns, &rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004230# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004231 ch_log(NULL, "Got size from termcap: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004232# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234#endif
4235
4236 /*
4237 * 4. If everything fails, use the old values
4238 */
4239 if (columns <= 0 || rows <= 0)
4240 return FAIL;
4241
4242 Rows = rows;
4243 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004244 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 return OK;
4246}
4247
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004248#if defined(FEAT_TERMINAL) || defined(PROTO)
4249/*
4250 * Report the windows size "rows" and "cols" to tty "fd".
4251 */
4252 int
4253mch_report_winsize(int fd, int rows, int cols)
4254{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004255 int tty_fd;
4256 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004257
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004258 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004259 if (tty_fd < 0)
4260 return FAIL;
4261
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004262# if defined(TIOCSWINSZ)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004263 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004264
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004265 ws.ws_col = cols;
4266 ws.ws_row = rows;
4267 ws.ws_xpixel = cols * 5;
4268 ws.ws_ypixel = rows * 10;
4269 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004270 ch_log(NULL, "ioctl(TIOCSWINSZ) %s", retval == 0 ? "success" : "failed");
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004271# elif defined(TIOCSSIZE)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004272 struct ttysize ts;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004273
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004274 ts.ts_cols = cols;
4275 ts.ts_lines = rows;
4276 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004277 ch_log(NULL, "ioctl(TIOCSSIZE) %s", retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004278# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004279 if (tty_fd != fd)
4280 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004281 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004282}
4283#endif
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285/*
4286 * Try to set the window size to Rows and Columns.
4287 */
4288 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004289mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290{
4291 if (*T_CWS)
4292 {
4293 /*
4294 * NOTE: if you get an error here that term_set_winsize() is
4295 * undefined, check the output of configure. It could probably not
4296 * find a ncurses, termcap or termlib library.
4297 */
4298 term_set_winsize((int)Rows, (int)Columns);
4299 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004300 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302}
4303
Bram Moolenaar0f873732019-12-05 20:28:46 +01004304#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306/*
4307 * Rows and/or Columns has changed.
4308 */
4309 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004310mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004312 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313}
4314
Bram Moolenaar205b8862011-09-07 15:04:31 +02004315/*
4316 * Wait for process "child" to end.
4317 * Return "child" if it exited properly, <= 0 on error.
4318 */
4319 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004320wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004321{
4322 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004323 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004324
4325 while (wait_pid != child)
4326 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004327 // When compiled with Python threads are probably used, in which case
4328 // wait() sometimes hangs for no obvious reason. Use waitpid()
4329 // instead and loop (like the GUI). Also needed for other interfaces,
4330 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004331# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004332 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004333# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004334 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004335# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004336 if (wait_pid == 0)
4337 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004338 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004339 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004340 if (++delay_msec > 10)
4341 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004342 continue;
4343 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004344 if (wait_pid <= 0
4345# ifdef ECHILD
4346 && errno == ECHILD
4347# endif
4348 )
4349 break;
4350 }
4351 return wait_pid;
4352}
4353
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004354#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004355/*
4356 * Set the environment for a child process.
4357 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004358 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004359set_child_environment(
4360 long rows,
4361 long columns,
4362 char *term,
4363 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004364{
4365# ifdef HAVE_SETENV
4366 char envbuf[50];
4367# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004368 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004369 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004370 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004371 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004372 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004373# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004374 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004375# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004376# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004377 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004378# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004379# endif
4380
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004381# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004382 setenv("TERM", term, 1);
4383 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004384 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004385 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004386 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004387 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004388 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004389 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004390 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004391# ifdef FEAT_TERMINAL
4392 if (is_terminal)
4393 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004394 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004395 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4396 }
4397# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004398# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004399 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004400# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004401# else
4402 /*
4403 * Putenv does not copy the string, it has to remain valid.
4404 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004405 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004406 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004407 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4408 putenv(envbuf_Term);
4409 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004410 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004411 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4412 putenv(envbuf_Lines);
4413 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4414 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004415 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004416 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004417 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004418# ifdef FEAT_TERMINAL
4419 if (is_terminal)
4420 {
4421 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004422 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004423 putenv(envbuf_Version);
4424 }
4425# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004426# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004427 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4428 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4429 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004430# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004431# endif
4432}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004433
4434 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004435set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004436{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004437 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004438}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004439#endif
4440
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004441#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004442/*
4443 * Open a PTY, with FD for the master and slave side.
4444 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004445 * When successful both file descriptors are stored and the allocated pty name
4446 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004447 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004448 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004449open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004450{
4451 char *tty_name;
4452
Bram Moolenaar59386482019-02-10 22:43:46 +01004453 if (name1 != NULL)
4454 *name1 = NULL;
4455 if (name2 != NULL)
4456 *name2 = NULL;
4457
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004458 *pty_master_fd = mch_openpty(&tty_name); // open pty
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004459 if (*pty_master_fd < 0)
4460 return;
4461
4462 // Leaving out O_NOCTTY may lead to waitpid() always returning
4463 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4464 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004465#ifdef O_NOCTTY
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004466 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004467#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004468 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004469#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004470 if (*pty_slave_fd < 0)
4471 {
4472 close(*pty_master_fd);
4473 *pty_master_fd = -1;
4474 }
4475 else
4476 {
4477 if (name1 != NULL)
4478 *name1 = vim_strsave((char_u *)tty_name);
4479 if (name2 != NULL)
4480 *name2 = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004481 }
4482}
4483#endif
4484
Bram Moolenaarfae42832017-08-01 22:24:26 +02004485/*
4486 * Send SIGINT to a child process if "c" is an interrupt character.
4487 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004488 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004489may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4490{
4491# ifdef SIGINT
4492 if (c == Ctrl_C || c == intr_char)
4493 {
4494# ifdef HAVE_SETSID
4495 kill(-pid, SIGINT);
4496# else
4497 kill(0, SIGINT);
4498# endif
4499 if (wpid > 0)
4500 kill(wpid, SIGINT);
4501 }
4502# endif
4503}
4504
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004505#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004506
Bram Moolenaar0f873732019-12-05 20:28:46 +01004507/*
4508 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4509 * pointers, the last one is NULL.
4510 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4511 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004512 int
4513unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004514 char_u *cmd,
4515 char ***argvp,
4516 char_u **sh_tofree,
4517 char_u **shcf_tofree)
4518{
4519 char **argv = NULL;
4520 int argc;
4521
4522 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004523 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004524 return FAIL;
4525
4526 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4527 return FAIL;
4528 *argvp = argv;
4529
4530 if (cmd != NULL)
4531 {
4532 char_u *s;
4533 char_u *p;
4534
4535 if (extra_shell_arg != NULL)
4536 argv[argc++] = (char *)extra_shell_arg;
4537
Bram Moolenaar0f873732019-12-05 20:28:46 +01004538 // Break 'shellcmdflag' into white separated parts. This doesn't
4539 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004540 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004541 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004542 return FAIL;
4543 s = *shcf_tofree;
4544 p = p_shcf;
4545 while (*p != NUL)
4546 {
4547 argv[argc++] = (char *)s;
4548 while (*p && *p != ' ' && *p != TAB)
4549 *s++ = *p++;
4550 *s++ = NUL;
4551 p = skipwhite(p);
4552 }
4553
4554 argv[argc++] = (char *)cmd;
4555 }
4556 argv[argc] = NULL;
4557 return OK;
4558}
4559#endif
4560
4561#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4562/*
4563 * Use a terminal window to run a shell command in.
4564 */
4565 static int
4566mch_call_shell_terminal(
4567 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004568 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004569{
4570 jobopt_T opt;
4571 char **argv = NULL;
4572 char_u *tofree1 = NULL;
4573 char_u *tofree2 = NULL;
4574 int retval = -1;
4575 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004576 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004577 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004578 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004579
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004580 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004581 goto theend;
4582
4583 init_job_options(&opt);
4584 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4585 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004586 if (buf == NULL)
4587 goto theend;
4588
4589 job = term_getjob(buf->b_term);
4590 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004591
Bram Moolenaar0f873732019-12-05 20:28:46 +01004592 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004593 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004594 if (curbuf == buf)
Bram Moolenaar13568252018-03-16 20:46:58 +01004595 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004596 // Only when managed to find a window for "buf",
4597 clear_oparg(&oa);
4598 while (term_use_loop())
Bram Moolenaar13568252018-03-16 20:46:58 +01004599 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004600 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4601 {
4602 // If terminal_loop() returns OK we got a key that is handled
4603 // in Normal model. We don't do redrawing anyway.
4604 if (terminal_loop(TRUE) == OK)
4605 normal_cmd(&oa, TRUE);
4606 }
4607 else
Bram Moolenaar13568252018-03-16 20:46:58 +01004608 normal_cmd(&oa, TRUE);
4609 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00004610 retval = job->jv_exitval;
4611 ch_log(NULL, "system command finished");
4612
4613 job_unref(job);
4614
4615 // restore curwin/curbuf and a few other things
4616 aucmd_restbuf(&aco);
Bram Moolenaar13568252018-03-16 20:46:58 +01004617 }
Bram Moolenaar13568252018-03-16 20:46:58 +01004618
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01004619 // Only require pressing Enter when redrawing, to avoid that system() gets
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004620 // the hit-enter prompt even though it didn't output anything.
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004621 if (RedrawingDisabled == 0)
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004622 wait_return(TRUE);
Bram Moolenaar13568252018-03-16 20:46:58 +01004623 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4624
4625theend:
4626 vim_free(argv);
4627 vim_free(tofree1);
4628 vim_free(tofree2);
4629 return retval;
4630}
4631#endif
4632
4633#ifdef USE_SYSTEM
4634/*
4635 * Use system() to start the shell: simple but slow.
4636 */
4637 static int
4638mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004639 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004640 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641{
4642#ifdef VMS
4643 char *ifn = NULL;
4644 char *ofn = NULL;
4645#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004646 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004647 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004648 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
4650 out_flush();
4651
4652 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004653 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
Bram Moolenaar62b42182010-09-21 22:09:37 +02004655# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004656 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004657 loose_clipboard();
4658# endif
4659
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (cmd == NULL)
4661 x = system((char *)p_sh);
4662 else
4663 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004664# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (ofn = strchr((char *)cmd, '>'))
4666 *ofn++ = '\0';
4667 if (ifn = strchr((char *)cmd, '<'))
4668 {
4669 char *p;
4670
4671 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004672 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 if (p)
4674 *p = '\0';
4675 }
4676 if (ofn)
4677 x = vms_sys((char *)cmd, ofn, ifn);
4678 else
4679 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004680# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004681 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004683 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 if (newcmd == NULL)
4685 x = 0;
4686 else
4687 {
4688 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4689 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4690 (char *)p_shcf,
4691 (char *)cmd);
4692 x = system((char *)newcmd);
4693 vim_free(newcmd);
4694 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004695# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 }
4697# ifdef VMS
4698 x = vms_sys_status(x);
4699# endif
4700 if (emsg_silent)
4701 ;
4702 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004703 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 else if (x && !(options & SHELL_SILENT))
4705 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004706 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 msg_outnum((long)x);
4708 msg_putchar('\n');
4709 }
4710
4711 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004712 {
4713 // The shell may have messed with the mode, always set it.
4714 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004715 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004718# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4719 restore_clipboard();
4720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723
Bram Moolenaar0f873732019-12-05 20:28:46 +01004724#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
Bram Moolenaar0f873732019-12-05 20:28:46 +01004726# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4727 // 127, some shells use that already
4728# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
Bram Moolenaar13568252018-03-16 20:46:58 +01004730/*
4731 * Don't use system(), use fork()/exec().
4732 */
4733 static int
4734mch_call_shell_fork(
4735 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004736 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004737{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004738 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004740 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 pid_t wait_pid = 0;
4742# ifdef HAVE_UNION_WAIT
4743 union wait status;
4744# else
4745 int status = -1;
4746# endif
4747 int retval = -1;
4748 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004749 char_u *tofree1 = NULL;
4750 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004752 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004753# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004755# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004756 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 int fd_fromshell[2];
4758 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004759 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
4761 out_flush();
4762 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004763 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004764 if (tmode == TMODE_RAW)
4765 // The shell may have messed with the mode, always set it later.
4766 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004768 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004769 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004772 * For the GUI, when writing the output into the buffer and when reading
4773 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4774 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004776 if ((options & (SHELL_READ|SHELL_WRITE))
4777# ifdef FEAT_GUI
4778 || (gui.in_use && show_shell_mess)
4779# endif
4780 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004782# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 /*
4784 * Try to open a master pty.
4785 * If this works, open the slave pty.
4786 * If the slave can't be opened, close the master pty.
4787 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004788 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004789 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 /*
4791 * If not opening a pty or it didn't work, try using pipes.
4792 */
4793 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004794# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
4796 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004797 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 {
4799 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004800 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 {
4802 close(fd_toshell[0]);
4803 close(fd_toshell[1]);
4804 }
4805 }
4806 if (pipe_error)
4807 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004808 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 out_flush();
4810 }
4811 }
4812 }
4813
Bram Moolenaar0f873732019-12-05 20:28:46 +01004814 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004816 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004817 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004818 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004819 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004821 UNBLOCK_SIGNALS(&curset);
4822
Bram Moolenaar32526b32019-01-19 17:43:09 +01004823 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004824 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004826 || (gui.in_use && show_shell_mess)
4827# endif
4828 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004830# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004831 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 {
4833 close(pty_master_fd);
4834 close(pty_slave_fd);
4835 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004836 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
4839 close(fd_toshell[0]);
4840 close(fd_toshell[1]);
4841 close(fd_fromshell[0]);
4842 close(fd_fromshell[1]);
4843 }
4844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004846 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004848 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004849 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00004851# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01004852 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004853 {
4854 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004855 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004856 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004857# endif
4858
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 if (!show_shell_mess || (options & SHELL_EXPAND))
4860 {
4861 int fd;
4862
4863 /*
4864 * Don't want to show any message from the shell. Can't just
4865 * close stdout and stderr though, because some systems will
4866 * break if you try to write to them after that, so we must
4867 * use dup() to replace them with something else -- webb
4868 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4869 * waiting for input.
4870 */
4871 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4872 fclose(stdin);
4873 fclose(stdout);
4874 fclose(stderr);
4875
4876 /*
4877 * If any of these open()'s and dup()'s fail, we just continue
4878 * anyway. It's not fatal, and on most systems it will make
4879 * no difference at all. On a few it will cause the execvp()
4880 * to exit with a non-zero status even when the completion
4881 * could be done, which is nothing too serious. If the open()
4882 * or dup() failed we'd just do the same thing ourselves
4883 * anyway -- webb
4884 */
4885 if (fd >= 0)
4886 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004887 vim_ignored = dup(fd); // To replace stdin (fd 0)
4888 vim_ignored = dup(fd); // To replace stdout (fd 1)
4889 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890
Bram Moolenaar0f873732019-12-05 20:28:46 +01004891 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 close(fd);
4893 }
4894 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004895 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004897 || gui.in_use
4898# endif
4899 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 {
4901
Bram Moolenaardf177f62005-02-22 08:39:57 +00004902# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004903 // Create our own process group, so that the child and all its
4904 // children can be kill()ed. Don't do this when using pipes,
4905 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004906 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004907 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004908 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004909# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004910 // When doing "!xterm&" and 'shell' is bash: the shell
4911 // will exit and send SIGHUP to all processes in its
4912 // group, killing the just started process. Ignore SIGHUP
4913 // to avoid that. (suggested by Simon Schubert)
ichizok378447f2023-05-11 22:25:42 +01004914 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar07256082009-02-04 13:19:42 +00004915# endif
4916 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004917# endif
4918# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004919 if (pty_slave_fd >= 0)
4920 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004921 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004922 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004923 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004924# ifdef TIOCSCTTY
4925 // Try to become controlling tty (probably doesn't work,
4926 // unless run by root)
4927 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4928# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004929 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004930# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004931 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
Bram Moolenaara5792f52005-11-23 21:25:05 +00004933 /*
4934 * stderr is only redirected when using the GUI, so that a
4935 * program like gpg can still access the terminal to get a
4936 * passphrase using stderr.
4937 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004938# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if (pty_master_fd >= 0)
4940 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004941 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942
Bram Moolenaar0f873732019-12-05 20:28:46 +01004943 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004945 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004947 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004948 if (gui.in_use)
4949 {
4950 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004951 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
Bram Moolenaar0f873732019-12-05 20:28:46 +01004954 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004957# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004959 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 close(fd_toshell[1]);
4961 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004962 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 close(fd_toshell[0]);
4964
Bram Moolenaar0f873732019-12-05 20:28:46 +01004965 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 close(fd_fromshell[0]);
4967 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004968 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 close(fd_fromshell[1]);
4970
Bram Moolenaara5792f52005-11-23 21:25:05 +00004971# ifdef FEAT_GUI
4972 if (gui.in_use)
4973 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004974 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004975 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004976 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004977 }
4978# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 }
4980 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004981
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 /*
4983 * There is no type cast for the argv, because the type may be
4984 * different on different machines. This may cause a warning
4985 * message with strict compilers, don't worry about it.
4986 * Call _exit() instead of exit() to avoid closing the connection
4987 * to the X server (esp. with GTK, which uses atexit()).
4988 */
4989 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004990 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004992 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 {
4994 /*
4995 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004996 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 */
4998 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004999 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005000 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005001# ifdef FEAT_JOB_CHANNEL
5002 ++dont_check_job_ended;
5003# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 /*
5005 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005006 * This is also used to pipe stdin/stdout to/from the external
5007 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005009 if ((options & (SHELL_READ|SHELL_WRITE))
5010# ifdef FEAT_GUI
5011 || (gui.in_use && show_shell_mess)
5012# endif
5013 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005015# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01005017 int buffer_off = 0; // valid bytes in buffer[]
5018 char_u ta_buf[BUFLEN + 1]; // TypeAHead
5019 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 int len;
5021 int p_more_save;
5022 int old_State;
5023 int c;
5024 int toshell_fd;
5025 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005026 garray_T ga;
5027 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01005028# ifdef ELAPSED_FUNC
5029 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031
Bram Moolenaardf177f62005-02-22 08:39:57 +00005032# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 if (pty_master_fd >= 0)
5034 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 fromshell_fd = pty_master_fd;
5036 toshell_fd = dup(pty_master_fd);
5037 }
5038 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00005039# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
5041 close(fd_toshell[0]);
5042 close(fd_fromshell[1]);
5043 toshell_fd = fd_toshell[1];
5044 fromshell_fd = fd_fromshell[0];
5045 }
5046
5047 /*
5048 * Write to the child if there are typed characters.
5049 * Read from the child if there are characters available.
5050 * Repeat the reading a few times if more characters are
5051 * available. Need to check for typed keys now and then, but
5052 * not too often (delays when no chars are available).
5053 * This loop is quit if no characters can be read from the pty
5054 * (WaitForChar detected special condition), or there are no
5055 * characters available and the child has exited.
5056 * Only check if the child has exited when there is no more
5057 * output. The child may exit before all the output has
5058 * been printed.
5059 *
5060 * Currently this busy loops!
5061 * This can probably dead-lock when the write blocks!
5062 */
5063 p_more_save = p_more;
5064 p_more = FALSE;
5065 old_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01005066 State = MODE_EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005068 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005069 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005070 // Fork a process that will write the lines to the
5071 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005072 if ((wpid = fork()) == -1)
5073 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005074 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005075 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005076 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00005077 {
5078 linenr_T lnum = curbuf->b_op_start.lnum;
5079 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005080 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005081 size_t l;
5082
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005083 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005084 for (;;)
5085 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005086 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005087 if (l == 0)
5088 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005089 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005090 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00005091 len = write(toshell_fd, "", (size_t)1);
5092 else
5093 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005094 char_u *s = vim_strchr(lp + written, NL);
5095
Bram Moolenaar89d40322006-08-29 15:30:07 +00005096 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00005097 s == NULL ? l
5098 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005099 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00005100 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005101 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005102 // Finished a line, add a NL, unless this line
5103 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005104 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005105 || (!curbuf->b_p_bin
5106 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005107 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaar88456cd2022-11-18 22:14:09 +00005108 && (lnum != curbuf->b_ml.ml_line_count
Bram Moolenaardf177f62005-02-22 08:39:57 +00005109 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005110 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005111 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005112 ++lnum;
5113 if (lnum > curbuf->b_op_end.lnum)
5114 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005115 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005116 close(toshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005117 break;
5118 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005119 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005120 written = 0;
5121 }
5122 else if (len > 0)
5123 written += len;
5124 }
5125 _exit(0);
5126 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005127 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005128 {
5129 close(toshell_fd);
5130 toshell_fd = -1;
5131 }
5132 }
5133
5134 if (options & SHELL_READ)
5135 ga_init2(&ga, 1, BUFLEN);
5136
5137 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005138# ifdef ELAPSED_FUNC
5139 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 for (;;)
5142 {
5143 /*
5144 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005145 * if there are any.
5146 * Don't do this if we are expanding wild cards (would eat
5147 * typeahead).
5148 * Don't do this when filtering and terminal is in cooked
5149 * mode, the shell command will handle the I/O. Avoids
5150 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005151 * Don't get characters when the child has already
5152 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005153 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005154 * while (noread_cnt > 4), avoids that ":r !ls" eats
5155 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 */
5157 len = 0;
5158 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005159 && ((options &
5160 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5161 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005162# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005163 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005164# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005165 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005166 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005167 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005169 if (ta_len == 0)
5170 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005171 // Get extra characters when we don't have any.
5172 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005173 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005174# ifdef ELAPSED_FUNC
5175 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005176# endif
5177 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5178 }
5179 if (ta_len > 0 || len > 0)
5180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 /*
5182 * For pipes:
5183 * Check for CTRL-C: send interrupt signal to child.
5184 * Check for CTRL-D: EOF, close pipe to child.
5185 */
5186 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5187 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 /*
5189 * Send SIGINT to the child's group or all
5190 * processes in our group.
5191 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005192 may_send_sigint(ta_buf[ta_len], pid, wpid);
5193
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 if (pty_master_fd < 0 && toshell_fd >= 0
5195 && ta_buf[ta_len] == Ctrl_D)
5196 {
5197 close(toshell_fd);
5198 toshell_fd = -1;
5199 }
5200 }
5201
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01005202 // Remove Vim-specific codes from the input.
5203 len = term_replace_keycodes(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204
5205 /*
5206 * For pipes: echo the typed characters.
5207 * For a pty this does not seem to work.
5208 */
5209 if (pty_master_fd < 0)
5210 {
5211 for (i = ta_len; i < ta_len + len; ++i)
5212 {
5213 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5214 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 else if (has_mbyte)
5216 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005217 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218
5219 msg_outtrans_len(ta_buf + i, l);
5220 i += l - 1;
5221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 else
5223 msg_outtrans_len(ta_buf + i, 1);
5224 }
5225 windgoto(msg_row, msg_col);
5226 out_flush();
5227 }
5228
5229 ta_len += len;
5230
5231 /*
5232 * Write the characters to the child, unless EOF has
5233 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005234 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005235 * When writing buffer lines, drop the typed
5236 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005238 if (options & SHELL_WRITE)
5239 ta_len = 0;
5240 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 {
5242 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5243 if (len > 0)
5244 {
5245 ta_len -= len;
5246 mch_memmove(ta_buf, ta_buf + len, ta_len);
5247 }
5248 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
5251
Bram Moolenaardf177f62005-02-22 08:39:57 +00005252 if (got_int)
5253 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005254 // CTRL-C sends a signal to the child, we ignore it
5255 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005256# ifdef HAVE_SETSID
5257 kill(-pid, SIGINT);
5258# else
5259 kill(0, SIGINT);
5260# endif
5261 if (wpid > 0)
5262 kill(wpid, SIGINT);
5263 got_int = FALSE;
5264 }
5265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 /*
5267 * Check if the child has any characters to be printed.
5268 * Read them and write them to our window. Repeat this as
5269 * long as there is something to do, avoid the 10ms wait
5270 * for mch_inchar(), or sending typeahead characters to
5271 * the external process.
5272 * TODO: This should handle escape sequences, compatible
5273 * to some terminal (vt52?).
5274 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005275 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005276 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005278 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005281 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005283
5284 noread_cnt = 0;
5285 if (options & SHELL_READ)
5286 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005287 // Do NUL -> NL translation, append NL separated
5288 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005289 for (i = 0; i < len; ++i)
5290 {
5291 if (buffer[i] == NL)
5292 append_ga_line(&ga);
5293 else if (buffer[i] == NUL)
5294 ga_append(&ga, NL);
5295 else
5296 ga_append(&ga, buffer[i]);
5297 }
5298 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005299 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 {
5301 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005302 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
Bram Moolenaardf177f62005-02-22 08:39:57 +00005304 len += buffer_off;
5305 buffer[len] = NUL;
5306
Bram Moolenaar0f873732019-12-05 20:28:46 +01005307 // Check if the last character in buffer[] is
5308 // incomplete, keep these bytes for the next
5309 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 for (p = buffer; p < buffer + len; p += l)
5311 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005312 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005314 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 else if (MB_BYTE2LEN(*p) != l)
5316 break;
5317 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005318 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005320 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 if (len >= 12)
5322 ++p;
5323 else
5324 {
5325 buffer_off = len;
5326 continue;
5327 }
5328 }
5329 c = *p;
5330 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005331 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 if (p < buffer + len)
5333 {
5334 *p = c;
5335 buffer_off = (buffer + len) - p;
5336 mch_memmove(buffer, p, buffer_off);
5337 continue;
5338 }
5339 buffer_off = 0;
5340 }
5341 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
5343 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005344 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
5346
5347 windgoto(msg_row, msg_col);
5348 cursor_on();
5349 out_flush();
5350 if (got_int)
5351 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005352
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005353# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005354 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005355 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005356 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005357
Bram Moolenaar0f873732019-12-05 20:28:46 +01005358 // Avoid that we keep looping here without
5359 // checking for a CTRL-C for a long time. Don't
5360 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005361 if (msec > 2000)
5362 {
5363 noread_cnt = 5;
5364 break;
5365 }
5366 }
5367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
5369
Bram Moolenaar0f873732019-12-05 20:28:46 +01005370 // If we already detected the child has finished, continue
5371 // reading output for a short while. Some text may be
5372 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005373 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005374 {
5375 if (noread_cnt < 5)
5376 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005377 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005378 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005379
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 /*
5381 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005382 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005384# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005385 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005386# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5390 || (wait_pid == pid && WIFEXITED(status)))
5391 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005392 // Don't break the loop yet, try reading more
5393 // characters from "fromshell_fd" first. When using
5394 // pipes there might still be something to read and
5395 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005398 else
5399 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005400
Bram Moolenaar95a51352013-03-21 22:53:50 +01005401# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005402 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005403 clip_update();
5404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 }
5406finished:
5407 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005408 if (options & SHELL_READ)
5409 {
5410 if (ga.ga_len > 0)
5411 {
5412 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005413 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005414 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005415 }
5416 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005417 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005418 ga_clear(&ga);
5419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 /*
5422 * Give all typeahead that wasn't used back to ui_inchar().
5423 */
5424 if (ta_len)
5425 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 State = old_State;
5427 if (toshell_fd >= 0)
5428 close(toshell_fd);
5429 close(fromshell_fd);
5430 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005431# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005432 else
5433 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005434 long delay_msec = 1;
5435
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005436 if (tmode == TMODE_RAW)
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005437 // Possibly disables modifyOtherKeys, so that the system
5438 // can recognize CTRL-C.
5439 out_str_t_TE();
Bram Moolenaar0981c872020-08-23 14:28:37 +02005440
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005441 /*
5442 * Similar to the loop above, but only handle X events, no
5443 * I/O.
5444 */
5445 for (;;)
5446 {
5447 if (got_int)
5448 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005449 // CTRL-C sends a signal to the child, we ignore it
5450 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005451# ifdef HAVE_SETSID
5452 kill(-pid, SIGINT);
5453# else
5454 kill(0, SIGINT);
5455# endif
5456 got_int = FALSE;
5457 }
5458# ifdef __NeXT__
5459 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5460# else
5461 wait_pid = waitpid(pid, &status, WNOHANG);
5462# endif
5463 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5464 || (wait_pid == pid && WIFEXITED(status)))
5465 {
5466 wait_pid = pid;
5467 break;
5468 }
5469
Bram Moolenaar0f873732019-12-05 20:28:46 +01005470 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005471 clip_update();
5472
Bram Moolenaar0f873732019-12-05 20:28:46 +01005473 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005474 // less time, gradually wait longer.
5475 mch_delay(delay_msec,
5476 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005477 if (++delay_msec > 10)
5478 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005479 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005480
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005481 if (tmode == TMODE_RAW)
5482 // possibly enables modifyOtherKeys again
Bram Moolenaar733a69b2022-12-01 12:03:47 +00005483 out_str_t_TI();
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005484 }
5485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
5487 /*
5488 * Wait until our child has exited.
5489 * Ignore wait() returning pids of other children and returning
5490 * because of some signal like SIGWINCH.
5491 * Don't wait if wait_pid was already set above, indicating the
5492 * child already exited.
5493 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005494 if (wait_pid != pid)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01005495 (void)wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
Bram Moolenaar624891f2010-10-13 16:22:09 +02005497# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005498 // Close slave side of pty. Only do this after the child has
5499 // exited, otherwise the child may hang when it tries to write on
5500 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005501 if (pty_master_fd >= 0)
5502 close(pty_slave_fd);
5503# endif
5504
Bram Moolenaar0f873732019-12-05 20:28:46 +01005505 // Make sure the child that writes to the external program is
5506 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005507 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005508 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005509 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005510 wait4pid(wpid, NULL);
5511 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005512
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005513# ifdef FEAT_JOB_CHANNEL
5514 --dont_check_job_ended;
5515# endif
5516
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 /*
5518 * Set to raw mode right now, otherwise a CTRL-C after
5519 * catch_signals() will kill Vim.
5520 */
5521 if (tmode == TMODE_RAW)
5522 settmode(TMODE_RAW);
5523 did_settmode = TRUE;
5524 set_signals();
5525
5526 if (WIFEXITED(status))
5527 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005528 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005530 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
5532 if (retval == EXEC_FAILED)
5533 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005534 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 msg_outtrans(p_sh);
5536 msg_putchar('\n');
5537 }
5538 else if (!(options & SHELL_SILENT))
5539 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005540 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 msg_outnum((long)retval);
5542 msg_putchar('\n');
5543 }
5544 }
5545 }
5546 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005547 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
5549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550
5551error:
5552 if (!did_settmode)
5553 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005554 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005556 vim_free(argv);
5557 vim_free(tofree1);
5558 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
5560 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005561}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005562#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005563
5564 int
5565mch_call_shell(
5566 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005567 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005568{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005569#ifdef FEAT_EVAL
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01005570 ch_log(NULL, "executing shell command: %s", cmd);
5571#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005572#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar524c8532022-09-27 15:48:20 +01005573 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL
5574 && (options & SHELL_SILENT) == 0)
Bram Moolenaar13568252018-03-16 20:46:58 +01005575 return mch_call_shell_terminal(cmd, options);
5576#endif
5577#ifdef USE_SYSTEM
5578 return mch_call_shell_system(cmd, options);
5579#else
5580 return mch_call_shell_fork(cmd, options);
5581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582}
5583
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005584#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005585 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005586mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005587{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005588 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005589 int fd_in[2] = {-1, -1}; // for stdin
5590 int fd_out[2] = {-1, -1}; // for stdout
5591 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005592 int pty_master_fd = -1;
5593 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005594 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005595 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5596 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5597 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005598 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005599 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5600 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005601 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005602 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005603 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005604
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005605 if (use_out_for_err && use_null_for_out)
5606 use_null_for_err = TRUE;
5607
Bram Moolenaar0f873732019-12-05 20:28:46 +01005608 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005609 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005610
Bram Moolenaarb2412082017-08-20 18:09:14 +02005611 if (options->jo_pty
5612 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005613 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005614 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005615 open_pty(&pty_master_fd, &pty_slave_fd,
5616 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005617
Bram Moolenaar0f873732019-12-05 20:28:46 +01005618 // TODO: without the channel feature connect the child to /dev/null?
5619 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005620 if (use_file_for_in)
5621 {
5622 char_u *fname = options->jo_io_name[PART_IN];
5623
5624 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5625 if (fd_in[0] < 0)
5626 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005627 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005628 goto failed;
5629 }
5630 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005631 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005632 // When writing buffer lines to the input don't use the pty, so that
5633 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005634 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5635 && pipe(fd_in) < 0)
5636 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005637
5638 if (use_file_for_out)
5639 {
5640 char_u *fname = options->jo_io_name[PART_OUT];
5641
5642 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5643 if (fd_out[1] < 0)
5644 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005645 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005646 goto failed;
5647 }
5648 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005649 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005650 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005651
5652 if (use_file_for_err)
5653 {
5654 char_u *fname = options->jo_io_name[PART_ERR];
5655
5656 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5657 if (fd_err[1] < 0)
5658 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005659 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005660 goto failed;
5661 }
5662 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005663 else if (!use_out_for_err && !use_null_for_err
5664 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005665 goto failed;
5666
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005667 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5668 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005669 if (options->jo_set & JO_CHANNEL)
5670 {
5671 channel = options->jo_channel;
5672 if (channel != NULL)
5673 ++channel->ch_refcount;
5674 }
5675 else
5676 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005677 if (channel == NULL)
5678 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005679 if (job->jv_tty_out != NULL)
5680 ch_log(channel, "using pty %s on fd %d",
5681 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005682 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005683
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005684 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005685 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005686 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005687 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005688 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005689 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005690 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005691 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005692 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005693 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005694 int null_fd = -1;
5695 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005696
Bram Moolenaar0f873732019-12-05 20:28:46 +01005697 // child
5698 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005699 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005700
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005701# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01005702 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005703 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005704 ch_logfile((char_u *)"", (char_u *)"");
5705# endif
5706
Bram Moolenaar835dc632016-02-07 14:27:38 +01005707# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005708 // Create our own process group, so that the child and all its
5709 // children can be kill()ed. Don't do this when using pipes,
5710 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005711 (void)setsid();
5712# endif
5713
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005714# ifdef FEAT_TERMINAL
5715 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005716 {
5717 char *term = (char *)T_NAME;
5718
5719#ifdef FEAT_GUI
5720 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005721 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005722 term = getenv("TERM");
5723#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005724 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005725 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005726 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005727 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005728 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005729 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005730 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005731 else if (t_colors > 16)
5732 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005733 else
5734 term = "xterm";
5735 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005736 set_child_environment(
5737 (long)options->jo_term_rows,
5738 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005739 term,
5740 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005741 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005742 else
5743# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005744 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005745
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005746 if (options->jo_env != NULL)
5747 {
5748 dict_T *dict = options->jo_env;
5749 hashitem_T *hi;
5750 int todo = (int)dict->dv_hashtab.ht_used;
5751
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005752 FOR_ALL_HASHTAB_ITEMS(&dict->dv_hashtab, hi, todo)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005753 if (!HASHITEM_EMPTY(hi))
5754 {
5755 typval_T *item = &dict_lookup(hi)->di_tv;
5756
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005757 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005758 --todo;
5759 }
5760 }
5761
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005762 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005763 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005764 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005765 if (null_fd < 0)
5766 {
5767 perror("opening /dev/null failed");
5768 _exit(OPEN_NULL_FAILED);
5769 }
5770 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005771
Bram Moolenaar223896d2017-08-02 22:33:28 +02005772 if (pty_slave_fd >= 0)
5773 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005774 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005775 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005776# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005777 // Try to become controlling tty (probably doesn't work,
5778 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005779 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5780# endif
5781 }
5782
Bram Moolenaar0f873732019-12-05 20:28:46 +01005783 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005784 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005785 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005786 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005787 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005788 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005789 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005790 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005791
Bram Moolenaar0f873732019-12-05 20:28:46 +01005792 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005793 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005794 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005795 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005796 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005797 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005798 }
5799 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005800 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005801 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005802 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005803 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005804 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005805
Bram Moolenaar0f873732019-12-05 20:28:46 +01005806 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005807 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005808 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005809 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005810 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005811 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005812 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005813 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005814
5815 if (fd_in[0] >= 0)
5816 close(fd_in[0]);
5817 if (fd_in[1] >= 0)
5818 close(fd_in[1]);
5819 if (fd_out[0] >= 0)
5820 close(fd_out[0]);
5821 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005822 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005823 if (fd_err[0] >= 0)
5824 close(fd_err[0]);
5825 if (fd_err[1] >= 0)
5826 close(fd_err[1]);
5827 if (pty_master_fd >= 0)
5828 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005829 close(pty_master_fd); // not used in the child
5830 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005831 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005832
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005833 if (null_fd >= 0)
5834 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005835
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005836 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5837 _exit(EXEC_FAILED);
5838
Bram Moolenaar0f873732019-12-05 20:28:46 +01005839 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005840 execvp(argv[0], argv);
5841
Bram Moolenaar4694a172016-04-21 14:05:23 +02005842 if (stderr_works)
5843 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005844# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005845 // calling free_all_mem() here causes problems. Ignore valgrind
5846 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005847# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005848 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005849 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005850
Bram Moolenaar0f873732019-12-05 20:28:46 +01005851 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005852 UNBLOCK_SIGNALS(&curset);
5853
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005854 job->jv_pid = pid;
5855 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005856 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005857
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005858 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005859 close(pty_slave_fd); // not used in the parent
5860 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005861 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005862 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005863 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005864 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005865 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005866 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005867 if (channel != NULL)
5868 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005869 int in_fd = INVALID_FD;
5870 int out_fd = INVALID_FD;
5871 int err_fd = INVALID_FD;
5872
5873 if (!(use_file_for_in || use_null_for_in))
5874 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5875
5876 if (!(use_file_for_out || use_null_for_out))
5877 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5878
5879 // When using pty_master_fd only set it for stdout, do not duplicate
5880 // it for stderr, it only needs to be read once.
5881 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5882 {
5883 if (fd_err[0] >= 0)
5884 err_fd = fd_err[0];
5885 else if (out_fd != pty_master_fd)
5886 err_fd = pty_master_fd;
5887 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005888
5889 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005890 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005891 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005892 else
5893 {
5894 if (fd_in[1] >= 0)
5895 close(fd_in[1]);
5896 if (fd_out[0] >= 0)
5897 close(fd_out[0]);
5898 if (fd_err[0] >= 0)
5899 close(fd_err[0]);
5900 if (pty_master_fd >= 0)
5901 close(pty_master_fd);
5902 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005903
Bram Moolenaar0f873732019-12-05 20:28:46 +01005904 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005905 return;
5906
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005907failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005908 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005909 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005910 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005911 if (fd_in[1] >= 0)
5912 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005913 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005914 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005915 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005916 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005917 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005918 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005919 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005920 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005921 if (pty_master_fd >= 0)
5922 close(pty_master_fd);
5923 if (pty_slave_fd >= 0)
5924 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005925}
5926
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005927 static char_u *
5928get_signal_name(int sig)
5929{
5930 int i;
5931 char_u numbuf[NUMBUFLEN];
5932
5933 if (sig == SIGKILL)
5934 return vim_strsave((char_u *)"kill");
5935
5936 for (i = 0; signal_info[i].sig != -1; i++)
5937 if (sig == signal_info[i].sig)
5938 return strlow_save((char_u *)signal_info[i].name);
5939
5940 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5941 return vim_strsave(numbuf);
5942}
5943
Bram Moolenaar835dc632016-02-07 14:27:38 +01005944 char *
5945mch_job_status(job_T *job)
5946{
5947# ifdef HAVE_UNION_WAIT
5948 union wait status;
5949# else
5950 int status = -1;
5951# endif
5952 pid_t wait_pid = 0;
5953
5954# ifdef __NeXT__
5955 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5956# else
5957 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5958# endif
5959 if (wait_pid == -1)
5960 {
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00005961 int waitpid_errno = errno;
5962 if (waitpid_errno == ECHILD && mch_process_running(job->jv_pid))
5963 // The process is alive, but it was probably reparented (for
5964 // example by ptrace called by a debugger like lldb or gdb).
5965 // Note: This assumes that process IDs are not reused.
5966 return "run";
5967
Bram Moolenaar0f873732019-12-05 20:28:46 +01005968 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005969 if (job->jv_status < JOB_ENDED)
5970 ch_log(job->jv_channel, "Job no longer exists: %s",
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00005971 strerror(waitpid_errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005972 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005973 }
5974 if (wait_pid == 0)
5975 return "run";
5976 if (WIFEXITED(status))
5977 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005978 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005979 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005980 if (job->jv_status < JOB_ENDED)
5981 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005982 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005983 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005984 if (WIFSIGNALED(status))
5985 {
5986 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005987 job->jv_termsig = get_signal_name(WTERMSIG(status));
5988 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5989 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5990 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005991 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005992 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005993 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005994
5995return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005996 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005997 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005998 return "dead";
5999}
6000
6001 job_T *
6002mch_detect_ended_job(job_T *job_list)
6003{
6004# ifdef HAVE_UNION_WAIT
6005 union wait status;
6006# else
6007 int status = -1;
6008# endif
6009 pid_t wait_pid = 0;
6010 job_T *job;
6011
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01006012# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006013 // Do not do this when waiting for a shell command to finish, we would get
6014 // the exit value here (and discard it), the exit value obtained there
6015 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01006016 if (dont_check_job_ended > 0)
6017 return NULL;
6018# endif
6019
Bram Moolenaar97792de2016-10-15 18:36:49 +02006020# ifdef __NeXT__
6021 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
6022# else
6023 wait_pid = waitpid(-1, &status, WNOHANG);
6024# endif
6025 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006026 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02006027 return NULL;
6028 for (job = job_list; job != NULL; job = job->jv_next)
6029 {
6030 if (job->jv_pid == wait_pid)
6031 {
6032 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006033 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02006034 job->jv_exitval = WEXITSTATUS(status);
6035 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006036 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02006037 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006038 job->jv_termsig = get_signal_name(WTERMSIG(status));
6039 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01006040 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02006041 {
6042 ch_log(job->jv_channel, "Job ended");
6043 job->jv_status = JOB_ENDED;
6044 }
6045 return job;
6046 }
6047 }
6048 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006049}
6050
Bram Moolenaar3a117e12016-10-30 21:57:52 +01006051/*
6052 * Send a (deadly) signal to "job".
6053 * Return FAIL if "how" is not a valid name.
6054 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01006055 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006056mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01006057{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006058 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006059
Bram Moolenaar923d9262016-02-25 20:56:01 +01006060 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01006061 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01006062 else if (STRCMP(how, "hup") == 0)
6063 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006064 else if (STRCMP(how, "quit") == 0)
6065 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01006066 else if (STRCMP(how, "int") == 0)
6067 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006068 else if (STRCMP(how, "kill") == 0)
6069 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02006070#ifdef SIGWINCH
6071 else if (STRCMP(how, "winch") == 0)
6072 sig = SIGWINCH;
6073#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006074 else if (isdigit(*how))
6075 sig = atoi((char *)how);
6076 else
6077 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01006078
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01006079 // Never kill ourselves!
6080 if (job->jv_pid != 0)
6081 {
6082 // TODO: have an option to only kill the process, not the group?
6083 kill(-job->jv_pid, sig);
6084 kill(job->jv_pid, sig);
6085 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01006086
Bram Moolenaar835dc632016-02-07 14:27:38 +01006087 return OK;
6088}
Bram Moolenaar76467df2016-02-12 19:30:26 +01006089
6090/*
6091 * Clear the data related to "job".
6092 */
6093 void
6094mch_clear_job(job_T *job)
6095{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006096 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01006097# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006098 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006099# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006100 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006101# endif
6102}
Bram Moolenaar835dc632016-02-07 14:27:38 +01006103#endif
6104
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006105#if defined(FEAT_TERMINAL) || defined(PROTO)
6106 int
6107mch_create_pty_channel(job_T *job, jobopt_T *options)
6108{
6109 int pty_master_fd = -1;
6110 int pty_slave_fd = -1;
6111 channel_T *channel;
6112
Bram Moolenaar59386482019-02-10 22:43:46 +01006113 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02006114 if (pty_master_fd < 0 || pty_slave_fd < 0)
6115 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006116 close(pty_slave_fd);
6117
6118 channel = add_channel();
6119 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006120 {
6121 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006122 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006123 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006124 if (job->jv_tty_out != NULL)
6125 ch_log(channel, "using pty %s on fd %d",
6126 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006127 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006128 channel->ch_keep_open = TRUE;
6129
Bram Moolenaar0f873732019-12-05 20:28:46 +01006130 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6131 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006132 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006133 channel_set_job(channel, job, options);
6134 return OK;
6135}
6136#endif
6137
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138/*
6139 * Check for CTRL-C typed by reading all available characters.
6140 * In cooked mode we should get SIGINT, no need to check.
6141 */
6142 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006143mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006145 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006146 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 fill_input_buf(FALSE);
6148}
6149
6150/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006151 * Wait "msec" msec until a character is available from the mouse, keyboard,
6152 * from inbuf[].
6153 * "msec" == -1 will block forever.
6154 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006155 * When "ignore_input" is TRUE even check for pending input when input is
6156 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006157 * "interrupted" (if not NULL) is set to TRUE when no character is available
6158 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006159 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006160 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 */
6162 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006163WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006165#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006166 return ui_wait_for_chars_or_timer(
6167 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006168#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006169 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006170#endif
6171}
6172
6173/*
6174 * Wait "msec" msec until a character is available from the mouse or keyboard
6175 * or from inbuf[].
6176 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006177 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006178 * "interrupted" (if not NULL) is set to TRUE when no character is available
6179 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006180 * When a GUI is being used, this will never get called -- webb
6181 */
6182 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006183WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006184{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185#ifdef FEAT_MOUSE_GPM
6186 int gpm_process_wanted;
6187#endif
6188#ifdef FEAT_XCLIPBOARD
6189 int rest;
6190#endif
6191 int avail;
6192
Bram Moolenaar0f873732019-12-05 20:28:46 +01006193 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 return 1;
6195
6196#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006197 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 if (WantQueryMouse)
6199 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006200 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006201 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006202 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 }
6204#endif
6205
6206 /*
6207 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6208 * events. This is a bit complicated, because they might both be defined.
6209 */
6210#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6211# ifdef FEAT_XCLIPBOARD
6212 rest = 0;
6213 if (do_xterm_trace())
6214 rest = msec;
6215# endif
6216 do
6217 {
6218# ifdef FEAT_XCLIPBOARD
6219 if (rest != 0)
6220 {
6221 msec = XT_TRACE_DELAY;
6222 if (rest >= 0 && rest < XT_TRACE_DELAY)
6223 msec = rest;
6224 if (rest >= 0)
6225 rest -= msec;
6226 }
6227# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01006228# ifdef FEAT_SOUND_MACOSX
6229 // Invoke any pending sound callbacks.
6230 process_cfrunloop();
6231# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006232# ifdef FEAT_SOUND_CANBERRA
6233 // Invoke any pending sound callbacks.
6234 if (has_sound_callback_in_queue())
6235 invoke_sound_callback();
6236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237# ifdef FEAT_MOUSE_GPM
6238 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006239 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006240 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006241 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006243 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006247 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 return 1;
6249# ifdef FEAT_XCLIPBOARD
6250 if (rest == 0 || !do_xterm_trace())
6251# endif
6252 break;
6253 }
6254 }
6255 while (FALSE
6256# ifdef FEAT_MOUSE_GPM
6257 || (gpm_process_wanted && mch_gpm_process() == 0)
6258# endif
6259# ifdef FEAT_XCLIPBOARD
6260 || (!avail && rest != 0)
6261# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006262 )
6263 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264
6265#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006266 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267#endif
6268 return avail;
6269}
6270
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006271#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272/*
6273 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006274 * "msec" == 0 will check for characters once.
6275 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006278 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006279 * "interrupted" (if not NULL) is set to TRUE when no character is available
6280 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006282 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006283RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284{
6285 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006286 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006287#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 static int busy = FALSE;
6289
Bram Moolenaar0f873732019-12-05 20:28:46 +01006290 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291# define MAY_LOOP
6292
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006293# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006294 // Remember at what time we started, so that we know how much longer we
6295 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006296 long start_msec = msec;
6297 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006299 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006300 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301# endif
6302
Bram Moolenaar0f873732019-12-05 20:28:46 +01006303 // Handle being called recursively. This may happen for the session
6304 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 if (busy)
6306 return 0;
6307#endif
6308
6309#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006310 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311#endif
6312 {
6313#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006314 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006315# ifdef FEAT_MZSCHEME
6316 int mzquantum_used = FALSE;
6317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318#endif
6319#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006320 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006321 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 int nfd;
6323# ifdef FEAT_XCLIPBOARD
6324 int xterm_idx = -1;
6325# endif
6326# ifdef FEAT_MOUSE_GPM
6327 int gpm_idx = -1;
6328# endif
6329# ifdef USE_XSMP
6330 int xsmp_idx = -1;
6331# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006332 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006334# ifdef FEAT_MZSCHEME
6335 mzvim_check_threads();
6336 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6337 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006338 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006339 mzquantum_used = TRUE;
6340 }
6341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 fds[0].fd = fd;
6343 fds[0].events = POLLIN;
6344 nfd = 1;
6345
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006347 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 if (xterm_Shell != (Widget)0)
6349 {
6350 xterm_idx = nfd;
6351 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6352 fds[nfd].events = POLLIN;
6353 nfd++;
6354 }
6355# endif
6356# ifdef FEAT_MOUSE_GPM
6357 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6358 {
6359 gpm_idx = nfd;
6360 fds[nfd].fd = gpm_fd;
6361 fds[nfd].events = POLLIN;
6362 nfd++;
6363 }
6364# endif
6365# ifdef USE_XSMP
6366 if (xsmp_icefd != -1)
6367 {
6368 xsmp_idx = nfd;
6369 fds[nfd].fd = xsmp_icefd;
6370 fds[nfd].events = POLLIN;
6371 nfd++;
6372 }
6373# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006374#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006375 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006376#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006377 if (interrupted != NULL)
6378 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006380 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006381
6382 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006383 if (result == 0 && interrupted != NULL && ret > 0)
6384 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006385
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006386# ifdef FEAT_MZSCHEME
6387 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006388 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006389 finished = FALSE;
6390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392# ifdef FEAT_XCLIPBOARD
6393 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6394 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006395 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006397 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 finished = FALSE;
6399 }
6400# endif
6401# ifdef FEAT_MOUSE_GPM
6402 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404# endif
6405# ifdef USE_XSMP
6406 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6407 {
6408 if (fds[xsmp_idx].revents & POLLIN)
6409 {
6410 busy = TRUE;
6411 xsmp_handle_requests();
6412 busy = FALSE;
6413 }
6414 else if (fds[xsmp_idx].revents & POLLHUP)
6415 {
6416 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006417 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 xsmp_close();
6419 }
6420 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006421 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 }
6423# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006424#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006425 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006426 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006427 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429
Bram Moolenaar0f873732019-12-05 20:28:46 +01006430#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431
6432 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006433 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006434 // These are static because they can take 8 Kbyte each and cause the
6435 // signal stack to run out with -O3.
6436 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006438 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006440# ifdef FEAT_MZSCHEME
6441 mzvim_check_threads();
6442 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6443 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006444 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006445 mzquantum_used = TRUE;
6446 }
6447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006449 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006451 tv.tv_sec = towait / 1000;
6452 tv.tv_usec = (towait % 1000) * (1000000/1000);
6453 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006455 else
6456 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457
6458 /*
6459 * Select on ready for reading and exceptional condition (end of file).
6460 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006461select_eintr:
6462 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006463 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 FD_ZERO(&efds);
6465 FD_SET(fd, &rfds);
K.Takatab247e062022-02-07 10:45:23 +00006466# ifndef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006467 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 FD_SET(fd, &efds);
6469# endif
6470 maxfd = fd;
6471
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006473 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 if (xterm_Shell != (Widget)0)
6475 {
6476 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6477 if (maxfd < ConnectionNumber(xterm_dpy))
6478 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006479
Bram Moolenaar0f873732019-12-05 20:28:46 +01006480 // An event may have already been read but not handled. In
6481 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006482 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 }
6484# endif
6485# ifdef FEAT_MOUSE_GPM
6486 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6487 {
6488 FD_SET(gpm_fd, &rfds);
6489 FD_SET(gpm_fd, &efds);
6490 if (maxfd < gpm_fd)
6491 maxfd = gpm_fd;
6492 }
6493# endif
6494# ifdef USE_XSMP
6495 if (xsmp_icefd != -1)
6496 {
6497 FD_SET(xsmp_icefd, &rfds);
6498 FD_SET(xsmp_icefd, &efds);
6499 if (maxfd < xsmp_icefd)
6500 maxfd = xsmp_icefd;
6501 }
6502# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006503# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006504 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006505# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006506 if (interrupted != NULL)
6507 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508
Bram Moolenaar643b6142018-09-12 20:29:09 +02006509 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6510 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006511 result = ret > 0 && FD_ISSET(fd, &rfds);
6512 if (result)
6513 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006514 else if (interrupted != NULL && ret > 0)
6515 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006516
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006517# ifdef EINTR
6518 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006519 {
dbivolaruab16ad32021-12-29 19:41:47 +00006520 // Check whether the EINTR is caused by SIGTSTP
6521 if (got_tstp && !in_mch_suspend)
6522 {
6523 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006524
dbivolaruab16ad32021-12-29 19:41:47 +00006525 ea.forceit = TRUE;
6526 ex_stop(&ea);
6527 got_tstp = FALSE;
6528 }
6529
Bram Moolenaar0f873732019-12-05 20:28:46 +01006530 // Check whether window has been resized, EINTR may be caused by
6531 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006532 if (do_resize)
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006533 {
Bram Moolenaar545c8a52023-06-21 15:51:47 +01006534# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006535 ch_log(NULL, "calling handle_resize() in RealWaitForChar()");
Bram Moolenaar545c8a52023-06-21 15:51:47 +01006536# endif
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006537 handle_resize();
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006538 }
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006539
Bram Moolenaar0f873732019-12-05 20:28:46 +01006540 // Interrupted by a signal, need to try again. We ignore msec
6541 // here, because we do want to check even after a timeout if
6542 // characters are available. Needed for reading output of an
6543 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006544 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006545 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006546# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006547# ifdef __TANDEM
6548 if (ret == -1 && errno == ENOTSUP)
6549 {
6550 FD_ZERO(&rfds);
6551 FD_ZERO(&efds);
6552 ret = 0;
6553 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006554# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006555# ifdef FEAT_MZSCHEME
6556 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006557 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006558 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559# endif
6560
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561# ifdef FEAT_XCLIPBOARD
6562 if (ret > 0 && xterm_Shell != (Widget)0
6563 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6564 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006565 xterm_update(); // Maybe we should hand out clipboard
6566 // continue looping when we only got the X event and the input
6567 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 if (--ret == 0 && !input_available())
6569 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006570 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 finished = FALSE;
6572 }
6573 }
6574# endif
6575# ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00006576 if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 {
6578 if (FD_ISSET(gpm_fd, &efds))
6579 gpm_close();
6580 else if (FD_ISSET(gpm_fd, &rfds))
6581 *check_for_gpm = 1;
6582 }
6583# endif
6584# ifdef USE_XSMP
6585 if (ret > 0 && xsmp_icefd != -1)
6586 {
6587 if (FD_ISSET(xsmp_icefd, &efds))
6588 {
6589 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006590 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 xsmp_close();
6592 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006593 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 }
6595 else if (FD_ISSET(xsmp_icefd, &rfds))
6596 {
6597 busy = TRUE;
6598 xsmp_handle_requests();
6599 busy = FALSE;
6600 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006601 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 }
6603 }
6604# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006605#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006606 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006607 if (ret >= 0)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01006608 (void)channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610
Bram Moolenaar0f873732019-12-05 20:28:46 +01006611#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612
6613#ifdef MAY_LOOP
6614 if (finished || msec == 0)
6615 break;
6616
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006617# ifdef FEAT_CLIENTSERVER
6618 if (server_waiting())
6619 break;
6620# endif
6621
Bram Moolenaar0f873732019-12-05 20:28:46 +01006622 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 if (msec > 0)
6624 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006625# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006626 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006627 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006629 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 msec = msec / 2;
6631# endif
6632 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006633 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 }
6635#endif
6636 }
6637
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006638 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639}
6640
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006642 * Expand a path into all matching files and/or directories. Handles "*",
6643 * "?", "[a-z]", "**", etc.
6644 * "path" has backslashes before chars that are not to be expanded.
6645 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 */
6647 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006648mch_expandpath(
6649 garray_T *gap,
6650 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006651 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652{
Bram Moolenaar02743632005-07-25 20:42:36 +00006653 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655
6656/*
6657 * mch_expand_wildcards() - this code does wild-card pattern matching using
6658 * the shell
6659 *
6660 * return OK for success, FAIL for error (you may lose some memory) and put
6661 * an error message in *file.
6662 *
6663 * num_pat is number of input patterns
6664 * pat is array of pointers to input patterns
6665 * num_file is pointer to number of matched file names
6666 * file is pointer to array of pointers to matched file names
6667 */
6668
6669#ifndef SEEK_SET
6670# define SEEK_SET 0
6671#endif
6672#ifndef SEEK_END
6673# define SEEK_END 2
6674#endif
6675
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006676#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006677
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006679mch_expand_wildcards(
6680 int num_pat,
6681 char_u **pat,
6682 int *num_file,
6683 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006684 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
6686 int i;
6687 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006688 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 char_u *p;
6690 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691
Bram Moolenaarc7247912008-01-13 12:54:11 +00006692 /*
6693 * This is the non-OS/2 implementation (really Unix).
6694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 int j;
6696 char_u *tempname;
6697 char_u *command;
6698 FILE *fd;
6699 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006700#define STYLE_ECHO 0 // use "echo", the default
6701#define STYLE_GLOB 1 // use "glob", for csh
6702#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6703#define STYLE_PRINT 3 // use "print -N", for zsh
6704#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6705 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 int shell_style = STYLE_ECHO;
6707 int check_spaces;
6708 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006709 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006710 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006711 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712
Bram Moolenaar0f873732019-12-05 20:28:46 +01006713 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 *file = NULL;
6715
6716 /*
6717 * If there are no wildcards, just copy the names to allocated memory.
6718 * Saves a lot of time, because we don't have to start a new shell.
6719 */
6720 if (!have_wildcard(num_pat, pat))
6721 return save_patterns(num_pat, pat, num_file, file);
6722
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006723# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006724 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006725 if (sandbox != 0 && check_secure())
6726 return FAIL;
6727# endif
6728
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 /*
6730 * Don't allow the use of backticks in secure and restricted mode.
6731 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006732 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 for (i = 0; i < num_pat; ++i)
6734 if (vim_strchr(pat[i], '`') != NULL
6735 && (check_restricted() || check_secure()))
6736 return FAIL;
6737
6738 /*
6739 * get a name for the temp file
6740 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006741 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006743 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 return FAIL;
6745 }
6746
6747 /*
6748 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006749 * file.
6750 * STYLE_BT: NL separated
6751 * If expanding `cmd` execute it directly.
6752 * STYLE_GLOB: NUL separated
6753 * If we use *csh, "glob" will work better than "echo".
6754 * STYLE_PRINT: NL or NUL separated
6755 * If we use *zsh, "print -N" will work better than "glob".
6756 * STYLE_VIMGLOB: NL separated
6757 * If we use *sh*, we define "vimglob()".
6758 * STYLE_ECHO: space separated.
6759 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 */
6761 if (num_pat == 1 && *pat[0] == '`'
6762 && (len = STRLEN(pat[0])) > 2
6763 && *(pat[0] + len - 1) == '`')
6764 shell_style = STYLE_BT;
6765 else if ((len = STRLEN(p_sh)) >= 3)
6766 {
6767 if (STRCMP(p_sh + len - 3, "csh") == 0)
6768 shell_style = STYLE_GLOB;
6769 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6770 shell_style = STYLE_PRINT;
6771 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006772 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6773 "sh") != NULL)
6774 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
Bram Moolenaar0f873732019-12-05 20:28:46 +01006776 // Compute the length of the command. We need 2 extra bytes: for the
6777 // optional '&' and for the NUL.
6778 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006780 if (shell_style == STYLE_VIMGLOB)
6781 len += STRLEN(sh_vimglob_func);
6782
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006783 for (i = 0; i < num_pat; ++i)
6784 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006785 // Count the length of the patterns in the same way as they are put in
6786 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006787#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006788 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006789#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006790 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006791 for (j = 0; pat[i][j] != NUL; ++j)
6792 {
6793 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006794 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006795 ++len;
6796 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006797#endif
6798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 command = alloc(len);
6800 if (command == NULL)
6801 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006802 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 vim_free(tempname);
6804 return FAIL;
6805 }
6806
6807 /*
6808 * Build the shell command:
6809 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6810 * recognizes this).
6811 * - Add the shell command to print the expanded names.
6812 * - Add the temp file name.
6813 * - Add the file name patterns.
6814 */
6815 if (shell_style == STYLE_BT)
6816 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006817 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006818 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006819 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006821 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006822 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006824 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006826 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 *p = ' ';
6828 }
6829 STRCAT(command, ">");
6830 }
6831 else
6832 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006833 STRCPY(command, "");
6834 if (shell_style == STYLE_GLOB)
6835 {
6836 // Assume the nonomatch option is valid only for csh like shells,
6837 // otherwise, this may set the positional parameters for the shell,
6838 // e.g. "$*".
6839 if (flags & EW_NOTFOUND)
6840 STRCAT(command, "set nonomatch; ");
6841 else
6842 STRCAT(command, "unset nonomatch; ");
6843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 if (shell_style == STYLE_GLOB)
6845 STRCAT(command, "glob >");
6846 else if (shell_style == STYLE_PRINT)
6847 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006848 else if (shell_style == STYLE_VIMGLOB)
6849 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 else
6851 STRCAT(command, "echo >");
6852 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006853
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006855
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 if (shell_style != STYLE_BT)
6857 for (i = 0; i < num_pat; ++i)
6858 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006859 // When using system() always add extra quotes, because the shell
6860 // is started twice. Otherwise put a backslash before special
6861 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862#ifdef USE_SYSTEM
6863 STRCAT(command, " \"");
6864 STRCAT(command, pat[i]);
6865 STRCAT(command, "\"");
6866#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006867 int intick = FALSE;
6868
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 p = command + STRLEN(command);
6870 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006871 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006872 {
6873 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006874 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006875 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6876 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006877 // Remove a backslash, take char literally. But keep
6878 // backslash inside backticks, before a special character
6879 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006880 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006881 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6882 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006883 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006884 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006885 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006886 else if (!intick
6887 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6888 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006889 // Put a backslash before a special character, but not
6890 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6891 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006892 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006893
Bram Moolenaar0f873732019-12-05 20:28:46 +01006894 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006895 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 *p = NUL;
6898#endif
6899 }
6900 if (flags & EW_SILENT)
6901 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006902 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006903 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904
6905 /*
6906 * Using zsh -G: If a pattern has no matches, it is just deleted from
6907 * the argument list, otherwise zsh gives an error message and doesn't
6908 * expand any other pattern.
6909 */
6910 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006911 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
6913 /*
6914 * If we use -f then shell variables set in .cshrc won't get expanded.
6915 * vi can do it, so we will too, but it is only necessary if there is a "$"
6916 * in one of the patterns, otherwise we can still use the fast option.
6917 */
6918 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006919 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920
6921 /*
6922 * execute the shell command
6923 */
6924 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6925
Bram Moolenaar0f873732019-12-05 20:28:46 +01006926 // When running in the background, give it some time to create the temp
6927 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006928 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006929 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930
Bram Moolenaar0f873732019-12-05 20:28:46 +01006931 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 show_shell_mess = TRUE;
6933 vim_free(command);
6934
Bram Moolenaar0f873732019-12-05 20:28:46 +01006935 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {
6937 mch_remove(tempname);
6938 vim_free(tempname);
6939 /*
6940 * With interactive completion, the error message is not printed.
6941 * However with USE_SYSTEM, I don't know how to turn off error messages
6942 * from the shell, so screen may still get messed up -- webb.
6943 */
6944#ifndef USE_SYSTEM
6945 if (!(flags & EW_SILENT))
6946#endif
6947 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006948 redraw_later_clear(); // probably messed up screen
6949 msg_putchar('\n'); // clear bottom line quickly
6950 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951#ifdef USE_SYSTEM
6952 if (!(flags & EW_SILENT))
6953#endif
6954 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006955 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006956 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 }
6958 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006959 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6960 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 if (shell_style == STYLE_BT)
6962 return FAIL;
6963 goto notfound;
6964 }
6965
6966 /*
6967 * read the names from the file into memory
6968 */
6969 fd = fopen((char *)tempname, READBIN);
6970 if (fd == NULL)
6971 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006972 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 if (!(flags & EW_SILENT))
6974 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006975 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006976 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 }
6978 vim_free(tempname);
6979 goto notfound;
6980 }
6981 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006982 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006984 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006985 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006986 buffer = NULL;
6987 else
6988 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 if (buffer == NULL)
6990 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006991 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 mch_remove(tempname);
6993 vim_free(tempname);
6994 fclose(fd);
6995 return FAIL;
6996 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006997 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 i = fread((char *)buffer, 1, len, fd);
6999 fclose(fd);
7000 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00007001 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007003 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007004 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 vim_free(tempname);
7006 vim_free(buffer);
7007 return FAIL;
7008 }
7009 vim_free(tempname);
7010
Bram Moolenaar1eed5322019-02-26 17:03:54 +01007011# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01007012 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02007014 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
7016 *p++ = buffer[i];
7017 len = p - buffer;
7018# endif
7019
7020
Bram Moolenaar0f873732019-12-05 20:28:46 +01007021 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 if (shell_style == STYLE_ECHO)
7023 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007024 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007026 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {
7028 while (*p != ' ' && *p != '\n')
7029 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007030 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 }
7032 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007033 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00007034 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007036 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007038 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {
7040 while (*p != '\n' && *p != NUL)
7041 ++p;
7042 if (*p != NUL)
7043 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007044 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 }
7046 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007047 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 else
7049 {
7050 /*
7051 * Some versions of zsh use spaces instead of NULs to separate
7052 * results. Only do this when there is no NUL before the end of the
7053 * buffer, otherwise we would never be able to use file names with
7054 * embedded spaces when zsh does use NULs.
7055 * When we found a NUL once, we know zsh is OK, set did_find_nul and
7056 * don't check for spaces again.
7057 */
7058 check_spaces = FALSE;
7059 if (shell_style == STYLE_PRINT && !did_find_nul)
7060 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007061 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007062 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01007063 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 did_find_nul = TRUE;
7065 else
7066 check_spaces = TRUE;
7067 }
7068
7069 /*
7070 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
7071 * already is one, for STYLE_GLOB it needs to be added.
7072 */
7073 if (len && buffer[len - 1] == NUL)
7074 --len;
7075 else
7076 buffer[len] = NUL;
7077 i = 0;
7078 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007079 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {
7081 ++i;
7082 *p = NUL;
7083 }
7084 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007085 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 }
7087 if (i == 0)
7088 {
7089 /*
7090 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
7091 * /bin/sh will happily expand it to nothing rather than returning an
7092 * error; and hey, it's good to check anyway -- webb.
7093 */
7094 vim_free(buffer);
7095 goto notfound;
7096 }
7097 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007098 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 if (*file == NULL)
7100 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007101 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 vim_free(buffer);
7103 return FAIL;
7104 }
7105
7106 /*
7107 * Isolate the individual file names.
7108 */
7109 p = buffer;
7110 for (i = 0; i < *num_file; ++i)
7111 {
7112 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007113 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00007114 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
7115 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00007117 while (!(shell_style == STYLE_ECHO && *p == ' ')
7118 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007120 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 *p = NUL;
7122 else
7123 {
7124 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007125 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 }
7127 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007128 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007130 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007132 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 }
7134 }
7135
7136 /*
7137 * Move the file names to allocated memory.
7138 */
7139 for (j = 0, i = 0; i < *num_file; ++i)
7140 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007141 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7143 continue;
7144
Bram Moolenaar0f873732019-12-05 20:28:46 +01007145 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 dir = (mch_isdir((*file)[i]));
7147 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7148 continue;
7149
Bram Moolenaar0f873732019-12-05 20:28:46 +01007150 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007151 if (!dir && (flags & EW_EXEC)
7152 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007153 continue;
7154
Bram Moolenaar964b3742019-05-24 18:54:09 +02007155 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 if (p)
7157 {
7158 STRCPY(p, (*file)[i]);
7159 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007160 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 (*file)[j++] = p;
7162 }
7163 }
7164 vim_free(buffer);
7165 *num_file = j;
7166
Bram Moolenaar0f873732019-12-05 20:28:46 +01007167 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007169 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 goto notfound;
7171 }
7172
7173 return OK;
7174
7175notfound:
7176 if (flags & EW_NOTFOUND)
7177 return save_patterns(num_pat, pat, num_file, file);
7178 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179}
7180
Bram Moolenaar0f873732019-12-05 20:28:46 +01007181#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007184save_patterns(
7185 int num_pat,
7186 char_u **pat,
7187 int *num_file,
7188 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189{
7190 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007191 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007193 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 if (*file == NULL)
7195 return FAIL;
7196 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007197 {
7198 s = vim_strsave(pat[i]);
7199 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007200 // Be compatible with expand_filename(): halve the number of
7201 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007202 backslash_halve(s);
7203 (*file)[i] = s;
7204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 *num_file = num_pat;
7206 return OK;
7207}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209/*
7210 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7211 * expand.
7212 */
7213 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007214mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007216 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 if (*p == '\\' && p[1] != NUL)
7219 ++p;
7220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 if (vim_strchr((char_u *)
7222#ifdef VMS
7223 "*?%"
7224#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226#endif
7227 , *p) != NULL)
7228 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 }
7230 return FALSE;
7231}
7232
7233/*
7234 * Return TRUE if the string "p" contains a wildcard.
7235 * Don't recognize '~' at the end as a wildcard.
7236 */
7237 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007238mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007240 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 if (*p == '\\' && p[1] != NUL)
7243 ++p;
7244 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 if (vim_strchr((char_u *)
7246#ifdef VMS
7247 "*?%$"
7248#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250#endif
7251 , *p) != NULL
7252 || (*p == '~' && p[1] != NUL))
7253 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 }
7255 return FALSE;
7256}
7257
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007259have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
7261 int i;
7262
7263 for (i = 0; i < num; i++)
7264 if (mch_has_wildcard(file[i]))
7265 return 1;
7266 return 0;
7267}
7268
7269 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007270have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271{
7272 int i;
7273
7274 for (i = 0; i < num; i++)
7275 if (vim_strchr(file[i], '$') != NULL)
7276 return TRUE;
7277 return FALSE;
7278}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007280#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281/*
7282 * Scaled-down version of rename(), which is missing in Xenix.
7283 * This version can only move regular files and will fail if the
7284 * destination exists.
7285 */
7286 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007287mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288{
7289 struct stat st;
7290
Bram Moolenaar0f873732019-12-05 20:28:46 +01007291 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007293 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007295 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 return 0;
7297 return -1;
7298}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007299#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007301#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007302# if defined(DYNAMIC_GPM) || defined(PROTO)
7303/*
7304 * Initialize Gpm's symbols for dynamic linking.
7305 * Must be called only if libgpm_hinst is NULL.
7306 */
7307 static int
7308load_libgpm(void)
7309{
7310 libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL);
7311
7312 if (libgpm_hinst == NULL)
7313 {
7314 if (p_verbose > 0)
7315 smsg_attr(HL_ATTR(HLF_W),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007316 _("Could not load gpm library: %s"), dlerror());
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007317 return FAIL;
7318 }
7319
7320 if (
7321 (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL
7322 || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL
7323 || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL
7324 || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL
7325 || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL
7326 )
7327 {
7328 semsg(_(e_could_not_load_library_str_str), "gpm", dlerror());
7329 dlclose(libgpm_hinst);
7330 libgpm_hinst = NULL;
7331 dll_gpm_flag = NULL;
7332 dll_gpm_fd = NULL;
7333 return FAIL;
7334 }
7335 return OK;
7336}
7337
7338 int
7339gpm_available(void)
7340{
7341 return libgpm_hinst != NULL || load_libgpm() == OK;
7342}
7343# endif // DYNAMIC_GPM
7344
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345/*
7346 * Initializes connection with gpm (if it isn't already opened)
7347 * Return 1 if succeeded (or connection already opened), 0 if failed
7348 */
7349 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007350gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007352 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007354#ifdef DYNAMIC_GPM
7355 if (!gpm_available())
7356 return 0;
7357#endif
7358
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007359 if (gpm_flag)
7360 return 1; // already open
7361
7362 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7363 gpm_connect.defaultMask = ~GPM_HARD;
7364 // Default handling for mouse move
7365 gpm_connect.minMod = 0; // Handle any modifier keys
7366 gpm_connect.maxMod = 0xffff;
7367 if (Gpm_Open(&gpm_connect, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007369 // gpm library tries to handling TSTP causes
7370 // problems. Anyways, we close connection to Gpm whenever
7371 // we are going to suspend or starting an external process
7372 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007373# ifdef SIGTSTP
ichizok378447f2023-05-11 22:25:42 +01007374 mch_signal(SIGTSTP, restricted ? SIG_IGN : sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007375# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007376 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007378 if (gpm_fd == -2)
7379 Gpm_Close(); // We don't want to talk to xterm via gpm
7380 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381}
7382
7383/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007384 * Returns TRUE if the GPM mouse is enabled.
7385 */
7386 int
7387gpm_enabled(void)
7388{
7389 return gpm_flag && gpm_fd >= 0;
7390}
7391
7392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 */
7395 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007396gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007398 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 Gpm_Close();
7400}
7401
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007402/*
7403 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007405 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 */
7407 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007408mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409{
7410 int button;
7411 static Gpm_Event gpm_event;
7412 char_u string[6];
7413 int_u vim_modifiers;
7414 int row,col;
7415 unsigned char buttons_mask;
7416 unsigned char gpm_modifiers;
7417 static unsigned char old_buttons = 0;
7418
7419 Gpm_GetEvent(&gpm_event);
7420
7421#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007422 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 if (hold_gui_events)
7424 return 0;
7425#endif
7426
7427 row = gpm_event.y - 1;
7428 col = gpm_event.x - 1;
7429
Bram Moolenaar0f873732019-12-05 20:28:46 +01007430 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 string[1] = 'M';
7432 string[2] = 'G';
7433 switch (GPM_BARE_EVENTS(gpm_event.type))
7434 {
7435 case GPM_DRAG:
7436 string[3] = MOUSE_DRAG;
7437 break;
7438 case GPM_DOWN:
7439 buttons_mask = gpm_event.buttons & ~old_buttons;
7440 old_buttons = gpm_event.buttons;
7441 switch (buttons_mask)
7442 {
7443 case GPM_B_LEFT:
7444 button = MOUSE_LEFT;
7445 break;
7446 case GPM_B_MIDDLE:
7447 button = MOUSE_MIDDLE;
7448 break;
7449 case GPM_B_RIGHT:
7450 button = MOUSE_RIGHT;
7451 break;
7452 default:
7453 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007454 // Don't know what to do. Can more than one button be
7455 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 }
7457 string[3] = (char_u)(button | 0x20);
7458 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7459 break;
7460 case GPM_UP:
7461 string[3] = MOUSE_RELEASE;
7462 old_buttons &= ~gpm_event.buttons;
7463 break;
7464 default:
7465 return 0;
7466 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007467 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 gpm_modifiers = gpm_event.modifiers;
7469 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007470 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7471 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7472 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7474 vim_modifiers |= MOUSE_SHIFT;
7475
7476 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7477 vim_modifiers |= MOUSE_CTRL;
7478 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7479 vim_modifiers |= MOUSE_ALT;
7480 string[3] |= vim_modifiers;
7481 string[4] = (char_u)(col + ' ' + 1);
7482 string[5] = (char_u)(row + ' ' + 1);
7483 add_to_input_buf(string, 6);
7484 return 6;
7485}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007486#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007488#ifdef FEAT_SYSMOUSE
7489/*
7490 * Initialize connection with sysmouse.
7491 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7492 * output, any sysmouse output than will be processed via sig_sysmouse().
7493 * Return OK if succeeded, FAIL if failed.
7494 */
7495 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007496sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007497{
7498 struct mouse_info mouse;
7499
7500 mouse.operation = MOUSE_MODE;
7501 mouse.u.mode.mode = 0;
7502 mouse.u.mode.signal = SIGUSR2;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007503 if (ioctl(1, CONS_MOUSECTL, &mouse) == -1)
7504 return FAIL;
7505
ichizok378447f2023-05-11 22:25:42 +01007506 mch_signal(SIGUSR2, sig_sysmouse);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007507 mouse.operation = MOUSE_SHOW;
7508 ioctl(1, CONS_MOUSECTL, &mouse);
7509 return OK;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007510}
7511
7512/*
7513 * Stop processing SIGUSR2 signals, and also make sure that
7514 * virtual console do not send us any sysmouse related signal.
7515 */
7516 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007517sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007518{
7519 struct mouse_info mouse;
7520
ichizok378447f2023-05-11 22:25:42 +01007521 mch_signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007522 mouse.operation = MOUSE_MODE;
7523 mouse.u.mode.mode = 0;
7524 mouse.u.mode.signal = 0;
7525 ioctl(1, CONS_MOUSECTL, &mouse);
7526}
7527
7528/*
7529 * Gets info from sysmouse and adds special keys to input buf.
7530 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007531 static void
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007532sig_sysmouse SIGDEFARG(sigarg)
7533{
7534 struct mouse_info mouse;
7535 struct video_info video;
7536 char_u string[6];
7537 int row, col;
7538 int button;
7539 int buttons;
7540 static int oldbuttons = 0;
7541
7542#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007543 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007544 if (hold_gui_events)
7545 return;
7546#endif
7547
7548 mouse.operation = MOUSE_GETINFO;
7549 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7550 && ioctl(1, FBIO_MODEINFO, &video) != -1
7551 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7552 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7553 {
7554 row = mouse.u.data.y / video.vi_cheight;
7555 col = mouse.u.data.x / video.vi_cwidth;
7556 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007557 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007558 string[1] = 'M';
7559 string[2] = 'S';
7560 if (oldbuttons == buttons && buttons != 0)
7561 {
7562 button = MOUSE_DRAG;
7563 }
7564 else
7565 {
7566 switch (buttons)
7567 {
7568 case 0:
7569 button = MOUSE_RELEASE;
7570 break;
7571 case 1:
7572 button = MOUSE_LEFT;
7573 break;
7574 case 2:
7575 button = MOUSE_MIDDLE;
7576 break;
7577 case 4:
7578 button = MOUSE_RIGHT;
7579 break;
7580 default:
7581 return;
7582 }
7583 oldbuttons = buttons;
7584 }
7585 string[3] = (char_u)(button);
7586 string[4] = (char_u)(col + ' ' + 1);
7587 string[5] = (char_u)(row + ' ' + 1);
7588 add_to_input_buf(string, 6);
7589 }
7590 return;
7591}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007592#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007593
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007595typedef char_u * (*STRPROCSTR)(char_u *);
7596typedef char_u * (*INTPROCSTR)(int);
7597typedef int (*STRPROCINT)(char_u *);
7598typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599
7600/*
7601 * Call a DLL routine which takes either a string or int param
7602 * and returns an allocated string.
7603 */
7604 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007605mch_libcall(
7606 char_u *libname,
7607 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007608 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007609 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007610 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007611 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612{
7613# if defined(USE_DLOPEN)
7614 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007615 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616# else
7617 shl_t hinstLib;
7618# endif
7619 STRPROCSTR ProcAdd;
7620 INTPROCSTR ProcAddI;
7621 char_u *retval_str = NULL;
7622 int retval_int = 0;
7623 int success = FALSE;
7624
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007625 /*
7626 * Get a handle to the DLL module.
7627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007629 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007630 (void)dlerror();
7631
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 hinstLib = dlopen((char *)libname, RTLD_LAZY
7633# ifdef RTLD_LOCAL
7634 | RTLD_LOCAL
7635# endif
7636 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007637 if (hinstLib == NULL)
7638 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007639 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007640 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007641 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007642 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644# else
7645 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7646# endif
7647
Bram Moolenaar0f873732019-12-05 20:28:46 +01007648 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 if (hinstLib != NULL)
7650 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007651# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 /*
7653 * Catch a crash when calling the library function. For example when
7654 * using a number where a string pointer is expected.
7655 */
7656 mch_startjmp();
7657 if (SETJMP(lc_jump_env) != 0)
7658 {
7659 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007660# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007661 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007662# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 mch_didjmp();
7664 }
7665 else
7666# endif
7667 {
7668 retval_str = NULL;
7669 retval_int = 0;
7670
7671 if (argstring != NULL)
7672 {
7673# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007674 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007675 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676# else
7677 if (shl_findsym(&hinstLib, (const char *)funcname,
7678 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7679 ProcAdd = NULL;
7680# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007681 if ((success = (ProcAdd != NULL
7682# if defined(USE_DLOPEN)
7683 && dlerr == NULL
7684# endif
7685 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 {
7687 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007688 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 else
7690 retval_str = (ProcAdd)(argstring);
7691 }
7692 }
7693 else
7694 {
7695# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007696 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007697 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698# else
7699 if (shl_findsym(&hinstLib, (const char *)funcname,
7700 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7701 ProcAddI = NULL;
7702# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007703 if ((success = (ProcAddI != NULL
7704# if defined(USE_DLOPEN)
7705 && dlerr == NULL
7706# endif
7707 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 {
7709 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007710 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 else
7712 retval_str = (ProcAddI)(argint);
7713 }
7714 }
7715
Bram Moolenaar0f873732019-12-05 20:28:46 +01007716 // Save the string before we free the library.
7717 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 if (string_result == NULL)
7719 *number_result = retval_int;
7720 else if (retval_str != NULL
7721 && retval_str != (char_u *)1
7722 && retval_str != (char_u *)-1)
7723 *string_result = vim_strsave(retval_str);
7724 }
7725
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007726# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 mch_endjmp();
7728# ifdef SIGHASARG
7729 if (lc_signal != 0)
7730 {
7731 int i;
7732
Bram Moolenaar0f873732019-12-05 20:28:46 +01007733 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 for (i = 0; signal_info[i].sig != -1; i++)
7735 if (lc_signal == signal_info[i].sig)
7736 break;
Bram Moolenaar50809a42023-05-20 16:39:07 +01007737 semsg(_(e_got_sig_str_in_libcall), signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 }
7739# endif
7740# endif
7741
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007743 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007744 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007745 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007746
Bram Moolenaar0f873732019-12-05 20:28:46 +01007747 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 (void)dlclose(hinstLib);
7749# else
7750 (void)shl_unload(hinstLib);
7751# endif
7752 }
7753
7754 if (!success)
7755 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007756 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 return FAIL;
7758 }
7759
7760 return OK;
7761}
7762#endif
7763
7764#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007765static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766static int xterm_button;
7767
7768/*
7769 * Setup a dummy window for X selections in a terminal.
7770 */
7771 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007772setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773{
7774 int z = 0;
7775 char *strp = "";
7776 Widget AppShell;
7777
7778 if (!x_connect_to_server())
7779 return;
7780
7781 open_app_context();
7782 if (app_context != NULL && xterm_Shell == (Widget)0)
7783 {
7784 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007785# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007787# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007788# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007789 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790
7791 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007792 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793# endif
7794
Bram Moolenaar0f873732019-12-05 20:28:46 +01007795 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 oldhandler = XSetErrorHandler(x_error_check);
7797
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007798# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007799 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7801 mch_startjmp();
7802 if (SETJMP(lc_jump_env) != 0)
7803 {
7804 mch_didjmp();
7805 xterm_dpy = NULL;
7806 }
7807 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 {
7810 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7811 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007812 if (xterm_dpy != NULL)
7813 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007814# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007816# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 }
7818
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007819# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007820 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007822# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007823 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 (void)XSetErrorHandler(oldhandler);
7825
7826 if (xterm_dpy == NULL)
7827 {
7828 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007829 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 return;
7831 }
7832
Bram Moolenaar0f873732019-12-05 20:28:46 +01007833 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 (void)XSetIOErrorHandler(x_IOerror_handler);
7835
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007836# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007838 {
7839 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007840 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007841 verbose_leave();
7842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843# endif
7844
Bram Moolenaar0f873732019-12-05 20:28:46 +01007845 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7847 applicationShellWidgetClass, xterm_dpy,
7848 NULL);
7849 if (AppShell == (Widget)0)
7850 return;
7851 xterm_Shell = XtVaCreatePopupShell("VIM",
7852 topLevelShellWidgetClass, AppShell,
7853 XtNmappedWhenManaged, 0,
7854 XtNwidth, 1,
7855 XtNheight, 1,
7856 NULL);
7857 if (xterm_Shell == (Widget)0)
7858 return;
7859
7860 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007861 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 if (x11_display == NULL)
7863 x11_display = xterm_dpy;
7864
7865 XtRealizeWidget(xterm_Shell);
7866 XSync(xterm_dpy, False);
7867 xterm_update();
7868 }
7869 if (xterm_Shell != (Widget)0)
7870 {
7871 clip_init(TRUE);
7872 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7873 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007874 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 if (test_x11_window(xterm_dpy) == FAIL)
7876 x11_window = 0;
7877 if (x11_window != 0)
7878 xterm_trace = 0;
7879 }
7880}
7881
7882 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007883start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884{
7885 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7886 return;
7887 xterm_trace = 1;
7888 xterm_button = button;
7889 do_xterm_trace();
7890}
7891
7892
7893 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007894stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
7896 if (xterm_trace < 0)
7897 return;
7898 xterm_trace = 0;
7899}
7900
7901/*
7902 * Query the xterm pointer and generate mouse termcodes if necessary
7903 * return TRUE if dragging is active, else FALSE
7904 */
7905 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007906do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
7908 Window root, child;
7909 int root_x, root_y;
7910 int win_x, win_y;
7911 int row, col;
7912 int_u mask_return;
7913 char_u buf[50];
7914 char_u *strp;
7915 long got_hints;
7916 static char_u *mouse_code;
7917 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7918 static int prev_row = 0, prev_col = 0;
7919 static XSizeHints xterm_hints;
7920
7921 if (xterm_trace <= 0)
7922 return FALSE;
7923
7924 if (xterm_trace == 1)
7925 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007926 // Get the hints just before tracking starts. The font size might
7927 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007928 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7929 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 || xterm_hints.width_inc <= 1
7931 || xterm_hints.height_inc <= 1)
7932 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007933 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 return FALSE;
7935 }
7936
Bram Moolenaar0f873732019-12-05 20:28:46 +01007937 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 mouse_code = find_termcode(mouse_name);
7939 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007940 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 xterm_trace = 2;
7942
Bram Moolenaar0f873732019-12-05 20:28:46 +01007943 // Find the offset of the chars, there might be a scrollbar on the
7944 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7946 &win_x, &win_y, &mask_return);
7947 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7948 - (xterm_hints.height_inc / 2);
7949 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7950 xterm_hints.y = 2;
7951 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7952 - (xterm_hints.width_inc / 2);
7953 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7954 xterm_hints.x = 2;
7955 return TRUE;
7956 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007957 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 {
7959 xterm_trace = 0;
7960 return FALSE;
7961 }
7962
7963 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7964 &win_x, &win_y, &mask_return);
7965
7966 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7967 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7968 if (row == prev_row && col == prev_col)
7969 return TRUE;
7970
7971 STRCPY(buf, mouse_code);
7972 strp = buf + STRLEN(buf);
7973 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7974 *strp++ = (char_u)(col + ' ' + 1);
7975 *strp++ = (char_u)(row + ' ' + 1);
7976 *strp = 0;
7977 add_to_input_buf(buf, STRLEN(buf));
7978
7979 prev_row = row;
7980 prev_col = col;
7981 return TRUE;
7982}
7983
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007984# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985/*
7986 * Destroy the display, window and app_context. Required for GTK.
7987 */
7988 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007989clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990{
7991 if (xterm_Shell != (Widget)0)
7992 {
7993 XtDestroyWidget(xterm_Shell);
7994 xterm_Shell = (Widget)0;
7995 }
7996 if (xterm_dpy != NULL)
7997 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007998# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007999 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00008001# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 if (x11_display == xterm_dpy)
8003 x11_display = NULL;
8004 xterm_dpy = NULL;
8005 }
Bram Moolenaare8208012008-06-20 09:59:25 +00008006# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 if (app_context != (XtAppContext)NULL)
8008 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008009 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 XtDestroyApplicationContext(app_context);
8011 app_context = (XtAppContext)NULL;
8012 }
Bram Moolenaare8208012008-06-20 09:59:25 +00008013# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014}
8015# endif
8016
8017/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01008018 * Catch up with GUI or X events.
8019 */
8020 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008021clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01008022{
8023# ifdef FEAT_GUI
8024 if (gui.in_use)
8025 gui_mch_update();
8026 else
8027# endif
8028 if (xterm_Shell != (Widget)0)
8029 xterm_update();
8030}
8031
8032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 * Catch up with any queued X events. This may put keyboard input into the
8034 * input buffer, call resize call-backs, trigger timers etc. If there is
8035 * nothing in the X event queue (& no timers pending), then we return
8036 * immediately.
8037 */
8038 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008039xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
8041 XEvent event;
8042
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008043 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008045 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008047 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008048 break;
8049
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008050 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008051 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008052 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008053 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008054#ifdef FEAT_CLIENTSERVER
8055 {
8056 XPropertyEvent *e = (XPropertyEvent *)&event;
8057
8058 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008060 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008063 XtDispatchEvent(&event);
8064 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008065 else
8066 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008067 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008068 XtAppProcessEvent(app_context, mask);
8069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 }
8071}
8072
8073 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008074clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075{
8076 if (xterm_Shell != (Widget)0)
8077 return clip_x11_own_selection(xterm_Shell, cbd);
8078 return FAIL;
8079}
8080
8081 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008082clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
8084 if (xterm_Shell != (Widget)0)
8085 clip_x11_lose_selection(xterm_Shell, cbd);
8086}
8087
8088 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008089clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090{
8091 if (xterm_Shell != (Widget)0)
8092 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
8093}
8094
8095 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008096clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097{
8098 clip_x11_set_selection(cbd);
8099}
8100#endif
8101
8102
8103#if defined(USE_XSMP) || defined(PROTO)
8104/*
8105 * Code for X Session Management Protocol.
8106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107
8108# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109/*
8110 * This is our chance to ask the user if they want to save,
8111 * or abort the logout
8112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008114xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115{
Bram Moolenaare1004402020-10-24 20:49:43 +02008116 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 int cancel_shutdown = False;
8118
Bram Moolenaare1004402020-10-24 20:49:43 +02008119 save_cmod_flags = cmdmod.cmod_flags;
8120 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01008121 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01008122 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02008124 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01008125 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 out_flush();
8127
Bram Moolenaar0f873732019-12-05 20:28:46 +01008128 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 SmcInteractDone(smc_conn, cancel_shutdown);
8130
Bram Moolenaar0f873732019-12-05 20:28:46 +01008131 // Finish off
8132 // Only end save-yourself here if we're not cancelling shutdown;
8133 // we'll get a cancelled callback later in which we'll end it.
8134 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 if (!cancel_shutdown)
8136 {
8137 xsmp.save_yourself = False;
8138 SmcSaveYourselfDone(smc_conn, True);
8139 }
8140}
8141# endif
8142
8143/*
8144 * Callback that starts save-yourself.
8145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008147xsmp_handle_save_yourself(
8148 SmcConn smc_conn,
8149 SmPointer client_data UNUSED,
8150 int save_type UNUSED,
8151 Bool shutdown,
8152 int interact_style UNUSED,
8153 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008155 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 if (xsmp.save_yourself)
8157 SmcSaveYourselfDone(smc_conn, True);
8158 xsmp.save_yourself = True;
8159 xsmp.shutdown = shutdown;
8160
Bram Moolenaar0f873732019-12-05 20:28:46 +01008161 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01008163 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164
8165 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008166 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167
8168# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008169 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008171 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 SmcInteractRequest(smc_conn, SmDialogError,
8173 xsmp_handle_interaction, client_data);
8174 else
8175# endif
8176 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008177 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 SmcSaveYourselfDone(smc_conn, True);
8179 xsmp.save_yourself = False;
8180 }
8181}
8182
8183
8184/*
8185 * Callback to warn us of imminent death.
8186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008188xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189{
8190 xsmp_close();
8191
Bram Moolenaar0f873732019-12-05 20:28:46 +01008192 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 getout_preserve_modified(0);
8194}
8195
8196
8197/*
8198 * Callback to tell us that save-yourself has completed.
8199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008201xsmp_save_complete(
8202 SmcConn smc_conn UNUSED,
8203 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204{
8205 xsmp.save_yourself = False;
8206}
8207
8208
8209/*
8210 * Callback to tell us that an instigated shutdown was cancelled
8211 * (maybe even by us)
8212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008214xsmp_shutdown_cancelled(
8215 SmcConn smc_conn,
8216 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217{
8218 if (xsmp.save_yourself)
8219 SmcSaveYourselfDone(smc_conn, True);
8220 xsmp.save_yourself = False;
8221 xsmp.shutdown = False;
8222}
8223
8224
8225/*
8226 * Callback to tell us that a new ICE connection has been established.
8227 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008229xsmp_ice_connection(
8230 IceConn iceConn,
8231 IcePointer clientData UNUSED,
8232 Bool opening,
8233 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008235 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 if (opening)
8237 {
8238 xsmp_icefd = IceConnectionNumber(iceConn);
8239 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8240 }
8241}
8242
8243
Bram Moolenaar0f873732019-12-05 20:28:46 +01008244// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008246xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247{
8248 Bool rep;
8249
8250 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8251 == IceProcessMessagesIOError)
8252 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008253 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008255 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 xsmp_close();
8257 return FAIL;
8258 }
8259 else
8260 return OK;
8261}
8262
8263static int dummy;
8264
Bram Moolenaar0f873732019-12-05 20:28:46 +01008265// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 void
8267xsmp_init(void)
8268{
8269 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 SmcCallbacks smcallbacks;
8271#if 0
8272 SmPropValue smname;
8273 SmProp smnameprop;
8274 SmProp *smprops[1];
8275#endif
8276
8277 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008278 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279
8280 xsmp.save_yourself = xsmp.shutdown = False;
8281
Bram Moolenaar0f873732019-12-05 20:28:46 +01008282 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8284 smcallbacks.save_yourself.client_data = NULL;
8285 smcallbacks.die.callback = xsmp_die;
8286 smcallbacks.die.client_data = NULL;
8287 smcallbacks.save_complete.callback = xsmp_save_complete;
8288 smcallbacks.save_complete.client_data = NULL;
8289 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8290 smcallbacks.shutdown_cancelled.client_data = NULL;
8291
Bram Moolenaar0f873732019-12-05 20:28:46 +01008292 // Set up a watch on ICE connection creations. The "dummy" argument is
8293 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8295 {
8296 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008297 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 return;
8299 }
8300
Bram Moolenaar0f873732019-12-05 20:28:46 +01008301 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 xsmp.smcconn = SmcOpenConnection(
8303 NULL,
8304 NULL,
8305 SmProtoMajor,
8306 SmProtoMinor,
8307 SmcSaveYourselfProcMask | SmcDieProcMask
8308 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8309 &smcallbacks,
8310 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008311 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008312 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 errorstring);
8314 if (xsmp.smcconn == NULL)
8315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008317 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008318 char errorreport[132];
8319
8320 // If the message is too long it might not be NUL terminated. Add
8321 // a NUL at the end to make sure we don't go over the end.
8322 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008323 vim_snprintf(errorreport, sizeof(errorreport),
8324 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008325 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 return;
8328 }
8329 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8330
8331#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008332 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 smname.value = "vim";
8334 smname.length = 3;
8335 smnameprop.name = "SmProgram";
8336 smnameprop.type = "SmARRAY8";
8337 smnameprop.num_vals = 1;
8338 smnameprop.vals = &smname;
8339
8340 smprops[0] = &smnameprop;
8341 SmcSetProperties(xsmp.smcconn, 1, smprops);
8342#endif
8343}
8344
8345
Bram Moolenaar0f873732019-12-05 20:28:46 +01008346// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008348xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008350 if (xsmp_icefd == -1)
8351 return;
8352
8353 SmcCloseConnection(xsmp.smcconn, 0, NULL);
8354 if (xsmp.clientid != NULL)
8355 free(xsmp.clientid);
8356 xsmp.clientid = NULL;
8357 xsmp_icefd = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008359#endif // USE_XSMP
Paul Ollis65745772022-06-05 16:55:54 +01008360
8361#if defined(FEAT_RELTIME) || defined(PROTO)
Bram Moolenaar08210f82023-04-13 19:15:54 +01008362# if defined(PROF_NSEC) || defined(PROTO)
Paul Ollis65745772022-06-05 16:55:54 +01008363/*
8364 * Implement timeout with timer_create() and timer_settime().
8365 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008366static volatile sig_atomic_t timeout_flag = FALSE;
8367static timer_t timer_id;
8368static int timer_created = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008369
8370/*
8371 * Callback for when the timer expires.
8372 */
8373 static void
8374set_flag(union sigval _unused UNUSED)
8375{
8376 timeout_flag = TRUE;
8377}
8378
8379/*
8380 * Stop any active timeout.
8381 */
8382 void
8383stop_timeout(void)
8384{
8385 static struct itimerspec disarm = {{0, 0}, {0, 0}};
8386
8387 if (timer_created)
8388 {
8389 int ret = timer_settime(timer_id, 0, &disarm, NULL);
8390
8391 if (ret < 0)
8392 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8393 }
8394
8395 // Clear the current timeout flag; any previous timeout should be
8396 // considered _not_ triggered.
8397 timeout_flag = FALSE;
8398}
8399
8400/*
8401 * Start the timeout timer.
8402 *
8403 * The return value is a pointer to a flag that is initialised to FALSE. If the
8404 * timeout expires, the flag is set to TRUE. This will only return pointers to
8405 * static memory; i.e. any pointer returned by this function may always be
8406 * safely dereferenced.
8407 *
8408 * This function is not expected to fail, but if it does it will still return a
8409 * valid flag pointer; the flag will remain stuck as FALSE .
8410 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008411 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008412start_timeout(long msec)
8413{
8414 struct itimerspec interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008415 {0, 0}, // Do not repeat.
8416 {msec / 1000, (msec % 1000) * 1000000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008417 int ret;
8418
8419 // This is really the caller's responsibility, but let's make sure the
8420 // previous timer has been stopped.
8421 stop_timeout();
Paul Ollis65745772022-06-05 16:55:54 +01008422
8423 if (!timer_created)
8424 {
8425 struct sigevent action = {0};
8426
8427 action.sigev_notify = SIGEV_THREAD;
8428 action.sigev_notify_function = set_flag;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008429 ret = timer_create(CLOCK_MONOTONIC, &action, &timer_id);
8430 if (ret < 0)
Paul Ollis65745772022-06-05 16:55:54 +01008431 {
8432 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8433 return &timeout_flag;
8434 }
8435 timer_created = TRUE;
8436 }
8437
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00008438# ifdef FEAT_EVAL
Bram Moolenaar616592e2022-06-17 15:17:10 +01008439 ch_log(NULL, "setting timeout timer to %d sec %ld nsec",
8440 (int)interval.it_value.tv_sec, (long)interval.it_value.tv_nsec);
Bram Moolenaar509ce032022-06-20 11:23:01 +01008441# endif
Paul Ollis65745772022-06-05 16:55:54 +01008442 ret = timer_settime(timer_id, 0, &interval, NULL);
8443 if (ret < 0)
8444 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8445
8446 return &timeout_flag;
8447}
8448
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008449/*
8450 * To be used before fork/exec: delete any created timer.
8451 */
8452 void
8453delete_timer(void)
8454{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008455 if (!timer_created)
8456 return;
8457
8458 timer_delete(timer_id);
8459 timer_created = FALSE;
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008460}
8461
Bram Moolenaar08210f82023-04-13 19:15:54 +01008462# else // PROF_NSEC
Paul Ollis65745772022-06-05 16:55:54 +01008463
8464/*
8465 * Implement timeout with setitimer()
8466 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008467static struct sigaction prev_sigaction;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008468static volatile sig_atomic_t timeout_flag = FALSE;
8469static int timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008470static int timer_handler_active = FALSE;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008471static volatile sig_atomic_t alarm_pending = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008472
8473/*
8474 * Handle SIGALRM for a timeout.
8475 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01008476 static void
Paul Ollis65745772022-06-05 16:55:54 +01008477set_flag SIGDEFARG(sigarg)
8478{
8479 if (alarm_pending)
8480 alarm_pending = FALSE;
8481 else
8482 timeout_flag = TRUE;
8483}
8484
8485/*
8486 * Stop any active timeout.
8487 */
8488 void
8489stop_timeout(void)
8490{
8491 static struct itimerval disarm = {{0, 0}, {0, 0}};
8492 int ret;
8493
8494 if (timer_active)
8495 {
8496 timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008497 ret = setitimer(ITIMER_REAL, &disarm, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008498 if (ret < 0)
8499 // Should only get here as a result of coding errors.
8500 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8501 }
8502
8503 if (timer_handler_active)
8504 {
8505 timer_handler_active = FALSE;
8506 ret = sigaction(SIGALRM, &prev_sigaction, NULL);
8507 if (ret < 0)
8508 // Should only get here as a result of coding errors.
8509 semsg(_(e_could_not_reset_handler_for_timeout_str),
8510 strerror(errno));
8511 }
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008512 timeout_flag = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008513}
8514
8515/*
8516 * Start the timeout timer.
8517 *
8518 * The return value is a pointer to a flag that is initialised to FALSE. If the
8519 * timeout expires, the flag is set to TRUE. This will only return pointers to
8520 * static memory; i.e. any pointer returned by this function may always be
8521 * safely dereferenced.
8522 *
8523 * This function is not expected to fail, but if it does it will still return a
8524 * valid flag pointer; the flag will remain stuck as FALSE .
8525 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008526 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008527start_timeout(long msec)
8528{
8529 struct itimerval interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008530 {0, 0}, // Do not repeat.
8531 {msec / 1000, (msec % 1000) * 1000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008532 struct sigaction handle_alarm;
8533 int ret;
8534 sigset_t sigs;
8535 sigset_t saved_sigs;
8536
8537 // This is really the caller's responsibility, but let's make sure the
8538 // previous timer has been stopped.
8539 stop_timeout();
8540
8541 // There is a small chance that SIGALRM is pending and so the handler must
8542 // ignore it on the first call.
8543 alarm_pending = FALSE;
8544 ret = sigemptyset(&sigs);
8545 ret = ret == 0 ? sigaddset(&sigs, SIGALRM) : ret;
8546 ret = ret == 0 ? sigprocmask(SIG_BLOCK, &sigs, &saved_sigs) : ret;
8547 timeout_flag = FALSE;
8548 ret = ret == 0 ? sigpending(&sigs) : ret;
8549 if (ret == 0)
8550 {
8551 alarm_pending = sigismember(&sigs, SIGALRM);
Bram Moolenaar1f89abf2022-06-06 10:07:01 +01008552 ret = sigprocmask(SIG_SETMASK, &saved_sigs, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008553 }
8554 if (unlikely(ret != 0 || alarm_pending < 0))
8555 {
8556 // Just catching coding errors. Write an error message, but carry on.
8557 semsg(_(e_could_not_check_for_pending_sigalrm_str), strerror(errno));
8558 alarm_pending = FALSE;
8559 }
8560
8561 // Set up the alarm handler first.
8562 ret = sigemptyset(&handle_alarm.sa_mask);
8563 handle_alarm.sa_handler = set_flag;
8564 handle_alarm.sa_flags = 0;
8565 ret = ret == 0 ? sigaction(SIGALRM, &handle_alarm, &prev_sigaction) : ret;
8566 if (ret < 0)
8567 {
8568 // Should only get here as a result of coding errors.
8569 semsg(_(e_could_not_set_handler_for_timeout_str), strerror(errno));
8570 return &timeout_flag;
8571 }
8572 timer_handler_active = TRUE;
8573
8574 // Set up the interval timer once the alarm handler is in place.
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008575 ret = setitimer(ITIMER_REAL, &interval, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008576 if (ret < 0)
8577 {
8578 // Should only get here as a result of coding errors.
8579 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8580 stop_timeout();
8581 return &timeout_flag;
8582 }
8583
8584 timer_active = TRUE;
8585 return &timeout_flag;
8586}
Bram Moolenaar08210f82023-04-13 19:15:54 +01008587# endif // PROF_NSEC
Paul Ollis65745772022-06-05 16:55:54 +01008588#endif // FEAT_RELTIME