blob: cbd8ba8bda6b3179df5ded8da2bcd571cea82c77 [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
58# include <gpm.h>
Bram Moolenaar0f873732019-12-05 20:28:46 +010059// <linux/keyboard.h> contains defines conflicting with "keymap.h",
60// I just copied relevant defines here. A cleaner solution would be to put gpm
61// code into separate file and include there linux/keyboard.h
62// #include <linux/keyboard.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000063# define KG_SHIFT 0
64# define KG_CTRL 2
65# define KG_ALT 3
66# define KG_ALTGR 1
67# define KG_SHIFTL 4
68# define KG_SHIFTR 5
69# define KG_CTRLL 6
70# define KG_CTRLR 7
71# define KG_CAPSSHIFT 8
72
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010073static void gpm_close(void);
74static int gpm_open(void);
75static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000076#endif
77
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000078#ifdef FEAT_SYSMOUSE
79# include <sys/consio.h>
80# include <sys/fbio.h>
81
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010082static int sysmouse_open(void);
83static void sysmouse_close(void);
Bram Moolenaard99df422016-01-29 23:20:40 +010084static RETSIGTYPE sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000085#endif
86
Bram Moolenaar071d4272004-06-13 20:20:40 +000087/*
88 * end of autoconf section. To be extended...
89 */
90
Bram Moolenaar0f873732019-12-05 20:28:46 +010091// Are the following #ifdefs still required? And why? Is that for X11?
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
93#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
94# ifdef SIGWINCH
95# undef SIGWINCH
96# endif
97# ifdef TIOCGWINSZ
98# undef TIOCGWINSZ
99# endif
100#endif
101
Bram Moolenaar0f873732019-12-05 20:28:46 +0100102#if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103# define SIGWINCH SIGWINDOW
104#endif
105
106#ifdef FEAT_X11
107# include <X11/Xlib.h>
108# include <X11/Xutil.h>
109# include <X11/Xatom.h>
110# ifdef FEAT_XCLIPBOARD
111# include <X11/Intrinsic.h>
112# include <X11/Shell.h>
113# include <X11/StringDefs.h>
114static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static void clip_update(void);
116static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117# endif
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119Window x11_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#endif
122
Bram Moolenaar5c3128e2020-05-11 20:54:42 +0200123static int ignore_sigtstp = FALSE;
124
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100125static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
127static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200128static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +0200129static int unix_did_set_title = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130static char_u *oldicon = NULL;
131static int did_set_icon = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Bram Moolenaar205b8862011-09-07 15:04:31 +0200135#ifdef HAVE_UNION_WAIT
136typedef union wait waitstatus;
137#else
138typedef int waitstatus;
139#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200140static int WaitForChar(long msec, int *interrupted, int ignore_input);
141static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar041c7102020-05-30 18:14:57 +0200142#ifdef VMS
Bram Moolenaarcda77642016-06-04 13:32:35 +0200143int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200145static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#endif
147
148#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static int do_xterm_trace(void);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100150# define XT_TRACE_DELAY 50 // delay for xterm tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#endif
152
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
155#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100156static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000158#if defined(SIGTSTP)
159static RETSIGTYPE sig_tstp SIGPROTOARG;
160// volatile because it is used in signal handler sig_tstp() and sigcont_handler().
161static volatile sig_atomic_t in_mch_suspend = FALSE;
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100164static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200166#if defined(SIGUSR1)
167static RETSIGTYPE catch_sigusr1 SIGPROTOARG;
168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100170static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000172#if defined(SIGALRM) && defined(FEAT_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100174static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100175// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200176static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100178static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100180static void catch_int_signal(void);
181static void set_signals(void);
182static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200183#ifdef HAVE_SIGPROCMASK
184# define SIGSET_DECL(set) sigset_t set;
185# define BLOCK_SIGNALS(set) block_signals(set)
186# define UNBLOCK_SIGNALS(set) unblock_signals(set)
187#else
188# define SIGSET_DECL(set)
189# define BLOCK_SIGNALS(set) do { /**/ } while (0)
190# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
191#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100192static int have_wildcard(int, char_u **);
193static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100195static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
197#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000198# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199#endif
200
Bram Moolenaar0f873732019-12-05 20:28:46 +0100201// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200202static volatile sig_atomic_t do_resize = FALSE;
dbivolaruab16ad32021-12-29 19:41:47 +0000203// volatile because it is used in signal handler sig_tstp().
204static volatile sig_atomic_t got_tstp = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205static char_u *extra_shell_arg = NULL;
206static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100207// volatile because it is used in signal handler deathtrap().
208static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
209// volatile because it is used in signal handler deathtrap().
210static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100212#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
213static int dont_check_job_ended = 0;
214#endif
215
Bram Moolenaar26e86442020-05-17 14:06:16 +0200216// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
217static tmode_T mch_cur_tmode = TMODE_COOK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
219#ifdef USE_XSMP
220typedef struct
221{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100222 SmcConn smcconn; // The SM connection ID
223 IceConn iceconn; // The ICE connection ID
224 char *clientid; // The client ID for the current smc session
225 Bool save_yourself; // If we're in the middle of a save_yourself
226 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227} xsmp_config_T;
228
229static xsmp_config_T xsmp;
230#endif
231
232#ifdef SYS_SIGLIST_DECLARED
233/*
234 * I have seen
235 * extern char *_sys_siglist[NSIG];
236 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
237 * that describe the signals. That is nearly what we want here. But
238 * autoconf does only check for sys_siglist (without the underscore), I
239 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100240 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 */
242#endif
243
244static struct signalinfo
245{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100246 int sig; // Signal number, eg. SIGSEGV etc
247 char *name; // Signal name (not char_u!).
248 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249} signal_info[] =
250{
251#ifdef SIGHUP
252 {SIGHUP, "HUP", TRUE},
253#endif
254#ifdef SIGQUIT
255 {SIGQUIT, "QUIT", TRUE},
256#endif
257#ifdef SIGILL
258 {SIGILL, "ILL", TRUE},
259#endif
260#ifdef SIGTRAP
261 {SIGTRAP, "TRAP", TRUE},
262#endif
263#ifdef SIGABRT
264 {SIGABRT, "ABRT", TRUE},
265#endif
266#ifdef SIGEMT
267 {SIGEMT, "EMT", TRUE},
268#endif
269#ifdef SIGFPE
270 {SIGFPE, "FPE", TRUE},
271#endif
272#ifdef SIGBUS
273 {SIGBUS, "BUS", TRUE},
274#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100275#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100276 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {SIGSEGV, "SEGV", TRUE},
278#endif
279#ifdef SIGSYS
280 {SIGSYS, "SYS", TRUE},
281#endif
282#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100283 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284#endif
285#ifdef SIGTERM
286 {SIGTERM, "TERM", TRUE},
287#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100288#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 {SIGVTALRM, "VTALRM", TRUE},
290#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000291#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100292 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
293 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 {SIGPROF, "PROF", TRUE},
295#endif
296#ifdef SIGXCPU
297 {SIGXCPU, "XCPU", TRUE},
298#endif
299#ifdef SIGXFSZ
300 {SIGXFSZ, "XFSZ", TRUE},
301#endif
302#ifdef SIGUSR1
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200303 {SIGUSR1, "USR1", FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000305#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100306 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {SIGUSR2, "USR2", TRUE},
308#endif
309#ifdef SIGINT
310 {SIGINT, "INT", FALSE},
311#endif
312#ifdef SIGWINCH
313 {SIGWINCH, "WINCH", FALSE},
314#endif
315#ifdef SIGTSTP
316 {SIGTSTP, "TSTP", FALSE},
317#endif
318#ifdef SIGPIPE
319 {SIGPIPE, "PIPE", FALSE},
320#endif
321 {-1, "Unknown!", FALSE}
322};
323
Bram Moolenaar25724922009-07-14 15:38:41 +0000324 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100325mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000326{
327 if (p_verbose >= 5)
328 {
329 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100330 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000331 verbose_leave();
332 }
333# ifdef VMS
334 return chdir(vms_fixfilename(path));
335# else
336 return chdir(path);
337# endif
338}
339
Bram Moolenaar0f873732019-12-05 20:28:46 +0100340// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100341#if defined(ECHOE) && defined(ICANON) \
342 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
343 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200344# define NEW_TTY_SYSTEM
345#endif
346
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000347/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100348 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100351mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200353 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100354 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100355 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356}
357
358/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100359 * Function passed to inchar_loop() to handle window resizing.
360 * If "check_only" is TRUE: Return whether there was a resize.
361 * If "check_only" is FALSE: Deal with the window resized.
362 */
363 static int
364resize_func(int check_only)
365{
366 if (check_only)
367 return do_resize;
368 while (do_resize)
369 handle_resize();
370 return FALSE;
371}
372
373/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000374 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 * Get a characters from the keyboard.
376 * Return the number of characters that are available.
377 * If wtime == 0 do not wait for characters.
378 * If wtime == n wait a short time for characters.
379 * If wtime == -1 wait forever for characters.
380 */
381 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100382mch_inchar(
383 char_u *buf,
384 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100385 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100386 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100388 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
389 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390}
391
392 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100393handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394{
395 do_resize = FALSE;
396 shell_resized();
397}
398
399/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200400 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 */
402 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100403mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200405 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406}
407
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200408#if defined(FEAT_TERMINAL) || defined(PROTO)
409/*
410 * Check for any pending input or messages.
411 */
412 int
413mch_check_messages(void)
414{
415 return WaitForChar(0L, NULL, TRUE);
416}
417#endif
418
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
420# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000421# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422# endif
423# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
424# include <sys/sysctl.h>
425# endif
426# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
427# include <sys/sysinfo.h>
428# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100429# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100430# include <mach/mach_host.h>
431# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433
434/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000435 * Return total amount of memory available in Kbyte.
436 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100439mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100442 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
Bram Moolenaar362dc332018-03-05 21:59:37 +0100444# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100445 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100446 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100447 mach_port_t host = mach_host_self();
448 kern_return_t kret;
449# ifdef HOST_VM_INFO64
450 struct vm_statistics64 vm_stat;
451 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
Bram Moolenaar988615f2018-02-27 17:58:20 +0100453 kret = host_statistics64(host, HOST_VM_INFO64,
454 (host_info64_t)&vm_stat, &count);
455# else
456 struct vm_statistics vm_stat;
457 natural_t count = HOST_VM_INFO_COUNT;
458
459 kret = host_statistics(host, HOST_VM_INFO,
460 (host_info_t)&vm_stat, &count);
461# endif
462 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100463 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100464 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
465 + vm_stat.inactive_count
466# ifdef MAC_OS_X_VERSION_10_9
467 + vm_stat.compressor_page_count
468# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100469 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100470 mach_port_deallocate(mach_task_self(), host);
471 }
472# endif
473
474# ifdef HAVE_SYSCTL
475 if (mem == 0)
476 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100477 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100478 int mib[2];
479 size_t len = sizeof(long_u);
480# ifdef HW_USERMEM64
481 long_u physmem;
482# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100483 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100484 union {
485 int_u u32;
486 long_u u64;
487 } physmem;
488# endif
489
490 mib[0] = CTL_HW;
491# ifdef HW_USERMEM64
492 mib[1] = HW_USERMEM64;
493# else
494 mib[1] = HW_USERMEM;
495# endif
496 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
497 {
498# ifdef HW_USERMEM64
499 mem = (long_u)physmem;
500# else
501 if (len == sizeof(physmem.u64))
502 mem = (long_u)physmem.u64;
503 else
504 mem = (long_u)physmem.u32;
505# endif
506 }
507 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200510# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (mem == 0)
512 {
513 struct sysinfo sinfo;
514
Bram Moolenaar0f873732019-12-05 20:28:46 +0100515 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000517 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200518# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100519 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000520 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
521 {
522 sinfo.mem_unit = sinfo.mem_unit >> 1;
523 --shiftright;
524 }
525 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200526# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200528# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200531# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200533# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (mem == 0)
535 {
536 long pagesize, pagecount;
537
Bram Moolenaar0f873732019-12-05 20:28:46 +0100538 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 pagesize = sysconf(_SC_PAGESIZE);
540 pagecount = sysconf(_SC_PHYS_PAGES);
541 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000542 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100543 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000544 while (shiftright > 0 && (pagesize & 1) == 0)
545 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000546 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000547 --shiftright;
548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200552# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
Bram Moolenaar0f873732019-12-05 20:28:46 +0100554 // Return the minimum of the physical memory and the user limit, because
555 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200556# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 {
558 struct rlimit rlp;
559
560 if (getrlimit(RLIMIT_DATA, &rlp) == 0
561 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200562# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200564# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000565 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000567 {
568 mem = (long_u)rlp.rlim_cur;
569 shiftright = 10;
570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000575 return mem >> shiftright;
576 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577}
578#endif
579
Bram Moolenaar0981c872020-08-23 14:28:37 +0200580/*
581 * "flags": MCH_DELAY_IGNOREINPUT - don't read input
582 * MCH_DELAY_SETTMODE - use settmode() even for short delays
583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200585mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200587 tmode_T old_tmode;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200588 int call_settmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000589#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100590 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
Bram Moolenaar0981c872020-08-23 14:28:37 +0200593 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100595 // Go to cooked mode without echo, to allow SIGINT interrupting us
596 // here. But we don't want QUIT to kill us (CTRL-\ used in a
597 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200598 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000599 in_mch_delay = TRUE;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200600 call_settmode = mch_cur_tmode == TMODE_RAW
601 && (msec > 500 || (flags & MCH_DELAY_SETTMODE));
602 if (call_settmode)
603 {
604 old_tmode = mch_cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 settmode(TMODE_SLEEP);
Bram Moolenaar80361a52020-10-05 21:39:25 +0200606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608 /*
609 * Everybody sleeps in a different way...
610 * Prefer nanosleep(), some versions of usleep() can only sleep up to
611 * one second.
612 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000613#ifdef FEAT_MZSCHEME
614 do
615 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100616 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000617 if (total > p_mzq)
618 msec = p_mzq;
619 else
620 msec = total;
621 total -= msec;
622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623#ifdef HAVE_NANOSLEEP
624 {
625 struct timespec ts;
626
627 ts.tv_sec = msec / 1000;
628 ts.tv_nsec = (msec % 1000) * 1000000;
629 (void)nanosleep(&ts, NULL);
630 }
631#else
632# ifdef HAVE_USLEEP
633 while (msec >= 1000)
634 {
635 usleep((unsigned int)(999 * 1000));
636 msec -= 999;
637 }
638 usleep((unsigned int)(msec * 1000));
639# else
640# ifndef HAVE_SELECT
641 poll(NULL, 0, (int)msec);
642# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {
644 struct timeval tv;
645
646 tv.tv_sec = msec / 1000;
647 tv.tv_usec = (msec % 1000) * 1000;
Bram Moolenaar0981c872020-08-23 14:28:37 +0200648 // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
649 // a patch from Sun to fix this. Reported by Gunnar Pedersen.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 select(0, NULL, NULL, NULL, &tv);
651 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100652# endif // HAVE_SELECT
653# endif // HAVE_NANOSLEEP
654#endif // HAVE_USLEEP
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000655#ifdef FEAT_MZSCHEME
656 }
657 while (total > 0);
658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659
Bram Moolenaar80361a52020-10-05 21:39:25 +0200660 if (call_settmode)
Bram Moolenaar26e86442020-05-17 14:06:16 +0200661 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000662 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200665 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666}
667
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000668#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
670# define HAVE_CHECK_STACK_GROWTH
671/*
672 * Support for checking for an almost-out-of-stack-space situation.
673 */
674
675/*
676 * Return a pointer to an item on the stack. Used to find out if the stack
677 * grows up or down.
678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679static int stack_grows_downwards;
680
681/*
682 * Find out if the stack grows upwards or downwards.
683 * "p" points to a variable on the stack of the caller.
684 */
685 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100686check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
688 int i;
689
690 stack_grows_downwards = (p > (char *)&i);
691}
692#endif
693
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000694#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695static char *stack_limit = NULL;
696
697#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
698# include <pthread.h>
699# include <pthread_np.h>
700#endif
701
702/*
703 * Find out until how var the stack can grow without getting into trouble.
704 * Called when starting up and when switching to the signal stack in
705 * deathtrap().
706 */
707 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100708get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709{
710 struct rlimit rlp;
711 int i;
712 long lim;
713
Bram Moolenaar0f873732019-12-05 20:28:46 +0100714 // Set the stack limit to 15/16 of the allowable size. Skip this when the
715 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 if (getrlimit(RLIMIT_STACK, &rlp) == 0
717 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
718# ifdef RLIM_INFINITY
719 && rlp.rlim_cur != RLIM_INFINITY
720# endif
721 )
722 {
723 lim = (long)rlp.rlim_cur;
724#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
725 {
726 pthread_attr_t attr;
727 size_t size;
728
Bram Moolenaar0f873732019-12-05 20:28:46 +0100729 // On FreeBSD the initial thread always has a fixed stack size, no
730 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 pthread_attr_init(&attr);
732 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
733 {
734 pthread_attr_getstacksize(&attr, &size);
735 if (lim > (long)size)
736 lim = (long)size;
737 }
738 pthread_attr_destroy(&attr);
739 }
740#endif
741 if (stack_grows_downwards)
742 {
743 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
744 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100745 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 stack_limit = (char *)((long)&i / 16L);
747 }
748 else
749 {
750 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
751 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100752 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 }
754 }
755}
756
757/*
758 * Return FAIL when running out of stack space.
759 * "p" must point to any variable local to the caller that's on the stack.
760 */
761 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100762mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
764 if (stack_limit != NULL)
765 {
766 if (stack_grows_downwards)
767 {
768 if (p < stack_limit)
769 return FAIL;
770 }
771 else if (p > stack_limit)
772 return FAIL;
773 }
774 return OK;
775}
776#endif
777
778#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
779/*
780 * Support for using the signal stack.
781 * This helps when we run out of stack space, which causes a SIGSEGV. The
782 * signal handler then must run on another stack, since the normal stack is
783 * completely full.
784 */
785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100787static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100789static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790# endif
791
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200792/*
793 * Get a size of signal stack.
794 * Preference (if available): sysconf > SIGSTKSZ > guessed size
795 */
796static long int get_signal_stack_size()
797{
798# ifdef HAVE_SYSCONF_SIGSTKSZ
799 long int size = -1;
800
801 // return size only if sysconf doesn't return an error
802 if ((size = sysconf(_SC_SIGSTKSZ)) > -1)
803 return size;
804# endif
805
806# ifdef SIGSTKSZ
807 // if sysconf() isn't available or gives error, return SIGSTKSZ
808 // if defined
809 return SIGSTKSZ;
810# endif
811
812 // otherwise guess the size
813 return 8000;
814}
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816static char *signal_stack;
817
818 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100819init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821 if (signal_stack != NULL)
822 {
823# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824# ifdef HAVE_SS_BASE
825 sigstk.ss_base = signal_stack;
826# else
827 sigstk.ss_sp = signal_stack;
828# endif
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200829 sigstk.ss_size = get_signal_stack_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 sigstk.ss_flags = 0;
831 (void)sigaltstack(&sigstk, NULL);
832# else
833 sigstk.ss_sp = signal_stack;
834 if (stack_grows_downwards)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200835 sigstk.ss_sp += get_signal_stack_size() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 sigstk.ss_onstack = 0;
837 (void)sigstack(&sigstk, NULL);
838# endif
839 }
840}
841#endif
842
843/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000844 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 * will barf when the second argument to signal() is ``wrong''.
846 * Let me try it with a few tricky defines from my own osdef.h (jw).
847 */
848#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 static RETSIGTYPE
850sig_winch SIGDEFARG(sigarg)
851{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100852 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
854 do_resize = TRUE;
855 SIGRETURN;
856}
857#endif
858
dbivolaruab16ad32021-12-29 19:41:47 +0000859#if defined(SIGTSTP)
860 static RETSIGTYPE
861sig_tstp SIGDEFARG(sigarg)
862{
863 // Second time we get called we actually need to suspend
864 if (in_mch_suspend)
865 {
866 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
867 raise(sigarg);
868 }
dbivolaru79a6e252022-01-23 16:41:14 +0000869 else
870 got_tstp = TRUE;
dbivolaruab16ad32021-12-29 19:41:47 +0000871
872 // this is not required on all systems, but it doesn't hurt anybody
873 signal(SIGTSTP, (RETSIGTYPE (*)())sig_tstp);
dbivolaruab16ad32021-12-29 19:41:47 +0000874 SIGRETURN;
875}
876#endif
877
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 static RETSIGTYPE
880catch_sigint SIGDEFARG(sigarg)
881{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100882 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
884 got_int = TRUE;
885 SIGRETURN;
886}
887#endif
888
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200889#if defined(SIGUSR1)
890 static RETSIGTYPE
891catch_sigusr1 SIGDEFARG(sigarg)
892{
893 // this is not required on all systems, but it doesn't hurt anybody
894 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
895 got_sigusr1 = TRUE;
896 SIGRETURN;
897}
898#endif
899
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 static RETSIGTYPE
902catch_sigpwr SIGDEFARG(sigarg)
903{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100904 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000905 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 /*
907 * I'm not sure we get the SIGPWR signal when the system is really going
908 * down or when the batteries are almost empty. Just preserve the swap
909 * files and don't exit, that can't do any harm.
910 */
911 ml_sync_all(FALSE, FALSE);
912 SIGRETURN;
913}
914#endif
915
916#ifdef SET_SIG_ALARM
917/*
918 * signal function for alarm().
919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 static RETSIGTYPE
921sig_alarm SIGDEFARG(sigarg)
922{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100923 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 sig_alarm_called = TRUE;
925 SIGRETURN;
926}
927#endif
928
Bram Moolenaaredce7422019-01-20 18:39:30 +0100929#if (defined(HAVE_SETJMP_H) \
930 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
931 || defined(FEAT_LIBCALL))) \
932 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100933# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100934
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100935// argument to SETJMP()
936static JMP_BUF lc_jump_env;
937
938# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100939// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100940// Volatile because it is used in signal handlers.
941static volatile sig_atomic_t lc_signal;
942# endif
943
944// TRUE when lc_jump_env is valid.
945// Volatile because it is used in signal handler deathtrap().
946static volatile sig_atomic_t lc_active INIT(= FALSE);
947
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948/*
949 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100950 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100952 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 * Usage:
954 * mch_startjmp();
955 * if (SETJMP(lc_jump_env) != 0)
956 * {
957 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100958 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 * }
960 * else
961 * {
962 * do_the_work;
963 * mch_endjmp();
964 * }
965 * Note: Can't move SETJMP() here, because a function calling setjmp() must
966 * not return before the saved environment is used.
967 * Returns OK for normal return, FAIL when the protected code caused a
968 * problem and LONGJMP() was used.
969 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100970 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100971mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100973# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100975# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 lc_active = TRUE;
977}
978
Bram Moolenaar113e1072019-01-20 15:30:40 +0100979 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100980mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
982 lc_active = FALSE;
983}
984
Bram Moolenaar113e1072019-01-20 15:30:40 +0100985 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100986mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987{
988# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100989 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
990 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 init_signal_stack();
992# endif
993}
994#endif
995
996/*
997 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200998 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001000 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1001 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 */
1003 static RETSIGTYPE
1004deathtrap SIGDEFARG(sigarg)
1005{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001006 static int entered = 0; // count the number of times we got here.
1007 // Note: when memory has been corrupted
1008 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009#ifdef SIGHASARG
1010 int i;
1011#endif
1012
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001013#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 /*
1015 * Catch a crash in protected code.
1016 * Restores the environment saved in lc_jump_env, which looks like
1017 * SETJMP() returns 1.
1018 */
1019 if (lc_active)
1020 {
1021# if defined(SIGHASARG)
1022 lc_signal = sigarg;
1023# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001024 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001026 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 }
1028#endif
1029
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001030#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001031# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001032 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1033 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1034 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001035 if (in_mch_delay && sigarg == SIGQUIT)
1036 SIGRETURN;
1037# endif
1038
Bram Moolenaar0f873732019-12-05 20:28:46 +01001039 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1040 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1041 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001042 if (entered == 0
1043 && (0
1044# ifdef SIGHUP
1045 || sigarg == SIGHUP
1046# endif
1047# ifdef SIGQUIT
1048 || sigarg == SIGQUIT
1049# endif
1050# ifdef SIGTERM
1051 || sigarg == SIGTERM
1052# endif
1053# ifdef SIGPWR
1054 || sigarg == SIGPWR
1055# endif
1056# ifdef SIGUSR1
1057 || sigarg == SIGUSR1
1058# endif
1059# ifdef SIGUSR2
1060 || sigarg == SIGUSR2
1061# endif
1062 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001063 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001064 SIGRETURN;
1065#endif
1066
Bram Moolenaar0f873732019-12-05 20:28:46 +01001067 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 ++entered;
1069
Bram Moolenaar0f873732019-12-05 20:28:46 +01001070 // Executing autocommands is likely to use more stack space than we have
1071 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001072 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001075 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 set_vim_var_nr(VV_DYING, (long)entered);
1077#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001078 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001080#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001081 // Since we are now using the signal stack, need to reset the stack
1082 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 get_stack_limit();
1084#endif
1085
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001086#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001087 // This is for opening gdb the moment Vim crashes.
1088 // You need to manually adjust the file name and Vim executable name.
1089 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001090 {
1091# define VI_GDB_FILE "/tmp/vimgdb"
1092# define VIM_NAME "/usr/bin/vim"
1093 FILE *fp = fopen(VI_GDB_FILE, "w");
1094 if (fp)
1095 {
1096 fprintf(fp,
1097 "file %s\n"
1098 "attach %d\n"
1099 "set height 1000\n"
1100 "bt full\n"
1101 , VIM_NAME, getpid());
1102 fclose(fp);
1103 system("xterm -e gdb -x "VI_GDB_FILE);
1104 unlink(VI_GDB_FILE);
1105 }
1106 }
1107#endif
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001110 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 for (i = 0; signal_info[i].sig != -1; i++)
1112 if (sigarg == signal_info[i].sig)
1113 break;
1114 deadly_signal = sigarg;
1115#endif
1116
Bram Moolenaar0f873732019-12-05 20:28:46 +01001117 full_screen = FALSE; // don't write message to the GUI, it might be
1118 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 /*
1120 * If something goes wrong after entering here, we may get here again.
1121 * When this happens, give a message and try to exit nicely (resetting the
1122 * terminal mode, etc.)
1123 * When this happens twice, just exit, don't even try to give a message,
1124 * stack may be corrupt or something weird.
1125 * When this still happens again (or memory was corrupted in such a way
1126 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1127 */
1128 if (entered >= 3)
1129 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001130 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 may_core_dump();
1132 if (entered >= 4)
1133 _exit(8);
1134 exit(7);
1135 }
1136 if (entered == 2)
1137 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001138 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001139 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 out_flush();
1141 getout(1);
1142 }
1143
Bram Moolenaar0f873732019-12-05 20:28:46 +01001144 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001146 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 signal_info[i].name);
1148#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001149 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001151
Bram Moolenaar0f873732019-12-05 20:28:46 +01001152 // Preserve files and exit. This sets the really_exiting flag to prevent
1153 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001154 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155
Bram Moolenaar0f873732019-12-05 20:28:46 +01001156 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001157
Bram Moolenaar009b2592004-10-24 19:18:58 +00001158#ifdef NBDEBUG
1159 reset_signals();
1160 may_core_dump();
1161 abort();
1162#endif
1163
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 SIGRETURN;
1165}
1166
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001168 * Invoked after receiving SIGCONT. We don't know what happened while
1169 * sleeping, deal with part of that.
1170 */
1171 static void
1172after_sigcont(void)
1173{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001174 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1175 // again later.
1176 oldtitle_outdated = TRUE;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001177
Bram Moolenaar2e310482018-08-21 13:09:10 +02001178 settmode(TMODE_RAW);
1179 need_check_timestamps = TRUE;
1180 did_check_timestamps = FALSE;
1181}
1182
1183#if defined(SIGCONT)
1184static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001185
1186/*
1187 * With multi-threading, suspending might not work immediately. Catch the
1188 * SIGCONT signal, which will be used as an indication whether the suspending
1189 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001190 *
1191 * On Linux, signal is not always handled immediately either.
1192 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001193 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001194 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001195 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001197static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001198static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199
1200/*
1201 * signal handler for SIGCONT
1202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 static RETSIGTYPE
1204sigcont_handler SIGDEFARG(sigarg)
1205{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001206 if (in_mch_suspend)
1207 {
1208 sigcont_received = TRUE;
1209 }
1210 else
1211 {
1212 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1213 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1214 // back to a sane mode. We should redraw, but we can't really do that
1215 // in a signal handler, do a redraw later.
1216 after_sigcont();
1217 redraw_later(CLEAR);
1218 cursor_on_force();
1219 out_flush();
1220 }
1221
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 SIGRETURN;
1223}
1224#endif
1225
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001226#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001227# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001228static void *clip_star_save = NULL;
1229static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001230# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001231
1232/*
1233 * Called when Vim is going to sleep or execute a shell command.
1234 * We can't respond to requests for the X selections. Lose them, otherwise
1235 * other applications will hang. But first copy the text to cut buffer 0.
1236 */
1237 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001238loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001239{
1240 if (clip_star.owned || clip_plus.owned)
1241 {
1242 x11_export_final_selection();
1243 if (clip_star.owned)
1244 clip_lose_selection(&clip_star);
1245 if (clip_plus.owned)
1246 clip_lose_selection(&clip_plus);
1247 if (x11_display != NULL)
1248 XFlush(x11_display);
1249 }
1250}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001251
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001252# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001253/*
1254 * Save clipboard text to restore later.
1255 */
1256 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001257save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001258{
1259 if (clip_star.owned)
1260 clip_star_save = get_register('*', TRUE);
1261 if (clip_plus.owned)
1262 clip_plus_save = get_register('+', TRUE);
1263}
1264
1265/*
1266 * Restore clipboard text if no one own the X selection.
1267 */
1268 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001269restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001270{
1271 if (clip_star_save != NULL)
1272 {
1273 if (!clip_gen_owner_exists(&clip_star))
1274 put_register('*', clip_star_save);
1275 else
1276 free_register(clip_star_save);
1277 clip_star_save = NULL;
1278 }
1279 if (clip_plus_save != NULL)
1280 {
1281 if (!clip_gen_owner_exists(&clip_plus))
1282 put_register('+', clip_plus_save);
1283 else
1284 free_register(clip_plus_save);
1285 clip_plus_save = NULL;
1286 }
1287}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001288# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001289#endif
1290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291/*
1292 * If the machine has job control, use it to suspend the program,
1293 * otherwise fake it by starting a new shell.
1294 */
1295 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001296mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001298 if (ignore_sigtstp)
1299 return;
1300
Bram Moolenaar041c7102020-05-30 18:14:57 +02001301#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001302 in_mch_suspend = TRUE;
1303
Bram Moolenaar0f873732019-12-05 20:28:46 +01001304 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001306 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307
1308# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001309 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001311# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 sigcont_received = FALSE;
1313# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001314
Bram Moolenaar0f873732019-12-05 20:28:46 +01001315 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001316
1317# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001318 /*
1319 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001320 * immediately, but somehow not all the time, probably because it's handled
1321 * in another thread. Do not call pause() because there would be race
1322 * condition which would hang Vim if signal happened in between the test of
1323 * sigcont_received and the call to pause(). If signal is not yet received,
1324 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1325 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001326 */
1327 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001328 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001329
Bram Moolenaar262735e2009-07-14 10:20:22 +00001330 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001331 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001334 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaar2e310482018-08-21 13:09:10 +02001336 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337#else
1338 suspend_shell();
1339#endif
1340}
1341
1342 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001343mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
1345 Columns = 80;
1346 Rows = 24;
1347
1348 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001349
1350#ifdef SIGTSTP
1351 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1352 // that indicates the shell (or program) that launched us does not support
1353 // tty job control and thus we should ignore that signal. If invoked as a
1354 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
1355 ignore_sigtstp = restricted || SIG_IGN == signal(SIGTSTP, SIG_ERR);
1356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001358
Bram Moolenaar56718732006-03-15 22:53:57 +00001359#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001360 mac_conv_init();
1361#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001362#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1363 win_clip_init();
1364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365}
1366
1367 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001368set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369{
1370#if defined(SIGWINCH)
1371 /*
1372 * WINDOW CHANGE signal is handled with sig_winch().
1373 */
1374 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1375#endif
1376
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001378 // See mch_init() for the conditions under which we ignore SIGTSTP.
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001379 // In the GUI default TSTP processing is OK.
1380 // Checking both gui.in_use and gui.starting because gui.in_use is not set
1381 // at this point (set after menus are displayed), but gui.starting is set.
1382 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN
1383# ifdef FEAT_GUI
1384 : gui.in_use || gui.starting ? SIG_DFL
1385# endif
1386 : (RETSIGTYPE (*)())sig_tstp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001388#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 signal(SIGCONT, sigcont_handler);
1390#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001391#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 /*
1393 * We want to ignore breaking of PIPEs.
1394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 signal(SIGPIPE, SIG_IGN);
1396#endif
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001399 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400#endif
1401
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001402#ifdef SIGUSR1
1403 /*
1404 * Call user's handler on SIGUSR1
1405 */
1406 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
1407#endif
1408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /*
1410 * Ignore alarm signals (Perl's alarm() generates it).
1411 */
1412#ifdef SIGALRM
1413 signal(SIGALRM, SIG_IGN);
1414#endif
1415
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001416#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 /*
1418 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1419 * work will be lost.
1420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1422#endif
1423
1424 /*
1425 * Arrange for other signals to gracefully shutdown Vim.
1426 */
1427 catch_signals(deathtrap, SIG_ERR);
1428
1429#if defined(FEAT_GUI) && defined(SIGHUP)
1430 /*
1431 * When the GUI is running, ignore the hangup signal.
1432 */
1433 if (gui.in_use)
1434 signal(SIGHUP, SIG_IGN);
1435#endif
1436}
1437
Bram Moolenaardf177f62005-02-22 08:39:57 +00001438#if defined(SIGINT) || defined(PROTO)
1439/*
1440 * Catch CTRL-C (only works while in Cooked mode).
1441 */
1442 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001443catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001444{
1445 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1446}
1447#endif
1448
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001450reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
1452 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001453#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001454 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 signal(SIGCONT, SIG_DFL);
1456#endif
1457}
1458
1459 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001460catch_signals(
1461 RETSIGTYPE (*func_deadly)(),
1462 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 int i;
1465
1466 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (signal_info[i].deadly)
1469 {
1470#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1471 struct sigaction sa;
1472
Bram Moolenaar0f873732019-12-05 20:28:46 +01001473 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 sa.sa_handler = func_deadly;
1475 sigemptyset(&sa.sa_mask);
1476# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001477 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1478 // thread handling in combination with using the alternate stack:
1479 // pthread library functions try to use the stack pointer to
1480 // identify the current thread, causing a SEGV signal, which
1481 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 sa.sa_flags = 0;
1483# else
1484 sa.sa_flags = SA_ONSTACK;
1485# endif
1486 sigaction(signal_info[i].sig, &sa, NULL);
1487#else
1488# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1489 struct sigvec sv;
1490
Bram Moolenaar0f873732019-12-05 20:28:46 +01001491 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 sv.sv_handler = func_deadly;
1493 sv.sv_mask = 0;
1494 sv.sv_flags = SV_ONSTACK;
1495 sigvec(signal_info[i].sig, &sv, NULL);
1496# else
1497 signal(signal_info[i].sig, func_deadly);
1498# endif
1499#endif
1500 }
1501 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001502 {
1503 // Deal with non-deadly signals.
1504#ifdef SIGTSTP
1505 signal(signal_info[i].sig,
1506 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1507 ? SIG_IGN : func_other);
1508#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001510#endif
1511 }
1512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513}
1514
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001515#ifdef HAVE_SIGPROCMASK
1516 static void
1517block_signals(sigset_t *set)
1518{
1519 sigset_t newset;
1520 int i;
1521
1522 sigemptyset(&newset);
1523
1524 for (i = 0; signal_info[i].sig != -1; i++)
1525 sigaddset(&newset, signal_info[i].sig);
1526
Bram Moolenaar2e310482018-08-21 13:09:10 +02001527# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001528 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001529 sigaddset(&newset, SIGCONT);
1530# endif
1531
1532 sigprocmask(SIG_BLOCK, &newset, set);
1533}
1534
1535 static void
1536unblock_signals(sigset_t *set)
1537{
1538 sigprocmask(SIG_SETMASK, set, NULL);
1539}
1540#endif
1541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001543 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001544 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1545 * return TRUE
1546 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1547 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001548 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001549 * Returns TRUE when Vim should exit.
1550 */
1551 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001552vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001553{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001554 static int got_signal = 0;
1555 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001556
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001557 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001558 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001559 case SIGNAL_BLOCK: blocked = TRUE;
1560 break;
1561
1562 case SIGNAL_UNBLOCK: blocked = FALSE;
1563 if (got_signal != 0)
1564 {
1565 kill(getpid(), got_signal);
1566 got_signal = 0;
1567 }
1568 break;
1569
1570 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001571 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001572 got_signal = sig;
1573#ifdef SIGPWR
1574 if (sig != SIGPWR)
1575#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001576 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001577 break;
1578 }
1579 return FALSE;
1580}
1581
1582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 * Check_win checks whether we have an interactive stdout.
1584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001586mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (isatty(1))
1589 return OK;
1590 return FAIL;
1591}
1592
1593/*
1594 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1595 */
1596 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001597mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
1599 if (isatty(read_cmd_fd))
1600 return TRUE;
1601 return FALSE;
1602}
1603
1604#ifdef FEAT_X11
1605
Bram Moolenaar651fca82021-11-29 20:39:38 +00001606# if defined(ELAPSED_TIMEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608/*
1609 * Give a message about the elapsed time for opening the X window.
1610 */
1611 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001612xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001614 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615}
1616# endif
1617#endif
1618
Bram Moolenaar651fca82021-11-29 20:39:38 +00001619#if defined(FEAT_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620/*
1621 * A few functions shared by X11 title and clipboard code.
1622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
1624static int got_x_error = FALSE;
1625
1626/*
1627 * X Error handler, otherwise X just exits! (very rude) -- webb
1628 */
1629 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001630x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001632 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 STRCAT(IObuff, _("\nVim: Got X error\n"));
1634
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001635 // In the GUI we cannot print a message and continue, because no X calls
1636 // are allowed here (causes my system to hang). Silently continuing seems
1637 // like the best alternative. Do preserve files, in case we crash.
1638 ml_sync_all(FALSE, FALSE);
1639
1640#ifdef FEAT_GUI
1641 if (!gui.in_use)
1642#endif
1643 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
Bram Moolenaar0f873732019-12-05 20:28:46 +01001645 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646}
1647
1648/*
1649 * Another X Error handler, just used to check for errors.
1650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001652x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
1654 got_x_error = TRUE;
1655 return 0;
1656}
1657
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001658/*
1659 * Return TRUE when connection to the X server is desired.
1660 */
1661 static int
1662x_connect_to_server(void)
1663{
1664 // No point in connecting if we are exiting or dying.
1665 if (exiting || v_dying)
1666 return FALSE;
1667
1668#if defined(FEAT_CLIENTSERVER)
1669 if (x_force_connect)
1670 return TRUE;
1671#endif
1672 if (x_no_connect)
1673 return FALSE;
1674
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001675 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001676 if (clip_exclude_prog != NULL)
1677 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001678 // Just in case we get called recursively, return FALSE. This could
1679 // happen if vpeekc() is used while executing the prog and it causes a
1680 // related callback to be invoked.
1681 if (regprog_in_use(clip_exclude_prog))
1682 return FALSE;
1683
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001684 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1685 return FALSE;
1686 }
1687 return TRUE;
1688}
1689
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001691# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692/*
1693 * An X IO Error handler, used to catch error while opening the display.
1694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001696x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001698 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001700# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001701 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001702# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703}
1704# endif
1705
1706/*
1707 * An X IO Error handler, used to catch terminal errors.
1708 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001709static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001712x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001715 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 x11_window = 0;
1717 x11_display = NULL;
1718 xterm_Shell = (Widget)0;
1719
Bram Moolenaar0f873732019-12-05 20:28:46 +01001720 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001722# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001723 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001724# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001726
1727/*
1728 * If the X11 connection was lost try to restore it.
1729 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001730 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001731 */
1732 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001733may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001734{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001735 // No point in restoring the connecting if we are exiting or dying.
1736 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001737 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001738 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001739
1740# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001741 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001742 if (app_context != (XtAppContext)NULL)
1743 {
1744 XtDestroyApplicationContext(app_context);
1745 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001746 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001747 }
1748# endif
1749
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001750 setup_term_clip();
1751 get_x11_title(FALSE);
1752 }
1753}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001754
1755 void
1756ex_xrestore(exarg_T *eap)
1757{
1758 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1759 {
1760 if (xterm_display_allocated)
1761 vim_free(xterm_display);
1762 xterm_display = (char *)vim_strsave(eap->arg);
1763 xterm_display_allocated = TRUE;
1764 }
1765 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001766 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001767
1768 clear_xterm_clip();
1769 x11_window = 0;
1770 xterm_dpy_retry_count = 5; // Try reconnecting five times
1771 may_restore_clipboard();
1772}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#endif
1774
1775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 * Test if "dpy" and x11_window are valid by getting the window title.
1777 * I don't actually want it yet, so there may be a simpler call to use, but
1778 * this will cause the error handler x_error_check() to be called if anything
1779 * is wrong, such as the window pointer being invalid (as can happen when the
1780 * user changes his DISPLAY, but not his WINDOWID) -- webb
1781 */
1782 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001783test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
1785 int (*old_handler)();
1786 XTextProperty text_prop;
1787
1788 old_handler = XSetErrorHandler(x_error_check);
1789 got_x_error = FALSE;
1790 if (XGetWMName(dpy, x11_window, &text_prop))
1791 XFree((void *)text_prop.value);
1792 XSync(dpy, False);
1793 (void)XSetErrorHandler(old_handler);
1794
1795 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001796 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 return (got_x_error ? FAIL : OK);
1799}
1800#endif
1801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802
1803#ifdef FEAT_X11
1804
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001805static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807/*
1808 * try to get x11 window and display
1809 *
1810 * return FAIL for failure, OK otherwise
1811 */
1812 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001813get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
1815 char *winid;
1816 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001817#define XD_NONE 0 // x11_display not set here
1818#define XD_HERE 1 // x11_display opened here
1819#define XD_GUI 2 // x11_display used from gui.dpy
1820#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 static int x11_display_from = XD_NONE;
1822 static int did_set_error_handler = FALSE;
1823
1824 if (!did_set_error_handler)
1825 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001826 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 (void)XSetErrorHandler(x_error_handler);
1828 did_set_error_handler = TRUE;
1829 }
1830
Bram Moolenaar9372a112005-12-06 19:59:18 +00001831#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 if (gui.in_use)
1833 {
1834 /*
1835 * If the X11 display was opened here before, for the window where Vim
1836 * was started, close that one now to avoid a memory leak.
1837 */
1838 if (x11_display_from == XD_HERE && x11_display != NULL)
1839 {
1840 XCloseDisplay(x11_display);
1841 x11_display_from = XD_NONE;
1842 }
1843 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1844 {
1845 x11_display_from = XD_GUI;
1846 return OK;
1847 }
1848 x11_display = NULL;
1849 return FAIL;
1850 }
1851 else if (x11_display_from == XD_GUI)
1852 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001853 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 x11_window = 0;
1855 x11_display = NULL;
1856 x11_display_from = XD_NONE;
1857 }
1858#endif
1859
Bram Moolenaar0f873732019-12-05 20:28:46 +01001860 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 if (!x_connect_to_server())
1862 return FAIL;
1863
1864 /*
1865 * If WINDOWID not set, should try another method to find out
1866 * what the current window number is. The only code I know for
1867 * this is very complicated.
1868 * We assume that zero is invalid for WINDOWID.
1869 */
1870 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1871 x11_window = (Window)atol(winid);
1872
1873#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001874 if (xterm_dpy == x11_display)
1875 // x11_display may have been set to xterm_dpy elsewhere
1876 x11_display_from = XD_XTERM;
1877
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (xterm_dpy != NULL && x11_window != 0)
1879 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001880 // We may have checked it already, but Gnome terminal can move us to
1881 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001882 if (x11_display_from != XD_XTERM)
1883 {
1884 /*
1885 * If the X11 display was opened here before, for the window where
1886 * Vim was started, close that one now to avoid a memory leak.
1887 */
1888 if (x11_display_from == XD_HERE && x11_display != NULL)
1889 XCloseDisplay(x11_display);
1890 x11_display = xterm_dpy;
1891 x11_display_from = XD_XTERM;
1892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (test_x11_window(x11_display) == FAIL)
1894 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001895 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 x11_window = 0;
1897 x11_display = NULL;
1898 x11_display_from = XD_NONE;
1899 return FAIL;
1900 }
1901 return OK;
1902 }
1903#endif
1904
1905 if (x11_window == 0 || x11_display == NULL)
1906 result = -1;
1907
Bram Moolenaar0f873732019-12-05 20:28:46 +01001908 if (result != -1) // Have already been here and set this
1909 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910
1911 if (x11_window != 0 && x11_display == NULL)
1912 {
1913#ifdef SET_SIG_ALARM
1914 RETSIGTYPE (*sig_save)();
1915#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001916#ifdef ELAPSED_FUNC
1917 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
1919 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001920 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921#endif
1922
1923#ifdef SET_SIG_ALARM
1924 /*
1925 * Opening the Display may hang if the DISPLAY setting is wrong, or
1926 * the network connection is bad. Set an alarm timer to get out.
1927 */
1928 sig_alarm_called = FALSE;
1929 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1930 (RETSIGTYPE (*)())sig_alarm);
1931 alarm(2);
1932#endif
1933 x11_display = XOpenDisplay(NULL);
1934
1935#ifdef SET_SIG_ALARM
1936 alarm(0);
1937 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1938 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001939 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940#endif
1941 if (x11_display != NULL)
1942 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001943# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001945 {
1946 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001947 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001948 verbose_leave();
1949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950# endif
1951 if (test_x11_window(x11_display) == FAIL)
1952 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001953 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 x11_window = 0;
1955 XCloseDisplay(x11_display);
1956 x11_display = NULL;
1957 }
1958 else
1959 x11_display_from = XD_HERE;
1960 }
1961 }
1962 if (x11_window == 0 || x11_display == NULL)
1963 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001964
1965# ifdef FEAT_EVAL
1966 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1967# endif
1968
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 return (result = OK);
1970}
1971
1972/*
1973 * Determine original x11 Window Title
1974 */
1975 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001976get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001978 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979}
1980
1981/*
1982 * Determine original x11 Window icon
1983 */
1984 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001985get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986{
1987 int retval = FALSE;
1988
1989 retval = get_x11_thing(FALSE, test_only);
1990
Bram Moolenaar0f873732019-12-05 20:28:46 +01001991 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 if (oldicon == NULL && !test_only)
1993 {
1994 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001995 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001997 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999
2000 return retval;
2001}
2002
2003 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002004get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002005 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01002006 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
2008 XTextProperty text_prop;
2009 int retval = FALSE;
2010 Status status;
2011
2012 if (get_x11_windis() == OK)
2013 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002014 // Get window/icon name if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (get_title)
2016 status = XGetWMName(x11_display, x11_window, &text_prop);
2017 else
2018 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2019
2020 /*
2021 * If terminal is xterm, then x11_window may be a child window of the
2022 * outer xterm window that actually contains the window/icon name, so
2023 * keep traversing up the tree until a window with a title/icon is
2024 * found.
2025 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002026 // Previously this was only done for xterm and alike. I don't see a
Bram Moolenaar0f873732019-12-05 20:28:46 +01002027 // reason why it would fail for other terminal emulators.
2028 // if (term_is_xterm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
2030 Window root;
2031 Window parent;
2032 Window win = x11_window;
2033 Window *children;
2034 unsigned int num_children;
2035
2036 while (!status || text_prop.value == NULL)
2037 {
2038 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2039 &num_children))
2040 break;
2041 if (children)
2042 XFree((void *)children);
2043 if (parent == root || parent == 0)
2044 break;
2045
2046 win = parent;
2047 if (get_title)
2048 status = XGetWMName(x11_display, win, &text_prop);
2049 else
2050 status = XGetWMIconName(x11_display, win, &text_prop);
2051 }
2052 }
2053 if (status && text_prop.value != NULL)
2054 {
2055 retval = TRUE;
2056 if (!test_only)
2057 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002058 if (get_title)
2059 vim_free(oldtitle);
2060 else
2061 vim_free(oldicon);
Bram Moolenaara12a1612019-01-24 16:39:02 +01002062 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 if (get_title)
2065 oldtitle = vim_strsave((char_u *)text_prop.value);
2066 else
2067 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 }
2069 else
2070 {
2071 char **cl;
2072 Status transform_status;
2073 int n = 0;
2074
2075 transform_status = XmbTextPropertyToTextList(x11_display,
2076 &text_prop,
2077 &cl, &n);
2078 if (transform_status >= Success && n > 0 && cl[0])
2079 {
2080 if (get_title)
2081 oldtitle = vim_strsave((char_u *) cl[0]);
2082 else
2083 oldicon = vim_strsave((char_u *) cl[0]);
2084 XFreeStringList(cl);
2085 }
2086 else
2087 {
2088 if (get_title)
2089 oldtitle = vim_strsave((char_u *)text_prop.value);
2090 else
2091 oldicon = vim_strsave((char_u *)text_prop.value);
2092 }
2093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
2095 XFree((void *)text_prop.value);
2096 }
2097 }
2098 return retval;
2099}
2100
Bram Moolenaar0f873732019-12-05 20:28:46 +01002101// Xutf8 functions are not available on older systems. Note that on some
2102// systems X_HAVE_UTF8_STRING may be defined in a header file but
2103// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2104// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002105#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002106# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107# define USE_UTF8_STRING
2108# endif
2109#endif
2110
2111/*
2112 * Set x11 Window Title
2113 *
2114 * get_x11_windis() must be called before this and have returned OK
2115 */
2116 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002117set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002119 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2120 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2121 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122#ifdef USE_UTF8_STRING
2123 if (enc_utf8)
2124 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2125 NULL, NULL, 0, NULL, NULL, NULL);
2126 else
2127#endif
2128 {
2129#if XtSpecificationRelease >= 4
2130# ifdef FEAT_XFONTSET
2131 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2132 NULL, NULL, 0, NULL, NULL, NULL);
2133# else
2134 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002135 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
Bram Moolenaar0f873732019-12-05 20:28:46 +01002137 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002138 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 XSetWMProperties(x11_display, x11_window, &text_prop,
2140 NULL, NULL, 0, NULL, NULL, NULL);
2141# endif
2142#else
2143 XStoreName(x11_display, x11_window, (char *)title);
2144#endif
2145 }
2146 XFlush(x11_display);
2147}
2148
2149/*
2150 * Set x11 Window icon
2151 *
2152 * get_x11_windis() must be called before this and have returned OK
2153 */
2154 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002155set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002157 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158#ifdef USE_UTF8_STRING
2159 if (enc_utf8)
2160 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2161 NULL, 0, NULL, NULL, NULL);
2162 else
2163#endif
2164 {
2165#if XtSpecificationRelease >= 4
2166# ifdef FEAT_XFONTSET
2167 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2168 NULL, 0, NULL, NULL, NULL);
2169# else
2170 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002171 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002173 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2175 NULL, 0, NULL, NULL, NULL);
2176# endif
2177#else
2178 XSetIconName(x11_display, x11_window, (char *)icon);
2179#endif
2180 }
2181 XFlush(x11_display);
2182}
2183
Bram Moolenaar0f873732019-12-05 20:28:46 +01002184#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002187get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188{
2189 return FALSE;
2190}
2191
2192 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002193get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194{
2195 if (!test_only)
2196 {
2197 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002198 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002200 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 }
2202 return FALSE;
2203}
2204
Bram Moolenaar0f873732019-12-05 20:28:46 +01002205#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206
2207 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002208mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209{
2210 return get_x11_title(TRUE);
2211}
2212
2213 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002214mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216 return get_x11_icon(TRUE);
2217}
2218
2219/*
2220 * Set the window title and icon.
2221 */
2222 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002223mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224{
2225 int type = 0;
2226 static int recursive = 0;
2227
Bram Moolenaar0f873732019-12-05 20:28:46 +01002228 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002230 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 return;
2232
Bram Moolenaar0f873732019-12-05 20:28:46 +01002233 // When one of the X11 functions causes a deadly signal, we get here again
2234 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 if (recursive)
2236 return;
2237 ++recursive;
2238
2239 /*
2240 * if the window ID and the display is known, we may use X11 calls
2241 */
2242#ifdef FEAT_X11
2243 if (get_x11_windis() == OK)
2244 type = 1;
2245#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002246# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002247 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 if (gui.in_use)
2249 type = 1;
2250# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251#endif
2252
2253 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002254 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 * than x11 calls, because the x11 calls don't always work
2256 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 if ((type || *T_TS != NUL) && title != NULL)
2258 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002259 if (oldtitle_outdated)
2260 {
2261 oldtitle_outdated = FALSE;
2262 VIM_CLEAR(oldtitle);
2263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 if (oldtitle == NULL
2265#ifdef FEAT_GUI
2266 && !gui.in_use
2267#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002268 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 (void)get_x11_title(FALSE);
2270
Bram Moolenaar0f873732019-12-05 20:28:46 +01002271 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 term_settitle(title);
2273#ifdef FEAT_X11
2274 else
2275# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002276 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002278 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002280#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002281 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 else
2283 gui_mch_settitle(title, icon);
2284#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002285 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287
2288 if ((type || *T_CIS != NUL) && icon != NULL)
2289 {
2290 if (oldicon == NULL
2291#ifdef FEAT_GUI
2292 && !gui.in_use
2293#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002294 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 get_x11_icon(FALSE);
2296
2297 if (*T_CIS != NUL)
2298 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002299 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002301 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 out_flush();
2303 }
2304#ifdef FEAT_X11
2305 else
2306# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002307 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002309 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310#endif
2311 did_set_icon = TRUE;
2312 }
2313 --recursive;
2314}
2315
2316/*
2317 * Restore the window/icon title.
2318 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002319 * SAVE_RESTORE_TITLE only restore title
2320 * SAVE_RESTORE_ICON only restore icon
2321 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 */
2323 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002324mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
Bram Moolenaardac13472019-09-16 21:06:21 +02002326 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002327
Bram Moolenaar0f873732019-12-05 20:28:46 +01002328 // only restore the title or icon when it has been set
Bram Moolenaardac13472019-09-16 21:06:21 +02002329 mch_settitle(((which & SAVE_RESTORE_TITLE) && unix_did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002331 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2332
Bram Moolenaare5c83282019-05-03 23:15:37 +02002333 if (do_push_pop)
2334 {
2335 // pop and push from/to the stack
2336 term_pop_title(which);
2337 term_push_title(which);
2338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339}
2340
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
2342/*
2343 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002344 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 */
2346 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002347vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
2349 if (name == NULL)
2350 return FALSE;
2351 return (STRNICMP(name, "xterm", 5) == 0
2352 || STRNICMP(name, "nxterm", 6) == 0
2353 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002354 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002356 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 || STRCMP(name, "builtin_xterm") == 0);
2358}
2359
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002360#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2361/*
2362 * Return TRUE if "name" appears to be that of a terminal
2363 * known to support the xterm-style mouse protocol.
2364 * Relies on term_is_xterm having been set to its correct value.
2365 */
2366 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002367use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002368{
2369 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002370 && (term_is_xterm
2371 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002372 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002373 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002374 || STRICMP(name, "st") == 0
2375 || STRNICMP(name, "st-", 3) == 0
2376 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002377}
2378#endif
2379
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380/*
2381 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2382 * Return 1 for "xterm".
2383 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002384 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002385 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 */
2387 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002388use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002390 if (ttym_flags == TTYM_SGR)
2391 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002392 if (ttym_flags == TTYM_URXVT)
2393 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (ttym_flags == TTYM_XTERM2)
2395 return 2;
2396 if (ttym_flags == TTYM_XTERM)
2397 return 1;
2398 return 0;
2399}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
2401 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002402vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403{
2404 if (name == NULL)
2405 return FALSE;
2406 return (STRNICMP(name, "iris-ansi", 9) == 0
2407 || STRCMP(name, "builtin_iris-ansi") == 0);
2408}
2409
2410 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002411vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412{
2413 if (name == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002414 return FALSE; // actually all ANSI comp. terminals should be here
2415 // catch VT100 - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002416 return ((STRNICMP(name, "vt", 2) == 0
2417 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 || STRCMP(name, "builtin_vt320") == 0);
2419}
2420
2421/*
2422 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2423 * This should include all windowed terminal emulators.
2424 */
2425 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002426vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427{
2428 if (name == NULL)
2429 return FALSE;
2430 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2431 return TRUE;
2432 return ( STRNICMP(name, "hpterm", 6) == 0
2433 || STRNICMP(name, "sun-cmd", 7) == 0
2434 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002435 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 || STRNICMP(name, "dtterm", 6) == 0);
2437}
2438
2439/*
2440 * Insert user name in s[len].
2441 * Return OK if a name found.
2442 */
2443 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002444mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445{
2446#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002447 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 return OK;
2449#else
2450 return mch_get_uname(getuid(), s, len);
2451#endif
2452}
2453
2454/*
2455 * Insert user name for "uid" in s[len].
2456 * Return OK if a name found.
2457 */
2458 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002459mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460{
2461#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2462 struct passwd *pw;
2463
2464 if ((pw = getpwuid(uid)) != NULL
2465 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2466 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002467 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 return OK;
2469 }
2470#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002471 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2472 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473}
2474
2475/*
2476 * Insert host name is s[len].
2477 */
2478
2479#ifdef HAVE_SYS_UTSNAME_H
2480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002481mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483 struct utsname vutsname;
2484
2485 if (uname(&vutsname) < 0)
2486 *s = NUL;
2487 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002488 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002490#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
2492# ifdef HAVE_SYS_SYSTEMINFO_H
2493# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2494# endif
2495
2496 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002497mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498{
2499# ifdef VAXC
2500 vaxc$gethostname((char *)s, len);
2501# else
2502 gethostname((char *)s, len);
2503# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002504 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002506#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
2508/*
2509 * return process ID
2510 */
2511 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002512mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513{
2514 return (long)getpid();
2515}
2516
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002517/*
2518 * return TRUE if process "pid" is still running
2519 */
2520 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002521mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002522{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002523 // If there is no error the process must be running.
2524 if (kill(pid, 0) == 0)
2525 return TRUE;
2526#ifdef ESRCH
2527 // If the error is ESRCH then the process is not running.
2528 if (errno == ESRCH)
2529 return FALSE;
2530#endif
2531 // If the process is running and owned by another user we get EPERM. With
2532 // other errors the process might be running, assuming it is then.
2533 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002534}
2535
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002538strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
2540 extern int sys_nerr;
2541 extern char *sys_errlist[];
2542 static char er[20];
2543
2544 if (err > 0 && err < sys_nerr)
2545 return (sys_errlist[err]);
2546 sprintf(er, "Error %d", err);
2547 return er;
2548}
2549#endif
2550
2551/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002552 * Get name of current directory into buffer "buf" of length "len" bytes.
2553 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 * Return OK for success, FAIL for failure.
2555 */
2556 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002557mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558{
2559#if defined(USE_GETCWD)
2560 if (getcwd((char *)buf, len) == NULL)
2561 {
2562 STRCPY(buf, strerror(errno));
2563 return FAIL;
2564 }
2565 return OK;
2566#else
2567 return (getwd((char *)buf) != NULL ? OK : FAIL);
2568#endif
2569}
2570
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002572 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 *
2574 * return FAIL for failure, OK for success
2575 */
2576 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002577mch_FullName(
2578 char_u *fname,
2579 char_u *buf,
2580 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002581 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
2583 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002584#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002586 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 char_u olddir[MAXPATHL];
2589 char_u *p;
2590 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002591#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002592 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2593 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002594#endif
2595
Bram Moolenaar38323e42007-03-06 19:22:53 +00002596#ifdef VMS
2597 fname = vms_fixfilename(fname);
2598#endif
2599
Bram Moolenaara2442432007-04-26 14:26:37 +00002600#ifdef __CYGWIN__
2601 /*
2602 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2603 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002604# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002605 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2606 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002607 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2608 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002609# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002610 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002611# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002612 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002613#endif
2614
Bram Moolenaar0f873732019-12-05 20:28:46 +01002615 // Expand it if forced or not an absolute path.
2616 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002617 if ((force || !mch_isFullName(fname))
2618 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
2620 /*
2621 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002622 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 * This will get the correct path name with "../" things.
2624 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002625 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002627 if (STRCMP(p, "/..") == 0)
2628 // for "/path/dir/.." include the "/.."
2629 p += 3;
2630
Bram Moolenaar38323e42007-03-06 19:22:53 +00002631#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 /*
2633 * Use fchdir() if possible, it's said to be faster and more
2634 * reliable. But on SunOS 4 it might not work. Check this by
2635 * doing a fchdir() right now.
2636 */
2637 if (!dont_fchdir)
2638 {
2639 fd = open(".", O_RDONLY | O_EXTRA, 0);
2640 if (fd >= 0 && fchdir(fd) < 0)
2641 {
2642 close(fd);
2643 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002644 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
2646 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
Bram Moolenaar0f873732019-12-05 20:28:46 +01002649 // Only change directory when we are sure we can return to where
2650 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002652#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 (mch_dirname(olddir, MAXPATHL) == FAIL
2656 || mch_chdir((char *)olddir) != 0))
2657 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002658 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 retval = FAIL;
2660 }
2661 else
2662 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002663 // The directory is copied into buf[], to be able to remove
2664 // the file name without changing it (could be a string in
2665 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 if (p - fname >= len)
2667 retval = FAIL;
2668 else
2669 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002670 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002672 {
2673 // Path does not exist (yet). For a full path fail,
2674 // will use the path as-is. For a relative path use
2675 // the current directory and append the file name.
2676 if (mch_isFullName(fname))
2677 retval = FAIL;
2678 else
2679 p = NULL;
2680 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002681 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002683 else
2684 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 *buf = NUL;
2686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 }
2688 }
2689 if (mch_dirname(buf, len) == FAIL)
2690 {
2691 retval = FAIL;
2692 *buf = NUL;
2693 }
2694 if (p != NULL)
2695 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002696#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 if (fd >= 0)
2698 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002699 if (p_verbose >= 5)
2700 {
2701 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002702 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002703 verbose_leave();
2704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 }
2707 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 l = mch_chdir((char *)olddir);
2710 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002711 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
itchyny051a40c2021-10-20 10:00:05 +01002713#ifdef HAVE_FCHDIR
2714 if (fd >= 0)
2715 close(fd);
2716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
2718 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002719 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002720 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002721#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002722 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002724 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002727
Bram Moolenaar0f873732019-12-05 20:28:46 +01002728 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002729 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 return FAIL;
2731
Bram Moolenaar0f873732019-12-05 20:28:46 +01002732 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 if (STRCMP(fname, ".") != 0)
2734 STRCAT(buf, fname);
2735
2736 return OK;
2737}
2738
2739/*
2740 * Return TRUE if "fname" does not depend on the current directory.
2741 */
2742 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002743mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002745#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 return ( fname[0] == '/' || fname[0] == '.' ||
2747 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2748 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2749 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002750#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752#endif
2753}
2754
Bram Moolenaar24552be2005-12-10 20:17:30 +00002755#if defined(USE_FNAME_CASE) || defined(PROTO)
2756/*
2757 * Set the case of the file name, if it already exists. This will cause the
2758 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002759 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002760 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002761 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002762fname_case(
2763 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002764 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002765{
2766 struct stat st;
2767 char_u *slash, *tail;
2768 DIR *dirp;
2769 struct dirent *dp;
2770
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002771 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002772 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002773 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002774 slash = vim_strrchr(name, '/');
2775 if (slash == NULL)
2776 {
2777 dirp = opendir(".");
2778 tail = name;
2779 }
2780 else
2781 {
2782 *slash = NUL;
2783 dirp = opendir((char *)name);
2784 *slash = '/';
2785 tail = slash + 1;
2786 }
2787
2788 if (dirp != NULL)
2789 {
2790 while ((dp = readdir(dirp)) != NULL)
2791 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002792 // Only accept names that differ in case and are the same byte
2793 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002794 if (STRICMP(tail, dp->d_name) == 0
2795 && STRLEN(tail) == STRLEN(dp->d_name))
2796 {
2797 char_u newname[MAXPATHL + 1];
2798 struct stat st2;
2799
Bram Moolenaar0f873732019-12-05 20:28:46 +01002800 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002801 vim_strncpy(newname, name, MAXPATHL);
2802 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2803 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002804 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002805 && st.st_ino == st2.st_ino
2806 && st.st_dev == st2.st_dev)
2807 {
2808 STRCPY(tail, dp->d_name);
2809 break;
2810 }
2811 }
2812 }
2813
2814 closedir(dirp);
2815 }
2816 }
2817}
2818#endif
2819
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820/*
2821 * Get file permissions for 'name'.
2822 * Returns -1 when it doesn't exist.
2823 */
2824 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002825mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
2827 struct stat statb;
2828
Bram Moolenaar0f873732019-12-05 20:28:46 +01002829 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830#ifdef VMS
2831 if (stat((char *)vms_fixfilename(name), &statb))
2832#else
2833 if (stat((char *)name, &statb))
2834#endif
2835 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002836#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002837 // The top bit makes the value negative, which means the file doesn't
2838 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002839 return statb.st_mode & ~S_ADDACE;
2840#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843}
2844
2845/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002846 * Set file permission for "name" to "perm".
2847 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 */
2849 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002850mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851{
2852 return (chmod((char *)
2853#ifdef VMS
2854 vms_fixfilename(name),
2855#else
2856 name,
2857#endif
2858 (mode_t)perm) == 0 ? OK : FAIL);
2859}
2860
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002861#if defined(HAVE_FCHMOD) || defined(PROTO)
2862/*
2863 * Set file permission for open file "fd" to "perm".
2864 * Return FAIL for failure, OK otherwise.
2865 */
2866 int
2867mch_fsetperm(int fd, long perm)
2868{
2869 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2870}
2871#endif
2872
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873#if defined(HAVE_ACL) || defined(PROTO)
2874# ifdef HAVE_SYS_ACL_H
2875# include <sys/acl.h>
2876# endif
2877# ifdef HAVE_SYS_ACCESS_H
2878# include <sys/access.h>
2879# endif
2880
2881# ifdef HAVE_SOLARIS_ACL
2882typedef struct vim_acl_solaris_T {
2883 int acl_cnt;
2884 aclent_t *acl_entry;
2885} vim_acl_solaris_T;
2886# endif
2887
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002888#if defined(HAVE_SELINUX) || defined(PROTO)
2889/*
2890 * Copy security info from "from_file" to "to_file".
2891 */
2892 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002893mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002894{
2895 if (from_file == NULL)
2896 return;
2897
2898 if (selinux_enabled == -1)
2899 selinux_enabled = is_selinux_enabled();
2900
2901 if (selinux_enabled > 0)
2902 {
Bram Moolenaar89560232020-10-09 23:04:47 +02002903 // Use "char *" instead of "security_context_t" to avoid a deprecation
2904 // warning.
2905 char *from_context = NULL;
2906 char *to_context = NULL;
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002907
2908 if (getfilecon((char *)from_file, &from_context) < 0)
2909 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002910 // If the filesystem doesn't support extended attributes,
2911 // the original had no special security context and the
2912 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002913 if (errno == EOPNOTSUPP)
2914 return;
2915
Bram Moolenaar32526b32019-01-19 17:43:09 +01002916 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002917 msg_outtrans(from_file);
2918 msg_putchar('\n');
2919 return;
2920 }
2921 if (getfilecon((char *)to_file, &to_context) < 0)
2922 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002923 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002924 msg_outtrans(to_file);
2925 msg_putchar('\n');
2926 freecon (from_context);
2927 return ;
2928 }
2929 if (strcmp(from_context, to_context) != 0)
2930 {
2931 if (setfilecon((char *)to_file, from_context) < 0)
2932 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002933 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002934 msg_outtrans(to_file);
2935 msg_putchar('\n');
2936 }
2937 }
2938 freecon(to_context);
2939 freecon(from_context);
2940 }
2941}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002942#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002943
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002944#if defined(HAVE_SMACK) && !defined(PROTO)
2945/*
2946 * Copy security info from "from_file" to "to_file".
2947 */
2948 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002949mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002950{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002951 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002952 {
2953 XATTR_NAME_SMACK,
2954 XATTR_NAME_SMACKEXEC,
2955 XATTR_NAME_SMACKMMAP
2956 };
2957
2958 char buffer[SMACK_LABEL_LEN];
2959 const char *name;
2960 int index;
2961 int ret;
2962 ssize_t size;
2963
2964 if (from_file == NULL)
2965 return;
2966
2967 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2968 / sizeof(smack_copied_attributes)[0]) ; index++)
2969 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002970 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002971 name = smack_copied_attributes[index];
2972
Bram Moolenaar0f873732019-12-05 20:28:46 +01002973 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002974 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2975 if (size >= 0)
2976 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002977 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002978 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2979 if (ret < 0)
2980 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002981 vim_snprintf((char *)IObuff, IOSIZE,
2982 _("Could not set security context %s for %s"),
2983 name, to_file);
2984 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002985 msg_putchar('\n');
2986 }
2987 }
2988 else
2989 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002990 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002991 switch (errno)
2992 {
2993 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002994 // extended attributes aren't supported or enabled
2995 // should a message be echoed? not sure...
2996 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002997
2998 case ERANGE:
2999 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003000 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003001 vim_snprintf((char *)IObuff, IOSIZE,
3002 _("Could not get security context %s for %s. Removing it!"),
3003 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003004 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003005 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003006 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003007
3008 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003009 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003010 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003011 // Silently ignore errors, apparently this happens when
3012 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003013 break;
3014 }
3015 }
3016 }
3017}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003018#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003019
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020/*
3021 * Return a pointer to the ACL of file "fname" in allocated memory.
3022 * Return NULL if the ACL is not available for whatever reason.
3023 */
3024 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003025mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026{
3027 vim_acl_T ret = NULL;
3028#ifdef HAVE_POSIX_ACL
3029 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3030#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003031#ifdef HAVE_SOLARIS_ZFS_ACL
3032 acl_t *aclent;
3033
3034 if (acl_get((char *)fname, 0, &aclent) < 0)
3035 return NULL;
3036 ret = (vim_acl_T)aclent;
3037#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038#ifdef HAVE_SOLARIS_ACL
3039 vim_acl_solaris_T *aclent;
3040
3041 aclent = malloc(sizeof(vim_acl_solaris_T));
3042 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3043 {
3044 free(aclent);
3045 return NULL;
3046 }
3047 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3048 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3049 {
3050 free(aclent->acl_entry);
3051 free(aclent);
3052 return NULL;
3053 }
3054 ret = (vim_acl_T)aclent;
3055#else
3056#if defined(HAVE_AIX_ACL)
3057 int aclsize;
3058 struct acl *aclent;
3059
3060 aclsize = sizeof(struct acl);
3061 aclent = malloc(aclsize);
3062 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3063 {
3064 if (errno == ENOSPC)
3065 {
3066 aclsize = aclent->acl_len;
3067 aclent = realloc(aclent, aclsize);
3068 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3069 {
3070 free(aclent);
3071 return NULL;
3072 }
3073 }
3074 else
3075 {
3076 free(aclent);
3077 return NULL;
3078 }
3079 }
3080 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003081#endif // HAVE_AIX_ACL
3082#endif // HAVE_SOLARIS_ACL
3083#endif // HAVE_SOLARIS_ZFS_ACL
3084#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 return ret;
3086}
3087
3088/*
3089 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3090 */
3091 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003092mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
3094 if (aclent == NULL)
3095 return;
3096#ifdef HAVE_POSIX_ACL
3097 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3098#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003099#ifdef HAVE_SOLARIS_ZFS_ACL
3100 acl_set((char *)fname, (acl_t *)aclent);
3101#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102#ifdef HAVE_SOLARIS_ACL
3103 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3104 ((vim_acl_solaris_T *)aclent)->acl_entry);
3105#else
3106#ifdef HAVE_AIX_ACL
3107 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003108#endif // HAVE_AIX_ACL
3109#endif // HAVE_SOLARIS_ACL
3110#endif // HAVE_SOLARIS_ZFS_ACL
3111#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112}
3113
3114 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003115mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
3117 if (aclent == NULL)
3118 return;
3119#ifdef HAVE_POSIX_ACL
3120 acl_free((acl_t)aclent);
3121#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003122#ifdef HAVE_SOLARIS_ZFS_ACL
3123 acl_free((acl_t *)aclent);
3124#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125#ifdef HAVE_SOLARIS_ACL
3126 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3127 free(aclent);
3128#else
3129#ifdef HAVE_AIX_ACL
3130 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003131#endif // HAVE_AIX_ACL
3132#endif // HAVE_SOLARIS_ACL
3133#endif // HAVE_SOLARIS_ZFS_ACL
3134#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135}
3136#endif
3137
3138/*
3139 * Set hidden flag for "name".
3140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003142mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003144 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145}
3146
3147/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003148 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 * return FALSE if "name" is not a directory
3150 * return FALSE for error
3151 */
3152 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003153mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
3155 struct stat statb;
3156
Bram Moolenaar0f873732019-12-05 20:28:46 +01003157 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 return FALSE;
3159 if (stat((char *)name, &statb))
3160 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162}
3163
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003164/*
3165 * return TRUE if "name" is a directory, NOT a symlink to a directory
3166 * return FALSE if "name" is not a directory
3167 * return FALSE for error
3168 */
3169 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003170mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003171{
3172 struct stat statb;
3173
Bram Moolenaar0f873732019-12-05 20:28:46 +01003174 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003175 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003176 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003177 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003178 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003179}
3180
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181/*
3182 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3183 */
3184 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003185executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
3187 struct stat st;
3188
3189 if (stat((char *)name, &st))
3190 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003191#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003192 // Like on Unix system file can have executable rights but not necessarily
3193 // be an executable, but on Unix is not a default for an ordinary file to
3194 // have an executable flag - on VMS it is in most cases.
3195 // Therefore, this check does not have any sense - let keep us to the
3196 // conventions instead:
3197 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003198 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003199 int vms_executable = 0;
3200 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3201 {
3202 if (strstr(vms_tolower((char*)name),".exe") != NULL
3203 || strstr(vms_tolower((char*)name),".com")!= NULL)
3204 vms_executable = 1;
3205 }
3206 return vms_executable;
3207#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210}
3211
3212/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003213 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003214 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 * Return -1 if unknown.
3216 */
3217 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003218mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219{
3220 char_u *buf;
3221 char_u *p, *e;
3222 int retval;
3223
Bram Moolenaar0f873732019-12-05 20:28:46 +01003224 // When "use_path" is false and if it's an absolute or relative path don't
3225 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003226 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003227 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003228 // There must be a path separator, files in the current directory
3229 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003230 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003231 {
3232 if (path != NULL)
3233 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003234 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003235 *path = FullName_save(name, TRUE);
3236 else
3237 *path = vim_strsave(name);
3238 }
3239 return TRUE;
3240 }
3241 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
3244 p = (char_u *)getenv("PATH");
3245 if (p == NULL || *p == NUL)
3246 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003247 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (buf == NULL)
3249 return -1;
3250
3251 /*
3252 * Walk through all entries in $PATH to check if "name" exists there and
3253 * is an executable file.
3254 */
3255 for (;;)
3256 {
3257 e = (char_u *)strchr((char *)p, ':');
3258 if (e == NULL)
3259 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003260 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 STRCPY(buf, "./");
3262 else
3263 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003264 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 add_pathsep(buf);
3266 }
3267 STRCAT(buf, name);
3268 retval = executable_file(buf);
3269 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003270 {
3271 if (path != NULL)
3272 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003273 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003274 *path = FullName_save(buf, TRUE);
3275 else
3276 *path = vim_strsave(buf);
3277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
3281 if (*e != ':')
3282 break;
3283 p = e + 1;
3284 }
3285
3286 vim_free(buf);
3287 return retval;
3288}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
3290/*
3291 * Check what "name" is:
3292 * NODE_NORMAL: file or directory (or doesn't exist)
3293 * NODE_WRITABLE: writable device, socket, fifo, etc.
3294 * NODE_OTHER: non-writable things
3295 */
3296 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003297mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
3299 struct stat st;
3300
3301 if (stat((char *)name, &st))
3302 return NODE_NORMAL;
3303 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3304 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003305 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003307 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 return NODE_WRITABLE;
3309}
3310
3311 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003312mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
3314#ifdef HAVE_CHECK_STACK_GROWTH
3315 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 check_stack_growth((char *)&i);
3318
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003319# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 get_stack_limit();
3321# endif
3322
3323#endif
3324
3325 /*
3326 * Setup an alternative stack for signals. Helps to catch signals when
3327 * running out of stack space.
3328 * Use of sigaltstack() is preferred, it's more portable.
3329 * Ignore any errors.
3330 */
3331#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003332 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 init_signal_stack();
3334#endif
3335}
3336
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003337#if defined(EXITFREE) || defined(PROTO)
3338 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003339mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003340{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003341# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3342 if (clip_star.owned)
3343 clip_lose_selection(&clip_star);
3344 if (clip_plus.owned)
3345 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003346# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003347# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003348 if (xterm_Shell != (Widget)0)
3349 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003350# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003351 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003352 if (xterm_dpy != NULL)
3353 XtCloseDisplay(xterm_dpy);
3354 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003355 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003356 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003357# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003358 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003359# endif
3360 }
3361# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003362# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003363# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003364 if (x11_display != NULL
3365# ifdef FEAT_XCLIPBOARD
3366 && x11_display != xterm_dpy
3367# endif
3368 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003369 XCloseDisplay(x11_display);
3370# endif
3371# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003372 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003373# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003374 vim_free(oldtitle);
3375 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003376}
3377#endif
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379/*
3380 * Output a newline when exiting.
3381 * Make sure the newline goes to the same stream as the text.
3382 */
3383 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003384exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003386 if (silent_mode)
3387 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 if (newline_on_exit || msg_didout)
3389 {
3390 if (msg_use_printf())
3391 {
3392 if (info_message)
3393 mch_msg("\n");
3394 else
3395 mch_errmsg("\r\n");
3396 }
3397 else
3398 out_char('\n');
3399 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003400 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003402 restore_cterm_colors(); // get original colors back
3403 msg_clr_eos_force(); // clear the rest of the display
3404 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
3406}
3407
Bram Moolenaarb4151682020-05-11 22:13:28 +02003408#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003409# if (defined(__GNUC__) \
3410 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3411 || (defined(__clang__) && (__clang_major__ >= 12))
3412extern void __gcov_dump(void);
3413extern void __gcov_reset(void);
3414# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3415# else
3416extern void __gcov_flush(void);
3417# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003418#endif
3419
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003421mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
3423 exiting = TRUE;
3424
3425#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3426 x11_export_final_selection();
3427#endif
3428
3429#ifdef FEAT_GUI
3430 if (!gui.in_use)
3431#endif
3432 {
3433 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003434 if (!is_not_a_term())
3435 {
3436 // restore xterm title and icon name
3437 mch_restore_title(SAVE_RESTORE_BOTH);
3438 term_pop_title(SAVE_RESTORE_BOTH);
3439 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003440
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 /*
3442 * When t_ti is not empty but it doesn't cause swapping terminal
3443 * pages, need to output a newline when msg_didout is set. But when
3444 * t_ti does swap pages it should not go to the shell page. Do this
3445 * before stoptermcap().
3446 */
3447 if (swapping_screen() && !newline_on_exit)
3448 exit_scroll();
3449
Bram Moolenaar0f873732019-12-05 20:28:46 +01003450 // Stop termcap: May need to check for T_CRV response, which
3451 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 stoptermcap();
3453
3454 /*
3455 * A newline is only required after a message in the alternate screen.
3456 * This is set to TRUE by wait_return().
3457 */
3458 if (!swapping_screen() || newline_on_exit)
3459 exit_scroll();
3460
Bram Moolenaar0f873732019-12-05 20:28:46 +01003461 // Cursor may have been switched off without calling starttermcap()
3462 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 if (full_screen)
3464 cursor_on();
3465 }
3466 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003467 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003468
3469#ifdef USE_GCOV_FLUSH
3470 // Flush coverage info before possibly being killed by a deadly signal.
3471 __gcov_flush();
3472#endif
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 may_core_dump();
3475#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 gui_exit(r);
3478#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003479
Bram Moolenaar56718732006-03-15 22:53:57 +00003480#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003481 mac_conv_cleanup();
3482#endif
3483
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003485 // A core dump won't be created if the signal handler
3486 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (deadly_signal != 0)
3488 return;
3489#endif
3490
Bram Moolenaar009b2592004-10-24 19:18:58 +00003491#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003492 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003493#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003494
3495#ifdef EXITFREE
3496 free_all_mem();
3497#endif
3498
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 exit(r);
3500}
3501
3502 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003503may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
3505 if (deadly_signal != 0)
3506 {
3507 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003508 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510}
3511
3512#ifndef VMS
3513
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003514/*
3515 * Get the file descriptor to use for tty operations.
3516 */
3517 static int
3518get_tty_fd(int fd)
3519{
3520 int tty_fd = fd;
3521
3522#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3523 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3524 // "fd" when it is a master device.
3525 if (mch_isatty(fd) > 1)
3526 {
3527 char *name;
3528
3529 name = ptsname(fd);
3530 if (name == NULL)
3531 return -1;
3532
3533 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3534 if (tty_fd < 0)
3535 return -1;
3536 }
3537#endif
3538 return tty_fd;
3539}
3540
3541 static int
3542mch_tcgetattr(int fd, void *term)
3543{
3544 int tty_fd;
3545 int retval = -1;
3546
3547 tty_fd = get_tty_fd(fd);
3548 if (tty_fd >= 0)
3549 {
3550#ifdef NEW_TTY_SYSTEM
3551# ifdef HAVE_TERMIOS_H
3552 retval = tcgetattr(tty_fd, (struct termios *)term);
3553# else
3554 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3555# endif
3556#else
3557 // for "old" tty systems
3558 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3559#endif
3560 if (tty_fd != fd)
3561 close(tty_fd);
3562 }
3563 return retval;
3564}
3565
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003567mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568{
3569 static int first = TRUE;
3570
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003571#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572# ifdef HAVE_TERMIOS_H
3573 static struct termios told;
3574 struct termios tnew;
3575# else
3576 static struct termio told;
3577 struct termio tnew;
3578# endif
3579
3580 if (first)
3581 {
3582 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003583 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
3585
3586 tnew = told;
3587 if (tmode == TMODE_RAW)
3588 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003589 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003590 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3591 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003593# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003594 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595# endif
3596 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003597# ifdef ONLCR
3598 // Don't map NL -> CR NL, we do it ourselves.
3599 // Also disable expanding tabs if possible.
3600# ifdef XTABS
3601 tnew.c_oflag &= ~(ONLCR | XTABS);
3602# else
3603# ifdef TAB3
3604 tnew.c_oflag &= ~(ONLCR | TAB3);
3605# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003607# endif
3608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003610 tnew.c_cc[VMIN] = 1; // return after 1 char
3611 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003614 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003615 // Also reset ICANON here, otherwise on Solaris select() won't see
3616 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003617 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003618 tnew.c_cc[VMIN] = 1; // return after 1 char
3619 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
3622# if defined(HAVE_TERMIOS_H)
3623 {
3624 int n = 10;
3625
Bram Moolenaar0f873732019-12-05 20:28:46 +01003626 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3627 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3629 && errno == EINTR && n > 0)
3630 --n;
3631 }
3632# else
3633 ioctl(read_cmd_fd, TCSETA, &tnew);
3634# endif
3635
3636#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 /*
3638 * for "old" tty systems
3639 */
3640# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003641# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642# endif
3643 static struct sgttyb ttybold;
3644 struct sgttyb ttybnew;
3645
3646 if (first)
3647 {
3648 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003649 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 }
3651
3652 ttybnew = ttybold;
3653 if (tmode == TMODE_RAW)
3654 {
3655 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3656 ttybnew.sg_flags |= RAW;
3657 }
3658 else if (tmode == TMODE_SLEEP)
3659 ttybnew.sg_flags &= ~(ECHO);
3660 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3661#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003662 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663}
3664
3665/*
3666 * Try to get the code for "t_kb" from the stty setting
3667 *
3668 * Even if termcap claims a backspace key, the user's setting *should*
3669 * prevail. stty knows more about reality than termcap does, and if
3670 * somebody's usual erase key is DEL (which, for most BSD users, it will
3671 * be), they're going to get really annoyed if their erase key starts
3672 * doing forward deletes for no reason. (Eric Fischer)
3673 */
3674 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003675get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003677 ttyinfo_T info;
3678 char_u buf[2];
3679 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003681 if (get_tty_info(read_cmd_fd, &info) == OK)
3682 {
3683 intr_char = info.interrupt;
3684 buf[0] = info.backspace;
3685 buf[1] = NUL;
3686 add_termcode((char_u *)"kb", buf, FALSE);
3687
Bram Moolenaar0f873732019-12-05 20:28:46 +01003688 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003689 p = find_termcode((char_u *)"kD");
3690 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3691 do_fixdel(NULL);
3692 }
3693}
3694
3695/*
3696 * Obtain the characters that Backspace and Enter produce on "fd".
3697 * Returns OK or FAIL.
3698 */
3699 int
3700get_tty_info(int fd, ttyinfo_T *info)
3701{
3702#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703# ifdef HAVE_TERMIOS_H
3704 struct termios keys;
3705# else
3706 struct termio keys;
3707# endif
3708
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003709 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003711 info->backspace = keys.c_cc[VERASE];
3712 info->interrupt = keys.c_cc[VINTR];
3713 if (keys.c_iflag & ICRNL)
3714 info->enter = NL;
3715 else
3716 info->enter = CAR;
3717 if (keys.c_oflag & ONLCR)
3718 info->nl_does_cr = TRUE;
3719 else
3720 info->nl_does_cr = FALSE;
3721 return OK;
3722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003724 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 struct sgttyb keys;
3726
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003727 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003729 info->backspace = keys.sg_erase;
3730 info->interrupt = keys.sg_kill;
3731 info->enter = CAR;
3732 info->nl_does_cr = TRUE;
3733 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003736 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737}
3738
Bram Moolenaar0f873732019-12-05 20:28:46 +01003739#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003741static int mouse_ison = FALSE;
3742
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743/*
3744 * Set mouse clicks on or off.
3745 */
3746 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003747mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003749#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003750 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 int xterm_mouse_vers;
3753
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003754#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003755 if (!on)
3756 // Make sure not tracing mouse movements. Important when a button-down
3757 // was received but no release yet.
3758 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003759#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003760
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003761 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003762#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003763 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003764#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003765 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003766 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 return;
3768
3769 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003770
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003771#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003772 if (ttym_flags == TTYM_URXVT)
3773 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003774 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003775 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003776 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003777#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003778
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003779 if (ttym_flags == TTYM_SGR)
3780 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003781 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003782 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003783 mouse_ison = on;
3784 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003785
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003786#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003787 if (bevalterm_ison != (p_bevalterm && on))
3788 {
3789 bevalterm_ison = (p_bevalterm && on);
3790 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003791 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003792 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003793 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003794#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003795
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (xterm_mouse_vers > 0)
3797 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003798 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 out_str_nf((char_u *)
3800 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003801 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003802#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003803 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003804#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003805 "\033[?1002h")
3806 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003807 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003809 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003810 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003813#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 else if (ttym_flags == TTYM_DEC)
3815 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003816 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003818 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003820 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003824#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 else
3826 {
3827 if (on)
3828 {
3829 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003830 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832 else
3833 {
3834 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003835 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003840#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003841 else
3842 {
3843 if (on)
3844 {
3845 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003846 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003847 }
3848 else
3849 {
3850 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003851 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003852 }
3853 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003854#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003855
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003856#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 else
3858 {
3859 if (on)
3860 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003861 // D - Enable Mouse up/down messages
3862 // L - Enable Left Button Reporting
3863 // M - Enable Middle Button Reporting
3864 // R - Enable Right Button Reporting
3865 // K - Enable SHIFT and CTRL key Reporting
3866 // + - Enable Advanced messaging of mouse moves and up/down messages
3867 // Q - Quiet No Ack
3868 // # - Numeric value of mouse pointer required
3869 // 0 = Multiview 2000 cursor, used as standard
3870 // 1 = Windows Arrow
3871 // 2 = Windows I Beam
3872 // 3 = Windows Hour Glass
3873 // 4 = Windows Cross Hair
3874 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003875# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003876 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003877 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003878# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003879 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003880# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003881 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883 else
3884 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003885 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003886 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 }
3888 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003889#endif
3890#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 else
3892 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003893 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (on)
3895 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3896 else
3897 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003898 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901}
3902
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003903#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003904/*
3905 * Called when 'balloonevalterm' changed.
3906 */
3907 void
3908mch_bevalterm_changed(void)
3909{
3910 mch_setmouse(mouse_ison);
3911}
3912#endif
3913
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914/*
3915 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3916 */
3917 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003918check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919{
3920# ifdef FEAT_MOUSE_XTERM
3921 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003922# ifdef FEAT_MOUSE_URXVT
3923 && use_xterm_mouse() != 3
3924# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925# ifdef FEAT_GUI
3926 && !gui.in_use
3927# endif
3928 )
3929 {
3930 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003931 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 if (*p_mouse != NUL)
3933 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003934 // force mouse off and maybe on to send possibly new mouse
3935 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 mch_setmouse(FALSE);
3937 setmouse();
3938 }
3939 }
3940 else
3941 del_mouse_termcode(KS_MOUSE);
3942# endif
3943
3944# ifdef FEAT_MOUSE_GPM
3945 if (!use_xterm_mouse()
3946# ifdef FEAT_GUI
3947 && !gui.in_use
3948# endif
3949 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003950 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003951 else
3952 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953# endif
3954
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003955# ifdef FEAT_SYSMOUSE
3956 if (!use_xterm_mouse()
3957# ifdef FEAT_GUI
3958 && !gui.in_use
3959# endif
3960 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003961 set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003962# endif
3963
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003965 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 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_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 else
3973 del_mouse_termcode(KS_JSBTERM_MOUSE);
3974# endif
3975
3976# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003977 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3978 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (!use_xterm_mouse()
3980# ifdef FEAT_GUI
3981 && !gui.in_use
3982# endif
3983 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003984 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 else
3986 del_mouse_termcode(KS_NETTERM_MOUSE);
3987# endif
3988
3989# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003990 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003991 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992# ifdef FEAT_GUI
3993 && !gui.in_use
3994# endif
3995 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003996 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003997 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 else
3999 del_mouse_termcode(KS_DEC_MOUSE);
4000# endif
4001# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004002 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004003 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004# ifdef FEAT_GUI
4005 && !gui.in_use
4006# endif
4007 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004008 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 else
4010 del_mouse_termcode(KS_PTERM_MOUSE);
4011# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004012# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004013 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004014# ifdef FEAT_GUI
4015 && !gui.in_use
4016# endif
4017 )
4018 {
4019 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004020 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004021
4022 if (*p_mouse != NUL)
4023 {
4024 mch_setmouse(FALSE);
4025 setmouse();
4026 }
4027 }
4028 else
4029 del_mouse_termcode(KS_URXVT_MOUSE);
4030# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004031 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004032# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004033 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004034# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004035 )
4036 {
4037 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004038 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004039
4040 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004041 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004042
4043 if (*p_mouse != NUL)
4044 {
4045 mch_setmouse(FALSE);
4046 setmouse();
4047 }
4048 }
4049 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004050 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004051 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004052 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056#ifndef VMS
4057
4058/*
4059 * Try to get the current window size:
4060 * 1. with an ioctl(), most accurate method
4061 * 2. from the environment variables LINES and COLUMNS
4062 * 3. from the termcap
4063 * 4. keep using the old values
4064 * Return OK when size could be determined, FAIL otherwise.
4065 */
4066 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004067mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068{
4069 long rows = 0;
4070 long columns = 0;
4071 char_u *p;
4072
4073 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 * 1. try using an ioctl. It is the most accurate method.
4075 *
4076 * Try using TIOCGWINSZ first, some systems that have it also define
4077 * TIOCGSIZE but don't have a struct ttysize.
4078 */
4079# ifdef TIOCGWINSZ
4080 {
4081 struct winsize ws;
4082 int fd = 1;
4083
Bram Moolenaar0f873732019-12-05 20:28:46 +01004084 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 if (!isatty(fd) && isatty(read_cmd_fd))
4086 fd = read_cmd_fd;
4087 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4088 {
4089 columns = ws.ws_col;
4090 rows = ws.ws_row;
4091 }
4092 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004093# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094# ifdef TIOCGSIZE
4095 {
4096 struct ttysize ts;
4097 int fd = 1;
4098
Bram Moolenaar0f873732019-12-05 20:28:46 +01004099 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (!isatty(fd) && isatty(read_cmd_fd))
4101 fd = read_cmd_fd;
4102 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4103 {
4104 columns = ts.ts_cols;
4105 rows = ts.ts_lines;
4106 }
4107 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004108# endif // TIOCGSIZE
4109# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110
4111 /*
4112 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004113 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4114 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004116 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 {
4118 if ((p = (char_u *)getenv("LINES")))
4119 rows = atoi((char *)p);
4120 if ((p = (char_u *)getenv("COLUMNS")))
4121 columns = atoi((char *)p);
4122 }
4123
4124#ifdef HAVE_TGETENT
4125 /*
4126 * 3. try reading "co" and "li" entries from termcap
4127 */
4128 if (columns == 0 || rows == 0)
4129 getlinecol(&columns, &rows);
4130#endif
4131
4132 /*
4133 * 4. If everything fails, use the old values
4134 */
4135 if (columns <= 0 || rows <= 0)
4136 return FAIL;
4137
4138 Rows = rows;
4139 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004140 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 return OK;
4142}
4143
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004144#if defined(FEAT_TERMINAL) || defined(PROTO)
4145/*
4146 * Report the windows size "rows" and "cols" to tty "fd".
4147 */
4148 int
4149mch_report_winsize(int fd, int rows, int cols)
4150{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004151 int tty_fd;
4152 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004153
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004154 tty_fd = get_tty_fd(fd);
4155 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004156 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004157# if defined(TIOCSWINSZ)
4158 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004159
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004160 ws.ws_col = cols;
4161 ws.ws_row = rows;
4162 ws.ws_xpixel = cols * 5;
4163 ws.ws_ypixel = rows * 10;
4164 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4165 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4166 retval == 0 ? "success" : "failed");
4167# elif defined(TIOCSSIZE)
4168 struct ttysize ts;
4169
4170 ts.ts_cols = cols;
4171 ts.ts_lines = rows;
4172 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4173 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4174 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004175# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004176 if (tty_fd != fd)
4177 close(tty_fd);
4178 }
4179 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004180}
4181#endif
4182
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183/*
4184 * Try to set the window size to Rows and Columns.
4185 */
4186 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004187mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188{
4189 if (*T_CWS)
4190 {
4191 /*
4192 * NOTE: if you get an error here that term_set_winsize() is
4193 * undefined, check the output of configure. It could probably not
4194 * find a ncurses, termcap or termlib library.
4195 */
4196 term_set_winsize((int)Rows, (int)Columns);
4197 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004198 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200}
4201
Bram Moolenaar0f873732019-12-05 20:28:46 +01004202#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
4204/*
4205 * Rows and/or Columns has changed.
4206 */
4207 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004208mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004210 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211}
4212
Bram Moolenaar205b8862011-09-07 15:04:31 +02004213/*
4214 * Wait for process "child" to end.
4215 * Return "child" if it exited properly, <= 0 on error.
4216 */
4217 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004218wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004219{
4220 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004221 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004222
4223 while (wait_pid != child)
4224 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004225 // When compiled with Python threads are probably used, in which case
4226 // wait() sometimes hangs for no obvious reason. Use waitpid()
4227 // instead and loop (like the GUI). Also needed for other interfaces,
4228 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004229# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004230 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004231# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004232 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004233# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004234 if (wait_pid == 0)
4235 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004236 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004237 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004238 if (++delay_msec > 10)
4239 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004240 continue;
4241 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004242 if (wait_pid <= 0
4243# ifdef ECHILD
4244 && errno == ECHILD
4245# endif
4246 )
4247 break;
4248 }
4249 return wait_pid;
4250}
4251
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004252#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004253/*
4254 * Set the environment for a child process.
4255 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004256 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004257set_child_environment(
4258 long rows,
4259 long columns,
4260 char *term,
4261 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004262{
4263# ifdef HAVE_SETENV
4264 char envbuf[50];
4265# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004266 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004267 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004268 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004269 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004270 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004271# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004272 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004273# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004274# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004275 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004276# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004277# endif
4278
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004279# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004280 setenv("TERM", term, 1);
4281 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004282 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004283 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004284 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004285 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004286 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004287 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004288 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004289# ifdef FEAT_TERMINAL
4290 if (is_terminal)
4291 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004292 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004293 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4294 }
4295# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004296# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004297 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004298# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004299# else
4300 /*
4301 * Putenv does not copy the string, it has to remain valid.
4302 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004303 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004304 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004305 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4306 putenv(envbuf_Term);
4307 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004308 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004309 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4310 putenv(envbuf_Lines);
4311 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4312 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004313 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004314 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004315 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004316# ifdef FEAT_TERMINAL
4317 if (is_terminal)
4318 {
4319 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004320 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004321 putenv(envbuf_Version);
4322 }
4323# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004324# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004325 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4326 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4327 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004328# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004329# endif
4330}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004331
4332 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004333set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004334{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004335 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004336}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004337#endif
4338
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004339#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004340/*
4341 * Open a PTY, with FD for the master and slave side.
4342 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004343 * When successful both file descriptors are stored and the allocated pty name
4344 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004345 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004346 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004347open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004348{
4349 char *tty_name;
4350
Bram Moolenaar59386482019-02-10 22:43:46 +01004351 if (name1 != NULL)
4352 *name1 = NULL;
4353 if (name2 != NULL)
4354 *name2 = NULL;
4355
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004356 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004357 if (*pty_master_fd >= 0)
4358 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004359 // Leaving out O_NOCTTY may lead to waitpid() always returning
4360 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4361 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004362#ifdef O_NOCTTY
4363 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4364#else
4365 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4366#endif
4367 if (*pty_slave_fd < 0)
4368 {
4369 close(*pty_master_fd);
4370 *pty_master_fd = -1;
4371 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004372 else
4373 {
4374 if (name1 != NULL)
4375 *name1 = vim_strsave((char_u *)tty_name);
4376 if (name2 != NULL)
4377 *name2 = vim_strsave((char_u *)tty_name);
4378 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004379 }
4380}
4381#endif
4382
Bram Moolenaarfae42832017-08-01 22:24:26 +02004383/*
4384 * Send SIGINT to a child process if "c" is an interrupt character.
4385 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004386 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004387may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4388{
4389# ifdef SIGINT
4390 if (c == Ctrl_C || c == intr_char)
4391 {
4392# ifdef HAVE_SETSID
4393 kill(-pid, SIGINT);
4394# else
4395 kill(0, SIGINT);
4396# endif
4397 if (wpid > 0)
4398 kill(wpid, SIGINT);
4399 }
4400# endif
4401}
4402
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004403#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004404
Bram Moolenaar0f873732019-12-05 20:28:46 +01004405/*
4406 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4407 * pointers, the last one is NULL.
4408 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4409 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004410 int
4411unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004412 char_u *cmd,
4413 char ***argvp,
4414 char_u **sh_tofree,
4415 char_u **shcf_tofree)
4416{
4417 char **argv = NULL;
4418 int argc;
4419
4420 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004421 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004422 return FAIL;
4423
4424 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4425 return FAIL;
4426 *argvp = argv;
4427
4428 if (cmd != NULL)
4429 {
4430 char_u *s;
4431 char_u *p;
4432
4433 if (extra_shell_arg != NULL)
4434 argv[argc++] = (char *)extra_shell_arg;
4435
Bram Moolenaar0f873732019-12-05 20:28:46 +01004436 // Break 'shellcmdflag' into white separated parts. This doesn't
4437 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004438 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004439 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004440 return FAIL;
4441 s = *shcf_tofree;
4442 p = p_shcf;
4443 while (*p != NUL)
4444 {
4445 argv[argc++] = (char *)s;
4446 while (*p && *p != ' ' && *p != TAB)
4447 *s++ = *p++;
4448 *s++ = NUL;
4449 p = skipwhite(p);
4450 }
4451
4452 argv[argc++] = (char *)cmd;
4453 }
4454 argv[argc] = NULL;
4455 return OK;
4456}
4457#endif
4458
4459#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4460/*
4461 * Use a terminal window to run a shell command in.
4462 */
4463 static int
4464mch_call_shell_terminal(
4465 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004466 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004467{
4468 jobopt_T opt;
4469 char **argv = NULL;
4470 char_u *tofree1 = NULL;
4471 char_u *tofree2 = NULL;
4472 int retval = -1;
4473 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004474 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004475 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004476 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004477
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004478 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004479 goto theend;
4480
4481 init_job_options(&opt);
4482 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4483 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004484 if (buf == NULL)
4485 goto theend;
4486
4487 job = term_getjob(buf->b_term);
4488 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004489
Bram Moolenaar0f873732019-12-05 20:28:46 +01004490 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004491 aucmd_prepbuf(&aco, buf);
4492
4493 clear_oparg(&oa);
4494 while (term_use_loop())
4495 {
4496 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4497 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004498 // If terminal_loop() returns OK we got a key that is handled
4499 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004500 if (terminal_loop(TRUE) == OK)
4501 normal_cmd(&oa, TRUE);
4502 }
4503 else
4504 normal_cmd(&oa, TRUE);
4505 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004506 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004507 ch_log(NULL, "system command finished");
4508
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004509 job_unref(job);
4510
Bram Moolenaar0f873732019-12-05 20:28:46 +01004511 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004512 aucmd_restbuf(&aco);
4513
4514 wait_return(TRUE);
4515 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4516
4517theend:
4518 vim_free(argv);
4519 vim_free(tofree1);
4520 vim_free(tofree2);
4521 return retval;
4522}
4523#endif
4524
4525#ifdef USE_SYSTEM
4526/*
4527 * Use system() to start the shell: simple but slow.
4528 */
4529 static int
4530mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004531 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004532 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533{
4534#ifdef VMS
4535 char *ifn = NULL;
4536 char *ofn = NULL;
4537#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004538 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004539 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004540 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
4542 out_flush();
4543
4544 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004545 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546
Bram Moolenaar62b42182010-09-21 22:09:37 +02004547# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004548 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004549 loose_clipboard();
4550# endif
4551
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 if (cmd == NULL)
4553 x = system((char *)p_sh);
4554 else
4555 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004556# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (ofn = strchr((char *)cmd, '>'))
4558 *ofn++ = '\0';
4559 if (ifn = strchr((char *)cmd, '<'))
4560 {
4561 char *p;
4562
4563 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004564 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 if (p)
4566 *p = '\0';
4567 }
4568 if (ofn)
4569 x = vms_sys((char *)cmd, ofn, ifn);
4570 else
4571 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004572# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004573 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004575 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 if (newcmd == NULL)
4577 x = 0;
4578 else
4579 {
4580 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4581 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4582 (char *)p_shcf,
4583 (char *)cmd);
4584 x = system((char *)newcmd);
4585 vim_free(newcmd);
4586 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 }
4589# ifdef VMS
4590 x = vms_sys_status(x);
4591# endif
4592 if (emsg_silent)
4593 ;
4594 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004595 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 else if (x && !(options & SHELL_SILENT))
4597 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004598 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 msg_outnum((long)x);
4600 msg_putchar('\n');
4601 }
4602
4603 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004604 {
4605 // The shell may have messed with the mode, always set it.
4606 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004607 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004610# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4611 restore_clipboard();
4612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004614}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
Bram Moolenaar0f873732019-12-05 20:28:46 +01004616#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617
Bram Moolenaar0f873732019-12-05 20:28:46 +01004618# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4619 // 127, some shells use that already
4620# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
Bram Moolenaar13568252018-03-16 20:46:58 +01004622/*
4623 * Don't use system(), use fork()/exec().
4624 */
4625 static int
4626mch_call_shell_fork(
4627 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004628 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004629{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004630 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004632 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 pid_t wait_pid = 0;
4634# ifdef HAVE_UNION_WAIT
4635 union wait status;
4636# else
4637 int status = -1;
4638# endif
4639 int retval = -1;
4640 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004641 char_u *tofree1 = NULL;
4642 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004644 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004645# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004647# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004648 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 int fd_fromshell[2];
4650 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004651 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
4653 out_flush();
4654 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004655 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004656 if (tmode == TMODE_RAW)
4657 // The shell may have messed with the mode, always set it later.
4658 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004660 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004661 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004662
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004664 * For the GUI, when writing the output into the buffer and when reading
4665 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4666 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004668 if ((options & (SHELL_READ|SHELL_WRITE))
4669# ifdef FEAT_GUI
4670 || (gui.in_use && show_shell_mess)
4671# endif
4672 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004674# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 /*
4676 * Try to open a master pty.
4677 * If this works, open the slave pty.
4678 * If the slave can't be opened, close the master pty.
4679 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004680 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004681 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 /*
4683 * If not opening a pty or it didn't work, try using pipes.
4684 */
4685 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 {
4688 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004689 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
4691 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004692 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 {
4694 close(fd_toshell[0]);
4695 close(fd_toshell[1]);
4696 }
4697 }
4698 if (pipe_error)
4699 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004700 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 out_flush();
4702 }
4703 }
4704 }
4705
Bram Moolenaar0f873732019-12-05 20:28:46 +01004706 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004708 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004709 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004710 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004711 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004713 UNBLOCK_SIGNALS(&curset);
4714
Bram Moolenaar32526b32019-01-19 17:43:09 +01004715 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004716 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004718 || (gui.in_use && show_shell_mess)
4719# endif
4720 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004722# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004723 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
4725 close(pty_master_fd);
4726 close(pty_slave_fd);
4727 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004728 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004729# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
4731 close(fd_toshell[0]);
4732 close(fd_toshell[1]);
4733 close(fd_fromshell[0]);
4734 close(fd_fromshell[1]);
4735 }
4736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004738 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004740 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004741 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
Bram Moolenaar819524702018-02-27 19:10:00 +01004743# ifdef FEAT_JOB_CHANNEL
4744 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004745 {
4746 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004747 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004748 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004749# endif
4750
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 if (!show_shell_mess || (options & SHELL_EXPAND))
4752 {
4753 int fd;
4754
4755 /*
4756 * Don't want to show any message from the shell. Can't just
4757 * close stdout and stderr though, because some systems will
4758 * break if you try to write to them after that, so we must
4759 * use dup() to replace them with something else -- webb
4760 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4761 * waiting for input.
4762 */
4763 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4764 fclose(stdin);
4765 fclose(stdout);
4766 fclose(stderr);
4767
4768 /*
4769 * If any of these open()'s and dup()'s fail, we just continue
4770 * anyway. It's not fatal, and on most systems it will make
4771 * no difference at all. On a few it will cause the execvp()
4772 * to exit with a non-zero status even when the completion
4773 * could be done, which is nothing too serious. If the open()
4774 * or dup() failed we'd just do the same thing ourselves
4775 * anyway -- webb
4776 */
4777 if (fd >= 0)
4778 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004779 vim_ignored = dup(fd); // To replace stdin (fd 0)
4780 vim_ignored = dup(fd); // To replace stdout (fd 1)
4781 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782
Bram Moolenaar0f873732019-12-05 20:28:46 +01004783 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 close(fd);
4785 }
4786 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004787 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004789 || gui.in_use
4790# endif
4791 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 {
4793
Bram Moolenaardf177f62005-02-22 08:39:57 +00004794# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004795 // Create our own process group, so that the child and all its
4796 // children can be kill()ed. Don't do this when using pipes,
4797 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004798 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004799 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004800 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004801# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004802 // When doing "!xterm&" and 'shell' is bash: the shell
4803 // will exit and send SIGHUP to all processes in its
4804 // group, killing the just started process. Ignore SIGHUP
4805 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004806 signal(SIGHUP, SIG_IGN);
4807# endif
4808 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004809# endif
4810# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004811 if (pty_slave_fd >= 0)
4812 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004813 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004814 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004815 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004816# ifdef TIOCSCTTY
4817 // Try to become controlling tty (probably doesn't work,
4818 // unless run by root)
4819 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4820# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004821 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004822# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004823 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824
Bram Moolenaara5792f52005-11-23 21:25:05 +00004825 /*
4826 * stderr is only redirected when using the GUI, so that a
4827 * program like gpg can still access the terminal to get a
4828 * passphrase using stderr.
4829 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004830# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 if (pty_master_fd >= 0)
4832 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004833 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
Bram Moolenaar0f873732019-12-05 20:28:46 +01004835 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004837 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004839 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004840 if (gui.in_use)
4841 {
4842 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004843 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
Bram Moolenaar0f873732019-12-05 20:28:46 +01004846 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
4848 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004851 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 close(fd_toshell[1]);
4853 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004854 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 close(fd_toshell[0]);
4856
Bram Moolenaar0f873732019-12-05 20:28:46 +01004857 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 close(fd_fromshell[0]);
4859 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004860 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 close(fd_fromshell[1]);
4862
Bram Moolenaara5792f52005-11-23 21:25:05 +00004863# ifdef FEAT_GUI
4864 if (gui.in_use)
4865 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004866 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004867 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004868 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004869 }
4870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004873
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 /*
4875 * There is no type cast for the argv, because the type may be
4876 * different on different machines. This may cause a warning
4877 * message with strict compilers, don't worry about it.
4878 * Call _exit() instead of exit() to avoid closing the connection
4879 * to the X server (esp. with GTK, which uses atexit()).
4880 */
4881 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004882 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004884 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 {
4886 /*
4887 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004888 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 */
4890 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004891 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004892 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004893# ifdef FEAT_JOB_CHANNEL
4894 ++dont_check_job_ended;
4895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 /*
4897 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004898 * This is also used to pipe stdin/stdout to/from the external
4899 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004901 if ((options & (SHELL_READ|SHELL_WRITE))
4902# ifdef FEAT_GUI
4903 || (gui.in_use && show_shell_mess)
4904# endif
4905 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004907# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004909 int buffer_off = 0; // valid bytes in buffer[]
4910 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4911 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 int len;
4913 int p_more_save;
4914 int old_State;
4915 int c;
4916 int toshell_fd;
4917 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004918 garray_T ga;
4919 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004920# ifdef ELAPSED_FUNC
4921 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923
Bram Moolenaardf177f62005-02-22 08:39:57 +00004924# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 if (pty_master_fd >= 0)
4926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 fromshell_fd = pty_master_fd;
4928 toshell_fd = dup(pty_master_fd);
4929 }
4930 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004931# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 {
4933 close(fd_toshell[0]);
4934 close(fd_fromshell[1]);
4935 toshell_fd = fd_toshell[1];
4936 fromshell_fd = fd_fromshell[0];
4937 }
4938
4939 /*
4940 * Write to the child if there are typed characters.
4941 * Read from the child if there are characters available.
4942 * Repeat the reading a few times if more characters are
4943 * available. Need to check for typed keys now and then, but
4944 * not too often (delays when no chars are available).
4945 * This loop is quit if no characters can be read from the pty
4946 * (WaitForChar detected special condition), or there are no
4947 * characters available and the child has exited.
4948 * Only check if the child has exited when there is no more
4949 * output. The child may exit before all the output has
4950 * been printed.
4951 *
4952 * Currently this busy loops!
4953 * This can probably dead-lock when the write blocks!
4954 */
4955 p_more_save = p_more;
4956 p_more = FALSE;
4957 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004958 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004960 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004961 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004962 // Fork a process that will write the lines to the
4963 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004964 if ((wpid = fork()) == -1)
4965 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004966 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004967 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004968 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004969 {
4970 linenr_T lnum = curbuf->b_op_start.lnum;
4971 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004972 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004973 size_t l;
4974
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004975 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004976 for (;;)
4977 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004978 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004979 if (l == 0)
4980 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004981 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004982 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004983 len = write(toshell_fd, "", (size_t)1);
4984 else
4985 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004986 char_u *s = vim_strchr(lp + written, NL);
4987
Bram Moolenaar89d40322006-08-29 15:30:07 +00004988 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004989 s == NULL ? l
4990 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004991 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004992 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004993 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004994 // Finished a line, add a NL, unless this line
4995 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004996 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004997 || (!curbuf->b_p_bin
4998 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004999 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00005000 && (lnum !=
5001 curbuf->b_ml.ml_line_count
5002 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005003 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005004 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005005 ++lnum;
5006 if (lnum > curbuf->b_op_end.lnum)
5007 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005008 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005009 close(toshell_fd);
5010 toshell_fd = -1;
5011 break;
5012 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005013 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005014 written = 0;
5015 }
5016 else if (len > 0)
5017 written += len;
5018 }
5019 _exit(0);
5020 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005021 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005022 {
5023 close(toshell_fd);
5024 toshell_fd = -1;
5025 }
5026 }
5027
5028 if (options & SHELL_READ)
5029 ga_init2(&ga, 1, BUFLEN);
5030
5031 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005032# ifdef ELAPSED_FUNC
5033 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 for (;;)
5036 {
5037 /*
5038 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005039 * if there are any.
5040 * Don't do this if we are expanding wild cards (would eat
5041 * typeahead).
5042 * Don't do this when filtering and terminal is in cooked
5043 * mode, the shell command will handle the I/O. Avoids
5044 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005045 * Don't get characters when the child has already
5046 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005047 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005048 * while (noread_cnt > 4), avoids that ":r !ls" eats
5049 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 */
5051 len = 0;
5052 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005053 && ((options &
5054 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5055 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005056# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005057 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005058# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005059 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005060 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005061 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005063 if (ta_len == 0)
5064 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005065 // Get extra characters when we don't have any.
5066 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005067 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005068# ifdef ELAPSED_FUNC
5069 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005070# endif
5071 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5072 }
5073 if (ta_len > 0 || len > 0)
5074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 /*
5076 * For pipes:
5077 * Check for CTRL-C: send interrupt signal to child.
5078 * Check for CTRL-D: EOF, close pipe to child.
5079 */
5080 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5081 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 /*
5083 * Send SIGINT to the child's group or all
5084 * processes in our group.
5085 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005086 may_send_sigint(ta_buf[ta_len], pid, wpid);
5087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 if (pty_master_fd < 0 && toshell_fd >= 0
5089 && ta_buf[ta_len] == Ctrl_D)
5090 {
5091 close(toshell_fd);
5092 toshell_fd = -1;
5093 }
5094 }
5095
Bram Moolenaarf4140482020-02-15 23:06:45 +01005096 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097
5098 /*
5099 * For pipes: echo the typed characters.
5100 * For a pty this does not seem to work.
5101 */
5102 if (pty_master_fd < 0)
5103 {
5104 for (i = ta_len; i < ta_len + len; ++i)
5105 {
5106 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5107 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 else if (has_mbyte)
5109 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005110 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
5112 msg_outtrans_len(ta_buf + i, l);
5113 i += l - 1;
5114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 else
5116 msg_outtrans_len(ta_buf + i, 1);
5117 }
5118 windgoto(msg_row, msg_col);
5119 out_flush();
5120 }
5121
5122 ta_len += len;
5123
5124 /*
5125 * Write the characters to the child, unless EOF has
5126 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005127 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005128 * When writing buffer lines, drop the typed
5129 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005131 if (options & SHELL_WRITE)
5132 ta_len = 0;
5133 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
5135 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5136 if (len > 0)
5137 {
5138 ta_len -= len;
5139 mch_memmove(ta_buf, ta_buf + len, ta_len);
5140 }
5141 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
5144
Bram Moolenaardf177f62005-02-22 08:39:57 +00005145 if (got_int)
5146 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005147 // CTRL-C sends a signal to the child, we ignore it
5148 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005149# ifdef HAVE_SETSID
5150 kill(-pid, SIGINT);
5151# else
5152 kill(0, SIGINT);
5153# endif
5154 if (wpid > 0)
5155 kill(wpid, SIGINT);
5156 got_int = FALSE;
5157 }
5158
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 /*
5160 * Check if the child has any characters to be printed.
5161 * Read them and write them to our window. Repeat this as
5162 * long as there is something to do, avoid the 10ms wait
5163 * for mch_inchar(), or sending typeahead characters to
5164 * the external process.
5165 * TODO: This should handle escape sequences, compatible
5166 * to some terminal (vt52?).
5167 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005168 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005169 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005171 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005174 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005176
5177 noread_cnt = 0;
5178 if (options & SHELL_READ)
5179 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005180 // Do NUL -> NL translation, append NL separated
5181 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005182 for (i = 0; i < len; ++i)
5183 {
5184 if (buffer[i] == NL)
5185 append_ga_line(&ga);
5186 else if (buffer[i] == NUL)
5187 ga_append(&ga, NL);
5188 else
5189 ga_append(&ga, buffer[i]);
5190 }
5191 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005192 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
5194 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005195 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196
Bram Moolenaardf177f62005-02-22 08:39:57 +00005197 len += buffer_off;
5198 buffer[len] = NUL;
5199
Bram Moolenaar0f873732019-12-05 20:28:46 +01005200 // Check if the last character in buffer[] is
5201 // incomplete, keep these bytes for the next
5202 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 for (p = buffer; p < buffer + len; p += l)
5204 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005205 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005207 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 else if (MB_BYTE2LEN(*p) != l)
5209 break;
5210 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005211 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005213 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (len >= 12)
5215 ++p;
5216 else
5217 {
5218 buffer_off = len;
5219 continue;
5220 }
5221 }
5222 c = *p;
5223 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005224 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 if (p < buffer + len)
5226 {
5227 *p = c;
5228 buffer_off = (buffer + len) - p;
5229 mch_memmove(buffer, p, buffer_off);
5230 continue;
5231 }
5232 buffer_off = 0;
5233 }
5234 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 {
5236 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005237 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
5239
5240 windgoto(msg_row, msg_col);
5241 cursor_on();
5242 out_flush();
5243 if (got_int)
5244 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005245
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005246# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005247 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005248 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005249 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005250
Bram Moolenaar0f873732019-12-05 20:28:46 +01005251 // Avoid that we keep looping here without
5252 // checking for a CTRL-C for a long time. Don't
5253 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005254 if (msec > 2000)
5255 {
5256 noread_cnt = 5;
5257 break;
5258 }
5259 }
5260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 }
5262
Bram Moolenaar0f873732019-12-05 20:28:46 +01005263 // If we already detected the child has finished, continue
5264 // reading output for a short while. Some text may be
5265 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005266 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005267 {
5268 if (noread_cnt < 5)
5269 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005270 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005271 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005272
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 /*
5274 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005275 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005277# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005278 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005279# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005281# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5283 || (wait_pid == pid && WIFEXITED(status)))
5284 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005285 // Don't break the loop yet, try reading more
5286 // characters from "fromshell_fd" first. When using
5287 // pipes there might still be something to read and
5288 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005291 else
5292 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005293
Bram Moolenaar95a51352013-03-21 22:53:50 +01005294# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005295 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005296 clip_update();
5297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 }
5299finished:
5300 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005301 if (options & SHELL_READ)
5302 {
5303 if (ga.ga_len > 0)
5304 {
5305 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005306 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005307 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005308 }
5309 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005310 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005311 ga_clear(&ga);
5312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 /*
5315 * Give all typeahead that wasn't used back to ui_inchar().
5316 */
5317 if (ta_len)
5318 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 State = old_State;
5320 if (toshell_fd >= 0)
5321 close(toshell_fd);
5322 close(fromshell_fd);
5323 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005324# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005325 else
5326 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005327 long delay_msec = 1;
5328
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005329 if (tmode == TMODE_RAW)
5330 // possibly disables modifyOtherKeys, so that the system
5331 // can recognize CTRL-C
5332 out_str(T_CTE);
Bram Moolenaar0981c872020-08-23 14:28:37 +02005333
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005334 /*
5335 * Similar to the loop above, but only handle X events, no
5336 * I/O.
5337 */
5338 for (;;)
5339 {
5340 if (got_int)
5341 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005342 // CTRL-C sends a signal to the child, we ignore it
5343 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005344# ifdef HAVE_SETSID
5345 kill(-pid, SIGINT);
5346# else
5347 kill(0, SIGINT);
5348# endif
5349 got_int = FALSE;
5350 }
5351# ifdef __NeXT__
5352 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5353# else
5354 wait_pid = waitpid(pid, &status, WNOHANG);
5355# endif
5356 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5357 || (wait_pid == pid && WIFEXITED(status)))
5358 {
5359 wait_pid = pid;
5360 break;
5361 }
5362
Bram Moolenaar0f873732019-12-05 20:28:46 +01005363 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005364 clip_update();
5365
Bram Moolenaar0f873732019-12-05 20:28:46 +01005366 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005367 // less time, gradually wait longer.
5368 mch_delay(delay_msec,
5369 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005370 if (++delay_msec > 10)
5371 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005372 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005373
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005374 if (tmode == TMODE_RAW)
5375 // possibly enables modifyOtherKeys again
5376 out_str(T_CTI);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005377 }
5378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379
5380 /*
5381 * Wait until our child has exited.
5382 * Ignore wait() returning pids of other children and returning
5383 * because of some signal like SIGWINCH.
5384 * Don't wait if wait_pid was already set above, indicating the
5385 * child already exited.
5386 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005387 if (wait_pid != pid)
5388 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389
Bram Moolenaar624891f2010-10-13 16:22:09 +02005390# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005391 // Close slave side of pty. Only do this after the child has
5392 // exited, otherwise the child may hang when it tries to write on
5393 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005394 if (pty_master_fd >= 0)
5395 close(pty_slave_fd);
5396# endif
5397
Bram Moolenaar0f873732019-12-05 20:28:46 +01005398 // Make sure the child that writes to the external program is
5399 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005400 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005401 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005402 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005403 wait4pid(wpid, NULL);
5404 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005405
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005406# ifdef FEAT_JOB_CHANNEL
5407 --dont_check_job_ended;
5408# endif
5409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 /*
5411 * Set to raw mode right now, otherwise a CTRL-C after
5412 * catch_signals() will kill Vim.
5413 */
5414 if (tmode == TMODE_RAW)
5415 settmode(TMODE_RAW);
5416 did_settmode = TRUE;
5417 set_signals();
5418
5419 if (WIFEXITED(status))
5420 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005421 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005423 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 {
5425 if (retval == EXEC_FAILED)
5426 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005427 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 msg_outtrans(p_sh);
5429 msg_putchar('\n');
5430 }
5431 else if (!(options & SHELL_SILENT))
5432 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005433 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 msg_outnum((long)retval);
5435 msg_putchar('\n');
5436 }
5437 }
5438 }
5439 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005440 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 }
5442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
5444error:
5445 if (!did_settmode)
5446 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005447 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005449 vim_free(argv);
5450 vim_free(tofree1);
5451 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452
5453 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005454}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005455#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005456
5457 int
5458mch_call_shell(
5459 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005460 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005461{
5462#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5463 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5464 return mch_call_shell_terminal(cmd, options);
5465#endif
5466#ifdef USE_SYSTEM
5467 return mch_call_shell_system(cmd, options);
5468#else
5469 return mch_call_shell_fork(cmd, options);
5470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471}
5472
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005473#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005474 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005475mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005476{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005477 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005478 int fd_in[2] = {-1, -1}; // for stdin
5479 int fd_out[2] = {-1, -1}; // for stdout
5480 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005481 int pty_master_fd = -1;
5482 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005483 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005484 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5485 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5486 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005487 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005488 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5489 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005490 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005491 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005492 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005493
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005494 if (use_out_for_err && use_null_for_out)
5495 use_null_for_err = TRUE;
5496
Bram Moolenaar0f873732019-12-05 20:28:46 +01005497 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005498 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005499
Bram Moolenaarb2412082017-08-20 18:09:14 +02005500 if (options->jo_pty
5501 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005502 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005503 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005504 open_pty(&pty_master_fd, &pty_slave_fd,
5505 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005506
Bram Moolenaar0f873732019-12-05 20:28:46 +01005507 // TODO: without the channel feature connect the child to /dev/null?
5508 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005509 if (use_file_for_in)
5510 {
5511 char_u *fname = options->jo_io_name[PART_IN];
5512
5513 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5514 if (fd_in[0] < 0)
5515 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005516 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005517 goto failed;
5518 }
5519 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005520 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005521 // When writing buffer lines to the input don't use the pty, so that
5522 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005523 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5524 && pipe(fd_in) < 0)
5525 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005526
5527 if (use_file_for_out)
5528 {
5529 char_u *fname = options->jo_io_name[PART_OUT];
5530
5531 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5532 if (fd_out[1] < 0)
5533 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005534 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005535 goto failed;
5536 }
5537 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005538 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005539 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005540
5541 if (use_file_for_err)
5542 {
5543 char_u *fname = options->jo_io_name[PART_ERR];
5544
5545 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5546 if (fd_err[1] < 0)
5547 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005548 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005549 goto failed;
5550 }
5551 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005552 else if (!use_out_for_err && !use_null_for_err
5553 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005554 goto failed;
5555
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005556 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5557 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005558 if (options->jo_set & JO_CHANNEL)
5559 {
5560 channel = options->jo_channel;
5561 if (channel != NULL)
5562 ++channel->ch_refcount;
5563 }
5564 else
5565 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005566 if (channel == NULL)
5567 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005568 if (job->jv_tty_out != NULL)
5569 ch_log(channel, "using pty %s on fd %d",
5570 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005571 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005572
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005573 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005574 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005575 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005576 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005577 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005578 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005579 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005580 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005581 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005582 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005583 int null_fd = -1;
5584 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005585
Bram Moolenaar0f873732019-12-05 20:28:46 +01005586 // child
5587 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005588 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005589
Bram Moolenaar819524702018-02-27 19:10:00 +01005590# ifdef FEAT_JOB_CHANNEL
5591 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005592 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005593 ch_logfile((char_u *)"", (char_u *)"");
5594# endif
5595
Bram Moolenaar835dc632016-02-07 14:27:38 +01005596# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005597 // Create our own process group, so that the child and all its
5598 // children can be kill()ed. Don't do this when using pipes,
5599 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005600 (void)setsid();
5601# endif
5602
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005603# ifdef FEAT_TERMINAL
5604 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005605 {
5606 char *term = (char *)T_NAME;
5607
5608#ifdef FEAT_GUI
5609 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005610 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005611 term = getenv("TERM");
5612#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005613 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005614 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005615 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005616 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005617 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005618 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005619 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005620 else if (t_colors > 16)
5621 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005622 else
5623 term = "xterm";
5624 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005625 set_child_environment(
5626 (long)options->jo_term_rows,
5627 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005628 term,
5629 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005630 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005631 else
5632# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005633 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005634
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005635 if (options->jo_env != NULL)
5636 {
5637 dict_T *dict = options->jo_env;
5638 hashitem_T *hi;
5639 int todo = (int)dict->dv_hashtab.ht_used;
5640
5641 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5642 if (!HASHITEM_EMPTY(hi))
5643 {
5644 typval_T *item = &dict_lookup(hi)->di_tv;
5645
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005646 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005647 --todo;
5648 }
5649 }
5650
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005651 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005652 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005653 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005654 if (null_fd < 0)
5655 {
5656 perror("opening /dev/null failed");
5657 _exit(OPEN_NULL_FAILED);
5658 }
5659 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005660
Bram Moolenaar223896d2017-08-02 22:33:28 +02005661 if (pty_slave_fd >= 0)
5662 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005663 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005664 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005665# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005666 // Try to become controlling tty (probably doesn't work,
5667 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005668 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5669# endif
5670 }
5671
Bram Moolenaar0f873732019-12-05 20:28:46 +01005672 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005673 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005674 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005675 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005676 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005677 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005678 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005679 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005680
Bram Moolenaar0f873732019-12-05 20:28:46 +01005681 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005682 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005683 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005684 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005685 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005686 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005687 }
5688 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005689 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005690 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005691 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005692 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005693 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005694
Bram Moolenaar0f873732019-12-05 20:28:46 +01005695 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005696 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005697 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005698 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005699 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005700 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005701 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005702 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005703
5704 if (fd_in[0] >= 0)
5705 close(fd_in[0]);
5706 if (fd_in[1] >= 0)
5707 close(fd_in[1]);
5708 if (fd_out[0] >= 0)
5709 close(fd_out[0]);
5710 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005711 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005712 if (fd_err[0] >= 0)
5713 close(fd_err[0]);
5714 if (fd_err[1] >= 0)
5715 close(fd_err[1]);
5716 if (pty_master_fd >= 0)
5717 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005718 close(pty_master_fd); // not used in the child
5719 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005720 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005721
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005722 if (null_fd >= 0)
5723 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005724
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005725 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5726 _exit(EXEC_FAILED);
5727
Bram Moolenaar0f873732019-12-05 20:28:46 +01005728 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005729 execvp(argv[0], argv);
5730
Bram Moolenaar4694a172016-04-21 14:05:23 +02005731 if (stderr_works)
5732 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005733# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005734 // calling free_all_mem() here causes problems. Ignore valgrind
5735 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005736# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005737 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005738 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005739
Bram Moolenaar0f873732019-12-05 20:28:46 +01005740 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005741 UNBLOCK_SIGNALS(&curset);
5742
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005743 job->jv_pid = pid;
5744 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005745 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005746
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005747 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005748 close(pty_slave_fd); // not used in the parent
5749 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005750 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005751 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005752 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005753 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005754 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005755 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005756 if (channel != NULL)
5757 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005758 int in_fd = INVALID_FD;
5759 int out_fd = INVALID_FD;
5760 int err_fd = INVALID_FD;
5761
5762 if (!(use_file_for_in || use_null_for_in))
5763 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5764
5765 if (!(use_file_for_out || use_null_for_out))
5766 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5767
5768 // When using pty_master_fd only set it for stdout, do not duplicate
5769 // it for stderr, it only needs to be read once.
5770 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5771 {
5772 if (fd_err[0] >= 0)
5773 err_fd = fd_err[0];
5774 else if (out_fd != pty_master_fd)
5775 err_fd = pty_master_fd;
5776 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005777
5778 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005779 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005780 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005781 else
5782 {
5783 if (fd_in[1] >= 0)
5784 close(fd_in[1]);
5785 if (fd_out[0] >= 0)
5786 close(fd_out[0]);
5787 if (fd_err[0] >= 0)
5788 close(fd_err[0]);
5789 if (pty_master_fd >= 0)
5790 close(pty_master_fd);
5791 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005792
Bram Moolenaar0f873732019-12-05 20:28:46 +01005793 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005794 return;
5795
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005796failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005797 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005798 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005799 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005800 if (fd_in[1] >= 0)
5801 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005802 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005803 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005804 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005805 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005806 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005807 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005808 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005809 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005810 if (pty_master_fd >= 0)
5811 close(pty_master_fd);
5812 if (pty_slave_fd >= 0)
5813 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005814}
5815
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005816 static char_u *
5817get_signal_name(int sig)
5818{
5819 int i;
5820 char_u numbuf[NUMBUFLEN];
5821
5822 if (sig == SIGKILL)
5823 return vim_strsave((char_u *)"kill");
5824
5825 for (i = 0; signal_info[i].sig != -1; i++)
5826 if (sig == signal_info[i].sig)
5827 return strlow_save((char_u *)signal_info[i].name);
5828
5829 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5830 return vim_strsave(numbuf);
5831}
5832
Bram Moolenaar835dc632016-02-07 14:27:38 +01005833 char *
5834mch_job_status(job_T *job)
5835{
5836# ifdef HAVE_UNION_WAIT
5837 union wait status;
5838# else
5839 int status = -1;
5840# endif
5841 pid_t wait_pid = 0;
5842
5843# ifdef __NeXT__
5844 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5845# else
5846 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5847# endif
5848 if (wait_pid == -1)
5849 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005850 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005851 if (job->jv_status < JOB_ENDED)
5852 ch_log(job->jv_channel, "Job no longer exists: %s",
5853 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005854 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005855 }
5856 if (wait_pid == 0)
5857 return "run";
5858 if (WIFEXITED(status))
5859 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005860 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005861 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005862 if (job->jv_status < JOB_ENDED)
5863 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005864 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005865 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005866 if (WIFSIGNALED(status))
5867 {
5868 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005869 job->jv_termsig = get_signal_name(WTERMSIG(status));
5870 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5871 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5872 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005873 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005874 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005875 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005876
5877return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005878 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005879 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005880 return "dead";
5881}
5882
5883 job_T *
5884mch_detect_ended_job(job_T *job_list)
5885{
5886# ifdef HAVE_UNION_WAIT
5887 union wait status;
5888# else
5889 int status = -1;
5890# endif
5891 pid_t wait_pid = 0;
5892 job_T *job;
5893
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005894# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005895 // Do not do this when waiting for a shell command to finish, we would get
5896 // the exit value here (and discard it), the exit value obtained there
5897 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005898 if (dont_check_job_ended > 0)
5899 return NULL;
5900# endif
5901
Bram Moolenaar97792de2016-10-15 18:36:49 +02005902# ifdef __NeXT__
5903 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5904# else
5905 wait_pid = waitpid(-1, &status, WNOHANG);
5906# endif
5907 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005908 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005909 return NULL;
5910 for (job = job_list; job != NULL; job = job->jv_next)
5911 {
5912 if (job->jv_pid == wait_pid)
5913 {
5914 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005915 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005916 job->jv_exitval = WEXITSTATUS(status);
5917 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005918 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005919 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005920 job->jv_termsig = get_signal_name(WTERMSIG(status));
5921 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005922 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005923 {
5924 ch_log(job->jv_channel, "Job ended");
5925 job->jv_status = JOB_ENDED;
5926 }
5927 return job;
5928 }
5929 }
5930 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005931}
5932
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005933/*
5934 * Send a (deadly) signal to "job".
5935 * Return FAIL if "how" is not a valid name.
5936 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005937 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005938mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005939{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005940 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005941
Bram Moolenaar923d9262016-02-25 20:56:01 +01005942 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005943 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005944 else if (STRCMP(how, "hup") == 0)
5945 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005946 else if (STRCMP(how, "quit") == 0)
5947 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005948 else if (STRCMP(how, "int") == 0)
5949 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005950 else if (STRCMP(how, "kill") == 0)
5951 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005952#ifdef SIGWINCH
5953 else if (STRCMP(how, "winch") == 0)
5954 sig = SIGWINCH;
5955#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005956 else if (isdigit(*how))
5957 sig = atoi((char *)how);
5958 else
5959 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005960
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005961 // Never kill ourselves!
5962 if (job->jv_pid != 0)
5963 {
5964 // TODO: have an option to only kill the process, not the group?
5965 kill(-job->jv_pid, sig);
5966 kill(job->jv_pid, sig);
5967 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005968
Bram Moolenaar835dc632016-02-07 14:27:38 +01005969 return OK;
5970}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005971
5972/*
5973 * Clear the data related to "job".
5974 */
5975 void
5976mch_clear_job(job_T *job)
5977{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005978 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005979# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005980 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005981# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005982 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005983# endif
5984}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005985#endif
5986
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005987#if defined(FEAT_TERMINAL) || defined(PROTO)
5988 int
5989mch_create_pty_channel(job_T *job, jobopt_T *options)
5990{
5991 int pty_master_fd = -1;
5992 int pty_slave_fd = -1;
5993 channel_T *channel;
5994
Bram Moolenaar59386482019-02-10 22:43:46 +01005995 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02005996 if (pty_master_fd < 0 || pty_slave_fd < 0)
5997 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005998 close(pty_slave_fd);
5999
6000 channel = add_channel();
6001 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006002 {
6003 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006004 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006005 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006006 if (job->jv_tty_out != NULL)
6007 ch_log(channel, "using pty %s on fd %d",
6008 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006009 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006010 channel->ch_keep_open = TRUE;
6011
Bram Moolenaar0f873732019-12-05 20:28:46 +01006012 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6013 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006014 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006015 channel_set_job(channel, job, options);
6016 return OK;
6017}
6018#endif
6019
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020/*
6021 * Check for CTRL-C typed by reading all available characters.
6022 * In cooked mode we should get SIGINT, no need to check.
6023 */
6024 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006025mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006027 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006028 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 fill_input_buf(FALSE);
6030}
6031
6032/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006033 * Wait "msec" msec until a character is available from the mouse, keyboard,
6034 * from inbuf[].
6035 * "msec" == -1 will block forever.
6036 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006037 * When "ignore_input" is TRUE even check for pending input when input is
6038 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006039 * "interrupted" (if not NULL) is set to TRUE when no character is available
6040 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006041 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006042 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 */
6044 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006045WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006047#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006048 return ui_wait_for_chars_or_timer(
6049 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006050#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006051 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006052#endif
6053}
6054
6055/*
6056 * Wait "msec" msec until a character is available from the mouse or keyboard
6057 * or from inbuf[].
6058 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006059 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006060 * "interrupted" (if not NULL) is set to TRUE when no character is available
6061 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006062 * When a GUI is being used, this will never get called -- webb
6063 */
6064 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006065WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006066{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067#ifdef FEAT_MOUSE_GPM
6068 int gpm_process_wanted;
6069#endif
6070#ifdef FEAT_XCLIPBOARD
6071 int rest;
6072#endif
6073 int avail;
6074
Bram Moolenaar0f873732019-12-05 20:28:46 +01006075 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 return 1;
6077
6078#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006079 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 if (WantQueryMouse)
6081 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006082 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006083 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006084 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 }
6086#endif
6087
6088 /*
6089 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6090 * events. This is a bit complicated, because they might both be defined.
6091 */
6092#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6093# ifdef FEAT_XCLIPBOARD
6094 rest = 0;
6095 if (do_xterm_trace())
6096 rest = msec;
6097# endif
6098 do
6099 {
6100# ifdef FEAT_XCLIPBOARD
6101 if (rest != 0)
6102 {
6103 msec = XT_TRACE_DELAY;
6104 if (rest >= 0 && rest < XT_TRACE_DELAY)
6105 msec = rest;
6106 if (rest >= 0)
6107 rest -= msec;
6108 }
6109# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006110# ifdef FEAT_SOUND_CANBERRA
6111 // Invoke any pending sound callbacks.
6112 if (has_sound_callback_in_queue())
6113 invoke_sound_callback();
6114# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115# ifdef FEAT_MOUSE_GPM
6116 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006117 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006118 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006119 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006121 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006125 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 return 1;
6127# ifdef FEAT_XCLIPBOARD
6128 if (rest == 0 || !do_xterm_trace())
6129# endif
6130 break;
6131 }
6132 }
6133 while (FALSE
6134# ifdef FEAT_MOUSE_GPM
6135 || (gpm_process_wanted && mch_gpm_process() == 0)
6136# endif
6137# ifdef FEAT_XCLIPBOARD
6138 || (!avail && rest != 0)
6139# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006140 )
6141 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
6143#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006144 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145#endif
6146 return avail;
6147}
6148
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006149#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150/*
6151 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006152 * "msec" == 0 will check for characters once.
6153 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006156 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006157 * "interrupted" (if not NULL) is set to TRUE when no character is available
6158 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006160 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006161RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162{
6163 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006164 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006165#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 static int busy = FALSE;
6167
Bram Moolenaar0f873732019-12-05 20:28:46 +01006168 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169# define MAY_LOOP
6170
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006171# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006172 // Remember at what time we started, so that we know how much longer we
6173 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006174 long start_msec = msec;
6175 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006177 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006178 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179# endif
6180
Bram Moolenaar0f873732019-12-05 20:28:46 +01006181 // Handle being called recursively. This may happen for the session
6182 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (busy)
6184 return 0;
6185#endif
6186
6187#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006188 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189#endif
6190 {
6191#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006192 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006193# ifdef FEAT_MZSCHEME
6194 int mzquantum_used = FALSE;
6195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196#endif
6197#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006198 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006199 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 int nfd;
6201# ifdef FEAT_XCLIPBOARD
6202 int xterm_idx = -1;
6203# endif
6204# ifdef FEAT_MOUSE_GPM
6205 int gpm_idx = -1;
6206# endif
6207# ifdef USE_XSMP
6208 int xsmp_idx = -1;
6209# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006210 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006212# ifdef FEAT_MZSCHEME
6213 mzvim_check_threads();
6214 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6215 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006216 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006217 mzquantum_used = TRUE;
6218 }
6219# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 fds[0].fd = fd;
6221 fds[0].events = POLLIN;
6222 nfd = 1;
6223
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006225 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 if (xterm_Shell != (Widget)0)
6227 {
6228 xterm_idx = nfd;
6229 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6230 fds[nfd].events = POLLIN;
6231 nfd++;
6232 }
6233# endif
6234# ifdef FEAT_MOUSE_GPM
6235 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6236 {
6237 gpm_idx = nfd;
6238 fds[nfd].fd = gpm_fd;
6239 fds[nfd].events = POLLIN;
6240 nfd++;
6241 }
6242# endif
6243# ifdef USE_XSMP
6244 if (xsmp_icefd != -1)
6245 {
6246 xsmp_idx = nfd;
6247 fds[nfd].fd = xsmp_icefd;
6248 fds[nfd].events = POLLIN;
6249 nfd++;
6250 }
6251# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006252#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006253 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006254#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006255 if (interrupted != NULL)
6256 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006258 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006259
6260 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006261 if (result == 0 && interrupted != NULL && ret > 0)
6262 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006263
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006264# ifdef FEAT_MZSCHEME
6265 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006266 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006267 finished = FALSE;
6268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270# ifdef FEAT_XCLIPBOARD
6271 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6272 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006273 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006275 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 finished = FALSE;
6277 }
6278# endif
6279# ifdef FEAT_MOUSE_GPM
6280 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282# endif
6283# ifdef USE_XSMP
6284 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6285 {
6286 if (fds[xsmp_idx].revents & POLLIN)
6287 {
6288 busy = TRUE;
6289 xsmp_handle_requests();
6290 busy = FALSE;
6291 }
6292 else if (fds[xsmp_idx].revents & POLLHUP)
6293 {
6294 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006295 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 xsmp_close();
6297 }
6298 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006299 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 }
6301# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006302#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006303 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006304 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006305 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307
Bram Moolenaar0f873732019-12-05 20:28:46 +01006308#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
6310 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006311 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006312 // These are static because they can take 8 Kbyte each and cause the
6313 // signal stack to run out with -O3.
6314 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006316 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006318# ifdef FEAT_MZSCHEME
6319 mzvim_check_threads();
6320 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6321 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006322 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006323 mzquantum_used = TRUE;
6324 }
6325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006327 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006329 tv.tv_sec = towait / 1000;
6330 tv.tv_usec = (towait % 1000) * (1000000/1000);
6331 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006333 else
6334 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335
6336 /*
6337 * Select on ready for reading and exceptional condition (end of file).
6338 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006339select_eintr:
6340 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006341 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 FD_ZERO(&efds);
6343 FD_SET(fd, &rfds);
6344# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006345 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 FD_SET(fd, &efds);
6347# endif
6348 maxfd = fd;
6349
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006351 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 if (xterm_Shell != (Widget)0)
6353 {
6354 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6355 if (maxfd < ConnectionNumber(xterm_dpy))
6356 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006357
Bram Moolenaar0f873732019-12-05 20:28:46 +01006358 // An event may have already been read but not handled. In
6359 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006360 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 }
6362# endif
6363# ifdef FEAT_MOUSE_GPM
6364 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6365 {
6366 FD_SET(gpm_fd, &rfds);
6367 FD_SET(gpm_fd, &efds);
6368 if (maxfd < gpm_fd)
6369 maxfd = gpm_fd;
6370 }
6371# endif
6372# ifdef USE_XSMP
6373 if (xsmp_icefd != -1)
6374 {
6375 FD_SET(xsmp_icefd, &rfds);
6376 FD_SET(xsmp_icefd, &efds);
6377 if (maxfd < xsmp_icefd)
6378 maxfd = xsmp_icefd;
6379 }
6380# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006381# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006382 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006383# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006384 if (interrupted != NULL)
6385 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386
Bram Moolenaar643b6142018-09-12 20:29:09 +02006387 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6388 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006389 result = ret > 0 && FD_ISSET(fd, &rfds);
6390 if (result)
6391 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006392 else if (interrupted != NULL && ret > 0)
6393 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006394
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006395# ifdef EINTR
6396 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006397 {
dbivolaruab16ad32021-12-29 19:41:47 +00006398 // Check whether the EINTR is caused by SIGTSTP
6399 if (got_tstp && !in_mch_suspend)
6400 {
6401 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006402
dbivolaruab16ad32021-12-29 19:41:47 +00006403 ea.forceit = TRUE;
6404 ex_stop(&ea);
6405 got_tstp = FALSE;
6406 }
6407
Bram Moolenaar0f873732019-12-05 20:28:46 +01006408 // Check whether window has been resized, EINTR may be caused by
6409 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006410 if (do_resize)
6411 handle_resize();
6412
Bram Moolenaar0f873732019-12-05 20:28:46 +01006413 // Interrupted by a signal, need to try again. We ignore msec
6414 // here, because we do want to check even after a timeout if
6415 // characters are available. Needed for reading output of an
6416 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006417 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006418 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006419# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006420# ifdef __TANDEM
6421 if (ret == -1 && errno == ENOTSUP)
6422 {
6423 FD_ZERO(&rfds);
6424 FD_ZERO(&efds);
6425 ret = 0;
6426 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006427# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006428# ifdef FEAT_MZSCHEME
6429 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006430 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006431 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432# endif
6433
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434# ifdef FEAT_XCLIPBOARD
6435 if (ret > 0 && xterm_Shell != (Widget)0
6436 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6437 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006438 xterm_update(); // Maybe we should hand out clipboard
6439 // continue looping when we only got the X event and the input
6440 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if (--ret == 0 && !input_available())
6442 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006443 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 finished = FALSE;
6445 }
6446 }
6447# endif
6448# ifdef FEAT_MOUSE_GPM
6449 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6450 {
6451 if (FD_ISSET(gpm_fd, &efds))
6452 gpm_close();
6453 else if (FD_ISSET(gpm_fd, &rfds))
6454 *check_for_gpm = 1;
6455 }
6456# endif
6457# ifdef USE_XSMP
6458 if (ret > 0 && xsmp_icefd != -1)
6459 {
6460 if (FD_ISSET(xsmp_icefd, &efds))
6461 {
6462 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006463 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 xsmp_close();
6465 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006466 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 }
6468 else if (FD_ISSET(xsmp_icefd, &rfds))
6469 {
6470 busy = TRUE;
6471 xsmp_handle_requests();
6472 busy = FALSE;
6473 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006474 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 }
6476 }
6477# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006478#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006479 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006480 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006481 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483
Bram Moolenaar0f873732019-12-05 20:28:46 +01006484#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
6486#ifdef MAY_LOOP
6487 if (finished || msec == 0)
6488 break;
6489
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006490# ifdef FEAT_CLIENTSERVER
6491 if (server_waiting())
6492 break;
6493# endif
6494
Bram Moolenaar0f873732019-12-05 20:28:46 +01006495 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 if (msec > 0)
6497 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006498# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006499 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006500 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006502 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 msec = msec / 2;
6504# endif
6505 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006506 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 }
6508#endif
6509 }
6510
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006511 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512}
6513
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006515 * Expand a path into all matching files and/or directories. Handles "*",
6516 * "?", "[a-z]", "**", etc.
6517 * "path" has backslashes before chars that are not to be expanded.
6518 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 */
6520 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006521mch_expandpath(
6522 garray_T *gap,
6523 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006524 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
Bram Moolenaar02743632005-07-25 20:42:36 +00006526 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528
6529/*
6530 * mch_expand_wildcards() - this code does wild-card pattern matching using
6531 * the shell
6532 *
6533 * return OK for success, FAIL for error (you may lose some memory) and put
6534 * an error message in *file.
6535 *
6536 * num_pat is number of input patterns
6537 * pat is array of pointers to input patterns
6538 * num_file is pointer to number of matched file names
6539 * file is pointer to array of pointers to matched file names
6540 */
6541
6542#ifndef SEEK_SET
6543# define SEEK_SET 0
6544#endif
6545#ifndef SEEK_END
6546# define SEEK_END 2
6547#endif
6548
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006549#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006550
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006552mch_expand_wildcards(
6553 int num_pat,
6554 char_u **pat,
6555 int *num_file,
6556 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006557 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558{
6559 int i;
6560 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006561 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 char_u *p;
6563 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564
Bram Moolenaarc7247912008-01-13 12:54:11 +00006565 /*
6566 * This is the non-OS/2 implementation (really Unix).
6567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 int j;
6569 char_u *tempname;
6570 char_u *command;
6571 FILE *fd;
6572 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006573#define STYLE_ECHO 0 // use "echo", the default
6574#define STYLE_GLOB 1 // use "glob", for csh
6575#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6576#define STYLE_PRINT 3 // use "print -N", for zsh
6577#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6578 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 int shell_style = STYLE_ECHO;
6580 int check_spaces;
6581 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006582 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006583 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006584 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585
Bram Moolenaar0f873732019-12-05 20:28:46 +01006586 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 *file = NULL;
6588
6589 /*
6590 * If there are no wildcards, just copy the names to allocated memory.
6591 * Saves a lot of time, because we don't have to start a new shell.
6592 */
6593 if (!have_wildcard(num_pat, pat))
6594 return save_patterns(num_pat, pat, num_file, file);
6595
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006596# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006597 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006598 if (sandbox != 0 && check_secure())
6599 return FAIL;
6600# endif
6601
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 /*
6603 * Don't allow the use of backticks in secure and restricted mode.
6604 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006605 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 for (i = 0; i < num_pat; ++i)
6607 if (vim_strchr(pat[i], '`') != NULL
6608 && (check_restricted() || check_secure()))
6609 return FAIL;
6610
6611 /*
6612 * get a name for the temp file
6613 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006614 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006616 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 return FAIL;
6618 }
6619
6620 /*
6621 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006622 * file.
6623 * STYLE_BT: NL separated
6624 * If expanding `cmd` execute it directly.
6625 * STYLE_GLOB: NUL separated
6626 * If we use *csh, "glob" will work better than "echo".
6627 * STYLE_PRINT: NL or NUL separated
6628 * If we use *zsh, "print -N" will work better than "glob".
6629 * STYLE_VIMGLOB: NL separated
6630 * If we use *sh*, we define "vimglob()".
6631 * STYLE_ECHO: space separated.
6632 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 */
6634 if (num_pat == 1 && *pat[0] == '`'
6635 && (len = STRLEN(pat[0])) > 2
6636 && *(pat[0] + len - 1) == '`')
6637 shell_style = STYLE_BT;
6638 else if ((len = STRLEN(p_sh)) >= 3)
6639 {
6640 if (STRCMP(p_sh + len - 3, "csh") == 0)
6641 shell_style = STYLE_GLOB;
6642 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6643 shell_style = STYLE_PRINT;
6644 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006645 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6646 "sh") != NULL)
6647 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648
Bram Moolenaar0f873732019-12-05 20:28:46 +01006649 // Compute the length of the command. We need 2 extra bytes: for the
6650 // optional '&' and for the NUL.
6651 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006653 if (shell_style == STYLE_VIMGLOB)
6654 len += STRLEN(sh_vimglob_func);
6655
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006656 for (i = 0; i < num_pat; ++i)
6657 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006658 // Count the length of the patterns in the same way as they are put in
6659 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006660#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006661 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006662#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006663 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006664 for (j = 0; pat[i][j] != NUL; ++j)
6665 {
6666 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006667 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006668 ++len;
6669 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006670#endif
6671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 command = alloc(len);
6673 if (command == NULL)
6674 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006675 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 vim_free(tempname);
6677 return FAIL;
6678 }
6679
6680 /*
6681 * Build the shell command:
6682 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6683 * recognizes this).
6684 * - Add the shell command to print the expanded names.
6685 * - Add the temp file name.
6686 * - Add the file name patterns.
6687 */
6688 if (shell_style == STYLE_BT)
6689 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006690 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006691 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006692 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006694 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006695 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006697 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006699 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 *p = ' ';
6701 }
6702 STRCAT(command, ">");
6703 }
6704 else
6705 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006706 STRCPY(command, "");
6707 if (shell_style == STYLE_GLOB)
6708 {
6709 // Assume the nonomatch option is valid only for csh like shells,
6710 // otherwise, this may set the positional parameters for the shell,
6711 // e.g. "$*".
6712 if (flags & EW_NOTFOUND)
6713 STRCAT(command, "set nonomatch; ");
6714 else
6715 STRCAT(command, "unset nonomatch; ");
6716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 if (shell_style == STYLE_GLOB)
6718 STRCAT(command, "glob >");
6719 else if (shell_style == STYLE_PRINT)
6720 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006721 else if (shell_style == STYLE_VIMGLOB)
6722 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 else
6724 STRCAT(command, "echo >");
6725 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006726
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006728
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 if (shell_style != STYLE_BT)
6730 for (i = 0; i < num_pat; ++i)
6731 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006732 // When using system() always add extra quotes, because the shell
6733 // is started twice. Otherwise put a backslash before special
6734 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735#ifdef USE_SYSTEM
6736 STRCAT(command, " \"");
6737 STRCAT(command, pat[i]);
6738 STRCAT(command, "\"");
6739#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006740 int intick = FALSE;
6741
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 p = command + STRLEN(command);
6743 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006744 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006745 {
6746 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006747 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006748 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6749 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006750 // Remove a backslash, take char literally. But keep
6751 // backslash inside backticks, before a special character
6752 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006753 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006754 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6755 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006756 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006757 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006758 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006759 else if (!intick
6760 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6761 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006762 // Put a backslash before a special character, but not
6763 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6764 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006765 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006766
Bram Moolenaar0f873732019-12-05 20:28:46 +01006767 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006768 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 *p = NUL;
6771#endif
6772 }
6773 if (flags & EW_SILENT)
6774 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006775 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006776 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777
6778 /*
6779 * Using zsh -G: If a pattern has no matches, it is just deleted from
6780 * the argument list, otherwise zsh gives an error message and doesn't
6781 * expand any other pattern.
6782 */
6783 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006784 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785
6786 /*
6787 * If we use -f then shell variables set in .cshrc won't get expanded.
6788 * vi can do it, so we will too, but it is only necessary if there is a "$"
6789 * in one of the patterns, otherwise we can still use the fast option.
6790 */
6791 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006792 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793
6794 /*
6795 * execute the shell command
6796 */
6797 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6798
Bram Moolenaar0f873732019-12-05 20:28:46 +01006799 // When running in the background, give it some time to create the temp
6800 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006801 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006802 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803
Bram Moolenaar0f873732019-12-05 20:28:46 +01006804 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 show_shell_mess = TRUE;
6806 vim_free(command);
6807
Bram Moolenaar0f873732019-12-05 20:28:46 +01006808 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 {
6810 mch_remove(tempname);
6811 vim_free(tempname);
6812 /*
6813 * With interactive completion, the error message is not printed.
6814 * However with USE_SYSTEM, I don't know how to turn off error messages
6815 * from the shell, so screen may still get messed up -- webb.
6816 */
6817#ifndef USE_SYSTEM
6818 if (!(flags & EW_SILENT))
6819#endif
6820 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006821 redraw_later_clear(); // probably messed up screen
6822 msg_putchar('\n'); // clear bottom line quickly
6823 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824#ifdef USE_SYSTEM
6825 if (!(flags & EW_SILENT))
6826#endif
6827 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006828 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006829 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 }
6831 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006832 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6833 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 if (shell_style == STYLE_BT)
6835 return FAIL;
6836 goto notfound;
6837 }
6838
6839 /*
6840 * read the names from the file into memory
6841 */
6842 fd = fopen((char *)tempname, READBIN);
6843 if (fd == NULL)
6844 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006845 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 if (!(flags & EW_SILENT))
6847 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006848 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006849 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 }
6851 vim_free(tempname);
6852 goto notfound;
6853 }
6854 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006855 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006857 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006858 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006859 buffer = NULL;
6860 else
6861 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 if (buffer == NULL)
6863 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006864 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 mch_remove(tempname);
6866 vim_free(tempname);
6867 fclose(fd);
6868 return FAIL;
6869 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006870 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 i = fread((char *)buffer, 1, len, fd);
6872 fclose(fd);
6873 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006874 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006876 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006877 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 vim_free(tempname);
6879 vim_free(buffer);
6880 return FAIL;
6881 }
6882 vim_free(tempname);
6883
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006884# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006885 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006887 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6889 *p++ = buffer[i];
6890 len = p - buffer;
6891# endif
6892
6893
Bram Moolenaar0f873732019-12-05 20:28:46 +01006894 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 if (shell_style == STYLE_ECHO)
6896 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006897 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006899 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {
6901 while (*p != ' ' && *p != '\n')
6902 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006903 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 }
6905 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006906 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006907 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006909 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006911 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 {
6913 while (*p != '\n' && *p != NUL)
6914 ++p;
6915 if (*p != NUL)
6916 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006917 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 }
6919 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006920 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 else
6922 {
6923 /*
6924 * Some versions of zsh use spaces instead of NULs to separate
6925 * results. Only do this when there is no NUL before the end of the
6926 * buffer, otherwise we would never be able to use file names with
6927 * embedded spaces when zsh does use NULs.
6928 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6929 * don't check for spaces again.
6930 */
6931 check_spaces = FALSE;
6932 if (shell_style == STYLE_PRINT && !did_find_nul)
6933 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006934 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006935 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006936 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 did_find_nul = TRUE;
6938 else
6939 check_spaces = TRUE;
6940 }
6941
6942 /*
6943 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6944 * already is one, for STYLE_GLOB it needs to be added.
6945 */
6946 if (len && buffer[len - 1] == NUL)
6947 --len;
6948 else
6949 buffer[len] = NUL;
6950 i = 0;
6951 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006952 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 {
6954 ++i;
6955 *p = NUL;
6956 }
6957 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006958 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
6960 if (i == 0)
6961 {
6962 /*
6963 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6964 * /bin/sh will happily expand it to nothing rather than returning an
6965 * error; and hey, it's good to check anyway -- webb.
6966 */
6967 vim_free(buffer);
6968 goto notfound;
6969 }
6970 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006971 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 if (*file == NULL)
6973 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006974 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 vim_free(buffer);
6976 return FAIL;
6977 }
6978
6979 /*
6980 * Isolate the individual file names.
6981 */
6982 p = buffer;
6983 for (i = 0; i < *num_file; ++i)
6984 {
6985 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006986 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006987 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6988 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006990 while (!(shell_style == STYLE_ECHO && *p == ' ')
6991 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006993 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 *p = NUL;
6995 else
6996 {
6997 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006998 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 }
7000 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007001 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007003 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007005 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 }
7007 }
7008
7009 /*
7010 * Move the file names to allocated memory.
7011 */
7012 for (j = 0, i = 0; i < *num_file; ++i)
7013 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007014 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7016 continue;
7017
Bram Moolenaar0f873732019-12-05 20:28:46 +01007018 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 dir = (mch_isdir((*file)[i]));
7020 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7021 continue;
7022
Bram Moolenaar0f873732019-12-05 20:28:46 +01007023 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007024 if (!dir && (flags & EW_EXEC)
7025 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007026 continue;
7027
Bram Moolenaar964b3742019-05-24 18:54:09 +02007028 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 if (p)
7030 {
7031 STRCPY(p, (*file)[i]);
7032 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007033 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 (*file)[j++] = p;
7035 }
7036 }
7037 vim_free(buffer);
7038 *num_file = j;
7039
Bram Moolenaar0f873732019-12-05 20:28:46 +01007040 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007042 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 goto notfound;
7044 }
7045
7046 return OK;
7047
7048notfound:
7049 if (flags & EW_NOTFOUND)
7050 return save_patterns(num_pat, pat, num_file, file);
7051 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052}
7053
Bram Moolenaar0f873732019-12-05 20:28:46 +01007054#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007057save_patterns(
7058 int num_pat,
7059 char_u **pat,
7060 int *num_file,
7061 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062{
7063 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007064 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007066 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 if (*file == NULL)
7068 return FAIL;
7069 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007070 {
7071 s = vim_strsave(pat[i]);
7072 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007073 // Be compatible with expand_filename(): halve the number of
7074 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007075 backslash_halve(s);
7076 (*file)[i] = s;
7077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 *num_file = num_pat;
7079 return OK;
7080}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082/*
7083 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7084 * expand.
7085 */
7086 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007087mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007089 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 if (*p == '\\' && p[1] != NUL)
7092 ++p;
7093 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 if (vim_strchr((char_u *)
7095#ifdef VMS
7096 "*?%"
7097#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099#endif
7100 , *p) != NULL)
7101 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 }
7103 return FALSE;
7104}
7105
7106/*
7107 * Return TRUE if the string "p" contains a wildcard.
7108 * Don't recognize '~' at the end as a wildcard.
7109 */
7110 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007111mch_has_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 || (*p == '~' && p[1] != NUL))
7126 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 }
7128 return FALSE;
7129}
7130
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007132have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133{
7134 int i;
7135
7136 for (i = 0; i < num; i++)
7137 if (mch_has_wildcard(file[i]))
7138 return 1;
7139 return 0;
7140}
7141
7142 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007143have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144{
7145 int i;
7146
7147 for (i = 0; i < num; i++)
7148 if (vim_strchr(file[i], '$') != NULL)
7149 return TRUE;
7150 return FALSE;
7151}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007153#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154/*
7155 * Scaled-down version of rename(), which is missing in Xenix.
7156 * This version can only move regular files and will fail if the
7157 * destination exists.
7158 */
7159 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007160mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161{
7162 struct stat st;
7163
Bram Moolenaar0f873732019-12-05 20:28:46 +01007164 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007166 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007168 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 return 0;
7170 return -1;
7171}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007172#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007174#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175/*
7176 * Initializes connection with gpm (if it isn't already opened)
7177 * Return 1 if succeeded (or connection already opened), 0 if failed
7178 */
7179 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007180gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007182 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
7184 if (!gpm_flag)
7185 {
7186 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7187 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007188 // Default handling for mouse move
7189 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 gpm_connect.maxMod = 0xffff;
7191 if (Gpm_Open(&gpm_connect, 0) > 0)
7192 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007193 // gpm library tries to handling TSTP causes
7194 // problems. Anyways, we close connection to Gpm whenever
7195 // we are going to suspend or starting an external process
7196 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007197# ifdef SIGTSTP
dbivolaruab16ad32021-12-29 19:41:47 +00007198 signal(SIGTSTP, restricted ? SIG_IGN : (RETSIGTYPE (*)())sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007199# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007200 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 }
7202 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007203 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 return 0;
7205 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007206 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207}
7208
7209/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007210 * Returns TRUE if the GPM mouse is enabled.
7211 */
7212 int
7213gpm_enabled(void)
7214{
7215 return gpm_flag && gpm_fd >= 0;
7216}
7217
7218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 */
7221 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007222gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007224 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 Gpm_Close();
7226}
7227
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007228/*
7229 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007231 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 */
7233 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007234mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235{
7236 int button;
7237 static Gpm_Event gpm_event;
7238 char_u string[6];
7239 int_u vim_modifiers;
7240 int row,col;
7241 unsigned char buttons_mask;
7242 unsigned char gpm_modifiers;
7243 static unsigned char old_buttons = 0;
7244
7245 Gpm_GetEvent(&gpm_event);
7246
7247#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007248 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 if (hold_gui_events)
7250 return 0;
7251#endif
7252
7253 row = gpm_event.y - 1;
7254 col = gpm_event.x - 1;
7255
Bram Moolenaar0f873732019-12-05 20:28:46 +01007256 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 string[1] = 'M';
7258 string[2] = 'G';
7259 switch (GPM_BARE_EVENTS(gpm_event.type))
7260 {
7261 case GPM_DRAG:
7262 string[3] = MOUSE_DRAG;
7263 break;
7264 case GPM_DOWN:
7265 buttons_mask = gpm_event.buttons & ~old_buttons;
7266 old_buttons = gpm_event.buttons;
7267 switch (buttons_mask)
7268 {
7269 case GPM_B_LEFT:
7270 button = MOUSE_LEFT;
7271 break;
7272 case GPM_B_MIDDLE:
7273 button = MOUSE_MIDDLE;
7274 break;
7275 case GPM_B_RIGHT:
7276 button = MOUSE_RIGHT;
7277 break;
7278 default:
7279 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007280 // Don't know what to do. Can more than one button be
7281 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 }
7283 string[3] = (char_u)(button | 0x20);
7284 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7285 break;
7286 case GPM_UP:
7287 string[3] = MOUSE_RELEASE;
7288 old_buttons &= ~gpm_event.buttons;
7289 break;
7290 default:
7291 return 0;
7292 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007293 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 gpm_modifiers = gpm_event.modifiers;
7295 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007296 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7297 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7298 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7300 vim_modifiers |= MOUSE_SHIFT;
7301
7302 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7303 vim_modifiers |= MOUSE_CTRL;
7304 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7305 vim_modifiers |= MOUSE_ALT;
7306 string[3] |= vim_modifiers;
7307 string[4] = (char_u)(col + ' ' + 1);
7308 string[5] = (char_u)(row + ' ' + 1);
7309 add_to_input_buf(string, 6);
7310 return 6;
7311}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007312#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007314#ifdef FEAT_SYSMOUSE
7315/*
7316 * Initialize connection with sysmouse.
7317 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7318 * output, any sysmouse output than will be processed via sig_sysmouse().
7319 * Return OK if succeeded, FAIL if failed.
7320 */
7321 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007322sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007323{
7324 struct mouse_info mouse;
7325
7326 mouse.operation = MOUSE_MODE;
7327 mouse.u.mode.mode = 0;
7328 mouse.u.mode.signal = SIGUSR2;
7329 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7330 {
7331 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7332 mouse.operation = MOUSE_SHOW;
7333 ioctl(1, CONS_MOUSECTL, &mouse);
7334 return OK;
7335 }
7336 return FAIL;
7337}
7338
7339/*
7340 * Stop processing SIGUSR2 signals, and also make sure that
7341 * virtual console do not send us any sysmouse related signal.
7342 */
7343 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007344sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007345{
7346 struct mouse_info mouse;
7347
7348 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7349 mouse.operation = MOUSE_MODE;
7350 mouse.u.mode.mode = 0;
7351 mouse.u.mode.signal = 0;
7352 ioctl(1, CONS_MOUSECTL, &mouse);
7353}
7354
7355/*
7356 * Gets info from sysmouse and adds special keys to input buf.
7357 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007358 static RETSIGTYPE
7359sig_sysmouse SIGDEFARG(sigarg)
7360{
7361 struct mouse_info mouse;
7362 struct video_info video;
7363 char_u string[6];
7364 int row, col;
7365 int button;
7366 int buttons;
7367 static int oldbuttons = 0;
7368
7369#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007370 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007371 if (hold_gui_events)
7372 return;
7373#endif
7374
7375 mouse.operation = MOUSE_GETINFO;
7376 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7377 && ioctl(1, FBIO_MODEINFO, &video) != -1
7378 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7379 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7380 {
7381 row = mouse.u.data.y / video.vi_cheight;
7382 col = mouse.u.data.x / video.vi_cwidth;
7383 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007384 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007385 string[1] = 'M';
7386 string[2] = 'S';
7387 if (oldbuttons == buttons && buttons != 0)
7388 {
7389 button = MOUSE_DRAG;
7390 }
7391 else
7392 {
7393 switch (buttons)
7394 {
7395 case 0:
7396 button = MOUSE_RELEASE;
7397 break;
7398 case 1:
7399 button = MOUSE_LEFT;
7400 break;
7401 case 2:
7402 button = MOUSE_MIDDLE;
7403 break;
7404 case 4:
7405 button = MOUSE_RIGHT;
7406 break;
7407 default:
7408 return;
7409 }
7410 oldbuttons = buttons;
7411 }
7412 string[3] = (char_u)(button);
7413 string[4] = (char_u)(col + ' ' + 1);
7414 string[5] = (char_u)(row + ' ' + 1);
7415 add_to_input_buf(string, 6);
7416 }
7417 return;
7418}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007419#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007420
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007422typedef char_u * (*STRPROCSTR)(char_u *);
7423typedef char_u * (*INTPROCSTR)(int);
7424typedef int (*STRPROCINT)(char_u *);
7425typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426
7427/*
7428 * Call a DLL routine which takes either a string or int param
7429 * and returns an allocated string.
7430 */
7431 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007432mch_libcall(
7433 char_u *libname,
7434 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007435 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007436 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007437 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007438 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
7440# if defined(USE_DLOPEN)
7441 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007442 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443# else
7444 shl_t hinstLib;
7445# endif
7446 STRPROCSTR ProcAdd;
7447 INTPROCSTR ProcAddI;
7448 char_u *retval_str = NULL;
7449 int retval_int = 0;
7450 int success = FALSE;
7451
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007452 /*
7453 * Get a handle to the DLL module.
7454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007456 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007457 (void)dlerror();
7458
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 hinstLib = dlopen((char *)libname, RTLD_LAZY
7460# ifdef RTLD_LOCAL
7461 | RTLD_LOCAL
7462# endif
7463 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007464 if (hinstLib == NULL)
7465 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007466 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007467 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007468 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007469 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471# else
7472 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7473# endif
7474
Bram Moolenaar0f873732019-12-05 20:28:46 +01007475 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 if (hinstLib != NULL)
7477 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007478# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 /*
7480 * Catch a crash when calling the library function. For example when
7481 * using a number where a string pointer is expected.
7482 */
7483 mch_startjmp();
7484 if (SETJMP(lc_jump_env) != 0)
7485 {
7486 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007487# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007488 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 mch_didjmp();
7491 }
7492 else
7493# endif
7494 {
7495 retval_str = NULL;
7496 retval_int = 0;
7497
7498 if (argstring != NULL)
7499 {
7500# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007501 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007502 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503# else
7504 if (shl_findsym(&hinstLib, (const char *)funcname,
7505 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7506 ProcAdd = NULL;
7507# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007508 if ((success = (ProcAdd != NULL
7509# if defined(USE_DLOPEN)
7510 && dlerr == NULL
7511# endif
7512 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 {
7514 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007515 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 else
7517 retval_str = (ProcAdd)(argstring);
7518 }
7519 }
7520 else
7521 {
7522# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007523 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007524 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525# else
7526 if (shl_findsym(&hinstLib, (const char *)funcname,
7527 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7528 ProcAddI = NULL;
7529# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007530 if ((success = (ProcAddI != NULL
7531# if defined(USE_DLOPEN)
7532 && dlerr == NULL
7533# endif
7534 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 {
7536 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007537 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 else
7539 retval_str = (ProcAddI)(argint);
7540 }
7541 }
7542
Bram Moolenaar0f873732019-12-05 20:28:46 +01007543 // Save the string before we free the library.
7544 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 if (string_result == NULL)
7546 *number_result = retval_int;
7547 else if (retval_str != NULL
7548 && retval_str != (char_u *)1
7549 && retval_str != (char_u *)-1)
7550 *string_result = vim_strsave(retval_str);
7551 }
7552
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007553# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 mch_endjmp();
7555# ifdef SIGHASARG
7556 if (lc_signal != 0)
7557 {
7558 int i;
7559
Bram Moolenaar0f873732019-12-05 20:28:46 +01007560 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 for (i = 0; signal_info[i].sig != -1; i++)
7562 if (lc_signal == signal_info[i].sig)
7563 break;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00007564 semsg(e_got_sig_str_in_libcall, signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 }
7566# endif
7567# endif
7568
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007570 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007571 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007572 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007573
Bram Moolenaar0f873732019-12-05 20:28:46 +01007574 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 (void)dlclose(hinstLib);
7576# else
7577 (void)shl_unload(hinstLib);
7578# endif
7579 }
7580
7581 if (!success)
7582 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007583 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 return FAIL;
7585 }
7586
7587 return OK;
7588}
7589#endif
7590
7591#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007592static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593static int xterm_button;
7594
7595/*
7596 * Setup a dummy window for X selections in a terminal.
7597 */
7598 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007599setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600{
7601 int z = 0;
7602 char *strp = "";
7603 Widget AppShell;
7604
7605 if (!x_connect_to_server())
7606 return;
7607
7608 open_app_context();
7609 if (app_context != NULL && xterm_Shell == (Widget)0)
7610 {
7611 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007612# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007614# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007615# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007616 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617
7618 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007619 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620# endif
7621
Bram Moolenaar0f873732019-12-05 20:28:46 +01007622 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 oldhandler = XSetErrorHandler(x_error_check);
7624
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007625# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007626 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7628 mch_startjmp();
7629 if (SETJMP(lc_jump_env) != 0)
7630 {
7631 mch_didjmp();
7632 xterm_dpy = NULL;
7633 }
7634 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007635# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 {
7637 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7638 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007639 if (xterm_dpy != NULL)
7640 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007641# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007643# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 }
7645
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007646# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007647 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007649# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007650 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 (void)XSetErrorHandler(oldhandler);
7652
7653 if (xterm_dpy == NULL)
7654 {
7655 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007656 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 return;
7658 }
7659
Bram Moolenaar0f873732019-12-05 20:28:46 +01007660 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 (void)XSetIOErrorHandler(x_IOerror_handler);
7662
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007663# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007665 {
7666 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007667 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007668 verbose_leave();
7669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670# endif
7671
Bram Moolenaar0f873732019-12-05 20:28:46 +01007672 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7674 applicationShellWidgetClass, xterm_dpy,
7675 NULL);
7676 if (AppShell == (Widget)0)
7677 return;
7678 xterm_Shell = XtVaCreatePopupShell("VIM",
7679 topLevelShellWidgetClass, AppShell,
7680 XtNmappedWhenManaged, 0,
7681 XtNwidth, 1,
7682 XtNheight, 1,
7683 NULL);
7684 if (xterm_Shell == (Widget)0)
7685 return;
7686
7687 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007688 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 if (x11_display == NULL)
7690 x11_display = xterm_dpy;
7691
7692 XtRealizeWidget(xterm_Shell);
7693 XSync(xterm_dpy, False);
7694 xterm_update();
7695 }
7696 if (xterm_Shell != (Widget)0)
7697 {
7698 clip_init(TRUE);
7699 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7700 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007701 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 if (test_x11_window(xterm_dpy) == FAIL)
7703 x11_window = 0;
7704 if (x11_window != 0)
7705 xterm_trace = 0;
7706 }
7707}
7708
7709 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007710start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711{
7712 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7713 return;
7714 xterm_trace = 1;
7715 xterm_button = button;
7716 do_xterm_trace();
7717}
7718
7719
7720 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007721stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722{
7723 if (xterm_trace < 0)
7724 return;
7725 xterm_trace = 0;
7726}
7727
7728/*
7729 * Query the xterm pointer and generate mouse termcodes if necessary
7730 * return TRUE if dragging is active, else FALSE
7731 */
7732 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007733do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734{
7735 Window root, child;
7736 int root_x, root_y;
7737 int win_x, win_y;
7738 int row, col;
7739 int_u mask_return;
7740 char_u buf[50];
7741 char_u *strp;
7742 long got_hints;
7743 static char_u *mouse_code;
7744 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7745 static int prev_row = 0, prev_col = 0;
7746 static XSizeHints xterm_hints;
7747
7748 if (xterm_trace <= 0)
7749 return FALSE;
7750
7751 if (xterm_trace == 1)
7752 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007753 // Get the hints just before tracking starts. The font size might
7754 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007755 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7756 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 || xterm_hints.width_inc <= 1
7758 || xterm_hints.height_inc <= 1)
7759 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007760 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 return FALSE;
7762 }
7763
Bram Moolenaar0f873732019-12-05 20:28:46 +01007764 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 mouse_code = find_termcode(mouse_name);
7766 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007767 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 xterm_trace = 2;
7769
Bram Moolenaar0f873732019-12-05 20:28:46 +01007770 // Find the offset of the chars, there might be a scrollbar on the
7771 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7773 &win_x, &win_y, &mask_return);
7774 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7775 - (xterm_hints.height_inc / 2);
7776 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7777 xterm_hints.y = 2;
7778 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7779 - (xterm_hints.width_inc / 2);
7780 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7781 xterm_hints.x = 2;
7782 return TRUE;
7783 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007784 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 {
7786 xterm_trace = 0;
7787 return FALSE;
7788 }
7789
7790 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7791 &win_x, &win_y, &mask_return);
7792
7793 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7794 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7795 if (row == prev_row && col == prev_col)
7796 return TRUE;
7797
7798 STRCPY(buf, mouse_code);
7799 strp = buf + STRLEN(buf);
7800 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7801 *strp++ = (char_u)(col + ' ' + 1);
7802 *strp++ = (char_u)(row + ' ' + 1);
7803 *strp = 0;
7804 add_to_input_buf(buf, STRLEN(buf));
7805
7806 prev_row = row;
7807 prev_col = col;
7808 return TRUE;
7809}
7810
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007811# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812/*
7813 * Destroy the display, window and app_context. Required for GTK.
7814 */
7815 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007816clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817{
7818 if (xterm_Shell != (Widget)0)
7819 {
7820 XtDestroyWidget(xterm_Shell);
7821 xterm_Shell = (Widget)0;
7822 }
7823 if (xterm_dpy != NULL)
7824 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007825# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007826 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 if (x11_display == xterm_dpy)
7830 x11_display = NULL;
7831 xterm_dpy = NULL;
7832 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007833# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 if (app_context != (XtAppContext)NULL)
7835 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007836 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 XtDestroyApplicationContext(app_context);
7838 app_context = (XtAppContext)NULL;
7839 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841}
7842# endif
7843
7844/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007845 * Catch up with GUI or X events.
7846 */
7847 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007848clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007849{
7850# ifdef FEAT_GUI
7851 if (gui.in_use)
7852 gui_mch_update();
7853 else
7854# endif
7855 if (xterm_Shell != (Widget)0)
7856 xterm_update();
7857}
7858
7859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 * Catch up with any queued X events. This may put keyboard input into the
7861 * input buffer, call resize call-backs, trigger timers etc. If there is
7862 * nothing in the X event queue (& no timers pending), then we return
7863 * immediately.
7864 */
7865 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007866xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867{
7868 XEvent event;
7869
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007870 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007872 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007874 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007875 break;
7876
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007877 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007878 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007879 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007880 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007881#ifdef FEAT_CLIENTSERVER
7882 {
7883 XPropertyEvent *e = (XPropertyEvent *)&event;
7884
7885 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007887 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007890 XtDispatchEvent(&event);
7891 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007892 else
7893 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007894 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007895 XtAppProcessEvent(app_context, mask);
7896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 }
7898}
7899
7900 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007901clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
7903 if (xterm_Shell != (Widget)0)
7904 return clip_x11_own_selection(xterm_Shell, cbd);
7905 return FAIL;
7906}
7907
7908 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007909clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910{
7911 if (xterm_Shell != (Widget)0)
7912 clip_x11_lose_selection(xterm_Shell, cbd);
7913}
7914
7915 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007916clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917{
7918 if (xterm_Shell != (Widget)0)
7919 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7920}
7921
7922 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007923clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924{
7925 clip_x11_set_selection(cbd);
7926}
7927#endif
7928
7929
7930#if defined(USE_XSMP) || defined(PROTO)
7931/*
7932 * Code for X Session Management Protocol.
7933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934
7935# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936/*
7937 * This is our chance to ask the user if they want to save,
7938 * or abort the logout
7939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007941xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
Bram Moolenaare1004402020-10-24 20:49:43 +02007943 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 int cancel_shutdown = False;
7945
Bram Moolenaare1004402020-10-24 20:49:43 +02007946 save_cmod_flags = cmdmod.cmod_flags;
7947 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007948 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007949 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02007951 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007952 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 out_flush();
7954
Bram Moolenaar0f873732019-12-05 20:28:46 +01007955 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 SmcInteractDone(smc_conn, cancel_shutdown);
7957
Bram Moolenaar0f873732019-12-05 20:28:46 +01007958 // Finish off
7959 // Only end save-yourself here if we're not cancelling shutdown;
7960 // we'll get a cancelled callback later in which we'll end it.
7961 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 if (!cancel_shutdown)
7963 {
7964 xsmp.save_yourself = False;
7965 SmcSaveYourselfDone(smc_conn, True);
7966 }
7967}
7968# endif
7969
7970/*
7971 * Callback that starts save-yourself.
7972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007974xsmp_handle_save_yourself(
7975 SmcConn smc_conn,
7976 SmPointer client_data UNUSED,
7977 int save_type UNUSED,
7978 Bool shutdown,
7979 int interact_style UNUSED,
7980 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007982 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 if (xsmp.save_yourself)
7984 SmcSaveYourselfDone(smc_conn, True);
7985 xsmp.save_yourself = True;
7986 xsmp.shutdown = shutdown;
7987
Bram Moolenaar0f873732019-12-05 20:28:46 +01007988 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007990 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991
7992 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007993 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994
7995# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007996 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007998 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 SmcInteractRequest(smc_conn, SmDialogError,
8000 xsmp_handle_interaction, client_data);
8001 else
8002# endif
8003 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008004 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 SmcSaveYourselfDone(smc_conn, True);
8006 xsmp.save_yourself = False;
8007 }
8008}
8009
8010
8011/*
8012 * Callback to warn us of imminent death.
8013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008015xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017 xsmp_close();
8018
Bram Moolenaar0f873732019-12-05 20:28:46 +01008019 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 getout_preserve_modified(0);
8021}
8022
8023
8024/*
8025 * Callback to tell us that save-yourself has completed.
8026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008028xsmp_save_complete(
8029 SmcConn smc_conn UNUSED,
8030 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031{
8032 xsmp.save_yourself = False;
8033}
8034
8035
8036/*
8037 * Callback to tell us that an instigated shutdown was cancelled
8038 * (maybe even by us)
8039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008041xsmp_shutdown_cancelled(
8042 SmcConn smc_conn,
8043 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
8045 if (xsmp.save_yourself)
8046 SmcSaveYourselfDone(smc_conn, True);
8047 xsmp.save_yourself = False;
8048 xsmp.shutdown = False;
8049}
8050
8051
8052/*
8053 * Callback to tell us that a new ICE connection has been established.
8054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008056xsmp_ice_connection(
8057 IceConn iceConn,
8058 IcePointer clientData UNUSED,
8059 Bool opening,
8060 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008062 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 if (opening)
8064 {
8065 xsmp_icefd = IceConnectionNumber(iceConn);
8066 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8067 }
8068}
8069
8070
Bram Moolenaar0f873732019-12-05 20:28:46 +01008071// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008073xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074{
8075 Bool rep;
8076
8077 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8078 == IceProcessMessagesIOError)
8079 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008080 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008082 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 xsmp_close();
8084 return FAIL;
8085 }
8086 else
8087 return OK;
8088}
8089
8090static int dummy;
8091
Bram Moolenaar0f873732019-12-05 20:28:46 +01008092// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 void
8094xsmp_init(void)
8095{
8096 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 SmcCallbacks smcallbacks;
8098#if 0
8099 SmPropValue smname;
8100 SmProp smnameprop;
8101 SmProp *smprops[1];
8102#endif
8103
8104 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008105 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106
8107 xsmp.save_yourself = xsmp.shutdown = False;
8108
Bram Moolenaar0f873732019-12-05 20:28:46 +01008109 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8111 smcallbacks.save_yourself.client_data = NULL;
8112 smcallbacks.die.callback = xsmp_die;
8113 smcallbacks.die.client_data = NULL;
8114 smcallbacks.save_complete.callback = xsmp_save_complete;
8115 smcallbacks.save_complete.client_data = NULL;
8116 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8117 smcallbacks.shutdown_cancelled.client_data = NULL;
8118
Bram Moolenaar0f873732019-12-05 20:28:46 +01008119 // Set up a watch on ICE connection creations. The "dummy" argument is
8120 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8122 {
8123 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008124 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 return;
8126 }
8127
Bram Moolenaar0f873732019-12-05 20:28:46 +01008128 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 xsmp.smcconn = SmcOpenConnection(
8130 NULL,
8131 NULL,
8132 SmProtoMajor,
8133 SmProtoMinor,
8134 SmcSaveYourselfProcMask | SmcDieProcMask
8135 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8136 &smcallbacks,
8137 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008138 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008139 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 errorstring);
8141 if (xsmp.smcconn == NULL)
8142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008144 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008145 char errorreport[132];
8146
8147 // If the message is too long it might not be NUL terminated. Add
8148 // a NUL at the end to make sure we don't go over the end.
8149 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008150 vim_snprintf(errorreport, sizeof(errorreport),
8151 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008152 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 return;
8155 }
8156 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8157
8158#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008159 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 smname.value = "vim";
8161 smname.length = 3;
8162 smnameprop.name = "SmProgram";
8163 smnameprop.type = "SmARRAY8";
8164 smnameprop.num_vals = 1;
8165 smnameprop.vals = &smname;
8166
8167 smprops[0] = &smnameprop;
8168 SmcSetProperties(xsmp.smcconn, 1, smprops);
8169#endif
8170}
8171
8172
Bram Moolenaar0f873732019-12-05 20:28:46 +01008173// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008175xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176{
8177 if (xsmp_icefd != -1)
8178 {
8179 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008180 if (xsmp.clientid != NULL)
8181 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008182 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 xsmp_icefd = -1;
8184 }
8185}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008186#endif // USE_XSMP