blob: 746bcff797c9cdbdcfb51c6233a1f76165283483 [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
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002437/*
2438 * Return TRUE if "name" is a vt300-like terminal name.
2439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002441vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442{
2443 if (name == NULL)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002444 return FALSE;
2445 // Actually all ANSI compatible terminals should be here.
2446 // Catch at least VT1xx - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002447 return ((STRNICMP(name, "vt", 2) == 0
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002448 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 || STRCMP(name, "builtin_vt320") == 0);
2450}
2451
2452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 * Insert user name in s[len].
2454 * Return OK if a name found.
2455 */
2456 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002457mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
2459#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002460 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 return OK;
2462#else
2463 return mch_get_uname(getuid(), s, len);
2464#endif
2465}
2466
2467/*
2468 * Insert user name for "uid" in s[len].
2469 * Return OK if a name found.
2470 */
2471 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002472mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473{
2474#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2475 struct passwd *pw;
2476
2477 if ((pw = getpwuid(uid)) != NULL
2478 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2479 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002480 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 return OK;
2482 }
2483#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002484 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2485 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486}
2487
2488/*
2489 * Insert host name is s[len].
2490 */
2491
2492#ifdef HAVE_SYS_UTSNAME_H
2493 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002494mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495{
2496 struct utsname vutsname;
2497
2498 if (uname(&vutsname) < 0)
2499 *s = NUL;
2500 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002501 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002503#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
2505# ifdef HAVE_SYS_SYSTEMINFO_H
2506# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2507# endif
2508
2509 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002510mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
2512# ifdef VAXC
2513 vaxc$gethostname((char *)s, len);
2514# else
2515 gethostname((char *)s, len);
2516# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002517 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002519#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520
2521/*
2522 * return process ID
2523 */
2524 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002525mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526{
2527 return (long)getpid();
2528}
2529
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002530/*
2531 * return TRUE if process "pid" is still running
2532 */
2533 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002534mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002535{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002536 // If there is no error the process must be running.
2537 if (kill(pid, 0) == 0)
2538 return TRUE;
2539#ifdef ESRCH
2540 // If the error is ESRCH then the process is not running.
2541 if (errno == ESRCH)
2542 return FALSE;
2543#endif
2544 // If the process is running and owned by another user we get EPERM. With
2545 // other errors the process might be running, assuming it is then.
2546 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002547}
2548
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002551strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
2553 extern int sys_nerr;
2554 extern char *sys_errlist[];
2555 static char er[20];
2556
2557 if (err > 0 && err < sys_nerr)
2558 return (sys_errlist[err]);
2559 sprintf(er, "Error %d", err);
2560 return er;
2561}
2562#endif
2563
2564/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002565 * Get name of current directory into buffer "buf" of length "len" bytes.
2566 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 * Return OK for success, FAIL for failure.
2568 */
2569 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002570mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
2572#if defined(USE_GETCWD)
2573 if (getcwd((char *)buf, len) == NULL)
2574 {
2575 STRCPY(buf, strerror(errno));
2576 return FAIL;
2577 }
2578 return OK;
2579#else
2580 return (getwd((char *)buf) != NULL ? OK : FAIL);
2581#endif
2582}
2583
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002585 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 *
2587 * return FAIL for failure, OK for success
2588 */
2589 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002590mch_FullName(
2591 char_u *fname,
2592 char_u *buf,
2593 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002594 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
2596 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002597#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002599 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 char_u olddir[MAXPATHL];
2602 char_u *p;
2603 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002604#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002605 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2606 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002607#endif
2608
Bram Moolenaar38323e42007-03-06 19:22:53 +00002609#ifdef VMS
2610 fname = vms_fixfilename(fname);
2611#endif
2612
Bram Moolenaara2442432007-04-26 14:26:37 +00002613#ifdef __CYGWIN__
2614 /*
2615 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2616 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002617# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002618 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2619 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002620 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2621 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002622# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002623 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002624# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002625 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002626#endif
2627
Bram Moolenaar0f873732019-12-05 20:28:46 +01002628 // Expand it if forced or not an absolute path.
2629 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002630 if ((force || !mch_isFullName(fname))
2631 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
2633 /*
2634 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002635 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 * This will get the correct path name with "../" things.
2637 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002638 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002640 if (STRCMP(p, "/..") == 0)
2641 // for "/path/dir/.." include the "/.."
2642 p += 3;
2643
Bram Moolenaar38323e42007-03-06 19:22:53 +00002644#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 /*
2646 * Use fchdir() if possible, it's said to be faster and more
2647 * reliable. But on SunOS 4 it might not work. Check this by
2648 * doing a fchdir() right now.
2649 */
2650 if (!dont_fchdir)
2651 {
2652 fd = open(".", O_RDONLY | O_EXTRA, 0);
2653 if (fd >= 0 && fchdir(fd) < 0)
2654 {
2655 close(fd);
2656 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002657 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
2659 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
Bram Moolenaar0f873732019-12-05 20:28:46 +01002662 // Only change directory when we are sure we can return to where
2663 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002665#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 (mch_dirname(olddir, MAXPATHL) == FAIL
2669 || mch_chdir((char *)olddir) != 0))
2670 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002671 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 retval = FAIL;
2673 }
2674 else
2675 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002676 // The directory is copied into buf[], to be able to remove
2677 // the file name without changing it (could be a string in
2678 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 if (p - fname >= len)
2680 retval = FAIL;
2681 else
2682 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002683 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002685 {
2686 // Path does not exist (yet). For a full path fail,
2687 // will use the path as-is. For a relative path use
2688 // the current directory and append the file name.
2689 if (mch_isFullName(fname))
2690 retval = FAIL;
2691 else
2692 p = NULL;
2693 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002694 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002696 else
2697 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 *buf = NUL;
2699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701 }
2702 if (mch_dirname(buf, len) == FAIL)
2703 {
2704 retval = FAIL;
2705 *buf = NUL;
2706 }
2707 if (p != NULL)
2708 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002709#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (fd >= 0)
2711 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002712 if (p_verbose >= 5)
2713 {
2714 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002715 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002716 verbose_leave();
2717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 }
2720 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 l = mch_chdir((char *)olddir);
2723 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002724 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 }
itchyny051a40c2021-10-20 10:00:05 +01002726#ifdef HAVE_FCHDIR
2727 if (fd >= 0)
2728 close(fd);
2729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
2731 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002732 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002733 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002734#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002735 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002737 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002740
Bram Moolenaar0f873732019-12-05 20:28:46 +01002741 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002742 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 return FAIL;
2744
Bram Moolenaar0f873732019-12-05 20:28:46 +01002745 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if (STRCMP(fname, ".") != 0)
2747 STRCAT(buf, fname);
2748
2749 return OK;
2750}
2751
2752/*
2753 * Return TRUE if "fname" does not depend on the current directory.
2754 */
2755 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002756mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002758#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 return ( fname[0] == '/' || fname[0] == '.' ||
2760 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2761 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2762 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002763#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765#endif
2766}
2767
Bram Moolenaar24552be2005-12-10 20:17:30 +00002768#if defined(USE_FNAME_CASE) || defined(PROTO)
2769/*
2770 * Set the case of the file name, if it already exists. This will cause the
2771 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002772 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002773 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002774 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002775fname_case(
2776 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002777 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002778{
2779 struct stat st;
2780 char_u *slash, *tail;
2781 DIR *dirp;
2782 struct dirent *dp;
2783
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002784 if (mch_lstat((char *)name, &st) < 0)
2785 return;
2786
2787 // Open the directory where the file is located.
2788 slash = vim_strrchr(name, '/');
2789 if (slash == NULL)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002790 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002791 dirp = opendir(".");
2792 tail = name;
2793 }
2794 else
2795 {
2796 *slash = NUL;
2797 dirp = opendir((char *)name);
2798 *slash = '/';
2799 tail = slash + 1;
2800 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002801
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002802 if (dirp == NULL)
2803 return;
2804
2805 while ((dp = readdir(dirp)) != NULL)
2806 {
2807 // Only accept names that differ in case and are the same byte
2808 // length. TODO: accept different length name.
2809 if (STRICMP(tail, dp->d_name) == 0
2810 && STRLEN(tail) == STRLEN(dp->d_name))
Bram Moolenaar24552be2005-12-10 20:17:30 +00002811 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002812 char_u newname[MAXPATHL + 1];
2813 struct stat st2;
2814
2815 // Verify the inode is equal.
2816 vim_strncpy(newname, name, MAXPATHL);
2817 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2818 MAXPATHL - (tail - name));
2819 if (mch_lstat((char *)newname, &st2) >= 0
2820 && st.st_ino == st2.st_ino
2821 && st.st_dev == st2.st_dev)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002822 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002823 STRCPY(tail, dp->d_name);
2824 break;
Bram Moolenaar24552be2005-12-10 20:17:30 +00002825 }
Bram Moolenaar24552be2005-12-10 20:17:30 +00002826 }
2827 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002828
2829 closedir(dirp);
Bram Moolenaar24552be2005-12-10 20:17:30 +00002830}
2831#endif
2832
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833/*
2834 * Get file permissions for 'name'.
2835 * Returns -1 when it doesn't exist.
2836 */
2837 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002838mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839{
2840 struct stat statb;
2841
Bram Moolenaar0f873732019-12-05 20:28:46 +01002842 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843#ifdef VMS
2844 if (stat((char *)vms_fixfilename(name), &statb))
2845#else
2846 if (stat((char *)name, &statb))
2847#endif
2848 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002849#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002850 // The top bit makes the value negative, which means the file doesn't
2851 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002852 return statb.st_mode & ~S_ADDACE;
2853#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856}
2857
2858/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002859 * Set file permission for "name" to "perm".
2860 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 */
2862 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002863mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
2865 return (chmod((char *)
2866#ifdef VMS
2867 vms_fixfilename(name),
2868#else
2869 name,
2870#endif
2871 (mode_t)perm) == 0 ? OK : FAIL);
2872}
2873
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002874#if defined(HAVE_FCHMOD) || defined(PROTO)
2875/*
2876 * Set file permission for open file "fd" to "perm".
2877 * Return FAIL for failure, OK otherwise.
2878 */
2879 int
2880mch_fsetperm(int fd, long perm)
2881{
2882 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2883}
2884#endif
2885
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886#if defined(HAVE_ACL) || defined(PROTO)
2887# ifdef HAVE_SYS_ACL_H
2888# include <sys/acl.h>
2889# endif
2890# ifdef HAVE_SYS_ACCESS_H
2891# include <sys/access.h>
2892# endif
2893
2894# ifdef HAVE_SOLARIS_ACL
2895typedef struct vim_acl_solaris_T {
2896 int acl_cnt;
2897 aclent_t *acl_entry;
2898} vim_acl_solaris_T;
2899# endif
2900
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002901#if defined(HAVE_SELINUX) || defined(PROTO)
2902/*
2903 * Copy security info from "from_file" to "to_file".
2904 */
2905 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002906mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002907{
2908 if (from_file == NULL)
2909 return;
2910
2911 if (selinux_enabled == -1)
2912 selinux_enabled = is_selinux_enabled();
2913
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002914 if (selinux_enabled <= 0)
2915 return;
2916
2917 // Use "char *" instead of "security_context_t" to avoid a deprecation
2918 // warning.
2919 char *from_context = NULL;
2920 char *to_context = NULL;
2921
2922 if (getfilecon((char *)from_file, &from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002923 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002924 // If the filesystem doesn't support extended attributes,
2925 // the original had no special security context and the
2926 // target cannot have one either.
2927 if (errno == EOPNOTSUPP)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002928 return;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002929
2930 msg_puts(_("\nCould not get security context for "));
2931 msg_outtrans(from_file);
2932 msg_putchar('\n');
2933 return;
2934 }
2935 if (getfilecon((char *)to_file, &to_context) < 0)
2936 {
2937 msg_puts(_("\nCould not get security context for "));
2938 msg_outtrans(to_file);
2939 msg_putchar('\n');
2940 freecon (from_context);
2941 return ;
2942 }
2943 if (strcmp(from_context, to_context) != 0)
2944 {
2945 if (setfilecon((char *)to_file, from_context) < 0)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002946 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002947 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002948 msg_outtrans(to_file);
2949 msg_putchar('\n');
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002950 }
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002951 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002952 freecon(to_context);
2953 freecon(from_context);
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002954}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002955#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002956
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002957#if defined(HAVE_SMACK) && !defined(PROTO)
2958/*
2959 * Copy security info from "from_file" to "to_file".
2960 */
2961 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002962mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002963{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002964 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002965 {
2966 XATTR_NAME_SMACK,
2967 XATTR_NAME_SMACKEXEC,
2968 XATTR_NAME_SMACKMMAP
2969 };
2970
2971 char buffer[SMACK_LABEL_LEN];
2972 const char *name;
2973 int index;
2974 int ret;
2975 ssize_t size;
2976
2977 if (from_file == NULL)
2978 return;
2979
2980 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2981 / sizeof(smack_copied_attributes)[0]) ; index++)
2982 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002983 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002984 name = smack_copied_attributes[index];
2985
Bram Moolenaar0f873732019-12-05 20:28:46 +01002986 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002987 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2988 if (size >= 0)
2989 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002990 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002991 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2992 if (ret < 0)
2993 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002994 vim_snprintf((char *)IObuff, IOSIZE,
2995 _("Could not set security context %s for %s"),
2996 name, to_file);
2997 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002998 msg_putchar('\n');
2999 }
3000 }
3001 else
3002 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003003 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003004 switch (errno)
3005 {
3006 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003007 // extended attributes aren't supported or enabled
3008 // should a message be echoed? not sure...
3009 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003010
3011 case ERANGE:
3012 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003013 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003014 vim_snprintf((char *)IObuff, IOSIZE,
3015 _("Could not get security context %s for %s. Removing it!"),
3016 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003017 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003018 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003019 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003020
3021 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003022 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003023 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003024 // Silently ignore errors, apparently this happens when
3025 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003026 break;
3027 }
3028 }
3029 }
3030}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003031#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003032
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033/*
3034 * Return a pointer to the ACL of file "fname" in allocated memory.
3035 * Return NULL if the ACL is not available for whatever reason.
3036 */
3037 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003038mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
3040 vim_acl_T ret = NULL;
3041#ifdef HAVE_POSIX_ACL
3042 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3043#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003044#ifdef HAVE_SOLARIS_ZFS_ACL
3045 acl_t *aclent;
3046
3047 if (acl_get((char *)fname, 0, &aclent) < 0)
3048 return NULL;
3049 ret = (vim_acl_T)aclent;
3050#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051#ifdef HAVE_SOLARIS_ACL
3052 vim_acl_solaris_T *aclent;
3053
3054 aclent = malloc(sizeof(vim_acl_solaris_T));
3055 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3056 {
3057 free(aclent);
3058 return NULL;
3059 }
3060 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3061 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3062 {
3063 free(aclent->acl_entry);
3064 free(aclent);
3065 return NULL;
3066 }
3067 ret = (vim_acl_T)aclent;
3068#else
3069#if defined(HAVE_AIX_ACL)
3070 int aclsize;
3071 struct acl *aclent;
3072
3073 aclsize = sizeof(struct acl);
3074 aclent = malloc(aclsize);
3075 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3076 {
3077 if (errno == ENOSPC)
3078 {
3079 aclsize = aclent->acl_len;
3080 aclent = realloc(aclent, aclsize);
3081 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3082 {
3083 free(aclent);
3084 return NULL;
3085 }
3086 }
3087 else
3088 {
3089 free(aclent);
3090 return NULL;
3091 }
3092 }
3093 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003094#endif // HAVE_AIX_ACL
3095#endif // HAVE_SOLARIS_ACL
3096#endif // HAVE_SOLARIS_ZFS_ACL
3097#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 return ret;
3099}
3100
3101/*
3102 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3103 */
3104 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003105mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 if (aclent == NULL)
3108 return;
3109#ifdef HAVE_POSIX_ACL
3110 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3111#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003112#ifdef HAVE_SOLARIS_ZFS_ACL
3113 acl_set((char *)fname, (acl_t *)aclent);
3114#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115#ifdef HAVE_SOLARIS_ACL
3116 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3117 ((vim_acl_solaris_T *)aclent)->acl_entry);
3118#else
3119#ifdef HAVE_AIX_ACL
3120 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003121#endif // HAVE_AIX_ACL
3122#endif // HAVE_SOLARIS_ACL
3123#endif // HAVE_SOLARIS_ZFS_ACL
3124#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125}
3126
3127 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003128mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 if (aclent == NULL)
3131 return;
3132#ifdef HAVE_POSIX_ACL
3133 acl_free((acl_t)aclent);
3134#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003135#ifdef HAVE_SOLARIS_ZFS_ACL
3136 acl_free((acl_t *)aclent);
3137#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#ifdef HAVE_SOLARIS_ACL
3139 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3140 free(aclent);
3141#else
3142#ifdef HAVE_AIX_ACL
3143 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003144#endif // HAVE_AIX_ACL
3145#endif // HAVE_SOLARIS_ACL
3146#endif // HAVE_SOLARIS_ZFS_ACL
3147#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148}
3149#endif
3150
3151/*
3152 * Set hidden flag for "name".
3153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003155mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003157 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158}
3159
3160/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003161 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 * return FALSE if "name" is not a directory
3163 * return FALSE for error
3164 */
3165 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003166mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167{
3168 struct stat statb;
3169
Bram Moolenaar0f873732019-12-05 20:28:46 +01003170 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 return FALSE;
3172 if (stat((char *)name, &statb))
3173 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175}
3176
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003177/*
3178 * return TRUE if "name" is a directory, NOT a symlink to a directory
3179 * return FALSE if "name" is not a directory
3180 * return FALSE for error
3181 */
3182 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003183mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003184{
3185 struct stat statb;
3186
Bram Moolenaar0f873732019-12-05 20:28:46 +01003187 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003188 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003189 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003190 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003191 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003192}
3193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194/*
3195 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3196 */
3197 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003198executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199{
3200 struct stat st;
3201
3202 if (stat((char *)name, &st))
3203 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003204#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003205 // Like on Unix system file can have executable rights but not necessarily
3206 // be an executable, but on Unix is not a default for an ordinary file to
3207 // have an executable flag - on VMS it is in most cases.
3208 // Therefore, this check does not have any sense - let keep us to the
3209 // conventions instead:
3210 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003211 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003212 int vms_executable = 0;
3213 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3214 {
3215 if (strstr(vms_tolower((char*)name),".exe") != NULL
3216 || strstr(vms_tolower((char*)name),".com")!= NULL)
3217 vms_executable = 1;
3218 }
3219 return vms_executable;
3220#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223}
3224
3225/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003226 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003227 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 * Return -1 if unknown.
3229 */
3230 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003231mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
3233 char_u *buf;
3234 char_u *p, *e;
3235 int retval;
3236
Bram Moolenaar0f873732019-12-05 20:28:46 +01003237 // When "use_path" is false and if it's an absolute or relative path don't
3238 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003239 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003240 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003241 // There must be a path separator, files in the current directory
3242 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003243 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003244 {
3245 if (path != NULL)
3246 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003247 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003248 *path = FullName_save(name, TRUE);
3249 else
3250 *path = vim_strsave(name);
3251 }
3252 return TRUE;
3253 }
3254 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
3257 p = (char_u *)getenv("PATH");
3258 if (p == NULL || *p == NUL)
3259 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003260 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 if (buf == NULL)
3262 return -1;
3263
3264 /*
3265 * Walk through all entries in $PATH to check if "name" exists there and
3266 * is an executable file.
3267 */
3268 for (;;)
3269 {
3270 e = (char_u *)strchr((char *)p, ':');
3271 if (e == NULL)
3272 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003273 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 STRCPY(buf, "./");
3275 else
3276 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003277 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 add_pathsep(buf);
3279 }
3280 STRCAT(buf, name);
3281 retval = executable_file(buf);
3282 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003283 {
3284 if (path != NULL)
3285 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003286 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003287 *path = FullName_save(buf, TRUE);
3288 else
3289 *path = vim_strsave(buf);
3290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
3294 if (*e != ':')
3295 break;
3296 p = e + 1;
3297 }
3298
3299 vim_free(buf);
3300 return retval;
3301}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302
3303/*
3304 * Check what "name" is:
3305 * NODE_NORMAL: file or directory (or doesn't exist)
3306 * NODE_WRITABLE: writable device, socket, fifo, etc.
3307 * NODE_OTHER: non-writable things
3308 */
3309 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003310mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311{
3312 struct stat st;
3313
3314 if (stat((char *)name, &st))
3315 return NODE_NORMAL;
3316 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3317 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003318 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003320 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 return NODE_WRITABLE;
3322}
3323
3324 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003325mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326{
3327#ifdef HAVE_CHECK_STACK_GROWTH
3328 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 check_stack_growth((char *)&i);
3331
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003332# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 get_stack_limit();
3334# endif
3335
3336#endif
3337
3338 /*
3339 * Setup an alternative stack for signals. Helps to catch signals when
3340 * running out of stack space.
3341 * Use of sigaltstack() is preferred, it's more portable.
3342 * Ignore any errors.
3343 */
3344#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003345 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 init_signal_stack();
3347#endif
3348}
3349
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003350#if defined(EXITFREE) || defined(PROTO)
3351 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003352mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003353{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003354# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3355 if (clip_star.owned)
3356 clip_lose_selection(&clip_star);
3357 if (clip_plus.owned)
3358 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003359# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003360# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003361 if (xterm_Shell != (Widget)0)
3362 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003363# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003364 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003365 if (xterm_dpy != NULL)
3366 XtCloseDisplay(xterm_dpy);
3367 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003368 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003369 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003370# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003371 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003372# endif
3373 }
3374# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003375# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003376# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003377 if (x11_display != NULL
3378# ifdef FEAT_XCLIPBOARD
3379 && x11_display != xterm_dpy
3380# endif
3381 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003382 XCloseDisplay(x11_display);
3383# endif
3384# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003385 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003386# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003387 vim_free(oldtitle);
3388 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003389}
3390#endif
3391
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392/*
3393 * Output a newline when exiting.
3394 * Make sure the newline goes to the same stream as the text.
3395 */
3396 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003397exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003399 if (silent_mode)
3400 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 if (newline_on_exit || msg_didout)
3402 {
3403 if (msg_use_printf())
3404 {
3405 if (info_message)
3406 mch_msg("\n");
3407 else
3408 mch_errmsg("\r\n");
3409 }
3410 else
3411 out_char('\n');
3412 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003413 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003415 restore_cterm_colors(); // get original colors back
3416 msg_clr_eos_force(); // clear the rest of the display
3417 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 }
3419}
3420
Bram Moolenaarb4151682020-05-11 22:13:28 +02003421#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003422# if (defined(__GNUC__) \
3423 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3424 || (defined(__clang__) && (__clang_major__ >= 12))
3425extern void __gcov_dump(void);
3426extern void __gcov_reset(void);
3427# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3428# else
3429extern void __gcov_flush(void);
3430# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003431#endif
3432
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003434mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
3436 exiting = TRUE;
3437
3438#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3439 x11_export_final_selection();
3440#endif
3441
3442#ifdef FEAT_GUI
3443 if (!gui.in_use)
3444#endif
3445 {
3446 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003447 if (!is_not_a_term())
3448 {
3449 // restore xterm title and icon name
3450 mch_restore_title(SAVE_RESTORE_BOTH);
3451 term_pop_title(SAVE_RESTORE_BOTH);
3452 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003453
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 /*
3455 * When t_ti is not empty but it doesn't cause swapping terminal
3456 * pages, need to output a newline when msg_didout is set. But when
3457 * t_ti does swap pages it should not go to the shell page. Do this
3458 * before stoptermcap().
3459 */
3460 if (swapping_screen() && !newline_on_exit)
3461 exit_scroll();
3462
Bram Moolenaar0f873732019-12-05 20:28:46 +01003463 // Stop termcap: May need to check for T_CRV response, which
3464 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 stoptermcap();
3466
3467 /*
3468 * A newline is only required after a message in the alternate screen.
3469 * This is set to TRUE by wait_return().
3470 */
3471 if (!swapping_screen() || newline_on_exit)
3472 exit_scroll();
3473
Bram Moolenaar0f873732019-12-05 20:28:46 +01003474 // Cursor may have been switched off without calling starttermcap()
3475 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (full_screen)
3477 cursor_on();
3478 }
3479 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003480 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003481
3482#ifdef USE_GCOV_FLUSH
3483 // Flush coverage info before possibly being killed by a deadly signal.
3484 __gcov_flush();
3485#endif
3486
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 may_core_dump();
3488#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 gui_exit(r);
3491#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003492
Bram Moolenaar56718732006-03-15 22:53:57 +00003493#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003494 mac_conv_cleanup();
3495#endif
3496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003498 // A core dump won't be created if the signal handler
3499 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 if (deadly_signal != 0)
3501 return;
3502#endif
3503
Bram Moolenaar009b2592004-10-24 19:18:58 +00003504#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003505 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003506#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003507
3508#ifdef EXITFREE
3509 free_all_mem();
3510#endif
3511
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 exit(r);
3513}
3514
3515 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003516may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517{
3518 if (deadly_signal != 0)
3519 {
3520 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003521 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523}
3524
3525#ifndef VMS
3526
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003527/*
3528 * Get the file descriptor to use for tty operations.
3529 */
3530 static int
3531get_tty_fd(int fd)
3532{
3533 int tty_fd = fd;
3534
3535#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3536 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3537 // "fd" when it is a master device.
3538 if (mch_isatty(fd) > 1)
3539 {
3540 char *name;
3541
3542 name = ptsname(fd);
3543 if (name == NULL)
3544 return -1;
3545
3546 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3547 if (tty_fd < 0)
3548 return -1;
3549 }
3550#endif
3551 return tty_fd;
3552}
3553
3554 static int
3555mch_tcgetattr(int fd, void *term)
3556{
3557 int tty_fd;
3558 int retval = -1;
3559
3560 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003561 if (tty_fd < 0)
3562 return -1;
3563
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003564#ifdef NEW_TTY_SYSTEM
3565# ifdef HAVE_TERMIOS_H
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003566 retval = tcgetattr(tty_fd, (struct termios *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003567# else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003568 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003569# endif
3570#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003571 // for "old" tty systems
3572 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003573#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003574 if (tty_fd != fd)
3575 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003576 return retval;
3577}
3578
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003580mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581{
3582 static int first = TRUE;
3583
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003584#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585# ifdef HAVE_TERMIOS_H
3586 static struct termios told;
3587 struct termios tnew;
3588# else
3589 static struct termio told;
3590 struct termio tnew;
3591# endif
3592
3593 if (first)
3594 {
3595 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003596 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598
3599 tnew = told;
3600 if (tmode == TMODE_RAW)
3601 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003602 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003603 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3604 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003606# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003607 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608# endif
3609 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003610# ifdef ONLCR
3611 // Don't map NL -> CR NL, we do it ourselves.
3612 // Also disable expanding tabs if possible.
3613# ifdef XTABS
3614 tnew.c_oflag &= ~(ONLCR | XTABS);
3615# else
3616# ifdef TAB3
3617 tnew.c_oflag &= ~(ONLCR | TAB3);
3618# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003620# endif
3621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003623 tnew.c_cc[VMIN] = 1; // return after 1 char
3624 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
3626 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003627 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003628 // Also reset ICANON here, otherwise on Solaris select() won't see
3629 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003630 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003631 tnew.c_cc[VMIN] = 1; // return after 1 char
3632 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
3635# if defined(HAVE_TERMIOS_H)
3636 {
3637 int n = 10;
3638
Bram Moolenaar0f873732019-12-05 20:28:46 +01003639 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3640 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3642 && errno == EINTR && n > 0)
3643 --n;
3644 }
3645# else
3646 ioctl(read_cmd_fd, TCSETA, &tnew);
3647# endif
3648
3649#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 /*
3651 * for "old" tty systems
3652 */
3653# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003654# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655# endif
3656 static struct sgttyb ttybold;
3657 struct sgttyb ttybnew;
3658
3659 if (first)
3660 {
3661 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003662 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
3664
3665 ttybnew = ttybold;
3666 if (tmode == TMODE_RAW)
3667 {
3668 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3669 ttybnew.sg_flags |= RAW;
3670 }
3671 else if (tmode == TMODE_SLEEP)
3672 ttybnew.sg_flags &= ~(ECHO);
3673 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3674#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003675 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676}
3677
3678/*
3679 * Try to get the code for "t_kb" from the stty setting
3680 *
3681 * Even if termcap claims a backspace key, the user's setting *should*
3682 * prevail. stty knows more about reality than termcap does, and if
3683 * somebody's usual erase key is DEL (which, for most BSD users, it will
3684 * be), they're going to get really annoyed if their erase key starts
3685 * doing forward deletes for no reason. (Eric Fischer)
3686 */
3687 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003688get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003690 ttyinfo_T info;
3691 char_u buf[2];
3692 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003694 if (get_tty_info(read_cmd_fd, &info) != OK)
3695 return;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003696
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003697 intr_char = info.interrupt;
3698 buf[0] = info.backspace;
3699 buf[1] = NUL;
3700 add_termcode((char_u *)"kb", buf, FALSE);
3701
3702 // If <BS> and <DEL> are now the same, redefine <DEL>.
3703 p = find_termcode((char_u *)"kD");
3704 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3705 do_fixdel(NULL);
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003706}
3707
3708/*
3709 * Obtain the characters that Backspace and Enter produce on "fd".
3710 * Returns OK or FAIL.
3711 */
3712 int
3713get_tty_info(int fd, ttyinfo_T *info)
3714{
3715#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716# ifdef HAVE_TERMIOS_H
3717 struct termios keys;
3718# else
3719 struct termio keys;
3720# endif
3721
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003722 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003724 info->backspace = keys.c_cc[VERASE];
3725 info->interrupt = keys.c_cc[VINTR];
3726 if (keys.c_iflag & ICRNL)
3727 info->enter = NL;
3728 else
3729 info->enter = CAR;
3730 if (keys.c_oflag & ONLCR)
3731 info->nl_does_cr = TRUE;
3732 else
3733 info->nl_does_cr = FALSE;
3734 return OK;
3735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003737 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 struct sgttyb keys;
3739
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003740 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003742 info->backspace = keys.sg_erase;
3743 info->interrupt = keys.sg_kill;
3744 info->enter = CAR;
3745 info->nl_does_cr = TRUE;
3746 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003749 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750}
3751
Bram Moolenaar0f873732019-12-05 20:28:46 +01003752#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003754static int mouse_ison = FALSE;
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756/*
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003757 * Set mouse clicks on or off and possible enable mouse movement events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 */
3759 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003760mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003762#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003763 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 int xterm_mouse_vers;
3766
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003767#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003768 if (!on)
3769 // Make sure not tracing mouse movements. Important when a button-down
3770 // was received but no release yet.
3771 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003772#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003773
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003774 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003775#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003776 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003777#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003778 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003779 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 return;
3781
3782 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003783
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003784#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003785 if (ttym_flags == TTYM_URXVT)
3786 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003787 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003788 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003789 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003790#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003791
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00003792 if (T_CXM != NULL && *T_CXM != NUL)
3793 {
3794 term_enable_mouse(on);
3795 }
3796 else if (ttym_flags == TTYM_SGR)
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003797 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003798 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003799 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003800 mouse_ison = on;
3801 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003802
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003803#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003804 if (bevalterm_ison != (p_bevalterm && on))
3805 {
3806 bevalterm_ison = (p_bevalterm && on);
3807 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003808 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003809 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003810 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003811#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003812
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if (xterm_mouse_vers > 0)
3814 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003815 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 out_str_nf((char_u *)
3817 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003818 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003819#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003820 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003821#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003822 "\033[?1002h")
3823 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003824 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003826 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003827 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003830#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 else if (ttym_flags == TTYM_DEC)
3832 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003833 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003835 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003837 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003841#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 else
3843 {
3844 if (on)
3845 {
3846 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003847 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849 else
3850 {
3851 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003852 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 }
3854 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003857#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003858 else
3859 {
3860 if (on)
3861 {
3862 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003863 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003864 }
3865 else
3866 {
3867 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003868 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003869 }
3870 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003871#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003872
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003873#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 else
3875 {
3876 if (on)
3877 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003878 // D - Enable Mouse up/down messages
3879 // L - Enable Left Button Reporting
3880 // M - Enable Middle Button Reporting
3881 // R - Enable Right Button Reporting
3882 // K - Enable SHIFT and CTRL key Reporting
3883 // + - Enable Advanced messaging of mouse moves and up/down messages
3884 // Q - Quiet No Ack
3885 // # - Numeric value of mouse pointer required
3886 // 0 = Multiview 2000 cursor, used as standard
3887 // 1 = Windows Arrow
3888 // 2 = Windows I Beam
3889 // 3 = Windows Hour Glass
3890 // 4 = Windows Cross Hair
3891 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003892# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003893 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003894 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003895# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003896 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003897# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003898 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
3900 else
3901 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003902 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003903 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
3905 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003906#endif
3907#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 else
3909 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003910 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 if (on)
3912 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3913 else
3914 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003915 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918}
3919
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003920#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003921/*
3922 * Called when 'balloonevalterm' changed.
3923 */
3924 void
3925mch_bevalterm_changed(void)
3926{
3927 mch_setmouse(mouse_ison);
3928}
3929#endif
3930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931/*
3932 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3933 */
3934 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003935check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936{
3937# ifdef FEAT_MOUSE_XTERM
3938 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003939# ifdef FEAT_MOUSE_URXVT
3940 && use_xterm_mouse() != 3
3941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942# ifdef FEAT_GUI
3943 && !gui.in_use
3944# endif
3945 )
3946 {
3947 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003948 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 if (*p_mouse != NUL)
3950 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003951 // force mouse off and maybe on to send possibly new mouse
3952 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 mch_setmouse(FALSE);
3954 setmouse();
3955 }
3956 }
3957 else
3958 del_mouse_termcode(KS_MOUSE);
3959# endif
3960
3961# ifdef FEAT_MOUSE_GPM
3962 if (!use_xterm_mouse()
3963# ifdef FEAT_GUI
3964 && !gui.in_use
3965# endif
3966 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003967 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003968 else
3969 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970# endif
3971
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003972# ifdef FEAT_SYSMOUSE
3973 if (!use_xterm_mouse()
3974# ifdef FEAT_GUI
3975 && !gui.in_use
3976# endif
3977 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003978 set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003979# endif
3980
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003982 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 if (!use_xterm_mouse()
3984# ifdef FEAT_GUI
3985 && !gui.in_use
3986# endif
3987 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003988 set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 else
3990 del_mouse_termcode(KS_JSBTERM_MOUSE);
3991# endif
3992
3993# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003994 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3995 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 if (!use_xterm_mouse()
3997# ifdef FEAT_GUI
3998 && !gui.in_use
3999# endif
4000 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004001 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 else
4003 del_mouse_termcode(KS_NETTERM_MOUSE);
4004# endif
4005
4006# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01004007 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004008 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009# ifdef FEAT_GUI
4010 && !gui.in_use
4011# endif
4012 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004013 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004014 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 else
4016 del_mouse_termcode(KS_DEC_MOUSE);
4017# endif
4018# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004019 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004020 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021# ifdef FEAT_GUI
4022 && !gui.in_use
4023# endif
4024 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004025 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 else
4027 del_mouse_termcode(KS_PTERM_MOUSE);
4028# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004029# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004030 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004031# ifdef FEAT_GUI
4032 && !gui.in_use
4033# endif
4034 )
4035 {
4036 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004037 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004038
4039 if (*p_mouse != NUL)
4040 {
4041 mch_setmouse(FALSE);
4042 setmouse();
4043 }
4044 }
4045 else
4046 del_mouse_termcode(KS_URXVT_MOUSE);
4047# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004048 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004049# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004050 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004051# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004052 )
4053 {
4054 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004055 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004056
4057 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004058 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004059
4060 if (*p_mouse != NUL)
4061 {
4062 mch_setmouse(FALSE);
4063 setmouse();
4064 }
4065 }
4066 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004067 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004068 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004069 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073#ifndef VMS
4074
4075/*
4076 * Try to get the current window size:
4077 * 1. with an ioctl(), most accurate method
4078 * 2. from the environment variables LINES and COLUMNS
4079 * 3. from the termcap
4080 * 4. keep using the old values
4081 * Return OK when size could be determined, FAIL otherwise.
4082 */
4083 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004084mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085{
4086 long rows = 0;
4087 long columns = 0;
4088 char_u *p;
4089
4090 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 * 1. try using an ioctl. It is the most accurate method.
4092 *
4093 * Try using TIOCGWINSZ first, some systems that have it also define
4094 * TIOCGSIZE but don't have a struct ttysize.
4095 */
4096# ifdef TIOCGWINSZ
4097 {
4098 struct winsize ws;
4099 int fd = 1;
4100
Bram Moolenaar0f873732019-12-05 20:28:46 +01004101 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 if (!isatty(fd) && isatty(read_cmd_fd))
4103 fd = read_cmd_fd;
4104 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4105 {
4106 columns = ws.ws_col;
4107 rows = ws.ws_row;
4108 }
4109 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004110# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111# ifdef TIOCGSIZE
4112 {
4113 struct ttysize ts;
4114 int fd = 1;
4115
Bram Moolenaar0f873732019-12-05 20:28:46 +01004116 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 if (!isatty(fd) && isatty(read_cmd_fd))
4118 fd = read_cmd_fd;
4119 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4120 {
4121 columns = ts.ts_cols;
4122 rows = ts.ts_lines;
4123 }
4124 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004125# endif // TIOCGSIZE
4126# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127
4128 /*
4129 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004130 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4131 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004133 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 {
4135 if ((p = (char_u *)getenv("LINES")))
4136 rows = atoi((char *)p);
4137 if ((p = (char_u *)getenv("COLUMNS")))
4138 columns = atoi((char *)p);
4139 }
4140
4141#ifdef HAVE_TGETENT
4142 /*
4143 * 3. try reading "co" and "li" entries from termcap
4144 */
4145 if (columns == 0 || rows == 0)
4146 getlinecol(&columns, &rows);
4147#endif
4148
4149 /*
4150 * 4. If everything fails, use the old values
4151 */
4152 if (columns <= 0 || rows <= 0)
4153 return FAIL;
4154
4155 Rows = rows;
4156 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004157 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return OK;
4159}
4160
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004161#if defined(FEAT_TERMINAL) || defined(PROTO)
4162/*
4163 * Report the windows size "rows" and "cols" to tty "fd".
4164 */
4165 int
4166mch_report_winsize(int fd, int rows, int cols)
4167{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004168 int tty_fd;
4169 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004170
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004171 tty_fd = get_tty_fd(fd);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004172 if (tty_fd < 0)
4173 return FAIL;
4174
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004175# if defined(TIOCSWINSZ)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004176 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004177
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004178 ws.ws_col = cols;
4179 ws.ws_row = rows;
4180 ws.ws_xpixel = cols * 5;
4181 ws.ws_ypixel = rows * 10;
4182 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4183 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4184 retval == 0 ? "success" : "failed");
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004185# elif defined(TIOCSSIZE)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004186 struct ttysize ts;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004187
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004188 ts.ts_cols = cols;
4189 ts.ts_lines = rows;
4190 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4191 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4192 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004193# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004194 if (tty_fd != fd)
4195 close(tty_fd);
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004196 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004197}
4198#endif
4199
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200/*
4201 * Try to set the window size to Rows and Columns.
4202 */
4203 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004204mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205{
4206 if (*T_CWS)
4207 {
4208 /*
4209 * NOTE: if you get an error here that term_set_winsize() is
4210 * undefined, check the output of configure. It could probably not
4211 * find a ncurses, termcap or termlib library.
4212 */
4213 term_set_winsize((int)Rows, (int)Columns);
4214 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004215 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217}
4218
Bram Moolenaar0f873732019-12-05 20:28:46 +01004219#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
4221/*
4222 * Rows and/or Columns has changed.
4223 */
4224 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004225mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004227 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228}
4229
Bram Moolenaar205b8862011-09-07 15:04:31 +02004230/*
4231 * Wait for process "child" to end.
4232 * Return "child" if it exited properly, <= 0 on error.
4233 */
4234 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004235wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004236{
4237 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004238 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004239
4240 while (wait_pid != child)
4241 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004242 // When compiled with Python threads are probably used, in which case
4243 // wait() sometimes hangs for no obvious reason. Use waitpid()
4244 // instead and loop (like the GUI). Also needed for other interfaces,
4245 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004246# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004247 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004248# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004249 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004250# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004251 if (wait_pid == 0)
4252 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004253 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004254 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004255 if (++delay_msec > 10)
4256 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004257 continue;
4258 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004259 if (wait_pid <= 0
4260# ifdef ECHILD
4261 && errno == ECHILD
4262# endif
4263 )
4264 break;
4265 }
4266 return wait_pid;
4267}
4268
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004269#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004270/*
4271 * Set the environment for a child process.
4272 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004273 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004274set_child_environment(
4275 long rows,
4276 long columns,
4277 char *term,
4278 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004279{
4280# ifdef HAVE_SETENV
4281 char envbuf[50];
4282# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004283 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004284 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004285 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004286 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004287 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004288# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004289 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004290# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004291# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004292 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004293# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004294# endif
4295
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004296# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004297 setenv("TERM", term, 1);
4298 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004299 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004300 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004301 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004302 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004303 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004304 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004305 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004306# ifdef FEAT_TERMINAL
4307 if (is_terminal)
4308 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004309 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004310 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4311 }
4312# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004313# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004314 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004315# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004316# else
4317 /*
4318 * Putenv does not copy the string, it has to remain valid.
4319 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004320 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004321 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004322 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4323 putenv(envbuf_Term);
4324 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004325 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004326 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4327 putenv(envbuf_Lines);
4328 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4329 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004330 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004331 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004332 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004333# ifdef FEAT_TERMINAL
4334 if (is_terminal)
4335 {
4336 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004337 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004338 putenv(envbuf_Version);
4339 }
4340# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004341# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004342 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4343 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4344 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004345# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004346# endif
4347}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004348
4349 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004350set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004351{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004352 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004353}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004354#endif
4355
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004356#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004357/*
4358 * Open a PTY, with FD for the master and slave side.
4359 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004360 * When successful both file descriptors are stored and the allocated pty name
4361 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004362 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004363 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004364open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004365{
4366 char *tty_name;
4367
Bram Moolenaar59386482019-02-10 22:43:46 +01004368 if (name1 != NULL)
4369 *name1 = NULL;
4370 if (name2 != NULL)
4371 *name2 = NULL;
4372
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004373 *pty_master_fd = mch_openpty(&tty_name); // open pty
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004374 if (*pty_master_fd < 0)
4375 return;
4376
4377 // Leaving out O_NOCTTY may lead to waitpid() always returning
4378 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4379 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004380#ifdef O_NOCTTY
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004381 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004382#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004383 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004384#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004385 if (*pty_slave_fd < 0)
4386 {
4387 close(*pty_master_fd);
4388 *pty_master_fd = -1;
4389 }
4390 else
4391 {
4392 if (name1 != NULL)
4393 *name1 = vim_strsave((char_u *)tty_name);
4394 if (name2 != NULL)
4395 *name2 = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004396 }
4397}
4398#endif
4399
Bram Moolenaarfae42832017-08-01 22:24:26 +02004400/*
4401 * Send SIGINT to a child process if "c" is an interrupt character.
4402 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004403 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004404may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4405{
4406# ifdef SIGINT
4407 if (c == Ctrl_C || c == intr_char)
4408 {
4409# ifdef HAVE_SETSID
4410 kill(-pid, SIGINT);
4411# else
4412 kill(0, SIGINT);
4413# endif
4414 if (wpid > 0)
4415 kill(wpid, SIGINT);
4416 }
4417# endif
4418}
4419
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004420#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004421
Bram Moolenaar0f873732019-12-05 20:28:46 +01004422/*
4423 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4424 * pointers, the last one is NULL.
4425 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4426 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004427 int
4428unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004429 char_u *cmd,
4430 char ***argvp,
4431 char_u **sh_tofree,
4432 char_u **shcf_tofree)
4433{
4434 char **argv = NULL;
4435 int argc;
4436
4437 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004438 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004439 return FAIL;
4440
4441 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4442 return FAIL;
4443 *argvp = argv;
4444
4445 if (cmd != NULL)
4446 {
4447 char_u *s;
4448 char_u *p;
4449
4450 if (extra_shell_arg != NULL)
4451 argv[argc++] = (char *)extra_shell_arg;
4452
Bram Moolenaar0f873732019-12-05 20:28:46 +01004453 // Break 'shellcmdflag' into white separated parts. This doesn't
4454 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004455 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004456 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004457 return FAIL;
4458 s = *shcf_tofree;
4459 p = p_shcf;
4460 while (*p != NUL)
4461 {
4462 argv[argc++] = (char *)s;
4463 while (*p && *p != ' ' && *p != TAB)
4464 *s++ = *p++;
4465 *s++ = NUL;
4466 p = skipwhite(p);
4467 }
4468
4469 argv[argc++] = (char *)cmd;
4470 }
4471 argv[argc] = NULL;
4472 return OK;
4473}
4474#endif
4475
4476#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4477/*
4478 * Use a terminal window to run a shell command in.
4479 */
4480 static int
4481mch_call_shell_terminal(
4482 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004483 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004484{
4485 jobopt_T opt;
4486 char **argv = NULL;
4487 char_u *tofree1 = NULL;
4488 char_u *tofree2 = NULL;
4489 int retval = -1;
4490 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004491 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004492 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004493 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004494
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004495 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004496 goto theend;
4497
4498 init_job_options(&opt);
4499 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4500 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004501 if (buf == NULL)
4502 goto theend;
4503
4504 job = term_getjob(buf->b_term);
4505 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004506
Bram Moolenaar0f873732019-12-05 20:28:46 +01004507 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004508 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004509 if (curbuf == buf)
Bram Moolenaar13568252018-03-16 20:46:58 +01004510 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004511 // Only when managed to find a window for "buf",
4512 clear_oparg(&oa);
4513 while (term_use_loop())
Bram Moolenaar13568252018-03-16 20:46:58 +01004514 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00004515 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4516 {
4517 // If terminal_loop() returns OK we got a key that is handled
4518 // in Normal model. We don't do redrawing anyway.
4519 if (terminal_loop(TRUE) == OK)
4520 normal_cmd(&oa, TRUE);
4521 }
4522 else
Bram Moolenaar13568252018-03-16 20:46:58 +01004523 normal_cmd(&oa, TRUE);
4524 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00004525 retval = job->jv_exitval;
4526 ch_log(NULL, "system command finished");
4527
4528 job_unref(job);
4529
4530 // restore curwin/curbuf and a few other things
4531 aucmd_restbuf(&aco);
Bram Moolenaar13568252018-03-16 20:46:58 +01004532 }
Bram Moolenaar13568252018-03-16 20:46:58 +01004533
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01004534 // Only require pressing Enter when redrawing, to avoid that system() gets
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004535 // the hit-enter prompt even though it didn't output anything.
4536 if (!RedrawingDisabled)
4537 wait_return(TRUE);
Bram Moolenaar13568252018-03-16 20:46:58 +01004538 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4539
4540theend:
4541 vim_free(argv);
4542 vim_free(tofree1);
4543 vim_free(tofree2);
4544 return retval;
4545}
4546#endif
4547
4548#ifdef USE_SYSTEM
4549/*
4550 * Use system() to start the shell: simple but slow.
4551 */
4552 static int
4553mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004554 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004555 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556{
4557#ifdef VMS
4558 char *ifn = NULL;
4559 char *ofn = NULL;
4560#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004561 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004562 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004563 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
4565 out_flush();
4566
4567 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004568 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569
Bram Moolenaar62b42182010-09-21 22:09:37 +02004570# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004571 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004572 loose_clipboard();
4573# endif
4574
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 if (cmd == NULL)
4576 x = system((char *)p_sh);
4577 else
4578 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004579# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if (ofn = strchr((char *)cmd, '>'))
4581 *ofn++ = '\0';
4582 if (ifn = strchr((char *)cmd, '<'))
4583 {
4584 char *p;
4585
4586 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004587 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if (p)
4589 *p = '\0';
4590 }
4591 if (ofn)
4592 x = vms_sys((char *)cmd, ofn, ifn);
4593 else
4594 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004595# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004596 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004598 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 if (newcmd == NULL)
4600 x = 0;
4601 else
4602 {
4603 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4604 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4605 (char *)p_shcf,
4606 (char *)cmd);
4607 x = system((char *)newcmd);
4608 vim_free(newcmd);
4609 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004610# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
4612# ifdef VMS
4613 x = vms_sys_status(x);
4614# endif
4615 if (emsg_silent)
4616 ;
4617 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004618 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 else if (x && !(options & SHELL_SILENT))
4620 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004621 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 msg_outnum((long)x);
4623 msg_putchar('\n');
4624 }
4625
4626 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004627 {
4628 // The shell may have messed with the mode, always set it.
4629 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004630 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004633# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4634 restore_clipboard();
4635# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004637}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
Bram Moolenaar0f873732019-12-05 20:28:46 +01004639#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
Bram Moolenaar0f873732019-12-05 20:28:46 +01004641# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4642 // 127, some shells use that already
4643# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644
Bram Moolenaar13568252018-03-16 20:46:58 +01004645/*
4646 * Don't use system(), use fork()/exec().
4647 */
4648 static int
4649mch_call_shell_fork(
4650 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004651 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004652{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004653 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004655 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 pid_t wait_pid = 0;
4657# ifdef HAVE_UNION_WAIT
4658 union wait status;
4659# else
4660 int status = -1;
4661# endif
4662 int retval = -1;
4663 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004664 char_u *tofree1 = NULL;
4665 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004667 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004668# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004670# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004671 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 int fd_fromshell[2];
4673 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004674 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
4676 out_flush();
4677 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004678 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004679 if (tmode == TMODE_RAW)
4680 // The shell may have messed with the mode, always set it later.
4681 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004683 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004684 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004685
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004687 * For the GUI, when writing the output into the buffer and when reading
4688 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4689 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004691 if ((options & (SHELL_READ|SHELL_WRITE))
4692# ifdef FEAT_GUI
4693 || (gui.in_use && show_shell_mess)
4694# endif
4695 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004697# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 /*
4699 * Try to open a master pty.
4700 * If this works, open the slave pty.
4701 * If the slave can't be opened, close the master pty.
4702 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004703 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004704 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 /*
4706 * If not opening a pty or it didn't work, try using pipes.
4707 */
4708 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
4711 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004712 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
4714 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004715 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
4717 close(fd_toshell[0]);
4718 close(fd_toshell[1]);
4719 }
4720 }
4721 if (pipe_error)
4722 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004723 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 out_flush();
4725 }
4726 }
4727 }
4728
Bram Moolenaar0f873732019-12-05 20:28:46 +01004729 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004731 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004732 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004733 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004734 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004736 UNBLOCK_SIGNALS(&curset);
4737
Bram Moolenaar32526b32019-01-19 17:43:09 +01004738 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004739 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004741 || (gui.in_use && show_shell_mess)
4742# endif
4743 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004745# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004746 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
4748 close(pty_master_fd);
4749 close(pty_slave_fd);
4750 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004751 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004752# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
4754 close(fd_toshell[0]);
4755 close(fd_toshell[1]);
4756 close(fd_fromshell[0]);
4757 close(fd_fromshell[1]);
4758 }
4759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004761 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004763 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004764 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00004766# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01004767 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004768 {
4769 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004770 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004771 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004772# endif
4773
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if (!show_shell_mess || (options & SHELL_EXPAND))
4775 {
4776 int fd;
4777
4778 /*
4779 * Don't want to show any message from the shell. Can't just
4780 * close stdout and stderr though, because some systems will
4781 * break if you try to write to them after that, so we must
4782 * use dup() to replace them with something else -- webb
4783 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4784 * waiting for input.
4785 */
4786 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4787 fclose(stdin);
4788 fclose(stdout);
4789 fclose(stderr);
4790
4791 /*
4792 * If any of these open()'s and dup()'s fail, we just continue
4793 * anyway. It's not fatal, and on most systems it will make
4794 * no difference at all. On a few it will cause the execvp()
4795 * to exit with a non-zero status even when the completion
4796 * could be done, which is nothing too serious. If the open()
4797 * or dup() failed we'd just do the same thing ourselves
4798 * anyway -- webb
4799 */
4800 if (fd >= 0)
4801 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004802 vim_ignored = dup(fd); // To replace stdin (fd 0)
4803 vim_ignored = dup(fd); // To replace stdout (fd 1)
4804 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805
Bram Moolenaar0f873732019-12-05 20:28:46 +01004806 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 close(fd);
4808 }
4809 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004810 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004812 || gui.in_use
4813# endif
4814 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
4816
Bram Moolenaardf177f62005-02-22 08:39:57 +00004817# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004818 // Create our own process group, so that the child and all its
4819 // children can be kill()ed. Don't do this when using pipes,
4820 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004821 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004822 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004823 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004824# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004825 // When doing "!xterm&" and 'shell' is bash: the shell
4826 // will exit and send SIGHUP to all processes in its
4827 // group, killing the just started process. Ignore SIGHUP
4828 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004829 signal(SIGHUP, SIG_IGN);
4830# endif
4831 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004832# endif
4833# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004834 if (pty_slave_fd >= 0)
4835 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004836 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004837 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004838 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004839# ifdef TIOCSCTTY
4840 // Try to become controlling tty (probably doesn't work,
4841 // unless run by root)
4842 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4843# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004844 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004845# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004846 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
Bram Moolenaara5792f52005-11-23 21:25:05 +00004848 /*
4849 * stderr is only redirected when using the GUI, so that a
4850 * program like gpg can still access the terminal to get a
4851 * passphrase using stderr.
4852 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004853# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 if (pty_master_fd >= 0)
4855 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004856 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857
Bram Moolenaar0f873732019-12-05 20:28:46 +01004858 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004860 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004862 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004863 if (gui.in_use)
4864 {
4865 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004866 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
Bram Moolenaar0f873732019-12-05 20:28:46 +01004869 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004874 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 close(fd_toshell[1]);
4876 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004877 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 close(fd_toshell[0]);
4879
Bram Moolenaar0f873732019-12-05 20:28:46 +01004880 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 close(fd_fromshell[0]);
4882 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004883 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 close(fd_fromshell[1]);
4885
Bram Moolenaara5792f52005-11-23 21:25:05 +00004886# ifdef FEAT_GUI
4887 if (gui.in_use)
4888 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004889 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004890 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004891 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004892 }
4893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 }
4895 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004896
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 /*
4898 * There is no type cast for the argv, because the type may be
4899 * different on different machines. This may cause a warning
4900 * message with strict compilers, don't worry about it.
4901 * Call _exit() instead of exit() to avoid closing the connection
4902 * to the X server (esp. with GTK, which uses atexit()).
4903 */
4904 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004905 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004907 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
4909 /*
4910 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004911 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 */
4913 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004914 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004915 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004916# ifdef FEAT_JOB_CHANNEL
4917 ++dont_check_job_ended;
4918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 /*
4920 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004921 * This is also used to pipe stdin/stdout to/from the external
4922 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004924 if ((options & (SHELL_READ|SHELL_WRITE))
4925# ifdef FEAT_GUI
4926 || (gui.in_use && show_shell_mess)
4927# endif
4928 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004930# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004932 int buffer_off = 0; // valid bytes in buffer[]
4933 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4934 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 int len;
4936 int p_more_save;
4937 int old_State;
4938 int c;
4939 int toshell_fd;
4940 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004941 garray_T ga;
4942 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004943# ifdef ELAPSED_FUNC
4944 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
Bram Moolenaardf177f62005-02-22 08:39:57 +00004947# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 if (pty_master_fd >= 0)
4949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 fromshell_fd = pty_master_fd;
4951 toshell_fd = dup(pty_master_fd);
4952 }
4953 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 {
4956 close(fd_toshell[0]);
4957 close(fd_fromshell[1]);
4958 toshell_fd = fd_toshell[1];
4959 fromshell_fd = fd_fromshell[0];
4960 }
4961
4962 /*
4963 * Write to the child if there are typed characters.
4964 * Read from the child if there are characters available.
4965 * Repeat the reading a few times if more characters are
4966 * available. Need to check for typed keys now and then, but
4967 * not too often (delays when no chars are available).
4968 * This loop is quit if no characters can be read from the pty
4969 * (WaitForChar detected special condition), or there are no
4970 * characters available and the child has exited.
4971 * Only check if the child has exited when there is no more
4972 * output. The child may exit before all the output has
4973 * been printed.
4974 *
4975 * Currently this busy loops!
4976 * This can probably dead-lock when the write blocks!
4977 */
4978 p_more_save = p_more;
4979 p_more = FALSE;
4980 old_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004981 State = MODE_EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004983 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004984 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004985 // Fork a process that will write the lines to the
4986 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004987 if ((wpid = fork()) == -1)
4988 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004989 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004990 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004991 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004992 {
4993 linenr_T lnum = curbuf->b_op_start.lnum;
4994 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004995 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004996 size_t l;
4997
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004998 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004999 for (;;)
5000 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005001 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005002 if (l == 0)
5003 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005004 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005005 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00005006 len = write(toshell_fd, "", (size_t)1);
5007 else
5008 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005009 char_u *s = vim_strchr(lp + written, NL);
5010
Bram Moolenaar89d40322006-08-29 15:30:07 +00005011 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00005012 s == NULL ? l
5013 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005014 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00005015 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005016 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005017 // Finished a line, add a NL, unless this line
5018 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005019 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005020 || (!curbuf->b_p_bin
5021 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005022 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaar88456cd2022-11-18 22:14:09 +00005023 && (lnum != curbuf->b_ml.ml_line_count
Bram Moolenaardf177f62005-02-22 08:39:57 +00005024 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005025 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005026 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005027 ++lnum;
5028 if (lnum > curbuf->b_op_end.lnum)
5029 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005030 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005031 close(toshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005032 break;
5033 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005034 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005035 written = 0;
5036 }
5037 else if (len > 0)
5038 written += len;
5039 }
5040 _exit(0);
5041 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005042 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005043 {
5044 close(toshell_fd);
5045 toshell_fd = -1;
5046 }
5047 }
5048
5049 if (options & SHELL_READ)
5050 ga_init2(&ga, 1, BUFLEN);
5051
5052 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005053# ifdef ELAPSED_FUNC
5054 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 for (;;)
5057 {
5058 /*
5059 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005060 * if there are any.
5061 * Don't do this if we are expanding wild cards (would eat
5062 * typeahead).
5063 * Don't do this when filtering and terminal is in cooked
5064 * mode, the shell command will handle the I/O. Avoids
5065 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005066 * Don't get characters when the child has already
5067 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005068 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005069 * while (noread_cnt > 4), avoids that ":r !ls" eats
5070 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 */
5072 len = 0;
5073 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005074 && ((options &
5075 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5076 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005077# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005078 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005079# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005080 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005081 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005082 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005084 if (ta_len == 0)
5085 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005086 // Get extra characters when we don't have any.
5087 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005088 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005089# ifdef ELAPSED_FUNC
5090 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005091# endif
5092 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5093 }
5094 if (ta_len > 0 || len > 0)
5095 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 /*
5097 * For pipes:
5098 * Check for CTRL-C: send interrupt signal to child.
5099 * Check for CTRL-D: EOF, close pipe to child.
5100 */
5101 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5102 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 /*
5104 * Send SIGINT to the child's group or all
5105 * processes in our group.
5106 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005107 may_send_sigint(ta_buf[ta_len], pid, wpid);
5108
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 if (pty_master_fd < 0 && toshell_fd >= 0
5110 && ta_buf[ta_len] == Ctrl_D)
5111 {
5112 close(toshell_fd);
5113 toshell_fd = -1;
5114 }
5115 }
5116
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01005117 // Remove Vim-specific codes from the input.
5118 len = term_replace_keycodes(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119
5120 /*
5121 * For pipes: echo the typed characters.
5122 * For a pty this does not seem to work.
5123 */
5124 if (pty_master_fd < 0)
5125 {
5126 for (i = ta_len; i < ta_len + len; ++i)
5127 {
5128 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5129 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 else if (has_mbyte)
5131 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005132 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
5134 msg_outtrans_len(ta_buf + i, l);
5135 i += l - 1;
5136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 else
5138 msg_outtrans_len(ta_buf + i, 1);
5139 }
5140 windgoto(msg_row, msg_col);
5141 out_flush();
5142 }
5143
5144 ta_len += len;
5145
5146 /*
5147 * Write the characters to the child, unless EOF has
5148 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005149 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005150 * When writing buffer lines, drop the typed
5151 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005153 if (options & SHELL_WRITE)
5154 ta_len = 0;
5155 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
5157 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5158 if (len > 0)
5159 {
5160 ta_len -= len;
5161 mch_memmove(ta_buf, ta_buf + len, ta_len);
5162 }
5163 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
5166
Bram Moolenaardf177f62005-02-22 08:39:57 +00005167 if (got_int)
5168 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005169 // CTRL-C sends a signal to the child, we ignore it
5170 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005171# ifdef HAVE_SETSID
5172 kill(-pid, SIGINT);
5173# else
5174 kill(0, SIGINT);
5175# endif
5176 if (wpid > 0)
5177 kill(wpid, SIGINT);
5178 got_int = FALSE;
5179 }
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 /*
5182 * Check if the child has any characters to be printed.
5183 * Read them and write them to our window. Repeat this as
5184 * long as there is something to do, avoid the 10ms wait
5185 * for mch_inchar(), or sending typeahead characters to
5186 * the external process.
5187 * TODO: This should handle escape sequences, compatible
5188 * to some terminal (vt52?).
5189 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005190 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005191 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005193 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005196 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005198
5199 noread_cnt = 0;
5200 if (options & SHELL_READ)
5201 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005202 // Do NUL -> NL translation, append NL separated
5203 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005204 for (i = 0; i < len; ++i)
5205 {
5206 if (buffer[i] == NL)
5207 append_ga_line(&ga);
5208 else if (buffer[i] == NUL)
5209 ga_append(&ga, NL);
5210 else
5211 ga_append(&ga, buffer[i]);
5212 }
5213 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005214 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
5216 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005217 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218
Bram Moolenaardf177f62005-02-22 08:39:57 +00005219 len += buffer_off;
5220 buffer[len] = NUL;
5221
Bram Moolenaar0f873732019-12-05 20:28:46 +01005222 // Check if the last character in buffer[] is
5223 // incomplete, keep these bytes for the next
5224 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 for (p = buffer; p < buffer + len; p += l)
5226 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005227 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005229 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 else if (MB_BYTE2LEN(*p) != l)
5231 break;
5232 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005233 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005235 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 if (len >= 12)
5237 ++p;
5238 else
5239 {
5240 buffer_off = len;
5241 continue;
5242 }
5243 }
5244 c = *p;
5245 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005246 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 if (p < buffer + len)
5248 {
5249 *p = c;
5250 buffer_off = (buffer + len) - p;
5251 mch_memmove(buffer, p, buffer_off);
5252 continue;
5253 }
5254 buffer_off = 0;
5255 }
5256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
5258 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005259 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 }
5261
5262 windgoto(msg_row, msg_col);
5263 cursor_on();
5264 out_flush();
5265 if (got_int)
5266 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005267
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005268# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005269 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005270 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005271 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005272
Bram Moolenaar0f873732019-12-05 20:28:46 +01005273 // Avoid that we keep looping here without
5274 // checking for a CTRL-C for a long time. Don't
5275 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005276 if (msec > 2000)
5277 {
5278 noread_cnt = 5;
5279 break;
5280 }
5281 }
5282# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 }
5284
Bram Moolenaar0f873732019-12-05 20:28:46 +01005285 // If we already detected the child has finished, continue
5286 // reading output for a short while. Some text may be
5287 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005288 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005289 {
5290 if (noread_cnt < 5)
5291 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005292 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005293 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005294
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 /*
5296 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005297 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005299# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005300 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005301# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5305 || (wait_pid == pid && WIFEXITED(status)))
5306 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005307 // Don't break the loop yet, try reading more
5308 // characters from "fromshell_fd" first. When using
5309 // pipes there might still be something to read and
5310 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005313 else
5314 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005315
Bram Moolenaar95a51352013-03-21 22:53:50 +01005316# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005317 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005318 clip_update();
5319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
5321finished:
5322 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005323 if (options & SHELL_READ)
5324 {
5325 if (ga.ga_len > 0)
5326 {
5327 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005328 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005329 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005330 }
5331 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005332 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005333 ga_clear(&ga);
5334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 /*
5337 * Give all typeahead that wasn't used back to ui_inchar().
5338 */
5339 if (ta_len)
5340 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 State = old_State;
5342 if (toshell_fd >= 0)
5343 close(toshell_fd);
5344 close(fromshell_fd);
5345 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005346# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005347 else
5348 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005349 long delay_msec = 1;
5350
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005351 if (tmode == TMODE_RAW)
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005352 // Possibly disables modifyOtherKeys, so that the system
5353 // can recognize CTRL-C.
5354 out_str_t_TE();
Bram Moolenaar0981c872020-08-23 14:28:37 +02005355
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005356 /*
5357 * Similar to the loop above, but only handle X events, no
5358 * I/O.
5359 */
5360 for (;;)
5361 {
5362 if (got_int)
5363 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005364 // CTRL-C sends a signal to the child, we ignore it
5365 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005366# ifdef HAVE_SETSID
5367 kill(-pid, SIGINT);
5368# else
5369 kill(0, SIGINT);
5370# endif
5371 got_int = FALSE;
5372 }
5373# ifdef __NeXT__
5374 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5375# else
5376 wait_pid = waitpid(pid, &status, WNOHANG);
5377# endif
5378 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5379 || (wait_pid == pid && WIFEXITED(status)))
5380 {
5381 wait_pid = pid;
5382 break;
5383 }
5384
Bram Moolenaar0f873732019-12-05 20:28:46 +01005385 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005386 clip_update();
5387
Bram Moolenaar0f873732019-12-05 20:28:46 +01005388 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005389 // less time, gradually wait longer.
5390 mch_delay(delay_msec,
5391 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005392 if (++delay_msec > 10)
5393 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005394 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005395
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005396 if (tmode == TMODE_RAW)
5397 // possibly enables modifyOtherKeys again
Bram Moolenaar733a69b2022-12-01 12:03:47 +00005398 out_str_t_TI();
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005399 }
5400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401
5402 /*
5403 * Wait until our child has exited.
5404 * Ignore wait() returning pids of other children and returning
5405 * because of some signal like SIGWINCH.
5406 * Don't wait if wait_pid was already set above, indicating the
5407 * child already exited.
5408 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005409 if (wait_pid != pid)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01005410 (void)wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
Bram Moolenaar624891f2010-10-13 16:22:09 +02005412# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005413 // Close slave side of pty. Only do this after the child has
5414 // exited, otherwise the child may hang when it tries to write on
5415 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005416 if (pty_master_fd >= 0)
5417 close(pty_slave_fd);
5418# endif
5419
Bram Moolenaar0f873732019-12-05 20:28:46 +01005420 // Make sure the child that writes to the external program is
5421 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005422 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005423 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005424 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005425 wait4pid(wpid, NULL);
5426 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005427
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005428# ifdef FEAT_JOB_CHANNEL
5429 --dont_check_job_ended;
5430# endif
5431
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 /*
5433 * Set to raw mode right now, otherwise a CTRL-C after
5434 * catch_signals() will kill Vim.
5435 */
5436 if (tmode == TMODE_RAW)
5437 settmode(TMODE_RAW);
5438 did_settmode = TRUE;
5439 set_signals();
5440
5441 if (WIFEXITED(status))
5442 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005443 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005445 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 {
5447 if (retval == EXEC_FAILED)
5448 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005449 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 msg_outtrans(p_sh);
5451 msg_putchar('\n');
5452 }
5453 else if (!(options & SHELL_SILENT))
5454 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005455 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 msg_outnum((long)retval);
5457 msg_putchar('\n');
5458 }
5459 }
5460 }
5461 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005462 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 }
5464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
5466error:
5467 if (!did_settmode)
5468 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005469 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005471 vim_free(argv);
5472 vim_free(tofree1);
5473 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
5475 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005476}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005477#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005478
5479 int
5480mch_call_shell(
5481 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005482 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005483{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005484#ifdef FEAT_EVAL
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01005485 ch_log(NULL, "executing shell command: %s", cmd);
5486#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005487#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar524c8532022-09-27 15:48:20 +01005488 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL
5489 && (options & SHELL_SILENT) == 0)
Bram Moolenaar13568252018-03-16 20:46:58 +01005490 return mch_call_shell_terminal(cmd, options);
5491#endif
5492#ifdef USE_SYSTEM
5493 return mch_call_shell_system(cmd, options);
5494#else
5495 return mch_call_shell_fork(cmd, options);
5496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497}
5498
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005499#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005500 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005501mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005502{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005503 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005504 int fd_in[2] = {-1, -1}; // for stdin
5505 int fd_out[2] = {-1, -1}; // for stdout
5506 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005507 int pty_master_fd = -1;
5508 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005509 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005510 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5511 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5512 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005513 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005514 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5515 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005516 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005517 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005518 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005519
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005520 if (use_out_for_err && use_null_for_out)
5521 use_null_for_err = TRUE;
5522
Bram Moolenaar0f873732019-12-05 20:28:46 +01005523 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005524 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005525
Bram Moolenaarb2412082017-08-20 18:09:14 +02005526 if (options->jo_pty
5527 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005528 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005529 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005530 open_pty(&pty_master_fd, &pty_slave_fd,
5531 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005532
Bram Moolenaar0f873732019-12-05 20:28:46 +01005533 // TODO: without the channel feature connect the child to /dev/null?
5534 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005535 if (use_file_for_in)
5536 {
5537 char_u *fname = options->jo_io_name[PART_IN];
5538
5539 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5540 if (fd_in[0] < 0)
5541 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005542 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005543 goto failed;
5544 }
5545 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005546 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005547 // When writing buffer lines to the input don't use the pty, so that
5548 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005549 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5550 && pipe(fd_in) < 0)
5551 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005552
5553 if (use_file_for_out)
5554 {
5555 char_u *fname = options->jo_io_name[PART_OUT];
5556
5557 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5558 if (fd_out[1] < 0)
5559 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005560 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005561 goto failed;
5562 }
5563 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005564 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005565 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005566
5567 if (use_file_for_err)
5568 {
5569 char_u *fname = options->jo_io_name[PART_ERR];
5570
5571 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5572 if (fd_err[1] < 0)
5573 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005574 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005575 goto failed;
5576 }
5577 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005578 else if (!use_out_for_err && !use_null_for_err
5579 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005580 goto failed;
5581
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005582 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5583 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005584 if (options->jo_set & JO_CHANNEL)
5585 {
5586 channel = options->jo_channel;
5587 if (channel != NULL)
5588 ++channel->ch_refcount;
5589 }
5590 else
5591 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005592 if (channel == NULL)
5593 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005594 if (job->jv_tty_out != NULL)
5595 ch_log(channel, "using pty %s on fd %d",
5596 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005597 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005598
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005599 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005600 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005601 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005602 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005603 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005604 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005605 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005606 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005607 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005608 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005609 int null_fd = -1;
5610 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005611
Bram Moolenaar0f873732019-12-05 20:28:46 +01005612 // child
5613 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005614 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005615
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005616# ifdef FEAT_EVAL
Bram Moolenaar819524702018-02-27 19:10:00 +01005617 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005618 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005619 ch_logfile((char_u *)"", (char_u *)"");
5620# endif
5621
Bram Moolenaar835dc632016-02-07 14:27:38 +01005622# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005623 // Create our own process group, so that the child and all its
5624 // children can be kill()ed. Don't do this when using pipes,
5625 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005626 (void)setsid();
5627# endif
5628
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005629# ifdef FEAT_TERMINAL
5630 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005631 {
5632 char *term = (char *)T_NAME;
5633
5634#ifdef FEAT_GUI
5635 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005636 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005637 term = getenv("TERM");
5638#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005639 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005640 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005641 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005642 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005643 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005644 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005645 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005646 else if (t_colors > 16)
5647 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005648 else
5649 term = "xterm";
5650 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005651 set_child_environment(
5652 (long)options->jo_term_rows,
5653 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005654 term,
5655 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005656 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005657 else
5658# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005659 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005660
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005661 if (options->jo_env != NULL)
5662 {
5663 dict_T *dict = options->jo_env;
5664 hashitem_T *hi;
5665 int todo = (int)dict->dv_hashtab.ht_used;
5666
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005667 FOR_ALL_HASHTAB_ITEMS(&dict->dv_hashtab, hi, todo)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005668 if (!HASHITEM_EMPTY(hi))
5669 {
5670 typval_T *item = &dict_lookup(hi)->di_tv;
5671
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005672 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005673 --todo;
5674 }
5675 }
5676
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005677 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005678 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005679 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005680 if (null_fd < 0)
5681 {
5682 perror("opening /dev/null failed");
5683 _exit(OPEN_NULL_FAILED);
5684 }
5685 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005686
Bram Moolenaar223896d2017-08-02 22:33:28 +02005687 if (pty_slave_fd >= 0)
5688 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005689 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005690 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005691# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005692 // Try to become controlling tty (probably doesn't work,
5693 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005694 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5695# endif
5696 }
5697
Bram Moolenaar0f873732019-12-05 20:28:46 +01005698 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005699 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005700 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005701 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005702 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005703 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005704 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005705 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005706
Bram Moolenaar0f873732019-12-05 20:28:46 +01005707 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005708 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005709 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005710 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005711 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005712 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005713 }
5714 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005715 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005716 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005717 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005718 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005719 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005720
Bram Moolenaar0f873732019-12-05 20:28:46 +01005721 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005722 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005723 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005724 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005725 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005726 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005727 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005728 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005729
5730 if (fd_in[0] >= 0)
5731 close(fd_in[0]);
5732 if (fd_in[1] >= 0)
5733 close(fd_in[1]);
5734 if (fd_out[0] >= 0)
5735 close(fd_out[0]);
5736 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005737 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005738 if (fd_err[0] >= 0)
5739 close(fd_err[0]);
5740 if (fd_err[1] >= 0)
5741 close(fd_err[1]);
5742 if (pty_master_fd >= 0)
5743 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005744 close(pty_master_fd); // not used in the child
5745 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005746 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005747
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005748 if (null_fd >= 0)
5749 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005750
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005751 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5752 _exit(EXEC_FAILED);
5753
Bram Moolenaar0f873732019-12-05 20:28:46 +01005754 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005755 execvp(argv[0], argv);
5756
Bram Moolenaar4694a172016-04-21 14:05:23 +02005757 if (stderr_works)
5758 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005759# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005760 // calling free_all_mem() here causes problems. Ignore valgrind
5761 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005762# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005763 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005764 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005765
Bram Moolenaar0f873732019-12-05 20:28:46 +01005766 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005767 UNBLOCK_SIGNALS(&curset);
5768
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005769 job->jv_pid = pid;
5770 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005771 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005772
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005773 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005774 close(pty_slave_fd); // not used in the parent
5775 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005776 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005777 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005778 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005779 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005780 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005781 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005782 if (channel != NULL)
5783 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005784 int in_fd = INVALID_FD;
5785 int out_fd = INVALID_FD;
5786 int err_fd = INVALID_FD;
5787
5788 if (!(use_file_for_in || use_null_for_in))
5789 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5790
5791 if (!(use_file_for_out || use_null_for_out))
5792 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5793
5794 // When using pty_master_fd only set it for stdout, do not duplicate
5795 // it for stderr, it only needs to be read once.
5796 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5797 {
5798 if (fd_err[0] >= 0)
5799 err_fd = fd_err[0];
5800 else if (out_fd != pty_master_fd)
5801 err_fd = pty_master_fd;
5802 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005803
5804 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005805 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005806 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005807 else
5808 {
5809 if (fd_in[1] >= 0)
5810 close(fd_in[1]);
5811 if (fd_out[0] >= 0)
5812 close(fd_out[0]);
5813 if (fd_err[0] >= 0)
5814 close(fd_err[0]);
5815 if (pty_master_fd >= 0)
5816 close(pty_master_fd);
5817 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005818
Bram Moolenaar0f873732019-12-05 20:28:46 +01005819 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005820 return;
5821
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005822failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005823 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005824 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005825 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005826 if (fd_in[1] >= 0)
5827 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005828 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005829 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005830 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005831 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005832 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005833 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005834 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005835 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005836 if (pty_master_fd >= 0)
5837 close(pty_master_fd);
5838 if (pty_slave_fd >= 0)
5839 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005840}
5841
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005842 static char_u *
5843get_signal_name(int sig)
5844{
5845 int i;
5846 char_u numbuf[NUMBUFLEN];
5847
5848 if (sig == SIGKILL)
5849 return vim_strsave((char_u *)"kill");
5850
5851 for (i = 0; signal_info[i].sig != -1; i++)
5852 if (sig == signal_info[i].sig)
5853 return strlow_save((char_u *)signal_info[i].name);
5854
5855 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5856 return vim_strsave(numbuf);
5857}
5858
Bram Moolenaar835dc632016-02-07 14:27:38 +01005859 char *
5860mch_job_status(job_T *job)
5861{
5862# ifdef HAVE_UNION_WAIT
5863 union wait status;
5864# else
5865 int status = -1;
5866# endif
5867 pid_t wait_pid = 0;
5868
5869# ifdef __NeXT__
5870 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5871# else
5872 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5873# endif
5874 if (wait_pid == -1)
5875 {
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00005876 int waitpid_errno = errno;
5877 if (waitpid_errno == ECHILD && mch_process_running(job->jv_pid))
5878 // The process is alive, but it was probably reparented (for
5879 // example by ptrace called by a debugger like lldb or gdb).
5880 // Note: This assumes that process IDs are not reused.
5881 return "run";
5882
Bram Moolenaar0f873732019-12-05 20:28:46 +01005883 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005884 if (job->jv_status < JOB_ENDED)
5885 ch_log(job->jv_channel, "Job no longer exists: %s",
Bram Moolenaar5c6a3c92023-03-04 13:23:26 +00005886 strerror(waitpid_errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005887 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005888 }
5889 if (wait_pid == 0)
5890 return "run";
5891 if (WIFEXITED(status))
5892 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005893 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005894 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005895 if (job->jv_status < JOB_ENDED)
5896 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005897 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005898 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005899 if (WIFSIGNALED(status))
5900 {
5901 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005902 job->jv_termsig = get_signal_name(WTERMSIG(status));
5903 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5904 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5905 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005906 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005907 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005908 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005909
5910return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005911 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005912 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005913 return "dead";
5914}
5915
5916 job_T *
5917mch_detect_ended_job(job_T *job_list)
5918{
5919# ifdef HAVE_UNION_WAIT
5920 union wait status;
5921# else
5922 int status = -1;
5923# endif
5924 pid_t wait_pid = 0;
5925 job_T *job;
5926
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005927# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005928 // Do not do this when waiting for a shell command to finish, we would get
5929 // the exit value here (and discard it), the exit value obtained there
5930 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005931 if (dont_check_job_ended > 0)
5932 return NULL;
5933# endif
5934
Bram Moolenaar97792de2016-10-15 18:36:49 +02005935# ifdef __NeXT__
5936 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5937# else
5938 wait_pid = waitpid(-1, &status, WNOHANG);
5939# endif
5940 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005941 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005942 return NULL;
5943 for (job = job_list; job != NULL; job = job->jv_next)
5944 {
5945 if (job->jv_pid == wait_pid)
5946 {
5947 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005948 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005949 job->jv_exitval = WEXITSTATUS(status);
5950 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005951 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005952 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005953 job->jv_termsig = get_signal_name(WTERMSIG(status));
5954 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005955 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005956 {
5957 ch_log(job->jv_channel, "Job ended");
5958 job->jv_status = JOB_ENDED;
5959 }
5960 return job;
5961 }
5962 }
5963 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005964}
5965
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005966/*
5967 * Send a (deadly) signal to "job".
5968 * Return FAIL if "how" is not a valid name.
5969 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005970 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005971mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005972{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005973 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005974
Bram Moolenaar923d9262016-02-25 20:56:01 +01005975 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005976 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005977 else if (STRCMP(how, "hup") == 0)
5978 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005979 else if (STRCMP(how, "quit") == 0)
5980 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005981 else if (STRCMP(how, "int") == 0)
5982 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005983 else if (STRCMP(how, "kill") == 0)
5984 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005985#ifdef SIGWINCH
5986 else if (STRCMP(how, "winch") == 0)
5987 sig = SIGWINCH;
5988#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005989 else if (isdigit(*how))
5990 sig = atoi((char *)how);
5991 else
5992 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005993
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005994 // Never kill ourselves!
5995 if (job->jv_pid != 0)
5996 {
5997 // TODO: have an option to only kill the process, not the group?
5998 kill(-job->jv_pid, sig);
5999 kill(job->jv_pid, sig);
6000 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01006001
Bram Moolenaar835dc632016-02-07 14:27:38 +01006002 return OK;
6003}
Bram Moolenaar76467df2016-02-12 19:30:26 +01006004
6005/*
6006 * Clear the data related to "job".
6007 */
6008 void
6009mch_clear_job(job_T *job)
6010{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006011 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01006012# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006013 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006014# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006015 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006016# endif
6017}
Bram Moolenaar835dc632016-02-07 14:27:38 +01006018#endif
6019
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006020#if defined(FEAT_TERMINAL) || defined(PROTO)
6021 int
6022mch_create_pty_channel(job_T *job, jobopt_T *options)
6023{
6024 int pty_master_fd = -1;
6025 int pty_slave_fd = -1;
6026 channel_T *channel;
6027
Bram Moolenaar59386482019-02-10 22:43:46 +01006028 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02006029 if (pty_master_fd < 0 || pty_slave_fd < 0)
6030 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006031 close(pty_slave_fd);
6032
6033 channel = add_channel();
6034 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006035 {
6036 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006037 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006038 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006039 if (job->jv_tty_out != NULL)
6040 ch_log(channel, "using pty %s on fd %d",
6041 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006042 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006043 channel->ch_keep_open = TRUE;
6044
Bram Moolenaar0f873732019-12-05 20:28:46 +01006045 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6046 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006047 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006048 channel_set_job(channel, job, options);
6049 return OK;
6050}
6051#endif
6052
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053/*
6054 * Check for CTRL-C typed by reading all available characters.
6055 * In cooked mode we should get SIGINT, no need to check.
6056 */
6057 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006058mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006060 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006061 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 fill_input_buf(FALSE);
6063}
6064
6065/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006066 * Wait "msec" msec until a character is available from the mouse, keyboard,
6067 * from inbuf[].
6068 * "msec" == -1 will block forever.
6069 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006070 * When "ignore_input" is TRUE even check for pending input when input is
6071 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006072 * "interrupted" (if not NULL) is set to TRUE when no character is available
6073 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006074 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006075 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 */
6077 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006078WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006080#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006081 return ui_wait_for_chars_or_timer(
6082 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006083#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006084 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006085#endif
6086}
6087
6088/*
6089 * Wait "msec" msec until a character is available from the mouse or keyboard
6090 * or from inbuf[].
6091 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006092 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006093 * "interrupted" (if not NULL) is set to TRUE when no character is available
6094 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006095 * When a GUI is being used, this will never get called -- webb
6096 */
6097 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006098WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006099{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100#ifdef FEAT_MOUSE_GPM
6101 int gpm_process_wanted;
6102#endif
6103#ifdef FEAT_XCLIPBOARD
6104 int rest;
6105#endif
6106 int avail;
6107
Bram Moolenaar0f873732019-12-05 20:28:46 +01006108 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 return 1;
6110
6111#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006112 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 if (WantQueryMouse)
6114 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006115 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006116 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006117 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 }
6119#endif
6120
6121 /*
6122 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6123 * events. This is a bit complicated, because they might both be defined.
6124 */
6125#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6126# ifdef FEAT_XCLIPBOARD
6127 rest = 0;
6128 if (do_xterm_trace())
6129 rest = msec;
6130# endif
6131 do
6132 {
6133# ifdef FEAT_XCLIPBOARD
6134 if (rest != 0)
6135 {
6136 msec = XT_TRACE_DELAY;
6137 if (rest >= 0 && rest < XT_TRACE_DELAY)
6138 msec = rest;
6139 if (rest >= 0)
6140 rest -= msec;
6141 }
6142# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01006143# ifdef FEAT_SOUND_MACOSX
6144 // Invoke any pending sound callbacks.
6145 process_cfrunloop();
6146# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006147# ifdef FEAT_SOUND_CANBERRA
6148 // Invoke any pending sound callbacks.
6149 if (has_sound_callback_in_queue())
6150 invoke_sound_callback();
6151# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152# ifdef FEAT_MOUSE_GPM
6153 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006154 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006155 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006156 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006158 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006162 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 return 1;
6164# ifdef FEAT_XCLIPBOARD
6165 if (rest == 0 || !do_xterm_trace())
6166# endif
6167 break;
6168 }
6169 }
6170 while (FALSE
6171# ifdef FEAT_MOUSE_GPM
6172 || (gpm_process_wanted && mch_gpm_process() == 0)
6173# endif
6174# ifdef FEAT_XCLIPBOARD
6175 || (!avail && rest != 0)
6176# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006177 )
6178 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
6180#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006181 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182#endif
6183 return avail;
6184}
6185
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006186#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187/*
6188 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006189 * "msec" == 0 will check for characters once.
6190 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006193 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006194 * "interrupted" (if not NULL) is set to TRUE when no character is available
6195 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006197 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006198RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
6200 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006201 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006202#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 static int busy = FALSE;
6204
Bram Moolenaar0f873732019-12-05 20:28:46 +01006205 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206# define MAY_LOOP
6207
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006208# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006209 // Remember at what time we started, so that we know how much longer we
6210 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006211 long start_msec = msec;
6212 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006214 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006215 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216# endif
6217
Bram Moolenaar0f873732019-12-05 20:28:46 +01006218 // Handle being called recursively. This may happen for the session
6219 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 if (busy)
6221 return 0;
6222#endif
6223
6224#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006225 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226#endif
6227 {
6228#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006229 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006230# ifdef FEAT_MZSCHEME
6231 int mzquantum_used = FALSE;
6232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233#endif
6234#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006235 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006236 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 int nfd;
6238# ifdef FEAT_XCLIPBOARD
6239 int xterm_idx = -1;
6240# endif
6241# ifdef FEAT_MOUSE_GPM
6242 int gpm_idx = -1;
6243# endif
6244# ifdef USE_XSMP
6245 int xsmp_idx = -1;
6246# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006247 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006249# ifdef FEAT_MZSCHEME
6250 mzvim_check_threads();
6251 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6252 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006253 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006254 mzquantum_used = TRUE;
6255 }
6256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 fds[0].fd = fd;
6258 fds[0].events = POLLIN;
6259 nfd = 1;
6260
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006262 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 if (xterm_Shell != (Widget)0)
6264 {
6265 xterm_idx = nfd;
6266 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6267 fds[nfd].events = POLLIN;
6268 nfd++;
6269 }
6270# endif
6271# ifdef FEAT_MOUSE_GPM
6272 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6273 {
6274 gpm_idx = nfd;
6275 fds[nfd].fd = gpm_fd;
6276 fds[nfd].events = POLLIN;
6277 nfd++;
6278 }
6279# endif
6280# ifdef USE_XSMP
6281 if (xsmp_icefd != -1)
6282 {
6283 xsmp_idx = nfd;
6284 fds[nfd].fd = xsmp_icefd;
6285 fds[nfd].events = POLLIN;
6286 nfd++;
6287 }
6288# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006289#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006290 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006291#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006292 if (interrupted != NULL)
6293 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006295 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006296
6297 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006298 if (result == 0 && interrupted != NULL && ret > 0)
6299 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006300
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006301# ifdef FEAT_MZSCHEME
6302 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006303 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006304 finished = FALSE;
6305# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307# ifdef FEAT_XCLIPBOARD
6308 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6309 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006310 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006312 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 finished = FALSE;
6314 }
6315# endif
6316# ifdef FEAT_MOUSE_GPM
6317 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319# endif
6320# ifdef USE_XSMP
6321 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6322 {
6323 if (fds[xsmp_idx].revents & POLLIN)
6324 {
6325 busy = TRUE;
6326 xsmp_handle_requests();
6327 busy = FALSE;
6328 }
6329 else if (fds[xsmp_idx].revents & POLLHUP)
6330 {
6331 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006332 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 xsmp_close();
6334 }
6335 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006336 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 }
6338# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006339#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006340 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006341 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006342 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344
Bram Moolenaar0f873732019-12-05 20:28:46 +01006345#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
6347 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006348 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006349 // These are static because they can take 8 Kbyte each and cause the
6350 // signal stack to run out with -O3.
6351 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006353 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006355# ifdef FEAT_MZSCHEME
6356 mzvim_check_threads();
6357 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6358 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006359 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006360 mzquantum_used = TRUE;
6361 }
6362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006364 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006366 tv.tv_sec = towait / 1000;
6367 tv.tv_usec = (towait % 1000) * (1000000/1000);
6368 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006370 else
6371 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372
6373 /*
6374 * Select on ready for reading and exceptional condition (end of file).
6375 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006376select_eintr:
6377 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006378 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 FD_ZERO(&efds);
6380 FD_SET(fd, &rfds);
K.Takatab247e062022-02-07 10:45:23 +00006381# ifndef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006382 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 FD_SET(fd, &efds);
6384# endif
6385 maxfd = fd;
6386
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006388 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 if (xterm_Shell != (Widget)0)
6390 {
6391 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6392 if (maxfd < ConnectionNumber(xterm_dpy))
6393 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006394
Bram Moolenaar0f873732019-12-05 20:28:46 +01006395 // An event may have already been read but not handled. In
6396 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006397 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399# endif
6400# ifdef FEAT_MOUSE_GPM
6401 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6402 {
6403 FD_SET(gpm_fd, &rfds);
6404 FD_SET(gpm_fd, &efds);
6405 if (maxfd < gpm_fd)
6406 maxfd = gpm_fd;
6407 }
6408# endif
6409# ifdef USE_XSMP
6410 if (xsmp_icefd != -1)
6411 {
6412 FD_SET(xsmp_icefd, &rfds);
6413 FD_SET(xsmp_icefd, &efds);
6414 if (maxfd < xsmp_icefd)
6415 maxfd = xsmp_icefd;
6416 }
6417# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006418# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006419 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006420# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006421 if (interrupted != NULL)
6422 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423
Bram Moolenaar643b6142018-09-12 20:29:09 +02006424 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6425 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006426 result = ret > 0 && FD_ISSET(fd, &rfds);
6427 if (result)
6428 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006429 else if (interrupted != NULL && ret > 0)
6430 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006431
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006432# ifdef EINTR
6433 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006434 {
dbivolaruab16ad32021-12-29 19:41:47 +00006435 // Check whether the EINTR is caused by SIGTSTP
6436 if (got_tstp && !in_mch_suspend)
6437 {
6438 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006439
dbivolaruab16ad32021-12-29 19:41:47 +00006440 ea.forceit = TRUE;
6441 ex_stop(&ea);
6442 got_tstp = FALSE;
6443 }
6444
Bram Moolenaar0f873732019-12-05 20:28:46 +01006445 // Check whether window has been resized, EINTR may be caused by
6446 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006447 if (do_resize)
6448 handle_resize();
6449
Bram Moolenaar0f873732019-12-05 20:28:46 +01006450 // Interrupted by a signal, need to try again. We ignore msec
6451 // here, because we do want to check even after a timeout if
6452 // characters are available. Needed for reading output of an
6453 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006454 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006455 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006456# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006457# ifdef __TANDEM
6458 if (ret == -1 && errno == ENOTSUP)
6459 {
6460 FD_ZERO(&rfds);
6461 FD_ZERO(&efds);
6462 ret = 0;
6463 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006464# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006465# ifdef FEAT_MZSCHEME
6466 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006467 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006468 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469# endif
6470
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471# ifdef FEAT_XCLIPBOARD
6472 if (ret > 0 && xterm_Shell != (Widget)0
6473 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6474 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006475 xterm_update(); // Maybe we should hand out clipboard
6476 // continue looping when we only got the X event and the input
6477 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 if (--ret == 0 && !input_available())
6479 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006480 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 finished = FALSE;
6482 }
6483 }
6484# endif
6485# ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00006486 if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 {
6488 if (FD_ISSET(gpm_fd, &efds))
6489 gpm_close();
6490 else if (FD_ISSET(gpm_fd, &rfds))
6491 *check_for_gpm = 1;
6492 }
6493# endif
6494# ifdef USE_XSMP
6495 if (ret > 0 && xsmp_icefd != -1)
6496 {
6497 if (FD_ISSET(xsmp_icefd, &efds))
6498 {
6499 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006500 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 xsmp_close();
6502 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006503 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 }
6505 else if (FD_ISSET(xsmp_icefd, &rfds))
6506 {
6507 busy = TRUE;
6508 xsmp_handle_requests();
6509 busy = FALSE;
6510 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006511 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 }
6513 }
6514# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006515#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006516 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006517 if (ret >= 0)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01006518 (void)channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
Bram Moolenaar0f873732019-12-05 20:28:46 +01006521#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522
6523#ifdef MAY_LOOP
6524 if (finished || msec == 0)
6525 break;
6526
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006527# ifdef FEAT_CLIENTSERVER
6528 if (server_waiting())
6529 break;
6530# endif
6531
Bram Moolenaar0f873732019-12-05 20:28:46 +01006532 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if (msec > 0)
6534 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006535# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006536 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006537 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006539 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 msec = msec / 2;
6541# endif
6542 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006543 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 }
6545#endif
6546 }
6547
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006548 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549}
6550
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006552 * Expand a path into all matching files and/or directories. Handles "*",
6553 * "?", "[a-z]", "**", etc.
6554 * "path" has backslashes before chars that are not to be expanded.
6555 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 */
6557 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006558mch_expandpath(
6559 garray_T *gap,
6560 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006561 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562{
Bram Moolenaar02743632005-07-25 20:42:36 +00006563 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565
6566/*
6567 * mch_expand_wildcards() - this code does wild-card pattern matching using
6568 * the shell
6569 *
6570 * return OK for success, FAIL for error (you may lose some memory) and put
6571 * an error message in *file.
6572 *
6573 * num_pat is number of input patterns
6574 * pat is array of pointers to input patterns
6575 * num_file is pointer to number of matched file names
6576 * file is pointer to array of pointers to matched file names
6577 */
6578
6579#ifndef SEEK_SET
6580# define SEEK_SET 0
6581#endif
6582#ifndef SEEK_END
6583# define SEEK_END 2
6584#endif
6585
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006586#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006587
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006589mch_expand_wildcards(
6590 int num_pat,
6591 char_u **pat,
6592 int *num_file,
6593 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006594 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
6596 int i;
6597 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006598 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 char_u *p;
6600 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601
Bram Moolenaarc7247912008-01-13 12:54:11 +00006602 /*
6603 * This is the non-OS/2 implementation (really Unix).
6604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 int j;
6606 char_u *tempname;
6607 char_u *command;
6608 FILE *fd;
6609 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006610#define STYLE_ECHO 0 // use "echo", the default
6611#define STYLE_GLOB 1 // use "glob", for csh
6612#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6613#define STYLE_PRINT 3 // use "print -N", for zsh
6614#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6615 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 int shell_style = STYLE_ECHO;
6617 int check_spaces;
6618 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006619 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006620 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006621 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622
Bram Moolenaar0f873732019-12-05 20:28:46 +01006623 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 *file = NULL;
6625
6626 /*
6627 * If there are no wildcards, just copy the names to allocated memory.
6628 * Saves a lot of time, because we don't have to start a new shell.
6629 */
6630 if (!have_wildcard(num_pat, pat))
6631 return save_patterns(num_pat, pat, num_file, file);
6632
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006633# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006634 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006635 if (sandbox != 0 && check_secure())
6636 return FAIL;
6637# endif
6638
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 /*
6640 * Don't allow the use of backticks in secure and restricted mode.
6641 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006642 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 for (i = 0; i < num_pat; ++i)
6644 if (vim_strchr(pat[i], '`') != NULL
6645 && (check_restricted() || check_secure()))
6646 return FAIL;
6647
6648 /*
6649 * get a name for the temp file
6650 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006651 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006653 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 return FAIL;
6655 }
6656
6657 /*
6658 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006659 * file.
6660 * STYLE_BT: NL separated
6661 * If expanding `cmd` execute it directly.
6662 * STYLE_GLOB: NUL separated
6663 * If we use *csh, "glob" will work better than "echo".
6664 * STYLE_PRINT: NL or NUL separated
6665 * If we use *zsh, "print -N" will work better than "glob".
6666 * STYLE_VIMGLOB: NL separated
6667 * If we use *sh*, we define "vimglob()".
6668 * STYLE_ECHO: space separated.
6669 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 */
6671 if (num_pat == 1 && *pat[0] == '`'
6672 && (len = STRLEN(pat[0])) > 2
6673 && *(pat[0] + len - 1) == '`')
6674 shell_style = STYLE_BT;
6675 else if ((len = STRLEN(p_sh)) >= 3)
6676 {
6677 if (STRCMP(p_sh + len - 3, "csh") == 0)
6678 shell_style = STYLE_GLOB;
6679 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6680 shell_style = STYLE_PRINT;
6681 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006682 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6683 "sh") != NULL)
6684 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685
Bram Moolenaar0f873732019-12-05 20:28:46 +01006686 // Compute the length of the command. We need 2 extra bytes: for the
6687 // optional '&' and for the NUL.
6688 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006690 if (shell_style == STYLE_VIMGLOB)
6691 len += STRLEN(sh_vimglob_func);
6692
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006693 for (i = 0; i < num_pat; ++i)
6694 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006695 // Count the length of the patterns in the same way as they are put in
6696 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006697#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006698 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006699#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006700 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006701 for (j = 0; pat[i][j] != NUL; ++j)
6702 {
6703 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006704 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006705 ++len;
6706 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006707#endif
6708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 command = alloc(len);
6710 if (command == NULL)
6711 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006712 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 vim_free(tempname);
6714 return FAIL;
6715 }
6716
6717 /*
6718 * Build the shell command:
6719 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6720 * recognizes this).
6721 * - Add the shell command to print the expanded names.
6722 * - Add the temp file name.
6723 * - Add the file name patterns.
6724 */
6725 if (shell_style == STYLE_BT)
6726 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006727 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006728 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006729 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006731 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006732 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006734 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006736 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 *p = ' ';
6738 }
6739 STRCAT(command, ">");
6740 }
6741 else
6742 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006743 STRCPY(command, "");
6744 if (shell_style == STYLE_GLOB)
6745 {
6746 // Assume the nonomatch option is valid only for csh like shells,
6747 // otherwise, this may set the positional parameters for the shell,
6748 // e.g. "$*".
6749 if (flags & EW_NOTFOUND)
6750 STRCAT(command, "set nonomatch; ");
6751 else
6752 STRCAT(command, "unset nonomatch; ");
6753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 if (shell_style == STYLE_GLOB)
6755 STRCAT(command, "glob >");
6756 else if (shell_style == STYLE_PRINT)
6757 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006758 else if (shell_style == STYLE_VIMGLOB)
6759 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 else
6761 STRCAT(command, "echo >");
6762 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006763
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006765
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 if (shell_style != STYLE_BT)
6767 for (i = 0; i < num_pat; ++i)
6768 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006769 // When using system() always add extra quotes, because the shell
6770 // is started twice. Otherwise put a backslash before special
6771 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772#ifdef USE_SYSTEM
6773 STRCAT(command, " \"");
6774 STRCAT(command, pat[i]);
6775 STRCAT(command, "\"");
6776#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006777 int intick = FALSE;
6778
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 p = command + STRLEN(command);
6780 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006781 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006782 {
6783 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006784 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006785 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6786 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006787 // Remove a backslash, take char literally. But keep
6788 // backslash inside backticks, before a special character
6789 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006790 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006791 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6792 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006793 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006794 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006795 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006796 else if (!intick
6797 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6798 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006799 // Put a backslash before a special character, but not
6800 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6801 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006802 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006803
Bram Moolenaar0f873732019-12-05 20:28:46 +01006804 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006805 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 *p = NUL;
6808#endif
6809 }
6810 if (flags & EW_SILENT)
6811 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006812 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006813 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814
6815 /*
6816 * Using zsh -G: If a pattern has no matches, it is just deleted from
6817 * the argument list, otherwise zsh gives an error message and doesn't
6818 * expand any other pattern.
6819 */
6820 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006821 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822
6823 /*
6824 * If we use -f then shell variables set in .cshrc won't get expanded.
6825 * vi can do it, so we will too, but it is only necessary if there is a "$"
6826 * in one of the patterns, otherwise we can still use the fast option.
6827 */
6828 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006829 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830
6831 /*
6832 * execute the shell command
6833 */
6834 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6835
Bram Moolenaar0f873732019-12-05 20:28:46 +01006836 // When running in the background, give it some time to create the temp
6837 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006838 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006839 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840
Bram Moolenaar0f873732019-12-05 20:28:46 +01006841 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 show_shell_mess = TRUE;
6843 vim_free(command);
6844
Bram Moolenaar0f873732019-12-05 20:28:46 +01006845 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 {
6847 mch_remove(tempname);
6848 vim_free(tempname);
6849 /*
6850 * With interactive completion, the error message is not printed.
6851 * However with USE_SYSTEM, I don't know how to turn off error messages
6852 * from the shell, so screen may still get messed up -- webb.
6853 */
6854#ifndef USE_SYSTEM
6855 if (!(flags & EW_SILENT))
6856#endif
6857 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006858 redraw_later_clear(); // probably messed up screen
6859 msg_putchar('\n'); // clear bottom line quickly
6860 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861#ifdef USE_SYSTEM
6862 if (!(flags & EW_SILENT))
6863#endif
6864 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006865 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006866 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 }
6868 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006869 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6870 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 if (shell_style == STYLE_BT)
6872 return FAIL;
6873 goto notfound;
6874 }
6875
6876 /*
6877 * read the names from the file into memory
6878 */
6879 fd = fopen((char *)tempname, READBIN);
6880 if (fd == NULL)
6881 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006882 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 if (!(flags & EW_SILENT))
6884 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006885 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006886 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
6888 vim_free(tempname);
6889 goto notfound;
6890 }
6891 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006892 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006894 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006895 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006896 buffer = NULL;
6897 else
6898 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 if (buffer == NULL)
6900 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006901 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 mch_remove(tempname);
6903 vim_free(tempname);
6904 fclose(fd);
6905 return FAIL;
6906 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006907 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 i = fread((char *)buffer, 1, len, fd);
6909 fclose(fd);
6910 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006911 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006913 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006914 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 vim_free(tempname);
6916 vim_free(buffer);
6917 return FAIL;
6918 }
6919 vim_free(tempname);
6920
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006921# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006922 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006924 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6926 *p++ = buffer[i];
6927 len = p - buffer;
6928# endif
6929
6930
Bram Moolenaar0f873732019-12-05 20:28:46 +01006931 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 if (shell_style == STYLE_ECHO)
6933 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006934 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006936 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 {
6938 while (*p != ' ' && *p != '\n')
6939 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006940 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 }
6942 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006943 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006944 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006946 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006948 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 {
6950 while (*p != '\n' && *p != NUL)
6951 ++p;
6952 if (*p != NUL)
6953 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006954 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 }
6956 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006957 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 else
6959 {
6960 /*
6961 * Some versions of zsh use spaces instead of NULs to separate
6962 * results. Only do this when there is no NUL before the end of the
6963 * buffer, otherwise we would never be able to use file names with
6964 * embedded spaces when zsh does use NULs.
6965 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6966 * don't check for spaces again.
6967 */
6968 check_spaces = FALSE;
6969 if (shell_style == STYLE_PRINT && !did_find_nul)
6970 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006971 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006972 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006973 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 did_find_nul = TRUE;
6975 else
6976 check_spaces = TRUE;
6977 }
6978
6979 /*
6980 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6981 * already is one, for STYLE_GLOB it needs to be added.
6982 */
6983 if (len && buffer[len - 1] == NUL)
6984 --len;
6985 else
6986 buffer[len] = NUL;
6987 i = 0;
6988 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006989 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {
6991 ++i;
6992 *p = NUL;
6993 }
6994 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006995 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 }
6997 if (i == 0)
6998 {
6999 /*
7000 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
7001 * /bin/sh will happily expand it to nothing rather than returning an
7002 * error; and hey, it's good to check anyway -- webb.
7003 */
7004 vim_free(buffer);
7005 goto notfound;
7006 }
7007 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007008 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 if (*file == NULL)
7010 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007011 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 vim_free(buffer);
7013 return FAIL;
7014 }
7015
7016 /*
7017 * Isolate the individual file names.
7018 */
7019 p = buffer;
7020 for (i = 0; i < *num_file; ++i)
7021 {
7022 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007023 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00007024 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
7025 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00007027 while (!(shell_style == STYLE_ECHO && *p == ' ')
7028 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007030 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 *p = NUL;
7032 else
7033 {
7034 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007035 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
7037 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007038 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007040 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007042 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 }
7044 }
7045
7046 /*
7047 * Move the file names to allocated memory.
7048 */
7049 for (j = 0, i = 0; i < *num_file; ++i)
7050 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007051 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7053 continue;
7054
Bram Moolenaar0f873732019-12-05 20:28:46 +01007055 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 dir = (mch_isdir((*file)[i]));
7057 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7058 continue;
7059
Bram Moolenaar0f873732019-12-05 20:28:46 +01007060 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007061 if (!dir && (flags & EW_EXEC)
7062 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007063 continue;
7064
Bram Moolenaar964b3742019-05-24 18:54:09 +02007065 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 if (p)
7067 {
7068 STRCPY(p, (*file)[i]);
7069 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007070 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 (*file)[j++] = p;
7072 }
7073 }
7074 vim_free(buffer);
7075 *num_file = j;
7076
Bram Moolenaar0f873732019-12-05 20:28:46 +01007077 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007079 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 goto notfound;
7081 }
7082
7083 return OK;
7084
7085notfound:
7086 if (flags & EW_NOTFOUND)
7087 return save_patterns(num_pat, pat, num_file, file);
7088 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089}
7090
Bram Moolenaar0f873732019-12-05 20:28:46 +01007091#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007094save_patterns(
7095 int num_pat,
7096 char_u **pat,
7097 int *num_file,
7098 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099{
7100 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007101 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007103 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 if (*file == NULL)
7105 return FAIL;
7106 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007107 {
7108 s = vim_strsave(pat[i]);
7109 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007110 // Be compatible with expand_filename(): halve the number of
7111 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007112 backslash_halve(s);
7113 (*file)[i] = s;
7114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 *num_file = num_pat;
7116 return OK;
7117}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119/*
7120 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7121 * expand.
7122 */
7123 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007124mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007126 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 if (*p == '\\' && p[1] != NUL)
7129 ++p;
7130 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 if (vim_strchr((char_u *)
7132#ifdef VMS
7133 "*?%"
7134#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136#endif
7137 , *p) != NULL)
7138 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 }
7140 return FALSE;
7141}
7142
7143/*
7144 * Return TRUE if the string "p" contains a wildcard.
7145 * Don't recognize '~' at the end as a wildcard.
7146 */
7147 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007148mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007150 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 if (*p == '\\' && p[1] != NUL)
7153 ++p;
7154 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 if (vim_strchr((char_u *)
7156#ifdef VMS
7157 "*?%$"
7158#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160#endif
7161 , *p) != NULL
7162 || (*p == '~' && p[1] != NUL))
7163 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 }
7165 return FALSE;
7166}
7167
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007169have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 int i;
7172
7173 for (i = 0; i < num; i++)
7174 if (mch_has_wildcard(file[i]))
7175 return 1;
7176 return 0;
7177}
7178
7179 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007180have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181{
7182 int i;
7183
7184 for (i = 0; i < num; i++)
7185 if (vim_strchr(file[i], '$') != NULL)
7186 return TRUE;
7187 return FALSE;
7188}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007190#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191/*
7192 * Scaled-down version of rename(), which is missing in Xenix.
7193 * This version can only move regular files and will fail if the
7194 * destination exists.
7195 */
7196 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007197mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198{
7199 struct stat st;
7200
Bram Moolenaar0f873732019-12-05 20:28:46 +01007201 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007203 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007205 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 return 0;
7207 return -1;
7208}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007209#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007211#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007212# if defined(DYNAMIC_GPM) || defined(PROTO)
7213/*
7214 * Initialize Gpm's symbols for dynamic linking.
7215 * Must be called only if libgpm_hinst is NULL.
7216 */
7217 static int
7218load_libgpm(void)
7219{
7220 libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL);
7221
7222 if (libgpm_hinst == NULL)
7223 {
7224 if (p_verbose > 0)
7225 smsg_attr(HL_ATTR(HLF_W),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007226 _("Could not load gpm library: %s"), dlerror());
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007227 return FAIL;
7228 }
7229
7230 if (
7231 (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL
7232 || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL
7233 || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL
7234 || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL
7235 || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL
7236 )
7237 {
7238 semsg(_(e_could_not_load_library_str_str), "gpm", dlerror());
7239 dlclose(libgpm_hinst);
7240 libgpm_hinst = NULL;
7241 dll_gpm_flag = NULL;
7242 dll_gpm_fd = NULL;
7243 return FAIL;
7244 }
7245 return OK;
7246}
7247
7248 int
7249gpm_available(void)
7250{
7251 return libgpm_hinst != NULL || load_libgpm() == OK;
7252}
7253# endif // DYNAMIC_GPM
7254
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255/*
7256 * Initializes connection with gpm (if it isn't already opened)
7257 * Return 1 if succeeded (or connection already opened), 0 if failed
7258 */
7259 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007260gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007262 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007264#ifdef DYNAMIC_GPM
7265 if (!gpm_available())
7266 return 0;
7267#endif
7268
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007269 if (gpm_flag)
7270 return 1; // already open
7271
7272 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7273 gpm_connect.defaultMask = ~GPM_HARD;
7274 // Default handling for mouse move
7275 gpm_connect.minMod = 0; // Handle any modifier keys
7276 gpm_connect.maxMod = 0xffff;
7277 if (Gpm_Open(&gpm_connect, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007279 // gpm library tries to handling TSTP causes
7280 // problems. Anyways, we close connection to Gpm whenever
7281 // we are going to suspend or starting an external process
7282 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007283# ifdef SIGTSTP
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007284 signal(SIGTSTP, restricted ? SIG_IGN : (void (*)())sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007285# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007286 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007288 if (gpm_fd == -2)
7289 Gpm_Close(); // We don't want to talk to xterm via gpm
7290 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291}
7292
7293/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007294 * Returns TRUE if the GPM mouse is enabled.
7295 */
7296 int
7297gpm_enabled(void)
7298{
7299 return gpm_flag && gpm_fd >= 0;
7300}
7301
7302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 */
7305 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007306gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007308 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 Gpm_Close();
7310}
7311
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007312/*
7313 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007315 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 */
7317 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007318mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319{
7320 int button;
7321 static Gpm_Event gpm_event;
7322 char_u string[6];
7323 int_u vim_modifiers;
7324 int row,col;
7325 unsigned char buttons_mask;
7326 unsigned char gpm_modifiers;
7327 static unsigned char old_buttons = 0;
7328
7329 Gpm_GetEvent(&gpm_event);
7330
7331#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007332 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 if (hold_gui_events)
7334 return 0;
7335#endif
7336
7337 row = gpm_event.y - 1;
7338 col = gpm_event.x - 1;
7339
Bram Moolenaar0f873732019-12-05 20:28:46 +01007340 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 string[1] = 'M';
7342 string[2] = 'G';
7343 switch (GPM_BARE_EVENTS(gpm_event.type))
7344 {
7345 case GPM_DRAG:
7346 string[3] = MOUSE_DRAG;
7347 break;
7348 case GPM_DOWN:
7349 buttons_mask = gpm_event.buttons & ~old_buttons;
7350 old_buttons = gpm_event.buttons;
7351 switch (buttons_mask)
7352 {
7353 case GPM_B_LEFT:
7354 button = MOUSE_LEFT;
7355 break;
7356 case GPM_B_MIDDLE:
7357 button = MOUSE_MIDDLE;
7358 break;
7359 case GPM_B_RIGHT:
7360 button = MOUSE_RIGHT;
7361 break;
7362 default:
7363 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007364 // Don't know what to do. Can more than one button be
7365 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 }
7367 string[3] = (char_u)(button | 0x20);
7368 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7369 break;
7370 case GPM_UP:
7371 string[3] = MOUSE_RELEASE;
7372 old_buttons &= ~gpm_event.buttons;
7373 break;
7374 default:
7375 return 0;
7376 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007377 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 gpm_modifiers = gpm_event.modifiers;
7379 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007380 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7381 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7382 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7384 vim_modifiers |= MOUSE_SHIFT;
7385
7386 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7387 vim_modifiers |= MOUSE_CTRL;
7388 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7389 vim_modifiers |= MOUSE_ALT;
7390 string[3] |= vim_modifiers;
7391 string[4] = (char_u)(col + ' ' + 1);
7392 string[5] = (char_u)(row + ' ' + 1);
7393 add_to_input_buf(string, 6);
7394 return 6;
7395}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007396#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007398#ifdef FEAT_SYSMOUSE
7399/*
7400 * Initialize connection with sysmouse.
7401 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7402 * output, any sysmouse output than will be processed via sig_sysmouse().
7403 * Return OK if succeeded, FAIL if failed.
7404 */
7405 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007406sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007407{
7408 struct mouse_info mouse;
7409
7410 mouse.operation = MOUSE_MODE;
7411 mouse.u.mode.mode = 0;
7412 mouse.u.mode.signal = SIGUSR2;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007413 if (ioctl(1, CONS_MOUSECTL, &mouse) == -1)
7414 return FAIL;
7415
7416 signal(SIGUSR2, (void (*)())sig_sysmouse);
7417 mouse.operation = MOUSE_SHOW;
7418 ioctl(1, CONS_MOUSECTL, &mouse);
7419 return OK;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007420}
7421
7422/*
7423 * Stop processing SIGUSR2 signals, and also make sure that
7424 * virtual console do not send us any sysmouse related signal.
7425 */
7426 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007427sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007428{
7429 struct mouse_info mouse;
7430
7431 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7432 mouse.operation = MOUSE_MODE;
7433 mouse.u.mode.mode = 0;
7434 mouse.u.mode.signal = 0;
7435 ioctl(1, CONS_MOUSECTL, &mouse);
7436}
7437
7438/*
7439 * Gets info from sysmouse and adds special keys to input buf.
7440 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007441 static void
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007442sig_sysmouse SIGDEFARG(sigarg)
7443{
7444 struct mouse_info mouse;
7445 struct video_info video;
7446 char_u string[6];
7447 int row, col;
7448 int button;
7449 int buttons;
7450 static int oldbuttons = 0;
7451
7452#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007453 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007454 if (hold_gui_events)
7455 return;
7456#endif
7457
7458 mouse.operation = MOUSE_GETINFO;
7459 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7460 && ioctl(1, FBIO_MODEINFO, &video) != -1
7461 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7462 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7463 {
7464 row = mouse.u.data.y / video.vi_cheight;
7465 col = mouse.u.data.x / video.vi_cwidth;
7466 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007467 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007468 string[1] = 'M';
7469 string[2] = 'S';
7470 if (oldbuttons == buttons && buttons != 0)
7471 {
7472 button = MOUSE_DRAG;
7473 }
7474 else
7475 {
7476 switch (buttons)
7477 {
7478 case 0:
7479 button = MOUSE_RELEASE;
7480 break;
7481 case 1:
7482 button = MOUSE_LEFT;
7483 break;
7484 case 2:
7485 button = MOUSE_MIDDLE;
7486 break;
7487 case 4:
7488 button = MOUSE_RIGHT;
7489 break;
7490 default:
7491 return;
7492 }
7493 oldbuttons = buttons;
7494 }
7495 string[3] = (char_u)(button);
7496 string[4] = (char_u)(col + ' ' + 1);
7497 string[5] = (char_u)(row + ' ' + 1);
7498 add_to_input_buf(string, 6);
7499 }
7500 return;
7501}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007502#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007503
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007505typedef char_u * (*STRPROCSTR)(char_u *);
7506typedef char_u * (*INTPROCSTR)(int);
7507typedef int (*STRPROCINT)(char_u *);
7508typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509
7510/*
7511 * Call a DLL routine which takes either a string or int param
7512 * and returns an allocated string.
7513 */
7514 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007515mch_libcall(
7516 char_u *libname,
7517 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007518 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007519 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007520 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007521 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
7523# if defined(USE_DLOPEN)
7524 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007525 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526# else
7527 shl_t hinstLib;
7528# endif
7529 STRPROCSTR ProcAdd;
7530 INTPROCSTR ProcAddI;
7531 char_u *retval_str = NULL;
7532 int retval_int = 0;
7533 int success = FALSE;
7534
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007535 /*
7536 * Get a handle to the DLL module.
7537 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007539 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007540 (void)dlerror();
7541
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 hinstLib = dlopen((char *)libname, RTLD_LAZY
7543# ifdef RTLD_LOCAL
7544 | RTLD_LOCAL
7545# endif
7546 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007547 if (hinstLib == NULL)
7548 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007549 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007550 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007551 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007552 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554# else
7555 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7556# endif
7557
Bram Moolenaar0f873732019-12-05 20:28:46 +01007558 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 if (hinstLib != NULL)
7560 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007561# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 /*
7563 * Catch a crash when calling the library function. For example when
7564 * using a number where a string pointer is expected.
7565 */
7566 mch_startjmp();
7567 if (SETJMP(lc_jump_env) != 0)
7568 {
7569 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007570# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007571 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 mch_didjmp();
7574 }
7575 else
7576# endif
7577 {
7578 retval_str = NULL;
7579 retval_int = 0;
7580
7581 if (argstring != NULL)
7582 {
7583# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007584 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007585 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586# else
7587 if (shl_findsym(&hinstLib, (const char *)funcname,
7588 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7589 ProcAdd = NULL;
7590# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007591 if ((success = (ProcAdd != NULL
7592# if defined(USE_DLOPEN)
7593 && dlerr == NULL
7594# endif
7595 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 {
7597 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007598 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 else
7600 retval_str = (ProcAdd)(argstring);
7601 }
7602 }
7603 else
7604 {
7605# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007606 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007607 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608# else
7609 if (shl_findsym(&hinstLib, (const char *)funcname,
7610 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7611 ProcAddI = NULL;
7612# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007613 if ((success = (ProcAddI != NULL
7614# if defined(USE_DLOPEN)
7615 && dlerr == NULL
7616# endif
7617 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 {
7619 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007620 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 else
7622 retval_str = (ProcAddI)(argint);
7623 }
7624 }
7625
Bram Moolenaar0f873732019-12-05 20:28:46 +01007626 // Save the string before we free the library.
7627 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 if (string_result == NULL)
7629 *number_result = retval_int;
7630 else if (retval_str != NULL
7631 && retval_str != (char_u *)1
7632 && retval_str != (char_u *)-1)
7633 *string_result = vim_strsave(retval_str);
7634 }
7635
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007636# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 mch_endjmp();
7638# ifdef SIGHASARG
7639 if (lc_signal != 0)
7640 {
7641 int i;
7642
Bram Moolenaar0f873732019-12-05 20:28:46 +01007643 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 for (i = 0; signal_info[i].sig != -1; i++)
7645 if (lc_signal == signal_info[i].sig)
7646 break;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00007647 semsg(e_got_sig_str_in_libcall, signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 }
7649# endif
7650# endif
7651
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007653 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007654 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007655 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007656
Bram Moolenaar0f873732019-12-05 20:28:46 +01007657 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 (void)dlclose(hinstLib);
7659# else
7660 (void)shl_unload(hinstLib);
7661# endif
7662 }
7663
7664 if (!success)
7665 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007666 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 return FAIL;
7668 }
7669
7670 return OK;
7671}
7672#endif
7673
7674#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007675static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676static int xterm_button;
7677
7678/*
7679 * Setup a dummy window for X selections in a terminal.
7680 */
7681 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007682setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683{
7684 int z = 0;
7685 char *strp = "";
7686 Widget AppShell;
7687
7688 if (!x_connect_to_server())
7689 return;
7690
7691 open_app_context();
7692 if (app_context != NULL && xterm_Shell == (Widget)0)
7693 {
7694 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007695# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007697# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007698# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007699 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700
7701 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007702 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703# endif
7704
Bram Moolenaar0f873732019-12-05 20:28:46 +01007705 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 oldhandler = XSetErrorHandler(x_error_check);
7707
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007708# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007709 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7711 mch_startjmp();
7712 if (SETJMP(lc_jump_env) != 0)
7713 {
7714 mch_didjmp();
7715 xterm_dpy = NULL;
7716 }
7717 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 {
7720 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7721 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007722 if (xterm_dpy != NULL)
7723 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007724# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007726# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 }
7728
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007729# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007730 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007732# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007733 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 (void)XSetErrorHandler(oldhandler);
7735
7736 if (xterm_dpy == NULL)
7737 {
7738 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007739 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 return;
7741 }
7742
Bram Moolenaar0f873732019-12-05 20:28:46 +01007743 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 (void)XSetIOErrorHandler(x_IOerror_handler);
7745
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007746# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007748 {
7749 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007750 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007751 verbose_leave();
7752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753# endif
7754
Bram Moolenaar0f873732019-12-05 20:28:46 +01007755 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7757 applicationShellWidgetClass, xterm_dpy,
7758 NULL);
7759 if (AppShell == (Widget)0)
7760 return;
7761 xterm_Shell = XtVaCreatePopupShell("VIM",
7762 topLevelShellWidgetClass, AppShell,
7763 XtNmappedWhenManaged, 0,
7764 XtNwidth, 1,
7765 XtNheight, 1,
7766 NULL);
7767 if (xterm_Shell == (Widget)0)
7768 return;
7769
7770 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007771 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 if (x11_display == NULL)
7773 x11_display = xterm_dpy;
7774
7775 XtRealizeWidget(xterm_Shell);
7776 XSync(xterm_dpy, False);
7777 xterm_update();
7778 }
7779 if (xterm_Shell != (Widget)0)
7780 {
7781 clip_init(TRUE);
7782 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7783 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007784 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 if (test_x11_window(xterm_dpy) == FAIL)
7786 x11_window = 0;
7787 if (x11_window != 0)
7788 xterm_trace = 0;
7789 }
7790}
7791
7792 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007793start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
7795 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7796 return;
7797 xterm_trace = 1;
7798 xterm_button = button;
7799 do_xterm_trace();
7800}
7801
7802
7803 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007804stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805{
7806 if (xterm_trace < 0)
7807 return;
7808 xterm_trace = 0;
7809}
7810
7811/*
7812 * Query the xterm pointer and generate mouse termcodes if necessary
7813 * return TRUE if dragging is active, else FALSE
7814 */
7815 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007816do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817{
7818 Window root, child;
7819 int root_x, root_y;
7820 int win_x, win_y;
7821 int row, col;
7822 int_u mask_return;
7823 char_u buf[50];
7824 char_u *strp;
7825 long got_hints;
7826 static char_u *mouse_code;
7827 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7828 static int prev_row = 0, prev_col = 0;
7829 static XSizeHints xterm_hints;
7830
7831 if (xterm_trace <= 0)
7832 return FALSE;
7833
7834 if (xterm_trace == 1)
7835 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007836 // Get the hints just before tracking starts. The font size might
7837 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007838 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7839 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 || xterm_hints.width_inc <= 1
7841 || xterm_hints.height_inc <= 1)
7842 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007843 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 return FALSE;
7845 }
7846
Bram Moolenaar0f873732019-12-05 20:28:46 +01007847 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 mouse_code = find_termcode(mouse_name);
7849 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007850 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 xterm_trace = 2;
7852
Bram Moolenaar0f873732019-12-05 20:28:46 +01007853 // Find the offset of the chars, there might be a scrollbar on the
7854 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7856 &win_x, &win_y, &mask_return);
7857 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7858 - (xterm_hints.height_inc / 2);
7859 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7860 xterm_hints.y = 2;
7861 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7862 - (xterm_hints.width_inc / 2);
7863 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7864 xterm_hints.x = 2;
7865 return TRUE;
7866 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007867 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {
7869 xterm_trace = 0;
7870 return FALSE;
7871 }
7872
7873 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7874 &win_x, &win_y, &mask_return);
7875
7876 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7877 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7878 if (row == prev_row && col == prev_col)
7879 return TRUE;
7880
7881 STRCPY(buf, mouse_code);
7882 strp = buf + STRLEN(buf);
7883 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7884 *strp++ = (char_u)(col + ' ' + 1);
7885 *strp++ = (char_u)(row + ' ' + 1);
7886 *strp = 0;
7887 add_to_input_buf(buf, STRLEN(buf));
7888
7889 prev_row = row;
7890 prev_col = col;
7891 return TRUE;
7892}
7893
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007894# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895/*
7896 * Destroy the display, window and app_context. Required for GTK.
7897 */
7898 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007899clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900{
7901 if (xterm_Shell != (Widget)0)
7902 {
7903 XtDestroyWidget(xterm_Shell);
7904 xterm_Shell = (Widget)0;
7905 }
7906 if (xterm_dpy != NULL)
7907 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007908# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007909 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007911# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 if (x11_display == xterm_dpy)
7913 x11_display = NULL;
7914 xterm_dpy = NULL;
7915 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007916# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 if (app_context != (XtAppContext)NULL)
7918 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007919 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 XtDestroyApplicationContext(app_context);
7921 app_context = (XtAppContext)NULL;
7922 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924}
7925# endif
7926
7927/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007928 * Catch up with GUI or X events.
7929 */
7930 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007931clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007932{
7933# ifdef FEAT_GUI
7934 if (gui.in_use)
7935 gui_mch_update();
7936 else
7937# endif
7938 if (xterm_Shell != (Widget)0)
7939 xterm_update();
7940}
7941
7942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 * Catch up with any queued X events. This may put keyboard input into the
7944 * input buffer, call resize call-backs, trigger timers etc. If there is
7945 * nothing in the X event queue (& no timers pending), then we return
7946 * immediately.
7947 */
7948 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007949xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950{
7951 XEvent event;
7952
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007953 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007955 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007957 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007958 break;
7959
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007960 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007961 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007962 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007963 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007964#ifdef FEAT_CLIENTSERVER
7965 {
7966 XPropertyEvent *e = (XPropertyEvent *)&event;
7967
7968 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007970 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007973 XtDispatchEvent(&event);
7974 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007975 else
7976 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007977 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007978 XtAppProcessEvent(app_context, mask);
7979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 }
7981}
7982
7983 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007984clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
7986 if (xterm_Shell != (Widget)0)
7987 return clip_x11_own_selection(xterm_Shell, cbd);
7988 return FAIL;
7989}
7990
7991 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007992clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993{
7994 if (xterm_Shell != (Widget)0)
7995 clip_x11_lose_selection(xterm_Shell, cbd);
7996}
7997
7998 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007999clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000{
8001 if (xterm_Shell != (Widget)0)
8002 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
8003}
8004
8005 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02008006clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007{
8008 clip_x11_set_selection(cbd);
8009}
8010#endif
8011
8012
8013#if defined(USE_XSMP) || defined(PROTO)
8014/*
8015 * Code for X Session Management Protocol.
8016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017
8018# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019/*
8020 * This is our chance to ask the user if they want to save,
8021 * or abort the logout
8022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008024xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025{
Bram Moolenaare1004402020-10-24 20:49:43 +02008026 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 int cancel_shutdown = False;
8028
Bram Moolenaare1004402020-10-24 20:49:43 +02008029 save_cmod_flags = cmdmod.cmod_flags;
8030 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01008031 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01008032 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02008034 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01008035 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 out_flush();
8037
Bram Moolenaar0f873732019-12-05 20:28:46 +01008038 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 SmcInteractDone(smc_conn, cancel_shutdown);
8040
Bram Moolenaar0f873732019-12-05 20:28:46 +01008041 // Finish off
8042 // Only end save-yourself here if we're not cancelling shutdown;
8043 // we'll get a cancelled callback later in which we'll end it.
8044 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 if (!cancel_shutdown)
8046 {
8047 xsmp.save_yourself = False;
8048 SmcSaveYourselfDone(smc_conn, True);
8049 }
8050}
8051# endif
8052
8053/*
8054 * Callback that starts save-yourself.
8055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008057xsmp_handle_save_yourself(
8058 SmcConn smc_conn,
8059 SmPointer client_data UNUSED,
8060 int save_type UNUSED,
8061 Bool shutdown,
8062 int interact_style UNUSED,
8063 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008065 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 if (xsmp.save_yourself)
8067 SmcSaveYourselfDone(smc_conn, True);
8068 xsmp.save_yourself = True;
8069 xsmp.shutdown = shutdown;
8070
Bram Moolenaar0f873732019-12-05 20:28:46 +01008071 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01008073 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074
8075 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008076 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077
8078# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008079 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008081 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 SmcInteractRequest(smc_conn, SmDialogError,
8083 xsmp_handle_interaction, client_data);
8084 else
8085# endif
8086 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008087 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 SmcSaveYourselfDone(smc_conn, True);
8089 xsmp.save_yourself = False;
8090 }
8091}
8092
8093
8094/*
8095 * Callback to warn us of imminent death.
8096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008098xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099{
8100 xsmp_close();
8101
Bram Moolenaar0f873732019-12-05 20:28:46 +01008102 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 getout_preserve_modified(0);
8104}
8105
8106
8107/*
8108 * Callback to tell us that save-yourself has completed.
8109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008111xsmp_save_complete(
8112 SmcConn smc_conn UNUSED,
8113 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114{
8115 xsmp.save_yourself = False;
8116}
8117
8118
8119/*
8120 * Callback to tell us that an instigated shutdown was cancelled
8121 * (maybe even by us)
8122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008124xsmp_shutdown_cancelled(
8125 SmcConn smc_conn,
8126 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127{
8128 if (xsmp.save_yourself)
8129 SmcSaveYourselfDone(smc_conn, True);
8130 xsmp.save_yourself = False;
8131 xsmp.shutdown = False;
8132}
8133
8134
8135/*
8136 * Callback to tell us that a new ICE connection has been established.
8137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008139xsmp_ice_connection(
8140 IceConn iceConn,
8141 IcePointer clientData UNUSED,
8142 Bool opening,
8143 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008145 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 if (opening)
8147 {
8148 xsmp_icefd = IceConnectionNumber(iceConn);
8149 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8150 }
8151}
8152
8153
Bram Moolenaar0f873732019-12-05 20:28:46 +01008154// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008156xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157{
8158 Bool rep;
8159
8160 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8161 == IceProcessMessagesIOError)
8162 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008163 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008165 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 xsmp_close();
8167 return FAIL;
8168 }
8169 else
8170 return OK;
8171}
8172
8173static int dummy;
8174
Bram Moolenaar0f873732019-12-05 20:28:46 +01008175// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 void
8177xsmp_init(void)
8178{
8179 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 SmcCallbacks smcallbacks;
8181#if 0
8182 SmPropValue smname;
8183 SmProp smnameprop;
8184 SmProp *smprops[1];
8185#endif
8186
8187 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008188 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189
8190 xsmp.save_yourself = xsmp.shutdown = False;
8191
Bram Moolenaar0f873732019-12-05 20:28:46 +01008192 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8194 smcallbacks.save_yourself.client_data = NULL;
8195 smcallbacks.die.callback = xsmp_die;
8196 smcallbacks.die.client_data = NULL;
8197 smcallbacks.save_complete.callback = xsmp_save_complete;
8198 smcallbacks.save_complete.client_data = NULL;
8199 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8200 smcallbacks.shutdown_cancelled.client_data = NULL;
8201
Bram Moolenaar0f873732019-12-05 20:28:46 +01008202 // Set up a watch on ICE connection creations. The "dummy" argument is
8203 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8205 {
8206 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008207 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 return;
8209 }
8210
Bram Moolenaar0f873732019-12-05 20:28:46 +01008211 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 xsmp.smcconn = SmcOpenConnection(
8213 NULL,
8214 NULL,
8215 SmProtoMajor,
8216 SmProtoMinor,
8217 SmcSaveYourselfProcMask | SmcDieProcMask
8218 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8219 &smcallbacks,
8220 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008221 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008222 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 errorstring);
8224 if (xsmp.smcconn == NULL)
8225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008227 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008228 char errorreport[132];
8229
8230 // If the message is too long it might not be NUL terminated. Add
8231 // a NUL at the end to make sure we don't go over the end.
8232 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008233 vim_snprintf(errorreport, sizeof(errorreport),
8234 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008235 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 return;
8238 }
8239 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8240
8241#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008242 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 smname.value = "vim";
8244 smname.length = 3;
8245 smnameprop.name = "SmProgram";
8246 smnameprop.type = "SmARRAY8";
8247 smnameprop.num_vals = 1;
8248 smnameprop.vals = &smname;
8249
8250 smprops[0] = &smnameprop;
8251 SmcSetProperties(xsmp.smcconn, 1, smprops);
8252#endif
8253}
8254
8255
Bram Moolenaar0f873732019-12-05 20:28:46 +01008256// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008258xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008260 if (xsmp_icefd == -1)
8261 return;
8262
8263 SmcCloseConnection(xsmp.smcconn, 0, NULL);
8264 if (xsmp.clientid != NULL)
8265 free(xsmp.clientid);
8266 xsmp.clientid = NULL;
8267 xsmp_icefd = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008269#endif // USE_XSMP
Paul Ollis65745772022-06-05 16:55:54 +01008270
8271#if defined(FEAT_RELTIME) || defined(PROTO)
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008272# if defined(HAVE_TIMER_CREATE) || defined(PROTO)
Paul Ollis65745772022-06-05 16:55:54 +01008273/*
8274 * Implement timeout with timer_create() and timer_settime().
8275 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008276static volatile sig_atomic_t timeout_flag = FALSE;
8277static timer_t timer_id;
8278static int timer_created = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008279
8280/*
8281 * Callback for when the timer expires.
8282 */
8283 static void
8284set_flag(union sigval _unused UNUSED)
8285{
8286 timeout_flag = TRUE;
8287}
8288
8289/*
8290 * Stop any active timeout.
8291 */
8292 void
8293stop_timeout(void)
8294{
8295 static struct itimerspec disarm = {{0, 0}, {0, 0}};
8296
8297 if (timer_created)
8298 {
8299 int ret = timer_settime(timer_id, 0, &disarm, NULL);
8300
8301 if (ret < 0)
8302 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8303 }
8304
8305 // Clear the current timeout flag; any previous timeout should be
8306 // considered _not_ triggered.
8307 timeout_flag = FALSE;
8308}
8309
8310/*
8311 * Start the timeout timer.
8312 *
8313 * The return value is a pointer to a flag that is initialised to FALSE. If the
8314 * timeout expires, the flag is set to TRUE. This will only return pointers to
8315 * static memory; i.e. any pointer returned by this function may always be
8316 * safely dereferenced.
8317 *
8318 * This function is not expected to fail, but if it does it will still return a
8319 * valid flag pointer; the flag will remain stuck as FALSE .
8320 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008321 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008322start_timeout(long msec)
8323{
8324 struct itimerspec interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008325 {0, 0}, // Do not repeat.
8326 {msec / 1000, (msec % 1000) * 1000000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008327 int ret;
8328
8329 // This is really the caller's responsibility, but let's make sure the
8330 // previous timer has been stopped.
8331 stop_timeout();
Paul Ollis65745772022-06-05 16:55:54 +01008332
8333 if (!timer_created)
8334 {
8335 struct sigevent action = {0};
8336
8337 action.sigev_notify = SIGEV_THREAD;
8338 action.sigev_notify_function = set_flag;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008339 ret = timer_create(CLOCK_MONOTONIC, &action, &timer_id);
8340 if (ret < 0)
Paul Ollis65745772022-06-05 16:55:54 +01008341 {
8342 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8343 return &timeout_flag;
8344 }
8345 timer_created = TRUE;
8346 }
8347
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00008348# ifdef FEAT_EVAL
Bram Moolenaar616592e2022-06-17 15:17:10 +01008349 ch_log(NULL, "setting timeout timer to %d sec %ld nsec",
8350 (int)interval.it_value.tv_sec, (long)interval.it_value.tv_nsec);
Bram Moolenaar509ce032022-06-20 11:23:01 +01008351# endif
Paul Ollis65745772022-06-05 16:55:54 +01008352 ret = timer_settime(timer_id, 0, &interval, NULL);
8353 if (ret < 0)
8354 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8355
8356 return &timeout_flag;
8357}
8358
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008359/*
8360 * To be used before fork/exec: delete any created timer.
8361 */
8362 void
8363delete_timer(void)
8364{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008365 if (!timer_created)
8366 return;
8367
8368 timer_delete(timer_id);
8369 timer_created = FALSE;
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008370}
8371
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008372# else // HAVE_TIMER_CREATE
Paul Ollis65745772022-06-05 16:55:54 +01008373
8374/*
8375 * Implement timeout with setitimer()
8376 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008377static struct sigaction prev_sigaction;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008378static volatile sig_atomic_t timeout_flag = FALSE;
8379static int timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008380static int timer_handler_active = FALSE;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008381static volatile sig_atomic_t alarm_pending = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008382
8383/*
8384 * Handle SIGALRM for a timeout.
8385 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01008386 static void
Paul Ollis65745772022-06-05 16:55:54 +01008387set_flag SIGDEFARG(sigarg)
8388{
8389 if (alarm_pending)
8390 alarm_pending = FALSE;
8391 else
8392 timeout_flag = TRUE;
8393}
8394
8395/*
8396 * Stop any active timeout.
8397 */
8398 void
8399stop_timeout(void)
8400{
8401 static struct itimerval disarm = {{0, 0}, {0, 0}};
8402 int ret;
8403
8404 if (timer_active)
8405 {
8406 timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008407 ret = setitimer(ITIMER_REAL, &disarm, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008408 if (ret < 0)
8409 // Should only get here as a result of coding errors.
8410 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8411 }
8412
8413 if (timer_handler_active)
8414 {
8415 timer_handler_active = FALSE;
8416 ret = sigaction(SIGALRM, &prev_sigaction, NULL);
8417 if (ret < 0)
8418 // Should only get here as a result of coding errors.
8419 semsg(_(e_could_not_reset_handler_for_timeout_str),
8420 strerror(errno));
8421 }
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008422 timeout_flag = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008423}
8424
8425/*
8426 * Start the timeout timer.
8427 *
8428 * The return value is a pointer to a flag that is initialised to FALSE. If the
8429 * timeout expires, the flag is set to TRUE. This will only return pointers to
8430 * static memory; i.e. any pointer returned by this function may always be
8431 * safely dereferenced.
8432 *
8433 * This function is not expected to fail, but if it does it will still return a
8434 * valid flag pointer; the flag will remain stuck as FALSE .
8435 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008436 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008437start_timeout(long msec)
8438{
8439 struct itimerval interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008440 {0, 0}, // Do not repeat.
8441 {msec / 1000, (msec % 1000) * 1000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008442 struct sigaction handle_alarm;
8443 int ret;
8444 sigset_t sigs;
8445 sigset_t saved_sigs;
8446
8447 // This is really the caller's responsibility, but let's make sure the
8448 // previous timer has been stopped.
8449 stop_timeout();
8450
8451 // There is a small chance that SIGALRM is pending and so the handler must
8452 // ignore it on the first call.
8453 alarm_pending = FALSE;
8454 ret = sigemptyset(&sigs);
8455 ret = ret == 0 ? sigaddset(&sigs, SIGALRM) : ret;
8456 ret = ret == 0 ? sigprocmask(SIG_BLOCK, &sigs, &saved_sigs) : ret;
8457 timeout_flag = FALSE;
8458 ret = ret == 0 ? sigpending(&sigs) : ret;
8459 if (ret == 0)
8460 {
8461 alarm_pending = sigismember(&sigs, SIGALRM);
Bram Moolenaar1f89abf2022-06-06 10:07:01 +01008462 ret = sigprocmask(SIG_SETMASK, &saved_sigs, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008463 }
8464 if (unlikely(ret != 0 || alarm_pending < 0))
8465 {
8466 // Just catching coding errors. Write an error message, but carry on.
8467 semsg(_(e_could_not_check_for_pending_sigalrm_str), strerror(errno));
8468 alarm_pending = FALSE;
8469 }
8470
8471 // Set up the alarm handler first.
8472 ret = sigemptyset(&handle_alarm.sa_mask);
8473 handle_alarm.sa_handler = set_flag;
8474 handle_alarm.sa_flags = 0;
8475 ret = ret == 0 ? sigaction(SIGALRM, &handle_alarm, &prev_sigaction) : ret;
8476 if (ret < 0)
8477 {
8478 // Should only get here as a result of coding errors.
8479 semsg(_(e_could_not_set_handler_for_timeout_str), strerror(errno));
8480 return &timeout_flag;
8481 }
8482 timer_handler_active = TRUE;
8483
8484 // Set up the interval timer once the alarm handler is in place.
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008485 ret = setitimer(ITIMER_REAL, &interval, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008486 if (ret < 0)
8487 {
8488 // Should only get here as a result of coding errors.
8489 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8490 stop_timeout();
8491 return &timeout_flag;
8492 }
8493
8494 timer_active = TRUE;
8495 return &timeout_flag;
8496}
8497# endif // HAVE_TIMER_CREATE
8498#endif // FEAT_RELTIME