blob: fd6d35fedcfce1860fa2ccaf6f2a4abfe831f3cf [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{
783 if (stack_limit != NULL)
784 {
785 if (stack_grows_downwards)
786 {
787 if (p < stack_limit)
788 return FAIL;
789 }
790 else if (p > stack_limit)
791 return FAIL;
792 }
793 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{
840 if (signal_stack != NULL)
841 {
842# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843# ifdef HAVE_SS_BASE
844 sigstk.ss_base = signal_stack;
845# else
846 sigstk.ss_sp = signal_stack;
847# endif
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200848 sigstk.ss_size = get_signal_stack_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 sigstk.ss_flags = 0;
850 (void)sigaltstack(&sigstk, NULL);
851# else
852 sigstk.ss_sp = signal_stack;
853 if (stack_grows_downwards)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200854 sigstk.ss_sp += get_signal_stack_size() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 sigstk.ss_onstack = 0;
856 (void)sigstack(&sigstk, NULL);
857# endif
858 }
859}
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
2023 if (get_x11_windis() == OK)
2024 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002025 // Get window/icon name if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 if (get_title)
2027 status = XGetWMName(x11_display, x11_window, &text_prop);
2028 else
2029 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2030
2031 /*
2032 * If terminal is xterm, then x11_window may be a child window of the
2033 * outer xterm window that actually contains the window/icon name, so
2034 * keep traversing up the tree until a window with a title/icon is
2035 * found.
2036 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002037 // Previously this was only done for xterm and alike. I don't see a
Bram Moolenaar0f873732019-12-05 20:28:46 +01002038 // reason why it would fail for other terminal emulators.
2039 // if (term_is_xterm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
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)
2048 {
2049 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;
2058 if (get_title)
2059 status = XGetWMName(x11_display, win, &text_prop);
2060 else
2061 status = XGetWMIconName(x11_display, win, &text_prop);
2062 }
2063 }
2064 if (status && text_prop.value != NULL)
2065 {
2066 retval = TRUE;
2067 if (!test_only)
2068 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002069 if (get_title)
2070 vim_free(oldtitle);
2071 else
2072 vim_free(oldicon);
Bram Moolenaara12a1612019-01-24 16:39:02 +01002073 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)
2076 oldtitle = vim_strsave((char_u *)text_prop.value);
2077 else
2078 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
2080 else
2081 {
2082 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 }
2097 else
2098 {
2099 if (get_title)
2100 oldtitle = vim_strsave((char_u *)text_prop.value);
2101 else
2102 oldicon = vim_strsave((char_u *)text_prop.value);
2103 }
2104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
2106 XFree((void *)text_prop.value);
2107 }
2108 }
2109 return retval;
2110}
2111
Bram Moolenaar0f873732019-12-05 20:28:46 +01002112// Xutf8 functions are not available on older systems. Note that on some
2113// systems X_HAVE_UTF8_STRING may be defined in a header file but
2114// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2115// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002116#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002117# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118# define USE_UTF8_STRING
2119# endif
2120#endif
2121
2122/*
2123 * Set x11 Window Title
2124 *
2125 * get_x11_windis() must be called before this and have returned OK
2126 */
2127 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002128set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002130 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2131 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2132 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133#ifdef USE_UTF8_STRING
2134 if (enc_utf8)
2135 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2136 NULL, NULL, 0, NULL, NULL, NULL);
2137 else
2138#endif
2139 {
2140#if XtSpecificationRelease >= 4
2141# ifdef FEAT_XFONTSET
2142 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2143 NULL, NULL, 0, NULL, NULL, NULL);
2144# else
2145 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002146 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
Bram Moolenaar0f873732019-12-05 20:28:46 +01002148 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002149 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 XSetWMProperties(x11_display, x11_window, &text_prop,
2151 NULL, NULL, 0, NULL, NULL, NULL);
2152# endif
2153#else
2154 XStoreName(x11_display, x11_window, (char *)title);
2155#endif
2156 }
2157 XFlush(x11_display);
2158}
2159
2160/*
2161 * Set x11 Window icon
2162 *
2163 * get_x11_windis() must be called before this and have returned OK
2164 */
2165 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002166set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002168 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169#ifdef USE_UTF8_STRING
2170 if (enc_utf8)
2171 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2172 NULL, 0, NULL, NULL, NULL);
2173 else
2174#endif
2175 {
2176#if XtSpecificationRelease >= 4
2177# ifdef FEAT_XFONTSET
2178 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2179 NULL, 0, NULL, NULL, NULL);
2180# else
2181 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002182 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002184 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2186 NULL, 0, NULL, NULL, NULL);
2187# endif
2188#else
2189 XSetIconName(x11_display, x11_window, (char *)icon);
2190#endif
2191 }
2192 XFlush(x11_display);
2193}
2194
Bram Moolenaar0f873732019-12-05 20:28:46 +01002195#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002198get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
2200 return FALSE;
2201}
2202
2203 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002204get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205{
2206 if (!test_only)
2207 {
2208 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002209 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002211 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 }
2213 return FALSE;
2214}
2215
Bram Moolenaar0f873732019-12-05 20:28:46 +01002216#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
2218 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002219mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220{
2221 return get_x11_title(TRUE);
2222}
2223
2224 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002225mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226{
2227 return get_x11_icon(TRUE);
2228}
2229
2230/*
2231 * Set the window title and icon.
2232 */
2233 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002234mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
2236 int type = 0;
2237 static int recursive = 0;
2238
Bram Moolenaar0f873732019-12-05 20:28:46 +01002239 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002241 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 return;
2243
Bram Moolenaar0f873732019-12-05 20:28:46 +01002244 // When one of the X11 functions causes a deadly signal, we get here again
2245 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 if (recursive)
2247 return;
2248 ++recursive;
2249
2250 /*
2251 * if the window ID and the display is known, we may use X11 calls
2252 */
2253#ifdef FEAT_X11
2254 if (get_x11_windis() == OK)
2255 type = 1;
2256#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002257# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002258 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 if (gui.in_use)
2260 type = 1;
2261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262#endif
2263
2264 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002265 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 * than x11 calls, because the x11 calls don't always work
2267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 if ((type || *T_TS != NUL) && title != NULL)
2269 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002270 if (oldtitle_outdated)
2271 {
2272 oldtitle_outdated = FALSE;
2273 VIM_CLEAR(oldtitle);
2274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (oldtitle == NULL
2276#ifdef FEAT_GUI
2277 && !gui.in_use
2278#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002279 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 (void)get_x11_title(FALSE);
2281
Bram Moolenaar0f873732019-12-05 20:28:46 +01002282 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 term_settitle(title);
2284#ifdef FEAT_X11
2285 else
2286# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002287 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002289 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002291#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002292 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 else
2294 gui_mch_settitle(title, icon);
2295#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002296 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 }
2298
2299 if ((type || *T_CIS != NUL) && icon != NULL)
2300 {
2301 if (oldicon == NULL
2302#ifdef FEAT_GUI
2303 && !gui.in_use
2304#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002305 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 get_x11_icon(FALSE);
2307
2308 if (*T_CIS != NUL)
2309 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002310 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002312 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 out_flush();
2314 }
2315#ifdef FEAT_X11
2316 else
2317# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002318 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002320 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321#endif
2322 did_set_icon = TRUE;
2323 }
2324 --recursive;
2325}
2326
2327/*
2328 * Restore the window/icon title.
2329 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002330 * SAVE_RESTORE_TITLE only restore title
2331 * SAVE_RESTORE_ICON only restore icon
2332 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 */
2334 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002335mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336{
Bram Moolenaardac13472019-09-16 21:06:21 +02002337 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002338
Bram Moolenaar0f873732019-12-05 20:28:46 +01002339 // only restore the title or icon when it has been set
Bram Moolenaardac13472019-09-16 21:06:21 +02002340 mch_settitle(((which & SAVE_RESTORE_TITLE) && unix_did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002342 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2343
Bram Moolenaare5c83282019-05-03 23:15:37 +02002344 if (do_push_pop)
2345 {
2346 // pop and push from/to the stack
2347 term_pop_title(which);
2348 term_push_title(which);
2349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350}
2351
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
2353/*
2354 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002355 * This matches "xterm.*", thus "xterm-256color", "xterm-kitty", etc.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002356 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 */
2358 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002359vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360{
2361 if (name == NULL)
2362 return FALSE;
2363 return (STRNICMP(name, "xterm", 5) == 0
2364 || STRNICMP(name, "nxterm", 6) == 0
2365 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002366 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002368 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 || STRCMP(name, "builtin_xterm") == 0);
2370}
2371
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002372#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2373/*
2374 * Return TRUE if "name" appears to be that of a terminal
2375 * known to support the xterm-style mouse protocol.
2376 * Relies on term_is_xterm having been set to its correct value.
2377 */
2378 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002379use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002380{
2381 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002382 && (term_is_xterm
2383 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002384 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002385 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002386 || STRICMP(name, "st") == 0
2387 || STRNICMP(name, "st-", 3) == 0
2388 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002389}
2390#endif
2391
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392/*
2393 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2394 * Return 1 for "xterm".
2395 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002396 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002397 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 */
2399 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002400use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002402 if (ttym_flags == TTYM_SGR)
2403 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002404 if (ttym_flags == TTYM_URXVT)
2405 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 if (ttym_flags == TTYM_XTERM2)
2407 return 2;
2408 if (ttym_flags == TTYM_XTERM)
2409 return 1;
2410 return 0;
2411}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002413/*
2414 * Return TRUE if "name" is an iris-ansi terminal name.
2415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002417vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419 if (name == NULL)
2420 return FALSE;
2421 return (STRNICMP(name, "iris-ansi", 9) == 0
2422 || STRCMP(name, "builtin_iris-ansi") == 0);
2423}
2424
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002425/*
2426 * Return TRUE if "name" is a vt300-like terminal name.
2427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002429vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
2431 if (name == NULL)
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002432 return FALSE;
2433 // Actually all ANSI compatible terminals should be here.
2434 // Catch at least VT1xx - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002435 return ((STRNICMP(name, "vt", 2) == 0
Bram Moolenaar88456cd2022-11-18 22:14:09 +00002436 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 || STRCMP(name, "builtin_vt320") == 0);
2438}
2439
2440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 * Insert user name in s[len].
2442 * Return OK if a name found.
2443 */
2444 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002445mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446{
2447#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002448 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 return OK;
2450#else
2451 return mch_get_uname(getuid(), s, len);
2452#endif
2453}
2454
2455/*
2456 * Insert user name for "uid" in s[len].
2457 * Return OK if a name found.
2458 */
2459 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002460mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461{
2462#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2463 struct passwd *pw;
2464
2465 if ((pw = getpwuid(uid)) != NULL
2466 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2467 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002468 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 return OK;
2470 }
2471#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002472 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2473 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474}
2475
2476/*
2477 * Insert host name is s[len].
2478 */
2479
2480#ifdef HAVE_SYS_UTSNAME_H
2481 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002482mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483{
2484 struct utsname vutsname;
2485
2486 if (uname(&vutsname) < 0)
2487 *s = NUL;
2488 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002489 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002491#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
2493# ifdef HAVE_SYS_SYSTEMINFO_H
2494# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2495# endif
2496
2497 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002498mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
2500# ifdef VAXC
2501 vaxc$gethostname((char *)s, len);
2502# else
2503 gethostname((char *)s, len);
2504# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002505 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002507#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
2509/*
2510 * return process ID
2511 */
2512 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002513mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514{
2515 return (long)getpid();
2516}
2517
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002518/*
2519 * return TRUE if process "pid" is still running
2520 */
2521 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002522mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002523{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002524 // If there is no error the process must be running.
2525 if (kill(pid, 0) == 0)
2526 return TRUE;
2527#ifdef ESRCH
2528 // If the error is ESRCH then the process is not running.
2529 if (errno == ESRCH)
2530 return FALSE;
2531#endif
2532 // If the process is running and owned by another user we get EPERM. With
2533 // other errors the process might be running, assuming it is then.
2534 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002535}
2536
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002539strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 extern int sys_nerr;
2542 extern char *sys_errlist[];
2543 static char er[20];
2544
2545 if (err > 0 && err < sys_nerr)
2546 return (sys_errlist[err]);
2547 sprintf(er, "Error %d", err);
2548 return er;
2549}
2550#endif
2551
2552/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002553 * Get name of current directory into buffer "buf" of length "len" bytes.
2554 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 * Return OK for success, FAIL for failure.
2556 */
2557 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002558mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559{
2560#if defined(USE_GETCWD)
2561 if (getcwd((char *)buf, len) == NULL)
2562 {
2563 STRCPY(buf, strerror(errno));
2564 return FAIL;
2565 }
2566 return OK;
2567#else
2568 return (getwd((char *)buf) != NULL ? OK : FAIL);
2569#endif
2570}
2571
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002573 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 *
2575 * return FAIL for failure, OK for success
2576 */
2577 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002578mch_FullName(
2579 char_u *fname,
2580 char_u *buf,
2581 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002582 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583{
2584 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002585#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002587 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 char_u olddir[MAXPATHL];
2590 char_u *p;
2591 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002592#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002593 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2594 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002595#endif
2596
Bram Moolenaar38323e42007-03-06 19:22:53 +00002597#ifdef VMS
2598 fname = vms_fixfilename(fname);
2599#endif
2600
Bram Moolenaara2442432007-04-26 14:26:37 +00002601#ifdef __CYGWIN__
2602 /*
2603 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2604 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002605# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002606 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2607 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002608 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2609 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002610# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002611 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002612# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002613 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002614#endif
2615
Bram Moolenaar0f873732019-12-05 20:28:46 +01002616 // Expand it if forced or not an absolute path.
2617 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002618 if ((force || !mch_isFullName(fname))
2619 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 {
2621 /*
2622 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002623 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 * This will get the correct path name with "../" things.
2625 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002626 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002628 if (STRCMP(p, "/..") == 0)
2629 // for "/path/dir/.." include the "/.."
2630 p += 3;
2631
Bram Moolenaar38323e42007-03-06 19:22:53 +00002632#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 /*
2634 * Use fchdir() if possible, it's said to be faster and more
2635 * reliable. But on SunOS 4 it might not work. Check this by
2636 * doing a fchdir() right now.
2637 */
2638 if (!dont_fchdir)
2639 {
2640 fd = open(".", O_RDONLY | O_EXTRA, 0);
2641 if (fd >= 0 && fchdir(fd) < 0)
2642 {
2643 close(fd);
2644 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002645 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 }
2647 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
Bram Moolenaar0f873732019-12-05 20:28:46 +01002650 // Only change directory when we are sure we can return to where
2651 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002653#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 (mch_dirname(olddir, MAXPATHL) == FAIL
2657 || mch_chdir((char *)olddir) != 0))
2658 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002659 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 retval = FAIL;
2661 }
2662 else
2663 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002664 // The directory is copied into buf[], to be able to remove
2665 // the file name without changing it (could be a string in
2666 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (p - fname >= len)
2668 retval = FAIL;
2669 else
2670 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002671 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002673 {
2674 // Path does not exist (yet). For a full path fail,
2675 // will use the path as-is. For a relative path use
2676 // the current directory and append the file name.
2677 if (mch_isFullName(fname))
2678 retval = FAIL;
2679 else
2680 p = NULL;
2681 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002682 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002684 else
2685 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 *buf = NUL;
2687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
2689 }
2690 if (mch_dirname(buf, len) == FAIL)
2691 {
2692 retval = FAIL;
2693 *buf = NUL;
2694 }
2695 if (p != NULL)
2696 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002697#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 if (fd >= 0)
2699 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002700 if (p_verbose >= 5)
2701 {
2702 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002703 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002704 verbose_leave();
2705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
2708 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 l = mch_chdir((char *)olddir);
2711 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002712 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
itchyny051a40c2021-10-20 10:00:05 +01002714#ifdef HAVE_FCHDIR
2715 if (fd >= 0)
2716 close(fd);
2717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718
2719 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002720 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002721 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002722#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002723 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002725 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002728
Bram Moolenaar0f873732019-12-05 20:28:46 +01002729 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002730 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 return FAIL;
2732
Bram Moolenaar0f873732019-12-05 20:28:46 +01002733 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 if (STRCMP(fname, ".") != 0)
2735 STRCAT(buf, fname);
2736
2737 return OK;
2738}
2739
2740/*
2741 * Return TRUE if "fname" does not depend on the current directory.
2742 */
2743 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002744mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002746#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 return ( fname[0] == '/' || fname[0] == '.' ||
2748 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2749 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2750 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002751#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753#endif
2754}
2755
Bram Moolenaar24552be2005-12-10 20:17:30 +00002756#if defined(USE_FNAME_CASE) || defined(PROTO)
2757/*
2758 * Set the case of the file name, if it already exists. This will cause the
2759 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002760 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002761 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002762 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002763fname_case(
2764 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002765 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002766{
2767 struct stat st;
2768 char_u *slash, *tail;
2769 DIR *dirp;
2770 struct dirent *dp;
2771
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002772 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002773 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002774 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002775 slash = vim_strrchr(name, '/');
2776 if (slash == NULL)
2777 {
2778 dirp = opendir(".");
2779 tail = name;
2780 }
2781 else
2782 {
2783 *slash = NUL;
2784 dirp = opendir((char *)name);
2785 *slash = '/';
2786 tail = slash + 1;
2787 }
2788
2789 if (dirp != NULL)
2790 {
2791 while ((dp = readdir(dirp)) != NULL)
2792 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002793 // Only accept names that differ in case and are the same byte
2794 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002795 if (STRICMP(tail, dp->d_name) == 0
2796 && STRLEN(tail) == STRLEN(dp->d_name))
2797 {
2798 char_u newname[MAXPATHL + 1];
2799 struct stat st2;
2800
Bram Moolenaar0f873732019-12-05 20:28:46 +01002801 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002802 vim_strncpy(newname, name, MAXPATHL);
2803 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2804 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002805 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002806 && st.st_ino == st2.st_ino
2807 && st.st_dev == st2.st_dev)
2808 {
2809 STRCPY(tail, dp->d_name);
2810 break;
2811 }
2812 }
2813 }
2814
2815 closedir(dirp);
2816 }
2817 }
2818}
2819#endif
2820
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821/*
2822 * Get file permissions for 'name'.
2823 * Returns -1 when it doesn't exist.
2824 */
2825 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002826mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
2828 struct stat statb;
2829
Bram Moolenaar0f873732019-12-05 20:28:46 +01002830 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831#ifdef VMS
2832 if (stat((char *)vms_fixfilename(name), &statb))
2833#else
2834 if (stat((char *)name, &statb))
2835#endif
2836 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002837#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002838 // The top bit makes the value negative, which means the file doesn't
2839 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002840 return statb.st_mode & ~S_ADDACE;
2841#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844}
2845
2846/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002847 * Set file permission for "name" to "perm".
2848 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 */
2850 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002851mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 return (chmod((char *)
2854#ifdef VMS
2855 vms_fixfilename(name),
2856#else
2857 name,
2858#endif
2859 (mode_t)perm) == 0 ? OK : FAIL);
2860}
2861
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002862#if defined(HAVE_FCHMOD) || defined(PROTO)
2863/*
2864 * Set file permission for open file "fd" to "perm".
2865 * Return FAIL for failure, OK otherwise.
2866 */
2867 int
2868mch_fsetperm(int fd, long perm)
2869{
2870 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2871}
2872#endif
2873
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874#if defined(HAVE_ACL) || defined(PROTO)
2875# ifdef HAVE_SYS_ACL_H
2876# include <sys/acl.h>
2877# endif
2878# ifdef HAVE_SYS_ACCESS_H
2879# include <sys/access.h>
2880# endif
2881
2882# ifdef HAVE_SOLARIS_ACL
2883typedef struct vim_acl_solaris_T {
2884 int acl_cnt;
2885 aclent_t *acl_entry;
2886} vim_acl_solaris_T;
2887# endif
2888
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002889#if defined(HAVE_SELINUX) || defined(PROTO)
2890/*
2891 * Copy security info from "from_file" to "to_file".
2892 */
2893 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002894mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002895{
2896 if (from_file == NULL)
2897 return;
2898
2899 if (selinux_enabled == -1)
2900 selinux_enabled = is_selinux_enabled();
2901
2902 if (selinux_enabled > 0)
2903 {
Bram Moolenaar89560232020-10-09 23:04:47 +02002904 // Use "char *" instead of "security_context_t" to avoid a deprecation
2905 // warning.
2906 char *from_context = NULL;
2907 char *to_context = NULL;
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002908
2909 if (getfilecon((char *)from_file, &from_context) < 0)
2910 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002911 // If the filesystem doesn't support extended attributes,
2912 // the original had no special security context and the
2913 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002914 if (errno == EOPNOTSUPP)
2915 return;
2916
Bram Moolenaar32526b32019-01-19 17:43:09 +01002917 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002918 msg_outtrans(from_file);
2919 msg_putchar('\n');
2920 return;
2921 }
2922 if (getfilecon((char *)to_file, &to_context) < 0)
2923 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002924 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002925 msg_outtrans(to_file);
2926 msg_putchar('\n');
2927 freecon (from_context);
2928 return ;
2929 }
2930 if (strcmp(from_context, to_context) != 0)
2931 {
2932 if (setfilecon((char *)to_file, from_context) < 0)
2933 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002934 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002935 msg_outtrans(to_file);
2936 msg_putchar('\n');
2937 }
2938 }
2939 freecon(to_context);
2940 freecon(from_context);
2941 }
2942}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002943#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002944
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002945#if defined(HAVE_SMACK) && !defined(PROTO)
2946/*
2947 * Copy security info from "from_file" to "to_file".
2948 */
2949 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002950mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002951{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002952 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002953 {
2954 XATTR_NAME_SMACK,
2955 XATTR_NAME_SMACKEXEC,
2956 XATTR_NAME_SMACKMMAP
2957 };
2958
2959 char buffer[SMACK_LABEL_LEN];
2960 const char *name;
2961 int index;
2962 int ret;
2963 ssize_t size;
2964
2965 if (from_file == NULL)
2966 return;
2967
2968 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2969 / sizeof(smack_copied_attributes)[0]) ; index++)
2970 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002971 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002972 name = smack_copied_attributes[index];
2973
Bram Moolenaar0f873732019-12-05 20:28:46 +01002974 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002975 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2976 if (size >= 0)
2977 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002978 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002979 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2980 if (ret < 0)
2981 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002982 vim_snprintf((char *)IObuff, IOSIZE,
2983 _("Could not set security context %s for %s"),
2984 name, to_file);
2985 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002986 msg_putchar('\n');
2987 }
2988 }
2989 else
2990 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002991 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002992 switch (errno)
2993 {
2994 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002995 // extended attributes aren't supported or enabled
2996 // should a message be echoed? not sure...
2997 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002998
2999 case ERANGE:
3000 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003001 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003002 vim_snprintf((char *)IObuff, IOSIZE,
3003 _("Could not get security context %s for %s. Removing it!"),
3004 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003005 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003006 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003007 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003008
3009 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003010 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003011 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003012 // Silently ignore errors, apparently this happens when
3013 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003014 break;
3015 }
3016 }
3017 }
3018}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003019#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003020
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021/*
3022 * Return a pointer to the ACL of file "fname" in allocated memory.
3023 * Return NULL if the ACL is not available for whatever reason.
3024 */
3025 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003026mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027{
3028 vim_acl_T ret = NULL;
3029#ifdef HAVE_POSIX_ACL
3030 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3031#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003032#ifdef HAVE_SOLARIS_ZFS_ACL
3033 acl_t *aclent;
3034
3035 if (acl_get((char *)fname, 0, &aclent) < 0)
3036 return NULL;
3037 ret = (vim_acl_T)aclent;
3038#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039#ifdef HAVE_SOLARIS_ACL
3040 vim_acl_solaris_T *aclent;
3041
3042 aclent = malloc(sizeof(vim_acl_solaris_T));
3043 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3044 {
3045 free(aclent);
3046 return NULL;
3047 }
3048 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3049 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3050 {
3051 free(aclent->acl_entry);
3052 free(aclent);
3053 return NULL;
3054 }
3055 ret = (vim_acl_T)aclent;
3056#else
3057#if defined(HAVE_AIX_ACL)
3058 int aclsize;
3059 struct acl *aclent;
3060
3061 aclsize = sizeof(struct acl);
3062 aclent = malloc(aclsize);
3063 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3064 {
3065 if (errno == ENOSPC)
3066 {
3067 aclsize = aclent->acl_len;
3068 aclent = realloc(aclent, aclsize);
3069 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3070 {
3071 free(aclent);
3072 return NULL;
3073 }
3074 }
3075 else
3076 {
3077 free(aclent);
3078 return NULL;
3079 }
3080 }
3081 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003082#endif // HAVE_AIX_ACL
3083#endif // HAVE_SOLARIS_ACL
3084#endif // HAVE_SOLARIS_ZFS_ACL
3085#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 return ret;
3087}
3088
3089/*
3090 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3091 */
3092 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003093mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094{
3095 if (aclent == NULL)
3096 return;
3097#ifdef HAVE_POSIX_ACL
3098 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3099#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003100#ifdef HAVE_SOLARIS_ZFS_ACL
3101 acl_set((char *)fname, (acl_t *)aclent);
3102#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103#ifdef HAVE_SOLARIS_ACL
3104 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3105 ((vim_acl_solaris_T *)aclent)->acl_entry);
3106#else
3107#ifdef HAVE_AIX_ACL
3108 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003109#endif // HAVE_AIX_ACL
3110#endif // HAVE_SOLARIS_ACL
3111#endif // HAVE_SOLARIS_ZFS_ACL
3112#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113}
3114
3115 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003116mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
3118 if (aclent == NULL)
3119 return;
3120#ifdef HAVE_POSIX_ACL
3121 acl_free((acl_t)aclent);
3122#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003123#ifdef HAVE_SOLARIS_ZFS_ACL
3124 acl_free((acl_t *)aclent);
3125#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126#ifdef HAVE_SOLARIS_ACL
3127 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3128 free(aclent);
3129#else
3130#ifdef HAVE_AIX_ACL
3131 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003132#endif // HAVE_AIX_ACL
3133#endif // HAVE_SOLARIS_ACL
3134#endif // HAVE_SOLARIS_ZFS_ACL
3135#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136}
3137#endif
3138
3139/*
3140 * Set hidden flag for "name".
3141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003143mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003145 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146}
3147
3148/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003149 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 * return FALSE if "name" is not a directory
3151 * return FALSE for error
3152 */
3153 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003154mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155{
3156 struct stat statb;
3157
Bram Moolenaar0f873732019-12-05 20:28:46 +01003158 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 return FALSE;
3160 if (stat((char *)name, &statb))
3161 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163}
3164
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003165/*
3166 * return TRUE if "name" is a directory, NOT a symlink to a directory
3167 * return FALSE if "name" is not a directory
3168 * return FALSE for error
3169 */
3170 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003171mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003172{
3173 struct stat statb;
3174
Bram Moolenaar0f873732019-12-05 20:28:46 +01003175 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003176 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003177 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003178 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003179 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003180}
3181
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182/*
3183 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3184 */
3185 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003186executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187{
3188 struct stat st;
3189
3190 if (stat((char *)name, &st))
3191 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003192#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003193 // Like on Unix system file can have executable rights but not necessarily
3194 // be an executable, but on Unix is not a default for an ordinary file to
3195 // have an executable flag - on VMS it is in most cases.
3196 // Therefore, this check does not have any sense - let keep us to the
3197 // conventions instead:
3198 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003199 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003200 int vms_executable = 0;
3201 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3202 {
3203 if (strstr(vms_tolower((char*)name),".exe") != NULL
3204 || strstr(vms_tolower((char*)name),".com")!= NULL)
3205 vms_executable = 1;
3206 }
3207 return vms_executable;
3208#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211}
3212
3213/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003214 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003215 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 * Return -1 if unknown.
3217 */
3218 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003219mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220{
3221 char_u *buf;
3222 char_u *p, *e;
3223 int retval;
3224
Bram Moolenaar0f873732019-12-05 20:28:46 +01003225 // When "use_path" is false and if it's an absolute or relative path don't
3226 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003227 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003228 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003229 // There must be a path separator, files in the current directory
3230 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003231 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003232 {
3233 if (path != NULL)
3234 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003235 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003236 *path = FullName_save(name, TRUE);
3237 else
3238 *path = vim_strsave(name);
3239 }
3240 return TRUE;
3241 }
3242 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245 p = (char_u *)getenv("PATH");
3246 if (p == NULL || *p == NUL)
3247 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003248 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 if (buf == NULL)
3250 return -1;
3251
3252 /*
3253 * Walk through all entries in $PATH to check if "name" exists there and
3254 * is an executable file.
3255 */
3256 for (;;)
3257 {
3258 e = (char_u *)strchr((char *)p, ':');
3259 if (e == NULL)
3260 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003261 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 STRCPY(buf, "./");
3263 else
3264 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003265 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 add_pathsep(buf);
3267 }
3268 STRCAT(buf, name);
3269 retval = executable_file(buf);
3270 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003271 {
3272 if (path != NULL)
3273 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003274 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003275 *path = FullName_save(buf, TRUE);
3276 else
3277 *path = vim_strsave(buf);
3278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281
3282 if (*e != ':')
3283 break;
3284 p = e + 1;
3285 }
3286
3287 vim_free(buf);
3288 return retval;
3289}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
3291/*
3292 * Check what "name" is:
3293 * NODE_NORMAL: file or directory (or doesn't exist)
3294 * NODE_WRITABLE: writable device, socket, fifo, etc.
3295 * NODE_OTHER: non-writable things
3296 */
3297 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003298mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
3300 struct stat st;
3301
3302 if (stat((char *)name, &st))
3303 return NODE_NORMAL;
3304 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3305 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003306 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003308 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 return NODE_WRITABLE;
3310}
3311
3312 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003313mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314{
3315#ifdef HAVE_CHECK_STACK_GROWTH
3316 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 check_stack_growth((char *)&i);
3319
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003320# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 get_stack_limit();
3322# endif
3323
3324#endif
3325
3326 /*
3327 * Setup an alternative stack for signals. Helps to catch signals when
3328 * running out of stack space.
3329 * Use of sigaltstack() is preferred, it's more portable.
3330 * Ignore any errors.
3331 */
3332#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003333 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 init_signal_stack();
3335#endif
3336}
3337
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003338#if defined(EXITFREE) || defined(PROTO)
3339 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003340mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003341{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003342# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3343 if (clip_star.owned)
3344 clip_lose_selection(&clip_star);
3345 if (clip_plus.owned)
3346 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003347# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003348# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003349 if (xterm_Shell != (Widget)0)
3350 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003351# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003352 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003353 if (xterm_dpy != NULL)
3354 XtCloseDisplay(xterm_dpy);
3355 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003356 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003357 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003358# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003359 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003360# endif
3361 }
3362# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003363# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003364# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003365 if (x11_display != NULL
3366# ifdef FEAT_XCLIPBOARD
3367 && x11_display != xterm_dpy
3368# endif
3369 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003370 XCloseDisplay(x11_display);
3371# endif
3372# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003373 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003374# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003375 vim_free(oldtitle);
3376 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003377}
3378#endif
3379
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380/*
3381 * Output a newline when exiting.
3382 * Make sure the newline goes to the same stream as the text.
3383 */
3384 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003385exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003387 if (silent_mode)
3388 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 if (newline_on_exit || msg_didout)
3390 {
3391 if (msg_use_printf())
3392 {
3393 if (info_message)
3394 mch_msg("\n");
3395 else
3396 mch_errmsg("\r\n");
3397 }
3398 else
3399 out_char('\n');
3400 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003401 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003403 restore_cterm_colors(); // get original colors back
3404 msg_clr_eos_force(); // clear the rest of the display
3405 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
3407}
3408
Bram Moolenaarb4151682020-05-11 22:13:28 +02003409#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003410# if (defined(__GNUC__) \
3411 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3412 || (defined(__clang__) && (__clang_major__ >= 12))
3413extern void __gcov_dump(void);
3414extern void __gcov_reset(void);
3415# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3416# else
3417extern void __gcov_flush(void);
3418# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003419#endif
3420
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003422mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
3424 exiting = TRUE;
3425
3426#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3427 x11_export_final_selection();
3428#endif
3429
3430#ifdef FEAT_GUI
3431 if (!gui.in_use)
3432#endif
3433 {
3434 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003435 if (!is_not_a_term())
3436 {
3437 // restore xterm title and icon name
3438 mch_restore_title(SAVE_RESTORE_BOTH);
3439 term_pop_title(SAVE_RESTORE_BOTH);
3440 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003441
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 /*
3443 * When t_ti is not empty but it doesn't cause swapping terminal
3444 * pages, need to output a newline when msg_didout is set. But when
3445 * t_ti does swap pages it should not go to the shell page. Do this
3446 * before stoptermcap().
3447 */
3448 if (swapping_screen() && !newline_on_exit)
3449 exit_scroll();
3450
Bram Moolenaar0f873732019-12-05 20:28:46 +01003451 // Stop termcap: May need to check for T_CRV response, which
3452 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 stoptermcap();
3454
3455 /*
3456 * A newline is only required after a message in the alternate screen.
3457 * This is set to TRUE by wait_return().
3458 */
3459 if (!swapping_screen() || newline_on_exit)
3460 exit_scroll();
3461
Bram Moolenaar0f873732019-12-05 20:28:46 +01003462 // Cursor may have been switched off without calling starttermcap()
3463 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 if (full_screen)
3465 cursor_on();
3466 }
3467 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003468 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003469
3470#ifdef USE_GCOV_FLUSH
3471 // Flush coverage info before possibly being killed by a deadly signal.
3472 __gcov_flush();
3473#endif
3474
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 may_core_dump();
3476#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 gui_exit(r);
3479#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003480
Bram Moolenaar56718732006-03-15 22:53:57 +00003481#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003482 mac_conv_cleanup();
3483#endif
3484
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003486 // A core dump won't be created if the signal handler
3487 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (deadly_signal != 0)
3489 return;
3490#endif
3491
Bram Moolenaar009b2592004-10-24 19:18:58 +00003492#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003493 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003494#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003495
3496#ifdef EXITFREE
3497 free_all_mem();
3498#endif
3499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 exit(r);
3501}
3502
3503 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003504may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505{
3506 if (deadly_signal != 0)
3507 {
3508 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003509 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511}
3512
3513#ifndef VMS
3514
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003515/*
3516 * Get the file descriptor to use for tty operations.
3517 */
3518 static int
3519get_tty_fd(int fd)
3520{
3521 int tty_fd = fd;
3522
3523#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3524 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3525 // "fd" when it is a master device.
3526 if (mch_isatty(fd) > 1)
3527 {
3528 char *name;
3529
3530 name = ptsname(fd);
3531 if (name == NULL)
3532 return -1;
3533
3534 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3535 if (tty_fd < 0)
3536 return -1;
3537 }
3538#endif
3539 return tty_fd;
3540}
3541
3542 static int
3543mch_tcgetattr(int fd, void *term)
3544{
3545 int tty_fd;
3546 int retval = -1;
3547
3548 tty_fd = get_tty_fd(fd);
3549 if (tty_fd >= 0)
3550 {
3551#ifdef NEW_TTY_SYSTEM
3552# ifdef HAVE_TERMIOS_H
3553 retval = tcgetattr(tty_fd, (struct termios *)term);
3554# else
3555 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3556# endif
3557#else
3558 // for "old" tty systems
3559 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3560#endif
3561 if (tty_fd != fd)
3562 close(tty_fd);
3563 }
3564 return retval;
3565}
3566
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003568mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
3570 static int first = TRUE;
3571
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003572#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573# ifdef HAVE_TERMIOS_H
3574 static struct termios told;
3575 struct termios tnew;
3576# else
3577 static struct termio told;
3578 struct termio tnew;
3579# endif
3580
3581 if (first)
3582 {
3583 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003584 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
3586
3587 tnew = told;
3588 if (tmode == TMODE_RAW)
3589 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003590 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003591 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3592 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003594# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003595 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596# endif
3597 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003598# ifdef ONLCR
3599 // Don't map NL -> CR NL, we do it ourselves.
3600 // Also disable expanding tabs if possible.
3601# ifdef XTABS
3602 tnew.c_oflag &= ~(ONLCR | XTABS);
3603# else
3604# ifdef TAB3
3605 tnew.c_oflag &= ~(ONLCR | TAB3);
3606# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003608# endif
3609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003611 tnew.c_cc[VMIN] = 1; // return after 1 char
3612 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 }
3614 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003615 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003616 // Also reset ICANON here, otherwise on Solaris select() won't see
3617 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003618 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003619 tnew.c_cc[VMIN] = 1; // return after 1 char
3620 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623# if defined(HAVE_TERMIOS_H)
3624 {
3625 int n = 10;
3626
Bram Moolenaar0f873732019-12-05 20:28:46 +01003627 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3628 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3630 && errno == EINTR && n > 0)
3631 --n;
3632 }
3633# else
3634 ioctl(read_cmd_fd, TCSETA, &tnew);
3635# endif
3636
3637#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 /*
3639 * for "old" tty systems
3640 */
3641# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003642# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643# endif
3644 static struct sgttyb ttybold;
3645 struct sgttyb ttybnew;
3646
3647 if (first)
3648 {
3649 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003650 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 }
3652
3653 ttybnew = ttybold;
3654 if (tmode == TMODE_RAW)
3655 {
3656 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3657 ttybnew.sg_flags |= RAW;
3658 }
3659 else if (tmode == TMODE_SLEEP)
3660 ttybnew.sg_flags &= ~(ECHO);
3661 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3662#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003663 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664}
3665
3666/*
3667 * Try to get the code for "t_kb" from the stty setting
3668 *
3669 * Even if termcap claims a backspace key, the user's setting *should*
3670 * prevail. stty knows more about reality than termcap does, and if
3671 * somebody's usual erase key is DEL (which, for most BSD users, it will
3672 * be), they're going to get really annoyed if their erase key starts
3673 * doing forward deletes for no reason. (Eric Fischer)
3674 */
3675 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003676get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003678 ttyinfo_T info;
3679 char_u buf[2];
3680 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003682 if (get_tty_info(read_cmd_fd, &info) == OK)
3683 {
3684 intr_char = info.interrupt;
3685 buf[0] = info.backspace;
3686 buf[1] = NUL;
3687 add_termcode((char_u *)"kb", buf, FALSE);
3688
Bram Moolenaar0f873732019-12-05 20:28:46 +01003689 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003690 p = find_termcode((char_u *)"kD");
3691 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3692 do_fixdel(NULL);
3693 }
3694}
3695
3696/*
3697 * Obtain the characters that Backspace and Enter produce on "fd".
3698 * Returns OK or FAIL.
3699 */
3700 int
3701get_tty_info(int fd, ttyinfo_T *info)
3702{
3703#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704# ifdef HAVE_TERMIOS_H
3705 struct termios keys;
3706# else
3707 struct termio keys;
3708# endif
3709
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003710 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003712 info->backspace = keys.c_cc[VERASE];
3713 info->interrupt = keys.c_cc[VINTR];
3714 if (keys.c_iflag & ICRNL)
3715 info->enter = NL;
3716 else
3717 info->enter = CAR;
3718 if (keys.c_oflag & ONLCR)
3719 info->nl_does_cr = TRUE;
3720 else
3721 info->nl_does_cr = FALSE;
3722 return OK;
3723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003725 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 struct sgttyb keys;
3727
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003728 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003730 info->backspace = keys.sg_erase;
3731 info->interrupt = keys.sg_kill;
3732 info->enter = CAR;
3733 info->nl_does_cr = TRUE;
3734 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003737 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738}
3739
Bram Moolenaar0f873732019-12-05 20:28:46 +01003740#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003742static int mouse_ison = FALSE;
3743
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744/*
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003745 * Set mouse clicks on or off and possible enable mouse movement events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 */
3747 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003748mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003750#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003751 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 int xterm_mouse_vers;
3754
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003755#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003756 if (!on)
3757 // Make sure not tracing mouse movements. Important when a button-down
3758 // was received but no release yet.
3759 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003760#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003761
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003762 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003763#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003764 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003765#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003766 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003767 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 return;
3769
3770 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003771
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003772#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003773 if (ttym_flags == TTYM_URXVT)
3774 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003775 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003776 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003777 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003778#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003779
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003780 if (ttym_flags == TTYM_SGR)
3781 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003782 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003783 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003784 mouse_ison = on;
3785 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003786
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003787#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003788 if (bevalterm_ison != (p_bevalterm && on))
3789 {
3790 bevalterm_ison = (p_bevalterm && on);
3791 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003792 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003793 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003794 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003795#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003796
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 if (xterm_mouse_vers > 0)
3798 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003799 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 out_str_nf((char_u *)
3801 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003802 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003803#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003804 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003805#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003806 "\033[?1002h")
3807 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003808 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003810 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003811 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 }
3813
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003814#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 else if (ttym_flags == TTYM_DEC)
3816 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003817 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003819 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003821 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003825#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 else
3827 {
3828 if (on)
3829 {
3830 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003831 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833 else
3834 {
3835 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003836 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838 }
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_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003842 else
3843 {
3844 if (on)
3845 {
3846 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003847 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003848 }
3849 else
3850 {
3851 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003852 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003853 }
3854 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003855#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003856
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003857#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 else
3859 {
3860 if (on)
3861 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003862 // D - Enable Mouse up/down messages
3863 // L - Enable Left Button Reporting
3864 // M - Enable Middle Button Reporting
3865 // R - Enable Right Button Reporting
3866 // K - Enable SHIFT and CTRL key Reporting
3867 // + - Enable Advanced messaging of mouse moves and up/down messages
3868 // Q - Quiet No Ack
3869 // # - Numeric value of mouse pointer required
3870 // 0 = Multiview 2000 cursor, used as standard
3871 // 1 = Windows Arrow
3872 // 2 = Windows I Beam
3873 // 3 = Windows Hour Glass
3874 // 4 = Windows Cross Hair
3875 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003876# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003877 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003878 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003879# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003880 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003881# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003882 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 }
3884 else
3885 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003886 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003887 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 }
3889 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003890#endif
3891#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 else
3893 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003894 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 if (on)
3896 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3897 else
3898 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003899 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902}
3903
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003904#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003905/*
3906 * Called when 'balloonevalterm' changed.
3907 */
3908 void
3909mch_bevalterm_changed(void)
3910{
3911 mch_setmouse(mouse_ison);
3912}
3913#endif
3914
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915/*
3916 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3917 */
3918 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003919check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
3921# ifdef FEAT_MOUSE_XTERM
3922 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003923# ifdef FEAT_MOUSE_URXVT
3924 && use_xterm_mouse() != 3
3925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926# ifdef FEAT_GUI
3927 && !gui.in_use
3928# endif
3929 )
3930 {
3931 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003932 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 if (*p_mouse != NUL)
3934 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003935 // force mouse off and maybe on to send possibly new mouse
3936 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 mch_setmouse(FALSE);
3938 setmouse();
3939 }
3940 }
3941 else
3942 del_mouse_termcode(KS_MOUSE);
3943# endif
3944
3945# ifdef FEAT_MOUSE_GPM
3946 if (!use_xterm_mouse()
3947# ifdef FEAT_GUI
3948 && !gui.in_use
3949# endif
3950 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003951 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003952 else
3953 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954# endif
3955
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003956# ifdef FEAT_SYSMOUSE
3957 if (!use_xterm_mouse()
3958# ifdef FEAT_GUI
3959 && !gui.in_use
3960# endif
3961 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003962 set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003963# endif
3964
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003966 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 if (!use_xterm_mouse()
3968# ifdef FEAT_GUI
3969 && !gui.in_use
3970# endif
3971 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003972 set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 else
3974 del_mouse_termcode(KS_JSBTERM_MOUSE);
3975# endif
3976
3977# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003978 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3979 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 if (!use_xterm_mouse()
3981# ifdef FEAT_GUI
3982 && !gui.in_use
3983# endif
3984 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003985 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 else
3987 del_mouse_termcode(KS_NETTERM_MOUSE);
3988# endif
3989
3990# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003991 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003992 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993# ifdef FEAT_GUI
3994 && !gui.in_use
3995# endif
3996 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003997 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003998 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 else
4000 del_mouse_termcode(KS_DEC_MOUSE);
4001# endif
4002# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004003 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004004 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005# ifdef FEAT_GUI
4006 && !gui.in_use
4007# endif
4008 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004009 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 else
4011 del_mouse_termcode(KS_PTERM_MOUSE);
4012# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004013# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004014 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004015# ifdef FEAT_GUI
4016 && !gui.in_use
4017# endif
4018 )
4019 {
4020 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004021 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004022
4023 if (*p_mouse != NUL)
4024 {
4025 mch_setmouse(FALSE);
4026 setmouse();
4027 }
4028 }
4029 else
4030 del_mouse_termcode(KS_URXVT_MOUSE);
4031# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004032 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004033# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004034 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004035# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004036 )
4037 {
4038 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004039 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004040
4041 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004042 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004043
4044 if (*p_mouse != NUL)
4045 {
4046 mch_setmouse(FALSE);
4047 setmouse();
4048 }
4049 }
4050 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004051 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004052 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004053 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057#ifndef VMS
4058
4059/*
4060 * Try to get the current window size:
4061 * 1. with an ioctl(), most accurate method
4062 * 2. from the environment variables LINES and COLUMNS
4063 * 3. from the termcap
4064 * 4. keep using the old values
4065 * Return OK when size could be determined, FAIL otherwise.
4066 */
4067 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004068mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069{
4070 long rows = 0;
4071 long columns = 0;
4072 char_u *p;
4073
4074 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 * 1. try using an ioctl. It is the most accurate method.
4076 *
4077 * Try using TIOCGWINSZ first, some systems that have it also define
4078 * TIOCGSIZE but don't have a struct ttysize.
4079 */
4080# ifdef TIOCGWINSZ
4081 {
4082 struct winsize ws;
4083 int fd = 1;
4084
Bram Moolenaar0f873732019-12-05 20:28:46 +01004085 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 if (!isatty(fd) && isatty(read_cmd_fd))
4087 fd = read_cmd_fd;
4088 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4089 {
4090 columns = ws.ws_col;
4091 rows = ws.ws_row;
4092 }
4093 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004094# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095# ifdef TIOCGSIZE
4096 {
4097 struct ttysize ts;
4098 int fd = 1;
4099
Bram Moolenaar0f873732019-12-05 20:28:46 +01004100 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 if (!isatty(fd) && isatty(read_cmd_fd))
4102 fd = read_cmd_fd;
4103 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4104 {
4105 columns = ts.ts_cols;
4106 rows = ts.ts_lines;
4107 }
4108 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004109# endif // TIOCGSIZE
4110# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
4112 /*
4113 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004114 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4115 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004117 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 {
4119 if ((p = (char_u *)getenv("LINES")))
4120 rows = atoi((char *)p);
4121 if ((p = (char_u *)getenv("COLUMNS")))
4122 columns = atoi((char *)p);
4123 }
4124
4125#ifdef HAVE_TGETENT
4126 /*
4127 * 3. try reading "co" and "li" entries from termcap
4128 */
4129 if (columns == 0 || rows == 0)
4130 getlinecol(&columns, &rows);
4131#endif
4132
4133 /*
4134 * 4. If everything fails, use the old values
4135 */
4136 if (columns <= 0 || rows <= 0)
4137 return FAIL;
4138
4139 Rows = rows;
4140 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004141 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 return OK;
4143}
4144
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004145#if defined(FEAT_TERMINAL) || defined(PROTO)
4146/*
4147 * Report the windows size "rows" and "cols" to tty "fd".
4148 */
4149 int
4150mch_report_winsize(int fd, int rows, int cols)
4151{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004152 int tty_fd;
4153 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004154
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004155 tty_fd = get_tty_fd(fd);
4156 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004157 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004158# if defined(TIOCSWINSZ)
4159 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004160
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004161 ws.ws_col = cols;
4162 ws.ws_row = rows;
4163 ws.ws_xpixel = cols * 5;
4164 ws.ws_ypixel = rows * 10;
4165 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4166 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4167 retval == 0 ? "success" : "failed");
4168# elif defined(TIOCSSIZE)
4169 struct ttysize ts;
4170
4171 ts.ts_cols = cols;
4172 ts.ts_lines = rows;
4173 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4174 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4175 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004176# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004177 if (tty_fd != fd)
4178 close(tty_fd);
4179 }
4180 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004181}
4182#endif
4183
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184/*
4185 * Try to set the window size to Rows and Columns.
4186 */
4187 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004188mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189{
4190 if (*T_CWS)
4191 {
4192 /*
4193 * NOTE: if you get an error here that term_set_winsize() is
4194 * undefined, check the output of configure. It could probably not
4195 * find a ncurses, termcap or termlib library.
4196 */
4197 term_set_winsize((int)Rows, (int)Columns);
4198 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004199 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201}
4202
Bram Moolenaar0f873732019-12-05 20:28:46 +01004203#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
4205/*
4206 * Rows and/or Columns has changed.
4207 */
4208 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004209mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004211 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212}
4213
Bram Moolenaar205b8862011-09-07 15:04:31 +02004214/*
4215 * Wait for process "child" to end.
4216 * Return "child" if it exited properly, <= 0 on error.
4217 */
4218 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004219wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004220{
4221 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004222 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004223
4224 while (wait_pid != child)
4225 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004226 // When compiled with Python threads are probably used, in which case
4227 // wait() sometimes hangs for no obvious reason. Use waitpid()
4228 // instead and loop (like the GUI). Also needed for other interfaces,
4229 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004230# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004231 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004232# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004233 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004234# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004235 if (wait_pid == 0)
4236 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004237 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004238 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004239 if (++delay_msec > 10)
4240 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004241 continue;
4242 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004243 if (wait_pid <= 0
4244# ifdef ECHILD
4245 && errno == ECHILD
4246# endif
4247 )
4248 break;
4249 }
4250 return wait_pid;
4251}
4252
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004253#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004254/*
4255 * Set the environment for a child process.
4256 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004257 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004258set_child_environment(
4259 long rows,
4260 long columns,
4261 char *term,
4262 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004263{
4264# ifdef HAVE_SETENV
4265 char envbuf[50];
4266# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004267 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004268 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004269 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004270 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004271 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004272# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004273 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004274# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004275# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004276 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004277# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004278# endif
4279
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004280# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004281 setenv("TERM", term, 1);
4282 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004283 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004284 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004285 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004286 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004287 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004288 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004289 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004290# ifdef FEAT_TERMINAL
4291 if (is_terminal)
4292 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004293 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004294 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4295 }
4296# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004297# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004298 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004299# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004300# else
4301 /*
4302 * Putenv does not copy the string, it has to remain valid.
4303 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004304 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004305 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004306 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4307 putenv(envbuf_Term);
4308 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004309 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004310 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4311 putenv(envbuf_Lines);
4312 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4313 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004314 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004315 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004316 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004317# ifdef FEAT_TERMINAL
4318 if (is_terminal)
4319 {
4320 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004321 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004322 putenv(envbuf_Version);
4323 }
4324# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004325# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004326 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4327 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4328 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004329# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004330# endif
4331}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004332
4333 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004334set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004335{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004336 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004337}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004338#endif
4339
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004340#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004341/*
4342 * Open a PTY, with FD for the master and slave side.
4343 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004344 * When successful both file descriptors are stored and the allocated pty name
4345 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004346 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004347 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004348open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004349{
4350 char *tty_name;
4351
Bram Moolenaar59386482019-02-10 22:43:46 +01004352 if (name1 != NULL)
4353 *name1 = NULL;
4354 if (name2 != NULL)
4355 *name2 = NULL;
4356
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004357 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004358 if (*pty_master_fd >= 0)
4359 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004360 // Leaving out O_NOCTTY may lead to waitpid() always returning
4361 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4362 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004363#ifdef O_NOCTTY
4364 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4365#else
4366 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4367#endif
4368 if (*pty_slave_fd < 0)
4369 {
4370 close(*pty_master_fd);
4371 *pty_master_fd = -1;
4372 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004373 else
4374 {
4375 if (name1 != NULL)
4376 *name1 = vim_strsave((char_u *)tty_name);
4377 if (name2 != NULL)
4378 *name2 = vim_strsave((char_u *)tty_name);
4379 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004380 }
4381}
4382#endif
4383
Bram Moolenaarfae42832017-08-01 22:24:26 +02004384/*
4385 * Send SIGINT to a child process if "c" is an interrupt character.
4386 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004387 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004388may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4389{
4390# ifdef SIGINT
4391 if (c == Ctrl_C || c == intr_char)
4392 {
4393# ifdef HAVE_SETSID
4394 kill(-pid, SIGINT);
4395# else
4396 kill(0, SIGINT);
4397# endif
4398 if (wpid > 0)
4399 kill(wpid, SIGINT);
4400 }
4401# endif
4402}
4403
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004404#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004405
Bram Moolenaar0f873732019-12-05 20:28:46 +01004406/*
4407 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4408 * pointers, the last one is NULL.
4409 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4410 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004411 int
4412unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004413 char_u *cmd,
4414 char ***argvp,
4415 char_u **sh_tofree,
4416 char_u **shcf_tofree)
4417{
4418 char **argv = NULL;
4419 int argc;
4420
4421 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004422 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004423 return FAIL;
4424
4425 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4426 return FAIL;
4427 *argvp = argv;
4428
4429 if (cmd != NULL)
4430 {
4431 char_u *s;
4432 char_u *p;
4433
4434 if (extra_shell_arg != NULL)
4435 argv[argc++] = (char *)extra_shell_arg;
4436
Bram Moolenaar0f873732019-12-05 20:28:46 +01004437 // Break 'shellcmdflag' into white separated parts. This doesn't
4438 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004439 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004440 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004441 return FAIL;
4442 s = *shcf_tofree;
4443 p = p_shcf;
4444 while (*p != NUL)
4445 {
4446 argv[argc++] = (char *)s;
4447 while (*p && *p != ' ' && *p != TAB)
4448 *s++ = *p++;
4449 *s++ = NUL;
4450 p = skipwhite(p);
4451 }
4452
4453 argv[argc++] = (char *)cmd;
4454 }
4455 argv[argc] = NULL;
4456 return OK;
4457}
4458#endif
4459
4460#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4461/*
4462 * Use a terminal window to run a shell command in.
4463 */
4464 static int
4465mch_call_shell_terminal(
4466 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004467 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004468{
4469 jobopt_T opt;
4470 char **argv = NULL;
4471 char_u *tofree1 = NULL;
4472 char_u *tofree2 = NULL;
4473 int retval = -1;
4474 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004475 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004476 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004477 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004478
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004479 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004480 goto theend;
4481
4482 init_job_options(&opt);
4483 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4484 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004485 if (buf == NULL)
4486 goto theend;
4487
4488 job = term_getjob(buf->b_term);
4489 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004490
Bram Moolenaar0f873732019-12-05 20:28:46 +01004491 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004492 aucmd_prepbuf(&aco, buf);
4493
4494 clear_oparg(&oa);
4495 while (term_use_loop())
4496 {
4497 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4498 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004499 // If terminal_loop() returns OK we got a key that is handled
4500 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004501 if (terminal_loop(TRUE) == OK)
4502 normal_cmd(&oa, TRUE);
4503 }
4504 else
4505 normal_cmd(&oa, TRUE);
4506 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004507 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004508 ch_log(NULL, "system command finished");
4509
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004510 job_unref(job);
4511
Bram Moolenaar0f873732019-12-05 20:28:46 +01004512 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004513 aucmd_restbuf(&aco);
4514
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01004515 // Only require pressing Enter when redrawing, to avoid that system() gets
Bram Moolenaarcb5ed4d2022-07-28 12:54:08 +01004516 // the hit-enter prompt even though it didn't output anything.
4517 if (!RedrawingDisabled)
4518 wait_return(TRUE);
Bram Moolenaar13568252018-03-16 20:46:58 +01004519 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4520
4521theend:
4522 vim_free(argv);
4523 vim_free(tofree1);
4524 vim_free(tofree2);
4525 return retval;
4526}
4527#endif
4528
4529#ifdef USE_SYSTEM
4530/*
4531 * Use system() to start the shell: simple but slow.
4532 */
4533 static int
4534mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004535 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004536 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537{
4538#ifdef VMS
4539 char *ifn = NULL;
4540 char *ofn = NULL;
4541#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004542 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004543 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004544 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545
4546 out_flush();
4547
4548 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004549 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
Bram Moolenaar62b42182010-09-21 22:09:37 +02004551# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004552 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004553 loose_clipboard();
4554# endif
4555
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (cmd == NULL)
4557 x = system((char *)p_sh);
4558 else
4559 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004560# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 if (ofn = strchr((char *)cmd, '>'))
4562 *ofn++ = '\0';
4563 if (ifn = strchr((char *)cmd, '<'))
4564 {
4565 char *p;
4566
4567 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004568 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 if (p)
4570 *p = '\0';
4571 }
4572 if (ofn)
4573 x = vms_sys((char *)cmd, ofn, ifn);
4574 else
4575 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004576# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004577 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004579 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if (newcmd == NULL)
4581 x = 0;
4582 else
4583 {
4584 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4585 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4586 (char *)p_shcf,
4587 (char *)cmd);
4588 x = system((char *)newcmd);
4589 vim_free(newcmd);
4590 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593# ifdef VMS
4594 x = vms_sys_status(x);
4595# endif
4596 if (emsg_silent)
4597 ;
4598 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004599 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 else if (x && !(options & SHELL_SILENT))
4601 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004602 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 msg_outnum((long)x);
4604 msg_putchar('\n');
4605 }
4606
4607 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004608 {
4609 // The shell may have messed with the mode, always set it.
4610 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004611 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004614# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4615 restore_clipboard();
4616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004618}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
Bram Moolenaar0f873732019-12-05 20:28:46 +01004620#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
Bram Moolenaar0f873732019-12-05 20:28:46 +01004622# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4623 // 127, some shells use that already
4624# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
Bram Moolenaar13568252018-03-16 20:46:58 +01004626/*
4627 * Don't use system(), use fork()/exec().
4628 */
4629 static int
4630mch_call_shell_fork(
4631 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004632 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004633{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004634 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004636 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 pid_t wait_pid = 0;
4638# ifdef HAVE_UNION_WAIT
4639 union wait status;
4640# else
4641 int status = -1;
4642# endif
4643 int retval = -1;
4644 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004645 char_u *tofree1 = NULL;
4646 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004648 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004649# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004651# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004652 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 int fd_fromshell[2];
4654 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004655 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656
4657 out_flush();
4658 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004659 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004660 if (tmode == TMODE_RAW)
4661 // The shell may have messed with the mode, always set it later.
4662 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004664 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004665 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004666
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004668 * For the GUI, when writing the output into the buffer and when reading
4669 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4670 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004672 if ((options & (SHELL_READ|SHELL_WRITE))
4673# ifdef FEAT_GUI
4674 || (gui.in_use && show_shell_mess)
4675# endif
4676 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004678# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 /*
4680 * Try to open a master pty.
4681 * If this works, open the slave pty.
4682 * If the slave can't be opened, close the master pty.
4683 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004684 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004685 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 /*
4687 * If not opening a pty or it didn't work, try using pipes.
4688 */
4689 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004690# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
4692 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004693 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
4695 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004696 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
4698 close(fd_toshell[0]);
4699 close(fd_toshell[1]);
4700 }
4701 }
4702 if (pipe_error)
4703 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004704 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 out_flush();
4706 }
4707 }
4708 }
4709
Bram Moolenaar0f873732019-12-05 20:28:46 +01004710 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004712 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004713 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004714 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004715 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004717 UNBLOCK_SIGNALS(&curset);
4718
Bram Moolenaar32526b32019-01-19 17:43:09 +01004719 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004720 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004722 || (gui.in_use && show_shell_mess)
4723# endif
4724 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004726# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004727 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 {
4729 close(pty_master_fd);
4730 close(pty_slave_fd);
4731 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004732 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004733# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
4735 close(fd_toshell[0]);
4736 close(fd_toshell[1]);
4737 close(fd_fromshell[0]);
4738 close(fd_fromshell[1]);
4739 }
4740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004742 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004744 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004745 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746
Bram Moolenaar819524702018-02-27 19:10:00 +01004747# ifdef FEAT_JOB_CHANNEL
4748 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004749 {
4750 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004751 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004752 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004753# endif
4754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 if (!show_shell_mess || (options & SHELL_EXPAND))
4756 {
4757 int fd;
4758
4759 /*
4760 * Don't want to show any message from the shell. Can't just
4761 * close stdout and stderr though, because some systems will
4762 * break if you try to write to them after that, so we must
4763 * use dup() to replace them with something else -- webb
4764 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4765 * waiting for input.
4766 */
4767 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4768 fclose(stdin);
4769 fclose(stdout);
4770 fclose(stderr);
4771
4772 /*
4773 * If any of these open()'s and dup()'s fail, we just continue
4774 * anyway. It's not fatal, and on most systems it will make
4775 * no difference at all. On a few it will cause the execvp()
4776 * to exit with a non-zero status even when the completion
4777 * could be done, which is nothing too serious. If the open()
4778 * or dup() failed we'd just do the same thing ourselves
4779 * anyway -- webb
4780 */
4781 if (fd >= 0)
4782 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004783 vim_ignored = dup(fd); // To replace stdin (fd 0)
4784 vim_ignored = dup(fd); // To replace stdout (fd 1)
4785 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
Bram Moolenaar0f873732019-12-05 20:28:46 +01004787 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 close(fd);
4789 }
4790 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004791 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004793 || gui.in_use
4794# endif
4795 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 {
4797
Bram Moolenaardf177f62005-02-22 08:39:57 +00004798# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004799 // Create our own process group, so that the child and all its
4800 // children can be kill()ed. Don't do this when using pipes,
4801 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004802 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004803 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004804 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004805# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004806 // When doing "!xterm&" and 'shell' is bash: the shell
4807 // will exit and send SIGHUP to all processes in its
4808 // group, killing the just started process. Ignore SIGHUP
4809 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004810 signal(SIGHUP, SIG_IGN);
4811# endif
4812 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004813# endif
4814# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004815 if (pty_slave_fd >= 0)
4816 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004817 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004818 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004819 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004820# ifdef TIOCSCTTY
4821 // Try to become controlling tty (probably doesn't work,
4822 // unless run by root)
4823 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4824# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004825 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004826# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004827 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828
Bram Moolenaara5792f52005-11-23 21:25:05 +00004829 /*
4830 * stderr is only redirected when using the GUI, so that a
4831 * program like gpg can still access the terminal to get a
4832 * passphrase using stderr.
4833 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004834# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 if (pty_master_fd >= 0)
4836 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004837 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838
Bram Moolenaar0f873732019-12-05 20:28:46 +01004839 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004841 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004843 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004844 if (gui.in_use)
4845 {
4846 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004847 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
Bram Moolenaar0f873732019-12-05 20:28:46 +01004850 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
4852 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004855 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 close(fd_toshell[1]);
4857 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004858 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 close(fd_toshell[0]);
4860
Bram Moolenaar0f873732019-12-05 20:28:46 +01004861 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 close(fd_fromshell[0]);
4863 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004864 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 close(fd_fromshell[1]);
4866
Bram Moolenaara5792f52005-11-23 21:25:05 +00004867# ifdef FEAT_GUI
4868 if (gui.in_use)
4869 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004870 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004871 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004872 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004873 }
4874# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
4876 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004877
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 /*
4879 * There is no type cast for the argv, because the type may be
4880 * different on different machines. This may cause a warning
4881 * message with strict compilers, don't worry about it.
4882 * Call _exit() instead of exit() to avoid closing the connection
4883 * to the X server (esp. with GTK, which uses atexit()).
4884 */
4885 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004886 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004888 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 {
4890 /*
4891 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004892 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 */
4894 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004895 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004896 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004897# ifdef FEAT_JOB_CHANNEL
4898 ++dont_check_job_ended;
4899# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 /*
4901 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004902 * This is also used to pipe stdin/stdout to/from the external
4903 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004905 if ((options & (SHELL_READ|SHELL_WRITE))
4906# ifdef FEAT_GUI
4907 || (gui.in_use && show_shell_mess)
4908# endif
4909 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004911# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004913 int buffer_off = 0; // valid bytes in buffer[]
4914 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4915 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 int len;
4917 int p_more_save;
4918 int old_State;
4919 int c;
4920 int toshell_fd;
4921 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004922 garray_T ga;
4923 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004924# ifdef ELAPSED_FUNC
4925 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
Bram Moolenaardf177f62005-02-22 08:39:57 +00004928# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 if (pty_master_fd >= 0)
4930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 fromshell_fd = pty_master_fd;
4932 toshell_fd = dup(pty_master_fd);
4933 }
4934 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004935# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
4937 close(fd_toshell[0]);
4938 close(fd_fromshell[1]);
4939 toshell_fd = fd_toshell[1];
4940 fromshell_fd = fd_fromshell[0];
4941 }
4942
4943 /*
4944 * Write to the child if there are typed characters.
4945 * Read from the child if there are characters available.
4946 * Repeat the reading a few times if more characters are
4947 * available. Need to check for typed keys now and then, but
4948 * not too often (delays when no chars are available).
4949 * This loop is quit if no characters can be read from the pty
4950 * (WaitForChar detected special condition), or there are no
4951 * characters available and the child has exited.
4952 * Only check if the child has exited when there is no more
4953 * output. The child may exit before all the output has
4954 * been printed.
4955 *
4956 * Currently this busy loops!
4957 * This can probably dead-lock when the write blocks!
4958 */
4959 p_more_save = p_more;
4960 p_more = FALSE;
4961 old_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004962 State = MODE_EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004964 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004965 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004966 // Fork a process that will write the lines to the
4967 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004968 if ((wpid = fork()) == -1)
4969 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004970 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004971 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004972 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004973 {
4974 linenr_T lnum = curbuf->b_op_start.lnum;
4975 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004976 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004977 size_t l;
4978
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004979 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004980 for (;;)
4981 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004982 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004983 if (l == 0)
4984 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004985 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004986 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004987 len = write(toshell_fd, "", (size_t)1);
4988 else
4989 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004990 char_u *s = vim_strchr(lp + written, NL);
4991
Bram Moolenaar89d40322006-08-29 15:30:07 +00004992 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004993 s == NULL ? l
4994 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004995 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004996 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004997 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004998 // Finished a line, add a NL, unless this line
4999 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005000 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005001 || (!curbuf->b_p_bin
5002 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005003 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaar88456cd2022-11-18 22:14:09 +00005004 && (lnum != curbuf->b_ml.ml_line_count
Bram Moolenaardf177f62005-02-22 08:39:57 +00005005 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005006 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005007 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005008 ++lnum;
5009 if (lnum > curbuf->b_op_end.lnum)
5010 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005011 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005012 close(toshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005013 break;
5014 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005015 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005016 written = 0;
5017 }
5018 else if (len > 0)
5019 written += len;
5020 }
5021 _exit(0);
5022 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005023 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005024 {
5025 close(toshell_fd);
5026 toshell_fd = -1;
5027 }
5028 }
5029
5030 if (options & SHELL_READ)
5031 ga_init2(&ga, 1, BUFLEN);
5032
5033 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005034# ifdef ELAPSED_FUNC
5035 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005036# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 for (;;)
5038 {
5039 /*
5040 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005041 * if there are any.
5042 * Don't do this if we are expanding wild cards (would eat
5043 * typeahead).
5044 * Don't do this when filtering and terminal is in cooked
5045 * mode, the shell command will handle the I/O. Avoids
5046 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005047 * Don't get characters when the child has already
5048 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005049 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005050 * while (noread_cnt > 4), avoids that ":r !ls" eats
5051 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 */
5053 len = 0;
5054 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005055 && ((options &
5056 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5057 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005058# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005059 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005060# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005061 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005062 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005063 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005065 if (ta_len == 0)
5066 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005067 // Get extra characters when we don't have any.
5068 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005069 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005070# ifdef ELAPSED_FUNC
5071 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005072# endif
5073 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5074 }
5075 if (ta_len > 0 || len > 0)
5076 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 /*
5078 * For pipes:
5079 * Check for CTRL-C: send interrupt signal to child.
5080 * Check for CTRL-D: EOF, close pipe to child.
5081 */
5082 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5083 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 /*
5085 * Send SIGINT to the child's group or all
5086 * processes in our group.
5087 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005088 may_send_sigint(ta_buf[ta_len], pid, wpid);
5089
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 if (pty_master_fd < 0 && toshell_fd >= 0
5091 && ta_buf[ta_len] == Ctrl_D)
5092 {
5093 close(toshell_fd);
5094 toshell_fd = -1;
5095 }
5096 }
5097
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01005098 // Remove Vim-specific codes from the input.
5099 len = term_replace_keycodes(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100
5101 /*
5102 * For pipes: echo the typed characters.
5103 * For a pty this does not seem to work.
5104 */
5105 if (pty_master_fd < 0)
5106 {
5107 for (i = ta_len; i < ta_len + len; ++i)
5108 {
5109 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5110 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 else if (has_mbyte)
5112 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005113 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
5115 msg_outtrans_len(ta_buf + i, l);
5116 i += l - 1;
5117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 else
5119 msg_outtrans_len(ta_buf + i, 1);
5120 }
5121 windgoto(msg_row, msg_col);
5122 out_flush();
5123 }
5124
5125 ta_len += len;
5126
5127 /*
5128 * Write the characters to the child, unless EOF has
5129 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005130 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005131 * When writing buffer lines, drop the typed
5132 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005134 if (options & SHELL_WRITE)
5135 ta_len = 0;
5136 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
5138 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5139 if (len > 0)
5140 {
5141 ta_len -= len;
5142 mch_memmove(ta_buf, ta_buf + len, ta_len);
5143 }
5144 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147
Bram Moolenaardf177f62005-02-22 08:39:57 +00005148 if (got_int)
5149 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005150 // CTRL-C sends a signal to the child, we ignore it
5151 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005152# ifdef HAVE_SETSID
5153 kill(-pid, SIGINT);
5154# else
5155 kill(0, SIGINT);
5156# endif
5157 if (wpid > 0)
5158 kill(wpid, SIGINT);
5159 got_int = FALSE;
5160 }
5161
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 /*
5163 * Check if the child has any characters to be printed.
5164 * Read them and write them to our window. Repeat this as
5165 * long as there is something to do, avoid the 10ms wait
5166 * for mch_inchar(), or sending typeahead characters to
5167 * the external process.
5168 * TODO: This should handle escape sequences, compatible
5169 * to some terminal (vt52?).
5170 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005171 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005172 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005174 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005177 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005179
5180 noread_cnt = 0;
5181 if (options & SHELL_READ)
5182 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005183 // Do NUL -> NL translation, append NL separated
5184 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005185 for (i = 0; i < len; ++i)
5186 {
5187 if (buffer[i] == NL)
5188 append_ga_line(&ga);
5189 else if (buffer[i] == NUL)
5190 ga_append(&ga, NL);
5191 else
5192 ga_append(&ga, buffer[i]);
5193 }
5194 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005195 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
5197 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005198 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199
Bram Moolenaardf177f62005-02-22 08:39:57 +00005200 len += buffer_off;
5201 buffer[len] = NUL;
5202
Bram Moolenaar0f873732019-12-05 20:28:46 +01005203 // Check if the last character in buffer[] is
5204 // incomplete, keep these bytes for the next
5205 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 for (p = buffer; p < buffer + len; p += l)
5207 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005208 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005210 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 else if (MB_BYTE2LEN(*p) != l)
5212 break;
5213 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005214 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005216 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (len >= 12)
5218 ++p;
5219 else
5220 {
5221 buffer_off = len;
5222 continue;
5223 }
5224 }
5225 c = *p;
5226 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005227 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (p < buffer + len)
5229 {
5230 *p = c;
5231 buffer_off = (buffer + len) - p;
5232 mch_memmove(buffer, p, buffer_off);
5233 continue;
5234 }
5235 buffer_off = 0;
5236 }
5237 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {
5239 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005240 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
5242
5243 windgoto(msg_row, msg_col);
5244 cursor_on();
5245 out_flush();
5246 if (got_int)
5247 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005248
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005249# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005250 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005251 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005252 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005253
Bram Moolenaar0f873732019-12-05 20:28:46 +01005254 // Avoid that we keep looping here without
5255 // checking for a CTRL-C for a long time. Don't
5256 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005257 if (msec > 2000)
5258 {
5259 noread_cnt = 5;
5260 break;
5261 }
5262 }
5263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 }
5265
Bram Moolenaar0f873732019-12-05 20:28:46 +01005266 // If we already detected the child has finished, continue
5267 // reading output for a short while. Some text may be
5268 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005269 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005270 {
5271 if (noread_cnt < 5)
5272 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005273 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005274 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005275
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 /*
5277 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005278 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005280# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005281 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005282# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5286 || (wait_pid == pid && WIFEXITED(status)))
5287 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005288 // Don't break the loop yet, try reading more
5289 // characters from "fromshell_fd" first. When using
5290 // pipes there might still be something to read and
5291 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005294 else
5295 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005296
Bram Moolenaar95a51352013-03-21 22:53:50 +01005297# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005298 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005299 clip_update();
5300# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 }
5302finished:
5303 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005304 if (options & SHELL_READ)
5305 {
5306 if (ga.ga_len > 0)
5307 {
5308 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005309 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005310 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005311 }
5312 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005313 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005314 ga_clear(&ga);
5315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 /*
5318 * Give all typeahead that wasn't used back to ui_inchar().
5319 */
5320 if (ta_len)
5321 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 State = old_State;
5323 if (toshell_fd >= 0)
5324 close(toshell_fd);
5325 close(fromshell_fd);
5326 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005327# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005328 else
5329 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005330 long delay_msec = 1;
5331
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005332 if (tmode == TMODE_RAW)
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005333 // Possibly disables modifyOtherKeys, so that the system
5334 // can recognize CTRL-C.
5335 out_str_t_TE();
Bram Moolenaar0981c872020-08-23 14:28:37 +02005336
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005337 /*
5338 * Similar to the loop above, but only handle X events, no
5339 * I/O.
5340 */
5341 for (;;)
5342 {
5343 if (got_int)
5344 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005345 // CTRL-C sends a signal to the child, we ignore it
5346 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005347# ifdef HAVE_SETSID
5348 kill(-pid, SIGINT);
5349# else
5350 kill(0, SIGINT);
5351# endif
5352 got_int = FALSE;
5353 }
5354# ifdef __NeXT__
5355 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5356# else
5357 wait_pid = waitpid(pid, &status, WNOHANG);
5358# endif
5359 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5360 || (wait_pid == pid && WIFEXITED(status)))
5361 {
5362 wait_pid = pid;
5363 break;
5364 }
5365
Bram Moolenaar0f873732019-12-05 20:28:46 +01005366 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005367 clip_update();
5368
Bram Moolenaar0f873732019-12-05 20:28:46 +01005369 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005370 // less time, gradually wait longer.
5371 mch_delay(delay_msec,
5372 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005373 if (++delay_msec > 10)
5374 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005375 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005376
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005377 if (tmode == TMODE_RAW)
5378 // possibly enables modifyOtherKeys again
5379 out_str(T_CTI);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005380 }
5381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383 /*
5384 * Wait until our child has exited.
5385 * Ignore wait() returning pids of other children and returning
5386 * because of some signal like SIGWINCH.
5387 * Don't wait if wait_pid was already set above, indicating the
5388 * child already exited.
5389 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005390 if (wait_pid != pid)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01005391 (void)wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392
Bram Moolenaar624891f2010-10-13 16:22:09 +02005393# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005394 // Close slave side of pty. Only do this after the child has
5395 // exited, otherwise the child may hang when it tries to write on
5396 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005397 if (pty_master_fd >= 0)
5398 close(pty_slave_fd);
5399# endif
5400
Bram Moolenaar0f873732019-12-05 20:28:46 +01005401 // Make sure the child that writes to the external program is
5402 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005403 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005404 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005405 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005406 wait4pid(wpid, NULL);
5407 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005408
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005409# ifdef FEAT_JOB_CHANNEL
5410 --dont_check_job_ended;
5411# endif
5412
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 /*
5414 * Set to raw mode right now, otherwise a CTRL-C after
5415 * catch_signals() will kill Vim.
5416 */
5417 if (tmode == TMODE_RAW)
5418 settmode(TMODE_RAW);
5419 did_settmode = TRUE;
5420 set_signals();
5421
5422 if (WIFEXITED(status))
5423 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005424 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005426 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 {
5428 if (retval == EXEC_FAILED)
5429 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005430 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 msg_outtrans(p_sh);
5432 msg_putchar('\n');
5433 }
5434 else if (!(options & SHELL_SILENT))
5435 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005436 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 msg_outnum((long)retval);
5438 msg_putchar('\n');
5439 }
5440 }
5441 }
5442 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005443 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
5445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446
5447error:
5448 if (!did_settmode)
5449 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005450 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005452 vim_free(argv);
5453 vim_free(tofree1);
5454 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455
5456 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005457}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005458#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005459
5460 int
5461mch_call_shell(
5462 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005463 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005464{
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01005465#ifdef FEAT_JOB_CHANNEL
5466 ch_log(NULL, "executing shell command: %s", cmd);
5467#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005468#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar524c8532022-09-27 15:48:20 +01005469 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL
5470 && (options & SHELL_SILENT) == 0)
Bram Moolenaar13568252018-03-16 20:46:58 +01005471 return mch_call_shell_terminal(cmd, options);
5472#endif
5473#ifdef USE_SYSTEM
5474 return mch_call_shell_system(cmd, options);
5475#else
5476 return mch_call_shell_fork(cmd, options);
5477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478}
5479
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005480#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005481 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005482mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005483{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005484 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005485 int fd_in[2] = {-1, -1}; // for stdin
5486 int fd_out[2] = {-1, -1}; // for stdout
5487 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005488 int pty_master_fd = -1;
5489 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005490 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005491 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5492 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5493 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005494 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005495 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5496 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005497 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005498 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005499 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005500
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005501 if (use_out_for_err && use_null_for_out)
5502 use_null_for_err = TRUE;
5503
Bram Moolenaar0f873732019-12-05 20:28:46 +01005504 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005505 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005506
Bram Moolenaarb2412082017-08-20 18:09:14 +02005507 if (options->jo_pty
5508 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005509 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005510 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005511 open_pty(&pty_master_fd, &pty_slave_fd,
5512 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005513
Bram Moolenaar0f873732019-12-05 20:28:46 +01005514 // TODO: without the channel feature connect the child to /dev/null?
5515 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005516 if (use_file_for_in)
5517 {
5518 char_u *fname = options->jo_io_name[PART_IN];
5519
5520 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5521 if (fd_in[0] < 0)
5522 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005523 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005524 goto failed;
5525 }
5526 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005527 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005528 // When writing buffer lines to the input don't use the pty, so that
5529 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005530 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5531 && pipe(fd_in) < 0)
5532 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005533
5534 if (use_file_for_out)
5535 {
5536 char_u *fname = options->jo_io_name[PART_OUT];
5537
5538 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5539 if (fd_out[1] < 0)
5540 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005541 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005542 goto failed;
5543 }
5544 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005545 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005546 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005547
5548 if (use_file_for_err)
5549 {
5550 char_u *fname = options->jo_io_name[PART_ERR];
5551
5552 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5553 if (fd_err[1] < 0)
5554 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005555 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005556 goto failed;
5557 }
5558 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005559 else if (!use_out_for_err && !use_null_for_err
5560 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005561 goto failed;
5562
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005563 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5564 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005565 if (options->jo_set & JO_CHANNEL)
5566 {
5567 channel = options->jo_channel;
5568 if (channel != NULL)
5569 ++channel->ch_refcount;
5570 }
5571 else
5572 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005573 if (channel == NULL)
5574 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005575 if (job->jv_tty_out != NULL)
5576 ch_log(channel, "using pty %s on fd %d",
5577 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005578 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005579
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005580 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005581 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005582 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005583 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005584 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005585 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005586 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005587 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005588 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005589 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005590 int null_fd = -1;
5591 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005592
Bram Moolenaar0f873732019-12-05 20:28:46 +01005593 // child
5594 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005595 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005596
Bram Moolenaar819524702018-02-27 19:10:00 +01005597# ifdef FEAT_JOB_CHANNEL
5598 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005599 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005600 ch_logfile((char_u *)"", (char_u *)"");
5601# endif
5602
Bram Moolenaar835dc632016-02-07 14:27:38 +01005603# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005604 // Create our own process group, so that the child and all its
5605 // children can be kill()ed. Don't do this when using pipes,
5606 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005607 (void)setsid();
5608# endif
5609
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005610# ifdef FEAT_TERMINAL
5611 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005612 {
5613 char *term = (char *)T_NAME;
5614
5615#ifdef FEAT_GUI
5616 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005617 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005618 term = getenv("TERM");
5619#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005620 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005621 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005622 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005623 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005624 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005625 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005626 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005627 else if (t_colors > 16)
5628 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005629 else
5630 term = "xterm";
5631 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005632 set_child_environment(
5633 (long)options->jo_term_rows,
5634 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005635 term,
5636 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005637 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005638 else
5639# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005640 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005641
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005642 if (options->jo_env != NULL)
5643 {
5644 dict_T *dict = options->jo_env;
5645 hashitem_T *hi;
5646 int todo = (int)dict->dv_hashtab.ht_used;
5647
5648 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5649 if (!HASHITEM_EMPTY(hi))
5650 {
5651 typval_T *item = &dict_lookup(hi)->di_tv;
5652
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005653 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005654 --todo;
5655 }
5656 }
5657
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005658 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005659 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005660 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005661 if (null_fd < 0)
5662 {
5663 perror("opening /dev/null failed");
5664 _exit(OPEN_NULL_FAILED);
5665 }
5666 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005667
Bram Moolenaar223896d2017-08-02 22:33:28 +02005668 if (pty_slave_fd >= 0)
5669 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005670 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005671 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005672# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005673 // Try to become controlling tty (probably doesn't work,
5674 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005675 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5676# endif
5677 }
5678
Bram Moolenaar0f873732019-12-05 20:28:46 +01005679 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005680 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005681 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005682 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005683 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005684 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005685 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005686 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005687
Bram Moolenaar0f873732019-12-05 20:28:46 +01005688 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005689 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005690 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005691 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005692 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005693 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005694 }
5695 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005696 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005697 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005698 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005699 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005700 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005701
Bram Moolenaar0f873732019-12-05 20:28:46 +01005702 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005703 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005704 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005705 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005706 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005707 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005708 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005709 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005710
5711 if (fd_in[0] >= 0)
5712 close(fd_in[0]);
5713 if (fd_in[1] >= 0)
5714 close(fd_in[1]);
5715 if (fd_out[0] >= 0)
5716 close(fd_out[0]);
5717 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005718 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005719 if (fd_err[0] >= 0)
5720 close(fd_err[0]);
5721 if (fd_err[1] >= 0)
5722 close(fd_err[1]);
5723 if (pty_master_fd >= 0)
5724 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005725 close(pty_master_fd); // not used in the child
5726 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005727 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005728
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005729 if (null_fd >= 0)
5730 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005731
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005732 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5733 _exit(EXEC_FAILED);
5734
Bram Moolenaar0f873732019-12-05 20:28:46 +01005735 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005736 execvp(argv[0], argv);
5737
Bram Moolenaar4694a172016-04-21 14:05:23 +02005738 if (stderr_works)
5739 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005740# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005741 // calling free_all_mem() here causes problems. Ignore valgrind
5742 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005743# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005744 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005745 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005746
Bram Moolenaar0f873732019-12-05 20:28:46 +01005747 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005748 UNBLOCK_SIGNALS(&curset);
5749
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005750 job->jv_pid = pid;
5751 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005752 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005753
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005754 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005755 close(pty_slave_fd); // not used in the parent
5756 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005757 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005758 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005759 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005760 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005761 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005762 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005763 if (channel != NULL)
5764 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005765 int in_fd = INVALID_FD;
5766 int out_fd = INVALID_FD;
5767 int err_fd = INVALID_FD;
5768
5769 if (!(use_file_for_in || use_null_for_in))
5770 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5771
5772 if (!(use_file_for_out || use_null_for_out))
5773 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5774
5775 // When using pty_master_fd only set it for stdout, do not duplicate
5776 // it for stderr, it only needs to be read once.
5777 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5778 {
5779 if (fd_err[0] >= 0)
5780 err_fd = fd_err[0];
5781 else if (out_fd != pty_master_fd)
5782 err_fd = pty_master_fd;
5783 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005784
5785 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005786 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005787 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005788 else
5789 {
5790 if (fd_in[1] >= 0)
5791 close(fd_in[1]);
5792 if (fd_out[0] >= 0)
5793 close(fd_out[0]);
5794 if (fd_err[0] >= 0)
5795 close(fd_err[0]);
5796 if (pty_master_fd >= 0)
5797 close(pty_master_fd);
5798 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005799
Bram Moolenaar0f873732019-12-05 20:28:46 +01005800 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005801 return;
5802
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005803failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005804 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005805 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005806 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005807 if (fd_in[1] >= 0)
5808 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005809 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005810 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005811 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005812 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005813 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005814 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005815 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005816 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005817 if (pty_master_fd >= 0)
5818 close(pty_master_fd);
5819 if (pty_slave_fd >= 0)
5820 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005821}
5822
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005823 static char_u *
5824get_signal_name(int sig)
5825{
5826 int i;
5827 char_u numbuf[NUMBUFLEN];
5828
5829 if (sig == SIGKILL)
5830 return vim_strsave((char_u *)"kill");
5831
5832 for (i = 0; signal_info[i].sig != -1; i++)
5833 if (sig == signal_info[i].sig)
5834 return strlow_save((char_u *)signal_info[i].name);
5835
5836 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5837 return vim_strsave(numbuf);
5838}
5839
Bram Moolenaar835dc632016-02-07 14:27:38 +01005840 char *
5841mch_job_status(job_T *job)
5842{
5843# ifdef HAVE_UNION_WAIT
5844 union wait status;
5845# else
5846 int status = -1;
5847# endif
5848 pid_t wait_pid = 0;
5849
5850# ifdef __NeXT__
5851 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5852# else
5853 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5854# endif
5855 if (wait_pid == -1)
5856 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005857 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005858 if (job->jv_status < JOB_ENDED)
5859 ch_log(job->jv_channel, "Job no longer exists: %s",
5860 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005861 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005862 }
5863 if (wait_pid == 0)
5864 return "run";
5865 if (WIFEXITED(status))
5866 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005867 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005868 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005869 if (job->jv_status < JOB_ENDED)
5870 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005871 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005872 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005873 if (WIFSIGNALED(status))
5874 {
5875 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005876 job->jv_termsig = get_signal_name(WTERMSIG(status));
5877 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5878 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5879 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005880 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005881 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005882 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005883
5884return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005885 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005886 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005887 return "dead";
5888}
5889
5890 job_T *
5891mch_detect_ended_job(job_T *job_list)
5892{
5893# ifdef HAVE_UNION_WAIT
5894 union wait status;
5895# else
5896 int status = -1;
5897# endif
5898 pid_t wait_pid = 0;
5899 job_T *job;
5900
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005901# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005902 // Do not do this when waiting for a shell command to finish, we would get
5903 // the exit value here (and discard it), the exit value obtained there
5904 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005905 if (dont_check_job_ended > 0)
5906 return NULL;
5907# endif
5908
Bram Moolenaar97792de2016-10-15 18:36:49 +02005909# ifdef __NeXT__
5910 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5911# else
5912 wait_pid = waitpid(-1, &status, WNOHANG);
5913# endif
5914 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005915 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005916 return NULL;
5917 for (job = job_list; job != NULL; job = job->jv_next)
5918 {
5919 if (job->jv_pid == wait_pid)
5920 {
5921 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005922 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005923 job->jv_exitval = WEXITSTATUS(status);
5924 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005925 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005926 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005927 job->jv_termsig = get_signal_name(WTERMSIG(status));
5928 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005929 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005930 {
5931 ch_log(job->jv_channel, "Job ended");
5932 job->jv_status = JOB_ENDED;
5933 }
5934 return job;
5935 }
5936 }
5937 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005938}
5939
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005940/*
5941 * Send a (deadly) signal to "job".
5942 * Return FAIL if "how" is not a valid name.
5943 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005944 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005945mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005946{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005947 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005948
Bram Moolenaar923d9262016-02-25 20:56:01 +01005949 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005950 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005951 else if (STRCMP(how, "hup") == 0)
5952 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005953 else if (STRCMP(how, "quit") == 0)
5954 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005955 else if (STRCMP(how, "int") == 0)
5956 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005957 else if (STRCMP(how, "kill") == 0)
5958 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005959#ifdef SIGWINCH
5960 else if (STRCMP(how, "winch") == 0)
5961 sig = SIGWINCH;
5962#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005963 else if (isdigit(*how))
5964 sig = atoi((char *)how);
5965 else
5966 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005967
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005968 // Never kill ourselves!
5969 if (job->jv_pid != 0)
5970 {
5971 // TODO: have an option to only kill the process, not the group?
5972 kill(-job->jv_pid, sig);
5973 kill(job->jv_pid, sig);
5974 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005975
Bram Moolenaar835dc632016-02-07 14:27:38 +01005976 return OK;
5977}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005978
5979/*
5980 * Clear the data related to "job".
5981 */
5982 void
5983mch_clear_job(job_T *job)
5984{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005985 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005986# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005987 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005988# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005989 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005990# endif
5991}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005992#endif
5993
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005994#if defined(FEAT_TERMINAL) || defined(PROTO)
5995 int
5996mch_create_pty_channel(job_T *job, jobopt_T *options)
5997{
5998 int pty_master_fd = -1;
5999 int pty_slave_fd = -1;
6000 channel_T *channel;
6001
Bram Moolenaar59386482019-02-10 22:43:46 +01006002 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02006003 if (pty_master_fd < 0 || pty_slave_fd < 0)
6004 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006005 close(pty_slave_fd);
6006
6007 channel = add_channel();
6008 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006009 {
6010 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006011 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006012 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006013 if (job->jv_tty_out != NULL)
6014 ch_log(channel, "using pty %s on fd %d",
6015 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006016 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006017 channel->ch_keep_open = TRUE;
6018
Bram Moolenaar0f873732019-12-05 20:28:46 +01006019 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6020 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006021 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006022 channel_set_job(channel, job, options);
6023 return OK;
6024}
6025#endif
6026
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027/*
6028 * Check for CTRL-C typed by reading all available characters.
6029 * In cooked mode we should get SIGINT, no need to check.
6030 */
6031 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006032mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006034 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006035 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 fill_input_buf(FALSE);
6037}
6038
6039/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006040 * Wait "msec" msec until a character is available from the mouse, keyboard,
6041 * from inbuf[].
6042 * "msec" == -1 will block forever.
6043 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006044 * When "ignore_input" is TRUE even check for pending input when input is
6045 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006046 * "interrupted" (if not NULL) is set to TRUE when no character is available
6047 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006048 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006049 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 */
6051 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006052WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006054#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006055 return ui_wait_for_chars_or_timer(
6056 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006057#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006058 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006059#endif
6060}
6061
6062/*
6063 * Wait "msec" msec until a character is available from the mouse or keyboard
6064 * or from inbuf[].
6065 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006066 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006067 * "interrupted" (if not NULL) is set to TRUE when no character is available
6068 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006069 * When a GUI is being used, this will never get called -- webb
6070 */
6071 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006072WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006073{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074#ifdef FEAT_MOUSE_GPM
6075 int gpm_process_wanted;
6076#endif
6077#ifdef FEAT_XCLIPBOARD
6078 int rest;
6079#endif
6080 int avail;
6081
Bram Moolenaar0f873732019-12-05 20:28:46 +01006082 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 return 1;
6084
6085#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006086 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 if (WantQueryMouse)
6088 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006089 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006090 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006091 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 }
6093#endif
6094
6095 /*
6096 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6097 * events. This is a bit complicated, because they might both be defined.
6098 */
6099#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6100# ifdef FEAT_XCLIPBOARD
6101 rest = 0;
6102 if (do_xterm_trace())
6103 rest = msec;
6104# endif
6105 do
6106 {
6107# ifdef FEAT_XCLIPBOARD
6108 if (rest != 0)
6109 {
6110 msec = XT_TRACE_DELAY;
6111 if (rest >= 0 && rest < XT_TRACE_DELAY)
6112 msec = rest;
6113 if (rest >= 0)
6114 rest -= msec;
6115 }
6116# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01006117# ifdef FEAT_SOUND_MACOSX
6118 // Invoke any pending sound callbacks.
6119 process_cfrunloop();
6120# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006121# ifdef FEAT_SOUND_CANBERRA
6122 // Invoke any pending sound callbacks.
6123 if (has_sound_callback_in_queue())
6124 invoke_sound_callback();
6125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126# ifdef FEAT_MOUSE_GPM
6127 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006128 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006129 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006130 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006132 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006136 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 return 1;
6138# ifdef FEAT_XCLIPBOARD
6139 if (rest == 0 || !do_xterm_trace())
6140# endif
6141 break;
6142 }
6143 }
6144 while (FALSE
6145# ifdef FEAT_MOUSE_GPM
6146 || (gpm_process_wanted && mch_gpm_process() == 0)
6147# endif
6148# ifdef FEAT_XCLIPBOARD
6149 || (!avail && rest != 0)
6150# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006151 )
6152 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
6154#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006155 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156#endif
6157 return avail;
6158}
6159
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006160#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161/*
6162 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006163 * "msec" == 0 will check for characters once.
6164 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006167 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006168 * "interrupted" (if not NULL) is set to TRUE when no character is available
6169 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006171 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006172RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173{
6174 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006175 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006176#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 static int busy = FALSE;
6178
Bram Moolenaar0f873732019-12-05 20:28:46 +01006179 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180# define MAY_LOOP
6181
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006182# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006183 // Remember at what time we started, so that we know how much longer we
6184 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006185 long start_msec = msec;
6186 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006188 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006189 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190# endif
6191
Bram Moolenaar0f873732019-12-05 20:28:46 +01006192 // Handle being called recursively. This may happen for the session
6193 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 if (busy)
6195 return 0;
6196#endif
6197
6198#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006199 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200#endif
6201 {
6202#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006203 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006204# ifdef FEAT_MZSCHEME
6205 int mzquantum_used = FALSE;
6206# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207#endif
6208#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006209 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006210 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 int nfd;
6212# ifdef FEAT_XCLIPBOARD
6213 int xterm_idx = -1;
6214# endif
6215# ifdef FEAT_MOUSE_GPM
6216 int gpm_idx = -1;
6217# endif
6218# ifdef USE_XSMP
6219 int xsmp_idx = -1;
6220# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006221 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006223# ifdef FEAT_MZSCHEME
6224 mzvim_check_threads();
6225 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6226 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006227 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006228 mzquantum_used = TRUE;
6229 }
6230# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 fds[0].fd = fd;
6232 fds[0].events = POLLIN;
6233 nfd = 1;
6234
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006236 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 if (xterm_Shell != (Widget)0)
6238 {
6239 xterm_idx = nfd;
6240 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6241 fds[nfd].events = POLLIN;
6242 nfd++;
6243 }
6244# endif
6245# ifdef FEAT_MOUSE_GPM
6246 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6247 {
6248 gpm_idx = nfd;
6249 fds[nfd].fd = gpm_fd;
6250 fds[nfd].events = POLLIN;
6251 nfd++;
6252 }
6253# endif
6254# ifdef USE_XSMP
6255 if (xsmp_icefd != -1)
6256 {
6257 xsmp_idx = nfd;
6258 fds[nfd].fd = xsmp_icefd;
6259 fds[nfd].events = POLLIN;
6260 nfd++;
6261 }
6262# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006263#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006264 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006265#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006266 if (interrupted != NULL)
6267 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006269 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006270
6271 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006272 if (result == 0 && interrupted != NULL && ret > 0)
6273 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006274
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006275# ifdef FEAT_MZSCHEME
6276 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006277 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006278 finished = FALSE;
6279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281# ifdef FEAT_XCLIPBOARD
6282 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6283 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006284 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006286 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 finished = FALSE;
6288 }
6289# endif
6290# ifdef FEAT_MOUSE_GPM
6291 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293# endif
6294# ifdef USE_XSMP
6295 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6296 {
6297 if (fds[xsmp_idx].revents & POLLIN)
6298 {
6299 busy = TRUE;
6300 xsmp_handle_requests();
6301 busy = FALSE;
6302 }
6303 else if (fds[xsmp_idx].revents & POLLHUP)
6304 {
6305 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006306 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 xsmp_close();
6308 }
6309 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006310 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 }
6312# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006313#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006314 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006315 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006316 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318
Bram Moolenaar0f873732019-12-05 20:28:46 +01006319#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320
6321 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006322 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006323 // These are static because they can take 8 Kbyte each and cause the
6324 // signal stack to run out with -O3.
6325 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006327 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006329# ifdef FEAT_MZSCHEME
6330 mzvim_check_threads();
6331 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6332 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006333 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006334 mzquantum_used = TRUE;
6335 }
6336# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006338 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006340 tv.tv_sec = towait / 1000;
6341 tv.tv_usec = (towait % 1000) * (1000000/1000);
6342 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006344 else
6345 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
6347 /*
6348 * Select on ready for reading and exceptional condition (end of file).
6349 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006350select_eintr:
6351 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006352 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 FD_ZERO(&efds);
6354 FD_SET(fd, &rfds);
K.Takatab247e062022-02-07 10:45:23 +00006355# ifndef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006356 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 FD_SET(fd, &efds);
6358# endif
6359 maxfd = fd;
6360
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006362 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 if (xterm_Shell != (Widget)0)
6364 {
6365 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6366 if (maxfd < ConnectionNumber(xterm_dpy))
6367 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006368
Bram Moolenaar0f873732019-12-05 20:28:46 +01006369 // An event may have already been read but not handled. In
6370 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006371 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 }
6373# endif
6374# ifdef FEAT_MOUSE_GPM
6375 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6376 {
6377 FD_SET(gpm_fd, &rfds);
6378 FD_SET(gpm_fd, &efds);
6379 if (maxfd < gpm_fd)
6380 maxfd = gpm_fd;
6381 }
6382# endif
6383# ifdef USE_XSMP
6384 if (xsmp_icefd != -1)
6385 {
6386 FD_SET(xsmp_icefd, &rfds);
6387 FD_SET(xsmp_icefd, &efds);
6388 if (maxfd < xsmp_icefd)
6389 maxfd = xsmp_icefd;
6390 }
6391# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006392# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006393 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006394# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006395 if (interrupted != NULL)
6396 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397
Bram Moolenaar643b6142018-09-12 20:29:09 +02006398 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6399 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006400 result = ret > 0 && FD_ISSET(fd, &rfds);
6401 if (result)
6402 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006403 else if (interrupted != NULL && ret > 0)
6404 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006405
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006406# ifdef EINTR
6407 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006408 {
dbivolaruab16ad32021-12-29 19:41:47 +00006409 // Check whether the EINTR is caused by SIGTSTP
6410 if (got_tstp && !in_mch_suspend)
6411 {
6412 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006413
dbivolaruab16ad32021-12-29 19:41:47 +00006414 ea.forceit = TRUE;
6415 ex_stop(&ea);
6416 got_tstp = FALSE;
6417 }
6418
Bram Moolenaar0f873732019-12-05 20:28:46 +01006419 // Check whether window has been resized, EINTR may be caused by
6420 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006421 if (do_resize)
6422 handle_resize();
6423
Bram Moolenaar0f873732019-12-05 20:28:46 +01006424 // Interrupted by a signal, need to try again. We ignore msec
6425 // here, because we do want to check even after a timeout if
6426 // characters are available. Needed for reading output of an
6427 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006428 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006429 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006430# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006431# ifdef __TANDEM
6432 if (ret == -1 && errno == ENOTSUP)
6433 {
6434 FD_ZERO(&rfds);
6435 FD_ZERO(&efds);
6436 ret = 0;
6437 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006438# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006439# ifdef FEAT_MZSCHEME
6440 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006441 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006442 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443# endif
6444
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445# ifdef FEAT_XCLIPBOARD
6446 if (ret > 0 && xterm_Shell != (Widget)0
6447 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6448 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006449 xterm_update(); // Maybe we should hand out clipboard
6450 // continue looping when we only got the X event and the input
6451 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 if (--ret == 0 && !input_available())
6453 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006454 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 finished = FALSE;
6456 }
6457 }
6458# endif
6459# ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00006460 if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 {
6462 if (FD_ISSET(gpm_fd, &efds))
6463 gpm_close();
6464 else if (FD_ISSET(gpm_fd, &rfds))
6465 *check_for_gpm = 1;
6466 }
6467# endif
6468# ifdef USE_XSMP
6469 if (ret > 0 && xsmp_icefd != -1)
6470 {
6471 if (FD_ISSET(xsmp_icefd, &efds))
6472 {
6473 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006474 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 xsmp_close();
6476 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006477 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 }
6479 else if (FD_ISSET(xsmp_icefd, &rfds))
6480 {
6481 busy = TRUE;
6482 xsmp_handle_requests();
6483 busy = FALSE;
6484 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006485 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 }
6487 }
6488# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006489#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006490 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006491 if (ret >= 0)
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01006492 (void)channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494
Bram Moolenaar0f873732019-12-05 20:28:46 +01006495#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496
6497#ifdef MAY_LOOP
6498 if (finished || msec == 0)
6499 break;
6500
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006501# ifdef FEAT_CLIENTSERVER
6502 if (server_waiting())
6503 break;
6504# endif
6505
Bram Moolenaar0f873732019-12-05 20:28:46 +01006506 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 if (msec > 0)
6508 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006509# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006510 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006511 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006513 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 msec = msec / 2;
6515# endif
6516 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006517 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 }
6519#endif
6520 }
6521
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006522 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523}
6524
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006526 * Expand a path into all matching files and/or directories. Handles "*",
6527 * "?", "[a-z]", "**", etc.
6528 * "path" has backslashes before chars that are not to be expanded.
6529 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 */
6531 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006532mch_expandpath(
6533 garray_T *gap,
6534 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006535 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
Bram Moolenaar02743632005-07-25 20:42:36 +00006537 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
6540/*
6541 * mch_expand_wildcards() - this code does wild-card pattern matching using
6542 * the shell
6543 *
6544 * return OK for success, FAIL for error (you may lose some memory) and put
6545 * an error message in *file.
6546 *
6547 * num_pat is number of input patterns
6548 * pat is array of pointers to input patterns
6549 * num_file is pointer to number of matched file names
6550 * file is pointer to array of pointers to matched file names
6551 */
6552
6553#ifndef SEEK_SET
6554# define SEEK_SET 0
6555#endif
6556#ifndef SEEK_END
6557# define SEEK_END 2
6558#endif
6559
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006560#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006561
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006563mch_expand_wildcards(
6564 int num_pat,
6565 char_u **pat,
6566 int *num_file,
6567 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006568 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
6570 int i;
6571 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006572 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 char_u *p;
6574 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575
Bram Moolenaarc7247912008-01-13 12:54:11 +00006576 /*
6577 * This is the non-OS/2 implementation (really Unix).
6578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 int j;
6580 char_u *tempname;
6581 char_u *command;
6582 FILE *fd;
6583 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006584#define STYLE_ECHO 0 // use "echo", the default
6585#define STYLE_GLOB 1 // use "glob", for csh
6586#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6587#define STYLE_PRINT 3 // use "print -N", for zsh
6588#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6589 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 int shell_style = STYLE_ECHO;
6591 int check_spaces;
6592 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006593 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006594 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006595 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596
Bram Moolenaar0f873732019-12-05 20:28:46 +01006597 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 *file = NULL;
6599
6600 /*
6601 * If there are no wildcards, just copy the names to allocated memory.
6602 * Saves a lot of time, because we don't have to start a new shell.
6603 */
6604 if (!have_wildcard(num_pat, pat))
6605 return save_patterns(num_pat, pat, num_file, file);
6606
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006607# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006608 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006609 if (sandbox != 0 && check_secure())
6610 return FAIL;
6611# endif
6612
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 /*
6614 * Don't allow the use of backticks in secure and restricted mode.
6615 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006616 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 for (i = 0; i < num_pat; ++i)
6618 if (vim_strchr(pat[i], '`') != NULL
6619 && (check_restricted() || check_secure()))
6620 return FAIL;
6621
6622 /*
6623 * get a name for the temp file
6624 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006625 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006627 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 return FAIL;
6629 }
6630
6631 /*
6632 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006633 * file.
6634 * STYLE_BT: NL separated
6635 * If expanding `cmd` execute it directly.
6636 * STYLE_GLOB: NUL separated
6637 * If we use *csh, "glob" will work better than "echo".
6638 * STYLE_PRINT: NL or NUL separated
6639 * If we use *zsh, "print -N" will work better than "glob".
6640 * STYLE_VIMGLOB: NL separated
6641 * If we use *sh*, we define "vimglob()".
6642 * STYLE_ECHO: space separated.
6643 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 */
6645 if (num_pat == 1 && *pat[0] == '`'
6646 && (len = STRLEN(pat[0])) > 2
6647 && *(pat[0] + len - 1) == '`')
6648 shell_style = STYLE_BT;
6649 else if ((len = STRLEN(p_sh)) >= 3)
6650 {
6651 if (STRCMP(p_sh + len - 3, "csh") == 0)
6652 shell_style = STYLE_GLOB;
6653 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6654 shell_style = STYLE_PRINT;
6655 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006656 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6657 "sh") != NULL)
6658 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
Bram Moolenaar0f873732019-12-05 20:28:46 +01006660 // Compute the length of the command. We need 2 extra bytes: for the
6661 // optional '&' and for the NUL.
6662 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006664 if (shell_style == STYLE_VIMGLOB)
6665 len += STRLEN(sh_vimglob_func);
6666
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006667 for (i = 0; i < num_pat; ++i)
6668 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006669 // Count the length of the patterns in the same way as they are put in
6670 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006671#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006672 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006673#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006674 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006675 for (j = 0; pat[i][j] != NUL; ++j)
6676 {
6677 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006678 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006679 ++len;
6680 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006681#endif
6682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 command = alloc(len);
6684 if (command == NULL)
6685 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006686 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 vim_free(tempname);
6688 return FAIL;
6689 }
6690
6691 /*
6692 * Build the shell command:
6693 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6694 * recognizes this).
6695 * - Add the shell command to print the expanded names.
6696 * - Add the temp file name.
6697 * - Add the file name patterns.
6698 */
6699 if (shell_style == STYLE_BT)
6700 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006701 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006702 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006703 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006705 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006706 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006708 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006710 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 *p = ' ';
6712 }
6713 STRCAT(command, ">");
6714 }
6715 else
6716 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006717 STRCPY(command, "");
6718 if (shell_style == STYLE_GLOB)
6719 {
6720 // Assume the nonomatch option is valid only for csh like shells,
6721 // otherwise, this may set the positional parameters for the shell,
6722 // e.g. "$*".
6723 if (flags & EW_NOTFOUND)
6724 STRCAT(command, "set nonomatch; ");
6725 else
6726 STRCAT(command, "unset nonomatch; ");
6727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 if (shell_style == STYLE_GLOB)
6729 STRCAT(command, "glob >");
6730 else if (shell_style == STYLE_PRINT)
6731 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006732 else if (shell_style == STYLE_VIMGLOB)
6733 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 else
6735 STRCAT(command, "echo >");
6736 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006737
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006739
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 if (shell_style != STYLE_BT)
6741 for (i = 0; i < num_pat; ++i)
6742 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006743 // When using system() always add extra quotes, because the shell
6744 // is started twice. Otherwise put a backslash before special
6745 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746#ifdef USE_SYSTEM
6747 STRCAT(command, " \"");
6748 STRCAT(command, pat[i]);
6749 STRCAT(command, "\"");
6750#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006751 int intick = FALSE;
6752
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 p = command + STRLEN(command);
6754 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006755 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006756 {
6757 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006758 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006759 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6760 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006761 // Remove a backslash, take char literally. But keep
6762 // backslash inside backticks, before a special character
6763 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006764 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006765 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6766 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006767 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006768 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006769 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006770 else if (!intick
6771 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6772 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006773 // Put a backslash before a special character, but not
6774 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6775 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006776 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006777
Bram Moolenaar0f873732019-12-05 20:28:46 +01006778 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006779 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 *p = NUL;
6782#endif
6783 }
6784 if (flags & EW_SILENT)
6785 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006786 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006787 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788
6789 /*
6790 * Using zsh -G: If a pattern has no matches, it is just deleted from
6791 * the argument list, otherwise zsh gives an error message and doesn't
6792 * expand any other pattern.
6793 */
6794 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006795 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796
6797 /*
6798 * If we use -f then shell variables set in .cshrc won't get expanded.
6799 * vi can do it, so we will too, but it is only necessary if there is a "$"
6800 * in one of the patterns, otherwise we can still use the fast option.
6801 */
6802 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006803 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804
6805 /*
6806 * execute the shell command
6807 */
6808 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6809
Bram Moolenaar0f873732019-12-05 20:28:46 +01006810 // When running in the background, give it some time to create the temp
6811 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006812 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006813 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814
Bram Moolenaar0f873732019-12-05 20:28:46 +01006815 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 show_shell_mess = TRUE;
6817 vim_free(command);
6818
Bram Moolenaar0f873732019-12-05 20:28:46 +01006819 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 {
6821 mch_remove(tempname);
6822 vim_free(tempname);
6823 /*
6824 * With interactive completion, the error message is not printed.
6825 * However with USE_SYSTEM, I don't know how to turn off error messages
6826 * from the shell, so screen may still get messed up -- webb.
6827 */
6828#ifndef USE_SYSTEM
6829 if (!(flags & EW_SILENT))
6830#endif
6831 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006832 redraw_later_clear(); // probably messed up screen
6833 msg_putchar('\n'); // clear bottom line quickly
6834 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835#ifdef USE_SYSTEM
6836 if (!(flags & EW_SILENT))
6837#endif
6838 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006839 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006840 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 }
6842 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006843 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6844 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 if (shell_style == STYLE_BT)
6846 return FAIL;
6847 goto notfound;
6848 }
6849
6850 /*
6851 * read the names from the file into memory
6852 */
6853 fd = fopen((char *)tempname, READBIN);
6854 if (fd == NULL)
6855 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006856 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 if (!(flags & EW_SILENT))
6858 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006859 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006860 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 }
6862 vim_free(tempname);
6863 goto notfound;
6864 }
6865 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006866 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006868 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006869 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006870 buffer = NULL;
6871 else
6872 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 if (buffer == NULL)
6874 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006875 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 mch_remove(tempname);
6877 vim_free(tempname);
6878 fclose(fd);
6879 return FAIL;
6880 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006881 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 i = fread((char *)buffer, 1, len, fd);
6883 fclose(fd);
6884 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006885 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006887 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006888 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 vim_free(tempname);
6890 vim_free(buffer);
6891 return FAIL;
6892 }
6893 vim_free(tempname);
6894
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006895# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006896 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006898 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6900 *p++ = buffer[i];
6901 len = p - buffer;
6902# endif
6903
6904
Bram Moolenaar0f873732019-12-05 20:28:46 +01006905 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 if (shell_style == STYLE_ECHO)
6907 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006908 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006910 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 {
6912 while (*p != ' ' && *p != '\n')
6913 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006914 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 }
6916 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006917 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006918 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006920 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006922 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 {
6924 while (*p != '\n' && *p != NUL)
6925 ++p;
6926 if (*p != NUL)
6927 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006928 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 }
6930 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006931 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 else
6933 {
6934 /*
6935 * Some versions of zsh use spaces instead of NULs to separate
6936 * results. Only do this when there is no NUL before the end of the
6937 * buffer, otherwise we would never be able to use file names with
6938 * embedded spaces when zsh does use NULs.
6939 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6940 * don't check for spaces again.
6941 */
6942 check_spaces = FALSE;
6943 if (shell_style == STYLE_PRINT && !did_find_nul)
6944 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006945 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006946 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006947 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 did_find_nul = TRUE;
6949 else
6950 check_spaces = TRUE;
6951 }
6952
6953 /*
6954 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6955 * already is one, for STYLE_GLOB it needs to be added.
6956 */
6957 if (len && buffer[len - 1] == NUL)
6958 --len;
6959 else
6960 buffer[len] = NUL;
6961 i = 0;
6962 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006963 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
6965 ++i;
6966 *p = NUL;
6967 }
6968 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006969 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 }
6971 if (i == 0)
6972 {
6973 /*
6974 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6975 * /bin/sh will happily expand it to nothing rather than returning an
6976 * error; and hey, it's good to check anyway -- webb.
6977 */
6978 vim_free(buffer);
6979 goto notfound;
6980 }
6981 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006982 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (*file == NULL)
6984 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006985 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 vim_free(buffer);
6987 return FAIL;
6988 }
6989
6990 /*
6991 * Isolate the individual file names.
6992 */
6993 p = buffer;
6994 for (i = 0; i < *num_file; ++i)
6995 {
6996 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006997 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006998 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6999 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00007001 while (!(shell_style == STYLE_ECHO && *p == ' ')
7002 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007004 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 *p = NUL;
7006 else
7007 {
7008 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007009 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 }
7011 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007012 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007014 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007016 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 }
7018 }
7019
7020 /*
7021 * Move the file names to allocated memory.
7022 */
7023 for (j = 0, i = 0; i < *num_file; ++i)
7024 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007025 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7027 continue;
7028
Bram Moolenaar0f873732019-12-05 20:28:46 +01007029 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 dir = (mch_isdir((*file)[i]));
7031 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7032 continue;
7033
Bram Moolenaar0f873732019-12-05 20:28:46 +01007034 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007035 if (!dir && (flags & EW_EXEC)
7036 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007037 continue;
7038
Bram Moolenaar964b3742019-05-24 18:54:09 +02007039 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 if (p)
7041 {
7042 STRCPY(p, (*file)[i]);
7043 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007044 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 (*file)[j++] = p;
7046 }
7047 }
7048 vim_free(buffer);
7049 *num_file = j;
7050
Bram Moolenaar0f873732019-12-05 20:28:46 +01007051 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007053 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 goto notfound;
7055 }
7056
7057 return OK;
7058
7059notfound:
7060 if (flags & EW_NOTFOUND)
7061 return save_patterns(num_pat, pat, num_file, file);
7062 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063}
7064
Bram Moolenaar0f873732019-12-05 20:28:46 +01007065#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007068save_patterns(
7069 int num_pat,
7070 char_u **pat,
7071 int *num_file,
7072 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
7074 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007075 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007077 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 if (*file == NULL)
7079 return FAIL;
7080 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007081 {
7082 s = vim_strsave(pat[i]);
7083 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007084 // Be compatible with expand_filename(): halve the number of
7085 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007086 backslash_halve(s);
7087 (*file)[i] = s;
7088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 *num_file = num_pat;
7090 return OK;
7091}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093/*
7094 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7095 * expand.
7096 */
7097 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007098mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007100 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 if (*p == '\\' && p[1] != NUL)
7103 ++p;
7104 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 if (vim_strchr((char_u *)
7106#ifdef VMS
7107 "*?%"
7108#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110#endif
7111 , *p) != NULL)
7112 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 }
7114 return FALSE;
7115}
7116
7117/*
7118 * Return TRUE if the string "p" contains a wildcard.
7119 * Don't recognize '~' at the end as a wildcard.
7120 */
7121 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007122mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007124 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 if (*p == '\\' && p[1] != NUL)
7127 ++p;
7128 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 if (vim_strchr((char_u *)
7130#ifdef VMS
7131 "*?%$"
7132#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134#endif
7135 , *p) != NULL
7136 || (*p == '~' && p[1] != NUL))
7137 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 }
7139 return FALSE;
7140}
7141
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007143have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144{
7145 int i;
7146
7147 for (i = 0; i < num; i++)
7148 if (mch_has_wildcard(file[i]))
7149 return 1;
7150 return 0;
7151}
7152
7153 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007154have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155{
7156 int i;
7157
7158 for (i = 0; i < num; i++)
7159 if (vim_strchr(file[i], '$') != NULL)
7160 return TRUE;
7161 return FALSE;
7162}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007164#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165/*
7166 * Scaled-down version of rename(), which is missing in Xenix.
7167 * This version can only move regular files and will fail if the
7168 * destination exists.
7169 */
7170 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007171mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
7173 struct stat st;
7174
Bram Moolenaar0f873732019-12-05 20:28:46 +01007175 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007177 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007179 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 return 0;
7181 return -1;
7182}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007183#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007185#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007186# if defined(DYNAMIC_GPM) || defined(PROTO)
7187/*
7188 * Initialize Gpm's symbols for dynamic linking.
7189 * Must be called only if libgpm_hinst is NULL.
7190 */
7191 static int
7192load_libgpm(void)
7193{
7194 libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL);
7195
7196 if (libgpm_hinst == NULL)
7197 {
7198 if (p_verbose > 0)
7199 smsg_attr(HL_ATTR(HLF_W),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007200 _("Could not load gpm library: %s"), dlerror());
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007201 return FAIL;
7202 }
7203
7204 if (
7205 (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL
7206 || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL
7207 || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL
7208 || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL
7209 || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL
7210 )
7211 {
7212 semsg(_(e_could_not_load_library_str_str), "gpm", dlerror());
7213 dlclose(libgpm_hinst);
7214 libgpm_hinst = NULL;
7215 dll_gpm_flag = NULL;
7216 dll_gpm_fd = NULL;
7217 return FAIL;
7218 }
7219 return OK;
7220}
7221
7222 int
7223gpm_available(void)
7224{
7225 return libgpm_hinst != NULL || load_libgpm() == OK;
7226}
7227# endif // DYNAMIC_GPM
7228
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229/*
7230 * Initializes connection with gpm (if it isn't already opened)
7231 * Return 1 if succeeded (or connection already opened), 0 if failed
7232 */
7233 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007234gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007236 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007238#ifdef DYNAMIC_GPM
7239 if (!gpm_available())
7240 return 0;
7241#endif
7242
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 if (!gpm_flag)
7244 {
7245 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7246 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007247 // Default handling for mouse move
7248 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 gpm_connect.maxMod = 0xffff;
7250 if (Gpm_Open(&gpm_connect, 0) > 0)
7251 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007252 // gpm library tries to handling TSTP causes
7253 // problems. Anyways, we close connection to Gpm whenever
7254 // we are going to suspend or starting an external process
7255 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007256# ifdef SIGTSTP
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007257 signal(SIGTSTP, restricted ? SIG_IGN : (void (*)())sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007258# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007259 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 }
7261 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007262 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 return 0;
7264 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007265 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266}
7267
7268/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007269 * Returns TRUE if the GPM mouse is enabled.
7270 */
7271 int
7272gpm_enabled(void)
7273{
7274 return gpm_flag && gpm_fd >= 0;
7275}
7276
7277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 */
7280 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007281gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007283 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 Gpm_Close();
7285}
7286
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007287/*
7288 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007290 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 */
7292 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007293mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
7295 int button;
7296 static Gpm_Event gpm_event;
7297 char_u string[6];
7298 int_u vim_modifiers;
7299 int row,col;
7300 unsigned char buttons_mask;
7301 unsigned char gpm_modifiers;
7302 static unsigned char old_buttons = 0;
7303
7304 Gpm_GetEvent(&gpm_event);
7305
7306#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007307 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 if (hold_gui_events)
7309 return 0;
7310#endif
7311
7312 row = gpm_event.y - 1;
7313 col = gpm_event.x - 1;
7314
Bram Moolenaar0f873732019-12-05 20:28:46 +01007315 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 string[1] = 'M';
7317 string[2] = 'G';
7318 switch (GPM_BARE_EVENTS(gpm_event.type))
7319 {
7320 case GPM_DRAG:
7321 string[3] = MOUSE_DRAG;
7322 break;
7323 case GPM_DOWN:
7324 buttons_mask = gpm_event.buttons & ~old_buttons;
7325 old_buttons = gpm_event.buttons;
7326 switch (buttons_mask)
7327 {
7328 case GPM_B_LEFT:
7329 button = MOUSE_LEFT;
7330 break;
7331 case GPM_B_MIDDLE:
7332 button = MOUSE_MIDDLE;
7333 break;
7334 case GPM_B_RIGHT:
7335 button = MOUSE_RIGHT;
7336 break;
7337 default:
7338 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007339 // Don't know what to do. Can more than one button be
7340 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 }
7342 string[3] = (char_u)(button | 0x20);
7343 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7344 break;
7345 case GPM_UP:
7346 string[3] = MOUSE_RELEASE;
7347 old_buttons &= ~gpm_event.buttons;
7348 break;
7349 default:
7350 return 0;
7351 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007352 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 gpm_modifiers = gpm_event.modifiers;
7354 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007355 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7356 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7357 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7359 vim_modifiers |= MOUSE_SHIFT;
7360
7361 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7362 vim_modifiers |= MOUSE_CTRL;
7363 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7364 vim_modifiers |= MOUSE_ALT;
7365 string[3] |= vim_modifiers;
7366 string[4] = (char_u)(col + ' ' + 1);
7367 string[5] = (char_u)(row + ' ' + 1);
7368 add_to_input_buf(string, 6);
7369 return 6;
7370}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007371#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007373#ifdef FEAT_SYSMOUSE
7374/*
7375 * Initialize connection with sysmouse.
7376 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7377 * output, any sysmouse output than will be processed via sig_sysmouse().
7378 * Return OK if succeeded, FAIL if failed.
7379 */
7380 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007381sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007382{
7383 struct mouse_info mouse;
7384
7385 mouse.operation = MOUSE_MODE;
7386 mouse.u.mode.mode = 0;
7387 mouse.u.mode.signal = SIGUSR2;
7388 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7389 {
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007390 signal(SIGUSR2, (void (*)())sig_sysmouse);
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007391 mouse.operation = MOUSE_SHOW;
7392 ioctl(1, CONS_MOUSECTL, &mouse);
7393 return OK;
7394 }
7395 return FAIL;
7396}
7397
7398/*
7399 * Stop processing SIGUSR2 signals, and also make sure that
7400 * virtual console do not send us any sysmouse related signal.
7401 */
7402 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007403sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007404{
7405 struct mouse_info mouse;
7406
7407 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7408 mouse.operation = MOUSE_MODE;
7409 mouse.u.mode.mode = 0;
7410 mouse.u.mode.signal = 0;
7411 ioctl(1, CONS_MOUSECTL, &mouse);
7412}
7413
7414/*
7415 * Gets info from sysmouse and adds special keys to input buf.
7416 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007417 static void
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007418sig_sysmouse SIGDEFARG(sigarg)
7419{
7420 struct mouse_info mouse;
7421 struct video_info video;
7422 char_u string[6];
7423 int row, col;
7424 int button;
7425 int buttons;
7426 static int oldbuttons = 0;
7427
7428#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007429 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007430 if (hold_gui_events)
7431 return;
7432#endif
7433
7434 mouse.operation = MOUSE_GETINFO;
7435 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7436 && ioctl(1, FBIO_MODEINFO, &video) != -1
7437 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7438 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7439 {
7440 row = mouse.u.data.y / video.vi_cheight;
7441 col = mouse.u.data.x / video.vi_cwidth;
7442 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007443 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007444 string[1] = 'M';
7445 string[2] = 'S';
7446 if (oldbuttons == buttons && buttons != 0)
7447 {
7448 button = MOUSE_DRAG;
7449 }
7450 else
7451 {
7452 switch (buttons)
7453 {
7454 case 0:
7455 button = MOUSE_RELEASE;
7456 break;
7457 case 1:
7458 button = MOUSE_LEFT;
7459 break;
7460 case 2:
7461 button = MOUSE_MIDDLE;
7462 break;
7463 case 4:
7464 button = MOUSE_RIGHT;
7465 break;
7466 default:
7467 return;
7468 }
7469 oldbuttons = buttons;
7470 }
7471 string[3] = (char_u)(button);
7472 string[4] = (char_u)(col + ' ' + 1);
7473 string[5] = (char_u)(row + ' ' + 1);
7474 add_to_input_buf(string, 6);
7475 }
7476 return;
7477}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007478#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007479
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007481typedef char_u * (*STRPROCSTR)(char_u *);
7482typedef char_u * (*INTPROCSTR)(int);
7483typedef int (*STRPROCINT)(char_u *);
7484typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485
7486/*
7487 * Call a DLL routine which takes either a string or int param
7488 * and returns an allocated string.
7489 */
7490 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007491mch_libcall(
7492 char_u *libname,
7493 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007494 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007495 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007496 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007497 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498{
7499# if defined(USE_DLOPEN)
7500 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007501 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502# else
7503 shl_t hinstLib;
7504# endif
7505 STRPROCSTR ProcAdd;
7506 INTPROCSTR ProcAddI;
7507 char_u *retval_str = NULL;
7508 int retval_int = 0;
7509 int success = FALSE;
7510
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007511 /*
7512 * Get a handle to the DLL module.
7513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007515 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007516 (void)dlerror();
7517
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 hinstLib = dlopen((char *)libname, RTLD_LAZY
7519# ifdef RTLD_LOCAL
7520 | RTLD_LOCAL
7521# endif
7522 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007523 if (hinstLib == NULL)
7524 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007525 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007526 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007527 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007528 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530# else
7531 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7532# endif
7533
Bram Moolenaar0f873732019-12-05 20:28:46 +01007534 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 if (hinstLib != NULL)
7536 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007537# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 /*
7539 * Catch a crash when calling the library function. For example when
7540 * using a number where a string pointer is expected.
7541 */
7542 mch_startjmp();
7543 if (SETJMP(lc_jump_env) != 0)
7544 {
7545 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007546# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007547 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 mch_didjmp();
7550 }
7551 else
7552# endif
7553 {
7554 retval_str = NULL;
7555 retval_int = 0;
7556
7557 if (argstring != NULL)
7558 {
7559# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007560 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007561 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562# else
7563 if (shl_findsym(&hinstLib, (const char *)funcname,
7564 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7565 ProcAdd = NULL;
7566# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007567 if ((success = (ProcAdd != NULL
7568# if defined(USE_DLOPEN)
7569 && dlerr == NULL
7570# endif
7571 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 {
7573 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007574 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 else
7576 retval_str = (ProcAdd)(argstring);
7577 }
7578 }
7579 else
7580 {
7581# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007582 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007583 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584# else
7585 if (shl_findsym(&hinstLib, (const char *)funcname,
7586 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7587 ProcAddI = NULL;
7588# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007589 if ((success = (ProcAddI != NULL
7590# if defined(USE_DLOPEN)
7591 && dlerr == NULL
7592# endif
7593 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 {
7595 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007596 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 else
7598 retval_str = (ProcAddI)(argint);
7599 }
7600 }
7601
Bram Moolenaar0f873732019-12-05 20:28:46 +01007602 // Save the string before we free the library.
7603 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 if (string_result == NULL)
7605 *number_result = retval_int;
7606 else if (retval_str != NULL
7607 && retval_str != (char_u *)1
7608 && retval_str != (char_u *)-1)
7609 *string_result = vim_strsave(retval_str);
7610 }
7611
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007612# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 mch_endjmp();
7614# ifdef SIGHASARG
7615 if (lc_signal != 0)
7616 {
7617 int i;
7618
Bram Moolenaar0f873732019-12-05 20:28:46 +01007619 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 for (i = 0; signal_info[i].sig != -1; i++)
7621 if (lc_signal == signal_info[i].sig)
7622 break;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00007623 semsg(e_got_sig_str_in_libcall, signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 }
7625# endif
7626# endif
7627
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007629 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007630 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007631 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007632
Bram Moolenaar0f873732019-12-05 20:28:46 +01007633 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 (void)dlclose(hinstLib);
7635# else
7636 (void)shl_unload(hinstLib);
7637# endif
7638 }
7639
7640 if (!success)
7641 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007642 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 return FAIL;
7644 }
7645
7646 return OK;
7647}
7648#endif
7649
7650#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007651static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652static int xterm_button;
7653
7654/*
7655 * Setup a dummy window for X selections in a terminal.
7656 */
7657 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007658setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659{
7660 int z = 0;
7661 char *strp = "";
7662 Widget AppShell;
7663
7664 if (!x_connect_to_server())
7665 return;
7666
7667 open_app_context();
7668 if (app_context != NULL && xterm_Shell == (Widget)0)
7669 {
7670 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007671# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007673# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007674# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007675 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676
7677 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007678 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679# endif
7680
Bram Moolenaar0f873732019-12-05 20:28:46 +01007681 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 oldhandler = XSetErrorHandler(x_error_check);
7683
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007684# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007685 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7687 mch_startjmp();
7688 if (SETJMP(lc_jump_env) != 0)
7689 {
7690 mch_didjmp();
7691 xterm_dpy = NULL;
7692 }
7693 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 {
7696 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7697 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007698 if (xterm_dpy != NULL)
7699 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007700# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007702# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 }
7704
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007705# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007706 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007708# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007709 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 (void)XSetErrorHandler(oldhandler);
7711
7712 if (xterm_dpy == NULL)
7713 {
7714 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007715 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 return;
7717 }
7718
Bram Moolenaar0f873732019-12-05 20:28:46 +01007719 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 (void)XSetIOErrorHandler(x_IOerror_handler);
7721
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007722# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007724 {
7725 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007726 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007727 verbose_leave();
7728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729# endif
7730
Bram Moolenaar0f873732019-12-05 20:28:46 +01007731 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7733 applicationShellWidgetClass, xterm_dpy,
7734 NULL);
7735 if (AppShell == (Widget)0)
7736 return;
7737 xterm_Shell = XtVaCreatePopupShell("VIM",
7738 topLevelShellWidgetClass, AppShell,
7739 XtNmappedWhenManaged, 0,
7740 XtNwidth, 1,
7741 XtNheight, 1,
7742 NULL);
7743 if (xterm_Shell == (Widget)0)
7744 return;
7745
7746 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007747 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 if (x11_display == NULL)
7749 x11_display = xterm_dpy;
7750
7751 XtRealizeWidget(xterm_Shell);
7752 XSync(xterm_dpy, False);
7753 xterm_update();
7754 }
7755 if (xterm_Shell != (Widget)0)
7756 {
7757 clip_init(TRUE);
7758 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7759 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007760 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 if (test_x11_window(xterm_dpy) == FAIL)
7762 x11_window = 0;
7763 if (x11_window != 0)
7764 xterm_trace = 0;
7765 }
7766}
7767
7768 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007769start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770{
7771 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7772 return;
7773 xterm_trace = 1;
7774 xterm_button = button;
7775 do_xterm_trace();
7776}
7777
7778
7779 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007780stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781{
7782 if (xterm_trace < 0)
7783 return;
7784 xterm_trace = 0;
7785}
7786
7787/*
7788 * Query the xterm pointer and generate mouse termcodes if necessary
7789 * return TRUE if dragging is active, else FALSE
7790 */
7791 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007792do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
7794 Window root, child;
7795 int root_x, root_y;
7796 int win_x, win_y;
7797 int row, col;
7798 int_u mask_return;
7799 char_u buf[50];
7800 char_u *strp;
7801 long got_hints;
7802 static char_u *mouse_code;
7803 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7804 static int prev_row = 0, prev_col = 0;
7805 static XSizeHints xterm_hints;
7806
7807 if (xterm_trace <= 0)
7808 return FALSE;
7809
7810 if (xterm_trace == 1)
7811 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007812 // Get the hints just before tracking starts. The font size might
7813 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007814 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7815 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 || xterm_hints.width_inc <= 1
7817 || xterm_hints.height_inc <= 1)
7818 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007819 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 return FALSE;
7821 }
7822
Bram Moolenaar0f873732019-12-05 20:28:46 +01007823 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 mouse_code = find_termcode(mouse_name);
7825 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007826 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 xterm_trace = 2;
7828
Bram Moolenaar0f873732019-12-05 20:28:46 +01007829 // Find the offset of the chars, there might be a scrollbar on the
7830 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7832 &win_x, &win_y, &mask_return);
7833 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7834 - (xterm_hints.height_inc / 2);
7835 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7836 xterm_hints.y = 2;
7837 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7838 - (xterm_hints.width_inc / 2);
7839 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7840 xterm_hints.x = 2;
7841 return TRUE;
7842 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007843 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {
7845 xterm_trace = 0;
7846 return FALSE;
7847 }
7848
7849 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7850 &win_x, &win_y, &mask_return);
7851
7852 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7853 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7854 if (row == prev_row && col == prev_col)
7855 return TRUE;
7856
7857 STRCPY(buf, mouse_code);
7858 strp = buf + STRLEN(buf);
7859 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7860 *strp++ = (char_u)(col + ' ' + 1);
7861 *strp++ = (char_u)(row + ' ' + 1);
7862 *strp = 0;
7863 add_to_input_buf(buf, STRLEN(buf));
7864
7865 prev_row = row;
7866 prev_col = col;
7867 return TRUE;
7868}
7869
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007870# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871/*
7872 * Destroy the display, window and app_context. Required for GTK.
7873 */
7874 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007875clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876{
7877 if (xterm_Shell != (Widget)0)
7878 {
7879 XtDestroyWidget(xterm_Shell);
7880 xterm_Shell = (Widget)0;
7881 }
7882 if (xterm_dpy != NULL)
7883 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007884# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007885 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 if (x11_display == xterm_dpy)
7889 x11_display = NULL;
7890 xterm_dpy = NULL;
7891 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007892# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 if (app_context != (XtAppContext)NULL)
7894 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007895 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 XtDestroyApplicationContext(app_context);
7897 app_context = (XtAppContext)NULL;
7898 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007899# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900}
7901# endif
7902
7903/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007904 * Catch up with GUI or X events.
7905 */
7906 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007907clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007908{
7909# ifdef FEAT_GUI
7910 if (gui.in_use)
7911 gui_mch_update();
7912 else
7913# endif
7914 if (xterm_Shell != (Widget)0)
7915 xterm_update();
7916}
7917
7918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 * Catch up with any queued X events. This may put keyboard input into the
7920 * input buffer, call resize call-backs, trigger timers etc. If there is
7921 * nothing in the X event queue (& no timers pending), then we return
7922 * immediately.
7923 */
7924 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007925xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927 XEvent event;
7928
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007929 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007931 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007933 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007934 break;
7935
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007936 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007937 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007938 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007939 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007940#ifdef FEAT_CLIENTSERVER
7941 {
7942 XPropertyEvent *e = (XPropertyEvent *)&event;
7943
7944 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007946 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007949 XtDispatchEvent(&event);
7950 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007951 else
7952 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007953 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007954 XtAppProcessEvent(app_context, mask);
7955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 }
7957}
7958
7959 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007960clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961{
7962 if (xterm_Shell != (Widget)0)
7963 return clip_x11_own_selection(xterm_Shell, cbd);
7964 return FAIL;
7965}
7966
7967 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007968clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 if (xterm_Shell != (Widget)0)
7971 clip_x11_lose_selection(xterm_Shell, cbd);
7972}
7973
7974 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007975clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 if (xterm_Shell != (Widget)0)
7978 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7979}
7980
7981 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007982clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983{
7984 clip_x11_set_selection(cbd);
7985}
7986#endif
7987
7988
7989#if defined(USE_XSMP) || defined(PROTO)
7990/*
7991 * Code for X Session Management Protocol.
7992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993
7994# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995/*
7996 * This is our chance to ask the user if they want to save,
7997 * or abort the logout
7998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008000xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001{
Bram Moolenaare1004402020-10-24 20:49:43 +02008002 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 int cancel_shutdown = False;
8004
Bram Moolenaare1004402020-10-24 20:49:43 +02008005 save_cmod_flags = cmdmod.cmod_flags;
8006 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01008007 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01008008 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02008010 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01008011 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 out_flush();
8013
Bram Moolenaar0f873732019-12-05 20:28:46 +01008014 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 SmcInteractDone(smc_conn, cancel_shutdown);
8016
Bram Moolenaar0f873732019-12-05 20:28:46 +01008017 // Finish off
8018 // Only end save-yourself here if we're not cancelling shutdown;
8019 // we'll get a cancelled callback later in which we'll end it.
8020 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 if (!cancel_shutdown)
8022 {
8023 xsmp.save_yourself = False;
8024 SmcSaveYourselfDone(smc_conn, True);
8025 }
8026}
8027# endif
8028
8029/*
8030 * Callback that starts save-yourself.
8031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008033xsmp_handle_save_yourself(
8034 SmcConn smc_conn,
8035 SmPointer client_data UNUSED,
8036 int save_type UNUSED,
8037 Bool shutdown,
8038 int interact_style UNUSED,
8039 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008041 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 if (xsmp.save_yourself)
8043 SmcSaveYourselfDone(smc_conn, True);
8044 xsmp.save_yourself = True;
8045 xsmp.shutdown = shutdown;
8046
Bram Moolenaar0f873732019-12-05 20:28:46 +01008047 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01008049 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050
8051 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008052 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053
8054# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008055 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008057 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 SmcInteractRequest(smc_conn, SmDialogError,
8059 xsmp_handle_interaction, client_data);
8060 else
8061# endif
8062 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008063 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 SmcSaveYourselfDone(smc_conn, True);
8065 xsmp.save_yourself = False;
8066 }
8067}
8068
8069
8070/*
8071 * Callback to warn us of imminent death.
8072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008074xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075{
8076 xsmp_close();
8077
Bram Moolenaar0f873732019-12-05 20:28:46 +01008078 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 getout_preserve_modified(0);
8080}
8081
8082
8083/*
8084 * Callback to tell us that save-yourself has completed.
8085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008087xsmp_save_complete(
8088 SmcConn smc_conn UNUSED,
8089 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090{
8091 xsmp.save_yourself = False;
8092}
8093
8094
8095/*
8096 * Callback to tell us that an instigated shutdown was cancelled
8097 * (maybe even by us)
8098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008100xsmp_shutdown_cancelled(
8101 SmcConn smc_conn,
8102 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103{
8104 if (xsmp.save_yourself)
8105 SmcSaveYourselfDone(smc_conn, True);
8106 xsmp.save_yourself = False;
8107 xsmp.shutdown = False;
8108}
8109
8110
8111/*
8112 * Callback to tell us that a new ICE connection has been established.
8113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008115xsmp_ice_connection(
8116 IceConn iceConn,
8117 IcePointer clientData UNUSED,
8118 Bool opening,
8119 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008121 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 if (opening)
8123 {
8124 xsmp_icefd = IceConnectionNumber(iceConn);
8125 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8126 }
8127}
8128
8129
Bram Moolenaar0f873732019-12-05 20:28:46 +01008130// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008132xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133{
8134 Bool rep;
8135
8136 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8137 == IceProcessMessagesIOError)
8138 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008139 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008141 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 xsmp_close();
8143 return FAIL;
8144 }
8145 else
8146 return OK;
8147}
8148
8149static int dummy;
8150
Bram Moolenaar0f873732019-12-05 20:28:46 +01008151// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 void
8153xsmp_init(void)
8154{
8155 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 SmcCallbacks smcallbacks;
8157#if 0
8158 SmPropValue smname;
8159 SmProp smnameprop;
8160 SmProp *smprops[1];
8161#endif
8162
8163 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008164 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165
8166 xsmp.save_yourself = xsmp.shutdown = False;
8167
Bram Moolenaar0f873732019-12-05 20:28:46 +01008168 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8170 smcallbacks.save_yourself.client_data = NULL;
8171 smcallbacks.die.callback = xsmp_die;
8172 smcallbacks.die.client_data = NULL;
8173 smcallbacks.save_complete.callback = xsmp_save_complete;
8174 smcallbacks.save_complete.client_data = NULL;
8175 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8176 smcallbacks.shutdown_cancelled.client_data = NULL;
8177
Bram Moolenaar0f873732019-12-05 20:28:46 +01008178 // Set up a watch on ICE connection creations. The "dummy" argument is
8179 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8181 {
8182 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008183 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 return;
8185 }
8186
Bram Moolenaar0f873732019-12-05 20:28:46 +01008187 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 xsmp.smcconn = SmcOpenConnection(
8189 NULL,
8190 NULL,
8191 SmProtoMajor,
8192 SmProtoMinor,
8193 SmcSaveYourselfProcMask | SmcDieProcMask
8194 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8195 &smcallbacks,
8196 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008197 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008198 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 errorstring);
8200 if (xsmp.smcconn == NULL)
8201 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008203 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008204 char errorreport[132];
8205
8206 // If the message is too long it might not be NUL terminated. Add
8207 // a NUL at the end to make sure we don't go over the end.
8208 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008209 vim_snprintf(errorreport, sizeof(errorreport),
8210 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008211 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 return;
8214 }
8215 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8216
8217#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008218 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 smname.value = "vim";
8220 smname.length = 3;
8221 smnameprop.name = "SmProgram";
8222 smnameprop.type = "SmARRAY8";
8223 smnameprop.num_vals = 1;
8224 smnameprop.vals = &smname;
8225
8226 smprops[0] = &smnameprop;
8227 SmcSetProperties(xsmp.smcconn, 1, smprops);
8228#endif
8229}
8230
8231
Bram Moolenaar0f873732019-12-05 20:28:46 +01008232// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008234xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
8236 if (xsmp_icefd != -1)
8237 {
8238 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008239 if (xsmp.clientid != NULL)
8240 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008241 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 xsmp_icefd = -1;
8243 }
8244}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008245#endif // USE_XSMP
Paul Ollis65745772022-06-05 16:55:54 +01008246
8247#if defined(FEAT_RELTIME) || defined(PROTO)
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008248# if defined(HAVE_TIMER_CREATE) || defined(PROTO)
Paul Ollis65745772022-06-05 16:55:54 +01008249/*
8250 * Implement timeout with timer_create() and timer_settime().
8251 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008252static volatile sig_atomic_t timeout_flag = FALSE;
8253static timer_t timer_id;
8254static int timer_created = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008255
8256/*
8257 * Callback for when the timer expires.
8258 */
8259 static void
8260set_flag(union sigval _unused UNUSED)
8261{
8262 timeout_flag = TRUE;
8263}
8264
8265/*
8266 * Stop any active timeout.
8267 */
8268 void
8269stop_timeout(void)
8270{
8271 static struct itimerspec disarm = {{0, 0}, {0, 0}};
8272
8273 if (timer_created)
8274 {
8275 int ret = timer_settime(timer_id, 0, &disarm, NULL);
8276
8277 if (ret < 0)
8278 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8279 }
8280
8281 // Clear the current timeout flag; any previous timeout should be
8282 // considered _not_ triggered.
8283 timeout_flag = FALSE;
8284}
8285
8286/*
8287 * Start the timeout timer.
8288 *
8289 * The return value is a pointer to a flag that is initialised to FALSE. If the
8290 * timeout expires, the flag is set to TRUE. This will only return pointers to
8291 * static memory; i.e. any pointer returned by this function may always be
8292 * safely dereferenced.
8293 *
8294 * This function is not expected to fail, but if it does it will still return a
8295 * valid flag pointer; the flag will remain stuck as FALSE .
8296 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008297 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008298start_timeout(long msec)
8299{
8300 struct itimerspec interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008301 {0, 0}, // Do not repeat.
8302 {msec / 1000, (msec % 1000) * 1000000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008303 int ret;
8304
8305 // This is really the caller's responsibility, but let's make sure the
8306 // previous timer has been stopped.
8307 stop_timeout();
Paul Ollis65745772022-06-05 16:55:54 +01008308
8309 if (!timer_created)
8310 {
8311 struct sigevent action = {0};
8312
8313 action.sigev_notify = SIGEV_THREAD;
8314 action.sigev_notify_function = set_flag;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008315 ret = timer_create(CLOCK_MONOTONIC, &action, &timer_id);
8316 if (ret < 0)
Paul Ollis65745772022-06-05 16:55:54 +01008317 {
8318 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8319 return &timeout_flag;
8320 }
8321 timer_created = TRUE;
8322 }
8323
Bram Moolenaar509ce032022-06-20 11:23:01 +01008324# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar616592e2022-06-17 15:17:10 +01008325 ch_log(NULL, "setting timeout timer to %d sec %ld nsec",
8326 (int)interval.it_value.tv_sec, (long)interval.it_value.tv_nsec);
Bram Moolenaar509ce032022-06-20 11:23:01 +01008327# endif
Paul Ollis65745772022-06-05 16:55:54 +01008328 ret = timer_settime(timer_id, 0, &interval, NULL);
8329 if (ret < 0)
8330 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8331
8332 return &timeout_flag;
8333}
8334
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008335/*
8336 * To be used before fork/exec: delete any created timer.
8337 */
8338 void
8339delete_timer(void)
8340{
8341 if (timer_created)
8342 {
8343 timer_delete(timer_id);
8344 timer_created = FALSE;
8345 }
8346}
8347
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008348# else // HAVE_TIMER_CREATE
Paul Ollis65745772022-06-05 16:55:54 +01008349
8350/*
8351 * Implement timeout with setitimer()
8352 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008353static struct sigaction prev_sigaction;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008354static volatile sig_atomic_t timeout_flag = FALSE;
8355static int timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008356static int timer_handler_active = FALSE;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008357static volatile sig_atomic_t alarm_pending = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008358
8359/*
8360 * Handle SIGALRM for a timeout.
8361 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01008362 static void
Paul Ollis65745772022-06-05 16:55:54 +01008363set_flag SIGDEFARG(sigarg)
8364{
8365 if (alarm_pending)
8366 alarm_pending = FALSE;
8367 else
8368 timeout_flag = TRUE;
8369}
8370
8371/*
8372 * Stop any active timeout.
8373 */
8374 void
8375stop_timeout(void)
8376{
8377 static struct itimerval disarm = {{0, 0}, {0, 0}};
8378 int ret;
8379
8380 if (timer_active)
8381 {
8382 timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008383 ret = setitimer(ITIMER_REAL, &disarm, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008384 if (ret < 0)
8385 // Should only get here as a result of coding errors.
8386 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8387 }
8388
8389 if (timer_handler_active)
8390 {
8391 timer_handler_active = FALSE;
8392 ret = sigaction(SIGALRM, &prev_sigaction, NULL);
8393 if (ret < 0)
8394 // Should only get here as a result of coding errors.
8395 semsg(_(e_could_not_reset_handler_for_timeout_str),
8396 strerror(errno));
8397 }
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008398 timeout_flag = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008399}
8400
8401/*
8402 * Start the timeout timer.
8403 *
8404 * The return value is a pointer to a flag that is initialised to FALSE. If the
8405 * timeout expires, the flag is set to TRUE. This will only return pointers to
8406 * static memory; i.e. any pointer returned by this function may always be
8407 * safely dereferenced.
8408 *
8409 * This function is not expected to fail, but if it does it will still return a
8410 * valid flag pointer; the flag will remain stuck as FALSE .
8411 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008412 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008413start_timeout(long msec)
8414{
8415 struct itimerval interval = {
Bram Moolenaar88456cd2022-11-18 22:14:09 +00008416 {0, 0}, // Do not repeat.
8417 {msec / 1000, (msec % 1000) * 1000}}; // Timeout interval
Paul Ollis65745772022-06-05 16:55:54 +01008418 struct sigaction handle_alarm;
8419 int ret;
8420 sigset_t sigs;
8421 sigset_t saved_sigs;
8422
8423 // This is really the caller's responsibility, but let's make sure the
8424 // previous timer has been stopped.
8425 stop_timeout();
8426
8427 // There is a small chance that SIGALRM is pending and so the handler must
8428 // ignore it on the first call.
8429 alarm_pending = FALSE;
8430 ret = sigemptyset(&sigs);
8431 ret = ret == 0 ? sigaddset(&sigs, SIGALRM) : ret;
8432 ret = ret == 0 ? sigprocmask(SIG_BLOCK, &sigs, &saved_sigs) : ret;
8433 timeout_flag = FALSE;
8434 ret = ret == 0 ? sigpending(&sigs) : ret;
8435 if (ret == 0)
8436 {
8437 alarm_pending = sigismember(&sigs, SIGALRM);
Bram Moolenaar1f89abf2022-06-06 10:07:01 +01008438 ret = sigprocmask(SIG_SETMASK, &saved_sigs, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008439 }
8440 if (unlikely(ret != 0 || alarm_pending < 0))
8441 {
8442 // Just catching coding errors. Write an error message, but carry on.
8443 semsg(_(e_could_not_check_for_pending_sigalrm_str), strerror(errno));
8444 alarm_pending = FALSE;
8445 }
8446
8447 // Set up the alarm handler first.
8448 ret = sigemptyset(&handle_alarm.sa_mask);
8449 handle_alarm.sa_handler = set_flag;
8450 handle_alarm.sa_flags = 0;
8451 ret = ret == 0 ? sigaction(SIGALRM, &handle_alarm, &prev_sigaction) : ret;
8452 if (ret < 0)
8453 {
8454 // Should only get here as a result of coding errors.
8455 semsg(_(e_could_not_set_handler_for_timeout_str), strerror(errno));
8456 return &timeout_flag;
8457 }
8458 timer_handler_active = TRUE;
8459
8460 // Set up the interval timer once the alarm handler is in place.
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008461 ret = setitimer(ITIMER_REAL, &interval, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008462 if (ret < 0)
8463 {
8464 // Should only get here as a result of coding errors.
8465 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8466 stop_timeout();
8467 return &timeout_flag;
8468 }
8469
8470 timer_active = TRUE;
8471 return &timeout_flag;
8472}
8473# endif // HAVE_TIMER_CREATE
8474#endif // FEAT_RELTIME