blob: 28113fcb3b40649049e2464dd720773728c8dbf4 [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 Moolenaard99df422016-01-29 23:20:40 +0100102static RETSIGTYPE 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 Moolenaard99df422016-01-29 23:20:40 +0100174static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000176#if defined(SIGTSTP)
177static RETSIGTYPE sig_tstp SIGPROTOARG;
178// volatile because it is used in signal handler sig_tstp() and sigcont_handler().
179static volatile sig_atomic_t in_mch_suspend = FALSE;
180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100182static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200184#if defined(SIGUSR1)
185static RETSIGTYPE catch_sigusr1 SIGPROTOARG;
186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100188static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000190#if defined(SIGALRM) && defined(FEAT_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100192static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100193// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200194static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100196static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100198static void catch_int_signal(void);
199static void set_signals(void);
200static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200201#ifdef HAVE_SIGPROCMASK
202# define SIGSET_DECL(set) sigset_t set;
203# define BLOCK_SIGNALS(set) block_signals(set)
204# define UNBLOCK_SIGNALS(set) unblock_signals(set)
205#else
206# define SIGSET_DECL(set)
207# define BLOCK_SIGNALS(set) do { /**/ } while (0)
208# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
209#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100210static int have_wildcard(int, char_u **);
211static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100213static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
215#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000216# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217#endif
218
Bram Moolenaar0f873732019-12-05 20:28:46 +0100219// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200220static volatile sig_atomic_t do_resize = FALSE;
dbivolaruab16ad32021-12-29 19:41:47 +0000221// volatile because it is used in signal handler sig_tstp().
222static volatile sig_atomic_t got_tstp = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223static char_u *extra_shell_arg = NULL;
224static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100225// volatile because it is used in signal handler deathtrap().
226static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
227// volatile because it is used in signal handler deathtrap().
228static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100230#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
231static int dont_check_job_ended = 0;
232#endif
233
Bram Moolenaar26e86442020-05-17 14:06:16 +0200234// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
235static tmode_T mch_cur_tmode = TMODE_COOK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236
237#ifdef USE_XSMP
238typedef struct
239{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100240 SmcConn smcconn; // The SM connection ID
241 IceConn iceconn; // The ICE connection ID
242 char *clientid; // The client ID for the current smc session
243 Bool save_yourself; // If we're in the middle of a save_yourself
244 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245} xsmp_config_T;
246
247static xsmp_config_T xsmp;
248#endif
249
250#ifdef SYS_SIGLIST_DECLARED
251/*
252 * I have seen
253 * extern char *_sys_siglist[NSIG];
254 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
255 * that describe the signals. That is nearly what we want here. But
256 * autoconf does only check for sys_siglist (without the underscore), I
257 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100258 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 */
260#endif
261
262static struct signalinfo
263{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100264 int sig; // Signal number, eg. SIGSEGV etc
265 char *name; // Signal name (not char_u!).
266 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267} signal_info[] =
268{
269#ifdef SIGHUP
270 {SIGHUP, "HUP", TRUE},
271#endif
272#ifdef SIGQUIT
273 {SIGQUIT, "QUIT", TRUE},
274#endif
275#ifdef SIGILL
276 {SIGILL, "ILL", TRUE},
277#endif
278#ifdef SIGTRAP
279 {SIGTRAP, "TRAP", TRUE},
280#endif
281#ifdef SIGABRT
282 {SIGABRT, "ABRT", TRUE},
283#endif
284#ifdef SIGEMT
285 {SIGEMT, "EMT", TRUE},
286#endif
287#ifdef SIGFPE
288 {SIGFPE, "FPE", TRUE},
289#endif
290#ifdef SIGBUS
291 {SIGBUS, "BUS", TRUE},
292#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100293#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100294 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 {SIGSEGV, "SEGV", TRUE},
296#endif
297#ifdef SIGSYS
298 {SIGSYS, "SYS", TRUE},
299#endif
300#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100301 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302#endif
303#ifdef SIGTERM
304 {SIGTERM, "TERM", TRUE},
305#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100306#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {SIGVTALRM, "VTALRM", TRUE},
308#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000309#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100310 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
311 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 {SIGPROF, "PROF", TRUE},
313#endif
314#ifdef SIGXCPU
315 {SIGXCPU, "XCPU", TRUE},
316#endif
317#ifdef SIGXFSZ
318 {SIGXFSZ, "XFSZ", TRUE},
319#endif
320#ifdef SIGUSR1
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200321 {SIGUSR1, "USR1", FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000323#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100324 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 {SIGUSR2, "USR2", TRUE},
326#endif
327#ifdef SIGINT
328 {SIGINT, "INT", FALSE},
329#endif
330#ifdef SIGWINCH
331 {SIGWINCH, "WINCH", FALSE},
332#endif
333#ifdef SIGTSTP
334 {SIGTSTP, "TSTP", FALSE},
335#endif
336#ifdef SIGPIPE
337 {SIGPIPE, "PIPE", FALSE},
338#endif
339 {-1, "Unknown!", FALSE}
340};
341
Bram Moolenaar25724922009-07-14 15:38:41 +0000342 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100343mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000344{
345 if (p_verbose >= 5)
346 {
347 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100348 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000349 verbose_leave();
350 }
351# ifdef VMS
352 return chdir(vms_fixfilename(path));
353# else
354 return chdir(path);
355# endif
356}
357
Bram Moolenaar0f873732019-12-05 20:28:46 +0100358// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100359#if defined(ECHOE) && defined(ICANON) \
360 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
361 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200362# define NEW_TTY_SYSTEM
363#endif
364
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000365/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100366 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100369mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200371 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100372 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100373 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374}
375
376/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100377 * Function passed to inchar_loop() to handle window resizing.
378 * If "check_only" is TRUE: Return whether there was a resize.
379 * If "check_only" is FALSE: Deal with the window resized.
380 */
381 static int
382resize_func(int check_only)
383{
384 if (check_only)
385 return do_resize;
386 while (do_resize)
387 handle_resize();
388 return FALSE;
389}
390
391/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000392 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 * Get a characters from the keyboard.
394 * Return the number of characters that are available.
395 * If wtime == 0 do not wait for characters.
396 * If wtime == n wait a short time for characters.
397 * If wtime == -1 wait forever for characters.
398 */
399 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100400mch_inchar(
401 char_u *buf,
402 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100403 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100404 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100406 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
407 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408}
409
410 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100411handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412{
413 do_resize = FALSE;
414 shell_resized();
415}
416
417/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200418 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 */
420 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100421mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200423 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424}
425
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200426#if defined(FEAT_TERMINAL) || defined(PROTO)
427/*
428 * Check for any pending input or messages.
429 */
430 int
431mch_check_messages(void)
432{
433 return WaitForChar(0L, NULL, TRUE);
434}
435#endif
436
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
438# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000439# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440# endif
441# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
442# include <sys/sysctl.h>
443# endif
444# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
445# include <sys/sysinfo.h>
446# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100447# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100448# include <mach/mach_host.h>
449# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451
452/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000453 * Return total amount of memory available in Kbyte.
454 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100457mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100460 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461
Bram Moolenaar362dc332018-03-05 21:59:37 +0100462# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100463 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100464 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100465 mach_port_t host = mach_host_self();
466 kern_return_t kret;
467# ifdef HOST_VM_INFO64
468 struct vm_statistics64 vm_stat;
469 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
Bram Moolenaar988615f2018-02-27 17:58:20 +0100471 kret = host_statistics64(host, HOST_VM_INFO64,
472 (host_info64_t)&vm_stat, &count);
473# else
474 struct vm_statistics vm_stat;
475 natural_t count = HOST_VM_INFO_COUNT;
476
477 kret = host_statistics(host, HOST_VM_INFO,
478 (host_info_t)&vm_stat, &count);
479# endif
480 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100481 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100482 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
483 + vm_stat.inactive_count
484# ifdef MAC_OS_X_VERSION_10_9
485 + vm_stat.compressor_page_count
486# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100487 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100488 mach_port_deallocate(mach_task_self(), host);
489 }
490# endif
491
492# ifdef HAVE_SYSCTL
493 if (mem == 0)
494 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100495 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100496 int mib[2];
497 size_t len = sizeof(long_u);
498# ifdef HW_USERMEM64
499 long_u physmem;
500# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100501 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100502 union {
503 int_u u32;
504 long_u u64;
505 } physmem;
506# endif
507
508 mib[0] = CTL_HW;
509# ifdef HW_USERMEM64
510 mib[1] = HW_USERMEM64;
511# else
512 mib[1] = HW_USERMEM;
513# endif
514 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
515 {
516# ifdef HW_USERMEM64
517 mem = (long_u)physmem;
518# else
519 if (len == sizeof(physmem.u64))
520 mem = (long_u)physmem.u64;
521 else
522 mem = (long_u)physmem.u32;
523# endif
524 }
525 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200528# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 if (mem == 0)
530 {
531 struct sysinfo sinfo;
532
Bram Moolenaar0f873732019-12-05 20:28:46 +0100533 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000535 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200536# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100537 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000538 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
539 {
540 sinfo.mem_unit = sinfo.mem_unit >> 1;
541 --shiftright;
542 }
543 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200544# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200546# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200549# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200551# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (mem == 0)
553 {
554 long pagesize, pagecount;
555
Bram Moolenaar0f873732019-12-05 20:28:46 +0100556 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 pagesize = sysconf(_SC_PAGESIZE);
558 pagecount = sysconf(_SC_PHYS_PAGES);
559 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000560 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100561 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000562 while (shiftright > 0 && (pagesize & 1) == 0)
563 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000564 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000565 --shiftright;
566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
Bram Moolenaar0f873732019-12-05 20:28:46 +0100572 // Return the minimum of the physical memory and the user limit, because
573 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200574# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 {
576 struct rlimit rlp;
577
578 if (getrlimit(RLIMIT_DATA, &rlp) == 0
579 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200580# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200582# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000583 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000585 {
586 mem = (long_u)rlp.rlim_cur;
587 shiftright = 10;
588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000593 return mem >> shiftright;
594 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595}
596#endif
597
Bram Moolenaar0981c872020-08-23 14:28:37 +0200598/*
599 * "flags": MCH_DELAY_IGNOREINPUT - don't read input
600 * MCH_DELAY_SETTMODE - use settmode() even for short delays
601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200603mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200605 tmode_T old_tmode;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200606 int call_settmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000607#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100608 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
Bram Moolenaar0981c872020-08-23 14:28:37 +0200611 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100613 // Go to cooked mode without echo, to allow SIGINT interrupting us
614 // here. But we don't want QUIT to kill us (CTRL-\ used in a
615 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200616 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000617 in_mch_delay = TRUE;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200618 call_settmode = mch_cur_tmode == TMODE_RAW
619 && (msec > 500 || (flags & MCH_DELAY_SETTMODE));
620 if (call_settmode)
621 {
622 old_tmode = mch_cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 settmode(TMODE_SLEEP);
Bram Moolenaar80361a52020-10-05 21:39:25 +0200624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
626 /*
627 * Everybody sleeps in a different way...
628 * Prefer nanosleep(), some versions of usleep() can only sleep up to
629 * one second.
630 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000631#ifdef FEAT_MZSCHEME
632 do
633 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100634 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000635 if (total > p_mzq)
636 msec = p_mzq;
637 else
638 msec = total;
639 total -= msec;
640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641#ifdef HAVE_NANOSLEEP
642 {
643 struct timespec ts;
644
645 ts.tv_sec = msec / 1000;
646 ts.tv_nsec = (msec % 1000) * 1000000;
647 (void)nanosleep(&ts, NULL);
648 }
649#else
650# ifdef HAVE_USLEEP
651 while (msec >= 1000)
652 {
653 usleep((unsigned int)(999 * 1000));
654 msec -= 999;
655 }
656 usleep((unsigned int)(msec * 1000));
657# else
658# ifndef HAVE_SELECT
659 poll(NULL, 0, (int)msec);
660# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
662 struct timeval tv;
663
664 tv.tv_sec = msec / 1000;
665 tv.tv_usec = (msec % 1000) * 1000;
Bram Moolenaar0981c872020-08-23 14:28:37 +0200666 // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
667 // a patch from Sun to fix this. Reported by Gunnar Pedersen.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 select(0, NULL, NULL, NULL, &tv);
669 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100670# endif // HAVE_SELECT
671# endif // HAVE_NANOSLEEP
672#endif // HAVE_USLEEP
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000673#ifdef FEAT_MZSCHEME
674 }
675 while (total > 0);
676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
Bram Moolenaar80361a52020-10-05 21:39:25 +0200678 if (call_settmode)
Bram Moolenaar26e86442020-05-17 14:06:16 +0200679 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000680 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 }
682 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200683 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684}
685
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000686#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
688# define HAVE_CHECK_STACK_GROWTH
689/*
690 * Support for checking for an almost-out-of-stack-space situation.
691 */
692
693/*
694 * Return a pointer to an item on the stack. Used to find out if the stack
695 * grows up or down.
696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697static int stack_grows_downwards;
698
699/*
700 * Find out if the stack grows upwards or downwards.
701 * "p" points to a variable on the stack of the caller.
702 */
703 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100704check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705{
706 int i;
707
708 stack_grows_downwards = (p > (char *)&i);
709}
710#endif
711
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000712#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713static char *stack_limit = NULL;
714
715#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
716# include <pthread.h>
717# include <pthread_np.h>
718#endif
719
720/*
721 * Find out until how var the stack can grow without getting into trouble.
722 * Called when starting up and when switching to the signal stack in
723 * deathtrap().
724 */
725 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100726get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
728 struct rlimit rlp;
729 int i;
730 long lim;
731
Bram Moolenaar0f873732019-12-05 20:28:46 +0100732 // Set the stack limit to 15/16 of the allowable size. Skip this when the
733 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 if (getrlimit(RLIMIT_STACK, &rlp) == 0
735 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
736# ifdef RLIM_INFINITY
737 && rlp.rlim_cur != RLIM_INFINITY
738# endif
739 )
740 {
741 lim = (long)rlp.rlim_cur;
742#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
743 {
744 pthread_attr_t attr;
745 size_t size;
746
Bram Moolenaar0f873732019-12-05 20:28:46 +0100747 // On FreeBSD the initial thread always has a fixed stack size, no
748 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 pthread_attr_init(&attr);
750 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
751 {
752 pthread_attr_getstacksize(&attr, &size);
753 if (lim > (long)size)
754 lim = (long)size;
755 }
756 pthread_attr_destroy(&attr);
757 }
758#endif
759 if (stack_grows_downwards)
760 {
761 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
762 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100763 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 stack_limit = (char *)((long)&i / 16L);
765 }
766 else
767 {
768 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
769 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100770 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 }
772 }
773}
774
775/*
776 * Return FAIL when running out of stack space.
777 * "p" must point to any variable local to the caller that's on the stack.
778 */
779 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100780mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781{
782 if (stack_limit != NULL)
783 {
784 if (stack_grows_downwards)
785 {
786 if (p < stack_limit)
787 return FAIL;
788 }
789 else if (p > stack_limit)
790 return FAIL;
791 }
792 return OK;
793}
794#endif
795
796#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
797/*
798 * Support for using the signal stack.
799 * This helps when we run out of stack space, which causes a SIGSEGV. The
800 * signal handler then must run on another stack, since the normal stack is
801 * completely full.
802 */
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100805static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100807static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808# endif
809
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200810/*
811 * Get a size of signal stack.
812 * Preference (if available): sysconf > SIGSTKSZ > guessed size
813 */
814static long int get_signal_stack_size()
815{
816# ifdef HAVE_SYSCONF_SIGSTKSZ
817 long int size = -1;
818
819 // return size only if sysconf doesn't return an error
820 if ((size = sysconf(_SC_SIGSTKSZ)) > -1)
821 return size;
822# endif
823
824# ifdef SIGSTKSZ
825 // if sysconf() isn't available or gives error, return SIGSTKSZ
826 // if defined
827 return SIGSTKSZ;
828# endif
829
830 // otherwise guess the size
831 return 8000;
832}
833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834static char *signal_stack;
835
836 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100837init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 if (signal_stack != NULL)
840 {
841# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842# ifdef HAVE_SS_BASE
843 sigstk.ss_base = signal_stack;
844# else
845 sigstk.ss_sp = signal_stack;
846# endif
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200847 sigstk.ss_size = get_signal_stack_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 sigstk.ss_flags = 0;
849 (void)sigaltstack(&sigstk, NULL);
850# else
851 sigstk.ss_sp = signal_stack;
852 if (stack_grows_downwards)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200853 sigstk.ss_sp += get_signal_stack_size() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 sigstk.ss_onstack = 0;
855 (void)sigstack(&sigstk, NULL);
856# endif
857 }
858}
859#endif
860
861/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000862 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * will barf when the second argument to signal() is ``wrong''.
864 * Let me try it with a few tricky defines from my own osdef.h (jw).
865 */
866#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 static RETSIGTYPE
868sig_winch SIGDEFARG(sigarg)
869{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100870 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
872 do_resize = TRUE;
873 SIGRETURN;
874}
875#endif
876
dbivolaruab16ad32021-12-29 19:41:47 +0000877#if defined(SIGTSTP)
878 static RETSIGTYPE
879sig_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.
dbivolaruab16ad32021-12-29 19:41:47 +0000893 signal(SIGTSTP, (RETSIGTYPE (*)())sig_tstp);
xtkobacbef12e2022-02-27 12:31:52 +0000894#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000895 SIGRETURN;
896}
897#endif
898
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 static RETSIGTYPE
901catch_sigint SIGDEFARG(sigarg)
902{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100903 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
905 got_int = TRUE;
906 SIGRETURN;
907}
908#endif
909
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200910#if defined(SIGUSR1)
911 static RETSIGTYPE
912catch_sigusr1 SIGDEFARG(sigarg)
913{
914 // this is not required on all systems, but it doesn't hurt anybody
915 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
916 got_sigusr1 = TRUE;
917 SIGRETURN;
918}
919#endif
920
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 static RETSIGTYPE
923catch_sigpwr SIGDEFARG(sigarg)
924{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100925 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000926 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 /*
928 * I'm not sure we get the SIGPWR signal when the system is really going
929 * down or when the batteries are almost empty. Just preserve the swap
930 * files and don't exit, that can't do any harm.
931 */
932 ml_sync_all(FALSE, FALSE);
933 SIGRETURN;
934}
935#endif
936
937#ifdef SET_SIG_ALARM
938/*
939 * signal function for alarm().
940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 static RETSIGTYPE
942sig_alarm SIGDEFARG(sigarg)
943{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100944 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 sig_alarm_called = TRUE;
946 SIGRETURN;
947}
948#endif
949
Bram Moolenaaredce7422019-01-20 18:39:30 +0100950#if (defined(HAVE_SETJMP_H) \
951 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
952 || defined(FEAT_LIBCALL))) \
953 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100954# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100955
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100956// argument to SETJMP()
957static JMP_BUF lc_jump_env;
958
959# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100960// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100961// Volatile because it is used in signal handlers.
962static volatile sig_atomic_t lc_signal;
963# endif
964
965// TRUE when lc_jump_env is valid.
966// Volatile because it is used in signal handler deathtrap().
967static volatile sig_atomic_t lc_active INIT(= FALSE);
968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969/*
970 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100971 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100973 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 * Usage:
975 * mch_startjmp();
976 * if (SETJMP(lc_jump_env) != 0)
977 * {
978 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100979 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 * }
981 * else
982 * {
983 * do_the_work;
984 * mch_endjmp();
985 * }
986 * Note: Can't move SETJMP() here, because a function calling setjmp() must
987 * not return before the saved environment is used.
988 * Returns OK for normal return, FAIL when the protected code caused a
989 * problem and LONGJMP() was used.
990 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100991 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100992mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100994# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100996# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 lc_active = TRUE;
998}
999
Bram Moolenaar113e1072019-01-20 15:30:40 +01001000 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001001mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003 lc_active = FALSE;
1004}
1005
Bram Moolenaar113e1072019-01-20 15:30:40 +01001006 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001007mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
1009# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001010 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1011 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 init_signal_stack();
1013# endif
1014}
1015#endif
1016
1017/*
1018 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001019 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001021 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1022 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 */
1024 static RETSIGTYPE
1025deathtrap SIGDEFARG(sigarg)
1026{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001027 static int entered = 0; // count the number of times we got here.
1028 // Note: when memory has been corrupted
1029 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030#ifdef SIGHASARG
1031 int i;
1032#endif
1033
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001034#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 /*
1036 * Catch a crash in protected code.
1037 * Restores the environment saved in lc_jump_env, which looks like
1038 * SETJMP() returns 1.
1039 */
1040 if (lc_active)
1041 {
1042# if defined(SIGHASARG)
1043 lc_signal = sigarg;
1044# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001045 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001047 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
1049#endif
1050
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001051#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001052# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001053 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1054 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1055 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001056 if (in_mch_delay && sigarg == SIGQUIT)
1057 SIGRETURN;
1058# endif
1059
Bram Moolenaar0f873732019-12-05 20:28:46 +01001060 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1061 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1062 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001063 if (entered == 0
1064 && (0
1065# ifdef SIGHUP
1066 || sigarg == SIGHUP
1067# endif
1068# ifdef SIGQUIT
1069 || sigarg == SIGQUIT
1070# endif
1071# ifdef SIGTERM
1072 || sigarg == SIGTERM
1073# endif
1074# ifdef SIGPWR
1075 || sigarg == SIGPWR
1076# endif
1077# ifdef SIGUSR1
1078 || sigarg == SIGUSR1
1079# endif
1080# ifdef SIGUSR2
1081 || sigarg == SIGUSR2
1082# endif
1083 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001084 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001085 SIGRETURN;
1086#endif
1087
Bram Moolenaar0f873732019-12-05 20:28:46 +01001088 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 ++entered;
1090
Bram Moolenaar0f873732019-12-05 20:28:46 +01001091 // Executing autocommands is likely to use more stack space than we have
1092 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001093 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001094
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001096 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 set_vim_var_nr(VV_DYING, (long)entered);
1098#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001099 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001101#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001102 // Since we are now using the signal stack, need to reset the stack
1103 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 get_stack_limit();
1105#endif
1106
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001107#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001108 // This is for opening gdb the moment Vim crashes.
1109 // You need to manually adjust the file name and Vim executable name.
1110 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001111 {
1112# define VI_GDB_FILE "/tmp/vimgdb"
1113# define VIM_NAME "/usr/bin/vim"
1114 FILE *fp = fopen(VI_GDB_FILE, "w");
1115 if (fp)
1116 {
1117 fprintf(fp,
1118 "file %s\n"
1119 "attach %d\n"
1120 "set height 1000\n"
1121 "bt full\n"
1122 , VIM_NAME, getpid());
1123 fclose(fp);
1124 system("xterm -e gdb -x "VI_GDB_FILE);
1125 unlink(VI_GDB_FILE);
1126 }
1127 }
1128#endif
1129
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001131 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 for (i = 0; signal_info[i].sig != -1; i++)
1133 if (sigarg == signal_info[i].sig)
1134 break;
1135 deadly_signal = sigarg;
1136#endif
1137
Bram Moolenaar0f873732019-12-05 20:28:46 +01001138 full_screen = FALSE; // don't write message to the GUI, it might be
1139 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 /*
1141 * If something goes wrong after entering here, we may get here again.
1142 * When this happens, give a message and try to exit nicely (resetting the
1143 * terminal mode, etc.)
1144 * When this happens twice, just exit, don't even try to give a message,
1145 * stack may be corrupt or something weird.
1146 * When this still happens again (or memory was corrupted in such a way
1147 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1148 */
1149 if (entered >= 3)
1150 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001151 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 may_core_dump();
1153 if (entered >= 4)
1154 _exit(8);
1155 exit(7);
1156 }
1157 if (entered == 2)
1158 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001159 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001160 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 out_flush();
1162 getout(1);
1163 }
1164
Bram Moolenaar0f873732019-12-05 20:28:46 +01001165 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001167 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 signal_info[i].name);
1169#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001170 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001172
Bram Moolenaar0f873732019-12-05 20:28:46 +01001173 // Preserve files and exit. This sets the really_exiting flag to prevent
1174 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001175 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
Bram Moolenaar0f873732019-12-05 20:28:46 +01001177 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001178
Bram Moolenaar009b2592004-10-24 19:18:58 +00001179#ifdef NBDEBUG
1180 reset_signals();
1181 may_core_dump();
1182 abort();
1183#endif
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 SIGRETURN;
1186}
1187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001189 * Invoked after receiving SIGCONT. We don't know what happened while
1190 * sleeping, deal with part of that.
1191 */
1192 static void
1193after_sigcont(void)
1194{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001195 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1196 // again later.
1197 oldtitle_outdated = TRUE;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001198
Bram Moolenaar2e310482018-08-21 13:09:10 +02001199 settmode(TMODE_RAW);
1200 need_check_timestamps = TRUE;
1201 did_check_timestamps = FALSE;
1202}
1203
1204#if defined(SIGCONT)
1205static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001206
1207/*
1208 * With multi-threading, suspending might not work immediately. Catch the
1209 * SIGCONT signal, which will be used as an indication whether the suspending
1210 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001211 *
1212 * On Linux, signal is not always handled immediately either.
1213 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001214 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001215 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001216 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001218static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001219static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220
1221/*
1222 * signal handler for SIGCONT
1223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 static RETSIGTYPE
1225sigcont_handler SIGDEFARG(sigarg)
1226{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001227 if (in_mch_suspend)
1228 {
1229 sigcont_received = TRUE;
1230 }
1231 else
1232 {
1233 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1234 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1235 // back to a sane mode. We should redraw, but we can't really do that
1236 // in a signal handler, do a redraw later.
1237 after_sigcont();
1238 redraw_later(CLEAR);
1239 cursor_on_force();
1240 out_flush();
1241 }
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 SIGRETURN;
1244}
1245#endif
1246
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001247#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001248# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001249static void *clip_star_save = NULL;
1250static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001251# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001252
1253/*
1254 * Called when Vim is going to sleep or execute a shell command.
1255 * We can't respond to requests for the X selections. Lose them, otherwise
1256 * other applications will hang. But first copy the text to cut buffer 0.
1257 */
1258 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001259loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001260{
1261 if (clip_star.owned || clip_plus.owned)
1262 {
1263 x11_export_final_selection();
1264 if (clip_star.owned)
1265 clip_lose_selection(&clip_star);
1266 if (clip_plus.owned)
1267 clip_lose_selection(&clip_plus);
1268 if (x11_display != NULL)
1269 XFlush(x11_display);
1270 }
1271}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001272
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001273# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001274/*
1275 * Save clipboard text to restore later.
1276 */
1277 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001278save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001279{
1280 if (clip_star.owned)
1281 clip_star_save = get_register('*', TRUE);
1282 if (clip_plus.owned)
1283 clip_plus_save = get_register('+', TRUE);
1284}
1285
1286/*
1287 * Restore clipboard text if no one own the X selection.
1288 */
1289 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001290restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001291{
1292 if (clip_star_save != NULL)
1293 {
1294 if (!clip_gen_owner_exists(&clip_star))
1295 put_register('*', clip_star_save);
1296 else
1297 free_register(clip_star_save);
1298 clip_star_save = NULL;
1299 }
1300 if (clip_plus_save != NULL)
1301 {
1302 if (!clip_gen_owner_exists(&clip_plus))
1303 put_register('+', clip_plus_save);
1304 else
1305 free_register(clip_plus_save);
1306 clip_plus_save = NULL;
1307 }
1308}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001309# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001310#endif
1311
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312/*
1313 * If the machine has job control, use it to suspend the program,
1314 * otherwise fake it by starting a new shell.
1315 */
1316 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001317mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001319 if (ignore_sigtstp)
1320 return;
1321
Bram Moolenaar041c7102020-05-30 18:14:57 +02001322#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001323 in_mch_suspend = TRUE;
1324
Bram Moolenaar0f873732019-12-05 20:28:46 +01001325 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001327 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328
1329# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001330 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001332# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 sigcont_received = FALSE;
1334# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001335
Bram Moolenaar0f873732019-12-05 20:28:46 +01001336 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001337
1338# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001339 /*
1340 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001341 * immediately, but somehow not all the time, probably because it's handled
1342 * in another thread. Do not call pause() because there would be race
1343 * condition which would hang Vim if signal happened in between the test of
1344 * sigcont_received and the call to pause(). If signal is not yet received,
1345 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1346 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001347 */
1348 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001349 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001350
Bram Moolenaar262735e2009-07-14 10:20:22 +00001351 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001352 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001355 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaar2e310482018-08-21 13:09:10 +02001357 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358#else
1359 suspend_shell();
1360#endif
1361}
1362
1363 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001364mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 Columns = 80;
1367 Rows = 24;
1368
1369 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001370
1371#ifdef SIGTSTP
1372 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1373 // that indicates the shell (or program) that launched us does not support
1374 // tty job control and thus we should ignore that signal. If invoked as a
1375 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
1376 ignore_sigtstp = restricted || SIG_IGN == signal(SIGTSTP, SIG_ERR);
1377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001379
Bram Moolenaar56718732006-03-15 22:53:57 +00001380#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001381 mac_conv_init();
1382#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001383#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1384 win_clip_init();
1385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386}
1387
1388 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001389set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390{
1391#if defined(SIGWINCH)
1392 /*
1393 * WINDOW CHANGE signal is handled with sig_winch().
1394 */
1395 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1396#endif
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001399 // See mch_init() for the conditions under which we ignore SIGTSTP.
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001400 // In the GUI default TSTP processing is OK.
1401 // Checking both gui.in_use and gui.starting because gui.in_use is not set
1402 // at this point (set after menus are displayed), but gui.starting is set.
1403 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN
1404# ifdef FEAT_GUI
1405 : gui.in_use || gui.starting ? SIG_DFL
1406# endif
1407 : (RETSIGTYPE (*)())sig_tstp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001409#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 signal(SIGCONT, sigcont_handler);
1411#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001412#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 /*
1414 * We want to ignore breaking of PIPEs.
1415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 signal(SIGPIPE, SIG_IGN);
1417#endif
1418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001420 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421#endif
1422
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001423#ifdef SIGUSR1
1424 /*
1425 * Call user's handler on SIGUSR1
1426 */
1427 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
1428#endif
1429
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 /*
1431 * Ignore alarm signals (Perl's alarm() generates it).
1432 */
1433#ifdef SIGALRM
1434 signal(SIGALRM, SIG_IGN);
1435#endif
1436
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001437#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 /*
1439 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1440 * work will be lost.
1441 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1443#endif
1444
1445 /*
1446 * Arrange for other signals to gracefully shutdown Vim.
1447 */
1448 catch_signals(deathtrap, SIG_ERR);
1449
1450#if defined(FEAT_GUI) && defined(SIGHUP)
1451 /*
1452 * When the GUI is running, ignore the hangup signal.
1453 */
1454 if (gui.in_use)
1455 signal(SIGHUP, SIG_IGN);
1456#endif
1457}
1458
Bram Moolenaardf177f62005-02-22 08:39:57 +00001459#if defined(SIGINT) || defined(PROTO)
1460/*
1461 * Catch CTRL-C (only works while in Cooked mode).
1462 */
1463 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001464catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001465{
1466 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1467}
1468#endif
1469
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001471reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472{
1473 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001474#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001475 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 signal(SIGCONT, SIG_DFL);
1477#endif
1478}
1479
1480 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001481catch_signals(
1482 RETSIGTYPE (*func_deadly)(),
1483 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 int i;
1486
1487 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (signal_info[i].deadly)
1490 {
1491#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1492 struct sigaction sa;
1493
Bram Moolenaar0f873732019-12-05 20:28:46 +01001494 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 sa.sa_handler = func_deadly;
1496 sigemptyset(&sa.sa_mask);
1497# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001498 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1499 // thread handling in combination with using the alternate stack:
1500 // pthread library functions try to use the stack pointer to
1501 // identify the current thread, causing a SEGV signal, which
1502 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 sa.sa_flags = 0;
1504# else
1505 sa.sa_flags = SA_ONSTACK;
1506# endif
1507 sigaction(signal_info[i].sig, &sa, NULL);
1508#else
1509# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1510 struct sigvec sv;
1511
Bram Moolenaar0f873732019-12-05 20:28:46 +01001512 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 sv.sv_handler = func_deadly;
1514 sv.sv_mask = 0;
1515 sv.sv_flags = SV_ONSTACK;
1516 sigvec(signal_info[i].sig, &sv, NULL);
1517# else
1518 signal(signal_info[i].sig, func_deadly);
1519# endif
1520#endif
1521 }
1522 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001523 {
1524 // Deal with non-deadly signals.
1525#ifdef SIGTSTP
1526 signal(signal_info[i].sig,
1527 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1528 ? SIG_IGN : func_other);
1529#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001531#endif
1532 }
1533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534}
1535
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001536#ifdef HAVE_SIGPROCMASK
1537 static void
1538block_signals(sigset_t *set)
1539{
1540 sigset_t newset;
1541 int i;
1542
1543 sigemptyset(&newset);
1544
1545 for (i = 0; signal_info[i].sig != -1; i++)
1546 sigaddset(&newset, signal_info[i].sig);
1547
Bram Moolenaar2e310482018-08-21 13:09:10 +02001548# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001549 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001550 sigaddset(&newset, SIGCONT);
1551# endif
1552
1553 sigprocmask(SIG_BLOCK, &newset, set);
1554}
1555
1556 static void
1557unblock_signals(sigset_t *set)
1558{
1559 sigprocmask(SIG_SETMASK, set, NULL);
1560}
1561#endif
1562
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001564 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001565 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1566 * return TRUE
1567 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1568 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001569 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001570 * Returns TRUE when Vim should exit.
1571 */
1572 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001573vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001574{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001575 static int got_signal = 0;
1576 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001577
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001578 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001579 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001580 case SIGNAL_BLOCK: blocked = TRUE;
1581 break;
1582
1583 case SIGNAL_UNBLOCK: blocked = FALSE;
1584 if (got_signal != 0)
1585 {
1586 kill(getpid(), got_signal);
1587 got_signal = 0;
1588 }
1589 break;
1590
1591 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001592 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001593 got_signal = sig;
1594#ifdef SIGPWR
1595 if (sig != SIGPWR)
1596#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001597 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001598 break;
1599 }
1600 return FALSE;
1601}
1602
1603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 * Check_win checks whether we have an interactive stdout.
1605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001607mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (isatty(1))
1610 return OK;
1611 return FAIL;
1612}
1613
1614/*
1615 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1616 */
1617 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001618mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 if (isatty(read_cmd_fd))
1621 return TRUE;
1622 return FALSE;
1623}
1624
1625#ifdef FEAT_X11
1626
Bram Moolenaar651fca82021-11-29 20:39:38 +00001627# if defined(ELAPSED_TIMEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629/*
1630 * Give a message about the elapsed time for opening the X window.
1631 */
1632 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001633xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001635 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636}
1637# endif
1638#endif
1639
Bram Moolenaar651fca82021-11-29 20:39:38 +00001640#if defined(FEAT_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641/*
1642 * A few functions shared by X11 title and clipboard code.
1643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645static int got_x_error = FALSE;
1646
1647/*
1648 * X Error handler, otherwise X just exits! (very rude) -- webb
1649 */
1650 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001651x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001653 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 STRCAT(IObuff, _("\nVim: Got X error\n"));
1655
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001656 // In the GUI we cannot print a message and continue, because no X calls
1657 // are allowed here (causes my system to hang). Silently continuing seems
1658 // like the best alternative. Do preserve files, in case we crash.
1659 ml_sync_all(FALSE, FALSE);
1660
1661#ifdef FEAT_GUI
1662 if (!gui.in_use)
1663#endif
1664 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaar0f873732019-12-05 20:28:46 +01001666 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667}
1668
1669/*
1670 * Another X Error handler, just used to check for errors.
1671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001673x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674{
1675 got_x_error = TRUE;
1676 return 0;
1677}
1678
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001679/*
1680 * Return TRUE when connection to the X server is desired.
1681 */
1682 static int
1683x_connect_to_server(void)
1684{
1685 // No point in connecting if we are exiting or dying.
1686 if (exiting || v_dying)
1687 return FALSE;
1688
1689#if defined(FEAT_CLIENTSERVER)
1690 if (x_force_connect)
1691 return TRUE;
1692#endif
1693 if (x_no_connect)
1694 return FALSE;
1695
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001696 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001697 if (clip_exclude_prog != NULL)
1698 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001699 // Just in case we get called recursively, return FALSE. This could
1700 // happen if vpeekc() is used while executing the prog and it causes a
1701 // related callback to be invoked.
1702 if (regprog_in_use(clip_exclude_prog))
1703 return FALSE;
1704
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001705 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1706 return FALSE;
1707 }
1708 return TRUE;
1709}
1710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001712# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713/*
1714 * An X IO Error handler, used to catch error while opening the display.
1715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001717x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001719 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001721# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001722 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001723# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724}
1725# endif
1726
1727/*
1728 * An X IO Error handler, used to catch terminal errors.
1729 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001730static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001733x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
1735 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001736 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 x11_window = 0;
1738 x11_display = NULL;
1739 xterm_Shell = (Widget)0;
1740
Bram Moolenaar0f873732019-12-05 20:28:46 +01001741 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001743# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001744 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001745# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001747
1748/*
1749 * If the X11 connection was lost try to restore it.
1750 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001751 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001752 */
1753 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001754may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001755{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001756 // No point in restoring the connecting if we are exiting or dying.
1757 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001758 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001759 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001760
1761# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001762 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001763 if (app_context != (XtAppContext)NULL)
1764 {
1765 XtDestroyApplicationContext(app_context);
1766 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001767 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001768 }
1769# endif
1770
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001771 setup_term_clip();
1772 get_x11_title(FALSE);
1773 }
1774}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001775
1776 void
1777ex_xrestore(exarg_T *eap)
1778{
1779 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1780 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001781 if (xterm_display_allocated)
1782 vim_free(xterm_display);
1783 xterm_display = (char *)vim_strsave(eap->arg);
1784 xterm_display_allocated = TRUE;
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001785 }
1786 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001787 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001788
1789 clear_xterm_clip();
1790 x11_window = 0;
1791 xterm_dpy_retry_count = 5; // Try reconnecting five times
1792 may_restore_clipboard();
1793}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794#endif
1795
1796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 * Test if "dpy" and x11_window are valid by getting the window title.
1798 * I don't actually want it yet, so there may be a simpler call to use, but
1799 * this will cause the error handler x_error_check() to be called if anything
1800 * is wrong, such as the window pointer being invalid (as can happen when the
1801 * user changes his DISPLAY, but not his WINDOWID) -- webb
1802 */
1803 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001804test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805{
1806 int (*old_handler)();
1807 XTextProperty text_prop;
1808
1809 old_handler = XSetErrorHandler(x_error_check);
1810 got_x_error = FALSE;
1811 if (XGetWMName(dpy, x11_window, &text_prop))
1812 XFree((void *)text_prop.value);
1813 XSync(dpy, False);
1814 (void)XSetErrorHandler(old_handler);
1815
1816 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001817 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
1819 return (got_x_error ? FAIL : OK);
1820}
1821#endif
1822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824#ifdef FEAT_X11
1825
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001826static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
1828/*
1829 * try to get x11 window and display
1830 *
1831 * return FAIL for failure, OK otherwise
1832 */
1833 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001834get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835{
1836 char *winid;
1837 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001838#define XD_NONE 0 // x11_display not set here
1839#define XD_HERE 1 // x11_display opened here
1840#define XD_GUI 2 // x11_display used from gui.dpy
1841#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 static int x11_display_from = XD_NONE;
1843 static int did_set_error_handler = FALSE;
1844
1845 if (!did_set_error_handler)
1846 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001847 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 (void)XSetErrorHandler(x_error_handler);
1849 did_set_error_handler = TRUE;
1850 }
1851
Bram Moolenaar9372a112005-12-06 19:59:18 +00001852#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (gui.in_use)
1854 {
1855 /*
1856 * If the X11 display was opened here before, for the window where Vim
1857 * was started, close that one now to avoid a memory leak.
1858 */
1859 if (x11_display_from == XD_HERE && x11_display != NULL)
1860 {
1861 XCloseDisplay(x11_display);
1862 x11_display_from = XD_NONE;
1863 }
1864 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1865 {
1866 x11_display_from = XD_GUI;
1867 return OK;
1868 }
1869 x11_display = NULL;
1870 return FAIL;
1871 }
1872 else if (x11_display_from == XD_GUI)
1873 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001874 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 x11_window = 0;
1876 x11_display = NULL;
1877 x11_display_from = XD_NONE;
1878 }
1879#endif
1880
Bram Moolenaar0f873732019-12-05 20:28:46 +01001881 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (!x_connect_to_server())
1883 return FAIL;
1884
1885 /*
1886 * If WINDOWID not set, should try another method to find out
1887 * what the current window number is. The only code I know for
1888 * this is very complicated.
1889 * We assume that zero is invalid for WINDOWID.
1890 */
1891 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1892 x11_window = (Window)atol(winid);
1893
1894#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001895 if (xterm_dpy == x11_display)
1896 // x11_display may have been set to xterm_dpy elsewhere
1897 x11_display_from = XD_XTERM;
1898
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 if (xterm_dpy != NULL && x11_window != 0)
1900 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001901 // We may have checked it already, but Gnome terminal can move us to
1902 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001903 if (x11_display_from != XD_XTERM)
1904 {
1905 /*
1906 * If the X11 display was opened here before, for the window where
1907 * Vim was started, close that one now to avoid a memory leak.
1908 */
1909 if (x11_display_from == XD_HERE && x11_display != NULL)
1910 XCloseDisplay(x11_display);
1911 x11_display = xterm_dpy;
1912 x11_display_from = XD_XTERM;
1913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (test_x11_window(x11_display) == FAIL)
1915 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001916 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 x11_window = 0;
1918 x11_display = NULL;
1919 x11_display_from = XD_NONE;
1920 return FAIL;
1921 }
1922 return OK;
1923 }
1924#endif
1925
1926 if (x11_window == 0 || x11_display == NULL)
1927 result = -1;
1928
Bram Moolenaar0f873732019-12-05 20:28:46 +01001929 if (result != -1) // Have already been here and set this
1930 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
1932 if (x11_window != 0 && x11_display == NULL)
1933 {
1934#ifdef SET_SIG_ALARM
1935 RETSIGTYPE (*sig_save)();
1936#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001937#ifdef ELAPSED_FUNC
1938 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
1940 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001941 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942#endif
1943
1944#ifdef SET_SIG_ALARM
1945 /*
1946 * Opening the Display may hang if the DISPLAY setting is wrong, or
1947 * the network connection is bad. Set an alarm timer to get out.
1948 */
1949 sig_alarm_called = FALSE;
1950 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1951 (RETSIGTYPE (*)())sig_alarm);
1952 alarm(2);
1953#endif
1954 x11_display = XOpenDisplay(NULL);
1955
1956#ifdef SET_SIG_ALARM
1957 alarm(0);
1958 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1959 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001960 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961#endif
1962 if (x11_display != NULL)
1963 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001964# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001966 {
1967 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001968 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001969 verbose_leave();
1970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971# endif
1972 if (test_x11_window(x11_display) == FAIL)
1973 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001974 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 x11_window = 0;
1976 XCloseDisplay(x11_display);
1977 x11_display = NULL;
1978 }
1979 else
1980 x11_display_from = XD_HERE;
1981 }
1982 }
1983 if (x11_window == 0 || x11_display == NULL)
1984 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001985
1986# ifdef FEAT_EVAL
1987 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1988# endif
1989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 return (result = OK);
1991}
1992
1993/*
1994 * Determine original x11 Window Title
1995 */
1996 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001997get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001999 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000}
2001
2002/*
2003 * Determine original x11 Window icon
2004 */
2005 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002006get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
2008 int retval = FALSE;
2009
2010 retval = get_x11_thing(FALSE, test_only);
2011
Bram Moolenaar0f873732019-12-05 20:28:46 +01002012 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 if (oldicon == NULL && !test_only)
2014 {
2015 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002016 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002018 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
2020
2021 return retval;
2022}
2023
2024 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002025get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002026 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01002027 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
2029 XTextProperty text_prop;
2030 int retval = FALSE;
2031 Status status;
2032
2033 if (get_x11_windis() == OK)
2034 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002035 // Get window/icon name if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (get_title)
2037 status = XGetWMName(x11_display, x11_window, &text_prop);
2038 else
2039 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2040
2041 /*
2042 * If terminal is xterm, then x11_window may be a child window of the
2043 * outer xterm window that actually contains the window/icon name, so
2044 * keep traversing up the tree until a window with a title/icon is
2045 * found.
2046 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002047 // Previously this was only done for xterm and alike. I don't see a
Bram Moolenaar0f873732019-12-05 20:28:46 +01002048 // reason why it would fail for other terminal emulators.
2049 // if (term_is_xterm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
2051 Window root;
2052 Window parent;
2053 Window win = x11_window;
2054 Window *children;
2055 unsigned int num_children;
2056
2057 while (!status || text_prop.value == NULL)
2058 {
2059 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2060 &num_children))
2061 break;
2062 if (children)
2063 XFree((void *)children);
2064 if (parent == root || parent == 0)
2065 break;
2066
2067 win = parent;
2068 if (get_title)
2069 status = XGetWMName(x11_display, win, &text_prop);
2070 else
2071 status = XGetWMIconName(x11_display, win, &text_prop);
2072 }
2073 }
2074 if (status && text_prop.value != NULL)
2075 {
2076 retval = TRUE;
2077 if (!test_only)
2078 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002079 if (get_title)
2080 vim_free(oldtitle);
2081 else
2082 vim_free(oldicon);
Bram Moolenaara12a1612019-01-24 16:39:02 +01002083 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 if (get_title)
2086 oldtitle = vim_strsave((char_u *)text_prop.value);
2087 else
2088 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
2090 else
2091 {
2092 char **cl;
2093 Status transform_status;
2094 int n = 0;
2095
2096 transform_status = XmbTextPropertyToTextList(x11_display,
2097 &text_prop,
2098 &cl, &n);
2099 if (transform_status >= Success && n > 0 && cl[0])
2100 {
2101 if (get_title)
2102 oldtitle = vim_strsave((char_u *) cl[0]);
2103 else
2104 oldicon = vim_strsave((char_u *) cl[0]);
2105 XFreeStringList(cl);
2106 }
2107 else
2108 {
2109 if (get_title)
2110 oldtitle = vim_strsave((char_u *)text_prop.value);
2111 else
2112 oldicon = vim_strsave((char_u *)text_prop.value);
2113 }
2114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 }
2116 XFree((void *)text_prop.value);
2117 }
2118 }
2119 return retval;
2120}
2121
Bram Moolenaar0f873732019-12-05 20:28:46 +01002122// Xutf8 functions are not available on older systems. Note that on some
2123// systems X_HAVE_UTF8_STRING may be defined in a header file but
2124// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2125// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002126#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002127# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128# define USE_UTF8_STRING
2129# endif
2130#endif
2131
2132/*
2133 * Set x11 Window Title
2134 *
2135 * get_x11_windis() must be called before this and have returned OK
2136 */
2137 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002138set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002140 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2141 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2142 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143#ifdef USE_UTF8_STRING
2144 if (enc_utf8)
2145 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2146 NULL, NULL, 0, NULL, NULL, NULL);
2147 else
2148#endif
2149 {
2150#if XtSpecificationRelease >= 4
2151# ifdef FEAT_XFONTSET
2152 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2153 NULL, NULL, 0, NULL, NULL, NULL);
2154# else
2155 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002156 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
Bram Moolenaar0f873732019-12-05 20:28:46 +01002158 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002159 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 XSetWMProperties(x11_display, x11_window, &text_prop,
2161 NULL, NULL, 0, NULL, NULL, NULL);
2162# endif
2163#else
2164 XStoreName(x11_display, x11_window, (char *)title);
2165#endif
2166 }
2167 XFlush(x11_display);
2168}
2169
2170/*
2171 * Set x11 Window icon
2172 *
2173 * get_x11_windis() must be called before this and have returned OK
2174 */
2175 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002176set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002178 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179#ifdef USE_UTF8_STRING
2180 if (enc_utf8)
2181 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2182 NULL, 0, NULL, NULL, NULL);
2183 else
2184#endif
2185 {
2186#if XtSpecificationRelease >= 4
2187# ifdef FEAT_XFONTSET
2188 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2189 NULL, 0, NULL, NULL, NULL);
2190# else
2191 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002192 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002194 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2196 NULL, 0, NULL, NULL, NULL);
2197# endif
2198#else
2199 XSetIconName(x11_display, x11_window, (char *)icon);
2200#endif
2201 }
2202 XFlush(x11_display);
2203}
2204
Bram Moolenaar0f873732019-12-05 20:28:46 +01002205#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002208get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209{
2210 return FALSE;
2211}
2212
2213 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002214get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216 if (!test_only)
2217 {
2218 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002219 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002221 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 }
2223 return FALSE;
2224}
2225
Bram Moolenaar0f873732019-12-05 20:28:46 +01002226#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
2228 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002229mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230{
2231 return get_x11_title(TRUE);
2232}
2233
2234 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002235mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236{
2237 return get_x11_icon(TRUE);
2238}
2239
2240/*
2241 * Set the window title and icon.
2242 */
2243 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002244mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245{
2246 int type = 0;
2247 static int recursive = 0;
2248
Bram Moolenaar0f873732019-12-05 20:28:46 +01002249 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002251 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 return;
2253
Bram Moolenaar0f873732019-12-05 20:28:46 +01002254 // When one of the X11 functions causes a deadly signal, we get here again
2255 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (recursive)
2257 return;
2258 ++recursive;
2259
2260 /*
2261 * if the window ID and the display is known, we may use X11 calls
2262 */
2263#ifdef FEAT_X11
2264 if (get_x11_windis() == OK)
2265 type = 1;
2266#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002267# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002268 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 if (gui.in_use)
2270 type = 1;
2271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272#endif
2273
2274 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002275 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 * than x11 calls, because the x11 calls don't always work
2277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 if ((type || *T_TS != NUL) && title != NULL)
2279 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002280 if (oldtitle_outdated)
2281 {
2282 oldtitle_outdated = FALSE;
2283 VIM_CLEAR(oldtitle);
2284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (oldtitle == NULL
2286#ifdef FEAT_GUI
2287 && !gui.in_use
2288#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002289 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 (void)get_x11_title(FALSE);
2291
Bram Moolenaar0f873732019-12-05 20:28:46 +01002292 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 term_settitle(title);
2294#ifdef FEAT_X11
2295 else
2296# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002297 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002299 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002301#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002302 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 else
2304 gui_mch_settitle(title, icon);
2305#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002306 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
2308
2309 if ((type || *T_CIS != NUL) && icon != NULL)
2310 {
2311 if (oldicon == NULL
2312#ifdef FEAT_GUI
2313 && !gui.in_use
2314#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002315 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 get_x11_icon(FALSE);
2317
2318 if (*T_CIS != NUL)
2319 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002320 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002322 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 out_flush();
2324 }
2325#ifdef FEAT_X11
2326 else
2327# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002328 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002330 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331#endif
2332 did_set_icon = TRUE;
2333 }
2334 --recursive;
2335}
2336
2337/*
2338 * Restore the window/icon title.
2339 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002340 * SAVE_RESTORE_TITLE only restore title
2341 * SAVE_RESTORE_ICON only restore icon
2342 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 */
2344 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002345mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346{
Bram Moolenaardac13472019-09-16 21:06:21 +02002347 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002348
Bram Moolenaar0f873732019-12-05 20:28:46 +01002349 // only restore the title or icon when it has been set
Bram Moolenaardac13472019-09-16 21:06:21 +02002350 mch_settitle(((which & SAVE_RESTORE_TITLE) && unix_did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002352 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2353
Bram Moolenaare5c83282019-05-03 23:15:37 +02002354 if (do_push_pop)
2355 {
2356 // pop and push from/to the stack
2357 term_pop_title(which);
2358 term_push_title(which);
2359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360}
2361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
2363/*
2364 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002365 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 */
2367 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002368vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370 if (name == NULL)
2371 return FALSE;
2372 return (STRNICMP(name, "xterm", 5) == 0
2373 || STRNICMP(name, "nxterm", 6) == 0
2374 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002375 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002377 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 || STRCMP(name, "builtin_xterm") == 0);
2379}
2380
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002381#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2382/*
2383 * Return TRUE if "name" appears to be that of a terminal
2384 * known to support the xterm-style mouse protocol.
2385 * Relies on term_is_xterm having been set to its correct value.
2386 */
2387 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002388use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002389{
2390 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002391 && (term_is_xterm
2392 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002393 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002394 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002395 || STRICMP(name, "st") == 0
2396 || STRNICMP(name, "st-", 3) == 0
2397 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002398}
2399#endif
2400
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401/*
2402 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2403 * Return 1 for "xterm".
2404 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002405 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002406 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 */
2408 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002409use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002411 if (ttym_flags == TTYM_SGR)
2412 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002413 if (ttym_flags == TTYM_URXVT)
2414 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 if (ttym_flags == TTYM_XTERM2)
2416 return 2;
2417 if (ttym_flags == TTYM_XTERM)
2418 return 1;
2419 return 0;
2420}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002423vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424{
2425 if (name == NULL)
2426 return FALSE;
2427 return (STRNICMP(name, "iris-ansi", 9) == 0
2428 || STRCMP(name, "builtin_iris-ansi") == 0);
2429}
2430
2431 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002432vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
2434 if (name == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002435 return FALSE; // actually all ANSI comp. terminals should be here
2436 // catch VT100 - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002437 return ((STRNICMP(name, "vt", 2) == 0
2438 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 || STRCMP(name, "builtin_vt320") == 0);
2440}
2441
2442/*
2443 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2444 * This should include all windowed terminal emulators.
2445 */
2446 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002447vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448{
2449 if (name == NULL)
2450 return FALSE;
2451 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2452 return TRUE;
2453 return ( STRNICMP(name, "hpterm", 6) == 0
2454 || STRNICMP(name, "sun-cmd", 7) == 0
2455 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002456 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 || STRNICMP(name, "dtterm", 6) == 0);
2458}
2459
2460/*
2461 * Insert user name in s[len].
2462 * Return OK if a name found.
2463 */
2464 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002465mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002468 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 return OK;
2470#else
2471 return mch_get_uname(getuid(), s, len);
2472#endif
2473}
2474
2475/*
2476 * Insert user name for "uid" in s[len].
2477 * Return OK if a name found.
2478 */
2479 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002480mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
2482#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2483 struct passwd *pw;
2484
2485 if ((pw = getpwuid(uid)) != NULL
2486 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2487 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002488 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 return OK;
2490 }
2491#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002492 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2493 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494}
2495
2496/*
2497 * Insert host name is s[len].
2498 */
2499
2500#ifdef HAVE_SYS_UTSNAME_H
2501 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002502mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503{
2504 struct utsname vutsname;
2505
2506 if (uname(&vutsname) < 0)
2507 *s = NUL;
2508 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002509 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002511#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513# ifdef HAVE_SYS_SYSTEMINFO_H
2514# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2515# endif
2516
2517 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002518mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519{
2520# ifdef VAXC
2521 vaxc$gethostname((char *)s, len);
2522# else
2523 gethostname((char *)s, len);
2524# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002525 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002527#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
2529/*
2530 * return process ID
2531 */
2532 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002533mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
2535 return (long)getpid();
2536}
2537
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002538/*
2539 * return TRUE if process "pid" is still running
2540 */
2541 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002542mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002543{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002544 // If there is no error the process must be running.
2545 if (kill(pid, 0) == 0)
2546 return TRUE;
2547#ifdef ESRCH
2548 // If the error is ESRCH then the process is not running.
2549 if (errno == ESRCH)
2550 return FALSE;
2551#endif
2552 // If the process is running and owned by another user we get EPERM. With
2553 // other errors the process might be running, assuming it is then.
2554 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002555}
2556
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002559strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
2561 extern int sys_nerr;
2562 extern char *sys_errlist[];
2563 static char er[20];
2564
2565 if (err > 0 && err < sys_nerr)
2566 return (sys_errlist[err]);
2567 sprintf(er, "Error %d", err);
2568 return er;
2569}
2570#endif
2571
2572/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002573 * Get name of current directory into buffer "buf" of length "len" bytes.
2574 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 * Return OK for success, FAIL for failure.
2576 */
2577 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002578mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579{
2580#if defined(USE_GETCWD)
2581 if (getcwd((char *)buf, len) == NULL)
2582 {
2583 STRCPY(buf, strerror(errno));
2584 return FAIL;
2585 }
2586 return OK;
2587#else
2588 return (getwd((char *)buf) != NULL ? OK : FAIL);
2589#endif
2590}
2591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002593 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 *
2595 * return FAIL for failure, OK for success
2596 */
2597 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002598mch_FullName(
2599 char_u *fname,
2600 char_u *buf,
2601 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002602 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603{
2604 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002605#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002607 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 char_u olddir[MAXPATHL];
2610 char_u *p;
2611 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002612#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002613 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2614 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002615#endif
2616
Bram Moolenaar38323e42007-03-06 19:22:53 +00002617#ifdef VMS
2618 fname = vms_fixfilename(fname);
2619#endif
2620
Bram Moolenaara2442432007-04-26 14:26:37 +00002621#ifdef __CYGWIN__
2622 /*
2623 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2624 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002625# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002626 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2627 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002628 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2629 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002630# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002631 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002632# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002633 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002634#endif
2635
Bram Moolenaar0f873732019-12-05 20:28:46 +01002636 // Expand it if forced or not an absolute path.
2637 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002638 if ((force || !mch_isFullName(fname))
2639 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 {
2641 /*
2642 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002643 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 * This will get the correct path name with "../" things.
2645 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002646 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002648 if (STRCMP(p, "/..") == 0)
2649 // for "/path/dir/.." include the "/.."
2650 p += 3;
2651
Bram Moolenaar38323e42007-03-06 19:22:53 +00002652#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 /*
2654 * Use fchdir() if possible, it's said to be faster and more
2655 * reliable. But on SunOS 4 it might not work. Check this by
2656 * doing a fchdir() right now.
2657 */
2658 if (!dont_fchdir)
2659 {
2660 fd = open(".", O_RDONLY | O_EXTRA, 0);
2661 if (fd >= 0 && fchdir(fd) < 0)
2662 {
2663 close(fd);
2664 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002665 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
Bram Moolenaar0f873732019-12-05 20:28:46 +01002670 // Only change directory when we are sure we can return to where
2671 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002673#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 (mch_dirname(olddir, MAXPATHL) == FAIL
2677 || mch_chdir((char *)olddir) != 0))
2678 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002679 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 retval = FAIL;
2681 }
2682 else
2683 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002684 // The directory is copied into buf[], to be able to remove
2685 // the file name without changing it (could be a string in
2686 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (p - fname >= len)
2688 retval = FAIL;
2689 else
2690 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002691 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002693 {
2694 // Path does not exist (yet). For a full path fail,
2695 // will use the path as-is. For a relative path use
2696 // the current directory and append the file name.
2697 if (mch_isFullName(fname))
2698 retval = FAIL;
2699 else
2700 p = NULL;
2701 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002702 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002704 else
2705 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 *buf = NUL;
2707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
2709 }
2710 if (mch_dirname(buf, len) == FAIL)
2711 {
2712 retval = FAIL;
2713 *buf = NUL;
2714 }
2715 if (p != NULL)
2716 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002717#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (fd >= 0)
2719 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002720 if (p_verbose >= 5)
2721 {
2722 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002723 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002724 verbose_leave();
2725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 l = mch_chdir((char *)olddir);
2731 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002732 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 }
itchyny051a40c2021-10-20 10:00:05 +01002734#ifdef HAVE_FCHDIR
2735 if (fd >= 0)
2736 close(fd);
2737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
2739 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002740 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002741 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002742#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002743 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002745 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002748
Bram Moolenaar0f873732019-12-05 20:28:46 +01002749 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002750 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 return FAIL;
2752
Bram Moolenaar0f873732019-12-05 20:28:46 +01002753 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 if (STRCMP(fname, ".") != 0)
2755 STRCAT(buf, fname);
2756
2757 return OK;
2758}
2759
2760/*
2761 * Return TRUE if "fname" does not depend on the current directory.
2762 */
2763 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002764mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002766#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 return ( fname[0] == '/' || fname[0] == '.' ||
2768 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2769 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2770 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002771#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773#endif
2774}
2775
Bram Moolenaar24552be2005-12-10 20:17:30 +00002776#if defined(USE_FNAME_CASE) || defined(PROTO)
2777/*
2778 * Set the case of the file name, if it already exists. This will cause the
2779 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002780 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002781 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002782 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002783fname_case(
2784 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002785 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002786{
2787 struct stat st;
2788 char_u *slash, *tail;
2789 DIR *dirp;
2790 struct dirent *dp;
2791
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002792 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002793 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002794 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002795 slash = vim_strrchr(name, '/');
2796 if (slash == NULL)
2797 {
2798 dirp = opendir(".");
2799 tail = name;
2800 }
2801 else
2802 {
2803 *slash = NUL;
2804 dirp = opendir((char *)name);
2805 *slash = '/';
2806 tail = slash + 1;
2807 }
2808
2809 if (dirp != NULL)
2810 {
2811 while ((dp = readdir(dirp)) != NULL)
2812 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002813 // Only accept names that differ in case and are the same byte
2814 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002815 if (STRICMP(tail, dp->d_name) == 0
2816 && STRLEN(tail) == STRLEN(dp->d_name))
2817 {
2818 char_u newname[MAXPATHL + 1];
2819 struct stat st2;
2820
Bram Moolenaar0f873732019-12-05 20:28:46 +01002821 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002822 vim_strncpy(newname, name, MAXPATHL);
2823 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2824 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002825 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002826 && st.st_ino == st2.st_ino
2827 && st.st_dev == st2.st_dev)
2828 {
2829 STRCPY(tail, dp->d_name);
2830 break;
2831 }
2832 }
2833 }
2834
2835 closedir(dirp);
2836 }
2837 }
2838}
2839#endif
2840
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841/*
2842 * Get file permissions for 'name'.
2843 * Returns -1 when it doesn't exist.
2844 */
2845 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002846mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847{
2848 struct stat statb;
2849
Bram Moolenaar0f873732019-12-05 20:28:46 +01002850 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851#ifdef VMS
2852 if (stat((char *)vms_fixfilename(name), &statb))
2853#else
2854 if (stat((char *)name, &statb))
2855#endif
2856 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002857#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002858 // The top bit makes the value negative, which means the file doesn't
2859 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002860 return statb.st_mode & ~S_ADDACE;
2861#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864}
2865
2866/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002867 * Set file permission for "name" to "perm".
2868 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 */
2870 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002871mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872{
2873 return (chmod((char *)
2874#ifdef VMS
2875 vms_fixfilename(name),
2876#else
2877 name,
2878#endif
2879 (mode_t)perm) == 0 ? OK : FAIL);
2880}
2881
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002882#if defined(HAVE_FCHMOD) || defined(PROTO)
2883/*
2884 * Set file permission for open file "fd" to "perm".
2885 * Return FAIL for failure, OK otherwise.
2886 */
2887 int
2888mch_fsetperm(int fd, long perm)
2889{
2890 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2891}
2892#endif
2893
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894#if defined(HAVE_ACL) || defined(PROTO)
2895# ifdef HAVE_SYS_ACL_H
2896# include <sys/acl.h>
2897# endif
2898# ifdef HAVE_SYS_ACCESS_H
2899# include <sys/access.h>
2900# endif
2901
2902# ifdef HAVE_SOLARIS_ACL
2903typedef struct vim_acl_solaris_T {
2904 int acl_cnt;
2905 aclent_t *acl_entry;
2906} vim_acl_solaris_T;
2907# endif
2908
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002909#if defined(HAVE_SELINUX) || defined(PROTO)
2910/*
2911 * Copy security info from "from_file" to "to_file".
2912 */
2913 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002914mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002915{
2916 if (from_file == NULL)
2917 return;
2918
2919 if (selinux_enabled == -1)
2920 selinux_enabled = is_selinux_enabled();
2921
2922 if (selinux_enabled > 0)
2923 {
Bram Moolenaar89560232020-10-09 23:04:47 +02002924 // Use "char *" instead of "security_context_t" to avoid a deprecation
2925 // warning.
2926 char *from_context = NULL;
2927 char *to_context = NULL;
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002928
2929 if (getfilecon((char *)from_file, &from_context) < 0)
2930 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002931 // If the filesystem doesn't support extended attributes,
2932 // the original had no special security context and the
2933 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002934 if (errno == EOPNOTSUPP)
2935 return;
2936
Bram Moolenaar32526b32019-01-19 17:43:09 +01002937 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002938 msg_outtrans(from_file);
2939 msg_putchar('\n');
2940 return;
2941 }
2942 if (getfilecon((char *)to_file, &to_context) < 0)
2943 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002944 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002945 msg_outtrans(to_file);
2946 msg_putchar('\n');
2947 freecon (from_context);
2948 return ;
2949 }
2950 if (strcmp(from_context, to_context) != 0)
2951 {
2952 if (setfilecon((char *)to_file, from_context) < 0)
2953 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002954 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002955 msg_outtrans(to_file);
2956 msg_putchar('\n');
2957 }
2958 }
2959 freecon(to_context);
2960 freecon(from_context);
2961 }
2962}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002963#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002964
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002965#if defined(HAVE_SMACK) && !defined(PROTO)
2966/*
2967 * Copy security info from "from_file" to "to_file".
2968 */
2969 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002970mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002971{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002972 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002973 {
2974 XATTR_NAME_SMACK,
2975 XATTR_NAME_SMACKEXEC,
2976 XATTR_NAME_SMACKMMAP
2977 };
2978
2979 char buffer[SMACK_LABEL_LEN];
2980 const char *name;
2981 int index;
2982 int ret;
2983 ssize_t size;
2984
2985 if (from_file == NULL)
2986 return;
2987
2988 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2989 / sizeof(smack_copied_attributes)[0]) ; index++)
2990 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002991 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002992 name = smack_copied_attributes[index];
2993
Bram Moolenaar0f873732019-12-05 20:28:46 +01002994 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002995 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2996 if (size >= 0)
2997 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002998 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002999 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
3000 if (ret < 0)
3001 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003002 vim_snprintf((char *)IObuff, IOSIZE,
3003 _("Could not set security context %s for %s"),
3004 name, to_file);
3005 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003006 msg_putchar('\n');
3007 }
3008 }
3009 else
3010 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003011 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003012 switch (errno)
3013 {
3014 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003015 // extended attributes aren't supported or enabled
3016 // should a message be echoed? not sure...
3017 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003018
3019 case ERANGE:
3020 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003021 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003022 vim_snprintf((char *)IObuff, IOSIZE,
3023 _("Could not get security context %s for %s. Removing it!"),
3024 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003025 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003026 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003027 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003028
3029 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003030 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003031 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003032 // Silently ignore errors, apparently this happens when
3033 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003034 break;
3035 }
3036 }
3037 }
3038}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003039#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003040
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041/*
3042 * Return a pointer to the ACL of file "fname" in allocated memory.
3043 * Return NULL if the ACL is not available for whatever reason.
3044 */
3045 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003046mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047{
3048 vim_acl_T ret = NULL;
3049#ifdef HAVE_POSIX_ACL
3050 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3051#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003052#ifdef HAVE_SOLARIS_ZFS_ACL
3053 acl_t *aclent;
3054
3055 if (acl_get((char *)fname, 0, &aclent) < 0)
3056 return NULL;
3057 ret = (vim_acl_T)aclent;
3058#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059#ifdef HAVE_SOLARIS_ACL
3060 vim_acl_solaris_T *aclent;
3061
3062 aclent = malloc(sizeof(vim_acl_solaris_T));
3063 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3064 {
3065 free(aclent);
3066 return NULL;
3067 }
3068 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3069 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3070 {
3071 free(aclent->acl_entry);
3072 free(aclent);
3073 return NULL;
3074 }
3075 ret = (vim_acl_T)aclent;
3076#else
3077#if defined(HAVE_AIX_ACL)
3078 int aclsize;
3079 struct acl *aclent;
3080
3081 aclsize = sizeof(struct acl);
3082 aclent = malloc(aclsize);
3083 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3084 {
3085 if (errno == ENOSPC)
3086 {
3087 aclsize = aclent->acl_len;
3088 aclent = realloc(aclent, aclsize);
3089 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3090 {
3091 free(aclent);
3092 return NULL;
3093 }
3094 }
3095 else
3096 {
3097 free(aclent);
3098 return NULL;
3099 }
3100 }
3101 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003102#endif // HAVE_AIX_ACL
3103#endif // HAVE_SOLARIS_ACL
3104#endif // HAVE_SOLARIS_ZFS_ACL
3105#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 return ret;
3107}
3108
3109/*
3110 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3111 */
3112 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003113mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
3115 if (aclent == NULL)
3116 return;
3117#ifdef HAVE_POSIX_ACL
3118 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3119#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003120#ifdef HAVE_SOLARIS_ZFS_ACL
3121 acl_set((char *)fname, (acl_t *)aclent);
3122#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#ifdef HAVE_SOLARIS_ACL
3124 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3125 ((vim_acl_solaris_T *)aclent)->acl_entry);
3126#else
3127#ifdef HAVE_AIX_ACL
3128 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003129#endif // HAVE_AIX_ACL
3130#endif // HAVE_SOLARIS_ACL
3131#endif // HAVE_SOLARIS_ZFS_ACL
3132#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133}
3134
3135 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003136mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
3138 if (aclent == NULL)
3139 return;
3140#ifdef HAVE_POSIX_ACL
3141 acl_free((acl_t)aclent);
3142#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003143#ifdef HAVE_SOLARIS_ZFS_ACL
3144 acl_free((acl_t *)aclent);
3145#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146#ifdef HAVE_SOLARIS_ACL
3147 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3148 free(aclent);
3149#else
3150#ifdef HAVE_AIX_ACL
3151 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003152#endif // HAVE_AIX_ACL
3153#endif // HAVE_SOLARIS_ACL
3154#endif // HAVE_SOLARIS_ZFS_ACL
3155#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156}
3157#endif
3158
3159/*
3160 * Set hidden flag for "name".
3161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003163mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003165 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166}
3167
3168/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003169 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 * return FALSE if "name" is not a directory
3171 * return FALSE for error
3172 */
3173 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003174mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175{
3176 struct stat statb;
3177
Bram Moolenaar0f873732019-12-05 20:28:46 +01003178 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 return FALSE;
3180 if (stat((char *)name, &statb))
3181 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183}
3184
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003185/*
3186 * return TRUE if "name" is a directory, NOT a symlink to a directory
3187 * return FALSE if "name" is not a directory
3188 * return FALSE for error
3189 */
3190 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003191mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003192{
3193 struct stat statb;
3194
Bram Moolenaar0f873732019-12-05 20:28:46 +01003195 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003196 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003197 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003198 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003199 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003200}
3201
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202/*
3203 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3204 */
3205 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003206executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207{
3208 struct stat st;
3209
3210 if (stat((char *)name, &st))
3211 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003212#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003213 // Like on Unix system file can have executable rights but not necessarily
3214 // be an executable, but on Unix is not a default for an ordinary file to
3215 // have an executable flag - on VMS it is in most cases.
3216 // Therefore, this check does not have any sense - let keep us to the
3217 // conventions instead:
3218 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003219 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003220 int vms_executable = 0;
3221 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3222 {
3223 if (strstr(vms_tolower((char*)name),".exe") != NULL
3224 || strstr(vms_tolower((char*)name),".com")!= NULL)
3225 vms_executable = 1;
3226 }
3227 return vms_executable;
3228#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231}
3232
3233/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003234 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003235 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 * Return -1 if unknown.
3237 */
3238 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003239mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240{
3241 char_u *buf;
3242 char_u *p, *e;
3243 int retval;
3244
Bram Moolenaar0f873732019-12-05 20:28:46 +01003245 // When "use_path" is false and if it's an absolute or relative path don't
3246 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003247 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003248 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003249 // There must be a path separator, files in the current directory
3250 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003251 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003252 {
3253 if (path != NULL)
3254 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003255 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003256 *path = FullName_save(name, TRUE);
3257 else
3258 *path = vim_strsave(name);
3259 }
3260 return TRUE;
3261 }
3262 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264
3265 p = (char_u *)getenv("PATH");
3266 if (p == NULL || *p == NUL)
3267 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003268 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 if (buf == NULL)
3270 return -1;
3271
3272 /*
3273 * Walk through all entries in $PATH to check if "name" exists there and
3274 * is an executable file.
3275 */
3276 for (;;)
3277 {
3278 e = (char_u *)strchr((char *)p, ':');
3279 if (e == NULL)
3280 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003281 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 STRCPY(buf, "./");
3283 else
3284 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003285 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 add_pathsep(buf);
3287 }
3288 STRCAT(buf, name);
3289 retval = executable_file(buf);
3290 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003291 {
3292 if (path != NULL)
3293 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003294 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003295 *path = FullName_save(buf, TRUE);
3296 else
3297 *path = vim_strsave(buf);
3298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301
3302 if (*e != ':')
3303 break;
3304 p = e + 1;
3305 }
3306
3307 vim_free(buf);
3308 return retval;
3309}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310
3311/*
3312 * Check what "name" is:
3313 * NODE_NORMAL: file or directory (or doesn't exist)
3314 * NODE_WRITABLE: writable device, socket, fifo, etc.
3315 * NODE_OTHER: non-writable things
3316 */
3317 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003318mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 struct stat st;
3321
3322 if (stat((char *)name, &st))
3323 return NODE_NORMAL;
3324 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3325 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003326 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003328 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 return NODE_WRITABLE;
3330}
3331
3332 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003333mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335#ifdef HAVE_CHECK_STACK_GROWTH
3336 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 check_stack_growth((char *)&i);
3339
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003340# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 get_stack_limit();
3342# endif
3343
3344#endif
3345
3346 /*
3347 * Setup an alternative stack for signals. Helps to catch signals when
3348 * running out of stack space.
3349 * Use of sigaltstack() is preferred, it's more portable.
3350 * Ignore any errors.
3351 */
3352#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003353 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 init_signal_stack();
3355#endif
3356}
3357
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003358#if defined(EXITFREE) || defined(PROTO)
3359 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003360mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003361{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003362# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3363 if (clip_star.owned)
3364 clip_lose_selection(&clip_star);
3365 if (clip_plus.owned)
3366 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003367# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003368# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003369 if (xterm_Shell != (Widget)0)
3370 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003371# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003372 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003373 if (xterm_dpy != NULL)
3374 XtCloseDisplay(xterm_dpy);
3375 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003376 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003377 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003378# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003379 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003380# endif
3381 }
3382# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003383# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003384# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003385 if (x11_display != NULL
3386# ifdef FEAT_XCLIPBOARD
3387 && x11_display != xterm_dpy
3388# endif
3389 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003390 XCloseDisplay(x11_display);
3391# endif
3392# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003393 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003394# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003395 vim_free(oldtitle);
3396 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003397}
3398#endif
3399
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400/*
3401 * Output a newline when exiting.
3402 * Make sure the newline goes to the same stream as the text.
3403 */
3404 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003405exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003407 if (silent_mode)
3408 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (newline_on_exit || msg_didout)
3410 {
3411 if (msg_use_printf())
3412 {
3413 if (info_message)
3414 mch_msg("\n");
3415 else
3416 mch_errmsg("\r\n");
3417 }
3418 else
3419 out_char('\n');
3420 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003421 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003423 restore_cterm_colors(); // get original colors back
3424 msg_clr_eos_force(); // clear the rest of the display
3425 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 }
3427}
3428
Bram Moolenaarb4151682020-05-11 22:13:28 +02003429#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003430# if (defined(__GNUC__) \
3431 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3432 || (defined(__clang__) && (__clang_major__ >= 12))
3433extern void __gcov_dump(void);
3434extern void __gcov_reset(void);
3435# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3436# else
3437extern void __gcov_flush(void);
3438# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003439#endif
3440
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003442mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
3444 exiting = TRUE;
3445
3446#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3447 x11_export_final_selection();
3448#endif
3449
3450#ifdef FEAT_GUI
3451 if (!gui.in_use)
3452#endif
3453 {
3454 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003455 if (!is_not_a_term())
3456 {
3457 // restore xterm title and icon name
3458 mch_restore_title(SAVE_RESTORE_BOTH);
3459 term_pop_title(SAVE_RESTORE_BOTH);
3460 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003461
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 /*
3463 * When t_ti is not empty but it doesn't cause swapping terminal
3464 * pages, need to output a newline when msg_didout is set. But when
3465 * t_ti does swap pages it should not go to the shell page. Do this
3466 * before stoptermcap().
3467 */
3468 if (swapping_screen() && !newline_on_exit)
3469 exit_scroll();
3470
Bram Moolenaar0f873732019-12-05 20:28:46 +01003471 // Stop termcap: May need to check for T_CRV response, which
3472 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 stoptermcap();
3474
3475 /*
3476 * A newline is only required after a message in the alternate screen.
3477 * This is set to TRUE by wait_return().
3478 */
3479 if (!swapping_screen() || newline_on_exit)
3480 exit_scroll();
3481
Bram Moolenaar0f873732019-12-05 20:28:46 +01003482 // Cursor may have been switched off without calling starttermcap()
3483 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (full_screen)
3485 cursor_on();
3486 }
3487 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003488 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003489
3490#ifdef USE_GCOV_FLUSH
3491 // Flush coverage info before possibly being killed by a deadly signal.
3492 __gcov_flush();
3493#endif
3494
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 may_core_dump();
3496#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 gui_exit(r);
3499#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003500
Bram Moolenaar56718732006-03-15 22:53:57 +00003501#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003502 mac_conv_cleanup();
3503#endif
3504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003506 // A core dump won't be created if the signal handler
3507 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (deadly_signal != 0)
3509 return;
3510#endif
3511
Bram Moolenaar009b2592004-10-24 19:18:58 +00003512#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003513 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003514#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003515
3516#ifdef EXITFREE
3517 free_all_mem();
3518#endif
3519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 exit(r);
3521}
3522
3523 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003524may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525{
3526 if (deadly_signal != 0)
3527 {
3528 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003529 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
3531}
3532
3533#ifndef VMS
3534
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003535/*
3536 * Get the file descriptor to use for tty operations.
3537 */
3538 static int
3539get_tty_fd(int fd)
3540{
3541 int tty_fd = fd;
3542
3543#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3544 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3545 // "fd" when it is a master device.
3546 if (mch_isatty(fd) > 1)
3547 {
3548 char *name;
3549
3550 name = ptsname(fd);
3551 if (name == NULL)
3552 return -1;
3553
3554 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3555 if (tty_fd < 0)
3556 return -1;
3557 }
3558#endif
3559 return tty_fd;
3560}
3561
3562 static int
3563mch_tcgetattr(int fd, void *term)
3564{
3565 int tty_fd;
3566 int retval = -1;
3567
3568 tty_fd = get_tty_fd(fd);
3569 if (tty_fd >= 0)
3570 {
3571#ifdef NEW_TTY_SYSTEM
3572# ifdef HAVE_TERMIOS_H
3573 retval = tcgetattr(tty_fd, (struct termios *)term);
3574# else
3575 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3576# endif
3577#else
3578 // for "old" tty systems
3579 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3580#endif
3581 if (tty_fd != fd)
3582 close(tty_fd);
3583 }
3584 return retval;
3585}
3586
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003588mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
3590 static int first = TRUE;
3591
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003592#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593# ifdef HAVE_TERMIOS_H
3594 static struct termios told;
3595 struct termios tnew;
3596# else
3597 static struct termio told;
3598 struct termio tnew;
3599# endif
3600
3601 if (first)
3602 {
3603 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003604 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
3606
3607 tnew = told;
3608 if (tmode == TMODE_RAW)
3609 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003610 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003611 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3612 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003614# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003615 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616# endif
3617 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003618# ifdef ONLCR
3619 // Don't map NL -> CR NL, we do it ourselves.
3620 // Also disable expanding tabs if possible.
3621# ifdef XTABS
3622 tnew.c_oflag &= ~(ONLCR | XTABS);
3623# else
3624# ifdef TAB3
3625 tnew.c_oflag &= ~(ONLCR | TAB3);
3626# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003628# endif
3629# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003631 tnew.c_cc[VMIN] = 1; // return after 1 char
3632 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
3634 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003635 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003636 // Also reset ICANON here, otherwise on Solaris select() won't see
3637 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003638 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003639 tnew.c_cc[VMIN] = 1; // return after 1 char
3640 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642
3643# if defined(HAVE_TERMIOS_H)
3644 {
3645 int n = 10;
3646
Bram Moolenaar0f873732019-12-05 20:28:46 +01003647 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3648 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3650 && errno == EINTR && n > 0)
3651 --n;
3652 }
3653# else
3654 ioctl(read_cmd_fd, TCSETA, &tnew);
3655# endif
3656
3657#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 /*
3659 * for "old" tty systems
3660 */
3661# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003662# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663# endif
3664 static struct sgttyb ttybold;
3665 struct sgttyb ttybnew;
3666
3667 if (first)
3668 {
3669 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003670 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
3672
3673 ttybnew = ttybold;
3674 if (tmode == TMODE_RAW)
3675 {
3676 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3677 ttybnew.sg_flags |= RAW;
3678 }
3679 else if (tmode == TMODE_SLEEP)
3680 ttybnew.sg_flags &= ~(ECHO);
3681 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3682#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003683 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684}
3685
3686/*
3687 * Try to get the code for "t_kb" from the stty setting
3688 *
3689 * Even if termcap claims a backspace key, the user's setting *should*
3690 * prevail. stty knows more about reality than termcap does, and if
3691 * somebody's usual erase key is DEL (which, for most BSD users, it will
3692 * be), they're going to get really annoyed if their erase key starts
3693 * doing forward deletes for no reason. (Eric Fischer)
3694 */
3695 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003696get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003698 ttyinfo_T info;
3699 char_u buf[2];
3700 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003702 if (get_tty_info(read_cmd_fd, &info) == OK)
3703 {
3704 intr_char = info.interrupt;
3705 buf[0] = info.backspace;
3706 buf[1] = NUL;
3707 add_termcode((char_u *)"kb", buf, FALSE);
3708
Bram Moolenaar0f873732019-12-05 20:28:46 +01003709 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003710 p = find_termcode((char_u *)"kD");
3711 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3712 do_fixdel(NULL);
3713 }
3714}
3715
3716/*
3717 * Obtain the characters that Backspace and Enter produce on "fd".
3718 * Returns OK or FAIL.
3719 */
3720 int
3721get_tty_info(int fd, ttyinfo_T *info)
3722{
3723#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724# ifdef HAVE_TERMIOS_H
3725 struct termios keys;
3726# else
3727 struct termio keys;
3728# endif
3729
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003730 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003732 info->backspace = keys.c_cc[VERASE];
3733 info->interrupt = keys.c_cc[VINTR];
3734 if (keys.c_iflag & ICRNL)
3735 info->enter = NL;
3736 else
3737 info->enter = CAR;
3738 if (keys.c_oflag & ONLCR)
3739 info->nl_does_cr = TRUE;
3740 else
3741 info->nl_does_cr = FALSE;
3742 return OK;
3743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003745 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 struct sgttyb keys;
3747
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003748 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003750 info->backspace = keys.sg_erase;
3751 info->interrupt = keys.sg_kill;
3752 info->enter = CAR;
3753 info->nl_does_cr = TRUE;
3754 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003757 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758}
3759
Bram Moolenaar0f873732019-12-05 20:28:46 +01003760#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003762static int mouse_ison = FALSE;
3763
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764/*
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003765 * Set mouse clicks on or off and possible enable mouse movement events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 */
3767 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003768mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003770#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003771 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 int xterm_mouse_vers;
3774
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003775#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003776 if (!on)
3777 // Make sure not tracing mouse movements. Important when a button-down
3778 // was received but no release yet.
3779 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003780#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003781
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003782 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003783#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003784 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003785#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003786 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003787 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 return;
3789
3790 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003791
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003792#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003793 if (ttym_flags == TTYM_URXVT)
3794 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003795 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003796 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003797 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003798#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003799
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003800 if (ttym_flags == TTYM_SGR)
3801 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003802 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003803 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003804 mouse_ison = on;
3805 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003806
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003807#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003808 if (bevalterm_ison != (p_bevalterm && on))
3809 {
3810 bevalterm_ison = (p_bevalterm && on);
3811 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003812 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003813 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003814 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003815#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 if (xterm_mouse_vers > 0)
3818 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003819 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 out_str_nf((char_u *)
3821 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003822 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003823#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003824 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003825#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003826 "\033[?1002h")
3827 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003828 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003830 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003831 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003834#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 else if (ttym_flags == TTYM_DEC)
3836 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003837 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003839 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003841 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003845#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 else
3847 {
3848 if (on)
3849 {
3850 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003851 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 }
3853 else
3854 {
3855 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003856 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 }
3858 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003861#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003862 else
3863 {
3864 if (on)
3865 {
3866 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003867 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003868 }
3869 else
3870 {
3871 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003872 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003873 }
3874 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003875#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003876
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003877#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 else
3879 {
3880 if (on)
3881 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003882 // D - Enable Mouse up/down messages
3883 // L - Enable Left Button Reporting
3884 // M - Enable Middle Button Reporting
3885 // R - Enable Right Button Reporting
3886 // K - Enable SHIFT and CTRL key Reporting
3887 // + - Enable Advanced messaging of mouse moves and up/down messages
3888 // Q - Quiet No Ack
3889 // # - Numeric value of mouse pointer required
3890 // 0 = Multiview 2000 cursor, used as standard
3891 // 1 = Windows Arrow
3892 // 2 = Windows I Beam
3893 // 3 = Windows Hour Glass
3894 // 4 = Windows Cross Hair
3895 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003896# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003897 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003898 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003899# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003900 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003901# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003902 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
3904 else
3905 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003906 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003907 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
3909 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003910#endif
3911#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 else
3913 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003914 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 if (on)
3916 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3917 else
3918 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003919 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922}
3923
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003924#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003925/*
3926 * Called when 'balloonevalterm' changed.
3927 */
3928 void
3929mch_bevalterm_changed(void)
3930{
3931 mch_setmouse(mouse_ison);
3932}
3933#endif
3934
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935/*
3936 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3937 */
3938 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003939check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
3941# ifdef FEAT_MOUSE_XTERM
3942 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003943# ifdef FEAT_MOUSE_URXVT
3944 && use_xterm_mouse() != 3
3945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946# ifdef FEAT_GUI
3947 && !gui.in_use
3948# endif
3949 )
3950 {
3951 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003952 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 if (*p_mouse != NUL)
3954 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003955 // force mouse off and maybe on to send possibly new mouse
3956 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 mch_setmouse(FALSE);
3958 setmouse();
3959 }
3960 }
3961 else
3962 del_mouse_termcode(KS_MOUSE);
3963# endif
3964
3965# ifdef FEAT_MOUSE_GPM
3966 if (!use_xterm_mouse()
3967# ifdef FEAT_GUI
3968 && !gui.in_use
3969# endif
3970 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003971 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003972 else
3973 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974# endif
3975
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003976# ifdef FEAT_SYSMOUSE
3977 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_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003983# endif
3984
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003986 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 if (!use_xterm_mouse()
3988# ifdef FEAT_GUI
3989 && !gui.in_use
3990# endif
3991 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003992 set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 else
3994 del_mouse_termcode(KS_JSBTERM_MOUSE);
3995# endif
3996
3997# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003998 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3999 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 if (!use_xterm_mouse()
4001# ifdef FEAT_GUI
4002 && !gui.in_use
4003# endif
4004 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004005 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 else
4007 del_mouse_termcode(KS_NETTERM_MOUSE);
4008# endif
4009
4010# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01004011 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004012 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013# ifdef FEAT_GUI
4014 && !gui.in_use
4015# endif
4016 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004017 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004018 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 else
4020 del_mouse_termcode(KS_DEC_MOUSE);
4021# endif
4022# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004023 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004024 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025# ifdef FEAT_GUI
4026 && !gui.in_use
4027# endif
4028 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004029 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 else
4031 del_mouse_termcode(KS_PTERM_MOUSE);
4032# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004033# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004034 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004035# ifdef FEAT_GUI
4036 && !gui.in_use
4037# endif
4038 )
4039 {
4040 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004041 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004042
4043 if (*p_mouse != NUL)
4044 {
4045 mch_setmouse(FALSE);
4046 setmouse();
4047 }
4048 }
4049 else
4050 del_mouse_termcode(KS_URXVT_MOUSE);
4051# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004052 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004053# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004054 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004055# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004056 )
4057 {
4058 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004059 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004060
4061 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004062 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004063
4064 if (*p_mouse != NUL)
4065 {
4066 mch_setmouse(FALSE);
4067 setmouse();
4068 }
4069 }
4070 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004071 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004072 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004073 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077#ifndef VMS
4078
4079/*
4080 * Try to get the current window size:
4081 * 1. with an ioctl(), most accurate method
4082 * 2. from the environment variables LINES and COLUMNS
4083 * 3. from the termcap
4084 * 4. keep using the old values
4085 * Return OK when size could be determined, FAIL otherwise.
4086 */
4087 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004088mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089{
4090 long rows = 0;
4091 long columns = 0;
4092 char_u *p;
4093
4094 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 * 1. try using an ioctl. It is the most accurate method.
4096 *
4097 * Try using TIOCGWINSZ first, some systems that have it also define
4098 * TIOCGSIZE but don't have a struct ttysize.
4099 */
4100# ifdef TIOCGWINSZ
4101 {
4102 struct winsize ws;
4103 int fd = 1;
4104
Bram Moolenaar0f873732019-12-05 20:28:46 +01004105 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 if (!isatty(fd) && isatty(read_cmd_fd))
4107 fd = read_cmd_fd;
4108 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4109 {
4110 columns = ws.ws_col;
4111 rows = ws.ws_row;
4112 }
4113 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004114# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115# ifdef TIOCGSIZE
4116 {
4117 struct ttysize ts;
4118 int fd = 1;
4119
Bram Moolenaar0f873732019-12-05 20:28:46 +01004120 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 if (!isatty(fd) && isatty(read_cmd_fd))
4122 fd = read_cmd_fd;
4123 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4124 {
4125 columns = ts.ts_cols;
4126 rows = ts.ts_lines;
4127 }
4128 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004129# endif // TIOCGSIZE
4130# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
4132 /*
4133 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004134 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4135 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004137 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 {
4139 if ((p = (char_u *)getenv("LINES")))
4140 rows = atoi((char *)p);
4141 if ((p = (char_u *)getenv("COLUMNS")))
4142 columns = atoi((char *)p);
4143 }
4144
4145#ifdef HAVE_TGETENT
4146 /*
4147 * 3. try reading "co" and "li" entries from termcap
4148 */
4149 if (columns == 0 || rows == 0)
4150 getlinecol(&columns, &rows);
4151#endif
4152
4153 /*
4154 * 4. If everything fails, use the old values
4155 */
4156 if (columns <= 0 || rows <= 0)
4157 return FAIL;
4158
4159 Rows = rows;
4160 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004161 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 return OK;
4163}
4164
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004165#if defined(FEAT_TERMINAL) || defined(PROTO)
4166/*
4167 * Report the windows size "rows" and "cols" to tty "fd".
4168 */
4169 int
4170mch_report_winsize(int fd, int rows, int cols)
4171{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004172 int tty_fd;
4173 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004174
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004175 tty_fd = get_tty_fd(fd);
4176 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004177 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004178# if defined(TIOCSWINSZ)
4179 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004180
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004181 ws.ws_col = cols;
4182 ws.ws_row = rows;
4183 ws.ws_xpixel = cols * 5;
4184 ws.ws_ypixel = rows * 10;
4185 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4186 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4187 retval == 0 ? "success" : "failed");
4188# elif defined(TIOCSSIZE)
4189 struct ttysize ts;
4190
4191 ts.ts_cols = cols;
4192 ts.ts_lines = rows;
4193 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4194 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4195 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004196# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004197 if (tty_fd != fd)
4198 close(tty_fd);
4199 }
4200 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004201}
4202#endif
4203
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204/*
4205 * Try to set the window size to Rows and Columns.
4206 */
4207 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004208mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209{
4210 if (*T_CWS)
4211 {
4212 /*
4213 * NOTE: if you get an error here that term_set_winsize() is
4214 * undefined, check the output of configure. It could probably not
4215 * find a ncurses, termcap or termlib library.
4216 */
4217 term_set_winsize((int)Rows, (int)Columns);
4218 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004219 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221}
4222
Bram Moolenaar0f873732019-12-05 20:28:46 +01004223#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
4225/*
4226 * Rows and/or Columns has changed.
4227 */
4228 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004229mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004231 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232}
4233
Bram Moolenaar205b8862011-09-07 15:04:31 +02004234/*
4235 * Wait for process "child" to end.
4236 * Return "child" if it exited properly, <= 0 on error.
4237 */
4238 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004239wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004240{
4241 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004242 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004243
4244 while (wait_pid != child)
4245 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004246 // When compiled with Python threads are probably used, in which case
4247 // wait() sometimes hangs for no obvious reason. Use waitpid()
4248 // instead and loop (like the GUI). Also needed for other interfaces,
4249 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004250# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004251 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004252# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004253 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004254# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004255 if (wait_pid == 0)
4256 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004257 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004258 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004259 if (++delay_msec > 10)
4260 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004261 continue;
4262 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004263 if (wait_pid <= 0
4264# ifdef ECHILD
4265 && errno == ECHILD
4266# endif
4267 )
4268 break;
4269 }
4270 return wait_pid;
4271}
4272
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004273#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004274/*
4275 * Set the environment for a child process.
4276 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004277 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004278set_child_environment(
4279 long rows,
4280 long columns,
4281 char *term,
4282 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004283{
4284# ifdef HAVE_SETENV
4285 char envbuf[50];
4286# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004287 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004288 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004289 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004290 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004291 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004292# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004293 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004294# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004295# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004296 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004297# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004298# endif
4299
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004300# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004301 setenv("TERM", term, 1);
4302 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004303 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004304 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004305 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004306 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004307 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004308 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004309 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004310# ifdef FEAT_TERMINAL
4311 if (is_terminal)
4312 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004313 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004314 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4315 }
4316# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004317# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004318 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004319# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004320# else
4321 /*
4322 * Putenv does not copy the string, it has to remain valid.
4323 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004324 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004325 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004326 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4327 putenv(envbuf_Term);
4328 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004329 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004330 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4331 putenv(envbuf_Lines);
4332 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4333 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004334 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004335 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004336 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004337# ifdef FEAT_TERMINAL
4338 if (is_terminal)
4339 {
4340 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004341 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004342 putenv(envbuf_Version);
4343 }
4344# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004345# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004346 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4347 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4348 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004349# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004350# endif
4351}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004352
4353 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004354set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004355{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004356 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004357}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004358#endif
4359
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004360#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004361/*
4362 * Open a PTY, with FD for the master and slave side.
4363 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004364 * When successful both file descriptors are stored and the allocated pty name
4365 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004366 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004367 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004368open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004369{
4370 char *tty_name;
4371
Bram Moolenaar59386482019-02-10 22:43:46 +01004372 if (name1 != NULL)
4373 *name1 = NULL;
4374 if (name2 != NULL)
4375 *name2 = NULL;
4376
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004377 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004378 if (*pty_master_fd >= 0)
4379 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004380 // Leaving out O_NOCTTY may lead to waitpid() always returning
4381 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4382 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004383#ifdef O_NOCTTY
4384 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4385#else
4386 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4387#endif
4388 if (*pty_slave_fd < 0)
4389 {
4390 close(*pty_master_fd);
4391 *pty_master_fd = -1;
4392 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004393 else
4394 {
4395 if (name1 != NULL)
4396 *name1 = vim_strsave((char_u *)tty_name);
4397 if (name2 != NULL)
4398 *name2 = vim_strsave((char_u *)tty_name);
4399 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004400 }
4401}
4402#endif
4403
Bram Moolenaarfae42832017-08-01 22:24:26 +02004404/*
4405 * Send SIGINT to a child process if "c" is an interrupt character.
4406 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004407 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004408may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4409{
4410# ifdef SIGINT
4411 if (c == Ctrl_C || c == intr_char)
4412 {
4413# ifdef HAVE_SETSID
4414 kill(-pid, SIGINT);
4415# else
4416 kill(0, SIGINT);
4417# endif
4418 if (wpid > 0)
4419 kill(wpid, SIGINT);
4420 }
4421# endif
4422}
4423
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004424#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004425
Bram Moolenaar0f873732019-12-05 20:28:46 +01004426/*
4427 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4428 * pointers, the last one is NULL.
4429 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4430 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004431 int
4432unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004433 char_u *cmd,
4434 char ***argvp,
4435 char_u **sh_tofree,
4436 char_u **shcf_tofree)
4437{
4438 char **argv = NULL;
4439 int argc;
4440
4441 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004442 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004443 return FAIL;
4444
4445 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4446 return FAIL;
4447 *argvp = argv;
4448
4449 if (cmd != NULL)
4450 {
4451 char_u *s;
4452 char_u *p;
4453
4454 if (extra_shell_arg != NULL)
4455 argv[argc++] = (char *)extra_shell_arg;
4456
Bram Moolenaar0f873732019-12-05 20:28:46 +01004457 // Break 'shellcmdflag' into white separated parts. This doesn't
4458 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004459 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004460 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004461 return FAIL;
4462 s = *shcf_tofree;
4463 p = p_shcf;
4464 while (*p != NUL)
4465 {
4466 argv[argc++] = (char *)s;
4467 while (*p && *p != ' ' && *p != TAB)
4468 *s++ = *p++;
4469 *s++ = NUL;
4470 p = skipwhite(p);
4471 }
4472
4473 argv[argc++] = (char *)cmd;
4474 }
4475 argv[argc] = NULL;
4476 return OK;
4477}
4478#endif
4479
4480#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4481/*
4482 * Use a terminal window to run a shell command in.
4483 */
4484 static int
4485mch_call_shell_terminal(
4486 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004487 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004488{
4489 jobopt_T opt;
4490 char **argv = NULL;
4491 char_u *tofree1 = NULL;
4492 char_u *tofree2 = NULL;
4493 int retval = -1;
4494 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004495 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004496 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004497 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004498
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004499 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004500 goto theend;
4501
4502 init_job_options(&opt);
4503 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4504 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004505 if (buf == NULL)
4506 goto theend;
4507
4508 job = term_getjob(buf->b_term);
4509 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004510
Bram Moolenaar0f873732019-12-05 20:28:46 +01004511 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004512 aucmd_prepbuf(&aco, buf);
4513
4514 clear_oparg(&oa);
4515 while (term_use_loop())
4516 {
4517 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4518 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004519 // If terminal_loop() returns OK we got a key that is handled
4520 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004521 if (terminal_loop(TRUE) == OK)
4522 normal_cmd(&oa, TRUE);
4523 }
4524 else
4525 normal_cmd(&oa, TRUE);
4526 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004527 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004528 ch_log(NULL, "system command finished");
4529
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004530 job_unref(job);
4531
Bram Moolenaar0f873732019-12-05 20:28:46 +01004532 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004533 aucmd_restbuf(&aco);
4534
4535 wait_return(TRUE);
4536 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4537
4538theend:
4539 vim_free(argv);
4540 vim_free(tofree1);
4541 vim_free(tofree2);
4542 return retval;
4543}
4544#endif
4545
4546#ifdef USE_SYSTEM
4547/*
4548 * Use system() to start the shell: simple but slow.
4549 */
4550 static int
4551mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004552 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004553 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554{
4555#ifdef VMS
4556 char *ifn = NULL;
4557 char *ofn = NULL;
4558#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004559 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004560 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004561 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
4563 out_flush();
4564
4565 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004566 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaar62b42182010-09-21 22:09:37 +02004568# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004569 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004570 loose_clipboard();
4571# endif
4572
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if (cmd == NULL)
4574 x = system((char *)p_sh);
4575 else
4576 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004577# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 if (ofn = strchr((char *)cmd, '>'))
4579 *ofn++ = '\0';
4580 if (ifn = strchr((char *)cmd, '<'))
4581 {
4582 char *p;
4583
4584 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004585 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 if (p)
4587 *p = '\0';
4588 }
4589 if (ofn)
4590 x = vms_sys((char *)cmd, ofn, ifn);
4591 else
4592 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004593# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004594 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004596 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 if (newcmd == NULL)
4598 x = 0;
4599 else
4600 {
4601 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4602 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4603 (char *)p_shcf,
4604 (char *)cmd);
4605 x = system((char *)newcmd);
4606 vim_free(newcmd);
4607 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
4610# ifdef VMS
4611 x = vms_sys_status(x);
4612# endif
4613 if (emsg_silent)
4614 ;
4615 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004616 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 else if (x && !(options & SHELL_SILENT))
4618 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004619 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 msg_outnum((long)x);
4621 msg_putchar('\n');
4622 }
4623
4624 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004625 {
4626 // The shell may have messed with the mode, always set it.
4627 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004628 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004631# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4632 restore_clipboard();
4633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004635}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636
Bram Moolenaar0f873732019-12-05 20:28:46 +01004637#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
Bram Moolenaar0f873732019-12-05 20:28:46 +01004639# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4640 // 127, some shells use that already
4641# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
Bram Moolenaar13568252018-03-16 20:46:58 +01004643/*
4644 * Don't use system(), use fork()/exec().
4645 */
4646 static int
4647mch_call_shell_fork(
4648 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004649 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004650{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004651 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004653 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 pid_t wait_pid = 0;
4655# ifdef HAVE_UNION_WAIT
4656 union wait status;
4657# else
4658 int status = -1;
4659# endif
4660 int retval = -1;
4661 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004662 char_u *tofree1 = NULL;
4663 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004665 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004666# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004668# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004669 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 int fd_fromshell[2];
4671 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004672 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
4674 out_flush();
4675 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004676 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004677 if (tmode == TMODE_RAW)
4678 // The shell may have messed with the mode, always set it later.
4679 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004681 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004682 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004683
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004685 * For the GUI, when writing the output into the buffer and when reading
4686 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4687 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004689 if ((options & (SHELL_READ|SHELL_WRITE))
4690# ifdef FEAT_GUI
4691 || (gui.in_use && show_shell_mess)
4692# endif
4693 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004695# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 /*
4697 * Try to open a master pty.
4698 * If this works, open the slave pty.
4699 * If the slave can't be opened, close the master pty.
4700 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004701 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004702 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 /*
4704 * If not opening a pty or it didn't work, try using pipes.
4705 */
4706 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
4709 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004710 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 {
4712 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004713 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
4715 close(fd_toshell[0]);
4716 close(fd_toshell[1]);
4717 }
4718 }
4719 if (pipe_error)
4720 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004721 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 out_flush();
4723 }
4724 }
4725 }
4726
Bram Moolenaar0f873732019-12-05 20:28:46 +01004727 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004729 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004730 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004731 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004732 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004734 UNBLOCK_SIGNALS(&curset);
4735
Bram Moolenaar32526b32019-01-19 17:43:09 +01004736 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004737 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004739 || (gui.in_use && show_shell_mess)
4740# endif
4741 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004743# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004744 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 {
4746 close(pty_master_fd);
4747 close(pty_slave_fd);
4748 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004749 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004750# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
4752 close(fd_toshell[0]);
4753 close(fd_toshell[1]);
4754 close(fd_fromshell[0]);
4755 close(fd_fromshell[1]);
4756 }
4757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004759 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004761 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004762 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
Bram Moolenaar819524702018-02-27 19:10:00 +01004764# ifdef FEAT_JOB_CHANNEL
4765 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004766 {
4767 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004768 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004769 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004770# endif
4771
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 if (!show_shell_mess || (options & SHELL_EXPAND))
4773 {
4774 int fd;
4775
4776 /*
4777 * Don't want to show any message from the shell. Can't just
4778 * close stdout and stderr though, because some systems will
4779 * break if you try to write to them after that, so we must
4780 * use dup() to replace them with something else -- webb
4781 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4782 * waiting for input.
4783 */
4784 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4785 fclose(stdin);
4786 fclose(stdout);
4787 fclose(stderr);
4788
4789 /*
4790 * If any of these open()'s and dup()'s fail, we just continue
4791 * anyway. It's not fatal, and on most systems it will make
4792 * no difference at all. On a few it will cause the execvp()
4793 * to exit with a non-zero status even when the completion
4794 * could be done, which is nothing too serious. If the open()
4795 * or dup() failed we'd just do the same thing ourselves
4796 * anyway -- webb
4797 */
4798 if (fd >= 0)
4799 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004800 vim_ignored = dup(fd); // To replace stdin (fd 0)
4801 vim_ignored = dup(fd); // To replace stdout (fd 1)
4802 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803
Bram Moolenaar0f873732019-12-05 20:28:46 +01004804 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 close(fd);
4806 }
4807 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004808 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004810 || gui.in_use
4811# endif
4812 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
4814
Bram Moolenaardf177f62005-02-22 08:39:57 +00004815# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004816 // Create our own process group, so that the child and all its
4817 // children can be kill()ed. Don't do this when using pipes,
4818 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004819 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004820 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004821 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004822# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004823 // When doing "!xterm&" and 'shell' is bash: the shell
4824 // will exit and send SIGHUP to all processes in its
4825 // group, killing the just started process. Ignore SIGHUP
4826 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004827 signal(SIGHUP, SIG_IGN);
4828# endif
4829 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004830# endif
4831# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004832 if (pty_slave_fd >= 0)
4833 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004834 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004835 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004836 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004837# ifdef TIOCSCTTY
4838 // Try to become controlling tty (probably doesn't work,
4839 // unless run by root)
4840 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4841# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004842 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004843# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004844 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
Bram Moolenaara5792f52005-11-23 21:25:05 +00004846 /*
4847 * stderr is only redirected when using the GUI, so that a
4848 * program like gpg can still access the terminal to get a
4849 * passphrase using stderr.
4850 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004851# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 if (pty_master_fd >= 0)
4853 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004854 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
Bram Moolenaar0f873732019-12-05 20:28:46 +01004856 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004858 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004860 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004861 if (gui.in_use)
4862 {
4863 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004864 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
Bram Moolenaar0f873732019-12-05 20:28:46 +01004867 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 }
4869 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004872 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 close(fd_toshell[1]);
4874 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004875 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 close(fd_toshell[0]);
4877
Bram Moolenaar0f873732019-12-05 20:28:46 +01004878 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 close(fd_fromshell[0]);
4880 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004881 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 close(fd_fromshell[1]);
4883
Bram Moolenaara5792f52005-11-23 21:25:05 +00004884# ifdef FEAT_GUI
4885 if (gui.in_use)
4886 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004887 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004888 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004889 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004890 }
4891# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
4893 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004894
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 /*
4896 * There is no type cast for the argv, because the type may be
4897 * different on different machines. This may cause a warning
4898 * message with strict compilers, don't worry about it.
4899 * Call _exit() instead of exit() to avoid closing the connection
4900 * to the X server (esp. with GTK, which uses atexit()).
4901 */
4902 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004903 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004905 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
4907 /*
4908 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004909 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 */
4911 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004912 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004913 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004914# ifdef FEAT_JOB_CHANNEL
4915 ++dont_check_job_ended;
4916# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 /*
4918 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004919 * This is also used to pipe stdin/stdout to/from the external
4920 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004922 if ((options & (SHELL_READ|SHELL_WRITE))
4923# ifdef FEAT_GUI
4924 || (gui.in_use && show_shell_mess)
4925# endif
4926 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004928# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004930 int buffer_off = 0; // valid bytes in buffer[]
4931 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4932 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 int len;
4934 int p_more_save;
4935 int old_State;
4936 int c;
4937 int toshell_fd;
4938 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004939 garray_T ga;
4940 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004941# ifdef ELAPSED_FUNC
4942 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004943# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
Bram Moolenaardf177f62005-02-22 08:39:57 +00004945# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 if (pty_master_fd >= 0)
4947 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 fromshell_fd = pty_master_fd;
4949 toshell_fd = dup(pty_master_fd);
4950 }
4951 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
4954 close(fd_toshell[0]);
4955 close(fd_fromshell[1]);
4956 toshell_fd = fd_toshell[1];
4957 fromshell_fd = fd_fromshell[0];
4958 }
4959
4960 /*
4961 * Write to the child if there are typed characters.
4962 * Read from the child if there are characters available.
4963 * Repeat the reading a few times if more characters are
4964 * available. Need to check for typed keys now and then, but
4965 * not too often (delays when no chars are available).
4966 * This loop is quit if no characters can be read from the pty
4967 * (WaitForChar detected special condition), or there are no
4968 * characters available and the child has exited.
4969 * Only check if the child has exited when there is no more
4970 * output. The child may exit before all the output has
4971 * been printed.
4972 *
4973 * Currently this busy loops!
4974 * This can probably dead-lock when the write blocks!
4975 */
4976 p_more_save = p_more;
4977 p_more = FALSE;
4978 old_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004979 State = MODE_EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004981 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004982 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004983 // Fork a process that will write the lines to the
4984 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004985 if ((wpid = fork()) == -1)
4986 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004987 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004988 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004989 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004990 {
4991 linenr_T lnum = curbuf->b_op_start.lnum;
4992 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004993 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004994 size_t l;
4995
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004996 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004997 for (;;)
4998 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004999 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005000 if (l == 0)
5001 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005002 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005003 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00005004 len = write(toshell_fd, "", (size_t)1);
5005 else
5006 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005007 char_u *s = vim_strchr(lp + written, NL);
5008
Bram Moolenaar89d40322006-08-29 15:30:07 +00005009 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00005010 s == NULL ? l
5011 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005012 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00005013 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005014 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005015 // Finished a line, add a NL, unless this line
5016 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005017 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005018 || (!curbuf->b_p_bin
5019 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005020 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00005021 && (lnum !=
5022 curbuf->b_ml.ml_line_count
5023 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005024 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005025 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005026 ++lnum;
5027 if (lnum > curbuf->b_op_end.lnum)
5028 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005029 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005030 close(toshell_fd);
5031 toshell_fd = -1;
5032 break;
5033 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005034 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005035 written = 0;
5036 }
5037 else if (len > 0)
5038 written += len;
5039 }
5040 _exit(0);
5041 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005042 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005043 {
5044 close(toshell_fd);
5045 toshell_fd = -1;
5046 }
5047 }
5048
5049 if (options & SHELL_READ)
5050 ga_init2(&ga, 1, BUFLEN);
5051
5052 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005053# ifdef ELAPSED_FUNC
5054 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 for (;;)
5057 {
5058 /*
5059 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005060 * if there are any.
5061 * Don't do this if we are expanding wild cards (would eat
5062 * typeahead).
5063 * Don't do this when filtering and terminal is in cooked
5064 * mode, the shell command will handle the I/O. Avoids
5065 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005066 * Don't get characters when the child has already
5067 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005068 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005069 * while (noread_cnt > 4), avoids that ":r !ls" eats
5070 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 */
5072 len = 0;
5073 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005074 && ((options &
5075 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5076 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005077# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005078 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005079# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005080 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005081 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005082 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005084 if (ta_len == 0)
5085 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005086 // Get extra characters when we don't have any.
5087 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005088 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005089# ifdef ELAPSED_FUNC
5090 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005091# endif
5092 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5093 }
5094 if (ta_len > 0 || len > 0)
5095 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 /*
5097 * For pipes:
5098 * Check for CTRL-C: send interrupt signal to child.
5099 * Check for CTRL-D: EOF, close pipe to child.
5100 */
5101 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5102 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 /*
5104 * Send SIGINT to the child's group or all
5105 * processes in our group.
5106 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005107 may_send_sigint(ta_buf[ta_len], pid, wpid);
5108
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 if (pty_master_fd < 0 && toshell_fd >= 0
5110 && ta_buf[ta_len] == Ctrl_D)
5111 {
5112 close(toshell_fd);
5113 toshell_fd = -1;
5114 }
5115 }
5116
Bram Moolenaarf4140482020-02-15 23:06:45 +01005117 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118
5119 /*
5120 * For pipes: echo the typed characters.
5121 * For a pty this does not seem to work.
5122 */
5123 if (pty_master_fd < 0)
5124 {
5125 for (i = ta_len; i < ta_len + len; ++i)
5126 {
5127 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5128 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 else if (has_mbyte)
5130 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005131 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
5133 msg_outtrans_len(ta_buf + i, l);
5134 i += l - 1;
5135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 else
5137 msg_outtrans_len(ta_buf + i, 1);
5138 }
5139 windgoto(msg_row, msg_col);
5140 out_flush();
5141 }
5142
5143 ta_len += len;
5144
5145 /*
5146 * Write the characters to the child, unless EOF has
5147 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005148 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005149 * When writing buffer lines, drop the typed
5150 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005152 if (options & SHELL_WRITE)
5153 ta_len = 0;
5154 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 {
5156 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5157 if (len > 0)
5158 {
5159 ta_len -= len;
5160 mch_memmove(ta_buf, ta_buf + len, ta_len);
5161 }
5162 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165
Bram Moolenaardf177f62005-02-22 08:39:57 +00005166 if (got_int)
5167 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005168 // CTRL-C sends a signal to the child, we ignore it
5169 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005170# ifdef HAVE_SETSID
5171 kill(-pid, SIGINT);
5172# else
5173 kill(0, SIGINT);
5174# endif
5175 if (wpid > 0)
5176 kill(wpid, SIGINT);
5177 got_int = FALSE;
5178 }
5179
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 /*
5181 * Check if the child has any characters to be printed.
5182 * Read them and write them to our window. Repeat this as
5183 * long as there is something to do, avoid the 10ms wait
5184 * for mch_inchar(), or sending typeahead characters to
5185 * the external process.
5186 * TODO: This should handle escape sequences, compatible
5187 * to some terminal (vt52?).
5188 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005189 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005190 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005192 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005195 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005197
5198 noread_cnt = 0;
5199 if (options & SHELL_READ)
5200 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005201 // Do NUL -> NL translation, append NL separated
5202 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005203 for (i = 0; i < len; ++i)
5204 {
5205 if (buffer[i] == NL)
5206 append_ga_line(&ga);
5207 else if (buffer[i] == NUL)
5208 ga_append(&ga, NL);
5209 else
5210 ga_append(&ga, buffer[i]);
5211 }
5212 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005213 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 {
5215 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005216 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
Bram Moolenaardf177f62005-02-22 08:39:57 +00005218 len += buffer_off;
5219 buffer[len] = NUL;
5220
Bram Moolenaar0f873732019-12-05 20:28:46 +01005221 // Check if the last character in buffer[] is
5222 // incomplete, keep these bytes for the next
5223 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 for (p = buffer; p < buffer + len; p += l)
5225 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005226 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005228 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 else if (MB_BYTE2LEN(*p) != l)
5230 break;
5231 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005232 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005234 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 if (len >= 12)
5236 ++p;
5237 else
5238 {
5239 buffer_off = len;
5240 continue;
5241 }
5242 }
5243 c = *p;
5244 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005245 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 if (p < buffer + len)
5247 {
5248 *p = c;
5249 buffer_off = (buffer + len) - p;
5250 mch_memmove(buffer, p, buffer_off);
5251 continue;
5252 }
5253 buffer_off = 0;
5254 }
5255 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 {
5257 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005258 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 }
5260
5261 windgoto(msg_row, msg_col);
5262 cursor_on();
5263 out_flush();
5264 if (got_int)
5265 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005266
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005267# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005268 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005269 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005270 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005271
Bram Moolenaar0f873732019-12-05 20:28:46 +01005272 // Avoid that we keep looping here without
5273 // checking for a CTRL-C for a long time. Don't
5274 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005275 if (msec > 2000)
5276 {
5277 noread_cnt = 5;
5278 break;
5279 }
5280 }
5281# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 }
5283
Bram Moolenaar0f873732019-12-05 20:28:46 +01005284 // If we already detected the child has finished, continue
5285 // reading output for a short while. Some text may be
5286 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005287 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005288 {
5289 if (noread_cnt < 5)
5290 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005291 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005292 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005293
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 /*
5295 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005296 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005298# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005299 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005300# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005302# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5304 || (wait_pid == pid && WIFEXITED(status)))
5305 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005306 // Don't break the loop yet, try reading more
5307 // characters from "fromshell_fd" first. When using
5308 // pipes there might still be something to read and
5309 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005312 else
5313 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005314
Bram Moolenaar95a51352013-03-21 22:53:50 +01005315# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005316 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005317 clip_update();
5318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 }
5320finished:
5321 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005322 if (options & SHELL_READ)
5323 {
5324 if (ga.ga_len > 0)
5325 {
5326 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005327 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005328 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005329 }
5330 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005331 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005332 ga_clear(&ga);
5333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 /*
5336 * Give all typeahead that wasn't used back to ui_inchar().
5337 */
5338 if (ta_len)
5339 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 State = old_State;
5341 if (toshell_fd >= 0)
5342 close(toshell_fd);
5343 close(fromshell_fd);
5344 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005345# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005346 else
5347 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005348 long delay_msec = 1;
5349
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005350 if (tmode == TMODE_RAW)
5351 // possibly disables modifyOtherKeys, so that the system
5352 // can recognize CTRL-C
5353 out_str(T_CTE);
Bram Moolenaar0981c872020-08-23 14:28:37 +02005354
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005355 /*
5356 * Similar to the loop above, but only handle X events, no
5357 * I/O.
5358 */
5359 for (;;)
5360 {
5361 if (got_int)
5362 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005363 // CTRL-C sends a signal to the child, we ignore it
5364 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005365# ifdef HAVE_SETSID
5366 kill(-pid, SIGINT);
5367# else
5368 kill(0, SIGINT);
5369# endif
5370 got_int = FALSE;
5371 }
5372# ifdef __NeXT__
5373 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5374# else
5375 wait_pid = waitpid(pid, &status, WNOHANG);
5376# endif
5377 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5378 || (wait_pid == pid && WIFEXITED(status)))
5379 {
5380 wait_pid = pid;
5381 break;
5382 }
5383
Bram Moolenaar0f873732019-12-05 20:28:46 +01005384 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005385 clip_update();
5386
Bram Moolenaar0f873732019-12-05 20:28:46 +01005387 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005388 // less time, gradually wait longer.
5389 mch_delay(delay_msec,
5390 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005391 if (++delay_msec > 10)
5392 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005393 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005394
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005395 if (tmode == TMODE_RAW)
5396 // possibly enables modifyOtherKeys again
5397 out_str(T_CTI);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005398 }
5399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
5401 /*
5402 * Wait until our child has exited.
5403 * Ignore wait() returning pids of other children and returning
5404 * because of some signal like SIGWINCH.
5405 * Don't wait if wait_pid was already set above, indicating the
5406 * child already exited.
5407 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005408 if (wait_pid != pid)
5409 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
Bram Moolenaar624891f2010-10-13 16:22:09 +02005411# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005412 // Close slave side of pty. Only do this after the child has
5413 // exited, otherwise the child may hang when it tries to write on
5414 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005415 if (pty_master_fd >= 0)
5416 close(pty_slave_fd);
5417# endif
5418
Bram Moolenaar0f873732019-12-05 20:28:46 +01005419 // Make sure the child that writes to the external program is
5420 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005421 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005422 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005423 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005424 wait4pid(wpid, NULL);
5425 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005426
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005427# ifdef FEAT_JOB_CHANNEL
5428 --dont_check_job_ended;
5429# endif
5430
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 /*
5432 * Set to raw mode right now, otherwise a CTRL-C after
5433 * catch_signals() will kill Vim.
5434 */
5435 if (tmode == TMODE_RAW)
5436 settmode(TMODE_RAW);
5437 did_settmode = TRUE;
5438 set_signals();
5439
5440 if (WIFEXITED(status))
5441 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005442 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005444 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
5446 if (retval == EXEC_FAILED)
5447 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005448 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 msg_outtrans(p_sh);
5450 msg_putchar('\n');
5451 }
5452 else if (!(options & SHELL_SILENT))
5453 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005454 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 msg_outnum((long)retval);
5456 msg_putchar('\n');
5457 }
5458 }
5459 }
5460 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005461 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 }
5463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
5465error:
5466 if (!did_settmode)
5467 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005468 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005470 vim_free(argv);
5471 vim_free(tofree1);
5472 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473
5474 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005475}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005476#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005477
5478 int
5479mch_call_shell(
5480 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005481 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005482{
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01005483#ifdef FEAT_JOB_CHANNEL
5484 ch_log(NULL, "executing shell command: %s", cmd);
5485#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005486#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5487 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5488 return mch_call_shell_terminal(cmd, options);
5489#endif
5490#ifdef USE_SYSTEM
5491 return mch_call_shell_system(cmd, options);
5492#else
5493 return mch_call_shell_fork(cmd, options);
5494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495}
5496
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005497#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005498 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005499mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005500{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005501 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005502 int fd_in[2] = {-1, -1}; // for stdin
5503 int fd_out[2] = {-1, -1}; // for stdout
5504 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005505 int pty_master_fd = -1;
5506 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005507 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005508 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5509 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5510 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005511 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005512 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5513 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005514 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005515 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005516 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005517
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005518 if (use_out_for_err && use_null_for_out)
5519 use_null_for_err = TRUE;
5520
Bram Moolenaar0f873732019-12-05 20:28:46 +01005521 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005522 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005523
Bram Moolenaarb2412082017-08-20 18:09:14 +02005524 if (options->jo_pty
5525 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005526 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005527 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005528 open_pty(&pty_master_fd, &pty_slave_fd,
5529 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005530
Bram Moolenaar0f873732019-12-05 20:28:46 +01005531 // TODO: without the channel feature connect the child to /dev/null?
5532 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005533 if (use_file_for_in)
5534 {
5535 char_u *fname = options->jo_io_name[PART_IN];
5536
5537 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5538 if (fd_in[0] < 0)
5539 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005540 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005541 goto failed;
5542 }
5543 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005544 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005545 // When writing buffer lines to the input don't use the pty, so that
5546 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005547 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5548 && pipe(fd_in) < 0)
5549 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005550
5551 if (use_file_for_out)
5552 {
5553 char_u *fname = options->jo_io_name[PART_OUT];
5554
5555 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5556 if (fd_out[1] < 0)
5557 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005558 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005559 goto failed;
5560 }
5561 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005562 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005563 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005564
5565 if (use_file_for_err)
5566 {
5567 char_u *fname = options->jo_io_name[PART_ERR];
5568
5569 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5570 if (fd_err[1] < 0)
5571 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005572 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005573 goto failed;
5574 }
5575 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005576 else if (!use_out_for_err && !use_null_for_err
5577 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005578 goto failed;
5579
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005580 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5581 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005582 if (options->jo_set & JO_CHANNEL)
5583 {
5584 channel = options->jo_channel;
5585 if (channel != NULL)
5586 ++channel->ch_refcount;
5587 }
5588 else
5589 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005590 if (channel == NULL)
5591 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005592 if (job->jv_tty_out != NULL)
5593 ch_log(channel, "using pty %s on fd %d",
5594 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005595 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005596
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005597 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005598 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005599 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005600 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005601 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005602 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005603 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005604 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005605 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005606 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005607 int null_fd = -1;
5608 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005609
Bram Moolenaar0f873732019-12-05 20:28:46 +01005610 // child
5611 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005612 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005613
Bram Moolenaar819524702018-02-27 19:10:00 +01005614# ifdef FEAT_JOB_CHANNEL
5615 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005616 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005617 ch_logfile((char_u *)"", (char_u *)"");
5618# endif
5619
Bram Moolenaar835dc632016-02-07 14:27:38 +01005620# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005621 // Create our own process group, so that the child and all its
5622 // children can be kill()ed. Don't do this when using pipes,
5623 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005624 (void)setsid();
5625# endif
5626
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005627# ifdef FEAT_TERMINAL
5628 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005629 {
5630 char *term = (char *)T_NAME;
5631
5632#ifdef FEAT_GUI
5633 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005634 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005635 term = getenv("TERM");
5636#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005637 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005638 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005639 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005640 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005641 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005642 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005643 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005644 else if (t_colors > 16)
5645 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005646 else
5647 term = "xterm";
5648 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005649 set_child_environment(
5650 (long)options->jo_term_rows,
5651 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005652 term,
5653 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005654 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005655 else
5656# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005657 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005658
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005659 if (options->jo_env != NULL)
5660 {
5661 dict_T *dict = options->jo_env;
5662 hashitem_T *hi;
5663 int todo = (int)dict->dv_hashtab.ht_used;
5664
5665 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5666 if (!HASHITEM_EMPTY(hi))
5667 {
5668 typval_T *item = &dict_lookup(hi)->di_tv;
5669
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005670 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005671 --todo;
5672 }
5673 }
5674
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005675 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005676 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005677 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005678 if (null_fd < 0)
5679 {
5680 perror("opening /dev/null failed");
5681 _exit(OPEN_NULL_FAILED);
5682 }
5683 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005684
Bram Moolenaar223896d2017-08-02 22:33:28 +02005685 if (pty_slave_fd >= 0)
5686 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005687 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005688 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005689# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005690 // Try to become controlling tty (probably doesn't work,
5691 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005692 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5693# endif
5694 }
5695
Bram Moolenaar0f873732019-12-05 20:28:46 +01005696 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005697 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005698 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005699 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005700 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005701 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005702 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005703 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005704
Bram Moolenaar0f873732019-12-05 20:28:46 +01005705 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005706 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005707 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005708 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005709 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005710 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005711 }
5712 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005713 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005714 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005715 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005716 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005717 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005718
Bram Moolenaar0f873732019-12-05 20:28:46 +01005719 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005720 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005721 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005722 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005723 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005724 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005725 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005726 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005727
5728 if (fd_in[0] >= 0)
5729 close(fd_in[0]);
5730 if (fd_in[1] >= 0)
5731 close(fd_in[1]);
5732 if (fd_out[0] >= 0)
5733 close(fd_out[0]);
5734 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005735 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005736 if (fd_err[0] >= 0)
5737 close(fd_err[0]);
5738 if (fd_err[1] >= 0)
5739 close(fd_err[1]);
5740 if (pty_master_fd >= 0)
5741 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005742 close(pty_master_fd); // not used in the child
5743 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005744 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005745
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005746 if (null_fd >= 0)
5747 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005748
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005749 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5750 _exit(EXEC_FAILED);
5751
Bram Moolenaar0f873732019-12-05 20:28:46 +01005752 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005753 execvp(argv[0], argv);
5754
Bram Moolenaar4694a172016-04-21 14:05:23 +02005755 if (stderr_works)
5756 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005757# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005758 // calling free_all_mem() here causes problems. Ignore valgrind
5759 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005760# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005761 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005762 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005763
Bram Moolenaar0f873732019-12-05 20:28:46 +01005764 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005765 UNBLOCK_SIGNALS(&curset);
5766
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005767 job->jv_pid = pid;
5768 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005769 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005770
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005771 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005772 close(pty_slave_fd); // not used in the parent
5773 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005774 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005775 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005776 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005777 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005778 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005779 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005780 if (channel != NULL)
5781 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005782 int in_fd = INVALID_FD;
5783 int out_fd = INVALID_FD;
5784 int err_fd = INVALID_FD;
5785
5786 if (!(use_file_for_in || use_null_for_in))
5787 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5788
5789 if (!(use_file_for_out || use_null_for_out))
5790 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5791
5792 // When using pty_master_fd only set it for stdout, do not duplicate
5793 // it for stderr, it only needs to be read once.
5794 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5795 {
5796 if (fd_err[0] >= 0)
5797 err_fd = fd_err[0];
5798 else if (out_fd != pty_master_fd)
5799 err_fd = pty_master_fd;
5800 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005801
5802 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005803 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005804 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005805 else
5806 {
5807 if (fd_in[1] >= 0)
5808 close(fd_in[1]);
5809 if (fd_out[0] >= 0)
5810 close(fd_out[0]);
5811 if (fd_err[0] >= 0)
5812 close(fd_err[0]);
5813 if (pty_master_fd >= 0)
5814 close(pty_master_fd);
5815 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005816
Bram Moolenaar0f873732019-12-05 20:28:46 +01005817 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005818 return;
5819
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005820failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005821 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005822 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005823 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005824 if (fd_in[1] >= 0)
5825 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005826 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005827 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005828 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005829 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005830 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005831 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005832 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005833 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005834 if (pty_master_fd >= 0)
5835 close(pty_master_fd);
5836 if (pty_slave_fd >= 0)
5837 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005838}
5839
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005840 static char_u *
5841get_signal_name(int sig)
5842{
5843 int i;
5844 char_u numbuf[NUMBUFLEN];
5845
5846 if (sig == SIGKILL)
5847 return vim_strsave((char_u *)"kill");
5848
5849 for (i = 0; signal_info[i].sig != -1; i++)
5850 if (sig == signal_info[i].sig)
5851 return strlow_save((char_u *)signal_info[i].name);
5852
5853 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5854 return vim_strsave(numbuf);
5855}
5856
Bram Moolenaar835dc632016-02-07 14:27:38 +01005857 char *
5858mch_job_status(job_T *job)
5859{
5860# ifdef HAVE_UNION_WAIT
5861 union wait status;
5862# else
5863 int status = -1;
5864# endif
5865 pid_t wait_pid = 0;
5866
5867# ifdef __NeXT__
5868 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5869# else
5870 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5871# endif
5872 if (wait_pid == -1)
5873 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005874 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005875 if (job->jv_status < JOB_ENDED)
5876 ch_log(job->jv_channel, "Job no longer exists: %s",
5877 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005878 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005879 }
5880 if (wait_pid == 0)
5881 return "run";
5882 if (WIFEXITED(status))
5883 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005884 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005885 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005886 if (job->jv_status < JOB_ENDED)
5887 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005888 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005889 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005890 if (WIFSIGNALED(status))
5891 {
5892 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005893 job->jv_termsig = get_signal_name(WTERMSIG(status));
5894 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5895 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5896 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005897 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005898 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005899 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005900
5901return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005902 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005903 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005904 return "dead";
5905}
5906
5907 job_T *
5908mch_detect_ended_job(job_T *job_list)
5909{
5910# ifdef HAVE_UNION_WAIT
5911 union wait status;
5912# else
5913 int status = -1;
5914# endif
5915 pid_t wait_pid = 0;
5916 job_T *job;
5917
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005918# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005919 // Do not do this when waiting for a shell command to finish, we would get
5920 // the exit value here (and discard it), the exit value obtained there
5921 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005922 if (dont_check_job_ended > 0)
5923 return NULL;
5924# endif
5925
Bram Moolenaar97792de2016-10-15 18:36:49 +02005926# ifdef __NeXT__
5927 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5928# else
5929 wait_pid = waitpid(-1, &status, WNOHANG);
5930# endif
5931 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005932 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005933 return NULL;
5934 for (job = job_list; job != NULL; job = job->jv_next)
5935 {
5936 if (job->jv_pid == wait_pid)
5937 {
5938 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005939 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005940 job->jv_exitval = WEXITSTATUS(status);
5941 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005942 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005943 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005944 job->jv_termsig = get_signal_name(WTERMSIG(status));
5945 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005946 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005947 {
5948 ch_log(job->jv_channel, "Job ended");
5949 job->jv_status = JOB_ENDED;
5950 }
5951 return job;
5952 }
5953 }
5954 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005955}
5956
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005957/*
5958 * Send a (deadly) signal to "job".
5959 * Return FAIL if "how" is not a valid name.
5960 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005961 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005962mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005963{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005964 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005965
Bram Moolenaar923d9262016-02-25 20:56:01 +01005966 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005967 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005968 else if (STRCMP(how, "hup") == 0)
5969 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005970 else if (STRCMP(how, "quit") == 0)
5971 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005972 else if (STRCMP(how, "int") == 0)
5973 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005974 else if (STRCMP(how, "kill") == 0)
5975 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005976#ifdef SIGWINCH
5977 else if (STRCMP(how, "winch") == 0)
5978 sig = SIGWINCH;
5979#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005980 else if (isdigit(*how))
5981 sig = atoi((char *)how);
5982 else
5983 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005984
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005985 // Never kill ourselves!
5986 if (job->jv_pid != 0)
5987 {
5988 // TODO: have an option to only kill the process, not the group?
5989 kill(-job->jv_pid, sig);
5990 kill(job->jv_pid, sig);
5991 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005992
Bram Moolenaar835dc632016-02-07 14:27:38 +01005993 return OK;
5994}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005995
5996/*
5997 * Clear the data related to "job".
5998 */
5999 void
6000mch_clear_job(job_T *job)
6001{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006002 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01006003# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006004 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006005# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006006 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006007# endif
6008}
Bram Moolenaar835dc632016-02-07 14:27:38 +01006009#endif
6010
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006011#if defined(FEAT_TERMINAL) || defined(PROTO)
6012 int
6013mch_create_pty_channel(job_T *job, jobopt_T *options)
6014{
6015 int pty_master_fd = -1;
6016 int pty_slave_fd = -1;
6017 channel_T *channel;
6018
Bram Moolenaar59386482019-02-10 22:43:46 +01006019 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02006020 if (pty_master_fd < 0 || pty_slave_fd < 0)
6021 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006022 close(pty_slave_fd);
6023
6024 channel = add_channel();
6025 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006026 {
6027 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006028 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006029 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006030 if (job->jv_tty_out != NULL)
6031 ch_log(channel, "using pty %s on fd %d",
6032 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006033 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006034 channel->ch_keep_open = TRUE;
6035
Bram Moolenaar0f873732019-12-05 20:28:46 +01006036 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6037 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006038 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006039 channel_set_job(channel, job, options);
6040 return OK;
6041}
6042#endif
6043
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044/*
6045 * Check for CTRL-C typed by reading all available characters.
6046 * In cooked mode we should get SIGINT, no need to check.
6047 */
6048 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006049mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006051 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006052 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 fill_input_buf(FALSE);
6054}
6055
6056/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006057 * Wait "msec" msec until a character is available from the mouse, keyboard,
6058 * from inbuf[].
6059 * "msec" == -1 will block forever.
6060 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006061 * When "ignore_input" is TRUE even check for pending input when input is
6062 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006063 * "interrupted" (if not NULL) is set to TRUE when no character is available
6064 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006065 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006066 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 */
6068 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006069WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006071#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006072 return ui_wait_for_chars_or_timer(
6073 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006074#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006075 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006076#endif
6077}
6078
6079/*
6080 * Wait "msec" msec until a character is available from the mouse or keyboard
6081 * or from inbuf[].
6082 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006083 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006084 * "interrupted" (if not NULL) is set to TRUE when no character is available
6085 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006086 * When a GUI is being used, this will never get called -- webb
6087 */
6088 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006089WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006090{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091#ifdef FEAT_MOUSE_GPM
6092 int gpm_process_wanted;
6093#endif
6094#ifdef FEAT_XCLIPBOARD
6095 int rest;
6096#endif
6097 int avail;
6098
Bram Moolenaar0f873732019-12-05 20:28:46 +01006099 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 return 1;
6101
6102#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006103 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 if (WantQueryMouse)
6105 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006106 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006107 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006108 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 }
6110#endif
6111
6112 /*
6113 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6114 * events. This is a bit complicated, because they might both be defined.
6115 */
6116#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6117# ifdef FEAT_XCLIPBOARD
6118 rest = 0;
6119 if (do_xterm_trace())
6120 rest = msec;
6121# endif
6122 do
6123 {
6124# ifdef FEAT_XCLIPBOARD
6125 if (rest != 0)
6126 {
6127 msec = XT_TRACE_DELAY;
6128 if (rest >= 0 && rest < XT_TRACE_DELAY)
6129 msec = rest;
6130 if (rest >= 0)
6131 rest -= msec;
6132 }
6133# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006134# ifdef FEAT_SOUND_CANBERRA
6135 // Invoke any pending sound callbacks.
6136 if (has_sound_callback_in_queue())
6137 invoke_sound_callback();
6138# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139# ifdef FEAT_MOUSE_GPM
6140 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006141 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006142 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006143 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006145 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006149 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 return 1;
6151# ifdef FEAT_XCLIPBOARD
6152 if (rest == 0 || !do_xterm_trace())
6153# endif
6154 break;
6155 }
6156 }
6157 while (FALSE
6158# ifdef FEAT_MOUSE_GPM
6159 || (gpm_process_wanted && mch_gpm_process() == 0)
6160# endif
6161# ifdef FEAT_XCLIPBOARD
6162 || (!avail && rest != 0)
6163# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006164 )
6165 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166
6167#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006168 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169#endif
6170 return avail;
6171}
6172
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006173#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174/*
6175 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006176 * "msec" == 0 will check for characters once.
6177 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006180 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006181 * "interrupted" (if not NULL) is set to TRUE when no character is available
6182 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006184 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006185RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186{
6187 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006188 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006189#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 static int busy = FALSE;
6191
Bram Moolenaar0f873732019-12-05 20:28:46 +01006192 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193# define MAY_LOOP
6194
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006195# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006196 // Remember at what time we started, so that we know how much longer we
6197 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006198 long start_msec = msec;
6199 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006201 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006202 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203# endif
6204
Bram Moolenaar0f873732019-12-05 20:28:46 +01006205 // Handle being called recursively. This may happen for the session
6206 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 if (busy)
6208 return 0;
6209#endif
6210
6211#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006212 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213#endif
6214 {
6215#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006216 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006217# ifdef FEAT_MZSCHEME
6218 int mzquantum_used = FALSE;
6219# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220#endif
6221#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006222 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006223 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 int nfd;
6225# ifdef FEAT_XCLIPBOARD
6226 int xterm_idx = -1;
6227# endif
6228# ifdef FEAT_MOUSE_GPM
6229 int gpm_idx = -1;
6230# endif
6231# ifdef USE_XSMP
6232 int xsmp_idx = -1;
6233# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006234 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006236# ifdef FEAT_MZSCHEME
6237 mzvim_check_threads();
6238 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6239 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006240 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006241 mzquantum_used = TRUE;
6242 }
6243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 fds[0].fd = fd;
6245 fds[0].events = POLLIN;
6246 nfd = 1;
6247
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006249 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 if (xterm_Shell != (Widget)0)
6251 {
6252 xterm_idx = nfd;
6253 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6254 fds[nfd].events = POLLIN;
6255 nfd++;
6256 }
6257# endif
6258# ifdef FEAT_MOUSE_GPM
6259 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6260 {
6261 gpm_idx = nfd;
6262 fds[nfd].fd = gpm_fd;
6263 fds[nfd].events = POLLIN;
6264 nfd++;
6265 }
6266# endif
6267# ifdef USE_XSMP
6268 if (xsmp_icefd != -1)
6269 {
6270 xsmp_idx = nfd;
6271 fds[nfd].fd = xsmp_icefd;
6272 fds[nfd].events = POLLIN;
6273 nfd++;
6274 }
6275# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006276#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006277 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006278#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006279 if (interrupted != NULL)
6280 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006282 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006283
6284 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006285 if (result == 0 && interrupted != NULL && ret > 0)
6286 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006287
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006288# ifdef FEAT_MZSCHEME
6289 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006290 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006291 finished = FALSE;
6292# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294# ifdef FEAT_XCLIPBOARD
6295 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6296 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006297 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006299 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 finished = FALSE;
6301 }
6302# endif
6303# ifdef FEAT_MOUSE_GPM
6304 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306# endif
6307# ifdef USE_XSMP
6308 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6309 {
6310 if (fds[xsmp_idx].revents & POLLIN)
6311 {
6312 busy = TRUE;
6313 xsmp_handle_requests();
6314 busy = FALSE;
6315 }
6316 else if (fds[xsmp_idx].revents & POLLHUP)
6317 {
6318 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006319 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 xsmp_close();
6321 }
6322 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006323 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 }
6325# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006326#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006327 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006328 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006329 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331
Bram Moolenaar0f873732019-12-05 20:28:46 +01006332#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333
6334 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006335 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006336 // These are static because they can take 8 Kbyte each and cause the
6337 // signal stack to run out with -O3.
6338 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006340 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006342# ifdef FEAT_MZSCHEME
6343 mzvim_check_threads();
6344 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6345 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006346 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006347 mzquantum_used = TRUE;
6348 }
6349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006351 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006353 tv.tv_sec = towait / 1000;
6354 tv.tv_usec = (towait % 1000) * (1000000/1000);
6355 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006357 else
6358 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359
6360 /*
6361 * Select on ready for reading and exceptional condition (end of file).
6362 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006363select_eintr:
6364 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006365 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 FD_ZERO(&efds);
6367 FD_SET(fd, &rfds);
K.Takatab247e062022-02-07 10:45:23 +00006368# ifndef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006369 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 FD_SET(fd, &efds);
6371# endif
6372 maxfd = fd;
6373
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006375 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 if (xterm_Shell != (Widget)0)
6377 {
6378 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6379 if (maxfd < ConnectionNumber(xterm_dpy))
6380 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006381
Bram Moolenaar0f873732019-12-05 20:28:46 +01006382 // An event may have already been read but not handled. In
6383 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006384 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 }
6386# endif
6387# ifdef FEAT_MOUSE_GPM
6388 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6389 {
6390 FD_SET(gpm_fd, &rfds);
6391 FD_SET(gpm_fd, &efds);
6392 if (maxfd < gpm_fd)
6393 maxfd = gpm_fd;
6394 }
6395# endif
6396# ifdef USE_XSMP
6397 if (xsmp_icefd != -1)
6398 {
6399 FD_SET(xsmp_icefd, &rfds);
6400 FD_SET(xsmp_icefd, &efds);
6401 if (maxfd < xsmp_icefd)
6402 maxfd = xsmp_icefd;
6403 }
6404# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006405# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006406 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006407# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006408 if (interrupted != NULL)
6409 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410
Bram Moolenaar643b6142018-09-12 20:29:09 +02006411 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6412 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006413 result = ret > 0 && FD_ISSET(fd, &rfds);
6414 if (result)
6415 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006416 else if (interrupted != NULL && ret > 0)
6417 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006418
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006419# ifdef EINTR
6420 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006421 {
dbivolaruab16ad32021-12-29 19:41:47 +00006422 // Check whether the EINTR is caused by SIGTSTP
6423 if (got_tstp && !in_mch_suspend)
6424 {
6425 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006426
dbivolaruab16ad32021-12-29 19:41:47 +00006427 ea.forceit = TRUE;
6428 ex_stop(&ea);
6429 got_tstp = FALSE;
6430 }
6431
Bram Moolenaar0f873732019-12-05 20:28:46 +01006432 // Check whether window has been resized, EINTR may be caused by
6433 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006434 if (do_resize)
6435 handle_resize();
6436
Bram Moolenaar0f873732019-12-05 20:28:46 +01006437 // Interrupted by a signal, need to try again. We ignore msec
6438 // here, because we do want to check even after a timeout if
6439 // characters are available. Needed for reading output of an
6440 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006441 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006442 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006443# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006444# ifdef __TANDEM
6445 if (ret == -1 && errno == ENOTSUP)
6446 {
6447 FD_ZERO(&rfds);
6448 FD_ZERO(&efds);
6449 ret = 0;
6450 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006451# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006452# ifdef FEAT_MZSCHEME
6453 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006454 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006455 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456# endif
6457
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458# ifdef FEAT_XCLIPBOARD
6459 if (ret > 0 && xterm_Shell != (Widget)0
6460 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6461 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006462 xterm_update(); // Maybe we should hand out clipboard
6463 // continue looping when we only got the X event and the input
6464 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 if (--ret == 0 && !input_available())
6466 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006467 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 finished = FALSE;
6469 }
6470 }
6471# endif
6472# ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00006473 if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 {
6475 if (FD_ISSET(gpm_fd, &efds))
6476 gpm_close();
6477 else if (FD_ISSET(gpm_fd, &rfds))
6478 *check_for_gpm = 1;
6479 }
6480# endif
6481# ifdef USE_XSMP
6482 if (ret > 0 && xsmp_icefd != -1)
6483 {
6484 if (FD_ISSET(xsmp_icefd, &efds))
6485 {
6486 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006487 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 xsmp_close();
6489 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006490 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 }
6492 else if (FD_ISSET(xsmp_icefd, &rfds))
6493 {
6494 busy = TRUE;
6495 xsmp_handle_requests();
6496 busy = FALSE;
6497 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006498 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 }
6500 }
6501# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006502#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006503 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006504 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006505 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
Bram Moolenaar0f873732019-12-05 20:28:46 +01006508#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
6510#ifdef MAY_LOOP
6511 if (finished || msec == 0)
6512 break;
6513
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006514# ifdef FEAT_CLIENTSERVER
6515 if (server_waiting())
6516 break;
6517# endif
6518
Bram Moolenaar0f873732019-12-05 20:28:46 +01006519 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 if (msec > 0)
6521 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006522# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006523 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006524 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006526 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 msec = msec / 2;
6528# endif
6529 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006530 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 }
6532#endif
6533 }
6534
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006535 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536}
6537
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006539 * Expand a path into all matching files and/or directories. Handles "*",
6540 * "?", "[a-z]", "**", etc.
6541 * "path" has backslashes before chars that are not to be expanded.
6542 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 */
6544 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006545mch_expandpath(
6546 garray_T *gap,
6547 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006548 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
Bram Moolenaar02743632005-07-25 20:42:36 +00006550 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552
6553/*
6554 * mch_expand_wildcards() - this code does wild-card pattern matching using
6555 * the shell
6556 *
6557 * return OK for success, FAIL for error (you may lose some memory) and put
6558 * an error message in *file.
6559 *
6560 * num_pat is number of input patterns
6561 * pat is array of pointers to input patterns
6562 * num_file is pointer to number of matched file names
6563 * file is pointer to array of pointers to matched file names
6564 */
6565
6566#ifndef SEEK_SET
6567# define SEEK_SET 0
6568#endif
6569#ifndef SEEK_END
6570# define SEEK_END 2
6571#endif
6572
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006573#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006574
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006576mch_expand_wildcards(
6577 int num_pat,
6578 char_u **pat,
6579 int *num_file,
6580 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006581 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582{
6583 int i;
6584 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006585 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 char_u *p;
6587 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588
Bram Moolenaarc7247912008-01-13 12:54:11 +00006589 /*
6590 * This is the non-OS/2 implementation (really Unix).
6591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 int j;
6593 char_u *tempname;
6594 char_u *command;
6595 FILE *fd;
6596 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006597#define STYLE_ECHO 0 // use "echo", the default
6598#define STYLE_GLOB 1 // use "glob", for csh
6599#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6600#define STYLE_PRINT 3 // use "print -N", for zsh
6601#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6602 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 int shell_style = STYLE_ECHO;
6604 int check_spaces;
6605 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006606 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006607 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006608 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609
Bram Moolenaar0f873732019-12-05 20:28:46 +01006610 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 *file = NULL;
6612
6613 /*
6614 * If there are no wildcards, just copy the names to allocated memory.
6615 * Saves a lot of time, because we don't have to start a new shell.
6616 */
6617 if (!have_wildcard(num_pat, pat))
6618 return save_patterns(num_pat, pat, num_file, file);
6619
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006620# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006621 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006622 if (sandbox != 0 && check_secure())
6623 return FAIL;
6624# endif
6625
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 /*
6627 * Don't allow the use of backticks in secure and restricted mode.
6628 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006629 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 for (i = 0; i < num_pat; ++i)
6631 if (vim_strchr(pat[i], '`') != NULL
6632 && (check_restricted() || check_secure()))
6633 return FAIL;
6634
6635 /*
6636 * get a name for the temp file
6637 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006638 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006640 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 return FAIL;
6642 }
6643
6644 /*
6645 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006646 * file.
6647 * STYLE_BT: NL separated
6648 * If expanding `cmd` execute it directly.
6649 * STYLE_GLOB: NUL separated
6650 * If we use *csh, "glob" will work better than "echo".
6651 * STYLE_PRINT: NL or NUL separated
6652 * If we use *zsh, "print -N" will work better than "glob".
6653 * STYLE_VIMGLOB: NL separated
6654 * If we use *sh*, we define "vimglob()".
6655 * STYLE_ECHO: space separated.
6656 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 */
6658 if (num_pat == 1 && *pat[0] == '`'
6659 && (len = STRLEN(pat[0])) > 2
6660 && *(pat[0] + len - 1) == '`')
6661 shell_style = STYLE_BT;
6662 else if ((len = STRLEN(p_sh)) >= 3)
6663 {
6664 if (STRCMP(p_sh + len - 3, "csh") == 0)
6665 shell_style = STYLE_GLOB;
6666 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6667 shell_style = STYLE_PRINT;
6668 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006669 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6670 "sh") != NULL)
6671 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672
Bram Moolenaar0f873732019-12-05 20:28:46 +01006673 // Compute the length of the command. We need 2 extra bytes: for the
6674 // optional '&' and for the NUL.
6675 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006677 if (shell_style == STYLE_VIMGLOB)
6678 len += STRLEN(sh_vimglob_func);
6679
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006680 for (i = 0; i < num_pat; ++i)
6681 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006682 // Count the length of the patterns in the same way as they are put in
6683 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006684#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006685 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006686#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006687 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006688 for (j = 0; pat[i][j] != NUL; ++j)
6689 {
6690 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006691 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006692 ++len;
6693 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006694#endif
6695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 command = alloc(len);
6697 if (command == NULL)
6698 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006699 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 vim_free(tempname);
6701 return FAIL;
6702 }
6703
6704 /*
6705 * Build the shell command:
6706 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6707 * recognizes this).
6708 * - Add the shell command to print the expanded names.
6709 * - Add the temp file name.
6710 * - Add the file name patterns.
6711 */
6712 if (shell_style == STYLE_BT)
6713 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006714 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006715 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006716 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006718 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006719 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006721 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006723 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 *p = ' ';
6725 }
6726 STRCAT(command, ">");
6727 }
6728 else
6729 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006730 STRCPY(command, "");
6731 if (shell_style == STYLE_GLOB)
6732 {
6733 // Assume the nonomatch option is valid only for csh like shells,
6734 // otherwise, this may set the positional parameters for the shell,
6735 // e.g. "$*".
6736 if (flags & EW_NOTFOUND)
6737 STRCAT(command, "set nonomatch; ");
6738 else
6739 STRCAT(command, "unset nonomatch; ");
6740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 if (shell_style == STYLE_GLOB)
6742 STRCAT(command, "glob >");
6743 else if (shell_style == STYLE_PRINT)
6744 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006745 else if (shell_style == STYLE_VIMGLOB)
6746 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 else
6748 STRCAT(command, "echo >");
6749 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006750
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006752
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 if (shell_style != STYLE_BT)
6754 for (i = 0; i < num_pat; ++i)
6755 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006756 // When using system() always add extra quotes, because the shell
6757 // is started twice. Otherwise put a backslash before special
6758 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759#ifdef USE_SYSTEM
6760 STRCAT(command, " \"");
6761 STRCAT(command, pat[i]);
6762 STRCAT(command, "\"");
6763#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006764 int intick = FALSE;
6765
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 p = command + STRLEN(command);
6767 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006768 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006769 {
6770 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006771 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006772 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6773 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006774 // Remove a backslash, take char literally. But keep
6775 // backslash inside backticks, before a special character
6776 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006777 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006778 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6779 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006780 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006781 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006782 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006783 else if (!intick
6784 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6785 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006786 // Put a backslash before a special character, but not
6787 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6788 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006789 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006790
Bram Moolenaar0f873732019-12-05 20:28:46 +01006791 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006792 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 *p = NUL;
6795#endif
6796 }
6797 if (flags & EW_SILENT)
6798 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006799 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006800 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
6802 /*
6803 * Using zsh -G: If a pattern has no matches, it is just deleted from
6804 * the argument list, otherwise zsh gives an error message and doesn't
6805 * expand any other pattern.
6806 */
6807 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006808 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809
6810 /*
6811 * If we use -f then shell variables set in .cshrc won't get expanded.
6812 * vi can do it, so we will too, but it is only necessary if there is a "$"
6813 * in one of the patterns, otherwise we can still use the fast option.
6814 */
6815 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006816 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
6818 /*
6819 * execute the shell command
6820 */
6821 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6822
Bram Moolenaar0f873732019-12-05 20:28:46 +01006823 // When running in the background, give it some time to create the temp
6824 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006825 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006826 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827
Bram Moolenaar0f873732019-12-05 20:28:46 +01006828 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 show_shell_mess = TRUE;
6830 vim_free(command);
6831
Bram Moolenaar0f873732019-12-05 20:28:46 +01006832 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 {
6834 mch_remove(tempname);
6835 vim_free(tempname);
6836 /*
6837 * With interactive completion, the error message is not printed.
6838 * However with USE_SYSTEM, I don't know how to turn off error messages
6839 * from the shell, so screen may still get messed up -- webb.
6840 */
6841#ifndef USE_SYSTEM
6842 if (!(flags & EW_SILENT))
6843#endif
6844 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006845 redraw_later_clear(); // probably messed up screen
6846 msg_putchar('\n'); // clear bottom line quickly
6847 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848#ifdef USE_SYSTEM
6849 if (!(flags & EW_SILENT))
6850#endif
6851 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006852 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006853 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 }
6855 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006856 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6857 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 if (shell_style == STYLE_BT)
6859 return FAIL;
6860 goto notfound;
6861 }
6862
6863 /*
6864 * read the names from the file into memory
6865 */
6866 fd = fopen((char *)tempname, READBIN);
6867 if (fd == NULL)
6868 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006869 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 if (!(flags & EW_SILENT))
6871 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006872 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006873 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 }
6875 vim_free(tempname);
6876 goto notfound;
6877 }
6878 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006879 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006881 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006882 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006883 buffer = NULL;
6884 else
6885 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 if (buffer == NULL)
6887 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006888 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 mch_remove(tempname);
6890 vim_free(tempname);
6891 fclose(fd);
6892 return FAIL;
6893 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006894 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 i = fread((char *)buffer, 1, len, fd);
6896 fclose(fd);
6897 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006898 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006900 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006901 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 vim_free(tempname);
6903 vim_free(buffer);
6904 return FAIL;
6905 }
6906 vim_free(tempname);
6907
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006908# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006909 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006911 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6913 *p++ = buffer[i];
6914 len = p - buffer;
6915# endif
6916
6917
Bram Moolenaar0f873732019-12-05 20:28:46 +01006918 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 if (shell_style == STYLE_ECHO)
6920 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006921 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006923 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 {
6925 while (*p != ' ' && *p != '\n')
6926 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006927 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 }
6929 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006930 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006931 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006933 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006935 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {
6937 while (*p != '\n' && *p != NUL)
6938 ++p;
6939 if (*p != NUL)
6940 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006941 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 }
6943 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006944 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 else
6946 {
6947 /*
6948 * Some versions of zsh use spaces instead of NULs to separate
6949 * results. Only do this when there is no NUL before the end of the
6950 * buffer, otherwise we would never be able to use file names with
6951 * embedded spaces when zsh does use NULs.
6952 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6953 * don't check for spaces again.
6954 */
6955 check_spaces = FALSE;
6956 if (shell_style == STYLE_PRINT && !did_find_nul)
6957 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006958 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006959 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006960 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 did_find_nul = TRUE;
6962 else
6963 check_spaces = TRUE;
6964 }
6965
6966 /*
6967 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6968 * already is one, for STYLE_GLOB it needs to be added.
6969 */
6970 if (len && buffer[len - 1] == NUL)
6971 --len;
6972 else
6973 buffer[len] = NUL;
6974 i = 0;
6975 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006976 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {
6978 ++i;
6979 *p = NUL;
6980 }
6981 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006982 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 }
6984 if (i == 0)
6985 {
6986 /*
6987 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6988 * /bin/sh will happily expand it to nothing rather than returning an
6989 * error; and hey, it's good to check anyway -- webb.
6990 */
6991 vim_free(buffer);
6992 goto notfound;
6993 }
6994 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006995 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 if (*file == NULL)
6997 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006998 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 vim_free(buffer);
7000 return FAIL;
7001 }
7002
7003 /*
7004 * Isolate the individual file names.
7005 */
7006 p = buffer;
7007 for (i = 0; i < *num_file; ++i)
7008 {
7009 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007010 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00007011 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
7012 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00007014 while (!(shell_style == STYLE_ECHO && *p == ' ')
7015 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007017 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 *p = NUL;
7019 else
7020 {
7021 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007022 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 }
7024 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007025 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007027 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007029 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 }
7031 }
7032
7033 /*
7034 * Move the file names to allocated memory.
7035 */
7036 for (j = 0, i = 0; i < *num_file; ++i)
7037 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007038 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7040 continue;
7041
Bram Moolenaar0f873732019-12-05 20:28:46 +01007042 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 dir = (mch_isdir((*file)[i]));
7044 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7045 continue;
7046
Bram Moolenaar0f873732019-12-05 20:28:46 +01007047 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007048 if (!dir && (flags & EW_EXEC)
7049 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007050 continue;
7051
Bram Moolenaar964b3742019-05-24 18:54:09 +02007052 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 if (p)
7054 {
7055 STRCPY(p, (*file)[i]);
7056 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007057 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 (*file)[j++] = p;
7059 }
7060 }
7061 vim_free(buffer);
7062 *num_file = j;
7063
Bram Moolenaar0f873732019-12-05 20:28:46 +01007064 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007066 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 goto notfound;
7068 }
7069
7070 return OK;
7071
7072notfound:
7073 if (flags & EW_NOTFOUND)
7074 return save_patterns(num_pat, pat, num_file, file);
7075 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076}
7077
Bram Moolenaar0f873732019-12-05 20:28:46 +01007078#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007081save_patterns(
7082 int num_pat,
7083 char_u **pat,
7084 int *num_file,
7085 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
7087 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007088 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007090 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 if (*file == NULL)
7092 return FAIL;
7093 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007094 {
7095 s = vim_strsave(pat[i]);
7096 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007097 // Be compatible with expand_filename(): halve the number of
7098 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007099 backslash_halve(s);
7100 (*file)[i] = s;
7101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 *num_file = num_pat;
7103 return OK;
7104}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106/*
7107 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7108 * expand.
7109 */
7110 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007111mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007113 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 if (*p == '\\' && p[1] != NUL)
7116 ++p;
7117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 if (vim_strchr((char_u *)
7119#ifdef VMS
7120 "*?%"
7121#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123#endif
7124 , *p) != NULL)
7125 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 }
7127 return FALSE;
7128}
7129
7130/*
7131 * Return TRUE if the string "p" contains a wildcard.
7132 * Don't recognize '~' at the end as a wildcard.
7133 */
7134 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007135mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007137 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 if (*p == '\\' && p[1] != NUL)
7140 ++p;
7141 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 if (vim_strchr((char_u *)
7143#ifdef VMS
7144 "*?%$"
7145#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147#endif
7148 , *p) != NULL
7149 || (*p == '~' && p[1] != NUL))
7150 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 }
7152 return FALSE;
7153}
7154
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007156have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157{
7158 int i;
7159
7160 for (i = 0; i < num; i++)
7161 if (mch_has_wildcard(file[i]))
7162 return 1;
7163 return 0;
7164}
7165
7166 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007167have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
7169 int i;
7170
7171 for (i = 0; i < num; i++)
7172 if (vim_strchr(file[i], '$') != NULL)
7173 return TRUE;
7174 return FALSE;
7175}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007177#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178/*
7179 * Scaled-down version of rename(), which is missing in Xenix.
7180 * This version can only move regular files and will fail if the
7181 * destination exists.
7182 */
7183 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007184mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185{
7186 struct stat st;
7187
Bram Moolenaar0f873732019-12-05 20:28:46 +01007188 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007190 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007192 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 return 0;
7194 return -1;
7195}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007196#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007198#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007199# if defined(DYNAMIC_GPM) || defined(PROTO)
7200/*
7201 * Initialize Gpm's symbols for dynamic linking.
7202 * Must be called only if libgpm_hinst is NULL.
7203 */
7204 static int
7205load_libgpm(void)
7206{
7207 libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL);
7208
7209 if (libgpm_hinst == NULL)
7210 {
7211 if (p_verbose > 0)
7212 smsg_attr(HL_ATTR(HLF_W),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007213 _("Could not load gpm library: %s"), dlerror());
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007214 return FAIL;
7215 }
7216
7217 if (
7218 (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL
7219 || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL
7220 || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL
7221 || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL
7222 || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL
7223 )
7224 {
7225 semsg(_(e_could_not_load_library_str_str), "gpm", dlerror());
7226 dlclose(libgpm_hinst);
7227 libgpm_hinst = NULL;
7228 dll_gpm_flag = NULL;
7229 dll_gpm_fd = NULL;
7230 return FAIL;
7231 }
7232 return OK;
7233}
7234
7235 int
7236gpm_available(void)
7237{
7238 return libgpm_hinst != NULL || load_libgpm() == OK;
7239}
7240# endif // DYNAMIC_GPM
7241
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242/*
7243 * Initializes connection with gpm (if it isn't already opened)
7244 * Return 1 if succeeded (or connection already opened), 0 if failed
7245 */
7246 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007247gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007249 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007251#ifdef DYNAMIC_GPM
7252 if (!gpm_available())
7253 return 0;
7254#endif
7255
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 if (!gpm_flag)
7257 {
7258 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7259 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007260 // Default handling for mouse move
7261 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 gpm_connect.maxMod = 0xffff;
7263 if (Gpm_Open(&gpm_connect, 0) > 0)
7264 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007265 // gpm library tries to handling TSTP causes
7266 // problems. Anyways, we close connection to Gpm whenever
7267 // we are going to suspend or starting an external process
7268 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007269# ifdef SIGTSTP
dbivolaruab16ad32021-12-29 19:41:47 +00007270 signal(SIGTSTP, restricted ? SIG_IGN : (RETSIGTYPE (*)())sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007271# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007272 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 }
7274 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007275 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 return 0;
7277 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007278 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279}
7280
7281/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007282 * Returns TRUE if the GPM mouse is enabled.
7283 */
7284 int
7285gpm_enabled(void)
7286{
7287 return gpm_flag && gpm_fd >= 0;
7288}
7289
7290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 */
7293 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007294gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007296 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 Gpm_Close();
7298}
7299
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007300/*
7301 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007303 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 */
7305 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007306mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
7308 int button;
7309 static Gpm_Event gpm_event;
7310 char_u string[6];
7311 int_u vim_modifiers;
7312 int row,col;
7313 unsigned char buttons_mask;
7314 unsigned char gpm_modifiers;
7315 static unsigned char old_buttons = 0;
7316
7317 Gpm_GetEvent(&gpm_event);
7318
7319#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007320 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 if (hold_gui_events)
7322 return 0;
7323#endif
7324
7325 row = gpm_event.y - 1;
7326 col = gpm_event.x - 1;
7327
Bram Moolenaar0f873732019-12-05 20:28:46 +01007328 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 string[1] = 'M';
7330 string[2] = 'G';
7331 switch (GPM_BARE_EVENTS(gpm_event.type))
7332 {
7333 case GPM_DRAG:
7334 string[3] = MOUSE_DRAG;
7335 break;
7336 case GPM_DOWN:
7337 buttons_mask = gpm_event.buttons & ~old_buttons;
7338 old_buttons = gpm_event.buttons;
7339 switch (buttons_mask)
7340 {
7341 case GPM_B_LEFT:
7342 button = MOUSE_LEFT;
7343 break;
7344 case GPM_B_MIDDLE:
7345 button = MOUSE_MIDDLE;
7346 break;
7347 case GPM_B_RIGHT:
7348 button = MOUSE_RIGHT;
7349 break;
7350 default:
7351 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007352 // Don't know what to do. Can more than one button be
7353 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 }
7355 string[3] = (char_u)(button | 0x20);
7356 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7357 break;
7358 case GPM_UP:
7359 string[3] = MOUSE_RELEASE;
7360 old_buttons &= ~gpm_event.buttons;
7361 break;
7362 default:
7363 return 0;
7364 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007365 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 gpm_modifiers = gpm_event.modifiers;
7367 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007368 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7369 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7370 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7372 vim_modifiers |= MOUSE_SHIFT;
7373
7374 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7375 vim_modifiers |= MOUSE_CTRL;
7376 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7377 vim_modifiers |= MOUSE_ALT;
7378 string[3] |= vim_modifiers;
7379 string[4] = (char_u)(col + ' ' + 1);
7380 string[5] = (char_u)(row + ' ' + 1);
7381 add_to_input_buf(string, 6);
7382 return 6;
7383}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007384#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007386#ifdef FEAT_SYSMOUSE
7387/*
7388 * Initialize connection with sysmouse.
7389 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7390 * output, any sysmouse output than will be processed via sig_sysmouse().
7391 * Return OK if succeeded, FAIL if failed.
7392 */
7393 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007394sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007395{
7396 struct mouse_info mouse;
7397
7398 mouse.operation = MOUSE_MODE;
7399 mouse.u.mode.mode = 0;
7400 mouse.u.mode.signal = SIGUSR2;
7401 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7402 {
7403 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7404 mouse.operation = MOUSE_SHOW;
7405 ioctl(1, CONS_MOUSECTL, &mouse);
7406 return OK;
7407 }
7408 return FAIL;
7409}
7410
7411/*
7412 * Stop processing SIGUSR2 signals, and also make sure that
7413 * virtual console do not send us any sysmouse related signal.
7414 */
7415 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007416sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007417{
7418 struct mouse_info mouse;
7419
7420 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7421 mouse.operation = MOUSE_MODE;
7422 mouse.u.mode.mode = 0;
7423 mouse.u.mode.signal = 0;
7424 ioctl(1, CONS_MOUSECTL, &mouse);
7425}
7426
7427/*
7428 * Gets info from sysmouse and adds special keys to input buf.
7429 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007430 static RETSIGTYPE
7431sig_sysmouse SIGDEFARG(sigarg)
7432{
7433 struct mouse_info mouse;
7434 struct video_info video;
7435 char_u string[6];
7436 int row, col;
7437 int button;
7438 int buttons;
7439 static int oldbuttons = 0;
7440
7441#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007442 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007443 if (hold_gui_events)
7444 return;
7445#endif
7446
7447 mouse.operation = MOUSE_GETINFO;
7448 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7449 && ioctl(1, FBIO_MODEINFO, &video) != -1
7450 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7451 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7452 {
7453 row = mouse.u.data.y / video.vi_cheight;
7454 col = mouse.u.data.x / video.vi_cwidth;
7455 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007456 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007457 string[1] = 'M';
7458 string[2] = 'S';
7459 if (oldbuttons == buttons && buttons != 0)
7460 {
7461 button = MOUSE_DRAG;
7462 }
7463 else
7464 {
7465 switch (buttons)
7466 {
7467 case 0:
7468 button = MOUSE_RELEASE;
7469 break;
7470 case 1:
7471 button = MOUSE_LEFT;
7472 break;
7473 case 2:
7474 button = MOUSE_MIDDLE;
7475 break;
7476 case 4:
7477 button = MOUSE_RIGHT;
7478 break;
7479 default:
7480 return;
7481 }
7482 oldbuttons = buttons;
7483 }
7484 string[3] = (char_u)(button);
7485 string[4] = (char_u)(col + ' ' + 1);
7486 string[5] = (char_u)(row + ' ' + 1);
7487 add_to_input_buf(string, 6);
7488 }
7489 return;
7490}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007491#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007492
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007494typedef char_u * (*STRPROCSTR)(char_u *);
7495typedef char_u * (*INTPROCSTR)(int);
7496typedef int (*STRPROCINT)(char_u *);
7497typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498
7499/*
7500 * Call a DLL routine which takes either a string or int param
7501 * and returns an allocated string.
7502 */
7503 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007504mch_libcall(
7505 char_u *libname,
7506 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007507 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007508 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007509 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007510 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511{
7512# if defined(USE_DLOPEN)
7513 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007514 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515# else
7516 shl_t hinstLib;
7517# endif
7518 STRPROCSTR ProcAdd;
7519 INTPROCSTR ProcAddI;
7520 char_u *retval_str = NULL;
7521 int retval_int = 0;
7522 int success = FALSE;
7523
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007524 /*
7525 * Get a handle to the DLL module.
7526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007528 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007529 (void)dlerror();
7530
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 hinstLib = dlopen((char *)libname, RTLD_LAZY
7532# ifdef RTLD_LOCAL
7533 | RTLD_LOCAL
7534# endif
7535 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007536 if (hinstLib == NULL)
7537 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007538 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007539 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007540 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007541 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543# else
7544 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7545# endif
7546
Bram Moolenaar0f873732019-12-05 20:28:46 +01007547 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 if (hinstLib != NULL)
7549 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007550# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 /*
7552 * Catch a crash when calling the library function. For example when
7553 * using a number where a string pointer is expected.
7554 */
7555 mch_startjmp();
7556 if (SETJMP(lc_jump_env) != 0)
7557 {
7558 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007559# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007560 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007561# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 mch_didjmp();
7563 }
7564 else
7565# endif
7566 {
7567 retval_str = NULL;
7568 retval_int = 0;
7569
7570 if (argstring != NULL)
7571 {
7572# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007573 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007574 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575# else
7576 if (shl_findsym(&hinstLib, (const char *)funcname,
7577 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7578 ProcAdd = NULL;
7579# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007580 if ((success = (ProcAdd != NULL
7581# if defined(USE_DLOPEN)
7582 && dlerr == NULL
7583# endif
7584 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 {
7586 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007587 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 else
7589 retval_str = (ProcAdd)(argstring);
7590 }
7591 }
7592 else
7593 {
7594# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007595 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007596 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597# else
7598 if (shl_findsym(&hinstLib, (const char *)funcname,
7599 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7600 ProcAddI = NULL;
7601# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007602 if ((success = (ProcAddI != NULL
7603# if defined(USE_DLOPEN)
7604 && dlerr == NULL
7605# endif
7606 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {
7608 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007609 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 else
7611 retval_str = (ProcAddI)(argint);
7612 }
7613 }
7614
Bram Moolenaar0f873732019-12-05 20:28:46 +01007615 // Save the string before we free the library.
7616 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 if (string_result == NULL)
7618 *number_result = retval_int;
7619 else if (retval_str != NULL
7620 && retval_str != (char_u *)1
7621 && retval_str != (char_u *)-1)
7622 *string_result = vim_strsave(retval_str);
7623 }
7624
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007625# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 mch_endjmp();
7627# ifdef SIGHASARG
7628 if (lc_signal != 0)
7629 {
7630 int i;
7631
Bram Moolenaar0f873732019-12-05 20:28:46 +01007632 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 for (i = 0; signal_info[i].sig != -1; i++)
7634 if (lc_signal == signal_info[i].sig)
7635 break;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00007636 semsg(e_got_sig_str_in_libcall, signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 }
7638# endif
7639# endif
7640
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007642 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007643 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007644 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007645
Bram Moolenaar0f873732019-12-05 20:28:46 +01007646 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 (void)dlclose(hinstLib);
7648# else
7649 (void)shl_unload(hinstLib);
7650# endif
7651 }
7652
7653 if (!success)
7654 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007655 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 return FAIL;
7657 }
7658
7659 return OK;
7660}
7661#endif
7662
7663#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007664static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665static int xterm_button;
7666
7667/*
7668 * Setup a dummy window for X selections in a terminal.
7669 */
7670 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007671setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
7673 int z = 0;
7674 char *strp = "";
7675 Widget AppShell;
7676
7677 if (!x_connect_to_server())
7678 return;
7679
7680 open_app_context();
7681 if (app_context != NULL && xterm_Shell == (Widget)0)
7682 {
7683 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007684# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007686# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007687# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007688 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689
7690 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007691 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692# endif
7693
Bram Moolenaar0f873732019-12-05 20:28:46 +01007694 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 oldhandler = XSetErrorHandler(x_error_check);
7696
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007697# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007698 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7700 mch_startjmp();
7701 if (SETJMP(lc_jump_env) != 0)
7702 {
7703 mch_didjmp();
7704 xterm_dpy = NULL;
7705 }
7706 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 {
7709 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7710 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007711 if (xterm_dpy != NULL)
7712 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007713# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007715# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 }
7717
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007718# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007719 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007721# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007722 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 (void)XSetErrorHandler(oldhandler);
7724
7725 if (xterm_dpy == NULL)
7726 {
7727 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007728 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 return;
7730 }
7731
Bram Moolenaar0f873732019-12-05 20:28:46 +01007732 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 (void)XSetIOErrorHandler(x_IOerror_handler);
7734
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007735# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007737 {
7738 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007739 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007740 verbose_leave();
7741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742# endif
7743
Bram Moolenaar0f873732019-12-05 20:28:46 +01007744 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7746 applicationShellWidgetClass, xterm_dpy,
7747 NULL);
7748 if (AppShell == (Widget)0)
7749 return;
7750 xterm_Shell = XtVaCreatePopupShell("VIM",
7751 topLevelShellWidgetClass, AppShell,
7752 XtNmappedWhenManaged, 0,
7753 XtNwidth, 1,
7754 XtNheight, 1,
7755 NULL);
7756 if (xterm_Shell == (Widget)0)
7757 return;
7758
7759 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007760 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 if (x11_display == NULL)
7762 x11_display = xterm_dpy;
7763
7764 XtRealizeWidget(xterm_Shell);
7765 XSync(xterm_dpy, False);
7766 xterm_update();
7767 }
7768 if (xterm_Shell != (Widget)0)
7769 {
7770 clip_init(TRUE);
7771 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7772 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007773 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 if (test_x11_window(xterm_dpy) == FAIL)
7775 x11_window = 0;
7776 if (x11_window != 0)
7777 xterm_trace = 0;
7778 }
7779}
7780
7781 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007782start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
7784 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7785 return;
7786 xterm_trace = 1;
7787 xterm_button = button;
7788 do_xterm_trace();
7789}
7790
7791
7792 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007793stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
7795 if (xterm_trace < 0)
7796 return;
7797 xterm_trace = 0;
7798}
7799
7800/*
7801 * Query the xterm pointer and generate mouse termcodes if necessary
7802 * return TRUE if dragging is active, else FALSE
7803 */
7804 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007805do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
7807 Window root, child;
7808 int root_x, root_y;
7809 int win_x, win_y;
7810 int row, col;
7811 int_u mask_return;
7812 char_u buf[50];
7813 char_u *strp;
7814 long got_hints;
7815 static char_u *mouse_code;
7816 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7817 static int prev_row = 0, prev_col = 0;
7818 static XSizeHints xterm_hints;
7819
7820 if (xterm_trace <= 0)
7821 return FALSE;
7822
7823 if (xterm_trace == 1)
7824 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007825 // Get the hints just before tracking starts. The font size might
7826 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007827 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7828 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 || xterm_hints.width_inc <= 1
7830 || xterm_hints.height_inc <= 1)
7831 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007832 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 return FALSE;
7834 }
7835
Bram Moolenaar0f873732019-12-05 20:28:46 +01007836 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 mouse_code = find_termcode(mouse_name);
7838 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007839 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 xterm_trace = 2;
7841
Bram Moolenaar0f873732019-12-05 20:28:46 +01007842 // Find the offset of the chars, there might be a scrollbar on the
7843 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7845 &win_x, &win_y, &mask_return);
7846 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7847 - (xterm_hints.height_inc / 2);
7848 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7849 xterm_hints.y = 2;
7850 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7851 - (xterm_hints.width_inc / 2);
7852 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7853 xterm_hints.x = 2;
7854 return TRUE;
7855 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007856 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {
7858 xterm_trace = 0;
7859 return FALSE;
7860 }
7861
7862 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7863 &win_x, &win_y, &mask_return);
7864
7865 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7866 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7867 if (row == prev_row && col == prev_col)
7868 return TRUE;
7869
7870 STRCPY(buf, mouse_code);
7871 strp = buf + STRLEN(buf);
7872 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7873 *strp++ = (char_u)(col + ' ' + 1);
7874 *strp++ = (char_u)(row + ' ' + 1);
7875 *strp = 0;
7876 add_to_input_buf(buf, STRLEN(buf));
7877
7878 prev_row = row;
7879 prev_col = col;
7880 return TRUE;
7881}
7882
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007883# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884/*
7885 * Destroy the display, window and app_context. Required for GTK.
7886 */
7887 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007888clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889{
7890 if (xterm_Shell != (Widget)0)
7891 {
7892 XtDestroyWidget(xterm_Shell);
7893 xterm_Shell = (Widget)0;
7894 }
7895 if (xterm_dpy != NULL)
7896 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007897# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007898 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007900# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 if (x11_display == xterm_dpy)
7902 x11_display = NULL;
7903 xterm_dpy = NULL;
7904 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007905# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 if (app_context != (XtAppContext)NULL)
7907 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007908 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 XtDestroyApplicationContext(app_context);
7910 app_context = (XtAppContext)NULL;
7911 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913}
7914# endif
7915
7916/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007917 * Catch up with GUI or X events.
7918 */
7919 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007920clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007921{
7922# ifdef FEAT_GUI
7923 if (gui.in_use)
7924 gui_mch_update();
7925 else
7926# endif
7927 if (xterm_Shell != (Widget)0)
7928 xterm_update();
7929}
7930
7931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 * Catch up with any queued X events. This may put keyboard input into the
7933 * input buffer, call resize call-backs, trigger timers etc. If there is
7934 * nothing in the X event queue (& no timers pending), then we return
7935 * immediately.
7936 */
7937 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007938xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939{
7940 XEvent event;
7941
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007942 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007944 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007946 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007947 break;
7948
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007949 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007950 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007951 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007952 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007953#ifdef FEAT_CLIENTSERVER
7954 {
7955 XPropertyEvent *e = (XPropertyEvent *)&event;
7956
7957 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007959 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007962 XtDispatchEvent(&event);
7963 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007964 else
7965 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007966 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007967 XtAppProcessEvent(app_context, mask);
7968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 }
7970}
7971
7972 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007973clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974{
7975 if (xterm_Shell != (Widget)0)
7976 return clip_x11_own_selection(xterm_Shell, cbd);
7977 return FAIL;
7978}
7979
7980 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007981clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982{
7983 if (xterm_Shell != (Widget)0)
7984 clip_x11_lose_selection(xterm_Shell, cbd);
7985}
7986
7987 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007988clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
7990 if (xterm_Shell != (Widget)0)
7991 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7992}
7993
7994 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007995clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996{
7997 clip_x11_set_selection(cbd);
7998}
7999#endif
8000
8001
8002#if defined(USE_XSMP) || defined(PROTO)
8003/*
8004 * Code for X Session Management Protocol.
8005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006
8007# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008/*
8009 * This is our chance to ask the user if they want to save,
8010 * or abort the logout
8011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008013xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014{
Bram Moolenaare1004402020-10-24 20:49:43 +02008015 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 int cancel_shutdown = False;
8017
Bram Moolenaare1004402020-10-24 20:49:43 +02008018 save_cmod_flags = cmdmod.cmod_flags;
8019 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01008020 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01008021 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02008023 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01008024 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 out_flush();
8026
Bram Moolenaar0f873732019-12-05 20:28:46 +01008027 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 SmcInteractDone(smc_conn, cancel_shutdown);
8029
Bram Moolenaar0f873732019-12-05 20:28:46 +01008030 // Finish off
8031 // Only end save-yourself here if we're not cancelling shutdown;
8032 // we'll get a cancelled callback later in which we'll end it.
8033 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 if (!cancel_shutdown)
8035 {
8036 xsmp.save_yourself = False;
8037 SmcSaveYourselfDone(smc_conn, True);
8038 }
8039}
8040# endif
8041
8042/*
8043 * Callback that starts save-yourself.
8044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008046xsmp_handle_save_yourself(
8047 SmcConn smc_conn,
8048 SmPointer client_data UNUSED,
8049 int save_type UNUSED,
8050 Bool shutdown,
8051 int interact_style UNUSED,
8052 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008054 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 if (xsmp.save_yourself)
8056 SmcSaveYourselfDone(smc_conn, True);
8057 xsmp.save_yourself = True;
8058 xsmp.shutdown = shutdown;
8059
Bram Moolenaar0f873732019-12-05 20:28:46 +01008060 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01008062 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
8064 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008065 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066
8067# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008068 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008070 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 SmcInteractRequest(smc_conn, SmDialogError,
8072 xsmp_handle_interaction, client_data);
8073 else
8074# endif
8075 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008076 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 SmcSaveYourselfDone(smc_conn, True);
8078 xsmp.save_yourself = False;
8079 }
8080}
8081
8082
8083/*
8084 * Callback to warn us of imminent death.
8085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008087xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
8089 xsmp_close();
8090
Bram Moolenaar0f873732019-12-05 20:28:46 +01008091 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 getout_preserve_modified(0);
8093}
8094
8095
8096/*
8097 * Callback to tell us that save-yourself has completed.
8098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008100xsmp_save_complete(
8101 SmcConn smc_conn UNUSED,
8102 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103{
8104 xsmp.save_yourself = False;
8105}
8106
8107
8108/*
8109 * Callback to tell us that an instigated shutdown was cancelled
8110 * (maybe even by us)
8111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008113xsmp_shutdown_cancelled(
8114 SmcConn smc_conn,
8115 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116{
8117 if (xsmp.save_yourself)
8118 SmcSaveYourselfDone(smc_conn, True);
8119 xsmp.save_yourself = False;
8120 xsmp.shutdown = False;
8121}
8122
8123
8124/*
8125 * Callback to tell us that a new ICE connection has been established.
8126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008128xsmp_ice_connection(
8129 IceConn iceConn,
8130 IcePointer clientData UNUSED,
8131 Bool opening,
8132 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008134 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 if (opening)
8136 {
8137 xsmp_icefd = IceConnectionNumber(iceConn);
8138 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8139 }
8140}
8141
8142
Bram Moolenaar0f873732019-12-05 20:28:46 +01008143// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008145xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146{
8147 Bool rep;
8148
8149 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8150 == IceProcessMessagesIOError)
8151 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008152 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008154 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 xsmp_close();
8156 return FAIL;
8157 }
8158 else
8159 return OK;
8160}
8161
8162static int dummy;
8163
Bram Moolenaar0f873732019-12-05 20:28:46 +01008164// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 void
8166xsmp_init(void)
8167{
8168 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 SmcCallbacks smcallbacks;
8170#if 0
8171 SmPropValue smname;
8172 SmProp smnameprop;
8173 SmProp *smprops[1];
8174#endif
8175
8176 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008177 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178
8179 xsmp.save_yourself = xsmp.shutdown = False;
8180
Bram Moolenaar0f873732019-12-05 20:28:46 +01008181 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8183 smcallbacks.save_yourself.client_data = NULL;
8184 smcallbacks.die.callback = xsmp_die;
8185 smcallbacks.die.client_data = NULL;
8186 smcallbacks.save_complete.callback = xsmp_save_complete;
8187 smcallbacks.save_complete.client_data = NULL;
8188 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8189 smcallbacks.shutdown_cancelled.client_data = NULL;
8190
Bram Moolenaar0f873732019-12-05 20:28:46 +01008191 // Set up a watch on ICE connection creations. The "dummy" argument is
8192 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8194 {
8195 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008196 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 return;
8198 }
8199
Bram Moolenaar0f873732019-12-05 20:28:46 +01008200 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 xsmp.smcconn = SmcOpenConnection(
8202 NULL,
8203 NULL,
8204 SmProtoMajor,
8205 SmProtoMinor,
8206 SmcSaveYourselfProcMask | SmcDieProcMask
8207 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8208 &smcallbacks,
8209 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008210 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008211 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 errorstring);
8213 if (xsmp.smcconn == NULL)
8214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008216 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008217 char errorreport[132];
8218
8219 // If the message is too long it might not be NUL terminated. Add
8220 // a NUL at the end to make sure we don't go over the end.
8221 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008222 vim_snprintf(errorreport, sizeof(errorreport),
8223 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008224 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 return;
8227 }
8228 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8229
8230#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008231 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 smname.value = "vim";
8233 smname.length = 3;
8234 smnameprop.name = "SmProgram";
8235 smnameprop.type = "SmARRAY8";
8236 smnameprop.num_vals = 1;
8237 smnameprop.vals = &smname;
8238
8239 smprops[0] = &smnameprop;
8240 SmcSetProperties(xsmp.smcconn, 1, smprops);
8241#endif
8242}
8243
8244
Bram Moolenaar0f873732019-12-05 20:28:46 +01008245// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008247xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248{
8249 if (xsmp_icefd != -1)
8250 {
8251 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008252 if (xsmp.clientid != NULL)
8253 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008254 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 xsmp_icefd = -1;
8256 }
8257}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008258#endif // USE_XSMP
Paul Ollis65745772022-06-05 16:55:54 +01008259
8260#if defined(FEAT_RELTIME) || defined(PROTO)
8261# if defined(HAVE_TIMER_CREATE) || defined(MACOS_X)
8262/*
8263 * Implement timeout with timer_create() and timer_settime().
8264 */
8265static int timeout_flag = FALSE;
8266static timer_t timer_id;
8267static int timer_created = FALSE;
8268
8269/*
8270 * Callback for when the timer expires.
8271 */
8272 static void
8273set_flag(union sigval _unused UNUSED)
8274{
8275 timeout_flag = TRUE;
8276}
8277
8278/*
8279 * Stop any active timeout.
8280 */
8281 void
8282stop_timeout(void)
8283{
8284 static struct itimerspec disarm = {{0, 0}, {0, 0}};
8285
8286 if (timer_created)
8287 {
8288 int ret = timer_settime(timer_id, 0, &disarm, NULL);
8289
8290 if (ret < 0)
8291 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8292 }
8293
8294 // Clear the current timeout flag; any previous timeout should be
8295 // considered _not_ triggered.
8296 timeout_flag = FALSE;
8297}
8298
8299/*
8300 * Start the timeout timer.
8301 *
8302 * The return value is a pointer to a flag that is initialised to FALSE. If the
8303 * timeout expires, the flag is set to TRUE. This will only return pointers to
8304 * static memory; i.e. any pointer returned by this function may always be
8305 * safely dereferenced.
8306 *
8307 * This function is not expected to fail, but if it does it will still return a
8308 * valid flag pointer; the flag will remain stuck as FALSE .
8309 */
8310 const int *
8311start_timeout(long msec)
8312{
8313 struct itimerspec interval = {
8314 {0, 0}, // Do not repeat.
8315 {msec / 1000, (msec % 1000) * 1000000}}; // Timeout interval
8316 int ret;
8317
8318 // This is really the caller's responsibility, but let's make sure the
8319 // previous timer has been stopped.
8320 stop_timeout();
8321 timeout_flag = FALSE;
8322
8323 if (!timer_created)
8324 {
8325 struct sigevent action = {0};
8326
8327 action.sigev_notify = SIGEV_THREAD;
8328 action.sigev_notify_function = set_flag;
8329 ret = timer_create(CLOCK_MONOTONIC, &action, &timer_id);
8330 if (ret < 0)
8331 {
8332 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8333 return &timeout_flag;
8334 }
8335 timer_created = TRUE;
8336 }
8337
8338 ret = timer_settime(timer_id, 0, &interval, NULL);
8339 if (ret < 0)
8340 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8341
8342 return &timeout_flag;
8343}
8344
8345# else
8346
8347/*
8348 * Implement timeout with setitimer()
8349 */
8350static struct itimerval prev_interval;
8351static struct sigaction prev_sigaction;
8352static int timeout_flag = FALSE;
8353static int timer_active = FALSE;
8354static int timer_handler_active = FALSE;
8355static int alarm_pending = FALSE;
8356
8357/*
8358 * Handle SIGALRM for a timeout.
8359 */
8360 static RETSIGTYPE
8361set_flag SIGDEFARG(sigarg)
8362{
8363 if (alarm_pending)
8364 alarm_pending = FALSE;
8365 else
8366 timeout_flag = TRUE;
8367}
8368
8369/*
8370 * Stop any active timeout.
8371 */
8372 void
8373stop_timeout(void)
8374{
8375 static struct itimerval disarm = {{0, 0}, {0, 0}};
8376 int ret;
8377
8378 if (timer_active)
8379 {
8380 timer_active = FALSE;
8381 ret = setitimer(ITIMER_REAL, &disarm, &prev_interval);
8382 if (ret < 0)
8383 // Should only get here as a result of coding errors.
8384 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8385 }
8386
8387 if (timer_handler_active)
8388 {
8389 timer_handler_active = FALSE;
8390 ret = sigaction(SIGALRM, &prev_sigaction, NULL);
8391 if (ret < 0)
8392 // Should only get here as a result of coding errors.
8393 semsg(_(e_could_not_reset_handler_for_timeout_str),
8394 strerror(errno));
8395 }
8396 timeout_flag = 0;
8397}
8398
8399/*
8400 * Start the timeout timer.
8401 *
8402 * The return value is a pointer to a flag that is initialised to FALSE. If the
8403 * timeout expires, the flag is set to TRUE. This will only return pointers to
8404 * static memory; i.e. any pointer returned by this function may always be
8405 * safely dereferenced.
8406 *
8407 * This function is not expected to fail, but if it does it will still return a
8408 * valid flag pointer; the flag will remain stuck as FALSE .
8409 */
8410 const int *
8411start_timeout(long msec)
8412{
8413 struct itimerval interval = {
8414 {0, 0}, // Do not repeat.
8415 {msec / 1000, (msec % 1000) * 1000}}; // Timeout interval
8416 struct sigaction handle_alarm;
8417 int ret;
8418 sigset_t sigs;
8419 sigset_t saved_sigs;
8420
8421 // This is really the caller's responsibility, but let's make sure the
8422 // previous timer has been stopped.
8423 stop_timeout();
8424
8425 // There is a small chance that SIGALRM is pending and so the handler must
8426 // ignore it on the first call.
8427 alarm_pending = FALSE;
8428 ret = sigemptyset(&sigs);
8429 ret = ret == 0 ? sigaddset(&sigs, SIGALRM) : ret;
8430 ret = ret == 0 ? sigprocmask(SIG_BLOCK, &sigs, &saved_sigs) : ret;
8431 timeout_flag = FALSE;
8432 ret = ret == 0 ? sigpending(&sigs) : ret;
8433 if (ret == 0)
8434 {
8435 alarm_pending = sigismember(&sigs, SIGALRM);
8436 ret = ret == 0 ? sigprocmask(SIG_SETMASK, &saved_sigs, NULL) : ret;
8437 }
8438 if (unlikely(ret != 0 || alarm_pending < 0))
8439 {
8440 // Just catching coding errors. Write an error message, but carry on.
8441 semsg(_(e_could_not_check_for_pending_sigalrm_str), strerror(errno));
8442 alarm_pending = FALSE;
8443 }
8444
8445 // Set up the alarm handler first.
8446 ret = sigemptyset(&handle_alarm.sa_mask);
8447 handle_alarm.sa_handler = set_flag;
8448 handle_alarm.sa_flags = 0;
8449 ret = ret == 0 ? sigaction(SIGALRM, &handle_alarm, &prev_sigaction) : ret;
8450 if (ret < 0)
8451 {
8452 // Should only get here as a result of coding errors.
8453 semsg(_(e_could_not_set_handler_for_timeout_str), strerror(errno));
8454 return &timeout_flag;
8455 }
8456 timer_handler_active = TRUE;
8457
8458 // Set up the interval timer once the alarm handler is in place.
8459 ret = setitimer(ITIMER_REAL, &interval, &prev_interval);
8460 if (ret < 0)
8461 {
8462 // Should only get here as a result of coding errors.
8463 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8464 stop_timeout();
8465 return &timeout_flag;
8466 }
8467
8468 timer_active = TRUE;
8469 return &timeout_flag;
8470}
8471# endif // HAVE_TIMER_CREATE
8472#endif // FEAT_RELTIME