blob: 5801965874edacd05903d7ecea77b7b3ff169a0a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * OS/2 port by Paul Slootman
5 * VMS merge by Zoltan Arpadffy
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code.
10 */
11
12/*
13 * os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
14 * Also for OS/2, using the excellent EMX package!!!
Bram Moolenaar041c7102020-05-30 18:14:57 +020015 * Also for Atari MiNT.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016 *
17 * A lot of this file was originally written by Juergen Weigert and later
18 * changed beyond recognition.
19 */
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#include "vim.h"
22
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023#ifdef FEAT_MZSCHEME
24# include "if_mzsch.h"
25#endif
26
Bram Moolenaar0f873732019-12-05 20:28:46 +010027#include "os_unixx.h" // unix includes for os_unix.c only
Bram Moolenaar071d4272004-06-13 20:20:40 +000028
29#ifdef USE_XSMP
30# include <X11/SM/SMlib.h>
31#endif
32
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000033#ifdef HAVE_SELINUX
34# include <selinux/selinux.h>
35static int selinux_enabled = -1;
36#endif
37
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020038#ifdef HAVE_SMACK
39# include <attr/xattr.h>
40# include <linux/xattr.h>
41# ifndef SMACK_LABEL_LEN
42# define SMACK_LABEL_LEN 1024
43# endif
44#endif
45
Bram Moolenaara2442432007-04-26 14:26:37 +000046#ifdef __CYGWIN__
K.Takata972db232022-02-04 10:45:38 +000047# include <cygwin/version.h>
48# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
Bram Moolenaar0f873732019-12-05 20:28:46 +010049 // for cygwin_conv_path()
K.Takata972db232022-02-04 10:45:38 +000050# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
51# define WIN32_LEAN_AND_MEAN
52# include <windows.h>
53# include "winclip.pro"
Bram Moolenaara2442432007-04-26 14:26:37 +000054# endif
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000058
Bram Moolenaar071d4272004-06-13 20:20:40 +000059# include <gpm.h>
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000060
61# ifdef DYNAMIC_GPM
62# define Gpm_Open (*dll_Gpm_Open)
63# define Gpm_Close (*dll_Gpm_Close)
64# define Gpm_GetEvent (*dll_Gpm_GetEvent)
65# define gpm_flag (dll_gpm_flag != NULL ? *dll_gpm_flag : 0)
66# define gpm_fd (dll_gpm_fd != NULL ? *dll_gpm_fd : -1)
67
68static int (*dll_Gpm_Open) (Gpm_Connect *, int);
69static int (*dll_Gpm_Close) (void);
70static int (*dll_Gpm_GetEvent) (Gpm_Event *);
71static int *dll_gpm_flag;
72static int *dll_gpm_fd;
73
74static void *libgpm_hinst;
75# endif
76
Bram Moolenaar0f873732019-12-05 20:28:46 +010077// <linux/keyboard.h> contains defines conflicting with "keymap.h",
78// I just copied relevant defines here. A cleaner solution would be to put gpm
79// code into separate file and include there linux/keyboard.h
80// #include <linux/keyboard.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000081# define KG_SHIFT 0
82# define KG_CTRL 2
83# define KG_ALT 3
84# define KG_ALTGR 1
85# define KG_SHIFTL 4
86# define KG_SHIFTR 5
87# define KG_CTRLL 6
88# define KG_CTRLR 7
89# define KG_CAPSSHIFT 8
90
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010091static void gpm_close(void);
92static int gpm_open(void);
93static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#endif
95
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000096#ifdef FEAT_SYSMOUSE
97# include <sys/consio.h>
98# include <sys/fbio.h>
99
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100100static int sysmouse_open(void);
101static void sysmouse_close(void);
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100102static void sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000103#endif
104
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105/*
106 * end of autoconf section. To be extended...
107 */
108
Bram Moolenaar0f873732019-12-05 20:28:46 +0100109// Are the following #ifdefs still required? And why? Is that for X11?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
111#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
112# ifdef SIGWINCH
113# undef SIGWINCH
114# endif
115# ifdef TIOCGWINSZ
116# undef TIOCGWINSZ
117# endif
118#endif
119
Bram Moolenaar0f873732019-12-05 20:28:46 +0100120#if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121# define SIGWINCH SIGWINDOW
122#endif
123
124#ifdef FEAT_X11
125# include <X11/Xlib.h>
126# include <X11/Xutil.h>
127# include <X11/Xatom.h>
128# ifdef FEAT_XCLIPBOARD
129# include <X11/Intrinsic.h>
130# include <X11/Shell.h>
131# include <X11/StringDefs.h>
132static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void clip_update(void);
134static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135# endif
136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137Window x11_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
140
Bram Moolenaar5c3128e2020-05-11 20:54:42 +0200141static int ignore_sigtstp = FALSE;
142
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200146static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +0200147static int unix_did_set_title = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static char_u *oldicon = NULL;
149static int did_set_icon = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
Bram Moolenaar205b8862011-09-07 15:04:31 +0200153#ifdef HAVE_UNION_WAIT
154typedef union wait waitstatus;
155#else
156typedef int waitstatus;
157#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200158static int WaitForChar(long msec, int *interrupted, int ignore_input);
159static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar041c7102020-05-30 18:14:57 +0200160#ifdef VMS
Bram Moolenaarcda77642016-06-04 13:32:35 +0200161int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200163static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#endif
165
166#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100167static int do_xterm_trace(void);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100168# define XT_TRACE_DELAY 50 // delay for xterm tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
170
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
173#if defined(SIGWINCH)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100174static void sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000176#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100177static void sig_tstp SIGPROTOARG;
Bram Moolenaarabd56da2022-06-23 20:46:27 +0100178// volatile because it is used in signal handler sig_tstp() and
179// sigcont_handler().
dbivolaruab16ad32021-12-29 19:41:47 +0000180static volatile sig_atomic_t in_mch_suspend = FALSE;
181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#if defined(SIGINT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100183static void catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200185#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100186static void catch_sigusr1 SIGPROTOARG;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100189static void catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000191#if defined(SIGALRM) && defined(FEAT_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define SET_SIG_ALARM
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100193static void sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100194// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200195static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196#endif
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100197static void deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100199static void catch_int_signal(void);
200static void set_signals(void);
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100201static void catch_signals(void (*func_deadly)(), void (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200202#ifdef HAVE_SIGPROCMASK
203# define SIGSET_DECL(set) sigset_t set;
204# define BLOCK_SIGNALS(set) block_signals(set)
205# define UNBLOCK_SIGNALS(set) unblock_signals(set)
206#else
207# define SIGSET_DECL(set)
208# define BLOCK_SIGNALS(set) do { /**/ } while (0)
209# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
210#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100211static int have_wildcard(int, char_u **);
212static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100214static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215
216#ifndef SIG_ERR
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100217# define SIG_ERR ((void (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#endif
219
Bram Moolenaar0f873732019-12-05 20:28:46 +0100220// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200221static volatile sig_atomic_t do_resize = FALSE;
dbivolaruab16ad32021-12-29 19:41:47 +0000222// volatile because it is used in signal handler sig_tstp().
223static volatile sig_atomic_t got_tstp = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224static char_u *extra_shell_arg = NULL;
225static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100226// volatile because it is used in signal handler deathtrap().
227static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
228// volatile because it is used in signal handler deathtrap().
229static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100231#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
232static int dont_check_job_ended = 0;
233#endif
234
Bram Moolenaar26e86442020-05-17 14:06:16 +0200235// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
236static tmode_T mch_cur_tmode = TMODE_COOK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237
238#ifdef USE_XSMP
239typedef struct
240{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100241 SmcConn smcconn; // The SM connection ID
242 IceConn iceconn; // The ICE connection ID
243 char *clientid; // The client ID for the current smc session
244 Bool save_yourself; // If we're in the middle of a save_yourself
245 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246} xsmp_config_T;
247
248static xsmp_config_T xsmp;
249#endif
250
251#ifdef SYS_SIGLIST_DECLARED
252/*
253 * I have seen
254 * extern char *_sys_siglist[NSIG];
Yegappan Lakshmananaebc6ef2022-08-27 21:24:26 +0100255 * on Linux, NetBSD and Solaris. It contains a nice list of strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 * that describe the signals. That is nearly what we want here. But
257 * autoconf does only check for sys_siglist (without the underscore), I
258 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100259 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 */
261#endif
262
263static struct signalinfo
264{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100265 int sig; // Signal number, eg. SIGSEGV etc
266 char *name; // Signal name (not char_u!).
267 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268} signal_info[] =
269{
270#ifdef SIGHUP
271 {SIGHUP, "HUP", TRUE},
272#endif
273#ifdef SIGQUIT
274 {SIGQUIT, "QUIT", TRUE},
275#endif
276#ifdef SIGILL
277 {SIGILL, "ILL", TRUE},
278#endif
279#ifdef SIGTRAP
280 {SIGTRAP, "TRAP", TRUE},
281#endif
282#ifdef SIGABRT
283 {SIGABRT, "ABRT", TRUE},
284#endif
285#ifdef SIGEMT
286 {SIGEMT, "EMT", TRUE},
287#endif
288#ifdef SIGFPE
289 {SIGFPE, "FPE", TRUE},
290#endif
291#ifdef SIGBUS
292 {SIGBUS, "BUS", TRUE},
293#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100294#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100295 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 {SIGSEGV, "SEGV", TRUE},
297#endif
298#ifdef SIGSYS
299 {SIGSYS, "SYS", TRUE},
300#endif
301#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100302 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303#endif
304#ifdef SIGTERM
305 {SIGTERM, "TERM", TRUE},
306#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100307#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 {SIGVTALRM, "VTALRM", TRUE},
309#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000310#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100311 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
312 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 {SIGPROF, "PROF", TRUE},
314#endif
315#ifdef SIGXCPU
316 {SIGXCPU, "XCPU", TRUE},
317#endif
318#ifdef SIGXFSZ
319 {SIGXFSZ, "XFSZ", TRUE},
320#endif
321#ifdef SIGUSR1
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200322 {SIGUSR1, "USR1", FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000324#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100325 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 {SIGUSR2, "USR2", TRUE},
327#endif
328#ifdef SIGINT
329 {SIGINT, "INT", FALSE},
330#endif
331#ifdef SIGWINCH
332 {SIGWINCH, "WINCH", FALSE},
333#endif
334#ifdef SIGTSTP
335 {SIGTSTP, "TSTP", FALSE},
336#endif
337#ifdef SIGPIPE
338 {SIGPIPE, "PIPE", FALSE},
339#endif
340 {-1, "Unknown!", FALSE}
341};
342
Bram Moolenaar25724922009-07-14 15:38:41 +0000343 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100344mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000345{
346 if (p_verbose >= 5)
347 {
348 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100349 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000350 verbose_leave();
351 }
352# ifdef VMS
353 return chdir(vms_fixfilename(path));
354# else
355 return chdir(path);
356# endif
357}
358
Bram Moolenaar0f873732019-12-05 20:28:46 +0100359// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100360#if defined(ECHOE) && defined(ICANON) \
361 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
362 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200363# define NEW_TTY_SYSTEM
364#endif
365
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000366/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100367 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100370mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200372 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100373 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100374 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375}
376
377/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100378 * Function passed to inchar_loop() to handle window resizing.
379 * If "check_only" is TRUE: Return whether there was a resize.
380 * If "check_only" is FALSE: Deal with the window resized.
381 */
382 static int
383resize_func(int check_only)
384{
385 if (check_only)
386 return do_resize;
387 while (do_resize)
388 handle_resize();
389 return FALSE;
390}
391
392/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000393 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 * Get a characters from the keyboard.
395 * Return the number of characters that are available.
396 * If wtime == 0 do not wait for characters.
397 * If wtime == n wait a short time for characters.
398 * If wtime == -1 wait forever for characters.
399 */
400 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100401mch_inchar(
402 char_u *buf,
403 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100404 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100405 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100407 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
408 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409}
410
411 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100412handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413{
414 do_resize = FALSE;
415 shell_resized();
416}
417
418/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200419 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 */
421 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100422mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200424 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425}
426
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200427#if defined(FEAT_TERMINAL) || defined(PROTO)
428/*
429 * Check for any pending input or messages.
430 */
431 int
432mch_check_messages(void)
433{
434 return WaitForChar(0L, NULL, TRUE);
435}
436#endif
437
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
439# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000440# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441# endif
442# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
443# include <sys/sysctl.h>
444# endif
445# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
446# include <sys/sysinfo.h>
447# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100448# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100449# include <mach/mach_host.h>
450# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
453/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000454 * Return total amount of memory available in Kbyte.
455 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100458mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100461 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
Bram Moolenaar362dc332018-03-05 21:59:37 +0100463# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100464 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100465 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100466 mach_port_t host = mach_host_self();
467 kern_return_t kret;
468# ifdef HOST_VM_INFO64
469 struct vm_statistics64 vm_stat;
470 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471
Bram Moolenaar988615f2018-02-27 17:58:20 +0100472 kret = host_statistics64(host, HOST_VM_INFO64,
473 (host_info64_t)&vm_stat, &count);
474# else
475 struct vm_statistics vm_stat;
476 natural_t count = HOST_VM_INFO_COUNT;
477
478 kret = host_statistics(host, HOST_VM_INFO,
479 (host_info_t)&vm_stat, &count);
480# endif
481 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100482 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100483 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
484 + vm_stat.inactive_count
485# ifdef MAC_OS_X_VERSION_10_9
486 + vm_stat.compressor_page_count
487# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100488 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100489 mach_port_deallocate(mach_task_self(), host);
490 }
491# endif
492
493# ifdef HAVE_SYSCTL
494 if (mem == 0)
495 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100496 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100497 int mib[2];
498 size_t len = sizeof(long_u);
499# ifdef HW_USERMEM64
500 long_u physmem;
501# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100502 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100503 union {
504 int_u u32;
505 long_u u64;
506 } physmem;
507# endif
508
509 mib[0] = CTL_HW;
510# ifdef HW_USERMEM64
511 mib[1] = HW_USERMEM64;
512# else
513 mib[1] = HW_USERMEM;
514# endif
515 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
516 {
517# ifdef HW_USERMEM64
518 mem = (long_u)physmem;
519# else
520 if (len == sizeof(physmem.u64))
521 mem = (long_u)physmem.u64;
522 else
523 mem = (long_u)physmem.u32;
524# endif
525 }
526 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200529# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 if (mem == 0)
531 {
532 struct sysinfo sinfo;
533
Bram Moolenaar0f873732019-12-05 20:28:46 +0100534 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000536 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200537# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100538 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000539 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
540 {
541 sinfo.mem_unit = sinfo.mem_unit >> 1;
542 --shiftright;
543 }
544 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200545# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200547# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200552# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 if (mem == 0)
554 {
555 long pagesize, pagecount;
556
Bram Moolenaar0f873732019-12-05 20:28:46 +0100557 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 pagesize = sysconf(_SC_PAGESIZE);
559 pagecount = sysconf(_SC_PHYS_PAGES);
560 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000561 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100562 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000563 while (shiftright > 0 && (pagesize & 1) == 0)
564 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000565 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000566 --shiftright;
567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200571# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
Bram Moolenaar0f873732019-12-05 20:28:46 +0100573 // Return the minimum of the physical memory and the user limit, because
574 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200575# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 {
577 struct rlimit rlp;
578
579 if (getrlimit(RLIMIT_DATA, &rlp) == 0
580 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200581# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200583# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000584 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000586 {
587 mem = (long_u)rlp.rlim_cur;
588 shiftright = 10;
589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000594 return mem >> shiftright;
595 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596}
597#endif
598
Bram Moolenaar0981c872020-08-23 14:28:37 +0200599/*
600 * "flags": MCH_DELAY_IGNOREINPUT - don't read input
601 * MCH_DELAY_SETTMODE - use settmode() even for short delays
602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200604mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200606 tmode_T old_tmode;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200607 int call_settmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000608#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100609 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611
Bram Moolenaar0981c872020-08-23 14:28:37 +0200612 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100614 // Go to cooked mode without echo, to allow SIGINT interrupting us
615 // here. But we don't want QUIT to kill us (CTRL-\ used in a
616 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200617 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000618 in_mch_delay = TRUE;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200619 call_settmode = mch_cur_tmode == TMODE_RAW
620 && (msec > 500 || (flags & MCH_DELAY_SETTMODE));
621 if (call_settmode)
622 {
623 old_tmode = mch_cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 settmode(TMODE_SLEEP);
Bram Moolenaar80361a52020-10-05 21:39:25 +0200625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627 /*
628 * Everybody sleeps in a different way...
629 * Prefer nanosleep(), some versions of usleep() can only sleep up to
630 * one second.
631 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000632#ifdef FEAT_MZSCHEME
633 do
634 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100635 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000636 if (total > p_mzq)
637 msec = p_mzq;
638 else
639 msec = total;
640 total -= msec;
641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#ifdef HAVE_NANOSLEEP
643 {
644 struct timespec ts;
645
646 ts.tv_sec = msec / 1000;
647 ts.tv_nsec = (msec % 1000) * 1000000;
648 (void)nanosleep(&ts, NULL);
649 }
650#else
651# ifdef HAVE_USLEEP
652 while (msec >= 1000)
653 {
654 usleep((unsigned int)(999 * 1000));
655 msec -= 999;
656 }
657 usleep((unsigned int)(msec * 1000));
658# else
659# ifndef HAVE_SELECT
660 poll(NULL, 0, (int)msec);
661# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 {
663 struct timeval tv;
664
665 tv.tv_sec = msec / 1000;
666 tv.tv_usec = (msec % 1000) * 1000;
Bram Moolenaar0981c872020-08-23 14:28:37 +0200667 // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
668 // a patch from Sun to fix this. Reported by Gunnar Pedersen.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 select(0, NULL, NULL, NULL, &tv);
670 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100671# endif // HAVE_SELECT
672# endif // HAVE_NANOSLEEP
673#endif // HAVE_USLEEP
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000674#ifdef FEAT_MZSCHEME
675 }
676 while (total > 0);
677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
Bram Moolenaar80361a52020-10-05 21:39:25 +0200679 if (call_settmode)
Bram Moolenaar26e86442020-05-17 14:06:16 +0200680 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000681 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 }
683 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200684 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685}
686
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000687#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
689# define HAVE_CHECK_STACK_GROWTH
690/*
691 * Support for checking for an almost-out-of-stack-space situation.
692 */
693
694/*
695 * Return a pointer to an item on the stack. Used to find out if the stack
696 * grows up or down.
697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698static int stack_grows_downwards;
699
700/*
701 * Find out if the stack grows upwards or downwards.
702 * "p" points to a variable on the stack of the caller.
703 */
704 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100705check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706{
707 int i;
708
709 stack_grows_downwards = (p > (char *)&i);
710}
711#endif
712
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000713#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714static char *stack_limit = NULL;
715
716#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
717# include <pthread.h>
718# include <pthread_np.h>
719#endif
720
721/*
722 * Find out until how var the stack can grow without getting into trouble.
723 * Called when starting up and when switching to the signal stack in
724 * deathtrap().
725 */
726 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100727get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728{
729 struct rlimit rlp;
730 int i;
731 long lim;
732
Bram Moolenaar0f873732019-12-05 20:28:46 +0100733 // Set the stack limit to 15/16 of the allowable size. Skip this when the
734 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 if (getrlimit(RLIMIT_STACK, &rlp) == 0
736 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
737# ifdef RLIM_INFINITY
738 && rlp.rlim_cur != RLIM_INFINITY
739# endif
740 )
741 {
742 lim = (long)rlp.rlim_cur;
743#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
744 {
745 pthread_attr_t attr;
746 size_t size;
747
Bram Moolenaar0f873732019-12-05 20:28:46 +0100748 // On FreeBSD the initial thread always has a fixed stack size, no
749 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 pthread_attr_init(&attr);
751 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
752 {
753 pthread_attr_getstacksize(&attr, &size);
754 if (lim > (long)size)
755 lim = (long)size;
756 }
757 pthread_attr_destroy(&attr);
758 }
759#endif
760 if (stack_grows_downwards)
761 {
762 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
763 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100764 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 stack_limit = (char *)((long)&i / 16L);
766 }
767 else
768 {
769 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
770 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100771 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 }
773 }
774}
775
776/*
777 * Return FAIL when running out of stack space.
778 * "p" must point to any variable local to the caller that's on the stack.
779 */
780 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100781mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000783 if (stack_limit == NULL)
784 return OK;
785
786 if (stack_grows_downwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000788 if (p < stack_limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 return FAIL;
790 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000791 else if (p > stack_limit)
792 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 return OK;
794}
795#endif
796
797#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
798/*
799 * Support for using the signal stack.
800 * This helps when we run out of stack space, which causes a SIGSEGV. The
801 * signal handler then must run on another stack, since the normal stack is
802 * completely full.
803 */
804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100806static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100808static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809# endif
810
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200811/*
812 * Get a size of signal stack.
813 * Preference (if available): sysconf > SIGSTKSZ > guessed size
814 */
815static long int get_signal_stack_size()
816{
817# ifdef HAVE_SYSCONF_SIGSTKSZ
818 long int size = -1;
819
820 // return size only if sysconf doesn't return an error
821 if ((size = sysconf(_SC_SIGSTKSZ)) > -1)
822 return size;
823# endif
824
825# ifdef SIGSTKSZ
826 // if sysconf() isn't available or gives error, return SIGSTKSZ
827 // if defined
828 return SIGSTKSZ;
829# endif
830
831 // otherwise guess the size
832 return 8000;
833}
834
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835static char *signal_stack;
836
837 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100838init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000840 if (signal_stack == NULL)
841 return;
842
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844# ifdef HAVE_SS_BASE
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000845 sigstk.ss_base = signal_stack;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000847 sigstk.ss_sp = signal_stack;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000849 sigstk.ss_size = get_signal_stack_size();
850 sigstk.ss_flags = 0;
851 (void)sigaltstack(&sigstk, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000853 sigstk.ss_sp = signal_stack;
854 if (stack_grows_downwards)
855 sigstk.ss_sp += get_signal_stack_size() - 1;
856 sigstk.ss_onstack = 0;
857 (void)sigstack(&sigstk, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859}
860#endif
861
862/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000863 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 * will barf when the second argument to signal() is ``wrong''.
865 * Let me try it with a few tricky defines from my own osdef.h (jw).
866 */
867#if defined(SIGWINCH)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100868 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869sig_winch SIGDEFARG(sigarg)
870{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100871 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100872 signal(SIGWINCH, (void (*)())sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 do_resize = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874}
875#endif
876
dbivolaruab16ad32021-12-29 19:41:47 +0000877#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100878 static void
dbivolaruab16ad32021-12-29 19:41:47 +0000879sig_tstp SIGDEFARG(sigarg)
880{
881 // Second time we get called we actually need to suspend
882 if (in_mch_suspend)
883 {
884 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
885 raise(sigarg);
886 }
dbivolaru79a6e252022-01-23 16:41:14 +0000887 else
888 got_tstp = TRUE;
dbivolaruab16ad32021-12-29 19:41:47 +0000889
ichizok5f823d12022-03-13 17:27:38 +0000890#if !defined(__ANDROID__) && !defined(__OpenBSD__) && !defined(__DragonFly__)
891 // This is not required on all systems. On some systems (at least Android,
892 // OpenBSD, and DragonFlyBSD) this breaks suspending with CTRL-Z.
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100893 signal(SIGTSTP, (void (*)())sig_tstp);
xtkobacbef12e2022-02-27 12:31:52 +0000894#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000895}
896#endif
897
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898#if defined(SIGINT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100899 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900catch_sigint SIGDEFARG(sigarg)
901{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100902 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100903 signal(SIGINT, (void (*)())catch_sigint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 got_int = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905}
906#endif
907
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200908#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100909 static void
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200910catch_sigusr1 SIGDEFARG(sigarg)
911{
912 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100913 signal(SIGUSR1, (void (*)())catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200914 got_sigusr1 = TRUE;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200915}
916#endif
917
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100919 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920catch_sigpwr SIGDEFARG(sigarg)
921{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100922 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100923 signal(SIGPWR, (void (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 /*
925 * I'm not sure we get the SIGPWR signal when the system is really going
926 * down or when the batteries are almost empty. Just preserve the swap
927 * files and don't exit, that can't do any harm.
928 */
929 ml_sync_all(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930}
931#endif
932
933#ifdef SET_SIG_ALARM
934/*
935 * signal function for alarm().
936 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100937 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938sig_alarm SIGDEFARG(sigarg)
939{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100940 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 sig_alarm_called = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942}
943#endif
944
Bram Moolenaaredce7422019-01-20 18:39:30 +0100945#if (defined(HAVE_SETJMP_H) \
946 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
947 || defined(FEAT_LIBCALL))) \
948 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100949# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100950
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100951// argument to SETJMP()
952static JMP_BUF lc_jump_env;
953
954# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100955// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100956// Volatile because it is used in signal handlers.
957static volatile sig_atomic_t lc_signal;
958# endif
959
960// TRUE when lc_jump_env is valid.
961// Volatile because it is used in signal handler deathtrap().
zeertzjq9b7d2a92022-08-26 10:33:54 +0100962static volatile sig_atomic_t lc_active = FALSE;
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100963
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964/*
965 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100966 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100968 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 * Usage:
970 * mch_startjmp();
971 * if (SETJMP(lc_jump_env) != 0)
972 * {
973 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100974 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 * }
976 * else
977 * {
978 * do_the_work;
979 * mch_endjmp();
980 * }
981 * Note: Can't move SETJMP() here, because a function calling setjmp() must
982 * not return before the saved environment is used.
983 * Returns OK for normal return, FAIL when the protected code caused a
984 * problem and LONGJMP() was used.
985 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100986 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100987mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100989# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100991# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 lc_active = TRUE;
993}
994
Bram Moolenaar113e1072019-01-20 15:30:40 +0100995 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100996mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997{
998 lc_active = FALSE;
999}
1000
Bram Moolenaar113e1072019-01-20 15:30:40 +01001001 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001002mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
1004# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001005 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1006 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 init_signal_stack();
1008# endif
1009}
1010#endif
1011
1012/*
1013 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001014 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001016 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1017 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001019 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020deathtrap SIGDEFARG(sigarg)
1021{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001022 static int entered = 0; // count the number of times we got here.
1023 // Note: when memory has been corrupted
1024 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025#ifdef SIGHASARG
1026 int i;
1027#endif
1028
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001029#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 /*
1031 * Catch a crash in protected code.
1032 * Restores the environment saved in lc_jump_env, which looks like
1033 * SETJMP() returns 1.
1034 */
1035 if (lc_active)
1036 {
1037# if defined(SIGHASARG)
1038 lc_signal = sigarg;
1039# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001040 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001042 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 }
1044#endif
1045
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001046#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001047# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001048 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1049 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1050 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001051 if (in_mch_delay && sigarg == SIGQUIT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001052 return;
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001053# endif
1054
Bram Moolenaar0f873732019-12-05 20:28:46 +01001055 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1056 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1057 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001058 if (entered == 0
1059 && (0
1060# ifdef SIGHUP
1061 || sigarg == SIGHUP
1062# endif
1063# ifdef SIGQUIT
1064 || sigarg == SIGQUIT
1065# endif
1066# ifdef SIGTERM
1067 || sigarg == SIGTERM
1068# endif
1069# ifdef SIGPWR
1070 || sigarg == SIGPWR
1071# endif
1072# ifdef SIGUSR1
1073 || sigarg == SIGUSR1
1074# endif
1075# ifdef SIGUSR2
1076 || sigarg == SIGUSR2
1077# endif
1078 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001079 && !vim_handle_signal(sigarg))
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001080 return;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001081#endif
1082
Bram Moolenaar0f873732019-12-05 20:28:46 +01001083 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 ++entered;
1085
Bram Moolenaar0f873732019-12-05 20:28:46 +01001086 // Executing autocommands is likely to use more stack space than we have
1087 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001088 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001089
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001091 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 set_vim_var_nr(VV_DYING, (long)entered);
1093#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001094 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001096#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001097 // Since we are now using the signal stack, need to reset the stack
1098 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 get_stack_limit();
1100#endif
1101
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001102#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001103 // This is for opening gdb the moment Vim crashes.
1104 // You need to manually adjust the file name and Vim executable name.
1105 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001106 {
1107# define VI_GDB_FILE "/tmp/vimgdb"
1108# define VIM_NAME "/usr/bin/vim"
1109 FILE *fp = fopen(VI_GDB_FILE, "w");
1110 if (fp)
1111 {
1112 fprintf(fp,
1113 "file %s\n"
1114 "attach %d\n"
1115 "set height 1000\n"
1116 "bt full\n"
1117 , VIM_NAME, getpid());
1118 fclose(fp);
1119 system("xterm -e gdb -x "VI_GDB_FILE);
1120 unlink(VI_GDB_FILE);
1121 }
1122 }
1123#endif
1124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001126 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 for (i = 0; signal_info[i].sig != -1; i++)
1128 if (sigarg == signal_info[i].sig)
1129 break;
1130 deadly_signal = sigarg;
1131#endif
1132
Bram Moolenaar0f873732019-12-05 20:28:46 +01001133 full_screen = FALSE; // don't write message to the GUI, it might be
1134 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 /*
1136 * If something goes wrong after entering here, we may get here again.
1137 * When this happens, give a message and try to exit nicely (resetting the
1138 * terminal mode, etc.)
1139 * When this happens twice, just exit, don't even try to give a message,
1140 * stack may be corrupt or something weird.
1141 * When this still happens again (or memory was corrupted in such a way
1142 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1143 */
1144 if (entered >= 3)
1145 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001146 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 may_core_dump();
1148 if (entered >= 4)
1149 _exit(8);
1150 exit(7);
1151 }
1152 if (entered == 2)
1153 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001154 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001155 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 out_flush();
1157 getout(1);
1158 }
1159
Bram Moolenaar0f873732019-12-05 20:28:46 +01001160 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001162 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 signal_info[i].name);
1164#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001165 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001167
Bram Moolenaar0f873732019-12-05 20:28:46 +01001168 // Preserve files and exit. This sets the really_exiting flag to prevent
1169 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001170 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
Bram Moolenaar0f873732019-12-05 20:28:46 +01001172 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001173
Bram Moolenaar009b2592004-10-24 19:18:58 +00001174#ifdef NBDEBUG
1175 reset_signals();
1176 may_core_dump();
1177 abort();
1178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179}
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001182 * Invoked after receiving SIGCONT. We don't know what happened while
1183 * sleeping, deal with part of that.
1184 */
1185 static void
1186after_sigcont(void)
1187{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001188 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1189 // again later.
1190 oldtitle_outdated = TRUE;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001191
Bram Moolenaar2e310482018-08-21 13:09:10 +02001192 settmode(TMODE_RAW);
1193 need_check_timestamps = TRUE;
1194 did_check_timestamps = FALSE;
1195}
1196
1197#if defined(SIGCONT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001198static void sigcont_handler SIGPROTOARG;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001199
1200/*
1201 * With multi-threading, suspending might not work immediately. Catch the
1202 * SIGCONT signal, which will be used as an indication whether the suspending
1203 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001204 *
1205 * On Linux, signal is not always handled immediately either.
1206 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001207 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001208 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001209 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001211static volatile sig_atomic_t sigcont_received;
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001212static void sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
1214/*
1215 * signal handler for SIGCONT
1216 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001217 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218sigcont_handler SIGDEFARG(sigarg)
1219{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001220 if (in_mch_suspend)
1221 {
1222 sigcont_received = TRUE;
1223 }
1224 else
1225 {
1226 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1227 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1228 // back to a sane mode. We should redraw, but we can't really do that
1229 // in a signal handler, do a redraw later.
1230 after_sigcont();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001231 redraw_later(UPD_CLEAR);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001232 cursor_on_force();
1233 out_flush();
1234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235}
1236#endif
1237
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001238#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001239# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001240static void *clip_star_save = NULL;
1241static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001242# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001243
1244/*
1245 * Called when Vim is going to sleep or execute a shell command.
1246 * We can't respond to requests for the X selections. Lose them, otherwise
1247 * other applications will hang. But first copy the text to cut buffer 0.
1248 */
1249 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001250loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001251{
1252 if (clip_star.owned || clip_plus.owned)
1253 {
1254 x11_export_final_selection();
1255 if (clip_star.owned)
1256 clip_lose_selection(&clip_star);
1257 if (clip_plus.owned)
1258 clip_lose_selection(&clip_plus);
1259 if (x11_display != NULL)
1260 XFlush(x11_display);
1261 }
1262}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001263
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001264# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001265/*
1266 * Save clipboard text to restore later.
1267 */
1268 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001269save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001270{
1271 if (clip_star.owned)
1272 clip_star_save = get_register('*', TRUE);
1273 if (clip_plus.owned)
1274 clip_plus_save = get_register('+', TRUE);
1275}
1276
1277/*
1278 * Restore clipboard text if no one own the X selection.
1279 */
1280 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001281restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001282{
1283 if (clip_star_save != NULL)
1284 {
1285 if (!clip_gen_owner_exists(&clip_star))
1286 put_register('*', clip_star_save);
1287 else
1288 free_register(clip_star_save);
1289 clip_star_save = NULL;
1290 }
1291 if (clip_plus_save != NULL)
1292 {
1293 if (!clip_gen_owner_exists(&clip_plus))
1294 put_register('+', clip_plus_save);
1295 else
1296 free_register(clip_plus_save);
1297 clip_plus_save = NULL;
1298 }
1299}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001300# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001301#endif
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303/*
1304 * If the machine has job control, use it to suspend the program,
1305 * otherwise fake it by starting a new shell.
1306 */
1307 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001308mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001310 if (ignore_sigtstp)
1311 return;
1312
Bram Moolenaar041c7102020-05-30 18:14:57 +02001313#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001314 in_mch_suspend = TRUE;
1315
Bram Moolenaar0f873732019-12-05 20:28:46 +01001316 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001318 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319
1320# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001321 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001323# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 sigcont_received = FALSE;
1325# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001326
Bram Moolenaar0f873732019-12-05 20:28:46 +01001327 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001328
1329# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001330 /*
1331 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001332 * immediately, but somehow not all the time, probably because it's handled
1333 * in another thread. Do not call pause() because there would be race
1334 * condition which would hang Vim if signal happened in between the test of
1335 * sigcont_received and the call to pause(). If signal is not yet received,
1336 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1337 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001338 */
1339 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001340 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001341
Bram Moolenaar262735e2009-07-14 10:20:22 +00001342 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001343 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001346 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
Bram Moolenaar2e310482018-08-21 13:09:10 +02001348 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349#else
1350 suspend_shell();
1351#endif
1352}
1353
1354 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001355mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356{
1357 Columns = 80;
1358 Rows = 24;
1359
1360 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001361
1362#ifdef SIGTSTP
1363 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1364 // that indicates the shell (or program) that launched us does not support
1365 // tty job control and thus we should ignore that signal. If invoked as a
1366 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
1367 ignore_sigtstp = restricted || SIG_IGN == signal(SIGTSTP, SIG_ERR);
1368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001370
Bram Moolenaar56718732006-03-15 22:53:57 +00001371#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001372 mac_conv_init();
1373#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001374#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1375 win_clip_init();
1376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377}
1378
1379 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001380set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381{
1382#if defined(SIGWINCH)
1383 /*
1384 * WINDOW CHANGE signal is handled with sig_winch().
1385 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001386 signal(SIGWINCH, (void (*)())sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#endif
1388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001390 // See mch_init() for the conditions under which we ignore SIGTSTP.
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001391 // In the GUI default TSTP processing is OK.
1392 // Checking both gui.in_use and gui.starting because gui.in_use is not set
1393 // at this point (set after menus are displayed), but gui.starting is set.
1394 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN
1395# ifdef FEAT_GUI
1396 : gui.in_use || gui.starting ? SIG_DFL
1397# endif
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001398 : (void (*)())sig_tstp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001400#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 signal(SIGCONT, sigcont_handler);
1402#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001403#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 /*
1405 * We want to ignore breaking of PIPEs.
1406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 signal(SIGPIPE, SIG_IGN);
1408#endif
1409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001411 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412#endif
1413
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001414#ifdef SIGUSR1
1415 /*
1416 * Call user's handler on SIGUSR1
1417 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001418 signal(SIGUSR1, (void (*)())catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001419#endif
1420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 /*
1422 * Ignore alarm signals (Perl's alarm() generates it).
1423 */
1424#ifdef SIGALRM
1425 signal(SIGALRM, SIG_IGN);
1426#endif
1427
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001428#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 /*
1430 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1431 * work will be lost.
1432 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001433 signal(SIGPWR, (void (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434#endif
1435
1436 /*
1437 * Arrange for other signals to gracefully shutdown Vim.
1438 */
1439 catch_signals(deathtrap, SIG_ERR);
1440
1441#if defined(FEAT_GUI) && defined(SIGHUP)
1442 /*
1443 * When the GUI is running, ignore the hangup signal.
1444 */
1445 if (gui.in_use)
1446 signal(SIGHUP, SIG_IGN);
1447#endif
1448}
1449
Bram Moolenaardf177f62005-02-22 08:39:57 +00001450#if defined(SIGINT) || defined(PROTO)
1451/*
1452 * Catch CTRL-C (only works while in Cooked mode).
1453 */
1454 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001455catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001456{
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001457 signal(SIGINT, (void (*)())catch_sigint);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001458}
1459#endif
1460
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001462reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001465#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001466 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 signal(SIGCONT, SIG_DFL);
1468#endif
1469}
1470
1471 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001472catch_signals(
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001473 void (*func_deadly)(),
1474 void (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
1476 int i;
1477
1478 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (signal_info[i].deadly)
1481 {
1482#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1483 struct sigaction sa;
1484
Bram Moolenaar0f873732019-12-05 20:28:46 +01001485 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 sa.sa_handler = func_deadly;
1487 sigemptyset(&sa.sa_mask);
1488# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001489 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1490 // thread handling in combination with using the alternate stack:
1491 // pthread library functions try to use the stack pointer to
1492 // identify the current thread, causing a SEGV signal, which
1493 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 sa.sa_flags = 0;
1495# else
1496 sa.sa_flags = SA_ONSTACK;
1497# endif
1498 sigaction(signal_info[i].sig, &sa, NULL);
1499#else
1500# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1501 struct sigvec sv;
1502
Bram Moolenaar0f873732019-12-05 20:28:46 +01001503 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 sv.sv_handler = func_deadly;
1505 sv.sv_mask = 0;
1506 sv.sv_flags = SV_ONSTACK;
1507 sigvec(signal_info[i].sig, &sv, NULL);
1508# else
1509 signal(signal_info[i].sig, func_deadly);
1510# endif
1511#endif
1512 }
1513 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001514 {
1515 // Deal with non-deadly signals.
1516#ifdef SIGTSTP
1517 signal(signal_info[i].sig,
1518 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1519 ? SIG_IGN : func_other);
1520#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001522#endif
1523 }
1524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525}
1526
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001527#ifdef HAVE_SIGPROCMASK
1528 static void
1529block_signals(sigset_t *set)
1530{
1531 sigset_t newset;
1532 int i;
1533
1534 sigemptyset(&newset);
1535
1536 for (i = 0; signal_info[i].sig != -1; i++)
1537 sigaddset(&newset, signal_info[i].sig);
1538
Bram Moolenaar2e310482018-08-21 13:09:10 +02001539# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001540 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001541 sigaddset(&newset, SIGCONT);
1542# endif
1543
1544 sigprocmask(SIG_BLOCK, &newset, set);
1545}
1546
1547 static void
1548unblock_signals(sigset_t *set)
1549{
1550 sigprocmask(SIG_SETMASK, set, NULL);
1551}
1552#endif
1553
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001555 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001556 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1557 * return TRUE
1558 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1559 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001560 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001561 * Returns TRUE when Vim should exit.
1562 */
1563 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001564vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001565{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001566 static int got_signal = 0;
1567 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001568
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001569 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001570 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001571 case SIGNAL_BLOCK: blocked = TRUE;
1572 break;
1573
1574 case SIGNAL_UNBLOCK: blocked = FALSE;
1575 if (got_signal != 0)
1576 {
1577 kill(getpid(), got_signal);
1578 got_signal = 0;
1579 }
1580 break;
1581
1582 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001583 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001584 got_signal = sig;
1585#ifdef SIGPWR
1586 if (sig != SIGPWR)
1587#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001588 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001589 break;
1590 }
1591 return FALSE;
1592}
1593
1594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 * Check_win checks whether we have an interactive stdout.
1596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001598mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 if (isatty(1))
1601 return OK;
1602 return FAIL;
1603}
1604
1605/*
1606 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1607 */
1608 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001609mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610{
1611 if (isatty(read_cmd_fd))
1612 return TRUE;
1613 return FALSE;
1614}
1615
1616#ifdef FEAT_X11
1617
Bram Moolenaar651fca82021-11-29 20:39:38 +00001618# if defined(ELAPSED_TIMEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620/*
1621 * Give a message about the elapsed time for opening the X window.
1622 */
1623 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001624xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001626 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627}
1628# endif
1629#endif
1630
Bram Moolenaar651fca82021-11-29 20:39:38 +00001631#if defined(FEAT_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632/*
1633 * A few functions shared by X11 title and clipboard code.
1634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
1636static int got_x_error = FALSE;
1637
1638/*
1639 * X Error handler, otherwise X just exits! (very rude) -- webb
1640 */
1641 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001642x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001644 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 STRCAT(IObuff, _("\nVim: Got X error\n"));
1646
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001647 // In the GUI we cannot print a message and continue, because no X calls
1648 // are allowed here (causes my system to hang). Silently continuing seems
1649 // like the best alternative. Do preserve files, in case we crash.
1650 ml_sync_all(FALSE, FALSE);
1651
1652#ifdef FEAT_GUI
1653 if (!gui.in_use)
1654#endif
1655 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaar0f873732019-12-05 20:28:46 +01001657 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658}
1659
1660/*
1661 * Another X Error handler, just used to check for errors.
1662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001664x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
1666 got_x_error = TRUE;
1667 return 0;
1668}
1669
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001670/*
1671 * Return TRUE when connection to the X server is desired.
1672 */
1673 static int
1674x_connect_to_server(void)
1675{
1676 // No point in connecting if we are exiting or dying.
1677 if (exiting || v_dying)
1678 return FALSE;
1679
1680#if defined(FEAT_CLIENTSERVER)
1681 if (x_force_connect)
1682 return TRUE;
1683#endif
1684 if (x_no_connect)
1685 return FALSE;
1686
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001687 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001688 if (clip_exclude_prog != NULL)
1689 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001690 // Just in case we get called recursively, return FALSE. This could
1691 // happen if vpeekc() is used while executing the prog and it causes a
1692 // related callback to be invoked.
1693 if (regprog_in_use(clip_exclude_prog))
1694 return FALSE;
1695
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001696 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1697 return FALSE;
1698 }
1699 return TRUE;
1700}
1701
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001703# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704/*
1705 * An X IO Error handler, used to catch error while opening the display.
1706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001708x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001710 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001712# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001713 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715}
1716# endif
1717
1718/*
1719 * An X IO Error handler, used to catch terminal errors.
1720 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001721static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001724x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001727 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 x11_window = 0;
1729 x11_display = NULL;
1730 xterm_Shell = (Widget)0;
1731
Bram Moolenaar0f873732019-12-05 20:28:46 +01001732 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001734# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001735 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001736# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001738
1739/*
1740 * If the X11 connection was lost try to restore it.
1741 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001742 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001743 */
1744 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001745may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001746{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001747 // No point in restoring the connecting if we are exiting or dying.
1748 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001749 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001750 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001751
1752# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001753 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001754 if (app_context != (XtAppContext)NULL)
1755 {
1756 XtDestroyApplicationContext(app_context);
1757 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001758 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001759 }
1760# endif
1761
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001762 setup_term_clip();
1763 get_x11_title(FALSE);
1764 }
1765}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001766
1767 void
1768ex_xrestore(exarg_T *eap)
1769{
1770 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1771 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001772 if (xterm_display_allocated)
1773 vim_free(xterm_display);
1774 xterm_display = (char *)vim_strsave(eap->arg);
1775 xterm_display_allocated = TRUE;
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001776 }
1777 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001778 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001779
1780 clear_xterm_clip();
1781 x11_window = 0;
1782 xterm_dpy_retry_count = 5; // Try reconnecting five times
1783 may_restore_clipboard();
1784}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785#endif
1786
1787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 * Test if "dpy" and x11_window are valid by getting the window title.
1789 * I don't actually want it yet, so there may be a simpler call to use, but
1790 * this will cause the error handler x_error_check() to be called if anything
1791 * is wrong, such as the window pointer being invalid (as can happen when the
1792 * user changes his DISPLAY, but not his WINDOWID) -- webb
1793 */
1794 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001795test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796{
1797 int (*old_handler)();
1798 XTextProperty text_prop;
1799
1800 old_handler = XSetErrorHandler(x_error_check);
1801 got_x_error = FALSE;
1802 if (XGetWMName(dpy, x11_window, &text_prop))
1803 XFree((void *)text_prop.value);
1804 XSync(dpy, False);
1805 (void)XSetErrorHandler(old_handler);
1806
1807 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001808 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
1810 return (got_x_error ? FAIL : OK);
1811}
1812#endif
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
1815#ifdef FEAT_X11
1816
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001817static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
1819/*
1820 * try to get x11 window and display
1821 *
1822 * return FAIL for failure, OK otherwise
1823 */
1824 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001825get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826{
1827 char *winid;
1828 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001829#define XD_NONE 0 // x11_display not set here
1830#define XD_HERE 1 // x11_display opened here
1831#define XD_GUI 2 // x11_display used from gui.dpy
1832#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 static int x11_display_from = XD_NONE;
1834 static int did_set_error_handler = FALSE;
1835
1836 if (!did_set_error_handler)
1837 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001838 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 (void)XSetErrorHandler(x_error_handler);
1840 did_set_error_handler = TRUE;
1841 }
1842
Bram Moolenaar9372a112005-12-06 19:59:18 +00001843#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 if (gui.in_use)
1845 {
1846 /*
1847 * If the X11 display was opened here before, for the window where Vim
1848 * was started, close that one now to avoid a memory leak.
1849 */
1850 if (x11_display_from == XD_HERE && x11_display != NULL)
1851 {
1852 XCloseDisplay(x11_display);
1853 x11_display_from = XD_NONE;
1854 }
1855 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1856 {
1857 x11_display_from = XD_GUI;
1858 return OK;
1859 }
1860 x11_display = NULL;
1861 return FAIL;
1862 }
1863 else if (x11_display_from == XD_GUI)
1864 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001865 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 x11_window = 0;
1867 x11_display = NULL;
1868 x11_display_from = XD_NONE;
1869 }
1870#endif
1871
Bram Moolenaar0f873732019-12-05 20:28:46 +01001872 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (!x_connect_to_server())
1874 return FAIL;
1875
1876 /*
1877 * If WINDOWID not set, should try another method to find out
1878 * what the current window number is. The only code I know for
1879 * this is very complicated.
1880 * We assume that zero is invalid for WINDOWID.
1881 */
1882 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1883 x11_window = (Window)atol(winid);
1884
1885#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001886 if (xterm_dpy == x11_display)
1887 // x11_display may have been set to xterm_dpy elsewhere
1888 x11_display_from = XD_XTERM;
1889
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (xterm_dpy != NULL && x11_window != 0)
1891 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001892 // We may have checked it already, but Gnome terminal can move us to
1893 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001894 if (x11_display_from != XD_XTERM)
1895 {
1896 /*
1897 * If the X11 display was opened here before, for the window where
1898 * Vim was started, close that one now to avoid a memory leak.
1899 */
1900 if (x11_display_from == XD_HERE && x11_display != NULL)
1901 XCloseDisplay(x11_display);
1902 x11_display = xterm_dpy;
1903 x11_display_from = XD_XTERM;
1904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (test_x11_window(x11_display) == FAIL)
1906 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001907 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 x11_window = 0;
1909 x11_display = NULL;
1910 x11_display_from = XD_NONE;
1911 return FAIL;
1912 }
1913 return OK;
1914 }
1915#endif
1916
1917 if (x11_window == 0 || x11_display == NULL)
1918 result = -1;
1919
Bram Moolenaar0f873732019-12-05 20:28:46 +01001920 if (result != -1) // Have already been here and set this
1921 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922
1923 if (x11_window != 0 && x11_display == NULL)
1924 {
1925#ifdef SET_SIG_ALARM
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001926 void (*sig_save)();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001928#ifdef ELAPSED_FUNC
1929 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
1931 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001932 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933#endif
1934
1935#ifdef SET_SIG_ALARM
1936 /*
1937 * Opening the Display may hang if the DISPLAY setting is wrong, or
1938 * the network connection is bad. Set an alarm timer to get out.
1939 */
1940 sig_alarm_called = FALSE;
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001941 sig_save = (void (*)())signal(SIGALRM, (void (*)())sig_alarm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 alarm(2);
1943#endif
1944 x11_display = XOpenDisplay(NULL);
1945
1946#ifdef SET_SIG_ALARM
1947 alarm(0);
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001948 signal(SIGALRM, (void (*)())sig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001950 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951#endif
1952 if (x11_display != NULL)
1953 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001954# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001956 {
1957 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001958 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001959 verbose_leave();
1960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961# endif
1962 if (test_x11_window(x11_display) == FAIL)
1963 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001964 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 x11_window = 0;
1966 XCloseDisplay(x11_display);
1967 x11_display = NULL;
1968 }
1969 else
1970 x11_display_from = XD_HERE;
1971 }
1972 }
1973 if (x11_window == 0 || x11_display == NULL)
1974 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001975
1976# ifdef FEAT_EVAL
1977 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1978# endif
1979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 return (result = OK);
1981}
1982
1983/*
1984 * Determine original x11 Window Title
1985 */
1986 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001987get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001989 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990}
1991
1992/*
1993 * Determine original x11 Window icon
1994 */
1995 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001996get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997{
1998 int retval = FALSE;
1999
2000 retval = get_x11_thing(FALSE, test_only);
2001
Bram Moolenaar0f873732019-12-05 20:28:46 +01002002 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (oldicon == NULL && !test_only)
2004 {
2005 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002006 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002008 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
2010
2011 return retval;
2012}
2013
2014 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002015get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002016 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01002017 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018{
2019 XTextProperty text_prop;
2020 int retval = FALSE;
2021 Status status;
2022
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002023 if (get_x11_windis() != OK)
2024 return FALSE;
2025
2026 // Get window/icon name if any
2027 if (get_title)
2028 status = XGetWMName(x11_display, x11_window, &text_prop);
2029 else
2030 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2031
2032 /*
2033 * If terminal is xterm, then x11_window may be a child window of the
2034 * outer xterm window that actually contains the window/icon name, so
2035 * keep traversing up the tree until a window with a title/icon is
2036 * found.
2037 */
2038 // Previously this was only done for xterm and alike. I don't see a
2039 // reason why it would fail for other terminal emulators.
2040 // if (term_is_xterm)
2041 Window root;
2042 Window parent;
2043 Window win = x11_window;
2044 Window *children;
2045 unsigned int num_children;
2046
2047 while (!status || text_prop.value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002049 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2050 &num_children))
2051 break;
2052 if (children)
2053 XFree((void *)children);
2054 if (parent == root || parent == 0)
2055 break;
2056
2057 win = parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002059 status = XGetWMName(x11_display, win, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002061 status = XGetWMIconName(x11_display, win, &text_prop);
2062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002064 if (status && text_prop.value != NULL)
2065 {
2066 retval = TRUE;
2067 if (!test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002069 if (get_title)
2070 vim_free(oldtitle);
2071 else
2072 vim_free(oldicon);
2073 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (get_title)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002076 oldtitle = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002078 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002080 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002082 char **cl;
2083 Status transform_status;
2084 int n = 0;
2085
2086 transform_status = XmbTextPropertyToTextList(x11_display,
2087 &text_prop,
2088 &cl, &n);
2089 if (transform_status >= Success && n > 0 && cl[0])
2090 {
2091 if (get_title)
2092 oldtitle = vim_strsave((char_u *) cl[0]);
2093 else
2094 oldicon = vim_strsave((char_u *) cl[0]);
2095 XFreeStringList(cl);
2096 }
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002097 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 if (get_title)
2100 oldtitle = vim_strsave((char_u *)text_prop.value);
2101 else
2102 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002106 XFree((void *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
2108 return retval;
2109}
2110
Bram Moolenaar0f873732019-12-05 20:28:46 +01002111// Xutf8 functions are not available on older systems. Note that on some
2112// systems X_HAVE_UTF8_STRING may be defined in a header file but
2113// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2114// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002115#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002116# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117# define USE_UTF8_STRING
2118# endif
2119#endif
2120
2121/*
2122 * Set x11 Window Title
2123 *
2124 * get_x11_windis() must be called before this and have returned OK
2125 */
2126 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002127set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002129 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2130 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2131 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132#ifdef USE_UTF8_STRING
2133 if (enc_utf8)
2134 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2135 NULL, NULL, 0, NULL, NULL, NULL);
2136 else
2137#endif
2138 {
2139#if XtSpecificationRelease >= 4
2140# ifdef FEAT_XFONTSET
2141 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2142 NULL, NULL, 0, NULL, NULL, NULL);
2143# else
2144 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002145 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
Bram Moolenaar0f873732019-12-05 20:28:46 +01002147 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002148 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 XSetWMProperties(x11_display, x11_window, &text_prop,
2150 NULL, NULL, 0, NULL, NULL, NULL);
2151# endif
2152#else
2153 XStoreName(x11_display, x11_window, (char *)title);
2154#endif
2155 }
2156 XFlush(x11_display);
2157}
2158
2159/*
2160 * Set x11 Window icon
2161 *
2162 * get_x11_windis() must be called before this and have returned OK
2163 */
2164 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002165set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002167 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168#ifdef USE_UTF8_STRING
2169 if (enc_utf8)
2170 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2171 NULL, 0, NULL, NULL, NULL);
2172 else
2173#endif
2174 {
2175#if XtSpecificationRelease >= 4
2176# ifdef FEAT_XFONTSET
2177 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2178 NULL, 0, NULL, NULL, NULL);
2179# else
2180 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002181 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002183 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2185 NULL, 0, NULL, NULL, NULL);
2186# endif
2187#else
2188 XSetIconName(x11_display, x11_window, (char *)icon);
2189#endif
2190 }
2191 XFlush(x11_display);
2192}
2193
Bram Moolenaar0f873732019-12-05 20:28:46 +01002194#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002197get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198{
2199 return FALSE;
2200}
2201
2202 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002203get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204{
2205 if (!test_only)
2206 {
2207 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002208 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002210 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
2212 return FALSE;
2213}
2214
Bram Moolenaar0f873732019-12-05 20:28:46 +01002215#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
2217 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002218mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219{
2220 return get_x11_title(TRUE);
2221}
2222
2223 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002224mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
2226 return get_x11_icon(TRUE);
2227}
2228
2229/*
2230 * Set the window title and icon.
2231 */
2232 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002233mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
2235 int type = 0;
2236 static int recursive = 0;
2237
Bram Moolenaar0f873732019-12-05 20:28:46 +01002238 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002240 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 return;
2242
Bram Moolenaar0f873732019-12-05 20:28:46 +01002243 // When one of the X11 functions causes a deadly signal, we get here again
2244 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 if (recursive)
2246 return;
2247 ++recursive;
2248
2249 /*
2250 * if the window ID and the display is known, we may use X11 calls
2251 */
2252#ifdef FEAT_X11
2253 if (get_x11_windis() == OK)
2254 type = 1;
2255#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002256# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002257 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (gui.in_use)
2259 type = 1;
2260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261#endif
2262
2263 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002264 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 * than x11 calls, because the x11 calls don't always work
2266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if ((type || *T_TS != NUL) && title != NULL)
2268 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002269 if (oldtitle_outdated)
2270 {
2271 oldtitle_outdated = FALSE;
2272 VIM_CLEAR(oldtitle);
2273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 if (oldtitle == NULL
2275#ifdef FEAT_GUI
2276 && !gui.in_use
2277#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002278 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 (void)get_x11_title(FALSE);
2280
Bram Moolenaar0f873732019-12-05 20:28:46 +01002281 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 term_settitle(title);
2283#ifdef FEAT_X11
2284 else
2285# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002286 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002288 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002290#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002291 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 else
2293 gui_mch_settitle(title, icon);
2294#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002295 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297
2298 if ((type || *T_CIS != NUL) && icon != NULL)
2299 {
2300 if (oldicon == NULL
2301#ifdef FEAT_GUI
2302 && !gui.in_use
2303#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002304 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 get_x11_icon(FALSE);
2306
2307 if (*T_CIS != NUL)
2308 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002309 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002311 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 out_flush();
2313 }
2314#ifdef FEAT_X11
2315 else
2316# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002317 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002319 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320#endif
2321 did_set_icon = TRUE;
2322 }
2323 --recursive;
2324}
2325
2326/*
2327 * Restore the window/icon title.
2328 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002329 * SAVE_RESTORE_TITLE only restore title
2330 * SAVE_RESTORE_ICON only restore icon
2331 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 */
2333 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002334mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
Bram Moolenaardac13472019-09-16 21:06:21 +02002336 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002337
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002338 // Only restore the title or icon when it has been set.
2339 // When using "oldtitle" make a copy, it might be freed halfway.
2340 char_u *title = ((which & SAVE_RESTORE_TITLE) && unix_did_set_title)
2341 ? (oldtitle ? oldtitle : p_titleold) : NULL;
2342 char_u *tofree = NULL;
2343 if (title == oldtitle && oldtitle != NULL)
2344 {
2345 tofree = vim_strsave(title);
2346 if (tofree != NULL)
2347 title = tofree;
2348 }
2349 mch_settitle(title,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002350 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002351 vim_free(tofree);
Bram Moolenaar40385db2018-08-07 22:31:44 +02002352
Bram Moolenaare5c83282019-05-03 23:15:37 +02002353 if (do_push_pop)
2354 {
2355 // pop and push from/to the stack
2356 term_pop_title(which);
2357 term_push_title(which);
2358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359}
2360
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362/*
2363 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002364 * This matches "xterm.*", thus "xterm-256color", "xterm-kitty", etc.
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002365 * Do not consider "xterm-kitty" an xterm, it is not fully xterm compatible,
2366 * using the "xterm-kitty" terminfo entry should work better.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002367 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 */
2369 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002370vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
2372 if (name == NULL)
2373 return FALSE;
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002374 return ((STRNICMP(name, "xterm", 5) == 0
2375 && STRNICMP(name, "xterm-kitty", 11) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 || STRNICMP(name, "nxterm", 6) == 0
2377 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002378 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002380 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 || STRCMP(name, "builtin_xterm") == 0);
2382}
2383
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002384#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2385/*
2386 * Return TRUE if "name" appears to be that of a terminal
2387 * known to support the xterm-style mouse protocol.
2388 * Relies on term_is_xterm having been set to its correct value.
2389 */
2390 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002391use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002392{
2393 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002394 && (term_is_xterm
2395 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002396 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002397 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002398 || STRICMP(name, "st") == 0
2399 || STRNICMP(name, "st-", 3) == 0
2400 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002401}
2402#endif
2403
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404/*
2405 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2406 * Return 1 for "xterm".
2407 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002408 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002409 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 */
2411 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002412use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002414 if (ttym_flags == TTYM_SGR)
2415 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002416 if (ttym_flags == TTYM_URXVT)
2417 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 if (ttym_flags == TTYM_XTERM2)
2419 return 2;
2420 if (ttym_flags == TTYM_XTERM)
2421 return 1;
2422 return 0;
2423}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002425/*
2426 * Return TRUE if "name" is an iris-ansi terminal name.
2427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002429vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
2431 if (name == NULL)
2432 return FALSE;
2433 return (STRNICMP(name, "iris-ansi", 9) == 0
2434 || STRCMP(name, "builtin_iris-ansi") == 0);
2435}
2436
Dominique Pellee764d1b2023-03-12 21:20:59 +00002437#if defined(VMS) || defined(PROTO)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002438/*
2439 * Return TRUE if "name" is a vt300-like terminal name.
2440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002442vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443{
2444 if (name == NULL)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002445 return FALSE;
2446 // Actually all ANSI compatible terminals should be here.
2447 // Catch at least VT1xx - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002448 return ((STRNICMP(name, "vt", 2) == 0
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002449 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 || STRCMP(name, "builtin_vt320") == 0);
2451}
Dominique Pellee764d1b2023-03-12 21:20:59 +00002452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
2454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 * Insert user name in s[len].
2456 * Return OK if a name found.
2457 */
2458 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002459mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460{
2461#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002462 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 return OK;
2464#else
2465 return mch_get_uname(getuid(), s, len);
2466#endif
2467}
2468
2469/*
2470 * Insert user name for "uid" in s[len].
2471 * Return OK if a name found.
2472 */
2473 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002474mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475{
2476#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2477 struct passwd *pw;
2478
2479 if ((pw = getpwuid(uid)) != NULL
2480 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2481 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002482 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 return OK;
2484 }
2485#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002486 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2487 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488}
2489
2490/*
2491 * Insert host name is s[len].
2492 */
2493
2494#ifdef HAVE_SYS_UTSNAME_H
2495 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002496mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 struct utsname vutsname;
2499
2500 if (uname(&vutsname) < 0)
2501 *s = NUL;
2502 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002503 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002505#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506
2507# ifdef HAVE_SYS_SYSTEMINFO_H
2508# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2509# endif
2510
2511 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002512mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513{
2514# ifdef VAXC
2515 vaxc$gethostname((char *)s, len);
2516# else
2517 gethostname((char *)s, len);
2518# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002519 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002521#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
2523/*
2524 * return process ID
2525 */
2526 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002527mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528{
2529 return (long)getpid();
2530}
2531
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002532/*
2533 * return TRUE if process "pid" is still running
2534 */
2535 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002536mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002537{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002538 // If there is no error the process must be running.
2539 if (kill(pid, 0) == 0)
2540 return TRUE;
2541#ifdef ESRCH
2542 // If the error is ESRCH then the process is not running.
2543 if (errno == ESRCH)
2544 return FALSE;
2545#endif
2546 // If the process is running and owned by another user we get EPERM. With
2547 // other errors the process might be running, assuming it is then.
2548 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002549}
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002553strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
2555 extern int sys_nerr;
2556 extern char *sys_errlist[];
2557 static char er[20];
2558
2559 if (err > 0 && err < sys_nerr)
2560 return (sys_errlist[err]);
2561 sprintf(er, "Error %d", err);
2562 return er;
2563}
2564#endif
2565
2566/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002567 * Get name of current directory into buffer "buf" of length "len" bytes.
2568 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 * Return OK for success, FAIL for failure.
2570 */
2571 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002572mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573{
2574#if defined(USE_GETCWD)
2575 if (getcwd((char *)buf, len) == NULL)
2576 {
2577 STRCPY(buf, strerror(errno));
2578 return FAIL;
2579 }
2580 return OK;
2581#else
2582 return (getwd((char *)buf) != NULL ? OK : FAIL);
2583#endif
2584}
2585
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002587 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 *
2589 * return FAIL for failure, OK for success
2590 */
2591 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002592mch_FullName(
2593 char_u *fname,
2594 char_u *buf,
2595 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002596 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597{
2598 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002599#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002601 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 char_u olddir[MAXPATHL];
2604 char_u *p;
2605 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002606#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002607 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2608 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002609#endif
2610
Bram Moolenaar38323e42007-03-06 19:22:53 +00002611#ifdef VMS
2612 fname = vms_fixfilename(fname);
2613#endif
2614
Bram Moolenaara2442432007-04-26 14:26:37 +00002615#ifdef __CYGWIN__
2616 /*
2617 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2618 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002619# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002620 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2621 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002622 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2623 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002624# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002625 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002626# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002627 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002628#endif
2629
Bram Moolenaar0f873732019-12-05 20:28:46 +01002630 // Expand it if forced or not an absolute path.
2631 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002632 if ((force || !mch_isFullName(fname))
2633 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
2635 /*
2636 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002637 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 * This will get the correct path name with "../" things.
2639 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002640 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002642 if (STRCMP(p, "/..") == 0)
2643 // for "/path/dir/.." include the "/.."
2644 p += 3;
2645
Bram Moolenaar38323e42007-03-06 19:22:53 +00002646#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 /*
2648 * Use fchdir() if possible, it's said to be faster and more
2649 * reliable. But on SunOS 4 it might not work. Check this by
2650 * doing a fchdir() right now.
2651 */
2652 if (!dont_fchdir)
2653 {
2654 fd = open(".", O_RDONLY | O_EXTRA, 0);
2655 if (fd >= 0 && fchdir(fd) < 0)
2656 {
2657 close(fd);
2658 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002659 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 }
2661 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
Bram Moolenaar0f873732019-12-05 20:28:46 +01002664 // Only change directory when we are sure we can return to where
2665 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002667#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 (mch_dirname(olddir, MAXPATHL) == FAIL
2671 || mch_chdir((char *)olddir) != 0))
2672 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002673 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 retval = FAIL;
2675 }
2676 else
2677 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002678 // The directory is copied into buf[], to be able to remove
2679 // the file name without changing it (could be a string in
2680 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (p - fname >= len)
2682 retval = FAIL;
2683 else
2684 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002685 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002687 {
2688 // Path does not exist (yet). For a full path fail,
2689 // will use the path as-is. For a relative path use
2690 // the current directory and append the file name.
2691 if (mch_isFullName(fname))
2692 retval = FAIL;
2693 else
2694 p = NULL;
2695 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002696 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002698 else
2699 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 *buf = NUL;
2701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 }
2703 }
2704 if (mch_dirname(buf, len) == FAIL)
2705 {
2706 retval = FAIL;
2707 *buf = NUL;
2708 }
2709 if (p != NULL)
2710 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002711#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 if (fd >= 0)
2713 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002714 if (p_verbose >= 5)
2715 {
2716 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002717 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002718 verbose_leave();
2719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 l = mch_chdir((char *)olddir);
2725 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002726 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
itchyny051a40c2021-10-20 10:00:05 +01002728#ifdef HAVE_FCHDIR
2729 if (fd >= 0)
2730 close(fd);
2731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
2733 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002734 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002735 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002736#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002737 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002739 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002742
Bram Moolenaar0f873732019-12-05 20:28:46 +01002743 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002744 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 return FAIL;
2746
Bram Moolenaar0f873732019-12-05 20:28:46 +01002747 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 if (STRCMP(fname, ".") != 0)
2749 STRCAT(buf, fname);
2750
2751 return OK;
2752}
2753
2754/*
2755 * Return TRUE if "fname" does not depend on the current directory.
2756 */
2757 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002758mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002760#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 return ( fname[0] == '/' || fname[0] == '.' ||
2762 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2763 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2764 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002765#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767#endif
2768}
2769
Bram Moolenaar24552be2005-12-10 20:17:30 +00002770#if defined(USE_FNAME_CASE) || defined(PROTO)
2771/*
2772 * Set the case of the file name, if it already exists. This will cause the
2773 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002774 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002775 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002776 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002777fname_case(
2778 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002779 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002780{
2781 struct stat st;
2782 char_u *slash, *tail;
2783 DIR *dirp;
2784 struct dirent *dp;
2785
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002786 if (mch_lstat((char *)name, &st) < 0)
2787 return;
2788
2789 // Open the directory where the file is located.
2790 slash = vim_strrchr(name, '/');
2791 if (slash == NULL)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002792 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002793 dirp = opendir(".");
2794 tail = name;
2795 }
2796 else
2797 {
2798 *slash = NUL;
2799 dirp = opendir((char *)name);
2800 *slash = '/';
2801 tail = slash + 1;
2802 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002803
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002804 if (dirp == NULL)
2805 return;
2806
2807 while ((dp = readdir(dirp)) != NULL)
2808 {
2809 // Only accept names that differ in case and are the same byte
2810 // length. TODO: accept different length name.
2811 if (STRICMP(tail, dp->d_name) == 0
2812 && STRLEN(tail) == STRLEN(dp->d_name))
Bram Moolenaar24552be2005-12-10 20:17:30 +00002813 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002814 char_u newname[MAXPATHL + 1];
2815 struct stat st2;
2816
2817 // Verify the inode is equal.
2818 vim_strncpy(newname, name, MAXPATHL);
2819 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2820 MAXPATHL - (tail - name));
2821 if (mch_lstat((char *)newname, &st2) >= 0
2822 && st.st_ino == st2.st_ino
2823 && st.st_dev == st2.st_dev)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002824 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002825 STRCPY(tail, dp->d_name);
2826 break;
Bram Moolenaar24552be2005-12-10 20:17:30 +00002827 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002828 }
2829 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002830
2831 closedir(dirp);
Bram Moolenaar24552be2005-12-10 20:17:30 +00002832}
2833#endif
2834
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835/*
2836 * Get file permissions for 'name'.
2837 * Returns -1 when it doesn't exist.
2838 */
2839 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002840mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841{
2842 struct stat statb;
2843
Bram Moolenaar0f873732019-12-05 20:28:46 +01002844 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845#ifdef VMS
2846 if (stat((char *)vms_fixfilename(name), &statb))
2847#else
2848 if (stat((char *)name, &statb))
2849#endif
2850 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002851#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002852 // The top bit makes the value negative, which means the file doesn't
2853 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002854 return statb.st_mode & ~S_ADDACE;
2855#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858}
2859
2860/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002861 * Set file permission for "name" to "perm".
2862 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 */
2864 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002865mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
2867 return (chmod((char *)
2868#ifdef VMS
2869 vms_fixfilename(name),
2870#else
2871 name,
2872#endif
2873 (mode_t)perm) == 0 ? OK : FAIL);
2874}
2875
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002876#if defined(HAVE_FCHMOD) || defined(PROTO)
2877/*
2878 * Set file permission for open file "fd" to "perm".
2879 * Return FAIL for failure, OK otherwise.
2880 */
2881 int
2882mch_fsetperm(int fd, long perm)
2883{
2884 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2885}
2886#endif
2887
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888#if defined(HAVE_ACL) || defined(PROTO)
2889# ifdef HAVE_SYS_ACL_H
2890# include <sys/acl.h>
2891# endif
2892# ifdef HAVE_SYS_ACCESS_H
2893# include <sys/access.h>
2894# endif
2895
2896# ifdef HAVE_SOLARIS_ACL
2897typedef struct vim_acl_solaris_T {
2898 int acl_cnt;
2899 aclent_t *acl_entry;
2900} vim_acl_solaris_T;
2901# endif
2902
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002903#if defined(HAVE_SELINUX) || defined(PROTO)
2904/*
2905 * Copy security info from "from_file" to "to_file".
2906 */
2907 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002908mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002909{
2910 if (from_file == NULL)
2911 return;
2912
2913 if (selinux_enabled == -1)
2914 selinux_enabled = is_selinux_enabled();
2915
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002916 if (selinux_enabled <= 0)
2917 return;
2918
2919 // Use "char *" instead of "security_context_t" to avoid a deprecation
2920 // warning.
2921 char *from_context = NULL;
2922 char *to_context = NULL;
2923
2924 if (getfilecon((char *)from_file, &from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002925 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002926 // If the filesystem doesn't support extended attributes,
2927 // the original had no special security context and the
2928 // target cannot have one either.
2929 if (errno == EOPNOTSUPP)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002930 return;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002931
2932 msg_puts(_("\nCould not get security context for "));
2933 msg_outtrans(from_file);
2934 msg_putchar('\n');
2935 return;
2936 }
2937 if (getfilecon((char *)to_file, &to_context) < 0)
2938 {
2939 msg_puts(_("\nCould not get security context for "));
2940 msg_outtrans(to_file);
2941 msg_putchar('\n');
2942 freecon (from_context);
2943 return ;
2944 }
2945 if (strcmp(from_context, to_context) != 0)
2946 {
2947 if (setfilecon((char *)to_file, from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002948 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002949 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002950 msg_outtrans(to_file);
2951 msg_putchar('\n');
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002952 }
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002953 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002954 freecon(to_context);
2955 freecon(from_context);
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002956}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002957#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002958
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002959#if defined(HAVE_SMACK) && !defined(PROTO)
2960/*
2961 * Copy security info from "from_file" to "to_file".
2962 */
2963 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002964mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002965{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002966 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002967 {
2968 XATTR_NAME_SMACK,
2969 XATTR_NAME_SMACKEXEC,
2970 XATTR_NAME_SMACKMMAP
2971 };
2972
2973 char buffer[SMACK_LABEL_LEN];
2974 const char *name;
2975 int index;
2976 int ret;
2977 ssize_t size;
2978
2979 if (from_file == NULL)
2980 return;
2981
2982 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2983 / sizeof(smack_copied_attributes)[0]) ; index++)
2984 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002985 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002986 name = smack_copied_attributes[index];
2987
Bram Moolenaar0f873732019-12-05 20:28:46 +01002988 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002989 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2990 if (size >= 0)
2991 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002992 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002993 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2994 if (ret < 0)
2995 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002996 vim_snprintf((char *)IObuff, IOSIZE,
2997 _("Could not set security context %s for %s"),
2998 name, to_file);
2999 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003000 msg_putchar('\n');
3001 }
3002 }
3003 else
3004 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003005 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003006 switch (errno)
3007 {
3008 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003009 // extended attributes aren't supported or enabled
3010 // should a message be echoed? not sure...
3011 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003012
3013 case ERANGE:
3014 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003015 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003016 vim_snprintf((char *)IObuff, IOSIZE,
3017 _("Could not get security context %s for %s. Removing it!"),
3018 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003019 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003020 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003021 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003022
3023 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003024 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003025 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003026 // Silently ignore errors, apparently this happens when
3027 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003028 break;
3029 }
3030 }
3031 }
3032}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003033#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003034
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035/*
3036 * Return a pointer to the ACL of file "fname" in allocated memory.
3037 * Return NULL if the ACL is not available for whatever reason.
3038 */
3039 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003040mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
3042 vim_acl_T ret = NULL;
3043#ifdef HAVE_POSIX_ACL
3044 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3045#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003046#ifdef HAVE_SOLARIS_ZFS_ACL
3047 acl_t *aclent;
3048
3049 if (acl_get((char *)fname, 0, &aclent) < 0)
3050 return NULL;
3051 ret = (vim_acl_T)aclent;
3052#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053#ifdef HAVE_SOLARIS_ACL
3054 vim_acl_solaris_T *aclent;
3055
3056 aclent = malloc(sizeof(vim_acl_solaris_T));
3057 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3058 {
3059 free(aclent);
3060 return NULL;
3061 }
3062 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3063 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3064 {
3065 free(aclent->acl_entry);
3066 free(aclent);
3067 return NULL;
3068 }
3069 ret = (vim_acl_T)aclent;
3070#else
3071#if defined(HAVE_AIX_ACL)
3072 int aclsize;
3073 struct acl *aclent;
3074
3075 aclsize = sizeof(struct acl);
3076 aclent = malloc(aclsize);
3077 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3078 {
3079 if (errno == ENOSPC)
3080 {
3081 aclsize = aclent->acl_len;
3082 aclent = realloc(aclent, aclsize);
3083 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3084 {
3085 free(aclent);
3086 return NULL;
3087 }
3088 }
3089 else
3090 {
3091 free(aclent);
3092 return NULL;
3093 }
3094 }
3095 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003096#endif // HAVE_AIX_ACL
3097#endif // HAVE_SOLARIS_ACL
3098#endif // HAVE_SOLARIS_ZFS_ACL
3099#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 return ret;
3101}
3102
3103/*
3104 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3105 */
3106 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003107mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
3109 if (aclent == NULL)
3110 return;
3111#ifdef HAVE_POSIX_ACL
3112 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3113#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003114#ifdef HAVE_SOLARIS_ZFS_ACL
3115 acl_set((char *)fname, (acl_t *)aclent);
3116#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117#ifdef HAVE_SOLARIS_ACL
3118 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3119 ((vim_acl_solaris_T *)aclent)->acl_entry);
3120#else
3121#ifdef HAVE_AIX_ACL
3122 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003123#endif // HAVE_AIX_ACL
3124#endif // HAVE_SOLARIS_ACL
3125#endif // HAVE_SOLARIS_ZFS_ACL
3126#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127}
3128
3129 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003130mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
3132 if (aclent == NULL)
3133 return;
3134#ifdef HAVE_POSIX_ACL
3135 acl_free((acl_t)aclent);
3136#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003137#ifdef HAVE_SOLARIS_ZFS_ACL
3138 acl_free((acl_t *)aclent);
3139#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140#ifdef HAVE_SOLARIS_ACL
3141 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3142 free(aclent);
3143#else
3144#ifdef HAVE_AIX_ACL
3145 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003146#endif // HAVE_AIX_ACL
3147#endif // HAVE_SOLARIS_ACL
3148#endif // HAVE_SOLARIS_ZFS_ACL
3149#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150}
3151#endif
3152
3153/*
3154 * Set hidden flag for "name".
3155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003157mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003159 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160}
3161
3162/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003163 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 * return FALSE if "name" is not a directory
3165 * return FALSE for error
3166 */
3167 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003168mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
3170 struct stat statb;
3171
Bram Moolenaar0f873732019-12-05 20:28:46 +01003172 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 return FALSE;
3174 if (stat((char *)name, &statb))
3175 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177}
3178
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003179/*
3180 * return TRUE if "name" is a directory, NOT a symlink to a directory
3181 * return FALSE if "name" is not a directory
3182 * return FALSE for error
3183 */
3184 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003185mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003186{
3187 struct stat statb;
3188
Bram Moolenaar0f873732019-12-05 20:28:46 +01003189 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003190 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003191 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003192 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003193 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003194}
3195
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196/*
3197 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3198 */
3199 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003200executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 struct stat st;
3203
3204 if (stat((char *)name, &st))
3205 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003206#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003207 // Like on Unix system file can have executable rights but not necessarily
3208 // be an executable, but on Unix is not a default for an ordinary file to
3209 // have an executable flag - on VMS it is in most cases.
3210 // Therefore, this check does not have any sense - let keep us to the
3211 // conventions instead:
3212 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003213 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003214 int vms_executable = 0;
3215 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3216 {
3217 if (strstr(vms_tolower((char*)name),".exe") != NULL
3218 || strstr(vms_tolower((char*)name),".com")!= NULL)
3219 vms_executable = 1;
3220 }
3221 return vms_executable;
3222#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225}
3226
3227/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003228 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003229 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 * Return -1 if unknown.
3231 */
3232 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003233mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
3235 char_u *buf;
3236 char_u *p, *e;
3237 int retval;
3238
Bram Moolenaar0f873732019-12-05 20:28:46 +01003239 // When "use_path" is false and if it's an absolute or relative path don't
3240 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003241 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003242 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003243 // There must be a path separator, files in the current directory
3244 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003245 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003246 {
3247 if (path != NULL)
3248 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003249 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003250 *path = FullName_save(name, TRUE);
3251 else
3252 *path = vim_strsave(name);
3253 }
3254 return TRUE;
3255 }
3256 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
3259 p = (char_u *)getenv("PATH");
3260 if (p == NULL || *p == NUL)
3261 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003262 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 if (buf == NULL)
3264 return -1;
3265
3266 /*
3267 * Walk through all entries in $PATH to check if "name" exists there and
3268 * is an executable file.
3269 */
3270 for (;;)
3271 {
3272 e = (char_u *)strchr((char *)p, ':');
3273 if (e == NULL)
3274 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003275 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 STRCPY(buf, "./");
3277 else
3278 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003279 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 add_pathsep(buf);
3281 }
3282 STRCAT(buf, name);
3283 retval = executable_file(buf);
3284 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003285 {
3286 if (path != NULL)
3287 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003288 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003289 *path = FullName_save(buf, TRUE);
3290 else
3291 *path = vim_strsave(buf);
3292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
3296 if (*e != ':')
3297 break;
3298 p = e + 1;
3299 }
3300
3301 vim_free(buf);
3302 return retval;
3303}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
3305/*
3306 * Check what "name" is:
3307 * NODE_NORMAL: file or directory (or doesn't exist)
3308 * NODE_WRITABLE: writable device, socket, fifo, etc.
3309 * NODE_OTHER: non-writable things
3310 */
3311 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003312mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
3314 struct stat st;
3315
3316 if (stat((char *)name, &st))
3317 return NODE_NORMAL;
3318 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3319 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003320 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003322 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 return NODE_WRITABLE;
3324}
3325
3326 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003327mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329#ifdef HAVE_CHECK_STACK_GROWTH
3330 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 check_stack_growth((char *)&i);
3333
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003334# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 get_stack_limit();
3336# endif
3337
3338#endif
3339
3340 /*
3341 * Setup an alternative stack for signals. Helps to catch signals when
3342 * running out of stack space.
3343 * Use of sigaltstack() is preferred, it's more portable.
3344 * Ignore any errors.
3345 */
3346#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003347 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 init_signal_stack();
3349#endif
3350}
3351
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003352#if defined(EXITFREE) || defined(PROTO)
3353 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003354mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003355{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003356# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3357 if (clip_star.owned)
3358 clip_lose_selection(&clip_star);
3359 if (clip_plus.owned)
3360 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003361# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003362# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003363 if (xterm_Shell != (Widget)0)
3364 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003365# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003366 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003367 if (xterm_dpy != NULL)
3368 XtCloseDisplay(xterm_dpy);
3369 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003370 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003371 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003372# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003373 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003374# endif
3375 }
3376# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003377# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003378# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003379 if (x11_display != NULL
3380# ifdef FEAT_XCLIPBOARD
3381 && x11_display != xterm_dpy
3382# endif
3383 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003384 XCloseDisplay(x11_display);
3385# endif
3386# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003387 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003388# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003389 vim_free(oldtitle);
3390 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003391}
3392#endif
3393
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394/*
3395 * Output a newline when exiting.
3396 * Make sure the newline goes to the same stream as the text.
3397 */
3398 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003399exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003401 if (silent_mode)
3402 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 if (newline_on_exit || msg_didout)
3404 {
3405 if (msg_use_printf())
3406 {
3407 if (info_message)
3408 mch_msg("\n");
3409 else
3410 mch_errmsg("\r\n");
3411 }
3412 else
3413 out_char('\n');
3414 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003415 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003417 restore_cterm_colors(); // get original colors back
3418 msg_clr_eos_force(); // clear the rest of the display
3419 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
3421}
3422
Bram Moolenaarb4151682020-05-11 22:13:28 +02003423#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003424# if (defined(__GNUC__) \
3425 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3426 || (defined(__clang__) && (__clang_major__ >= 12))
3427extern void __gcov_dump(void);
3428extern void __gcov_reset(void);
3429# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3430# else
3431extern void __gcov_flush(void);
3432# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003433#endif
3434
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003436mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
3438 exiting = TRUE;
3439
3440#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3441 x11_export_final_selection();
3442#endif
3443
3444#ifdef FEAT_GUI
3445 if (!gui.in_use)
3446#endif
3447 {
3448 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003449 if (!is_not_a_term())
3450 {
3451 // restore xterm title and icon name
3452 mch_restore_title(SAVE_RESTORE_BOTH);
3453 term_pop_title(SAVE_RESTORE_BOTH);
3454 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003455
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 /*
3457 * When t_ti is not empty but it doesn't cause swapping terminal
3458 * pages, need to output a newline when msg_didout is set. But when
3459 * t_ti does swap pages it should not go to the shell page. Do this
3460 * before stoptermcap().
3461 */
3462 if (swapping_screen() && !newline_on_exit)
3463 exit_scroll();
3464
Bram Moolenaar0f873732019-12-05 20:28:46 +01003465 // Stop termcap: May need to check for T_CRV response, which
3466 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 stoptermcap();
3468
3469 /*
3470 * A newline is only required after a message in the alternate screen.
3471 * This is set to TRUE by wait_return().
3472 */
3473 if (!swapping_screen() || newline_on_exit)
3474 exit_scroll();
3475
Bram Moolenaar0f873732019-12-05 20:28:46 +01003476 // Cursor may have been switched off without calling starttermcap()
3477 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 if (full_screen)
3479 cursor_on();
3480 }
3481 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003482 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003483
3484#ifdef USE_GCOV_FLUSH
3485 // Flush coverage info before possibly being killed by a deadly signal.
3486 __gcov_flush();
3487#endif
3488
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 may_core_dump();
3490#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 gui_exit(r);
3493#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003494
Bram Moolenaar56718732006-03-15 22:53:57 +00003495#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003496 mac_conv_cleanup();
3497#endif
3498
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003500 // A core dump won't be created if the signal handler
3501 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 if (deadly_signal != 0)
3503 return;
3504#endif
3505
Bram Moolenaar009b2592004-10-24 19:18:58 +00003506#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003507 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003508#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003509
3510#ifdef EXITFREE
3511 free_all_mem();
3512#endif
3513
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 exit(r);
3515}
3516
3517 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003518may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520 if (deadly_signal != 0)
3521 {
3522 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003523 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
3525}
3526
3527#ifndef VMS
3528
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003529/*
3530 * Get the file descriptor to use for tty operations.
3531 */
3532 static int
3533get_tty_fd(int fd)
3534{
3535 int tty_fd = fd;
3536
3537#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3538 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3539 // "fd" when it is a master device.
3540 if (mch_isatty(fd) > 1)
3541 {
3542 char *name;
3543
3544 name = ptsname(fd);
3545 if (name == NULL)
3546 return -1;
3547
3548 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3549 if (tty_fd < 0)
3550 return -1;
3551 }
3552#endif
3553 return tty_fd;
3554}
3555
3556 static int
3557mch_tcgetattr(int fd, void *term)
3558{
3559 int tty_fd;
3560 int retval = -1;
3561
3562 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003563 if (tty_fd < 0)
3564 return -1;
3565
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003566#ifdef NEW_TTY_SYSTEM
3567# ifdef HAVE_TERMIOS_H
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003568 retval = tcgetattr(tty_fd, (struct termios *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003569# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003570 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003571# endif
3572#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003573 // for "old" tty systems
3574 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003575#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003576 if (tty_fd != fd)
3577 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003578 return retval;
3579}
3580
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003582mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583{
3584 static int first = TRUE;
3585
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003586#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587# ifdef HAVE_TERMIOS_H
3588 static struct termios told;
3589 struct termios tnew;
3590# else
3591 static struct termio told;
3592 struct termio tnew;
3593# endif
3594
3595 if (first)
3596 {
3597 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003598 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 }
3600
3601 tnew = told;
3602 if (tmode == TMODE_RAW)
3603 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003604 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003605 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3606 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003608# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003609 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610# endif
3611 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003612# ifdef ONLCR
3613 // Don't map NL -> CR NL, we do it ourselves.
3614 // Also disable expanding tabs if possible.
3615# ifdef XTABS
3616 tnew.c_oflag &= ~(ONLCR | XTABS);
3617# else
3618# ifdef TAB3
3619 tnew.c_oflag &= ~(ONLCR | TAB3);
3620# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003622# endif
3623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003625 tnew.c_cc[VMIN] = 1; // return after 1 char
3626 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 }
3628 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003629 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003630 // Also reset ICANON here, otherwise on Solaris select() won't see
3631 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003632 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003633 tnew.c_cc[VMIN] = 1; // return after 1 char
3634 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
3637# if defined(HAVE_TERMIOS_H)
3638 {
3639 int n = 10;
3640
Bram Moolenaar0f873732019-12-05 20:28:46 +01003641 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3642 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3644 && errno == EINTR && n > 0)
3645 --n;
3646 }
3647# else
3648 ioctl(read_cmd_fd, TCSETA, &tnew);
3649# endif
3650
3651#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 /*
3653 * for "old" tty systems
3654 */
3655# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003656# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657# endif
3658 static struct sgttyb ttybold;
3659 struct sgttyb ttybnew;
3660
3661 if (first)
3662 {
3663 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003664 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
3666
3667 ttybnew = ttybold;
3668 if (tmode == TMODE_RAW)
3669 {
3670 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3671 ttybnew.sg_flags |= RAW;
3672 }
3673 else if (tmode == TMODE_SLEEP)
3674 ttybnew.sg_flags &= ~(ECHO);
3675 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3676#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003677 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678}
3679
3680/*
3681 * Try to get the code for "t_kb" from the stty setting
3682 *
3683 * Even if termcap claims a backspace key, the user's setting *should*
3684 * prevail. stty knows more about reality than termcap does, and if
3685 * somebody's usual erase key is DEL (which, for most BSD users, it will
3686 * be), they're going to get really annoyed if their erase key starts
3687 * doing forward deletes for no reason. (Eric Fischer)
3688 */
3689 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003690get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003692 ttyinfo_T info;
3693 char_u buf[2];
3694 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003696 if (get_tty_info(read_cmd_fd, &info) != OK)
3697 return;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003698
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003699 intr_char = info.interrupt;
3700 buf[0] = info.backspace;
3701 buf[1] = NUL;
3702 add_termcode((char_u *)"kb", buf, FALSE);
3703
3704 // If <BS> and <DEL> are now the same, redefine <DEL>.
3705 p = find_termcode((char_u *)"kD");
3706 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3707 do_fixdel(NULL);
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003708}
3709
3710/*
3711 * Obtain the characters that Backspace and Enter produce on "fd".
3712 * Returns OK or FAIL.
3713 */
3714 int
3715get_tty_info(int fd, ttyinfo_T *info)
3716{
3717#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718# ifdef HAVE_TERMIOS_H
3719 struct termios keys;
3720# else
3721 struct termio keys;
3722# endif
3723
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003724 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003726 info->backspace = keys.c_cc[VERASE];
3727 info->interrupt = keys.c_cc[VINTR];
3728 if (keys.c_iflag & ICRNL)
3729 info->enter = NL;
3730 else
3731 info->enter = CAR;
3732 if (keys.c_oflag & ONLCR)
3733 info->nl_does_cr = TRUE;
3734 else
3735 info->nl_does_cr = FALSE;
3736 return OK;
3737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003739 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 struct sgttyb keys;
3741
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003742 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003744 info->backspace = keys.sg_erase;
3745 info->interrupt = keys.sg_kill;
3746 info->enter = CAR;
3747 info->nl_does_cr = TRUE;
3748 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003751 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752}
3753
Bram Moolenaar0f873732019-12-05 20:28:46 +01003754#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003756static int mouse_ison = FALSE;
3757
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758/*
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003759 * Set mouse clicks on or off and possible enable mouse movement events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 */
3761 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003762mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003764#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003765 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 int xterm_mouse_vers;
3768
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003769#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003770 if (!on)
3771 // Make sure not tracing mouse movements. Important when a button-down
3772 // was received but no release yet.
3773 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003774#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003775
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003776 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003777#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003778 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003779#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003780 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003781 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 return;
3783
3784 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003785
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003786#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003787 if (ttym_flags == TTYM_URXVT)
3788 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003789 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003790 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003791 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003792#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003793
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00003794 if (T_CXM != NULL && *T_CXM != NUL)
3795 {
3796 term_enable_mouse(on);
3797 }
3798 else if (ttym_flags == TTYM_SGR)
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003799 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003800 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003801 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003802 mouse_ison = on;
3803 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003804
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003805#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003806 if (bevalterm_ison != (p_bevalterm && on))
3807 {
3808 bevalterm_ison = (p_bevalterm && on);
3809 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003810 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003811 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003812 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003813#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003814
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 if (xterm_mouse_vers > 0)
3816 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003817 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 out_str_nf((char_u *)
3819 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003820 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003821#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003822 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003823#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003824 "\033[?1002h")
3825 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003826 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003828 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003829 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003832#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 else if (ttym_flags == TTYM_DEC)
3834 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003835 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003837 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003839 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003843#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 else
3845 {
3846 if (on)
3847 {
3848 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003849 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
3851 else
3852 {
3853 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003854 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
3856 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003859#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003860 else
3861 {
3862 if (on)
3863 {
3864 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003865 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003866 }
3867 else
3868 {
3869 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003870 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003871 }
3872 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003873#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003874
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003875#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 else
3877 {
3878 if (on)
3879 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003880 // D - Enable Mouse up/down messages
3881 // L - Enable Left Button Reporting
3882 // M - Enable Middle Button Reporting
3883 // R - Enable Right Button Reporting
3884 // K - Enable SHIFT and CTRL key Reporting
3885 // + - Enable Advanced messaging of mouse moves and up/down messages
3886 // Q - Quiet No Ack
3887 // # - Numeric value of mouse pointer required
3888 // 0 = Multiview 2000 cursor, used as standard
3889 // 1 = Windows Arrow
3890 // 2 = Windows I Beam
3891 // 3 = Windows Hour Glass
3892 // 4 = Windows Cross Hair
3893 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003894# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003895 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003896 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003897# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003898 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003899# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003900 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902 else
3903 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003904 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003905 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
3907 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003908#endif
3909#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 else
3911 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003912 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 if (on)
3914 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3915 else
3916 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003917 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920}
3921
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003922#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003923/*
3924 * Called when 'balloonevalterm' changed.
3925 */
3926 void
3927mch_bevalterm_changed(void)
3928{
3929 mch_setmouse(mouse_ison);
3930}
3931#endif
3932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933/*
3934 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3935 */
3936 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003937check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938{
3939# ifdef FEAT_MOUSE_XTERM
3940 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003941# ifdef FEAT_MOUSE_URXVT
3942 && use_xterm_mouse() != 3
3943# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944# ifdef FEAT_GUI
3945 && !gui.in_use
3946# endif
3947 )
3948 {
3949 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003950 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 if (*p_mouse != NUL)
3952 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003953 // force mouse off and maybe on to send possibly new mouse
3954 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 mch_setmouse(FALSE);
3956 setmouse();
3957 }
3958 }
3959 else
3960 del_mouse_termcode(KS_MOUSE);
3961# endif
3962
3963# ifdef FEAT_MOUSE_GPM
3964 if (!use_xterm_mouse()
3965# ifdef FEAT_GUI
3966 && !gui.in_use
3967# endif
3968 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003969 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003970 else
3971 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972# endif
3973
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003974# ifdef FEAT_SYSMOUSE
3975 if (!use_xterm_mouse()
3976# ifdef FEAT_GUI
3977 && !gui.in_use
3978# endif
3979 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003980 set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003981# endif
3982
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003984 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 if (!use_xterm_mouse()
3986# ifdef FEAT_GUI
3987 && !gui.in_use
3988# endif
3989 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003990 set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 else
3992 del_mouse_termcode(KS_JSBTERM_MOUSE);
3993# endif
3994
3995# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003996 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3997 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (!use_xterm_mouse()
3999# ifdef FEAT_GUI
4000 && !gui.in_use
4001# endif
4002 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004003 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 else
4005 del_mouse_termcode(KS_NETTERM_MOUSE);
4006# endif
4007
4008# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01004009 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004010 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011# ifdef FEAT_GUI
4012 && !gui.in_use
4013# endif
4014 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004015 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004016 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 else
4018 del_mouse_termcode(KS_DEC_MOUSE);
4019# endif
4020# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004021 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004022 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023# ifdef FEAT_GUI
4024 && !gui.in_use
4025# endif
4026 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004027 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 else
4029 del_mouse_termcode(KS_PTERM_MOUSE);
4030# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004031# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004032 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004033# ifdef FEAT_GUI
4034 && !gui.in_use
4035# endif
4036 )
4037 {
4038 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004039 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004040
4041 if (*p_mouse != NUL)
4042 {
4043 mch_setmouse(FALSE);
4044 setmouse();
4045 }
4046 }
4047 else
4048 del_mouse_termcode(KS_URXVT_MOUSE);
4049# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004050 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004051# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004052 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004053# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004054 )
4055 {
4056 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004057 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004058
4059 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004060 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004061
4062 if (*p_mouse != NUL)
4063 {
4064 mch_setmouse(FALSE);
4065 setmouse();
4066 }
4067 }
4068 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004069 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004070 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004071 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075#ifndef VMS
4076
4077/*
4078 * Try to get the current window size:
4079 * 1. with an ioctl(), most accurate method
4080 * 2. from the environment variables LINES and COLUMNS
4081 * 3. from the termcap
4082 * 4. keep using the old values
4083 * Return OK when size could be determined, FAIL otherwise.
4084 */
4085 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004086mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087{
4088 long rows = 0;
4089 long columns = 0;
4090 char_u *p;
4091
4092 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 * 1. try using an ioctl. It is the most accurate method.
4094 *
4095 * Try using TIOCGWINSZ first, some systems that have it also define
4096 * TIOCGSIZE but don't have a struct ttysize.
4097 */
4098# ifdef TIOCGWINSZ
4099 {
4100 struct winsize ws;
4101 int fd = 1;
4102
Bram Moolenaar0f873732019-12-05 20:28:46 +01004103 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 if (!isatty(fd) && isatty(read_cmd_fd))
4105 fd = read_cmd_fd;
4106 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4107 {
4108 columns = ws.ws_col;
4109 rows = ws.ws_row;
4110 }
4111 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004112# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113# ifdef TIOCGSIZE
4114 {
4115 struct ttysize ts;
4116 int fd = 1;
4117
Bram Moolenaar0f873732019-12-05 20:28:46 +01004118 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 if (!isatty(fd) && isatty(read_cmd_fd))
4120 fd = read_cmd_fd;
4121 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4122 {
4123 columns = ts.ts_cols;
4124 rows = ts.ts_lines;
4125 }
4126 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004127# endif // TIOCGSIZE
4128# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
4130 /*
4131 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004132 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4133 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004135 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
4137 if ((p = (char_u *)getenv("LINES")))
4138 rows = atoi((char *)p);
4139 if ((p = (char_u *)getenv("COLUMNS")))
4140 columns = atoi((char *)p);
4141 }
4142
4143#ifdef HAVE_TGETENT
4144 /*
4145 * 3. try reading "co" and "li" entries from termcap
4146 */
4147 if (columns == 0 || rows == 0)
4148 getlinecol(&columns, &rows);
4149#endif
4150
4151 /*
4152 * 4. If everything fails, use the old values
4153 */
4154 if (columns <= 0 || rows <= 0)
4155 return FAIL;
4156
4157 Rows = rows;
4158 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004159 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 return OK;
4161}
4162
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004163#if defined(FEAT_TERMINAL) || defined(PROTO)
4164/*
4165 * Report the windows size "rows" and "cols" to tty "fd".
4166 */
4167 int
4168mch_report_winsize(int fd, int rows, int cols)
4169{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004170 int tty_fd;
4171 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004172
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004173 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004174 if (tty_fd < 0)
4175 return FAIL;
4176
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004177# if defined(TIOCSWINSZ)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004178 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004179
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004180 ws.ws_col = cols;
4181 ws.ws_row = rows;
4182 ws.ws_xpixel = cols * 5;
4183 ws.ws_ypixel = rows * 10;
4184 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4185 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4186 retval == 0 ? "success" : "failed");
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004187# elif defined(TIOCSSIZE)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004188 struct ttysize ts;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004189
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004190 ts.ts_cols = cols;
4191 ts.ts_lines = rows;
4192 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4193 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4194 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004195# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004196 if (tty_fd != fd)
4197 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004198 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004199}
4200#endif
4201
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202/*
4203 * Try to set the window size to Rows and Columns.
4204 */
4205 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004206mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208 if (*T_CWS)
4209 {
4210 /*
4211 * NOTE: if you get an error here that term_set_winsize() is
4212 * undefined, check the output of configure. It could probably not
4213 * find a ncurses, termcap or termlib library.
4214 */
4215 term_set_winsize((int)Rows, (int)Columns);
4216 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004217 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219}
4220
Bram Moolenaar0f873732019-12-05 20:28:46 +01004221#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223/*
4224 * Rows and/or Columns has changed.
4225 */
4226 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004227mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004229 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230}
4231
Bram Moolenaar205b8862011-09-07 15:04:31 +02004232/*
4233 * Wait for process "child" to end.
4234 * Return "child" if it exited properly, <= 0 on error.
4235 */
4236 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004237wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004238{
4239 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004240 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004241
4242 while (wait_pid != child)
4243 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004244 // When compiled with Python threads are probably used, in which case
4245 // wait() sometimes hangs for no obvious reason. Use waitpid()
4246 // instead and loop (like the GUI). Also needed for other interfaces,
4247 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004248# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004249 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004250# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004251 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004252# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004253 if (wait_pid == 0)
4254 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004255 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004256 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004257 if (++delay_msec > 10)
4258 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004259 continue;
4260 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004261 if (wait_pid <= 0
4262# ifdef ECHILD
4263 && errno == ECHILD
4264# endif
4265 )
4266 break;
4267 }
4268 return wait_pid;
4269}
4270
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004271#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004272/*
4273 * Set the environment for a child process.
4274 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004275 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004276set_child_environment(
4277 long rows,
4278 long columns,
4279 char *term,
4280 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004281{
4282# ifdef HAVE_SETENV
4283 char envbuf[50];
4284# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004285 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004286 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004287 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004288 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004289 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004290# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004291 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004292# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004293# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004294 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004295# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004296# endif
4297
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004298# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004299 setenv("TERM", term, 1);
4300 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004301 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004302 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004303 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004304 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004305 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004306 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004307 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004308# ifdef FEAT_TERMINAL
4309 if (is_terminal)
4310 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004311 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004312 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4313 }
4314# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004315# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004316 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004317# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004318# else
4319 /*
4320 * Putenv does not copy the string, it has to remain valid.
4321 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004322 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004323 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004324 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4325 putenv(envbuf_Term);
4326 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004327 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004328 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4329 putenv(envbuf_Lines);
4330 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4331 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004332 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004333 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004334 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004335# ifdef FEAT_TERMINAL
4336 if (is_terminal)
4337 {
4338 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004339 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004340 putenv(envbuf_Version);
4341 }
4342# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004343# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004344 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4345 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4346 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004347# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004348# endif
4349}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004350
4351 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004352set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004353{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004354 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004355}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004356#endif
4357
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004358#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004359/*
4360 * Open a PTY, with FD for the master and slave side.
4361 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004362 * When successful both file descriptors are stored and the allocated pty name
4363 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004364 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004365 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004366open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004367{
4368 char *tty_name;
4369
Bram Moolenaar59386482019-02-10 22:43:46 +01004370 if (name1 != NULL)
4371 *name1 = NULL;
4372 if (name2 != NULL)
4373 *name2 = NULL;
4374
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004375 *pty_master_fd = mch_openpty(&tty_name); // open pty
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004376 if (*pty_master_fd < 0)
4377 return;
4378
4379 // Leaving out O_NOCTTY may lead to waitpid() always returning
4380 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4381 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004382#ifdef O_NOCTTY
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004383 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004384#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004385 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004386#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004387 if (*pty_slave_fd < 0)
4388 {
4389 close(*pty_master_fd);
4390 *pty_master_fd = -1;
4391 }
4392 else
4393 {
4394 if (name1 != NULL)
4395 *name1 = vim_strsave((char_u *)tty_name);
4396 if (name2 != NULL)
4397 *name2 = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004398 }
4399}
4400#endif
4401
Bram Moolenaarfae42832017-08-01 22:24:26 +02004402/*
4403 * Send SIGINT to a child process if "c" is an interrupt character.
4404 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004405 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004406may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4407{
4408# ifdef SIGINT
4409 if (c == Ctrl_C || c == intr_char)
4410 {
4411# ifdef HAVE_SETSID
4412 kill(-pid, SIGINT);
4413# else
4414 kill(0, SIGINT);
4415# endif
4416 if (wpid > 0)
4417 kill(wpid, SIGINT);
4418 }
4419# endif
4420}
4421
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004422#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004423
Bram Moolenaar0f873732019-12-05 20:28:46 +01004424/*
4425 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4426 * pointers, the last one is NULL.
4427 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4428 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004429 int
4430unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004431 char_u *cmd,
4432 char ***argvp,
4433 char_u **sh_tofree,
4434 char_u **shcf_tofree)
4435{
4436 char **argv = NULL;
4437 int argc;
4438
4439 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004440 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004441 return FAIL;
4442
4443 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4444 return FAIL;
4445 *argvp = argv;
4446
4447 if (cmd != NULL)
4448 {
4449 char_u *s;
4450 char_u *p;
4451
4452 if (extra_shell_arg != NULL)
4453 argv[argc++] = (char *)extra_shell_arg;
4454
Bram Moolenaar0f873732019-12-05 20:28:46 +01004455 // Break 'shellcmdflag' into white separated parts. This doesn't
4456 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004457 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004458 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004459 return FAIL;
4460 s = *shcf_tofree;
4461 p = p_shcf;
4462 while (*p != NUL)
4463 {
4464 argv[argc++] = (char *)s;
4465 while (*p && *p != ' ' && *p != TAB)
4466 *s++ = *p++;
4467 *s++ = NUL;
4468 p = skipwhite(p);
4469 }
4470
4471 argv[argc++] = (char *)cmd;
4472 }
4473 argv[argc] = NULL;
4474 return OK;
4475}
4476#endif
4477
4478#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4479/*
4480 * Use a terminal window to run a shell command in.
4481 */
4482 static int
4483mch_call_shell_terminal(
4484 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004485 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004486{
4487 jobopt_T opt;
4488 char **argv = NULL;
4489 char_u *tofree1 = NULL;
4490 char_u *tofree2 = NULL;
4491 int retval = -1;
4492 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004493 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004494 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004495 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004496
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004497 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004498 goto theend;
4499
4500 init_job_options(&opt);
4501 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4502 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004503 if (buf == NULL)
4504 goto theend;
4505
4506 job = term_getjob(buf->b_term);
4507 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004508
Bram Moolenaar0f873732019-12-05 20:28:46 +01004509 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004510 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004511 if (curbuf == buf)
Bram Moolenaar13568252018-03-16 20:46:58 +01004512 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004513 // Only when managed to find a window for "buf",
4514 clear_oparg(&oa);
4515 while (term_use_loop())
Bram Moolenaar13568252018-03-16 20:46:58 +01004516 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004517 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4518 {
4519 // If terminal_loop() returns OK we got a key that is handled
4520 // in Normal model. We don't do redrawing anyway.
4521 if (terminal_loop(TRUE) == OK)
4522 normal_cmd(&oa, TRUE);
4523 }
4524 else
Bram Moolenaar13568252018-03-16 20:46:58 +01004525 normal_cmd(&oa, TRUE);
4526 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00004527 retval = job->jv_exitval;
4528 ch_log(NULL, "system command finished");
4529
4530 job_unref(job);
4531
4532 // restore curwin/curbuf and a few other things
4533 aucmd_restbuf(&aco);
Bram Moolenaar13568252018-03-16 20:46:58 +01004534 }
Bram Moolenaar13568252018-03-16 20:46:58 +01004535
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01004536 // Only require pressing Enter when redrawing, to avoid that system() gets
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004537 // the hit-enter prompt even though it didn't output anything.
4538 if (!RedrawingDisabled)
4539 wait_return(TRUE);
Bram Moolenaar13568252018-03-16 20:46:58 +01004540 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4541
4542theend:
4543 vim_free(argv);
4544 vim_free(tofree1);
4545 vim_free(tofree2);
4546 return retval;
4547}
4548#endif
4549
4550#ifdef USE_SYSTEM
4551/*
4552 * Use system() to start the shell: simple but slow.
4553 */
4554 static int
4555mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004556 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004557 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558{
4559#ifdef VMS
4560 char *ifn = NULL;
4561 char *ofn = NULL;
4562#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004563 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004564 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004565 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
4567 out_flush();
4568
4569 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004570 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571
Bram Moolenaar62b42182010-09-21 22:09:37 +02004572# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004573 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004574 loose_clipboard();
4575# endif
4576
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 if (cmd == NULL)
4578 x = system((char *)p_sh);
4579 else
4580 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004581# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 if (ofn = strchr((char *)cmd, '>'))
4583 *ofn++ = '\0';
4584 if (ifn = strchr((char *)cmd, '<'))
4585 {
4586 char *p;
4587
4588 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004589 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 if (p)
4591 *p = '\0';
4592 }
4593 if (ofn)
4594 x = vms_sys((char *)cmd, ofn, ifn);
4595 else
4596 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004597# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004598 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004600 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 if (newcmd == NULL)
4602 x = 0;
4603 else
4604 {
4605 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4606 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4607 (char *)p_shcf,
4608 (char *)cmd);
4609 x = system((char *)newcmd);
4610 vim_free(newcmd);
4611 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 }
4614# ifdef VMS
4615 x = vms_sys_status(x);
4616# endif
4617 if (emsg_silent)
4618 ;
4619 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004620 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 else if (x && !(options & SHELL_SILENT))
4622 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004623 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 msg_outnum((long)x);
4625 msg_putchar('\n');
4626 }
4627
4628 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004629 {
4630 // The shell may have messed with the mode, always set it.
4631 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004632 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004635# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4636 restore_clipboard();
4637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004639}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
Bram Moolenaar0f873732019-12-05 20:28:46 +01004641#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
Bram Moolenaar0f873732019-12-05 20:28:46 +01004643# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4644 // 127, some shells use that already
4645# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646
Bram Moolenaar13568252018-03-16 20:46:58 +01004647/*
4648 * Don't use system(), use fork()/exec().
4649 */
4650 static int
4651mch_call_shell_fork(
4652 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004653 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004654{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004655 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004657 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 pid_t wait_pid = 0;
4659# ifdef HAVE_UNION_WAIT
4660 union wait status;
4661# else
4662 int status = -1;
4663# endif
4664 int retval = -1;
4665 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004666 char_u *tofree1 = NULL;
4667 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004669 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004670# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004672# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004673 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 int fd_fromshell[2];
4675 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004676 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
4678 out_flush();
4679 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004680 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004681 if (tmode == TMODE_RAW)
4682 // The shell may have messed with the mode, always set it later.
4683 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004685 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004686 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004687
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004689 * For the GUI, when writing the output into the buffer and when reading
4690 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4691 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004693 if ((options & (SHELL_READ|SHELL_WRITE))
4694# ifdef FEAT_GUI
4695 || (gui.in_use && show_shell_mess)
4696# endif
4697 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004699# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 /*
4701 * Try to open a master pty.
4702 * If this works, open the slave pty.
4703 * If the slave can't be opened, close the master pty.
4704 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004705 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004706 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 /*
4708 * If not opening a pty or it didn't work, try using pipes.
4709 */
4710 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004711# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
4713 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004714 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
4716 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004717 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 {
4719 close(fd_toshell[0]);
4720 close(fd_toshell[1]);
4721 }
4722 }
4723 if (pipe_error)
4724 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004725 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 out_flush();
4727 }
4728 }
4729 }
4730
Bram Moolenaar0f873732019-12-05 20:28:46 +01004731 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004733 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004734 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004735 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004736 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004738 UNBLOCK_SIGNALS(&curset);
4739
Bram Moolenaar32526b32019-01-19 17:43:09 +01004740 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004741 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004743 || (gui.in_use && show_shell_mess)
4744# endif
4745 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004747# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004748 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
4750 close(pty_master_fd);
4751 close(pty_slave_fd);
4752 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004753 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004754# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
4756 close(fd_toshell[0]);
4757 close(fd_toshell[1]);
4758 close(fd_fromshell[0]);
4759 close(fd_fromshell[1]);
4760 }
4761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004763 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004765 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004766 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00004768# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01004769 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004770 {
4771 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004772 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004773 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004774# endif
4775
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 if (!show_shell_mess || (options & SHELL_EXPAND))
4777 {
4778 int fd;
4779
4780 /*
4781 * Don't want to show any message from the shell. Can't just
4782 * close stdout and stderr though, because some systems will
4783 * break if you try to write to them after that, so we must
4784 * use dup() to replace them with something else -- webb
4785 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4786 * waiting for input.
4787 */
4788 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4789 fclose(stdin);
4790 fclose(stdout);
4791 fclose(stderr);
4792
4793 /*
4794 * If any of these open()'s and dup()'s fail, we just continue
4795 * anyway. It's not fatal, and on most systems it will make
4796 * no difference at all. On a few it will cause the execvp()
4797 * to exit with a non-zero status even when the completion
4798 * could be done, which is nothing too serious. If the open()
4799 * or dup() failed we'd just do the same thing ourselves
4800 * anyway -- webb
4801 */
4802 if (fd >= 0)
4803 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004804 vim_ignored = dup(fd); // To replace stdin (fd 0)
4805 vim_ignored = dup(fd); // To replace stdout (fd 1)
4806 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807
Bram Moolenaar0f873732019-12-05 20:28:46 +01004808 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 close(fd);
4810 }
4811 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004812 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004814 || gui.in_use
4815# endif
4816 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
4818
Bram Moolenaardf177f62005-02-22 08:39:57 +00004819# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004820 // Create our own process group, so that the child and all its
4821 // children can be kill()ed. Don't do this when using pipes,
4822 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004823 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004824 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004825 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004826# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004827 // When doing "!xterm&" and 'shell' is bash: the shell
4828 // will exit and send SIGHUP to all processes in its
4829 // group, killing the just started process. Ignore SIGHUP
4830 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004831 signal(SIGHUP, SIG_IGN);
4832# endif
4833 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004834# endif
4835# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004836 if (pty_slave_fd >= 0)
4837 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004838 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004839 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004840 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004841# ifdef TIOCSCTTY
4842 // Try to become controlling tty (probably doesn't work,
4843 // unless run by root)
4844 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4845# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004846 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004847# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004848 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
Bram Moolenaara5792f52005-11-23 21:25:05 +00004850 /*
4851 * stderr is only redirected when using the GUI, so that a
4852 * program like gpg can still access the terminal to get a
4853 * passphrase using stderr.
4854 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004855# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 if (pty_master_fd >= 0)
4857 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004858 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
Bram Moolenaar0f873732019-12-05 20:28:46 +01004860 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004862 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004864 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004865 if (gui.in_use)
4866 {
4867 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004868 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
Bram Moolenaar0f873732019-12-05 20:28:46 +01004871 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
4873 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004874# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004876 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 close(fd_toshell[1]);
4878 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004879 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 close(fd_toshell[0]);
4881
Bram Moolenaar0f873732019-12-05 20:28:46 +01004882 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 close(fd_fromshell[0]);
4884 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004885 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 close(fd_fromshell[1]);
4887
Bram Moolenaara5792f52005-11-23 21:25:05 +00004888# ifdef FEAT_GUI
4889 if (gui.in_use)
4890 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004891 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004892 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004893 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004894 }
4895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 }
4897 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004898
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 /*
4900 * There is no type cast for the argv, because the type may be
4901 * different on different machines. This may cause a warning
4902 * message with strict compilers, don't worry about it.
4903 * Call _exit() instead of exit() to avoid closing the connection
4904 * to the X server (esp. with GTK, which uses atexit()).
4905 */
4906 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004907 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004909 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 {
4911 /*
4912 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004913 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 */
4915 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004916 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004917 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004918# ifdef FEAT_JOB_CHANNEL
4919 ++dont_check_job_ended;
4920# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 /*
4922 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004923 * This is also used to pipe stdin/stdout to/from the external
4924 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004926 if ((options & (SHELL_READ|SHELL_WRITE))
4927# ifdef FEAT_GUI
4928 || (gui.in_use && show_shell_mess)
4929# endif
4930 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004932# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004934 int buffer_off = 0; // valid bytes in buffer[]
4935 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4936 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 int len;
4938 int p_more_save;
4939 int old_State;
4940 int c;
4941 int toshell_fd;
4942 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004943 garray_T ga;
4944 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004945# ifdef ELAPSED_FUNC
4946 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004947# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
Bram Moolenaardf177f62005-02-22 08:39:57 +00004949# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 if (pty_master_fd >= 0)
4951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 fromshell_fd = pty_master_fd;
4953 toshell_fd = dup(pty_master_fd);
4954 }
4955 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
4958 close(fd_toshell[0]);
4959 close(fd_fromshell[1]);
4960 toshell_fd = fd_toshell[1];
4961 fromshell_fd = fd_fromshell[0];
4962 }
4963
4964 /*
4965 * Write to the child if there are typed characters.
4966 * Read from the child if there are characters available.
4967 * Repeat the reading a few times if more characters are
4968 * available. Need to check for typed keys now and then, but
4969 * not too often (delays when no chars are available).
4970 * This loop is quit if no characters can be read from the pty
4971 * (WaitForChar detected special condition), or there are no
4972 * characters available and the child has exited.
4973 * Only check if the child has exited when there is no more
4974 * output. The child may exit before all the output has
4975 * been printed.
4976 *
4977 * Currently this busy loops!
4978 * This can probably dead-lock when the write blocks!
4979 */
4980 p_more_save = p_more;
4981 p_more = FALSE;
4982 old_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004983 State = MODE_EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004985 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004986 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004987 // Fork a process that will write the lines to the
4988 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004989 if ((wpid = fork()) == -1)
4990 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004991 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004992 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004993 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004994 {
4995 linenr_T lnum = curbuf->b_op_start.lnum;
4996 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004997 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004998 size_t l;
4999
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005000 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005001 for (;;)
5002 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005003 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005004 if (l == 0)
5005 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005006 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005007 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00005008 len = write(toshell_fd, "", (size_t)1);
5009 else
5010 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005011 char_u *s = vim_strchr(lp + written, NL);
5012
Bram Moolenaar89d40322006-08-29 15:30:07 +00005013 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00005014 s == NULL ? l
5015 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005016 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00005017 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005018 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005019 // Finished a line, add a NL, unless this line
5020 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005021 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005022 || (!curbuf->b_p_bin
5023 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005024 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaar88456cd2022-11-18 22:14:09 +00005025 && (lnum != curbuf->b_ml.ml_line_count
Bram Moolenaardf177f62005-02-22 08:39:57 +00005026 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005027 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005028 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005029 ++lnum;
5030 if (lnum > curbuf->b_op_end.lnum)
5031 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005032 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005033 close(toshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005034 break;
5035 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005036 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005037 written = 0;
5038 }
5039 else if (len > 0)
5040 written += len;
5041 }
5042 _exit(0);
5043 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005044 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005045 {
5046 close(toshell_fd);
5047 toshell_fd = -1;
5048 }
5049 }
5050
5051 if (options & SHELL_READ)
5052 ga_init2(&ga, 1, BUFLEN);
5053
5054 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005055# ifdef ELAPSED_FUNC
5056 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005057# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 for (;;)
5059 {
5060 /*
5061 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005062 * if there are any.
5063 * Don't do this if we are expanding wild cards (would eat
5064 * typeahead).
5065 * Don't do this when filtering and terminal is in cooked
5066 * mode, the shell command will handle the I/O. Avoids
5067 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005068 * Don't get characters when the child has already
5069 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005070 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005071 * while (noread_cnt > 4), avoids that ":r !ls" eats
5072 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 */
5074 len = 0;
5075 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005076 && ((options &
5077 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5078 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005079# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005080 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005081# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005082 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005083 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005084 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005086 if (ta_len == 0)
5087 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005088 // Get extra characters when we don't have any.
5089 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005090 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005091# ifdef ELAPSED_FUNC
5092 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005093# endif
5094 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5095 }
5096 if (ta_len > 0 || len > 0)
5097 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 /*
5099 * For pipes:
5100 * Check for CTRL-C: send interrupt signal to child.
5101 * Check for CTRL-D: EOF, close pipe to child.
5102 */
5103 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 /*
5106 * Send SIGINT to the child's group or all
5107 * processes in our group.
5108 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005109 may_send_sigint(ta_buf[ta_len], pid, wpid);
5110
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 if (pty_master_fd < 0 && toshell_fd >= 0
5112 && ta_buf[ta_len] == Ctrl_D)
5113 {
5114 close(toshell_fd);
5115 toshell_fd = -1;
5116 }
5117 }
5118
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01005119 // Remove Vim-specific codes from the input.
5120 len = term_replace_keycodes(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121
5122 /*
5123 * For pipes: echo the typed characters.
5124 * For a pty this does not seem to work.
5125 */
5126 if (pty_master_fd < 0)
5127 {
5128 for (i = ta_len; i < ta_len + len; ++i)
5129 {
5130 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5131 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 else if (has_mbyte)
5133 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005134 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135
5136 msg_outtrans_len(ta_buf + i, l);
5137 i += l - 1;
5138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 else
5140 msg_outtrans_len(ta_buf + i, 1);
5141 }
5142 windgoto(msg_row, msg_col);
5143 out_flush();
5144 }
5145
5146 ta_len += len;
5147
5148 /*
5149 * Write the characters to the child, unless EOF has
5150 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005151 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005152 * When writing buffer lines, drop the typed
5153 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005155 if (options & SHELL_WRITE)
5156 ta_len = 0;
5157 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 {
5159 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5160 if (len > 0)
5161 {
5162 ta_len -= len;
5163 mch_memmove(ta_buf, ta_buf + len, ta_len);
5164 }
5165 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
5168
Bram Moolenaardf177f62005-02-22 08:39:57 +00005169 if (got_int)
5170 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005171 // CTRL-C sends a signal to the child, we ignore it
5172 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005173# ifdef HAVE_SETSID
5174 kill(-pid, SIGINT);
5175# else
5176 kill(0, SIGINT);
5177# endif
5178 if (wpid > 0)
5179 kill(wpid, SIGINT);
5180 got_int = FALSE;
5181 }
5182
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 /*
5184 * Check if the child has any characters to be printed.
5185 * Read them and write them to our window. Repeat this as
5186 * long as there is something to do, avoid the 10ms wait
5187 * for mch_inchar(), or sending typeahead characters to
5188 * the external process.
5189 * TODO: This should handle escape sequences, compatible
5190 * to some terminal (vt52?).
5191 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005192 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005193 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005195 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005198 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005200
5201 noread_cnt = 0;
5202 if (options & SHELL_READ)
5203 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005204 // Do NUL -> NL translation, append NL separated
5205 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005206 for (i = 0; i < len; ++i)
5207 {
5208 if (buffer[i] == NL)
5209 append_ga_line(&ga);
5210 else if (buffer[i] == NUL)
5211 ga_append(&ga, NL);
5212 else
5213 ga_append(&ga, buffer[i]);
5214 }
5215 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005216 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
5218 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005219 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
Bram Moolenaardf177f62005-02-22 08:39:57 +00005221 len += buffer_off;
5222 buffer[len] = NUL;
5223
Bram Moolenaar0f873732019-12-05 20:28:46 +01005224 // Check if the last character in buffer[] is
5225 // incomplete, keep these bytes for the next
5226 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 for (p = buffer; p < buffer + len; p += l)
5228 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005229 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005231 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 else if (MB_BYTE2LEN(*p) != l)
5233 break;
5234 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005235 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005237 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 if (len >= 12)
5239 ++p;
5240 else
5241 {
5242 buffer_off = len;
5243 continue;
5244 }
5245 }
5246 c = *p;
5247 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005248 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 if (p < buffer + len)
5250 {
5251 *p = c;
5252 buffer_off = (buffer + len) - p;
5253 mch_memmove(buffer, p, buffer_off);
5254 continue;
5255 }
5256 buffer_off = 0;
5257 }
5258 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
5260 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005261 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 }
5263
5264 windgoto(msg_row, msg_col);
5265 cursor_on();
5266 out_flush();
5267 if (got_int)
5268 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005269
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005270# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005271 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005272 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005273 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005274
Bram Moolenaar0f873732019-12-05 20:28:46 +01005275 // Avoid that we keep looping here without
5276 // checking for a CTRL-C for a long time. Don't
5277 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005278 if (msec > 2000)
5279 {
5280 noread_cnt = 5;
5281 break;
5282 }
5283 }
5284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286
Bram Moolenaar0f873732019-12-05 20:28:46 +01005287 // If we already detected the child has finished, continue
5288 // reading output for a short while. Some text may be
5289 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005290 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005291 {
5292 if (noread_cnt < 5)
5293 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005294 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005295 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 /*
5298 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005299 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005301# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005302 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005303# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005305# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5307 || (wait_pid == pid && WIFEXITED(status)))
5308 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005309 // Don't break the loop yet, try reading more
5310 // characters from "fromshell_fd" first. When using
5311 // pipes there might still be something to read and
5312 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005315 else
5316 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005317
Bram Moolenaar95a51352013-03-21 22:53:50 +01005318# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005319 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005320 clip_update();
5321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 }
5323finished:
5324 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005325 if (options & SHELL_READ)
5326 {
5327 if (ga.ga_len > 0)
5328 {
5329 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005330 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005331 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005332 }
5333 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005334 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005335 ga_clear(&ga);
5336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 /*
5339 * Give all typeahead that wasn't used back to ui_inchar().
5340 */
5341 if (ta_len)
5342 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 State = old_State;
5344 if (toshell_fd >= 0)
5345 close(toshell_fd);
5346 close(fromshell_fd);
5347 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005348# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005349 else
5350 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005351 long delay_msec = 1;
5352
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005353 if (tmode == TMODE_RAW)
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005354 // Possibly disables modifyOtherKeys, so that the system
5355 // can recognize CTRL-C.
5356 out_str_t_TE();
Bram Moolenaar0981c872020-08-23 14:28:37 +02005357
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005358 /*
5359 * Similar to the loop above, but only handle X events, no
5360 * I/O.
5361 */
5362 for (;;)
5363 {
5364 if (got_int)
5365 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005366 // CTRL-C sends a signal to the child, we ignore it
5367 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005368# ifdef HAVE_SETSID
5369 kill(-pid, SIGINT);
5370# else
5371 kill(0, SIGINT);
5372# endif
5373 got_int = FALSE;
5374 }
5375# ifdef __NeXT__
5376 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5377# else
5378 wait_pid = waitpid(pid, &status, WNOHANG);
5379# endif
5380 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5381 || (wait_pid == pid && WIFEXITED(status)))
5382 {
5383 wait_pid = pid;
5384 break;
5385 }
5386
Bram Moolenaar0f873732019-12-05 20:28:46 +01005387 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005388 clip_update();
5389
Bram Moolenaar0f873732019-12-05 20:28:46 +01005390 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005391 // less time, gradually wait longer.
5392 mch_delay(delay_msec,
5393 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005394 if (++delay_msec > 10)
5395 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005396 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005397
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005398 if (tmode == TMODE_RAW)
5399 // possibly enables modifyOtherKeys again
Bram Moolenaar733a69b2022-12-01 12:03:47 +00005400 out_str_t_TI();
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005401 }
5402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403
5404 /*
5405 * Wait until our child has exited.
5406 * Ignore wait() returning pids of other children and returning
5407 * because of some signal like SIGWINCH.
5408 * Don't wait if wait_pid was already set above, indicating the
5409 * child already exited.
5410 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005411 if (wait_pid != pid)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01005412 (void)wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413
Bram Moolenaar624891f2010-10-13 16:22:09 +02005414# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005415 // Close slave side of pty. Only do this after the child has
5416 // exited, otherwise the child may hang when it tries to write on
5417 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005418 if (pty_master_fd >= 0)
5419 close(pty_slave_fd);
5420# endif
5421
Bram Moolenaar0f873732019-12-05 20:28:46 +01005422 // Make sure the child that writes to the external program is
5423 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005424 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005425 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005426 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005427 wait4pid(wpid, NULL);
5428 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005429
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005430# ifdef FEAT_JOB_CHANNEL
5431 --dont_check_job_ended;
5432# endif
5433
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 /*
5435 * Set to raw mode right now, otherwise a CTRL-C after
5436 * catch_signals() will kill Vim.
5437 */
5438 if (tmode == TMODE_RAW)
5439 settmode(TMODE_RAW);
5440 did_settmode = TRUE;
5441 set_signals();
5442
5443 if (WIFEXITED(status))
5444 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005445 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005447 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
5449 if (retval == EXEC_FAILED)
5450 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005451 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 msg_outtrans(p_sh);
5453 msg_putchar('\n');
5454 }
5455 else if (!(options & SHELL_SILENT))
5456 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005457 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 msg_outnum((long)retval);
5459 msg_putchar('\n');
5460 }
5461 }
5462 }
5463 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005464 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 }
5466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467
5468error:
5469 if (!did_settmode)
5470 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005471 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005473 vim_free(argv);
5474 vim_free(tofree1);
5475 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
5477 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005478}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005479#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005480
5481 int
5482mch_call_shell(
5483 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005484 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005485{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005486#ifdef FEAT_EVAL
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01005487 ch_log(NULL, "executing shell command: %s", cmd);
5488#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005489#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar524c8532022-09-27 15:48:20 +01005490 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL
5491 && (options & SHELL_SILENT) == 0)
Bram Moolenaar13568252018-03-16 20:46:58 +01005492 return mch_call_shell_terminal(cmd, options);
5493#endif
5494#ifdef USE_SYSTEM
5495 return mch_call_shell_system(cmd, options);
5496#else
5497 return mch_call_shell_fork(cmd, options);
5498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499}
5500
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005501#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005502 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005503mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005504{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005505 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005506 int fd_in[2] = {-1, -1}; // for stdin
5507 int fd_out[2] = {-1, -1}; // for stdout
5508 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005509 int pty_master_fd = -1;
5510 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005511 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005512 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5513 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5514 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005515 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005516 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5517 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005518 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005519 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005520 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005521
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005522 if (use_out_for_err && use_null_for_out)
5523 use_null_for_err = TRUE;
5524
Bram Moolenaar0f873732019-12-05 20:28:46 +01005525 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005526 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005527
Bram Moolenaarb2412082017-08-20 18:09:14 +02005528 if (options->jo_pty
5529 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005530 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005531 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005532 open_pty(&pty_master_fd, &pty_slave_fd,
5533 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005534
Bram Moolenaar0f873732019-12-05 20:28:46 +01005535 // TODO: without the channel feature connect the child to /dev/null?
5536 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005537 if (use_file_for_in)
5538 {
5539 char_u *fname = options->jo_io_name[PART_IN];
5540
5541 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5542 if (fd_in[0] < 0)
5543 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005544 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005545 goto failed;
5546 }
5547 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005548 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005549 // When writing buffer lines to the input don't use the pty, so that
5550 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005551 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5552 && pipe(fd_in) < 0)
5553 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005554
5555 if (use_file_for_out)
5556 {
5557 char_u *fname = options->jo_io_name[PART_OUT];
5558
5559 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5560 if (fd_out[1] < 0)
5561 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005562 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005563 goto failed;
5564 }
5565 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005566 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005567 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005568
5569 if (use_file_for_err)
5570 {
5571 char_u *fname = options->jo_io_name[PART_ERR];
5572
5573 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5574 if (fd_err[1] < 0)
5575 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005576 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005577 goto failed;
5578 }
5579 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005580 else if (!use_out_for_err && !use_null_for_err
5581 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005582 goto failed;
5583
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005584 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5585 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005586 if (options->jo_set & JO_CHANNEL)
5587 {
5588 channel = options->jo_channel;
5589 if (channel != NULL)
5590 ++channel->ch_refcount;
5591 }
5592 else
5593 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005594 if (channel == NULL)
5595 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005596 if (job->jv_tty_out != NULL)
5597 ch_log(channel, "using pty %s on fd %d",
5598 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005599 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005600
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005601 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005602 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005603 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005604 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005605 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005606 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005607 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005608 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005609 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005610 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005611 int null_fd = -1;
5612 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005613
Bram Moolenaar0f873732019-12-05 20:28:46 +01005614 // child
5615 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005616 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005617
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005618# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01005619 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005620 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005621 ch_logfile((char_u *)"", (char_u *)"");
5622# endif
5623
Bram Moolenaar835dc632016-02-07 14:27:38 +01005624# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005625 // Create our own process group, so that the child and all its
5626 // children can be kill()ed. Don't do this when using pipes,
5627 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005628 (void)setsid();
5629# endif
5630
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005631# ifdef FEAT_TERMINAL
5632 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005633 {
5634 char *term = (char *)T_NAME;
5635
5636#ifdef FEAT_GUI
5637 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005638 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005639 term = getenv("TERM");
5640#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005641 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005642 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005643 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005644 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005645 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005646 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005647 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005648 else if (t_colors > 16)
5649 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005650 else
5651 term = "xterm";
5652 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005653 set_child_environment(
5654 (long)options->jo_term_rows,
5655 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005656 term,
5657 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005658 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005659 else
5660# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005661 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005662
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005663 if (options->jo_env != NULL)
5664 {
5665 dict_T *dict = options->jo_env;
5666 hashitem_T *hi;
5667 int todo = (int)dict->dv_hashtab.ht_used;
5668
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005669 FOR_ALL_HASHTAB_ITEMS(&dict->dv_hashtab, hi, todo)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005670 if (!HASHITEM_EMPTY(hi))
5671 {
5672 typval_T *item = &dict_lookup(hi)->di_tv;
5673
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005674 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005675 --todo;
5676 }
5677 }
5678
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005679 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005680 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005681 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005682 if (null_fd < 0)
5683 {
5684 perror("opening /dev/null failed");
5685 _exit(OPEN_NULL_FAILED);
5686 }
5687 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005688
Bram Moolenaar223896d2017-08-02 22:33:28 +02005689 if (pty_slave_fd >= 0)
5690 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005691 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005692 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005693# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005694 // Try to become controlling tty (probably doesn't work,
5695 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005696 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5697# endif
5698 }
5699
Bram Moolenaar0f873732019-12-05 20:28:46 +01005700 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005701 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005702 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005703 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005704 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005705 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005706 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005707 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005708
Bram Moolenaar0f873732019-12-05 20:28:46 +01005709 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005710 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005711 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005712 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005713 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005714 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005715 }
5716 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005717 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005718 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005719 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005720 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005721 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005722
Bram Moolenaar0f873732019-12-05 20:28:46 +01005723 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005724 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005725 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005726 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005727 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005728 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005729 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005730 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005731
5732 if (fd_in[0] >= 0)
5733 close(fd_in[0]);
5734 if (fd_in[1] >= 0)
5735 close(fd_in[1]);
5736 if (fd_out[0] >= 0)
5737 close(fd_out[0]);
5738 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005739 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005740 if (fd_err[0] >= 0)
5741 close(fd_err[0]);
5742 if (fd_err[1] >= 0)
5743 close(fd_err[1]);
5744 if (pty_master_fd >= 0)
5745 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005746 close(pty_master_fd); // not used in the child
5747 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005748 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005749
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005750 if (null_fd >= 0)
5751 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005752
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005753 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5754 _exit(EXEC_FAILED);
5755
Bram Moolenaar0f873732019-12-05 20:28:46 +01005756 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005757 execvp(argv[0], argv);
5758
Bram Moolenaar4694a172016-04-21 14:05:23 +02005759 if (stderr_works)
5760 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005761# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005762 // calling free_all_mem() here causes problems. Ignore valgrind
5763 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005764# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005765 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005766 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005767
Bram Moolenaar0f873732019-12-05 20:28:46 +01005768 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005769 UNBLOCK_SIGNALS(&curset);
5770
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005771 job->jv_pid = pid;
5772 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005773 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005774
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005775 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005776 close(pty_slave_fd); // not used in the parent
5777 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005778 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005779 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005780 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005781 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005782 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005783 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005784 if (channel != NULL)
5785 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005786 int in_fd = INVALID_FD;
5787 int out_fd = INVALID_FD;
5788 int err_fd = INVALID_FD;
5789
5790 if (!(use_file_for_in || use_null_for_in))
5791 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5792
5793 if (!(use_file_for_out || use_null_for_out))
5794 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5795
5796 // When using pty_master_fd only set it for stdout, do not duplicate
5797 // it for stderr, it only needs to be read once.
5798 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5799 {
5800 if (fd_err[0] >= 0)
5801 err_fd = fd_err[0];
5802 else if (out_fd != pty_master_fd)
5803 err_fd = pty_master_fd;
5804 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005805
5806 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005807 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005808 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005809 else
5810 {
5811 if (fd_in[1] >= 0)
5812 close(fd_in[1]);
5813 if (fd_out[0] >= 0)
5814 close(fd_out[0]);
5815 if (fd_err[0] >= 0)
5816 close(fd_err[0]);
5817 if (pty_master_fd >= 0)
5818 close(pty_master_fd);
5819 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005820
Bram Moolenaar0f873732019-12-05 20:28:46 +01005821 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005822 return;
5823
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005824failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005825 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005826 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005827 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005828 if (fd_in[1] >= 0)
5829 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005830 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005831 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005832 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005833 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005834 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005835 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005836 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005837 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005838 if (pty_master_fd >= 0)
5839 close(pty_master_fd);
5840 if (pty_slave_fd >= 0)
5841 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005842}
5843
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005844 static char_u *
5845get_signal_name(int sig)
5846{
5847 int i;
5848 char_u numbuf[NUMBUFLEN];
5849
5850 if (sig == SIGKILL)
5851 return vim_strsave((char_u *)"kill");
5852
5853 for (i = 0; signal_info[i].sig != -1; i++)
5854 if (sig == signal_info[i].sig)
5855 return strlow_save((char_u *)signal_info[i].name);
5856
5857 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5858 return vim_strsave(numbuf);
5859}
5860
Bram Moolenaar835dc632016-02-07 14:27:38 +01005861 char *
5862mch_job_status(job_T *job)
5863{
5864# ifdef HAVE_UNION_WAIT
5865 union wait status;
5866# else
5867 int status = -1;
5868# endif
5869 pid_t wait_pid = 0;
5870
5871# ifdef __NeXT__
5872 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5873# else
5874 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5875# endif
5876 if (wait_pid == -1)
5877 {
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00005878 int waitpid_errno = errno;
5879 if (waitpid_errno == ECHILD && mch_process_running(job->jv_pid))
5880 // The process is alive, but it was probably reparented (for
5881 // example by ptrace called by a debugger like lldb or gdb).
5882 // Note: This assumes that process IDs are not reused.
5883 return "run";
5884
Bram Moolenaar0f873732019-12-05 20:28:46 +01005885 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005886 if (job->jv_status < JOB_ENDED)
5887 ch_log(job->jv_channel, "Job no longer exists: %s",
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00005888 strerror(waitpid_errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005889 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005890 }
5891 if (wait_pid == 0)
5892 return "run";
5893 if (WIFEXITED(status))
5894 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005895 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005896 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005897 if (job->jv_status < JOB_ENDED)
5898 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005899 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005900 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005901 if (WIFSIGNALED(status))
5902 {
5903 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005904 job->jv_termsig = get_signal_name(WTERMSIG(status));
5905 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5906 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5907 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005908 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005909 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005910 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005911
5912return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005913 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005914 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005915 return "dead";
5916}
5917
5918 job_T *
5919mch_detect_ended_job(job_T *job_list)
5920{
5921# ifdef HAVE_UNION_WAIT
5922 union wait status;
5923# else
5924 int status = -1;
5925# endif
5926 pid_t wait_pid = 0;
5927 job_T *job;
5928
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005929# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005930 // Do not do this when waiting for a shell command to finish, we would get
5931 // the exit value here (and discard it), the exit value obtained there
5932 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005933 if (dont_check_job_ended > 0)
5934 return NULL;
5935# endif
5936
Bram Moolenaar97792de2016-10-15 18:36:49 +02005937# ifdef __NeXT__
5938 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5939# else
5940 wait_pid = waitpid(-1, &status, WNOHANG);
5941# endif
5942 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005943 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005944 return NULL;
5945 for (job = job_list; job != NULL; job = job->jv_next)
5946 {
5947 if (job->jv_pid == wait_pid)
5948 {
5949 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005950 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005951 job->jv_exitval = WEXITSTATUS(status);
5952 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005953 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005954 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005955 job->jv_termsig = get_signal_name(WTERMSIG(status));
5956 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005957 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005958 {
5959 ch_log(job->jv_channel, "Job ended");
5960 job->jv_status = JOB_ENDED;
5961 }
5962 return job;
5963 }
5964 }
5965 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005966}
5967
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005968/*
5969 * Send a (deadly) signal to "job".
5970 * Return FAIL if "how" is not a valid name.
5971 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005972 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005973mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005974{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005975 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005976
Bram Moolenaar923d9262016-02-25 20:56:01 +01005977 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005978 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005979 else if (STRCMP(how, "hup") == 0)
5980 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005981 else if (STRCMP(how, "quit") == 0)
5982 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005983 else if (STRCMP(how, "int") == 0)
5984 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005985 else if (STRCMP(how, "kill") == 0)
5986 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005987#ifdef SIGWINCH
5988 else if (STRCMP(how, "winch") == 0)
5989 sig = SIGWINCH;
5990#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005991 else if (isdigit(*how))
5992 sig = atoi((char *)how);
5993 else
5994 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005995
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005996 // Never kill ourselves!
5997 if (job->jv_pid != 0)
5998 {
5999 // TODO: have an option to only kill the process, not the group?
6000 kill(-job->jv_pid, sig);
6001 kill(job->jv_pid, sig);
6002 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01006003
Bram Moolenaar835dc632016-02-07 14:27:38 +01006004 return OK;
6005}
Bram Moolenaar76467df2016-02-12 19:30:26 +01006006
6007/*
6008 * Clear the data related to "job".
6009 */
6010 void
6011mch_clear_job(job_T *job)
6012{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006013 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01006014# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006015 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006016# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006017 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006018# endif
6019}
Bram Moolenaar835dc632016-02-07 14:27:38 +01006020#endif
6021
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006022#if defined(FEAT_TERMINAL) || defined(PROTO)
6023 int
6024mch_create_pty_channel(job_T *job, jobopt_T *options)
6025{
6026 int pty_master_fd = -1;
6027 int pty_slave_fd = -1;
6028 channel_T *channel;
6029
Bram Moolenaar59386482019-02-10 22:43:46 +01006030 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02006031 if (pty_master_fd < 0 || pty_slave_fd < 0)
6032 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006033 close(pty_slave_fd);
6034
6035 channel = add_channel();
6036 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006037 {
6038 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006039 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006040 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006041 if (job->jv_tty_out != NULL)
6042 ch_log(channel, "using pty %s on fd %d",
6043 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006044 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006045 channel->ch_keep_open = TRUE;
6046
Bram Moolenaar0f873732019-12-05 20:28:46 +01006047 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6048 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006049 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006050 channel_set_job(channel, job, options);
6051 return OK;
6052}
6053#endif
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055/*
6056 * Check for CTRL-C typed by reading all available characters.
6057 * In cooked mode we should get SIGINT, no need to check.
6058 */
6059 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006060mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006062 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006063 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 fill_input_buf(FALSE);
6065}
6066
6067/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006068 * Wait "msec" msec until a character is available from the mouse, keyboard,
6069 * from inbuf[].
6070 * "msec" == -1 will block forever.
6071 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006072 * When "ignore_input" is TRUE even check for pending input when input is
6073 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006074 * "interrupted" (if not NULL) is set to TRUE when no character is available
6075 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006076 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006077 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 */
6079 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006080WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006082#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006083 return ui_wait_for_chars_or_timer(
6084 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006085#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006086 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006087#endif
6088}
6089
6090/*
6091 * Wait "msec" msec until a character is available from the mouse or keyboard
6092 * or from inbuf[].
6093 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006094 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006095 * "interrupted" (if not NULL) is set to TRUE when no character is available
6096 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006097 * When a GUI is being used, this will never get called -- webb
6098 */
6099 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006100WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006101{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102#ifdef FEAT_MOUSE_GPM
6103 int gpm_process_wanted;
6104#endif
6105#ifdef FEAT_XCLIPBOARD
6106 int rest;
6107#endif
6108 int avail;
6109
Bram Moolenaar0f873732019-12-05 20:28:46 +01006110 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 return 1;
6112
6113#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006114 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 if (WantQueryMouse)
6116 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006117 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006118 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006119 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 }
6121#endif
6122
6123 /*
6124 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6125 * events. This is a bit complicated, because they might both be defined.
6126 */
6127#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6128# ifdef FEAT_XCLIPBOARD
6129 rest = 0;
6130 if (do_xterm_trace())
6131 rest = msec;
6132# endif
6133 do
6134 {
6135# ifdef FEAT_XCLIPBOARD
6136 if (rest != 0)
6137 {
6138 msec = XT_TRACE_DELAY;
6139 if (rest >= 0 && rest < XT_TRACE_DELAY)
6140 msec = rest;
6141 if (rest >= 0)
6142 rest -= msec;
6143 }
6144# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01006145# ifdef FEAT_SOUND_MACOSX
6146 // Invoke any pending sound callbacks.
6147 process_cfrunloop();
6148# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006149# ifdef FEAT_SOUND_CANBERRA
6150 // Invoke any pending sound callbacks.
6151 if (has_sound_callback_in_queue())
6152 invoke_sound_callback();
6153# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154# ifdef FEAT_MOUSE_GPM
6155 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006156 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006157 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006158 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006160 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006164 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 return 1;
6166# ifdef FEAT_XCLIPBOARD
6167 if (rest == 0 || !do_xterm_trace())
6168# endif
6169 break;
6170 }
6171 }
6172 while (FALSE
6173# ifdef FEAT_MOUSE_GPM
6174 || (gpm_process_wanted && mch_gpm_process() == 0)
6175# endif
6176# ifdef FEAT_XCLIPBOARD
6177 || (!avail && rest != 0)
6178# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006179 )
6180 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
6182#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006183 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184#endif
6185 return avail;
6186}
6187
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006188#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189/*
6190 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006191 * "msec" == 0 will check for characters once.
6192 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006195 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006196 * "interrupted" (if not NULL) is set to TRUE when no character is available
6197 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006199 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006200RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201{
6202 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006203 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006204#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 static int busy = FALSE;
6206
Bram Moolenaar0f873732019-12-05 20:28:46 +01006207 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208# define MAY_LOOP
6209
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006210# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006211 // Remember at what time we started, so that we know how much longer we
6212 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006213 long start_msec = msec;
6214 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006216 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006217 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218# endif
6219
Bram Moolenaar0f873732019-12-05 20:28:46 +01006220 // Handle being called recursively. This may happen for the session
6221 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if (busy)
6223 return 0;
6224#endif
6225
6226#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006227 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228#endif
6229 {
6230#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006231 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006232# ifdef FEAT_MZSCHEME
6233 int mzquantum_used = FALSE;
6234# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235#endif
6236#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006237 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006238 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 int nfd;
6240# ifdef FEAT_XCLIPBOARD
6241 int xterm_idx = -1;
6242# endif
6243# ifdef FEAT_MOUSE_GPM
6244 int gpm_idx = -1;
6245# endif
6246# ifdef USE_XSMP
6247 int xsmp_idx = -1;
6248# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006249 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006251# ifdef FEAT_MZSCHEME
6252 mzvim_check_threads();
6253 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6254 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006255 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006256 mzquantum_used = TRUE;
6257 }
6258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 fds[0].fd = fd;
6260 fds[0].events = POLLIN;
6261 nfd = 1;
6262
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006264 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 if (xterm_Shell != (Widget)0)
6266 {
6267 xterm_idx = nfd;
6268 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6269 fds[nfd].events = POLLIN;
6270 nfd++;
6271 }
6272# endif
6273# ifdef FEAT_MOUSE_GPM
6274 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6275 {
6276 gpm_idx = nfd;
6277 fds[nfd].fd = gpm_fd;
6278 fds[nfd].events = POLLIN;
6279 nfd++;
6280 }
6281# endif
6282# ifdef USE_XSMP
6283 if (xsmp_icefd != -1)
6284 {
6285 xsmp_idx = nfd;
6286 fds[nfd].fd = xsmp_icefd;
6287 fds[nfd].events = POLLIN;
6288 nfd++;
6289 }
6290# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006291#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006292 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006293#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006294 if (interrupted != NULL)
6295 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006297 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006298
6299 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006300 if (result == 0 && interrupted != NULL && ret > 0)
6301 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006302
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006303# ifdef FEAT_MZSCHEME
6304 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006305 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006306 finished = FALSE;
6307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309# ifdef FEAT_XCLIPBOARD
6310 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6311 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006312 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006314 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 finished = FALSE;
6316 }
6317# endif
6318# ifdef FEAT_MOUSE_GPM
6319 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321# endif
6322# ifdef USE_XSMP
6323 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6324 {
6325 if (fds[xsmp_idx].revents & POLLIN)
6326 {
6327 busy = TRUE;
6328 xsmp_handle_requests();
6329 busy = FALSE;
6330 }
6331 else if (fds[xsmp_idx].revents & POLLHUP)
6332 {
6333 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006334 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 xsmp_close();
6336 }
6337 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006338 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 }
6340# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006341#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006342 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006343 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006344 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
Bram Moolenaar0f873732019-12-05 20:28:46 +01006347#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348
6349 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006350 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006351 // These are static because they can take 8 Kbyte each and cause the
6352 // signal stack to run out with -O3.
6353 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006355 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006357# ifdef FEAT_MZSCHEME
6358 mzvim_check_threads();
6359 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6360 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006361 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006362 mzquantum_used = TRUE;
6363 }
6364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006366 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006368 tv.tv_sec = towait / 1000;
6369 tv.tv_usec = (towait % 1000) * (1000000/1000);
6370 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006372 else
6373 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374
6375 /*
6376 * Select on ready for reading and exceptional condition (end of file).
6377 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006378select_eintr:
6379 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006380 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 FD_ZERO(&efds);
6382 FD_SET(fd, &rfds);
K.Takatab247e062022-02-07 10:45:23 +00006383# ifndef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006384 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 FD_SET(fd, &efds);
6386# endif
6387 maxfd = fd;
6388
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006390 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 if (xterm_Shell != (Widget)0)
6392 {
6393 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6394 if (maxfd < ConnectionNumber(xterm_dpy))
6395 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006396
Bram Moolenaar0f873732019-12-05 20:28:46 +01006397 // An event may have already been read but not handled. In
6398 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006399 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 }
6401# endif
6402# ifdef FEAT_MOUSE_GPM
6403 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6404 {
6405 FD_SET(gpm_fd, &rfds);
6406 FD_SET(gpm_fd, &efds);
6407 if (maxfd < gpm_fd)
6408 maxfd = gpm_fd;
6409 }
6410# endif
6411# ifdef USE_XSMP
6412 if (xsmp_icefd != -1)
6413 {
6414 FD_SET(xsmp_icefd, &rfds);
6415 FD_SET(xsmp_icefd, &efds);
6416 if (maxfd < xsmp_icefd)
6417 maxfd = xsmp_icefd;
6418 }
6419# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006420# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006421 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006422# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006423 if (interrupted != NULL)
6424 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425
Bram Moolenaar643b6142018-09-12 20:29:09 +02006426 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6427 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006428 result = ret > 0 && FD_ISSET(fd, &rfds);
6429 if (result)
6430 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006431 else if (interrupted != NULL && ret > 0)
6432 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006433
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006434# ifdef EINTR
6435 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006436 {
dbivolaruab16ad32021-12-29 19:41:47 +00006437 // Check whether the EINTR is caused by SIGTSTP
6438 if (got_tstp && !in_mch_suspend)
6439 {
6440 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006441
dbivolaruab16ad32021-12-29 19:41:47 +00006442 ea.forceit = TRUE;
6443 ex_stop(&ea);
6444 got_tstp = FALSE;
6445 }
6446
Bram Moolenaar0f873732019-12-05 20:28:46 +01006447 // Check whether window has been resized, EINTR may be caused by
6448 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006449 if (do_resize)
6450 handle_resize();
6451
Bram Moolenaar0f873732019-12-05 20:28:46 +01006452 // Interrupted by a signal, need to try again. We ignore msec
6453 // here, because we do want to check even after a timeout if
6454 // characters are available. Needed for reading output of an
6455 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006456 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006457 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006458# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006459# ifdef __TANDEM
6460 if (ret == -1 && errno == ENOTSUP)
6461 {
6462 FD_ZERO(&rfds);
6463 FD_ZERO(&efds);
6464 ret = 0;
6465 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006466# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006467# ifdef FEAT_MZSCHEME
6468 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006469 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006470 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471# endif
6472
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473# ifdef FEAT_XCLIPBOARD
6474 if (ret > 0 && xterm_Shell != (Widget)0
6475 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6476 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006477 xterm_update(); // Maybe we should hand out clipboard
6478 // continue looping when we only got the X event and the input
6479 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 if (--ret == 0 && !input_available())
6481 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006482 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 finished = FALSE;
6484 }
6485 }
6486# endif
6487# ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00006488 if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 {
6490 if (FD_ISSET(gpm_fd, &efds))
6491 gpm_close();
6492 else if (FD_ISSET(gpm_fd, &rfds))
6493 *check_for_gpm = 1;
6494 }
6495# endif
6496# ifdef USE_XSMP
6497 if (ret > 0 && xsmp_icefd != -1)
6498 {
6499 if (FD_ISSET(xsmp_icefd, &efds))
6500 {
6501 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006502 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 xsmp_close();
6504 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006505 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 }
6507 else if (FD_ISSET(xsmp_icefd, &rfds))
6508 {
6509 busy = TRUE;
6510 xsmp_handle_requests();
6511 busy = FALSE;
6512 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006513 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 }
6515 }
6516# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006517#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006518 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006519 if (ret >= 0)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01006520 (void)channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522
Bram Moolenaar0f873732019-12-05 20:28:46 +01006523#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524
6525#ifdef MAY_LOOP
6526 if (finished || msec == 0)
6527 break;
6528
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006529# ifdef FEAT_CLIENTSERVER
6530 if (server_waiting())
6531 break;
6532# endif
6533
Bram Moolenaar0f873732019-12-05 20:28:46 +01006534 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 if (msec > 0)
6536 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006537# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006538 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006539 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006541 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 msec = msec / 2;
6543# endif
6544 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006545 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 }
6547#endif
6548 }
6549
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006550 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551}
6552
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006554 * Expand a path into all matching files and/or directories. Handles "*",
6555 * "?", "[a-z]", "**", etc.
6556 * "path" has backslashes before chars that are not to be expanded.
6557 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 */
6559 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006560mch_expandpath(
6561 garray_T *gap,
6562 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006563 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564{
Bram Moolenaar02743632005-07-25 20:42:36 +00006565 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
6568/*
6569 * mch_expand_wildcards() - this code does wild-card pattern matching using
6570 * the shell
6571 *
6572 * return OK for success, FAIL for error (you may lose some memory) and put
6573 * an error message in *file.
6574 *
6575 * num_pat is number of input patterns
6576 * pat is array of pointers to input patterns
6577 * num_file is pointer to number of matched file names
6578 * file is pointer to array of pointers to matched file names
6579 */
6580
6581#ifndef SEEK_SET
6582# define SEEK_SET 0
6583#endif
6584#ifndef SEEK_END
6585# define SEEK_END 2
6586#endif
6587
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006588#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006589
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006591mch_expand_wildcards(
6592 int num_pat,
6593 char_u **pat,
6594 int *num_file,
6595 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006596 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597{
6598 int i;
6599 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006600 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 char_u *p;
6602 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603
Bram Moolenaarc7247912008-01-13 12:54:11 +00006604 /*
6605 * This is the non-OS/2 implementation (really Unix).
6606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 int j;
6608 char_u *tempname;
6609 char_u *command;
6610 FILE *fd;
6611 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006612#define STYLE_ECHO 0 // use "echo", the default
6613#define STYLE_GLOB 1 // use "glob", for csh
6614#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6615#define STYLE_PRINT 3 // use "print -N", for zsh
6616#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6617 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 int shell_style = STYLE_ECHO;
6619 int check_spaces;
6620 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006621 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006622 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006623 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
Bram Moolenaar0f873732019-12-05 20:28:46 +01006625 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 *file = NULL;
6627
6628 /*
6629 * If there are no wildcards, just copy the names to allocated memory.
6630 * Saves a lot of time, because we don't have to start a new shell.
6631 */
6632 if (!have_wildcard(num_pat, pat))
6633 return save_patterns(num_pat, pat, num_file, file);
6634
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006635# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006636 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006637 if (sandbox != 0 && check_secure())
6638 return FAIL;
6639# endif
6640
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 /*
6642 * Don't allow the use of backticks in secure and restricted mode.
6643 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006644 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 for (i = 0; i < num_pat; ++i)
6646 if (vim_strchr(pat[i], '`') != NULL
6647 && (check_restricted() || check_secure()))
6648 return FAIL;
6649
6650 /*
6651 * get a name for the temp file
6652 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006653 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006655 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 return FAIL;
6657 }
6658
6659 /*
6660 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006661 * file.
6662 * STYLE_BT: NL separated
6663 * If expanding `cmd` execute it directly.
6664 * STYLE_GLOB: NUL separated
6665 * If we use *csh, "glob" will work better than "echo".
6666 * STYLE_PRINT: NL or NUL separated
6667 * If we use *zsh, "print -N" will work better than "glob".
6668 * STYLE_VIMGLOB: NL separated
6669 * If we use *sh*, we define "vimglob()".
6670 * STYLE_ECHO: space separated.
6671 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 */
6673 if (num_pat == 1 && *pat[0] == '`'
6674 && (len = STRLEN(pat[0])) > 2
6675 && *(pat[0] + len - 1) == '`')
6676 shell_style = STYLE_BT;
6677 else if ((len = STRLEN(p_sh)) >= 3)
6678 {
6679 if (STRCMP(p_sh + len - 3, "csh") == 0)
6680 shell_style = STYLE_GLOB;
6681 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6682 shell_style = STYLE_PRINT;
6683 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006684 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6685 "sh") != NULL)
6686 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687
Bram Moolenaar0f873732019-12-05 20:28:46 +01006688 // Compute the length of the command. We need 2 extra bytes: for the
6689 // optional '&' and for the NUL.
6690 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006692 if (shell_style == STYLE_VIMGLOB)
6693 len += STRLEN(sh_vimglob_func);
6694
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006695 for (i = 0; i < num_pat; ++i)
6696 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006697 // Count the length of the patterns in the same way as they are put in
6698 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006699#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006700 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006701#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006702 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006703 for (j = 0; pat[i][j] != NUL; ++j)
6704 {
6705 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006706 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006707 ++len;
6708 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006709#endif
6710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 command = alloc(len);
6712 if (command == NULL)
6713 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006714 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 vim_free(tempname);
6716 return FAIL;
6717 }
6718
6719 /*
6720 * Build the shell command:
6721 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6722 * recognizes this).
6723 * - Add the shell command to print the expanded names.
6724 * - Add the temp file name.
6725 * - Add the file name patterns.
6726 */
6727 if (shell_style == STYLE_BT)
6728 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006729 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006730 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006731 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006733 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006734 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006736 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006738 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 *p = ' ';
6740 }
6741 STRCAT(command, ">");
6742 }
6743 else
6744 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006745 STRCPY(command, "");
6746 if (shell_style == STYLE_GLOB)
6747 {
6748 // Assume the nonomatch option is valid only for csh like shells,
6749 // otherwise, this may set the positional parameters for the shell,
6750 // e.g. "$*".
6751 if (flags & EW_NOTFOUND)
6752 STRCAT(command, "set nonomatch; ");
6753 else
6754 STRCAT(command, "unset nonomatch; ");
6755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 if (shell_style == STYLE_GLOB)
6757 STRCAT(command, "glob >");
6758 else if (shell_style == STYLE_PRINT)
6759 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006760 else if (shell_style == STYLE_VIMGLOB)
6761 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 else
6763 STRCAT(command, "echo >");
6764 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006765
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006767
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 if (shell_style != STYLE_BT)
6769 for (i = 0; i < num_pat; ++i)
6770 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006771 // When using system() always add extra quotes, because the shell
6772 // is started twice. Otherwise put a backslash before special
6773 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774#ifdef USE_SYSTEM
6775 STRCAT(command, " \"");
6776 STRCAT(command, pat[i]);
6777 STRCAT(command, "\"");
6778#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006779 int intick = FALSE;
6780
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 p = command + STRLEN(command);
6782 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006783 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006784 {
6785 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006786 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006787 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6788 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006789 // Remove a backslash, take char literally. But keep
6790 // backslash inside backticks, before a special character
6791 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006792 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006793 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6794 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006795 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006796 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006797 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006798 else if (!intick
6799 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6800 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006801 // Put a backslash before a special character, but not
6802 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6803 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006804 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006805
Bram Moolenaar0f873732019-12-05 20:28:46 +01006806 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006807 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 *p = NUL;
6810#endif
6811 }
6812 if (flags & EW_SILENT)
6813 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006814 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006815 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
6817 /*
6818 * Using zsh -G: If a pattern has no matches, it is just deleted from
6819 * the argument list, otherwise zsh gives an error message and doesn't
6820 * expand any other pattern.
6821 */
6822 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006823 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824
6825 /*
6826 * If we use -f then shell variables set in .cshrc won't get expanded.
6827 * vi can do it, so we will too, but it is only necessary if there is a "$"
6828 * in one of the patterns, otherwise we can still use the fast option.
6829 */
6830 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006831 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832
6833 /*
6834 * execute the shell command
6835 */
6836 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6837
Bram Moolenaar0f873732019-12-05 20:28:46 +01006838 // When running in the background, give it some time to create the temp
6839 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006840 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006841 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842
Bram Moolenaar0f873732019-12-05 20:28:46 +01006843 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 show_shell_mess = TRUE;
6845 vim_free(command);
6846
Bram Moolenaar0f873732019-12-05 20:28:46 +01006847 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {
6849 mch_remove(tempname);
6850 vim_free(tempname);
6851 /*
6852 * With interactive completion, the error message is not printed.
6853 * However with USE_SYSTEM, I don't know how to turn off error messages
6854 * from the shell, so screen may still get messed up -- webb.
6855 */
6856#ifndef USE_SYSTEM
6857 if (!(flags & EW_SILENT))
6858#endif
6859 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006860 redraw_later_clear(); // probably messed up screen
6861 msg_putchar('\n'); // clear bottom line quickly
6862 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863#ifdef USE_SYSTEM
6864 if (!(flags & EW_SILENT))
6865#endif
6866 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006867 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006868 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 }
6870 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006871 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6872 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 if (shell_style == STYLE_BT)
6874 return FAIL;
6875 goto notfound;
6876 }
6877
6878 /*
6879 * read the names from the file into memory
6880 */
6881 fd = fopen((char *)tempname, READBIN);
6882 if (fd == NULL)
6883 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006884 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 if (!(flags & EW_SILENT))
6886 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006887 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006888 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 }
6890 vim_free(tempname);
6891 goto notfound;
6892 }
6893 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006894 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006896 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006897 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006898 buffer = NULL;
6899 else
6900 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 if (buffer == NULL)
6902 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006903 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 mch_remove(tempname);
6905 vim_free(tempname);
6906 fclose(fd);
6907 return FAIL;
6908 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006909 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 i = fread((char *)buffer, 1, len, fd);
6911 fclose(fd);
6912 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006913 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006915 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006916 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 vim_free(tempname);
6918 vim_free(buffer);
6919 return FAIL;
6920 }
6921 vim_free(tempname);
6922
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006923# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006924 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006926 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6928 *p++ = buffer[i];
6929 len = p - buffer;
6930# endif
6931
6932
Bram Moolenaar0f873732019-12-05 20:28:46 +01006933 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 if (shell_style == STYLE_ECHO)
6935 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006936 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006938 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 {
6940 while (*p != ' ' && *p != '\n')
6941 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006942 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
6944 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006945 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006946 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006948 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006950 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {
6952 while (*p != '\n' && *p != NUL)
6953 ++p;
6954 if (*p != NUL)
6955 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006956 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 }
6958 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006959 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 else
6961 {
6962 /*
6963 * Some versions of zsh use spaces instead of NULs to separate
6964 * results. Only do this when there is no NUL before the end of the
6965 * buffer, otherwise we would never be able to use file names with
6966 * embedded spaces when zsh does use NULs.
6967 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6968 * don't check for spaces again.
6969 */
6970 check_spaces = FALSE;
6971 if (shell_style == STYLE_PRINT && !did_find_nul)
6972 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006973 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006974 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006975 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 did_find_nul = TRUE;
6977 else
6978 check_spaces = TRUE;
6979 }
6980
6981 /*
6982 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6983 * already is one, for STYLE_GLOB it needs to be added.
6984 */
6985 if (len && buffer[len - 1] == NUL)
6986 --len;
6987 else
6988 buffer[len] = NUL;
6989 i = 0;
6990 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006991 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {
6993 ++i;
6994 *p = NUL;
6995 }
6996 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006997 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 }
6999 if (i == 0)
7000 {
7001 /*
7002 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
7003 * /bin/sh will happily expand it to nothing rather than returning an
7004 * error; and hey, it's good to check anyway -- webb.
7005 */
7006 vim_free(buffer);
7007 goto notfound;
7008 }
7009 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007010 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 if (*file == NULL)
7012 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007013 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 vim_free(buffer);
7015 return FAIL;
7016 }
7017
7018 /*
7019 * Isolate the individual file names.
7020 */
7021 p = buffer;
7022 for (i = 0; i < *num_file; ++i)
7023 {
7024 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007025 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00007026 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
7027 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00007029 while (!(shell_style == STYLE_ECHO && *p == ' ')
7030 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007032 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 *p = NUL;
7034 else
7035 {
7036 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007037 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 }
7039 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007040 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007042 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007044 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 }
7046 }
7047
7048 /*
7049 * Move the file names to allocated memory.
7050 */
7051 for (j = 0, i = 0; i < *num_file; ++i)
7052 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007053 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7055 continue;
7056
Bram Moolenaar0f873732019-12-05 20:28:46 +01007057 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 dir = (mch_isdir((*file)[i]));
7059 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7060 continue;
7061
Bram Moolenaar0f873732019-12-05 20:28:46 +01007062 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007063 if (!dir && (flags & EW_EXEC)
7064 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007065 continue;
7066
Bram Moolenaar964b3742019-05-24 18:54:09 +02007067 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 if (p)
7069 {
7070 STRCPY(p, (*file)[i]);
7071 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007072 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 (*file)[j++] = p;
7074 }
7075 }
7076 vim_free(buffer);
7077 *num_file = j;
7078
Bram Moolenaar0f873732019-12-05 20:28:46 +01007079 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007081 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 goto notfound;
7083 }
7084
7085 return OK;
7086
7087notfound:
7088 if (flags & EW_NOTFOUND)
7089 return save_patterns(num_pat, pat, num_file, file);
7090 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091}
7092
Bram Moolenaar0f873732019-12-05 20:28:46 +01007093#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007096save_patterns(
7097 int num_pat,
7098 char_u **pat,
7099 int *num_file,
7100 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101{
7102 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007103 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007105 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 if (*file == NULL)
7107 return FAIL;
7108 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007109 {
7110 s = vim_strsave(pat[i]);
7111 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007112 // Be compatible with expand_filename(): halve the number of
7113 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007114 backslash_halve(s);
7115 (*file)[i] = s;
7116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 *num_file = num_pat;
7118 return OK;
7119}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121/*
7122 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7123 * expand.
7124 */
7125 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007126mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007128 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 if (*p == '\\' && p[1] != NUL)
7131 ++p;
7132 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 if (vim_strchr((char_u *)
7134#ifdef VMS
7135 "*?%"
7136#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138#endif
7139 , *p) != NULL)
7140 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 }
7142 return FALSE;
7143}
7144
7145/*
7146 * Return TRUE if the string "p" contains a wildcard.
7147 * Don't recognize '~' at the end as a wildcard.
7148 */
7149 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007150mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007152 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 if (*p == '\\' && p[1] != NUL)
7155 ++p;
7156 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 if (vim_strchr((char_u *)
7158#ifdef VMS
7159 "*?%$"
7160#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162#endif
7163 , *p) != NULL
7164 || (*p == '~' && p[1] != NUL))
7165 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 }
7167 return FALSE;
7168}
7169
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007171have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
7173 int i;
7174
7175 for (i = 0; i < num; i++)
7176 if (mch_has_wildcard(file[i]))
7177 return 1;
7178 return 0;
7179}
7180
7181 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007182have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183{
7184 int i;
7185
7186 for (i = 0; i < num; i++)
7187 if (vim_strchr(file[i], '$') != NULL)
7188 return TRUE;
7189 return FALSE;
7190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007192#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193/*
7194 * Scaled-down version of rename(), which is missing in Xenix.
7195 * This version can only move regular files and will fail if the
7196 * destination exists.
7197 */
7198 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007199mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 struct stat st;
7202
Bram Moolenaar0f873732019-12-05 20:28:46 +01007203 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007205 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007207 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 return 0;
7209 return -1;
7210}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007211#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007213#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007214# if defined(DYNAMIC_GPM) || defined(PROTO)
7215/*
7216 * Initialize Gpm's symbols for dynamic linking.
7217 * Must be called only if libgpm_hinst is NULL.
7218 */
7219 static int
7220load_libgpm(void)
7221{
7222 libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL);
7223
7224 if (libgpm_hinst == NULL)
7225 {
7226 if (p_verbose > 0)
7227 smsg_attr(HL_ATTR(HLF_W),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007228 _("Could not load gpm library: %s"), dlerror());
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007229 return FAIL;
7230 }
7231
7232 if (
7233 (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL
7234 || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL
7235 || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL
7236 || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL
7237 || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL
7238 )
7239 {
7240 semsg(_(e_could_not_load_library_str_str), "gpm", dlerror());
7241 dlclose(libgpm_hinst);
7242 libgpm_hinst = NULL;
7243 dll_gpm_flag = NULL;
7244 dll_gpm_fd = NULL;
7245 return FAIL;
7246 }
7247 return OK;
7248}
7249
7250 int
7251gpm_available(void)
7252{
7253 return libgpm_hinst != NULL || load_libgpm() == OK;
7254}
7255# endif // DYNAMIC_GPM
7256
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257/*
7258 * Initializes connection with gpm (if it isn't already opened)
7259 * Return 1 if succeeded (or connection already opened), 0 if failed
7260 */
7261 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007262gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007264 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007266#ifdef DYNAMIC_GPM
7267 if (!gpm_available())
7268 return 0;
7269#endif
7270
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007271 if (gpm_flag)
7272 return 1; // already open
7273
7274 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7275 gpm_connect.defaultMask = ~GPM_HARD;
7276 // Default handling for mouse move
7277 gpm_connect.minMod = 0; // Handle any modifier keys
7278 gpm_connect.maxMod = 0xffff;
7279 if (Gpm_Open(&gpm_connect, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007281 // gpm library tries to handling TSTP causes
7282 // problems. Anyways, we close connection to Gpm whenever
7283 // we are going to suspend or starting an external process
7284 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007285# ifdef SIGTSTP
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007286 signal(SIGTSTP, restricted ? SIG_IGN : (void (*)())sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007287# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007288 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007290 if (gpm_fd == -2)
7291 Gpm_Close(); // We don't want to talk to xterm via gpm
7292 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293}
7294
7295/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007296 * Returns TRUE if the GPM mouse is enabled.
7297 */
7298 int
7299gpm_enabled(void)
7300{
7301 return gpm_flag && gpm_fd >= 0;
7302}
7303
7304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 */
7307 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007308gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007310 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 Gpm_Close();
7312}
7313
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007314/*
7315 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007317 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 */
7319 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007320mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
7322 int button;
7323 static Gpm_Event gpm_event;
7324 char_u string[6];
7325 int_u vim_modifiers;
7326 int row,col;
7327 unsigned char buttons_mask;
7328 unsigned char gpm_modifiers;
7329 static unsigned char old_buttons = 0;
7330
7331 Gpm_GetEvent(&gpm_event);
7332
7333#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007334 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 if (hold_gui_events)
7336 return 0;
7337#endif
7338
7339 row = gpm_event.y - 1;
7340 col = gpm_event.x - 1;
7341
Bram Moolenaar0f873732019-12-05 20:28:46 +01007342 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 string[1] = 'M';
7344 string[2] = 'G';
7345 switch (GPM_BARE_EVENTS(gpm_event.type))
7346 {
7347 case GPM_DRAG:
7348 string[3] = MOUSE_DRAG;
7349 break;
7350 case GPM_DOWN:
7351 buttons_mask = gpm_event.buttons & ~old_buttons;
7352 old_buttons = gpm_event.buttons;
7353 switch (buttons_mask)
7354 {
7355 case GPM_B_LEFT:
7356 button = MOUSE_LEFT;
7357 break;
7358 case GPM_B_MIDDLE:
7359 button = MOUSE_MIDDLE;
7360 break;
7361 case GPM_B_RIGHT:
7362 button = MOUSE_RIGHT;
7363 break;
7364 default:
7365 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007366 // Don't know what to do. Can more than one button be
7367 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 }
7369 string[3] = (char_u)(button | 0x20);
7370 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7371 break;
7372 case GPM_UP:
7373 string[3] = MOUSE_RELEASE;
7374 old_buttons &= ~gpm_event.buttons;
7375 break;
7376 default:
7377 return 0;
7378 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007379 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 gpm_modifiers = gpm_event.modifiers;
7381 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007382 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7383 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7384 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7386 vim_modifiers |= MOUSE_SHIFT;
7387
7388 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7389 vim_modifiers |= MOUSE_CTRL;
7390 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7391 vim_modifiers |= MOUSE_ALT;
7392 string[3] |= vim_modifiers;
7393 string[4] = (char_u)(col + ' ' + 1);
7394 string[5] = (char_u)(row + ' ' + 1);
7395 add_to_input_buf(string, 6);
7396 return 6;
7397}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007398#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007400#ifdef FEAT_SYSMOUSE
7401/*
7402 * Initialize connection with sysmouse.
7403 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7404 * output, any sysmouse output than will be processed via sig_sysmouse().
7405 * Return OK if succeeded, FAIL if failed.
7406 */
7407 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007408sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007409{
7410 struct mouse_info mouse;
7411
7412 mouse.operation = MOUSE_MODE;
7413 mouse.u.mode.mode = 0;
7414 mouse.u.mode.signal = SIGUSR2;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007415 if (ioctl(1, CONS_MOUSECTL, &mouse) == -1)
7416 return FAIL;
7417
7418 signal(SIGUSR2, (void (*)())sig_sysmouse);
7419 mouse.operation = MOUSE_SHOW;
7420 ioctl(1, CONS_MOUSECTL, &mouse);
7421 return OK;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007422}
7423
7424/*
7425 * Stop processing SIGUSR2 signals, and also make sure that
7426 * virtual console do not send us any sysmouse related signal.
7427 */
7428 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007429sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007430{
7431 struct mouse_info mouse;
7432
7433 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7434 mouse.operation = MOUSE_MODE;
7435 mouse.u.mode.mode = 0;
7436 mouse.u.mode.signal = 0;
7437 ioctl(1, CONS_MOUSECTL, &mouse);
7438}
7439
7440/*
7441 * Gets info from sysmouse and adds special keys to input buf.
7442 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007443 static void
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007444sig_sysmouse SIGDEFARG(sigarg)
7445{
7446 struct mouse_info mouse;
7447 struct video_info video;
7448 char_u string[6];
7449 int row, col;
7450 int button;
7451 int buttons;
7452 static int oldbuttons = 0;
7453
7454#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007455 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007456 if (hold_gui_events)
7457 return;
7458#endif
7459
7460 mouse.operation = MOUSE_GETINFO;
7461 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7462 && ioctl(1, FBIO_MODEINFO, &video) != -1
7463 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7464 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7465 {
7466 row = mouse.u.data.y / video.vi_cheight;
7467 col = mouse.u.data.x / video.vi_cwidth;
7468 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007469 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007470 string[1] = 'M';
7471 string[2] = 'S';
7472 if (oldbuttons == buttons && buttons != 0)
7473 {
7474 button = MOUSE_DRAG;
7475 }
7476 else
7477 {
7478 switch (buttons)
7479 {
7480 case 0:
7481 button = MOUSE_RELEASE;
7482 break;
7483 case 1:
7484 button = MOUSE_LEFT;
7485 break;
7486 case 2:
7487 button = MOUSE_MIDDLE;
7488 break;
7489 case 4:
7490 button = MOUSE_RIGHT;
7491 break;
7492 default:
7493 return;
7494 }
7495 oldbuttons = buttons;
7496 }
7497 string[3] = (char_u)(button);
7498 string[4] = (char_u)(col + ' ' + 1);
7499 string[5] = (char_u)(row + ' ' + 1);
7500 add_to_input_buf(string, 6);
7501 }
7502 return;
7503}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007504#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007505
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007507typedef char_u * (*STRPROCSTR)(char_u *);
7508typedef char_u * (*INTPROCSTR)(int);
7509typedef int (*STRPROCINT)(char_u *);
7510typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511
7512/*
7513 * Call a DLL routine which takes either a string or int param
7514 * and returns an allocated string.
7515 */
7516 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007517mch_libcall(
7518 char_u *libname,
7519 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007520 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007521 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007522 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007523 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524{
7525# if defined(USE_DLOPEN)
7526 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007527 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528# else
7529 shl_t hinstLib;
7530# endif
7531 STRPROCSTR ProcAdd;
7532 INTPROCSTR ProcAddI;
7533 char_u *retval_str = NULL;
7534 int retval_int = 0;
7535 int success = FALSE;
7536
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007537 /*
7538 * Get a handle to the DLL module.
7539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007541 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007542 (void)dlerror();
7543
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 hinstLib = dlopen((char *)libname, RTLD_LAZY
7545# ifdef RTLD_LOCAL
7546 | RTLD_LOCAL
7547# endif
7548 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007549 if (hinstLib == NULL)
7550 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007551 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007552 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007553 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007554 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556# else
7557 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7558# endif
7559
Bram Moolenaar0f873732019-12-05 20:28:46 +01007560 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 if (hinstLib != NULL)
7562 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007563# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 /*
7565 * Catch a crash when calling the library function. For example when
7566 * using a number where a string pointer is expected.
7567 */
7568 mch_startjmp();
7569 if (SETJMP(lc_jump_env) != 0)
7570 {
7571 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007572# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007573 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007574# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 mch_didjmp();
7576 }
7577 else
7578# endif
7579 {
7580 retval_str = NULL;
7581 retval_int = 0;
7582
7583 if (argstring != NULL)
7584 {
7585# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007586 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007587 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588# else
7589 if (shl_findsym(&hinstLib, (const char *)funcname,
7590 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7591 ProcAdd = NULL;
7592# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007593 if ((success = (ProcAdd != NULL
7594# if defined(USE_DLOPEN)
7595 && dlerr == NULL
7596# endif
7597 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 {
7599 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007600 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 else
7602 retval_str = (ProcAdd)(argstring);
7603 }
7604 }
7605 else
7606 {
7607# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007608 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007609 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610# else
7611 if (shl_findsym(&hinstLib, (const char *)funcname,
7612 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7613 ProcAddI = NULL;
7614# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007615 if ((success = (ProcAddI != NULL
7616# if defined(USE_DLOPEN)
7617 && dlerr == NULL
7618# endif
7619 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 {
7621 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007622 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 else
7624 retval_str = (ProcAddI)(argint);
7625 }
7626 }
7627
Bram Moolenaar0f873732019-12-05 20:28:46 +01007628 // Save the string before we free the library.
7629 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 if (string_result == NULL)
7631 *number_result = retval_int;
7632 else if (retval_str != NULL
7633 && retval_str != (char_u *)1
7634 && retval_str != (char_u *)-1)
7635 *string_result = vim_strsave(retval_str);
7636 }
7637
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007638# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 mch_endjmp();
7640# ifdef SIGHASARG
7641 if (lc_signal != 0)
7642 {
7643 int i;
7644
Bram Moolenaar0f873732019-12-05 20:28:46 +01007645 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 for (i = 0; signal_info[i].sig != -1; i++)
7647 if (lc_signal == signal_info[i].sig)
7648 break;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00007649 semsg(e_got_sig_str_in_libcall, signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 }
7651# endif
7652# endif
7653
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007655 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007656 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007657 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007658
Bram Moolenaar0f873732019-12-05 20:28:46 +01007659 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 (void)dlclose(hinstLib);
7661# else
7662 (void)shl_unload(hinstLib);
7663# endif
7664 }
7665
7666 if (!success)
7667 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007668 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 return FAIL;
7670 }
7671
7672 return OK;
7673}
7674#endif
7675
7676#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007677static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678static int xterm_button;
7679
7680/*
7681 * Setup a dummy window for X selections in a terminal.
7682 */
7683 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007684setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685{
7686 int z = 0;
7687 char *strp = "";
7688 Widget AppShell;
7689
7690 if (!x_connect_to_server())
7691 return;
7692
7693 open_app_context();
7694 if (app_context != NULL && xterm_Shell == (Widget)0)
7695 {
7696 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007697# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007699# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007700# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007701 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702
7703 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007704 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705# endif
7706
Bram Moolenaar0f873732019-12-05 20:28:46 +01007707 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 oldhandler = XSetErrorHandler(x_error_check);
7709
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007710# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007711 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7713 mch_startjmp();
7714 if (SETJMP(lc_jump_env) != 0)
7715 {
7716 mch_didjmp();
7717 xterm_dpy = NULL;
7718 }
7719 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {
7722 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7723 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007724 if (xterm_dpy != NULL)
7725 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007726# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007728# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 }
7730
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007731# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007732 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007734# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007735 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 (void)XSetErrorHandler(oldhandler);
7737
7738 if (xterm_dpy == NULL)
7739 {
7740 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007741 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 return;
7743 }
7744
Bram Moolenaar0f873732019-12-05 20:28:46 +01007745 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 (void)XSetIOErrorHandler(x_IOerror_handler);
7747
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007748# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007750 {
7751 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007752 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007753 verbose_leave();
7754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755# endif
7756
Bram Moolenaar0f873732019-12-05 20:28:46 +01007757 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7759 applicationShellWidgetClass, xterm_dpy,
7760 NULL);
7761 if (AppShell == (Widget)0)
7762 return;
7763 xterm_Shell = XtVaCreatePopupShell("VIM",
7764 topLevelShellWidgetClass, AppShell,
7765 XtNmappedWhenManaged, 0,
7766 XtNwidth, 1,
7767 XtNheight, 1,
7768 NULL);
7769 if (xterm_Shell == (Widget)0)
7770 return;
7771
7772 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007773 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 if (x11_display == NULL)
7775 x11_display = xterm_dpy;
7776
7777 XtRealizeWidget(xterm_Shell);
7778 XSync(xterm_dpy, False);
7779 xterm_update();
7780 }
7781 if (xterm_Shell != (Widget)0)
7782 {
7783 clip_init(TRUE);
7784 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7785 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007786 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 if (test_x11_window(xterm_dpy) == FAIL)
7788 x11_window = 0;
7789 if (x11_window != 0)
7790 xterm_trace = 0;
7791 }
7792}
7793
7794 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007795start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
7797 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7798 return;
7799 xterm_trace = 1;
7800 xterm_button = button;
7801 do_xterm_trace();
7802}
7803
7804
7805 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007806stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
7808 if (xterm_trace < 0)
7809 return;
7810 xterm_trace = 0;
7811}
7812
7813/*
7814 * Query the xterm pointer and generate mouse termcodes if necessary
7815 * return TRUE if dragging is active, else FALSE
7816 */
7817 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007818do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820 Window root, child;
7821 int root_x, root_y;
7822 int win_x, win_y;
7823 int row, col;
7824 int_u mask_return;
7825 char_u buf[50];
7826 char_u *strp;
7827 long got_hints;
7828 static char_u *mouse_code;
7829 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7830 static int prev_row = 0, prev_col = 0;
7831 static XSizeHints xterm_hints;
7832
7833 if (xterm_trace <= 0)
7834 return FALSE;
7835
7836 if (xterm_trace == 1)
7837 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007838 // Get the hints just before tracking starts. The font size might
7839 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007840 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7841 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 || xterm_hints.width_inc <= 1
7843 || xterm_hints.height_inc <= 1)
7844 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007845 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 return FALSE;
7847 }
7848
Bram Moolenaar0f873732019-12-05 20:28:46 +01007849 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 mouse_code = find_termcode(mouse_name);
7851 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007852 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 xterm_trace = 2;
7854
Bram Moolenaar0f873732019-12-05 20:28:46 +01007855 // Find the offset of the chars, there might be a scrollbar on the
7856 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7858 &win_x, &win_y, &mask_return);
7859 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7860 - (xterm_hints.height_inc / 2);
7861 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7862 xterm_hints.y = 2;
7863 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7864 - (xterm_hints.width_inc / 2);
7865 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7866 xterm_hints.x = 2;
7867 return TRUE;
7868 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007869 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {
7871 xterm_trace = 0;
7872 return FALSE;
7873 }
7874
7875 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7876 &win_x, &win_y, &mask_return);
7877
7878 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7879 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7880 if (row == prev_row && col == prev_col)
7881 return TRUE;
7882
7883 STRCPY(buf, mouse_code);
7884 strp = buf + STRLEN(buf);
7885 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7886 *strp++ = (char_u)(col + ' ' + 1);
7887 *strp++ = (char_u)(row + ' ' + 1);
7888 *strp = 0;
7889 add_to_input_buf(buf, STRLEN(buf));
7890
7891 prev_row = row;
7892 prev_col = col;
7893 return TRUE;
7894}
7895
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007896# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897/*
7898 * Destroy the display, window and app_context. Required for GTK.
7899 */
7900 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007901clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
7903 if (xterm_Shell != (Widget)0)
7904 {
7905 XtDestroyWidget(xterm_Shell);
7906 xterm_Shell = (Widget)0;
7907 }
7908 if (xterm_dpy != NULL)
7909 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007910# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007911 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 if (x11_display == xterm_dpy)
7915 x11_display = NULL;
7916 xterm_dpy = NULL;
7917 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007918# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 if (app_context != (XtAppContext)NULL)
7920 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007921 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 XtDestroyApplicationContext(app_context);
7923 app_context = (XtAppContext)NULL;
7924 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926}
7927# endif
7928
7929/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007930 * Catch up with GUI or X events.
7931 */
7932 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007933clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007934{
7935# ifdef FEAT_GUI
7936 if (gui.in_use)
7937 gui_mch_update();
7938 else
7939# endif
7940 if (xterm_Shell != (Widget)0)
7941 xterm_update();
7942}
7943
7944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 * Catch up with any queued X events. This may put keyboard input into the
7946 * input buffer, call resize call-backs, trigger timers etc. If there is
7947 * nothing in the X event queue (& no timers pending), then we return
7948 * immediately.
7949 */
7950 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007951xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952{
7953 XEvent event;
7954
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007955 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007957 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007959 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007960 break;
7961
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007962 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007963 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007964 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007965 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007966#ifdef FEAT_CLIENTSERVER
7967 {
7968 XPropertyEvent *e = (XPropertyEvent *)&event;
7969
7970 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007972 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007975 XtDispatchEvent(&event);
7976 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007977 else
7978 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007979 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007980 XtAppProcessEvent(app_context, mask);
7981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 }
7983}
7984
7985 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007986clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988 if (xterm_Shell != (Widget)0)
7989 return clip_x11_own_selection(xterm_Shell, cbd);
7990 return FAIL;
7991}
7992
7993 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007994clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995{
7996 if (xterm_Shell != (Widget)0)
7997 clip_x11_lose_selection(xterm_Shell, cbd);
7998}
7999
8000 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008001clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002{
8003 if (xterm_Shell != (Widget)0)
8004 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
8005}
8006
8007 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008008clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009{
8010 clip_x11_set_selection(cbd);
8011}
8012#endif
8013
8014
8015#if defined(USE_XSMP) || defined(PROTO)
8016/*
8017 * Code for X Session Management Protocol.
8018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
8020# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021/*
8022 * This is our chance to ask the user if they want to save,
8023 * or abort the logout
8024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008026xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027{
Bram Moolenaare1004402020-10-24 20:49:43 +02008028 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 int cancel_shutdown = False;
8030
Bram Moolenaare1004402020-10-24 20:49:43 +02008031 save_cmod_flags = cmdmod.cmod_flags;
8032 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01008033 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01008034 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02008036 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01008037 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 out_flush();
8039
Bram Moolenaar0f873732019-12-05 20:28:46 +01008040 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 SmcInteractDone(smc_conn, cancel_shutdown);
8042
Bram Moolenaar0f873732019-12-05 20:28:46 +01008043 // Finish off
8044 // Only end save-yourself here if we're not cancelling shutdown;
8045 // we'll get a cancelled callback later in which we'll end it.
8046 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 if (!cancel_shutdown)
8048 {
8049 xsmp.save_yourself = False;
8050 SmcSaveYourselfDone(smc_conn, True);
8051 }
8052}
8053# endif
8054
8055/*
8056 * Callback that starts save-yourself.
8057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008059xsmp_handle_save_yourself(
8060 SmcConn smc_conn,
8061 SmPointer client_data UNUSED,
8062 int save_type UNUSED,
8063 Bool shutdown,
8064 int interact_style UNUSED,
8065 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008067 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 if (xsmp.save_yourself)
8069 SmcSaveYourselfDone(smc_conn, True);
8070 xsmp.save_yourself = True;
8071 xsmp.shutdown = shutdown;
8072
Bram Moolenaar0f873732019-12-05 20:28:46 +01008073 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01008075 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076
8077 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008078 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079
8080# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008081 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008083 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 SmcInteractRequest(smc_conn, SmDialogError,
8085 xsmp_handle_interaction, client_data);
8086 else
8087# endif
8088 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008089 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 SmcSaveYourselfDone(smc_conn, True);
8091 xsmp.save_yourself = False;
8092 }
8093}
8094
8095
8096/*
8097 * Callback to warn us of imminent death.
8098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008100xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101{
8102 xsmp_close();
8103
Bram Moolenaar0f873732019-12-05 20:28:46 +01008104 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 getout_preserve_modified(0);
8106}
8107
8108
8109/*
8110 * Callback to tell us that save-yourself has completed.
8111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008113xsmp_save_complete(
8114 SmcConn smc_conn UNUSED,
8115 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116{
8117 xsmp.save_yourself = False;
8118}
8119
8120
8121/*
8122 * Callback to tell us that an instigated shutdown was cancelled
8123 * (maybe even by us)
8124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008126xsmp_shutdown_cancelled(
8127 SmcConn smc_conn,
8128 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129{
8130 if (xsmp.save_yourself)
8131 SmcSaveYourselfDone(smc_conn, True);
8132 xsmp.save_yourself = False;
8133 xsmp.shutdown = False;
8134}
8135
8136
8137/*
8138 * Callback to tell us that a new ICE connection has been established.
8139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008141xsmp_ice_connection(
8142 IceConn iceConn,
8143 IcePointer clientData UNUSED,
8144 Bool opening,
8145 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008147 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 if (opening)
8149 {
8150 xsmp_icefd = IceConnectionNumber(iceConn);
8151 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8152 }
8153}
8154
8155
Bram Moolenaar0f873732019-12-05 20:28:46 +01008156// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008158xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159{
8160 Bool rep;
8161
8162 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8163 == IceProcessMessagesIOError)
8164 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008165 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008167 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 xsmp_close();
8169 return FAIL;
8170 }
8171 else
8172 return OK;
8173}
8174
8175static int dummy;
8176
Bram Moolenaar0f873732019-12-05 20:28:46 +01008177// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 void
8179xsmp_init(void)
8180{
8181 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 SmcCallbacks smcallbacks;
8183#if 0
8184 SmPropValue smname;
8185 SmProp smnameprop;
8186 SmProp *smprops[1];
8187#endif
8188
8189 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008190 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191
8192 xsmp.save_yourself = xsmp.shutdown = False;
8193
Bram Moolenaar0f873732019-12-05 20:28:46 +01008194 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8196 smcallbacks.save_yourself.client_data = NULL;
8197 smcallbacks.die.callback = xsmp_die;
8198 smcallbacks.die.client_data = NULL;
8199 smcallbacks.save_complete.callback = xsmp_save_complete;
8200 smcallbacks.save_complete.client_data = NULL;
8201 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8202 smcallbacks.shutdown_cancelled.client_data = NULL;
8203
Bram Moolenaar0f873732019-12-05 20:28:46 +01008204 // Set up a watch on ICE connection creations. The "dummy" argument is
8205 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8207 {
8208 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008209 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 return;
8211 }
8212
Bram Moolenaar0f873732019-12-05 20:28:46 +01008213 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 xsmp.smcconn = SmcOpenConnection(
8215 NULL,
8216 NULL,
8217 SmProtoMajor,
8218 SmProtoMinor,
8219 SmcSaveYourselfProcMask | SmcDieProcMask
8220 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8221 &smcallbacks,
8222 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008223 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008224 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 errorstring);
8226 if (xsmp.smcconn == NULL)
8227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008229 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008230 char errorreport[132];
8231
8232 // If the message is too long it might not be NUL terminated. Add
8233 // a NUL at the end to make sure we don't go over the end.
8234 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008235 vim_snprintf(errorreport, sizeof(errorreport),
8236 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008237 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 return;
8240 }
8241 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8242
8243#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008244 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 smname.value = "vim";
8246 smname.length = 3;
8247 smnameprop.name = "SmProgram";
8248 smnameprop.type = "SmARRAY8";
8249 smnameprop.num_vals = 1;
8250 smnameprop.vals = &smname;
8251
8252 smprops[0] = &smnameprop;
8253 SmcSetProperties(xsmp.smcconn, 1, smprops);
8254#endif
8255}
8256
8257
Bram Moolenaar0f873732019-12-05 20:28:46 +01008258// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008260xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008262 if (xsmp_icefd == -1)
8263 return;
8264
8265 SmcCloseConnection(xsmp.smcconn, 0, NULL);
8266 if (xsmp.clientid != NULL)
8267 free(xsmp.clientid);
8268 xsmp.clientid = NULL;
8269 xsmp_icefd = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008271#endif // USE_XSMP
Paul Ollis65745772022-06-05 16:55:54 +01008272
8273#if defined(FEAT_RELTIME) || defined(PROTO)
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008274# if defined(HAVE_TIMER_CREATE) || defined(PROTO)
Paul Ollis65745772022-06-05 16:55:54 +01008275/*
8276 * Implement timeout with timer_create() and timer_settime().
8277 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008278static volatile sig_atomic_t timeout_flag = FALSE;
8279static timer_t timer_id;
8280static int timer_created = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008281
8282/*
8283 * Callback for when the timer expires.
8284 */
8285 static void
8286set_flag(union sigval _unused UNUSED)
8287{
8288 timeout_flag = TRUE;
8289}
8290
8291/*
8292 * Stop any active timeout.
8293 */
8294 void
8295stop_timeout(void)
8296{
8297 static struct itimerspec disarm = {{0, 0}, {0, 0}};
8298
8299 if (timer_created)
8300 {
8301 int ret = timer_settime(timer_id, 0, &disarm, NULL);
8302
8303 if (ret < 0)
8304 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8305 }
8306
8307 // Clear the current timeout flag; any previous timeout should be
8308 // considered _not_ triggered.
8309 timeout_flag = FALSE;
8310}
8311
8312/*
8313 * Start the timeout timer.
8314 *
8315 * The return value is a pointer to a flag that is initialised to FALSE. If the
8316 * timeout expires, the flag is set to TRUE. This will only return pointers to
8317 * static memory; i.e. any pointer returned by this function may always be
8318 * safely dereferenced.
8319 *
8320 * This function is not expected to fail, but if it does it will still return a
8321 * valid flag pointer; the flag will remain stuck as FALSE .
8322 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008323 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008324start_timeout(long msec)
8325{
8326 struct itimerspec interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008327 {0, 0}, // Do not repeat.
8328 {msec / 1000, (msec % 1000) * 1000000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008329 int ret;
8330
8331 // This is really the caller's responsibility, but let's make sure the
8332 // previous timer has been stopped.
8333 stop_timeout();
Paul Ollis65745772022-06-05 16:55:54 +01008334
8335 if (!timer_created)
8336 {
8337 struct sigevent action = {0};
8338
8339 action.sigev_notify = SIGEV_THREAD;
8340 action.sigev_notify_function = set_flag;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008341 ret = timer_create(CLOCK_MONOTONIC, &action, &timer_id);
8342 if (ret < 0)
Paul Ollis65745772022-06-05 16:55:54 +01008343 {
8344 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8345 return &timeout_flag;
8346 }
8347 timer_created = TRUE;
8348 }
8349
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00008350# ifdef FEAT_EVAL
Bram Moolenaar616592e2022-06-17 15:17:10 +01008351 ch_log(NULL, "setting timeout timer to %d sec %ld nsec",
8352 (int)interval.it_value.tv_sec, (long)interval.it_value.tv_nsec);
Bram Moolenaar509ce032022-06-20 11:23:01 +01008353# endif
Paul Ollis65745772022-06-05 16:55:54 +01008354 ret = timer_settime(timer_id, 0, &interval, NULL);
8355 if (ret < 0)
8356 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8357
8358 return &timeout_flag;
8359}
8360
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008361/*
8362 * To be used before fork/exec: delete any created timer.
8363 */
8364 void
8365delete_timer(void)
8366{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008367 if (!timer_created)
8368 return;
8369
8370 timer_delete(timer_id);
8371 timer_created = FALSE;
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008372}
8373
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008374# else // HAVE_TIMER_CREATE
Paul Ollis65745772022-06-05 16:55:54 +01008375
8376/*
8377 * Implement timeout with setitimer()
8378 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008379static struct sigaction prev_sigaction;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008380static volatile sig_atomic_t timeout_flag = FALSE;
8381static int timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008382static int timer_handler_active = FALSE;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008383static volatile sig_atomic_t alarm_pending = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008384
8385/*
8386 * Handle SIGALRM for a timeout.
8387 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01008388 static void
Paul Ollis65745772022-06-05 16:55:54 +01008389set_flag SIGDEFARG(sigarg)
8390{
8391 if (alarm_pending)
8392 alarm_pending = FALSE;
8393 else
8394 timeout_flag = TRUE;
8395}
8396
8397/*
8398 * Stop any active timeout.
8399 */
8400 void
8401stop_timeout(void)
8402{
8403 static struct itimerval disarm = {{0, 0}, {0, 0}};
8404 int ret;
8405
8406 if (timer_active)
8407 {
8408 timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008409 ret = setitimer(ITIMER_REAL, &disarm, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008410 if (ret < 0)
8411 // Should only get here as a result of coding errors.
8412 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8413 }
8414
8415 if (timer_handler_active)
8416 {
8417 timer_handler_active = FALSE;
8418 ret = sigaction(SIGALRM, &prev_sigaction, NULL);
8419 if (ret < 0)
8420 // Should only get here as a result of coding errors.
8421 semsg(_(e_could_not_reset_handler_for_timeout_str),
8422 strerror(errno));
8423 }
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008424 timeout_flag = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008425}
8426
8427/*
8428 * Start the timeout timer.
8429 *
8430 * The return value is a pointer to a flag that is initialised to FALSE. If the
8431 * timeout expires, the flag is set to TRUE. This will only return pointers to
8432 * static memory; i.e. any pointer returned by this function may always be
8433 * safely dereferenced.
8434 *
8435 * This function is not expected to fail, but if it does it will still return a
8436 * valid flag pointer; the flag will remain stuck as FALSE .
8437 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008438 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008439start_timeout(long msec)
8440{
8441 struct itimerval interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008442 {0, 0}, // Do not repeat.
8443 {msec / 1000, (msec % 1000) * 1000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008444 struct sigaction handle_alarm;
8445 int ret;
8446 sigset_t sigs;
8447 sigset_t saved_sigs;
8448
8449 // This is really the caller's responsibility, but let's make sure the
8450 // previous timer has been stopped.
8451 stop_timeout();
8452
8453 // There is a small chance that SIGALRM is pending and so the handler must
8454 // ignore it on the first call.
8455 alarm_pending = FALSE;
8456 ret = sigemptyset(&sigs);
8457 ret = ret == 0 ? sigaddset(&sigs, SIGALRM) : ret;
8458 ret = ret == 0 ? sigprocmask(SIG_BLOCK, &sigs, &saved_sigs) : ret;
8459 timeout_flag = FALSE;
8460 ret = ret == 0 ? sigpending(&sigs) : ret;
8461 if (ret == 0)
8462 {
8463 alarm_pending = sigismember(&sigs, SIGALRM);
Bram Moolenaar1f89abf2022-06-06 10:07:01 +01008464 ret = sigprocmask(SIG_SETMASK, &saved_sigs, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008465 }
8466 if (unlikely(ret != 0 || alarm_pending < 0))
8467 {
8468 // Just catching coding errors. Write an error message, but carry on.
8469 semsg(_(e_could_not_check_for_pending_sigalrm_str), strerror(errno));
8470 alarm_pending = FALSE;
8471 }
8472
8473 // Set up the alarm handler first.
8474 ret = sigemptyset(&handle_alarm.sa_mask);
8475 handle_alarm.sa_handler = set_flag;
8476 handle_alarm.sa_flags = 0;
8477 ret = ret == 0 ? sigaction(SIGALRM, &handle_alarm, &prev_sigaction) : ret;
8478 if (ret < 0)
8479 {
8480 // Should only get here as a result of coding errors.
8481 semsg(_(e_could_not_set_handler_for_timeout_str), strerror(errno));
8482 return &timeout_flag;
8483 }
8484 timer_handler_active = TRUE;
8485
8486 // Set up the interval timer once the alarm handler is in place.
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008487 ret = setitimer(ITIMER_REAL, &interval, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008488 if (ret < 0)
8489 {
8490 // Should only get here as a result of coding errors.
8491 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8492 stop_timeout();
8493 return &timeout_flag;
8494 }
8495
8496 timer_active = TRUE;
8497 return &timeout_flag;
8498}
8499# endif // HAVE_TIMER_CREATE
8500#endif // FEAT_RELTIME