blob: 18c766d246b3066512a9d8eca75ce20d7909fe90 [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];
255 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
256 * 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().
962static volatile sig_atomic_t lc_active INIT(= FALSE);
963
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();
1231 redraw_later(CLEAR);
1232 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 Moolenaar3a7c85b2005-02-05 21:39:53 +00002355 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 */
2357 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002358vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
2360 if (name == NULL)
2361 return FALSE;
2362 return (STRNICMP(name, "xterm", 5) == 0
2363 || STRNICMP(name, "nxterm", 6) == 0
2364 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002365 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002367 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 || STRCMP(name, "builtin_xterm") == 0);
2369}
2370
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002371#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2372/*
2373 * Return TRUE if "name" appears to be that of a terminal
2374 * known to support the xterm-style mouse protocol.
2375 * Relies on term_is_xterm having been set to its correct value.
2376 */
2377 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002378use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002379{
2380 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002381 && (term_is_xterm
2382 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002383 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002384 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002385 || STRICMP(name, "st") == 0
2386 || STRNICMP(name, "st-", 3) == 0
2387 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002388}
2389#endif
2390
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391/*
2392 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2393 * Return 1 for "xterm".
2394 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002395 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002396 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 */
2398 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002399use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002401 if (ttym_flags == TTYM_SGR)
2402 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002403 if (ttym_flags == TTYM_URXVT)
2404 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 if (ttym_flags == TTYM_XTERM2)
2406 return 2;
2407 if (ttym_flags == TTYM_XTERM)
2408 return 1;
2409 return 0;
2410}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
2412 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002413vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414{
2415 if (name == NULL)
2416 return FALSE;
2417 return (STRNICMP(name, "iris-ansi", 9) == 0
2418 || STRCMP(name, "builtin_iris-ansi") == 0);
2419}
2420
2421 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002422vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423{
2424 if (name == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002425 return FALSE; // actually all ANSI comp. terminals should be here
2426 // catch VT100 - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002427 return ((STRNICMP(name, "vt", 2) == 0
2428 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 || STRCMP(name, "builtin_vt320") == 0);
2430}
2431
2432/*
2433 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2434 * This should include all windowed terminal emulators.
2435 */
2436 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002437vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 if (name == NULL)
2440 return FALSE;
2441 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2442 return TRUE;
2443 return ( STRNICMP(name, "hpterm", 6) == 0
2444 || STRNICMP(name, "sun-cmd", 7) == 0
2445 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002446 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 || STRNICMP(name, "dtterm", 6) == 0);
2448}
2449
2450/*
2451 * Insert user name in s[len].
2452 * Return OK if a name found.
2453 */
2454 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002455mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002458 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 return OK;
2460#else
2461 return mch_get_uname(getuid(), s, len);
2462#endif
2463}
2464
2465/*
2466 * Insert user name for "uid" in s[len].
2467 * Return OK if a name found.
2468 */
2469 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002470mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2473 struct passwd *pw;
2474
2475 if ((pw = getpwuid(uid)) != NULL
2476 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2477 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002478 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 return OK;
2480 }
2481#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002482 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2483 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484}
2485
2486/*
2487 * Insert host name is s[len].
2488 */
2489
2490#ifdef HAVE_SYS_UTSNAME_H
2491 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002492mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494 struct utsname vutsname;
2495
2496 if (uname(&vutsname) < 0)
2497 *s = NUL;
2498 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002499 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002501#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503# ifdef HAVE_SYS_SYSTEMINFO_H
2504# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2505# endif
2506
2507 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002508mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510# ifdef VAXC
2511 vaxc$gethostname((char *)s, len);
2512# else
2513 gethostname((char *)s, len);
2514# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002515 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002517#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
2519/*
2520 * return process ID
2521 */
2522 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002523mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525 return (long)getpid();
2526}
2527
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002528/*
2529 * return TRUE if process "pid" is still running
2530 */
2531 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002532mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002533{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002534 // If there is no error the process must be running.
2535 if (kill(pid, 0) == 0)
2536 return TRUE;
2537#ifdef ESRCH
2538 // If the error is ESRCH then the process is not running.
2539 if (errno == ESRCH)
2540 return FALSE;
2541#endif
2542 // If the process is running and owned by another user we get EPERM. With
2543 // other errors the process might be running, assuming it is then.
2544 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002545}
2546
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002549strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
2551 extern int sys_nerr;
2552 extern char *sys_errlist[];
2553 static char er[20];
2554
2555 if (err > 0 && err < sys_nerr)
2556 return (sys_errlist[err]);
2557 sprintf(er, "Error %d", err);
2558 return er;
2559}
2560#endif
2561
2562/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002563 * Get name of current directory into buffer "buf" of length "len" bytes.
2564 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 * Return OK for success, FAIL for failure.
2566 */
2567 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002568mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569{
2570#if defined(USE_GETCWD)
2571 if (getcwd((char *)buf, len) == NULL)
2572 {
2573 STRCPY(buf, strerror(errno));
2574 return FAIL;
2575 }
2576 return OK;
2577#else
2578 return (getwd((char *)buf) != NULL ? OK : FAIL);
2579#endif
2580}
2581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002583 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 *
2585 * return FAIL for failure, OK for success
2586 */
2587 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002588mch_FullName(
2589 char_u *fname,
2590 char_u *buf,
2591 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002592 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
2594 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002595#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002597 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 char_u olddir[MAXPATHL];
2600 char_u *p;
2601 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002602#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002603 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2604 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002605#endif
2606
Bram Moolenaar38323e42007-03-06 19:22:53 +00002607#ifdef VMS
2608 fname = vms_fixfilename(fname);
2609#endif
2610
Bram Moolenaara2442432007-04-26 14:26:37 +00002611#ifdef __CYGWIN__
2612 /*
2613 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2614 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002615# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002616 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2617 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002618 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2619 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002620# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002621 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002622# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002623 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002624#endif
2625
Bram Moolenaar0f873732019-12-05 20:28:46 +01002626 // Expand it if forced or not an absolute path.
2627 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002628 if ((force || !mch_isFullName(fname))
2629 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
2631 /*
2632 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002633 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 * This will get the correct path name with "../" things.
2635 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002636 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002638 if (STRCMP(p, "/..") == 0)
2639 // for "/path/dir/.." include the "/.."
2640 p += 3;
2641
Bram Moolenaar38323e42007-03-06 19:22:53 +00002642#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 /*
2644 * Use fchdir() if possible, it's said to be faster and more
2645 * reliable. But on SunOS 4 it might not work. Check this by
2646 * doing a fchdir() right now.
2647 */
2648 if (!dont_fchdir)
2649 {
2650 fd = open(".", O_RDONLY | O_EXTRA, 0);
2651 if (fd >= 0 && fchdir(fd) < 0)
2652 {
2653 close(fd);
2654 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002655 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 }
2657 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
Bram Moolenaar0f873732019-12-05 20:28:46 +01002660 // Only change directory when we are sure we can return to where
2661 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002663#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 (mch_dirname(olddir, MAXPATHL) == FAIL
2667 || mch_chdir((char *)olddir) != 0))
2668 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002669 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 retval = FAIL;
2671 }
2672 else
2673 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002674 // The directory is copied into buf[], to be able to remove
2675 // the file name without changing it (could be a string in
2676 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 if (p - fname >= len)
2678 retval = FAIL;
2679 else
2680 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002681 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002683 {
2684 // Path does not exist (yet). For a full path fail,
2685 // will use the path as-is. For a relative path use
2686 // the current directory and append the file name.
2687 if (mch_isFullName(fname))
2688 retval = FAIL;
2689 else
2690 p = NULL;
2691 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002692 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002694 else
2695 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 *buf = NUL;
2697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
2699 }
2700 if (mch_dirname(buf, len) == FAIL)
2701 {
2702 retval = FAIL;
2703 *buf = NUL;
2704 }
2705 if (p != NULL)
2706 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002707#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (fd >= 0)
2709 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002710 if (p_verbose >= 5)
2711 {
2712 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002713 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002714 verbose_leave();
2715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
2718 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 l = mch_chdir((char *)olddir);
2721 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002722 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
itchyny051a40c2021-10-20 10:00:05 +01002724#ifdef HAVE_FCHDIR
2725 if (fd >= 0)
2726 close(fd);
2727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002730 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002731 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002732#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002733 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002735 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002738
Bram Moolenaar0f873732019-12-05 20:28:46 +01002739 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002740 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 return FAIL;
2742
Bram Moolenaar0f873732019-12-05 20:28:46 +01002743 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 if (STRCMP(fname, ".") != 0)
2745 STRCAT(buf, fname);
2746
2747 return OK;
2748}
2749
2750/*
2751 * Return TRUE if "fname" does not depend on the current directory.
2752 */
2753 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002754mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002756#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 return ( fname[0] == '/' || fname[0] == '.' ||
2758 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2759 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2760 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002761#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763#endif
2764}
2765
Bram Moolenaar24552be2005-12-10 20:17:30 +00002766#if defined(USE_FNAME_CASE) || defined(PROTO)
2767/*
2768 * Set the case of the file name, if it already exists. This will cause the
2769 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002770 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002771 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002772 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002773fname_case(
2774 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002775 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002776{
2777 struct stat st;
2778 char_u *slash, *tail;
2779 DIR *dirp;
2780 struct dirent *dp;
2781
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002782 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002783 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002784 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002785 slash = vim_strrchr(name, '/');
2786 if (slash == NULL)
2787 {
2788 dirp = opendir(".");
2789 tail = name;
2790 }
2791 else
2792 {
2793 *slash = NUL;
2794 dirp = opendir((char *)name);
2795 *slash = '/';
2796 tail = slash + 1;
2797 }
2798
2799 if (dirp != NULL)
2800 {
2801 while ((dp = readdir(dirp)) != NULL)
2802 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002803 // Only accept names that differ in case and are the same byte
2804 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002805 if (STRICMP(tail, dp->d_name) == 0
2806 && STRLEN(tail) == STRLEN(dp->d_name))
2807 {
2808 char_u newname[MAXPATHL + 1];
2809 struct stat st2;
2810
Bram Moolenaar0f873732019-12-05 20:28:46 +01002811 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002812 vim_strncpy(newname, name, MAXPATHL);
2813 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2814 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002815 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002816 && st.st_ino == st2.st_ino
2817 && st.st_dev == st2.st_dev)
2818 {
2819 STRCPY(tail, dp->d_name);
2820 break;
2821 }
2822 }
2823 }
2824
2825 closedir(dirp);
2826 }
2827 }
2828}
2829#endif
2830
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831/*
2832 * Get file permissions for 'name'.
2833 * Returns -1 when it doesn't exist.
2834 */
2835 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002836mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837{
2838 struct stat statb;
2839
Bram Moolenaar0f873732019-12-05 20:28:46 +01002840 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841#ifdef VMS
2842 if (stat((char *)vms_fixfilename(name), &statb))
2843#else
2844 if (stat((char *)name, &statb))
2845#endif
2846 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002847#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002848 // The top bit makes the value negative, which means the file doesn't
2849 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002850 return statb.st_mode & ~S_ADDACE;
2851#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854}
2855
2856/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002857 * Set file permission for "name" to "perm".
2858 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 */
2860 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002861mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
2863 return (chmod((char *)
2864#ifdef VMS
2865 vms_fixfilename(name),
2866#else
2867 name,
2868#endif
2869 (mode_t)perm) == 0 ? OK : FAIL);
2870}
2871
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002872#if defined(HAVE_FCHMOD) || defined(PROTO)
2873/*
2874 * Set file permission for open file "fd" to "perm".
2875 * Return FAIL for failure, OK otherwise.
2876 */
2877 int
2878mch_fsetperm(int fd, long perm)
2879{
2880 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2881}
2882#endif
2883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884#if defined(HAVE_ACL) || defined(PROTO)
2885# ifdef HAVE_SYS_ACL_H
2886# include <sys/acl.h>
2887# endif
2888# ifdef HAVE_SYS_ACCESS_H
2889# include <sys/access.h>
2890# endif
2891
2892# ifdef HAVE_SOLARIS_ACL
2893typedef struct vim_acl_solaris_T {
2894 int acl_cnt;
2895 aclent_t *acl_entry;
2896} vim_acl_solaris_T;
2897# endif
2898
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002899#if defined(HAVE_SELINUX) || defined(PROTO)
2900/*
2901 * Copy security info from "from_file" to "to_file".
2902 */
2903 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002904mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002905{
2906 if (from_file == NULL)
2907 return;
2908
2909 if (selinux_enabled == -1)
2910 selinux_enabled = is_selinux_enabled();
2911
2912 if (selinux_enabled > 0)
2913 {
Bram Moolenaar89560232020-10-09 23:04:47 +02002914 // Use "char *" instead of "security_context_t" to avoid a deprecation
2915 // warning.
2916 char *from_context = NULL;
2917 char *to_context = NULL;
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002918
2919 if (getfilecon((char *)from_file, &from_context) < 0)
2920 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002921 // If the filesystem doesn't support extended attributes,
2922 // the original had no special security context and the
2923 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002924 if (errno == EOPNOTSUPP)
2925 return;
2926
Bram Moolenaar32526b32019-01-19 17:43:09 +01002927 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002928 msg_outtrans(from_file);
2929 msg_putchar('\n');
2930 return;
2931 }
2932 if (getfilecon((char *)to_file, &to_context) < 0)
2933 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002934 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002935 msg_outtrans(to_file);
2936 msg_putchar('\n');
2937 freecon (from_context);
2938 return ;
2939 }
2940 if (strcmp(from_context, to_context) != 0)
2941 {
2942 if (setfilecon((char *)to_file, from_context) < 0)
2943 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002944 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002945 msg_outtrans(to_file);
2946 msg_putchar('\n');
2947 }
2948 }
2949 freecon(to_context);
2950 freecon(from_context);
2951 }
2952}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002953#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002954
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002955#if defined(HAVE_SMACK) && !defined(PROTO)
2956/*
2957 * Copy security info from "from_file" to "to_file".
2958 */
2959 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002960mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002961{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002962 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002963 {
2964 XATTR_NAME_SMACK,
2965 XATTR_NAME_SMACKEXEC,
2966 XATTR_NAME_SMACKMMAP
2967 };
2968
2969 char buffer[SMACK_LABEL_LEN];
2970 const char *name;
2971 int index;
2972 int ret;
2973 ssize_t size;
2974
2975 if (from_file == NULL)
2976 return;
2977
2978 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2979 / sizeof(smack_copied_attributes)[0]) ; index++)
2980 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002981 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002982 name = smack_copied_attributes[index];
2983
Bram Moolenaar0f873732019-12-05 20:28:46 +01002984 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002985 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2986 if (size >= 0)
2987 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002988 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002989 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2990 if (ret < 0)
2991 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002992 vim_snprintf((char *)IObuff, IOSIZE,
2993 _("Could not set security context %s for %s"),
2994 name, to_file);
2995 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002996 msg_putchar('\n');
2997 }
2998 }
2999 else
3000 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003001 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003002 switch (errno)
3003 {
3004 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003005 // extended attributes aren't supported or enabled
3006 // should a message be echoed? not sure...
3007 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003008
3009 case ERANGE:
3010 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003011 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003012 vim_snprintf((char *)IObuff, IOSIZE,
3013 _("Could not get security context %s for %s. Removing it!"),
3014 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003015 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003016 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003017 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003018
3019 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003020 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003021 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003022 // Silently ignore errors, apparently this happens when
3023 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003024 break;
3025 }
3026 }
3027 }
3028}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003029#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003030
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031/*
3032 * Return a pointer to the ACL of file "fname" in allocated memory.
3033 * Return NULL if the ACL is not available for whatever reason.
3034 */
3035 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003036mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037{
3038 vim_acl_T ret = NULL;
3039#ifdef HAVE_POSIX_ACL
3040 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3041#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003042#ifdef HAVE_SOLARIS_ZFS_ACL
3043 acl_t *aclent;
3044
3045 if (acl_get((char *)fname, 0, &aclent) < 0)
3046 return NULL;
3047 ret = (vim_acl_T)aclent;
3048#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049#ifdef HAVE_SOLARIS_ACL
3050 vim_acl_solaris_T *aclent;
3051
3052 aclent = malloc(sizeof(vim_acl_solaris_T));
3053 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3054 {
3055 free(aclent);
3056 return NULL;
3057 }
3058 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3059 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3060 {
3061 free(aclent->acl_entry);
3062 free(aclent);
3063 return NULL;
3064 }
3065 ret = (vim_acl_T)aclent;
3066#else
3067#if defined(HAVE_AIX_ACL)
3068 int aclsize;
3069 struct acl *aclent;
3070
3071 aclsize = sizeof(struct acl);
3072 aclent = malloc(aclsize);
3073 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3074 {
3075 if (errno == ENOSPC)
3076 {
3077 aclsize = aclent->acl_len;
3078 aclent = realloc(aclent, aclsize);
3079 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3080 {
3081 free(aclent);
3082 return NULL;
3083 }
3084 }
3085 else
3086 {
3087 free(aclent);
3088 return NULL;
3089 }
3090 }
3091 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003092#endif // HAVE_AIX_ACL
3093#endif // HAVE_SOLARIS_ACL
3094#endif // HAVE_SOLARIS_ZFS_ACL
3095#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 return ret;
3097}
3098
3099/*
3100 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3101 */
3102 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003103mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104{
3105 if (aclent == NULL)
3106 return;
3107#ifdef HAVE_POSIX_ACL
3108 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3109#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003110#ifdef HAVE_SOLARIS_ZFS_ACL
3111 acl_set((char *)fname, (acl_t *)aclent);
3112#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113#ifdef HAVE_SOLARIS_ACL
3114 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3115 ((vim_acl_solaris_T *)aclent)->acl_entry);
3116#else
3117#ifdef HAVE_AIX_ACL
3118 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003119#endif // HAVE_AIX_ACL
3120#endif // HAVE_SOLARIS_ACL
3121#endif // HAVE_SOLARIS_ZFS_ACL
3122#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123}
3124
3125 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003126mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127{
3128 if (aclent == NULL)
3129 return;
3130#ifdef HAVE_POSIX_ACL
3131 acl_free((acl_t)aclent);
3132#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003133#ifdef HAVE_SOLARIS_ZFS_ACL
3134 acl_free((acl_t *)aclent);
3135#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136#ifdef HAVE_SOLARIS_ACL
3137 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3138 free(aclent);
3139#else
3140#ifdef HAVE_AIX_ACL
3141 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003142#endif // HAVE_AIX_ACL
3143#endif // HAVE_SOLARIS_ACL
3144#endif // HAVE_SOLARIS_ZFS_ACL
3145#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146}
3147#endif
3148
3149/*
3150 * Set hidden flag for "name".
3151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003153mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003155 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156}
3157
3158/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003159 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 * return FALSE if "name" is not a directory
3161 * return FALSE for error
3162 */
3163 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003164mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
3166 struct stat statb;
3167
Bram Moolenaar0f873732019-12-05 20:28:46 +01003168 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 return FALSE;
3170 if (stat((char *)name, &statb))
3171 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173}
3174
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003175/*
3176 * return TRUE if "name" is a directory, NOT a symlink to a directory
3177 * return FALSE if "name" is not a directory
3178 * return FALSE for error
3179 */
3180 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003181mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003182{
3183 struct stat statb;
3184
Bram Moolenaar0f873732019-12-05 20:28:46 +01003185 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003186 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003187 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003188 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003189 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003190}
3191
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192/*
3193 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3194 */
3195 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003196executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197{
3198 struct stat st;
3199
3200 if (stat((char *)name, &st))
3201 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003202#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003203 // Like on Unix system file can have executable rights but not necessarily
3204 // be an executable, but on Unix is not a default for an ordinary file to
3205 // have an executable flag - on VMS it is in most cases.
3206 // Therefore, this check does not have any sense - let keep us to the
3207 // conventions instead:
3208 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003209 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003210 int vms_executable = 0;
3211 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3212 {
3213 if (strstr(vms_tolower((char*)name),".exe") != NULL
3214 || strstr(vms_tolower((char*)name),".com")!= NULL)
3215 vms_executable = 1;
3216 }
3217 return vms_executable;
3218#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221}
3222
3223/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003224 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003225 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 * Return -1 if unknown.
3227 */
3228 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003229mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
3231 char_u *buf;
3232 char_u *p, *e;
3233 int retval;
3234
Bram Moolenaar0f873732019-12-05 20:28:46 +01003235 // When "use_path" is false and if it's an absolute or relative path don't
3236 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003237 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003238 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003239 // There must be a path separator, files in the current directory
3240 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003241 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003242 {
3243 if (path != NULL)
3244 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003245 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003246 *path = FullName_save(name, TRUE);
3247 else
3248 *path = vim_strsave(name);
3249 }
3250 return TRUE;
3251 }
3252 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
3255 p = (char_u *)getenv("PATH");
3256 if (p == NULL || *p == NUL)
3257 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003258 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 if (buf == NULL)
3260 return -1;
3261
3262 /*
3263 * Walk through all entries in $PATH to check if "name" exists there and
3264 * is an executable file.
3265 */
3266 for (;;)
3267 {
3268 e = (char_u *)strchr((char *)p, ':');
3269 if (e == NULL)
3270 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003271 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 STRCPY(buf, "./");
3273 else
3274 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003275 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 add_pathsep(buf);
3277 }
3278 STRCAT(buf, name);
3279 retval = executable_file(buf);
3280 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003281 {
3282 if (path != NULL)
3283 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003284 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003285 *path = FullName_save(buf, TRUE);
3286 else
3287 *path = vim_strsave(buf);
3288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
3292 if (*e != ':')
3293 break;
3294 p = e + 1;
3295 }
3296
3297 vim_free(buf);
3298 return retval;
3299}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300
3301/*
3302 * Check what "name" is:
3303 * NODE_NORMAL: file or directory (or doesn't exist)
3304 * NODE_WRITABLE: writable device, socket, fifo, etc.
3305 * NODE_OTHER: non-writable things
3306 */
3307 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003308mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 struct stat st;
3311
3312 if (stat((char *)name, &st))
3313 return NODE_NORMAL;
3314 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3315 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003316 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003318 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 return NODE_WRITABLE;
3320}
3321
3322 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003323mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
3325#ifdef HAVE_CHECK_STACK_GROWTH
3326 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 check_stack_growth((char *)&i);
3329
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003330# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 get_stack_limit();
3332# endif
3333
3334#endif
3335
3336 /*
3337 * Setup an alternative stack for signals. Helps to catch signals when
3338 * running out of stack space.
3339 * Use of sigaltstack() is preferred, it's more portable.
3340 * Ignore any errors.
3341 */
3342#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003343 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 init_signal_stack();
3345#endif
3346}
3347
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003348#if defined(EXITFREE) || defined(PROTO)
3349 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003350mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003351{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003352# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3353 if (clip_star.owned)
3354 clip_lose_selection(&clip_star);
3355 if (clip_plus.owned)
3356 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003357# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003358# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003359 if (xterm_Shell != (Widget)0)
3360 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003361# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003362 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003363 if (xterm_dpy != NULL)
3364 XtCloseDisplay(xterm_dpy);
3365 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003366 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003367 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003368# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003369 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003370# endif
3371 }
3372# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003373# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003374# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003375 if (x11_display != NULL
3376# ifdef FEAT_XCLIPBOARD
3377 && x11_display != xterm_dpy
3378# endif
3379 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003380 XCloseDisplay(x11_display);
3381# endif
3382# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003383 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003384# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003385 vim_free(oldtitle);
3386 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003387}
3388#endif
3389
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390/*
3391 * Output a newline when exiting.
3392 * Make sure the newline goes to the same stream as the text.
3393 */
3394 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003395exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003397 if (silent_mode)
3398 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 if (newline_on_exit || msg_didout)
3400 {
3401 if (msg_use_printf())
3402 {
3403 if (info_message)
3404 mch_msg("\n");
3405 else
3406 mch_errmsg("\r\n");
3407 }
3408 else
3409 out_char('\n');
3410 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003411 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003413 restore_cterm_colors(); // get original colors back
3414 msg_clr_eos_force(); // clear the rest of the display
3415 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
3417}
3418
Bram Moolenaarb4151682020-05-11 22:13:28 +02003419#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003420# if (defined(__GNUC__) \
3421 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3422 || (defined(__clang__) && (__clang_major__ >= 12))
3423extern void __gcov_dump(void);
3424extern void __gcov_reset(void);
3425# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3426# else
3427extern void __gcov_flush(void);
3428# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003429#endif
3430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003432mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 exiting = TRUE;
3435
3436#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3437 x11_export_final_selection();
3438#endif
3439
3440#ifdef FEAT_GUI
3441 if (!gui.in_use)
3442#endif
3443 {
3444 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003445 if (!is_not_a_term())
3446 {
3447 // restore xterm title and icon name
3448 mch_restore_title(SAVE_RESTORE_BOTH);
3449 term_pop_title(SAVE_RESTORE_BOTH);
3450 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003451
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 /*
3453 * When t_ti is not empty but it doesn't cause swapping terminal
3454 * pages, need to output a newline when msg_didout is set. But when
3455 * t_ti does swap pages it should not go to the shell page. Do this
3456 * before stoptermcap().
3457 */
3458 if (swapping_screen() && !newline_on_exit)
3459 exit_scroll();
3460
Bram Moolenaar0f873732019-12-05 20:28:46 +01003461 // Stop termcap: May need to check for T_CRV response, which
3462 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 stoptermcap();
3464
3465 /*
3466 * A newline is only required after a message in the alternate screen.
3467 * This is set to TRUE by wait_return().
3468 */
3469 if (!swapping_screen() || newline_on_exit)
3470 exit_scroll();
3471
Bram Moolenaar0f873732019-12-05 20:28:46 +01003472 // Cursor may have been switched off without calling starttermcap()
3473 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 if (full_screen)
3475 cursor_on();
3476 }
3477 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003478 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003479
3480#ifdef USE_GCOV_FLUSH
3481 // Flush coverage info before possibly being killed by a deadly signal.
3482 __gcov_flush();
3483#endif
3484
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 may_core_dump();
3486#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 gui_exit(r);
3489#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003490
Bram Moolenaar56718732006-03-15 22:53:57 +00003491#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003492 mac_conv_cleanup();
3493#endif
3494
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003496 // A core dump won't be created if the signal handler
3497 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 if (deadly_signal != 0)
3499 return;
3500#endif
3501
Bram Moolenaar009b2592004-10-24 19:18:58 +00003502#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003503 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003504#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003505
3506#ifdef EXITFREE
3507 free_all_mem();
3508#endif
3509
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 exit(r);
3511}
3512
3513 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003514may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
3516 if (deadly_signal != 0)
3517 {
3518 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003519 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521}
3522
3523#ifndef VMS
3524
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003525/*
3526 * Get the file descriptor to use for tty operations.
3527 */
3528 static int
3529get_tty_fd(int fd)
3530{
3531 int tty_fd = fd;
3532
3533#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3534 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3535 // "fd" when it is a master device.
3536 if (mch_isatty(fd) > 1)
3537 {
3538 char *name;
3539
3540 name = ptsname(fd);
3541 if (name == NULL)
3542 return -1;
3543
3544 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3545 if (tty_fd < 0)
3546 return -1;
3547 }
3548#endif
3549 return tty_fd;
3550}
3551
3552 static int
3553mch_tcgetattr(int fd, void *term)
3554{
3555 int tty_fd;
3556 int retval = -1;
3557
3558 tty_fd = get_tty_fd(fd);
3559 if (tty_fd >= 0)
3560 {
3561#ifdef NEW_TTY_SYSTEM
3562# ifdef HAVE_TERMIOS_H
3563 retval = tcgetattr(tty_fd, (struct termios *)term);
3564# else
3565 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3566# endif
3567#else
3568 // for "old" tty systems
3569 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3570#endif
3571 if (tty_fd != fd)
3572 close(tty_fd);
3573 }
3574 return retval;
3575}
3576
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003578mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
3580 static int first = TRUE;
3581
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003582#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583# ifdef HAVE_TERMIOS_H
3584 static struct termios told;
3585 struct termios tnew;
3586# else
3587 static struct termio told;
3588 struct termio tnew;
3589# endif
3590
3591 if (first)
3592 {
3593 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003594 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 }
3596
3597 tnew = told;
3598 if (tmode == TMODE_RAW)
3599 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003600 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003601 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3602 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003604# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003605 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606# endif
3607 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003608# ifdef ONLCR
3609 // Don't map NL -> CR NL, we do it ourselves.
3610 // Also disable expanding tabs if possible.
3611# ifdef XTABS
3612 tnew.c_oflag &= ~(ONLCR | XTABS);
3613# else
3614# ifdef TAB3
3615 tnew.c_oflag &= ~(ONLCR | TAB3);
3616# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003618# endif
3619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003621 tnew.c_cc[VMIN] = 1; // return after 1 char
3622 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003625 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003626 // Also reset ICANON here, otherwise on Solaris select() won't see
3627 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003628 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003629 tnew.c_cc[VMIN] = 1; // return after 1 char
3630 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
3633# if defined(HAVE_TERMIOS_H)
3634 {
3635 int n = 10;
3636
Bram Moolenaar0f873732019-12-05 20:28:46 +01003637 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3638 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3640 && errno == EINTR && n > 0)
3641 --n;
3642 }
3643# else
3644 ioctl(read_cmd_fd, TCSETA, &tnew);
3645# endif
3646
3647#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 /*
3649 * for "old" tty systems
3650 */
3651# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003652# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653# endif
3654 static struct sgttyb ttybold;
3655 struct sgttyb ttybnew;
3656
3657 if (first)
3658 {
3659 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003660 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
3662
3663 ttybnew = ttybold;
3664 if (tmode == TMODE_RAW)
3665 {
3666 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3667 ttybnew.sg_flags |= RAW;
3668 }
3669 else if (tmode == TMODE_SLEEP)
3670 ttybnew.sg_flags &= ~(ECHO);
3671 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3672#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003673 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674}
3675
3676/*
3677 * Try to get the code for "t_kb" from the stty setting
3678 *
3679 * Even if termcap claims a backspace key, the user's setting *should*
3680 * prevail. stty knows more about reality than termcap does, and if
3681 * somebody's usual erase key is DEL (which, for most BSD users, it will
3682 * be), they're going to get really annoyed if their erase key starts
3683 * doing forward deletes for no reason. (Eric Fischer)
3684 */
3685 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003686get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003688 ttyinfo_T info;
3689 char_u buf[2];
3690 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003692 if (get_tty_info(read_cmd_fd, &info) == OK)
3693 {
3694 intr_char = info.interrupt;
3695 buf[0] = info.backspace;
3696 buf[1] = NUL;
3697 add_termcode((char_u *)"kb", buf, FALSE);
3698
Bram Moolenaar0f873732019-12-05 20:28:46 +01003699 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003700 p = find_termcode((char_u *)"kD");
3701 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3702 do_fixdel(NULL);
3703 }
3704}
3705
3706/*
3707 * Obtain the characters that Backspace and Enter produce on "fd".
3708 * Returns OK or FAIL.
3709 */
3710 int
3711get_tty_info(int fd, ttyinfo_T *info)
3712{
3713#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714# ifdef HAVE_TERMIOS_H
3715 struct termios keys;
3716# else
3717 struct termio keys;
3718# endif
3719
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003720 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003722 info->backspace = keys.c_cc[VERASE];
3723 info->interrupt = keys.c_cc[VINTR];
3724 if (keys.c_iflag & ICRNL)
3725 info->enter = NL;
3726 else
3727 info->enter = CAR;
3728 if (keys.c_oflag & ONLCR)
3729 info->nl_does_cr = TRUE;
3730 else
3731 info->nl_does_cr = FALSE;
3732 return OK;
3733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003735 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 struct sgttyb keys;
3737
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003738 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003740 info->backspace = keys.sg_erase;
3741 info->interrupt = keys.sg_kill;
3742 info->enter = CAR;
3743 info->nl_does_cr = TRUE;
3744 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003747 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748}
3749
Bram Moolenaar0f873732019-12-05 20:28:46 +01003750#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003752static int mouse_ison = FALSE;
3753
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754/*
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003755 * Set mouse clicks on or off and possible enable mouse movement events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 */
3757 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003758mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003760#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003761 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 int xterm_mouse_vers;
3764
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003765#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003766 if (!on)
3767 // Make sure not tracing mouse movements. Important when a button-down
3768 // was received but no release yet.
3769 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003770#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003771
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003772 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003773#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003774 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003775#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003776 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003777 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 return;
3779
3780 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003781
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003782#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003783 if (ttym_flags == TTYM_URXVT)
3784 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003785 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003786 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003787 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003788#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003789
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003790 if (ttym_flags == TTYM_SGR)
3791 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003792 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003793 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003794 mouse_ison = on;
3795 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003796
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003797#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003798 if (bevalterm_ison != (p_bevalterm && on))
3799 {
3800 bevalterm_ison = (p_bevalterm && on);
3801 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003802 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003803 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003804 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003805#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 if (xterm_mouse_vers > 0)
3808 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003809 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 out_str_nf((char_u *)
3811 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003812 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003813#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003814 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003815#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003816 "\033[?1002h")
3817 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003818 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003820 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003821 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
3823
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003824#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 else if (ttym_flags == TTYM_DEC)
3826 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003827 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003829 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003831 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003835#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 else
3837 {
3838 if (on)
3839 {
3840 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003841 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843 else
3844 {
3845 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003846 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003851#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003852 else
3853 {
3854 if (on)
3855 {
3856 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003857 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003858 }
3859 else
3860 {
3861 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003862 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003863 }
3864 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003865#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003866
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003867#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 else
3869 {
3870 if (on)
3871 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003872 // D - Enable Mouse up/down messages
3873 // L - Enable Left Button Reporting
3874 // M - Enable Middle Button Reporting
3875 // R - Enable Right Button Reporting
3876 // K - Enable SHIFT and CTRL key Reporting
3877 // + - Enable Advanced messaging of mouse moves and up/down messages
3878 // Q - Quiet No Ack
3879 // # - Numeric value of mouse pointer required
3880 // 0 = Multiview 2000 cursor, used as standard
3881 // 1 = Windows Arrow
3882 // 2 = Windows I Beam
3883 // 3 = Windows Hour Glass
3884 // 4 = Windows Cross Hair
3885 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003886# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003887 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003888 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003889# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003890 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003891# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003892 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
3894 else
3895 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003896 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003897 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
3899 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003900#endif
3901#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 else
3903 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003904 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 if (on)
3906 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3907 else
3908 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003909 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912}
3913
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003914#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003915/*
3916 * Called when 'balloonevalterm' changed.
3917 */
3918 void
3919mch_bevalterm_changed(void)
3920{
3921 mch_setmouse(mouse_ison);
3922}
3923#endif
3924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925/*
3926 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3927 */
3928 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003929check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930{
3931# ifdef FEAT_MOUSE_XTERM
3932 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003933# ifdef FEAT_MOUSE_URXVT
3934 && use_xterm_mouse() != 3
3935# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936# ifdef FEAT_GUI
3937 && !gui.in_use
3938# endif
3939 )
3940 {
3941 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003942 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 if (*p_mouse != NUL)
3944 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003945 // force mouse off and maybe on to send possibly new mouse
3946 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 mch_setmouse(FALSE);
3948 setmouse();
3949 }
3950 }
3951 else
3952 del_mouse_termcode(KS_MOUSE);
3953# endif
3954
3955# ifdef FEAT_MOUSE_GPM
3956 if (!use_xterm_mouse()
3957# ifdef FEAT_GUI
3958 && !gui.in_use
3959# endif
3960 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003961 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003962 else
3963 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964# endif
3965
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003966# ifdef FEAT_SYSMOUSE
3967 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_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003973# endif
3974
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003976 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 if (!use_xterm_mouse()
3978# ifdef FEAT_GUI
3979 && !gui.in_use
3980# endif
3981 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003982 set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 else
3984 del_mouse_termcode(KS_JSBTERM_MOUSE);
3985# endif
3986
3987# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003988 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3989 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 if (!use_xterm_mouse()
3991# ifdef FEAT_GUI
3992 && !gui.in_use
3993# endif
3994 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003995 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 else
3997 del_mouse_termcode(KS_NETTERM_MOUSE);
3998# endif
3999
4000# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01004001 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004002 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003# ifdef FEAT_GUI
4004 && !gui.in_use
4005# endif
4006 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004007 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004008 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 else
4010 del_mouse_termcode(KS_DEC_MOUSE);
4011# endif
4012# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004013 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004014 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015# ifdef FEAT_GUI
4016 && !gui.in_use
4017# endif
4018 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004019 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 else
4021 del_mouse_termcode(KS_PTERM_MOUSE);
4022# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004023# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004024 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004025# ifdef FEAT_GUI
4026 && !gui.in_use
4027# endif
4028 )
4029 {
4030 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004031 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004032
4033 if (*p_mouse != NUL)
4034 {
4035 mch_setmouse(FALSE);
4036 setmouse();
4037 }
4038 }
4039 else
4040 del_mouse_termcode(KS_URXVT_MOUSE);
4041# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004042 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004043# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004044 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004045# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004046 )
4047 {
4048 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004049 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004050
4051 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004052 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004053
4054 if (*p_mouse != NUL)
4055 {
4056 mch_setmouse(FALSE);
4057 setmouse();
4058 }
4059 }
4060 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004061 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004062 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004063 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067#ifndef VMS
4068
4069/*
4070 * Try to get the current window size:
4071 * 1. with an ioctl(), most accurate method
4072 * 2. from the environment variables LINES and COLUMNS
4073 * 3. from the termcap
4074 * 4. keep using the old values
4075 * Return OK when size could be determined, FAIL otherwise.
4076 */
4077 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004078mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079{
4080 long rows = 0;
4081 long columns = 0;
4082 char_u *p;
4083
4084 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 * 1. try using an ioctl. It is the most accurate method.
4086 *
4087 * Try using TIOCGWINSZ first, some systems that have it also define
4088 * TIOCGSIZE but don't have a struct ttysize.
4089 */
4090# ifdef TIOCGWINSZ
4091 {
4092 struct winsize ws;
4093 int fd = 1;
4094
Bram Moolenaar0f873732019-12-05 20:28:46 +01004095 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 if (!isatty(fd) && isatty(read_cmd_fd))
4097 fd = read_cmd_fd;
4098 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4099 {
4100 columns = ws.ws_col;
4101 rows = ws.ws_row;
4102 }
4103 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004104# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105# ifdef TIOCGSIZE
4106 {
4107 struct ttysize ts;
4108 int fd = 1;
4109
Bram Moolenaar0f873732019-12-05 20:28:46 +01004110 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 if (!isatty(fd) && isatty(read_cmd_fd))
4112 fd = read_cmd_fd;
4113 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4114 {
4115 columns = ts.ts_cols;
4116 rows = ts.ts_lines;
4117 }
4118 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004119# endif // TIOCGSIZE
4120# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
4122 /*
4123 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004124 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4125 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004127 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
4129 if ((p = (char_u *)getenv("LINES")))
4130 rows = atoi((char *)p);
4131 if ((p = (char_u *)getenv("COLUMNS")))
4132 columns = atoi((char *)p);
4133 }
4134
4135#ifdef HAVE_TGETENT
4136 /*
4137 * 3. try reading "co" and "li" entries from termcap
4138 */
4139 if (columns == 0 || rows == 0)
4140 getlinecol(&columns, &rows);
4141#endif
4142
4143 /*
4144 * 4. If everything fails, use the old values
4145 */
4146 if (columns <= 0 || rows <= 0)
4147 return FAIL;
4148
4149 Rows = rows;
4150 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004151 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 return OK;
4153}
4154
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004155#if defined(FEAT_TERMINAL) || defined(PROTO)
4156/*
4157 * Report the windows size "rows" and "cols" to tty "fd".
4158 */
4159 int
4160mch_report_winsize(int fd, int rows, int cols)
4161{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004162 int tty_fd;
4163 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004164
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004165 tty_fd = get_tty_fd(fd);
4166 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004167 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004168# if defined(TIOCSWINSZ)
4169 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004170
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004171 ws.ws_col = cols;
4172 ws.ws_row = rows;
4173 ws.ws_xpixel = cols * 5;
4174 ws.ws_ypixel = rows * 10;
4175 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4176 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4177 retval == 0 ? "success" : "failed");
4178# elif defined(TIOCSSIZE)
4179 struct ttysize ts;
4180
4181 ts.ts_cols = cols;
4182 ts.ts_lines = rows;
4183 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4184 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4185 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004186# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004187 if (tty_fd != fd)
4188 close(tty_fd);
4189 }
4190 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004191}
4192#endif
4193
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194/*
4195 * Try to set the window size to Rows and Columns.
4196 */
4197 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004198mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199{
4200 if (*T_CWS)
4201 {
4202 /*
4203 * NOTE: if you get an error here that term_set_winsize() is
4204 * undefined, check the output of configure. It could probably not
4205 * find a ncurses, termcap or termlib library.
4206 */
4207 term_set_winsize((int)Rows, (int)Columns);
4208 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004209 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
4211}
4212
Bram Moolenaar0f873732019-12-05 20:28:46 +01004213#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
4215/*
4216 * Rows and/or Columns has changed.
4217 */
4218 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004219mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004221 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222}
4223
Bram Moolenaar205b8862011-09-07 15:04:31 +02004224/*
4225 * Wait for process "child" to end.
4226 * Return "child" if it exited properly, <= 0 on error.
4227 */
4228 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004229wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004230{
4231 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004232 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004233
4234 while (wait_pid != child)
4235 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004236 // When compiled with Python threads are probably used, in which case
4237 // wait() sometimes hangs for no obvious reason. Use waitpid()
4238 // instead and loop (like the GUI). Also needed for other interfaces,
4239 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004240# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004241 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004242# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004243 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004244# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004245 if (wait_pid == 0)
4246 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004247 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004248 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004249 if (++delay_msec > 10)
4250 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004251 continue;
4252 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004253 if (wait_pid <= 0
4254# ifdef ECHILD
4255 && errno == ECHILD
4256# endif
4257 )
4258 break;
4259 }
4260 return wait_pid;
4261}
4262
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004263#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004264/*
4265 * Set the environment for a child process.
4266 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004267 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004268set_child_environment(
4269 long rows,
4270 long columns,
4271 char *term,
4272 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004273{
4274# ifdef HAVE_SETENV
4275 char envbuf[50];
4276# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004277 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004278 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004279 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004280 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004281 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004282# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004283 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004284# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004285# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004286 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004287# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004288# endif
4289
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004290# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004291 setenv("TERM", term, 1);
4292 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004293 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004294 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004295 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004296 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004297 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004298 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004299 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004300# ifdef FEAT_TERMINAL
4301 if (is_terminal)
4302 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004303 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004304 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4305 }
4306# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004307# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004308 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004309# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004310# else
4311 /*
4312 * Putenv does not copy the string, it has to remain valid.
4313 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004314 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004315 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004316 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4317 putenv(envbuf_Term);
4318 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004319 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004320 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4321 putenv(envbuf_Lines);
4322 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4323 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004324 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004325 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004326 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004327# ifdef FEAT_TERMINAL
4328 if (is_terminal)
4329 {
4330 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004331 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004332 putenv(envbuf_Version);
4333 }
4334# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004335# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004336 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4337 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4338 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004339# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004340# endif
4341}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004342
4343 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004344set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004345{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004346 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004347}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004348#endif
4349
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004350#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004351/*
4352 * Open a PTY, with FD for the master and slave side.
4353 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004354 * When successful both file descriptors are stored and the allocated pty name
4355 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004356 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004357 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004358open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004359{
4360 char *tty_name;
4361
Bram Moolenaar59386482019-02-10 22:43:46 +01004362 if (name1 != NULL)
4363 *name1 = NULL;
4364 if (name2 != NULL)
4365 *name2 = NULL;
4366
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004367 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004368 if (*pty_master_fd >= 0)
4369 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004370 // Leaving out O_NOCTTY may lead to waitpid() always returning
4371 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4372 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004373#ifdef O_NOCTTY
4374 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4375#else
4376 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4377#endif
4378 if (*pty_slave_fd < 0)
4379 {
4380 close(*pty_master_fd);
4381 *pty_master_fd = -1;
4382 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004383 else
4384 {
4385 if (name1 != NULL)
4386 *name1 = vim_strsave((char_u *)tty_name);
4387 if (name2 != NULL)
4388 *name2 = vim_strsave((char_u *)tty_name);
4389 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004390 }
4391}
4392#endif
4393
Bram Moolenaarfae42832017-08-01 22:24:26 +02004394/*
4395 * Send SIGINT to a child process if "c" is an interrupt character.
4396 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004397 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004398may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4399{
4400# ifdef SIGINT
4401 if (c == Ctrl_C || c == intr_char)
4402 {
4403# ifdef HAVE_SETSID
4404 kill(-pid, SIGINT);
4405# else
4406 kill(0, SIGINT);
4407# endif
4408 if (wpid > 0)
4409 kill(wpid, SIGINT);
4410 }
4411# endif
4412}
4413
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004414#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004415
Bram Moolenaar0f873732019-12-05 20:28:46 +01004416/*
4417 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4418 * pointers, the last one is NULL.
4419 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4420 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004421 int
4422unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004423 char_u *cmd,
4424 char ***argvp,
4425 char_u **sh_tofree,
4426 char_u **shcf_tofree)
4427{
4428 char **argv = NULL;
4429 int argc;
4430
4431 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004432 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004433 return FAIL;
4434
4435 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4436 return FAIL;
4437 *argvp = argv;
4438
4439 if (cmd != NULL)
4440 {
4441 char_u *s;
4442 char_u *p;
4443
4444 if (extra_shell_arg != NULL)
4445 argv[argc++] = (char *)extra_shell_arg;
4446
Bram Moolenaar0f873732019-12-05 20:28:46 +01004447 // Break 'shellcmdflag' into white separated parts. This doesn't
4448 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004449 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004450 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004451 return FAIL;
4452 s = *shcf_tofree;
4453 p = p_shcf;
4454 while (*p != NUL)
4455 {
4456 argv[argc++] = (char *)s;
4457 while (*p && *p != ' ' && *p != TAB)
4458 *s++ = *p++;
4459 *s++ = NUL;
4460 p = skipwhite(p);
4461 }
4462
4463 argv[argc++] = (char *)cmd;
4464 }
4465 argv[argc] = NULL;
4466 return OK;
4467}
4468#endif
4469
4470#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4471/*
4472 * Use a terminal window to run a shell command in.
4473 */
4474 static int
4475mch_call_shell_terminal(
4476 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004477 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004478{
4479 jobopt_T opt;
4480 char **argv = NULL;
4481 char_u *tofree1 = NULL;
4482 char_u *tofree2 = NULL;
4483 int retval = -1;
4484 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004485 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004486 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004487 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004488
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004489 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004490 goto theend;
4491
4492 init_job_options(&opt);
4493 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4494 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004495 if (buf == NULL)
4496 goto theend;
4497
4498 job = term_getjob(buf->b_term);
4499 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004500
Bram Moolenaar0f873732019-12-05 20:28:46 +01004501 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004502 aucmd_prepbuf(&aco, buf);
4503
4504 clear_oparg(&oa);
4505 while (term_use_loop())
4506 {
4507 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4508 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004509 // If terminal_loop() returns OK we got a key that is handled
4510 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004511 if (terminal_loop(TRUE) == OK)
4512 normal_cmd(&oa, TRUE);
4513 }
4514 else
4515 normal_cmd(&oa, TRUE);
4516 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004517 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004518 ch_log(NULL, "system command finished");
4519
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004520 job_unref(job);
4521
Bram Moolenaar0f873732019-12-05 20:28:46 +01004522 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004523 aucmd_restbuf(&aco);
4524
4525 wait_return(TRUE);
4526 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4527
4528theend:
4529 vim_free(argv);
4530 vim_free(tofree1);
4531 vim_free(tofree2);
4532 return retval;
4533}
4534#endif
4535
4536#ifdef USE_SYSTEM
4537/*
4538 * Use system() to start the shell: simple but slow.
4539 */
4540 static int
4541mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004542 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004543 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544{
4545#ifdef VMS
4546 char *ifn = NULL;
4547 char *ofn = NULL;
4548#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004549 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004550 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004551 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
4553 out_flush();
4554
4555 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004556 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
Bram Moolenaar62b42182010-09-21 22:09:37 +02004558# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004559 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004560 loose_clipboard();
4561# endif
4562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 if (cmd == NULL)
4564 x = system((char *)p_sh);
4565 else
4566 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004567# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 if (ofn = strchr((char *)cmd, '>'))
4569 *ofn++ = '\0';
4570 if (ifn = strchr((char *)cmd, '<'))
4571 {
4572 char *p;
4573
4574 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004575 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 if (p)
4577 *p = '\0';
4578 }
4579 if (ofn)
4580 x = vms_sys((char *)cmd, ofn, ifn);
4581 else
4582 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004583# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004584 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004586 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 if (newcmd == NULL)
4588 x = 0;
4589 else
4590 {
4591 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4592 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4593 (char *)p_shcf,
4594 (char *)cmd);
4595 x = system((char *)newcmd);
4596 vim_free(newcmd);
4597 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004598# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
4600# ifdef VMS
4601 x = vms_sys_status(x);
4602# endif
4603 if (emsg_silent)
4604 ;
4605 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004606 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 else if (x && !(options & SHELL_SILENT))
4608 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004609 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 msg_outnum((long)x);
4611 msg_putchar('\n');
4612 }
4613
4614 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004615 {
4616 // The shell may have messed with the mode, always set it.
4617 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004618 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004621# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4622 restore_clipboard();
4623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004625}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
Bram Moolenaar0f873732019-12-05 20:28:46 +01004627#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
Bram Moolenaar0f873732019-12-05 20:28:46 +01004629# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4630 // 127, some shells use that already
4631# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632
Bram Moolenaar13568252018-03-16 20:46:58 +01004633/*
4634 * Don't use system(), use fork()/exec().
4635 */
4636 static int
4637mch_call_shell_fork(
4638 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004639 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004640{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004641 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004643 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 pid_t wait_pid = 0;
4645# ifdef HAVE_UNION_WAIT
4646 union wait status;
4647# else
4648 int status = -1;
4649# endif
4650 int retval = -1;
4651 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004652 char_u *tofree1 = NULL;
4653 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004655 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004656# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004658# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004659 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 int fd_fromshell[2];
4661 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004662 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
4664 out_flush();
4665 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004666 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004667 if (tmode == TMODE_RAW)
4668 // The shell may have messed with the mode, always set it later.
4669 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004671 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004672 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004673
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004675 * For the GUI, when writing the output into the buffer and when reading
4676 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4677 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004679 if ((options & (SHELL_READ|SHELL_WRITE))
4680# ifdef FEAT_GUI
4681 || (gui.in_use && show_shell_mess)
4682# endif
4683 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004685# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 /*
4687 * Try to open a master pty.
4688 * If this works, open the slave pty.
4689 * If the slave can't be opened, close the master pty.
4690 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004691 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004692 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 /*
4694 * If not opening a pty or it didn't work, try using pipes.
4695 */
4696 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
4699 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004700 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 {
4702 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004703 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 {
4705 close(fd_toshell[0]);
4706 close(fd_toshell[1]);
4707 }
4708 }
4709 if (pipe_error)
4710 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004711 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 out_flush();
4713 }
4714 }
4715 }
4716
Bram Moolenaar0f873732019-12-05 20:28:46 +01004717 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004719 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004720 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004721 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004722 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004724 UNBLOCK_SIGNALS(&curset);
4725
Bram Moolenaar32526b32019-01-19 17:43:09 +01004726 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004727 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004729 || (gui.in_use && show_shell_mess)
4730# endif
4731 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004733# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004734 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 {
4736 close(pty_master_fd);
4737 close(pty_slave_fd);
4738 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004739 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004740# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 {
4742 close(fd_toshell[0]);
4743 close(fd_toshell[1]);
4744 close(fd_fromshell[0]);
4745 close(fd_fromshell[1]);
4746 }
4747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004749 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004751 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004752 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753
Bram Moolenaar819524702018-02-27 19:10:00 +01004754# ifdef FEAT_JOB_CHANNEL
4755 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004756 {
4757 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004758 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004759 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004760# endif
4761
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 if (!show_shell_mess || (options & SHELL_EXPAND))
4763 {
4764 int fd;
4765
4766 /*
4767 * Don't want to show any message from the shell. Can't just
4768 * close stdout and stderr though, because some systems will
4769 * break if you try to write to them after that, so we must
4770 * use dup() to replace them with something else -- webb
4771 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4772 * waiting for input.
4773 */
4774 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4775 fclose(stdin);
4776 fclose(stdout);
4777 fclose(stderr);
4778
4779 /*
4780 * If any of these open()'s and dup()'s fail, we just continue
4781 * anyway. It's not fatal, and on most systems it will make
4782 * no difference at all. On a few it will cause the execvp()
4783 * to exit with a non-zero status even when the completion
4784 * could be done, which is nothing too serious. If the open()
4785 * or dup() failed we'd just do the same thing ourselves
4786 * anyway -- webb
4787 */
4788 if (fd >= 0)
4789 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004790 vim_ignored = dup(fd); // To replace stdin (fd 0)
4791 vim_ignored = dup(fd); // To replace stdout (fd 1)
4792 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
Bram Moolenaar0f873732019-12-05 20:28:46 +01004794 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 close(fd);
4796 }
4797 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004798 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004800 || gui.in_use
4801# endif
4802 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
4804
Bram Moolenaardf177f62005-02-22 08:39:57 +00004805# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004806 // Create our own process group, so that the child and all its
4807 // children can be kill()ed. Don't do this when using pipes,
4808 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004809 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004810 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004811 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004812# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004813 // When doing "!xterm&" and 'shell' is bash: the shell
4814 // will exit and send SIGHUP to all processes in its
4815 // group, killing the just started process. Ignore SIGHUP
4816 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004817 signal(SIGHUP, SIG_IGN);
4818# endif
4819 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004820# endif
4821# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004822 if (pty_slave_fd >= 0)
4823 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004824 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004825 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004826 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004827# ifdef TIOCSCTTY
4828 // Try to become controlling tty (probably doesn't work,
4829 // unless run by root)
4830 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4831# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004832 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004833# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004834 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835
Bram Moolenaara5792f52005-11-23 21:25:05 +00004836 /*
4837 * stderr is only redirected when using the GUI, so that a
4838 * program like gpg can still access the terminal to get a
4839 * passphrase using stderr.
4840 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004841# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 if (pty_master_fd >= 0)
4843 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004844 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
Bram Moolenaar0f873732019-12-05 20:28:46 +01004846 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004848 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004850 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004851 if (gui.in_use)
4852 {
4853 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004854 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856
Bram Moolenaar0f873732019-12-05 20:28:46 +01004857 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 }
4859 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004860# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004862 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 close(fd_toshell[1]);
4864 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004865 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 close(fd_toshell[0]);
4867
Bram Moolenaar0f873732019-12-05 20:28:46 +01004868 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 close(fd_fromshell[0]);
4870 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004871 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 close(fd_fromshell[1]);
4873
Bram Moolenaara5792f52005-11-23 21:25:05 +00004874# ifdef FEAT_GUI
4875 if (gui.in_use)
4876 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004877 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004878 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004879 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004880 }
4881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004884
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 /*
4886 * There is no type cast for the argv, because the type may be
4887 * different on different machines. This may cause a warning
4888 * message with strict compilers, don't worry about it.
4889 * Call _exit() instead of exit() to avoid closing the connection
4890 * to the X server (esp. with GTK, which uses atexit()).
4891 */
4892 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004893 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004895 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
4897 /*
4898 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004899 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 */
4901 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004902 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004903 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004904# ifdef FEAT_JOB_CHANNEL
4905 ++dont_check_job_ended;
4906# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 /*
4908 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004909 * This is also used to pipe stdin/stdout to/from the external
4910 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004912 if ((options & (SHELL_READ|SHELL_WRITE))
4913# ifdef FEAT_GUI
4914 || (gui.in_use && show_shell_mess)
4915# endif
4916 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004918# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004920 int buffer_off = 0; // valid bytes in buffer[]
4921 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4922 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 int len;
4924 int p_more_save;
4925 int old_State;
4926 int c;
4927 int toshell_fd;
4928 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004929 garray_T ga;
4930 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004931# ifdef ELAPSED_FUNC
4932 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004933# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934
Bram Moolenaardf177f62005-02-22 08:39:57 +00004935# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (pty_master_fd >= 0)
4937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 fromshell_fd = pty_master_fd;
4939 toshell_fd = dup(pty_master_fd);
4940 }
4941 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
4944 close(fd_toshell[0]);
4945 close(fd_fromshell[1]);
4946 toshell_fd = fd_toshell[1];
4947 fromshell_fd = fd_fromshell[0];
4948 }
4949
4950 /*
4951 * Write to the child if there are typed characters.
4952 * Read from the child if there are characters available.
4953 * Repeat the reading a few times if more characters are
4954 * available. Need to check for typed keys now and then, but
4955 * not too often (delays when no chars are available).
4956 * This loop is quit if no characters can be read from the pty
4957 * (WaitForChar detected special condition), or there are no
4958 * characters available and the child has exited.
4959 * Only check if the child has exited when there is no more
4960 * output. The child may exit before all the output has
4961 * been printed.
4962 *
4963 * Currently this busy loops!
4964 * This can probably dead-lock when the write blocks!
4965 */
4966 p_more_save = p_more;
4967 p_more = FALSE;
4968 old_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004969 State = MODE_EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004971 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004972 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004973 // Fork a process that will write the lines to the
4974 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004975 if ((wpid = fork()) == -1)
4976 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004977 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004978 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004979 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004980 {
4981 linenr_T lnum = curbuf->b_op_start.lnum;
4982 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004983 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004984 size_t l;
4985
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004986 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004987 for (;;)
4988 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004989 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004990 if (l == 0)
4991 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004992 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004993 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004994 len = write(toshell_fd, "", (size_t)1);
4995 else
4996 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004997 char_u *s = vim_strchr(lp + written, NL);
4998
Bram Moolenaar89d40322006-08-29 15:30:07 +00004999 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00005000 s == NULL ? l
5001 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005002 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00005003 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005004 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005005 // Finished a line, add a NL, unless this line
5006 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005007 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005008 || (!curbuf->b_p_bin
5009 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005010 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00005011 && (lnum !=
5012 curbuf->b_ml.ml_line_count
5013 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005014 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005015 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005016 ++lnum;
5017 if (lnum > curbuf->b_op_end.lnum)
5018 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005019 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005020 close(toshell_fd);
5021 toshell_fd = -1;
5022 break;
5023 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005024 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005025 written = 0;
5026 }
5027 else if (len > 0)
5028 written += len;
5029 }
5030 _exit(0);
5031 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005032 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005033 {
5034 close(toshell_fd);
5035 toshell_fd = -1;
5036 }
5037 }
5038
5039 if (options & SHELL_READ)
5040 ga_init2(&ga, 1, BUFLEN);
5041
5042 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005043# ifdef ELAPSED_FUNC
5044 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005045# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 for (;;)
5047 {
5048 /*
5049 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005050 * if there are any.
5051 * Don't do this if we are expanding wild cards (would eat
5052 * typeahead).
5053 * Don't do this when filtering and terminal is in cooked
5054 * mode, the shell command will handle the I/O. Avoids
5055 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005056 * Don't get characters when the child has already
5057 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005058 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005059 * while (noread_cnt > 4), avoids that ":r !ls" eats
5060 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 */
5062 len = 0;
5063 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005064 && ((options &
5065 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5066 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005067# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005068 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005069# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005070 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005071 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005072 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005074 if (ta_len == 0)
5075 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005076 // Get extra characters when we don't have any.
5077 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005078 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005079# ifdef ELAPSED_FUNC
5080 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005081# endif
5082 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5083 }
5084 if (ta_len > 0 || len > 0)
5085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 /*
5087 * For pipes:
5088 * Check for CTRL-C: send interrupt signal to child.
5089 * Check for CTRL-D: EOF, close pipe to child.
5090 */
5091 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5092 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 /*
5094 * Send SIGINT to the child's group or all
5095 * processes in our group.
5096 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005097 may_send_sigint(ta_buf[ta_len], pid, wpid);
5098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 if (pty_master_fd < 0 && toshell_fd >= 0
5100 && ta_buf[ta_len] == Ctrl_D)
5101 {
5102 close(toshell_fd);
5103 toshell_fd = -1;
5104 }
5105 }
5106
Bram Moolenaarf4140482020-02-15 23:06:45 +01005107 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
5109 /*
5110 * For pipes: echo the typed characters.
5111 * For a pty this does not seem to work.
5112 */
5113 if (pty_master_fd < 0)
5114 {
5115 for (i = ta_len; i < ta_len + len; ++i)
5116 {
5117 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5118 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 else if (has_mbyte)
5120 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005121 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122
5123 msg_outtrans_len(ta_buf + i, l);
5124 i += l - 1;
5125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 else
5127 msg_outtrans_len(ta_buf + i, 1);
5128 }
5129 windgoto(msg_row, msg_col);
5130 out_flush();
5131 }
5132
5133 ta_len += len;
5134
5135 /*
5136 * Write the characters to the child, unless EOF has
5137 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005138 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005139 * When writing buffer lines, drop the typed
5140 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005142 if (options & SHELL_WRITE)
5143 ta_len = 0;
5144 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
5146 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5147 if (len > 0)
5148 {
5149 ta_len -= len;
5150 mch_memmove(ta_buf, ta_buf + len, ta_len);
5151 }
5152 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
5155
Bram Moolenaardf177f62005-02-22 08:39:57 +00005156 if (got_int)
5157 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005158 // CTRL-C sends a signal to the child, we ignore it
5159 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005160# ifdef HAVE_SETSID
5161 kill(-pid, SIGINT);
5162# else
5163 kill(0, SIGINT);
5164# endif
5165 if (wpid > 0)
5166 kill(wpid, SIGINT);
5167 got_int = FALSE;
5168 }
5169
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 /*
5171 * Check if the child has any characters to be printed.
5172 * Read them and write them to our window. Repeat this as
5173 * long as there is something to do, avoid the 10ms wait
5174 * for mch_inchar(), or sending typeahead characters to
5175 * the external process.
5176 * TODO: This should handle escape sequences, compatible
5177 * to some terminal (vt52?).
5178 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005179 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005180 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005182 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005185 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005187
5188 noread_cnt = 0;
5189 if (options & SHELL_READ)
5190 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005191 // Do NUL -> NL translation, append NL separated
5192 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005193 for (i = 0; i < len; ++i)
5194 {
5195 if (buffer[i] == NL)
5196 append_ga_line(&ga);
5197 else if (buffer[i] == NUL)
5198 ga_append(&ga, NL);
5199 else
5200 ga_append(&ga, buffer[i]);
5201 }
5202 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005203 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
5205 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005206 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207
Bram Moolenaardf177f62005-02-22 08:39:57 +00005208 len += buffer_off;
5209 buffer[len] = NUL;
5210
Bram Moolenaar0f873732019-12-05 20:28:46 +01005211 // Check if the last character in buffer[] is
5212 // incomplete, keep these bytes for the next
5213 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 for (p = buffer; p < buffer + len; p += l)
5215 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005216 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005218 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 else if (MB_BYTE2LEN(*p) != l)
5220 break;
5221 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005222 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005224 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 if (len >= 12)
5226 ++p;
5227 else
5228 {
5229 buffer_off = len;
5230 continue;
5231 }
5232 }
5233 c = *p;
5234 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005235 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 if (p < buffer + len)
5237 {
5238 *p = c;
5239 buffer_off = (buffer + len) - p;
5240 mch_memmove(buffer, p, buffer_off);
5241 continue;
5242 }
5243 buffer_off = 0;
5244 }
5245 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 {
5247 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005248 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250
5251 windgoto(msg_row, msg_col);
5252 cursor_on();
5253 out_flush();
5254 if (got_int)
5255 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005256
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005257# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005258 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005259 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005260 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005261
Bram Moolenaar0f873732019-12-05 20:28:46 +01005262 // Avoid that we keep looping here without
5263 // checking for a CTRL-C for a long time. Don't
5264 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005265 if (msec > 2000)
5266 {
5267 noread_cnt = 5;
5268 break;
5269 }
5270 }
5271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
5273
Bram Moolenaar0f873732019-12-05 20:28:46 +01005274 // If we already detected the child has finished, continue
5275 // reading output for a short while. Some text may be
5276 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005277 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005278 {
5279 if (noread_cnt < 5)
5280 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005281 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005282 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005283
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 /*
5285 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005286 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005288# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005289 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005290# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005292# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5294 || (wait_pid == pid && WIFEXITED(status)))
5295 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005296 // Don't break the loop yet, try reading more
5297 // characters from "fromshell_fd" first. When using
5298 // pipes there might still be something to read and
5299 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005302 else
5303 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005304
Bram Moolenaar95a51352013-03-21 22:53:50 +01005305# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005306 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005307 clip_update();
5308# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
5310finished:
5311 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005312 if (options & SHELL_READ)
5313 {
5314 if (ga.ga_len > 0)
5315 {
5316 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005317 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005318 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005319 }
5320 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005321 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005322 ga_clear(&ga);
5323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 /*
5326 * Give all typeahead that wasn't used back to ui_inchar().
5327 */
5328 if (ta_len)
5329 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 State = old_State;
5331 if (toshell_fd >= 0)
5332 close(toshell_fd);
5333 close(fromshell_fd);
5334 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005335# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005336 else
5337 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005338 long delay_msec = 1;
5339
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005340 if (tmode == TMODE_RAW)
5341 // possibly disables modifyOtherKeys, so that the system
5342 // can recognize CTRL-C
5343 out_str(T_CTE);
Bram Moolenaar0981c872020-08-23 14:28:37 +02005344
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005345 /*
5346 * Similar to the loop above, but only handle X events, no
5347 * I/O.
5348 */
5349 for (;;)
5350 {
5351 if (got_int)
5352 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005353 // CTRL-C sends a signal to the child, we ignore it
5354 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005355# ifdef HAVE_SETSID
5356 kill(-pid, SIGINT);
5357# else
5358 kill(0, SIGINT);
5359# endif
5360 got_int = FALSE;
5361 }
5362# ifdef __NeXT__
5363 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5364# else
5365 wait_pid = waitpid(pid, &status, WNOHANG);
5366# endif
5367 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5368 || (wait_pid == pid && WIFEXITED(status)))
5369 {
5370 wait_pid = pid;
5371 break;
5372 }
5373
Bram Moolenaar0f873732019-12-05 20:28:46 +01005374 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005375 clip_update();
5376
Bram Moolenaar0f873732019-12-05 20:28:46 +01005377 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005378 // less time, gradually wait longer.
5379 mch_delay(delay_msec,
5380 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005381 if (++delay_msec > 10)
5382 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005383 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005384
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005385 if (tmode == TMODE_RAW)
5386 // possibly enables modifyOtherKeys again
5387 out_str(T_CTI);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005388 }
5389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
5391 /*
5392 * Wait until our child has exited.
5393 * Ignore wait() returning pids of other children and returning
5394 * because of some signal like SIGWINCH.
5395 * Don't wait if wait_pid was already set above, indicating the
5396 * child already exited.
5397 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005398 if (wait_pid != pid)
5399 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
Bram Moolenaar624891f2010-10-13 16:22:09 +02005401# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005402 // Close slave side of pty. Only do this after the child has
5403 // exited, otherwise the child may hang when it tries to write on
5404 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005405 if (pty_master_fd >= 0)
5406 close(pty_slave_fd);
5407# endif
5408
Bram Moolenaar0f873732019-12-05 20:28:46 +01005409 // Make sure the child that writes to the external program is
5410 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005411 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005412 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005413 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005414 wait4pid(wpid, NULL);
5415 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005416
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005417# ifdef FEAT_JOB_CHANNEL
5418 --dont_check_job_ended;
5419# endif
5420
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 /*
5422 * Set to raw mode right now, otherwise a CTRL-C after
5423 * catch_signals() will kill Vim.
5424 */
5425 if (tmode == TMODE_RAW)
5426 settmode(TMODE_RAW);
5427 did_settmode = TRUE;
5428 set_signals();
5429
5430 if (WIFEXITED(status))
5431 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005432 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005434 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 {
5436 if (retval == EXEC_FAILED)
5437 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005438 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 msg_outtrans(p_sh);
5440 msg_putchar('\n');
5441 }
5442 else if (!(options & SHELL_SILENT))
5443 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005444 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 msg_outnum((long)retval);
5446 msg_putchar('\n');
5447 }
5448 }
5449 }
5450 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005451 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 }
5453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
5455error:
5456 if (!did_settmode)
5457 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005458 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005460 vim_free(argv);
5461 vim_free(tofree1);
5462 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463
5464 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005465}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005466#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005467
5468 int
5469mch_call_shell(
5470 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005471 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005472{
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01005473#ifdef FEAT_JOB_CHANNEL
5474 ch_log(NULL, "executing shell command: %s", cmd);
5475#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005476#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5477 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5478 return mch_call_shell_terminal(cmd, options);
5479#endif
5480#ifdef USE_SYSTEM
5481 return mch_call_shell_system(cmd, options);
5482#else
5483 return mch_call_shell_fork(cmd, options);
5484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485}
5486
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005487#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005488 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005489mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005490{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005491 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005492 int fd_in[2] = {-1, -1}; // for stdin
5493 int fd_out[2] = {-1, -1}; // for stdout
5494 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005495 int pty_master_fd = -1;
5496 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005497 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005498 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5499 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5500 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005501 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005502 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5503 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005504 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005505 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005506 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005507
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005508 if (use_out_for_err && use_null_for_out)
5509 use_null_for_err = TRUE;
5510
Bram Moolenaar0f873732019-12-05 20:28:46 +01005511 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005512 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005513
Bram Moolenaarb2412082017-08-20 18:09:14 +02005514 if (options->jo_pty
5515 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005516 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005517 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005518 open_pty(&pty_master_fd, &pty_slave_fd,
5519 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005520
Bram Moolenaar0f873732019-12-05 20:28:46 +01005521 // TODO: without the channel feature connect the child to /dev/null?
5522 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005523 if (use_file_for_in)
5524 {
5525 char_u *fname = options->jo_io_name[PART_IN];
5526
5527 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5528 if (fd_in[0] < 0)
5529 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005530 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005531 goto failed;
5532 }
5533 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005534 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005535 // When writing buffer lines to the input don't use the pty, so that
5536 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005537 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5538 && pipe(fd_in) < 0)
5539 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005540
5541 if (use_file_for_out)
5542 {
5543 char_u *fname = options->jo_io_name[PART_OUT];
5544
5545 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5546 if (fd_out[1] < 0)
5547 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005548 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005549 goto failed;
5550 }
5551 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005552 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005553 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005554
5555 if (use_file_for_err)
5556 {
5557 char_u *fname = options->jo_io_name[PART_ERR];
5558
5559 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5560 if (fd_err[1] < 0)
5561 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005562 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005563 goto failed;
5564 }
5565 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005566 else if (!use_out_for_err && !use_null_for_err
5567 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005568 goto failed;
5569
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005570 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5571 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005572 if (options->jo_set & JO_CHANNEL)
5573 {
5574 channel = options->jo_channel;
5575 if (channel != NULL)
5576 ++channel->ch_refcount;
5577 }
5578 else
5579 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005580 if (channel == NULL)
5581 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005582 if (job->jv_tty_out != NULL)
5583 ch_log(channel, "using pty %s on fd %d",
5584 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005585 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005586
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005587 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005588 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005589 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005590 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005591 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005592 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005593 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005594 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005595 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005596 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005597 int null_fd = -1;
5598 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005599
Bram Moolenaar0f873732019-12-05 20:28:46 +01005600 // child
5601 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005602 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005603
Bram Moolenaar819524702018-02-27 19:10:00 +01005604# ifdef FEAT_JOB_CHANNEL
5605 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005606 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005607 ch_logfile((char_u *)"", (char_u *)"");
5608# endif
5609
Bram Moolenaar835dc632016-02-07 14:27:38 +01005610# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005611 // Create our own process group, so that the child and all its
5612 // children can be kill()ed. Don't do this when using pipes,
5613 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005614 (void)setsid();
5615# endif
5616
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005617# ifdef FEAT_TERMINAL
5618 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005619 {
5620 char *term = (char *)T_NAME;
5621
5622#ifdef FEAT_GUI
5623 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005624 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005625 term = getenv("TERM");
5626#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005627 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005628 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005629 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005630 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005631 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005632 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005633 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005634 else if (t_colors > 16)
5635 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005636 else
5637 term = "xterm";
5638 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005639 set_child_environment(
5640 (long)options->jo_term_rows,
5641 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005642 term,
5643 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005644 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005645 else
5646# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005647 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005648
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005649 if (options->jo_env != NULL)
5650 {
5651 dict_T *dict = options->jo_env;
5652 hashitem_T *hi;
5653 int todo = (int)dict->dv_hashtab.ht_used;
5654
5655 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5656 if (!HASHITEM_EMPTY(hi))
5657 {
5658 typval_T *item = &dict_lookup(hi)->di_tv;
5659
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005660 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005661 --todo;
5662 }
5663 }
5664
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005665 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005666 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005667 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005668 if (null_fd < 0)
5669 {
5670 perror("opening /dev/null failed");
5671 _exit(OPEN_NULL_FAILED);
5672 }
5673 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005674
Bram Moolenaar223896d2017-08-02 22:33:28 +02005675 if (pty_slave_fd >= 0)
5676 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005677 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005678 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005679# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005680 // Try to become controlling tty (probably doesn't work,
5681 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005682 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5683# endif
5684 }
5685
Bram Moolenaar0f873732019-12-05 20:28:46 +01005686 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005687 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005688 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005689 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005690 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005691 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005692 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005693 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005694
Bram Moolenaar0f873732019-12-05 20:28:46 +01005695 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005696 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005697 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005698 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005699 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005700 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005701 }
5702 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005703 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005704 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005705 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005706 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005707 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005708
Bram Moolenaar0f873732019-12-05 20:28:46 +01005709 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005710 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005711 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005712 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005713 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005714 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005715 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005716 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005717
5718 if (fd_in[0] >= 0)
5719 close(fd_in[0]);
5720 if (fd_in[1] >= 0)
5721 close(fd_in[1]);
5722 if (fd_out[0] >= 0)
5723 close(fd_out[0]);
5724 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005725 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005726 if (fd_err[0] >= 0)
5727 close(fd_err[0]);
5728 if (fd_err[1] >= 0)
5729 close(fd_err[1]);
5730 if (pty_master_fd >= 0)
5731 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005732 close(pty_master_fd); // not used in the child
5733 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005734 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005735
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005736 if (null_fd >= 0)
5737 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005738
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005739 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5740 _exit(EXEC_FAILED);
5741
Bram Moolenaar0f873732019-12-05 20:28:46 +01005742 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005743 execvp(argv[0], argv);
5744
Bram Moolenaar4694a172016-04-21 14:05:23 +02005745 if (stderr_works)
5746 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005747# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005748 // calling free_all_mem() here causes problems. Ignore valgrind
5749 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005750# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005751 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005752 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005753
Bram Moolenaar0f873732019-12-05 20:28:46 +01005754 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005755 UNBLOCK_SIGNALS(&curset);
5756
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005757 job->jv_pid = pid;
5758 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005759 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005760
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005761 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005762 close(pty_slave_fd); // not used in the parent
5763 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005764 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005765 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005766 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005767 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005768 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005769 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005770 if (channel != NULL)
5771 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005772 int in_fd = INVALID_FD;
5773 int out_fd = INVALID_FD;
5774 int err_fd = INVALID_FD;
5775
5776 if (!(use_file_for_in || use_null_for_in))
5777 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5778
5779 if (!(use_file_for_out || use_null_for_out))
5780 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5781
5782 // When using pty_master_fd only set it for stdout, do not duplicate
5783 // it for stderr, it only needs to be read once.
5784 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5785 {
5786 if (fd_err[0] >= 0)
5787 err_fd = fd_err[0];
5788 else if (out_fd != pty_master_fd)
5789 err_fd = pty_master_fd;
5790 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005791
5792 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005793 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005794 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005795 else
5796 {
5797 if (fd_in[1] >= 0)
5798 close(fd_in[1]);
5799 if (fd_out[0] >= 0)
5800 close(fd_out[0]);
5801 if (fd_err[0] >= 0)
5802 close(fd_err[0]);
5803 if (pty_master_fd >= 0)
5804 close(pty_master_fd);
5805 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005806
Bram Moolenaar0f873732019-12-05 20:28:46 +01005807 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005808 return;
5809
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005810failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005811 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005812 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005813 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005814 if (fd_in[1] >= 0)
5815 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005816 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005817 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005818 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005819 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005820 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005821 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005822 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005823 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005824 if (pty_master_fd >= 0)
5825 close(pty_master_fd);
5826 if (pty_slave_fd >= 0)
5827 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005828}
5829
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005830 static char_u *
5831get_signal_name(int sig)
5832{
5833 int i;
5834 char_u numbuf[NUMBUFLEN];
5835
5836 if (sig == SIGKILL)
5837 return vim_strsave((char_u *)"kill");
5838
5839 for (i = 0; signal_info[i].sig != -1; i++)
5840 if (sig == signal_info[i].sig)
5841 return strlow_save((char_u *)signal_info[i].name);
5842
5843 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5844 return vim_strsave(numbuf);
5845}
5846
Bram Moolenaar835dc632016-02-07 14:27:38 +01005847 char *
5848mch_job_status(job_T *job)
5849{
5850# ifdef HAVE_UNION_WAIT
5851 union wait status;
5852# else
5853 int status = -1;
5854# endif
5855 pid_t wait_pid = 0;
5856
5857# ifdef __NeXT__
5858 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5859# else
5860 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5861# endif
5862 if (wait_pid == -1)
5863 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005864 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005865 if (job->jv_status < JOB_ENDED)
5866 ch_log(job->jv_channel, "Job no longer exists: %s",
5867 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005868 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005869 }
5870 if (wait_pid == 0)
5871 return "run";
5872 if (WIFEXITED(status))
5873 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005874 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005875 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005876 if (job->jv_status < JOB_ENDED)
5877 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005878 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005879 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005880 if (WIFSIGNALED(status))
5881 {
5882 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005883 job->jv_termsig = get_signal_name(WTERMSIG(status));
5884 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5885 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5886 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005887 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005888 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005889 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005890
5891return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005892 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005893 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005894 return "dead";
5895}
5896
5897 job_T *
5898mch_detect_ended_job(job_T *job_list)
5899{
5900# ifdef HAVE_UNION_WAIT
5901 union wait status;
5902# else
5903 int status = -1;
5904# endif
5905 pid_t wait_pid = 0;
5906 job_T *job;
5907
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005908# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005909 // Do not do this when waiting for a shell command to finish, we would get
5910 // the exit value here (and discard it), the exit value obtained there
5911 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005912 if (dont_check_job_ended > 0)
5913 return NULL;
5914# endif
5915
Bram Moolenaar97792de2016-10-15 18:36:49 +02005916# ifdef __NeXT__
5917 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5918# else
5919 wait_pid = waitpid(-1, &status, WNOHANG);
5920# endif
5921 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005922 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005923 return NULL;
5924 for (job = job_list; job != NULL; job = job->jv_next)
5925 {
5926 if (job->jv_pid == wait_pid)
5927 {
5928 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005929 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005930 job->jv_exitval = WEXITSTATUS(status);
5931 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005932 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005933 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005934 job->jv_termsig = get_signal_name(WTERMSIG(status));
5935 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005936 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005937 {
5938 ch_log(job->jv_channel, "Job ended");
5939 job->jv_status = JOB_ENDED;
5940 }
5941 return job;
5942 }
5943 }
5944 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005945}
5946
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005947/*
5948 * Send a (deadly) signal to "job".
5949 * Return FAIL if "how" is not a valid name.
5950 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005951 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005952mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005953{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005954 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005955
Bram Moolenaar923d9262016-02-25 20:56:01 +01005956 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005957 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005958 else if (STRCMP(how, "hup") == 0)
5959 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005960 else if (STRCMP(how, "quit") == 0)
5961 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005962 else if (STRCMP(how, "int") == 0)
5963 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005964 else if (STRCMP(how, "kill") == 0)
5965 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005966#ifdef SIGWINCH
5967 else if (STRCMP(how, "winch") == 0)
5968 sig = SIGWINCH;
5969#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005970 else if (isdigit(*how))
5971 sig = atoi((char *)how);
5972 else
5973 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005974
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005975 // Never kill ourselves!
5976 if (job->jv_pid != 0)
5977 {
5978 // TODO: have an option to only kill the process, not the group?
5979 kill(-job->jv_pid, sig);
5980 kill(job->jv_pid, sig);
5981 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005982
Bram Moolenaar835dc632016-02-07 14:27:38 +01005983 return OK;
5984}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005985
5986/*
5987 * Clear the data related to "job".
5988 */
5989 void
5990mch_clear_job(job_T *job)
5991{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005992 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005993# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005994 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005995# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005996 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005997# endif
5998}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005999#endif
6000
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006001#if defined(FEAT_TERMINAL) || defined(PROTO)
6002 int
6003mch_create_pty_channel(job_T *job, jobopt_T *options)
6004{
6005 int pty_master_fd = -1;
6006 int pty_slave_fd = -1;
6007 channel_T *channel;
6008
Bram Moolenaar59386482019-02-10 22:43:46 +01006009 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02006010 if (pty_master_fd < 0 || pty_slave_fd < 0)
6011 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006012 close(pty_slave_fd);
6013
6014 channel = add_channel();
6015 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006016 {
6017 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006018 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006019 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006020 if (job->jv_tty_out != NULL)
6021 ch_log(channel, "using pty %s on fd %d",
6022 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006023 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006024 channel->ch_keep_open = TRUE;
6025
Bram Moolenaar0f873732019-12-05 20:28:46 +01006026 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6027 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006028 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006029 channel_set_job(channel, job, options);
6030 return OK;
6031}
6032#endif
6033
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034/*
6035 * Check for CTRL-C typed by reading all available characters.
6036 * In cooked mode we should get SIGINT, no need to check.
6037 */
6038 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006039mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006041 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006042 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 fill_input_buf(FALSE);
6044}
6045
6046/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006047 * Wait "msec" msec until a character is available from the mouse, keyboard,
6048 * from inbuf[].
6049 * "msec" == -1 will block forever.
6050 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006051 * When "ignore_input" is TRUE even check for pending input when input is
6052 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006053 * "interrupted" (if not NULL) is set to TRUE when no character is available
6054 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006055 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006056 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 */
6058 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006059WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006061#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006062 return ui_wait_for_chars_or_timer(
6063 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006064#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006065 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006066#endif
6067}
6068
6069/*
6070 * Wait "msec" msec until a character is available from the mouse or keyboard
6071 * or from inbuf[].
6072 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006073 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006074 * "interrupted" (if not NULL) is set to TRUE when no character is available
6075 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006076 * When a GUI is being used, this will never get called -- webb
6077 */
6078 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006079WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006080{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081#ifdef FEAT_MOUSE_GPM
6082 int gpm_process_wanted;
6083#endif
6084#ifdef FEAT_XCLIPBOARD
6085 int rest;
6086#endif
6087 int avail;
6088
Bram Moolenaar0f873732019-12-05 20:28:46 +01006089 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 return 1;
6091
6092#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006093 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 if (WantQueryMouse)
6095 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006096 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006097 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006098 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 }
6100#endif
6101
6102 /*
6103 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6104 * events. This is a bit complicated, because they might both be defined.
6105 */
6106#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6107# ifdef FEAT_XCLIPBOARD
6108 rest = 0;
6109 if (do_xterm_trace())
6110 rest = msec;
6111# endif
6112 do
6113 {
6114# ifdef FEAT_XCLIPBOARD
6115 if (rest != 0)
6116 {
6117 msec = XT_TRACE_DELAY;
6118 if (rest >= 0 && rest < XT_TRACE_DELAY)
6119 msec = rest;
6120 if (rest >= 0)
6121 rest -= msec;
6122 }
6123# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006124# ifdef FEAT_SOUND_CANBERRA
6125 // Invoke any pending sound callbacks.
6126 if (has_sound_callback_in_queue())
6127 invoke_sound_callback();
6128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129# ifdef FEAT_MOUSE_GPM
6130 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006131 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006132 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006133 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006135 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006137# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006139 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 return 1;
6141# ifdef FEAT_XCLIPBOARD
6142 if (rest == 0 || !do_xterm_trace())
6143# endif
6144 break;
6145 }
6146 }
6147 while (FALSE
6148# ifdef FEAT_MOUSE_GPM
6149 || (gpm_process_wanted && mch_gpm_process() == 0)
6150# endif
6151# ifdef FEAT_XCLIPBOARD
6152 || (!avail && rest != 0)
6153# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006154 )
6155 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
6157#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006158 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159#endif
6160 return avail;
6161}
6162
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006163#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164/*
6165 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006166 * "msec" == 0 will check for characters once.
6167 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006170 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006171 * "interrupted" (if not NULL) is set to TRUE when no character is available
6172 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006174 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006175RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176{
6177 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006178 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006179#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 static int busy = FALSE;
6181
Bram Moolenaar0f873732019-12-05 20:28:46 +01006182 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183# define MAY_LOOP
6184
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006185# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006186 // Remember at what time we started, so that we know how much longer we
6187 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006188 long start_msec = msec;
6189 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006191 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006192 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193# endif
6194
Bram Moolenaar0f873732019-12-05 20:28:46 +01006195 // Handle being called recursively. This may happen for the session
6196 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 if (busy)
6198 return 0;
6199#endif
6200
6201#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006202 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203#endif
6204 {
6205#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006206 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006207# ifdef FEAT_MZSCHEME
6208 int mzquantum_used = FALSE;
6209# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210#endif
6211#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006212 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006213 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 int nfd;
6215# ifdef FEAT_XCLIPBOARD
6216 int xterm_idx = -1;
6217# endif
6218# ifdef FEAT_MOUSE_GPM
6219 int gpm_idx = -1;
6220# endif
6221# ifdef USE_XSMP
6222 int xsmp_idx = -1;
6223# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006224 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006226# ifdef FEAT_MZSCHEME
6227 mzvim_check_threads();
6228 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6229 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006230 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006231 mzquantum_used = TRUE;
6232 }
6233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 fds[0].fd = fd;
6235 fds[0].events = POLLIN;
6236 nfd = 1;
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006239 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 if (xterm_Shell != (Widget)0)
6241 {
6242 xterm_idx = nfd;
6243 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6244 fds[nfd].events = POLLIN;
6245 nfd++;
6246 }
6247# endif
6248# ifdef FEAT_MOUSE_GPM
6249 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6250 {
6251 gpm_idx = nfd;
6252 fds[nfd].fd = gpm_fd;
6253 fds[nfd].events = POLLIN;
6254 nfd++;
6255 }
6256# endif
6257# ifdef USE_XSMP
6258 if (xsmp_icefd != -1)
6259 {
6260 xsmp_idx = nfd;
6261 fds[nfd].fd = xsmp_icefd;
6262 fds[nfd].events = POLLIN;
6263 nfd++;
6264 }
6265# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006266#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006267 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006268#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006269 if (interrupted != NULL)
6270 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006272 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006273
6274 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006275 if (result == 0 && interrupted != NULL && ret > 0)
6276 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006277
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006278# ifdef FEAT_MZSCHEME
6279 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006280 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006281 finished = FALSE;
6282# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284# ifdef FEAT_XCLIPBOARD
6285 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6286 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006287 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006289 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 finished = FALSE;
6291 }
6292# endif
6293# ifdef FEAT_MOUSE_GPM
6294 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296# endif
6297# ifdef USE_XSMP
6298 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6299 {
6300 if (fds[xsmp_idx].revents & POLLIN)
6301 {
6302 busy = TRUE;
6303 xsmp_handle_requests();
6304 busy = FALSE;
6305 }
6306 else if (fds[xsmp_idx].revents & POLLHUP)
6307 {
6308 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006309 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 xsmp_close();
6311 }
6312 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006313 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 }
6315# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006316#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006317 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006318 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006319 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321
Bram Moolenaar0f873732019-12-05 20:28:46 +01006322#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323
6324 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006325 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006326 // These are static because they can take 8 Kbyte each and cause the
6327 // signal stack to run out with -O3.
6328 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006330 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006332# ifdef FEAT_MZSCHEME
6333 mzvim_check_threads();
6334 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6335 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006336 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006337 mzquantum_used = TRUE;
6338 }
6339# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006341 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006343 tv.tv_sec = towait / 1000;
6344 tv.tv_usec = (towait % 1000) * (1000000/1000);
6345 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006347 else
6348 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349
6350 /*
6351 * Select on ready for reading and exceptional condition (end of file).
6352 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006353select_eintr:
6354 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006355 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 FD_ZERO(&efds);
6357 FD_SET(fd, &rfds);
K.Takatab247e062022-02-07 10:45:23 +00006358# ifndef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006359 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 FD_SET(fd, &efds);
6361# endif
6362 maxfd = fd;
6363
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006365 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 if (xterm_Shell != (Widget)0)
6367 {
6368 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6369 if (maxfd < ConnectionNumber(xterm_dpy))
6370 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006371
Bram Moolenaar0f873732019-12-05 20:28:46 +01006372 // An event may have already been read but not handled. In
6373 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006374 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 }
6376# endif
6377# ifdef FEAT_MOUSE_GPM
6378 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6379 {
6380 FD_SET(gpm_fd, &rfds);
6381 FD_SET(gpm_fd, &efds);
6382 if (maxfd < gpm_fd)
6383 maxfd = gpm_fd;
6384 }
6385# endif
6386# ifdef USE_XSMP
6387 if (xsmp_icefd != -1)
6388 {
6389 FD_SET(xsmp_icefd, &rfds);
6390 FD_SET(xsmp_icefd, &efds);
6391 if (maxfd < xsmp_icefd)
6392 maxfd = xsmp_icefd;
6393 }
6394# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006395# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006396 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006397# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006398 if (interrupted != NULL)
6399 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400
Bram Moolenaar643b6142018-09-12 20:29:09 +02006401 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6402 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006403 result = ret > 0 && FD_ISSET(fd, &rfds);
6404 if (result)
6405 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006406 else if (interrupted != NULL && ret > 0)
6407 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006408
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006409# ifdef EINTR
6410 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006411 {
dbivolaruab16ad32021-12-29 19:41:47 +00006412 // Check whether the EINTR is caused by SIGTSTP
6413 if (got_tstp && !in_mch_suspend)
6414 {
6415 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006416
dbivolaruab16ad32021-12-29 19:41:47 +00006417 ea.forceit = TRUE;
6418 ex_stop(&ea);
6419 got_tstp = FALSE;
6420 }
6421
Bram Moolenaar0f873732019-12-05 20:28:46 +01006422 // Check whether window has been resized, EINTR may be caused by
6423 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006424 if (do_resize)
6425 handle_resize();
6426
Bram Moolenaar0f873732019-12-05 20:28:46 +01006427 // Interrupted by a signal, need to try again. We ignore msec
6428 // here, because we do want to check even after a timeout if
6429 // characters are available. Needed for reading output of an
6430 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006431 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006432 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006433# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006434# ifdef __TANDEM
6435 if (ret == -1 && errno == ENOTSUP)
6436 {
6437 FD_ZERO(&rfds);
6438 FD_ZERO(&efds);
6439 ret = 0;
6440 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006441# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006442# ifdef FEAT_MZSCHEME
6443 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006444 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006445 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446# endif
6447
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448# ifdef FEAT_XCLIPBOARD
6449 if (ret > 0 && xterm_Shell != (Widget)0
6450 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6451 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006452 xterm_update(); // Maybe we should hand out clipboard
6453 // continue looping when we only got the X event and the input
6454 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 if (--ret == 0 && !input_available())
6456 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006457 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 finished = FALSE;
6459 }
6460 }
6461# endif
6462# ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00006463 if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
6465 if (FD_ISSET(gpm_fd, &efds))
6466 gpm_close();
6467 else if (FD_ISSET(gpm_fd, &rfds))
6468 *check_for_gpm = 1;
6469 }
6470# endif
6471# ifdef USE_XSMP
6472 if (ret > 0 && xsmp_icefd != -1)
6473 {
6474 if (FD_ISSET(xsmp_icefd, &efds))
6475 {
6476 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006477 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 xsmp_close();
6479 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006480 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 }
6482 else if (FD_ISSET(xsmp_icefd, &rfds))
6483 {
6484 busy = TRUE;
6485 xsmp_handle_requests();
6486 busy = FALSE;
6487 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006488 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 }
6490 }
6491# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006492#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006493 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006494 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006495 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497
Bram Moolenaar0f873732019-12-05 20:28:46 +01006498#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
6500#ifdef MAY_LOOP
6501 if (finished || msec == 0)
6502 break;
6503
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006504# ifdef FEAT_CLIENTSERVER
6505 if (server_waiting())
6506 break;
6507# endif
6508
Bram Moolenaar0f873732019-12-05 20:28:46 +01006509 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 if (msec > 0)
6511 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006512# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006513 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006514 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006516 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 msec = msec / 2;
6518# endif
6519 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006520 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 }
6522#endif
6523 }
6524
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006525 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526}
6527
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006529 * Expand a path into all matching files and/or directories. Handles "*",
6530 * "?", "[a-z]", "**", etc.
6531 * "path" has backslashes before chars that are not to be expanded.
6532 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 */
6534 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006535mch_expandpath(
6536 garray_T *gap,
6537 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006538 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539{
Bram Moolenaar02743632005-07-25 20:42:36 +00006540 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542
6543/*
6544 * mch_expand_wildcards() - this code does wild-card pattern matching using
6545 * the shell
6546 *
6547 * return OK for success, FAIL for error (you may lose some memory) and put
6548 * an error message in *file.
6549 *
6550 * num_pat is number of input patterns
6551 * pat is array of pointers to input patterns
6552 * num_file is pointer to number of matched file names
6553 * file is pointer to array of pointers to matched file names
6554 */
6555
6556#ifndef SEEK_SET
6557# define SEEK_SET 0
6558#endif
6559#ifndef SEEK_END
6560# define SEEK_END 2
6561#endif
6562
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006563#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006564
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006566mch_expand_wildcards(
6567 int num_pat,
6568 char_u **pat,
6569 int *num_file,
6570 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006571 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572{
6573 int i;
6574 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006575 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 char_u *p;
6577 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578
Bram Moolenaarc7247912008-01-13 12:54:11 +00006579 /*
6580 * This is the non-OS/2 implementation (really Unix).
6581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 int j;
6583 char_u *tempname;
6584 char_u *command;
6585 FILE *fd;
6586 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006587#define STYLE_ECHO 0 // use "echo", the default
6588#define STYLE_GLOB 1 // use "glob", for csh
6589#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6590#define STYLE_PRINT 3 // use "print -N", for zsh
6591#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6592 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 int shell_style = STYLE_ECHO;
6594 int check_spaces;
6595 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006596 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006597 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006598 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599
Bram Moolenaar0f873732019-12-05 20:28:46 +01006600 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 *file = NULL;
6602
6603 /*
6604 * If there are no wildcards, just copy the names to allocated memory.
6605 * Saves a lot of time, because we don't have to start a new shell.
6606 */
6607 if (!have_wildcard(num_pat, pat))
6608 return save_patterns(num_pat, pat, num_file, file);
6609
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006610# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006611 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006612 if (sandbox != 0 && check_secure())
6613 return FAIL;
6614# endif
6615
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 /*
6617 * Don't allow the use of backticks in secure and restricted mode.
6618 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006619 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 for (i = 0; i < num_pat; ++i)
6621 if (vim_strchr(pat[i], '`') != NULL
6622 && (check_restricted() || check_secure()))
6623 return FAIL;
6624
6625 /*
6626 * get a name for the temp file
6627 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006628 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006630 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 return FAIL;
6632 }
6633
6634 /*
6635 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006636 * file.
6637 * STYLE_BT: NL separated
6638 * If expanding `cmd` execute it directly.
6639 * STYLE_GLOB: NUL separated
6640 * If we use *csh, "glob" will work better than "echo".
6641 * STYLE_PRINT: NL or NUL separated
6642 * If we use *zsh, "print -N" will work better than "glob".
6643 * STYLE_VIMGLOB: NL separated
6644 * If we use *sh*, we define "vimglob()".
6645 * STYLE_ECHO: space separated.
6646 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 */
6648 if (num_pat == 1 && *pat[0] == '`'
6649 && (len = STRLEN(pat[0])) > 2
6650 && *(pat[0] + len - 1) == '`')
6651 shell_style = STYLE_BT;
6652 else if ((len = STRLEN(p_sh)) >= 3)
6653 {
6654 if (STRCMP(p_sh + len - 3, "csh") == 0)
6655 shell_style = STYLE_GLOB;
6656 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6657 shell_style = STYLE_PRINT;
6658 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006659 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6660 "sh") != NULL)
6661 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
Bram Moolenaar0f873732019-12-05 20:28:46 +01006663 // Compute the length of the command. We need 2 extra bytes: for the
6664 // optional '&' and for the NUL.
6665 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006667 if (shell_style == STYLE_VIMGLOB)
6668 len += STRLEN(sh_vimglob_func);
6669
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006670 for (i = 0; i < num_pat; ++i)
6671 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006672 // Count the length of the patterns in the same way as they are put in
6673 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006674#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006675 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006676#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006677 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006678 for (j = 0; pat[i][j] != NUL; ++j)
6679 {
6680 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006681 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006682 ++len;
6683 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006684#endif
6685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 command = alloc(len);
6687 if (command == NULL)
6688 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006689 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 vim_free(tempname);
6691 return FAIL;
6692 }
6693
6694 /*
6695 * Build the shell command:
6696 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6697 * recognizes this).
6698 * - Add the shell command to print the expanded names.
6699 * - Add the temp file name.
6700 * - Add the file name patterns.
6701 */
6702 if (shell_style == STYLE_BT)
6703 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006704 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006705 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006706 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006708 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006709 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006711 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006713 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 *p = ' ';
6715 }
6716 STRCAT(command, ">");
6717 }
6718 else
6719 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006720 STRCPY(command, "");
6721 if (shell_style == STYLE_GLOB)
6722 {
6723 // Assume the nonomatch option is valid only for csh like shells,
6724 // otherwise, this may set the positional parameters for the shell,
6725 // e.g. "$*".
6726 if (flags & EW_NOTFOUND)
6727 STRCAT(command, "set nonomatch; ");
6728 else
6729 STRCAT(command, "unset nonomatch; ");
6730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 if (shell_style == STYLE_GLOB)
6732 STRCAT(command, "glob >");
6733 else if (shell_style == STYLE_PRINT)
6734 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006735 else if (shell_style == STYLE_VIMGLOB)
6736 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 else
6738 STRCAT(command, "echo >");
6739 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006740
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006742
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 if (shell_style != STYLE_BT)
6744 for (i = 0; i < num_pat; ++i)
6745 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006746 // When using system() always add extra quotes, because the shell
6747 // is started twice. Otherwise put a backslash before special
6748 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749#ifdef USE_SYSTEM
6750 STRCAT(command, " \"");
6751 STRCAT(command, pat[i]);
6752 STRCAT(command, "\"");
6753#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006754 int intick = FALSE;
6755
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 p = command + STRLEN(command);
6757 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006758 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006759 {
6760 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006761 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006762 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6763 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006764 // Remove a backslash, take char literally. But keep
6765 // backslash inside backticks, before a special character
6766 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006767 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006768 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6769 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006770 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006771 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006772 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006773 else if (!intick
6774 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6775 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006776 // Put a backslash before a special character, but not
6777 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6778 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006779 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006780
Bram Moolenaar0f873732019-12-05 20:28:46 +01006781 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006782 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 *p = NUL;
6785#endif
6786 }
6787 if (flags & EW_SILENT)
6788 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006789 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006790 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791
6792 /*
6793 * Using zsh -G: If a pattern has no matches, it is just deleted from
6794 * the argument list, otherwise zsh gives an error message and doesn't
6795 * expand any other pattern.
6796 */
6797 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006798 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799
6800 /*
6801 * If we use -f then shell variables set in .cshrc won't get expanded.
6802 * vi can do it, so we will too, but it is only necessary if there is a "$"
6803 * in one of the patterns, otherwise we can still use the fast option.
6804 */
6805 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006806 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807
6808 /*
6809 * execute the shell command
6810 */
6811 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6812
Bram Moolenaar0f873732019-12-05 20:28:46 +01006813 // When running in the background, give it some time to create the temp
6814 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006815 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006816 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
Bram Moolenaar0f873732019-12-05 20:28:46 +01006818 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 show_shell_mess = TRUE;
6820 vim_free(command);
6821
Bram Moolenaar0f873732019-12-05 20:28:46 +01006822 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 {
6824 mch_remove(tempname);
6825 vim_free(tempname);
6826 /*
6827 * With interactive completion, the error message is not printed.
6828 * However with USE_SYSTEM, I don't know how to turn off error messages
6829 * from the shell, so screen may still get messed up -- webb.
6830 */
6831#ifndef USE_SYSTEM
6832 if (!(flags & EW_SILENT))
6833#endif
6834 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006835 redraw_later_clear(); // probably messed up screen
6836 msg_putchar('\n'); // clear bottom line quickly
6837 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838#ifdef USE_SYSTEM
6839 if (!(flags & EW_SILENT))
6840#endif
6841 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006842 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006843 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 }
6845 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006846 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6847 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 if (shell_style == STYLE_BT)
6849 return FAIL;
6850 goto notfound;
6851 }
6852
6853 /*
6854 * read the names from the file into memory
6855 */
6856 fd = fopen((char *)tempname, READBIN);
6857 if (fd == NULL)
6858 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006859 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 if (!(flags & EW_SILENT))
6861 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006862 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006863 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 }
6865 vim_free(tempname);
6866 goto notfound;
6867 }
6868 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006869 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006871 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006872 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006873 buffer = NULL;
6874 else
6875 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 if (buffer == NULL)
6877 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006878 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 mch_remove(tempname);
6880 vim_free(tempname);
6881 fclose(fd);
6882 return FAIL;
6883 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006884 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 i = fread((char *)buffer, 1, len, fd);
6886 fclose(fd);
6887 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006888 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006890 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006891 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 vim_free(tempname);
6893 vim_free(buffer);
6894 return FAIL;
6895 }
6896 vim_free(tempname);
6897
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006898# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006899 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006901 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6903 *p++ = buffer[i];
6904 len = p - buffer;
6905# endif
6906
6907
Bram Moolenaar0f873732019-12-05 20:28:46 +01006908 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 if (shell_style == STYLE_ECHO)
6910 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006911 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006913 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {
6915 while (*p != ' ' && *p != '\n')
6916 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006917 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 }
6919 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006920 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006921 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006923 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006925 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 {
6927 while (*p != '\n' && *p != NUL)
6928 ++p;
6929 if (*p != NUL)
6930 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006931 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 }
6933 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006934 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 else
6936 {
6937 /*
6938 * Some versions of zsh use spaces instead of NULs to separate
6939 * results. Only do this when there is no NUL before the end of the
6940 * buffer, otherwise we would never be able to use file names with
6941 * embedded spaces when zsh does use NULs.
6942 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6943 * don't check for spaces again.
6944 */
6945 check_spaces = FALSE;
6946 if (shell_style == STYLE_PRINT && !did_find_nul)
6947 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006948 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006949 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006950 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 did_find_nul = TRUE;
6952 else
6953 check_spaces = TRUE;
6954 }
6955
6956 /*
6957 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6958 * already is one, for STYLE_GLOB it needs to be added.
6959 */
6960 if (len && buffer[len - 1] == NUL)
6961 --len;
6962 else
6963 buffer[len] = NUL;
6964 i = 0;
6965 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006966 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 {
6968 ++i;
6969 *p = NUL;
6970 }
6971 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006972 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 }
6974 if (i == 0)
6975 {
6976 /*
6977 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6978 * /bin/sh will happily expand it to nothing rather than returning an
6979 * error; and hey, it's good to check anyway -- webb.
6980 */
6981 vim_free(buffer);
6982 goto notfound;
6983 }
6984 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006985 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 if (*file == NULL)
6987 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006988 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 vim_free(buffer);
6990 return FAIL;
6991 }
6992
6993 /*
6994 * Isolate the individual file names.
6995 */
6996 p = buffer;
6997 for (i = 0; i < *num_file; ++i)
6998 {
6999 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007000 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00007001 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
7002 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00007004 while (!(shell_style == STYLE_ECHO && *p == ' ')
7005 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007007 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 *p = NUL;
7009 else
7010 {
7011 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007012 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 }
7014 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007015 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007017 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007019 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 }
7021 }
7022
7023 /*
7024 * Move the file names to allocated memory.
7025 */
7026 for (j = 0, i = 0; i < *num_file; ++i)
7027 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007028 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7030 continue;
7031
Bram Moolenaar0f873732019-12-05 20:28:46 +01007032 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 dir = (mch_isdir((*file)[i]));
7034 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7035 continue;
7036
Bram Moolenaar0f873732019-12-05 20:28:46 +01007037 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007038 if (!dir && (flags & EW_EXEC)
7039 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007040 continue;
7041
Bram Moolenaar964b3742019-05-24 18:54:09 +02007042 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 if (p)
7044 {
7045 STRCPY(p, (*file)[i]);
7046 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007047 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 (*file)[j++] = p;
7049 }
7050 }
7051 vim_free(buffer);
7052 *num_file = j;
7053
Bram Moolenaar0f873732019-12-05 20:28:46 +01007054 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007056 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 goto notfound;
7058 }
7059
7060 return OK;
7061
7062notfound:
7063 if (flags & EW_NOTFOUND)
7064 return save_patterns(num_pat, pat, num_file, file);
7065 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066}
7067
Bram Moolenaar0f873732019-12-05 20:28:46 +01007068#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007071save_patterns(
7072 int num_pat,
7073 char_u **pat,
7074 int *num_file,
7075 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
7077 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007078 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007080 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 if (*file == NULL)
7082 return FAIL;
7083 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007084 {
7085 s = vim_strsave(pat[i]);
7086 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007087 // Be compatible with expand_filename(): halve the number of
7088 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007089 backslash_halve(s);
7090 (*file)[i] = s;
7091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 *num_file = num_pat;
7093 return OK;
7094}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096/*
7097 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7098 * expand.
7099 */
7100 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007101mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007103 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 if (*p == '\\' && p[1] != NUL)
7106 ++p;
7107 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 if (vim_strchr((char_u *)
7109#ifdef VMS
7110 "*?%"
7111#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113#endif
7114 , *p) != NULL)
7115 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 }
7117 return FALSE;
7118}
7119
7120/*
7121 * Return TRUE if the string "p" contains a wildcard.
7122 * Don't recognize '~' at the end as a wildcard.
7123 */
7124 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007125mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007127 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 if (*p == '\\' && p[1] != NUL)
7130 ++p;
7131 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 if (vim_strchr((char_u *)
7133#ifdef VMS
7134 "*?%$"
7135#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137#endif
7138 , *p) != NULL
7139 || (*p == '~' && p[1] != NUL))
7140 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 }
7142 return FALSE;
7143}
7144
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007146have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
7148 int i;
7149
7150 for (i = 0; i < num; i++)
7151 if (mch_has_wildcard(file[i]))
7152 return 1;
7153 return 0;
7154}
7155
7156 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007157have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158{
7159 int i;
7160
7161 for (i = 0; i < num; i++)
7162 if (vim_strchr(file[i], '$') != NULL)
7163 return TRUE;
7164 return FALSE;
7165}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007167#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168/*
7169 * Scaled-down version of rename(), which is missing in Xenix.
7170 * This version can only move regular files and will fail if the
7171 * destination exists.
7172 */
7173 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007174mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
7176 struct stat st;
7177
Bram Moolenaar0f873732019-12-05 20:28:46 +01007178 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007180 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007182 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 return 0;
7184 return -1;
7185}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007186#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007188#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007189# if defined(DYNAMIC_GPM) || defined(PROTO)
7190/*
7191 * Initialize Gpm's symbols for dynamic linking.
7192 * Must be called only if libgpm_hinst is NULL.
7193 */
7194 static int
7195load_libgpm(void)
7196{
7197 libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL);
7198
7199 if (libgpm_hinst == NULL)
7200 {
7201 if (p_verbose > 0)
7202 smsg_attr(HL_ATTR(HLF_W),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007203 _("Could not load gpm library: %s"), dlerror());
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007204 return FAIL;
7205 }
7206
7207 if (
7208 (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL
7209 || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL
7210 || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL
7211 || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL
7212 || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL
7213 )
7214 {
7215 semsg(_(e_could_not_load_library_str_str), "gpm", dlerror());
7216 dlclose(libgpm_hinst);
7217 libgpm_hinst = NULL;
7218 dll_gpm_flag = NULL;
7219 dll_gpm_fd = NULL;
7220 return FAIL;
7221 }
7222 return OK;
7223}
7224
7225 int
7226gpm_available(void)
7227{
7228 return libgpm_hinst != NULL || load_libgpm() == OK;
7229}
7230# endif // DYNAMIC_GPM
7231
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232/*
7233 * Initializes connection with gpm (if it isn't already opened)
7234 * Return 1 if succeeded (or connection already opened), 0 if failed
7235 */
7236 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007237gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007239 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007241#ifdef DYNAMIC_GPM
7242 if (!gpm_available())
7243 return 0;
7244#endif
7245
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 if (!gpm_flag)
7247 {
7248 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7249 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007250 // Default handling for mouse move
7251 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 gpm_connect.maxMod = 0xffff;
7253 if (Gpm_Open(&gpm_connect, 0) > 0)
7254 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007255 // gpm library tries to handling TSTP causes
7256 // problems. Anyways, we close connection to Gpm whenever
7257 // we are going to suspend or starting an external process
7258 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007259# ifdef SIGTSTP
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007260 signal(SIGTSTP, restricted ? SIG_IGN : (void (*)())sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007261# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007262 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 }
7264 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007265 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 return 0;
7267 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007268 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269}
7270
7271/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007272 * Returns TRUE if the GPM mouse is enabled.
7273 */
7274 int
7275gpm_enabled(void)
7276{
7277 return gpm_flag && gpm_fd >= 0;
7278}
7279
7280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 */
7283 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007284gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007286 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 Gpm_Close();
7288}
7289
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007290/*
7291 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007293 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 */
7295 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007296mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297{
7298 int button;
7299 static Gpm_Event gpm_event;
7300 char_u string[6];
7301 int_u vim_modifiers;
7302 int row,col;
7303 unsigned char buttons_mask;
7304 unsigned char gpm_modifiers;
7305 static unsigned char old_buttons = 0;
7306
7307 Gpm_GetEvent(&gpm_event);
7308
7309#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007310 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 if (hold_gui_events)
7312 return 0;
7313#endif
7314
7315 row = gpm_event.y - 1;
7316 col = gpm_event.x - 1;
7317
Bram Moolenaar0f873732019-12-05 20:28:46 +01007318 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 string[1] = 'M';
7320 string[2] = 'G';
7321 switch (GPM_BARE_EVENTS(gpm_event.type))
7322 {
7323 case GPM_DRAG:
7324 string[3] = MOUSE_DRAG;
7325 break;
7326 case GPM_DOWN:
7327 buttons_mask = gpm_event.buttons & ~old_buttons;
7328 old_buttons = gpm_event.buttons;
7329 switch (buttons_mask)
7330 {
7331 case GPM_B_LEFT:
7332 button = MOUSE_LEFT;
7333 break;
7334 case GPM_B_MIDDLE:
7335 button = MOUSE_MIDDLE;
7336 break;
7337 case GPM_B_RIGHT:
7338 button = MOUSE_RIGHT;
7339 break;
7340 default:
7341 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007342 // Don't know what to do. Can more than one button be
7343 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 }
7345 string[3] = (char_u)(button | 0x20);
7346 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7347 break;
7348 case GPM_UP:
7349 string[3] = MOUSE_RELEASE;
7350 old_buttons &= ~gpm_event.buttons;
7351 break;
7352 default:
7353 return 0;
7354 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007355 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 gpm_modifiers = gpm_event.modifiers;
7357 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007358 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7359 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7360 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7362 vim_modifiers |= MOUSE_SHIFT;
7363
7364 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7365 vim_modifiers |= MOUSE_CTRL;
7366 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7367 vim_modifiers |= MOUSE_ALT;
7368 string[3] |= vim_modifiers;
7369 string[4] = (char_u)(col + ' ' + 1);
7370 string[5] = (char_u)(row + ' ' + 1);
7371 add_to_input_buf(string, 6);
7372 return 6;
7373}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007374#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007376#ifdef FEAT_SYSMOUSE
7377/*
7378 * Initialize connection with sysmouse.
7379 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7380 * output, any sysmouse output than will be processed via sig_sysmouse().
7381 * Return OK if succeeded, FAIL if failed.
7382 */
7383 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007384sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007385{
7386 struct mouse_info mouse;
7387
7388 mouse.operation = MOUSE_MODE;
7389 mouse.u.mode.mode = 0;
7390 mouse.u.mode.signal = SIGUSR2;
7391 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7392 {
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007393 signal(SIGUSR2, (void (*)())sig_sysmouse);
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007394 mouse.operation = MOUSE_SHOW;
7395 ioctl(1, CONS_MOUSECTL, &mouse);
7396 return OK;
7397 }
7398 return FAIL;
7399}
7400
7401/*
7402 * Stop processing SIGUSR2 signals, and also make sure that
7403 * virtual console do not send us any sysmouse related signal.
7404 */
7405 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007406sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007407{
7408 struct mouse_info mouse;
7409
7410 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7411 mouse.operation = MOUSE_MODE;
7412 mouse.u.mode.mode = 0;
7413 mouse.u.mode.signal = 0;
7414 ioctl(1, CONS_MOUSECTL, &mouse);
7415}
7416
7417/*
7418 * Gets info from sysmouse and adds special keys to input buf.
7419 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007420 static void
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007421sig_sysmouse SIGDEFARG(sigarg)
7422{
7423 struct mouse_info mouse;
7424 struct video_info video;
7425 char_u string[6];
7426 int row, col;
7427 int button;
7428 int buttons;
7429 static int oldbuttons = 0;
7430
7431#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007432 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007433 if (hold_gui_events)
7434 return;
7435#endif
7436
7437 mouse.operation = MOUSE_GETINFO;
7438 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7439 && ioctl(1, FBIO_MODEINFO, &video) != -1
7440 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7441 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7442 {
7443 row = mouse.u.data.y / video.vi_cheight;
7444 col = mouse.u.data.x / video.vi_cwidth;
7445 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007446 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007447 string[1] = 'M';
7448 string[2] = 'S';
7449 if (oldbuttons == buttons && buttons != 0)
7450 {
7451 button = MOUSE_DRAG;
7452 }
7453 else
7454 {
7455 switch (buttons)
7456 {
7457 case 0:
7458 button = MOUSE_RELEASE;
7459 break;
7460 case 1:
7461 button = MOUSE_LEFT;
7462 break;
7463 case 2:
7464 button = MOUSE_MIDDLE;
7465 break;
7466 case 4:
7467 button = MOUSE_RIGHT;
7468 break;
7469 default:
7470 return;
7471 }
7472 oldbuttons = buttons;
7473 }
7474 string[3] = (char_u)(button);
7475 string[4] = (char_u)(col + ' ' + 1);
7476 string[5] = (char_u)(row + ' ' + 1);
7477 add_to_input_buf(string, 6);
7478 }
7479 return;
7480}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007481#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007482
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007484typedef char_u * (*STRPROCSTR)(char_u *);
7485typedef char_u * (*INTPROCSTR)(int);
7486typedef int (*STRPROCINT)(char_u *);
7487typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488
7489/*
7490 * Call a DLL routine which takes either a string or int param
7491 * and returns an allocated string.
7492 */
7493 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007494mch_libcall(
7495 char_u *libname,
7496 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007497 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007498 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007499 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007500 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501{
7502# if defined(USE_DLOPEN)
7503 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007504 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505# else
7506 shl_t hinstLib;
7507# endif
7508 STRPROCSTR ProcAdd;
7509 INTPROCSTR ProcAddI;
7510 char_u *retval_str = NULL;
7511 int retval_int = 0;
7512 int success = FALSE;
7513
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007514 /*
7515 * Get a handle to the DLL module.
7516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007518 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007519 (void)dlerror();
7520
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 hinstLib = dlopen((char *)libname, RTLD_LAZY
7522# ifdef RTLD_LOCAL
7523 | RTLD_LOCAL
7524# endif
7525 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007526 if (hinstLib == NULL)
7527 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007528 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007529 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007530 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007531 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533# else
7534 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7535# endif
7536
Bram Moolenaar0f873732019-12-05 20:28:46 +01007537 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 if (hinstLib != NULL)
7539 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007540# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 /*
7542 * Catch a crash when calling the library function. For example when
7543 * using a number where a string pointer is expected.
7544 */
7545 mch_startjmp();
7546 if (SETJMP(lc_jump_env) != 0)
7547 {
7548 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007549# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007550 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007551# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 mch_didjmp();
7553 }
7554 else
7555# endif
7556 {
7557 retval_str = NULL;
7558 retval_int = 0;
7559
7560 if (argstring != NULL)
7561 {
7562# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007563 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007564 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565# else
7566 if (shl_findsym(&hinstLib, (const char *)funcname,
7567 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7568 ProcAdd = NULL;
7569# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007570 if ((success = (ProcAdd != NULL
7571# if defined(USE_DLOPEN)
7572 && dlerr == NULL
7573# endif
7574 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 {
7576 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007577 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 else
7579 retval_str = (ProcAdd)(argstring);
7580 }
7581 }
7582 else
7583 {
7584# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007585 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007586 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587# else
7588 if (shl_findsym(&hinstLib, (const char *)funcname,
7589 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7590 ProcAddI = NULL;
7591# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007592 if ((success = (ProcAddI != NULL
7593# if defined(USE_DLOPEN)
7594 && dlerr == NULL
7595# endif
7596 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 {
7598 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007599 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 else
7601 retval_str = (ProcAddI)(argint);
7602 }
7603 }
7604
Bram Moolenaar0f873732019-12-05 20:28:46 +01007605 // Save the string before we free the library.
7606 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 if (string_result == NULL)
7608 *number_result = retval_int;
7609 else if (retval_str != NULL
7610 && retval_str != (char_u *)1
7611 && retval_str != (char_u *)-1)
7612 *string_result = vim_strsave(retval_str);
7613 }
7614
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007615# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 mch_endjmp();
7617# ifdef SIGHASARG
7618 if (lc_signal != 0)
7619 {
7620 int i;
7621
Bram Moolenaar0f873732019-12-05 20:28:46 +01007622 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 for (i = 0; signal_info[i].sig != -1; i++)
7624 if (lc_signal == signal_info[i].sig)
7625 break;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00007626 semsg(e_got_sig_str_in_libcall, signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 }
7628# endif
7629# endif
7630
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007632 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007633 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007634 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007635
Bram Moolenaar0f873732019-12-05 20:28:46 +01007636 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 (void)dlclose(hinstLib);
7638# else
7639 (void)shl_unload(hinstLib);
7640# endif
7641 }
7642
7643 if (!success)
7644 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007645 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 return FAIL;
7647 }
7648
7649 return OK;
7650}
7651#endif
7652
7653#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007654static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655static int xterm_button;
7656
7657/*
7658 * Setup a dummy window for X selections in a terminal.
7659 */
7660 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007661setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
7663 int z = 0;
7664 char *strp = "";
7665 Widget AppShell;
7666
7667 if (!x_connect_to_server())
7668 return;
7669
7670 open_app_context();
7671 if (app_context != NULL && xterm_Shell == (Widget)0)
7672 {
7673 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007674# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007676# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007677# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007678 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679
7680 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007681 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682# endif
7683
Bram Moolenaar0f873732019-12-05 20:28:46 +01007684 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 oldhandler = XSetErrorHandler(x_error_check);
7686
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007687# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007688 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7690 mch_startjmp();
7691 if (SETJMP(lc_jump_env) != 0)
7692 {
7693 mch_didjmp();
7694 xterm_dpy = NULL;
7695 }
7696 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 {
7699 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7700 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007701 if (xterm_dpy != NULL)
7702 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007703# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007705# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 }
7707
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007708# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007709 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007711# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007712 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 (void)XSetErrorHandler(oldhandler);
7714
7715 if (xterm_dpy == NULL)
7716 {
7717 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007718 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 return;
7720 }
7721
Bram Moolenaar0f873732019-12-05 20:28:46 +01007722 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 (void)XSetIOErrorHandler(x_IOerror_handler);
7724
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007725# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007727 {
7728 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007729 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007730 verbose_leave();
7731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732# endif
7733
Bram Moolenaar0f873732019-12-05 20:28:46 +01007734 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7736 applicationShellWidgetClass, xterm_dpy,
7737 NULL);
7738 if (AppShell == (Widget)0)
7739 return;
7740 xterm_Shell = XtVaCreatePopupShell("VIM",
7741 topLevelShellWidgetClass, AppShell,
7742 XtNmappedWhenManaged, 0,
7743 XtNwidth, 1,
7744 XtNheight, 1,
7745 NULL);
7746 if (xterm_Shell == (Widget)0)
7747 return;
7748
7749 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007750 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 if (x11_display == NULL)
7752 x11_display = xterm_dpy;
7753
7754 XtRealizeWidget(xterm_Shell);
7755 XSync(xterm_dpy, False);
7756 xterm_update();
7757 }
7758 if (xterm_Shell != (Widget)0)
7759 {
7760 clip_init(TRUE);
7761 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7762 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007763 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 if (test_x11_window(xterm_dpy) == FAIL)
7765 x11_window = 0;
7766 if (x11_window != 0)
7767 xterm_trace = 0;
7768 }
7769}
7770
7771 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007772start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773{
7774 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7775 return;
7776 xterm_trace = 1;
7777 xterm_button = button;
7778 do_xterm_trace();
7779}
7780
7781
7782 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007783stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
7785 if (xterm_trace < 0)
7786 return;
7787 xterm_trace = 0;
7788}
7789
7790/*
7791 * Query the xterm pointer and generate mouse termcodes if necessary
7792 * return TRUE if dragging is active, else FALSE
7793 */
7794 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007795do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
7797 Window root, child;
7798 int root_x, root_y;
7799 int win_x, win_y;
7800 int row, col;
7801 int_u mask_return;
7802 char_u buf[50];
7803 char_u *strp;
7804 long got_hints;
7805 static char_u *mouse_code;
7806 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7807 static int prev_row = 0, prev_col = 0;
7808 static XSizeHints xterm_hints;
7809
7810 if (xterm_trace <= 0)
7811 return FALSE;
7812
7813 if (xterm_trace == 1)
7814 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007815 // Get the hints just before tracking starts. The font size might
7816 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007817 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7818 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 || xterm_hints.width_inc <= 1
7820 || xterm_hints.height_inc <= 1)
7821 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007822 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 return FALSE;
7824 }
7825
Bram Moolenaar0f873732019-12-05 20:28:46 +01007826 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 mouse_code = find_termcode(mouse_name);
7828 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007829 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 xterm_trace = 2;
7831
Bram Moolenaar0f873732019-12-05 20:28:46 +01007832 // Find the offset of the chars, there might be a scrollbar on the
7833 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7835 &win_x, &win_y, &mask_return);
7836 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7837 - (xterm_hints.height_inc / 2);
7838 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7839 xterm_hints.y = 2;
7840 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7841 - (xterm_hints.width_inc / 2);
7842 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7843 xterm_hints.x = 2;
7844 return TRUE;
7845 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007846 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {
7848 xterm_trace = 0;
7849 return FALSE;
7850 }
7851
7852 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7853 &win_x, &win_y, &mask_return);
7854
7855 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7856 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7857 if (row == prev_row && col == prev_col)
7858 return TRUE;
7859
7860 STRCPY(buf, mouse_code);
7861 strp = buf + STRLEN(buf);
7862 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7863 *strp++ = (char_u)(col + ' ' + 1);
7864 *strp++ = (char_u)(row + ' ' + 1);
7865 *strp = 0;
7866 add_to_input_buf(buf, STRLEN(buf));
7867
7868 prev_row = row;
7869 prev_col = col;
7870 return TRUE;
7871}
7872
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007873# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874/*
7875 * Destroy the display, window and app_context. Required for GTK.
7876 */
7877 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007878clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
7880 if (xterm_Shell != (Widget)0)
7881 {
7882 XtDestroyWidget(xterm_Shell);
7883 xterm_Shell = (Widget)0;
7884 }
7885 if (xterm_dpy != NULL)
7886 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007887# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007888 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 if (x11_display == xterm_dpy)
7892 x11_display = NULL;
7893 xterm_dpy = NULL;
7894 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007895# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 if (app_context != (XtAppContext)NULL)
7897 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007898 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 XtDestroyApplicationContext(app_context);
7900 app_context = (XtAppContext)NULL;
7901 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903}
7904# endif
7905
7906/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007907 * Catch up with GUI or X events.
7908 */
7909 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007910clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007911{
7912# ifdef FEAT_GUI
7913 if (gui.in_use)
7914 gui_mch_update();
7915 else
7916# endif
7917 if (xterm_Shell != (Widget)0)
7918 xterm_update();
7919}
7920
7921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 * Catch up with any queued X events. This may put keyboard input into the
7923 * input buffer, call resize call-backs, trigger timers etc. If there is
7924 * nothing in the X event queue (& no timers pending), then we return
7925 * immediately.
7926 */
7927 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007928xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
7930 XEvent event;
7931
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007932 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007934 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007936 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007937 break;
7938
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007939 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007940 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007941 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007942 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007943#ifdef FEAT_CLIENTSERVER
7944 {
7945 XPropertyEvent *e = (XPropertyEvent *)&event;
7946
7947 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007949 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007952 XtDispatchEvent(&event);
7953 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007954 else
7955 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007956 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007957 XtAppProcessEvent(app_context, mask);
7958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 }
7960}
7961
7962 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007963clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
7965 if (xterm_Shell != (Widget)0)
7966 return clip_x11_own_selection(xterm_Shell, cbd);
7967 return FAIL;
7968}
7969
7970 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007971clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972{
7973 if (xterm_Shell != (Widget)0)
7974 clip_x11_lose_selection(xterm_Shell, cbd);
7975}
7976
7977 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007978clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979{
7980 if (xterm_Shell != (Widget)0)
7981 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7982}
7983
7984 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007985clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986{
7987 clip_x11_set_selection(cbd);
7988}
7989#endif
7990
7991
7992#if defined(USE_XSMP) || defined(PROTO)
7993/*
7994 * Code for X Session Management Protocol.
7995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996
7997# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998/*
7999 * This is our chance to ask the user if they want to save,
8000 * or abort the logout
8001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008003xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004{
Bram Moolenaare1004402020-10-24 20:49:43 +02008005 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 int cancel_shutdown = False;
8007
Bram Moolenaare1004402020-10-24 20:49:43 +02008008 save_cmod_flags = cmdmod.cmod_flags;
8009 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01008010 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01008011 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02008013 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01008014 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 out_flush();
8016
Bram Moolenaar0f873732019-12-05 20:28:46 +01008017 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 SmcInteractDone(smc_conn, cancel_shutdown);
8019
Bram Moolenaar0f873732019-12-05 20:28:46 +01008020 // Finish off
8021 // Only end save-yourself here if we're not cancelling shutdown;
8022 // we'll get a cancelled callback later in which we'll end it.
8023 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 if (!cancel_shutdown)
8025 {
8026 xsmp.save_yourself = False;
8027 SmcSaveYourselfDone(smc_conn, True);
8028 }
8029}
8030# endif
8031
8032/*
8033 * Callback that starts save-yourself.
8034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008036xsmp_handle_save_yourself(
8037 SmcConn smc_conn,
8038 SmPointer client_data UNUSED,
8039 int save_type UNUSED,
8040 Bool shutdown,
8041 int interact_style UNUSED,
8042 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008044 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 if (xsmp.save_yourself)
8046 SmcSaveYourselfDone(smc_conn, True);
8047 xsmp.save_yourself = True;
8048 xsmp.shutdown = shutdown;
8049
Bram Moolenaar0f873732019-12-05 20:28:46 +01008050 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01008052 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053
8054 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008055 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056
8057# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008058 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008060 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 SmcInteractRequest(smc_conn, SmDialogError,
8062 xsmp_handle_interaction, client_data);
8063 else
8064# endif
8065 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008066 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 SmcSaveYourselfDone(smc_conn, True);
8068 xsmp.save_yourself = False;
8069 }
8070}
8071
8072
8073/*
8074 * Callback to warn us of imminent death.
8075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008077xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078{
8079 xsmp_close();
8080
Bram Moolenaar0f873732019-12-05 20:28:46 +01008081 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 getout_preserve_modified(0);
8083}
8084
8085
8086/*
8087 * Callback to tell us that save-yourself has completed.
8088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008090xsmp_save_complete(
8091 SmcConn smc_conn UNUSED,
8092 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093{
8094 xsmp.save_yourself = False;
8095}
8096
8097
8098/*
8099 * Callback to tell us that an instigated shutdown was cancelled
8100 * (maybe even by us)
8101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008103xsmp_shutdown_cancelled(
8104 SmcConn smc_conn,
8105 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106{
8107 if (xsmp.save_yourself)
8108 SmcSaveYourselfDone(smc_conn, True);
8109 xsmp.save_yourself = False;
8110 xsmp.shutdown = False;
8111}
8112
8113
8114/*
8115 * Callback to tell us that a new ICE connection has been established.
8116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008118xsmp_ice_connection(
8119 IceConn iceConn,
8120 IcePointer clientData UNUSED,
8121 Bool opening,
8122 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008124 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 if (opening)
8126 {
8127 xsmp_icefd = IceConnectionNumber(iceConn);
8128 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8129 }
8130}
8131
8132
Bram Moolenaar0f873732019-12-05 20:28:46 +01008133// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008135xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136{
8137 Bool rep;
8138
8139 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8140 == IceProcessMessagesIOError)
8141 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008142 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008144 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 xsmp_close();
8146 return FAIL;
8147 }
8148 else
8149 return OK;
8150}
8151
8152static int dummy;
8153
Bram Moolenaar0f873732019-12-05 20:28:46 +01008154// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 void
8156xsmp_init(void)
8157{
8158 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 SmcCallbacks smcallbacks;
8160#if 0
8161 SmPropValue smname;
8162 SmProp smnameprop;
8163 SmProp *smprops[1];
8164#endif
8165
8166 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008167 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168
8169 xsmp.save_yourself = xsmp.shutdown = False;
8170
Bram Moolenaar0f873732019-12-05 20:28:46 +01008171 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8173 smcallbacks.save_yourself.client_data = NULL;
8174 smcallbacks.die.callback = xsmp_die;
8175 smcallbacks.die.client_data = NULL;
8176 smcallbacks.save_complete.callback = xsmp_save_complete;
8177 smcallbacks.save_complete.client_data = NULL;
8178 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8179 smcallbacks.shutdown_cancelled.client_data = NULL;
8180
Bram Moolenaar0f873732019-12-05 20:28:46 +01008181 // Set up a watch on ICE connection creations. The "dummy" argument is
8182 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8184 {
8185 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008186 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 return;
8188 }
8189
Bram Moolenaar0f873732019-12-05 20:28:46 +01008190 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 xsmp.smcconn = SmcOpenConnection(
8192 NULL,
8193 NULL,
8194 SmProtoMajor,
8195 SmProtoMinor,
8196 SmcSaveYourselfProcMask | SmcDieProcMask
8197 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8198 &smcallbacks,
8199 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008200 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008201 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 errorstring);
8203 if (xsmp.smcconn == NULL)
8204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008206 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008207 char errorreport[132];
8208
8209 // If the message is too long it might not be NUL terminated. Add
8210 // a NUL at the end to make sure we don't go over the end.
8211 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008212 vim_snprintf(errorreport, sizeof(errorreport),
8213 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008214 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 return;
8217 }
8218 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8219
8220#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008221 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 smname.value = "vim";
8223 smname.length = 3;
8224 smnameprop.name = "SmProgram";
8225 smnameprop.type = "SmARRAY8";
8226 smnameprop.num_vals = 1;
8227 smnameprop.vals = &smname;
8228
8229 smprops[0] = &smnameprop;
8230 SmcSetProperties(xsmp.smcconn, 1, smprops);
8231#endif
8232}
8233
8234
Bram Moolenaar0f873732019-12-05 20:28:46 +01008235// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008237xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238{
8239 if (xsmp_icefd != -1)
8240 {
8241 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008242 if (xsmp.clientid != NULL)
8243 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008244 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 xsmp_icefd = -1;
8246 }
8247}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008248#endif // USE_XSMP
Paul Ollis65745772022-06-05 16:55:54 +01008249
8250#if defined(FEAT_RELTIME) || defined(PROTO)
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008251# if defined(HAVE_TIMER_CREATE) || defined(PROTO)
Paul Ollis65745772022-06-05 16:55:54 +01008252/*
8253 * Implement timeout with timer_create() and timer_settime().
8254 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008255static volatile sig_atomic_t timeout_flag = FALSE;
8256static timer_t timer_id;
8257static int timer_created = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008258
8259/*
8260 * Callback for when the timer expires.
8261 */
8262 static void
8263set_flag(union sigval _unused UNUSED)
8264{
8265 timeout_flag = TRUE;
8266}
8267
8268/*
8269 * Stop any active timeout.
8270 */
8271 void
8272stop_timeout(void)
8273{
8274 static struct itimerspec disarm = {{0, 0}, {0, 0}};
8275
8276 if (timer_created)
8277 {
8278 int ret = timer_settime(timer_id, 0, &disarm, NULL);
8279
8280 if (ret < 0)
8281 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8282 }
8283
8284 // Clear the current timeout flag; any previous timeout should be
8285 // considered _not_ triggered.
8286 timeout_flag = FALSE;
8287}
8288
8289/*
8290 * Start the timeout timer.
8291 *
8292 * The return value is a pointer to a flag that is initialised to FALSE. If the
8293 * timeout expires, the flag is set to TRUE. This will only return pointers to
8294 * static memory; i.e. any pointer returned by this function may always be
8295 * safely dereferenced.
8296 *
8297 * This function is not expected to fail, but if it does it will still return a
8298 * valid flag pointer; the flag will remain stuck as FALSE .
8299 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008300 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008301start_timeout(long msec)
8302{
8303 struct itimerspec interval = {
8304 {0, 0}, // Do not repeat.
8305 {msec / 1000, (msec % 1000) * 1000000}}; // Timeout interval
8306 int ret;
8307
8308 // This is really the caller's responsibility, but let's make sure the
8309 // previous timer has been stopped.
8310 stop_timeout();
Paul Ollis65745772022-06-05 16:55:54 +01008311
8312 if (!timer_created)
8313 {
8314 struct sigevent action = {0};
8315
8316 action.sigev_notify = SIGEV_THREAD;
8317 action.sigev_notify_function = set_flag;
8318 ret = timer_create(CLOCK_MONOTONIC, &action, &timer_id);
8319 if (ret < 0)
8320 {
8321 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8322 return &timeout_flag;
8323 }
8324 timer_created = TRUE;
8325 }
8326
Bram Moolenaar509ce032022-06-20 11:23:01 +01008327# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar616592e2022-06-17 15:17:10 +01008328 ch_log(NULL, "setting timeout timer to %d sec %ld nsec",
8329 (int)interval.it_value.tv_sec, (long)interval.it_value.tv_nsec);
Bram Moolenaar509ce032022-06-20 11:23:01 +01008330# endif
Paul Ollis65745772022-06-05 16:55:54 +01008331 ret = timer_settime(timer_id, 0, &interval, NULL);
8332 if (ret < 0)
8333 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8334
8335 return &timeout_flag;
8336}
8337
Bram Moolenaarc72e31d2022-06-16 18:47:20 +01008338/*
8339 * To be used before fork/exec: delete any created timer.
8340 */
8341 void
8342delete_timer(void)
8343{
8344 if (timer_created)
8345 {
8346 timer_delete(timer_id);
8347 timer_created = FALSE;
8348 }
8349}
8350
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008351# else // HAVE_TIMER_CREATE
Paul Ollis65745772022-06-05 16:55:54 +01008352
8353/*
8354 * Implement timeout with setitimer()
8355 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008356static struct sigaction prev_sigaction;
8357static volatile sig_atomic_t timeout_flag = FALSE;
8358static int timer_active = FALSE;
8359static int timer_handler_active = FALSE;
8360static volatile sig_atomic_t alarm_pending = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008361
8362/*
8363 * Handle SIGALRM for a timeout.
8364 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01008365 static void
Paul Ollis65745772022-06-05 16:55:54 +01008366set_flag SIGDEFARG(sigarg)
8367{
8368 if (alarm_pending)
8369 alarm_pending = FALSE;
8370 else
8371 timeout_flag = TRUE;
8372}
8373
8374/*
8375 * Stop any active timeout.
8376 */
8377 void
8378stop_timeout(void)
8379{
8380 static struct itimerval disarm = {{0, 0}, {0, 0}};
8381 int ret;
8382
8383 if (timer_active)
8384 {
8385 timer_active = FALSE;
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008386 ret = setitimer(ITIMER_REAL, &disarm, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008387 if (ret < 0)
8388 // Should only get here as a result of coding errors.
8389 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8390 }
8391
8392 if (timer_handler_active)
8393 {
8394 timer_handler_active = FALSE;
8395 ret = sigaction(SIGALRM, &prev_sigaction, NULL);
8396 if (ret < 0)
8397 // Should only get here as a result of coding errors.
8398 semsg(_(e_could_not_reset_handler_for_timeout_str),
8399 strerror(errno));
8400 }
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008401 timeout_flag = FALSE;
Paul Ollis65745772022-06-05 16:55:54 +01008402}
8403
8404/*
8405 * Start the timeout timer.
8406 *
8407 * The return value is a pointer to a flag that is initialised to FALSE. If the
8408 * timeout expires, the flag is set to TRUE. This will only return pointers to
8409 * static memory; i.e. any pointer returned by this function may always be
8410 * safely dereferenced.
8411 *
8412 * This function is not expected to fail, but if it does it will still return a
8413 * valid flag pointer; the flag will remain stuck as FALSE .
8414 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008415 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008416start_timeout(long msec)
8417{
8418 struct itimerval interval = {
8419 {0, 0}, // Do not repeat.
8420 {msec / 1000, (msec % 1000) * 1000}}; // Timeout interval
8421 struct sigaction handle_alarm;
8422 int ret;
8423 sigset_t sigs;
8424 sigset_t saved_sigs;
8425
8426 // This is really the caller's responsibility, but let's make sure the
8427 // previous timer has been stopped.
8428 stop_timeout();
8429
8430 // There is a small chance that SIGALRM is pending and so the handler must
8431 // ignore it on the first call.
8432 alarm_pending = FALSE;
8433 ret = sigemptyset(&sigs);
8434 ret = ret == 0 ? sigaddset(&sigs, SIGALRM) : ret;
8435 ret = ret == 0 ? sigprocmask(SIG_BLOCK, &sigs, &saved_sigs) : ret;
8436 timeout_flag = FALSE;
8437 ret = ret == 0 ? sigpending(&sigs) : ret;
8438 if (ret == 0)
8439 {
8440 alarm_pending = sigismember(&sigs, SIGALRM);
Bram Moolenaar1f89abf2022-06-06 10:07:01 +01008441 ret = sigprocmask(SIG_SETMASK, &saved_sigs, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008442 }
8443 if (unlikely(ret != 0 || alarm_pending < 0))
8444 {
8445 // Just catching coding errors. Write an error message, but carry on.
8446 semsg(_(e_could_not_check_for_pending_sigalrm_str), strerror(errno));
8447 alarm_pending = FALSE;
8448 }
8449
8450 // Set up the alarm handler first.
8451 ret = sigemptyset(&handle_alarm.sa_mask);
8452 handle_alarm.sa_handler = set_flag;
8453 handle_alarm.sa_flags = 0;
8454 ret = ret == 0 ? sigaction(SIGALRM, &handle_alarm, &prev_sigaction) : ret;
8455 if (ret < 0)
8456 {
8457 // Should only get here as a result of coding errors.
8458 semsg(_(e_could_not_set_handler_for_timeout_str), strerror(errno));
8459 return &timeout_flag;
8460 }
8461 timer_handler_active = TRUE;
8462
8463 // Set up the interval timer once the alarm handler is in place.
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008464 ret = setitimer(ITIMER_REAL, &interval, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008465 if (ret < 0)
8466 {
8467 // Should only get here as a result of coding errors.
8468 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8469 stop_timeout();
8470 return &timeout_flag;
8471 }
8472
8473 timer_active = TRUE;
8474 return &timeout_flag;
8475}
8476# endif // HAVE_TIMER_CREATE
8477#endif // FEAT_RELTIME