blob: 80ca0ce26de2acffa29974a71b93f21f5da7e1e6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * OS/2 port by Paul Slootman
5 * VMS merge by Zoltan Arpadffy
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code.
10 */
11
12/*
13 * os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
14 * Also for OS/2, using the excellent EMX package!!!
Bram Moolenaar041c7102020-05-30 18:14:57 +020015 * Also for Atari MiNT.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016 *
17 * A lot of this file was originally written by Juergen Weigert and later
18 * changed beyond recognition.
19 */
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#include "vim.h"
22
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023#ifdef FEAT_MZSCHEME
24# include "if_mzsch.h"
25#endif
26
Bram Moolenaar0f873732019-12-05 20:28:46 +010027#include "os_unixx.h" // unix includes for os_unix.c only
Bram Moolenaar071d4272004-06-13 20:20:40 +000028
29#ifdef USE_XSMP
30# include <X11/SM/SMlib.h>
31#endif
32
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000033#ifdef HAVE_SELINUX
34# include <selinux/selinux.h>
35static int selinux_enabled = -1;
36#endif
37
Christian Brabandte085dfd2023-09-30 12:49:18 +020038#ifdef FEAT_XATTR
zeertzjq6de4e582023-09-30 14:19:14 +020039# include <sys/xattr.h>
Christian Brabandte085dfd2023-09-30 12:49:18 +020040#endif
41
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020042#ifdef HAVE_SMACK
zeertzjq6de4e582023-09-30 14:19:14 +020043# include <sys/xattr.h>
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020044# include <linux/xattr.h>
45# ifndef SMACK_LABEL_LEN
46# define SMACK_LABEL_LEN 1024
47# endif
48#endif
49
Bram Moolenaara2442432007-04-26 14:26:37 +000050#ifdef __CYGWIN__
K.Takata972db232022-02-04 10:45:38 +000051# include <cygwin/version.h>
52# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
Bram Moolenaar0f873732019-12-05 20:28:46 +010053 // for cygwin_conv_path()
K.Takata972db232022-02-04 10:45:38 +000054# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
55# define WIN32_LEAN_AND_MEAN
56# include <windows.h>
57# include "winclip.pro"
Bram Moolenaara2442432007-04-26 14:26:37 +000058# endif
59#endif
60
Bram Moolenaar071d4272004-06-13 20:20:40 +000061#ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000062
Bram Moolenaar071d4272004-06-13 20:20:40 +000063# include <gpm.h>
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000064
65# ifdef DYNAMIC_GPM
66# define Gpm_Open (*dll_Gpm_Open)
67# define Gpm_Close (*dll_Gpm_Close)
68# define Gpm_GetEvent (*dll_Gpm_GetEvent)
69# define gpm_flag (dll_gpm_flag != NULL ? *dll_gpm_flag : 0)
70# define gpm_fd (dll_gpm_fd != NULL ? *dll_gpm_fd : -1)
71
72static int (*dll_Gpm_Open) (Gpm_Connect *, int);
73static int (*dll_Gpm_Close) (void);
74static int (*dll_Gpm_GetEvent) (Gpm_Event *);
75static int *dll_gpm_flag;
76static int *dll_gpm_fd;
77
78static void *libgpm_hinst;
79# endif
80
Bram Moolenaar0f873732019-12-05 20:28:46 +010081// <linux/keyboard.h> contains defines conflicting with "keymap.h",
82// I just copied relevant defines here. A cleaner solution would be to put gpm
83// code into separate file and include there linux/keyboard.h
84// #include <linux/keyboard.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000085# define KG_SHIFT 0
86# define KG_CTRL 2
87# define KG_ALT 3
88# define KG_ALTGR 1
89# define KG_SHIFTL 4
90# define KG_SHIFTR 5
91# define KG_CTRLL 6
92# define KG_CTRLR 7
93# define KG_CAPSSHIFT 8
94
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010095static void gpm_close(void);
96static int gpm_open(void);
97static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098#endif
99
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000100#ifdef FEAT_SYSMOUSE
101# include <sys/consio.h>
102# include <sys/fbio.h>
103
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100104static int sysmouse_open(void);
105static void sysmouse_close(void);
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100106static void sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000107#endif
108
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109/*
110 * end of autoconf section. To be extended...
111 */
112
Bram Moolenaar0f873732019-12-05 20:28:46 +0100113// Are the following #ifdefs still required? And why? Is that for X11?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114
115#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
116# ifdef SIGWINCH
117# undef SIGWINCH
118# endif
119# ifdef TIOCGWINSZ
120# undef TIOCGWINSZ
121# endif
122#endif
123
Bram Moolenaar0f873732019-12-05 20:28:46 +0100124#if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125# define SIGWINCH SIGWINDOW
126#endif
127
128#ifdef FEAT_X11
129# include <X11/Xlib.h>
130# include <X11/Xutil.h>
131# include <X11/Xatom.h>
132# ifdef FEAT_XCLIPBOARD
133# include <X11/Intrinsic.h>
134# include <X11/Shell.h>
135# include <X11/StringDefs.h>
136static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void clip_update(void);
138static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139# endif
140
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141Window x11_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143#endif
144
Bram Moolenaar5c3128e2020-05-11 20:54:42 +0200145static int ignore_sigtstp = FALSE;
146
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
149static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200150static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +0200151static int unix_did_set_title = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152static char_u *oldicon = NULL;
153static int did_set_icon = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156
Bram Moolenaar205b8862011-09-07 15:04:31 +0200157#ifdef HAVE_UNION_WAIT
158typedef union wait waitstatus;
159#else
160typedef int waitstatus;
161#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200162static int WaitForChar(long msec, int *interrupted, int ignore_input);
163static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar041c7102020-05-30 18:14:57 +0200164#ifdef VMS
Bram Moolenaarcda77642016-06-04 13:32:35 +0200165int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200167static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169
170#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int do_xterm_trace(void);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100172# define XT_TRACE_DELAY 50 // delay for xterm tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#endif
174
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100175static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
177#if defined(SIGWINCH)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100178static void sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000180#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100181static void sig_tstp SIGPROTOARG;
Bram Moolenaarabd56da2022-06-23 20:46:27 +0100182// volatile because it is used in signal handler sig_tstp() and
183// sigcont_handler().
dbivolaruab16ad32021-12-29 19:41:47 +0000184static volatile sig_atomic_t in_mch_suspend = FALSE;
185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186#if defined(SIGINT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100187static void catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200189#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100190static void catch_sigusr1 SIGPROTOARG;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100193static void catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194#endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000195#if defined(SIGALRM) && defined(FEAT_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196# define SET_SIG_ALARM
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100197static void sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100198// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200199static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200#endif
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100201static void deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100203static void catch_int_signal(void);
204static void set_signals(void);
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100205static void catch_signals(void (*func_deadly)(int), void (*func_other)(int));
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200206#ifdef HAVE_SIGPROCMASK
207# define SIGSET_DECL(set) sigset_t set;
208# define BLOCK_SIGNALS(set) block_signals(set)
209# define UNBLOCK_SIGNALS(set) unblock_signals(set)
210#else
211# define SIGSET_DECL(set)
212# define BLOCK_SIGNALS(set) do { /**/ } while (0)
213# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
214#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100215static int have_wildcard(int, char_u **);
216static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100218static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
220#ifndef SIG_ERR
ichizok378447f2023-05-11 22:25:42 +0100221# define SIG_ERR ((sighandler_T)-1)
222#endif
223#ifndef SIG_HOLD
224# define SIG_HOLD ((sighandler_T)-2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#endif
226
Bram Moolenaar0f873732019-12-05 20:28:46 +0100227// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200228static volatile sig_atomic_t do_resize = FALSE;
dbivolaruab16ad32021-12-29 19:41:47 +0000229// volatile because it is used in signal handler sig_tstp().
230static volatile sig_atomic_t got_tstp = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231static char_u *extra_shell_arg = NULL;
232static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100233// volatile because it is used in signal handler deathtrap().
234static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
235// volatile because it is used in signal handler deathtrap().
236static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100238#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
239static int dont_check_job_ended = 0;
240#endif
241
Bram Moolenaar26e86442020-05-17 14:06:16 +0200242// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
243static tmode_T mch_cur_tmode = TMODE_COOK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244
245#ifdef USE_XSMP
246typedef struct
247{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100248 SmcConn smcconn; // The SM connection ID
249 IceConn iceconn; // The ICE connection ID
250 char *clientid; // The client ID for the current smc session
251 Bool save_yourself; // If we're in the middle of a save_yourself
252 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253} xsmp_config_T;
254
255static xsmp_config_T xsmp;
256#endif
257
258#ifdef SYS_SIGLIST_DECLARED
259/*
260 * I have seen
261 * extern char *_sys_siglist[NSIG];
Yegappan Lakshmananaebc6ef2022-08-27 21:24:26 +0100262 * on Linux, NetBSD and Solaris. It contains a nice list of strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 * that describe the signals. That is nearly what we want here. But
264 * autoconf does only check for sys_siglist (without the underscore), I
265 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100266 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 */
268#endif
269
270static struct signalinfo
271{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100272 int sig; // Signal number, eg. SIGSEGV etc
273 char *name; // Signal name (not char_u!).
274 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275} signal_info[] =
276{
277#ifdef SIGHUP
278 {SIGHUP, "HUP", TRUE},
279#endif
280#ifdef SIGQUIT
281 {SIGQUIT, "QUIT", TRUE},
282#endif
283#ifdef SIGILL
284 {SIGILL, "ILL", TRUE},
285#endif
286#ifdef SIGTRAP
287 {SIGTRAP, "TRAP", TRUE},
288#endif
289#ifdef SIGABRT
290 {SIGABRT, "ABRT", TRUE},
291#endif
292#ifdef SIGEMT
293 {SIGEMT, "EMT", TRUE},
294#endif
295#ifdef SIGFPE
296 {SIGFPE, "FPE", TRUE},
297#endif
298#ifdef SIGBUS
299 {SIGBUS, "BUS", TRUE},
300#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100301#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100302 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 {SIGSEGV, "SEGV", TRUE},
304#endif
305#ifdef SIGSYS
306 {SIGSYS, "SYS", TRUE},
307#endif
308#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100309 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#endif
311#ifdef SIGTERM
312 {SIGTERM, "TERM", TRUE},
313#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100314#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 {SIGVTALRM, "VTALRM", TRUE},
316#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000317#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100318 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
319 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 {SIGPROF, "PROF", TRUE},
321#endif
322#ifdef SIGXCPU
323 {SIGXCPU, "XCPU", TRUE},
324#endif
325#ifdef SIGXFSZ
326 {SIGXFSZ, "XFSZ", TRUE},
327#endif
328#ifdef SIGUSR1
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200329 {SIGUSR1, "USR1", FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000331#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100332 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 {SIGUSR2, "USR2", TRUE},
334#endif
335#ifdef SIGINT
336 {SIGINT, "INT", FALSE},
337#endif
338#ifdef SIGWINCH
339 {SIGWINCH, "WINCH", FALSE},
340#endif
341#ifdef SIGTSTP
342 {SIGTSTP, "TSTP", FALSE},
343#endif
344#ifdef SIGPIPE
345 {SIGPIPE, "PIPE", FALSE},
346#endif
347 {-1, "Unknown!", FALSE}
348};
349
ichizok378447f2023-05-11 22:25:42 +0100350 sighandler_T
351mch_signal(int sig, sighandler_T func)
352{
353#if defined(HAVE_SIGACTION) && defined(HAVE_SIGPROCMASK)
354 // Modern implementation: use sigaction().
355 struct sigaction sa, old;
356 sigset_t curset;
357 int blocked;
358
359 if (sigprocmask(SIG_BLOCK, NULL, &curset) == -1)
360 return SIG_ERR;
361
362 blocked = sigismember(&curset, sig);
363
364 if (func == SIG_HOLD)
365 {
366 if (blocked)
367 return SIG_HOLD;
368
369 sigemptyset(&curset);
370 sigaddset(&curset, sig);
371
372 if (sigaction(sig, NULL, &old) == -1
373 || sigprocmask(SIG_BLOCK, &curset, NULL) == -1)
374 return SIG_ERR;
375 return old.sa_handler;
376 }
377
378 if (blocked)
379 {
380 sigemptyset(&curset);
381 sigaddset(&curset, sig);
382
383 if (sigprocmask(SIG_UNBLOCK, &curset, NULL) == -1)
384 return SIG_ERR;
385 }
386
387 sa.sa_handler = func;
388 sigemptyset(&sa.sa_mask);
389# ifdef SA_RESTART
390 sa.sa_flags = SA_RESTART;
391# else
392 sa.sa_flags = 0;
393# endif
394 if (sigaction(sig, &sa, &old) == -1)
395 return SIG_ERR;
396 return blocked ? SIG_HOLD: old.sa_handler;
397#elif defined(HAVE_SIGSET)
398 // Using sigset() is preferred above signal().
399 return sigset(sig, func);
400#else
401 // Oldest and most compatible solution.
402 return signal(sig, func);
403#endif
404}
405
Bram Moolenaar25724922009-07-14 15:38:41 +0000406 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100407mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000408{
409 if (p_verbose >= 5)
410 {
411 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100412 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000413 verbose_leave();
414 }
ichizok378447f2023-05-11 22:25:42 +0100415#ifdef VMS
Bram Moolenaar25724922009-07-14 15:38:41 +0000416 return chdir(vms_fixfilename(path));
ichizok378447f2023-05-11 22:25:42 +0100417#else
Bram Moolenaar25724922009-07-14 15:38:41 +0000418 return chdir(path);
ichizok378447f2023-05-11 22:25:42 +0100419#endif
Bram Moolenaar25724922009-07-14 15:38:41 +0000420}
421
Bram Moolenaar0f873732019-12-05 20:28:46 +0100422// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100423#if defined(ECHOE) && defined(ICANON) \
424 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
425 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200426# define NEW_TTY_SYSTEM
427#endif
428
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000429/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100430 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100433mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200435 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100436 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100437 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438}
439
440/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100441 * Function passed to inchar_loop() to handle window resizing.
442 * If "check_only" is TRUE: Return whether there was a resize.
443 * If "check_only" is FALSE: Deal with the window resized.
444 */
445 static int
446resize_func(int check_only)
447{
448 if (check_only)
449 return do_resize;
450 while (do_resize)
Bram Moolenaar55f1b822023-06-21 13:42:48 +0100451 {
Bram Moolenaar545c8a52023-06-21 15:51:47 +0100452#ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +0100453 ch_log(NULL, "calling handle_resize() in resize_func()");
Bram Moolenaar545c8a52023-06-21 15:51:47 +0100454#endif
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100455 handle_resize();
Bram Moolenaar55f1b822023-06-21 13:42:48 +0100456 }
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100457 return FALSE;
458}
459
460/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000461 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 * Get a characters from the keyboard.
463 * Return the number of characters that are available.
464 * If wtime == 0 do not wait for characters.
465 * If wtime == n wait a short time for characters.
466 * If wtime == -1 wait forever for characters.
467 */
468 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100469mch_inchar(
470 char_u *buf,
471 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100472 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100473 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100475 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
476 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477}
478
479 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100480handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481{
482 do_resize = FALSE;
483 shell_resized();
484}
485
486/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200487 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 */
489 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100490mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200492 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493}
494
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200495#if defined(FEAT_TERMINAL) || defined(PROTO)
496/*
497 * Check for any pending input or messages.
498 */
499 int
500mch_check_messages(void)
501{
502 return WaitForChar(0L, NULL, TRUE);
503}
504#endif
505
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
507# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000508# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509# endif
510# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
511# include <sys/sysctl.h>
512# endif
513# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
514# include <sys/sysinfo.h>
515# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100516# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100517# include <mach/mach_host.h>
518# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100519# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520
521/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000522 * Return total amount of memory available in Kbyte.
523 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100526mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100529 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530
Bram Moolenaar362dc332018-03-05 21:59:37 +0100531# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100532 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100533 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100534 mach_port_t host = mach_host_self();
535 kern_return_t kret;
536# ifdef HOST_VM_INFO64
537 struct vm_statistics64 vm_stat;
538 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
Bram Moolenaar988615f2018-02-27 17:58:20 +0100540 kret = host_statistics64(host, HOST_VM_INFO64,
541 (host_info64_t)&vm_stat, &count);
542# else
543 struct vm_statistics vm_stat;
544 natural_t count = HOST_VM_INFO_COUNT;
545
546 kret = host_statistics(host, HOST_VM_INFO,
547 (host_info_t)&vm_stat, &count);
548# endif
549 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100550 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100551 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
552 + vm_stat.inactive_count
553# ifdef MAC_OS_X_VERSION_10_9
554 + vm_stat.compressor_page_count
555# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100556 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100557 mach_port_deallocate(mach_task_self(), host);
558 }
559# endif
560
561# ifdef HAVE_SYSCTL
562 if (mem == 0)
563 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100564 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100565 int mib[2];
566 size_t len = sizeof(long_u);
567# ifdef HW_USERMEM64
568 long_u physmem;
569# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100570 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100571 union {
572 int_u u32;
573 long_u u64;
574 } physmem;
575# endif
576
577 mib[0] = CTL_HW;
578# ifdef HW_USERMEM64
579 mib[1] = HW_USERMEM64;
580# else
581 mib[1] = HW_USERMEM;
582# endif
583 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
584 {
585# ifdef HW_USERMEM64
586 mem = (long_u)physmem;
587# else
588 if (len == sizeof(physmem.u64))
589 mem = (long_u)physmem.u64;
590 else
591 mem = (long_u)physmem.u32;
592# endif
593 }
594 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200595# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200597# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (mem == 0)
599 {
600 struct sysinfo sinfo;
601
Bram Moolenaar0f873732019-12-05 20:28:46 +0100602 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000604 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200605# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100606 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000607 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
608 {
609 sinfo.mem_unit = sinfo.mem_unit >> 1;
610 --shiftright;
611 }
612 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200613# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200615# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200620# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 if (mem == 0)
622 {
623 long pagesize, pagecount;
624
Bram Moolenaar0f873732019-12-05 20:28:46 +0100625 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 pagesize = sysconf(_SC_PAGESIZE);
627 pagecount = sysconf(_SC_PHYS_PAGES);
628 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000629 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100630 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000631 while (shiftright > 0 && (pagesize & 1) == 0)
632 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000633 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000634 --shiftright;
635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
Bram Moolenaar0f873732019-12-05 20:28:46 +0100641 // Return the minimum of the physical memory and the user limit, because
642 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200643# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {
645 struct rlimit rlp;
646
647 if (getrlimit(RLIMIT_DATA, &rlp) == 0
648 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200649# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200651# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000652 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000654 {
655 mem = (long_u)rlp.rlim_cur;
656 shiftright = 10;
657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200659# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000662 return mem >> shiftright;
663 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664}
665#endif
666
Bram Moolenaar0981c872020-08-23 14:28:37 +0200667/*
668 * "flags": MCH_DELAY_IGNOREINPUT - don't read input
669 * MCH_DELAY_SETTMODE - use settmode() even for short delays
670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200672mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200674 tmode_T old_tmode;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200675 int call_settmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000676#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100677 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
Bram Moolenaar0981c872020-08-23 14:28:37 +0200680 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100682 // Go to cooked mode without echo, to allow SIGINT interrupting us
683 // here. But we don't want QUIT to kill us (CTRL-\ used in a
684 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200685 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000686 in_mch_delay = TRUE;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200687 call_settmode = mch_cur_tmode == TMODE_RAW
688 && (msec > 500 || (flags & MCH_DELAY_SETTMODE));
689 if (call_settmode)
690 {
691 old_tmode = mch_cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 settmode(TMODE_SLEEP);
Bram Moolenaar80361a52020-10-05 21:39:25 +0200693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
695 /*
696 * Everybody sleeps in a different way...
697 * Prefer nanosleep(), some versions of usleep() can only sleep up to
698 * one second.
699 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000700#ifdef FEAT_MZSCHEME
701 do
702 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100703 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000704 if (total > p_mzq)
705 msec = p_mzq;
706 else
707 msec = total;
708 total -= msec;
709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#ifdef HAVE_NANOSLEEP
711 {
712 struct timespec ts;
713
714 ts.tv_sec = msec / 1000;
715 ts.tv_nsec = (msec % 1000) * 1000000;
716 (void)nanosleep(&ts, NULL);
717 }
718#else
719# ifdef HAVE_USLEEP
720 while (msec >= 1000)
721 {
722 usleep((unsigned int)(999 * 1000));
723 msec -= 999;
724 }
725 usleep((unsigned int)(msec * 1000));
726# else
727# ifndef HAVE_SELECT
728 poll(NULL, 0, (int)msec);
729# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {
731 struct timeval tv;
732
733 tv.tv_sec = msec / 1000;
734 tv.tv_usec = (msec % 1000) * 1000;
Bram Moolenaar0981c872020-08-23 14:28:37 +0200735 // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
736 // a patch from Sun to fix this. Reported by Gunnar Pedersen.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 select(0, NULL, NULL, NULL, &tv);
738 }
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100739# endif
740# endif
741#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000742#ifdef FEAT_MZSCHEME
743 }
744 while (total > 0);
745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746
Bram Moolenaar80361a52020-10-05 21:39:25 +0200747 if (call_settmode)
Bram Moolenaar26e86442020-05-17 14:06:16 +0200748 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000749 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 }
751 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200752 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753}
754
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000755#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
757# define HAVE_CHECK_STACK_GROWTH
758/*
759 * Support for checking for an almost-out-of-stack-space situation.
760 */
761
762/*
763 * Return a pointer to an item on the stack. Used to find out if the stack
764 * grows up or down.
765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766static int stack_grows_downwards;
767
768/*
769 * Find out if the stack grows upwards or downwards.
770 * "p" points to a variable on the stack of the caller.
771 */
772 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100773check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
775 int i;
776
777 stack_grows_downwards = (p > (char *)&i);
778}
779#endif
780
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000781#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782static char *stack_limit = NULL;
783
784#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
785# include <pthread.h>
786# include <pthread_np.h>
787#endif
788
789/*
790 * Find out until how var the stack can grow without getting into trouble.
791 * Called when starting up and when switching to the signal stack in
792 * deathtrap().
793 */
794 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100795get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
797 struct rlimit rlp;
798 int i;
799 long lim;
800
Bram Moolenaar0f873732019-12-05 20:28:46 +0100801 // Set the stack limit to 15/16 of the allowable size. Skip this when the
802 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 if (getrlimit(RLIMIT_STACK, &rlp) == 0
804 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
805# ifdef RLIM_INFINITY
806 && rlp.rlim_cur != RLIM_INFINITY
807# endif
808 )
809 {
810 lim = (long)rlp.rlim_cur;
811#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
812 {
813 pthread_attr_t attr;
814 size_t size;
815
Bram Moolenaar0f873732019-12-05 20:28:46 +0100816 // On FreeBSD the initial thread always has a fixed stack size, no
817 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 pthread_attr_init(&attr);
819 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
820 {
821 pthread_attr_getstacksize(&attr, &size);
822 if (lim > (long)size)
823 lim = (long)size;
824 }
825 pthread_attr_destroy(&attr);
826 }
827#endif
828 if (stack_grows_downwards)
829 {
830 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
831 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100832 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 stack_limit = (char *)((long)&i / 16L);
834 }
835 else
836 {
837 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
838 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100839 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 }
841 }
842}
843
844/*
845 * Return FAIL when running out of stack space.
846 * "p" must point to any variable local to the caller that's on the stack.
847 */
848 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100849mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000851 if (stack_limit == NULL)
852 return OK;
853
854 if (stack_grows_downwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000856 if (p < stack_limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 return FAIL;
858 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000859 else if (p > stack_limit)
860 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 return OK;
862}
863#endif
864
865#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
866/*
867 * Support for using the signal stack.
868 * This helps when we run out of stack space, which causes a SIGSEGV. The
869 * signal handler then must run on another stack, since the normal stack is
870 * completely full.
871 */
872
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100874static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100876static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877# endif
878
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200879/*
880 * Get a size of signal stack.
881 * Preference (if available): sysconf > SIGSTKSZ > guessed size
882 */
Michael Jarvisbe9624e2023-04-19 20:28:48 +0100883static long int get_signal_stack_size(void)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200884{
885# ifdef HAVE_SYSCONF_SIGSTKSZ
886 long int size = -1;
887
888 // return size only if sysconf doesn't return an error
889 if ((size = sysconf(_SC_SIGSTKSZ)) > -1)
890 return size;
891# endif
892
893# ifdef SIGSTKSZ
894 // if sysconf() isn't available or gives error, return SIGSTKSZ
895 // if defined
896 return SIGSTKSZ;
897# endif
898
899 // otherwise guess the size
900 return 8000;
901}
902
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903static char *signal_stack;
904
905 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100906init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000908 if (signal_stack == NULL)
909 return;
910
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912# ifdef HAVE_SS_BASE
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000913 sigstk.ss_base = signal_stack;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000915 sigstk.ss_sp = signal_stack;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000917 sigstk.ss_size = get_signal_stack_size();
918 sigstk.ss_flags = 0;
919 (void)sigaltstack(&sigstk, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000921 sigstk.ss_sp = signal_stack;
922 if (stack_grows_downwards)
923 sigstk.ss_sp += get_signal_stack_size() - 1;
924 sigstk.ss_onstack = 0;
925 (void)sigstack(&sigstk, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927}
928#endif
929
930/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000931 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 * will barf when the second argument to signal() is ``wrong''.
933 * Let me try it with a few tricky defines from my own osdef.h (jw).
934 */
935#if defined(SIGWINCH)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100936 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937sig_winch SIGDEFARG(sigarg)
938{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100939 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100940 mch_signal(SIGWINCH, sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 do_resize = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942}
943#endif
944
dbivolaruab16ad32021-12-29 19:41:47 +0000945#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100946 static void
dbivolaruab16ad32021-12-29 19:41:47 +0000947sig_tstp SIGDEFARG(sigarg)
948{
949 // Second time we get called we actually need to suspend
950 if (in_mch_suspend)
951 {
ichizok378447f2023-05-11 22:25:42 +0100952 mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
dbivolaruab16ad32021-12-29 19:41:47 +0000953 raise(sigarg);
954 }
dbivolaru79a6e252022-01-23 16:41:14 +0000955 else
956 got_tstp = TRUE;
dbivolaruab16ad32021-12-29 19:41:47 +0000957
ichizok5f823d12022-03-13 17:27:38 +0000958#if !defined(__ANDROID__) && !defined(__OpenBSD__) && !defined(__DragonFly__)
959 // This is not required on all systems. On some systems (at least Android,
960 // OpenBSD, and DragonFlyBSD) this breaks suspending with CTRL-Z.
ichizok378447f2023-05-11 22:25:42 +0100961 mch_signal(SIGTSTP, sig_tstp);
xtkobacbef12e2022-02-27 12:31:52 +0000962#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000963}
964#endif
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966#if defined(SIGINT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100967 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968catch_sigint SIGDEFARG(sigarg)
969{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100970 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100971 mch_signal(SIGINT, catch_sigint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 got_int = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974#endif
975
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200976#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100977 static void
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200978catch_sigusr1 SIGDEFARG(sigarg)
979{
980 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100981 mch_signal(SIGUSR1, catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200982 got_sigusr1 = TRUE;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200983}
984#endif
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100987 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988catch_sigpwr SIGDEFARG(sigarg)
989{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100990 // this is not required on all systems, but it doesn't hurt anybody
ichizok378447f2023-05-11 22:25:42 +0100991 mch_signal(SIGPWR, catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 /*
993 * I'm not sure we get the SIGPWR signal when the system is really going
994 * down or when the batteries are almost empty. Just preserve the swap
995 * files and don't exit, that can't do any harm.
996 */
997 ml_sync_all(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998}
999#endif
1000
1001#ifdef SET_SIG_ALARM
1002/*
1003 * signal function for alarm().
1004 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001005 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006sig_alarm SIGDEFARG(sigarg)
1007{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001008 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 sig_alarm_called = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010}
1011#endif
1012
Bram Moolenaaredce7422019-01-20 18:39:30 +01001013#if (defined(HAVE_SETJMP_H) \
1014 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
1015 || defined(FEAT_LIBCALL))) \
1016 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001017# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +01001018
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001019// argument to SETJMP()
1020static JMP_BUF lc_jump_env;
1021
1022# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001023// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001024// Volatile because it is used in signal handlers.
1025static volatile sig_atomic_t lc_signal;
1026# endif
1027
1028// TRUE when lc_jump_env is valid.
1029// Volatile because it is used in signal handler deathtrap().
zeertzjq9b7d2a92022-08-26 10:33:54 +01001030static volatile sig_atomic_t lc_active = FALSE;
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001031
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032/*
1033 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001034 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001036 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 * Usage:
1038 * mch_startjmp();
1039 * if (SETJMP(lc_jump_env) != 0)
1040 * {
1041 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001042 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 * }
1044 * else
1045 * {
1046 * do_the_work;
1047 * mch_endjmp();
1048 * }
1049 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1050 * not return before the saved environment is used.
1051 * Returns OK for normal return, FAIL when the protected code caused a
1052 * problem and LONGJMP() was used.
1053 */
Bram Moolenaar113e1072019-01-20 15:30:40 +01001054 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001055mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001057# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 lc_active = TRUE;
1061}
1062
Bram Moolenaar113e1072019-01-20 15:30:40 +01001063 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001064mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
1066 lc_active = FALSE;
1067}
1068
Bram Moolenaar113e1072019-01-20 15:30:40 +01001069 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001070mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
1072# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001073 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1074 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 init_signal_stack();
1076# endif
1077}
1078#endif
1079
1080/*
1081 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001082 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001084 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1085 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001087 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088deathtrap SIGDEFARG(sigarg)
1089{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001090 static int entered = 0; // count the number of times we got here.
1091 // Note: when memory has been corrupted
1092 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093#ifdef SIGHASARG
1094 int i;
1095#endif
1096
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001097#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 /*
1099 * Catch a crash in protected code.
1100 * Restores the environment saved in lc_jump_env, which looks like
1101 * SETJMP() returns 1.
1102 */
1103 if (lc_active)
1104 {
1105# if defined(SIGHASARG)
1106 lc_signal = sigarg;
1107# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001108 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001110 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
1112#endif
1113
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001114#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001115# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001116 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1117 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1118 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001119 if (in_mch_delay && sigarg == SIGQUIT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001120 return;
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001121# endif
1122
Bram Moolenaar0f873732019-12-05 20:28:46 +01001123 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1124 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1125 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001126 if (entered == 0
1127 && (0
1128# ifdef SIGHUP
1129 || sigarg == SIGHUP
1130# endif
1131# ifdef SIGQUIT
1132 || sigarg == SIGQUIT
1133# endif
1134# ifdef SIGTERM
1135 || sigarg == SIGTERM
1136# endif
1137# ifdef SIGPWR
1138 || sigarg == SIGPWR
1139# endif
1140# ifdef SIGUSR1
1141 || sigarg == SIGUSR1
1142# endif
1143# ifdef SIGUSR2
1144 || sigarg == SIGUSR2
1145# endif
1146 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001147 && !vim_handle_signal(sigarg))
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001148 return;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001149#endif
1150
Bram Moolenaar0f873732019-12-05 20:28:46 +01001151 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 ++entered;
1153
Bram Moolenaar0f873732019-12-05 20:28:46 +01001154 // Executing autocommands is likely to use more stack space than we have
1155 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001156 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001159 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 set_vim_var_nr(VV_DYING, (long)entered);
1161#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001162 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001164#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001165 // Since we are now using the signal stack, need to reset the stack
1166 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 get_stack_limit();
1168#endif
1169
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001170#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001171 // This is for opening gdb the moment Vim crashes.
1172 // You need to manually adjust the file name and Vim executable name.
1173 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001174 {
1175# define VI_GDB_FILE "/tmp/vimgdb"
1176# define VIM_NAME "/usr/bin/vim"
1177 FILE *fp = fopen(VI_GDB_FILE, "w");
1178 if (fp)
1179 {
1180 fprintf(fp,
1181 "file %s\n"
1182 "attach %d\n"
1183 "set height 1000\n"
1184 "bt full\n"
1185 , VIM_NAME, getpid());
1186 fclose(fp);
1187 system("xterm -e gdb -x "VI_GDB_FILE);
1188 unlink(VI_GDB_FILE);
1189 }
1190 }
1191#endif
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001194 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 for (i = 0; signal_info[i].sig != -1; i++)
1196 if (sigarg == signal_info[i].sig)
1197 break;
1198 deadly_signal = sigarg;
1199#endif
1200
Bram Moolenaar0f873732019-12-05 20:28:46 +01001201 full_screen = FALSE; // don't write message to the GUI, it might be
1202 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 /*
1204 * If something goes wrong after entering here, we may get here again.
1205 * When this happens, give a message and try to exit nicely (resetting the
1206 * terminal mode, etc.)
1207 * When this happens twice, just exit, don't even try to give a message,
1208 * stack may be corrupt or something weird.
1209 * When this still happens again (or memory was corrupted in such a way
1210 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1211 */
1212 if (entered >= 3)
1213 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001214 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 may_core_dump();
1216 if (entered >= 4)
1217 _exit(8);
1218 exit(7);
1219 }
1220 if (entered == 2)
1221 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001222 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001223 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 out_flush();
1225 getout(1);
1226 }
1227
Bram Moolenaar0f873732019-12-05 20:28:46 +01001228 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001230 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 signal_info[i].name);
1232#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001233 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001235
Bram Moolenaar0f873732019-12-05 20:28:46 +01001236 // Preserve files and exit. This sets the really_exiting flag to prevent
1237 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001238 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
Bram Moolenaar0f873732019-12-05 20:28:46 +01001240 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001241
Bram Moolenaar009b2592004-10-24 19:18:58 +00001242#ifdef NBDEBUG
1243 reset_signals();
1244 may_core_dump();
1245 abort();
1246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247}
1248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001250 * Invoked after receiving SIGCONT. We don't know what happened while
1251 * sleeping, deal with part of that.
1252 */
1253 static void
1254after_sigcont(void)
1255{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001256 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1257 // again later.
1258 oldtitle_outdated = TRUE;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001259
Bram Moolenaar2e310482018-08-21 13:09:10 +02001260 settmode(TMODE_RAW);
1261 need_check_timestamps = TRUE;
1262 did_check_timestamps = FALSE;
1263}
1264
1265#if defined(SIGCONT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001266static void sigcont_handler SIGPROTOARG;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001267
1268/*
1269 * With multi-threading, suspending might not work immediately. Catch the
1270 * SIGCONT signal, which will be used as an indication whether the suspending
1271 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001272 *
1273 * On Linux, signal is not always handled immediately either.
1274 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001275 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001276 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001277 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001279static volatile sig_atomic_t sigcont_received;
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001280static void sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281
1282/*
1283 * signal handler for SIGCONT
1284 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001285 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286sigcont_handler SIGDEFARG(sigarg)
1287{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001288 if (in_mch_suspend)
1289 {
1290 sigcont_received = TRUE;
1291 }
1292 else
1293 {
1294 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1295 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1296 // back to a sane mode. We should redraw, but we can't really do that
1297 // in a signal handler, do a redraw later.
1298 after_sigcont();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001299 redraw_later(UPD_CLEAR);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001300 cursor_on_force();
1301 out_flush();
1302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303}
1304#endif
1305
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001306#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001307# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001308static void *clip_star_save = NULL;
1309static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001310# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001311
1312/*
1313 * Called when Vim is going to sleep or execute a shell command.
1314 * We can't respond to requests for the X selections. Lose them, otherwise
1315 * other applications will hang. But first copy the text to cut buffer 0.
1316 */
1317 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001318loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001319{
1320 if (clip_star.owned || clip_plus.owned)
1321 {
1322 x11_export_final_selection();
1323 if (clip_star.owned)
1324 clip_lose_selection(&clip_star);
1325 if (clip_plus.owned)
1326 clip_lose_selection(&clip_plus);
1327 if (x11_display != NULL)
1328 XFlush(x11_display);
1329 }
1330}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001331
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001332# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001333/*
1334 * Save clipboard text to restore later.
1335 */
1336 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001337save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001338{
1339 if (clip_star.owned)
1340 clip_star_save = get_register('*', TRUE);
1341 if (clip_plus.owned)
1342 clip_plus_save = get_register('+', TRUE);
1343}
1344
1345/*
1346 * Restore clipboard text if no one own the X selection.
1347 */
1348 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001349restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001350{
1351 if (clip_star_save != NULL)
1352 {
1353 if (!clip_gen_owner_exists(&clip_star))
1354 put_register('*', clip_star_save);
1355 else
1356 free_register(clip_star_save);
1357 clip_star_save = NULL;
1358 }
1359 if (clip_plus_save != NULL)
1360 {
1361 if (!clip_gen_owner_exists(&clip_plus))
1362 put_register('+', clip_plus_save);
1363 else
1364 free_register(clip_plus_save);
1365 clip_plus_save = NULL;
1366 }
1367}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001368# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001369#endif
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371/*
1372 * If the machine has job control, use it to suspend the program,
1373 * otherwise fake it by starting a new shell.
1374 */
1375 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001376mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001378 if (ignore_sigtstp)
1379 return;
1380
Bram Moolenaar041c7102020-05-30 18:14:57 +02001381#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001382 in_mch_suspend = TRUE;
1383
Bram Moolenaar0f873732019-12-05 20:28:46 +01001384 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001386 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
1388# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001389 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001391# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 sigcont_received = FALSE;
1393# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001394
Bram Moolenaar0f873732019-12-05 20:28:46 +01001395 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001396
1397# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001398 /*
1399 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001400 * immediately, but somehow not all the time, probably because it's handled
1401 * in another thread. Do not call pause() because there would be race
1402 * condition which would hang Vim if signal happened in between the test of
1403 * sigcont_received and the call to pause(). If signal is not yet received,
1404 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1405 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001406 */
1407 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001408 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001409
Bram Moolenaar262735e2009-07-14 10:20:22 +00001410 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001411 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001414 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
Bram Moolenaar2e310482018-08-21 13:09:10 +02001416 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417#else
1418 suspend_shell();
1419#endif
1420}
1421
1422 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001423mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
1425 Columns = 80;
1426 Rows = 24;
1427
1428 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001429
1430#ifdef SIGTSTP
1431 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1432 // that indicates the shell (or program) that launched us does not support
1433 // tty job control and thus we should ignore that signal. If invoked as a
1434 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
ichizok378447f2023-05-11 22:25:42 +01001435 ignore_sigtstp = restricted || SIG_IGN == mch_signal(SIGTSTP, SIG_ERR);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001438
Bram Moolenaar56718732006-03-15 22:53:57 +00001439#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001440 mac_conv_init();
1441#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001442#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1443 win_clip_init();
1444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445}
1446
1447 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001448set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
1450#if defined(SIGWINCH)
1451 /*
1452 * WINDOW CHANGE signal is handled with sig_winch().
1453 */
ichizok378447f2023-05-11 22:25:42 +01001454 mch_signal(SIGWINCH, sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#endif
1456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001458 // See mch_init() for the conditions under which we ignore SIGTSTP.
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001459 // In the GUI default TSTP processing is OK.
1460 // Checking both gui.in_use and gui.starting because gui.in_use is not set
1461 // at this point (set after menus are displayed), but gui.starting is set.
ichizok378447f2023-05-11 22:25:42 +01001462 mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001463# ifdef FEAT_GUI
1464 : gui.in_use || gui.starting ? SIG_DFL
1465# endif
ichizok378447f2023-05-11 22:25:42 +01001466 : sig_tstp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001468#if defined(SIGCONT)
ichizok378447f2023-05-11 22:25:42 +01001469 mch_signal(SIGCONT, sigcont_handler);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001471#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 /*
1473 * We want to ignore breaking of PIPEs.
1474 */
ichizok378447f2023-05-11 22:25:42 +01001475 mch_signal(SIGPIPE, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476#endif
1477
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001479 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480#endif
1481
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001482#ifdef SIGUSR1
1483 /*
1484 * Call user's handler on SIGUSR1
1485 */
ichizok378447f2023-05-11 22:25:42 +01001486 mch_signal(SIGUSR1, catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001487#endif
1488
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 /*
1490 * Ignore alarm signals (Perl's alarm() generates it).
1491 */
1492#ifdef SIGALRM
ichizok378447f2023-05-11 22:25:42 +01001493 mch_signal(SIGALRM, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494#endif
1495
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001496#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 /*
1498 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1499 * work will be lost.
1500 */
ichizok378447f2023-05-11 22:25:42 +01001501 mch_signal(SIGPWR, catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502#endif
1503
1504 /*
1505 * Arrange for other signals to gracefully shutdown Vim.
1506 */
1507 catch_signals(deathtrap, SIG_ERR);
1508
1509#if defined(FEAT_GUI) && defined(SIGHUP)
1510 /*
1511 * When the GUI is running, ignore the hangup signal.
1512 */
1513 if (gui.in_use)
ichizok378447f2023-05-11 22:25:42 +01001514 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515#endif
1516}
1517
Bram Moolenaardf177f62005-02-22 08:39:57 +00001518#if defined(SIGINT) || defined(PROTO)
1519/*
1520 * Catch CTRL-C (only works while in Cooked mode).
1521 */
1522 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001523catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001524{
ichizok378447f2023-05-11 22:25:42 +01001525 mch_signal(SIGINT, catch_sigint);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001526}
1527#endif
1528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001530reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001533#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001534 // SIGCONT isn't in the list, because its default action is ignore
ichizok378447f2023-05-11 22:25:42 +01001535 mch_signal(SIGCONT, SIG_DFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536#endif
1537}
1538
1539 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001540catch_signals(
Michael Jarvisbe9624e2023-04-19 20:28:48 +01001541 void (*func_deadly)(int),
1542 void (*func_other)(int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
1544 int i;
1545
1546 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (signal_info[i].deadly)
1549 {
1550#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1551 struct sigaction sa;
1552
Bram Moolenaar0f873732019-12-05 20:28:46 +01001553 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 sa.sa_handler = func_deadly;
1555 sigemptyset(&sa.sa_mask);
1556# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001557 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1558 // thread handling in combination with using the alternate stack:
1559 // pthread library functions try to use the stack pointer to
1560 // identify the current thread, causing a SEGV signal, which
1561 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 sa.sa_flags = 0;
1563# else
1564 sa.sa_flags = SA_ONSTACK;
1565# endif
1566 sigaction(signal_info[i].sig, &sa, NULL);
1567#else
1568# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1569 struct sigvec sv;
1570
Bram Moolenaar0f873732019-12-05 20:28:46 +01001571 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 sv.sv_handler = func_deadly;
1573 sv.sv_mask = 0;
1574 sv.sv_flags = SV_ONSTACK;
1575 sigvec(signal_info[i].sig, &sv, NULL);
1576# else
ichizok378447f2023-05-11 22:25:42 +01001577 mch_signal(signal_info[i].sig, func_deadly);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578# endif
1579#endif
1580 }
1581 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001582 {
1583 // Deal with non-deadly signals.
1584#ifdef SIGTSTP
ichizok378447f2023-05-11 22:25:42 +01001585 mch_signal(signal_info[i].sig,
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001586 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1587 ? SIG_IGN : func_other);
1588#else
ichizok378447f2023-05-11 22:25:42 +01001589 mch_signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001590#endif
1591 }
1592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593}
1594
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001595#ifdef HAVE_SIGPROCMASK
1596 static void
1597block_signals(sigset_t *set)
1598{
1599 sigset_t newset;
1600 int i;
1601
1602 sigemptyset(&newset);
1603
1604 for (i = 0; signal_info[i].sig != -1; i++)
1605 sigaddset(&newset, signal_info[i].sig);
1606
Bram Moolenaar2e310482018-08-21 13:09:10 +02001607# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001608 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001609 sigaddset(&newset, SIGCONT);
1610# endif
1611
1612 sigprocmask(SIG_BLOCK, &newset, set);
1613}
1614
1615 static void
1616unblock_signals(sigset_t *set)
1617{
1618 sigprocmask(SIG_SETMASK, set, NULL);
1619}
1620#endif
1621
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001623 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001624 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1625 * return TRUE
1626 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1627 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001628 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001629 * Returns TRUE when Vim should exit.
1630 */
1631 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001632vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001633{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001634 static int got_signal = 0;
1635 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001636
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001637 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001638 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001639 case SIGNAL_BLOCK: blocked = TRUE;
1640 break;
1641
1642 case SIGNAL_UNBLOCK: blocked = FALSE;
1643 if (got_signal != 0)
1644 {
1645 kill(getpid(), got_signal);
1646 got_signal = 0;
1647 }
1648 break;
1649
1650 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001651 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001652 got_signal = sig;
1653#ifdef SIGPWR
1654 if (sig != SIGPWR)
1655#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001656 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001657 break;
1658 }
1659 return FALSE;
1660}
1661
1662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 * Check_win checks whether we have an interactive stdout.
1664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001666mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 if (isatty(1))
1669 return OK;
1670 return FAIL;
1671}
1672
1673/*
1674 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1675 */
1676 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001677mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
1679 if (isatty(read_cmd_fd))
1680 return TRUE;
1681 return FALSE;
1682}
1683
1684#ifdef FEAT_X11
1685
Bram Moolenaar651fca82021-11-29 20:39:38 +00001686# if defined(ELAPSED_TIMEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688/*
1689 * Give a message about the elapsed time for opening the X window.
1690 */
1691 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001692xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001694 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695}
1696# endif
1697#endif
1698
Bram Moolenaar651fca82021-11-29 20:39:38 +00001699#if defined(FEAT_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700/*
1701 * A few functions shared by X11 title and clipboard code.
1702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
1704static int got_x_error = FALSE;
1705
1706/*
1707 * X Error handler, otherwise X just exits! (very rude) -- webb
1708 */
1709 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001710x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001712 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 STRCAT(IObuff, _("\nVim: Got X error\n"));
1714
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001715 // In the GUI we cannot print a message and continue, because no X calls
1716 // are allowed here (causes my system to hang). Silently continuing seems
1717 // like the best alternative. Do preserve files, in case we crash.
1718 ml_sync_all(FALSE, FALSE);
1719
1720#ifdef FEAT_GUI
1721 if (!gui.in_use)
1722#endif
1723 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
Bram Moolenaar0f873732019-12-05 20:28:46 +01001725 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726}
1727
1728/*
1729 * Another X Error handler, just used to check for errors.
1730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001732x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
1734 got_x_error = TRUE;
1735 return 0;
1736}
1737
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001738/*
1739 * Return TRUE when connection to the X server is desired.
1740 */
1741 static int
1742x_connect_to_server(void)
1743{
1744 // No point in connecting if we are exiting or dying.
1745 if (exiting || v_dying)
1746 return FALSE;
1747
1748#if defined(FEAT_CLIENTSERVER)
1749 if (x_force_connect)
1750 return TRUE;
1751#endif
1752 if (x_no_connect)
1753 return FALSE;
1754
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001755 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001756 if (clip_exclude_prog != NULL)
1757 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001758 // Just in case we get called recursively, return FALSE. This could
1759 // happen if vpeekc() is used while executing the prog and it causes a
1760 // related callback to be invoked.
1761 if (regprog_in_use(clip_exclude_prog))
1762 return FALSE;
1763
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001764 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1765 return FALSE;
1766 }
1767 return TRUE;
1768}
1769
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001771# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772/*
1773 * An X IO Error handler, used to catch error while opening the display.
1774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001776x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001778 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001780# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001781 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783}
1784# endif
1785
1786/*
1787 * An X IO Error handler, used to catch terminal errors.
1788 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001789static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001792x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
1794 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001795 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 x11_window = 0;
1797 x11_display = NULL;
1798 xterm_Shell = (Widget)0;
1799
Bram Moolenaar0f873732019-12-05 20:28:46 +01001800 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001802# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001803 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001806
1807/*
1808 * If the X11 connection was lost try to restore it.
1809 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001810 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001811 */
1812 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001813may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001814{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001815 // No point in restoring the connecting if we are exiting or dying.
1816 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001817 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001818 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001819
1820# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001821 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001822 if (app_context != (XtAppContext)NULL)
1823 {
1824 XtDestroyApplicationContext(app_context);
1825 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001826 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001827 }
1828# endif
1829
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001830 setup_term_clip();
1831 get_x11_title(FALSE);
1832 }
1833}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001834
1835 void
1836ex_xrestore(exarg_T *eap)
1837{
1838 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1839 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001840 if (xterm_display_allocated)
1841 vim_free(xterm_display);
1842 xterm_display = (char *)vim_strsave(eap->arg);
1843 xterm_display_allocated = TRUE;
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001844 }
1845 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001846 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001847
1848 clear_xterm_clip();
1849 x11_window = 0;
1850 xterm_dpy_retry_count = 5; // Try reconnecting five times
1851 may_restore_clipboard();
1852}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853#endif
1854
1855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 * Test if "dpy" and x11_window are valid by getting the window title.
1857 * I don't actually want it yet, so there may be a simpler call to use, but
1858 * this will cause the error handler x_error_check() to be called if anything
1859 * is wrong, such as the window pointer being invalid (as can happen when the
1860 * user changes his DISPLAY, but not his WINDOWID) -- webb
1861 */
1862 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001863test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864{
Dominique Pellé4927bc72023-09-24 16:12:07 +02001865 int (*old_handler)(Display*, XErrorEvent*);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 XTextProperty text_prop;
1867
1868 old_handler = XSetErrorHandler(x_error_check);
1869 got_x_error = FALSE;
1870 if (XGetWMName(dpy, x11_window, &text_prop))
1871 XFree((void *)text_prop.value);
1872 XSync(dpy, False);
1873 (void)XSetErrorHandler(old_handler);
1874
1875 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001876 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 return (got_x_error ? FAIL : OK);
1879}
1880#endif
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
1883#ifdef FEAT_X11
1884
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001885static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886
1887/*
1888 * try to get x11 window and display
1889 *
1890 * return FAIL for failure, OK otherwise
1891 */
1892 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001893get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 char *winid;
1896 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001897#define XD_NONE 0 // x11_display not set here
1898#define XD_HERE 1 // x11_display opened here
1899#define XD_GUI 2 // x11_display used from gui.dpy
1900#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 static int x11_display_from = XD_NONE;
1902 static int did_set_error_handler = FALSE;
1903
1904 if (!did_set_error_handler)
1905 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001906 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 (void)XSetErrorHandler(x_error_handler);
1908 did_set_error_handler = TRUE;
1909 }
1910
Bram Moolenaar9372a112005-12-06 19:59:18 +00001911#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (gui.in_use)
1913 {
1914 /*
1915 * If the X11 display was opened here before, for the window where Vim
1916 * was started, close that one now to avoid a memory leak.
1917 */
1918 if (x11_display_from == XD_HERE && x11_display != NULL)
1919 {
1920 XCloseDisplay(x11_display);
1921 x11_display_from = XD_NONE;
1922 }
1923 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1924 {
1925 x11_display_from = XD_GUI;
1926 return OK;
1927 }
1928 x11_display = NULL;
1929 return FAIL;
1930 }
1931 else if (x11_display_from == XD_GUI)
1932 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001933 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 x11_window = 0;
1935 x11_display = NULL;
1936 x11_display_from = XD_NONE;
1937 }
1938#endif
1939
Bram Moolenaar0f873732019-12-05 20:28:46 +01001940 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (!x_connect_to_server())
1942 return FAIL;
1943
1944 /*
1945 * If WINDOWID not set, should try another method to find out
1946 * what the current window number is. The only code I know for
1947 * this is very complicated.
1948 * We assume that zero is invalid for WINDOWID.
1949 */
1950 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1951 x11_window = (Window)atol(winid);
1952
1953#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001954 if (xterm_dpy == x11_display)
1955 // x11_display may have been set to xterm_dpy elsewhere
1956 x11_display_from = XD_XTERM;
1957
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 if (xterm_dpy != NULL && x11_window != 0)
1959 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001960 // We may have checked it already, but Gnome terminal can move us to
1961 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001962 if (x11_display_from != XD_XTERM)
1963 {
1964 /*
1965 * If the X11 display was opened here before, for the window where
1966 * Vim was started, close that one now to avoid a memory leak.
1967 */
1968 if (x11_display_from == XD_HERE && x11_display != NULL)
1969 XCloseDisplay(x11_display);
1970 x11_display = xterm_dpy;
1971 x11_display_from = XD_XTERM;
1972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 if (test_x11_window(x11_display) == FAIL)
1974 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001975 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 x11_window = 0;
1977 x11_display = NULL;
1978 x11_display_from = XD_NONE;
1979 return FAIL;
1980 }
1981 return OK;
1982 }
1983#endif
1984
1985 if (x11_window == 0 || x11_display == NULL)
1986 result = -1;
1987
Bram Moolenaar0f873732019-12-05 20:28:46 +01001988 if (result != -1) // Have already been here and set this
1989 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990
1991 if (x11_window != 0 && x11_display == NULL)
1992 {
1993#ifdef SET_SIG_ALARM
ichizok378447f2023-05-11 22:25:42 +01001994 sighandler_T sig_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001996#ifdef ELAPSED_FUNC
1997 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998
1999 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01002000 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001#endif
2002
2003#ifdef SET_SIG_ALARM
2004 /*
2005 * Opening the Display may hang if the DISPLAY setting is wrong, or
2006 * the network connection is bad. Set an alarm timer to get out.
2007 */
2008 sig_alarm_called = FALSE;
ichizok378447f2023-05-11 22:25:42 +01002009 sig_save = mch_signal(SIGALRM, sig_alarm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 alarm(2);
2011#endif
2012 x11_display = XOpenDisplay(NULL);
2013
2014#ifdef SET_SIG_ALARM
2015 alarm(0);
ichizok378447f2023-05-11 22:25:42 +01002016 mch_signal(SIGALRM, sig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01002018 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019#endif
2020 if (x11_display != NULL)
2021 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002022# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00002024 {
2025 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002026 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00002027 verbose_leave();
2028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029# endif
2030 if (test_x11_window(x11_display) == FAIL)
2031 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002032 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 x11_window = 0;
2034 XCloseDisplay(x11_display);
2035 x11_display = NULL;
2036 }
2037 else
2038 x11_display_from = XD_HERE;
2039 }
2040 }
2041 if (x11_window == 0 || x11_display == NULL)
2042 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02002043
2044# ifdef FEAT_EVAL
2045 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
2046# endif
2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 return (result = OK);
2049}
2050
2051/*
2052 * Determine original x11 Window Title
2053 */
2054 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002055get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056{
Bram Moolenaar47136d72004-10-12 20:02:24 +00002057 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058}
2059
2060/*
2061 * Determine original x11 Window icon
2062 */
2063 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002064get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
2066 int retval = FALSE;
2067
2068 retval = get_x11_thing(FALSE, test_only);
2069
Bram Moolenaar0f873732019-12-05 20:28:46 +01002070 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (oldicon == NULL && !test_only)
2072 {
2073 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002074 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002076 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 }
2078
2079 return retval;
2080}
2081
2082 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002083get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002084 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01002085 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086{
2087 XTextProperty text_prop;
2088 int retval = FALSE;
2089 Status status;
2090
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002091 if (get_x11_windis() != OK)
2092 return FALSE;
2093
2094 // Get window/icon name if any
2095 if (get_title)
2096 status = XGetWMName(x11_display, x11_window, &text_prop);
2097 else
2098 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2099
2100 /*
2101 * If terminal is xterm, then x11_window may be a child window of the
2102 * outer xterm window that actually contains the window/icon name, so
2103 * keep traversing up the tree until a window with a title/icon is
2104 * found.
2105 */
2106 // Previously this was only done for xterm and alike. I don't see a
2107 // reason why it would fail for other terminal emulators.
2108 // if (term_is_xterm)
2109 Window root;
2110 Window parent;
2111 Window win = x11_window;
2112 Window *children;
2113 unsigned int num_children;
2114
2115 while (!status || text_prop.value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002117 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2118 &num_children))
2119 break;
2120 if (children)
2121 XFree((void *)children);
2122 if (parent == root || parent == 0)
2123 break;
2124
2125 win = parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002127 status = XGetWMName(x11_display, win, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002129 status = XGetWMIconName(x11_display, win, &text_prop);
2130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002132 if (status && text_prop.value != NULL)
2133 {
2134 retval = TRUE;
2135 if (!test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002137 if (get_title)
2138 vim_free(oldtitle);
2139 else
2140 vim_free(oldicon);
2141 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002144 oldtitle = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002146 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002148 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002150 char **cl;
2151 Status transform_status;
2152 int n = 0;
2153
2154 transform_status = XmbTextPropertyToTextList(x11_display,
2155 &text_prop,
2156 &cl, &n);
2157 if (transform_status >= Success && n > 0 && cl[0])
2158 {
2159 if (get_title)
2160 oldtitle = vim_strsave((char_u *) cl[0]);
2161 else
2162 oldicon = vim_strsave((char_u *) cl[0]);
2163 XFreeStringList(cl);
2164 }
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002165 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 if (get_title)
2168 oldtitle = vim_strsave((char_u *)text_prop.value);
2169 else
2170 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002174 XFree((void *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 }
2176 return retval;
2177}
2178
Bram Moolenaar0f873732019-12-05 20:28:46 +01002179// Xutf8 functions are not available on older systems. Note that on some
2180// systems X_HAVE_UTF8_STRING may be defined in a header file but
2181// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2182// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002183#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002184# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185# define USE_UTF8_STRING
2186# endif
2187#endif
2188
2189/*
2190 * Set x11 Window Title
2191 *
2192 * get_x11_windis() must be called before this and have returned OK
2193 */
2194 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002195set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002197 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2198 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2199 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200#ifdef USE_UTF8_STRING
2201 if (enc_utf8)
2202 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2203 NULL, NULL, 0, NULL, NULL, NULL);
2204 else
2205#endif
2206 {
2207#if XtSpecificationRelease >= 4
2208# ifdef FEAT_XFONTSET
2209 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2210 NULL, NULL, 0, NULL, NULL, NULL);
2211# else
2212 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002213 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214
Bram Moolenaar0f873732019-12-05 20:28:46 +01002215 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002216 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 XSetWMProperties(x11_display, x11_window, &text_prop,
2218 NULL, NULL, 0, NULL, NULL, NULL);
2219# endif
2220#else
2221 XStoreName(x11_display, x11_window, (char *)title);
2222#endif
2223 }
2224 XFlush(x11_display);
2225}
2226
2227/*
2228 * Set x11 Window icon
2229 *
2230 * get_x11_windis() must be called before this and have returned OK
2231 */
2232 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002233set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002235 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236#ifdef USE_UTF8_STRING
2237 if (enc_utf8)
2238 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2239 NULL, 0, NULL, NULL, NULL);
2240 else
2241#endif
2242 {
2243#if XtSpecificationRelease >= 4
2244# ifdef FEAT_XFONTSET
2245 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2246 NULL, 0, NULL, NULL, NULL);
2247# else
2248 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002249 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002251 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2253 NULL, 0, NULL, NULL, NULL);
2254# endif
2255#else
2256 XSetIconName(x11_display, x11_window, (char *)icon);
2257#endif
2258 }
2259 XFlush(x11_display);
2260}
2261
Bram Moolenaar0f873732019-12-05 20:28:46 +01002262#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002265get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 return FALSE;
2268}
2269
2270 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002271get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
2273 if (!test_only)
2274 {
2275 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002276 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002278 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280 return FALSE;
2281}
2282
Bram Moolenaar0f873732019-12-05 20:28:46 +01002283#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
2285 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002286mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288 return get_x11_title(TRUE);
2289}
2290
2291 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002292mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
2294 return get_x11_icon(TRUE);
2295}
2296
2297/*
2298 * Set the window title and icon.
2299 */
2300 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002301mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302{
2303 int type = 0;
2304 static int recursive = 0;
2305
Bram Moolenaar0f873732019-12-05 20:28:46 +01002306 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002308 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 return;
2310
Bram Moolenaar0f873732019-12-05 20:28:46 +01002311 // When one of the X11 functions causes a deadly signal, we get here again
2312 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (recursive)
2314 return;
2315 ++recursive;
2316
2317 /*
2318 * if the window ID and the display is known, we may use X11 calls
2319 */
2320#ifdef FEAT_X11
2321 if (get_x11_windis() == OK)
2322 type = 1;
lilydjwg6e0a18f2024-01-29 20:54:28 +01002323#endif
2324#if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002325 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 if (gui.in_use)
2327 type = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328#endif
2329
2330 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002331 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 * than x11 calls, because the x11 calls don't always work
2333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if ((type || *T_TS != NUL) && title != NULL)
2335 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002336 if (oldtitle_outdated)
2337 {
2338 oldtitle_outdated = FALSE;
2339 VIM_CLEAR(oldtitle);
2340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (oldtitle == NULL
2342#ifdef FEAT_GUI
2343 && !gui.in_use
2344#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002345 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 (void)get_x11_title(FALSE);
2347
Bram Moolenaar0f873732019-12-05 20:28:46 +01002348 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 term_settitle(title);
2350#ifdef FEAT_X11
2351 else
2352# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002353 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002355 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002357#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002358 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 else
2360 gui_mch_settitle(title, icon);
2361#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002362 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
2364
2365 if ((type || *T_CIS != NUL) && icon != NULL)
2366 {
2367 if (oldicon == NULL
2368#ifdef FEAT_GUI
2369 && !gui.in_use
2370#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002371 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 get_x11_icon(FALSE);
2373
2374 if (*T_CIS != NUL)
2375 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002376 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002378 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 out_flush();
2380 }
2381#ifdef FEAT_X11
2382 else
2383# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002384 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002386 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387#endif
2388 did_set_icon = TRUE;
2389 }
2390 --recursive;
2391}
2392
2393/*
2394 * Restore the window/icon title.
2395 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002396 * SAVE_RESTORE_TITLE only restore title
2397 * SAVE_RESTORE_ICON only restore icon
2398 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 */
2400 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002401mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
Bram Moolenaardac13472019-09-16 21:06:21 +02002403 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002404
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002405 // Only restore the title or icon when it has been set.
2406 // When using "oldtitle" make a copy, it might be freed halfway.
2407 char_u *title = ((which & SAVE_RESTORE_TITLE) && unix_did_set_title)
2408 ? (oldtitle ? oldtitle : p_titleold) : NULL;
2409 char_u *tofree = NULL;
2410 if (title == oldtitle && oldtitle != NULL)
2411 {
2412 tofree = vim_strsave(title);
2413 if (tofree != NULL)
2414 title = tofree;
2415 }
2416 mch_settitle(title,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002417 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002418 vim_free(tofree);
Bram Moolenaar40385db2018-08-07 22:31:44 +02002419
Bram Moolenaare5c83282019-05-03 23:15:37 +02002420 if (do_push_pop)
2421 {
2422 // pop and push from/to the stack
2423 term_pop_title(which);
2424 term_push_title(which);
2425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426}
2427
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429/*
2430 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002431 * This matches "xterm.*", thus "xterm-256color", "xterm-kitty", etc.
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002432 * Do not consider "xterm-kitty" an xterm, it is not fully xterm compatible,
2433 * using the "xterm-kitty" terminfo entry should work better.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002434 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 */
2436 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002437vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 if (name == NULL)
2440 return FALSE;
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002441 return ((STRNICMP(name, "xterm", 5) == 0
2442 && STRNICMP(name, "xterm-kitty", 11) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 || STRNICMP(name, "nxterm", 6) == 0
2444 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002445 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002447 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 || STRCMP(name, "builtin_xterm") == 0);
2449}
2450
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002451#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2452/*
2453 * Return TRUE if "name" appears to be that of a terminal
2454 * known to support the xterm-style mouse protocol.
2455 * Relies on term_is_xterm having been set to its correct value.
2456 */
2457 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002458use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002459{
2460 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002461 && (term_is_xterm
2462 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002463 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002464 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002465 || STRICMP(name, "st") == 0
2466 || STRNICMP(name, "st-", 3) == 0
2467 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002468}
2469#endif
2470
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471/*
2472 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2473 * Return 1 for "xterm".
2474 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002475 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002476 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 */
2478 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002479use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002481 if (ttym_flags == TTYM_SGR)
2482 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002483 if (ttym_flags == TTYM_URXVT)
2484 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (ttym_flags == TTYM_XTERM2)
2486 return 2;
2487 if (ttym_flags == TTYM_XTERM)
2488 return 1;
2489 return 0;
2490}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002492/*
2493 * Return TRUE if "name" is an iris-ansi terminal name.
2494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002496vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 if (name == NULL)
2499 return FALSE;
2500 return (STRNICMP(name, "iris-ansi", 9) == 0
2501 || STRCMP(name, "builtin_iris-ansi") == 0);
2502}
2503
Dominique Pellee764d1b2023-03-12 21:20:59 +00002504#if defined(VMS) || defined(PROTO)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002505/*
2506 * Return TRUE if "name" is a vt300-like terminal name.
2507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002509vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
2511 if (name == NULL)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002512 return FALSE;
2513 // Actually all ANSI compatible terminals should be here.
2514 // Catch at least VT1xx - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002515 return ((STRNICMP(name, "vt", 2) == 0
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002516 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 || STRCMP(name, "builtin_vt320") == 0);
2518}
Dominique Pellee764d1b2023-03-12 21:20:59 +00002519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520
2521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 * Insert user name in s[len].
2523 * Return OK if a name found.
2524 */
2525 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002526mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
2528#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002529 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 return OK;
2531#else
2532 return mch_get_uname(getuid(), s, len);
2533#endif
2534}
2535
2536/*
2537 * Insert user name for "uid" in s[len].
2538 * Return OK if a name found.
2539 */
2540 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002541mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542{
2543#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2544 struct passwd *pw;
2545
2546 if ((pw = getpwuid(uid)) != NULL
2547 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2548 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002549 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 return OK;
2551 }
2552#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002553 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2554 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555}
2556
2557/*
2558 * Insert host name is s[len].
2559 */
2560
2561#ifdef HAVE_SYS_UTSNAME_H
2562 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002563mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
2565 struct utsname vutsname;
2566
2567 if (uname(&vutsname) < 0)
2568 *s = NUL;
2569 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002570 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002572#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
2574# ifdef HAVE_SYS_SYSTEMINFO_H
2575# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2576# endif
2577
2578 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002579mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
2581# ifdef VAXC
2582 vaxc$gethostname((char *)s, len);
2583# else
2584 gethostname((char *)s, len);
2585# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002586 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002588#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
2590/*
2591 * return process ID
2592 */
2593 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002594mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
2596 return (long)getpid();
2597}
2598
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002599/*
2600 * return TRUE if process "pid" is still running
2601 */
2602 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002603mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002604{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002605 // If there is no error the process must be running.
2606 if (kill(pid, 0) == 0)
2607 return TRUE;
2608#ifdef ESRCH
2609 // If the error is ESRCH then the process is not running.
2610 if (errno == ESRCH)
2611 return FALSE;
2612#endif
2613 // If the process is running and owned by another user we get EPERM. With
2614 // other errors the process might be running, assuming it is then.
2615 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002616}
2617
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002620strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621{
2622 extern int sys_nerr;
2623 extern char *sys_errlist[];
2624 static char er[20];
2625
2626 if (err > 0 && err < sys_nerr)
2627 return (sys_errlist[err]);
2628 sprintf(er, "Error %d", err);
2629 return er;
2630}
2631#endif
2632
2633/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002634 * Get name of current directory into buffer "buf" of length "len" bytes.
2635 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 * Return OK for success, FAIL for failure.
2637 */
2638 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002639mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
2641#if defined(USE_GETCWD)
2642 if (getcwd((char *)buf, len) == NULL)
2643 {
2644 STRCPY(buf, strerror(errno));
2645 return FAIL;
2646 }
2647 return OK;
2648#else
2649 return (getwd((char *)buf) != NULL ? OK : FAIL);
2650#endif
2651}
2652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002654 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 *
2656 * return FAIL for failure, OK for success
2657 */
2658 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002659mch_FullName(
2660 char_u *fname,
2661 char_u *buf,
2662 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002663 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664{
2665 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002666#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002668 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 char_u olddir[MAXPATHL];
2671 char_u *p;
2672 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002673#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002674 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2675 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002676#endif
2677
Bram Moolenaar38323e42007-03-06 19:22:53 +00002678#ifdef VMS
2679 fname = vms_fixfilename(fname);
2680#endif
2681
Bram Moolenaara2442432007-04-26 14:26:37 +00002682#ifdef __CYGWIN__
2683 /*
2684 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2685 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002686# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002687 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2688 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002689 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2690 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002691# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002692 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002693# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002694 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002695#endif
2696
Bram Moolenaar0f873732019-12-05 20:28:46 +01002697 // Expand it if forced or not an absolute path.
2698 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002699 if ((force || !mch_isFullName(fname))
2700 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
zeertzjq1ee74202024-07-12 07:29:14 +02002702 if (p == NULL && STRCMP(fname, "..") == 0)
2703 // Handle ".." without path separators.
2704 p = fname + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 /*
2706 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002707 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 * This will get the correct path name with "../" things.
2709 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002710 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002712 if (STRCMP(p, "/..") == 0)
zeertzjq1ee74202024-07-12 07:29:14 +02002713 // For "/path/dir/.." include the "/..".
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002714 p += 3;
2715
Bram Moolenaar38323e42007-03-06 19:22:53 +00002716#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 /*
2718 * Use fchdir() if possible, it's said to be faster and more
2719 * reliable. But on SunOS 4 it might not work. Check this by
2720 * doing a fchdir() right now.
2721 */
2722 if (!dont_fchdir)
2723 {
2724 fd = open(".", O_RDONLY | O_EXTRA, 0);
2725 if (fd >= 0 && fchdir(fd) < 0)
2726 {
2727 close(fd);
2728 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002729 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
Bram Moolenaar0f873732019-12-05 20:28:46 +01002734 // Only change directory when we are sure we can return to where
2735 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002737#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 (mch_dirname(olddir, MAXPATHL) == FAIL
2741 || mch_chdir((char *)olddir) != 0))
2742 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002743 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 retval = FAIL;
2745 }
2746 else
2747 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002748 // The directory is copied into buf[], to be able to remove
2749 // the file name without changing it (could be a string in
2750 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (p - fname >= len)
2752 retval = FAIL;
2753 else
2754 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002755 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002757 {
2758 // Path does not exist (yet). For a full path fail,
2759 // will use the path as-is. For a relative path use
2760 // the current directory and append the file name.
2761 if (mch_isFullName(fname))
2762 retval = FAIL;
2763 else
2764 p = NULL;
2765 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002766 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002768 else
2769 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 *buf = NUL;
2771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
2773 }
2774 if (mch_dirname(buf, len) == FAIL)
2775 {
2776 retval = FAIL;
2777 *buf = NUL;
2778 }
2779 if (p != NULL)
2780 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002781#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 if (fd >= 0)
2783 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002784 if (p_verbose >= 5)
2785 {
2786 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002787 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002788 verbose_leave();
2789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
2792 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 l = mch_chdir((char *)olddir);
2795 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002796 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
itchyny051a40c2021-10-20 10:00:05 +01002798#ifdef HAVE_FCHDIR
2799 if (fd >= 0)
2800 close(fd);
2801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
2803 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002804 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002805 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002806#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002807 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002809 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002812
Bram Moolenaar0f873732019-12-05 20:28:46 +01002813 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002814 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 return FAIL;
2816
Bram Moolenaar0f873732019-12-05 20:28:46 +01002817 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 if (STRCMP(fname, ".") != 0)
2819 STRCAT(buf, fname);
2820
2821 return OK;
2822}
2823
2824/*
2825 * Return TRUE if "fname" does not depend on the current directory.
2826 */
2827 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002828mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002830#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 return ( fname[0] == '/' || fname[0] == '.' ||
2832 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2833 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2834 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002835#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837#endif
2838}
2839
Bram Moolenaar24552be2005-12-10 20:17:30 +00002840#if defined(USE_FNAME_CASE) || defined(PROTO)
2841/*
2842 * Set the case of the file name, if it already exists. This will cause the
2843 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002844 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002845 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002846 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002847fname_case(
2848 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002849 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002850{
2851 struct stat st;
2852 char_u *slash, *tail;
2853 DIR *dirp;
2854 struct dirent *dp;
2855
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002856 if (mch_lstat((char *)name, &st) < 0)
2857 return;
2858
2859 // Open the directory where the file is located.
2860 slash = vim_strrchr(name, '/');
2861 if (slash == NULL)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002862 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002863 dirp = opendir(".");
2864 tail = name;
2865 }
2866 else
2867 {
2868 *slash = NUL;
2869 dirp = opendir((char *)name);
2870 *slash = '/';
2871 tail = slash + 1;
2872 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002873
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002874 if (dirp == NULL)
2875 return;
2876
2877 while ((dp = readdir(dirp)) != NULL)
2878 {
2879 // Only accept names that differ in case and are the same byte
2880 // length. TODO: accept different length name.
2881 if (STRICMP(tail, dp->d_name) == 0
2882 && STRLEN(tail) == STRLEN(dp->d_name))
Bram Moolenaar24552be2005-12-10 20:17:30 +00002883 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002884 char_u newname[MAXPATHL + 1];
2885 struct stat st2;
2886
2887 // Verify the inode is equal.
2888 vim_strncpy(newname, name, MAXPATHL);
2889 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2890 MAXPATHL - (tail - name));
2891 if (mch_lstat((char *)newname, &st2) >= 0
2892 && st.st_ino == st2.st_ino
2893 && st.st_dev == st2.st_dev)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002894 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002895 STRCPY(tail, dp->d_name);
2896 break;
Bram Moolenaar24552be2005-12-10 20:17:30 +00002897 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002898 }
2899 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002900
2901 closedir(dirp);
Bram Moolenaar24552be2005-12-10 20:17:30 +00002902}
2903#endif
2904
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905/*
2906 * Get file permissions for 'name'.
2907 * Returns -1 when it doesn't exist.
2908 */
2909 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002910mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911{
2912 struct stat statb;
2913
Bram Moolenaar0f873732019-12-05 20:28:46 +01002914 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915#ifdef VMS
2916 if (stat((char *)vms_fixfilename(name), &statb))
2917#else
2918 if (stat((char *)name, &statb))
2919#endif
2920 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002921#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002922 // The top bit makes the value negative, which means the file doesn't
2923 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002924 return statb.st_mode & ~S_ADDACE;
2925#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928}
2929
2930/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002931 * Set file permission for "name" to "perm".
2932 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 */
2934 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002935mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936{
2937 return (chmod((char *)
2938#ifdef VMS
2939 vms_fixfilename(name),
2940#else
2941 name,
2942#endif
2943 (mode_t)perm) == 0 ? OK : FAIL);
2944}
2945
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002946#if defined(HAVE_FCHMOD) || defined(PROTO)
2947/*
2948 * Set file permission for open file "fd" to "perm".
2949 * Return FAIL for failure, OK otherwise.
2950 */
2951 int
2952mch_fsetperm(int fd, long perm)
2953{
2954 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2955}
2956#endif
2957
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958#if defined(HAVE_ACL) || defined(PROTO)
2959# ifdef HAVE_SYS_ACL_H
2960# include <sys/acl.h>
2961# endif
2962# ifdef HAVE_SYS_ACCESS_H
2963# include <sys/access.h>
2964# endif
2965
2966# ifdef HAVE_SOLARIS_ACL
2967typedef struct vim_acl_solaris_T {
2968 int acl_cnt;
2969 aclent_t *acl_entry;
2970} vim_acl_solaris_T;
2971# endif
2972
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002973#if defined(HAVE_SELINUX) || defined(PROTO)
2974/*
2975 * Copy security info from "from_file" to "to_file".
2976 */
2977 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002978mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002979{
2980 if (from_file == NULL)
2981 return;
2982
2983 if (selinux_enabled == -1)
2984 selinux_enabled = is_selinux_enabled();
2985
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002986 if (selinux_enabled <= 0)
2987 return;
2988
2989 // Use "char *" instead of "security_context_t" to avoid a deprecation
2990 // warning.
2991 char *from_context = NULL;
2992 char *to_context = NULL;
2993
2994 if (getfilecon((char *)from_file, &from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002995 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002996 // If the filesystem doesn't support extended attributes,
2997 // the original had no special security context and the
2998 // target cannot have one either.
2999 if (errno == EOPNOTSUPP)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003000 return;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003001
3002 msg_puts(_("\nCould not get security context for "));
3003 msg_outtrans(from_file);
3004 msg_putchar('\n');
3005 return;
3006 }
3007 if (getfilecon((char *)to_file, &to_context) < 0)
3008 {
3009 msg_puts(_("\nCould not get security context for "));
3010 msg_outtrans(to_file);
3011 msg_putchar('\n');
3012 freecon (from_context);
3013 return ;
3014 }
3015 if (strcmp(from_context, to_context) != 0)
3016 {
3017 if (setfilecon((char *)to_file, from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003018 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003019 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003020 msg_outtrans(to_file);
3021 msg_putchar('\n');
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003022 }
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003023 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003024 freecon(to_context);
3025 freecon(from_context);
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003026}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003027#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003028
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003029#if defined(HAVE_SMACK) && !defined(PROTO)
3030/*
3031 * Copy security info from "from_file" to "to_file".
3032 */
3033 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01003034mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003035{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02003036 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003037 {
3038 XATTR_NAME_SMACK,
3039 XATTR_NAME_SMACKEXEC,
3040 XATTR_NAME_SMACKMMAP
3041 };
3042
3043 char buffer[SMACK_LABEL_LEN];
3044 const char *name;
3045 int index;
3046 int ret;
3047 ssize_t size;
3048
3049 if (from_file == NULL)
3050 return;
3051
Christian Brabandt5a679b22023-10-16 10:17:13 +02003052 size = listxattr((char *)from_file, NULL, 0);
3053 // not supported or no attributes to copy
Paul R. Tagliamonte14759de2024-03-10 08:35:10 +01003054 if (size <= 0)
Christian Brabandt5a679b22023-10-16 10:17:13 +02003055 return;
3056
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003057 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
3058 / sizeof(smack_copied_attributes)[0]) ; index++)
3059 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003060 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003061 name = smack_copied_attributes[index];
3062
Bram Moolenaar0f873732019-12-05 20:28:46 +01003063 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003064 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
3065 if (size >= 0)
3066 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003067 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003068 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
3069 if (ret < 0)
3070 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003071 vim_snprintf((char *)IObuff, IOSIZE,
3072 _("Could not set security context %s for %s"),
3073 name, to_file);
3074 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003075 msg_putchar('\n');
3076 }
3077 }
3078 else
3079 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003080 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003081 switch (errno)
3082 {
3083 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003084 // extended attributes aren't supported or enabled
3085 // should a message be echoed? not sure...
3086 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003087
3088 case ERANGE:
3089 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003090 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003091 vim_snprintf((char *)IObuff, IOSIZE,
3092 _("Could not get security context %s for %s. Removing it!"),
3093 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003094 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003095 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003096 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003097
3098 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003099 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003100 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003101 // Silently ignore errors, apparently this happens when
3102 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003103 break;
3104 }
3105 }
3106 }
3107}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003108#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003109
Christian Brabandte085dfd2023-09-30 12:49:18 +02003110#ifdef FEAT_XATTR
3111/*
3112 * Copy extended attributes from_file to to_file
3113 */
3114 void
3115mch_copy_xattr(char_u *from_file, char_u *to_file)
3116{
3117 char *xattr_buf;
Paul R. Tagliamonte14759de2024-03-10 08:35:10 +01003118 ssize_t size;
3119 ssize_t tsize;
Christian Brabandte085dfd2023-09-30 12:49:18 +02003120 ssize_t keylen, vallen, max_vallen = 0;
3121 char *key;
3122 char *val = NULL;
3123 char *errmsg = NULL;
3124
3125 if (from_file == NULL)
3126 return;
3127
3128 // get the length of the extended attributes
3129 size = listxattr((char *)from_file, NULL, 0);
3130 // not supported or no attributes to copy
Paul R. Tagliamonte14759de2024-03-10 08:35:10 +01003131 if (size <= 0)
Christian Brabandte085dfd2023-09-30 12:49:18 +02003132 return;
3133 xattr_buf = (char*)alloc(size);
3134 if (xattr_buf == NULL)
3135 return;
3136 size = listxattr((char *)from_file, xattr_buf, size);
3137 tsize = size;
3138
3139 errno = 0;
3140
3141 for (int round = 0; round < 2; round++)
3142 {
3143
3144 key = xattr_buf;
3145 if (round == 1)
3146 size = tsize;
3147
3148 while (size > 0)
3149 {
3150 vallen = getxattr((char *)from_file, key,
3151 val, round ? max_vallen : 0);
3152 // only set the attribute in the second round
3153 if (vallen >= 0 && round &&
3154 setxattr((char *)to_file, key, val, vallen, 0) == 0)
3155 ;
3156 else if (errno)
3157 {
3158 switch (errno)
3159 {
3160 case E2BIG:
3161 errmsg = e_xattr_e2big;
3162 goto error_exit;
3163 case ENOTSUP:
Gene C993b1752023-10-02 22:42:26 +02003164 case EACCES:
3165 case EPERM:
3166 break;
Christian Brabandte085dfd2023-09-30 12:49:18 +02003167 case ERANGE:
3168 errmsg = e_xattr_erange;
3169 goto error_exit;
3170 default:
3171 errmsg = e_xattr_other;
3172 goto error_exit;
3173 }
3174 }
3175
3176 if (round == 0 && vallen > max_vallen)
3177 max_vallen = vallen;
3178
3179 // add one for terminating null
3180 keylen = STRLEN(key) + 1;
3181 size -= keylen;
3182 key += keylen;
3183 }
3184 if (round)
3185 break;
3186
3187 val = (char*)alloc(max_vallen + 1);
3188 if (val == NULL)
3189 goto error_exit;
3190
3191 }
3192error_exit:
3193 vim_free(xattr_buf);
3194 vim_free(val);
3195
3196 if (errmsg != NULL)
zeertzjq7ece0362023-10-01 09:07:14 +02003197 emsg(_(errmsg));
Christian Brabandte085dfd2023-09-30 12:49:18 +02003198}
3199#endif
3200
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201/*
3202 * Return a pointer to the ACL of file "fname" in allocated memory.
3203 * Return NULL if the ACL is not available for whatever reason.
3204 */
3205 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003206mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207{
3208 vim_acl_T ret = NULL;
3209#ifdef HAVE_POSIX_ACL
3210 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3211#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003212#ifdef HAVE_SOLARIS_ZFS_ACL
3213 acl_t *aclent;
3214
3215 if (acl_get((char *)fname, 0, &aclent) < 0)
3216 return NULL;
3217 ret = (vim_acl_T)aclent;
3218#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219#ifdef HAVE_SOLARIS_ACL
3220 vim_acl_solaris_T *aclent;
3221
3222 aclent = malloc(sizeof(vim_acl_solaris_T));
3223 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3224 {
3225 free(aclent);
3226 return NULL;
3227 }
3228 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3229 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3230 {
3231 free(aclent->acl_entry);
3232 free(aclent);
3233 return NULL;
3234 }
3235 ret = (vim_acl_T)aclent;
3236#else
3237#if defined(HAVE_AIX_ACL)
3238 int aclsize;
3239 struct acl *aclent;
3240
3241 aclsize = sizeof(struct acl);
3242 aclent = malloc(aclsize);
3243 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3244 {
3245 if (errno == ENOSPC)
3246 {
3247 aclsize = aclent->acl_len;
3248 aclent = realloc(aclent, aclsize);
3249 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3250 {
3251 free(aclent);
3252 return NULL;
3253 }
3254 }
3255 else
3256 {
3257 free(aclent);
3258 return NULL;
3259 }
3260 }
3261 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003262#endif // HAVE_AIX_ACL
3263#endif // HAVE_SOLARIS_ACL
3264#endif // HAVE_SOLARIS_ZFS_ACL
3265#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 return ret;
3267}
3268
3269/*
3270 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3271 */
3272 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003273mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274{
3275 if (aclent == NULL)
3276 return;
3277#ifdef HAVE_POSIX_ACL
3278 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3279#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003280#ifdef HAVE_SOLARIS_ZFS_ACL
3281 acl_set((char *)fname, (acl_t *)aclent);
3282#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283#ifdef HAVE_SOLARIS_ACL
3284 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3285 ((vim_acl_solaris_T *)aclent)->acl_entry);
3286#else
3287#ifdef HAVE_AIX_ACL
3288 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003289#endif // HAVE_AIX_ACL
3290#endif // HAVE_SOLARIS_ACL
3291#endif // HAVE_SOLARIS_ZFS_ACL
3292#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293}
3294
3295 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003296mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 if (aclent == NULL)
3299 return;
3300#ifdef HAVE_POSIX_ACL
3301 acl_free((acl_t)aclent);
3302#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003303#ifdef HAVE_SOLARIS_ZFS_ACL
3304 acl_free((acl_t *)aclent);
3305#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#ifdef HAVE_SOLARIS_ACL
3307 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3308 free(aclent);
3309#else
3310#ifdef HAVE_AIX_ACL
3311 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003312#endif // HAVE_AIX_ACL
3313#endif // HAVE_SOLARIS_ACL
3314#endif // HAVE_SOLARIS_ZFS_ACL
3315#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316}
3317#endif
3318
3319/*
3320 * Set hidden flag for "name".
3321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003323mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003325 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326}
3327
3328/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003329 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 * return FALSE if "name" is not a directory
3331 * return FALSE for error
3332 */
3333 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003334mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
3336 struct stat statb;
3337
Bram Moolenaar0f873732019-12-05 20:28:46 +01003338 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 return FALSE;
3340 if (stat((char *)name, &statb))
3341 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343}
3344
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003345/*
3346 * return TRUE if "name" is a directory, NOT a symlink to a directory
3347 * return FALSE if "name" is not a directory
3348 * return FALSE for error
3349 */
3350 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003351mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003352{
3353 struct stat statb;
3354
Bram Moolenaar0f873732019-12-05 20:28:46 +01003355 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003356 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003357 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003358 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003359 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003360}
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362/*
3363 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3364 */
3365 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003366executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
3368 struct stat st;
3369
3370 if (stat((char *)name, &st))
3371 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003372#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003373 // Like on Unix system file can have executable rights but not necessarily
3374 // be an executable, but on Unix is not a default for an ordinary file to
3375 // have an executable flag - on VMS it is in most cases.
3376 // Therefore, this check does not have any sense - let keep us to the
3377 // conventions instead:
3378 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003379 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003380 int vms_executable = 0;
3381 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3382 {
3383 if (strstr(vms_tolower((char*)name),".exe") != NULL
3384 || strstr(vms_tolower((char*)name),".com")!= NULL)
3385 vms_executable = 1;
3386 }
3387 return vms_executable;
3388#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391}
3392
3393/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003394 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003395 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 * Return -1 if unknown.
3397 */
3398 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003399mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 char_u *buf;
3402 char_u *p, *e;
3403 int retval;
3404
Bram Moolenaar0f873732019-12-05 20:28:46 +01003405 // When "use_path" is false and if it's an absolute or relative path don't
3406 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003407 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003408 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003409 // There must be a path separator, files in the current directory
3410 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003411 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003412 {
3413 if (path != NULL)
3414 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003415 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003416 *path = FullName_save(name, TRUE);
3417 else
3418 *path = vim_strsave(name);
3419 }
3420 return TRUE;
3421 }
3422 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425 p = (char_u *)getenv("PATH");
3426 if (p == NULL || *p == NUL)
3427 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003428 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if (buf == NULL)
3430 return -1;
3431
3432 /*
3433 * Walk through all entries in $PATH to check if "name" exists there and
3434 * is an executable file.
3435 */
3436 for (;;)
3437 {
3438 e = (char_u *)strchr((char *)p, ':');
3439 if (e == NULL)
3440 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003441 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 STRCPY(buf, "./");
3443 else
3444 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003445 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 add_pathsep(buf);
3447 }
3448 STRCAT(buf, name);
3449 retval = executable_file(buf);
3450 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003451 {
3452 if (path != NULL)
3453 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003454 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003455 *path = FullName_save(buf, TRUE);
3456 else
3457 *path = vim_strsave(buf);
3458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
3462 if (*e != ':')
3463 break;
3464 p = e + 1;
3465 }
3466
3467 vim_free(buf);
3468 return retval;
3469}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
3471/*
3472 * Check what "name" is:
3473 * NODE_NORMAL: file or directory (or doesn't exist)
3474 * NODE_WRITABLE: writable device, socket, fifo, etc.
3475 * NODE_OTHER: non-writable things
3476 */
3477 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003478mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
3480 struct stat st;
3481
3482 if (stat((char *)name, &st))
3483 return NODE_NORMAL;
3484 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3485 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003486 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003488 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 return NODE_WRITABLE;
3490}
3491
3492 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003493mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495#ifdef HAVE_CHECK_STACK_GROWTH
3496 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 check_stack_growth((char *)&i);
3499
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003500# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 get_stack_limit();
3502# endif
3503
3504#endif
3505
3506 /*
3507 * Setup an alternative stack for signals. Helps to catch signals when
3508 * running out of stack space.
3509 * Use of sigaltstack() is preferred, it's more portable.
3510 * Ignore any errors.
3511 */
3512#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003513 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 init_signal_stack();
3515#endif
3516}
3517
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003518#if defined(EXITFREE) || defined(PROTO)
3519 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003520mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003521{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003522# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3523 if (clip_star.owned)
3524 clip_lose_selection(&clip_star);
3525 if (clip_plus.owned)
3526 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003527# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003528# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003529 if (xterm_Shell != (Widget)0)
3530 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003531# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003532 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003533 if (xterm_dpy != NULL)
3534 XtCloseDisplay(xterm_dpy);
3535 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003536 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003537 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003538# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003539 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003540# endif
3541 }
3542# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003543# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003544# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003545 if (x11_display != NULL
3546# ifdef FEAT_XCLIPBOARD
3547 && x11_display != xterm_dpy
3548# endif
3549 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003550 XCloseDisplay(x11_display);
3551# endif
3552# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003553 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003554# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003555 vim_free(oldtitle);
3556 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003557}
3558#endif
3559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560/*
3561 * Output a newline when exiting.
3562 * Make sure the newline goes to the same stream as the text.
3563 */
3564 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003565exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003567 if (silent_mode)
3568 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 if (newline_on_exit || msg_didout)
3570 {
3571 if (msg_use_printf())
3572 {
3573 if (info_message)
3574 mch_msg("\n");
3575 else
3576 mch_errmsg("\r\n");
3577 }
3578 else
3579 out_char('\n');
3580 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003581 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003583 restore_cterm_colors(); // get original colors back
3584 msg_clr_eos_force(); // clear the rest of the display
3585 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
3587}
3588
Bram Moolenaarb4151682020-05-11 22:13:28 +02003589#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003590# if (defined(__GNUC__) \
3591 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3592 || (defined(__clang__) && (__clang_major__ >= 12))
3593extern void __gcov_dump(void);
3594extern void __gcov_reset(void);
3595# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3596# else
3597extern void __gcov_flush(void);
3598# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003599#endif
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003602mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603{
3604 exiting = TRUE;
3605
3606#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3607 x11_export_final_selection();
3608#endif
3609
3610#ifdef FEAT_GUI
3611 if (!gui.in_use)
3612#endif
3613 {
3614 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003615 if (!is_not_a_term())
3616 {
3617 // restore xterm title and icon name
3618 mch_restore_title(SAVE_RESTORE_BOTH);
3619 term_pop_title(SAVE_RESTORE_BOTH);
3620 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003621
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 /*
3623 * When t_ti is not empty but it doesn't cause swapping terminal
3624 * pages, need to output a newline when msg_didout is set. But when
3625 * t_ti does swap pages it should not go to the shell page. Do this
3626 * before stoptermcap().
3627 */
3628 if (swapping_screen() && !newline_on_exit)
3629 exit_scroll();
3630
Bram Moolenaar0f873732019-12-05 20:28:46 +01003631 // Stop termcap: May need to check for T_CRV response, which
3632 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 stoptermcap();
3634
3635 /*
3636 * A newline is only required after a message in the alternate screen.
3637 * This is set to TRUE by wait_return().
3638 */
3639 if (!swapping_screen() || newline_on_exit)
3640 exit_scroll();
3641
Bram Moolenaar0f873732019-12-05 20:28:46 +01003642 // Cursor may have been switched off without calling starttermcap()
3643 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 if (full_screen)
3645 cursor_on();
3646 }
3647 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003648 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003649
3650#ifdef USE_GCOV_FLUSH
3651 // Flush coverage info before possibly being killed by a deadly signal.
3652 __gcov_flush();
3653#endif
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 may_core_dump();
3656#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 gui_exit(r);
3659#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003660
Bram Moolenaar56718732006-03-15 22:53:57 +00003661#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003662 mac_conv_cleanup();
3663#endif
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003666 // A core dump won't be created if the signal handler
3667 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 if (deadly_signal != 0)
3669 return;
3670#endif
3671
Bram Moolenaar009b2592004-10-24 19:18:58 +00003672#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003673 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003674#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003675
3676#ifdef EXITFREE
3677 free_all_mem();
3678#endif
3679
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 exit(r);
3681}
3682
3683 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003684may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685{
3686 if (deadly_signal != 0)
3687 {
ichizok378447f2023-05-11 22:25:42 +01003688 mch_signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003689 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691}
3692
3693#ifndef VMS
3694
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003695/*
3696 * Get the file descriptor to use for tty operations.
3697 */
3698 static int
3699get_tty_fd(int fd)
3700{
3701 int tty_fd = fd;
3702
3703#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3704 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3705 // "fd" when it is a master device.
3706 if (mch_isatty(fd) > 1)
3707 {
3708 char *name;
3709
3710 name = ptsname(fd);
3711 if (name == NULL)
3712 return -1;
3713
3714 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3715 if (tty_fd < 0)
3716 return -1;
3717 }
3718#endif
3719 return tty_fd;
3720}
3721
3722 static int
3723mch_tcgetattr(int fd, void *term)
3724{
3725 int tty_fd;
3726 int retval = -1;
3727
3728 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003729 if (tty_fd < 0)
3730 return -1;
3731
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003732#ifdef NEW_TTY_SYSTEM
3733# ifdef HAVE_TERMIOS_H
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003734 retval = tcgetattr(tty_fd, (struct termios *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003735# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003736 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003737# endif
3738#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003739 // for "old" tty systems
3740 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003741#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003742 if (tty_fd != fd)
3743 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003744 return retval;
3745}
3746
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003748mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749{
3750 static int first = TRUE;
3751
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003752#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753# ifdef HAVE_TERMIOS_H
3754 static struct termios told;
3755 struct termios tnew;
3756# else
3757 static struct termio told;
3758 struct termio tnew;
3759# endif
3760
3761 if (first)
3762 {
3763 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003764 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766
3767 tnew = told;
3768 if (tmode == TMODE_RAW)
3769 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003770 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003771 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
Anton Sharonov49528da2024-04-14 20:02:24 +02003772 tnew.c_iflag &= ~(ICRNL |
3773 (T_XON == NULL || *T_XON == NUL ? IXON : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003775# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003776 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777# endif
3778 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003779# ifdef ONLCR
3780 // Don't map NL -> CR NL, we do it ourselves.
3781 // Also disable expanding tabs if possible.
3782# ifdef XTABS
3783 tnew.c_oflag &= ~(ONLCR | XTABS);
3784# else
3785# ifdef TAB3
3786 tnew.c_oflag &= ~(ONLCR | TAB3);
3787# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003789# endif
3790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003792 tnew.c_cc[VMIN] = 1; // return after 1 char
3793 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003796 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003797 // Also reset ICANON here, otherwise on Solaris select() won't see
3798 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003799 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003800 tnew.c_cc[VMIN] = 1; // return after 1 char
3801 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
3804# if defined(HAVE_TERMIOS_H)
3805 {
3806 int n = 10;
3807
Bram Moolenaar0f873732019-12-05 20:28:46 +01003808 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3809 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3811 && errno == EINTR && n > 0)
3812 --n;
3813 }
3814# else
3815 ioctl(read_cmd_fd, TCSETA, &tnew);
3816# endif
3817
3818#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 /*
3820 * for "old" tty systems
3821 */
3822# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003823# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824# endif
3825 static struct sgttyb ttybold;
3826 struct sgttyb ttybnew;
3827
3828 if (first)
3829 {
3830 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003831 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833
3834 ttybnew = ttybold;
3835 if (tmode == TMODE_RAW)
3836 {
3837 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3838 ttybnew.sg_flags |= RAW;
3839 }
3840 else if (tmode == TMODE_SLEEP)
3841 ttybnew.sg_flags &= ~(ECHO);
3842 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3843#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003844 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845}
3846
3847/*
3848 * Try to get the code for "t_kb" from the stty setting
3849 *
3850 * Even if termcap claims a backspace key, the user's setting *should*
3851 * prevail. stty knows more about reality than termcap does, and if
3852 * somebody's usual erase key is DEL (which, for most BSD users, it will
3853 * be), they're going to get really annoyed if their erase key starts
3854 * doing forward deletes for no reason. (Eric Fischer)
3855 */
3856 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003857get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003859 ttyinfo_T info;
3860 char_u buf[2];
3861 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003863 if (get_tty_info(read_cmd_fd, &info) != OK)
3864 return;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003865
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003866 intr_char = info.interrupt;
3867 buf[0] = info.backspace;
3868 buf[1] = NUL;
3869 add_termcode((char_u *)"kb", buf, FALSE);
3870
3871 // If <BS> and <DEL> are now the same, redefine <DEL>.
3872 p = find_termcode((char_u *)"kD");
3873 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3874 do_fixdel(NULL);
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003875}
3876
3877/*
3878 * Obtain the characters that Backspace and Enter produce on "fd".
3879 * Returns OK or FAIL.
3880 */
3881 int
3882get_tty_info(int fd, ttyinfo_T *info)
3883{
3884#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885# ifdef HAVE_TERMIOS_H
3886 struct termios keys;
3887# else
3888 struct termio keys;
3889# endif
3890
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003891 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003893 info->backspace = keys.c_cc[VERASE];
3894 info->interrupt = keys.c_cc[VINTR];
3895 if (keys.c_iflag & ICRNL)
3896 info->enter = NL;
3897 else
3898 info->enter = CAR;
3899 if (keys.c_oflag & ONLCR)
3900 info->nl_does_cr = TRUE;
3901 else
3902 info->nl_does_cr = FALSE;
3903 return OK;
3904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003906 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 struct sgttyb keys;
3908
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003909 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003911 info->backspace = keys.sg_erase;
3912 info->interrupt = keys.sg_kill;
3913 info->enter = CAR;
3914 info->nl_does_cr = TRUE;
3915 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003918 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919}
3920
Bram Moolenaar0f873732019-12-05 20:28:46 +01003921#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003923static int mouse_ison = FALSE;
3924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925/*
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003926 * Set mouse clicks on or off and possible enable mouse movement events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 */
3928 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003929mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003931#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003932 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 int xterm_mouse_vers;
3935
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003936#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003937 if (!on)
3938 // Make sure not tracing mouse movements. Important when a button-down
3939 // was received but no release yet.
3940 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003941#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003942
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003943 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003944#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003945 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003946#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003947 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003948 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 return;
3950
3951 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003952
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003953#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003954 if (ttym_flags == TTYM_URXVT)
3955 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003956 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003957 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003958 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003959#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003960
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00003961 if (T_CXM != NULL && *T_CXM != NUL)
3962 {
3963 term_enable_mouse(on);
3964 }
3965 else if (ttym_flags == TTYM_SGR)
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003966 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003967 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003968 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003969 mouse_ison = on;
3970 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003971
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003972#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003973 if (bevalterm_ison != (p_bevalterm && on))
3974 {
3975 bevalterm_ison = (p_bevalterm && on);
3976 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003977 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003978 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003979 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003980#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003981
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 if (xterm_mouse_vers > 0)
3983 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003984 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 out_str_nf((char_u *)
3986 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003987 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003988#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003989 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003990#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003991 "\033[?1002h")
3992 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003993 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003995 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003996 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
3998
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003999#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 else if (ttym_flags == TTYM_DEC)
4001 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004002 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01004004 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004006 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004010#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 else
4012 {
4013 if (on)
4014 {
4015 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004016 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018 else
4019 {
4020 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004021 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 }
4023 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004026#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004027 else
4028 {
4029 if (on)
4030 {
4031 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004032 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004033 }
4034 else
4035 {
4036 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004037 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004038 }
4039 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004040#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004041
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004042#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 else
4044 {
4045 if (on)
4046 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004047 // D - Enable Mouse up/down messages
4048 // L - Enable Left Button Reporting
4049 // M - Enable Middle Button Reporting
4050 // R - Enable Right Button Reporting
4051 // K - Enable SHIFT and CTRL key Reporting
4052 // + - Enable Advanced messaging of mouse moves and up/down messages
4053 // Q - Quiet No Ack
4054 // # - Numeric value of mouse pointer required
4055 // 0 = Multiview 2000 cursor, used as standard
4056 // 1 = Windows Arrow
4057 // 2 = Windows I Beam
4058 // 3 = Windows Hour Glass
4059 // 4 = Windows Cross Hair
4060 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004061# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01004062 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004063 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004064# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004065 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004066# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004067 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
4069 else
4070 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004071 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004072 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004075#endif
4076#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 else
4078 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004079 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 if (on)
4081 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
4082 else
4083 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004084 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087}
4088
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004089#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004090/*
4091 * Called when 'balloonevalterm' changed.
4092 */
4093 void
4094mch_bevalterm_changed(void)
4095{
4096 mch_setmouse(mouse_ison);
4097}
4098#endif
4099
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100/*
4101 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
4102 */
4103 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004104check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105{
4106# ifdef FEAT_MOUSE_XTERM
4107 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02004108# ifdef FEAT_MOUSE_URXVT
4109 && use_xterm_mouse() != 3
4110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111# ifdef FEAT_GUI
4112 && !gui.in_use
4113# endif
4114 )
4115 {
4116 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004117 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 if (*p_mouse != NUL)
4119 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004120 // force mouse off and maybe on to send possibly new mouse
4121 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 mch_setmouse(FALSE);
4123 setmouse();
4124 }
4125 }
4126 else
4127 del_mouse_termcode(KS_MOUSE);
4128# endif
4129
4130# ifdef FEAT_MOUSE_GPM
4131 if (!use_xterm_mouse()
4132# ifdef FEAT_GUI
4133 && !gui.in_use
4134# endif
4135 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004136 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02004137 else
4138 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139# endif
4140
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004141# ifdef FEAT_SYSMOUSE
4142 if (!use_xterm_mouse()
4143# ifdef FEAT_GUI
4144 && !gui.in_use
4145# endif
4146 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004147 set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00004148# endif
4149
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01004151 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 if (!use_xterm_mouse()
4153# ifdef FEAT_GUI
4154 && !gui.in_use
4155# endif
4156 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004157 set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 else
4159 del_mouse_termcode(KS_JSBTERM_MOUSE);
4160# endif
4161
4162# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01004163 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
4164 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 if (!use_xterm_mouse()
4166# ifdef FEAT_GUI
4167 && !gui.in_use
4168# endif
4169 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004170 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 else
4172 del_mouse_termcode(KS_NETTERM_MOUSE);
4173# endif
4174
4175# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01004176 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004177 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178# ifdef FEAT_GUI
4179 && !gui.in_use
4180# endif
4181 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004182 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004183 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 else
4185 del_mouse_termcode(KS_DEC_MOUSE);
4186# endif
4187# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004188 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004189 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190# ifdef FEAT_GUI
4191 && !gui.in_use
4192# endif
4193 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004194 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 else
4196 del_mouse_termcode(KS_PTERM_MOUSE);
4197# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004198# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004199 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004200# ifdef FEAT_GUI
4201 && !gui.in_use
4202# endif
4203 )
4204 {
4205 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004206 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004207
4208 if (*p_mouse != NUL)
4209 {
4210 mch_setmouse(FALSE);
4211 setmouse();
4212 }
4213 }
4214 else
4215 del_mouse_termcode(KS_URXVT_MOUSE);
4216# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004217 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004218# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004219 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004220# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004221 )
4222 {
4223 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004224 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004225
4226 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004227 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004228
4229 if (*p_mouse != NUL)
4230 {
4231 mch_setmouse(FALSE);
4232 setmouse();
4233 }
4234 }
4235 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004236 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004237 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004238 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242#ifndef VMS
4243
4244/*
4245 * Try to get the current window size:
4246 * 1. with an ioctl(), most accurate method
4247 * 2. from the environment variables LINES and COLUMNS
4248 * 3. from the termcap
4249 * 4. keep using the old values
4250 * Return OK when size could be determined, FAIL otherwise.
4251 */
4252 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004253mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254{
4255 long rows = 0;
4256 long columns = 0;
4257 char_u *p;
4258
4259 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 * 1. try using an ioctl. It is the most accurate method.
4261 *
4262 * Try using TIOCGWINSZ first, some systems that have it also define
4263 * TIOCGSIZE but don't have a struct ttysize.
4264 */
4265# ifdef TIOCGWINSZ
4266 {
4267 struct winsize ws;
4268 int fd = 1;
4269
Bram Moolenaar0f873732019-12-05 20:28:46 +01004270 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (!isatty(fd) && isatty(read_cmd_fd))
4272 fd = read_cmd_fd;
4273 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4274 {
4275 columns = ws.ws_col;
4276 rows = ws.ws_row;
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004277# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004278 ch_log(NULL, "Got size with TIOCGWINSZ: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004282# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283# ifdef TIOCGSIZE
4284 {
4285 struct ttysize ts;
4286 int fd = 1;
4287
Bram Moolenaar0f873732019-12-05 20:28:46 +01004288 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 if (!isatty(fd) && isatty(read_cmd_fd))
4290 fd = read_cmd_fd;
4291 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4292 {
4293 columns = ts.ts_cols;
4294 rows = ts.ts_lines;
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004295# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004296 ch_log(NULL, "Got size with TIOCGSIZE: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004300# endif // TIOCGSIZE
4301# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
4303 /*
4304 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004305 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4306 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004308 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 {
4310 if ((p = (char_u *)getenv("LINES")))
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 rows = atoi((char *)p);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004313# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004314 ch_log(NULL, "Got 'lines' from $LINES: %ld", rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004315# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 if ((p = (char_u *)getenv("COLUMNS")))
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 columns = atoi((char *)p);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004320# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004321 ch_log(NULL, "Got 'columns' from $COLUMNS: %ld", columns);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004322# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 }
4325
4326#ifdef HAVE_TGETENT
4327 /*
4328 * 3. try reading "co" and "li" entries from termcap
4329 */
4330 if (columns == 0 || rows == 0)
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 getlinecol(&columns, &rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004333# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004334 ch_log(NULL, "Got size from termcap: %ld x %ld", columns, rows);
Bram Moolenaar545c8a52023-06-21 15:51:47 +01004335# endif
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337#endif
4338
4339 /*
4340 * 4. If everything fails, use the old values
4341 */
4342 if (columns <= 0 || rows <= 0)
4343 return FAIL;
4344
4345 Rows = rows;
4346 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004347 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 return OK;
4349}
4350
mikoto20001083cae2024-11-11 21:24:14 +01004351#if defined(FEAT_EVAL) || defined(PROTO)
4352 void
4353f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv)
4354{
4355 struct cellsize cs;
4356 mch_calc_cell_size(&cs);
4357
4358 if (rettv_list_alloc(rettv) == FAIL)
4359 return;
4360
mikoto2000de094dc2024-11-14 22:13:48 +01004361 // failed get pixel size.
4362 if (cs.cs_xpixel == -1)
4363 return;
4364
4365#if defined(FEAT_GUI)
4366 // gui return [].
4367 if (gui.in_use)
4368 return;
4369#endif
4370
4371 // success pixel size and no gui.
mikoto20001083cae2024-11-11 21:24:14 +01004372 list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_xpixel);
4373 list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_ypixel);
4374}
4375#endif
4376
4377/*
4378 * Try to get the current terminal cell size.
mikoto2000de094dc2024-11-14 22:13:48 +01004379 * On failure, returns -1x-1
mikoto20001083cae2024-11-11 21:24:14 +01004380 */
4381 void
4382mch_calc_cell_size(struct cellsize *cs_out)
4383{
mikoto2000de094dc2024-11-14 22:13:48 +01004384 // get current tty size.
4385 struct winsize ws;
4386 int fd = 1;
4387 int retval = -1;
4388 retval = ioctl(fd, TIOCGWINSZ, &ws);
4389
4390#ifdef FEAT_EVAL
4391 ch_log(NULL, "ioctl(TIOCGWINSZ) %s", retval == 0 ? "success" : "failed");
mikoto20001083cae2024-11-11 21:24:14 +01004392#endif
mikoto20001083cae2024-11-11 21:24:14 +01004393
mikoto2000de094dc2024-11-14 22:13:48 +01004394 if (retval == -1)
4395 {
4396 cs_out->cs_xpixel = -1;
4397 cs_out->cs_ypixel = -1;
4398 return;
4399 }
mikoto20001083cae2024-11-11 21:24:14 +01004400
mikoto2000de094dc2024-11-14 22:13:48 +01004401 // calculate parent tty's pixel per cell.
4402 int x_cell_size = ws.ws_xpixel / ws.ws_col;
4403 int y_cell_size = ws.ws_ypixel / ws.ws_row;
mikoto20001083cae2024-11-11 21:24:14 +01004404
mikoto2000de094dc2024-11-14 22:13:48 +01004405 // calculate current tty's pixel
4406 cs_out->cs_xpixel = x_cell_size;
4407 cs_out->cs_ypixel = y_cell_size;
4408
4409#ifdef FEAT_EVAL
4410 ch_log(NULL, "Got cell pixel size with TIOCGWINSZ: %d x %d", x_cell_size, y_cell_size);
mikoto20001083cae2024-11-11 21:24:14 +01004411#endif
4412}
4413
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004414#if defined(FEAT_TERMINAL) || defined(PROTO)
4415/*
4416 * Report the windows size "rows" and "cols" to tty "fd".
4417 */
4418 int
4419mch_report_winsize(int fd, int rows, int cols)
4420{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004421 int tty_fd;
4422 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004423
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004424 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004425 if (tty_fd < 0)
4426 return FAIL;
4427
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004428# if defined(TIOCSWINSZ)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004429 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004430
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004431 ws.ws_col = cols;
4432 ws.ws_row = rows;
mikoto20001083cae2024-11-11 21:24:14 +01004433
4434 // calcurate and set tty pixel size
4435 struct cellsize cs;
4436 mch_calc_cell_size(&cs);
mikoto2000de094dc2024-11-14 22:13:48 +01004437
4438 if (cs.cs_xpixel == -1)
4439 {
4440 // failed get pixel size.
4441 ws.ws_xpixel = 0;
4442 ws.ws_ypixel = 0;
4443 }
4444 else
4445 {
4446 ws.ws_xpixel = cols * cs.cs_xpixel;
4447 ws.ws_ypixel = rows * cs.cs_ypixel;
4448 }
mikoto20001083cae2024-11-11 21:24:14 +01004449
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004450 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004451 ch_log(NULL, "ioctl(TIOCSWINSZ) %s", retval == 0 ? "success" : "failed");
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004452# elif defined(TIOCSSIZE)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004453 struct ttysize ts;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004454
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004455 ts.ts_cols = cols;
4456 ts.ts_lines = rows;
4457 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
Bram Moolenaar55f1b822023-06-21 13:42:48 +01004458 ch_log(NULL, "ioctl(TIOCSSIZE) %s", retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004459# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004460 if (tty_fd != fd)
4461 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004462 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004463}
4464#endif
4465
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466/*
4467 * Try to set the window size to Rows and Columns.
4468 */
4469 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004470mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471{
4472 if (*T_CWS)
4473 {
4474 /*
4475 * NOTE: if you get an error here that term_set_winsize() is
4476 * undefined, check the output of configure. It could probably not
4477 * find a ncurses, termcap or termlib library.
4478 */
4479 term_set_winsize((int)Rows, (int)Columns);
4480 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004481 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483}
4484
Bram Moolenaar0f873732019-12-05 20:28:46 +01004485#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
4487/*
4488 * Rows and/or Columns has changed.
4489 */
4490 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004491mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004493 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494}
4495
Bram Moolenaar205b8862011-09-07 15:04:31 +02004496/*
4497 * Wait for process "child" to end.
4498 * Return "child" if it exited properly, <= 0 on error.
4499 */
4500 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004501wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004502{
4503 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004504 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004505
4506 while (wait_pid != child)
4507 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004508 // When compiled with Python threads are probably used, in which case
4509 // wait() sometimes hangs for no obvious reason. Use waitpid()
4510 // instead and loop (like the GUI). Also needed for other interfaces,
4511 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004512# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004513 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004514# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004515 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004516# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004517 if (wait_pid == 0)
4518 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004519 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004520 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004521 if (++delay_msec > 10)
4522 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004523 continue;
4524 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004525 if (wait_pid <= 0
4526# ifdef ECHILD
4527 && errno == ECHILD
4528# endif
4529 )
4530 break;
4531 }
4532 return wait_pid;
4533}
4534
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004535#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004536/*
4537 * Set the environment for a child process.
4538 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004539 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004540set_child_environment(
4541 long rows,
4542 long columns,
4543 char *term,
4544 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004545{
4546# ifdef HAVE_SETENV
4547 char envbuf[50];
4548# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004549 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004550 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004551 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004552 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004553 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004554# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004555 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004556# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004557# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004558 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004559# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004560# endif
4561
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004562# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004563 setenv("TERM", term, 1);
4564 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004565 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004566 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004567 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004568 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004569 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004570 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004571 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004572# ifdef FEAT_TERMINAL
4573 if (is_terminal)
4574 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004575 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004576 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4577 }
4578# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004579# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004580 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004581# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004582# else
4583 /*
4584 * Putenv does not copy the string, it has to remain valid.
4585 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004586 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004587 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004588 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4589 putenv(envbuf_Term);
4590 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004591 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004592 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4593 putenv(envbuf_Lines);
4594 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4595 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004596 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004597 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004598 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004599# ifdef FEAT_TERMINAL
4600 if (is_terminal)
4601 {
4602 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004603 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004604 putenv(envbuf_Version);
4605 }
4606# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004607# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004608 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4609 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4610 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004611# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004612# endif
4613}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004614
4615 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004616set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004617{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004618 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004619}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004620#endif
4621
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004622#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004623/*
4624 * Open a PTY, with FD for the master and slave side.
4625 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004626 * When successful both file descriptors are stored and the allocated pty name
4627 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004628 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004629 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004630open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004631{
4632 char *tty_name;
4633
Bram Moolenaar59386482019-02-10 22:43:46 +01004634 if (name1 != NULL)
4635 *name1 = NULL;
4636 if (name2 != NULL)
4637 *name2 = NULL;
4638
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004639 *pty_master_fd = mch_openpty(&tty_name); // open pty
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004640 if (*pty_master_fd < 0)
4641 return;
4642
4643 // Leaving out O_NOCTTY may lead to waitpid() always returning
4644 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4645 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004646#ifdef O_NOCTTY
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004647 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004648#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004649 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004650#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004651 if (*pty_slave_fd < 0)
4652 {
4653 close(*pty_master_fd);
4654 *pty_master_fd = -1;
4655 }
4656 else
4657 {
4658 if (name1 != NULL)
4659 *name1 = vim_strsave((char_u *)tty_name);
4660 if (name2 != NULL)
4661 *name2 = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004662 }
4663}
4664#endif
4665
Bram Moolenaarfae42832017-08-01 22:24:26 +02004666/*
4667 * Send SIGINT to a child process if "c" is an interrupt character.
4668 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004669 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004670may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4671{
4672# ifdef SIGINT
4673 if (c == Ctrl_C || c == intr_char)
4674 {
4675# ifdef HAVE_SETSID
4676 kill(-pid, SIGINT);
4677# else
4678 kill(0, SIGINT);
4679# endif
4680 if (wpid > 0)
4681 kill(wpid, SIGINT);
4682 }
4683# endif
4684}
4685
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004686#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004687
Bram Moolenaar0f873732019-12-05 20:28:46 +01004688/*
4689 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4690 * pointers, the last one is NULL.
4691 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4692 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004693 int
4694unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004695 char_u *cmd,
4696 char ***argvp,
4697 char_u **sh_tofree,
4698 char_u **shcf_tofree)
4699{
4700 char **argv = NULL;
4701 int argc;
4702
4703 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004704 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004705 return FAIL;
4706
4707 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4708 return FAIL;
4709 *argvp = argv;
4710
4711 if (cmd != NULL)
4712 {
4713 char_u *s;
4714 char_u *p;
4715
4716 if (extra_shell_arg != NULL)
4717 argv[argc++] = (char *)extra_shell_arg;
4718
Bram Moolenaar0f873732019-12-05 20:28:46 +01004719 // Break 'shellcmdflag' into white separated parts. This doesn't
4720 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004721 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004722 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004723 return FAIL;
4724 s = *shcf_tofree;
4725 p = p_shcf;
4726 while (*p != NUL)
4727 {
4728 argv[argc++] = (char *)s;
4729 while (*p && *p != ' ' && *p != TAB)
4730 *s++ = *p++;
4731 *s++ = NUL;
4732 p = skipwhite(p);
4733 }
4734
4735 argv[argc++] = (char *)cmd;
4736 }
4737 argv[argc] = NULL;
4738 return OK;
4739}
4740#endif
4741
4742#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4743/*
4744 * Use a terminal window to run a shell command in.
4745 */
4746 static int
4747mch_call_shell_terminal(
4748 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004749 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004750{
4751 jobopt_T opt;
4752 char **argv = NULL;
4753 char_u *tofree1 = NULL;
4754 char_u *tofree2 = NULL;
4755 int retval = -1;
4756 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004757 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004758 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004759 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004760
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004761 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004762 goto theend;
4763
4764 init_job_options(&opt);
4765 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4766 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004767 if (buf == NULL)
4768 goto theend;
4769
4770 job = term_getjob(buf->b_term);
4771 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004772
Bram Moolenaar0f873732019-12-05 20:28:46 +01004773 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004774 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004775 if (curbuf == buf)
Bram Moolenaar13568252018-03-16 20:46:58 +01004776 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004777 // Only when managed to find a window for "buf",
4778 clear_oparg(&oa);
4779 while (term_use_loop())
Bram Moolenaar13568252018-03-16 20:46:58 +01004780 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004781 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4782 {
4783 // If terminal_loop() returns OK we got a key that is handled
4784 // in Normal model. We don't do redrawing anyway.
4785 if (terminal_loop(TRUE) == OK)
4786 normal_cmd(&oa, TRUE);
4787 }
4788 else
Bram Moolenaar13568252018-03-16 20:46:58 +01004789 normal_cmd(&oa, TRUE);
4790 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00004791 retval = job->jv_exitval;
4792 ch_log(NULL, "system command finished");
4793
4794 job_unref(job);
4795
4796 // restore curwin/curbuf and a few other things
4797 aucmd_restbuf(&aco);
Bram Moolenaar13568252018-03-16 20:46:58 +01004798 }
Bram Moolenaar13568252018-03-16 20:46:58 +01004799
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01004800 // Only require pressing Enter when redrawing, to avoid that system() gets
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004801 // the hit-enter prompt even though it didn't output anything.
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004802 if (RedrawingDisabled == 0)
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004803 wait_return(TRUE);
Bram Moolenaar13568252018-03-16 20:46:58 +01004804 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4805
4806theend:
4807 vim_free(argv);
4808 vim_free(tofree1);
4809 vim_free(tofree2);
4810 return retval;
4811}
4812#endif
4813
4814#ifdef USE_SYSTEM
4815/*
4816 * Use system() to start the shell: simple but slow.
4817 */
4818 static int
4819mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004820 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004821 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822{
4823#ifdef VMS
4824 char *ifn = NULL;
4825 char *ofn = NULL;
4826#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004827 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004828 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004829 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830
4831 out_flush();
4832
4833 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004834 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835
Bram Moolenaar62b42182010-09-21 22:09:37 +02004836# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004837 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004838 loose_clipboard();
4839# endif
4840
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 if (cmd == NULL)
4842 x = system((char *)p_sh);
4843 else
4844 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004845# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 if (ofn = strchr((char *)cmd, '>'))
4847 *ofn++ = '\0';
4848 if (ifn = strchr((char *)cmd, '<'))
4849 {
4850 char *p;
4851
4852 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004853 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 if (p)
4855 *p = '\0';
4856 }
4857 if (ofn)
4858 x = vms_sys((char *)cmd, ofn, ifn);
4859 else
4860 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004861# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004862 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004864 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 if (newcmd == NULL)
4866 x = 0;
4867 else
4868 {
4869 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4870 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4871 (char *)p_shcf,
4872 (char *)cmd);
4873 x = system((char *)newcmd);
4874 vim_free(newcmd);
4875 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004876# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878# ifdef VMS
4879 x = vms_sys_status(x);
4880# endif
4881 if (emsg_silent)
4882 ;
4883 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004884 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 else if (x && !(options & SHELL_SILENT))
4886 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004887 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 msg_outnum((long)x);
4889 msg_putchar('\n');
4890 }
4891
4892 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004893 {
4894 // The shell may have messed with the mode, always set it.
4895 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004896 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004899# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4900 restore_clipboard();
4901# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004903}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904
Bram Moolenaar0f873732019-12-05 20:28:46 +01004905#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906
Bram Moolenaar0f873732019-12-05 20:28:46 +01004907# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4908 // 127, some shells use that already
4909# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910
Bram Moolenaar13568252018-03-16 20:46:58 +01004911/*
4912 * Don't use system(), use fork()/exec().
4913 */
4914 static int
4915mch_call_shell_fork(
4916 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004917 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004918{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004919 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004921 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 pid_t wait_pid = 0;
4923# ifdef HAVE_UNION_WAIT
4924 union wait status;
4925# else
4926 int status = -1;
4927# endif
4928 int retval = -1;
4929 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004930 char_u *tofree1 = NULL;
4931 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004933 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004934# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004936# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004937 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 int fd_fromshell[2];
4939 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004940 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941
4942 out_flush();
4943 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004944 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004945 if (tmode == TMODE_RAW)
4946 // The shell may have messed with the mode, always set it later.
4947 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004949 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004950 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004951
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004953 * For the GUI, when writing the output into the buffer and when reading
4954 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4955 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004957 if ((options & (SHELL_READ|SHELL_WRITE))
4958# ifdef FEAT_GUI
4959 || (gui.in_use && show_shell_mess)
4960# endif
4961 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004963# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 /*
4965 * Try to open a master pty.
4966 * If this works, open the slave pty.
4967 * If the slave can't be opened, close the master pty.
4968 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004969 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004970 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 /*
4972 * If not opening a pty or it didn't work, try using pipes.
4973 */
4974 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004975# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
4977 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004978 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
4980 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004981 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
4983 close(fd_toshell[0]);
4984 close(fd_toshell[1]);
4985 }
4986 }
4987 if (pipe_error)
4988 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004989 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 out_flush();
4991 }
4992 }
4993 }
4994
Bram Moolenaar0f873732019-12-05 20:28:46 +01004995 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004997 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004998 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004999 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005000 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005002 UNBLOCK_SIGNALS(&curset);
5003
Bram Moolenaar32526b32019-01-19 17:43:09 +01005004 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005005 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00005007 || (gui.in_use && show_shell_mess)
5008# endif
5009 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005011# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005012 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
5014 close(pty_master_fd);
5015 close(pty_slave_fd);
5016 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005017 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00005018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
5020 close(fd_toshell[0]);
5021 close(fd_toshell[1]);
5022 close(fd_fromshell[0]);
5023 close(fd_fromshell[1]);
5024 }
5025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005027 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005029 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005030 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005032# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01005033 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02005034 {
5035 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01005036 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02005037 }
Bram Moolenaar819524702018-02-27 19:10:00 +01005038# endif
5039
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 if (!show_shell_mess || (options & SHELL_EXPAND))
5041 {
5042 int fd;
5043
5044 /*
5045 * Don't want to show any message from the shell. Can't just
5046 * close stdout and stderr though, because some systems will
5047 * break if you try to write to them after that, so we must
5048 * use dup() to replace them with something else -- webb
5049 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
5050 * waiting for input.
5051 */
5052 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
5053 fclose(stdin);
5054 fclose(stdout);
5055 fclose(stderr);
5056
5057 /*
5058 * If any of these open()'s and dup()'s fail, we just continue
5059 * anyway. It's not fatal, and on most systems it will make
5060 * no difference at all. On a few it will cause the execvp()
5061 * to exit with a non-zero status even when the completion
5062 * could be done, which is nothing too serious. If the open()
5063 * or dup() failed we'd just do the same thing ourselves
5064 * anyway -- webb
5065 */
5066 if (fd >= 0)
5067 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005068 vim_ignored = dup(fd); // To replace stdin (fd 0)
5069 vim_ignored = dup(fd); // To replace stdout (fd 1)
5070 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
Bram Moolenaar0f873732019-12-05 20:28:46 +01005072 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 close(fd);
5074 }
5075 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005076 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00005078 || gui.in_use
5079# endif
5080 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
5082
Bram Moolenaardf177f62005-02-22 08:39:57 +00005083# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005084 // Create our own process group, so that the child and all its
5085 // children can be kill()ed. Don't do this when using pipes,
5086 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005087 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00005088 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005089 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00005090# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005091 // When doing "!xterm&" and 'shell' is bash: the shell
5092 // will exit and send SIGHUP to all processes in its
5093 // group, killing the just started process. Ignore SIGHUP
5094 // to avoid that. (suggested by Simon Schubert)
ichizok378447f2023-05-11 22:25:42 +01005095 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar07256082009-02-04 13:19:42 +00005096# endif
5097 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005098# endif
5099# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005100 if (pty_slave_fd >= 0)
5101 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005102 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005103 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005104 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01005105# ifdef TIOCSCTTY
5106 // Try to become controlling tty (probably doesn't work,
5107 // unless run by root)
5108 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5109# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005110 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005111# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005112 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113
Bram Moolenaara5792f52005-11-23 21:25:05 +00005114 /*
5115 * stderr is only redirected when using the GUI, so that a
5116 * program like gpg can still access the terminal to get a
5117 * passphrase using stderr.
5118 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005119# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 if (pty_master_fd >= 0)
5121 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005122 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
Bram Moolenaar0f873732019-12-05 20:28:46 +01005124 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005126 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005128 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00005129 if (gui.in_use)
5130 {
5131 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005132 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00005133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134
Bram Moolenaar0f873732019-12-05 20:28:46 +01005135 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 }
5137 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00005138# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005140 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 close(fd_toshell[1]);
5142 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005143 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 close(fd_toshell[0]);
5145
Bram Moolenaar0f873732019-12-05 20:28:46 +01005146 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 close(fd_fromshell[0]);
5148 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005149 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 close(fd_fromshell[1]);
5151
Bram Moolenaara5792f52005-11-23 21:25:05 +00005152# ifdef FEAT_GUI
5153 if (gui.in_use)
5154 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005155 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00005156 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02005157 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00005158 }
5159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
5161 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005162
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 /*
5164 * There is no type cast for the argv, because the type may be
5165 * different on different machines. This may cause a warning
5166 * message with strict compilers, don't worry about it.
5167 * Call _exit() instead of exit() to avoid closing the connection
5168 * to the X server (esp. with GTK, which uses atexit()).
5169 */
5170 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005171 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005173 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {
5175 /*
5176 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005177 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 */
5179 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005180 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005181 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005182# ifdef FEAT_JOB_CHANNEL
5183 ++dont_check_job_ended;
5184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 /*
5186 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005187 * This is also used to pipe stdin/stdout to/from the external
5188 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005190 if ((options & (SHELL_READ|SHELL_WRITE))
5191# ifdef FEAT_GUI
5192 || (gui.in_use && show_shell_mess)
5193# endif
5194 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005196# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01005198 int buffer_off = 0; // valid bytes in buffer[]
5199 char_u ta_buf[BUFLEN + 1]; // TypeAHead
5200 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 int len;
5202 int p_more_save;
5203 int old_State;
5204 int c;
5205 int toshell_fd;
5206 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005207 garray_T ga;
5208 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01005209# ifdef ELAPSED_FUNC
5210 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005211# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212
Bram Moolenaardf177f62005-02-22 08:39:57 +00005213# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (pty_master_fd >= 0)
5215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 fromshell_fd = pty_master_fd;
5217 toshell_fd = dup(pty_master_fd);
5218 }
5219 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00005220# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
5222 close(fd_toshell[0]);
5223 close(fd_fromshell[1]);
5224 toshell_fd = fd_toshell[1];
5225 fromshell_fd = fd_fromshell[0];
5226 }
5227
5228 /*
5229 * Write to the child if there are typed characters.
5230 * Read from the child if there are characters available.
5231 * Repeat the reading a few times if more characters are
5232 * available. Need to check for typed keys now and then, but
5233 * not too often (delays when no chars are available).
5234 * This loop is quit if no characters can be read from the pty
5235 * (WaitForChar detected special condition), or there are no
5236 * characters available and the child has exited.
5237 * Only check if the child has exited when there is no more
5238 * output. The child may exit before all the output has
5239 * been printed.
5240 *
5241 * Currently this busy loops!
5242 * This can probably dead-lock when the write blocks!
5243 */
5244 p_more_save = p_more;
5245 p_more = FALSE;
5246 old_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01005247 State = MODE_EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005249 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005250 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005251 // Fork a process that will write the lines to the
5252 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005253 if ((wpid = fork()) == -1)
5254 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005255 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005256 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005257 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00005258 {
5259 linenr_T lnum = curbuf->b_op_start.lnum;
5260 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005261 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005262 size_t l;
5263
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005264 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005265 for (;;)
5266 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005267 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005268 if (l == 0)
5269 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005270 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005271 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00005272 len = write(toshell_fd, "", (size_t)1);
5273 else
5274 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005275 char_u *s = vim_strchr(lp + written, NL);
5276
Bram Moolenaar89d40322006-08-29 15:30:07 +00005277 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00005278 s == NULL ? l
5279 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005280 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00005281 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005282 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005283 // Finished a line, add a NL, unless this line
5284 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005285 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005286 || (!curbuf->b_p_bin
5287 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005288 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaar88456cd2022-11-18 22:14:09 +00005289 && (lnum != curbuf->b_ml.ml_line_count
Bram Moolenaardf177f62005-02-22 08:39:57 +00005290 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005291 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005292 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005293 ++lnum;
5294 if (lnum > curbuf->b_op_end.lnum)
5295 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005296 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005297 close(toshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005298 break;
5299 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005300 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005301 written = 0;
5302 }
5303 else if (len > 0)
5304 written += len;
5305 }
5306 _exit(0);
5307 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005308 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005309 {
5310 close(toshell_fd);
5311 toshell_fd = -1;
5312 }
5313 }
5314
5315 if (options & SHELL_READ)
5316 ga_init2(&ga, 1, BUFLEN);
5317
5318 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005319# ifdef ELAPSED_FUNC
5320 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 for (;;)
5323 {
5324 /*
5325 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005326 * if there are any.
5327 * Don't do this if we are expanding wild cards (would eat
5328 * typeahead).
5329 * Don't do this when filtering and terminal is in cooked
5330 * mode, the shell command will handle the I/O. Avoids
5331 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005332 * Don't get characters when the child has already
5333 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005334 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005335 * while (noread_cnt > 4), avoids that ":r !ls" eats
5336 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 */
5338 len = 0;
5339 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005340 && ((options &
5341 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5342 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005343# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005344 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005345# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005346 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005347 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005348 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005350 if (ta_len == 0)
5351 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005352 // Get extra characters when we don't have any.
5353 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005354 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005355# ifdef ELAPSED_FUNC
5356 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005357# endif
5358 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5359 }
5360 if (ta_len > 0 || len > 0)
5361 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 /*
5363 * For pipes:
5364 * Check for CTRL-C: send interrupt signal to child.
5365 * Check for CTRL-D: EOF, close pipe to child.
5366 */
5367 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5368 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 /*
5370 * Send SIGINT to the child's group or all
5371 * processes in our group.
5372 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005373 may_send_sigint(ta_buf[ta_len], pid, wpid);
5374
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 if (pty_master_fd < 0 && toshell_fd >= 0
5376 && ta_buf[ta_len] == Ctrl_D)
5377 {
5378 close(toshell_fd);
5379 toshell_fd = -1;
5380 }
5381 }
5382
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01005383 // Remove Vim-specific codes from the input.
5384 len = term_replace_keycodes(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385
5386 /*
5387 * For pipes: echo the typed characters.
5388 * For a pty this does not seem to work.
5389 */
5390 if (pty_master_fd < 0)
5391 {
5392 for (i = ta_len; i < ta_len + len; ++i)
5393 {
5394 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5395 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 else if (has_mbyte)
5397 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005398 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399
5400 msg_outtrans_len(ta_buf + i, l);
5401 i += l - 1;
5402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 else
5404 msg_outtrans_len(ta_buf + i, 1);
5405 }
5406 windgoto(msg_row, msg_col);
5407 out_flush();
5408 }
5409
5410 ta_len += len;
5411
5412 /*
5413 * Write the characters to the child, unless EOF has
5414 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005415 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005416 * When writing buffer lines, drop the typed
5417 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005419 if (options & SHELL_WRITE)
5420 ta_len = 0;
5421 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 {
5423 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5424 if (len > 0)
5425 {
5426 ta_len -= len;
5427 mch_memmove(ta_buf, ta_buf + len, ta_len);
5428 }
5429 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
5432
Bram Moolenaardf177f62005-02-22 08:39:57 +00005433 if (got_int)
5434 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005435 // CTRL-C sends a signal to the child, we ignore it
5436 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005437# ifdef HAVE_SETSID
5438 kill(-pid, SIGINT);
5439# else
5440 kill(0, SIGINT);
5441# endif
5442 if (wpid > 0)
5443 kill(wpid, SIGINT);
5444 got_int = FALSE;
5445 }
5446
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 /*
5448 * Check if the child has any characters to be printed.
5449 * Read them and write them to our window. Repeat this as
5450 * long as there is something to do, avoid the 10ms wait
5451 * for mch_inchar(), or sending typeahead characters to
5452 * the external process.
5453 * TODO: This should handle escape sequences, compatible
5454 * to some terminal (vt52?).
5455 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005456 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005457 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005459 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005462 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005464
5465 noread_cnt = 0;
5466 if (options & SHELL_READ)
5467 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005468 // Do NUL -> NL translation, append NL separated
5469 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005470 for (i = 0; i < len; ++i)
5471 {
5472 if (buffer[i] == NL)
5473 append_ga_line(&ga);
5474 else if (buffer[i] == NUL)
5475 ga_append(&ga, NL);
5476 else
5477 ga_append(&ga, buffer[i]);
5478 }
5479 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005480 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
5482 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005483 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
Bram Moolenaardf177f62005-02-22 08:39:57 +00005485 len += buffer_off;
5486 buffer[len] = NUL;
5487
Bram Moolenaar0f873732019-12-05 20:28:46 +01005488 // Check if the last character in buffer[] is
5489 // incomplete, keep these bytes for the next
5490 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 for (p = buffer; p < buffer + len; p += l)
5492 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005493 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005495 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 else if (MB_BYTE2LEN(*p) != l)
5497 break;
5498 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005499 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005501 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 if (len >= 12)
5503 ++p;
5504 else
5505 {
5506 buffer_off = len;
5507 continue;
5508 }
5509 }
5510 c = *p;
5511 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005512 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 if (p < buffer + len)
5514 {
5515 *p = c;
5516 buffer_off = (buffer + len) - p;
5517 mch_memmove(buffer, p, buffer_off);
5518 continue;
5519 }
5520 buffer_off = 0;
5521 }
5522 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 {
5524 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005525 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 }
5527
5528 windgoto(msg_row, msg_col);
5529 cursor_on();
5530 out_flush();
5531 if (got_int)
5532 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005533
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005534# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005535 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005536 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005537 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005538
Bram Moolenaar0f873732019-12-05 20:28:46 +01005539 // Avoid that we keep looping here without
5540 // checking for a CTRL-C for a long time. Don't
5541 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005542 if (msec > 2000)
5543 {
5544 noread_cnt = 5;
5545 break;
5546 }
5547 }
5548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
5550
Bram Moolenaar0f873732019-12-05 20:28:46 +01005551 // If we already detected the child has finished, continue
5552 // reading output for a short while. Some text may be
5553 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005554 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005555 {
5556 if (noread_cnt < 5)
5557 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005558 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005559 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005560
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 /*
5562 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005563 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005565# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005566 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005567# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5571 || (wait_pid == pid && WIFEXITED(status)))
5572 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005573 // Don't break the loop yet, try reading more
5574 // characters from "fromshell_fd" first. When using
5575 // pipes there might still be something to read and
5576 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005579 else
5580 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005581
Bram Moolenaar95a51352013-03-21 22:53:50 +01005582# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005583 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005584 clip_update();
5585# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587finished:
5588 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005589 if (options & SHELL_READ)
5590 {
5591 if (ga.ga_len > 0)
5592 {
5593 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005594 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005595 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005596 }
5597 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005598 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005599 ga_clear(&ga);
5600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 /*
5603 * Give all typeahead that wasn't used back to ui_inchar().
5604 */
5605 if (ta_len)
5606 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 State = old_State;
5608 if (toshell_fd >= 0)
5609 close(toshell_fd);
5610 close(fromshell_fd);
5611 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005612# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005613 else
5614 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005615 long delay_msec = 1;
5616
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005617 if (tmode == TMODE_RAW)
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005618 // Possibly disables modifyOtherKeys, so that the system
5619 // can recognize CTRL-C.
5620 out_str_t_TE();
Bram Moolenaar0981c872020-08-23 14:28:37 +02005621
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005622 /*
5623 * Similar to the loop above, but only handle X events, no
5624 * I/O.
5625 */
5626 for (;;)
5627 {
5628 if (got_int)
5629 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005630 // CTRL-C sends a signal to the child, we ignore it
5631 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005632# ifdef HAVE_SETSID
5633 kill(-pid, SIGINT);
5634# else
5635 kill(0, SIGINT);
5636# endif
5637 got_int = FALSE;
5638 }
5639# ifdef __NeXT__
5640 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5641# else
5642 wait_pid = waitpid(pid, &status, WNOHANG);
5643# endif
5644 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5645 || (wait_pid == pid && WIFEXITED(status)))
5646 {
5647 wait_pid = pid;
5648 break;
5649 }
5650
Bram Moolenaar0f873732019-12-05 20:28:46 +01005651 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005652 clip_update();
5653
Bram Moolenaar0f873732019-12-05 20:28:46 +01005654 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005655 // less time, gradually wait longer.
5656 mch_delay(delay_msec,
5657 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005658 if (++delay_msec > 10)
5659 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005660 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005661
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005662 if (tmode == TMODE_RAW)
5663 // possibly enables modifyOtherKeys again
Bram Moolenaar733a69b2022-12-01 12:03:47 +00005664 out_str_t_TI();
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005665 }
5666# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
5668 /*
5669 * Wait until our child has exited.
5670 * Ignore wait() returning pids of other children and returning
5671 * because of some signal like SIGWINCH.
5672 * Don't wait if wait_pid was already set above, indicating the
5673 * child already exited.
5674 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005675 if (wait_pid != pid)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01005676 (void)wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
Bram Moolenaar624891f2010-10-13 16:22:09 +02005678# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005679 // Close slave side of pty. Only do this after the child has
5680 // exited, otherwise the child may hang when it tries to write on
5681 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005682 if (pty_master_fd >= 0)
5683 close(pty_slave_fd);
5684# endif
5685
Bram Moolenaar0f873732019-12-05 20:28:46 +01005686 // Make sure the child that writes to the external program is
5687 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005688 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005689 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005690 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005691 wait4pid(wpid, NULL);
5692 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005693
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005694# ifdef FEAT_JOB_CHANNEL
5695 --dont_check_job_ended;
5696# endif
5697
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 /*
5699 * Set to raw mode right now, otherwise a CTRL-C after
5700 * catch_signals() will kill Vim.
5701 */
5702 if (tmode == TMODE_RAW)
5703 settmode(TMODE_RAW);
5704 did_settmode = TRUE;
5705 set_signals();
5706
5707 if (WIFEXITED(status))
5708 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005709 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005711 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
5713 if (retval == EXEC_FAILED)
5714 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005715 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 msg_outtrans(p_sh);
5717 msg_putchar('\n');
5718 }
5719 else if (!(options & SHELL_SILENT))
5720 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005721 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 msg_outnum((long)retval);
5723 msg_putchar('\n');
5724 }
5725 }
5726 }
5727 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005728 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 }
5730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
5732error:
5733 if (!did_settmode)
5734 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005735 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005737 vim_free(argv);
5738 vim_free(tofree1);
5739 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
5741 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005742}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005743#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005744
5745 int
5746mch_call_shell(
5747 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005748 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005749{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005750#ifdef FEAT_EVAL
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01005751 ch_log(NULL, "executing shell command: %s", cmd);
5752#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005753#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar524c8532022-09-27 15:48:20 +01005754 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL
5755 && (options & SHELL_SILENT) == 0)
Bram Moolenaar13568252018-03-16 20:46:58 +01005756 return mch_call_shell_terminal(cmd, options);
5757#endif
5758#ifdef USE_SYSTEM
5759 return mch_call_shell_system(cmd, options);
5760#else
5761 return mch_call_shell_fork(cmd, options);
5762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763}
5764
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005765#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005766 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005767mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005768{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005769 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005770 int fd_in[2] = {-1, -1}; // for stdin
5771 int fd_out[2] = {-1, -1}; // for stdout
5772 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005773 int pty_master_fd = -1;
5774 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005775 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005776 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5777 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5778 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005779 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005780 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5781 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005782 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005783 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005784 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005785
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005786 if (use_out_for_err && use_null_for_out)
5787 use_null_for_err = TRUE;
5788
Bram Moolenaar0f873732019-12-05 20:28:46 +01005789 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005790 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005791
Bram Moolenaarb2412082017-08-20 18:09:14 +02005792 if (options->jo_pty
5793 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005794 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005795 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005796 open_pty(&pty_master_fd, &pty_slave_fd,
5797 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005798
Bram Moolenaar0f873732019-12-05 20:28:46 +01005799 // TODO: without the channel feature connect the child to /dev/null?
5800 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005801 if (use_file_for_in)
5802 {
5803 char_u *fname = options->jo_io_name[PART_IN];
5804
5805 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5806 if (fd_in[0] < 0)
5807 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005808 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005809 goto failed;
5810 }
5811 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005812 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005813 // When writing buffer lines to the input don't use the pty, so that
5814 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005815 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5816 && pipe(fd_in) < 0)
5817 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005818
5819 if (use_file_for_out)
5820 {
5821 char_u *fname = options->jo_io_name[PART_OUT];
5822
5823 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5824 if (fd_out[1] < 0)
5825 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005826 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005827 goto failed;
5828 }
5829 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005830 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005831 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005832
5833 if (use_file_for_err)
5834 {
5835 char_u *fname = options->jo_io_name[PART_ERR];
5836
5837 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5838 if (fd_err[1] < 0)
5839 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005840 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005841 goto failed;
5842 }
5843 }
Christian Brabandtb50bc9a2024-09-30 21:29:43 +02005844 // only create a pipe for the error fd, when either a callback has been setup
5845 // or pty is not used (e.g. terminal uses pty by default)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005846 else if (!use_out_for_err && !use_null_for_err
Christian Brabandtb50bc9a2024-09-30 21:29:43 +02005847 && (pty_master_fd < 0 || (options->jo_set & JO_ERR_CALLBACK))
5848 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005849 goto failed;
5850
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005851 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5852 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005853 if (options->jo_set & JO_CHANNEL)
5854 {
5855 channel = options->jo_channel;
5856 if (channel != NULL)
5857 ++channel->ch_refcount;
5858 }
5859 else
5860 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005861 if (channel == NULL)
5862 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005863 if (job->jv_tty_out != NULL)
5864 ch_log(channel, "using pty %s on fd %d",
5865 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005866 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005867
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005868 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005869 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005870 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005871 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005872 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005873 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005874 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005875 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005876 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005877 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005878 int null_fd = -1;
5879 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005880
Bram Moolenaar0f873732019-12-05 20:28:46 +01005881 // child
5882 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005883 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005884
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005885# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01005886 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005887 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005888 ch_logfile((char_u *)"", (char_u *)"");
5889# endif
5890
Bram Moolenaar835dc632016-02-07 14:27:38 +01005891# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005892 // Create our own process group, so that the child and all its
5893 // children can be kill()ed. Don't do this when using pipes,
5894 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005895 (void)setsid();
5896# endif
5897
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005898# ifdef FEAT_TERMINAL
5899 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005900 {
5901 char *term = (char *)T_NAME;
5902
5903#ifdef FEAT_GUI
5904 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005905 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005906 term = getenv("TERM");
5907#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005908 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005909 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005910 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005911 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005912 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005913 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005914 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005915 else if (t_colors > 16)
5916 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005917 else
5918 term = "xterm";
5919 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005920 set_child_environment(
5921 (long)options->jo_term_rows,
5922 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005923 term,
5924 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005925 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005926 else
5927# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005928 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005929
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005930 if (options->jo_env != NULL)
5931 {
5932 dict_T *dict = options->jo_env;
5933 hashitem_T *hi;
5934 int todo = (int)dict->dv_hashtab.ht_used;
5935
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005936 FOR_ALL_HASHTAB_ITEMS(&dict->dv_hashtab, hi, todo)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005937 if (!HASHITEM_EMPTY(hi))
5938 {
5939 typval_T *item = &dict_lookup(hi)->di_tv;
5940
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005941 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005942 --todo;
5943 }
5944 }
5945
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005946 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005947 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005948 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005949 if (null_fd < 0)
5950 {
5951 perror("opening /dev/null failed");
5952 _exit(OPEN_NULL_FAILED);
5953 }
5954 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005955
Bram Moolenaar223896d2017-08-02 22:33:28 +02005956 if (pty_slave_fd >= 0)
5957 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005958 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005959 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005960# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005961 // Try to become controlling tty (probably doesn't work,
5962 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005963 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5964# endif
5965 }
5966
Bram Moolenaar0f873732019-12-05 20:28:46 +01005967 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005968 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005969 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005970 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005971 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005972 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005973 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005974 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005975
Bram Moolenaar0f873732019-12-05 20:28:46 +01005976 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005977 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005978 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005979 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005980 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005981 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005982 }
5983 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005984 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005985 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005986 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005987 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005988 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005989
Bram Moolenaar0f873732019-12-05 20:28:46 +01005990 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005991 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005992 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005993 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005994 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005995 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005996 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005997 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005998
5999 if (fd_in[0] >= 0)
6000 close(fd_in[0]);
6001 if (fd_in[1] >= 0)
6002 close(fd_in[1]);
6003 if (fd_out[0] >= 0)
6004 close(fd_out[0]);
6005 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01006006 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02006007 if (fd_err[0] >= 0)
6008 close(fd_err[0]);
6009 if (fd_err[1] >= 0)
6010 close(fd_err[1]);
6011 if (pty_master_fd >= 0)
6012 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006013 close(pty_master_fd); // not used in the child
6014 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01006015 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02006016
Bram Moolenaarf65333c2016-03-08 18:27:21 +01006017 if (null_fd >= 0)
6018 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006019
Bram Moolenaar05aafed2017-08-11 19:12:11 +02006020 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
6021 _exit(EXEC_FAILED);
6022
Bram Moolenaar0f873732019-12-05 20:28:46 +01006023 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01006024 execvp(argv[0], argv);
6025
Bram Moolenaar4694a172016-04-21 14:05:23 +02006026 if (stderr_works)
6027 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02006028# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01006029 // calling free_all_mem() here causes problems. Ignore valgrind
6030 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02006031# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01006032 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01006033 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006034
Bram Moolenaar0f873732019-12-05 20:28:46 +01006035 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02006036 UNBLOCK_SIGNALS(&curset);
6037
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006038 job->jv_pid = pid;
6039 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006040 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006041
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02006042 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006043 close(pty_slave_fd); // not used in the parent
6044 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01006045 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01006046 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01006047 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01006048 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01006049 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01006050 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01006051 if (channel != NULL)
6052 {
Bram Moolenaar652de232019-04-04 20:13:09 +02006053 int in_fd = INVALID_FD;
6054 int out_fd = INVALID_FD;
6055 int err_fd = INVALID_FD;
6056
6057 if (!(use_file_for_in || use_null_for_in))
6058 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
6059
6060 if (!(use_file_for_out || use_null_for_out))
6061 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
6062
6063 // When using pty_master_fd only set it for stdout, do not duplicate
6064 // it for stderr, it only needs to be read once.
6065 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
6066 {
6067 if (fd_err[0] >= 0)
6068 err_fd = fd_err[0];
6069 else if (out_fd != pty_master_fd)
6070 err_fd = pty_master_fd;
6071 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02006072
6073 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01006074 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01006075 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02006076 else
6077 {
6078 if (fd_in[1] >= 0)
6079 close(fd_in[1]);
6080 if (fd_out[0] >= 0)
6081 close(fd_out[0]);
6082 if (fd_err[0] >= 0)
6083 close(fd_err[0]);
6084 if (pty_master_fd >= 0)
6085 close(pty_master_fd);
6086 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006087
Bram Moolenaar0f873732019-12-05 20:28:46 +01006088 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006089 return;
6090
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006091failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01006092 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006093 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006094 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01006095 if (fd_in[1] >= 0)
6096 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006097 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006098 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01006099 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006100 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006101 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006102 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01006103 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006104 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02006105 if (pty_master_fd >= 0)
6106 close(pty_master_fd);
6107 if (pty_slave_fd >= 0)
6108 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01006109}
6110
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006111 static char_u *
6112get_signal_name(int sig)
6113{
6114 int i;
6115 char_u numbuf[NUMBUFLEN];
6116
6117 if (sig == SIGKILL)
6118 return vim_strsave((char_u *)"kill");
6119
6120 for (i = 0; signal_info[i].sig != -1; i++)
6121 if (sig == signal_info[i].sig)
6122 return strlow_save((char_u *)signal_info[i].name);
6123
6124 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
6125 return vim_strsave(numbuf);
6126}
6127
Bram Moolenaar835dc632016-02-07 14:27:38 +01006128 char *
6129mch_job_status(job_T *job)
6130{
6131# ifdef HAVE_UNION_WAIT
6132 union wait status;
6133# else
6134 int status = -1;
6135# endif
6136 pid_t wait_pid = 0;
6137
6138# ifdef __NeXT__
6139 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
6140# else
6141 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
6142# endif
6143 if (wait_pid == -1)
6144 {
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00006145 int waitpid_errno = errno;
6146 if (waitpid_errno == ECHILD && mch_process_running(job->jv_pid))
6147 // The process is alive, but it was probably reparented (for
6148 // example by ptrace called by a debugger like lldb or gdb).
6149 // Note: This assumes that process IDs are not reused.
6150 return "run";
6151
Bram Moolenaar0f873732019-12-05 20:28:46 +01006152 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006153 if (job->jv_status < JOB_ENDED)
6154 ch_log(job->jv_channel, "Job no longer exists: %s",
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00006155 strerror(waitpid_errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02006156 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006157 }
6158 if (wait_pid == 0)
6159 return "run";
6160 if (WIFEXITED(status))
6161 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006162 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01006163 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006164 if (job->jv_status < JOB_ENDED)
6165 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02006166 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006167 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01006168 if (WIFSIGNALED(status))
6169 {
6170 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006171 job->jv_termsig = get_signal_name(WTERMSIG(status));
6172 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
6173 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
6174 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02006175 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01006176 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01006177 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02006178
6179return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01006180 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02006181 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02006182 return "dead";
6183}
6184
6185 job_T *
6186mch_detect_ended_job(job_T *job_list)
6187{
6188# ifdef HAVE_UNION_WAIT
6189 union wait status;
6190# else
6191 int status = -1;
6192# endif
6193 pid_t wait_pid = 0;
6194 job_T *job;
6195
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01006196# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006197 // Do not do this when waiting for a shell command to finish, we would get
6198 // the exit value here (and discard it), the exit value obtained there
6199 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01006200 if (dont_check_job_ended > 0)
6201 return NULL;
6202# endif
6203
Bram Moolenaar97792de2016-10-15 18:36:49 +02006204# ifdef __NeXT__
6205 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
6206# else
6207 wait_pid = waitpid(-1, &status, WNOHANG);
6208# endif
6209 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006210 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02006211 return NULL;
6212 for (job = job_list; job != NULL; job = job->jv_next)
6213 {
6214 if (job->jv_pid == wait_pid)
6215 {
6216 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006217 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02006218 job->jv_exitval = WEXITSTATUS(status);
6219 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006220 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02006221 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01006222 job->jv_termsig = get_signal_name(WTERMSIG(status));
6223 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01006224 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02006225 {
6226 ch_log(job->jv_channel, "Job ended");
6227 job->jv_status = JOB_ENDED;
6228 }
6229 return job;
6230 }
6231 }
6232 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006233}
6234
Bram Moolenaar3a117e12016-10-30 21:57:52 +01006235/*
6236 * Send a (deadly) signal to "job".
6237 * Return FAIL if "how" is not a valid name.
6238 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01006239 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006240mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01006241{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01006242 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006243
Bram Moolenaar923d9262016-02-25 20:56:01 +01006244 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01006245 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01006246 else if (STRCMP(how, "hup") == 0)
6247 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006248 else if (STRCMP(how, "quit") == 0)
6249 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01006250 else if (STRCMP(how, "int") == 0)
6251 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006252 else if (STRCMP(how, "kill") == 0)
6253 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02006254#ifdef SIGWINCH
6255 else if (STRCMP(how, "winch") == 0)
6256 sig = SIGWINCH;
6257#endif
Keith Thompson184f71c2024-01-04 21:19:04 +01006258 else if (SAFE_isdigit(*how))
Bram Moolenaar835dc632016-02-07 14:27:38 +01006259 sig = atoi((char *)how);
6260 else
6261 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01006262
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01006263 // Never kill ourselves!
6264 if (job->jv_pid != 0)
6265 {
6266 // TODO: have an option to only kill the process, not the group?
6267 kill(-job->jv_pid, sig);
6268 kill(job->jv_pid, sig);
6269 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01006270
Bram Moolenaar835dc632016-02-07 14:27:38 +01006271 return OK;
6272}
Bram Moolenaar76467df2016-02-12 19:30:26 +01006273
6274/*
6275 * Clear the data related to "job".
6276 */
6277 void
6278mch_clear_job(job_T *job)
6279{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006280 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01006281# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006282 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006283# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006284 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006285# endif
6286}
Bram Moolenaar835dc632016-02-07 14:27:38 +01006287#endif
6288
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006289#if defined(FEAT_TERMINAL) || defined(PROTO)
6290 int
6291mch_create_pty_channel(job_T *job, jobopt_T *options)
6292{
6293 int pty_master_fd = -1;
6294 int pty_slave_fd = -1;
6295 channel_T *channel;
6296
Bram Moolenaar59386482019-02-10 22:43:46 +01006297 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02006298 if (pty_master_fd < 0 || pty_slave_fd < 0)
6299 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006300 close(pty_slave_fd);
6301
6302 channel = add_channel();
6303 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006304 {
6305 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006306 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006307 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006308 if (job->jv_tty_out != NULL)
6309 ch_log(channel, "using pty %s on fd %d",
6310 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006311 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006312 channel->ch_keep_open = TRUE;
6313
Bram Moolenaar0f873732019-12-05 20:28:46 +01006314 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6315 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006316 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006317 channel_set_job(channel, job, options);
6318 return OK;
6319}
6320#endif
6321
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322/*
6323 * Check for CTRL-C typed by reading all available characters.
6324 * In cooked mode we should get SIGINT, no need to check.
6325 */
6326 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006327mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006329 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006330 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 fill_input_buf(FALSE);
6332}
6333
6334/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006335 * Wait "msec" msec until a character is available from the mouse, keyboard,
6336 * from inbuf[].
6337 * "msec" == -1 will block forever.
6338 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006339 * When "ignore_input" is TRUE even check for pending input when input is
6340 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006341 * "interrupted" (if not NULL) is set to TRUE when no character is available
6342 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006343 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006344 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 */
6346 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006347WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006349#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006350 return ui_wait_for_chars_or_timer(
6351 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006352#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006353 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006354#endif
6355}
6356
6357/*
6358 * Wait "msec" msec until a character is available from the mouse or keyboard
6359 * or from inbuf[].
6360 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006361 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006362 * "interrupted" (if not NULL) is set to TRUE when no character is available
6363 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006364 * When a GUI is being used, this will never get called -- webb
6365 */
6366 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006367WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006368{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369#ifdef FEAT_MOUSE_GPM
6370 int gpm_process_wanted;
6371#endif
6372#ifdef FEAT_XCLIPBOARD
6373 int rest;
6374#endif
6375 int avail;
6376
Bram Moolenaar0f873732019-12-05 20:28:46 +01006377 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 return 1;
6379
6380#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006381 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 if (WantQueryMouse)
6383 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006384 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006385 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006386 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 }
6388#endif
6389
6390 /*
6391 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6392 * events. This is a bit complicated, because they might both be defined.
6393 */
6394#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6395# ifdef FEAT_XCLIPBOARD
6396 rest = 0;
6397 if (do_xterm_trace())
6398 rest = msec;
6399# endif
6400 do
6401 {
6402# ifdef FEAT_XCLIPBOARD
6403 if (rest != 0)
6404 {
6405 msec = XT_TRACE_DELAY;
6406 if (rest >= 0 && rest < XT_TRACE_DELAY)
6407 msec = rest;
6408 if (rest >= 0)
6409 rest -= msec;
6410 }
6411# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01006412# ifdef FEAT_SOUND_MACOSX
6413 // Invoke any pending sound callbacks.
6414 process_cfrunloop();
6415# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006416# ifdef FEAT_SOUND_CANBERRA
6417 // Invoke any pending sound callbacks.
6418 if (has_sound_callback_in_queue())
6419 invoke_sound_callback();
6420# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421# ifdef FEAT_MOUSE_GPM
6422 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006423 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006424 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006425 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006427 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006429# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006431 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 return 1;
6433# ifdef FEAT_XCLIPBOARD
6434 if (rest == 0 || !do_xterm_trace())
6435# endif
6436 break;
6437 }
6438 }
6439 while (FALSE
6440# ifdef FEAT_MOUSE_GPM
6441 || (gpm_process_wanted && mch_gpm_process() == 0)
6442# endif
6443# ifdef FEAT_XCLIPBOARD
6444 || (!avail && rest != 0)
6445# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006446 )
6447 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448
6449#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006450 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451#endif
6452 return avail;
6453}
6454
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006455#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456/*
6457 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006458 * "msec" == 0 will check for characters once.
6459 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006462 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006463 * "interrupted" (if not NULL) is set to TRUE when no character is available
6464 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006466 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006467RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
6469 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006470 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006471#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 static int busy = FALSE;
6473
Bram Moolenaar0f873732019-12-05 20:28:46 +01006474 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475# define MAY_LOOP
6476
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006477# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006478 // Remember at what time we started, so that we know how much longer we
6479 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006480 long start_msec = msec;
6481 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006483 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006484 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485# endif
6486
Bram Moolenaar0f873732019-12-05 20:28:46 +01006487 // Handle being called recursively. This may happen for the session
6488 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 if (busy)
6490 return 0;
6491#endif
6492
6493#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006494 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495#endif
6496 {
6497#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006498 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006499# ifdef FEAT_MZSCHEME
6500 int mzquantum_used = FALSE;
6501# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502#endif
6503#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006504 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006505 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 int nfd;
6507# ifdef FEAT_XCLIPBOARD
6508 int xterm_idx = -1;
6509# endif
6510# ifdef FEAT_MOUSE_GPM
6511 int gpm_idx = -1;
6512# endif
6513# ifdef USE_XSMP
6514 int xsmp_idx = -1;
6515# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006516 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006518# ifdef FEAT_MZSCHEME
6519 mzvim_check_threads();
6520 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6521 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006522 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006523 mzquantum_used = TRUE;
6524 }
6525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 fds[0].fd = fd;
6527 fds[0].events = POLLIN;
6528 nfd = 1;
6529
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006531 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 if (xterm_Shell != (Widget)0)
6533 {
6534 xterm_idx = nfd;
6535 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6536 fds[nfd].events = POLLIN;
6537 nfd++;
6538 }
6539# endif
6540# ifdef FEAT_MOUSE_GPM
6541 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6542 {
6543 gpm_idx = nfd;
6544 fds[nfd].fd = gpm_fd;
6545 fds[nfd].events = POLLIN;
6546 nfd++;
6547 }
6548# endif
6549# ifdef USE_XSMP
6550 if (xsmp_icefd != -1)
6551 {
6552 xsmp_idx = nfd;
6553 fds[nfd].fd = xsmp_icefd;
6554 fds[nfd].events = POLLIN;
6555 nfd++;
6556 }
6557# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006558#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006559 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006560#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006561 if (interrupted != NULL)
6562 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006564 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006565
6566 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006567 if (result == 0 && interrupted != NULL && ret > 0)
6568 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006569
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006570# ifdef FEAT_MZSCHEME
6571 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006572 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006573 finished = FALSE;
6574# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576# ifdef FEAT_XCLIPBOARD
6577 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6578 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006579 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006581 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 finished = FALSE;
6583 }
6584# endif
6585# ifdef FEAT_MOUSE_GPM
6586 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588# endif
6589# ifdef USE_XSMP
6590 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6591 {
6592 if (fds[xsmp_idx].revents & POLLIN)
6593 {
6594 busy = TRUE;
6595 xsmp_handle_requests();
6596 busy = FALSE;
6597 }
6598 else if (fds[xsmp_idx].revents & POLLHUP)
6599 {
6600 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006601 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 xsmp_close();
6603 }
6604 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006605 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 }
6607# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006608#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006609 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006610 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006611 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613
Bram Moolenaar0f873732019-12-05 20:28:46 +01006614#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615
6616 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006617 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006618 // These are static because they can take 8 Kbyte each and cause the
6619 // signal stack to run out with -O3.
6620 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006622 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006624# ifdef FEAT_MZSCHEME
6625 mzvim_check_threads();
6626 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6627 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006628 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006629 mzquantum_used = TRUE;
6630 }
6631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006633 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006635 tv.tv_sec = towait / 1000;
6636 tv.tv_usec = (towait % 1000) * (1000000/1000);
6637 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006639 else
6640 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641
6642 /*
6643 * Select on ready for reading and exceptional condition (end of file).
6644 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006645select_eintr:
6646 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006647 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 FD_ZERO(&efds);
6649 FD_SET(fd, &rfds);
K.Takatab247e062022-02-07 10:45:23 +00006650# ifndef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006651 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 FD_SET(fd, &efds);
6653# endif
6654 maxfd = fd;
6655
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006657 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 if (xterm_Shell != (Widget)0)
6659 {
6660 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6661 if (maxfd < ConnectionNumber(xterm_dpy))
6662 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006663
Bram Moolenaar0f873732019-12-05 20:28:46 +01006664 // An event may have already been read but not handled. In
6665 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006666 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 }
6668# endif
6669# ifdef FEAT_MOUSE_GPM
6670 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6671 {
6672 FD_SET(gpm_fd, &rfds);
6673 FD_SET(gpm_fd, &efds);
6674 if (maxfd < gpm_fd)
6675 maxfd = gpm_fd;
6676 }
6677# endif
6678# ifdef USE_XSMP
6679 if (xsmp_icefd != -1)
6680 {
6681 FD_SET(xsmp_icefd, &rfds);
6682 FD_SET(xsmp_icefd, &efds);
6683 if (maxfd < xsmp_icefd)
6684 maxfd = xsmp_icefd;
6685 }
6686# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006687# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006688 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006689# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006690 if (interrupted != NULL)
6691 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
Bram Moolenaar643b6142018-09-12 20:29:09 +02006693 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6694 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006695 result = ret > 0 && FD_ISSET(fd, &rfds);
6696 if (result)
6697 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006698 else if (interrupted != NULL && ret > 0)
6699 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006700
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006701# ifdef EINTR
6702 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006703 {
dbivolaruab16ad32021-12-29 19:41:47 +00006704 // Check whether the EINTR is caused by SIGTSTP
6705 if (got_tstp && !in_mch_suspend)
6706 {
6707 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006708
dbivolaruab16ad32021-12-29 19:41:47 +00006709 ea.forceit = TRUE;
6710 ex_stop(&ea);
6711 got_tstp = FALSE;
6712 }
6713
Bram Moolenaar0f873732019-12-05 20:28:46 +01006714 // Check whether window has been resized, EINTR may be caused by
6715 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006716 if (do_resize)
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006717 {
Bram Moolenaar545c8a52023-06-21 15:51:47 +01006718# ifdef FEAT_EVAL
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006719 ch_log(NULL, "calling handle_resize() in RealWaitForChar()");
Bram Moolenaar545c8a52023-06-21 15:51:47 +01006720# endif
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006721 handle_resize();
Bram Moolenaar55f1b822023-06-21 13:42:48 +01006722 }
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006723
Bram Moolenaar0f873732019-12-05 20:28:46 +01006724 // Interrupted by a signal, need to try again. We ignore msec
6725 // here, because we do want to check even after a timeout if
6726 // characters are available. Needed for reading output of an
6727 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006728 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006729 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006730# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006731# ifdef __TANDEM
6732 if (ret == -1 && errno == ENOTSUP)
6733 {
6734 FD_ZERO(&rfds);
6735 FD_ZERO(&efds);
6736 ret = 0;
6737 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006738# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006739# ifdef FEAT_MZSCHEME
6740 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006741 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006742 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743# endif
6744
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745# ifdef FEAT_XCLIPBOARD
6746 if (ret > 0 && xterm_Shell != (Widget)0
6747 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6748 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006749 xterm_update(); // Maybe we should hand out clipboard
6750 // continue looping when we only got the X event and the input
6751 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 if (--ret == 0 && !input_available())
6753 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006754 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 finished = FALSE;
6756 }
6757 }
6758# endif
6759# ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00006760 if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 {
6762 if (FD_ISSET(gpm_fd, &efds))
6763 gpm_close();
6764 else if (FD_ISSET(gpm_fd, &rfds))
6765 *check_for_gpm = 1;
6766 }
6767# endif
6768# ifdef USE_XSMP
6769 if (ret > 0 && xsmp_icefd != -1)
6770 {
6771 if (FD_ISSET(xsmp_icefd, &efds))
6772 {
6773 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006774 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 xsmp_close();
6776 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006777 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 }
6779 else if (FD_ISSET(xsmp_icefd, &rfds))
6780 {
6781 busy = TRUE;
6782 xsmp_handle_requests();
6783 busy = FALSE;
6784 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006785 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 }
6787 }
6788# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006789#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006790 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006791 if (ret >= 0)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01006792 (void)channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794
Bram Moolenaar0f873732019-12-05 20:28:46 +01006795#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796
6797#ifdef MAY_LOOP
6798 if (finished || msec == 0)
6799 break;
6800
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006801# ifdef FEAT_CLIENTSERVER
6802 if (server_waiting())
6803 break;
6804# endif
6805
Bram Moolenaar0f873732019-12-05 20:28:46 +01006806 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 if (msec > 0)
6808 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006809# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006810 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006811 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006813 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 msec = msec / 2;
6815# endif
6816 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006817 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 }
6819#endif
6820 }
6821
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006822 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823}
6824
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006826 * Expand a path into all matching files and/or directories. Handles "*",
6827 * "?", "[a-z]", "**", etc.
6828 * "path" has backslashes before chars that are not to be expanded.
6829 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 */
6831 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006832mch_expandpath(
6833 garray_T *gap,
6834 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006835 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836{
Bram Moolenaar02743632005-07-25 20:42:36 +00006837 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839
6840/*
6841 * mch_expand_wildcards() - this code does wild-card pattern matching using
6842 * the shell
6843 *
6844 * return OK for success, FAIL for error (you may lose some memory) and put
6845 * an error message in *file.
6846 *
6847 * num_pat is number of input patterns
6848 * pat is array of pointers to input patterns
6849 * num_file is pointer to number of matched file names
6850 * file is pointer to array of pointers to matched file names
6851 */
6852
6853#ifndef SEEK_SET
6854# define SEEK_SET 0
6855#endif
6856#ifndef SEEK_END
6857# define SEEK_END 2
6858#endif
6859
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006860#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006861
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006863mch_expand_wildcards(
6864 int num_pat,
6865 char_u **pat,
6866 int *num_file,
6867 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006868 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
6870 int i;
6871 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006872 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 char_u *p;
6874 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875
Bram Moolenaarc7247912008-01-13 12:54:11 +00006876 /*
6877 * This is the non-OS/2 implementation (really Unix).
6878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 int j;
6880 char_u *tempname;
6881 char_u *command;
6882 FILE *fd;
6883 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006884#define STYLE_ECHO 0 // use "echo", the default
6885#define STYLE_GLOB 1 // use "glob", for csh
6886#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6887#define STYLE_PRINT 3 // use "print -N", for zsh
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006888#define STYLE_BT 4 // `cmd` expansion, execute the pattern directly
6889#define STYLE_GLOBSTAR 5 // use extended shell glob for bash (this uses extended
6890 // globbing functionality using globstar, needs bash > 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 int shell_style = STYLE_ECHO;
6892 int check_spaces;
6893 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006894 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006895 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006896 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006897 // vimglob() function with globstar setting enabled, only for bash >= 4.X
6898 static char *sh_globstar_opt = "[[ ${BASH_VERSINFO[0]} -ge 4 ]] && shopt -s globstar; ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899
Bram Moolenaar0f873732019-12-05 20:28:46 +01006900 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 *file = NULL;
6902
6903 /*
6904 * If there are no wildcards, just copy the names to allocated memory.
6905 * Saves a lot of time, because we don't have to start a new shell.
6906 */
6907 if (!have_wildcard(num_pat, pat))
6908 return save_patterns(num_pat, pat, num_file, file);
6909
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006910# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006911 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006912 if (sandbox != 0 && check_secure())
6913 return FAIL;
6914# endif
6915
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 /*
6917 * Don't allow the use of backticks in secure and restricted mode.
6918 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006919 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 for (i = 0; i < num_pat; ++i)
6921 if (vim_strchr(pat[i], '`') != NULL
6922 && (check_restricted() || check_secure()))
6923 return FAIL;
6924
6925 /*
6926 * get a name for the temp file
6927 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006928 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006930 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 return FAIL;
6932 }
6933
6934 /*
6935 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006936 * file.
6937 * STYLE_BT: NL separated
6938 * If expanding `cmd` execute it directly.
6939 * STYLE_GLOB: NUL separated
6940 * If we use *csh, "glob" will work better than "echo".
6941 * STYLE_PRINT: NL or NUL separated
6942 * If we use *zsh, "print -N" will work better than "glob".
6943 * STYLE_VIMGLOB: NL separated
6944 * If we use *sh*, we define "vimglob()".
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006945 * STYLE_GLOBSTAR: NL separated
6946 * If we use *bash*, we define "vimglob() and enable globstar option".
Bram Moolenaarc7247912008-01-13 12:54:11 +00006947 * STYLE_ECHO: space separated.
6948 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 */
6950 if (num_pat == 1 && *pat[0] == '`'
6951 && (len = STRLEN(pat[0])) > 2
6952 && *(pat[0] + len - 1) == '`')
6953 shell_style = STYLE_BT;
6954 else if ((len = STRLEN(p_sh)) >= 3)
6955 {
6956 if (STRCMP(p_sh + len - 3, "csh") == 0)
6957 shell_style = STYLE_GLOB;
6958 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6959 shell_style = STYLE_PRINT;
6960 }
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006961 if (shell_style == STYLE_ECHO)
6962 {
6963 if (strstr((char *)gettail(p_sh), "bash") != NULL)
6964 shell_style = STYLE_GLOBSTAR;
6965 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
6966 shell_style = STYLE_VIMGLOB;
6967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
Bram Moolenaar0f873732019-12-05 20:28:46 +01006969 // Compute the length of the command. We need 2 extra bytes: for the
6970 // optional '&' and for the NUL.
6971 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006973 if (shell_style == STYLE_VIMGLOB)
6974 len += STRLEN(sh_vimglob_func);
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02006975 else if (shell_style == STYLE_GLOBSTAR)
6976 len += STRLEN(sh_vimglob_func)
6977 + STRLEN(sh_globstar_opt);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006978
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006979 for (i = 0; i < num_pat; ++i)
6980 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006981 // Count the length of the patterns in the same way as they are put in
6982 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006983#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006984 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006985#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006986 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006987 for (j = 0; pat[i][j] != NUL; ++j)
6988 {
6989 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006990 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006991 ++len;
6992 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006993#endif
6994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 command = alloc(len);
6996 if (command == NULL)
6997 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006998 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 vim_free(tempname);
7000 return FAIL;
7001 }
7002
7003 /*
7004 * Build the shell command:
7005 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
7006 * recognizes this).
7007 * - Add the shell command to print the expanded names.
7008 * - Add the temp file name.
7009 * - Add the file name patterns.
7010 */
7011 if (shell_style == STYLE_BT)
7012 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007013 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00007014 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01007015 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007017 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01007018 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007020 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01007022 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 *p = ' ';
7024 }
7025 STRCAT(command, ">");
7026 }
7027 else
7028 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00007029 STRCPY(command, "");
7030 if (shell_style == STYLE_GLOB)
7031 {
7032 // Assume the nonomatch option is valid only for csh like shells,
7033 // otherwise, this may set the positional parameters for the shell,
7034 // e.g. "$*".
7035 if (flags & EW_NOTFOUND)
7036 STRCAT(command, "set nonomatch; ");
7037 else
7038 STRCAT(command, "unset nonomatch; ");
7039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 if (shell_style == STYLE_GLOB)
7041 STRCAT(command, "glob >");
7042 else if (shell_style == STYLE_PRINT)
7043 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00007044 else if (shell_style == STYLE_VIMGLOB)
7045 STRCAT(command, sh_vimglob_func);
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02007046 else if (shell_style == STYLE_GLOBSTAR)
7047 {
7048 STRCAT(command, sh_globstar_opt);
7049 STRCAT(command, sh_vimglob_func);
7050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 else
7052 STRCAT(command, "echo >");
7053 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00007054
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00007056
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 if (shell_style != STYLE_BT)
7058 for (i = 0; i < num_pat; ++i)
7059 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007060 // When using system() always add extra quotes, because the shell
7061 // is started twice. Otherwise put a backslash before special
7062 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063#ifdef USE_SYSTEM
7064 STRCAT(command, " \"");
7065 STRCAT(command, pat[i]);
7066 STRCAT(command, "\"");
7067#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00007068 int intick = FALSE;
7069
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 p = command + STRLEN(command);
7071 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00007072 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00007073 {
7074 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00007075 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00007076 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
7077 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007078 // Remove a backslash, take char literally. But keep
7079 // backslash inside backticks, before a special character
7080 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007081 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00007082 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
7083 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007084 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00007085 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00007086 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02007087 else if (!intick
7088 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
7089 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007090 // Put a backslash before a special character, but not
7091 // when inside ``. And not for $var when EW_KEEPDOLLAR is
7092 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00007093 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00007094
Bram Moolenaar0f873732019-12-05 20:28:46 +01007095 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00007096 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00007097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 *p = NUL;
7099#endif
7100 }
7101 if (flags & EW_SILENT)
7102 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01007103 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007104 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
7106 /*
7107 * Using zsh -G: If a pattern has no matches, it is just deleted from
7108 * the argument list, otherwise zsh gives an error message and doesn't
7109 * expand any other pattern.
7110 */
7111 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007112 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113
7114 /*
7115 * If we use -f then shell variables set in .cshrc won't get expanded.
7116 * vi can do it, so we will too, but it is only necessary if there is a "$"
7117 * in one of the patterns, otherwise we can still use the fast option.
7118 */
7119 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007120 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121
7122 /*
7123 * execute the shell command
7124 */
7125 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
7126
Bram Moolenaar0f873732019-12-05 20:28:46 +01007127 // When running in the background, give it some time to create the temp
7128 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01007129 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02007130 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131
Bram Moolenaar0f873732019-12-05 20:28:46 +01007132 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 show_shell_mess = TRUE;
7134 vim_free(command);
7135
Bram Moolenaar0f873732019-12-05 20:28:46 +01007136 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 {
7138 mch_remove(tempname);
7139 vim_free(tempname);
7140 /*
7141 * With interactive completion, the error message is not printed.
7142 * However with USE_SYSTEM, I don't know how to turn off error messages
7143 * from the shell, so screen may still get messed up -- webb.
7144 */
7145#ifndef USE_SYSTEM
7146 if (!(flags & EW_SILENT))
7147#endif
7148 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007149 redraw_later_clear(); // probably messed up screen
7150 msg_putchar('\n'); // clear bottom line quickly
7151 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152#ifdef USE_SYSTEM
7153 if (!(flags & EW_SILENT))
7154#endif
7155 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00007156 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01007157 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 }
7159 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007160 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
7161 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 if (shell_style == STYLE_BT)
7163 return FAIL;
7164 goto notfound;
7165 }
7166
7167 /*
7168 * read the names from the file into memory
7169 */
7170 fd = fopen((char *)tempname, READBIN);
7171 if (fd == NULL)
7172 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007173 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 if (!(flags & EW_SILENT))
7175 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00007176 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01007177 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 }
7179 vim_free(tempname);
7180 goto notfound;
7181 }
7182 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007183 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02007185 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007186 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02007187 buffer = NULL;
7188 else
7189 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 if (buffer == NULL)
7191 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007192 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 mch_remove(tempname);
7194 vim_free(tempname);
7195 fclose(fd);
7196 return FAIL;
7197 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02007198 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 i = fread((char *)buffer, 1, len, fd);
7200 fclose(fd);
7201 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00007202 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007204 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007205 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 vim_free(tempname);
7207 vim_free(buffer);
7208 return FAIL;
7209 }
7210 vim_free(tempname);
7211
Bram Moolenaar1eed5322019-02-26 17:03:54 +01007212# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01007213 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02007215 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
7217 *p++ = buffer[i];
7218 len = p - buffer;
7219# endif
7220
7221
Bram Moolenaar0f873732019-12-05 20:28:46 +01007222 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 if (shell_style == STYLE_ECHO)
7224 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007225 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007227 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 {
7229 while (*p != ' ' && *p != '\n')
7230 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007231 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 }
7233 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007234 // file names are separated with NL
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02007235 else if (shell_style == STYLE_BT ||
7236 shell_style == STYLE_VIMGLOB ||
7237 shell_style == STYLE_GLOBSTAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007239 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007241 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 {
7243 while (*p != '\n' && *p != NUL)
7244 ++p;
7245 if (*p != NUL)
7246 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007247 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 }
7249 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007250 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 else
7252 {
7253 /*
7254 * Some versions of zsh use spaces instead of NULs to separate
7255 * results. Only do this when there is no NUL before the end of the
7256 * buffer, otherwise we would never be able to use file names with
7257 * embedded spaces when zsh does use NULs.
7258 * When we found a NUL once, we know zsh is OK, set did_find_nul and
7259 * don't check for spaces again.
7260 */
7261 check_spaces = FALSE;
7262 if (shell_style == STYLE_PRINT && !did_find_nul)
7263 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007264 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007265 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01007266 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 did_find_nul = TRUE;
7268 else
7269 check_spaces = TRUE;
7270 }
7271
7272 /*
7273 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
7274 * already is one, for STYLE_GLOB it needs to be added.
7275 */
7276 if (len && buffer[len - 1] == NUL)
7277 --len;
7278 else
7279 buffer[len] = NUL;
7280 i = 0;
7281 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007282 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 {
7284 ++i;
7285 *p = NUL;
7286 }
7287 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007288 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 }
7290 if (i == 0)
7291 {
7292 /*
7293 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
7294 * /bin/sh will happily expand it to nothing rather than returning an
7295 * error; and hey, it's good to check anyway -- webb.
7296 */
7297 vim_free(buffer);
7298 goto notfound;
7299 }
7300 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007301 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 if (*file == NULL)
7303 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007304 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 vim_free(buffer);
7306 return FAIL;
7307 }
7308
7309 /*
7310 * Isolate the individual file names.
7311 */
7312 p = buffer;
7313 for (i = 0; i < *num_file; ++i)
7314 {
7315 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007316 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00007317 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02007318 || shell_style == STYLE_VIMGLOB || shell_style == STYLE_GLOBSTAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00007320 while (!(shell_style == STYLE_ECHO && *p == ' ')
7321 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007323 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 *p = NUL;
7325 else
7326 {
7327 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007328 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 }
7330 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007331 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007333 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007335 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 }
7337 }
7338
7339 /*
7340 * Move the file names to allocated memory.
7341 */
7342 for (j = 0, i = 0; i < *num_file; ++i)
7343 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007344 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7346 continue;
7347
Bram Moolenaar0f873732019-12-05 20:28:46 +01007348 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 dir = (mch_isdir((*file)[i]));
7350 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7351 continue;
7352
Bram Moolenaar0f873732019-12-05 20:28:46 +01007353 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007354 if (!dir && (flags & EW_EXEC)
7355 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007356 continue;
7357
Bram Moolenaar964b3742019-05-24 18:54:09 +02007358 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 if (p)
7360 {
7361 STRCPY(p, (*file)[i]);
7362 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007363 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 (*file)[j++] = p;
7365 }
7366 }
7367 vim_free(buffer);
7368 *num_file = j;
7369
Bram Moolenaar0f873732019-12-05 20:28:46 +01007370 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007372 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 goto notfound;
7374 }
7375
7376 return OK;
7377
7378notfound:
7379 if (flags & EW_NOTFOUND)
7380 return save_patterns(num_pat, pat, num_file, file);
7381 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382}
7383
Bram Moolenaar0f873732019-12-05 20:28:46 +01007384#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007387save_patterns(
7388 int num_pat,
7389 char_u **pat,
7390 int *num_file,
7391 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392{
7393 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007394 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007396 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 if (*file == NULL)
7398 return FAIL;
7399 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007400 {
7401 s = vim_strsave(pat[i]);
7402 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007403 // Be compatible with expand_filename(): halve the number of
7404 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007405 backslash_halve(s);
7406 (*file)[i] = s;
7407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 *num_file = num_pat;
7409 return OK;
7410}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412/*
7413 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7414 * expand.
7415 */
7416 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007417mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007419 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 if (*p == '\\' && p[1] != NUL)
7422 ++p;
7423 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 if (vim_strchr((char_u *)
7425#ifdef VMS
7426 "*?%"
7427#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429#endif
7430 , *p) != NULL)
7431 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 }
7433 return FALSE;
7434}
7435
7436/*
7437 * Return TRUE if the string "p" contains a wildcard.
7438 * Don't recognize '~' at the end as a wildcard.
7439 */
7440 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007441mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007443 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 if (*p == '\\' && p[1] != NUL)
7446 ++p;
7447 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 if (vim_strchr((char_u *)
7449#ifdef VMS
7450 "*?%$"
7451#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453#endif
7454 , *p) != NULL
7455 || (*p == '~' && p[1] != NUL))
7456 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 }
7458 return FALSE;
7459}
7460
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007462have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463{
7464 int i;
7465
7466 for (i = 0; i < num; i++)
7467 if (mch_has_wildcard(file[i]))
7468 return 1;
7469 return 0;
7470}
7471
7472 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007473have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474{
7475 int i;
7476
7477 for (i = 0; i < num; i++)
7478 if (vim_strchr(file[i], '$') != NULL)
7479 return TRUE;
7480 return FALSE;
7481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007483#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484/*
7485 * Scaled-down version of rename(), which is missing in Xenix.
7486 * This version can only move regular files and will fail if the
7487 * destination exists.
7488 */
7489 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007490mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
7492 struct stat st;
7493
Bram Moolenaar0f873732019-12-05 20:28:46 +01007494 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007496 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007498 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 return 0;
7500 return -1;
7501}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007502#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007504#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007505# if defined(DYNAMIC_GPM) || defined(PROTO)
7506/*
7507 * Initialize Gpm's symbols for dynamic linking.
7508 * Must be called only if libgpm_hinst is NULL.
7509 */
7510 static int
7511load_libgpm(void)
7512{
7513 libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL);
7514
7515 if (libgpm_hinst == NULL)
7516 {
7517 if (p_verbose > 0)
7518 smsg_attr(HL_ATTR(HLF_W),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007519 _("Could not load gpm library: %s"), dlerror());
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007520 return FAIL;
7521 }
7522
7523 if (
7524 (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL
7525 || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL
7526 || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL
7527 || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL
7528 || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL
7529 )
7530 {
7531 semsg(_(e_could_not_load_library_str_str), "gpm", dlerror());
7532 dlclose(libgpm_hinst);
7533 libgpm_hinst = NULL;
7534 dll_gpm_flag = NULL;
7535 dll_gpm_fd = NULL;
7536 return FAIL;
7537 }
7538 return OK;
7539}
7540
7541 int
7542gpm_available(void)
7543{
7544 return libgpm_hinst != NULL || load_libgpm() == OK;
7545}
7546# endif // DYNAMIC_GPM
7547
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548/*
7549 * Initializes connection with gpm (if it isn't already opened)
7550 * Return 1 if succeeded (or connection already opened), 0 if failed
7551 */
7552 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007553gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007555 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007557#ifdef DYNAMIC_GPM
7558 if (!gpm_available())
7559 return 0;
7560#endif
7561
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007562 if (gpm_flag)
7563 return 1; // already open
7564
7565 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7566 gpm_connect.defaultMask = ~GPM_HARD;
7567 // Default handling for mouse move
7568 gpm_connect.minMod = 0; // Handle any modifier keys
7569 gpm_connect.maxMod = 0xffff;
7570 if (Gpm_Open(&gpm_connect, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007572 // gpm library tries to handling TSTP causes
7573 // problems. Anyways, we close connection to Gpm whenever
7574 // we are going to suspend or starting an external process
7575 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007576# ifdef SIGTSTP
ichizok378447f2023-05-11 22:25:42 +01007577 mch_signal(SIGTSTP, restricted ? SIG_IGN : sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007578# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007579 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007581 if (gpm_fd == -2)
Julio Bcc59d622024-04-04 21:55:10 +02007582 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007583 Gpm_Close(); // We don't want to talk to xterm via gpm
Julio Bcc59d622024-04-04 21:55:10 +02007584
zeertzjqd9be94c2024-07-14 10:20:20 +02007585 // Gpm_Close fails to properly restore the WINCH and TSTP handlers,
7586 // leading to Vim ignoring resize signals. We have to re-initialize
7587 // these handlers again here.
Julio Bcc59d622024-04-04 21:55:10 +02007588# ifdef SIGWINCH
7589 mch_signal(SIGWINCH, sig_winch);
7590# endif
7591# ifdef SIGTSTP
7592 mch_signal(SIGTSTP, restricted ? SIG_IGN : sig_tstp);
7593# endif
7594 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007595 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596}
7597
7598/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007599 * Returns TRUE if the GPM mouse is enabled.
7600 */
7601 int
7602gpm_enabled(void)
7603{
7604 return gpm_flag && gpm_fd >= 0;
7605}
7606
7607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 */
7610 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007611gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007613 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 Gpm_Close();
7615}
7616
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007617/*
7618 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007620 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 */
7622 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007623mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624{
7625 int button;
7626 static Gpm_Event gpm_event;
7627 char_u string[6];
7628 int_u vim_modifiers;
7629 int row,col;
7630 unsigned char buttons_mask;
7631 unsigned char gpm_modifiers;
7632 static unsigned char old_buttons = 0;
7633
7634 Gpm_GetEvent(&gpm_event);
7635
7636#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007637 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 if (hold_gui_events)
7639 return 0;
7640#endif
7641
7642 row = gpm_event.y - 1;
7643 col = gpm_event.x - 1;
7644
Bram Moolenaar0f873732019-12-05 20:28:46 +01007645 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 string[1] = 'M';
7647 string[2] = 'G';
7648 switch (GPM_BARE_EVENTS(gpm_event.type))
7649 {
7650 case GPM_DRAG:
7651 string[3] = MOUSE_DRAG;
7652 break;
7653 case GPM_DOWN:
7654 buttons_mask = gpm_event.buttons & ~old_buttons;
7655 old_buttons = gpm_event.buttons;
7656 switch (buttons_mask)
7657 {
7658 case GPM_B_LEFT:
7659 button = MOUSE_LEFT;
7660 break;
7661 case GPM_B_MIDDLE:
7662 button = MOUSE_MIDDLE;
7663 break;
7664 case GPM_B_RIGHT:
7665 button = MOUSE_RIGHT;
7666 break;
7667 default:
7668 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007669 // Don't know what to do. Can more than one button be
7670 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 }
7672 string[3] = (char_u)(button | 0x20);
7673 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7674 break;
7675 case GPM_UP:
7676 string[3] = MOUSE_RELEASE;
7677 old_buttons &= ~gpm_event.buttons;
7678 break;
7679 default:
7680 return 0;
7681 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007682 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 gpm_modifiers = gpm_event.modifiers;
7684 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007685 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7686 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7687 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7689 vim_modifiers |= MOUSE_SHIFT;
7690
7691 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7692 vim_modifiers |= MOUSE_CTRL;
7693 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7694 vim_modifiers |= MOUSE_ALT;
7695 string[3] |= vim_modifiers;
7696 string[4] = (char_u)(col + ' ' + 1);
7697 string[5] = (char_u)(row + ' ' + 1);
7698 add_to_input_buf(string, 6);
7699 return 6;
7700}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007701#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007703#ifdef FEAT_SYSMOUSE
7704/*
7705 * Initialize connection with sysmouse.
7706 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7707 * output, any sysmouse output than will be processed via sig_sysmouse().
7708 * Return OK if succeeded, FAIL if failed.
7709 */
7710 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007711sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007712{
7713 struct mouse_info mouse;
7714
7715 mouse.operation = MOUSE_MODE;
7716 mouse.u.mode.mode = 0;
7717 mouse.u.mode.signal = SIGUSR2;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007718 if (ioctl(1, CONS_MOUSECTL, &mouse) == -1)
7719 return FAIL;
7720
ichizok378447f2023-05-11 22:25:42 +01007721 mch_signal(SIGUSR2, sig_sysmouse);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007722 mouse.operation = MOUSE_SHOW;
7723 ioctl(1, CONS_MOUSECTL, &mouse);
7724 return OK;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007725}
7726
7727/*
7728 * Stop processing SIGUSR2 signals, and also make sure that
7729 * virtual console do not send us any sysmouse related signal.
7730 */
7731 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007732sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007733{
7734 struct mouse_info mouse;
7735
ichizok378447f2023-05-11 22:25:42 +01007736 mch_signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007737 mouse.operation = MOUSE_MODE;
7738 mouse.u.mode.mode = 0;
7739 mouse.u.mode.signal = 0;
7740 ioctl(1, CONS_MOUSECTL, &mouse);
7741}
7742
7743/*
7744 * Gets info from sysmouse and adds special keys to input buf.
7745 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007746 static void
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007747sig_sysmouse SIGDEFARG(sigarg)
7748{
7749 struct mouse_info mouse;
7750 struct video_info video;
7751 char_u string[6];
7752 int row, col;
7753 int button;
7754 int buttons;
7755 static int oldbuttons = 0;
7756
7757#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007758 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007759 if (hold_gui_events)
7760 return;
7761#endif
7762
7763 mouse.operation = MOUSE_GETINFO;
7764 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7765 && ioctl(1, FBIO_MODEINFO, &video) != -1
7766 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7767 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7768 {
7769 row = mouse.u.data.y / video.vi_cheight;
7770 col = mouse.u.data.x / video.vi_cwidth;
7771 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007772 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007773 string[1] = 'M';
7774 string[2] = 'S';
7775 if (oldbuttons == buttons && buttons != 0)
7776 {
7777 button = MOUSE_DRAG;
7778 }
7779 else
7780 {
7781 switch (buttons)
7782 {
7783 case 0:
7784 button = MOUSE_RELEASE;
7785 break;
7786 case 1:
7787 button = MOUSE_LEFT;
7788 break;
7789 case 2:
7790 button = MOUSE_MIDDLE;
7791 break;
7792 case 4:
7793 button = MOUSE_RIGHT;
7794 break;
7795 default:
7796 return;
7797 }
7798 oldbuttons = buttons;
7799 }
7800 string[3] = (char_u)(button);
7801 string[4] = (char_u)(col + ' ' + 1);
7802 string[5] = (char_u)(row + ' ' + 1);
7803 add_to_input_buf(string, 6);
7804 }
7805 return;
7806}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007807#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007808
LemonBoy9987fe82024-07-04 13:20:49 +02007809/*
7810 * Fill the buffer 'buf' with 'len' random bytes.
7811 * Returns FAIL if the OS PRNG is not available or something went wrong.
7812 */
7813 int
7814mch_get_random(char_u *buf, int len)
7815{
7816 static int dev_urandom_state = NOTDONE;
7817
7818 if (dev_urandom_state == FAIL)
7819 return FAIL;
7820
7821 int fd = open("/dev/urandom", O_RDONLY);
7822
7823 // Attempt reading /dev/urandom.
7824 if (fd == -1)
7825 dev_urandom_state = FAIL;
7826 else if (read(fd, buf, len) == len)
Christian Brabandt93a3d2b2024-07-05 09:54:30 +02007827 {
LemonBoy9987fe82024-07-04 13:20:49 +02007828 dev_urandom_state = OK;
Christian Brabandt93a3d2b2024-07-05 09:54:30 +02007829 close(fd);
7830 }
LemonBoy9987fe82024-07-04 13:20:49 +02007831 else
7832 {
7833 dev_urandom_state = FAIL;
7834 close(fd);
7835 }
7836
7837 return dev_urandom_state;
7838}
7839
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007841typedef char_u * (*STRPROCSTR)(char_u *);
7842typedef char_u * (*INTPROCSTR)(int);
7843typedef int (*STRPROCINT)(char_u *);
7844typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845
7846/*
7847 * Call a DLL routine which takes either a string or int param
7848 * and returns an allocated string.
7849 */
7850 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007851mch_libcall(
7852 char_u *libname,
7853 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007854 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007855 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007856 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007857 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
7859# if defined(USE_DLOPEN)
7860 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007861 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862# else
7863 shl_t hinstLib;
7864# endif
7865 STRPROCSTR ProcAdd;
7866 INTPROCSTR ProcAddI;
7867 char_u *retval_str = NULL;
7868 int retval_int = 0;
7869 int success = FALSE;
7870
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007871 /*
7872 * Get a handle to the DLL module.
7873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007875 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007876 (void)dlerror();
7877
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 hinstLib = dlopen((char *)libname, RTLD_LAZY
7879# ifdef RTLD_LOCAL
7880 | RTLD_LOCAL
7881# endif
7882 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007883 if (hinstLib == NULL)
7884 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007885 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007886 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007887 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007888 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890# else
7891 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7892# endif
7893
Bram Moolenaar0f873732019-12-05 20:28:46 +01007894 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 if (hinstLib != NULL)
7896 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007897# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 /*
7899 * Catch a crash when calling the library function. For example when
7900 * using a number where a string pointer is expected.
7901 */
7902 mch_startjmp();
7903 if (SETJMP(lc_jump_env) != 0)
7904 {
7905 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007906# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007907 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 mch_didjmp();
7910 }
7911 else
7912# endif
7913 {
7914 retval_str = NULL;
7915 retval_int = 0;
7916
7917 if (argstring != NULL)
7918 {
7919# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007920 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007921 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922# else
7923 if (shl_findsym(&hinstLib, (const char *)funcname,
7924 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7925 ProcAdd = NULL;
7926# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007927 if ((success = (ProcAdd != NULL
7928# if defined(USE_DLOPEN)
7929 && dlerr == NULL
7930# endif
7931 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {
7933 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007934 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 else
7936 retval_str = (ProcAdd)(argstring);
7937 }
7938 }
7939 else
7940 {
7941# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007942 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007943 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944# else
7945 if (shl_findsym(&hinstLib, (const char *)funcname,
7946 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7947 ProcAddI = NULL;
7948# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007949 if ((success = (ProcAddI != NULL
7950# if defined(USE_DLOPEN)
7951 && dlerr == NULL
7952# endif
7953 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {
7955 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007956 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 else
7958 retval_str = (ProcAddI)(argint);
7959 }
7960 }
7961
Bram Moolenaar0f873732019-12-05 20:28:46 +01007962 // Save the string before we free the library.
7963 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 if (string_result == NULL)
7965 *number_result = retval_int;
7966 else if (retval_str != NULL
7967 && retval_str != (char_u *)1
7968 && retval_str != (char_u *)-1)
7969 *string_result = vim_strsave(retval_str);
7970 }
7971
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007972# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 mch_endjmp();
7974# ifdef SIGHASARG
7975 if (lc_signal != 0)
7976 {
7977 int i;
7978
Bram Moolenaar0f873732019-12-05 20:28:46 +01007979 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 for (i = 0; signal_info[i].sig != -1; i++)
7981 if (lc_signal == signal_info[i].sig)
7982 break;
Bram Moolenaar50809a42023-05-20 16:39:07 +01007983 semsg(_(e_got_sig_str_in_libcall), signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 }
7985# endif
7986# endif
7987
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007989 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007990 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007991 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007992
Bram Moolenaar0f873732019-12-05 20:28:46 +01007993 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 (void)dlclose(hinstLib);
7995# else
7996 (void)shl_unload(hinstLib);
7997# endif
7998 }
7999
8000 if (!success)
8001 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008002 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 return FAIL;
8004 }
8005
8006 return OK;
8007}
8008#endif
8009
8010#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008011static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012static int xterm_button;
8013
8014/*
8015 * Setup a dummy window for X selections in a terminal.
8016 */
8017 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008018setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019{
8020 int z = 0;
8021 char *strp = "";
8022 Widget AppShell;
8023
8024 if (!x_connect_to_server())
8025 return;
8026
8027 open_app_context();
8028 if (app_context != NULL && xterm_Shell == (Widget)0)
8029 {
Dominique Pellé4927bc72023-09-24 16:12:07 +02008030 int (*oldhandler)(Display*, XErrorEvent*);
Bram Moolenaarb2148f52019-01-20 23:43:57 +01008031# if defined(USING_SETJMP)
Dominique Pellé4927bc72023-09-24 16:12:07 +02008032 int (*oldIOhandler)(Display*);
Bram Moolenaaredce7422019-01-20 18:39:30 +01008033# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01008034# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01008035 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036
8037 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01008038 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039# endif
8040
Bram Moolenaar0f873732019-12-05 20:28:46 +01008041 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 oldhandler = XSetErrorHandler(x_error_check);
8043
Bram Moolenaarb2148f52019-01-20 23:43:57 +01008044# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008045 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
8047 mch_startjmp();
8048 if (SETJMP(lc_jump_env) != 0)
8049 {
8050 mch_didjmp();
8051 xterm_dpy = NULL;
8052 }
8053 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01008054# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 {
8056 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
8057 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008058 if (xterm_dpy != NULL)
8059 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01008060# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01008062# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 }
8064
Bram Moolenaarb2148f52019-01-20 23:43:57 +01008065# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008066 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01008068# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01008069 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 (void)XSetErrorHandler(oldhandler);
8071
8072 if (xterm_dpy == NULL)
8073 {
8074 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008075 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 return;
8077 }
8078
Bram Moolenaar0f873732019-12-05 20:28:46 +01008079 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 (void)XSetIOErrorHandler(x_IOerror_handler);
8081
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01008082# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008084 {
8085 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01008086 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008087 verbose_leave();
8088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089# endif
8090
Bram Moolenaar0f873732019-12-05 20:28:46 +01008091 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
8093 applicationShellWidgetClass, xterm_dpy,
8094 NULL);
8095 if (AppShell == (Widget)0)
8096 return;
8097 xterm_Shell = XtVaCreatePopupShell("VIM",
8098 topLevelShellWidgetClass, AppShell,
8099 XtNmappedWhenManaged, 0,
8100 XtNwidth, 1,
8101 XtNheight, 1,
8102 NULL);
8103 if (xterm_Shell == (Widget)0)
8104 return;
8105
8106 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02008107 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 if (x11_display == NULL)
8109 x11_display = xterm_dpy;
8110
8111 XtRealizeWidget(xterm_Shell);
8112 XSync(xterm_dpy, False);
8113 xterm_update();
8114 }
8115 if (xterm_Shell != (Widget)0)
8116 {
8117 clip_init(TRUE);
8118 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
8119 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01008120 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 if (test_x11_window(xterm_dpy) == FAIL)
8122 x11_window = 0;
8123 if (x11_window != 0)
8124 xterm_trace = 0;
8125 }
8126}
8127
8128 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008129start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130{
8131 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
8132 return;
8133 xterm_trace = 1;
8134 xterm_button = button;
8135 do_xterm_trace();
8136}
8137
8138
8139 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008140stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141{
8142 if (xterm_trace < 0)
8143 return;
8144 xterm_trace = 0;
8145}
8146
8147/*
8148 * Query the xterm pointer and generate mouse termcodes if necessary
8149 * return TRUE if dragging is active, else FALSE
8150 */
8151 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008152do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153{
8154 Window root, child;
8155 int root_x, root_y;
8156 int win_x, win_y;
8157 int row, col;
8158 int_u mask_return;
8159 char_u buf[50];
8160 char_u *strp;
8161 long got_hints;
8162 static char_u *mouse_code;
8163 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
8164 static int prev_row = 0, prev_col = 0;
8165 static XSizeHints xterm_hints;
8166
8167 if (xterm_trace <= 0)
8168 return FALSE;
8169
8170 if (xterm_trace == 1)
8171 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008172 // Get the hints just before tracking starts. The font size might
8173 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00008174 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
8175 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 || xterm_hints.width_inc <= 1
8177 || xterm_hints.height_inc <= 1)
8178 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008179 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 return FALSE;
8181 }
8182
Bram Moolenaar0f873732019-12-05 20:28:46 +01008183 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 mouse_code = find_termcode(mouse_name);
8185 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02008186 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 xterm_trace = 2;
8188
Bram Moolenaar0f873732019-12-05 20:28:46 +01008189 // Find the offset of the chars, there might be a scrollbar on the
8190 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
8192 &win_x, &win_y, &mask_return);
8193 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
8194 - (xterm_hints.height_inc / 2);
8195 if (xterm_hints.y <= xterm_hints.height_inc / 2)
8196 xterm_hints.y = 2;
8197 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
8198 - (xterm_hints.width_inc / 2);
8199 if (xterm_hints.x <= xterm_hints.width_inc / 2)
8200 xterm_hints.x = 2;
8201 return TRUE;
8202 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02008203 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {
8205 xterm_trace = 0;
8206 return FALSE;
8207 }
8208
8209 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
8210 &win_x, &win_y, &mask_return);
8211
8212 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
8213 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
8214 if (row == prev_row && col == prev_col)
8215 return TRUE;
8216
8217 STRCPY(buf, mouse_code);
8218 strp = buf + STRLEN(buf);
8219 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
8220 *strp++ = (char_u)(col + ' ' + 1);
8221 *strp++ = (char_u)(row + ' ' + 1);
8222 *strp = 0;
8223 add_to_input_buf(buf, STRLEN(buf));
8224
8225 prev_row = row;
8226 prev_col = col;
8227 return TRUE;
8228}
8229
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02008230# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231/*
8232 * Destroy the display, window and app_context. Required for GTK.
8233 */
8234 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008235clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237 if (xterm_Shell != (Widget)0)
8238 {
8239 XtDestroyWidget(xterm_Shell);
8240 xterm_Shell = (Widget)0;
8241 }
8242 if (xterm_dpy != NULL)
8243 {
Bram Moolenaare8208012008-06-20 09:59:25 +00008244# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008245 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00008247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 if (x11_display == xterm_dpy)
8249 x11_display = NULL;
8250 xterm_dpy = NULL;
8251 }
Bram Moolenaare8208012008-06-20 09:59:25 +00008252# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 if (app_context != (XtAppContext)NULL)
8254 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008255 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 XtDestroyApplicationContext(app_context);
8257 app_context = (XtAppContext)NULL;
8258 }
Bram Moolenaare8208012008-06-20 09:59:25 +00008259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260}
8261# endif
8262
8263/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01008264 * Catch up with GUI or X events.
8265 */
8266 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008267clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01008268{
8269# ifdef FEAT_GUI
8270 if (gui.in_use)
8271 gui_mch_update();
8272 else
8273# endif
8274 if (xterm_Shell != (Widget)0)
8275 xterm_update();
8276}
8277
8278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 * Catch up with any queued X events. This may put keyboard input into the
8280 * input buffer, call resize call-backs, trigger timers etc. If there is
8281 * nothing in the X event queue (& no timers pending), then we return
8282 * immediately.
8283 */
8284 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008285xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286{
8287 XEvent event;
8288
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008289 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008291 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008293 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008294 break;
8295
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008296 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008297 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008298 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008299 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008300#ifdef FEAT_CLIENTSERVER
8301 {
8302 XPropertyEvent *e = (XPropertyEvent *)&event;
8303
8304 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008306 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008309 XtDispatchEvent(&event);
8310 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01008311 else
8312 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008313 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008314 XtAppProcessEvent(app_context, mask);
8315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 }
8317}
8318
8319 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008320clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321{
8322 if (xterm_Shell != (Widget)0)
8323 return clip_x11_own_selection(xterm_Shell, cbd);
8324 return FAIL;
8325}
8326
8327 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008328clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329{
8330 if (xterm_Shell != (Widget)0)
8331 clip_x11_lose_selection(xterm_Shell, cbd);
8332}
8333
8334 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008335clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336{
8337 if (xterm_Shell != (Widget)0)
8338 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
8339}
8340
8341 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008342clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343{
8344 clip_x11_set_selection(cbd);
8345}
8346#endif
8347
8348
8349#if defined(USE_XSMP) || defined(PROTO)
8350/*
8351 * Code for X Session Management Protocol.
8352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353
8354# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355/*
8356 * This is our chance to ask the user if they want to save,
8357 * or abort the logout
8358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008360xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361{
Bram Moolenaare1004402020-10-24 20:49:43 +02008362 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 int cancel_shutdown = False;
8364
Bram Moolenaare1004402020-10-24 20:49:43 +02008365 save_cmod_flags = cmdmod.cmod_flags;
8366 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01008367 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01008368 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02008370 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01008371 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 out_flush();
8373
Bram Moolenaar0f873732019-12-05 20:28:46 +01008374 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 SmcInteractDone(smc_conn, cancel_shutdown);
8376
Bram Moolenaar0f873732019-12-05 20:28:46 +01008377 // Finish off
8378 // Only end save-yourself here if we're not cancelling shutdown;
8379 // we'll get a cancelled callback later in which we'll end it.
8380 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 if (!cancel_shutdown)
8382 {
8383 xsmp.save_yourself = False;
8384 SmcSaveYourselfDone(smc_conn, True);
8385 }
8386}
8387# endif
8388
8389/*
8390 * Callback that starts save-yourself.
8391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008393xsmp_handle_save_yourself(
8394 SmcConn smc_conn,
8395 SmPointer client_data UNUSED,
8396 int save_type UNUSED,
8397 Bool shutdown,
8398 int interact_style UNUSED,
8399 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008401 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 if (xsmp.save_yourself)
8403 SmcSaveYourselfDone(smc_conn, True);
8404 xsmp.save_yourself = True;
8405 xsmp.shutdown = shutdown;
8406
Bram Moolenaar0f873732019-12-05 20:28:46 +01008407 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01008409 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410
8411 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008412 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
8414# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008415 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008417 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 SmcInteractRequest(smc_conn, SmDialogError,
8419 xsmp_handle_interaction, client_data);
8420 else
8421# endif
8422 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008423 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 SmcSaveYourselfDone(smc_conn, True);
8425 xsmp.save_yourself = False;
8426 }
8427}
8428
8429
8430/*
8431 * Callback to warn us of imminent death.
8432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008434xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435{
8436 xsmp_close();
8437
Bram Moolenaar0f873732019-12-05 20:28:46 +01008438 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 getout_preserve_modified(0);
8440}
8441
8442
8443/*
8444 * Callback to tell us that save-yourself has completed.
8445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008447xsmp_save_complete(
8448 SmcConn smc_conn UNUSED,
8449 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450{
8451 xsmp.save_yourself = False;
8452}
8453
8454
8455/*
8456 * Callback to tell us that an instigated shutdown was cancelled
8457 * (maybe even by us)
8458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008460xsmp_shutdown_cancelled(
8461 SmcConn smc_conn,
8462 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463{
8464 if (xsmp.save_yourself)
8465 SmcSaveYourselfDone(smc_conn, True);
8466 xsmp.save_yourself = False;
8467 xsmp.shutdown = False;
8468}
8469
8470
8471/*
8472 * Callback to tell us that a new ICE connection has been established.
8473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008475xsmp_ice_connection(
8476 IceConn iceConn,
8477 IcePointer clientData UNUSED,
8478 Bool opening,
8479 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008481 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 if (opening)
8483 {
8484 xsmp_icefd = IceConnectionNumber(iceConn);
8485 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8486 }
8487}
8488
8489
Bram Moolenaar0f873732019-12-05 20:28:46 +01008490// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008492xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493{
8494 Bool rep;
8495
8496 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8497 == IceProcessMessagesIOError)
8498 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008499 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008501 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 xsmp_close();
8503 return FAIL;
8504 }
8505 else
8506 return OK;
8507}
8508
8509static int dummy;
8510
Bram Moolenaar0f873732019-12-05 20:28:46 +01008511// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 void
8513xsmp_init(void)
8514{
8515 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 SmcCallbacks smcallbacks;
8517#if 0
8518 SmPropValue smname;
8519 SmProp smnameprop;
8520 SmProp *smprops[1];
8521#endif
8522
8523 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008524 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525
8526 xsmp.save_yourself = xsmp.shutdown = False;
8527
Bram Moolenaar0f873732019-12-05 20:28:46 +01008528 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8530 smcallbacks.save_yourself.client_data = NULL;
8531 smcallbacks.die.callback = xsmp_die;
8532 smcallbacks.die.client_data = NULL;
8533 smcallbacks.save_complete.callback = xsmp_save_complete;
8534 smcallbacks.save_complete.client_data = NULL;
8535 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8536 smcallbacks.shutdown_cancelled.client_data = NULL;
8537
Bram Moolenaar0f873732019-12-05 20:28:46 +01008538 // Set up a watch on ICE connection creations. The "dummy" argument is
8539 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8541 {
8542 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008543 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 return;
8545 }
8546
Bram Moolenaar0f873732019-12-05 20:28:46 +01008547 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 xsmp.smcconn = SmcOpenConnection(
8549 NULL,
8550 NULL,
8551 SmProtoMajor,
8552 SmProtoMinor,
8553 SmcSaveYourselfProcMask | SmcDieProcMask
8554 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8555 &smcallbacks,
8556 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008557 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008558 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 errorstring);
8560 if (xsmp.smcconn == NULL)
8561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008563 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008564 char errorreport[132];
8565
8566 // If the message is too long it might not be NUL terminated. Add
8567 // a NUL at the end to make sure we don't go over the end.
8568 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008569 vim_snprintf(errorreport, sizeof(errorreport),
8570 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008571 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 return;
8574 }
8575 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8576
8577#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008578 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 smname.value = "vim";
8580 smname.length = 3;
8581 smnameprop.name = "SmProgram";
8582 smnameprop.type = "SmARRAY8";
8583 smnameprop.num_vals = 1;
8584 smnameprop.vals = &smname;
8585
8586 smprops[0] = &smnameprop;
8587 SmcSetProperties(xsmp.smcconn, 1, smprops);
8588#endif
8589}
8590
8591
Bram Moolenaar0f873732019-12-05 20:28:46 +01008592// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008594xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008596 if (xsmp_icefd == -1)
8597 return;
8598
8599 SmcCloseConnection(xsmp.smcconn, 0, NULL);
8600 if (xsmp.clientid != NULL)
8601 free(xsmp.clientid);
8602 xsmp.clientid = NULL;
8603 xsmp_icefd = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008605#endif // USE_XSMP
Paul Ollis65745772022-06-05 16:55:54 +01008606
8607#if defined(FEAT_RELTIME) || defined(PROTO)
Bram Moolenaar08210f82023-04-13 19:15:54 +01008608# if defined(PROF_NSEC) || defined(PROTO)
Paul Ollis65745772022-06-05 16:55:54 +01008609/*
8610 * Implement timeout with timer_create() and timer_settime().
8611 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008612static volatile sig_atomic_t timeout_flag = FALSE;
8613static timer_t timer_id;
8614static int timer_created = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008615
8616/*
8617 * Callback for when the timer expires.
8618 */
8619 static void
8620set_flag(union sigval _unused UNUSED)
8621{
8622 timeout_flag = TRUE;
8623}
8624
8625/*
8626 * Stop any active timeout.
8627 */
8628 void
8629stop_timeout(void)
8630{
8631 static struct itimerspec disarm = {{0, 0}, {0, 0}};
8632
8633 if (timer_created)
8634 {
8635 int ret = timer_settime(timer_id, 0, &disarm, NULL);
8636
8637 if (ret < 0)
8638 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8639 }
8640
8641 // Clear the current timeout flag; any previous timeout should be
8642 // considered _not_ triggered.
8643 timeout_flag = FALSE;
8644}
8645
8646/*
8647 * Start the timeout timer.
8648 *
8649 * The return value is a pointer to a flag that is initialised to FALSE. If the
8650 * timeout expires, the flag is set to TRUE. This will only return pointers to
8651 * static memory; i.e. any pointer returned by this function may always be
8652 * safely dereferenced.
8653 *
8654 * This function is not expected to fail, but if it does it will still return a
8655 * valid flag pointer; the flag will remain stuck as FALSE .
8656 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008657 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008658start_timeout(long msec)
8659{
8660 struct itimerspec interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008661 {0, 0}, // Do not repeat.
8662 {msec / 1000, (msec % 1000) * 1000000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008663 int ret;
8664
8665 // This is really the caller's responsibility, but let's make sure the
8666 // previous timer has been stopped.
8667 stop_timeout();
Paul Ollis65745772022-06-05 16:55:54 +01008668
8669 if (!timer_created)
8670 {
8671 struct sigevent action = {0};
8672
8673 action.sigev_notify = SIGEV_THREAD;
8674 action.sigev_notify_function = set_flag;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008675 ret = timer_create(CLOCK_MONOTONIC, &action, &timer_id);
8676 if (ret < 0)
Paul Ollis65745772022-06-05 16:55:54 +01008677 {
8678 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8679 return &timeout_flag;
8680 }
8681 timer_created = TRUE;
8682 }
8683
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00008684# ifdef FEAT_EVAL
Bram Moolenaar616592e2022-06-17 15:17:10 +01008685 ch_log(NULL, "setting timeout timer to %d sec %ld nsec",
8686 (int)interval.it_value.tv_sec, (long)interval.it_value.tv_nsec);
Bram Moolenaar509ce032022-06-20 11:23:01 +01008687# endif
Paul Ollis65745772022-06-05 16:55:54 +01008688 ret = timer_settime(timer_id, 0, &interval, NULL);
8689 if (ret < 0)
8690 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8691
8692 return &timeout_flag;
8693}
8694
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008695/*
8696 * To be used before fork/exec: delete any created timer.
8697 */
8698 void
8699delete_timer(void)
8700{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008701 if (!timer_created)
8702 return;
8703
8704 timer_delete(timer_id);
8705 timer_created = FALSE;
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008706}
8707
Bram Moolenaar08210f82023-04-13 19:15:54 +01008708# else // PROF_NSEC
Paul Ollis65745772022-06-05 16:55:54 +01008709
8710/*
8711 * Implement timeout with setitimer()
8712 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008713static struct sigaction prev_sigaction;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008714static volatile sig_atomic_t timeout_flag = FALSE;
8715static int timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008716static int timer_handler_active = FALSE;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008717static volatile sig_atomic_t alarm_pending = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008718
8719/*
8720 * Handle SIGALRM for a timeout.
8721 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01008722 static void
Paul Ollis65745772022-06-05 16:55:54 +01008723set_flag SIGDEFARG(sigarg)
8724{
8725 if (alarm_pending)
8726 alarm_pending = FALSE;
8727 else
8728 timeout_flag = TRUE;
8729}
8730
8731/*
8732 * Stop any active timeout.
8733 */
8734 void
8735stop_timeout(void)
8736{
8737 static struct itimerval disarm = {{0, 0}, {0, 0}};
8738 int ret;
8739
8740 if (timer_active)
8741 {
8742 timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008743 ret = setitimer(ITIMER_REAL, &disarm, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008744 if (ret < 0)
8745 // Should only get here as a result of coding errors.
8746 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8747 }
8748
8749 if (timer_handler_active)
8750 {
8751 timer_handler_active = FALSE;
8752 ret = sigaction(SIGALRM, &prev_sigaction, NULL);
8753 if (ret < 0)
8754 // Should only get here as a result of coding errors.
8755 semsg(_(e_could_not_reset_handler_for_timeout_str),
8756 strerror(errno));
8757 }
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008758 timeout_flag = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008759}
8760
8761/*
8762 * Start the timeout timer.
8763 *
8764 * The return value is a pointer to a flag that is initialised to FALSE. If the
8765 * timeout expires, the flag is set to TRUE. This will only return pointers to
8766 * static memory; i.e. any pointer returned by this function may always be
8767 * safely dereferenced.
8768 *
8769 * This function is not expected to fail, but if it does it will still return a
8770 * valid flag pointer; the flag will remain stuck as FALSE .
8771 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008772 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008773start_timeout(long msec)
8774{
8775 struct itimerval interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008776 {0, 0}, // Do not repeat.
8777 {msec / 1000, (msec % 1000) * 1000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008778 struct sigaction handle_alarm;
8779 int ret;
8780 sigset_t sigs;
8781 sigset_t saved_sigs;
8782
8783 // This is really the caller's responsibility, but let's make sure the
8784 // previous timer has been stopped.
8785 stop_timeout();
8786
8787 // There is a small chance that SIGALRM is pending and so the handler must
8788 // ignore it on the first call.
8789 alarm_pending = FALSE;
8790 ret = sigemptyset(&sigs);
8791 ret = ret == 0 ? sigaddset(&sigs, SIGALRM) : ret;
8792 ret = ret == 0 ? sigprocmask(SIG_BLOCK, &sigs, &saved_sigs) : ret;
8793 timeout_flag = FALSE;
8794 ret = ret == 0 ? sigpending(&sigs) : ret;
8795 if (ret == 0)
8796 {
8797 alarm_pending = sigismember(&sigs, SIGALRM);
Bram Moolenaar1f89abf2022-06-06 10:07:01 +01008798 ret = sigprocmask(SIG_SETMASK, &saved_sigs, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008799 }
8800 if (unlikely(ret != 0 || alarm_pending < 0))
8801 {
8802 // Just catching coding errors. Write an error message, but carry on.
8803 semsg(_(e_could_not_check_for_pending_sigalrm_str), strerror(errno));
8804 alarm_pending = FALSE;
8805 }
8806
8807 // Set up the alarm handler first.
8808 ret = sigemptyset(&handle_alarm.sa_mask);
8809 handle_alarm.sa_handler = set_flag;
8810 handle_alarm.sa_flags = 0;
8811 ret = ret == 0 ? sigaction(SIGALRM, &handle_alarm, &prev_sigaction) : ret;
8812 if (ret < 0)
8813 {
8814 // Should only get here as a result of coding errors.
8815 semsg(_(e_could_not_set_handler_for_timeout_str), strerror(errno));
8816 return &timeout_flag;
8817 }
8818 timer_handler_active = TRUE;
8819
8820 // Set up the interval timer once the alarm handler is in place.
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008821 ret = setitimer(ITIMER_REAL, &interval, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008822 if (ret < 0)
8823 {
8824 // Should only get here as a result of coding errors.
8825 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8826 stop_timeout();
8827 return &timeout_flag;
8828 }
8829
8830 timer_active = TRUE;
8831 return &timeout_flag;
8832}
Bram Moolenaar08210f82023-04-13 19:15:54 +01008833# endif // PROF_NSEC
Paul Ollis65745772022-06-05 16:55:54 +01008834#endif // FEAT_RELTIME