blob: 984869809d9bdeca7af939533c8db5d2c43a9c34 [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__
Bram Moolenaar4f974752019-02-17 17:44:42 +010047# ifndef MSWIN
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000048# include <cygwin/version.h>
Bram Moolenaar0f873732019-12-05 20:28:46 +010049# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
50 // for cygwin_conv_path()
Bram Moolenaar693e40c2013-02-26 14:56:42 +010051# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
52# define WIN32_LEAN_AND_MEAN
53# include <windows.h>
54# include "winclip.pro"
55# endif
Bram Moolenaara2442432007-04-26 14:26:37 +000056# endif
57#endif
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#ifdef FEAT_MOUSE_GPM
60# include <gpm.h>
Bram Moolenaar0f873732019-12-05 20:28:46 +010061// <linux/keyboard.h> contains defines conflicting with "keymap.h",
62// I just copied relevant defines here. A cleaner solution would be to put gpm
63// code into separate file and include there linux/keyboard.h
64// #include <linux/keyboard.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000065# define KG_SHIFT 0
66# define KG_CTRL 2
67# define KG_ALT 3
68# define KG_ALTGR 1
69# define KG_SHIFTL 4
70# define KG_SHIFTR 5
71# define KG_CTRLL 6
72# define KG_CTRLR 7
73# define KG_CAPSSHIFT 8
74
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010075static void gpm_close(void);
76static int gpm_open(void);
77static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000078#endif
79
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000080#ifdef FEAT_SYSMOUSE
81# include <sys/consio.h>
82# include <sys/fbio.h>
83
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010084static int sysmouse_open(void);
85static void sysmouse_close(void);
Bram Moolenaard99df422016-01-29 23:20:40 +010086static RETSIGTYPE sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000087#endif
88
Bram Moolenaar071d4272004-06-13 20:20:40 +000089/*
90 * end of autoconf section. To be extended...
91 */
92
Bram Moolenaar0f873732019-12-05 20:28:46 +010093// Are the following #ifdefs still required? And why? Is that for X11?
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
95#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
96# ifdef SIGWINCH
97# undef SIGWINCH
98# endif
99# ifdef TIOCGWINSZ
100# undef TIOCGWINSZ
101# endif
102#endif
103
Bram Moolenaar0f873732019-12-05 20:28:46 +0100104#if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105# define SIGWINCH SIGWINDOW
106#endif
107
108#ifdef FEAT_X11
109# include <X11/Xlib.h>
110# include <X11/Xutil.h>
111# include <X11/Xatom.h>
112# ifdef FEAT_XCLIPBOARD
113# include <X11/Intrinsic.h>
114# include <X11/Shell.h>
115# include <X11/StringDefs.h>
116static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static void clip_update(void);
118static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119# endif
120
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121Window x11_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#endif
124
Bram Moolenaar5c3128e2020-05-11 20:54:42 +0200125static int ignore_sigtstp = FALSE;
126
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200130static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +0200131static int unix_did_set_title = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132static char_u *oldicon = NULL;
133static int did_set_icon = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaar205b8862011-09-07 15:04:31 +0200137#ifdef HAVE_UNION_WAIT
138typedef union wait waitstatus;
139#else
140typedef int waitstatus;
141#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200142static int WaitForChar(long msec, int *interrupted, int ignore_input);
143static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar041c7102020-05-30 18:14:57 +0200144#ifdef VMS
Bram Moolenaarcda77642016-06-04 13:32:35 +0200145int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200147static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
149
150#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static int do_xterm_trace(void);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100152# define XT_TRACE_DELAY 50 // delay for xterm tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#endif
154
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156
157#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100158static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000160#if defined(SIGTSTP)
161static RETSIGTYPE sig_tstp SIGPROTOARG;
162// volatile because it is used in signal handler sig_tstp() and sigcont_handler().
163static volatile sig_atomic_t in_mch_suspend = FALSE;
164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100166static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200168#if defined(SIGUSR1)
169static RETSIGTYPE catch_sigusr1 SIGPROTOARG;
170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100172static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000174#if defined(SIGALRM) && defined(FEAT_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100176static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100177// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200178static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100180static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100182static void catch_int_signal(void);
183static void set_signals(void);
184static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200185#ifdef HAVE_SIGPROCMASK
186# define SIGSET_DECL(set) sigset_t set;
187# define BLOCK_SIGNALS(set) block_signals(set)
188# define UNBLOCK_SIGNALS(set) unblock_signals(set)
189#else
190# define SIGSET_DECL(set)
191# define BLOCK_SIGNALS(set) do { /**/ } while (0)
192# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
193#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100194static int have_wildcard(int, char_u **);
195static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100197static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000200# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#endif
202
Bram Moolenaar0f873732019-12-05 20:28:46 +0100203// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200204static volatile sig_atomic_t do_resize = FALSE;
dbivolaruab16ad32021-12-29 19:41:47 +0000205// volatile because it is used in signal handler sig_tstp().
206static volatile sig_atomic_t got_tstp = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207static char_u *extra_shell_arg = NULL;
208static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100209// volatile because it is used in signal handler deathtrap().
210static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
211// volatile because it is used in signal handler deathtrap().
212static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100214#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
215static int dont_check_job_ended = 0;
216#endif
217
Bram Moolenaar26e86442020-05-17 14:06:16 +0200218// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
219static tmode_T mch_cur_tmode = TMODE_COOK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220
221#ifdef USE_XSMP
222typedef struct
223{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100224 SmcConn smcconn; // The SM connection ID
225 IceConn iceconn; // The ICE connection ID
226 char *clientid; // The client ID for the current smc session
227 Bool save_yourself; // If we're in the middle of a save_yourself
228 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229} xsmp_config_T;
230
231static xsmp_config_T xsmp;
232#endif
233
234#ifdef SYS_SIGLIST_DECLARED
235/*
236 * I have seen
237 * extern char *_sys_siglist[NSIG];
238 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
239 * that describe the signals. That is nearly what we want here. But
240 * autoconf does only check for sys_siglist (without the underscore), I
241 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100242 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 */
244#endif
245
246static struct signalinfo
247{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100248 int sig; // Signal number, eg. SIGSEGV etc
249 char *name; // Signal name (not char_u!).
250 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251} signal_info[] =
252{
253#ifdef SIGHUP
254 {SIGHUP, "HUP", TRUE},
255#endif
256#ifdef SIGQUIT
257 {SIGQUIT, "QUIT", TRUE},
258#endif
259#ifdef SIGILL
260 {SIGILL, "ILL", TRUE},
261#endif
262#ifdef SIGTRAP
263 {SIGTRAP, "TRAP", TRUE},
264#endif
265#ifdef SIGABRT
266 {SIGABRT, "ABRT", TRUE},
267#endif
268#ifdef SIGEMT
269 {SIGEMT, "EMT", TRUE},
270#endif
271#ifdef SIGFPE
272 {SIGFPE, "FPE", TRUE},
273#endif
274#ifdef SIGBUS
275 {SIGBUS, "BUS", TRUE},
276#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100277#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100278 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 {SIGSEGV, "SEGV", TRUE},
280#endif
281#ifdef SIGSYS
282 {SIGSYS, "SYS", TRUE},
283#endif
284#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100285 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286#endif
287#ifdef SIGTERM
288 {SIGTERM, "TERM", TRUE},
289#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100290#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 {SIGVTALRM, "VTALRM", TRUE},
292#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000293#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100294 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
295 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 {SIGPROF, "PROF", TRUE},
297#endif
298#ifdef SIGXCPU
299 {SIGXCPU, "XCPU", TRUE},
300#endif
301#ifdef SIGXFSZ
302 {SIGXFSZ, "XFSZ", TRUE},
303#endif
304#ifdef SIGUSR1
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200305 {SIGUSR1, "USR1", FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000307#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100308 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 {SIGUSR2, "USR2", TRUE},
310#endif
311#ifdef SIGINT
312 {SIGINT, "INT", FALSE},
313#endif
314#ifdef SIGWINCH
315 {SIGWINCH, "WINCH", FALSE},
316#endif
317#ifdef SIGTSTP
318 {SIGTSTP, "TSTP", FALSE},
319#endif
320#ifdef SIGPIPE
321 {SIGPIPE, "PIPE", FALSE},
322#endif
323 {-1, "Unknown!", FALSE}
324};
325
Bram Moolenaar25724922009-07-14 15:38:41 +0000326 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100327mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000328{
329 if (p_verbose >= 5)
330 {
331 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100332 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000333 verbose_leave();
334 }
335# ifdef VMS
336 return chdir(vms_fixfilename(path));
337# else
338 return chdir(path);
339# endif
340}
341
Bram Moolenaar0f873732019-12-05 20:28:46 +0100342// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100343#if defined(ECHOE) && defined(ICANON) \
344 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
345 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200346# define NEW_TTY_SYSTEM
347#endif
348
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000349/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100350 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100353mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200355 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100356 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100357 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358}
359
360/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100361 * Function passed to inchar_loop() to handle window resizing.
362 * If "check_only" is TRUE: Return whether there was a resize.
363 * If "check_only" is FALSE: Deal with the window resized.
364 */
365 static int
366resize_func(int check_only)
367{
368 if (check_only)
369 return do_resize;
370 while (do_resize)
371 handle_resize();
372 return FALSE;
373}
374
375/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000376 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 * Get a characters from the keyboard.
378 * Return the number of characters that are available.
379 * If wtime == 0 do not wait for characters.
380 * If wtime == n wait a short time for characters.
381 * If wtime == -1 wait forever for characters.
382 */
383 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100384mch_inchar(
385 char_u *buf,
386 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100387 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100388 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100390 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
391 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392}
393
394 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100395handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396{
397 do_resize = FALSE;
398 shell_resized();
399}
400
401/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200402 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 */
404 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100405mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200407 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408}
409
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200410#if defined(FEAT_TERMINAL) || defined(PROTO)
411/*
412 * Check for any pending input or messages.
413 */
414 int
415mch_check_messages(void)
416{
417 return WaitForChar(0L, NULL, TRUE);
418}
419#endif
420
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
422# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000423# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424# endif
425# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
426# include <sys/sysctl.h>
427# endif
428# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
429# include <sys/sysinfo.h>
430# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100431# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100432# include <mach/mach_host.h>
433# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435
436/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000437 * Return total amount of memory available in Kbyte.
438 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100441mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100444 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445
Bram Moolenaar362dc332018-03-05 21:59:37 +0100446# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100447 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100448 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100449 mach_port_t host = mach_host_self();
450 kern_return_t kret;
451# ifdef HOST_VM_INFO64
452 struct vm_statistics64 vm_stat;
453 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454
Bram Moolenaar988615f2018-02-27 17:58:20 +0100455 kret = host_statistics64(host, HOST_VM_INFO64,
456 (host_info64_t)&vm_stat, &count);
457# else
458 struct vm_statistics vm_stat;
459 natural_t count = HOST_VM_INFO_COUNT;
460
461 kret = host_statistics(host, HOST_VM_INFO,
462 (host_info_t)&vm_stat, &count);
463# endif
464 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100465 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100466 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
467 + vm_stat.inactive_count
468# ifdef MAC_OS_X_VERSION_10_9
469 + vm_stat.compressor_page_count
470# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100471 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100472 mach_port_deallocate(mach_task_self(), host);
473 }
474# endif
475
476# ifdef HAVE_SYSCTL
477 if (mem == 0)
478 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100479 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100480 int mib[2];
481 size_t len = sizeof(long_u);
482# ifdef HW_USERMEM64
483 long_u physmem;
484# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100485 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100486 union {
487 int_u u32;
488 long_u u64;
489 } physmem;
490# endif
491
492 mib[0] = CTL_HW;
493# ifdef HW_USERMEM64
494 mib[1] = HW_USERMEM64;
495# else
496 mib[1] = HW_USERMEM;
497# endif
498 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
499 {
500# ifdef HW_USERMEM64
501 mem = (long_u)physmem;
502# else
503 if (len == sizeof(physmem.u64))
504 mem = (long_u)physmem.u64;
505 else
506 mem = (long_u)physmem.u32;
507# endif
508 }
509 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200512# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (mem == 0)
514 {
515 struct sysinfo sinfo;
516
Bram Moolenaar0f873732019-12-05 20:28:46 +0100517 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000519 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200520# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100521 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000522 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
523 {
524 sinfo.mem_unit = sinfo.mem_unit >> 1;
525 --shiftright;
526 }
527 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200528# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200530# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200533# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200535# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 if (mem == 0)
537 {
538 long pagesize, pagecount;
539
Bram Moolenaar0f873732019-12-05 20:28:46 +0100540 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 pagesize = sysconf(_SC_PAGESIZE);
542 pagecount = sysconf(_SC_PHYS_PAGES);
543 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000544 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100545 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000546 while (shiftright > 0 && (pagesize & 1) == 0)
547 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000548 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000549 --shiftright;
550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200554# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
Bram Moolenaar0f873732019-12-05 20:28:46 +0100556 // Return the minimum of the physical memory and the user limit, because
557 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200558# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 {
560 struct rlimit rlp;
561
562 if (getrlimit(RLIMIT_DATA, &rlp) == 0
563 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200564# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200566# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000567 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000569 {
570 mem = (long_u)rlp.rlim_cur;
571 shiftright = 10;
572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200574# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
576 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000577 return mem >> shiftright;
578 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579}
580#endif
581
Bram Moolenaar0981c872020-08-23 14:28:37 +0200582/*
583 * "flags": MCH_DELAY_IGNOREINPUT - don't read input
584 * MCH_DELAY_SETTMODE - use settmode() even for short delays
585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200587mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200589 tmode_T old_tmode;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200590 int call_settmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000591#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100592 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594
Bram Moolenaar0981c872020-08-23 14:28:37 +0200595 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100597 // Go to cooked mode without echo, to allow SIGINT interrupting us
598 // here. But we don't want QUIT to kill us (CTRL-\ used in a
599 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200600 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000601 in_mch_delay = TRUE;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200602 call_settmode = mch_cur_tmode == TMODE_RAW
603 && (msec > 500 || (flags & MCH_DELAY_SETTMODE));
604 if (call_settmode)
605 {
606 old_tmode = mch_cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 settmode(TMODE_SLEEP);
Bram Moolenaar80361a52020-10-05 21:39:25 +0200608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
610 /*
611 * Everybody sleeps in a different way...
612 * Prefer nanosleep(), some versions of usleep() can only sleep up to
613 * one second.
614 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000615#ifdef FEAT_MZSCHEME
616 do
617 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100618 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000619 if (total > p_mzq)
620 msec = p_mzq;
621 else
622 msec = total;
623 total -= msec;
624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625#ifdef HAVE_NANOSLEEP
626 {
627 struct timespec ts;
628
629 ts.tv_sec = msec / 1000;
630 ts.tv_nsec = (msec % 1000) * 1000000;
631 (void)nanosleep(&ts, NULL);
632 }
633#else
634# ifdef HAVE_USLEEP
635 while (msec >= 1000)
636 {
637 usleep((unsigned int)(999 * 1000));
638 msec -= 999;
639 }
640 usleep((unsigned int)(msec * 1000));
641# else
642# ifndef HAVE_SELECT
643 poll(NULL, 0, (int)msec);
644# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 {
646 struct timeval tv;
647
648 tv.tv_sec = msec / 1000;
649 tv.tv_usec = (msec % 1000) * 1000;
Bram Moolenaar0981c872020-08-23 14:28:37 +0200650 // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
651 // a patch from Sun to fix this. Reported by Gunnar Pedersen.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 select(0, NULL, NULL, NULL, &tv);
653 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100654# endif // HAVE_SELECT
655# endif // HAVE_NANOSLEEP
656#endif // HAVE_USLEEP
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000657#ifdef FEAT_MZSCHEME
658 }
659 while (total > 0);
660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661
Bram Moolenaar80361a52020-10-05 21:39:25 +0200662 if (call_settmode)
Bram Moolenaar26e86442020-05-17 14:06:16 +0200663 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000664 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
666 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200667 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668}
669
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000670#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
672# define HAVE_CHECK_STACK_GROWTH
673/*
674 * Support for checking for an almost-out-of-stack-space situation.
675 */
676
677/*
678 * Return a pointer to an item on the stack. Used to find out if the stack
679 * grows up or down.
680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681static int stack_grows_downwards;
682
683/*
684 * Find out if the stack grows upwards or downwards.
685 * "p" points to a variable on the stack of the caller.
686 */
687 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100688check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689{
690 int i;
691
692 stack_grows_downwards = (p > (char *)&i);
693}
694#endif
695
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000696#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697static char *stack_limit = NULL;
698
699#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
700# include <pthread.h>
701# include <pthread_np.h>
702#endif
703
704/*
705 * Find out until how var the stack can grow without getting into trouble.
706 * Called when starting up and when switching to the signal stack in
707 * deathtrap().
708 */
709 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100710get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
712 struct rlimit rlp;
713 int i;
714 long lim;
715
Bram Moolenaar0f873732019-12-05 20:28:46 +0100716 // Set the stack limit to 15/16 of the allowable size. Skip this when the
717 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 if (getrlimit(RLIMIT_STACK, &rlp) == 0
719 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
720# ifdef RLIM_INFINITY
721 && rlp.rlim_cur != RLIM_INFINITY
722# endif
723 )
724 {
725 lim = (long)rlp.rlim_cur;
726#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
727 {
728 pthread_attr_t attr;
729 size_t size;
730
Bram Moolenaar0f873732019-12-05 20:28:46 +0100731 // On FreeBSD the initial thread always has a fixed stack size, no
732 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 pthread_attr_init(&attr);
734 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
735 {
736 pthread_attr_getstacksize(&attr, &size);
737 if (lim > (long)size)
738 lim = (long)size;
739 }
740 pthread_attr_destroy(&attr);
741 }
742#endif
743 if (stack_grows_downwards)
744 {
745 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
746 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100747 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 stack_limit = (char *)((long)&i / 16L);
749 }
750 else
751 {
752 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
753 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100754 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 }
756 }
757}
758
759/*
760 * Return FAIL when running out of stack space.
761 * "p" must point to any variable local to the caller that's on the stack.
762 */
763 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100764mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
766 if (stack_limit != NULL)
767 {
768 if (stack_grows_downwards)
769 {
770 if (p < stack_limit)
771 return FAIL;
772 }
773 else if (p > stack_limit)
774 return FAIL;
775 }
776 return OK;
777}
778#endif
779
780#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
781/*
782 * Support for using the signal stack.
783 * This helps when we run out of stack space, which causes a SIGSEGV. The
784 * signal handler then must run on another stack, since the normal stack is
785 * completely full.
786 */
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100789static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100791static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792# endif
793
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200794/*
795 * Get a size of signal stack.
796 * Preference (if available): sysconf > SIGSTKSZ > guessed size
797 */
798static long int get_signal_stack_size()
799{
800# ifdef HAVE_SYSCONF_SIGSTKSZ
801 long int size = -1;
802
803 // return size only if sysconf doesn't return an error
804 if ((size = sysconf(_SC_SIGSTKSZ)) > -1)
805 return size;
806# endif
807
808# ifdef SIGSTKSZ
809 // if sysconf() isn't available or gives error, return SIGSTKSZ
810 // if defined
811 return SIGSTKSZ;
812# endif
813
814 // otherwise guess the size
815 return 8000;
816}
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818static char *signal_stack;
819
820 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100821init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
823 if (signal_stack != NULL)
824 {
825# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826# ifdef HAVE_SS_BASE
827 sigstk.ss_base = signal_stack;
828# else
829 sigstk.ss_sp = signal_stack;
830# endif
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200831 sigstk.ss_size = get_signal_stack_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 sigstk.ss_flags = 0;
833 (void)sigaltstack(&sigstk, NULL);
834# else
835 sigstk.ss_sp = signal_stack;
836 if (stack_grows_downwards)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200837 sigstk.ss_sp += get_signal_stack_size() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 sigstk.ss_onstack = 0;
839 (void)sigstack(&sigstk, NULL);
840# endif
841 }
842}
843#endif
844
845/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000846 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 * will barf when the second argument to signal() is ``wrong''.
848 * Let me try it with a few tricky defines from my own osdef.h (jw).
849 */
850#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 static RETSIGTYPE
852sig_winch SIGDEFARG(sigarg)
853{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100854 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
856 do_resize = TRUE;
857 SIGRETURN;
858}
859#endif
860
dbivolaruab16ad32021-12-29 19:41:47 +0000861#if defined(SIGTSTP)
862 static RETSIGTYPE
863sig_tstp SIGDEFARG(sigarg)
864{
865 // Second time we get called we actually need to suspend
866 if (in_mch_suspend)
867 {
868 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
869 raise(sigarg);
870 }
dbivolaru79a6e252022-01-23 16:41:14 +0000871 else
872 got_tstp = TRUE;
dbivolaruab16ad32021-12-29 19:41:47 +0000873
874 // this is not required on all systems, but it doesn't hurt anybody
875 signal(SIGTSTP, (RETSIGTYPE (*)())sig_tstp);
dbivolaruab16ad32021-12-29 19:41:47 +0000876 SIGRETURN;
877}
878#endif
879
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 static RETSIGTYPE
882catch_sigint SIGDEFARG(sigarg)
883{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100884 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
886 got_int = TRUE;
887 SIGRETURN;
888}
889#endif
890
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200891#if defined(SIGUSR1)
892 static RETSIGTYPE
893catch_sigusr1 SIGDEFARG(sigarg)
894{
895 // this is not required on all systems, but it doesn't hurt anybody
896 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
897 got_sigusr1 = TRUE;
898 SIGRETURN;
899}
900#endif
901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 static RETSIGTYPE
904catch_sigpwr SIGDEFARG(sigarg)
905{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100906 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000907 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 /*
909 * I'm not sure we get the SIGPWR signal when the system is really going
910 * down or when the batteries are almost empty. Just preserve the swap
911 * files and don't exit, that can't do any harm.
912 */
913 ml_sync_all(FALSE, FALSE);
914 SIGRETURN;
915}
916#endif
917
918#ifdef SET_SIG_ALARM
919/*
920 * signal function for alarm().
921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 static RETSIGTYPE
923sig_alarm SIGDEFARG(sigarg)
924{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100925 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 sig_alarm_called = TRUE;
927 SIGRETURN;
928}
929#endif
930
Bram Moolenaaredce7422019-01-20 18:39:30 +0100931#if (defined(HAVE_SETJMP_H) \
932 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
933 || defined(FEAT_LIBCALL))) \
934 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100935# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100936
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100937// argument to SETJMP()
938static JMP_BUF lc_jump_env;
939
940# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100941// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100942// Volatile because it is used in signal handlers.
943static volatile sig_atomic_t lc_signal;
944# endif
945
946// TRUE when lc_jump_env is valid.
947// Volatile because it is used in signal handler deathtrap().
948static volatile sig_atomic_t lc_active INIT(= FALSE);
949
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950/*
951 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100952 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100954 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 * Usage:
956 * mch_startjmp();
957 * if (SETJMP(lc_jump_env) != 0)
958 * {
959 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100960 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 * }
962 * else
963 * {
964 * do_the_work;
965 * mch_endjmp();
966 * }
967 * Note: Can't move SETJMP() here, because a function calling setjmp() must
968 * not return before the saved environment is used.
969 * Returns OK for normal return, FAIL when the protected code caused a
970 * problem and LONGJMP() was used.
971 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100972 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100973mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974{
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100975# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 lc_active = TRUE;
979}
980
Bram Moolenaar113e1072019-01-20 15:30:40 +0100981 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100982mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983{
984 lc_active = FALSE;
985}
986
Bram Moolenaar113e1072019-01-20 15:30:40 +0100987 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100988mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989{
990# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100991 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
992 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 init_signal_stack();
994# endif
995}
996#endif
997
998/*
999 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001000 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001002 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1003 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 */
1005 static RETSIGTYPE
1006deathtrap SIGDEFARG(sigarg)
1007{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001008 static int entered = 0; // count the number of times we got here.
1009 // Note: when memory has been corrupted
1010 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011#ifdef SIGHASARG
1012 int i;
1013#endif
1014
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001015#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 /*
1017 * Catch a crash in protected code.
1018 * Restores the environment saved in lc_jump_env, which looks like
1019 * SETJMP() returns 1.
1020 */
1021 if (lc_active)
1022 {
1023# if defined(SIGHASARG)
1024 lc_signal = sigarg;
1025# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001026 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001028 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
1030#endif
1031
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001032#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001033# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001034 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1035 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1036 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001037 if (in_mch_delay && sigarg == SIGQUIT)
1038 SIGRETURN;
1039# endif
1040
Bram Moolenaar0f873732019-12-05 20:28:46 +01001041 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1042 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1043 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001044 if (entered == 0
1045 && (0
1046# ifdef SIGHUP
1047 || sigarg == SIGHUP
1048# endif
1049# ifdef SIGQUIT
1050 || sigarg == SIGQUIT
1051# endif
1052# ifdef SIGTERM
1053 || sigarg == SIGTERM
1054# endif
1055# ifdef SIGPWR
1056 || sigarg == SIGPWR
1057# endif
1058# ifdef SIGUSR1
1059 || sigarg == SIGUSR1
1060# endif
1061# ifdef SIGUSR2
1062 || sigarg == SIGUSR2
1063# endif
1064 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001065 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001066 SIGRETURN;
1067#endif
1068
Bram Moolenaar0f873732019-12-05 20:28:46 +01001069 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 ++entered;
1071
Bram Moolenaar0f873732019-12-05 20:28:46 +01001072 // Executing autocommands is likely to use more stack space than we have
1073 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001074 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001075
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001077 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 set_vim_var_nr(VV_DYING, (long)entered);
1079#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001080 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001082#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001083 // Since we are now using the signal stack, need to reset the stack
1084 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 get_stack_limit();
1086#endif
1087
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001088#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001089 // This is for opening gdb the moment Vim crashes.
1090 // You need to manually adjust the file name and Vim executable name.
1091 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001092 {
1093# define VI_GDB_FILE "/tmp/vimgdb"
1094# define VIM_NAME "/usr/bin/vim"
1095 FILE *fp = fopen(VI_GDB_FILE, "w");
1096 if (fp)
1097 {
1098 fprintf(fp,
1099 "file %s\n"
1100 "attach %d\n"
1101 "set height 1000\n"
1102 "bt full\n"
1103 , VIM_NAME, getpid());
1104 fclose(fp);
1105 system("xterm -e gdb -x "VI_GDB_FILE);
1106 unlink(VI_GDB_FILE);
1107 }
1108 }
1109#endif
1110
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001112 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 for (i = 0; signal_info[i].sig != -1; i++)
1114 if (sigarg == signal_info[i].sig)
1115 break;
1116 deadly_signal = sigarg;
1117#endif
1118
Bram Moolenaar0f873732019-12-05 20:28:46 +01001119 full_screen = FALSE; // don't write message to the GUI, it might be
1120 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 /*
1122 * If something goes wrong after entering here, we may get here again.
1123 * When this happens, give a message and try to exit nicely (resetting the
1124 * terminal mode, etc.)
1125 * When this happens twice, just exit, don't even try to give a message,
1126 * stack may be corrupt or something weird.
1127 * When this still happens again (or memory was corrupted in such a way
1128 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1129 */
1130 if (entered >= 3)
1131 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001132 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 may_core_dump();
1134 if (entered >= 4)
1135 _exit(8);
1136 exit(7);
1137 }
1138 if (entered == 2)
1139 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001140 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001141 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 out_flush();
1143 getout(1);
1144 }
1145
Bram Moolenaar0f873732019-12-05 20:28:46 +01001146 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001148 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 signal_info[i].name);
1150#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001151 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001153
Bram Moolenaar0f873732019-12-05 20:28:46 +01001154 // Preserve files and exit. This sets the really_exiting flag to prevent
1155 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001156 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
Bram Moolenaar0f873732019-12-05 20:28:46 +01001158 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001159
Bram Moolenaar009b2592004-10-24 19:18:58 +00001160#ifdef NBDEBUG
1161 reset_signals();
1162 may_core_dump();
1163 abort();
1164#endif
1165
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 SIGRETURN;
1167}
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001170 * Invoked after receiving SIGCONT. We don't know what happened while
1171 * sleeping, deal with part of that.
1172 */
1173 static void
1174after_sigcont(void)
1175{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001176 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1177 // again later.
1178 oldtitle_outdated = TRUE;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001179
Bram Moolenaar2e310482018-08-21 13:09:10 +02001180 settmode(TMODE_RAW);
1181 need_check_timestamps = TRUE;
1182 did_check_timestamps = FALSE;
1183}
1184
1185#if defined(SIGCONT)
1186static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001187
1188/*
1189 * With multi-threading, suspending might not work immediately. Catch the
1190 * SIGCONT signal, which will be used as an indication whether the suspending
1191 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001192 *
1193 * On Linux, signal is not always handled immediately either.
1194 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001195 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001196 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001197 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001199static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001200static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
1202/*
1203 * signal handler for SIGCONT
1204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 static RETSIGTYPE
1206sigcont_handler SIGDEFARG(sigarg)
1207{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001208 if (in_mch_suspend)
1209 {
1210 sigcont_received = TRUE;
1211 }
1212 else
1213 {
1214 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1215 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1216 // back to a sane mode. We should redraw, but we can't really do that
1217 // in a signal handler, do a redraw later.
1218 after_sigcont();
1219 redraw_later(CLEAR);
1220 cursor_on_force();
1221 out_flush();
1222 }
1223
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 SIGRETURN;
1225}
1226#endif
1227
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001228#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001229# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001230static void *clip_star_save = NULL;
1231static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001232# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001233
1234/*
1235 * Called when Vim is going to sleep or execute a shell command.
1236 * We can't respond to requests for the X selections. Lose them, otherwise
1237 * other applications will hang. But first copy the text to cut buffer 0.
1238 */
1239 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001240loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001241{
1242 if (clip_star.owned || clip_plus.owned)
1243 {
1244 x11_export_final_selection();
1245 if (clip_star.owned)
1246 clip_lose_selection(&clip_star);
1247 if (clip_plus.owned)
1248 clip_lose_selection(&clip_plus);
1249 if (x11_display != NULL)
1250 XFlush(x11_display);
1251 }
1252}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001253
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001254# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001255/*
1256 * Save clipboard text to restore later.
1257 */
1258 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001259save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001260{
1261 if (clip_star.owned)
1262 clip_star_save = get_register('*', TRUE);
1263 if (clip_plus.owned)
1264 clip_plus_save = get_register('+', TRUE);
1265}
1266
1267/*
1268 * Restore clipboard text if no one own the X selection.
1269 */
1270 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001271restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001272{
1273 if (clip_star_save != NULL)
1274 {
1275 if (!clip_gen_owner_exists(&clip_star))
1276 put_register('*', clip_star_save);
1277 else
1278 free_register(clip_star_save);
1279 clip_star_save = NULL;
1280 }
1281 if (clip_plus_save != NULL)
1282 {
1283 if (!clip_gen_owner_exists(&clip_plus))
1284 put_register('+', clip_plus_save);
1285 else
1286 free_register(clip_plus_save);
1287 clip_plus_save = NULL;
1288 }
1289}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001290# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001291#endif
1292
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293/*
1294 * If the machine has job control, use it to suspend the program,
1295 * otherwise fake it by starting a new shell.
1296 */
1297 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001298mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001300 if (ignore_sigtstp)
1301 return;
1302
Bram Moolenaar041c7102020-05-30 18:14:57 +02001303#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001304 in_mch_suspend = TRUE;
1305
Bram Moolenaar0f873732019-12-05 20:28:46 +01001306 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001308 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309
1310# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001311 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001313# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 sigcont_received = FALSE;
1315# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001316
Bram Moolenaar0f873732019-12-05 20:28:46 +01001317 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001318
1319# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001320 /*
1321 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001322 * immediately, but somehow not all the time, probably because it's handled
1323 * in another thread. Do not call pause() because there would be race
1324 * condition which would hang Vim if signal happened in between the test of
1325 * sigcont_received and the call to pause(). If signal is not yet received,
1326 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1327 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001328 */
1329 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001330 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001331
Bram Moolenaar262735e2009-07-14 10:20:22 +00001332 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001333 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001336 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
Bram Moolenaar2e310482018-08-21 13:09:10 +02001338 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339#else
1340 suspend_shell();
1341#endif
1342}
1343
1344 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001345mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346{
1347 Columns = 80;
1348 Rows = 24;
1349
1350 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001351
1352#ifdef SIGTSTP
1353 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1354 // that indicates the shell (or program) that launched us does not support
1355 // tty job control and thus we should ignore that signal. If invoked as a
1356 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
1357 ignore_sigtstp = restricted || SIG_IGN == signal(SIGTSTP, SIG_ERR);
1358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001360
Bram Moolenaar56718732006-03-15 22:53:57 +00001361#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001362 mac_conv_init();
1363#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001364#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1365 win_clip_init();
1366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367}
1368
1369 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001370set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
1372#if defined(SIGWINCH)
1373 /*
1374 * WINDOW CHANGE signal is handled with sig_winch().
1375 */
1376 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1377#endif
1378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001380 // See mch_init() for the conditions under which we ignore SIGTSTP.
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001381 // In the GUI default TSTP processing is OK.
1382 // Checking both gui.in_use and gui.starting because gui.in_use is not set
1383 // at this point (set after menus are displayed), but gui.starting is set.
1384 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN
1385# ifdef FEAT_GUI
1386 : gui.in_use || gui.starting ? SIG_DFL
1387# endif
1388 : (RETSIGTYPE (*)())sig_tstp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001390#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 signal(SIGCONT, sigcont_handler);
1392#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001393#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 /*
1395 * We want to ignore breaking of PIPEs.
1396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 signal(SIGPIPE, SIG_IGN);
1398#endif
1399
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001401 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#endif
1403
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001404#ifdef SIGUSR1
1405 /*
1406 * Call user's handler on SIGUSR1
1407 */
1408 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
1409#endif
1410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 /*
1412 * Ignore alarm signals (Perl's alarm() generates it).
1413 */
1414#ifdef SIGALRM
1415 signal(SIGALRM, SIG_IGN);
1416#endif
1417
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001418#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 /*
1420 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1421 * work will be lost.
1422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1424#endif
1425
1426 /*
1427 * Arrange for other signals to gracefully shutdown Vim.
1428 */
1429 catch_signals(deathtrap, SIG_ERR);
1430
1431#if defined(FEAT_GUI) && defined(SIGHUP)
1432 /*
1433 * When the GUI is running, ignore the hangup signal.
1434 */
1435 if (gui.in_use)
1436 signal(SIGHUP, SIG_IGN);
1437#endif
1438}
1439
Bram Moolenaardf177f62005-02-22 08:39:57 +00001440#if defined(SIGINT) || defined(PROTO)
1441/*
1442 * Catch CTRL-C (only works while in Cooked mode).
1443 */
1444 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001445catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001446{
1447 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1448}
1449#endif
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001452reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453{
1454 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001455#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001456 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 signal(SIGCONT, SIG_DFL);
1458#endif
1459}
1460
1461 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001462catch_signals(
1463 RETSIGTYPE (*func_deadly)(),
1464 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 int i;
1467
1468 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 if (signal_info[i].deadly)
1471 {
1472#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1473 struct sigaction sa;
1474
Bram Moolenaar0f873732019-12-05 20:28:46 +01001475 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 sa.sa_handler = func_deadly;
1477 sigemptyset(&sa.sa_mask);
1478# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001479 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1480 // thread handling in combination with using the alternate stack:
1481 // pthread library functions try to use the stack pointer to
1482 // identify the current thread, causing a SEGV signal, which
1483 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 sa.sa_flags = 0;
1485# else
1486 sa.sa_flags = SA_ONSTACK;
1487# endif
1488 sigaction(signal_info[i].sig, &sa, NULL);
1489#else
1490# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1491 struct sigvec sv;
1492
Bram Moolenaar0f873732019-12-05 20:28:46 +01001493 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 sv.sv_handler = func_deadly;
1495 sv.sv_mask = 0;
1496 sv.sv_flags = SV_ONSTACK;
1497 sigvec(signal_info[i].sig, &sv, NULL);
1498# else
1499 signal(signal_info[i].sig, func_deadly);
1500# endif
1501#endif
1502 }
1503 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001504 {
1505 // Deal with non-deadly signals.
1506#ifdef SIGTSTP
1507 signal(signal_info[i].sig,
1508 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1509 ? SIG_IGN : func_other);
1510#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001512#endif
1513 }
1514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515}
1516
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001517#ifdef HAVE_SIGPROCMASK
1518 static void
1519block_signals(sigset_t *set)
1520{
1521 sigset_t newset;
1522 int i;
1523
1524 sigemptyset(&newset);
1525
1526 for (i = 0; signal_info[i].sig != -1; i++)
1527 sigaddset(&newset, signal_info[i].sig);
1528
Bram Moolenaar2e310482018-08-21 13:09:10 +02001529# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001530 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001531 sigaddset(&newset, SIGCONT);
1532# endif
1533
1534 sigprocmask(SIG_BLOCK, &newset, set);
1535}
1536
1537 static void
1538unblock_signals(sigset_t *set)
1539{
1540 sigprocmask(SIG_SETMASK, set, NULL);
1541}
1542#endif
1543
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001545 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001546 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1547 * return TRUE
1548 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1549 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001550 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001551 * Returns TRUE when Vim should exit.
1552 */
1553 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001554vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001555{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001556 static int got_signal = 0;
1557 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001558
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001559 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001560 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001561 case SIGNAL_BLOCK: blocked = TRUE;
1562 break;
1563
1564 case SIGNAL_UNBLOCK: blocked = FALSE;
1565 if (got_signal != 0)
1566 {
1567 kill(getpid(), got_signal);
1568 got_signal = 0;
1569 }
1570 break;
1571
1572 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001573 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001574 got_signal = sig;
1575#ifdef SIGPWR
1576 if (sig != SIGPWR)
1577#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001578 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001579 break;
1580 }
1581 return FALSE;
1582}
1583
1584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 * Check_win checks whether we have an interactive stdout.
1586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001588mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 if (isatty(1))
1591 return OK;
1592 return FAIL;
1593}
1594
1595/*
1596 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1597 */
1598 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001599mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600{
1601 if (isatty(read_cmd_fd))
1602 return TRUE;
1603 return FALSE;
1604}
1605
1606#ifdef FEAT_X11
1607
Bram Moolenaar651fca82021-11-29 20:39:38 +00001608# if defined(ELAPSED_TIMEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610/*
1611 * Give a message about the elapsed time for opening the X window.
1612 */
1613 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001614xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001616 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617}
1618# endif
1619#endif
1620
Bram Moolenaar651fca82021-11-29 20:39:38 +00001621#if defined(FEAT_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622/*
1623 * A few functions shared by X11 title and clipboard code.
1624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
1626static int got_x_error = FALSE;
1627
1628/*
1629 * X Error handler, otherwise X just exits! (very rude) -- webb
1630 */
1631 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001632x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001634 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 STRCAT(IObuff, _("\nVim: Got X error\n"));
1636
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001637 // In the GUI we cannot print a message and continue, because no X calls
1638 // are allowed here (causes my system to hang). Silently continuing seems
1639 // like the best alternative. Do preserve files, in case we crash.
1640 ml_sync_all(FALSE, FALSE);
1641
1642#ifdef FEAT_GUI
1643 if (!gui.in_use)
1644#endif
1645 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
Bram Moolenaar0f873732019-12-05 20:28:46 +01001647 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648}
1649
1650/*
1651 * Another X Error handler, just used to check for errors.
1652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001654x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655{
1656 got_x_error = TRUE;
1657 return 0;
1658}
1659
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001660/*
1661 * Return TRUE when connection to the X server is desired.
1662 */
1663 static int
1664x_connect_to_server(void)
1665{
1666 // No point in connecting if we are exiting or dying.
1667 if (exiting || v_dying)
1668 return FALSE;
1669
1670#if defined(FEAT_CLIENTSERVER)
1671 if (x_force_connect)
1672 return TRUE;
1673#endif
1674 if (x_no_connect)
1675 return FALSE;
1676
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001677 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001678 if (clip_exclude_prog != NULL)
1679 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001680 // Just in case we get called recursively, return FALSE. This could
1681 // happen if vpeekc() is used while executing the prog and it causes a
1682 // related callback to be invoked.
1683 if (regprog_in_use(clip_exclude_prog))
1684 return FALSE;
1685
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001686 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1687 return FALSE;
1688 }
1689 return TRUE;
1690}
1691
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001693# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694/*
1695 * An X IO Error handler, used to catch error while opening the display.
1696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001698x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001700 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001702# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001703 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705}
1706# endif
1707
1708/*
1709 * An X IO Error handler, used to catch terminal errors.
1710 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001711static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001714x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
1716 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001717 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 x11_window = 0;
1719 x11_display = NULL;
1720 xterm_Shell = (Widget)0;
1721
Bram Moolenaar0f873732019-12-05 20:28:46 +01001722 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001724# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001725 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001726# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001728
1729/*
1730 * If the X11 connection was lost try to restore it.
1731 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001732 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001733 */
1734 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001735may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001736{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001737 // No point in restoring the connecting if we are exiting or dying.
1738 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001739 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001740 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001741
1742# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001743 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001744 if (app_context != (XtAppContext)NULL)
1745 {
1746 XtDestroyApplicationContext(app_context);
1747 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001748 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001749 }
1750# endif
1751
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001752 setup_term_clip();
1753 get_x11_title(FALSE);
1754 }
1755}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001756
1757 void
1758ex_xrestore(exarg_T *eap)
1759{
1760 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1761 {
1762 if (xterm_display_allocated)
1763 vim_free(xterm_display);
1764 xterm_display = (char *)vim_strsave(eap->arg);
1765 xterm_display_allocated = TRUE;
1766 }
1767 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001768 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001769
1770 clear_xterm_clip();
1771 x11_window = 0;
1772 xterm_dpy_retry_count = 5; // Try reconnecting five times
1773 may_restore_clipboard();
1774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775#endif
1776
1777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 * Test if "dpy" and x11_window are valid by getting the window title.
1779 * I don't actually want it yet, so there may be a simpler call to use, but
1780 * this will cause the error handler x_error_check() to be called if anything
1781 * is wrong, such as the window pointer being invalid (as can happen when the
1782 * user changes his DISPLAY, but not his WINDOWID) -- webb
1783 */
1784 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001785test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 int (*old_handler)();
1788 XTextProperty text_prop;
1789
1790 old_handler = XSetErrorHandler(x_error_check);
1791 got_x_error = FALSE;
1792 if (XGetWMName(dpy, x11_window, &text_prop))
1793 XFree((void *)text_prop.value);
1794 XSync(dpy, False);
1795 (void)XSetErrorHandler(old_handler);
1796
1797 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001798 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
1800 return (got_x_error ? FAIL : OK);
1801}
1802#endif
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804
1805#ifdef FEAT_X11
1806
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001807static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809/*
1810 * try to get x11 window and display
1811 *
1812 * return FAIL for failure, OK otherwise
1813 */
1814 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001815get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816{
1817 char *winid;
1818 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001819#define XD_NONE 0 // x11_display not set here
1820#define XD_HERE 1 // x11_display opened here
1821#define XD_GUI 2 // x11_display used from gui.dpy
1822#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 static int x11_display_from = XD_NONE;
1824 static int did_set_error_handler = FALSE;
1825
1826 if (!did_set_error_handler)
1827 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001828 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 (void)XSetErrorHandler(x_error_handler);
1830 did_set_error_handler = TRUE;
1831 }
1832
Bram Moolenaar9372a112005-12-06 19:59:18 +00001833#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 if (gui.in_use)
1835 {
1836 /*
1837 * If the X11 display was opened here before, for the window where Vim
1838 * was started, close that one now to avoid a memory leak.
1839 */
1840 if (x11_display_from == XD_HERE && x11_display != NULL)
1841 {
1842 XCloseDisplay(x11_display);
1843 x11_display_from = XD_NONE;
1844 }
1845 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1846 {
1847 x11_display_from = XD_GUI;
1848 return OK;
1849 }
1850 x11_display = NULL;
1851 return FAIL;
1852 }
1853 else if (x11_display_from == XD_GUI)
1854 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001855 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 x11_window = 0;
1857 x11_display = NULL;
1858 x11_display_from = XD_NONE;
1859 }
1860#endif
1861
Bram Moolenaar0f873732019-12-05 20:28:46 +01001862 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 if (!x_connect_to_server())
1864 return FAIL;
1865
1866 /*
1867 * If WINDOWID not set, should try another method to find out
1868 * what the current window number is. The only code I know for
1869 * this is very complicated.
1870 * We assume that zero is invalid for WINDOWID.
1871 */
1872 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1873 x11_window = (Window)atol(winid);
1874
1875#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001876 if (xterm_dpy == x11_display)
1877 // x11_display may have been set to xterm_dpy elsewhere
1878 x11_display_from = XD_XTERM;
1879
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (xterm_dpy != NULL && x11_window != 0)
1881 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001882 // We may have checked it already, but Gnome terminal can move us to
1883 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001884 if (x11_display_from != XD_XTERM)
1885 {
1886 /*
1887 * If the X11 display was opened here before, for the window where
1888 * Vim was started, close that one now to avoid a memory leak.
1889 */
1890 if (x11_display_from == XD_HERE && x11_display != NULL)
1891 XCloseDisplay(x11_display);
1892 x11_display = xterm_dpy;
1893 x11_display_from = XD_XTERM;
1894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (test_x11_window(x11_display) == FAIL)
1896 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001897 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 x11_window = 0;
1899 x11_display = NULL;
1900 x11_display_from = XD_NONE;
1901 return FAIL;
1902 }
1903 return OK;
1904 }
1905#endif
1906
1907 if (x11_window == 0 || x11_display == NULL)
1908 result = -1;
1909
Bram Moolenaar0f873732019-12-05 20:28:46 +01001910 if (result != -1) // Have already been here and set this
1911 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
1913 if (x11_window != 0 && x11_display == NULL)
1914 {
1915#ifdef SET_SIG_ALARM
1916 RETSIGTYPE (*sig_save)();
1917#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001918#ifdef ELAPSED_FUNC
1919 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
1921 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001922 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923#endif
1924
1925#ifdef SET_SIG_ALARM
1926 /*
1927 * Opening the Display may hang if the DISPLAY setting is wrong, or
1928 * the network connection is bad. Set an alarm timer to get out.
1929 */
1930 sig_alarm_called = FALSE;
1931 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1932 (RETSIGTYPE (*)())sig_alarm);
1933 alarm(2);
1934#endif
1935 x11_display = XOpenDisplay(NULL);
1936
1937#ifdef SET_SIG_ALARM
1938 alarm(0);
1939 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1940 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001941 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942#endif
1943 if (x11_display != NULL)
1944 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001945# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001947 {
1948 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001949 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001950 verbose_leave();
1951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952# endif
1953 if (test_x11_window(x11_display) == FAIL)
1954 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001955 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 x11_window = 0;
1957 XCloseDisplay(x11_display);
1958 x11_display = NULL;
1959 }
1960 else
1961 x11_display_from = XD_HERE;
1962 }
1963 }
1964 if (x11_window == 0 || x11_display == NULL)
1965 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001966
1967# ifdef FEAT_EVAL
1968 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1969# endif
1970
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 return (result = OK);
1972}
1973
1974/*
1975 * Determine original x11 Window Title
1976 */
1977 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001978get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001980 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981}
1982
1983/*
1984 * Determine original x11 Window icon
1985 */
1986 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001987get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
1989 int retval = FALSE;
1990
1991 retval = get_x11_thing(FALSE, test_only);
1992
Bram Moolenaar0f873732019-12-05 20:28:46 +01001993 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 if (oldicon == NULL && !test_only)
1995 {
1996 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001997 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001999 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 }
2001
2002 return retval;
2003}
2004
2005 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002006get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002007 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01002008 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 XTextProperty text_prop;
2011 int retval = FALSE;
2012 Status status;
2013
2014 if (get_x11_windis() == OK)
2015 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002016 // Get window/icon name if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (get_title)
2018 status = XGetWMName(x11_display, x11_window, &text_prop);
2019 else
2020 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2021
2022 /*
2023 * If terminal is xterm, then x11_window may be a child window of the
2024 * outer xterm window that actually contains the window/icon name, so
2025 * keep traversing up the tree until a window with a title/icon is
2026 * found.
2027 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002028 // Previously this was only done for xterm and alike. I don't see a
Bram Moolenaar0f873732019-12-05 20:28:46 +01002029 // reason why it would fail for other terminal emulators.
2030 // if (term_is_xterm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
2032 Window root;
2033 Window parent;
2034 Window win = x11_window;
2035 Window *children;
2036 unsigned int num_children;
2037
2038 while (!status || text_prop.value == NULL)
2039 {
2040 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2041 &num_children))
2042 break;
2043 if (children)
2044 XFree((void *)children);
2045 if (parent == root || parent == 0)
2046 break;
2047
2048 win = parent;
2049 if (get_title)
2050 status = XGetWMName(x11_display, win, &text_prop);
2051 else
2052 status = XGetWMIconName(x11_display, win, &text_prop);
2053 }
2054 }
2055 if (status && text_prop.value != NULL)
2056 {
2057 retval = TRUE;
2058 if (!test_only)
2059 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002060 if (get_title)
2061 vim_free(oldtitle);
2062 else
2063 vim_free(oldicon);
Bram Moolenaara12a1612019-01-24 16:39:02 +01002064 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (get_title)
2067 oldtitle = vim_strsave((char_u *)text_prop.value);
2068 else
2069 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071 else
2072 {
2073 char **cl;
2074 Status transform_status;
2075 int n = 0;
2076
2077 transform_status = XmbTextPropertyToTextList(x11_display,
2078 &text_prop,
2079 &cl, &n);
2080 if (transform_status >= Success && n > 0 && cl[0])
2081 {
2082 if (get_title)
2083 oldtitle = vim_strsave((char_u *) cl[0]);
2084 else
2085 oldicon = vim_strsave((char_u *) cl[0]);
2086 XFreeStringList(cl);
2087 }
2088 else
2089 {
2090 if (get_title)
2091 oldtitle = vim_strsave((char_u *)text_prop.value);
2092 else
2093 oldicon = vim_strsave((char_u *)text_prop.value);
2094 }
2095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 }
2097 XFree((void *)text_prop.value);
2098 }
2099 }
2100 return retval;
2101}
2102
Bram Moolenaar0f873732019-12-05 20:28:46 +01002103// Xutf8 functions are not available on older systems. Note that on some
2104// systems X_HAVE_UTF8_STRING may be defined in a header file but
2105// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2106// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002107#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002108# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109# define USE_UTF8_STRING
2110# endif
2111#endif
2112
2113/*
2114 * Set x11 Window Title
2115 *
2116 * get_x11_windis() must be called before this and have returned OK
2117 */
2118 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002119set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002121 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2122 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2123 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124#ifdef USE_UTF8_STRING
2125 if (enc_utf8)
2126 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2127 NULL, NULL, 0, NULL, NULL, NULL);
2128 else
2129#endif
2130 {
2131#if XtSpecificationRelease >= 4
2132# ifdef FEAT_XFONTSET
2133 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2134 NULL, NULL, 0, NULL, NULL, NULL);
2135# else
2136 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002137 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
Bram Moolenaar0f873732019-12-05 20:28:46 +01002139 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002140 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 XSetWMProperties(x11_display, x11_window, &text_prop,
2142 NULL, NULL, 0, NULL, NULL, NULL);
2143# endif
2144#else
2145 XStoreName(x11_display, x11_window, (char *)title);
2146#endif
2147 }
2148 XFlush(x11_display);
2149}
2150
2151/*
2152 * Set x11 Window icon
2153 *
2154 * get_x11_windis() must be called before this and have returned OK
2155 */
2156 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002157set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002159 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160#ifdef USE_UTF8_STRING
2161 if (enc_utf8)
2162 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2163 NULL, 0, NULL, NULL, NULL);
2164 else
2165#endif
2166 {
2167#if XtSpecificationRelease >= 4
2168# ifdef FEAT_XFONTSET
2169 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2170 NULL, 0, NULL, NULL, NULL);
2171# else
2172 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002173 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002175 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2177 NULL, 0, NULL, NULL, NULL);
2178# endif
2179#else
2180 XSetIconName(x11_display, x11_window, (char *)icon);
2181#endif
2182 }
2183 XFlush(x11_display);
2184}
2185
Bram Moolenaar0f873732019-12-05 20:28:46 +01002186#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002189get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
2191 return FALSE;
2192}
2193
2194 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002195get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196{
2197 if (!test_only)
2198 {
2199 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002200 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002202 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
2204 return FALSE;
2205}
2206
Bram Moolenaar0f873732019-12-05 20:28:46 +01002207#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208
2209 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002210mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211{
2212 return get_x11_title(TRUE);
2213}
2214
2215 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002216mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217{
2218 return get_x11_icon(TRUE);
2219}
2220
2221/*
2222 * Set the window title and icon.
2223 */
2224 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002225mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226{
2227 int type = 0;
2228 static int recursive = 0;
2229
Bram Moolenaar0f873732019-12-05 20:28:46 +01002230 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002232 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 return;
2234
Bram Moolenaar0f873732019-12-05 20:28:46 +01002235 // When one of the X11 functions causes a deadly signal, we get here again
2236 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 if (recursive)
2238 return;
2239 ++recursive;
2240
2241 /*
2242 * if the window ID and the display is known, we may use X11 calls
2243 */
2244#ifdef FEAT_X11
2245 if (get_x11_windis() == OK)
2246 type = 1;
2247#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002248# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002249 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 if (gui.in_use)
2251 type = 1;
2252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253#endif
2254
2255 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002256 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 * than x11 calls, because the x11 calls don't always work
2258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 if ((type || *T_TS != NUL) && title != NULL)
2260 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002261 if (oldtitle_outdated)
2262 {
2263 oldtitle_outdated = FALSE;
2264 VIM_CLEAR(oldtitle);
2265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 if (oldtitle == NULL
2267#ifdef FEAT_GUI
2268 && !gui.in_use
2269#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002270 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 (void)get_x11_title(FALSE);
2272
Bram Moolenaar0f873732019-12-05 20:28:46 +01002273 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 term_settitle(title);
2275#ifdef FEAT_X11
2276 else
2277# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002278 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002280 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002282#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002283 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 else
2285 gui_mch_settitle(title, icon);
2286#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002287 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289
2290 if ((type || *T_CIS != NUL) && icon != NULL)
2291 {
2292 if (oldicon == NULL
2293#ifdef FEAT_GUI
2294 && !gui.in_use
2295#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002296 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 get_x11_icon(FALSE);
2298
2299 if (*T_CIS != NUL)
2300 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002301 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002303 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 out_flush();
2305 }
2306#ifdef FEAT_X11
2307 else
2308# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002309 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002311 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312#endif
2313 did_set_icon = TRUE;
2314 }
2315 --recursive;
2316}
2317
2318/*
2319 * Restore the window/icon title.
2320 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002321 * SAVE_RESTORE_TITLE only restore title
2322 * SAVE_RESTORE_ICON only restore icon
2323 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 */
2325 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002326mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
Bram Moolenaardac13472019-09-16 21:06:21 +02002328 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002329
Bram Moolenaar0f873732019-12-05 20:28:46 +01002330 // only restore the title or icon when it has been set
Bram Moolenaardac13472019-09-16 21:06:21 +02002331 mch_settitle(((which & SAVE_RESTORE_TITLE) && unix_did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002333 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2334
Bram Moolenaare5c83282019-05-03 23:15:37 +02002335 if (do_push_pop)
2336 {
2337 // pop and push from/to the stack
2338 term_pop_title(which);
2339 term_push_title(which);
2340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341}
2342
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
2344/*
2345 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002346 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 */
2348 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002349vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350{
2351 if (name == NULL)
2352 return FALSE;
2353 return (STRNICMP(name, "xterm", 5) == 0
2354 || STRNICMP(name, "nxterm", 6) == 0
2355 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002356 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002358 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 || STRCMP(name, "builtin_xterm") == 0);
2360}
2361
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002362#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2363/*
2364 * Return TRUE if "name" appears to be that of a terminal
2365 * known to support the xterm-style mouse protocol.
2366 * Relies on term_is_xterm having been set to its correct value.
2367 */
2368 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002369use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002370{
2371 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002372 && (term_is_xterm
2373 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002374 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002375 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002376 || STRICMP(name, "st") == 0
2377 || STRNICMP(name, "st-", 3) == 0
2378 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002379}
2380#endif
2381
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382/*
2383 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2384 * Return 1 for "xterm".
2385 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002386 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002387 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 */
2389 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002390use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002392 if (ttym_flags == TTYM_SGR)
2393 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002394 if (ttym_flags == TTYM_URXVT)
2395 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 if (ttym_flags == TTYM_XTERM2)
2397 return 2;
2398 if (ttym_flags == TTYM_XTERM)
2399 return 1;
2400 return 0;
2401}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
2403 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002404vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
2406 if (name == NULL)
2407 return FALSE;
2408 return (STRNICMP(name, "iris-ansi", 9) == 0
2409 || STRCMP(name, "builtin_iris-ansi") == 0);
2410}
2411
2412 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002413vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414{
2415 if (name == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002416 return FALSE; // actually all ANSI comp. terminals should be here
2417 // catch VT100 - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002418 return ((STRNICMP(name, "vt", 2) == 0
2419 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 || STRCMP(name, "builtin_vt320") == 0);
2421}
2422
2423/*
2424 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2425 * This should include all windowed terminal emulators.
2426 */
2427 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002428vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429{
2430 if (name == NULL)
2431 return FALSE;
2432 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2433 return TRUE;
2434 return ( STRNICMP(name, "hpterm", 6) == 0
2435 || STRNICMP(name, "sun-cmd", 7) == 0
2436 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002437 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 || STRNICMP(name, "dtterm", 6) == 0);
2439}
2440
2441/*
2442 * Insert user name in s[len].
2443 * Return OK if a name found.
2444 */
2445 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002446mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447{
2448#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002449 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 return OK;
2451#else
2452 return mch_get_uname(getuid(), s, len);
2453#endif
2454}
2455
2456/*
2457 * Insert user name for "uid" in s[len].
2458 * Return OK if a name found.
2459 */
2460 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002461mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2464 struct passwd *pw;
2465
2466 if ((pw = getpwuid(uid)) != NULL
2467 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2468 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002469 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 return OK;
2471 }
2472#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002473 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2474 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475}
2476
2477/*
2478 * Insert host name is s[len].
2479 */
2480
2481#ifdef HAVE_SYS_UTSNAME_H
2482 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002483mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484{
2485 struct utsname vutsname;
2486
2487 if (uname(&vutsname) < 0)
2488 *s = NUL;
2489 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002490 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002492#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
2494# ifdef HAVE_SYS_SYSTEMINFO_H
2495# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2496# endif
2497
2498 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002499mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
2501# ifdef VAXC
2502 vaxc$gethostname((char *)s, len);
2503# else
2504 gethostname((char *)s, len);
2505# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002506 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002508#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510/*
2511 * return process ID
2512 */
2513 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002514mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516 return (long)getpid();
2517}
2518
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002519/*
2520 * return TRUE if process "pid" is still running
2521 */
2522 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002523mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002524{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002525 // If there is no error the process must be running.
2526 if (kill(pid, 0) == 0)
2527 return TRUE;
2528#ifdef ESRCH
2529 // If the error is ESRCH then the process is not running.
2530 if (errno == ESRCH)
2531 return FALSE;
2532#endif
2533 // If the process is running and owned by another user we get EPERM. With
2534 // other errors the process might be running, assuming it is then.
2535 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002536}
2537
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002540strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541{
2542 extern int sys_nerr;
2543 extern char *sys_errlist[];
2544 static char er[20];
2545
2546 if (err > 0 && err < sys_nerr)
2547 return (sys_errlist[err]);
2548 sprintf(er, "Error %d", err);
2549 return er;
2550}
2551#endif
2552
2553/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002554 * Get name of current directory into buffer "buf" of length "len" bytes.
2555 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 * Return OK for success, FAIL for failure.
2557 */
2558 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002559mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
2561#if defined(USE_GETCWD)
2562 if (getcwd((char *)buf, len) == NULL)
2563 {
2564 STRCPY(buf, strerror(errno));
2565 return FAIL;
2566 }
2567 return OK;
2568#else
2569 return (getwd((char *)buf) != NULL ? OK : FAIL);
2570#endif
2571}
2572
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002574 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 *
2576 * return FAIL for failure, OK for success
2577 */
2578 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002579mch_FullName(
2580 char_u *fname,
2581 char_u *buf,
2582 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002583 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584{
2585 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002586#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002588 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 char_u olddir[MAXPATHL];
2591 char_u *p;
2592 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002593#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002594 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2595 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002596#endif
2597
Bram Moolenaar38323e42007-03-06 19:22:53 +00002598#ifdef VMS
2599 fname = vms_fixfilename(fname);
2600#endif
2601
Bram Moolenaara2442432007-04-26 14:26:37 +00002602#ifdef __CYGWIN__
2603 /*
2604 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2605 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002606# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002607 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2608 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002609 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2610 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002611# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002612 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002613# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002614 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002615#endif
2616
Bram Moolenaar0f873732019-12-05 20:28:46 +01002617 // Expand it if forced or not an absolute path.
2618 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002619 if ((force || !mch_isFullName(fname))
2620 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 {
2622 /*
2623 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002624 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 * This will get the correct path name with "../" things.
2626 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002627 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002629 if (STRCMP(p, "/..") == 0)
2630 // for "/path/dir/.." include the "/.."
2631 p += 3;
2632
Bram Moolenaar38323e42007-03-06 19:22:53 +00002633#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 /*
2635 * Use fchdir() if possible, it's said to be faster and more
2636 * reliable. But on SunOS 4 it might not work. Check this by
2637 * doing a fchdir() right now.
2638 */
2639 if (!dont_fchdir)
2640 {
2641 fd = open(".", O_RDONLY | O_EXTRA, 0);
2642 if (fd >= 0 && fchdir(fd) < 0)
2643 {
2644 close(fd);
2645 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002646 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 }
2648 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
Bram Moolenaar0f873732019-12-05 20:28:46 +01002651 // Only change directory when we are sure we can return to where
2652 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002654#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 (mch_dirname(olddir, MAXPATHL) == FAIL
2658 || mch_chdir((char *)olddir) != 0))
2659 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002660 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 retval = FAIL;
2662 }
2663 else
2664 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002665 // The directory is copied into buf[], to be able to remove
2666 // the file name without changing it (could be a string in
2667 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (p - fname >= len)
2669 retval = FAIL;
2670 else
2671 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002672 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002674 {
2675 // Path does not exist (yet). For a full path fail,
2676 // will use the path as-is. For a relative path use
2677 // the current directory and append the file name.
2678 if (mch_isFullName(fname))
2679 retval = FAIL;
2680 else
2681 p = NULL;
2682 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002683 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002685 else
2686 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 *buf = NUL;
2688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
2690 }
2691 if (mch_dirname(buf, len) == FAIL)
2692 {
2693 retval = FAIL;
2694 *buf = NUL;
2695 }
2696 if (p != NULL)
2697 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002698#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (fd >= 0)
2700 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002701 if (p_verbose >= 5)
2702 {
2703 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002704 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002705 verbose_leave();
2706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
2709 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 l = mch_chdir((char *)olddir);
2712 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002713 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 }
itchyny051a40c2021-10-20 10:00:05 +01002715#ifdef HAVE_FCHDIR
2716 if (fd >= 0)
2717 close(fd);
2718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002721 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002722 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002723#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002724 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002726 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002729
Bram Moolenaar0f873732019-12-05 20:28:46 +01002730 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002731 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 return FAIL;
2733
Bram Moolenaar0f873732019-12-05 20:28:46 +01002734 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 if (STRCMP(fname, ".") != 0)
2736 STRCAT(buf, fname);
2737
2738 return OK;
2739}
2740
2741/*
2742 * Return TRUE if "fname" does not depend on the current directory.
2743 */
2744 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002745mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002747#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 return ( fname[0] == '/' || fname[0] == '.' ||
2749 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2750 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2751 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002752#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754#endif
2755}
2756
Bram Moolenaar24552be2005-12-10 20:17:30 +00002757#if defined(USE_FNAME_CASE) || defined(PROTO)
2758/*
2759 * Set the case of the file name, if it already exists. This will cause the
2760 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002761 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002762 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002763 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002764fname_case(
2765 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002766 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002767{
2768 struct stat st;
2769 char_u *slash, *tail;
2770 DIR *dirp;
2771 struct dirent *dp;
2772
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002773 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002774 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002775 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002776 slash = vim_strrchr(name, '/');
2777 if (slash == NULL)
2778 {
2779 dirp = opendir(".");
2780 tail = name;
2781 }
2782 else
2783 {
2784 *slash = NUL;
2785 dirp = opendir((char *)name);
2786 *slash = '/';
2787 tail = slash + 1;
2788 }
2789
2790 if (dirp != NULL)
2791 {
2792 while ((dp = readdir(dirp)) != NULL)
2793 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002794 // Only accept names that differ in case and are the same byte
2795 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002796 if (STRICMP(tail, dp->d_name) == 0
2797 && STRLEN(tail) == STRLEN(dp->d_name))
2798 {
2799 char_u newname[MAXPATHL + 1];
2800 struct stat st2;
2801
Bram Moolenaar0f873732019-12-05 20:28:46 +01002802 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002803 vim_strncpy(newname, name, MAXPATHL);
2804 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2805 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002806 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002807 && st.st_ino == st2.st_ino
2808 && st.st_dev == st2.st_dev)
2809 {
2810 STRCPY(tail, dp->d_name);
2811 break;
2812 }
2813 }
2814 }
2815
2816 closedir(dirp);
2817 }
2818 }
2819}
2820#endif
2821
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822/*
2823 * Get file permissions for 'name'.
2824 * Returns -1 when it doesn't exist.
2825 */
2826 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002827mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828{
2829 struct stat statb;
2830
Bram Moolenaar0f873732019-12-05 20:28:46 +01002831 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832#ifdef VMS
2833 if (stat((char *)vms_fixfilename(name), &statb))
2834#else
2835 if (stat((char *)name, &statb))
2836#endif
2837 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002838#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002839 // The top bit makes the value negative, which means the file doesn't
2840 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002841 return statb.st_mode & ~S_ADDACE;
2842#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845}
2846
2847/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002848 * Set file permission for "name" to "perm".
2849 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 */
2851 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002852mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853{
2854 return (chmod((char *)
2855#ifdef VMS
2856 vms_fixfilename(name),
2857#else
2858 name,
2859#endif
2860 (mode_t)perm) == 0 ? OK : FAIL);
2861}
2862
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002863#if defined(HAVE_FCHMOD) || defined(PROTO)
2864/*
2865 * Set file permission for open file "fd" to "perm".
2866 * Return FAIL for failure, OK otherwise.
2867 */
2868 int
2869mch_fsetperm(int fd, long perm)
2870{
2871 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2872}
2873#endif
2874
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875#if defined(HAVE_ACL) || defined(PROTO)
2876# ifdef HAVE_SYS_ACL_H
2877# include <sys/acl.h>
2878# endif
2879# ifdef HAVE_SYS_ACCESS_H
2880# include <sys/access.h>
2881# endif
2882
2883# ifdef HAVE_SOLARIS_ACL
2884typedef struct vim_acl_solaris_T {
2885 int acl_cnt;
2886 aclent_t *acl_entry;
2887} vim_acl_solaris_T;
2888# endif
2889
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002890#if defined(HAVE_SELINUX) || defined(PROTO)
2891/*
2892 * Copy security info from "from_file" to "to_file".
2893 */
2894 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002895mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002896{
2897 if (from_file == NULL)
2898 return;
2899
2900 if (selinux_enabled == -1)
2901 selinux_enabled = is_selinux_enabled();
2902
2903 if (selinux_enabled > 0)
2904 {
Bram Moolenaar89560232020-10-09 23:04:47 +02002905 // Use "char *" instead of "security_context_t" to avoid a deprecation
2906 // warning.
2907 char *from_context = NULL;
2908 char *to_context = NULL;
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002909
2910 if (getfilecon((char *)from_file, &from_context) < 0)
2911 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002912 // If the filesystem doesn't support extended attributes,
2913 // the original had no special security context and the
2914 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002915 if (errno == EOPNOTSUPP)
2916 return;
2917
Bram Moolenaar32526b32019-01-19 17:43:09 +01002918 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002919 msg_outtrans(from_file);
2920 msg_putchar('\n');
2921 return;
2922 }
2923 if (getfilecon((char *)to_file, &to_context) < 0)
2924 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002925 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002926 msg_outtrans(to_file);
2927 msg_putchar('\n');
2928 freecon (from_context);
2929 return ;
2930 }
2931 if (strcmp(from_context, to_context) != 0)
2932 {
2933 if (setfilecon((char *)to_file, from_context) < 0)
2934 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002935 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002936 msg_outtrans(to_file);
2937 msg_putchar('\n');
2938 }
2939 }
2940 freecon(to_context);
2941 freecon(from_context);
2942 }
2943}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002944#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002945
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002946#if defined(HAVE_SMACK) && !defined(PROTO)
2947/*
2948 * Copy security info from "from_file" to "to_file".
2949 */
2950 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002951mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002952{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002953 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002954 {
2955 XATTR_NAME_SMACK,
2956 XATTR_NAME_SMACKEXEC,
2957 XATTR_NAME_SMACKMMAP
2958 };
2959
2960 char buffer[SMACK_LABEL_LEN];
2961 const char *name;
2962 int index;
2963 int ret;
2964 ssize_t size;
2965
2966 if (from_file == NULL)
2967 return;
2968
2969 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2970 / sizeof(smack_copied_attributes)[0]) ; index++)
2971 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002972 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002973 name = smack_copied_attributes[index];
2974
Bram Moolenaar0f873732019-12-05 20:28:46 +01002975 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002976 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2977 if (size >= 0)
2978 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002979 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002980 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2981 if (ret < 0)
2982 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002983 vim_snprintf((char *)IObuff, IOSIZE,
2984 _("Could not set security context %s for %s"),
2985 name, to_file);
2986 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002987 msg_putchar('\n');
2988 }
2989 }
2990 else
2991 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002992 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002993 switch (errno)
2994 {
2995 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002996 // extended attributes aren't supported or enabled
2997 // should a message be echoed? not sure...
2998 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002999
3000 case ERANGE:
3001 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003002 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003003 vim_snprintf((char *)IObuff, IOSIZE,
3004 _("Could not get security context %s for %s. Removing it!"),
3005 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003006 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003007 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003008 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003009
3010 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003011 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003012 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003013 // Silently ignore errors, apparently this happens when
3014 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003015 break;
3016 }
3017 }
3018 }
3019}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003020#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022/*
3023 * Return a pointer to the ACL of file "fname" in allocated memory.
3024 * Return NULL if the ACL is not available for whatever reason.
3025 */
3026 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003027mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
3029 vim_acl_T ret = NULL;
3030#ifdef HAVE_POSIX_ACL
3031 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3032#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003033#ifdef HAVE_SOLARIS_ZFS_ACL
3034 acl_t *aclent;
3035
3036 if (acl_get((char *)fname, 0, &aclent) < 0)
3037 return NULL;
3038 ret = (vim_acl_T)aclent;
3039#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040#ifdef HAVE_SOLARIS_ACL
3041 vim_acl_solaris_T *aclent;
3042
3043 aclent = malloc(sizeof(vim_acl_solaris_T));
3044 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3045 {
3046 free(aclent);
3047 return NULL;
3048 }
3049 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3050 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3051 {
3052 free(aclent->acl_entry);
3053 free(aclent);
3054 return NULL;
3055 }
3056 ret = (vim_acl_T)aclent;
3057#else
3058#if defined(HAVE_AIX_ACL)
3059 int aclsize;
3060 struct acl *aclent;
3061
3062 aclsize = sizeof(struct acl);
3063 aclent = malloc(aclsize);
3064 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3065 {
3066 if (errno == ENOSPC)
3067 {
3068 aclsize = aclent->acl_len;
3069 aclent = realloc(aclent, aclsize);
3070 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3071 {
3072 free(aclent);
3073 return NULL;
3074 }
3075 }
3076 else
3077 {
3078 free(aclent);
3079 return NULL;
3080 }
3081 }
3082 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003083#endif // HAVE_AIX_ACL
3084#endif // HAVE_SOLARIS_ACL
3085#endif // HAVE_SOLARIS_ZFS_ACL
3086#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 return ret;
3088}
3089
3090/*
3091 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3092 */
3093 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003094mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095{
3096 if (aclent == NULL)
3097 return;
3098#ifdef HAVE_POSIX_ACL
3099 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3100#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003101#ifdef HAVE_SOLARIS_ZFS_ACL
3102 acl_set((char *)fname, (acl_t *)aclent);
3103#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104#ifdef HAVE_SOLARIS_ACL
3105 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3106 ((vim_acl_solaris_T *)aclent)->acl_entry);
3107#else
3108#ifdef HAVE_AIX_ACL
3109 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003110#endif // HAVE_AIX_ACL
3111#endif // HAVE_SOLARIS_ACL
3112#endif // HAVE_SOLARIS_ZFS_ACL
3113#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114}
3115
3116 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003117mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
3119 if (aclent == NULL)
3120 return;
3121#ifdef HAVE_POSIX_ACL
3122 acl_free((acl_t)aclent);
3123#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003124#ifdef HAVE_SOLARIS_ZFS_ACL
3125 acl_free((acl_t *)aclent);
3126#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127#ifdef HAVE_SOLARIS_ACL
3128 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3129 free(aclent);
3130#else
3131#ifdef HAVE_AIX_ACL
3132 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003133#endif // HAVE_AIX_ACL
3134#endif // HAVE_SOLARIS_ACL
3135#endif // HAVE_SOLARIS_ZFS_ACL
3136#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137}
3138#endif
3139
3140/*
3141 * Set hidden flag for "name".
3142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003144mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003146 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147}
3148
3149/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003150 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 * return FALSE if "name" is not a directory
3152 * return FALSE for error
3153 */
3154 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003155mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
3157 struct stat statb;
3158
Bram Moolenaar0f873732019-12-05 20:28:46 +01003159 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 return FALSE;
3161 if (stat((char *)name, &statb))
3162 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164}
3165
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003166/*
3167 * return TRUE if "name" is a directory, NOT a symlink to a directory
3168 * return FALSE if "name" is not a directory
3169 * return FALSE for error
3170 */
3171 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003172mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003173{
3174 struct stat statb;
3175
Bram Moolenaar0f873732019-12-05 20:28:46 +01003176 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003177 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003178 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003179 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003180 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003181}
3182
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183/*
3184 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3185 */
3186 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003187executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188{
3189 struct stat st;
3190
3191 if (stat((char *)name, &st))
3192 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003193#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003194 // Like on Unix system file can have executable rights but not necessarily
3195 // be an executable, but on Unix is not a default for an ordinary file to
3196 // have an executable flag - on VMS it is in most cases.
3197 // Therefore, this check does not have any sense - let keep us to the
3198 // conventions instead:
3199 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003200 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003201 int vms_executable = 0;
3202 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3203 {
3204 if (strstr(vms_tolower((char*)name),".exe") != NULL
3205 || strstr(vms_tolower((char*)name),".com")!= NULL)
3206 vms_executable = 1;
3207 }
3208 return vms_executable;
3209#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212}
3213
3214/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003215 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003216 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 * Return -1 if unknown.
3218 */
3219 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003220mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222 char_u *buf;
3223 char_u *p, *e;
3224 int retval;
3225
Bram Moolenaar0f873732019-12-05 20:28:46 +01003226 // When "use_path" is false and if it's an absolute or relative path don't
3227 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003228 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003229 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003230 // There must be a path separator, files in the current directory
3231 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003232 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003233 {
3234 if (path != NULL)
3235 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003236 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003237 *path = FullName_save(name, TRUE);
3238 else
3239 *path = vim_strsave(name);
3240 }
3241 return TRUE;
3242 }
3243 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
3246 p = (char_u *)getenv("PATH");
3247 if (p == NULL || *p == NUL)
3248 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003249 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (buf == NULL)
3251 return -1;
3252
3253 /*
3254 * Walk through all entries in $PATH to check if "name" exists there and
3255 * is an executable file.
3256 */
3257 for (;;)
3258 {
3259 e = (char_u *)strchr((char *)p, ':');
3260 if (e == NULL)
3261 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003262 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 STRCPY(buf, "./");
3264 else
3265 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003266 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 add_pathsep(buf);
3268 }
3269 STRCAT(buf, name);
3270 retval = executable_file(buf);
3271 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003272 {
3273 if (path != NULL)
3274 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003275 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003276 *path = FullName_save(buf, TRUE);
3277 else
3278 *path = vim_strsave(buf);
3279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
3283 if (*e != ':')
3284 break;
3285 p = e + 1;
3286 }
3287
3288 vim_free(buf);
3289 return retval;
3290}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
3292/*
3293 * Check what "name" is:
3294 * NODE_NORMAL: file or directory (or doesn't exist)
3295 * NODE_WRITABLE: writable device, socket, fifo, etc.
3296 * NODE_OTHER: non-writable things
3297 */
3298 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003299mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
3301 struct stat st;
3302
3303 if (stat((char *)name, &st))
3304 return NODE_NORMAL;
3305 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3306 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003307 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003309 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 return NODE_WRITABLE;
3311}
3312
3313 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003314mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
3316#ifdef HAVE_CHECK_STACK_GROWTH
3317 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 check_stack_growth((char *)&i);
3320
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003321# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 get_stack_limit();
3323# endif
3324
3325#endif
3326
3327 /*
3328 * Setup an alternative stack for signals. Helps to catch signals when
3329 * running out of stack space.
3330 * Use of sigaltstack() is preferred, it's more portable.
3331 * Ignore any errors.
3332 */
3333#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003334 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 init_signal_stack();
3336#endif
3337}
3338
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003339#if defined(EXITFREE) || defined(PROTO)
3340 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003341mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003342{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003343# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3344 if (clip_star.owned)
3345 clip_lose_selection(&clip_star);
3346 if (clip_plus.owned)
3347 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003348# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003349# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003350 if (xterm_Shell != (Widget)0)
3351 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003352# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003353 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003354 if (xterm_dpy != NULL)
3355 XtCloseDisplay(xterm_dpy);
3356 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003357 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003358 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003359# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003360 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003361# endif
3362 }
3363# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003364# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003365# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003366 if (x11_display != NULL
3367# ifdef FEAT_XCLIPBOARD
3368 && x11_display != xterm_dpy
3369# endif
3370 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003371 XCloseDisplay(x11_display);
3372# endif
3373# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003374 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003375# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003376 vim_free(oldtitle);
3377 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003378}
3379#endif
3380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381/*
3382 * Output a newline when exiting.
3383 * Make sure the newline goes to the same stream as the text.
3384 */
3385 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003386exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003388 if (silent_mode)
3389 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 if (newline_on_exit || msg_didout)
3391 {
3392 if (msg_use_printf())
3393 {
3394 if (info_message)
3395 mch_msg("\n");
3396 else
3397 mch_errmsg("\r\n");
3398 }
3399 else
3400 out_char('\n');
3401 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003402 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003404 restore_cterm_colors(); // get original colors back
3405 msg_clr_eos_force(); // clear the rest of the display
3406 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 }
3408}
3409
Bram Moolenaarb4151682020-05-11 22:13:28 +02003410#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003411# if (defined(__GNUC__) \
3412 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3413 || (defined(__clang__) && (__clang_major__ >= 12))
3414extern void __gcov_dump(void);
3415extern void __gcov_reset(void);
3416# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3417# else
3418extern void __gcov_flush(void);
3419# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003420#endif
3421
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003423mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
3425 exiting = TRUE;
3426
3427#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3428 x11_export_final_selection();
3429#endif
3430
3431#ifdef FEAT_GUI
3432 if (!gui.in_use)
3433#endif
3434 {
3435 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003436 if (!is_not_a_term())
3437 {
3438 // restore xterm title and icon name
3439 mch_restore_title(SAVE_RESTORE_BOTH);
3440 term_pop_title(SAVE_RESTORE_BOTH);
3441 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003442
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 /*
3444 * When t_ti is not empty but it doesn't cause swapping terminal
3445 * pages, need to output a newline when msg_didout is set. But when
3446 * t_ti does swap pages it should not go to the shell page. Do this
3447 * before stoptermcap().
3448 */
3449 if (swapping_screen() && !newline_on_exit)
3450 exit_scroll();
3451
Bram Moolenaar0f873732019-12-05 20:28:46 +01003452 // Stop termcap: May need to check for T_CRV response, which
3453 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 stoptermcap();
3455
3456 /*
3457 * A newline is only required after a message in the alternate screen.
3458 * This is set to TRUE by wait_return().
3459 */
3460 if (!swapping_screen() || newline_on_exit)
3461 exit_scroll();
3462
Bram Moolenaar0f873732019-12-05 20:28:46 +01003463 // Cursor may have been switched off without calling starttermcap()
3464 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 if (full_screen)
3466 cursor_on();
3467 }
3468 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003469 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003470
3471#ifdef USE_GCOV_FLUSH
3472 // Flush coverage info before possibly being killed by a deadly signal.
3473 __gcov_flush();
3474#endif
3475
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 may_core_dump();
3477#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 gui_exit(r);
3480#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003481
Bram Moolenaar56718732006-03-15 22:53:57 +00003482#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003483 mac_conv_cleanup();
3484#endif
3485
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003487 // A core dump won't be created if the signal handler
3488 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 if (deadly_signal != 0)
3490 return;
3491#endif
3492
Bram Moolenaar009b2592004-10-24 19:18:58 +00003493#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003494 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003495#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003496
3497#ifdef EXITFREE
3498 free_all_mem();
3499#endif
3500
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 exit(r);
3502}
3503
3504 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003505may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506{
3507 if (deadly_signal != 0)
3508 {
3509 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003510 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 }
3512}
3513
3514#ifndef VMS
3515
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003516/*
3517 * Get the file descriptor to use for tty operations.
3518 */
3519 static int
3520get_tty_fd(int fd)
3521{
3522 int tty_fd = fd;
3523
3524#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3525 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3526 // "fd" when it is a master device.
3527 if (mch_isatty(fd) > 1)
3528 {
3529 char *name;
3530
3531 name = ptsname(fd);
3532 if (name == NULL)
3533 return -1;
3534
3535 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3536 if (tty_fd < 0)
3537 return -1;
3538 }
3539#endif
3540 return tty_fd;
3541}
3542
3543 static int
3544mch_tcgetattr(int fd, void *term)
3545{
3546 int tty_fd;
3547 int retval = -1;
3548
3549 tty_fd = get_tty_fd(fd);
3550 if (tty_fd >= 0)
3551 {
3552#ifdef NEW_TTY_SYSTEM
3553# ifdef HAVE_TERMIOS_H
3554 retval = tcgetattr(tty_fd, (struct termios *)term);
3555# else
3556 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3557# endif
3558#else
3559 // for "old" tty systems
3560 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3561#endif
3562 if (tty_fd != fd)
3563 close(tty_fd);
3564 }
3565 return retval;
3566}
3567
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003569mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
3571 static int first = TRUE;
3572
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003573#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574# ifdef HAVE_TERMIOS_H
3575 static struct termios told;
3576 struct termios tnew;
3577# else
3578 static struct termio told;
3579 struct termio tnew;
3580# endif
3581
3582 if (first)
3583 {
3584 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003585 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
3587
3588 tnew = told;
3589 if (tmode == TMODE_RAW)
3590 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003591 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003592 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3593 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003595# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003596 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597# endif
3598 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003599# ifdef ONLCR
3600 // Don't map NL -> CR NL, we do it ourselves.
3601 // Also disable expanding tabs if possible.
3602# ifdef XTABS
3603 tnew.c_oflag &= ~(ONLCR | XTABS);
3604# else
3605# ifdef TAB3
3606 tnew.c_oflag &= ~(ONLCR | TAB3);
3607# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003609# endif
3610# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003612 tnew.c_cc[VMIN] = 1; // return after 1 char
3613 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
3615 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003616 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003617 // Also reset ICANON here, otherwise on Solaris select() won't see
3618 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003619 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003620 tnew.c_cc[VMIN] = 1; // return after 1 char
3621 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
3624# if defined(HAVE_TERMIOS_H)
3625 {
3626 int n = 10;
3627
Bram Moolenaar0f873732019-12-05 20:28:46 +01003628 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3629 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3631 && errno == EINTR && n > 0)
3632 --n;
3633 }
3634# else
3635 ioctl(read_cmd_fd, TCSETA, &tnew);
3636# endif
3637
3638#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 /*
3640 * for "old" tty systems
3641 */
3642# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003643# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644# endif
3645 static struct sgttyb ttybold;
3646 struct sgttyb ttybnew;
3647
3648 if (first)
3649 {
3650 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003651 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 }
3653
3654 ttybnew = ttybold;
3655 if (tmode == TMODE_RAW)
3656 {
3657 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3658 ttybnew.sg_flags |= RAW;
3659 }
3660 else if (tmode == TMODE_SLEEP)
3661 ttybnew.sg_flags &= ~(ECHO);
3662 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3663#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003664 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665}
3666
3667/*
3668 * Try to get the code for "t_kb" from the stty setting
3669 *
3670 * Even if termcap claims a backspace key, the user's setting *should*
3671 * prevail. stty knows more about reality than termcap does, and if
3672 * somebody's usual erase key is DEL (which, for most BSD users, it will
3673 * be), they're going to get really annoyed if their erase key starts
3674 * doing forward deletes for no reason. (Eric Fischer)
3675 */
3676 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003677get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003679 ttyinfo_T info;
3680 char_u buf[2];
3681 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003683 if (get_tty_info(read_cmd_fd, &info) == OK)
3684 {
3685 intr_char = info.interrupt;
3686 buf[0] = info.backspace;
3687 buf[1] = NUL;
3688 add_termcode((char_u *)"kb", buf, FALSE);
3689
Bram Moolenaar0f873732019-12-05 20:28:46 +01003690 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003691 p = find_termcode((char_u *)"kD");
3692 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3693 do_fixdel(NULL);
3694 }
3695}
3696
3697/*
3698 * Obtain the characters that Backspace and Enter produce on "fd".
3699 * Returns OK or FAIL.
3700 */
3701 int
3702get_tty_info(int fd, ttyinfo_T *info)
3703{
3704#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705# ifdef HAVE_TERMIOS_H
3706 struct termios keys;
3707# else
3708 struct termio keys;
3709# endif
3710
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003711 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003713 info->backspace = keys.c_cc[VERASE];
3714 info->interrupt = keys.c_cc[VINTR];
3715 if (keys.c_iflag & ICRNL)
3716 info->enter = NL;
3717 else
3718 info->enter = CAR;
3719 if (keys.c_oflag & ONLCR)
3720 info->nl_does_cr = TRUE;
3721 else
3722 info->nl_does_cr = FALSE;
3723 return OK;
3724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003726 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 struct sgttyb keys;
3728
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003729 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003731 info->backspace = keys.sg_erase;
3732 info->interrupt = keys.sg_kill;
3733 info->enter = CAR;
3734 info->nl_does_cr = TRUE;
3735 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003738 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739}
3740
Bram Moolenaar0f873732019-12-05 20:28:46 +01003741#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003743static int mouse_ison = FALSE;
3744
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745/*
3746 * Set mouse clicks on or off.
3747 */
3748 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003749mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003751#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003752 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 int xterm_mouse_vers;
3755
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003756#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003757 if (!on)
3758 // Make sure not tracing mouse movements. Important when a button-down
3759 // was received but no release yet.
3760 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003761#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003762
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003763 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003764#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003765 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003766#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003767 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003768 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 return;
3770
3771 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003772
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003773#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003774 if (ttym_flags == TTYM_URXVT)
3775 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003776 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003777 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003778 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003779#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003780
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003781 if (ttym_flags == TTYM_SGR)
3782 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003783 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003784 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003785 mouse_ison = on;
3786 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003787
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003788#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003789 if (bevalterm_ison != (p_bevalterm && on))
3790 {
3791 bevalterm_ison = (p_bevalterm && on);
3792 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003793 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003794 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003795 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003796#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003797
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (xterm_mouse_vers > 0)
3799 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003800 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 out_str_nf((char_u *)
3802 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003803 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003804#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003805 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003806#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003807 "\033[?1002h")
3808 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003809 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003811 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003812 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 }
3814
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003815#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 else if (ttym_flags == TTYM_DEC)
3817 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003818 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003820 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003822 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003826#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 else
3828 {
3829 if (on)
3830 {
3831 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003832 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834 else
3835 {
3836 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003837 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003842#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003843 else
3844 {
3845 if (on)
3846 {
3847 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003848 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003849 }
3850 else
3851 {
3852 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003853 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003854 }
3855 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003856#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003857
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003858#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 else
3860 {
3861 if (on)
3862 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003863 // D - Enable Mouse up/down messages
3864 // L - Enable Left Button Reporting
3865 // M - Enable Middle Button Reporting
3866 // R - Enable Right Button Reporting
3867 // K - Enable SHIFT and CTRL key Reporting
3868 // + - Enable Advanced messaging of mouse moves and up/down messages
3869 // Q - Quiet No Ack
3870 // # - Numeric value of mouse pointer required
3871 // 0 = Multiview 2000 cursor, used as standard
3872 // 1 = Windows Arrow
3873 // 2 = Windows I Beam
3874 // 3 = Windows Hour Glass
3875 // 4 = Windows Cross Hair
3876 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003877# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003878 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003879 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003880# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003881 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003882# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003883 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 }
3885 else
3886 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003887 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003888 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 }
3890 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003891#endif
3892#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 else
3894 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003895 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if (on)
3897 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3898 else
3899 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003900 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903}
3904
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003905#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003906/*
3907 * Called when 'balloonevalterm' changed.
3908 */
3909 void
3910mch_bevalterm_changed(void)
3911{
3912 mch_setmouse(mouse_ison);
3913}
3914#endif
3915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916/*
3917 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3918 */
3919 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003920check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921{
3922# ifdef FEAT_MOUSE_XTERM
3923 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003924# ifdef FEAT_MOUSE_URXVT
3925 && use_xterm_mouse() != 3
3926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927# ifdef FEAT_GUI
3928 && !gui.in_use
3929# endif
3930 )
3931 {
3932 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003933 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 if (*p_mouse != NUL)
3935 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003936 // force mouse off and maybe on to send possibly new mouse
3937 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 mch_setmouse(FALSE);
3939 setmouse();
3940 }
3941 }
3942 else
3943 del_mouse_termcode(KS_MOUSE);
3944# endif
3945
3946# ifdef FEAT_MOUSE_GPM
3947 if (!use_xterm_mouse()
3948# ifdef FEAT_GUI
3949 && !gui.in_use
3950# endif
3951 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003952 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003953 else
3954 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955# endif
3956
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003957# ifdef FEAT_SYSMOUSE
3958 if (!use_xterm_mouse()
3959# ifdef FEAT_GUI
3960 && !gui.in_use
3961# endif
3962 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003963 set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003964# endif
3965
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003967 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 if (!use_xterm_mouse()
3969# ifdef FEAT_GUI
3970 && !gui.in_use
3971# endif
3972 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003973 set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 else
3975 del_mouse_termcode(KS_JSBTERM_MOUSE);
3976# endif
3977
3978# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003979 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3980 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 if (!use_xterm_mouse()
3982# ifdef FEAT_GUI
3983 && !gui.in_use
3984# endif
3985 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003986 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 else
3988 del_mouse_termcode(KS_NETTERM_MOUSE);
3989# endif
3990
3991# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003992 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003993 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994# ifdef FEAT_GUI
3995 && !gui.in_use
3996# endif
3997 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003998 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003999 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 else
4001 del_mouse_termcode(KS_DEC_MOUSE);
4002# endif
4003# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004004 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004005 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006# ifdef FEAT_GUI
4007 && !gui.in_use
4008# endif
4009 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004010 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 else
4012 del_mouse_termcode(KS_PTERM_MOUSE);
4013# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004014# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004015 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004016# ifdef FEAT_GUI
4017 && !gui.in_use
4018# endif
4019 )
4020 {
4021 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004022 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004023
4024 if (*p_mouse != NUL)
4025 {
4026 mch_setmouse(FALSE);
4027 setmouse();
4028 }
4029 }
4030 else
4031 del_mouse_termcode(KS_URXVT_MOUSE);
4032# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004033 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004034# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004035 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004036# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004037 )
4038 {
4039 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004040 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004041
4042 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004043 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004044
4045 if (*p_mouse != NUL)
4046 {
4047 mch_setmouse(FALSE);
4048 setmouse();
4049 }
4050 }
4051 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004052 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004053 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004054 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058#ifndef VMS
4059
4060/*
4061 * Try to get the current window size:
4062 * 1. with an ioctl(), most accurate method
4063 * 2. from the environment variables LINES and COLUMNS
4064 * 3. from the termcap
4065 * 4. keep using the old values
4066 * Return OK when size could be determined, FAIL otherwise.
4067 */
4068 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004069mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070{
4071 long rows = 0;
4072 long columns = 0;
4073 char_u *p;
4074
4075 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 * 1. try using an ioctl. It is the most accurate method.
4077 *
4078 * Try using TIOCGWINSZ first, some systems that have it also define
4079 * TIOCGSIZE but don't have a struct ttysize.
4080 */
4081# ifdef TIOCGWINSZ
4082 {
4083 struct winsize ws;
4084 int fd = 1;
4085
Bram Moolenaar0f873732019-12-05 20:28:46 +01004086 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 if (!isatty(fd) && isatty(read_cmd_fd))
4088 fd = read_cmd_fd;
4089 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4090 {
4091 columns = ws.ws_col;
4092 rows = ws.ws_row;
4093 }
4094 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004095# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096# ifdef TIOCGSIZE
4097 {
4098 struct ttysize ts;
4099 int fd = 1;
4100
Bram Moolenaar0f873732019-12-05 20:28:46 +01004101 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 if (!isatty(fd) && isatty(read_cmd_fd))
4103 fd = read_cmd_fd;
4104 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4105 {
4106 columns = ts.ts_cols;
4107 rows = ts.ts_lines;
4108 }
4109 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004110# endif // TIOCGSIZE
4111# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
4113 /*
4114 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004115 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4116 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004118 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 {
4120 if ((p = (char_u *)getenv("LINES")))
4121 rows = atoi((char *)p);
4122 if ((p = (char_u *)getenv("COLUMNS")))
4123 columns = atoi((char *)p);
4124 }
4125
4126#ifdef HAVE_TGETENT
4127 /*
4128 * 3. try reading "co" and "li" entries from termcap
4129 */
4130 if (columns == 0 || rows == 0)
4131 getlinecol(&columns, &rows);
4132#endif
4133
4134 /*
4135 * 4. If everything fails, use the old values
4136 */
4137 if (columns <= 0 || rows <= 0)
4138 return FAIL;
4139
4140 Rows = rows;
4141 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004142 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 return OK;
4144}
4145
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004146#if defined(FEAT_TERMINAL) || defined(PROTO)
4147/*
4148 * Report the windows size "rows" and "cols" to tty "fd".
4149 */
4150 int
4151mch_report_winsize(int fd, int rows, int cols)
4152{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004153 int tty_fd;
4154 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004155
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004156 tty_fd = get_tty_fd(fd);
4157 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004158 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004159# if defined(TIOCSWINSZ)
4160 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004161
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004162 ws.ws_col = cols;
4163 ws.ws_row = rows;
4164 ws.ws_xpixel = cols * 5;
4165 ws.ws_ypixel = rows * 10;
4166 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4167 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4168 retval == 0 ? "success" : "failed");
4169# elif defined(TIOCSSIZE)
4170 struct ttysize ts;
4171
4172 ts.ts_cols = cols;
4173 ts.ts_lines = rows;
4174 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4175 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4176 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004177# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004178 if (tty_fd != fd)
4179 close(tty_fd);
4180 }
4181 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004182}
4183#endif
4184
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185/*
4186 * Try to set the window size to Rows and Columns.
4187 */
4188 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004189mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190{
4191 if (*T_CWS)
4192 {
4193 /*
4194 * NOTE: if you get an error here that term_set_winsize() is
4195 * undefined, check the output of configure. It could probably not
4196 * find a ncurses, termcap or termlib library.
4197 */
4198 term_set_winsize((int)Rows, (int)Columns);
4199 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004200 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202}
4203
Bram Moolenaar0f873732019-12-05 20:28:46 +01004204#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
4206/*
4207 * Rows and/or Columns has changed.
4208 */
4209 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004210mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004212 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213}
4214
Bram Moolenaar205b8862011-09-07 15:04:31 +02004215/*
4216 * Wait for process "child" to end.
4217 * Return "child" if it exited properly, <= 0 on error.
4218 */
4219 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004220wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004221{
4222 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004223 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004224
4225 while (wait_pid != child)
4226 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004227 // When compiled with Python threads are probably used, in which case
4228 // wait() sometimes hangs for no obvious reason. Use waitpid()
4229 // instead and loop (like the GUI). Also needed for other interfaces,
4230 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004231# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004232 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004233# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004234 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004235# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004236 if (wait_pid == 0)
4237 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004238 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004239 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004240 if (++delay_msec > 10)
4241 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004242 continue;
4243 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004244 if (wait_pid <= 0
4245# ifdef ECHILD
4246 && errno == ECHILD
4247# endif
4248 )
4249 break;
4250 }
4251 return wait_pid;
4252}
4253
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004254#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004255/*
4256 * Set the environment for a child process.
4257 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004258 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004259set_child_environment(
4260 long rows,
4261 long columns,
4262 char *term,
4263 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004264{
4265# ifdef HAVE_SETENV
4266 char envbuf[50];
4267# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004268 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004269 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004270 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004271 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004272 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004273# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004274 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004275# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004276# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004277 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004278# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004279# endif
4280
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004281# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004282 setenv("TERM", term, 1);
4283 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004284 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004285 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004286 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004287 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004288 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004289 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004290 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004291# ifdef FEAT_TERMINAL
4292 if (is_terminal)
4293 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004294 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004295 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4296 }
4297# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004298# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004299 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004300# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004301# else
4302 /*
4303 * Putenv does not copy the string, it has to remain valid.
4304 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004305 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004306 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004307 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4308 putenv(envbuf_Term);
4309 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004310 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004311 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4312 putenv(envbuf_Lines);
4313 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4314 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004315 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004316 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004317 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004318# ifdef FEAT_TERMINAL
4319 if (is_terminal)
4320 {
4321 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004322 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004323 putenv(envbuf_Version);
4324 }
4325# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004326# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004327 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4328 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4329 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004330# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004331# endif
4332}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004333
4334 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004335set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004336{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004337 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004338}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004339#endif
4340
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004341#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004342/*
4343 * Open a PTY, with FD for the master and slave side.
4344 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004345 * When successful both file descriptors are stored and the allocated pty name
4346 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004347 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004348 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004349open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004350{
4351 char *tty_name;
4352
Bram Moolenaar59386482019-02-10 22:43:46 +01004353 if (name1 != NULL)
4354 *name1 = NULL;
4355 if (name2 != NULL)
4356 *name2 = NULL;
4357
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004358 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004359 if (*pty_master_fd >= 0)
4360 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004361 // Leaving out O_NOCTTY may lead to waitpid() always returning
4362 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4363 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004364#ifdef O_NOCTTY
4365 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4366#else
4367 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4368#endif
4369 if (*pty_slave_fd < 0)
4370 {
4371 close(*pty_master_fd);
4372 *pty_master_fd = -1;
4373 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004374 else
4375 {
4376 if (name1 != NULL)
4377 *name1 = vim_strsave((char_u *)tty_name);
4378 if (name2 != NULL)
4379 *name2 = vim_strsave((char_u *)tty_name);
4380 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004381 }
4382}
4383#endif
4384
Bram Moolenaarfae42832017-08-01 22:24:26 +02004385/*
4386 * Send SIGINT to a child process if "c" is an interrupt character.
4387 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004388 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004389may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4390{
4391# ifdef SIGINT
4392 if (c == Ctrl_C || c == intr_char)
4393 {
4394# ifdef HAVE_SETSID
4395 kill(-pid, SIGINT);
4396# else
4397 kill(0, SIGINT);
4398# endif
4399 if (wpid > 0)
4400 kill(wpid, SIGINT);
4401 }
4402# endif
4403}
4404
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004405#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004406
Bram Moolenaar0f873732019-12-05 20:28:46 +01004407/*
4408 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4409 * pointers, the last one is NULL.
4410 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4411 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004412 int
4413unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004414 char_u *cmd,
4415 char ***argvp,
4416 char_u **sh_tofree,
4417 char_u **shcf_tofree)
4418{
4419 char **argv = NULL;
4420 int argc;
4421
4422 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004423 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004424 return FAIL;
4425
4426 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4427 return FAIL;
4428 *argvp = argv;
4429
4430 if (cmd != NULL)
4431 {
4432 char_u *s;
4433 char_u *p;
4434
4435 if (extra_shell_arg != NULL)
4436 argv[argc++] = (char *)extra_shell_arg;
4437
Bram Moolenaar0f873732019-12-05 20:28:46 +01004438 // Break 'shellcmdflag' into white separated parts. This doesn't
4439 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004440 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004441 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004442 return FAIL;
4443 s = *shcf_tofree;
4444 p = p_shcf;
4445 while (*p != NUL)
4446 {
4447 argv[argc++] = (char *)s;
4448 while (*p && *p != ' ' && *p != TAB)
4449 *s++ = *p++;
4450 *s++ = NUL;
4451 p = skipwhite(p);
4452 }
4453
4454 argv[argc++] = (char *)cmd;
4455 }
4456 argv[argc] = NULL;
4457 return OK;
4458}
4459#endif
4460
4461#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4462/*
4463 * Use a terminal window to run a shell command in.
4464 */
4465 static int
4466mch_call_shell_terminal(
4467 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004468 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004469{
4470 jobopt_T opt;
4471 char **argv = NULL;
4472 char_u *tofree1 = NULL;
4473 char_u *tofree2 = NULL;
4474 int retval = -1;
4475 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004476 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004477 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004478 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004479
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004480 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004481 goto theend;
4482
4483 init_job_options(&opt);
4484 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4485 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004486 if (buf == NULL)
4487 goto theend;
4488
4489 job = term_getjob(buf->b_term);
4490 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004491
Bram Moolenaar0f873732019-12-05 20:28:46 +01004492 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004493 aucmd_prepbuf(&aco, buf);
4494
4495 clear_oparg(&oa);
4496 while (term_use_loop())
4497 {
4498 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4499 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004500 // If terminal_loop() returns OK we got a key that is handled
4501 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004502 if (terminal_loop(TRUE) == OK)
4503 normal_cmd(&oa, TRUE);
4504 }
4505 else
4506 normal_cmd(&oa, TRUE);
4507 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004508 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004509 ch_log(NULL, "system command finished");
4510
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004511 job_unref(job);
4512
Bram Moolenaar0f873732019-12-05 20:28:46 +01004513 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004514 aucmd_restbuf(&aco);
4515
4516 wait_return(TRUE);
4517 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4518
4519theend:
4520 vim_free(argv);
4521 vim_free(tofree1);
4522 vim_free(tofree2);
4523 return retval;
4524}
4525#endif
4526
4527#ifdef USE_SYSTEM
4528/*
4529 * Use system() to start the shell: simple but slow.
4530 */
4531 static int
4532mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004533 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004534 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535{
4536#ifdef VMS
4537 char *ifn = NULL;
4538 char *ofn = NULL;
4539#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004540 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004541 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004542 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
4544 out_flush();
4545
4546 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004547 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
Bram Moolenaar62b42182010-09-21 22:09:37 +02004549# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004550 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004551 loose_clipboard();
4552# endif
4553
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 if (cmd == NULL)
4555 x = system((char *)p_sh);
4556 else
4557 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004558# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 if (ofn = strchr((char *)cmd, '>'))
4560 *ofn++ = '\0';
4561 if (ifn = strchr((char *)cmd, '<'))
4562 {
4563 char *p;
4564
4565 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004566 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (p)
4568 *p = '\0';
4569 }
4570 if (ofn)
4571 x = vms_sys((char *)cmd, ofn, ifn);
4572 else
4573 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004574# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004575 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004577 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 if (newcmd == NULL)
4579 x = 0;
4580 else
4581 {
4582 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4583 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4584 (char *)p_shcf,
4585 (char *)cmd);
4586 x = system((char *)newcmd);
4587 vim_free(newcmd);
4588 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004589# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591# ifdef VMS
4592 x = vms_sys_status(x);
4593# endif
4594 if (emsg_silent)
4595 ;
4596 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004597 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 else if (x && !(options & SHELL_SILENT))
4599 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004600 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 msg_outnum((long)x);
4602 msg_putchar('\n');
4603 }
4604
4605 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004606 {
4607 // The shell may have messed with the mode, always set it.
4608 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004609 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004612# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4613 restore_clipboard();
4614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004616}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617
Bram Moolenaar0f873732019-12-05 20:28:46 +01004618#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
Bram Moolenaar0f873732019-12-05 20:28:46 +01004620# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4621 // 127, some shells use that already
4622# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623
Bram Moolenaar13568252018-03-16 20:46:58 +01004624/*
4625 * Don't use system(), use fork()/exec().
4626 */
4627 static int
4628mch_call_shell_fork(
4629 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004630 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004631{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004632 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004634 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 pid_t wait_pid = 0;
4636# ifdef HAVE_UNION_WAIT
4637 union wait status;
4638# else
4639 int status = -1;
4640# endif
4641 int retval = -1;
4642 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004643 char_u *tofree1 = NULL;
4644 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004646 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004647# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004649# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004650 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 int fd_fromshell[2];
4652 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004653 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
4655 out_flush();
4656 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004657 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004658 if (tmode == TMODE_RAW)
4659 // The shell may have messed with the mode, always set it later.
4660 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004662 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004663 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004664
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004666 * For the GUI, when writing the output into the buffer and when reading
4667 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4668 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004670 if ((options & (SHELL_READ|SHELL_WRITE))
4671# ifdef FEAT_GUI
4672 || (gui.in_use && show_shell_mess)
4673# endif
4674 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004676# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 /*
4678 * Try to open a master pty.
4679 * If this works, open the slave pty.
4680 * If the slave can't be opened, close the master pty.
4681 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004682 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004683 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 /*
4685 * If not opening a pty or it didn't work, try using pipes.
4686 */
4687 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 {
4690 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004691 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
4693 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004694 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
4696 close(fd_toshell[0]);
4697 close(fd_toshell[1]);
4698 }
4699 }
4700 if (pipe_error)
4701 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004702 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 out_flush();
4704 }
4705 }
4706 }
4707
Bram Moolenaar0f873732019-12-05 20:28:46 +01004708 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004710 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004711 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004712 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004713 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004715 UNBLOCK_SIGNALS(&curset);
4716
Bram Moolenaar32526b32019-01-19 17:43:09 +01004717 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004718 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004720 || (gui.in_use && show_shell_mess)
4721# endif
4722 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004724# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004725 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 {
4727 close(pty_master_fd);
4728 close(pty_slave_fd);
4729 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004730 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004731# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
4733 close(fd_toshell[0]);
4734 close(fd_toshell[1]);
4735 close(fd_fromshell[0]);
4736 close(fd_fromshell[1]);
4737 }
4738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004740 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004742 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004743 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744
Bram Moolenaar819524702018-02-27 19:10:00 +01004745# ifdef FEAT_JOB_CHANNEL
4746 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004747 {
4748 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004749 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004750 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004751# endif
4752
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 if (!show_shell_mess || (options & SHELL_EXPAND))
4754 {
4755 int fd;
4756
4757 /*
4758 * Don't want to show any message from the shell. Can't just
4759 * close stdout and stderr though, because some systems will
4760 * break if you try to write to them after that, so we must
4761 * use dup() to replace them with something else -- webb
4762 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4763 * waiting for input.
4764 */
4765 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4766 fclose(stdin);
4767 fclose(stdout);
4768 fclose(stderr);
4769
4770 /*
4771 * If any of these open()'s and dup()'s fail, we just continue
4772 * anyway. It's not fatal, and on most systems it will make
4773 * no difference at all. On a few it will cause the execvp()
4774 * to exit with a non-zero status even when the completion
4775 * could be done, which is nothing too serious. If the open()
4776 * or dup() failed we'd just do the same thing ourselves
4777 * anyway -- webb
4778 */
4779 if (fd >= 0)
4780 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004781 vim_ignored = dup(fd); // To replace stdin (fd 0)
4782 vim_ignored = dup(fd); // To replace stdout (fd 1)
4783 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
Bram Moolenaar0f873732019-12-05 20:28:46 +01004785 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 close(fd);
4787 }
4788 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004789 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004791 || gui.in_use
4792# endif
4793 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
4795
Bram Moolenaardf177f62005-02-22 08:39:57 +00004796# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004797 // Create our own process group, so that the child and all its
4798 // children can be kill()ed. Don't do this when using pipes,
4799 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004800 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004801 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004802 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004803# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004804 // When doing "!xterm&" and 'shell' is bash: the shell
4805 // will exit and send SIGHUP to all processes in its
4806 // group, killing the just started process. Ignore SIGHUP
4807 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004808 signal(SIGHUP, SIG_IGN);
4809# endif
4810 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004811# endif
4812# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004813 if (pty_slave_fd >= 0)
4814 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004815 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004816 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004817 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004818# ifdef TIOCSCTTY
4819 // Try to become controlling tty (probably doesn't work,
4820 // unless run by root)
4821 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4822# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004823 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004824# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004825 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826
Bram Moolenaara5792f52005-11-23 21:25:05 +00004827 /*
4828 * stderr is only redirected when using the GUI, so that a
4829 * program like gpg can still access the terminal to get a
4830 * passphrase using stderr.
4831 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004832# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if (pty_master_fd >= 0)
4834 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004835 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
Bram Moolenaar0f873732019-12-05 20:28:46 +01004837 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004839 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004841 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004842 if (gui.in_use)
4843 {
4844 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004845 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
Bram Moolenaar0f873732019-12-05 20:28:46 +01004848 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 }
4850 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004853 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 close(fd_toshell[1]);
4855 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004856 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 close(fd_toshell[0]);
4858
Bram Moolenaar0f873732019-12-05 20:28:46 +01004859 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 close(fd_fromshell[0]);
4861 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004862 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 close(fd_fromshell[1]);
4864
Bram Moolenaara5792f52005-11-23 21:25:05 +00004865# ifdef FEAT_GUI
4866 if (gui.in_use)
4867 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004868 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004869 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004870 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004871 }
4872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004875
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 /*
4877 * There is no type cast for the argv, because the type may be
4878 * different on different machines. This may cause a warning
4879 * message with strict compilers, don't worry about it.
4880 * Call _exit() instead of exit() to avoid closing the connection
4881 * to the X server (esp. with GTK, which uses atexit()).
4882 */
4883 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004884 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004886 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 {
4888 /*
4889 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004890 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 */
4892 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004893 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004894 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004895# ifdef FEAT_JOB_CHANNEL
4896 ++dont_check_job_ended;
4897# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 /*
4899 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004900 * This is also used to pipe stdin/stdout to/from the external
4901 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004903 if ((options & (SHELL_READ|SHELL_WRITE))
4904# ifdef FEAT_GUI
4905 || (gui.in_use && show_shell_mess)
4906# endif
4907 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004909# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004911 int buffer_off = 0; // valid bytes in buffer[]
4912 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4913 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 int len;
4915 int p_more_save;
4916 int old_State;
4917 int c;
4918 int toshell_fd;
4919 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004920 garray_T ga;
4921 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004922# ifdef ELAPSED_FUNC
4923 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004924# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
Bram Moolenaardf177f62005-02-22 08:39:57 +00004926# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (pty_master_fd >= 0)
4928 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 fromshell_fd = pty_master_fd;
4930 toshell_fd = dup(pty_master_fd);
4931 }
4932 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004933# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
4935 close(fd_toshell[0]);
4936 close(fd_fromshell[1]);
4937 toshell_fd = fd_toshell[1];
4938 fromshell_fd = fd_fromshell[0];
4939 }
4940
4941 /*
4942 * Write to the child if there are typed characters.
4943 * Read from the child if there are characters available.
4944 * Repeat the reading a few times if more characters are
4945 * available. Need to check for typed keys now and then, but
4946 * not too often (delays when no chars are available).
4947 * This loop is quit if no characters can be read from the pty
4948 * (WaitForChar detected special condition), or there are no
4949 * characters available and the child has exited.
4950 * Only check if the child has exited when there is no more
4951 * output. The child may exit before all the output has
4952 * been printed.
4953 *
4954 * Currently this busy loops!
4955 * This can probably dead-lock when the write blocks!
4956 */
4957 p_more_save = p_more;
4958 p_more = FALSE;
4959 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004960 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004962 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004963 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004964 // Fork a process that will write the lines to the
4965 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004966 if ((wpid = fork()) == -1)
4967 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004968 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004969 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004970 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004971 {
4972 linenr_T lnum = curbuf->b_op_start.lnum;
4973 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004974 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004975 size_t l;
4976
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004977 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004978 for (;;)
4979 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004980 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004981 if (l == 0)
4982 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004983 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004984 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004985 len = write(toshell_fd, "", (size_t)1);
4986 else
4987 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004988 char_u *s = vim_strchr(lp + written, NL);
4989
Bram Moolenaar89d40322006-08-29 15:30:07 +00004990 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004991 s == NULL ? l
4992 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004993 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004994 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004995 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004996 // Finished a line, add a NL, unless this line
4997 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004998 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004999 || (!curbuf->b_p_bin
5000 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005001 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00005002 && (lnum !=
5003 curbuf->b_ml.ml_line_count
5004 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005005 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005006 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005007 ++lnum;
5008 if (lnum > curbuf->b_op_end.lnum)
5009 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005010 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005011 close(toshell_fd);
5012 toshell_fd = -1;
5013 break;
5014 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005015 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005016 written = 0;
5017 }
5018 else if (len > 0)
5019 written += len;
5020 }
5021 _exit(0);
5022 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005023 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005024 {
5025 close(toshell_fd);
5026 toshell_fd = -1;
5027 }
5028 }
5029
5030 if (options & SHELL_READ)
5031 ga_init2(&ga, 1, BUFLEN);
5032
5033 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005034# ifdef ELAPSED_FUNC
5035 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005036# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 for (;;)
5038 {
5039 /*
5040 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005041 * if there are any.
5042 * Don't do this if we are expanding wild cards (would eat
5043 * typeahead).
5044 * Don't do this when filtering and terminal is in cooked
5045 * mode, the shell command will handle the I/O. Avoids
5046 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005047 * Don't get characters when the child has already
5048 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005049 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005050 * while (noread_cnt > 4), avoids that ":r !ls" eats
5051 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 */
5053 len = 0;
5054 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005055 && ((options &
5056 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5057 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005058# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005059 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005060# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005061 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005062 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005063 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005065 if (ta_len == 0)
5066 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005067 // Get extra characters when we don't have any.
5068 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005069 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005070# ifdef ELAPSED_FUNC
5071 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005072# endif
5073 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5074 }
5075 if (ta_len > 0 || len > 0)
5076 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 /*
5078 * For pipes:
5079 * Check for CTRL-C: send interrupt signal to child.
5080 * Check for CTRL-D: EOF, close pipe to child.
5081 */
5082 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5083 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 /*
5085 * Send SIGINT to the child's group or all
5086 * processes in our group.
5087 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005088 may_send_sigint(ta_buf[ta_len], pid, wpid);
5089
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 if (pty_master_fd < 0 && toshell_fd >= 0
5091 && ta_buf[ta_len] == Ctrl_D)
5092 {
5093 close(toshell_fd);
5094 toshell_fd = -1;
5095 }
5096 }
5097
Bram Moolenaarf4140482020-02-15 23:06:45 +01005098 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099
5100 /*
5101 * For pipes: echo the typed characters.
5102 * For a pty this does not seem to work.
5103 */
5104 if (pty_master_fd < 0)
5105 {
5106 for (i = ta_len; i < ta_len + len; ++i)
5107 {
5108 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5109 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 else if (has_mbyte)
5111 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005112 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113
5114 msg_outtrans_len(ta_buf + i, l);
5115 i += l - 1;
5116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 else
5118 msg_outtrans_len(ta_buf + i, 1);
5119 }
5120 windgoto(msg_row, msg_col);
5121 out_flush();
5122 }
5123
5124 ta_len += len;
5125
5126 /*
5127 * Write the characters to the child, unless EOF has
5128 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005129 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005130 * When writing buffer lines, drop the typed
5131 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005133 if (options & SHELL_WRITE)
5134 ta_len = 0;
5135 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 {
5137 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5138 if (len > 0)
5139 {
5140 ta_len -= len;
5141 mch_memmove(ta_buf, ta_buf + len, ta_len);
5142 }
5143 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
5146
Bram Moolenaardf177f62005-02-22 08:39:57 +00005147 if (got_int)
5148 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005149 // CTRL-C sends a signal to the child, we ignore it
5150 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005151# ifdef HAVE_SETSID
5152 kill(-pid, SIGINT);
5153# else
5154 kill(0, SIGINT);
5155# endif
5156 if (wpid > 0)
5157 kill(wpid, SIGINT);
5158 got_int = FALSE;
5159 }
5160
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 /*
5162 * Check if the child has any characters to be printed.
5163 * Read them and write them to our window. Repeat this as
5164 * long as there is something to do, avoid the 10ms wait
5165 * for mch_inchar(), or sending typeahead characters to
5166 * the external process.
5167 * TODO: This should handle escape sequences, compatible
5168 * to some terminal (vt52?).
5169 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005170 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005171 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005173 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005176 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005178
5179 noread_cnt = 0;
5180 if (options & SHELL_READ)
5181 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005182 // Do NUL -> NL translation, append NL separated
5183 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005184 for (i = 0; i < len; ++i)
5185 {
5186 if (buffer[i] == NL)
5187 append_ga_line(&ga);
5188 else if (buffer[i] == NUL)
5189 ga_append(&ga, NL);
5190 else
5191 ga_append(&ga, buffer[i]);
5192 }
5193 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005194 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
5196 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005197 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
Bram Moolenaardf177f62005-02-22 08:39:57 +00005199 len += buffer_off;
5200 buffer[len] = NUL;
5201
Bram Moolenaar0f873732019-12-05 20:28:46 +01005202 // Check if the last character in buffer[] is
5203 // incomplete, keep these bytes for the next
5204 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 for (p = buffer; p < buffer + len; p += l)
5206 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005207 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005209 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 else if (MB_BYTE2LEN(*p) != l)
5211 break;
5212 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005213 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005215 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 if (len >= 12)
5217 ++p;
5218 else
5219 {
5220 buffer_off = len;
5221 continue;
5222 }
5223 }
5224 c = *p;
5225 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005226 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 if (p < buffer + len)
5228 {
5229 *p = c;
5230 buffer_off = (buffer + len) - p;
5231 mch_memmove(buffer, p, buffer_off);
5232 continue;
5233 }
5234 buffer_off = 0;
5235 }
5236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 {
5238 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005239 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 }
5241
5242 windgoto(msg_row, msg_col);
5243 cursor_on();
5244 out_flush();
5245 if (got_int)
5246 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005247
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005248# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005249 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005250 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005251 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005252
Bram Moolenaar0f873732019-12-05 20:28:46 +01005253 // Avoid that we keep looping here without
5254 // checking for a CTRL-C for a long time. Don't
5255 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005256 if (msec > 2000)
5257 {
5258 noread_cnt = 5;
5259 break;
5260 }
5261 }
5262# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
5264
Bram Moolenaar0f873732019-12-05 20:28:46 +01005265 // If we already detected the child has finished, continue
5266 // reading output for a short while. Some text may be
5267 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005268 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005269 {
5270 if (noread_cnt < 5)
5271 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005272 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005273 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 /*
5276 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005277 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005279# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005280 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005281# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005283# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5285 || (wait_pid == pid && WIFEXITED(status)))
5286 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005287 // Don't break the loop yet, try reading more
5288 // characters from "fromshell_fd" first. When using
5289 // pipes there might still be something to read and
5290 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005293 else
5294 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005295
Bram Moolenaar95a51352013-03-21 22:53:50 +01005296# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005297 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005298 clip_update();
5299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 }
5301finished:
5302 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005303 if (options & SHELL_READ)
5304 {
5305 if (ga.ga_len > 0)
5306 {
5307 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005308 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005309 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005310 }
5311 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005312 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005313 ga_clear(&ga);
5314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 /*
5317 * Give all typeahead that wasn't used back to ui_inchar().
5318 */
5319 if (ta_len)
5320 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 State = old_State;
5322 if (toshell_fd >= 0)
5323 close(toshell_fd);
5324 close(fromshell_fd);
5325 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005326# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005327 else
5328 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005329 long delay_msec = 1;
5330
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005331 if (tmode == TMODE_RAW)
5332 // possibly disables modifyOtherKeys, so that the system
5333 // can recognize CTRL-C
5334 out_str(T_CTE);
Bram Moolenaar0981c872020-08-23 14:28:37 +02005335
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005336 /*
5337 * Similar to the loop above, but only handle X events, no
5338 * I/O.
5339 */
5340 for (;;)
5341 {
5342 if (got_int)
5343 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005344 // CTRL-C sends a signal to the child, we ignore it
5345 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005346# ifdef HAVE_SETSID
5347 kill(-pid, SIGINT);
5348# else
5349 kill(0, SIGINT);
5350# endif
5351 got_int = FALSE;
5352 }
5353# ifdef __NeXT__
5354 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5355# else
5356 wait_pid = waitpid(pid, &status, WNOHANG);
5357# endif
5358 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5359 || (wait_pid == pid && WIFEXITED(status)))
5360 {
5361 wait_pid = pid;
5362 break;
5363 }
5364
Bram Moolenaar0f873732019-12-05 20:28:46 +01005365 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005366 clip_update();
5367
Bram Moolenaar0f873732019-12-05 20:28:46 +01005368 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005369 // less time, gradually wait longer.
5370 mch_delay(delay_msec,
5371 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005372 if (++delay_msec > 10)
5373 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005374 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005375
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005376 if (tmode == TMODE_RAW)
5377 // possibly enables modifyOtherKeys again
5378 out_str(T_CTI);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005379 }
5380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381
5382 /*
5383 * Wait until our child has exited.
5384 * Ignore wait() returning pids of other children and returning
5385 * because of some signal like SIGWINCH.
5386 * Don't wait if wait_pid was already set above, indicating the
5387 * child already exited.
5388 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005389 if (wait_pid != pid)
5390 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
Bram Moolenaar624891f2010-10-13 16:22:09 +02005392# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005393 // Close slave side of pty. Only do this after the child has
5394 // exited, otherwise the child may hang when it tries to write on
5395 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005396 if (pty_master_fd >= 0)
5397 close(pty_slave_fd);
5398# endif
5399
Bram Moolenaar0f873732019-12-05 20:28:46 +01005400 // Make sure the child that writes to the external program is
5401 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005402 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005403 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005404 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005405 wait4pid(wpid, NULL);
5406 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005407
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005408# ifdef FEAT_JOB_CHANNEL
5409 --dont_check_job_ended;
5410# endif
5411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 /*
5413 * Set to raw mode right now, otherwise a CTRL-C after
5414 * catch_signals() will kill Vim.
5415 */
5416 if (tmode == TMODE_RAW)
5417 settmode(TMODE_RAW);
5418 did_settmode = TRUE;
5419 set_signals();
5420
5421 if (WIFEXITED(status))
5422 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005423 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005425 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 {
5427 if (retval == EXEC_FAILED)
5428 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005429 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 msg_outtrans(p_sh);
5431 msg_putchar('\n');
5432 }
5433 else if (!(options & SHELL_SILENT))
5434 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005435 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 msg_outnum((long)retval);
5437 msg_putchar('\n');
5438 }
5439 }
5440 }
5441 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005442 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 }
5444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
5446error:
5447 if (!did_settmode)
5448 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005449 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005451 vim_free(argv);
5452 vim_free(tofree1);
5453 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
5455 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005456}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005457#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005458
5459 int
5460mch_call_shell(
5461 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005462 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005463{
5464#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5465 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5466 return mch_call_shell_terminal(cmd, options);
5467#endif
5468#ifdef USE_SYSTEM
5469 return mch_call_shell_system(cmd, options);
5470#else
5471 return mch_call_shell_fork(cmd, options);
5472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473}
5474
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005475#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005476 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005477mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005478{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005479 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005480 int fd_in[2] = {-1, -1}; // for stdin
5481 int fd_out[2] = {-1, -1}; // for stdout
5482 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005483 int pty_master_fd = -1;
5484 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005485 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005486 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5487 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5488 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005489 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005490 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5491 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005492 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005493 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005494 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005495
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005496 if (use_out_for_err && use_null_for_out)
5497 use_null_for_err = TRUE;
5498
Bram Moolenaar0f873732019-12-05 20:28:46 +01005499 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005500 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005501
Bram Moolenaarb2412082017-08-20 18:09:14 +02005502 if (options->jo_pty
5503 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005504 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005505 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005506 open_pty(&pty_master_fd, &pty_slave_fd,
5507 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005508
Bram Moolenaar0f873732019-12-05 20:28:46 +01005509 // TODO: without the channel feature connect the child to /dev/null?
5510 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005511 if (use_file_for_in)
5512 {
5513 char_u *fname = options->jo_io_name[PART_IN];
5514
5515 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5516 if (fd_in[0] < 0)
5517 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005518 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005519 goto failed;
5520 }
5521 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005522 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005523 // When writing buffer lines to the input don't use the pty, so that
5524 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005525 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5526 && pipe(fd_in) < 0)
5527 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005528
5529 if (use_file_for_out)
5530 {
5531 char_u *fname = options->jo_io_name[PART_OUT];
5532
5533 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5534 if (fd_out[1] < 0)
5535 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005536 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005537 goto failed;
5538 }
5539 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005540 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005541 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005542
5543 if (use_file_for_err)
5544 {
5545 char_u *fname = options->jo_io_name[PART_ERR];
5546
5547 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5548 if (fd_err[1] < 0)
5549 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005550 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005551 goto failed;
5552 }
5553 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005554 else if (!use_out_for_err && !use_null_for_err
5555 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005556 goto failed;
5557
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005558 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5559 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005560 if (options->jo_set & JO_CHANNEL)
5561 {
5562 channel = options->jo_channel;
5563 if (channel != NULL)
5564 ++channel->ch_refcount;
5565 }
5566 else
5567 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005568 if (channel == NULL)
5569 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005570 if (job->jv_tty_out != NULL)
5571 ch_log(channel, "using pty %s on fd %d",
5572 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005573 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005574
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005575 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005576 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005577 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005578 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005579 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005580 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005581 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005582 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005583 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005584 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005585 int null_fd = -1;
5586 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005587
Bram Moolenaar0f873732019-12-05 20:28:46 +01005588 // child
5589 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005590 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005591
Bram Moolenaar819524702018-02-27 19:10:00 +01005592# ifdef FEAT_JOB_CHANNEL
5593 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005594 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005595 ch_logfile((char_u *)"", (char_u *)"");
5596# endif
5597
Bram Moolenaar835dc632016-02-07 14:27:38 +01005598# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005599 // Create our own process group, so that the child and all its
5600 // children can be kill()ed. Don't do this when using pipes,
5601 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005602 (void)setsid();
5603# endif
5604
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005605# ifdef FEAT_TERMINAL
5606 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005607 {
5608 char *term = (char *)T_NAME;
5609
5610#ifdef FEAT_GUI
5611 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005612 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005613 term = getenv("TERM");
5614#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005615 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005616 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005617 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005618 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005619 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005620 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005621 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005622 else if (t_colors > 16)
5623 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005624 else
5625 term = "xterm";
5626 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005627 set_child_environment(
5628 (long)options->jo_term_rows,
5629 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005630 term,
5631 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005632 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005633 else
5634# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005635 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005636
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005637 if (options->jo_env != NULL)
5638 {
5639 dict_T *dict = options->jo_env;
5640 hashitem_T *hi;
5641 int todo = (int)dict->dv_hashtab.ht_used;
5642
5643 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5644 if (!HASHITEM_EMPTY(hi))
5645 {
5646 typval_T *item = &dict_lookup(hi)->di_tv;
5647
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005648 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005649 --todo;
5650 }
5651 }
5652
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005653 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005654 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005655 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005656 if (null_fd < 0)
5657 {
5658 perror("opening /dev/null failed");
5659 _exit(OPEN_NULL_FAILED);
5660 }
5661 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005662
Bram Moolenaar223896d2017-08-02 22:33:28 +02005663 if (pty_slave_fd >= 0)
5664 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005665 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005666 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005667# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005668 // Try to become controlling tty (probably doesn't work,
5669 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005670 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5671# endif
5672 }
5673
Bram Moolenaar0f873732019-12-05 20:28:46 +01005674 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005675 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005676 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005677 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005678 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005679 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005680 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005681 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005682
Bram Moolenaar0f873732019-12-05 20:28:46 +01005683 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005684 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005685 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005686 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005687 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005688 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005689 }
5690 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005691 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005692 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005693 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005694 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005695 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005696
Bram Moolenaar0f873732019-12-05 20:28:46 +01005697 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005698 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005699 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005700 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005701 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005702 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005703 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005704 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005705
5706 if (fd_in[0] >= 0)
5707 close(fd_in[0]);
5708 if (fd_in[1] >= 0)
5709 close(fd_in[1]);
5710 if (fd_out[0] >= 0)
5711 close(fd_out[0]);
5712 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005713 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005714 if (fd_err[0] >= 0)
5715 close(fd_err[0]);
5716 if (fd_err[1] >= 0)
5717 close(fd_err[1]);
5718 if (pty_master_fd >= 0)
5719 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005720 close(pty_master_fd); // not used in the child
5721 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005722 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005723
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005724 if (null_fd >= 0)
5725 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005726
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005727 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5728 _exit(EXEC_FAILED);
5729
Bram Moolenaar0f873732019-12-05 20:28:46 +01005730 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005731 execvp(argv[0], argv);
5732
Bram Moolenaar4694a172016-04-21 14:05:23 +02005733 if (stderr_works)
5734 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005735# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005736 // calling free_all_mem() here causes problems. Ignore valgrind
5737 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005738# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005739 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005740 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005741
Bram Moolenaar0f873732019-12-05 20:28:46 +01005742 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005743 UNBLOCK_SIGNALS(&curset);
5744
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005745 job->jv_pid = pid;
5746 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005747 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005748
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005749 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005750 close(pty_slave_fd); // not used in the parent
5751 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005752 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005753 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005754 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005755 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005756 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005757 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005758 if (channel != NULL)
5759 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005760 int in_fd = INVALID_FD;
5761 int out_fd = INVALID_FD;
5762 int err_fd = INVALID_FD;
5763
5764 if (!(use_file_for_in || use_null_for_in))
5765 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5766
5767 if (!(use_file_for_out || use_null_for_out))
5768 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5769
5770 // When using pty_master_fd only set it for stdout, do not duplicate
5771 // it for stderr, it only needs to be read once.
5772 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5773 {
5774 if (fd_err[0] >= 0)
5775 err_fd = fd_err[0];
5776 else if (out_fd != pty_master_fd)
5777 err_fd = pty_master_fd;
5778 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005779
5780 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005781 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005782 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005783 else
5784 {
5785 if (fd_in[1] >= 0)
5786 close(fd_in[1]);
5787 if (fd_out[0] >= 0)
5788 close(fd_out[0]);
5789 if (fd_err[0] >= 0)
5790 close(fd_err[0]);
5791 if (pty_master_fd >= 0)
5792 close(pty_master_fd);
5793 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005794
Bram Moolenaar0f873732019-12-05 20:28:46 +01005795 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005796 return;
5797
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005798failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005799 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005800 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005801 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005802 if (fd_in[1] >= 0)
5803 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005804 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005805 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005806 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005807 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005808 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005809 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005810 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005811 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005812 if (pty_master_fd >= 0)
5813 close(pty_master_fd);
5814 if (pty_slave_fd >= 0)
5815 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005816}
5817
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005818 static char_u *
5819get_signal_name(int sig)
5820{
5821 int i;
5822 char_u numbuf[NUMBUFLEN];
5823
5824 if (sig == SIGKILL)
5825 return vim_strsave((char_u *)"kill");
5826
5827 for (i = 0; signal_info[i].sig != -1; i++)
5828 if (sig == signal_info[i].sig)
5829 return strlow_save((char_u *)signal_info[i].name);
5830
5831 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5832 return vim_strsave(numbuf);
5833}
5834
Bram Moolenaar835dc632016-02-07 14:27:38 +01005835 char *
5836mch_job_status(job_T *job)
5837{
5838# ifdef HAVE_UNION_WAIT
5839 union wait status;
5840# else
5841 int status = -1;
5842# endif
5843 pid_t wait_pid = 0;
5844
5845# ifdef __NeXT__
5846 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5847# else
5848 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5849# endif
5850 if (wait_pid == -1)
5851 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005852 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005853 if (job->jv_status < JOB_ENDED)
5854 ch_log(job->jv_channel, "Job no longer exists: %s",
5855 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005856 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005857 }
5858 if (wait_pid == 0)
5859 return "run";
5860 if (WIFEXITED(status))
5861 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005862 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005863 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005864 if (job->jv_status < JOB_ENDED)
5865 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005866 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005867 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005868 if (WIFSIGNALED(status))
5869 {
5870 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005871 job->jv_termsig = get_signal_name(WTERMSIG(status));
5872 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5873 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5874 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005875 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005876 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005877 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005878
5879return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005880 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005881 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005882 return "dead";
5883}
5884
5885 job_T *
5886mch_detect_ended_job(job_T *job_list)
5887{
5888# ifdef HAVE_UNION_WAIT
5889 union wait status;
5890# else
5891 int status = -1;
5892# endif
5893 pid_t wait_pid = 0;
5894 job_T *job;
5895
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005896# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005897 // Do not do this when waiting for a shell command to finish, we would get
5898 // the exit value here (and discard it), the exit value obtained there
5899 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005900 if (dont_check_job_ended > 0)
5901 return NULL;
5902# endif
5903
Bram Moolenaar97792de2016-10-15 18:36:49 +02005904# ifdef __NeXT__
5905 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5906# else
5907 wait_pid = waitpid(-1, &status, WNOHANG);
5908# endif
5909 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005910 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005911 return NULL;
5912 for (job = job_list; job != NULL; job = job->jv_next)
5913 {
5914 if (job->jv_pid == wait_pid)
5915 {
5916 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005917 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005918 job->jv_exitval = WEXITSTATUS(status);
5919 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005920 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005921 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005922 job->jv_termsig = get_signal_name(WTERMSIG(status));
5923 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005924 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005925 {
5926 ch_log(job->jv_channel, "Job ended");
5927 job->jv_status = JOB_ENDED;
5928 }
5929 return job;
5930 }
5931 }
5932 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005933}
5934
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005935/*
5936 * Send a (deadly) signal to "job".
5937 * Return FAIL if "how" is not a valid name.
5938 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005939 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005940mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005941{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005942 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005943
Bram Moolenaar923d9262016-02-25 20:56:01 +01005944 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005945 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005946 else if (STRCMP(how, "hup") == 0)
5947 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005948 else if (STRCMP(how, "quit") == 0)
5949 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005950 else if (STRCMP(how, "int") == 0)
5951 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005952 else if (STRCMP(how, "kill") == 0)
5953 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005954#ifdef SIGWINCH
5955 else if (STRCMP(how, "winch") == 0)
5956 sig = SIGWINCH;
5957#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005958 else if (isdigit(*how))
5959 sig = atoi((char *)how);
5960 else
5961 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005962
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005963 // Never kill ourselves!
5964 if (job->jv_pid != 0)
5965 {
5966 // TODO: have an option to only kill the process, not the group?
5967 kill(-job->jv_pid, sig);
5968 kill(job->jv_pid, sig);
5969 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005970
Bram Moolenaar835dc632016-02-07 14:27:38 +01005971 return OK;
5972}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005973
5974/*
5975 * Clear the data related to "job".
5976 */
5977 void
5978mch_clear_job(job_T *job)
5979{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005980 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005981# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005982 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005983# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005984 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005985# endif
5986}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005987#endif
5988
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005989#if defined(FEAT_TERMINAL) || defined(PROTO)
5990 int
5991mch_create_pty_channel(job_T *job, jobopt_T *options)
5992{
5993 int pty_master_fd = -1;
5994 int pty_slave_fd = -1;
5995 channel_T *channel;
5996
Bram Moolenaar59386482019-02-10 22:43:46 +01005997 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02005998 if (pty_master_fd < 0 || pty_slave_fd < 0)
5999 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006000 close(pty_slave_fd);
6001
6002 channel = add_channel();
6003 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006004 {
6005 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006006 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006007 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006008 if (job->jv_tty_out != NULL)
6009 ch_log(channel, "using pty %s on fd %d",
6010 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006011 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006012 channel->ch_keep_open = TRUE;
6013
Bram Moolenaar0f873732019-12-05 20:28:46 +01006014 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6015 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006016 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006017 channel_set_job(channel, job, options);
6018 return OK;
6019}
6020#endif
6021
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022/*
6023 * Check for CTRL-C typed by reading all available characters.
6024 * In cooked mode we should get SIGINT, no need to check.
6025 */
6026 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006027mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006029 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006030 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 fill_input_buf(FALSE);
6032}
6033
6034/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006035 * Wait "msec" msec until a character is available from the mouse, keyboard,
6036 * from inbuf[].
6037 * "msec" == -1 will block forever.
6038 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006039 * When "ignore_input" is TRUE even check for pending input when input is
6040 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006041 * "interrupted" (if not NULL) is set to TRUE when no character is available
6042 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006043 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006044 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 */
6046 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006047WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006049#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006050 return ui_wait_for_chars_or_timer(
6051 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006052#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006053 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006054#endif
6055}
6056
6057/*
6058 * Wait "msec" msec until a character is available from the mouse or keyboard
6059 * or from inbuf[].
6060 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006061 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006062 * "interrupted" (if not NULL) is set to TRUE when no character is available
6063 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006064 * When a GUI is being used, this will never get called -- webb
6065 */
6066 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006067WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006068{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069#ifdef FEAT_MOUSE_GPM
6070 int gpm_process_wanted;
6071#endif
6072#ifdef FEAT_XCLIPBOARD
6073 int rest;
6074#endif
6075 int avail;
6076
Bram Moolenaar0f873732019-12-05 20:28:46 +01006077 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 return 1;
6079
6080#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006081 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 if (WantQueryMouse)
6083 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006084 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006085 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006086 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 }
6088#endif
6089
6090 /*
6091 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6092 * events. This is a bit complicated, because they might both be defined.
6093 */
6094#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6095# ifdef FEAT_XCLIPBOARD
6096 rest = 0;
6097 if (do_xterm_trace())
6098 rest = msec;
6099# endif
6100 do
6101 {
6102# ifdef FEAT_XCLIPBOARD
6103 if (rest != 0)
6104 {
6105 msec = XT_TRACE_DELAY;
6106 if (rest >= 0 && rest < XT_TRACE_DELAY)
6107 msec = rest;
6108 if (rest >= 0)
6109 rest -= msec;
6110 }
6111# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006112# ifdef FEAT_SOUND_CANBERRA
6113 // Invoke any pending sound callbacks.
6114 if (has_sound_callback_in_queue())
6115 invoke_sound_callback();
6116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117# ifdef FEAT_MOUSE_GPM
6118 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006119 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006120 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006121 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006123 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006127 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 return 1;
6129# ifdef FEAT_XCLIPBOARD
6130 if (rest == 0 || !do_xterm_trace())
6131# endif
6132 break;
6133 }
6134 }
6135 while (FALSE
6136# ifdef FEAT_MOUSE_GPM
6137 || (gpm_process_wanted && mch_gpm_process() == 0)
6138# endif
6139# ifdef FEAT_XCLIPBOARD
6140 || (!avail && rest != 0)
6141# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006142 )
6143 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144
6145#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006146 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147#endif
6148 return avail;
6149}
6150
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006151#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152/*
6153 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006154 * "msec" == 0 will check for characters once.
6155 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006158 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006159 * "interrupted" (if not NULL) is set to TRUE when no character is available
6160 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006162 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006163RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164{
6165 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006166 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006167#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 static int busy = FALSE;
6169
Bram Moolenaar0f873732019-12-05 20:28:46 +01006170 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171# define MAY_LOOP
6172
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006173# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006174 // Remember at what time we started, so that we know how much longer we
6175 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006176 long start_msec = msec;
6177 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006179 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006180 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181# endif
6182
Bram Moolenaar0f873732019-12-05 20:28:46 +01006183 // Handle being called recursively. This may happen for the session
6184 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (busy)
6186 return 0;
6187#endif
6188
6189#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006190 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191#endif
6192 {
6193#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006194 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006195# ifdef FEAT_MZSCHEME
6196 int mzquantum_used = FALSE;
6197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198#endif
6199#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006200 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006201 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 int nfd;
6203# ifdef FEAT_XCLIPBOARD
6204 int xterm_idx = -1;
6205# endif
6206# ifdef FEAT_MOUSE_GPM
6207 int gpm_idx = -1;
6208# endif
6209# ifdef USE_XSMP
6210 int xsmp_idx = -1;
6211# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006212 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006214# ifdef FEAT_MZSCHEME
6215 mzvim_check_threads();
6216 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6217 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006218 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006219 mzquantum_used = TRUE;
6220 }
6221# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 fds[0].fd = fd;
6223 fds[0].events = POLLIN;
6224 nfd = 1;
6225
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006227 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 if (xterm_Shell != (Widget)0)
6229 {
6230 xterm_idx = nfd;
6231 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6232 fds[nfd].events = POLLIN;
6233 nfd++;
6234 }
6235# endif
6236# ifdef FEAT_MOUSE_GPM
6237 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6238 {
6239 gpm_idx = nfd;
6240 fds[nfd].fd = gpm_fd;
6241 fds[nfd].events = POLLIN;
6242 nfd++;
6243 }
6244# endif
6245# ifdef USE_XSMP
6246 if (xsmp_icefd != -1)
6247 {
6248 xsmp_idx = nfd;
6249 fds[nfd].fd = xsmp_icefd;
6250 fds[nfd].events = POLLIN;
6251 nfd++;
6252 }
6253# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006254#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006255 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006256#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006257 if (interrupted != NULL)
6258 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006260 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006261
6262 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006263 if (result == 0 && interrupted != NULL && ret > 0)
6264 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006265
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006266# ifdef FEAT_MZSCHEME
6267 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006268 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006269 finished = FALSE;
6270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272# ifdef FEAT_XCLIPBOARD
6273 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6274 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006275 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006277 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 finished = FALSE;
6279 }
6280# endif
6281# ifdef FEAT_MOUSE_GPM
6282 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284# endif
6285# ifdef USE_XSMP
6286 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6287 {
6288 if (fds[xsmp_idx].revents & POLLIN)
6289 {
6290 busy = TRUE;
6291 xsmp_handle_requests();
6292 busy = FALSE;
6293 }
6294 else if (fds[xsmp_idx].revents & POLLHUP)
6295 {
6296 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006297 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 xsmp_close();
6299 }
6300 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006301 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 }
6303# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006304#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006305 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006306 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006307 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
Bram Moolenaar0f873732019-12-05 20:28:46 +01006310#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311
6312 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006313 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006314 // These are static because they can take 8 Kbyte each and cause the
6315 // signal stack to run out with -O3.
6316 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006318 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006320# ifdef FEAT_MZSCHEME
6321 mzvim_check_threads();
6322 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6323 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006324 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006325 mzquantum_used = TRUE;
6326 }
6327# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006329 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006331 tv.tv_sec = towait / 1000;
6332 tv.tv_usec = (towait % 1000) * (1000000/1000);
6333 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006335 else
6336 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337
6338 /*
6339 * Select on ready for reading and exceptional condition (end of file).
6340 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006341select_eintr:
6342 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006343 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 FD_ZERO(&efds);
6345 FD_SET(fd, &rfds);
6346# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006347 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 FD_SET(fd, &efds);
6349# endif
6350 maxfd = fd;
6351
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006353 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 if (xterm_Shell != (Widget)0)
6355 {
6356 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6357 if (maxfd < ConnectionNumber(xterm_dpy))
6358 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006359
Bram Moolenaar0f873732019-12-05 20:28:46 +01006360 // An event may have already been read but not handled. In
6361 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006362 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 }
6364# endif
6365# ifdef FEAT_MOUSE_GPM
6366 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6367 {
6368 FD_SET(gpm_fd, &rfds);
6369 FD_SET(gpm_fd, &efds);
6370 if (maxfd < gpm_fd)
6371 maxfd = gpm_fd;
6372 }
6373# endif
6374# ifdef USE_XSMP
6375 if (xsmp_icefd != -1)
6376 {
6377 FD_SET(xsmp_icefd, &rfds);
6378 FD_SET(xsmp_icefd, &efds);
6379 if (maxfd < xsmp_icefd)
6380 maxfd = xsmp_icefd;
6381 }
6382# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006383# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006384 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006385# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006386 if (interrupted != NULL)
6387 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388
Bram Moolenaar643b6142018-09-12 20:29:09 +02006389 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6390 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006391 result = ret > 0 && FD_ISSET(fd, &rfds);
6392 if (result)
6393 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006394 else if (interrupted != NULL && ret > 0)
6395 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006396
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006397# ifdef EINTR
6398 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006399 {
dbivolaruab16ad32021-12-29 19:41:47 +00006400 // Check whether the EINTR is caused by SIGTSTP
6401 if (got_tstp && !in_mch_suspend)
6402 {
6403 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006404
dbivolaruab16ad32021-12-29 19:41:47 +00006405 ea.forceit = TRUE;
6406 ex_stop(&ea);
6407 got_tstp = FALSE;
6408 }
6409
Bram Moolenaar0f873732019-12-05 20:28:46 +01006410 // Check whether window has been resized, EINTR may be caused by
6411 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006412 if (do_resize)
6413 handle_resize();
6414
Bram Moolenaar0f873732019-12-05 20:28:46 +01006415 // Interrupted by a signal, need to try again. We ignore msec
6416 // here, because we do want to check even after a timeout if
6417 // characters are available. Needed for reading output of an
6418 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006419 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006420 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006421# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006422# ifdef __TANDEM
6423 if (ret == -1 && errno == ENOTSUP)
6424 {
6425 FD_ZERO(&rfds);
6426 FD_ZERO(&efds);
6427 ret = 0;
6428 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006429# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006430# ifdef FEAT_MZSCHEME
6431 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006432 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006433 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434# endif
6435
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436# ifdef FEAT_XCLIPBOARD
6437 if (ret > 0 && xterm_Shell != (Widget)0
6438 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6439 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006440 xterm_update(); // Maybe we should hand out clipboard
6441 // continue looping when we only got the X event and the input
6442 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 if (--ret == 0 && !input_available())
6444 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006445 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 finished = FALSE;
6447 }
6448 }
6449# endif
6450# ifdef FEAT_MOUSE_GPM
6451 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6452 {
6453 if (FD_ISSET(gpm_fd, &efds))
6454 gpm_close();
6455 else if (FD_ISSET(gpm_fd, &rfds))
6456 *check_for_gpm = 1;
6457 }
6458# endif
6459# ifdef USE_XSMP
6460 if (ret > 0 && xsmp_icefd != -1)
6461 {
6462 if (FD_ISSET(xsmp_icefd, &efds))
6463 {
6464 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006465 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 xsmp_close();
6467 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006468 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 }
6470 else if (FD_ISSET(xsmp_icefd, &rfds))
6471 {
6472 busy = TRUE;
6473 xsmp_handle_requests();
6474 busy = FALSE;
6475 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006476 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 }
6478 }
6479# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006480#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006481 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006482 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006483 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
Bram Moolenaar0f873732019-12-05 20:28:46 +01006486#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
6488#ifdef MAY_LOOP
6489 if (finished || msec == 0)
6490 break;
6491
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006492# ifdef FEAT_CLIENTSERVER
6493 if (server_waiting())
6494 break;
6495# endif
6496
Bram Moolenaar0f873732019-12-05 20:28:46 +01006497 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 if (msec > 0)
6499 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006500# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006501 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006502 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006504 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 msec = msec / 2;
6506# endif
6507 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006508 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 }
6510#endif
6511 }
6512
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006513 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514}
6515
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006517 * Expand a path into all matching files and/or directories. Handles "*",
6518 * "?", "[a-z]", "**", etc.
6519 * "path" has backslashes before chars that are not to be expanded.
6520 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 */
6522 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006523mch_expandpath(
6524 garray_T *gap,
6525 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006526 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527{
Bram Moolenaar02743632005-07-25 20:42:36 +00006528 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
6531/*
6532 * mch_expand_wildcards() - this code does wild-card pattern matching using
6533 * the shell
6534 *
6535 * return OK for success, FAIL for error (you may lose some memory) and put
6536 * an error message in *file.
6537 *
6538 * num_pat is number of input patterns
6539 * pat is array of pointers to input patterns
6540 * num_file is pointer to number of matched file names
6541 * file is pointer to array of pointers to matched file names
6542 */
6543
6544#ifndef SEEK_SET
6545# define SEEK_SET 0
6546#endif
6547#ifndef SEEK_END
6548# define SEEK_END 2
6549#endif
6550
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006551#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006552
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006554mch_expand_wildcards(
6555 int num_pat,
6556 char_u **pat,
6557 int *num_file,
6558 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006559 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560{
6561 int i;
6562 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006563 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 char_u *p;
6565 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
Bram Moolenaarc7247912008-01-13 12:54:11 +00006567 /*
6568 * This is the non-OS/2 implementation (really Unix).
6569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 int j;
6571 char_u *tempname;
6572 char_u *command;
6573 FILE *fd;
6574 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006575#define STYLE_ECHO 0 // use "echo", the default
6576#define STYLE_GLOB 1 // use "glob", for csh
6577#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6578#define STYLE_PRINT 3 // use "print -N", for zsh
6579#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6580 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 int shell_style = STYLE_ECHO;
6582 int check_spaces;
6583 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006584 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006585 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006586 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587
Bram Moolenaar0f873732019-12-05 20:28:46 +01006588 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 *file = NULL;
6590
6591 /*
6592 * If there are no wildcards, just copy the names to allocated memory.
6593 * Saves a lot of time, because we don't have to start a new shell.
6594 */
6595 if (!have_wildcard(num_pat, pat))
6596 return save_patterns(num_pat, pat, num_file, file);
6597
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006598# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006599 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006600 if (sandbox != 0 && check_secure())
6601 return FAIL;
6602# endif
6603
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 /*
6605 * Don't allow the use of backticks in secure and restricted mode.
6606 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006607 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 for (i = 0; i < num_pat; ++i)
6609 if (vim_strchr(pat[i], '`') != NULL
6610 && (check_restricted() || check_secure()))
6611 return FAIL;
6612
6613 /*
6614 * get a name for the temp file
6615 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006616 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006618 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 return FAIL;
6620 }
6621
6622 /*
6623 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006624 * file.
6625 * STYLE_BT: NL separated
6626 * If expanding `cmd` execute it directly.
6627 * STYLE_GLOB: NUL separated
6628 * If we use *csh, "glob" will work better than "echo".
6629 * STYLE_PRINT: NL or NUL separated
6630 * If we use *zsh, "print -N" will work better than "glob".
6631 * STYLE_VIMGLOB: NL separated
6632 * If we use *sh*, we define "vimglob()".
6633 * STYLE_ECHO: space separated.
6634 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 */
6636 if (num_pat == 1 && *pat[0] == '`'
6637 && (len = STRLEN(pat[0])) > 2
6638 && *(pat[0] + len - 1) == '`')
6639 shell_style = STYLE_BT;
6640 else if ((len = STRLEN(p_sh)) >= 3)
6641 {
6642 if (STRCMP(p_sh + len - 3, "csh") == 0)
6643 shell_style = STYLE_GLOB;
6644 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6645 shell_style = STYLE_PRINT;
6646 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006647 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6648 "sh") != NULL)
6649 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650
Bram Moolenaar0f873732019-12-05 20:28:46 +01006651 // Compute the length of the command. We need 2 extra bytes: for the
6652 // optional '&' and for the NUL.
6653 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006655 if (shell_style == STYLE_VIMGLOB)
6656 len += STRLEN(sh_vimglob_func);
6657
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006658 for (i = 0; i < num_pat; ++i)
6659 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006660 // Count the length of the patterns in the same way as they are put in
6661 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006662#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006663 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006664#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006665 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006666 for (j = 0; pat[i][j] != NUL; ++j)
6667 {
6668 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006669 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006670 ++len;
6671 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006672#endif
6673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 command = alloc(len);
6675 if (command == NULL)
6676 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006677 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 vim_free(tempname);
6679 return FAIL;
6680 }
6681
6682 /*
6683 * Build the shell command:
6684 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6685 * recognizes this).
6686 * - Add the shell command to print the expanded names.
6687 * - Add the temp file name.
6688 * - Add the file name patterns.
6689 */
6690 if (shell_style == STYLE_BT)
6691 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006692 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006693 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006694 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006696 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006697 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006699 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006701 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 *p = ' ';
6703 }
6704 STRCAT(command, ">");
6705 }
6706 else
6707 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006708 STRCPY(command, "");
6709 if (shell_style == STYLE_GLOB)
6710 {
6711 // Assume the nonomatch option is valid only for csh like shells,
6712 // otherwise, this may set the positional parameters for the shell,
6713 // e.g. "$*".
6714 if (flags & EW_NOTFOUND)
6715 STRCAT(command, "set nonomatch; ");
6716 else
6717 STRCAT(command, "unset nonomatch; ");
6718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 if (shell_style == STYLE_GLOB)
6720 STRCAT(command, "glob >");
6721 else if (shell_style == STYLE_PRINT)
6722 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006723 else if (shell_style == STYLE_VIMGLOB)
6724 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 else
6726 STRCAT(command, "echo >");
6727 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006728
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006730
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 if (shell_style != STYLE_BT)
6732 for (i = 0; i < num_pat; ++i)
6733 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006734 // When using system() always add extra quotes, because the shell
6735 // is started twice. Otherwise put a backslash before special
6736 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737#ifdef USE_SYSTEM
6738 STRCAT(command, " \"");
6739 STRCAT(command, pat[i]);
6740 STRCAT(command, "\"");
6741#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006742 int intick = FALSE;
6743
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 p = command + STRLEN(command);
6745 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006746 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006747 {
6748 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006749 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006750 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6751 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006752 // Remove a backslash, take char literally. But keep
6753 // backslash inside backticks, before a special character
6754 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006755 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006756 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6757 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006758 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006759 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006760 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006761 else if (!intick
6762 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6763 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006764 // Put a backslash before a special character, but not
6765 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6766 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006767 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006768
Bram Moolenaar0f873732019-12-05 20:28:46 +01006769 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006770 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 *p = NUL;
6773#endif
6774 }
6775 if (flags & EW_SILENT)
6776 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006777 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006778 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779
6780 /*
6781 * Using zsh -G: If a pattern has no matches, it is just deleted from
6782 * the argument list, otherwise zsh gives an error message and doesn't
6783 * expand any other pattern.
6784 */
6785 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006786 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
6788 /*
6789 * If we use -f then shell variables set in .cshrc won't get expanded.
6790 * vi can do it, so we will too, but it is only necessary if there is a "$"
6791 * in one of the patterns, otherwise we can still use the fast option.
6792 */
6793 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006794 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
6796 /*
6797 * execute the shell command
6798 */
6799 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6800
Bram Moolenaar0f873732019-12-05 20:28:46 +01006801 // When running in the background, give it some time to create the temp
6802 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006803 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006804 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
Bram Moolenaar0f873732019-12-05 20:28:46 +01006806 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 show_shell_mess = TRUE;
6808 vim_free(command);
6809
Bram Moolenaar0f873732019-12-05 20:28:46 +01006810 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {
6812 mch_remove(tempname);
6813 vim_free(tempname);
6814 /*
6815 * With interactive completion, the error message is not printed.
6816 * However with USE_SYSTEM, I don't know how to turn off error messages
6817 * from the shell, so screen may still get messed up -- webb.
6818 */
6819#ifndef USE_SYSTEM
6820 if (!(flags & EW_SILENT))
6821#endif
6822 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006823 redraw_later_clear(); // probably messed up screen
6824 msg_putchar('\n'); // clear bottom line quickly
6825 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826#ifdef USE_SYSTEM
6827 if (!(flags & EW_SILENT))
6828#endif
6829 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006830 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006831 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 }
6833 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006834 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6835 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 if (shell_style == STYLE_BT)
6837 return FAIL;
6838 goto notfound;
6839 }
6840
6841 /*
6842 * read the names from the file into memory
6843 */
6844 fd = fopen((char *)tempname, READBIN);
6845 if (fd == NULL)
6846 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006847 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 if (!(flags & EW_SILENT))
6849 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006850 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006851 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 }
6853 vim_free(tempname);
6854 goto notfound;
6855 }
6856 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006857 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006859 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006860 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006861 buffer = NULL;
6862 else
6863 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 if (buffer == NULL)
6865 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006866 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 mch_remove(tempname);
6868 vim_free(tempname);
6869 fclose(fd);
6870 return FAIL;
6871 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006872 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 i = fread((char *)buffer, 1, len, fd);
6874 fclose(fd);
6875 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006876 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006878 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006879 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 vim_free(tempname);
6881 vim_free(buffer);
6882 return FAIL;
6883 }
6884 vim_free(tempname);
6885
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006886# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006887 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006889 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6891 *p++ = buffer[i];
6892 len = p - buffer;
6893# endif
6894
6895
Bram Moolenaar0f873732019-12-05 20:28:46 +01006896 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 if (shell_style == STYLE_ECHO)
6898 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006899 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006901 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 {
6903 while (*p != ' ' && *p != '\n')
6904 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006905 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 }
6907 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006908 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006909 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006911 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006913 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {
6915 while (*p != '\n' && *p != NUL)
6916 ++p;
6917 if (*p != NUL)
6918 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006919 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 }
6921 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006922 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 else
6924 {
6925 /*
6926 * Some versions of zsh use spaces instead of NULs to separate
6927 * results. Only do this when there is no NUL before the end of the
6928 * buffer, otherwise we would never be able to use file names with
6929 * embedded spaces when zsh does use NULs.
6930 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6931 * don't check for spaces again.
6932 */
6933 check_spaces = FALSE;
6934 if (shell_style == STYLE_PRINT && !did_find_nul)
6935 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006936 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006937 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006938 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 did_find_nul = TRUE;
6940 else
6941 check_spaces = TRUE;
6942 }
6943
6944 /*
6945 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6946 * already is one, for STYLE_GLOB it needs to be added.
6947 */
6948 if (len && buffer[len - 1] == NUL)
6949 --len;
6950 else
6951 buffer[len] = NUL;
6952 i = 0;
6953 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006954 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 {
6956 ++i;
6957 *p = NUL;
6958 }
6959 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006960 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 }
6962 if (i == 0)
6963 {
6964 /*
6965 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6966 * /bin/sh will happily expand it to nothing rather than returning an
6967 * error; and hey, it's good to check anyway -- webb.
6968 */
6969 vim_free(buffer);
6970 goto notfound;
6971 }
6972 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006973 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 if (*file == NULL)
6975 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006976 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 vim_free(buffer);
6978 return FAIL;
6979 }
6980
6981 /*
6982 * Isolate the individual file names.
6983 */
6984 p = buffer;
6985 for (i = 0; i < *num_file; ++i)
6986 {
6987 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006988 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006989 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6990 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006992 while (!(shell_style == STYLE_ECHO && *p == ' ')
6993 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006995 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 *p = NUL;
6997 else
6998 {
6999 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007000 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 }
7002 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007003 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007005 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007007 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 }
7009 }
7010
7011 /*
7012 * Move the file names to allocated memory.
7013 */
7014 for (j = 0, i = 0; i < *num_file; ++i)
7015 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007016 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7018 continue;
7019
Bram Moolenaar0f873732019-12-05 20:28:46 +01007020 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 dir = (mch_isdir((*file)[i]));
7022 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7023 continue;
7024
Bram Moolenaar0f873732019-12-05 20:28:46 +01007025 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007026 if (!dir && (flags & EW_EXEC)
7027 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007028 continue;
7029
Bram Moolenaar964b3742019-05-24 18:54:09 +02007030 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 if (p)
7032 {
7033 STRCPY(p, (*file)[i]);
7034 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007035 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 (*file)[j++] = p;
7037 }
7038 }
7039 vim_free(buffer);
7040 *num_file = j;
7041
Bram Moolenaar0f873732019-12-05 20:28:46 +01007042 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007044 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 goto notfound;
7046 }
7047
7048 return OK;
7049
7050notfound:
7051 if (flags & EW_NOTFOUND)
7052 return save_patterns(num_pat, pat, num_file, file);
7053 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054}
7055
Bram Moolenaar0f873732019-12-05 20:28:46 +01007056#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007059save_patterns(
7060 int num_pat,
7061 char_u **pat,
7062 int *num_file,
7063 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064{
7065 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007066 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007068 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 if (*file == NULL)
7070 return FAIL;
7071 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007072 {
7073 s = vim_strsave(pat[i]);
7074 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007075 // Be compatible with expand_filename(): halve the number of
7076 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007077 backslash_halve(s);
7078 (*file)[i] = s;
7079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 *num_file = num_pat;
7081 return OK;
7082}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084/*
7085 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7086 * expand.
7087 */
7088 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007089mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007091 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 if (*p == '\\' && p[1] != NUL)
7094 ++p;
7095 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 if (vim_strchr((char_u *)
7097#ifdef VMS
7098 "*?%"
7099#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101#endif
7102 , *p) != NULL)
7103 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 }
7105 return FALSE;
7106}
7107
7108/*
7109 * Return TRUE if the string "p" contains a wildcard.
7110 * Don't recognize '~' at the end as a wildcard.
7111 */
7112 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007113mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007115 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 if (*p == '\\' && p[1] != NUL)
7118 ++p;
7119 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 if (vim_strchr((char_u *)
7121#ifdef VMS
7122 "*?%$"
7123#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125#endif
7126 , *p) != NULL
7127 || (*p == '~' && p[1] != NUL))
7128 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 }
7130 return FALSE;
7131}
7132
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007134have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135{
7136 int i;
7137
7138 for (i = 0; i < num; i++)
7139 if (mch_has_wildcard(file[i]))
7140 return 1;
7141 return 0;
7142}
7143
7144 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007145have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
7147 int i;
7148
7149 for (i = 0; i < num; i++)
7150 if (vim_strchr(file[i], '$') != NULL)
7151 return TRUE;
7152 return FALSE;
7153}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007155#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156/*
7157 * Scaled-down version of rename(), which is missing in Xenix.
7158 * This version can only move regular files and will fail if the
7159 * destination exists.
7160 */
7161 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007162mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163{
7164 struct stat st;
7165
Bram Moolenaar0f873732019-12-05 20:28:46 +01007166 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007168 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007170 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 return 0;
7172 return -1;
7173}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007174#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007176#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177/*
7178 * Initializes connection with gpm (if it isn't already opened)
7179 * Return 1 if succeeded (or connection already opened), 0 if failed
7180 */
7181 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007182gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007184 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
7186 if (!gpm_flag)
7187 {
7188 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7189 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007190 // Default handling for mouse move
7191 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 gpm_connect.maxMod = 0xffff;
7193 if (Gpm_Open(&gpm_connect, 0) > 0)
7194 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007195 // gpm library tries to handling TSTP causes
7196 // problems. Anyways, we close connection to Gpm whenever
7197 // we are going to suspend or starting an external process
7198 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007199# ifdef SIGTSTP
dbivolaruab16ad32021-12-29 19:41:47 +00007200 signal(SIGTSTP, restricted ? SIG_IGN : (RETSIGTYPE (*)())sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007201# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007202 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 }
7204 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007205 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 return 0;
7207 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007208 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209}
7210
7211/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007212 * Returns TRUE if the GPM mouse is enabled.
7213 */
7214 int
7215gpm_enabled(void)
7216{
7217 return gpm_flag && gpm_fd >= 0;
7218}
7219
7220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 */
7223 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007224gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007226 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 Gpm_Close();
7228}
7229
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007230/*
7231 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007233 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 */
7235 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007236mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
7238 int button;
7239 static Gpm_Event gpm_event;
7240 char_u string[6];
7241 int_u vim_modifiers;
7242 int row,col;
7243 unsigned char buttons_mask;
7244 unsigned char gpm_modifiers;
7245 static unsigned char old_buttons = 0;
7246
7247 Gpm_GetEvent(&gpm_event);
7248
7249#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007250 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 if (hold_gui_events)
7252 return 0;
7253#endif
7254
7255 row = gpm_event.y - 1;
7256 col = gpm_event.x - 1;
7257
Bram Moolenaar0f873732019-12-05 20:28:46 +01007258 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 string[1] = 'M';
7260 string[2] = 'G';
7261 switch (GPM_BARE_EVENTS(gpm_event.type))
7262 {
7263 case GPM_DRAG:
7264 string[3] = MOUSE_DRAG;
7265 break;
7266 case GPM_DOWN:
7267 buttons_mask = gpm_event.buttons & ~old_buttons;
7268 old_buttons = gpm_event.buttons;
7269 switch (buttons_mask)
7270 {
7271 case GPM_B_LEFT:
7272 button = MOUSE_LEFT;
7273 break;
7274 case GPM_B_MIDDLE:
7275 button = MOUSE_MIDDLE;
7276 break;
7277 case GPM_B_RIGHT:
7278 button = MOUSE_RIGHT;
7279 break;
7280 default:
7281 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007282 // Don't know what to do. Can more than one button be
7283 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 }
7285 string[3] = (char_u)(button | 0x20);
7286 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7287 break;
7288 case GPM_UP:
7289 string[3] = MOUSE_RELEASE;
7290 old_buttons &= ~gpm_event.buttons;
7291 break;
7292 default:
7293 return 0;
7294 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007295 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 gpm_modifiers = gpm_event.modifiers;
7297 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007298 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7299 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7300 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7302 vim_modifiers |= MOUSE_SHIFT;
7303
7304 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7305 vim_modifiers |= MOUSE_CTRL;
7306 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7307 vim_modifiers |= MOUSE_ALT;
7308 string[3] |= vim_modifiers;
7309 string[4] = (char_u)(col + ' ' + 1);
7310 string[5] = (char_u)(row + ' ' + 1);
7311 add_to_input_buf(string, 6);
7312 return 6;
7313}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007314#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007316#ifdef FEAT_SYSMOUSE
7317/*
7318 * Initialize connection with sysmouse.
7319 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7320 * output, any sysmouse output than will be processed via sig_sysmouse().
7321 * Return OK if succeeded, FAIL if failed.
7322 */
7323 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007324sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007325{
7326 struct mouse_info mouse;
7327
7328 mouse.operation = MOUSE_MODE;
7329 mouse.u.mode.mode = 0;
7330 mouse.u.mode.signal = SIGUSR2;
7331 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7332 {
7333 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7334 mouse.operation = MOUSE_SHOW;
7335 ioctl(1, CONS_MOUSECTL, &mouse);
7336 return OK;
7337 }
7338 return FAIL;
7339}
7340
7341/*
7342 * Stop processing SIGUSR2 signals, and also make sure that
7343 * virtual console do not send us any sysmouse related signal.
7344 */
7345 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007346sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007347{
7348 struct mouse_info mouse;
7349
7350 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7351 mouse.operation = MOUSE_MODE;
7352 mouse.u.mode.mode = 0;
7353 mouse.u.mode.signal = 0;
7354 ioctl(1, CONS_MOUSECTL, &mouse);
7355}
7356
7357/*
7358 * Gets info from sysmouse and adds special keys to input buf.
7359 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007360 static RETSIGTYPE
7361sig_sysmouse SIGDEFARG(sigarg)
7362{
7363 struct mouse_info mouse;
7364 struct video_info video;
7365 char_u string[6];
7366 int row, col;
7367 int button;
7368 int buttons;
7369 static int oldbuttons = 0;
7370
7371#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007372 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007373 if (hold_gui_events)
7374 return;
7375#endif
7376
7377 mouse.operation = MOUSE_GETINFO;
7378 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7379 && ioctl(1, FBIO_MODEINFO, &video) != -1
7380 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7381 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7382 {
7383 row = mouse.u.data.y / video.vi_cheight;
7384 col = mouse.u.data.x / video.vi_cwidth;
7385 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007386 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007387 string[1] = 'M';
7388 string[2] = 'S';
7389 if (oldbuttons == buttons && buttons != 0)
7390 {
7391 button = MOUSE_DRAG;
7392 }
7393 else
7394 {
7395 switch (buttons)
7396 {
7397 case 0:
7398 button = MOUSE_RELEASE;
7399 break;
7400 case 1:
7401 button = MOUSE_LEFT;
7402 break;
7403 case 2:
7404 button = MOUSE_MIDDLE;
7405 break;
7406 case 4:
7407 button = MOUSE_RIGHT;
7408 break;
7409 default:
7410 return;
7411 }
7412 oldbuttons = buttons;
7413 }
7414 string[3] = (char_u)(button);
7415 string[4] = (char_u)(col + ' ' + 1);
7416 string[5] = (char_u)(row + ' ' + 1);
7417 add_to_input_buf(string, 6);
7418 }
7419 return;
7420}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007421#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007422
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007424typedef char_u * (*STRPROCSTR)(char_u *);
7425typedef char_u * (*INTPROCSTR)(int);
7426typedef int (*STRPROCINT)(char_u *);
7427typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428
7429/*
7430 * Call a DLL routine which takes either a string or int param
7431 * and returns an allocated string.
7432 */
7433 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007434mch_libcall(
7435 char_u *libname,
7436 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007437 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007438 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007439 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007440 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441{
7442# if defined(USE_DLOPEN)
7443 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007444 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445# else
7446 shl_t hinstLib;
7447# endif
7448 STRPROCSTR ProcAdd;
7449 INTPROCSTR ProcAddI;
7450 char_u *retval_str = NULL;
7451 int retval_int = 0;
7452 int success = FALSE;
7453
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007454 /*
7455 * Get a handle to the DLL module.
7456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007458 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007459 (void)dlerror();
7460
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 hinstLib = dlopen((char *)libname, RTLD_LAZY
7462# ifdef RTLD_LOCAL
7463 | RTLD_LOCAL
7464# endif
7465 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007466 if (hinstLib == NULL)
7467 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007468 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007469 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007470 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007471 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473# else
7474 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7475# endif
7476
Bram Moolenaar0f873732019-12-05 20:28:46 +01007477 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 if (hinstLib != NULL)
7479 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007480# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 /*
7482 * Catch a crash when calling the library function. For example when
7483 * using a number where a string pointer is expected.
7484 */
7485 mch_startjmp();
7486 if (SETJMP(lc_jump_env) != 0)
7487 {
7488 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007489# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007490 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 mch_didjmp();
7493 }
7494 else
7495# endif
7496 {
7497 retval_str = NULL;
7498 retval_int = 0;
7499
7500 if (argstring != NULL)
7501 {
7502# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007503 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007504 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505# else
7506 if (shl_findsym(&hinstLib, (const char *)funcname,
7507 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7508 ProcAdd = NULL;
7509# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007510 if ((success = (ProcAdd != NULL
7511# if defined(USE_DLOPEN)
7512 && dlerr == NULL
7513# endif
7514 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 {
7516 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007517 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 else
7519 retval_str = (ProcAdd)(argstring);
7520 }
7521 }
7522 else
7523 {
7524# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007525 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007526 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527# else
7528 if (shl_findsym(&hinstLib, (const char *)funcname,
7529 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7530 ProcAddI = NULL;
7531# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007532 if ((success = (ProcAddI != NULL
7533# if defined(USE_DLOPEN)
7534 && dlerr == NULL
7535# endif
7536 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 {
7538 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007539 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 else
7541 retval_str = (ProcAddI)(argint);
7542 }
7543 }
7544
Bram Moolenaar0f873732019-12-05 20:28:46 +01007545 // Save the string before we free the library.
7546 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 if (string_result == NULL)
7548 *number_result = retval_int;
7549 else if (retval_str != NULL
7550 && retval_str != (char_u *)1
7551 && retval_str != (char_u *)-1)
7552 *string_result = vim_strsave(retval_str);
7553 }
7554
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007555# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 mch_endjmp();
7557# ifdef SIGHASARG
7558 if (lc_signal != 0)
7559 {
7560 int i;
7561
Bram Moolenaar0f873732019-12-05 20:28:46 +01007562 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 for (i = 0; signal_info[i].sig != -1; i++)
7564 if (lc_signal == signal_info[i].sig)
7565 break;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00007566 semsg(e_got_sig_str_in_libcall, signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 }
7568# endif
7569# endif
7570
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007572 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007573 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007574 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007575
Bram Moolenaar0f873732019-12-05 20:28:46 +01007576 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 (void)dlclose(hinstLib);
7578# else
7579 (void)shl_unload(hinstLib);
7580# endif
7581 }
7582
7583 if (!success)
7584 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007585 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 return FAIL;
7587 }
7588
7589 return OK;
7590}
7591#endif
7592
7593#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007594static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595static int xterm_button;
7596
7597/*
7598 * Setup a dummy window for X selections in a terminal.
7599 */
7600 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007601setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602{
7603 int z = 0;
7604 char *strp = "";
7605 Widget AppShell;
7606
7607 if (!x_connect_to_server())
7608 return;
7609
7610 open_app_context();
7611 if (app_context != NULL && xterm_Shell == (Widget)0)
7612 {
7613 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007614# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007616# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007617# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007618 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619
7620 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007621 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622# endif
7623
Bram Moolenaar0f873732019-12-05 20:28:46 +01007624 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 oldhandler = XSetErrorHandler(x_error_check);
7626
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007627# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007628 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7630 mch_startjmp();
7631 if (SETJMP(lc_jump_env) != 0)
7632 {
7633 mch_didjmp();
7634 xterm_dpy = NULL;
7635 }
7636 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {
7639 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7640 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007641 if (xterm_dpy != NULL)
7642 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007643# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 }
7647
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007648# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007649 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007651# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007652 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 (void)XSetErrorHandler(oldhandler);
7654
7655 if (xterm_dpy == NULL)
7656 {
7657 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007658 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 return;
7660 }
7661
Bram Moolenaar0f873732019-12-05 20:28:46 +01007662 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 (void)XSetIOErrorHandler(x_IOerror_handler);
7664
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007665# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007667 {
7668 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007669 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007670 verbose_leave();
7671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672# endif
7673
Bram Moolenaar0f873732019-12-05 20:28:46 +01007674 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7676 applicationShellWidgetClass, xterm_dpy,
7677 NULL);
7678 if (AppShell == (Widget)0)
7679 return;
7680 xterm_Shell = XtVaCreatePopupShell("VIM",
7681 topLevelShellWidgetClass, AppShell,
7682 XtNmappedWhenManaged, 0,
7683 XtNwidth, 1,
7684 XtNheight, 1,
7685 NULL);
7686 if (xterm_Shell == (Widget)0)
7687 return;
7688
7689 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007690 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 if (x11_display == NULL)
7692 x11_display = xterm_dpy;
7693
7694 XtRealizeWidget(xterm_Shell);
7695 XSync(xterm_dpy, False);
7696 xterm_update();
7697 }
7698 if (xterm_Shell != (Widget)0)
7699 {
7700 clip_init(TRUE);
7701 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7702 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007703 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 if (test_x11_window(xterm_dpy) == FAIL)
7705 x11_window = 0;
7706 if (x11_window != 0)
7707 xterm_trace = 0;
7708 }
7709}
7710
7711 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007712start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
7714 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7715 return;
7716 xterm_trace = 1;
7717 xterm_button = button;
7718 do_xterm_trace();
7719}
7720
7721
7722 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007723stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724{
7725 if (xterm_trace < 0)
7726 return;
7727 xterm_trace = 0;
7728}
7729
7730/*
7731 * Query the xterm pointer and generate mouse termcodes if necessary
7732 * return TRUE if dragging is active, else FALSE
7733 */
7734 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007735do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
7737 Window root, child;
7738 int root_x, root_y;
7739 int win_x, win_y;
7740 int row, col;
7741 int_u mask_return;
7742 char_u buf[50];
7743 char_u *strp;
7744 long got_hints;
7745 static char_u *mouse_code;
7746 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7747 static int prev_row = 0, prev_col = 0;
7748 static XSizeHints xterm_hints;
7749
7750 if (xterm_trace <= 0)
7751 return FALSE;
7752
7753 if (xterm_trace == 1)
7754 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007755 // Get the hints just before tracking starts. The font size might
7756 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007757 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7758 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 || xterm_hints.width_inc <= 1
7760 || xterm_hints.height_inc <= 1)
7761 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007762 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 return FALSE;
7764 }
7765
Bram Moolenaar0f873732019-12-05 20:28:46 +01007766 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 mouse_code = find_termcode(mouse_name);
7768 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007769 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 xterm_trace = 2;
7771
Bram Moolenaar0f873732019-12-05 20:28:46 +01007772 // Find the offset of the chars, there might be a scrollbar on the
7773 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7775 &win_x, &win_y, &mask_return);
7776 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7777 - (xterm_hints.height_inc / 2);
7778 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7779 xterm_hints.y = 2;
7780 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7781 - (xterm_hints.width_inc / 2);
7782 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7783 xterm_hints.x = 2;
7784 return TRUE;
7785 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007786 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 {
7788 xterm_trace = 0;
7789 return FALSE;
7790 }
7791
7792 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7793 &win_x, &win_y, &mask_return);
7794
7795 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7796 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7797 if (row == prev_row && col == prev_col)
7798 return TRUE;
7799
7800 STRCPY(buf, mouse_code);
7801 strp = buf + STRLEN(buf);
7802 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7803 *strp++ = (char_u)(col + ' ' + 1);
7804 *strp++ = (char_u)(row + ' ' + 1);
7805 *strp = 0;
7806 add_to_input_buf(buf, STRLEN(buf));
7807
7808 prev_row = row;
7809 prev_col = col;
7810 return TRUE;
7811}
7812
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007813# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814/*
7815 * Destroy the display, window and app_context. Required for GTK.
7816 */
7817 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007818clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820 if (xterm_Shell != (Widget)0)
7821 {
7822 XtDestroyWidget(xterm_Shell);
7823 xterm_Shell = (Widget)0;
7824 }
7825 if (xterm_dpy != NULL)
7826 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007827# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007828 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007830# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 if (x11_display == xterm_dpy)
7832 x11_display = NULL;
7833 xterm_dpy = NULL;
7834 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007835# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 if (app_context != (XtAppContext)NULL)
7837 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007838 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 XtDestroyApplicationContext(app_context);
7840 app_context = (XtAppContext)NULL;
7841 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843}
7844# endif
7845
7846/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007847 * Catch up with GUI or X events.
7848 */
7849 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007850clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007851{
7852# ifdef FEAT_GUI
7853 if (gui.in_use)
7854 gui_mch_update();
7855 else
7856# endif
7857 if (xterm_Shell != (Widget)0)
7858 xterm_update();
7859}
7860
7861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 * Catch up with any queued X events. This may put keyboard input into the
7863 * input buffer, call resize call-backs, trigger timers etc. If there is
7864 * nothing in the X event queue (& no timers pending), then we return
7865 * immediately.
7866 */
7867 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007868xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869{
7870 XEvent event;
7871
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007872 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007874 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007876 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007877 break;
7878
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007879 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007880 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007881 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007882 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007883#ifdef FEAT_CLIENTSERVER
7884 {
7885 XPropertyEvent *e = (XPropertyEvent *)&event;
7886
7887 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007889 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007892 XtDispatchEvent(&event);
7893 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007894 else
7895 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007896 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007897 XtAppProcessEvent(app_context, mask);
7898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 }
7900}
7901
7902 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007903clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904{
7905 if (xterm_Shell != (Widget)0)
7906 return clip_x11_own_selection(xterm_Shell, cbd);
7907 return FAIL;
7908}
7909
7910 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007911clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912{
7913 if (xterm_Shell != (Widget)0)
7914 clip_x11_lose_selection(xterm_Shell, cbd);
7915}
7916
7917 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007918clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919{
7920 if (xterm_Shell != (Widget)0)
7921 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7922}
7923
7924 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007925clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927 clip_x11_set_selection(cbd);
7928}
7929#endif
7930
7931
7932#if defined(USE_XSMP) || defined(PROTO)
7933/*
7934 * Code for X Session Management Protocol.
7935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936
7937# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938/*
7939 * This is our chance to ask the user if they want to save,
7940 * or abort the logout
7941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007943xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944{
Bram Moolenaare1004402020-10-24 20:49:43 +02007945 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 int cancel_shutdown = False;
7947
Bram Moolenaare1004402020-10-24 20:49:43 +02007948 save_cmod_flags = cmdmod.cmod_flags;
7949 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007950 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007951 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02007953 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007954 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 out_flush();
7956
Bram Moolenaar0f873732019-12-05 20:28:46 +01007957 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 SmcInteractDone(smc_conn, cancel_shutdown);
7959
Bram Moolenaar0f873732019-12-05 20:28:46 +01007960 // Finish off
7961 // Only end save-yourself here if we're not cancelling shutdown;
7962 // we'll get a cancelled callback later in which we'll end it.
7963 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 if (!cancel_shutdown)
7965 {
7966 xsmp.save_yourself = False;
7967 SmcSaveYourselfDone(smc_conn, True);
7968 }
7969}
7970# endif
7971
7972/*
7973 * Callback that starts save-yourself.
7974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007976xsmp_handle_save_yourself(
7977 SmcConn smc_conn,
7978 SmPointer client_data UNUSED,
7979 int save_type UNUSED,
7980 Bool shutdown,
7981 int interact_style UNUSED,
7982 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007984 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 if (xsmp.save_yourself)
7986 SmcSaveYourselfDone(smc_conn, True);
7987 xsmp.save_yourself = True;
7988 xsmp.shutdown = shutdown;
7989
Bram Moolenaar0f873732019-12-05 20:28:46 +01007990 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007992 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993
7994 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007995 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996
7997# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007998 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008000 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 SmcInteractRequest(smc_conn, SmDialogError,
8002 xsmp_handle_interaction, client_data);
8003 else
8004# endif
8005 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008006 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 SmcSaveYourselfDone(smc_conn, True);
8008 xsmp.save_yourself = False;
8009 }
8010}
8011
8012
8013/*
8014 * Callback to warn us of imminent death.
8015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008017xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018{
8019 xsmp_close();
8020
Bram Moolenaar0f873732019-12-05 20:28:46 +01008021 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 getout_preserve_modified(0);
8023}
8024
8025
8026/*
8027 * Callback to tell us that save-yourself has completed.
8028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008030xsmp_save_complete(
8031 SmcConn smc_conn UNUSED,
8032 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033{
8034 xsmp.save_yourself = False;
8035}
8036
8037
8038/*
8039 * Callback to tell us that an instigated shutdown was cancelled
8040 * (maybe even by us)
8041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008043xsmp_shutdown_cancelled(
8044 SmcConn smc_conn,
8045 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046{
8047 if (xsmp.save_yourself)
8048 SmcSaveYourselfDone(smc_conn, True);
8049 xsmp.save_yourself = False;
8050 xsmp.shutdown = False;
8051}
8052
8053
8054/*
8055 * Callback to tell us that a new ICE connection has been established.
8056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008058xsmp_ice_connection(
8059 IceConn iceConn,
8060 IcePointer clientData UNUSED,
8061 Bool opening,
8062 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008064 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (opening)
8066 {
8067 xsmp_icefd = IceConnectionNumber(iceConn);
8068 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8069 }
8070}
8071
8072
Bram Moolenaar0f873732019-12-05 20:28:46 +01008073// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008075xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
8077 Bool rep;
8078
8079 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8080 == IceProcessMessagesIOError)
8081 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008082 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008084 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 xsmp_close();
8086 return FAIL;
8087 }
8088 else
8089 return OK;
8090}
8091
8092static int dummy;
8093
Bram Moolenaar0f873732019-12-05 20:28:46 +01008094// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 void
8096xsmp_init(void)
8097{
8098 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 SmcCallbacks smcallbacks;
8100#if 0
8101 SmPropValue smname;
8102 SmProp smnameprop;
8103 SmProp *smprops[1];
8104#endif
8105
8106 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008107 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108
8109 xsmp.save_yourself = xsmp.shutdown = False;
8110
Bram Moolenaar0f873732019-12-05 20:28:46 +01008111 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8113 smcallbacks.save_yourself.client_data = NULL;
8114 smcallbacks.die.callback = xsmp_die;
8115 smcallbacks.die.client_data = NULL;
8116 smcallbacks.save_complete.callback = xsmp_save_complete;
8117 smcallbacks.save_complete.client_data = NULL;
8118 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8119 smcallbacks.shutdown_cancelled.client_data = NULL;
8120
Bram Moolenaar0f873732019-12-05 20:28:46 +01008121 // Set up a watch on ICE connection creations. The "dummy" argument is
8122 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8124 {
8125 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008126 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 return;
8128 }
8129
Bram Moolenaar0f873732019-12-05 20:28:46 +01008130 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 xsmp.smcconn = SmcOpenConnection(
8132 NULL,
8133 NULL,
8134 SmProtoMajor,
8135 SmProtoMinor,
8136 SmcSaveYourselfProcMask | SmcDieProcMask
8137 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8138 &smcallbacks,
8139 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008140 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008141 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 errorstring);
8143 if (xsmp.smcconn == NULL)
8144 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008146 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008147 char errorreport[132];
8148
8149 // If the message is too long it might not be NUL terminated. Add
8150 // a NUL at the end to make sure we don't go over the end.
8151 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008152 vim_snprintf(errorreport, sizeof(errorreport),
8153 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008154 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 return;
8157 }
8158 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8159
8160#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008161 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 smname.value = "vim";
8163 smname.length = 3;
8164 smnameprop.name = "SmProgram";
8165 smnameprop.type = "SmARRAY8";
8166 smnameprop.num_vals = 1;
8167 smnameprop.vals = &smname;
8168
8169 smprops[0] = &smnameprop;
8170 SmcSetProperties(xsmp.smcconn, 1, smprops);
8171#endif
8172}
8173
8174
Bram Moolenaar0f873732019-12-05 20:28:46 +01008175// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008177xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178{
8179 if (xsmp_icefd != -1)
8180 {
8181 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008182 if (xsmp.clientid != NULL)
8183 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008184 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 xsmp_icefd = -1;
8186 }
8187}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008188#endif // USE_XSMP