blob: 58f8276fb595d20e177a9e82c6a011f63ea6d91d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * OS/2 port by Paul Slootman
5 * VMS merge by Zoltan Arpadffy
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code.
10 */
11
12/*
13 * os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
14 * Also for OS/2, using the excellent EMX package!!!
Bram Moolenaar041c7102020-05-30 18:14:57 +020015 * Also for Atari MiNT.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016 *
17 * A lot of this file was originally written by Juergen Weigert and later
18 * changed beyond recognition.
19 */
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#include "vim.h"
22
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023#ifdef FEAT_MZSCHEME
24# include "if_mzsch.h"
25#endif
26
Bram Moolenaar0f873732019-12-05 20:28:46 +010027#include "os_unixx.h" // unix includes for os_unix.c only
Bram Moolenaar071d4272004-06-13 20:20:40 +000028
29#ifdef USE_XSMP
30# include <X11/SM/SMlib.h>
31#endif
32
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000033#ifdef HAVE_SELINUX
34# include <selinux/selinux.h>
35static int selinux_enabled = -1;
36#endif
37
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020038#ifdef HAVE_SMACK
39# include <attr/xattr.h>
40# include <linux/xattr.h>
41# ifndef SMACK_LABEL_LEN
42# define SMACK_LABEL_LEN 1024
43# endif
44#endif
45
Bram Moolenaara2442432007-04-26 14:26:37 +000046#ifdef __CYGWIN__
K.Takata972db232022-02-04 10:45:38 +000047# include <cygwin/version.h>
48# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
Bram Moolenaar0f873732019-12-05 20:28:46 +010049 // for cygwin_conv_path()
K.Takata972db232022-02-04 10:45:38 +000050# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
51# define WIN32_LEAN_AND_MEAN
52# include <windows.h>
53# include "winclip.pro"
Bram Moolenaara2442432007-04-26 14:26:37 +000054# endif
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000058
Bram Moolenaar071d4272004-06-13 20:20:40 +000059# include <gpm.h>
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000060
61# ifdef DYNAMIC_GPM
62# define Gpm_Open (*dll_Gpm_Open)
63# define Gpm_Close (*dll_Gpm_Close)
64# define Gpm_GetEvent (*dll_Gpm_GetEvent)
65# define gpm_flag (dll_gpm_flag != NULL ? *dll_gpm_flag : 0)
66# define gpm_fd (dll_gpm_fd != NULL ? *dll_gpm_fd : -1)
67
68static int (*dll_Gpm_Open) (Gpm_Connect *, int);
69static int (*dll_Gpm_Close) (void);
70static int (*dll_Gpm_GetEvent) (Gpm_Event *);
71static int *dll_gpm_flag;
72static int *dll_gpm_fd;
73
74static void *libgpm_hinst;
75# endif
76
Bram Moolenaar0f873732019-12-05 20:28:46 +010077// <linux/keyboard.h> contains defines conflicting with "keymap.h",
78// I just copied relevant defines here. A cleaner solution would be to put gpm
79// code into separate file and include there linux/keyboard.h
80// #include <linux/keyboard.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000081# define KG_SHIFT 0
82# define KG_CTRL 2
83# define KG_ALT 3
84# define KG_ALTGR 1
85# define KG_SHIFTL 4
86# define KG_SHIFTR 5
87# define KG_CTRLL 6
88# define KG_CTRLR 7
89# define KG_CAPSSHIFT 8
90
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010091static void gpm_close(void);
92static int gpm_open(void);
93static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#endif
95
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000096#ifdef FEAT_SYSMOUSE
97# include <sys/consio.h>
98# include <sys/fbio.h>
99
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100100static int sysmouse_open(void);
101static void sysmouse_close(void);
Bram Moolenaard99df422016-01-29 23:20:40 +0100102static RETSIGTYPE sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000103#endif
104
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105/*
106 * end of autoconf section. To be extended...
107 */
108
Bram Moolenaar0f873732019-12-05 20:28:46 +0100109// Are the following #ifdefs still required? And why? Is that for X11?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
111#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
112# ifdef SIGWINCH
113# undef SIGWINCH
114# endif
115# ifdef TIOCGWINSZ
116# undef TIOCGWINSZ
117# endif
118#endif
119
Bram Moolenaar0f873732019-12-05 20:28:46 +0100120#if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121# define SIGWINCH SIGWINDOW
122#endif
123
124#ifdef FEAT_X11
125# include <X11/Xlib.h>
126# include <X11/Xutil.h>
127# include <X11/Xatom.h>
128# ifdef FEAT_XCLIPBOARD
129# include <X11/Intrinsic.h>
130# include <X11/Shell.h>
131# include <X11/StringDefs.h>
132static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void clip_update(void);
134static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135# endif
136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137Window x11_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
140
Bram Moolenaar5c3128e2020-05-11 20:54:42 +0200141static int ignore_sigtstp = FALSE;
142
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200146static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +0200147static int unix_did_set_title = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static char_u *oldicon = NULL;
149static int did_set_icon = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
Bram Moolenaar205b8862011-09-07 15:04:31 +0200153#ifdef HAVE_UNION_WAIT
154typedef union wait waitstatus;
155#else
156typedef int waitstatus;
157#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200158static int WaitForChar(long msec, int *interrupted, int ignore_input);
159static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar041c7102020-05-30 18:14:57 +0200160#ifdef VMS
Bram Moolenaarcda77642016-06-04 13:32:35 +0200161int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200163static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#endif
165
166#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100167static int do_xterm_trace(void);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100168# define XT_TRACE_DELAY 50 // delay for xterm tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
170
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
173#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100174static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000176#if defined(SIGTSTP)
177static RETSIGTYPE sig_tstp SIGPROTOARG;
178// volatile because it is used in signal handler sig_tstp() and sigcont_handler().
179static volatile sig_atomic_t in_mch_suspend = FALSE;
180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100182static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200184#if defined(SIGUSR1)
185static RETSIGTYPE catch_sigusr1 SIGPROTOARG;
186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100188static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000190#if defined(SIGALRM) && defined(FEAT_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100192static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100193// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200194static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100196static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100198static void catch_int_signal(void);
199static void set_signals(void);
200static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200201#ifdef HAVE_SIGPROCMASK
202# define SIGSET_DECL(set) sigset_t set;
203# define BLOCK_SIGNALS(set) block_signals(set)
204# define UNBLOCK_SIGNALS(set) unblock_signals(set)
205#else
206# define SIGSET_DECL(set)
207# define BLOCK_SIGNALS(set) do { /**/ } while (0)
208# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
209#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100210static int have_wildcard(int, char_u **);
211static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100213static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
215#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000216# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217#endif
218
Bram Moolenaar0f873732019-12-05 20:28:46 +0100219// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200220static volatile sig_atomic_t do_resize = FALSE;
dbivolaruab16ad32021-12-29 19:41:47 +0000221// volatile because it is used in signal handler sig_tstp().
222static volatile sig_atomic_t got_tstp = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223static char_u *extra_shell_arg = NULL;
224static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100225// volatile because it is used in signal handler deathtrap().
226static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
227// volatile because it is used in signal handler deathtrap().
228static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100230#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
231static int dont_check_job_ended = 0;
232#endif
233
Bram Moolenaar26e86442020-05-17 14:06:16 +0200234// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
235static tmode_T mch_cur_tmode = TMODE_COOK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236
237#ifdef USE_XSMP
238typedef struct
239{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100240 SmcConn smcconn; // The SM connection ID
241 IceConn iceconn; // The ICE connection ID
242 char *clientid; // The client ID for the current smc session
243 Bool save_yourself; // If we're in the middle of a save_yourself
244 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245} xsmp_config_T;
246
247static xsmp_config_T xsmp;
248#endif
249
250#ifdef SYS_SIGLIST_DECLARED
251/*
252 * I have seen
253 * extern char *_sys_siglist[NSIG];
254 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
255 * that describe the signals. That is nearly what we want here. But
256 * autoconf does only check for sys_siglist (without the underscore), I
257 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100258 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 */
260#endif
261
262static struct signalinfo
263{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100264 int sig; // Signal number, eg. SIGSEGV etc
265 char *name; // Signal name (not char_u!).
266 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267} signal_info[] =
268{
269#ifdef SIGHUP
270 {SIGHUP, "HUP", TRUE},
271#endif
272#ifdef SIGQUIT
273 {SIGQUIT, "QUIT", TRUE},
274#endif
275#ifdef SIGILL
276 {SIGILL, "ILL", TRUE},
277#endif
278#ifdef SIGTRAP
279 {SIGTRAP, "TRAP", TRUE},
280#endif
281#ifdef SIGABRT
282 {SIGABRT, "ABRT", TRUE},
283#endif
284#ifdef SIGEMT
285 {SIGEMT, "EMT", TRUE},
286#endif
287#ifdef SIGFPE
288 {SIGFPE, "FPE", TRUE},
289#endif
290#ifdef SIGBUS
291 {SIGBUS, "BUS", TRUE},
292#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100293#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100294 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 {SIGSEGV, "SEGV", TRUE},
296#endif
297#ifdef SIGSYS
298 {SIGSYS, "SYS", TRUE},
299#endif
300#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100301 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302#endif
303#ifdef SIGTERM
304 {SIGTERM, "TERM", TRUE},
305#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100306#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {SIGVTALRM, "VTALRM", TRUE},
308#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000309#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100310 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
311 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 {SIGPROF, "PROF", TRUE},
313#endif
314#ifdef SIGXCPU
315 {SIGXCPU, "XCPU", TRUE},
316#endif
317#ifdef SIGXFSZ
318 {SIGXFSZ, "XFSZ", TRUE},
319#endif
320#ifdef SIGUSR1
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200321 {SIGUSR1, "USR1", FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000323#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100324 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 {SIGUSR2, "USR2", TRUE},
326#endif
327#ifdef SIGINT
328 {SIGINT, "INT", FALSE},
329#endif
330#ifdef SIGWINCH
331 {SIGWINCH, "WINCH", FALSE},
332#endif
333#ifdef SIGTSTP
334 {SIGTSTP, "TSTP", FALSE},
335#endif
336#ifdef SIGPIPE
337 {SIGPIPE, "PIPE", FALSE},
338#endif
339 {-1, "Unknown!", FALSE}
340};
341
Bram Moolenaar25724922009-07-14 15:38:41 +0000342 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100343mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000344{
345 if (p_verbose >= 5)
346 {
347 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100348 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000349 verbose_leave();
350 }
351# ifdef VMS
352 return chdir(vms_fixfilename(path));
353# else
354 return chdir(path);
355# endif
356}
357
Bram Moolenaar0f873732019-12-05 20:28:46 +0100358// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100359#if defined(ECHOE) && defined(ICANON) \
360 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
361 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200362# define NEW_TTY_SYSTEM
363#endif
364
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000365/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100366 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100369mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200371 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100372 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100373 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374}
375
376/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100377 * Function passed to inchar_loop() to handle window resizing.
378 * If "check_only" is TRUE: Return whether there was a resize.
379 * If "check_only" is FALSE: Deal with the window resized.
380 */
381 static int
382resize_func(int check_only)
383{
384 if (check_only)
385 return do_resize;
386 while (do_resize)
387 handle_resize();
388 return FALSE;
389}
390
391/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000392 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 * Get a characters from the keyboard.
394 * Return the number of characters that are available.
395 * If wtime == 0 do not wait for characters.
396 * If wtime == n wait a short time for characters.
397 * If wtime == -1 wait forever for characters.
398 */
399 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100400mch_inchar(
401 char_u *buf,
402 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100403 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100404 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100406 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
407 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408}
409
410 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100411handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412{
413 do_resize = FALSE;
414 shell_resized();
415}
416
417/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200418 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 */
420 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100421mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200423 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424}
425
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200426#if defined(FEAT_TERMINAL) || defined(PROTO)
427/*
428 * Check for any pending input or messages.
429 */
430 int
431mch_check_messages(void)
432{
433 return WaitForChar(0L, NULL, TRUE);
434}
435#endif
436
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
438# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000439# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440# endif
441# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
442# include <sys/sysctl.h>
443# endif
444# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
445# include <sys/sysinfo.h>
446# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100447# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100448# include <mach/mach_host.h>
449# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451
452/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000453 * Return total amount of memory available in Kbyte.
454 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100457mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100460 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461
Bram Moolenaar362dc332018-03-05 21:59:37 +0100462# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100463 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100464 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100465 mach_port_t host = mach_host_self();
466 kern_return_t kret;
467# ifdef HOST_VM_INFO64
468 struct vm_statistics64 vm_stat;
469 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
Bram Moolenaar988615f2018-02-27 17:58:20 +0100471 kret = host_statistics64(host, HOST_VM_INFO64,
472 (host_info64_t)&vm_stat, &count);
473# else
474 struct vm_statistics vm_stat;
475 natural_t count = HOST_VM_INFO_COUNT;
476
477 kret = host_statistics(host, HOST_VM_INFO,
478 (host_info_t)&vm_stat, &count);
479# endif
480 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100481 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100482 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
483 + vm_stat.inactive_count
484# ifdef MAC_OS_X_VERSION_10_9
485 + vm_stat.compressor_page_count
486# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100487 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100488 mach_port_deallocate(mach_task_self(), host);
489 }
490# endif
491
492# ifdef HAVE_SYSCTL
493 if (mem == 0)
494 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100495 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100496 int mib[2];
497 size_t len = sizeof(long_u);
498# ifdef HW_USERMEM64
499 long_u physmem;
500# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100501 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100502 union {
503 int_u u32;
504 long_u u64;
505 } physmem;
506# endif
507
508 mib[0] = CTL_HW;
509# ifdef HW_USERMEM64
510 mib[1] = HW_USERMEM64;
511# else
512 mib[1] = HW_USERMEM;
513# endif
514 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
515 {
516# ifdef HW_USERMEM64
517 mem = (long_u)physmem;
518# else
519 if (len == sizeof(physmem.u64))
520 mem = (long_u)physmem.u64;
521 else
522 mem = (long_u)physmem.u32;
523# endif
524 }
525 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200528# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 if (mem == 0)
530 {
531 struct sysinfo sinfo;
532
Bram Moolenaar0f873732019-12-05 20:28:46 +0100533 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000535 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200536# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100537 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000538 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
539 {
540 sinfo.mem_unit = sinfo.mem_unit >> 1;
541 --shiftright;
542 }
543 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200544# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200546# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200549# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200551# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (mem == 0)
553 {
554 long pagesize, pagecount;
555
Bram Moolenaar0f873732019-12-05 20:28:46 +0100556 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 pagesize = sysconf(_SC_PAGESIZE);
558 pagecount = sysconf(_SC_PHYS_PAGES);
559 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000560 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100561 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000562 while (shiftright > 0 && (pagesize & 1) == 0)
563 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000564 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000565 --shiftright;
566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
Bram Moolenaar0f873732019-12-05 20:28:46 +0100572 // Return the minimum of the physical memory and the user limit, because
573 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200574# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 {
576 struct rlimit rlp;
577
578 if (getrlimit(RLIMIT_DATA, &rlp) == 0
579 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200580# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200582# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000583 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000585 {
586 mem = (long_u)rlp.rlim_cur;
587 shiftright = 10;
588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000593 return mem >> shiftright;
594 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595}
596#endif
597
Bram Moolenaar0981c872020-08-23 14:28:37 +0200598/*
599 * "flags": MCH_DELAY_IGNOREINPUT - don't read input
600 * MCH_DELAY_SETTMODE - use settmode() even for short delays
601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200603mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200605 tmode_T old_tmode;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200606 int call_settmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000607#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100608 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
Bram Moolenaar0981c872020-08-23 14:28:37 +0200611 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100613 // Go to cooked mode without echo, to allow SIGINT interrupting us
614 // here. But we don't want QUIT to kill us (CTRL-\ used in a
615 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200616 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000617 in_mch_delay = TRUE;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200618 call_settmode = mch_cur_tmode == TMODE_RAW
619 && (msec > 500 || (flags & MCH_DELAY_SETTMODE));
620 if (call_settmode)
621 {
622 old_tmode = mch_cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 settmode(TMODE_SLEEP);
Bram Moolenaar80361a52020-10-05 21:39:25 +0200624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
626 /*
627 * Everybody sleeps in a different way...
628 * Prefer nanosleep(), some versions of usleep() can only sleep up to
629 * one second.
630 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000631#ifdef FEAT_MZSCHEME
632 do
633 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100634 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000635 if (total > p_mzq)
636 msec = p_mzq;
637 else
638 msec = total;
639 total -= msec;
640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641#ifdef HAVE_NANOSLEEP
642 {
643 struct timespec ts;
644
645 ts.tv_sec = msec / 1000;
646 ts.tv_nsec = (msec % 1000) * 1000000;
647 (void)nanosleep(&ts, NULL);
648 }
649#else
650# ifdef HAVE_USLEEP
651 while (msec >= 1000)
652 {
653 usleep((unsigned int)(999 * 1000));
654 msec -= 999;
655 }
656 usleep((unsigned int)(msec * 1000));
657# else
658# ifndef HAVE_SELECT
659 poll(NULL, 0, (int)msec);
660# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
662 struct timeval tv;
663
664 tv.tv_sec = msec / 1000;
665 tv.tv_usec = (msec % 1000) * 1000;
Bram Moolenaar0981c872020-08-23 14:28:37 +0200666 // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
667 // a patch from Sun to fix this. Reported by Gunnar Pedersen.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 select(0, NULL, NULL, NULL, &tv);
669 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100670# endif // HAVE_SELECT
671# endif // HAVE_NANOSLEEP
672#endif // HAVE_USLEEP
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000673#ifdef FEAT_MZSCHEME
674 }
675 while (total > 0);
676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
Bram Moolenaar80361a52020-10-05 21:39:25 +0200678 if (call_settmode)
Bram Moolenaar26e86442020-05-17 14:06:16 +0200679 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000680 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 }
682 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200683 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684}
685
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000686#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
688# define HAVE_CHECK_STACK_GROWTH
689/*
690 * Support for checking for an almost-out-of-stack-space situation.
691 */
692
693/*
694 * Return a pointer to an item on the stack. Used to find out if the stack
695 * grows up or down.
696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697static int stack_grows_downwards;
698
699/*
700 * Find out if the stack grows upwards or downwards.
701 * "p" points to a variable on the stack of the caller.
702 */
703 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100704check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705{
706 int i;
707
708 stack_grows_downwards = (p > (char *)&i);
709}
710#endif
711
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000712#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713static char *stack_limit = NULL;
714
715#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
716# include <pthread.h>
717# include <pthread_np.h>
718#endif
719
720/*
721 * Find out until how var the stack can grow without getting into trouble.
722 * Called when starting up and when switching to the signal stack in
723 * deathtrap().
724 */
725 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100726get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
728 struct rlimit rlp;
729 int i;
730 long lim;
731
Bram Moolenaar0f873732019-12-05 20:28:46 +0100732 // Set the stack limit to 15/16 of the allowable size. Skip this when the
733 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 if (getrlimit(RLIMIT_STACK, &rlp) == 0
735 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
736# ifdef RLIM_INFINITY
737 && rlp.rlim_cur != RLIM_INFINITY
738# endif
739 )
740 {
741 lim = (long)rlp.rlim_cur;
742#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
743 {
744 pthread_attr_t attr;
745 size_t size;
746
Bram Moolenaar0f873732019-12-05 20:28:46 +0100747 // On FreeBSD the initial thread always has a fixed stack size, no
748 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 pthread_attr_init(&attr);
750 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
751 {
752 pthread_attr_getstacksize(&attr, &size);
753 if (lim > (long)size)
754 lim = (long)size;
755 }
756 pthread_attr_destroy(&attr);
757 }
758#endif
759 if (stack_grows_downwards)
760 {
761 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
762 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100763 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 stack_limit = (char *)((long)&i / 16L);
765 }
766 else
767 {
768 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
769 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100770 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 }
772 }
773}
774
775/*
776 * Return FAIL when running out of stack space.
777 * "p" must point to any variable local to the caller that's on the stack.
778 */
779 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100780mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781{
782 if (stack_limit != NULL)
783 {
784 if (stack_grows_downwards)
785 {
786 if (p < stack_limit)
787 return FAIL;
788 }
789 else if (p > stack_limit)
790 return FAIL;
791 }
792 return OK;
793}
794#endif
795
796#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
797/*
798 * Support for using the signal stack.
799 * This helps when we run out of stack space, which causes a SIGSEGV. The
800 * signal handler then must run on another stack, since the normal stack is
801 * completely full.
802 */
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100805static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100807static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808# endif
809
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200810/*
811 * Get a size of signal stack.
812 * Preference (if available): sysconf > SIGSTKSZ > guessed size
813 */
814static long int get_signal_stack_size()
815{
816# ifdef HAVE_SYSCONF_SIGSTKSZ
817 long int size = -1;
818
819 // return size only if sysconf doesn't return an error
820 if ((size = sysconf(_SC_SIGSTKSZ)) > -1)
821 return size;
822# endif
823
824# ifdef SIGSTKSZ
825 // if sysconf() isn't available or gives error, return SIGSTKSZ
826 // if defined
827 return SIGSTKSZ;
828# endif
829
830 // otherwise guess the size
831 return 8000;
832}
833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834static char *signal_stack;
835
836 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100837init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 if (signal_stack != NULL)
840 {
841# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842# ifdef HAVE_SS_BASE
843 sigstk.ss_base = signal_stack;
844# else
845 sigstk.ss_sp = signal_stack;
846# endif
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200847 sigstk.ss_size = get_signal_stack_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 sigstk.ss_flags = 0;
849 (void)sigaltstack(&sigstk, NULL);
850# else
851 sigstk.ss_sp = signal_stack;
852 if (stack_grows_downwards)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200853 sigstk.ss_sp += get_signal_stack_size() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 sigstk.ss_onstack = 0;
855 (void)sigstack(&sigstk, NULL);
856# endif
857 }
858}
859#endif
860
861/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000862 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * will barf when the second argument to signal() is ``wrong''.
864 * Let me try it with a few tricky defines from my own osdef.h (jw).
865 */
866#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 static RETSIGTYPE
868sig_winch SIGDEFARG(sigarg)
869{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100870 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
872 do_resize = TRUE;
873 SIGRETURN;
874}
875#endif
876
dbivolaruab16ad32021-12-29 19:41:47 +0000877#if defined(SIGTSTP)
878 static RETSIGTYPE
879sig_tstp SIGDEFARG(sigarg)
880{
881 // Second time we get called we actually need to suspend
882 if (in_mch_suspend)
883 {
884 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
885 raise(sigarg);
886 }
dbivolaru79a6e252022-01-23 16:41:14 +0000887 else
888 got_tstp = TRUE;
dbivolaruab16ad32021-12-29 19:41:47 +0000889
xtkobacbef12e2022-02-27 12:31:52 +0000890#ifndef __ANDROID__
891 // this is not required on all systems
dbivolaruab16ad32021-12-29 19:41:47 +0000892 signal(SIGTSTP, (RETSIGTYPE (*)())sig_tstp);
xtkobacbef12e2022-02-27 12:31:52 +0000893#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000894 SIGRETURN;
895}
896#endif
897
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 static RETSIGTYPE
900catch_sigint SIGDEFARG(sigarg)
901{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100902 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
904 got_int = TRUE;
905 SIGRETURN;
906}
907#endif
908
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200909#if defined(SIGUSR1)
910 static RETSIGTYPE
911catch_sigusr1 SIGDEFARG(sigarg)
912{
913 // this is not required on all systems, but it doesn't hurt anybody
914 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
915 got_sigusr1 = TRUE;
916 SIGRETURN;
917}
918#endif
919
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 static RETSIGTYPE
922catch_sigpwr SIGDEFARG(sigarg)
923{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100924 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000925 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 /*
927 * I'm not sure we get the SIGPWR signal when the system is really going
928 * down or when the batteries are almost empty. Just preserve the swap
929 * files and don't exit, that can't do any harm.
930 */
931 ml_sync_all(FALSE, FALSE);
932 SIGRETURN;
933}
934#endif
935
936#ifdef SET_SIG_ALARM
937/*
938 * signal function for alarm().
939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 static RETSIGTYPE
941sig_alarm SIGDEFARG(sigarg)
942{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100943 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 sig_alarm_called = TRUE;
945 SIGRETURN;
946}
947#endif
948
Bram Moolenaaredce7422019-01-20 18:39:30 +0100949#if (defined(HAVE_SETJMP_H) \
950 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
951 || defined(FEAT_LIBCALL))) \
952 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100953# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100954
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100955// argument to SETJMP()
956static JMP_BUF lc_jump_env;
957
958# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100959// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100960// Volatile because it is used in signal handlers.
961static volatile sig_atomic_t lc_signal;
962# endif
963
964// TRUE when lc_jump_env is valid.
965// Volatile because it is used in signal handler deathtrap().
966static volatile sig_atomic_t lc_active INIT(= FALSE);
967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968/*
969 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100970 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100972 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 * Usage:
974 * mch_startjmp();
975 * if (SETJMP(lc_jump_env) != 0)
976 * {
977 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100978 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 * }
980 * else
981 * {
982 * do_the_work;
983 * mch_endjmp();
984 * }
985 * Note: Can't move SETJMP() here, because a function calling setjmp() must
986 * not return before the saved environment is used.
987 * Returns OK for normal return, FAIL when the protected code caused a
988 * problem and LONGJMP() was used.
989 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100990 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100991mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100993# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 lc_active = TRUE;
997}
998
Bram Moolenaar113e1072019-01-20 15:30:40 +0100999 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001000mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
1002 lc_active = FALSE;
1003}
1004
Bram Moolenaar113e1072019-01-20 15:30:40 +01001005 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001006mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007{
1008# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001009 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1010 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 init_signal_stack();
1012# endif
1013}
1014#endif
1015
1016/*
1017 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001018 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001020 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1021 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 */
1023 static RETSIGTYPE
1024deathtrap SIGDEFARG(sigarg)
1025{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001026 static int entered = 0; // count the number of times we got here.
1027 // Note: when memory has been corrupted
1028 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#ifdef SIGHASARG
1030 int i;
1031#endif
1032
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001033#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 /*
1035 * Catch a crash in protected code.
1036 * Restores the environment saved in lc_jump_env, which looks like
1037 * SETJMP() returns 1.
1038 */
1039 if (lc_active)
1040 {
1041# if defined(SIGHASARG)
1042 lc_signal = sigarg;
1043# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001044 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001046 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 }
1048#endif
1049
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001050#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001051# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001052 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1053 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1054 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001055 if (in_mch_delay && sigarg == SIGQUIT)
1056 SIGRETURN;
1057# endif
1058
Bram Moolenaar0f873732019-12-05 20:28:46 +01001059 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1060 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1061 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001062 if (entered == 0
1063 && (0
1064# ifdef SIGHUP
1065 || sigarg == SIGHUP
1066# endif
1067# ifdef SIGQUIT
1068 || sigarg == SIGQUIT
1069# endif
1070# ifdef SIGTERM
1071 || sigarg == SIGTERM
1072# endif
1073# ifdef SIGPWR
1074 || sigarg == SIGPWR
1075# endif
1076# ifdef SIGUSR1
1077 || sigarg == SIGUSR1
1078# endif
1079# ifdef SIGUSR2
1080 || sigarg == SIGUSR2
1081# endif
1082 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001083 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001084 SIGRETURN;
1085#endif
1086
Bram Moolenaar0f873732019-12-05 20:28:46 +01001087 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 ++entered;
1089
Bram Moolenaar0f873732019-12-05 20:28:46 +01001090 // Executing autocommands is likely to use more stack space than we have
1091 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001092 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001095 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 set_vim_var_nr(VV_DYING, (long)entered);
1097#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001098 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001100#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001101 // Since we are now using the signal stack, need to reset the stack
1102 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 get_stack_limit();
1104#endif
1105
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001106#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001107 // This is for opening gdb the moment Vim crashes.
1108 // You need to manually adjust the file name and Vim executable name.
1109 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001110 {
1111# define VI_GDB_FILE "/tmp/vimgdb"
1112# define VIM_NAME "/usr/bin/vim"
1113 FILE *fp = fopen(VI_GDB_FILE, "w");
1114 if (fp)
1115 {
1116 fprintf(fp,
1117 "file %s\n"
1118 "attach %d\n"
1119 "set height 1000\n"
1120 "bt full\n"
1121 , VIM_NAME, getpid());
1122 fclose(fp);
1123 system("xterm -e gdb -x "VI_GDB_FILE);
1124 unlink(VI_GDB_FILE);
1125 }
1126 }
1127#endif
1128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001130 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 for (i = 0; signal_info[i].sig != -1; i++)
1132 if (sigarg == signal_info[i].sig)
1133 break;
1134 deadly_signal = sigarg;
1135#endif
1136
Bram Moolenaar0f873732019-12-05 20:28:46 +01001137 full_screen = FALSE; // don't write message to the GUI, it might be
1138 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 /*
1140 * If something goes wrong after entering here, we may get here again.
1141 * When this happens, give a message and try to exit nicely (resetting the
1142 * terminal mode, etc.)
1143 * When this happens twice, just exit, don't even try to give a message,
1144 * stack may be corrupt or something weird.
1145 * When this still happens again (or memory was corrupted in such a way
1146 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1147 */
1148 if (entered >= 3)
1149 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001150 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 may_core_dump();
1152 if (entered >= 4)
1153 _exit(8);
1154 exit(7);
1155 }
1156 if (entered == 2)
1157 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001158 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001159 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 out_flush();
1161 getout(1);
1162 }
1163
Bram Moolenaar0f873732019-12-05 20:28:46 +01001164 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001166 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 signal_info[i].name);
1168#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001169 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001171
Bram Moolenaar0f873732019-12-05 20:28:46 +01001172 // Preserve files and exit. This sets the really_exiting flag to prevent
1173 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001174 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175
Bram Moolenaar0f873732019-12-05 20:28:46 +01001176 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001177
Bram Moolenaar009b2592004-10-24 19:18:58 +00001178#ifdef NBDEBUG
1179 reset_signals();
1180 may_core_dump();
1181 abort();
1182#endif
1183
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 SIGRETURN;
1185}
1186
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001188 * Invoked after receiving SIGCONT. We don't know what happened while
1189 * sleeping, deal with part of that.
1190 */
1191 static void
1192after_sigcont(void)
1193{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001194 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1195 // again later.
1196 oldtitle_outdated = TRUE;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001197
Bram Moolenaar2e310482018-08-21 13:09:10 +02001198 settmode(TMODE_RAW);
1199 need_check_timestamps = TRUE;
1200 did_check_timestamps = FALSE;
1201}
1202
1203#if defined(SIGCONT)
1204static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001205
1206/*
1207 * With multi-threading, suspending might not work immediately. Catch the
1208 * SIGCONT signal, which will be used as an indication whether the suspending
1209 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001210 *
1211 * On Linux, signal is not always handled immediately either.
1212 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001213 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001214 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001215 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001217static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001218static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
1220/*
1221 * signal handler for SIGCONT
1222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 static RETSIGTYPE
1224sigcont_handler SIGDEFARG(sigarg)
1225{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001226 if (in_mch_suspend)
1227 {
1228 sigcont_received = TRUE;
1229 }
1230 else
1231 {
1232 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1233 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1234 // back to a sane mode. We should redraw, but we can't really do that
1235 // in a signal handler, do a redraw later.
1236 after_sigcont();
1237 redraw_later(CLEAR);
1238 cursor_on_force();
1239 out_flush();
1240 }
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 SIGRETURN;
1243}
1244#endif
1245
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001246#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001247# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001248static void *clip_star_save = NULL;
1249static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001250# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001251
1252/*
1253 * Called when Vim is going to sleep or execute a shell command.
1254 * We can't respond to requests for the X selections. Lose them, otherwise
1255 * other applications will hang. But first copy the text to cut buffer 0.
1256 */
1257 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001258loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001259{
1260 if (clip_star.owned || clip_plus.owned)
1261 {
1262 x11_export_final_selection();
1263 if (clip_star.owned)
1264 clip_lose_selection(&clip_star);
1265 if (clip_plus.owned)
1266 clip_lose_selection(&clip_plus);
1267 if (x11_display != NULL)
1268 XFlush(x11_display);
1269 }
1270}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001271
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001272# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001273/*
1274 * Save clipboard text to restore later.
1275 */
1276 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001277save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001278{
1279 if (clip_star.owned)
1280 clip_star_save = get_register('*', TRUE);
1281 if (clip_plus.owned)
1282 clip_plus_save = get_register('+', TRUE);
1283}
1284
1285/*
1286 * Restore clipboard text if no one own the X selection.
1287 */
1288 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001289restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001290{
1291 if (clip_star_save != NULL)
1292 {
1293 if (!clip_gen_owner_exists(&clip_star))
1294 put_register('*', clip_star_save);
1295 else
1296 free_register(clip_star_save);
1297 clip_star_save = NULL;
1298 }
1299 if (clip_plus_save != NULL)
1300 {
1301 if (!clip_gen_owner_exists(&clip_plus))
1302 put_register('+', clip_plus_save);
1303 else
1304 free_register(clip_plus_save);
1305 clip_plus_save = NULL;
1306 }
1307}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001308# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001309#endif
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311/*
1312 * If the machine has job control, use it to suspend the program,
1313 * otherwise fake it by starting a new shell.
1314 */
1315 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001316mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001318 if (ignore_sigtstp)
1319 return;
1320
Bram Moolenaar041c7102020-05-30 18:14:57 +02001321#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001322 in_mch_suspend = TRUE;
1323
Bram Moolenaar0f873732019-12-05 20:28:46 +01001324 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001326 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327
1328# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001329 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001331# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 sigcont_received = FALSE;
1333# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001334
Bram Moolenaar0f873732019-12-05 20:28:46 +01001335 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001336
1337# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001338 /*
1339 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001340 * immediately, but somehow not all the time, probably because it's handled
1341 * in another thread. Do not call pause() because there would be race
1342 * condition which would hang Vim if signal happened in between the test of
1343 * sigcont_received and the call to pause(). If signal is not yet received,
1344 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1345 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001346 */
1347 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001348 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001349
Bram Moolenaar262735e2009-07-14 10:20:22 +00001350 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001351 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001354 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
Bram Moolenaar2e310482018-08-21 13:09:10 +02001356 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357#else
1358 suspend_shell();
1359#endif
1360}
1361
1362 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001363mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
1365 Columns = 80;
1366 Rows = 24;
1367
1368 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001369
1370#ifdef SIGTSTP
1371 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1372 // that indicates the shell (or program) that launched us does not support
1373 // tty job control and thus we should ignore that signal. If invoked as a
1374 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
1375 ignore_sigtstp = restricted || SIG_IGN == signal(SIGTSTP, SIG_ERR);
1376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001378
Bram Moolenaar56718732006-03-15 22:53:57 +00001379#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001380 mac_conv_init();
1381#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001382#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1383 win_clip_init();
1384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385}
1386
1387 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001388set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
1390#if defined(SIGWINCH)
1391 /*
1392 * WINDOW CHANGE signal is handled with sig_winch().
1393 */
1394 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1395#endif
1396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001398 // See mch_init() for the conditions under which we ignore SIGTSTP.
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001399 // In the GUI default TSTP processing is OK.
1400 // Checking both gui.in_use and gui.starting because gui.in_use is not set
1401 // at this point (set after menus are displayed), but gui.starting is set.
1402 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN
1403# ifdef FEAT_GUI
1404 : gui.in_use || gui.starting ? SIG_DFL
1405# endif
1406 : (RETSIGTYPE (*)())sig_tstp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001408#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 signal(SIGCONT, sigcont_handler);
1410#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001411#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 /*
1413 * We want to ignore breaking of PIPEs.
1414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 signal(SIGPIPE, SIG_IGN);
1416#endif
1417
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001419 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420#endif
1421
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001422#ifdef SIGUSR1
1423 /*
1424 * Call user's handler on SIGUSR1
1425 */
1426 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
1427#endif
1428
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 /*
1430 * Ignore alarm signals (Perl's alarm() generates it).
1431 */
1432#ifdef SIGALRM
1433 signal(SIGALRM, SIG_IGN);
1434#endif
1435
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001436#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 /*
1438 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1439 * work will be lost.
1440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1442#endif
1443
1444 /*
1445 * Arrange for other signals to gracefully shutdown Vim.
1446 */
1447 catch_signals(deathtrap, SIG_ERR);
1448
1449#if defined(FEAT_GUI) && defined(SIGHUP)
1450 /*
1451 * When the GUI is running, ignore the hangup signal.
1452 */
1453 if (gui.in_use)
1454 signal(SIGHUP, SIG_IGN);
1455#endif
1456}
1457
Bram Moolenaardf177f62005-02-22 08:39:57 +00001458#if defined(SIGINT) || defined(PROTO)
1459/*
1460 * Catch CTRL-C (only works while in Cooked mode).
1461 */
1462 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001463catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001464{
1465 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1466}
1467#endif
1468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001470reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
1472 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001473#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001474 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 signal(SIGCONT, SIG_DFL);
1476#endif
1477}
1478
1479 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001480catch_signals(
1481 RETSIGTYPE (*func_deadly)(),
1482 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483{
1484 int i;
1485
1486 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (signal_info[i].deadly)
1489 {
1490#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1491 struct sigaction sa;
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 sa.sa_handler = func_deadly;
1495 sigemptyset(&sa.sa_mask);
1496# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001497 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1498 // thread handling in combination with using the alternate stack:
1499 // pthread library functions try to use the stack pointer to
1500 // identify the current thread, causing a SEGV signal, which
1501 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 sa.sa_flags = 0;
1503# else
1504 sa.sa_flags = SA_ONSTACK;
1505# endif
1506 sigaction(signal_info[i].sig, &sa, NULL);
1507#else
1508# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1509 struct sigvec sv;
1510
Bram Moolenaar0f873732019-12-05 20:28:46 +01001511 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 sv.sv_handler = func_deadly;
1513 sv.sv_mask = 0;
1514 sv.sv_flags = SV_ONSTACK;
1515 sigvec(signal_info[i].sig, &sv, NULL);
1516# else
1517 signal(signal_info[i].sig, func_deadly);
1518# endif
1519#endif
1520 }
1521 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001522 {
1523 // Deal with non-deadly signals.
1524#ifdef SIGTSTP
1525 signal(signal_info[i].sig,
1526 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1527 ? SIG_IGN : func_other);
1528#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001530#endif
1531 }
1532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533}
1534
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001535#ifdef HAVE_SIGPROCMASK
1536 static void
1537block_signals(sigset_t *set)
1538{
1539 sigset_t newset;
1540 int i;
1541
1542 sigemptyset(&newset);
1543
1544 for (i = 0; signal_info[i].sig != -1; i++)
1545 sigaddset(&newset, signal_info[i].sig);
1546
Bram Moolenaar2e310482018-08-21 13:09:10 +02001547# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001548 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001549 sigaddset(&newset, SIGCONT);
1550# endif
1551
1552 sigprocmask(SIG_BLOCK, &newset, set);
1553}
1554
1555 static void
1556unblock_signals(sigset_t *set)
1557{
1558 sigprocmask(SIG_SETMASK, set, NULL);
1559}
1560#endif
1561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001563 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001564 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1565 * return TRUE
1566 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1567 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001568 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001569 * Returns TRUE when Vim should exit.
1570 */
1571 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001572vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001573{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001574 static int got_signal = 0;
1575 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001576
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001577 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001578 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001579 case SIGNAL_BLOCK: blocked = TRUE;
1580 break;
1581
1582 case SIGNAL_UNBLOCK: blocked = FALSE;
1583 if (got_signal != 0)
1584 {
1585 kill(getpid(), got_signal);
1586 got_signal = 0;
1587 }
1588 break;
1589
1590 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001591 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001592 got_signal = sig;
1593#ifdef SIGPWR
1594 if (sig != SIGPWR)
1595#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001596 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001597 break;
1598 }
1599 return FALSE;
1600}
1601
1602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 * Check_win checks whether we have an interactive stdout.
1604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001606mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (isatty(1))
1609 return OK;
1610 return FAIL;
1611}
1612
1613/*
1614 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1615 */
1616 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001617mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
1619 if (isatty(read_cmd_fd))
1620 return TRUE;
1621 return FALSE;
1622}
1623
1624#ifdef FEAT_X11
1625
Bram Moolenaar651fca82021-11-29 20:39:38 +00001626# if defined(ELAPSED_TIMEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628/*
1629 * Give a message about the elapsed time for opening the X window.
1630 */
1631 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001632xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001634 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635}
1636# endif
1637#endif
1638
Bram Moolenaar651fca82021-11-29 20:39:38 +00001639#if defined(FEAT_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640/*
1641 * A few functions shared by X11 title and clipboard code.
1642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
1644static int got_x_error = FALSE;
1645
1646/*
1647 * X Error handler, otherwise X just exits! (very rude) -- webb
1648 */
1649 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001650x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001652 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 STRCAT(IObuff, _("\nVim: Got X error\n"));
1654
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001655 // In the GUI we cannot print a message and continue, because no X calls
1656 // are allowed here (causes my system to hang). Silently continuing seems
1657 // like the best alternative. Do preserve files, in case we crash.
1658 ml_sync_all(FALSE, FALSE);
1659
1660#ifdef FEAT_GUI
1661 if (!gui.in_use)
1662#endif
1663 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaar0f873732019-12-05 20:28:46 +01001665 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666}
1667
1668/*
1669 * Another X Error handler, just used to check for errors.
1670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001672x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673{
1674 got_x_error = TRUE;
1675 return 0;
1676}
1677
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001678/*
1679 * Return TRUE when connection to the X server is desired.
1680 */
1681 static int
1682x_connect_to_server(void)
1683{
1684 // No point in connecting if we are exiting or dying.
1685 if (exiting || v_dying)
1686 return FALSE;
1687
1688#if defined(FEAT_CLIENTSERVER)
1689 if (x_force_connect)
1690 return TRUE;
1691#endif
1692 if (x_no_connect)
1693 return FALSE;
1694
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001695 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001696 if (clip_exclude_prog != NULL)
1697 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001698 // Just in case we get called recursively, return FALSE. This could
1699 // happen if vpeekc() is used while executing the prog and it causes a
1700 // related callback to be invoked.
1701 if (regprog_in_use(clip_exclude_prog))
1702 return FALSE;
1703
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001704 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1705 return FALSE;
1706 }
1707 return TRUE;
1708}
1709
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001711# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712/*
1713 * An X IO Error handler, used to catch error while opening the display.
1714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001716x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001718 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001720# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001721 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723}
1724# endif
1725
1726/*
1727 * An X IO Error handler, used to catch terminal errors.
1728 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001729static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001732x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
1734 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001735 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 x11_window = 0;
1737 x11_display = NULL;
1738 xterm_Shell = (Widget)0;
1739
Bram Moolenaar0f873732019-12-05 20:28:46 +01001740 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001742# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001743 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001744# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001746
1747/*
1748 * If the X11 connection was lost try to restore it.
1749 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001750 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001751 */
1752 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001753may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001754{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001755 // No point in restoring the connecting if we are exiting or dying.
1756 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001757 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001758 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001759
1760# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001761 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001762 if (app_context != (XtAppContext)NULL)
1763 {
1764 XtDestroyApplicationContext(app_context);
1765 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001766 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001767 }
1768# endif
1769
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001770 setup_term_clip();
1771 get_x11_title(FALSE);
1772 }
1773}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001774
1775 void
1776ex_xrestore(exarg_T *eap)
1777{
1778 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1779 {
1780 if (xterm_display_allocated)
1781 vim_free(xterm_display);
1782 xterm_display = (char *)vim_strsave(eap->arg);
1783 xterm_display_allocated = TRUE;
1784 }
1785 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001786 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001787
1788 clear_xterm_clip();
1789 x11_window = 0;
1790 xterm_dpy_retry_count = 5; // Try reconnecting five times
1791 may_restore_clipboard();
1792}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#endif
1794
1795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 * Test if "dpy" and x11_window are valid by getting the window title.
1797 * I don't actually want it yet, so there may be a simpler call to use, but
1798 * this will cause the error handler x_error_check() to be called if anything
1799 * is wrong, such as the window pointer being invalid (as can happen when the
1800 * user changes his DISPLAY, but not his WINDOWID) -- webb
1801 */
1802 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001803test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804{
1805 int (*old_handler)();
1806 XTextProperty text_prop;
1807
1808 old_handler = XSetErrorHandler(x_error_check);
1809 got_x_error = FALSE;
1810 if (XGetWMName(dpy, x11_window, &text_prop))
1811 XFree((void *)text_prop.value);
1812 XSync(dpy, False);
1813 (void)XSetErrorHandler(old_handler);
1814
1815 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001816 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 return (got_x_error ? FAIL : OK);
1819}
1820#endif
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
1823#ifdef FEAT_X11
1824
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001825static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
1827/*
1828 * try to get x11 window and display
1829 *
1830 * return FAIL for failure, OK otherwise
1831 */
1832 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001833get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
1835 char *winid;
1836 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001837#define XD_NONE 0 // x11_display not set here
1838#define XD_HERE 1 // x11_display opened here
1839#define XD_GUI 2 // x11_display used from gui.dpy
1840#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 static int x11_display_from = XD_NONE;
1842 static int did_set_error_handler = FALSE;
1843
1844 if (!did_set_error_handler)
1845 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001846 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 (void)XSetErrorHandler(x_error_handler);
1848 did_set_error_handler = TRUE;
1849 }
1850
Bram Moolenaar9372a112005-12-06 19:59:18 +00001851#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if (gui.in_use)
1853 {
1854 /*
1855 * If the X11 display was opened here before, for the window where Vim
1856 * was started, close that one now to avoid a memory leak.
1857 */
1858 if (x11_display_from == XD_HERE && x11_display != NULL)
1859 {
1860 XCloseDisplay(x11_display);
1861 x11_display_from = XD_NONE;
1862 }
1863 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1864 {
1865 x11_display_from = XD_GUI;
1866 return OK;
1867 }
1868 x11_display = NULL;
1869 return FAIL;
1870 }
1871 else if (x11_display_from == XD_GUI)
1872 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001873 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 x11_window = 0;
1875 x11_display = NULL;
1876 x11_display_from = XD_NONE;
1877 }
1878#endif
1879
Bram Moolenaar0f873732019-12-05 20:28:46 +01001880 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (!x_connect_to_server())
1882 return FAIL;
1883
1884 /*
1885 * If WINDOWID not set, should try another method to find out
1886 * what the current window number is. The only code I know for
1887 * this is very complicated.
1888 * We assume that zero is invalid for WINDOWID.
1889 */
1890 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1891 x11_window = (Window)atol(winid);
1892
1893#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001894 if (xterm_dpy == x11_display)
1895 // x11_display may have been set to xterm_dpy elsewhere
1896 x11_display_from = XD_XTERM;
1897
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (xterm_dpy != NULL && x11_window != 0)
1899 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001900 // We may have checked it already, but Gnome terminal can move us to
1901 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001902 if (x11_display_from != XD_XTERM)
1903 {
1904 /*
1905 * If the X11 display was opened here before, for the window where
1906 * Vim was started, close that one now to avoid a memory leak.
1907 */
1908 if (x11_display_from == XD_HERE && x11_display != NULL)
1909 XCloseDisplay(x11_display);
1910 x11_display = xterm_dpy;
1911 x11_display_from = XD_XTERM;
1912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (test_x11_window(x11_display) == FAIL)
1914 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001915 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 x11_window = 0;
1917 x11_display = NULL;
1918 x11_display_from = XD_NONE;
1919 return FAIL;
1920 }
1921 return OK;
1922 }
1923#endif
1924
1925 if (x11_window == 0 || x11_display == NULL)
1926 result = -1;
1927
Bram Moolenaar0f873732019-12-05 20:28:46 +01001928 if (result != -1) // Have already been here and set this
1929 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
1931 if (x11_window != 0 && x11_display == NULL)
1932 {
1933#ifdef SET_SIG_ALARM
1934 RETSIGTYPE (*sig_save)();
1935#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001936#ifdef ELAPSED_FUNC
1937 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
1939 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001940 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941#endif
1942
1943#ifdef SET_SIG_ALARM
1944 /*
1945 * Opening the Display may hang if the DISPLAY setting is wrong, or
1946 * the network connection is bad. Set an alarm timer to get out.
1947 */
1948 sig_alarm_called = FALSE;
1949 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1950 (RETSIGTYPE (*)())sig_alarm);
1951 alarm(2);
1952#endif
1953 x11_display = XOpenDisplay(NULL);
1954
1955#ifdef SET_SIG_ALARM
1956 alarm(0);
1957 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1958 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001959 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960#endif
1961 if (x11_display != NULL)
1962 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001963# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001965 {
1966 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001967 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001968 verbose_leave();
1969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970# endif
1971 if (test_x11_window(x11_display) == FAIL)
1972 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001973 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 x11_window = 0;
1975 XCloseDisplay(x11_display);
1976 x11_display = NULL;
1977 }
1978 else
1979 x11_display_from = XD_HERE;
1980 }
1981 }
1982 if (x11_window == 0 || x11_display == NULL)
1983 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001984
1985# ifdef FEAT_EVAL
1986 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1987# endif
1988
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 return (result = OK);
1990}
1991
1992/*
1993 * Determine original x11 Window Title
1994 */
1995 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001996get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001998 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999}
2000
2001/*
2002 * Determine original x11 Window icon
2003 */
2004 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002005get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 int retval = FALSE;
2008
2009 retval = get_x11_thing(FALSE, test_only);
2010
Bram Moolenaar0f873732019-12-05 20:28:46 +01002011 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if (oldicon == NULL && !test_only)
2013 {
2014 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002015 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002017 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
2019
2020 return retval;
2021}
2022
2023 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002024get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002025 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01002026 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027{
2028 XTextProperty text_prop;
2029 int retval = FALSE;
2030 Status status;
2031
2032 if (get_x11_windis() == OK)
2033 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002034 // Get window/icon name if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if (get_title)
2036 status = XGetWMName(x11_display, x11_window, &text_prop);
2037 else
2038 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2039
2040 /*
2041 * If terminal is xterm, then x11_window may be a child window of the
2042 * outer xterm window that actually contains the window/icon name, so
2043 * keep traversing up the tree until a window with a title/icon is
2044 * found.
2045 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002046 // Previously this was only done for xterm and alike. I don't see a
Bram Moolenaar0f873732019-12-05 20:28:46 +01002047 // reason why it would fail for other terminal emulators.
2048 // if (term_is_xterm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 {
2050 Window root;
2051 Window parent;
2052 Window win = x11_window;
2053 Window *children;
2054 unsigned int num_children;
2055
2056 while (!status || text_prop.value == NULL)
2057 {
2058 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2059 &num_children))
2060 break;
2061 if (children)
2062 XFree((void *)children);
2063 if (parent == root || parent == 0)
2064 break;
2065
2066 win = parent;
2067 if (get_title)
2068 status = XGetWMName(x11_display, win, &text_prop);
2069 else
2070 status = XGetWMIconName(x11_display, win, &text_prop);
2071 }
2072 }
2073 if (status && text_prop.value != NULL)
2074 {
2075 retval = TRUE;
2076 if (!test_only)
2077 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002078 if (get_title)
2079 vim_free(oldtitle);
2080 else
2081 vim_free(oldicon);
Bram Moolenaara12a1612019-01-24 16:39:02 +01002082 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (get_title)
2085 oldtitle = vim_strsave((char_u *)text_prop.value);
2086 else
2087 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
2089 else
2090 {
2091 char **cl;
2092 Status transform_status;
2093 int n = 0;
2094
2095 transform_status = XmbTextPropertyToTextList(x11_display,
2096 &text_prop,
2097 &cl, &n);
2098 if (transform_status >= Success && n > 0 && cl[0])
2099 {
2100 if (get_title)
2101 oldtitle = vim_strsave((char_u *) cl[0]);
2102 else
2103 oldicon = vim_strsave((char_u *) cl[0]);
2104 XFreeStringList(cl);
2105 }
2106 else
2107 {
2108 if (get_title)
2109 oldtitle = vim_strsave((char_u *)text_prop.value);
2110 else
2111 oldicon = vim_strsave((char_u *)text_prop.value);
2112 }
2113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 }
2115 XFree((void *)text_prop.value);
2116 }
2117 }
2118 return retval;
2119}
2120
Bram Moolenaar0f873732019-12-05 20:28:46 +01002121// Xutf8 functions are not available on older systems. Note that on some
2122// systems X_HAVE_UTF8_STRING may be defined in a header file but
2123// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2124// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002125#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002126# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127# define USE_UTF8_STRING
2128# endif
2129#endif
2130
2131/*
2132 * Set x11 Window Title
2133 *
2134 * get_x11_windis() must be called before this and have returned OK
2135 */
2136 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002137set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002139 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2140 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2141 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142#ifdef USE_UTF8_STRING
2143 if (enc_utf8)
2144 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2145 NULL, NULL, 0, NULL, NULL, NULL);
2146 else
2147#endif
2148 {
2149#if XtSpecificationRelease >= 4
2150# ifdef FEAT_XFONTSET
2151 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2152 NULL, NULL, 0, NULL, NULL, NULL);
2153# else
2154 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002155 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
Bram Moolenaar0f873732019-12-05 20:28:46 +01002157 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002158 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 XSetWMProperties(x11_display, x11_window, &text_prop,
2160 NULL, NULL, 0, NULL, NULL, NULL);
2161# endif
2162#else
2163 XStoreName(x11_display, x11_window, (char *)title);
2164#endif
2165 }
2166 XFlush(x11_display);
2167}
2168
2169/*
2170 * Set x11 Window icon
2171 *
2172 * get_x11_windis() must be called before this and have returned OK
2173 */
2174 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002175set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002177 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178#ifdef USE_UTF8_STRING
2179 if (enc_utf8)
2180 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2181 NULL, 0, NULL, NULL, NULL);
2182 else
2183#endif
2184 {
2185#if XtSpecificationRelease >= 4
2186# ifdef FEAT_XFONTSET
2187 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2188 NULL, 0, NULL, NULL, NULL);
2189# else
2190 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002191 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002193 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2195 NULL, 0, NULL, NULL, NULL);
2196# endif
2197#else
2198 XSetIconName(x11_display, x11_window, (char *)icon);
2199#endif
2200 }
2201 XFlush(x11_display);
2202}
2203
Bram Moolenaar0f873732019-12-05 20:28:46 +01002204#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002207get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
2209 return FALSE;
2210}
2211
2212 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002213get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214{
2215 if (!test_only)
2216 {
2217 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002218 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002220 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 }
2222 return FALSE;
2223}
2224
Bram Moolenaar0f873732019-12-05 20:28:46 +01002225#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002228mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229{
2230 return get_x11_title(TRUE);
2231}
2232
2233 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002234mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
2236 return get_x11_icon(TRUE);
2237}
2238
2239/*
2240 * Set the window title and icon.
2241 */
2242 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002243mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244{
2245 int type = 0;
2246 static int recursive = 0;
2247
Bram Moolenaar0f873732019-12-05 20:28:46 +01002248 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002250 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 return;
2252
Bram Moolenaar0f873732019-12-05 20:28:46 +01002253 // When one of the X11 functions causes a deadly signal, we get here again
2254 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if (recursive)
2256 return;
2257 ++recursive;
2258
2259 /*
2260 * if the window ID and the display is known, we may use X11 calls
2261 */
2262#ifdef FEAT_X11
2263 if (get_x11_windis() == OK)
2264 type = 1;
2265#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002266# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002267 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 if (gui.in_use)
2269 type = 1;
2270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271#endif
2272
2273 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002274 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 * than x11 calls, because the x11 calls don't always work
2276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 if ((type || *T_TS != NUL) && title != NULL)
2278 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002279 if (oldtitle_outdated)
2280 {
2281 oldtitle_outdated = FALSE;
2282 VIM_CLEAR(oldtitle);
2283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 if (oldtitle == NULL
2285#ifdef FEAT_GUI
2286 && !gui.in_use
2287#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002288 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 (void)get_x11_title(FALSE);
2290
Bram Moolenaar0f873732019-12-05 20:28:46 +01002291 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 term_settitle(title);
2293#ifdef FEAT_X11
2294 else
2295# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002296 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002298 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002300#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002301 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 else
2303 gui_mch_settitle(title, icon);
2304#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002305 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 }
2307
2308 if ((type || *T_CIS != NUL) && icon != NULL)
2309 {
2310 if (oldicon == NULL
2311#ifdef FEAT_GUI
2312 && !gui.in_use
2313#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002314 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 get_x11_icon(FALSE);
2316
2317 if (*T_CIS != NUL)
2318 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002319 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002321 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 out_flush();
2323 }
2324#ifdef FEAT_X11
2325 else
2326# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002327 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002329 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#endif
2331 did_set_icon = TRUE;
2332 }
2333 --recursive;
2334}
2335
2336/*
2337 * Restore the window/icon title.
2338 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002339 * SAVE_RESTORE_TITLE only restore title
2340 * SAVE_RESTORE_ICON only restore icon
2341 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 */
2343 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002344mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345{
Bram Moolenaardac13472019-09-16 21:06:21 +02002346 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002347
Bram Moolenaar0f873732019-12-05 20:28:46 +01002348 // only restore the title or icon when it has been set
Bram Moolenaardac13472019-09-16 21:06:21 +02002349 mch_settitle(((which & SAVE_RESTORE_TITLE) && unix_did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002351 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2352
Bram Moolenaare5c83282019-05-03 23:15:37 +02002353 if (do_push_pop)
2354 {
2355 // pop and push from/to the stack
2356 term_pop_title(which);
2357 term_push_title(which);
2358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359}
2360
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362/*
2363 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002364 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 */
2366 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002367vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368{
2369 if (name == NULL)
2370 return FALSE;
2371 return (STRNICMP(name, "xterm", 5) == 0
2372 || STRNICMP(name, "nxterm", 6) == 0
2373 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002374 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002376 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 || STRCMP(name, "builtin_xterm") == 0);
2378}
2379
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002380#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2381/*
2382 * Return TRUE if "name" appears to be that of a terminal
2383 * known to support the xterm-style mouse protocol.
2384 * Relies on term_is_xterm having been set to its correct value.
2385 */
2386 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002387use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002388{
2389 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002390 && (term_is_xterm
2391 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002392 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002393 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002394 || STRICMP(name, "st") == 0
2395 || STRNICMP(name, "st-", 3) == 0
2396 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002397}
2398#endif
2399
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400/*
2401 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2402 * Return 1 for "xterm".
2403 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002404 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002405 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 */
2407 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002408use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002410 if (ttym_flags == TTYM_SGR)
2411 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002412 if (ttym_flags == TTYM_URXVT)
2413 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (ttym_flags == TTYM_XTERM2)
2415 return 2;
2416 if (ttym_flags == TTYM_XTERM)
2417 return 1;
2418 return 0;
2419}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
2421 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002422vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423{
2424 if (name == NULL)
2425 return FALSE;
2426 return (STRNICMP(name, "iris-ansi", 9) == 0
2427 || STRCMP(name, "builtin_iris-ansi") == 0);
2428}
2429
2430 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002431vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432{
2433 if (name == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002434 return FALSE; // actually all ANSI comp. terminals should be here
2435 // catch VT100 - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002436 return ((STRNICMP(name, "vt", 2) == 0
2437 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 || STRCMP(name, "builtin_vt320") == 0);
2439}
2440
2441/*
2442 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2443 * This should include all windowed terminal emulators.
2444 */
2445 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002446vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447{
2448 if (name == NULL)
2449 return FALSE;
2450 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2451 return TRUE;
2452 return ( STRNICMP(name, "hpterm", 6) == 0
2453 || STRNICMP(name, "sun-cmd", 7) == 0
2454 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002455 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 || STRNICMP(name, "dtterm", 6) == 0);
2457}
2458
2459/*
2460 * Insert user name in s[len].
2461 * Return OK if a name found.
2462 */
2463 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002464mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
2466#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002467 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 return OK;
2469#else
2470 return mch_get_uname(getuid(), s, len);
2471#endif
2472}
2473
2474/*
2475 * Insert user name for "uid" in s[len].
2476 * Return OK if a name found.
2477 */
2478 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002479mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480{
2481#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2482 struct passwd *pw;
2483
2484 if ((pw = getpwuid(uid)) != NULL
2485 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2486 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002487 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 return OK;
2489 }
2490#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002491 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2492 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493}
2494
2495/*
2496 * Insert host name is s[len].
2497 */
2498
2499#ifdef HAVE_SYS_UTSNAME_H
2500 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002501mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
2503 struct utsname vutsname;
2504
2505 if (uname(&vutsname) < 0)
2506 *s = NUL;
2507 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002508 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002510#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
2512# ifdef HAVE_SYS_SYSTEMINFO_H
2513# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2514# endif
2515
2516 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002517mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
2519# ifdef VAXC
2520 vaxc$gethostname((char *)s, len);
2521# else
2522 gethostname((char *)s, len);
2523# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002524 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002526#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
2528/*
2529 * return process ID
2530 */
2531 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002532mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533{
2534 return (long)getpid();
2535}
2536
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002537/*
2538 * return TRUE if process "pid" is still running
2539 */
2540 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002541mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002542{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002543 // If there is no error the process must be running.
2544 if (kill(pid, 0) == 0)
2545 return TRUE;
2546#ifdef ESRCH
2547 // If the error is ESRCH then the process is not running.
2548 if (errno == ESRCH)
2549 return FALSE;
2550#endif
2551 // If the process is running and owned by another user we get EPERM. With
2552 // other errors the process might be running, assuming it is then.
2553 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002554}
2555
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002558strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559{
2560 extern int sys_nerr;
2561 extern char *sys_errlist[];
2562 static char er[20];
2563
2564 if (err > 0 && err < sys_nerr)
2565 return (sys_errlist[err]);
2566 sprintf(er, "Error %d", err);
2567 return er;
2568}
2569#endif
2570
2571/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002572 * Get name of current directory into buffer "buf" of length "len" bytes.
2573 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 * Return OK for success, FAIL for failure.
2575 */
2576 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002577mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578{
2579#if defined(USE_GETCWD)
2580 if (getcwd((char *)buf, len) == NULL)
2581 {
2582 STRCPY(buf, strerror(errno));
2583 return FAIL;
2584 }
2585 return OK;
2586#else
2587 return (getwd((char *)buf) != NULL ? OK : FAIL);
2588#endif
2589}
2590
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002592 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 *
2594 * return FAIL for failure, OK for success
2595 */
2596 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002597mch_FullName(
2598 char_u *fname,
2599 char_u *buf,
2600 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002601 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602{
2603 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002604#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002606 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 char_u olddir[MAXPATHL];
2609 char_u *p;
2610 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002611#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002612 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2613 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002614#endif
2615
Bram Moolenaar38323e42007-03-06 19:22:53 +00002616#ifdef VMS
2617 fname = vms_fixfilename(fname);
2618#endif
2619
Bram Moolenaara2442432007-04-26 14:26:37 +00002620#ifdef __CYGWIN__
2621 /*
2622 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2623 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002624# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002625 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2626 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002627 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2628 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002629# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002630 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002631# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002632 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002633#endif
2634
Bram Moolenaar0f873732019-12-05 20:28:46 +01002635 // Expand it if forced or not an absolute path.
2636 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002637 if ((force || !mch_isFullName(fname))
2638 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 {
2640 /*
2641 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002642 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 * This will get the correct path name with "../" things.
2644 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002645 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002647 if (STRCMP(p, "/..") == 0)
2648 // for "/path/dir/.." include the "/.."
2649 p += 3;
2650
Bram Moolenaar38323e42007-03-06 19:22:53 +00002651#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 /*
2653 * Use fchdir() if possible, it's said to be faster and more
2654 * reliable. But on SunOS 4 it might not work. Check this by
2655 * doing a fchdir() right now.
2656 */
2657 if (!dont_fchdir)
2658 {
2659 fd = open(".", O_RDONLY | O_EXTRA, 0);
2660 if (fd >= 0 && fchdir(fd) < 0)
2661 {
2662 close(fd);
2663 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002664 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
2666 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668
Bram Moolenaar0f873732019-12-05 20:28:46 +01002669 // Only change directory when we are sure we can return to where
2670 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002672#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 (mch_dirname(olddir, MAXPATHL) == FAIL
2676 || mch_chdir((char *)olddir) != 0))
2677 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002678 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 retval = FAIL;
2680 }
2681 else
2682 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002683 // The directory is copied into buf[], to be able to remove
2684 // the file name without changing it (could be a string in
2685 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (p - fname >= len)
2687 retval = FAIL;
2688 else
2689 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002690 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002692 {
2693 // Path does not exist (yet). For a full path fail,
2694 // will use the path as-is. For a relative path use
2695 // the current directory and append the file name.
2696 if (mch_isFullName(fname))
2697 retval = FAIL;
2698 else
2699 p = NULL;
2700 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002701 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002703 else
2704 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 *buf = NUL;
2706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
2708 }
2709 if (mch_dirname(buf, len) == FAIL)
2710 {
2711 retval = FAIL;
2712 *buf = NUL;
2713 }
2714 if (p != NULL)
2715 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002716#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 if (fd >= 0)
2718 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002719 if (p_verbose >= 5)
2720 {
2721 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002722 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002723 verbose_leave();
2724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 }
2727 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 l = mch_chdir((char *)olddir);
2730 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002731 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
itchyny051a40c2021-10-20 10:00:05 +01002733#ifdef HAVE_FCHDIR
2734 if (fd >= 0)
2735 close(fd);
2736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
2738 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002739 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002740 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002741#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002742 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002744 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002747
Bram Moolenaar0f873732019-12-05 20:28:46 +01002748 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002749 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 return FAIL;
2751
Bram Moolenaar0f873732019-12-05 20:28:46 +01002752 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 if (STRCMP(fname, ".") != 0)
2754 STRCAT(buf, fname);
2755
2756 return OK;
2757}
2758
2759/*
2760 * Return TRUE if "fname" does not depend on the current directory.
2761 */
2762 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002763mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002765#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 return ( fname[0] == '/' || fname[0] == '.' ||
2767 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2768 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2769 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002770#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772#endif
2773}
2774
Bram Moolenaar24552be2005-12-10 20:17:30 +00002775#if defined(USE_FNAME_CASE) || defined(PROTO)
2776/*
2777 * Set the case of the file name, if it already exists. This will cause the
2778 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002779 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002780 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002781 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002782fname_case(
2783 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002784 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002785{
2786 struct stat st;
2787 char_u *slash, *tail;
2788 DIR *dirp;
2789 struct dirent *dp;
2790
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002791 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002792 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002793 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002794 slash = vim_strrchr(name, '/');
2795 if (slash == NULL)
2796 {
2797 dirp = opendir(".");
2798 tail = name;
2799 }
2800 else
2801 {
2802 *slash = NUL;
2803 dirp = opendir((char *)name);
2804 *slash = '/';
2805 tail = slash + 1;
2806 }
2807
2808 if (dirp != NULL)
2809 {
2810 while ((dp = readdir(dirp)) != NULL)
2811 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002812 // Only accept names that differ in case and are the same byte
2813 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002814 if (STRICMP(tail, dp->d_name) == 0
2815 && STRLEN(tail) == STRLEN(dp->d_name))
2816 {
2817 char_u newname[MAXPATHL + 1];
2818 struct stat st2;
2819
Bram Moolenaar0f873732019-12-05 20:28:46 +01002820 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002821 vim_strncpy(newname, name, MAXPATHL);
2822 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2823 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002824 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002825 && st.st_ino == st2.st_ino
2826 && st.st_dev == st2.st_dev)
2827 {
2828 STRCPY(tail, dp->d_name);
2829 break;
2830 }
2831 }
2832 }
2833
2834 closedir(dirp);
2835 }
2836 }
2837}
2838#endif
2839
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840/*
2841 * Get file permissions for 'name'.
2842 * Returns -1 when it doesn't exist.
2843 */
2844 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002845mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846{
2847 struct stat statb;
2848
Bram Moolenaar0f873732019-12-05 20:28:46 +01002849 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850#ifdef VMS
2851 if (stat((char *)vms_fixfilename(name), &statb))
2852#else
2853 if (stat((char *)name, &statb))
2854#endif
2855 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002856#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002857 // The top bit makes the value negative, which means the file doesn't
2858 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002859 return statb.st_mode & ~S_ADDACE;
2860#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863}
2864
2865/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002866 * Set file permission for "name" to "perm".
2867 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 */
2869 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002870mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871{
2872 return (chmod((char *)
2873#ifdef VMS
2874 vms_fixfilename(name),
2875#else
2876 name,
2877#endif
2878 (mode_t)perm) == 0 ? OK : FAIL);
2879}
2880
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002881#if defined(HAVE_FCHMOD) || defined(PROTO)
2882/*
2883 * Set file permission for open file "fd" to "perm".
2884 * Return FAIL for failure, OK otherwise.
2885 */
2886 int
2887mch_fsetperm(int fd, long perm)
2888{
2889 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2890}
2891#endif
2892
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893#if defined(HAVE_ACL) || defined(PROTO)
2894# ifdef HAVE_SYS_ACL_H
2895# include <sys/acl.h>
2896# endif
2897# ifdef HAVE_SYS_ACCESS_H
2898# include <sys/access.h>
2899# endif
2900
2901# ifdef HAVE_SOLARIS_ACL
2902typedef struct vim_acl_solaris_T {
2903 int acl_cnt;
2904 aclent_t *acl_entry;
2905} vim_acl_solaris_T;
2906# endif
2907
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002908#if defined(HAVE_SELINUX) || defined(PROTO)
2909/*
2910 * Copy security info from "from_file" to "to_file".
2911 */
2912 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002913mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002914{
2915 if (from_file == NULL)
2916 return;
2917
2918 if (selinux_enabled == -1)
2919 selinux_enabled = is_selinux_enabled();
2920
2921 if (selinux_enabled > 0)
2922 {
Bram Moolenaar89560232020-10-09 23:04:47 +02002923 // Use "char *" instead of "security_context_t" to avoid a deprecation
2924 // warning.
2925 char *from_context = NULL;
2926 char *to_context = NULL;
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002927
2928 if (getfilecon((char *)from_file, &from_context) < 0)
2929 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002930 // If the filesystem doesn't support extended attributes,
2931 // the original had no special security context and the
2932 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002933 if (errno == EOPNOTSUPP)
2934 return;
2935
Bram Moolenaar32526b32019-01-19 17:43:09 +01002936 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002937 msg_outtrans(from_file);
2938 msg_putchar('\n');
2939 return;
2940 }
2941 if (getfilecon((char *)to_file, &to_context) < 0)
2942 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002943 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002944 msg_outtrans(to_file);
2945 msg_putchar('\n');
2946 freecon (from_context);
2947 return ;
2948 }
2949 if (strcmp(from_context, to_context) != 0)
2950 {
2951 if (setfilecon((char *)to_file, from_context) < 0)
2952 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002953 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002954 msg_outtrans(to_file);
2955 msg_putchar('\n');
2956 }
2957 }
2958 freecon(to_context);
2959 freecon(from_context);
2960 }
2961}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002962#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002963
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002964#if defined(HAVE_SMACK) && !defined(PROTO)
2965/*
2966 * Copy security info from "from_file" to "to_file".
2967 */
2968 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002969mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002970{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002971 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002972 {
2973 XATTR_NAME_SMACK,
2974 XATTR_NAME_SMACKEXEC,
2975 XATTR_NAME_SMACKMMAP
2976 };
2977
2978 char buffer[SMACK_LABEL_LEN];
2979 const char *name;
2980 int index;
2981 int ret;
2982 ssize_t size;
2983
2984 if (from_file == NULL)
2985 return;
2986
2987 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2988 / sizeof(smack_copied_attributes)[0]) ; index++)
2989 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002990 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002991 name = smack_copied_attributes[index];
2992
Bram Moolenaar0f873732019-12-05 20:28:46 +01002993 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002994 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2995 if (size >= 0)
2996 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002997 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002998 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2999 if (ret < 0)
3000 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003001 vim_snprintf((char *)IObuff, IOSIZE,
3002 _("Could not set security context %s for %s"),
3003 name, to_file);
3004 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003005 msg_putchar('\n');
3006 }
3007 }
3008 else
3009 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003010 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003011 switch (errno)
3012 {
3013 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003014 // extended attributes aren't supported or enabled
3015 // should a message be echoed? not sure...
3016 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003017
3018 case ERANGE:
3019 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003020 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003021 vim_snprintf((char *)IObuff, IOSIZE,
3022 _("Could not get security context %s for %s. Removing it!"),
3023 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003024 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003025 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003026 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003027
3028 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003029 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003030 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003031 // Silently ignore errors, apparently this happens when
3032 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003033 break;
3034 }
3035 }
3036 }
3037}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003038#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003039
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040/*
3041 * Return a pointer to the ACL of file "fname" in allocated memory.
3042 * Return NULL if the ACL is not available for whatever reason.
3043 */
3044 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003045mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
3047 vim_acl_T ret = NULL;
3048#ifdef HAVE_POSIX_ACL
3049 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3050#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003051#ifdef HAVE_SOLARIS_ZFS_ACL
3052 acl_t *aclent;
3053
3054 if (acl_get((char *)fname, 0, &aclent) < 0)
3055 return NULL;
3056 ret = (vim_acl_T)aclent;
3057#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058#ifdef HAVE_SOLARIS_ACL
3059 vim_acl_solaris_T *aclent;
3060
3061 aclent = malloc(sizeof(vim_acl_solaris_T));
3062 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3063 {
3064 free(aclent);
3065 return NULL;
3066 }
3067 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3068 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3069 {
3070 free(aclent->acl_entry);
3071 free(aclent);
3072 return NULL;
3073 }
3074 ret = (vim_acl_T)aclent;
3075#else
3076#if defined(HAVE_AIX_ACL)
3077 int aclsize;
3078 struct acl *aclent;
3079
3080 aclsize = sizeof(struct acl);
3081 aclent = malloc(aclsize);
3082 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3083 {
3084 if (errno == ENOSPC)
3085 {
3086 aclsize = aclent->acl_len;
3087 aclent = realloc(aclent, aclsize);
3088 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3089 {
3090 free(aclent);
3091 return NULL;
3092 }
3093 }
3094 else
3095 {
3096 free(aclent);
3097 return NULL;
3098 }
3099 }
3100 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003101#endif // HAVE_AIX_ACL
3102#endif // HAVE_SOLARIS_ACL
3103#endif // HAVE_SOLARIS_ZFS_ACL
3104#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 return ret;
3106}
3107
3108/*
3109 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3110 */
3111 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003112mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
3114 if (aclent == NULL)
3115 return;
3116#ifdef HAVE_POSIX_ACL
3117 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3118#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003119#ifdef HAVE_SOLARIS_ZFS_ACL
3120 acl_set((char *)fname, (acl_t *)aclent);
3121#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122#ifdef HAVE_SOLARIS_ACL
3123 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3124 ((vim_acl_solaris_T *)aclent)->acl_entry);
3125#else
3126#ifdef HAVE_AIX_ACL
3127 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003128#endif // HAVE_AIX_ACL
3129#endif // HAVE_SOLARIS_ACL
3130#endif // HAVE_SOLARIS_ZFS_ACL
3131#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132}
3133
3134 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003135mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136{
3137 if (aclent == NULL)
3138 return;
3139#ifdef HAVE_POSIX_ACL
3140 acl_free((acl_t)aclent);
3141#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003142#ifdef HAVE_SOLARIS_ZFS_ACL
3143 acl_free((acl_t *)aclent);
3144#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145#ifdef HAVE_SOLARIS_ACL
3146 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3147 free(aclent);
3148#else
3149#ifdef HAVE_AIX_ACL
3150 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003151#endif // HAVE_AIX_ACL
3152#endif // HAVE_SOLARIS_ACL
3153#endif // HAVE_SOLARIS_ZFS_ACL
3154#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155}
3156#endif
3157
3158/*
3159 * Set hidden flag for "name".
3160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003162mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003164 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165}
3166
3167/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003168 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 * return FALSE if "name" is not a directory
3170 * return FALSE for error
3171 */
3172 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003173mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174{
3175 struct stat statb;
3176
Bram Moolenaar0f873732019-12-05 20:28:46 +01003177 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 return FALSE;
3179 if (stat((char *)name, &statb))
3180 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182}
3183
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003184/*
3185 * return TRUE if "name" is a directory, NOT a symlink to a directory
3186 * return FALSE if "name" is not a directory
3187 * return FALSE for error
3188 */
3189 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003190mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003191{
3192 struct stat statb;
3193
Bram Moolenaar0f873732019-12-05 20:28:46 +01003194 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003195 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003196 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003197 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003198 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003199}
3200
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201/*
3202 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3203 */
3204 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003205executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206{
3207 struct stat st;
3208
3209 if (stat((char *)name, &st))
3210 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003211#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003212 // Like on Unix system file can have executable rights but not necessarily
3213 // be an executable, but on Unix is not a default for an ordinary file to
3214 // have an executable flag - on VMS it is in most cases.
3215 // Therefore, this check does not have any sense - let keep us to the
3216 // conventions instead:
3217 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003218 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003219 int vms_executable = 0;
3220 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3221 {
3222 if (strstr(vms_tolower((char*)name),".exe") != NULL
3223 || strstr(vms_tolower((char*)name),".com")!= NULL)
3224 vms_executable = 1;
3225 }
3226 return vms_executable;
3227#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230}
3231
3232/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003233 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003234 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 * Return -1 if unknown.
3236 */
3237 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003238mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239{
3240 char_u *buf;
3241 char_u *p, *e;
3242 int retval;
3243
Bram Moolenaar0f873732019-12-05 20:28:46 +01003244 // When "use_path" is false and if it's an absolute or relative path don't
3245 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003246 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003247 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003248 // There must be a path separator, files in the current directory
3249 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003250 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003251 {
3252 if (path != NULL)
3253 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003254 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003255 *path = FullName_save(name, TRUE);
3256 else
3257 *path = vim_strsave(name);
3258 }
3259 return TRUE;
3260 }
3261 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263
3264 p = (char_u *)getenv("PATH");
3265 if (p == NULL || *p == NUL)
3266 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003267 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 if (buf == NULL)
3269 return -1;
3270
3271 /*
3272 * Walk through all entries in $PATH to check if "name" exists there and
3273 * is an executable file.
3274 */
3275 for (;;)
3276 {
3277 e = (char_u *)strchr((char *)p, ':');
3278 if (e == NULL)
3279 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003280 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 STRCPY(buf, "./");
3282 else
3283 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003284 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 add_pathsep(buf);
3286 }
3287 STRCAT(buf, name);
3288 retval = executable_file(buf);
3289 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003290 {
3291 if (path != NULL)
3292 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003293 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003294 *path = FullName_save(buf, TRUE);
3295 else
3296 *path = vim_strsave(buf);
3297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300
3301 if (*e != ':')
3302 break;
3303 p = e + 1;
3304 }
3305
3306 vim_free(buf);
3307 return retval;
3308}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309
3310/*
3311 * Check what "name" is:
3312 * NODE_NORMAL: file or directory (or doesn't exist)
3313 * NODE_WRITABLE: writable device, socket, fifo, etc.
3314 * NODE_OTHER: non-writable things
3315 */
3316 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003317mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
3319 struct stat st;
3320
3321 if (stat((char *)name, &st))
3322 return NODE_NORMAL;
3323 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3324 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003325 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003327 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 return NODE_WRITABLE;
3329}
3330
3331 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003332mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334#ifdef HAVE_CHECK_STACK_GROWTH
3335 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 check_stack_growth((char *)&i);
3338
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003339# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 get_stack_limit();
3341# endif
3342
3343#endif
3344
3345 /*
3346 * Setup an alternative stack for signals. Helps to catch signals when
3347 * running out of stack space.
3348 * Use of sigaltstack() is preferred, it's more portable.
3349 * Ignore any errors.
3350 */
3351#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003352 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 init_signal_stack();
3354#endif
3355}
3356
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003357#if defined(EXITFREE) || defined(PROTO)
3358 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003359mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003360{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003361# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3362 if (clip_star.owned)
3363 clip_lose_selection(&clip_star);
3364 if (clip_plus.owned)
3365 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003366# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003367# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003368 if (xterm_Shell != (Widget)0)
3369 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003370# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003371 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003372 if (xterm_dpy != NULL)
3373 XtCloseDisplay(xterm_dpy);
3374 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003375 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003376 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003377# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003378 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003379# endif
3380 }
3381# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003382# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003383# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003384 if (x11_display != NULL
3385# ifdef FEAT_XCLIPBOARD
3386 && x11_display != xterm_dpy
3387# endif
3388 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003389 XCloseDisplay(x11_display);
3390# endif
3391# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003392 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003393# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003394 vim_free(oldtitle);
3395 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003396}
3397#endif
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399/*
3400 * Output a newline when exiting.
3401 * Make sure the newline goes to the same stream as the text.
3402 */
3403 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003404exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003406 if (silent_mode)
3407 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (newline_on_exit || msg_didout)
3409 {
3410 if (msg_use_printf())
3411 {
3412 if (info_message)
3413 mch_msg("\n");
3414 else
3415 mch_errmsg("\r\n");
3416 }
3417 else
3418 out_char('\n');
3419 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003420 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003422 restore_cterm_colors(); // get original colors back
3423 msg_clr_eos_force(); // clear the rest of the display
3424 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
3426}
3427
Bram Moolenaarb4151682020-05-11 22:13:28 +02003428#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003429# if (defined(__GNUC__) \
3430 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3431 || (defined(__clang__) && (__clang_major__ >= 12))
3432extern void __gcov_dump(void);
3433extern void __gcov_reset(void);
3434# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3435# else
3436extern void __gcov_flush(void);
3437# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003438#endif
3439
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003441mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 exiting = TRUE;
3444
3445#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3446 x11_export_final_selection();
3447#endif
3448
3449#ifdef FEAT_GUI
3450 if (!gui.in_use)
3451#endif
3452 {
3453 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003454 if (!is_not_a_term())
3455 {
3456 // restore xterm title and icon name
3457 mch_restore_title(SAVE_RESTORE_BOTH);
3458 term_pop_title(SAVE_RESTORE_BOTH);
3459 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003460
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 /*
3462 * When t_ti is not empty but it doesn't cause swapping terminal
3463 * pages, need to output a newline when msg_didout is set. But when
3464 * t_ti does swap pages it should not go to the shell page. Do this
3465 * before stoptermcap().
3466 */
3467 if (swapping_screen() && !newline_on_exit)
3468 exit_scroll();
3469
Bram Moolenaar0f873732019-12-05 20:28:46 +01003470 // Stop termcap: May need to check for T_CRV response, which
3471 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 stoptermcap();
3473
3474 /*
3475 * A newline is only required after a message in the alternate screen.
3476 * This is set to TRUE by wait_return().
3477 */
3478 if (!swapping_screen() || newline_on_exit)
3479 exit_scroll();
3480
Bram Moolenaar0f873732019-12-05 20:28:46 +01003481 // Cursor may have been switched off without calling starttermcap()
3482 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (full_screen)
3484 cursor_on();
3485 }
3486 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003487 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003488
3489#ifdef USE_GCOV_FLUSH
3490 // Flush coverage info before possibly being killed by a deadly signal.
3491 __gcov_flush();
3492#endif
3493
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 may_core_dump();
3495#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 gui_exit(r);
3498#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003499
Bram Moolenaar56718732006-03-15 22:53:57 +00003500#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003501 mac_conv_cleanup();
3502#endif
3503
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003505 // A core dump won't be created if the signal handler
3506 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 if (deadly_signal != 0)
3508 return;
3509#endif
3510
Bram Moolenaar009b2592004-10-24 19:18:58 +00003511#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003512 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003513#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003514
3515#ifdef EXITFREE
3516 free_all_mem();
3517#endif
3518
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 exit(r);
3520}
3521
3522 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003523may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524{
3525 if (deadly_signal != 0)
3526 {
3527 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003528 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
3530}
3531
3532#ifndef VMS
3533
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003534/*
3535 * Get the file descriptor to use for tty operations.
3536 */
3537 static int
3538get_tty_fd(int fd)
3539{
3540 int tty_fd = fd;
3541
3542#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3543 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3544 // "fd" when it is a master device.
3545 if (mch_isatty(fd) > 1)
3546 {
3547 char *name;
3548
3549 name = ptsname(fd);
3550 if (name == NULL)
3551 return -1;
3552
3553 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3554 if (tty_fd < 0)
3555 return -1;
3556 }
3557#endif
3558 return tty_fd;
3559}
3560
3561 static int
3562mch_tcgetattr(int fd, void *term)
3563{
3564 int tty_fd;
3565 int retval = -1;
3566
3567 tty_fd = get_tty_fd(fd);
3568 if (tty_fd >= 0)
3569 {
3570#ifdef NEW_TTY_SYSTEM
3571# ifdef HAVE_TERMIOS_H
3572 retval = tcgetattr(tty_fd, (struct termios *)term);
3573# else
3574 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3575# endif
3576#else
3577 // for "old" tty systems
3578 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3579#endif
3580 if (tty_fd != fd)
3581 close(tty_fd);
3582 }
3583 return retval;
3584}
3585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003587mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588{
3589 static int first = TRUE;
3590
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003591#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592# ifdef HAVE_TERMIOS_H
3593 static struct termios told;
3594 struct termios tnew;
3595# else
3596 static struct termio told;
3597 struct termio tnew;
3598# endif
3599
3600 if (first)
3601 {
3602 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003603 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 }
3605
3606 tnew = told;
3607 if (tmode == TMODE_RAW)
3608 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003609 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003610 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3611 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003613# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003614 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615# endif
3616 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003617# ifdef ONLCR
3618 // Don't map NL -> CR NL, we do it ourselves.
3619 // Also disable expanding tabs if possible.
3620# ifdef XTABS
3621 tnew.c_oflag &= ~(ONLCR | XTABS);
3622# else
3623# ifdef TAB3
3624 tnew.c_oflag &= ~(ONLCR | TAB3);
3625# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003627# endif
3628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003630 tnew.c_cc[VMIN] = 1; // return after 1 char
3631 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
3633 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003634 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003635 // Also reset ICANON here, otherwise on Solaris select() won't see
3636 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003637 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003638 tnew.c_cc[VMIN] = 1; // return after 1 char
3639 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642# if defined(HAVE_TERMIOS_H)
3643 {
3644 int n = 10;
3645
Bram Moolenaar0f873732019-12-05 20:28:46 +01003646 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3647 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3649 && errno == EINTR && n > 0)
3650 --n;
3651 }
3652# else
3653 ioctl(read_cmd_fd, TCSETA, &tnew);
3654# endif
3655
3656#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 /*
3658 * for "old" tty systems
3659 */
3660# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003661# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662# endif
3663 static struct sgttyb ttybold;
3664 struct sgttyb ttybnew;
3665
3666 if (first)
3667 {
3668 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003669 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
3671
3672 ttybnew = ttybold;
3673 if (tmode == TMODE_RAW)
3674 {
3675 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3676 ttybnew.sg_flags |= RAW;
3677 }
3678 else if (tmode == TMODE_SLEEP)
3679 ttybnew.sg_flags &= ~(ECHO);
3680 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3681#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003682 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683}
3684
3685/*
3686 * Try to get the code for "t_kb" from the stty setting
3687 *
3688 * Even if termcap claims a backspace key, the user's setting *should*
3689 * prevail. stty knows more about reality than termcap does, and if
3690 * somebody's usual erase key is DEL (which, for most BSD users, it will
3691 * be), they're going to get really annoyed if their erase key starts
3692 * doing forward deletes for no reason. (Eric Fischer)
3693 */
3694 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003695get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003697 ttyinfo_T info;
3698 char_u buf[2];
3699 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003701 if (get_tty_info(read_cmd_fd, &info) == OK)
3702 {
3703 intr_char = info.interrupt;
3704 buf[0] = info.backspace;
3705 buf[1] = NUL;
3706 add_termcode((char_u *)"kb", buf, FALSE);
3707
Bram Moolenaar0f873732019-12-05 20:28:46 +01003708 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003709 p = find_termcode((char_u *)"kD");
3710 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3711 do_fixdel(NULL);
3712 }
3713}
3714
3715/*
3716 * Obtain the characters that Backspace and Enter produce on "fd".
3717 * Returns OK or FAIL.
3718 */
3719 int
3720get_tty_info(int fd, ttyinfo_T *info)
3721{
3722#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723# ifdef HAVE_TERMIOS_H
3724 struct termios keys;
3725# else
3726 struct termio keys;
3727# endif
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.c_cc[VERASE];
3732 info->interrupt = keys.c_cc[VINTR];
3733 if (keys.c_iflag & ICRNL)
3734 info->enter = NL;
3735 else
3736 info->enter = CAR;
3737 if (keys.c_oflag & ONLCR)
3738 info->nl_does_cr = TRUE;
3739 else
3740 info->nl_does_cr = FALSE;
3741 return OK;
3742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003744 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 struct sgttyb keys;
3746
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003747 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003749 info->backspace = keys.sg_erase;
3750 info->interrupt = keys.sg_kill;
3751 info->enter = CAR;
3752 info->nl_does_cr = TRUE;
3753 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003756 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757}
3758
Bram Moolenaar0f873732019-12-05 20:28:46 +01003759#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003761static int mouse_ison = FALSE;
3762
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763/*
3764 * Set mouse clicks on or off.
3765 */
3766 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003767mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003769#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003770 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 int xterm_mouse_vers;
3773
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003774#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003775 if (!on)
3776 // Make sure not tracing mouse movements. Important when a button-down
3777 // was received but no release yet.
3778 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003779#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003780
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003781 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003782#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003783 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003784#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003785 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003786 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 return;
3788
3789 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003790
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003791#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003792 if (ttym_flags == TTYM_URXVT)
3793 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003794 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003795 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003796 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003797#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003798
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003799 if (ttym_flags == TTYM_SGR)
3800 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003801 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003802 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003803 mouse_ison = on;
3804 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003805
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003806#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003807 if (bevalterm_ison != (p_bevalterm && on))
3808 {
3809 bevalterm_ison = (p_bevalterm && on);
3810 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003811 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003812 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003813 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003814#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003815
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 if (xterm_mouse_vers > 0)
3817 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003818 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 out_str_nf((char_u *)
3820 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003821 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003822#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003823 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003824#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003825 "\033[?1002h")
3826 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003827 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003829 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003830 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003833#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 else if (ttym_flags == TTYM_DEC)
3835 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003836 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003838 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003840 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003844#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 else
3846 {
3847 if (on)
3848 {
3849 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003850 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852 else
3853 {
3854 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003855 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
3857 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003860#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003861 else
3862 {
3863 if (on)
3864 {
3865 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003866 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003867 }
3868 else
3869 {
3870 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003871 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003872 }
3873 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003874#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003875
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003876#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 else
3878 {
3879 if (on)
3880 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003881 // D - Enable Mouse up/down messages
3882 // L - Enable Left Button Reporting
3883 // M - Enable Middle Button Reporting
3884 // R - Enable Right Button Reporting
3885 // K - Enable SHIFT and CTRL key Reporting
3886 // + - Enable Advanced messaging of mouse moves and up/down messages
3887 // Q - Quiet No Ack
3888 // # - Numeric value of mouse pointer required
3889 // 0 = Multiview 2000 cursor, used as standard
3890 // 1 = Windows Arrow
3891 // 2 = Windows I Beam
3892 // 3 = Windows Hour Glass
3893 // 4 = Windows Cross Hair
3894 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003895# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003896 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003897 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003898# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003899 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003900# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003901 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
3903 else
3904 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003905 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003906 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003909#endif
3910#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 else
3912 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003913 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if (on)
3915 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3916 else
3917 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003918 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921}
3922
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003923#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003924/*
3925 * Called when 'balloonevalterm' changed.
3926 */
3927 void
3928mch_bevalterm_changed(void)
3929{
3930 mch_setmouse(mouse_ison);
3931}
3932#endif
3933
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934/*
3935 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3936 */
3937 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003938check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939{
3940# ifdef FEAT_MOUSE_XTERM
3941 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003942# ifdef FEAT_MOUSE_URXVT
3943 && use_xterm_mouse() != 3
3944# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945# ifdef FEAT_GUI
3946 && !gui.in_use
3947# endif
3948 )
3949 {
3950 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003951 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 if (*p_mouse != NUL)
3953 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003954 // force mouse off and maybe on to send possibly new mouse
3955 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 mch_setmouse(FALSE);
3957 setmouse();
3958 }
3959 }
3960 else
3961 del_mouse_termcode(KS_MOUSE);
3962# endif
3963
3964# ifdef FEAT_MOUSE_GPM
3965 if (!use_xterm_mouse()
3966# ifdef FEAT_GUI
3967 && !gui.in_use
3968# endif
3969 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003970 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003971 else
3972 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973# endif
3974
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003975# ifdef FEAT_SYSMOUSE
3976 if (!use_xterm_mouse()
3977# ifdef FEAT_GUI
3978 && !gui.in_use
3979# endif
3980 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003981 set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003982# endif
3983
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003985 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 if (!use_xterm_mouse()
3987# ifdef FEAT_GUI
3988 && !gui.in_use
3989# endif
3990 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003991 set_mouse_termcode(KS_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 else
3993 del_mouse_termcode(KS_JSBTERM_MOUSE);
3994# endif
3995
3996# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003997 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3998 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 if (!use_xterm_mouse()
4000# ifdef FEAT_GUI
4001 && !gui.in_use
4002# endif
4003 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004004 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 else
4006 del_mouse_termcode(KS_NETTERM_MOUSE);
4007# endif
4008
4009# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01004010 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004011 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012# ifdef FEAT_GUI
4013 && !gui.in_use
4014# endif
4015 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004016 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004017 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 else
4019 del_mouse_termcode(KS_DEC_MOUSE);
4020# endif
4021# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004022 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004023 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024# ifdef FEAT_GUI
4025 && !gui.in_use
4026# endif
4027 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004028 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 else
4030 del_mouse_termcode(KS_PTERM_MOUSE);
4031# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004032# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004033 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004034# ifdef FEAT_GUI
4035 && !gui.in_use
4036# endif
4037 )
4038 {
4039 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004040 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004041
4042 if (*p_mouse != NUL)
4043 {
4044 mch_setmouse(FALSE);
4045 setmouse();
4046 }
4047 }
4048 else
4049 del_mouse_termcode(KS_URXVT_MOUSE);
4050# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004051 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004052# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004053 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004054# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004055 )
4056 {
4057 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004058 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004059
4060 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004061 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004062
4063 if (*p_mouse != NUL)
4064 {
4065 mch_setmouse(FALSE);
4066 setmouse();
4067 }
4068 }
4069 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004070 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004071 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004072 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076#ifndef VMS
4077
4078/*
4079 * Try to get the current window size:
4080 * 1. with an ioctl(), most accurate method
4081 * 2. from the environment variables LINES and COLUMNS
4082 * 3. from the termcap
4083 * 4. keep using the old values
4084 * Return OK when size could be determined, FAIL otherwise.
4085 */
4086 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004087mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088{
4089 long rows = 0;
4090 long columns = 0;
4091 char_u *p;
4092
4093 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 * 1. try using an ioctl. It is the most accurate method.
4095 *
4096 * Try using TIOCGWINSZ first, some systems that have it also define
4097 * TIOCGSIZE but don't have a struct ttysize.
4098 */
4099# ifdef TIOCGWINSZ
4100 {
4101 struct winsize ws;
4102 int fd = 1;
4103
Bram Moolenaar0f873732019-12-05 20:28:46 +01004104 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 if (!isatty(fd) && isatty(read_cmd_fd))
4106 fd = read_cmd_fd;
4107 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4108 {
4109 columns = ws.ws_col;
4110 rows = ws.ws_row;
4111 }
4112 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004113# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114# ifdef TIOCGSIZE
4115 {
4116 struct ttysize ts;
4117 int fd = 1;
4118
Bram Moolenaar0f873732019-12-05 20:28:46 +01004119 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 if (!isatty(fd) && isatty(read_cmd_fd))
4121 fd = read_cmd_fd;
4122 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4123 {
4124 columns = ts.ts_cols;
4125 rows = ts.ts_lines;
4126 }
4127 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004128# endif // TIOCGSIZE
4129# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
4131 /*
4132 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004133 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4134 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004136 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 {
4138 if ((p = (char_u *)getenv("LINES")))
4139 rows = atoi((char *)p);
4140 if ((p = (char_u *)getenv("COLUMNS")))
4141 columns = atoi((char *)p);
4142 }
4143
4144#ifdef HAVE_TGETENT
4145 /*
4146 * 3. try reading "co" and "li" entries from termcap
4147 */
4148 if (columns == 0 || rows == 0)
4149 getlinecol(&columns, &rows);
4150#endif
4151
4152 /*
4153 * 4. If everything fails, use the old values
4154 */
4155 if (columns <= 0 || rows <= 0)
4156 return FAIL;
4157
4158 Rows = rows;
4159 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004160 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 return OK;
4162}
4163
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004164#if defined(FEAT_TERMINAL) || defined(PROTO)
4165/*
4166 * Report the windows size "rows" and "cols" to tty "fd".
4167 */
4168 int
4169mch_report_winsize(int fd, int rows, int cols)
4170{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004171 int tty_fd;
4172 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004173
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004174 tty_fd = get_tty_fd(fd);
4175 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004176 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004177# if defined(TIOCSWINSZ)
4178 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004179
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004180 ws.ws_col = cols;
4181 ws.ws_row = rows;
4182 ws.ws_xpixel = cols * 5;
4183 ws.ws_ypixel = rows * 10;
4184 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4185 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4186 retval == 0 ? "success" : "failed");
4187# elif defined(TIOCSSIZE)
4188 struct ttysize ts;
4189
4190 ts.ts_cols = cols;
4191 ts.ts_lines = rows;
4192 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4193 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4194 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004195# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004196 if (tty_fd != fd)
4197 close(tty_fd);
4198 }
4199 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004200}
4201#endif
4202
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203/*
4204 * Try to set the window size to Rows and Columns.
4205 */
4206 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004207mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208{
4209 if (*T_CWS)
4210 {
4211 /*
4212 * NOTE: if you get an error here that term_set_winsize() is
4213 * undefined, check the output of configure. It could probably not
4214 * find a ncurses, termcap or termlib library.
4215 */
4216 term_set_winsize((int)Rows, (int)Columns);
4217 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004218 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
4220}
4221
Bram Moolenaar0f873732019-12-05 20:28:46 +01004222#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
4224/*
4225 * Rows and/or Columns has changed.
4226 */
4227 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004228mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004230 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231}
4232
Bram Moolenaar205b8862011-09-07 15:04:31 +02004233/*
4234 * Wait for process "child" to end.
4235 * Return "child" if it exited properly, <= 0 on error.
4236 */
4237 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004238wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004239{
4240 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004241 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004242
4243 while (wait_pid != child)
4244 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004245 // When compiled with Python threads are probably used, in which case
4246 // wait() sometimes hangs for no obvious reason. Use waitpid()
4247 // instead and loop (like the GUI). Also needed for other interfaces,
4248 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004249# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004250 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004251# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004252 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004253# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004254 if (wait_pid == 0)
4255 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004256 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004257 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004258 if (++delay_msec > 10)
4259 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004260 continue;
4261 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004262 if (wait_pid <= 0
4263# ifdef ECHILD
4264 && errno == ECHILD
4265# endif
4266 )
4267 break;
4268 }
4269 return wait_pid;
4270}
4271
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004272#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004273/*
4274 * Set the environment for a child process.
4275 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004276 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004277set_child_environment(
4278 long rows,
4279 long columns,
4280 char *term,
4281 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004282{
4283# ifdef HAVE_SETENV
4284 char envbuf[50];
4285# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004286 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004287 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004288 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004289 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004290 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004291# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004292 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004293# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004294# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004295 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004296# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004297# endif
4298
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004299# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004300 setenv("TERM", term, 1);
4301 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004302 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004303 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004304 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004305 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004306 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004307 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004308 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004309# ifdef FEAT_TERMINAL
4310 if (is_terminal)
4311 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004312 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004313 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4314 }
4315# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004316# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004317 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004318# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004319# else
4320 /*
4321 * Putenv does not copy the string, it has to remain valid.
4322 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004323 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004324 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004325 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4326 putenv(envbuf_Term);
4327 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004328 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004329 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4330 putenv(envbuf_Lines);
4331 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4332 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004333 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004334 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004335 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004336# ifdef FEAT_TERMINAL
4337 if (is_terminal)
4338 {
4339 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004340 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004341 putenv(envbuf_Version);
4342 }
4343# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004344# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004345 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4346 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4347 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004348# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004349# endif
4350}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004351
4352 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004353set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004354{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004355 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004356}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004357#endif
4358
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004359#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004360/*
4361 * Open a PTY, with FD for the master and slave side.
4362 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004363 * When successful both file descriptors are stored and the allocated pty name
4364 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004365 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004366 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004367open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004368{
4369 char *tty_name;
4370
Bram Moolenaar59386482019-02-10 22:43:46 +01004371 if (name1 != NULL)
4372 *name1 = NULL;
4373 if (name2 != NULL)
4374 *name2 = NULL;
4375
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004376 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004377 if (*pty_master_fd >= 0)
4378 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004379 // Leaving out O_NOCTTY may lead to waitpid() always returning
4380 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4381 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004382#ifdef O_NOCTTY
4383 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4384#else
4385 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4386#endif
4387 if (*pty_slave_fd < 0)
4388 {
4389 close(*pty_master_fd);
4390 *pty_master_fd = -1;
4391 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004392 else
4393 {
4394 if (name1 != NULL)
4395 *name1 = vim_strsave((char_u *)tty_name);
4396 if (name2 != NULL)
4397 *name2 = vim_strsave((char_u *)tty_name);
4398 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004399 }
4400}
4401#endif
4402
Bram Moolenaarfae42832017-08-01 22:24:26 +02004403/*
4404 * Send SIGINT to a child process if "c" is an interrupt character.
4405 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004406 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004407may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4408{
4409# ifdef SIGINT
4410 if (c == Ctrl_C || c == intr_char)
4411 {
4412# ifdef HAVE_SETSID
4413 kill(-pid, SIGINT);
4414# else
4415 kill(0, SIGINT);
4416# endif
4417 if (wpid > 0)
4418 kill(wpid, SIGINT);
4419 }
4420# endif
4421}
4422
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004423#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004424
Bram Moolenaar0f873732019-12-05 20:28:46 +01004425/*
4426 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4427 * pointers, the last one is NULL.
4428 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4429 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004430 int
4431unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004432 char_u *cmd,
4433 char ***argvp,
4434 char_u **sh_tofree,
4435 char_u **shcf_tofree)
4436{
4437 char **argv = NULL;
4438 int argc;
4439
4440 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004441 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004442 return FAIL;
4443
4444 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4445 return FAIL;
4446 *argvp = argv;
4447
4448 if (cmd != NULL)
4449 {
4450 char_u *s;
4451 char_u *p;
4452
4453 if (extra_shell_arg != NULL)
4454 argv[argc++] = (char *)extra_shell_arg;
4455
Bram Moolenaar0f873732019-12-05 20:28:46 +01004456 // Break 'shellcmdflag' into white separated parts. This doesn't
4457 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004458 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004459 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004460 return FAIL;
4461 s = *shcf_tofree;
4462 p = p_shcf;
4463 while (*p != NUL)
4464 {
4465 argv[argc++] = (char *)s;
4466 while (*p && *p != ' ' && *p != TAB)
4467 *s++ = *p++;
4468 *s++ = NUL;
4469 p = skipwhite(p);
4470 }
4471
4472 argv[argc++] = (char *)cmd;
4473 }
4474 argv[argc] = NULL;
4475 return OK;
4476}
4477#endif
4478
4479#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4480/*
4481 * Use a terminal window to run a shell command in.
4482 */
4483 static int
4484mch_call_shell_terminal(
4485 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004486 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004487{
4488 jobopt_T opt;
4489 char **argv = NULL;
4490 char_u *tofree1 = NULL;
4491 char_u *tofree2 = NULL;
4492 int retval = -1;
4493 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004494 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004495 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004496 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004497
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004498 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004499 goto theend;
4500
4501 init_job_options(&opt);
4502 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4503 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004504 if (buf == NULL)
4505 goto theend;
4506
4507 job = term_getjob(buf->b_term);
4508 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004509
Bram Moolenaar0f873732019-12-05 20:28:46 +01004510 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004511 aucmd_prepbuf(&aco, buf);
4512
4513 clear_oparg(&oa);
4514 while (term_use_loop())
4515 {
4516 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4517 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004518 // If terminal_loop() returns OK we got a key that is handled
4519 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004520 if (terminal_loop(TRUE) == OK)
4521 normal_cmd(&oa, TRUE);
4522 }
4523 else
4524 normal_cmd(&oa, TRUE);
4525 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004526 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004527 ch_log(NULL, "system command finished");
4528
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004529 job_unref(job);
4530
Bram Moolenaar0f873732019-12-05 20:28:46 +01004531 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004532 aucmd_restbuf(&aco);
4533
4534 wait_return(TRUE);
4535 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4536
4537theend:
4538 vim_free(argv);
4539 vim_free(tofree1);
4540 vim_free(tofree2);
4541 return retval;
4542}
4543#endif
4544
4545#ifdef USE_SYSTEM
4546/*
4547 * Use system() to start the shell: simple but slow.
4548 */
4549 static int
4550mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004551 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004552 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553{
4554#ifdef VMS
4555 char *ifn = NULL;
4556 char *ofn = NULL;
4557#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004558 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004559 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004560 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
4562 out_flush();
4563
4564 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004565 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
Bram Moolenaar62b42182010-09-21 22:09:37 +02004567# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004568 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004569 loose_clipboard();
4570# endif
4571
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 if (cmd == NULL)
4573 x = system((char *)p_sh);
4574 else
4575 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004576# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 if (ofn = strchr((char *)cmd, '>'))
4578 *ofn++ = '\0';
4579 if (ifn = strchr((char *)cmd, '<'))
4580 {
4581 char *p;
4582
4583 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004584 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 if (p)
4586 *p = '\0';
4587 }
4588 if (ofn)
4589 x = vms_sys((char *)cmd, ofn, ifn);
4590 else
4591 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004592# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004593 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004595 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 if (newcmd == NULL)
4597 x = 0;
4598 else
4599 {
4600 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4601 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4602 (char *)p_shcf,
4603 (char *)cmd);
4604 x = system((char *)newcmd);
4605 vim_free(newcmd);
4606 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609# ifdef VMS
4610 x = vms_sys_status(x);
4611# endif
4612 if (emsg_silent)
4613 ;
4614 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004615 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 else if (x && !(options & SHELL_SILENT))
4617 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004618 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 msg_outnum((long)x);
4620 msg_putchar('\n');
4621 }
4622
4623 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004624 {
4625 // The shell may have messed with the mode, always set it.
4626 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004627 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004630# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4631 restore_clipboard();
4632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004634}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635
Bram Moolenaar0f873732019-12-05 20:28:46 +01004636#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637
Bram Moolenaar0f873732019-12-05 20:28:46 +01004638# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4639 // 127, some shells use that already
4640# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
Bram Moolenaar13568252018-03-16 20:46:58 +01004642/*
4643 * Don't use system(), use fork()/exec().
4644 */
4645 static int
4646mch_call_shell_fork(
4647 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004648 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004649{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004650 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004652 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 pid_t wait_pid = 0;
4654# ifdef HAVE_UNION_WAIT
4655 union wait status;
4656# else
4657 int status = -1;
4658# endif
4659 int retval = -1;
4660 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004661 char_u *tofree1 = NULL;
4662 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004664 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004665# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004667# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004668 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 int fd_fromshell[2];
4670 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004671 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
4673 out_flush();
4674 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004675 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004676 if (tmode == TMODE_RAW)
4677 // The shell may have messed with the mode, always set it later.
4678 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004680 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004681 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004682
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004684 * For the GUI, when writing the output into the buffer and when reading
4685 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4686 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004688 if ((options & (SHELL_READ|SHELL_WRITE))
4689# ifdef FEAT_GUI
4690 || (gui.in_use && show_shell_mess)
4691# endif
4692 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004694# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 /*
4696 * Try to open a master pty.
4697 * If this works, open the slave pty.
4698 * If the slave can't be opened, close the master pty.
4699 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004700 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004701 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 /*
4703 * If not opening a pty or it didn't work, try using pipes.
4704 */
4705 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004706# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 {
4708 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004709 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
4711 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004712 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
4714 close(fd_toshell[0]);
4715 close(fd_toshell[1]);
4716 }
4717 }
4718 if (pipe_error)
4719 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004720 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 out_flush();
4722 }
4723 }
4724 }
4725
Bram Moolenaar0f873732019-12-05 20:28:46 +01004726 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004728 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004729 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004730 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004731 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004733 UNBLOCK_SIGNALS(&curset);
4734
Bram Moolenaar32526b32019-01-19 17:43:09 +01004735 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004736 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004738 || (gui.in_use && show_shell_mess)
4739# endif
4740 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004742# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004743 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
4745 close(pty_master_fd);
4746 close(pty_slave_fd);
4747 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004748 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 {
4751 close(fd_toshell[0]);
4752 close(fd_toshell[1]);
4753 close(fd_fromshell[0]);
4754 close(fd_fromshell[1]);
4755 }
4756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004758 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004760 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004761 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
Bram Moolenaar819524702018-02-27 19:10:00 +01004763# ifdef FEAT_JOB_CHANNEL
4764 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004765 {
4766 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004767 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004768 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004769# endif
4770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 if (!show_shell_mess || (options & SHELL_EXPAND))
4772 {
4773 int fd;
4774
4775 /*
4776 * Don't want to show any message from the shell. Can't just
4777 * close stdout and stderr though, because some systems will
4778 * break if you try to write to them after that, so we must
4779 * use dup() to replace them with something else -- webb
4780 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4781 * waiting for input.
4782 */
4783 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4784 fclose(stdin);
4785 fclose(stdout);
4786 fclose(stderr);
4787
4788 /*
4789 * If any of these open()'s and dup()'s fail, we just continue
4790 * anyway. It's not fatal, and on most systems it will make
4791 * no difference at all. On a few it will cause the execvp()
4792 * to exit with a non-zero status even when the completion
4793 * could be done, which is nothing too serious. If the open()
4794 * or dup() failed we'd just do the same thing ourselves
4795 * anyway -- webb
4796 */
4797 if (fd >= 0)
4798 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004799 vim_ignored = dup(fd); // To replace stdin (fd 0)
4800 vim_ignored = dup(fd); // To replace stdout (fd 1)
4801 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
Bram Moolenaar0f873732019-12-05 20:28:46 +01004803 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 close(fd);
4805 }
4806 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004807 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004809 || gui.in_use
4810# endif
4811 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 {
4813
Bram Moolenaardf177f62005-02-22 08:39:57 +00004814# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004815 // Create our own process group, so that the child and all its
4816 // children can be kill()ed. Don't do this when using pipes,
4817 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004818 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004819 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004820 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004821# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004822 // When doing "!xterm&" and 'shell' is bash: the shell
4823 // will exit and send SIGHUP to all processes in its
4824 // group, killing the just started process. Ignore SIGHUP
4825 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004826 signal(SIGHUP, SIG_IGN);
4827# endif
4828 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004829# endif
4830# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004831 if (pty_slave_fd >= 0)
4832 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004833 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004834 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004835 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004836# ifdef TIOCSCTTY
4837 // Try to become controlling tty (probably doesn't work,
4838 // unless run by root)
4839 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4840# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004841 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004842# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004843 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
Bram Moolenaara5792f52005-11-23 21:25:05 +00004845 /*
4846 * stderr is only redirected when using the GUI, so that a
4847 * program like gpg can still access the terminal to get a
4848 * passphrase using stderr.
4849 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004850# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 if (pty_master_fd >= 0)
4852 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004853 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
Bram Moolenaar0f873732019-12-05 20:28:46 +01004855 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004857 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004859 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004860 if (gui.in_use)
4861 {
4862 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004863 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
Bram Moolenaar0f873732019-12-05 20:28:46 +01004866 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004869# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004871 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 close(fd_toshell[1]);
4873 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004874 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 close(fd_toshell[0]);
4876
Bram Moolenaar0f873732019-12-05 20:28:46 +01004877 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 close(fd_fromshell[0]);
4879 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004880 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 close(fd_fromshell[1]);
4882
Bram Moolenaara5792f52005-11-23 21:25:05 +00004883# ifdef FEAT_GUI
4884 if (gui.in_use)
4885 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004886 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004887 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004888 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004889 }
4890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004893
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 /*
4895 * There is no type cast for the argv, because the type may be
4896 * different on different machines. This may cause a warning
4897 * message with strict compilers, don't worry about it.
4898 * Call _exit() instead of exit() to avoid closing the connection
4899 * to the X server (esp. with GTK, which uses atexit()).
4900 */
4901 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004902 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004904 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
4906 /*
4907 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004908 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 */
4910 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004911 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004912 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004913# ifdef FEAT_JOB_CHANNEL
4914 ++dont_check_job_ended;
4915# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 /*
4917 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004918 * This is also used to pipe stdin/stdout to/from the external
4919 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004921 if ((options & (SHELL_READ|SHELL_WRITE))
4922# ifdef FEAT_GUI
4923 || (gui.in_use && show_shell_mess)
4924# endif
4925 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004927# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004929 int buffer_off = 0; // valid bytes in buffer[]
4930 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4931 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 int len;
4933 int p_more_save;
4934 int old_State;
4935 int c;
4936 int toshell_fd;
4937 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004938 garray_T ga;
4939 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004940# ifdef ELAPSED_FUNC
4941 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
Bram Moolenaardf177f62005-02-22 08:39:57 +00004944# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 if (pty_master_fd >= 0)
4946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 fromshell_fd = pty_master_fd;
4948 toshell_fd = dup(pty_master_fd);
4949 }
4950 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 {
4953 close(fd_toshell[0]);
4954 close(fd_fromshell[1]);
4955 toshell_fd = fd_toshell[1];
4956 fromshell_fd = fd_fromshell[0];
4957 }
4958
4959 /*
4960 * Write to the child if there are typed characters.
4961 * Read from the child if there are characters available.
4962 * Repeat the reading a few times if more characters are
4963 * available. Need to check for typed keys now and then, but
4964 * not too often (delays when no chars are available).
4965 * This loop is quit if no characters can be read from the pty
4966 * (WaitForChar detected special condition), or there are no
4967 * characters available and the child has exited.
4968 * Only check if the child has exited when there is no more
4969 * output. The child may exit before all the output has
4970 * been printed.
4971 *
4972 * Currently this busy loops!
4973 * This can probably dead-lock when the write blocks!
4974 */
4975 p_more_save = p_more;
4976 p_more = FALSE;
4977 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004978 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004980 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004981 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004982 // Fork a process that will write the lines to the
4983 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004984 if ((wpid = fork()) == -1)
4985 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004986 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004987 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004988 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004989 {
4990 linenr_T lnum = curbuf->b_op_start.lnum;
4991 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004992 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004993 size_t l;
4994
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004995 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004996 for (;;)
4997 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004998 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004999 if (l == 0)
5000 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005001 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005002 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00005003 len = write(toshell_fd, "", (size_t)1);
5004 else
5005 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005006 char_u *s = vim_strchr(lp + written, NL);
5007
Bram Moolenaar89d40322006-08-29 15:30:07 +00005008 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00005009 s == NULL ? l
5010 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005011 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00005012 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005013 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005014 // Finished a line, add a NL, unless this line
5015 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005016 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005017 || (!curbuf->b_p_bin
5018 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005019 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00005020 && (lnum !=
5021 curbuf->b_ml.ml_line_count
5022 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005023 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005024 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005025 ++lnum;
5026 if (lnum > curbuf->b_op_end.lnum)
5027 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005028 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005029 close(toshell_fd);
5030 toshell_fd = -1;
5031 break;
5032 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005033 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005034 written = 0;
5035 }
5036 else if (len > 0)
5037 written += len;
5038 }
5039 _exit(0);
5040 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005041 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005042 {
5043 close(toshell_fd);
5044 toshell_fd = -1;
5045 }
5046 }
5047
5048 if (options & SHELL_READ)
5049 ga_init2(&ga, 1, BUFLEN);
5050
5051 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005052# ifdef ELAPSED_FUNC
5053 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005054# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 for (;;)
5056 {
5057 /*
5058 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005059 * if there are any.
5060 * Don't do this if we are expanding wild cards (would eat
5061 * typeahead).
5062 * Don't do this when filtering and terminal is in cooked
5063 * mode, the shell command will handle the I/O. Avoids
5064 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005065 * Don't get characters when the child has already
5066 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005067 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005068 * while (noread_cnt > 4), avoids that ":r !ls" eats
5069 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
5071 len = 0;
5072 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005073 && ((options &
5074 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5075 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005076# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005077 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005078# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005079 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005080 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005081 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005083 if (ta_len == 0)
5084 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005085 // Get extra characters when we don't have any.
5086 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005087 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005088# ifdef ELAPSED_FUNC
5089 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005090# endif
5091 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5092 }
5093 if (ta_len > 0 || len > 0)
5094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 /*
5096 * For pipes:
5097 * Check for CTRL-C: send interrupt signal to child.
5098 * Check for CTRL-D: EOF, close pipe to child.
5099 */
5100 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5101 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 /*
5103 * Send SIGINT to the child's group or all
5104 * processes in our group.
5105 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005106 may_send_sigint(ta_buf[ta_len], pid, wpid);
5107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 if (pty_master_fd < 0 && toshell_fd >= 0
5109 && ta_buf[ta_len] == Ctrl_D)
5110 {
5111 close(toshell_fd);
5112 toshell_fd = -1;
5113 }
5114 }
5115
Bram Moolenaarf4140482020-02-15 23:06:45 +01005116 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117
5118 /*
5119 * For pipes: echo the typed characters.
5120 * For a pty this does not seem to work.
5121 */
5122 if (pty_master_fd < 0)
5123 {
5124 for (i = ta_len; i < ta_len + len; ++i)
5125 {
5126 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5127 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 else if (has_mbyte)
5129 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005130 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131
5132 msg_outtrans_len(ta_buf + i, l);
5133 i += l - 1;
5134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 else
5136 msg_outtrans_len(ta_buf + i, 1);
5137 }
5138 windgoto(msg_row, msg_col);
5139 out_flush();
5140 }
5141
5142 ta_len += len;
5143
5144 /*
5145 * Write the characters to the child, unless EOF has
5146 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005147 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005148 * When writing buffer lines, drop the typed
5149 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005151 if (options & SHELL_WRITE)
5152 ta_len = 0;
5153 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 {
5155 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5156 if (len > 0)
5157 {
5158 ta_len -= len;
5159 mch_memmove(ta_buf, ta_buf + len, ta_len);
5160 }
5161 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
5164
Bram Moolenaardf177f62005-02-22 08:39:57 +00005165 if (got_int)
5166 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005167 // CTRL-C sends a signal to the child, we ignore it
5168 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005169# ifdef HAVE_SETSID
5170 kill(-pid, SIGINT);
5171# else
5172 kill(0, SIGINT);
5173# endif
5174 if (wpid > 0)
5175 kill(wpid, SIGINT);
5176 got_int = FALSE;
5177 }
5178
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 /*
5180 * Check if the child has any characters to be printed.
5181 * Read them and write them to our window. Repeat this as
5182 * long as there is something to do, avoid the 10ms wait
5183 * for mch_inchar(), or sending typeahead characters to
5184 * the external process.
5185 * TODO: This should handle escape sequences, compatible
5186 * to some terminal (vt52?).
5187 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005188 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005189 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005191 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005194 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005196
5197 noread_cnt = 0;
5198 if (options & SHELL_READ)
5199 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005200 // Do NUL -> NL translation, append NL separated
5201 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005202 for (i = 0; i < len; ++i)
5203 {
5204 if (buffer[i] == NL)
5205 append_ga_line(&ga);
5206 else if (buffer[i] == NUL)
5207 ga_append(&ga, NL);
5208 else
5209 ga_append(&ga, buffer[i]);
5210 }
5211 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005212 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {
5214 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005215 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216
Bram Moolenaardf177f62005-02-22 08:39:57 +00005217 len += buffer_off;
5218 buffer[len] = NUL;
5219
Bram Moolenaar0f873732019-12-05 20:28:46 +01005220 // Check if the last character in buffer[] is
5221 // incomplete, keep these bytes for the next
5222 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 for (p = buffer; p < buffer + len; p += l)
5224 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005225 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005227 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 else if (MB_BYTE2LEN(*p) != l)
5229 break;
5230 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005231 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005233 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 if (len >= 12)
5235 ++p;
5236 else
5237 {
5238 buffer_off = len;
5239 continue;
5240 }
5241 }
5242 c = *p;
5243 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005244 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 if (p < buffer + len)
5246 {
5247 *p = c;
5248 buffer_off = (buffer + len) - p;
5249 mch_memmove(buffer, p, buffer_off);
5250 continue;
5251 }
5252 buffer_off = 0;
5253 }
5254 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 {
5256 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005257 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 }
5259
5260 windgoto(msg_row, msg_col);
5261 cursor_on();
5262 out_flush();
5263 if (got_int)
5264 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005265
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005266# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005267 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005268 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005269 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005270
Bram Moolenaar0f873732019-12-05 20:28:46 +01005271 // Avoid that we keep looping here without
5272 // checking for a CTRL-C for a long time. Don't
5273 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005274 if (msec > 2000)
5275 {
5276 noread_cnt = 5;
5277 break;
5278 }
5279 }
5280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
5282
Bram Moolenaar0f873732019-12-05 20:28:46 +01005283 // If we already detected the child has finished, continue
5284 // reading output for a short while. Some text may be
5285 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005286 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005287 {
5288 if (noread_cnt < 5)
5289 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005290 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005291 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005292
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 /*
5294 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005295 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005297# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005298 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005299# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005301# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5303 || (wait_pid == pid && WIFEXITED(status)))
5304 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005305 // Don't break the loop yet, try reading more
5306 // characters from "fromshell_fd" first. When using
5307 // pipes there might still be something to read and
5308 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005311 else
5312 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005313
Bram Moolenaar95a51352013-03-21 22:53:50 +01005314# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005315 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005316 clip_update();
5317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 }
5319finished:
5320 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005321 if (options & SHELL_READ)
5322 {
5323 if (ga.ga_len > 0)
5324 {
5325 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005326 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005327 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005328 }
5329 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005330 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005331 ga_clear(&ga);
5332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 /*
5335 * Give all typeahead that wasn't used back to ui_inchar().
5336 */
5337 if (ta_len)
5338 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 State = old_State;
5340 if (toshell_fd >= 0)
5341 close(toshell_fd);
5342 close(fromshell_fd);
5343 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005344# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005345 else
5346 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005347 long delay_msec = 1;
5348
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005349 if (tmode == TMODE_RAW)
5350 // possibly disables modifyOtherKeys, so that the system
5351 // can recognize CTRL-C
5352 out_str(T_CTE);
Bram Moolenaar0981c872020-08-23 14:28:37 +02005353
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005354 /*
5355 * Similar to the loop above, but only handle X events, no
5356 * I/O.
5357 */
5358 for (;;)
5359 {
5360 if (got_int)
5361 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005362 // CTRL-C sends a signal to the child, we ignore it
5363 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005364# ifdef HAVE_SETSID
5365 kill(-pid, SIGINT);
5366# else
5367 kill(0, SIGINT);
5368# endif
5369 got_int = FALSE;
5370 }
5371# ifdef __NeXT__
5372 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5373# else
5374 wait_pid = waitpid(pid, &status, WNOHANG);
5375# endif
5376 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5377 || (wait_pid == pid && WIFEXITED(status)))
5378 {
5379 wait_pid = pid;
5380 break;
5381 }
5382
Bram Moolenaar0f873732019-12-05 20:28:46 +01005383 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005384 clip_update();
5385
Bram Moolenaar0f873732019-12-05 20:28:46 +01005386 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005387 // less time, gradually wait longer.
5388 mch_delay(delay_msec,
5389 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005390 if (++delay_msec > 10)
5391 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005392 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005393
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005394 if (tmode == TMODE_RAW)
5395 // possibly enables modifyOtherKeys again
5396 out_str(T_CTI);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005397 }
5398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399
5400 /*
5401 * Wait until our child has exited.
5402 * Ignore wait() returning pids of other children and returning
5403 * because of some signal like SIGWINCH.
5404 * Don't wait if wait_pid was already set above, indicating the
5405 * child already exited.
5406 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005407 if (wait_pid != pid)
5408 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
Bram Moolenaar624891f2010-10-13 16:22:09 +02005410# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005411 // Close slave side of pty. Only do this after the child has
5412 // exited, otherwise the child may hang when it tries to write on
5413 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005414 if (pty_master_fd >= 0)
5415 close(pty_slave_fd);
5416# endif
5417
Bram Moolenaar0f873732019-12-05 20:28:46 +01005418 // Make sure the child that writes to the external program is
5419 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005420 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005421 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005422 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005423 wait4pid(wpid, NULL);
5424 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005425
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005426# ifdef FEAT_JOB_CHANNEL
5427 --dont_check_job_ended;
5428# endif
5429
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 /*
5431 * Set to raw mode right now, otherwise a CTRL-C after
5432 * catch_signals() will kill Vim.
5433 */
5434 if (tmode == TMODE_RAW)
5435 settmode(TMODE_RAW);
5436 did_settmode = TRUE;
5437 set_signals();
5438
5439 if (WIFEXITED(status))
5440 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005441 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005443 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {
5445 if (retval == EXEC_FAILED)
5446 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005447 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 msg_outtrans(p_sh);
5449 msg_putchar('\n');
5450 }
5451 else if (!(options & SHELL_SILENT))
5452 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005453 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 msg_outnum((long)retval);
5455 msg_putchar('\n');
5456 }
5457 }
5458 }
5459 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005460 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 }
5462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463
5464error:
5465 if (!did_settmode)
5466 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005467 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005469 vim_free(argv);
5470 vim_free(tofree1);
5471 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
5473 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005474}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005475#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005476
5477 int
5478mch_call_shell(
5479 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005480 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005481{
5482#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5483 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5484 return mch_call_shell_terminal(cmd, options);
5485#endif
5486#ifdef USE_SYSTEM
5487 return mch_call_shell_system(cmd, options);
5488#else
5489 return mch_call_shell_fork(cmd, options);
5490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491}
5492
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005493#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005494 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005495mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005496{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005497 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005498 int fd_in[2] = {-1, -1}; // for stdin
5499 int fd_out[2] = {-1, -1}; // for stdout
5500 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005501 int pty_master_fd = -1;
5502 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005503 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005504 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5505 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5506 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005507 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005508 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5509 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005510 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005511 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005512 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005513
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005514 if (use_out_for_err && use_null_for_out)
5515 use_null_for_err = TRUE;
5516
Bram Moolenaar0f873732019-12-05 20:28:46 +01005517 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005518 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005519
Bram Moolenaarb2412082017-08-20 18:09:14 +02005520 if (options->jo_pty
5521 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005522 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005523 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005524 open_pty(&pty_master_fd, &pty_slave_fd,
5525 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005526
Bram Moolenaar0f873732019-12-05 20:28:46 +01005527 // TODO: without the channel feature connect the child to /dev/null?
5528 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005529 if (use_file_for_in)
5530 {
5531 char_u *fname = options->jo_io_name[PART_IN];
5532
5533 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5534 if (fd_in[0] < 0)
5535 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005536 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005537 goto failed;
5538 }
5539 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005540 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005541 // When writing buffer lines to the input don't use the pty, so that
5542 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005543 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5544 && pipe(fd_in) < 0)
5545 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005546
5547 if (use_file_for_out)
5548 {
5549 char_u *fname = options->jo_io_name[PART_OUT];
5550
5551 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5552 if (fd_out[1] < 0)
5553 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005554 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005555 goto failed;
5556 }
5557 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005558 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005559 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005560
5561 if (use_file_for_err)
5562 {
5563 char_u *fname = options->jo_io_name[PART_ERR];
5564
5565 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5566 if (fd_err[1] < 0)
5567 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005568 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005569 goto failed;
5570 }
5571 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005572 else if (!use_out_for_err && !use_null_for_err
5573 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005574 goto failed;
5575
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005576 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5577 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005578 if (options->jo_set & JO_CHANNEL)
5579 {
5580 channel = options->jo_channel;
5581 if (channel != NULL)
5582 ++channel->ch_refcount;
5583 }
5584 else
5585 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005586 if (channel == NULL)
5587 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005588 if (job->jv_tty_out != NULL)
5589 ch_log(channel, "using pty %s on fd %d",
5590 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005591 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005592
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005593 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005594 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005595 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005596 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005597 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005598 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005599 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005600 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005601 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005602 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005603 int null_fd = -1;
5604 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005605
Bram Moolenaar0f873732019-12-05 20:28:46 +01005606 // child
5607 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005608 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005609
Bram Moolenaar819524702018-02-27 19:10:00 +01005610# ifdef FEAT_JOB_CHANNEL
5611 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005612 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005613 ch_logfile((char_u *)"", (char_u *)"");
5614# endif
5615
Bram Moolenaar835dc632016-02-07 14:27:38 +01005616# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005617 // Create our own process group, so that the child and all its
5618 // children can be kill()ed. Don't do this when using pipes,
5619 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005620 (void)setsid();
5621# endif
5622
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005623# ifdef FEAT_TERMINAL
5624 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005625 {
5626 char *term = (char *)T_NAME;
5627
5628#ifdef FEAT_GUI
5629 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005630 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005631 term = getenv("TERM");
5632#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005633 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005634 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005635 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005636 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005637 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005638 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005639 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005640 else if (t_colors > 16)
5641 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005642 else
5643 term = "xterm";
5644 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005645 set_child_environment(
5646 (long)options->jo_term_rows,
5647 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005648 term,
5649 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005650 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005651 else
5652# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005653 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005654
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005655 if (options->jo_env != NULL)
5656 {
5657 dict_T *dict = options->jo_env;
5658 hashitem_T *hi;
5659 int todo = (int)dict->dv_hashtab.ht_used;
5660
5661 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5662 if (!HASHITEM_EMPTY(hi))
5663 {
5664 typval_T *item = &dict_lookup(hi)->di_tv;
5665
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005666 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005667 --todo;
5668 }
5669 }
5670
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005671 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005672 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005673 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005674 if (null_fd < 0)
5675 {
5676 perror("opening /dev/null failed");
5677 _exit(OPEN_NULL_FAILED);
5678 }
5679 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005680
Bram Moolenaar223896d2017-08-02 22:33:28 +02005681 if (pty_slave_fd >= 0)
5682 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005683 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005684 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005685# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005686 // Try to become controlling tty (probably doesn't work,
5687 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005688 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5689# endif
5690 }
5691
Bram Moolenaar0f873732019-12-05 20:28:46 +01005692 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005693 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005694 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005695 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005696 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005697 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005698 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005699 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005700
Bram Moolenaar0f873732019-12-05 20:28:46 +01005701 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005702 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005703 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005704 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005705 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005706 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005707 }
5708 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005709 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005710 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005711 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005712 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005713 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005714
Bram Moolenaar0f873732019-12-05 20:28:46 +01005715 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005716 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005717 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005718 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005719 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005720 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005721 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005722 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005723
5724 if (fd_in[0] >= 0)
5725 close(fd_in[0]);
5726 if (fd_in[1] >= 0)
5727 close(fd_in[1]);
5728 if (fd_out[0] >= 0)
5729 close(fd_out[0]);
5730 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005731 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005732 if (fd_err[0] >= 0)
5733 close(fd_err[0]);
5734 if (fd_err[1] >= 0)
5735 close(fd_err[1]);
5736 if (pty_master_fd >= 0)
5737 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005738 close(pty_master_fd); // not used in the child
5739 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005740 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005741
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005742 if (null_fd >= 0)
5743 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005744
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005745 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5746 _exit(EXEC_FAILED);
5747
Bram Moolenaar0f873732019-12-05 20:28:46 +01005748 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005749 execvp(argv[0], argv);
5750
Bram Moolenaar4694a172016-04-21 14:05:23 +02005751 if (stderr_works)
5752 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005753# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005754 // calling free_all_mem() here causes problems. Ignore valgrind
5755 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005756# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005757 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005758 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005759
Bram Moolenaar0f873732019-12-05 20:28:46 +01005760 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005761 UNBLOCK_SIGNALS(&curset);
5762
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005763 job->jv_pid = pid;
5764 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005765 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005766
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005767 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005768 close(pty_slave_fd); // not used in the parent
5769 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005770 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005771 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005772 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005773 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005774 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005775 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005776 if (channel != NULL)
5777 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005778 int in_fd = INVALID_FD;
5779 int out_fd = INVALID_FD;
5780 int err_fd = INVALID_FD;
5781
5782 if (!(use_file_for_in || use_null_for_in))
5783 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5784
5785 if (!(use_file_for_out || use_null_for_out))
5786 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5787
5788 // When using pty_master_fd only set it for stdout, do not duplicate
5789 // it for stderr, it only needs to be read once.
5790 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5791 {
5792 if (fd_err[0] >= 0)
5793 err_fd = fd_err[0];
5794 else if (out_fd != pty_master_fd)
5795 err_fd = pty_master_fd;
5796 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005797
5798 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005799 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005800 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005801 else
5802 {
5803 if (fd_in[1] >= 0)
5804 close(fd_in[1]);
5805 if (fd_out[0] >= 0)
5806 close(fd_out[0]);
5807 if (fd_err[0] >= 0)
5808 close(fd_err[0]);
5809 if (pty_master_fd >= 0)
5810 close(pty_master_fd);
5811 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005812
Bram Moolenaar0f873732019-12-05 20:28:46 +01005813 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005814 return;
5815
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005816failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005817 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005818 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005819 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005820 if (fd_in[1] >= 0)
5821 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005822 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005823 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005824 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005825 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005826 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005827 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005828 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005829 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005830 if (pty_master_fd >= 0)
5831 close(pty_master_fd);
5832 if (pty_slave_fd >= 0)
5833 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005834}
5835
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005836 static char_u *
5837get_signal_name(int sig)
5838{
5839 int i;
5840 char_u numbuf[NUMBUFLEN];
5841
5842 if (sig == SIGKILL)
5843 return vim_strsave((char_u *)"kill");
5844
5845 for (i = 0; signal_info[i].sig != -1; i++)
5846 if (sig == signal_info[i].sig)
5847 return strlow_save((char_u *)signal_info[i].name);
5848
5849 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5850 return vim_strsave(numbuf);
5851}
5852
Bram Moolenaar835dc632016-02-07 14:27:38 +01005853 char *
5854mch_job_status(job_T *job)
5855{
5856# ifdef HAVE_UNION_WAIT
5857 union wait status;
5858# else
5859 int status = -1;
5860# endif
5861 pid_t wait_pid = 0;
5862
5863# ifdef __NeXT__
5864 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5865# else
5866 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5867# endif
5868 if (wait_pid == -1)
5869 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005870 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005871 if (job->jv_status < JOB_ENDED)
5872 ch_log(job->jv_channel, "Job no longer exists: %s",
5873 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005874 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005875 }
5876 if (wait_pid == 0)
5877 return "run";
5878 if (WIFEXITED(status))
5879 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005880 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005881 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005882 if (job->jv_status < JOB_ENDED)
5883 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005884 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005885 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005886 if (WIFSIGNALED(status))
5887 {
5888 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005889 job->jv_termsig = get_signal_name(WTERMSIG(status));
5890 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5891 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5892 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005893 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005894 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005895 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005896
5897return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005898 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005899 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005900 return "dead";
5901}
5902
5903 job_T *
5904mch_detect_ended_job(job_T *job_list)
5905{
5906# ifdef HAVE_UNION_WAIT
5907 union wait status;
5908# else
5909 int status = -1;
5910# endif
5911 pid_t wait_pid = 0;
5912 job_T *job;
5913
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005914# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005915 // Do not do this when waiting for a shell command to finish, we would get
5916 // the exit value here (and discard it), the exit value obtained there
5917 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005918 if (dont_check_job_ended > 0)
5919 return NULL;
5920# endif
5921
Bram Moolenaar97792de2016-10-15 18:36:49 +02005922# ifdef __NeXT__
5923 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5924# else
5925 wait_pid = waitpid(-1, &status, WNOHANG);
5926# endif
5927 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005928 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005929 return NULL;
5930 for (job = job_list; job != NULL; job = job->jv_next)
5931 {
5932 if (job->jv_pid == wait_pid)
5933 {
5934 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005935 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005936 job->jv_exitval = WEXITSTATUS(status);
5937 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005938 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005939 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005940 job->jv_termsig = get_signal_name(WTERMSIG(status));
5941 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005942 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005943 {
5944 ch_log(job->jv_channel, "Job ended");
5945 job->jv_status = JOB_ENDED;
5946 }
5947 return job;
5948 }
5949 }
5950 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005951}
5952
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005953/*
5954 * Send a (deadly) signal to "job".
5955 * Return FAIL if "how" is not a valid name.
5956 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005957 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005958mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005959{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005960 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005961
Bram Moolenaar923d9262016-02-25 20:56:01 +01005962 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005963 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005964 else if (STRCMP(how, "hup") == 0)
5965 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005966 else if (STRCMP(how, "quit") == 0)
5967 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005968 else if (STRCMP(how, "int") == 0)
5969 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005970 else if (STRCMP(how, "kill") == 0)
5971 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005972#ifdef SIGWINCH
5973 else if (STRCMP(how, "winch") == 0)
5974 sig = SIGWINCH;
5975#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005976 else if (isdigit(*how))
5977 sig = atoi((char *)how);
5978 else
5979 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005980
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005981 // Never kill ourselves!
5982 if (job->jv_pid != 0)
5983 {
5984 // TODO: have an option to only kill the process, not the group?
5985 kill(-job->jv_pid, sig);
5986 kill(job->jv_pid, sig);
5987 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005988
Bram Moolenaar835dc632016-02-07 14:27:38 +01005989 return OK;
5990}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005991
5992/*
5993 * Clear the data related to "job".
5994 */
5995 void
5996mch_clear_job(job_T *job)
5997{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005998 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005999# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006000 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006001# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01006002 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006003# endif
6004}
Bram Moolenaar835dc632016-02-07 14:27:38 +01006005#endif
6006
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006007#if defined(FEAT_TERMINAL) || defined(PROTO)
6008 int
6009mch_create_pty_channel(job_T *job, jobopt_T *options)
6010{
6011 int pty_master_fd = -1;
6012 int pty_slave_fd = -1;
6013 channel_T *channel;
6014
Bram Moolenaar59386482019-02-10 22:43:46 +01006015 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02006016 if (pty_master_fd < 0 || pty_slave_fd < 0)
6017 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006018 close(pty_slave_fd);
6019
6020 channel = add_channel();
6021 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006022 {
6023 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006024 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006025 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006026 if (job->jv_tty_out != NULL)
6027 ch_log(channel, "using pty %s on fd %d",
6028 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006029 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006030 channel->ch_keep_open = TRUE;
6031
Bram Moolenaar0f873732019-12-05 20:28:46 +01006032 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6033 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006034 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006035 channel_set_job(channel, job, options);
6036 return OK;
6037}
6038#endif
6039
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040/*
6041 * Check for CTRL-C typed by reading all available characters.
6042 * In cooked mode we should get SIGINT, no need to check.
6043 */
6044 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006045mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006047 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006048 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 fill_input_buf(FALSE);
6050}
6051
6052/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006053 * Wait "msec" msec until a character is available from the mouse, keyboard,
6054 * from inbuf[].
6055 * "msec" == -1 will block forever.
6056 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006057 * When "ignore_input" is TRUE even check for pending input when input is
6058 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006059 * "interrupted" (if not NULL) is set to TRUE when no character is available
6060 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006061 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006062 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 */
6064 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006065WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006067#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006068 return ui_wait_for_chars_or_timer(
6069 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006070#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006071 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006072#endif
6073}
6074
6075/*
6076 * Wait "msec" msec until a character is available from the mouse or keyboard
6077 * or from inbuf[].
6078 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006079 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006080 * "interrupted" (if not NULL) is set to TRUE when no character is available
6081 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006082 * When a GUI is being used, this will never get called -- webb
6083 */
6084 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006085WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006086{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087#ifdef FEAT_MOUSE_GPM
6088 int gpm_process_wanted;
6089#endif
6090#ifdef FEAT_XCLIPBOARD
6091 int rest;
6092#endif
6093 int avail;
6094
Bram Moolenaar0f873732019-12-05 20:28:46 +01006095 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 return 1;
6097
6098#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006099 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 if (WantQueryMouse)
6101 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006102 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006103 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006104 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 }
6106#endif
6107
6108 /*
6109 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6110 * events. This is a bit complicated, because they might both be defined.
6111 */
6112#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6113# ifdef FEAT_XCLIPBOARD
6114 rest = 0;
6115 if (do_xterm_trace())
6116 rest = msec;
6117# endif
6118 do
6119 {
6120# ifdef FEAT_XCLIPBOARD
6121 if (rest != 0)
6122 {
6123 msec = XT_TRACE_DELAY;
6124 if (rest >= 0 && rest < XT_TRACE_DELAY)
6125 msec = rest;
6126 if (rest >= 0)
6127 rest -= msec;
6128 }
6129# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006130# ifdef FEAT_SOUND_CANBERRA
6131 // Invoke any pending sound callbacks.
6132 if (has_sound_callback_in_queue())
6133 invoke_sound_callback();
6134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135# ifdef FEAT_MOUSE_GPM
6136 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006137 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006138 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006139 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006141 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006143# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006145 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 return 1;
6147# ifdef FEAT_XCLIPBOARD
6148 if (rest == 0 || !do_xterm_trace())
6149# endif
6150 break;
6151 }
6152 }
6153 while (FALSE
6154# ifdef FEAT_MOUSE_GPM
6155 || (gpm_process_wanted && mch_gpm_process() == 0)
6156# endif
6157# ifdef FEAT_XCLIPBOARD
6158 || (!avail && rest != 0)
6159# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006160 )
6161 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162
6163#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006164 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165#endif
6166 return avail;
6167}
6168
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006169#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170/*
6171 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006172 * "msec" == 0 will check for characters once.
6173 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006176 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006177 * "interrupted" (if not NULL) is set to TRUE when no character is available
6178 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006180 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006181RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182{
6183 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006184 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006185#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 static int busy = FALSE;
6187
Bram Moolenaar0f873732019-12-05 20:28:46 +01006188 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189# define MAY_LOOP
6190
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006191# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006192 // Remember at what time we started, so that we know how much longer we
6193 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006194 long start_msec = msec;
6195 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006197 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006198 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199# endif
6200
Bram Moolenaar0f873732019-12-05 20:28:46 +01006201 // Handle being called recursively. This may happen for the session
6202 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 if (busy)
6204 return 0;
6205#endif
6206
6207#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006208 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209#endif
6210 {
6211#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006212 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006213# ifdef FEAT_MZSCHEME
6214 int mzquantum_used = FALSE;
6215# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216#endif
6217#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006218 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006219 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 int nfd;
6221# ifdef FEAT_XCLIPBOARD
6222 int xterm_idx = -1;
6223# endif
6224# ifdef FEAT_MOUSE_GPM
6225 int gpm_idx = -1;
6226# endif
6227# ifdef USE_XSMP
6228 int xsmp_idx = -1;
6229# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006230 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006232# ifdef FEAT_MZSCHEME
6233 mzvim_check_threads();
6234 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6235 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006236 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006237 mzquantum_used = TRUE;
6238 }
6239# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 fds[0].fd = fd;
6241 fds[0].events = POLLIN;
6242 nfd = 1;
6243
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006245 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 if (xterm_Shell != (Widget)0)
6247 {
6248 xterm_idx = nfd;
6249 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6250 fds[nfd].events = POLLIN;
6251 nfd++;
6252 }
6253# endif
6254# ifdef FEAT_MOUSE_GPM
6255 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6256 {
6257 gpm_idx = nfd;
6258 fds[nfd].fd = gpm_fd;
6259 fds[nfd].events = POLLIN;
6260 nfd++;
6261 }
6262# endif
6263# ifdef USE_XSMP
6264 if (xsmp_icefd != -1)
6265 {
6266 xsmp_idx = nfd;
6267 fds[nfd].fd = xsmp_icefd;
6268 fds[nfd].events = POLLIN;
6269 nfd++;
6270 }
6271# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006272#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006273 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006274#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006275 if (interrupted != NULL)
6276 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006278 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006279
6280 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006281 if (result == 0 && interrupted != NULL && ret > 0)
6282 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006283
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006284# ifdef FEAT_MZSCHEME
6285 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006286 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006287 finished = FALSE;
6288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290# ifdef FEAT_XCLIPBOARD
6291 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6292 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006293 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006295 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 finished = FALSE;
6297 }
6298# endif
6299# ifdef FEAT_MOUSE_GPM
6300 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302# endif
6303# ifdef USE_XSMP
6304 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6305 {
6306 if (fds[xsmp_idx].revents & POLLIN)
6307 {
6308 busy = TRUE;
6309 xsmp_handle_requests();
6310 busy = FALSE;
6311 }
6312 else if (fds[xsmp_idx].revents & POLLHUP)
6313 {
6314 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006315 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 xsmp_close();
6317 }
6318 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006319 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 }
6321# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006322#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006323 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006324 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006325 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327
Bram Moolenaar0f873732019-12-05 20:28:46 +01006328#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329
6330 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006331 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006332 // These are static because they can take 8 Kbyte each and cause the
6333 // signal stack to run out with -O3.
6334 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006336 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006338# ifdef FEAT_MZSCHEME
6339 mzvim_check_threads();
6340 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6341 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006342 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006343 mzquantum_used = TRUE;
6344 }
6345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006347 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006349 tv.tv_sec = towait / 1000;
6350 tv.tv_usec = (towait % 1000) * (1000000/1000);
6351 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006353 else
6354 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355
6356 /*
6357 * Select on ready for reading and exceptional condition (end of file).
6358 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006359select_eintr:
6360 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006361 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 FD_ZERO(&efds);
6363 FD_SET(fd, &rfds);
K.Takatab247e062022-02-07 10:45:23 +00006364# ifndef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006365 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 FD_SET(fd, &efds);
6367# endif
6368 maxfd = fd;
6369
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006371 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 if (xterm_Shell != (Widget)0)
6373 {
6374 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6375 if (maxfd < ConnectionNumber(xterm_dpy))
6376 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006377
Bram Moolenaar0f873732019-12-05 20:28:46 +01006378 // An event may have already been read but not handled. In
6379 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006380 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 }
6382# endif
6383# ifdef FEAT_MOUSE_GPM
6384 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6385 {
6386 FD_SET(gpm_fd, &rfds);
6387 FD_SET(gpm_fd, &efds);
6388 if (maxfd < gpm_fd)
6389 maxfd = gpm_fd;
6390 }
6391# endif
6392# ifdef USE_XSMP
6393 if (xsmp_icefd != -1)
6394 {
6395 FD_SET(xsmp_icefd, &rfds);
6396 FD_SET(xsmp_icefd, &efds);
6397 if (maxfd < xsmp_icefd)
6398 maxfd = xsmp_icefd;
6399 }
6400# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006401# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006402 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006403# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006404 if (interrupted != NULL)
6405 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
Bram Moolenaar643b6142018-09-12 20:29:09 +02006407 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6408 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006409 result = ret > 0 && FD_ISSET(fd, &rfds);
6410 if (result)
6411 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006412 else if (interrupted != NULL && ret > 0)
6413 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006414
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006415# ifdef EINTR
6416 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006417 {
dbivolaruab16ad32021-12-29 19:41:47 +00006418 // Check whether the EINTR is caused by SIGTSTP
6419 if (got_tstp && !in_mch_suspend)
6420 {
6421 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006422
dbivolaruab16ad32021-12-29 19:41:47 +00006423 ea.forceit = TRUE;
6424 ex_stop(&ea);
6425 got_tstp = FALSE;
6426 }
6427
Bram Moolenaar0f873732019-12-05 20:28:46 +01006428 // Check whether window has been resized, EINTR may be caused by
6429 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006430 if (do_resize)
6431 handle_resize();
6432
Bram Moolenaar0f873732019-12-05 20:28:46 +01006433 // Interrupted by a signal, need to try again. We ignore msec
6434 // here, because we do want to check even after a timeout if
6435 // characters are available. Needed for reading output of an
6436 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006437 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006438 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006439# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006440# ifdef __TANDEM
6441 if (ret == -1 && errno == ENOTSUP)
6442 {
6443 FD_ZERO(&rfds);
6444 FD_ZERO(&efds);
6445 ret = 0;
6446 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006447# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006448# ifdef FEAT_MZSCHEME
6449 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006450 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006451 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452# endif
6453
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454# ifdef FEAT_XCLIPBOARD
6455 if (ret > 0 && xterm_Shell != (Widget)0
6456 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6457 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006458 xterm_update(); // Maybe we should hand out clipboard
6459 // continue looping when we only got the X event and the input
6460 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 if (--ret == 0 && !input_available())
6462 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006463 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 finished = FALSE;
6465 }
6466 }
6467# endif
6468# ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00006469 if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 {
6471 if (FD_ISSET(gpm_fd, &efds))
6472 gpm_close();
6473 else if (FD_ISSET(gpm_fd, &rfds))
6474 *check_for_gpm = 1;
6475 }
6476# endif
6477# ifdef USE_XSMP
6478 if (ret > 0 && xsmp_icefd != -1)
6479 {
6480 if (FD_ISSET(xsmp_icefd, &efds))
6481 {
6482 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006483 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 xsmp_close();
6485 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006486 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 }
6488 else if (FD_ISSET(xsmp_icefd, &rfds))
6489 {
6490 busy = TRUE;
6491 xsmp_handle_requests();
6492 busy = FALSE;
6493 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006494 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 }
6496 }
6497# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006498#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006499 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006500 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006501 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
Bram Moolenaar0f873732019-12-05 20:28:46 +01006504#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
6506#ifdef MAY_LOOP
6507 if (finished || msec == 0)
6508 break;
6509
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006510# ifdef FEAT_CLIENTSERVER
6511 if (server_waiting())
6512 break;
6513# endif
6514
Bram Moolenaar0f873732019-12-05 20:28:46 +01006515 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 if (msec > 0)
6517 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006518# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006519 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006520 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006522 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 msec = msec / 2;
6524# endif
6525 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006526 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 }
6528#endif
6529 }
6530
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006531 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532}
6533
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006535 * Expand a path into all matching files and/or directories. Handles "*",
6536 * "?", "[a-z]", "**", etc.
6537 * "path" has backslashes before chars that are not to be expanded.
6538 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 */
6540 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006541mch_expandpath(
6542 garray_T *gap,
6543 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006544 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545{
Bram Moolenaar02743632005-07-25 20:42:36 +00006546 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
6549/*
6550 * mch_expand_wildcards() - this code does wild-card pattern matching using
6551 * the shell
6552 *
6553 * return OK for success, FAIL for error (you may lose some memory) and put
6554 * an error message in *file.
6555 *
6556 * num_pat is number of input patterns
6557 * pat is array of pointers to input patterns
6558 * num_file is pointer to number of matched file names
6559 * file is pointer to array of pointers to matched file names
6560 */
6561
6562#ifndef SEEK_SET
6563# define SEEK_SET 0
6564#endif
6565#ifndef SEEK_END
6566# define SEEK_END 2
6567#endif
6568
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006569#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006570
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006572mch_expand_wildcards(
6573 int num_pat,
6574 char_u **pat,
6575 int *num_file,
6576 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006577 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578{
6579 int i;
6580 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006581 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 char_u *p;
6583 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584
Bram Moolenaarc7247912008-01-13 12:54:11 +00006585 /*
6586 * This is the non-OS/2 implementation (really Unix).
6587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 int j;
6589 char_u *tempname;
6590 char_u *command;
6591 FILE *fd;
6592 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006593#define STYLE_ECHO 0 // use "echo", the default
6594#define STYLE_GLOB 1 // use "glob", for csh
6595#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6596#define STYLE_PRINT 3 // use "print -N", for zsh
6597#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6598 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 int shell_style = STYLE_ECHO;
6600 int check_spaces;
6601 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006602 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006603 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006604 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605
Bram Moolenaar0f873732019-12-05 20:28:46 +01006606 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 *file = NULL;
6608
6609 /*
6610 * If there are no wildcards, just copy the names to allocated memory.
6611 * Saves a lot of time, because we don't have to start a new shell.
6612 */
6613 if (!have_wildcard(num_pat, pat))
6614 return save_patterns(num_pat, pat, num_file, file);
6615
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006616# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006617 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006618 if (sandbox != 0 && check_secure())
6619 return FAIL;
6620# endif
6621
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 /*
6623 * Don't allow the use of backticks in secure and restricted mode.
6624 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006625 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 for (i = 0; i < num_pat; ++i)
6627 if (vim_strchr(pat[i], '`') != NULL
6628 && (check_restricted() || check_secure()))
6629 return FAIL;
6630
6631 /*
6632 * get a name for the temp file
6633 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006634 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006636 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 return FAIL;
6638 }
6639
6640 /*
6641 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006642 * file.
6643 * STYLE_BT: NL separated
6644 * If expanding `cmd` execute it directly.
6645 * STYLE_GLOB: NUL separated
6646 * If we use *csh, "glob" will work better than "echo".
6647 * STYLE_PRINT: NL or NUL separated
6648 * If we use *zsh, "print -N" will work better than "glob".
6649 * STYLE_VIMGLOB: NL separated
6650 * If we use *sh*, we define "vimglob()".
6651 * STYLE_ECHO: space separated.
6652 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 */
6654 if (num_pat == 1 && *pat[0] == '`'
6655 && (len = STRLEN(pat[0])) > 2
6656 && *(pat[0] + len - 1) == '`')
6657 shell_style = STYLE_BT;
6658 else if ((len = STRLEN(p_sh)) >= 3)
6659 {
6660 if (STRCMP(p_sh + len - 3, "csh") == 0)
6661 shell_style = STYLE_GLOB;
6662 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6663 shell_style = STYLE_PRINT;
6664 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006665 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6666 "sh") != NULL)
6667 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668
Bram Moolenaar0f873732019-12-05 20:28:46 +01006669 // Compute the length of the command. We need 2 extra bytes: for the
6670 // optional '&' and for the NUL.
6671 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006673 if (shell_style == STYLE_VIMGLOB)
6674 len += STRLEN(sh_vimglob_func);
6675
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006676 for (i = 0; i < num_pat; ++i)
6677 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006678 // Count the length of the patterns in the same way as they are put in
6679 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006680#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006681 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006682#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006683 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006684 for (j = 0; pat[i][j] != NUL; ++j)
6685 {
6686 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006687 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006688 ++len;
6689 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006690#endif
6691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 command = alloc(len);
6693 if (command == NULL)
6694 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006695 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 vim_free(tempname);
6697 return FAIL;
6698 }
6699
6700 /*
6701 * Build the shell command:
6702 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6703 * recognizes this).
6704 * - Add the shell command to print the expanded names.
6705 * - Add the temp file name.
6706 * - Add the file name patterns.
6707 */
6708 if (shell_style == STYLE_BT)
6709 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006710 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006711 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006712 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006714 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006715 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006717 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006719 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 *p = ' ';
6721 }
6722 STRCAT(command, ">");
6723 }
6724 else
6725 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006726 STRCPY(command, "");
6727 if (shell_style == STYLE_GLOB)
6728 {
6729 // Assume the nonomatch option is valid only for csh like shells,
6730 // otherwise, this may set the positional parameters for the shell,
6731 // e.g. "$*".
6732 if (flags & EW_NOTFOUND)
6733 STRCAT(command, "set nonomatch; ");
6734 else
6735 STRCAT(command, "unset nonomatch; ");
6736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 if (shell_style == STYLE_GLOB)
6738 STRCAT(command, "glob >");
6739 else if (shell_style == STYLE_PRINT)
6740 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006741 else if (shell_style == STYLE_VIMGLOB)
6742 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 else
6744 STRCAT(command, "echo >");
6745 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006746
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006748
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 if (shell_style != STYLE_BT)
6750 for (i = 0; i < num_pat; ++i)
6751 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006752 // When using system() always add extra quotes, because the shell
6753 // is started twice. Otherwise put a backslash before special
6754 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755#ifdef USE_SYSTEM
6756 STRCAT(command, " \"");
6757 STRCAT(command, pat[i]);
6758 STRCAT(command, "\"");
6759#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006760 int intick = FALSE;
6761
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 p = command + STRLEN(command);
6763 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006764 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006765 {
6766 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006767 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006768 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6769 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006770 // Remove a backslash, take char literally. But keep
6771 // backslash inside backticks, before a special character
6772 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006773 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006774 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6775 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006776 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006777 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006778 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006779 else if (!intick
6780 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6781 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006782 // Put a backslash before a special character, but not
6783 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6784 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006785 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006786
Bram Moolenaar0f873732019-12-05 20:28:46 +01006787 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006788 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 *p = NUL;
6791#endif
6792 }
6793 if (flags & EW_SILENT)
6794 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006795 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006796 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
6798 /*
6799 * Using zsh -G: If a pattern has no matches, it is just deleted from
6800 * the argument list, otherwise zsh gives an error message and doesn't
6801 * expand any other pattern.
6802 */
6803 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006804 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
6806 /*
6807 * If we use -f then shell variables set in .cshrc won't get expanded.
6808 * vi can do it, so we will too, but it is only necessary if there is a "$"
6809 * in one of the patterns, otherwise we can still use the fast option.
6810 */
6811 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006812 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813
6814 /*
6815 * execute the shell command
6816 */
6817 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6818
Bram Moolenaar0f873732019-12-05 20:28:46 +01006819 // When running in the background, give it some time to create the temp
6820 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006821 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006822 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823
Bram Moolenaar0f873732019-12-05 20:28:46 +01006824 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 show_shell_mess = TRUE;
6826 vim_free(command);
6827
Bram Moolenaar0f873732019-12-05 20:28:46 +01006828 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 {
6830 mch_remove(tempname);
6831 vim_free(tempname);
6832 /*
6833 * With interactive completion, the error message is not printed.
6834 * However with USE_SYSTEM, I don't know how to turn off error messages
6835 * from the shell, so screen may still get messed up -- webb.
6836 */
6837#ifndef USE_SYSTEM
6838 if (!(flags & EW_SILENT))
6839#endif
6840 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006841 redraw_later_clear(); // probably messed up screen
6842 msg_putchar('\n'); // clear bottom line quickly
6843 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844#ifdef USE_SYSTEM
6845 if (!(flags & EW_SILENT))
6846#endif
6847 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006848 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006849 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 }
6851 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006852 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6853 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 if (shell_style == STYLE_BT)
6855 return FAIL;
6856 goto notfound;
6857 }
6858
6859 /*
6860 * read the names from the file into memory
6861 */
6862 fd = fopen((char *)tempname, READBIN);
6863 if (fd == NULL)
6864 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006865 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 if (!(flags & EW_SILENT))
6867 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006868 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006869 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 }
6871 vim_free(tempname);
6872 goto notfound;
6873 }
6874 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006875 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006877 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006878 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006879 buffer = NULL;
6880 else
6881 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 if (buffer == NULL)
6883 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006884 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 mch_remove(tempname);
6886 vim_free(tempname);
6887 fclose(fd);
6888 return FAIL;
6889 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006890 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 i = fread((char *)buffer, 1, len, fd);
6892 fclose(fd);
6893 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006894 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006896 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006897 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 vim_free(tempname);
6899 vim_free(buffer);
6900 return FAIL;
6901 }
6902 vim_free(tempname);
6903
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006904# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006905 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006907 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6909 *p++ = buffer[i];
6910 len = p - buffer;
6911# endif
6912
6913
Bram Moolenaar0f873732019-12-05 20:28:46 +01006914 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 if (shell_style == STYLE_ECHO)
6916 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006917 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006919 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 {
6921 while (*p != ' ' && *p != '\n')
6922 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006923 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 }
6925 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006926 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006927 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006929 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006931 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 {
6933 while (*p != '\n' && *p != NUL)
6934 ++p;
6935 if (*p != NUL)
6936 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006937 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 }
6939 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006940 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 else
6942 {
6943 /*
6944 * Some versions of zsh use spaces instead of NULs to separate
6945 * results. Only do this when there is no NUL before the end of the
6946 * buffer, otherwise we would never be able to use file names with
6947 * embedded spaces when zsh does use NULs.
6948 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6949 * don't check for spaces again.
6950 */
6951 check_spaces = FALSE;
6952 if (shell_style == STYLE_PRINT && !did_find_nul)
6953 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006954 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006955 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006956 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 did_find_nul = TRUE;
6958 else
6959 check_spaces = TRUE;
6960 }
6961
6962 /*
6963 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6964 * already is one, for STYLE_GLOB it needs to be added.
6965 */
6966 if (len && buffer[len - 1] == NUL)
6967 --len;
6968 else
6969 buffer[len] = NUL;
6970 i = 0;
6971 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006972 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {
6974 ++i;
6975 *p = NUL;
6976 }
6977 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006978 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 }
6980 if (i == 0)
6981 {
6982 /*
6983 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6984 * /bin/sh will happily expand it to nothing rather than returning an
6985 * error; and hey, it's good to check anyway -- webb.
6986 */
6987 vim_free(buffer);
6988 goto notfound;
6989 }
6990 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006991 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (*file == NULL)
6993 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006994 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 vim_free(buffer);
6996 return FAIL;
6997 }
6998
6999 /*
7000 * Isolate the individual file names.
7001 */
7002 p = buffer;
7003 for (i = 0; i < *num_file; ++i)
7004 {
7005 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007006 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00007007 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
7008 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00007010 while (!(shell_style == STYLE_ECHO && *p == ' ')
7011 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007013 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 *p = NUL;
7015 else
7016 {
7017 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007018 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 }
7020 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007021 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007023 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007025 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 }
7027 }
7028
7029 /*
7030 * Move the file names to allocated memory.
7031 */
7032 for (j = 0, i = 0; i < *num_file; ++i)
7033 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007034 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7036 continue;
7037
Bram Moolenaar0f873732019-12-05 20:28:46 +01007038 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 dir = (mch_isdir((*file)[i]));
7040 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7041 continue;
7042
Bram Moolenaar0f873732019-12-05 20:28:46 +01007043 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007044 if (!dir && (flags & EW_EXEC)
7045 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007046 continue;
7047
Bram Moolenaar964b3742019-05-24 18:54:09 +02007048 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 if (p)
7050 {
7051 STRCPY(p, (*file)[i]);
7052 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007053 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 (*file)[j++] = p;
7055 }
7056 }
7057 vim_free(buffer);
7058 *num_file = j;
7059
Bram Moolenaar0f873732019-12-05 20:28:46 +01007060 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007062 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 goto notfound;
7064 }
7065
7066 return OK;
7067
7068notfound:
7069 if (flags & EW_NOTFOUND)
7070 return save_patterns(num_pat, pat, num_file, file);
7071 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072}
7073
Bram Moolenaar0f873732019-12-05 20:28:46 +01007074#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007077save_patterns(
7078 int num_pat,
7079 char_u **pat,
7080 int *num_file,
7081 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082{
7083 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007084 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007086 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 if (*file == NULL)
7088 return FAIL;
7089 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007090 {
7091 s = vim_strsave(pat[i]);
7092 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007093 // Be compatible with expand_filename(): halve the number of
7094 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007095 backslash_halve(s);
7096 (*file)[i] = s;
7097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 *num_file = num_pat;
7099 return OK;
7100}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102/*
7103 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7104 * expand.
7105 */
7106 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007107mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007109 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 if (*p == '\\' && p[1] != NUL)
7112 ++p;
7113 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 if (vim_strchr((char_u *)
7115#ifdef VMS
7116 "*?%"
7117#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119#endif
7120 , *p) != NULL)
7121 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 }
7123 return FALSE;
7124}
7125
7126/*
7127 * Return TRUE if the string "p" contains a wildcard.
7128 * Don't recognize '~' at the end as a wildcard.
7129 */
7130 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007131mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007133 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 if (*p == '\\' && p[1] != NUL)
7136 ++p;
7137 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 if (vim_strchr((char_u *)
7139#ifdef VMS
7140 "*?%$"
7141#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143#endif
7144 , *p) != NULL
7145 || (*p == '~' && p[1] != NUL))
7146 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 }
7148 return FALSE;
7149}
7150
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007152have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
7154 int i;
7155
7156 for (i = 0; i < num; i++)
7157 if (mch_has_wildcard(file[i]))
7158 return 1;
7159 return 0;
7160}
7161
7162 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007163have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164{
7165 int i;
7166
7167 for (i = 0; i < num; i++)
7168 if (vim_strchr(file[i], '$') != NULL)
7169 return TRUE;
7170 return FALSE;
7171}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007173#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174/*
7175 * Scaled-down version of rename(), which is missing in Xenix.
7176 * This version can only move regular files and will fail if the
7177 * destination exists.
7178 */
7179 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007180mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181{
7182 struct stat st;
7183
Bram Moolenaar0f873732019-12-05 20:28:46 +01007184 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007186 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007188 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 return 0;
7190 return -1;
7191}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007192#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007194#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007195# if defined(DYNAMIC_GPM) || defined(PROTO)
7196/*
7197 * Initialize Gpm's symbols for dynamic linking.
7198 * Must be called only if libgpm_hinst is NULL.
7199 */
7200 static int
7201load_libgpm(void)
7202{
7203 libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL);
7204
7205 if (libgpm_hinst == NULL)
7206 {
7207 if (p_verbose > 0)
7208 smsg_attr(HL_ATTR(HLF_W),
7209 _("Could not load gpm library: %s"), dlerror());
7210 return FAIL;
7211 }
7212
7213 if (
7214 (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL
7215 || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL
7216 || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL
7217 || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL
7218 || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL
7219 )
7220 {
7221 semsg(_(e_could_not_load_library_str_str), "gpm", dlerror());
7222 dlclose(libgpm_hinst);
7223 libgpm_hinst = NULL;
7224 dll_gpm_flag = NULL;
7225 dll_gpm_fd = NULL;
7226 return FAIL;
7227 }
7228 return OK;
7229}
7230
7231 int
7232gpm_available(void)
7233{
7234 return libgpm_hinst != NULL || load_libgpm() == OK;
7235}
7236# endif // DYNAMIC_GPM
7237
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238/*
7239 * Initializes connection with gpm (if it isn't already opened)
7240 * Return 1 if succeeded (or connection already opened), 0 if failed
7241 */
7242 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007243gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007245 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007247#ifdef DYNAMIC_GPM
7248 if (!gpm_available())
7249 return 0;
7250#endif
7251
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 if (!gpm_flag)
7253 {
7254 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7255 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007256 // Default handling for mouse move
7257 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 gpm_connect.maxMod = 0xffff;
7259 if (Gpm_Open(&gpm_connect, 0) > 0)
7260 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007261 // gpm library tries to handling TSTP causes
7262 // problems. Anyways, we close connection to Gpm whenever
7263 // we are going to suspend or starting an external process
7264 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007265# ifdef SIGTSTP
dbivolaruab16ad32021-12-29 19:41:47 +00007266 signal(SIGTSTP, restricted ? SIG_IGN : (RETSIGTYPE (*)())sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007267# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007268 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 }
7270 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007271 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 return 0;
7273 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007274 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275}
7276
7277/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007278 * Returns TRUE if the GPM mouse is enabled.
7279 */
7280 int
7281gpm_enabled(void)
7282{
7283 return gpm_flag && gpm_fd >= 0;
7284}
7285
7286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 */
7289 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007290gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007292 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 Gpm_Close();
7294}
7295
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007296/*
7297 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007299 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 */
7301 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007302mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
7304 int button;
7305 static Gpm_Event gpm_event;
7306 char_u string[6];
7307 int_u vim_modifiers;
7308 int row,col;
7309 unsigned char buttons_mask;
7310 unsigned char gpm_modifiers;
7311 static unsigned char old_buttons = 0;
7312
7313 Gpm_GetEvent(&gpm_event);
7314
7315#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007316 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 if (hold_gui_events)
7318 return 0;
7319#endif
7320
7321 row = gpm_event.y - 1;
7322 col = gpm_event.x - 1;
7323
Bram Moolenaar0f873732019-12-05 20:28:46 +01007324 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 string[1] = 'M';
7326 string[2] = 'G';
7327 switch (GPM_BARE_EVENTS(gpm_event.type))
7328 {
7329 case GPM_DRAG:
7330 string[3] = MOUSE_DRAG;
7331 break;
7332 case GPM_DOWN:
7333 buttons_mask = gpm_event.buttons & ~old_buttons;
7334 old_buttons = gpm_event.buttons;
7335 switch (buttons_mask)
7336 {
7337 case GPM_B_LEFT:
7338 button = MOUSE_LEFT;
7339 break;
7340 case GPM_B_MIDDLE:
7341 button = MOUSE_MIDDLE;
7342 break;
7343 case GPM_B_RIGHT:
7344 button = MOUSE_RIGHT;
7345 break;
7346 default:
7347 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007348 // Don't know what to do. Can more than one button be
7349 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 }
7351 string[3] = (char_u)(button | 0x20);
7352 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7353 break;
7354 case GPM_UP:
7355 string[3] = MOUSE_RELEASE;
7356 old_buttons &= ~gpm_event.buttons;
7357 break;
7358 default:
7359 return 0;
7360 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007361 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 gpm_modifiers = gpm_event.modifiers;
7363 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007364 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7365 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7366 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7368 vim_modifiers |= MOUSE_SHIFT;
7369
7370 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7371 vim_modifiers |= MOUSE_CTRL;
7372 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7373 vim_modifiers |= MOUSE_ALT;
7374 string[3] |= vim_modifiers;
7375 string[4] = (char_u)(col + ' ' + 1);
7376 string[5] = (char_u)(row + ' ' + 1);
7377 add_to_input_buf(string, 6);
7378 return 6;
7379}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007380#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007382#ifdef FEAT_SYSMOUSE
7383/*
7384 * Initialize connection with sysmouse.
7385 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7386 * output, any sysmouse output than will be processed via sig_sysmouse().
7387 * Return OK if succeeded, FAIL if failed.
7388 */
7389 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007390sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007391{
7392 struct mouse_info mouse;
7393
7394 mouse.operation = MOUSE_MODE;
7395 mouse.u.mode.mode = 0;
7396 mouse.u.mode.signal = SIGUSR2;
7397 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7398 {
7399 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7400 mouse.operation = MOUSE_SHOW;
7401 ioctl(1, CONS_MOUSECTL, &mouse);
7402 return OK;
7403 }
7404 return FAIL;
7405}
7406
7407/*
7408 * Stop processing SIGUSR2 signals, and also make sure that
7409 * virtual console do not send us any sysmouse related signal.
7410 */
7411 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007412sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007413{
7414 struct mouse_info mouse;
7415
7416 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7417 mouse.operation = MOUSE_MODE;
7418 mouse.u.mode.mode = 0;
7419 mouse.u.mode.signal = 0;
7420 ioctl(1, CONS_MOUSECTL, &mouse);
7421}
7422
7423/*
7424 * Gets info from sysmouse and adds special keys to input buf.
7425 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007426 static RETSIGTYPE
7427sig_sysmouse SIGDEFARG(sigarg)
7428{
7429 struct mouse_info mouse;
7430 struct video_info video;
7431 char_u string[6];
7432 int row, col;
7433 int button;
7434 int buttons;
7435 static int oldbuttons = 0;
7436
7437#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007438 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007439 if (hold_gui_events)
7440 return;
7441#endif
7442
7443 mouse.operation = MOUSE_GETINFO;
7444 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7445 && ioctl(1, FBIO_MODEINFO, &video) != -1
7446 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7447 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7448 {
7449 row = mouse.u.data.y / video.vi_cheight;
7450 col = mouse.u.data.x / video.vi_cwidth;
7451 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007452 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007453 string[1] = 'M';
7454 string[2] = 'S';
7455 if (oldbuttons == buttons && buttons != 0)
7456 {
7457 button = MOUSE_DRAG;
7458 }
7459 else
7460 {
7461 switch (buttons)
7462 {
7463 case 0:
7464 button = MOUSE_RELEASE;
7465 break;
7466 case 1:
7467 button = MOUSE_LEFT;
7468 break;
7469 case 2:
7470 button = MOUSE_MIDDLE;
7471 break;
7472 case 4:
7473 button = MOUSE_RIGHT;
7474 break;
7475 default:
7476 return;
7477 }
7478 oldbuttons = buttons;
7479 }
7480 string[3] = (char_u)(button);
7481 string[4] = (char_u)(col + ' ' + 1);
7482 string[5] = (char_u)(row + ' ' + 1);
7483 add_to_input_buf(string, 6);
7484 }
7485 return;
7486}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007487#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007488
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007490typedef char_u * (*STRPROCSTR)(char_u *);
7491typedef char_u * (*INTPROCSTR)(int);
7492typedef int (*STRPROCINT)(char_u *);
7493typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494
7495/*
7496 * Call a DLL routine which takes either a string or int param
7497 * and returns an allocated string.
7498 */
7499 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007500mch_libcall(
7501 char_u *libname,
7502 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007503 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007504 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007505 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007506 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507{
7508# if defined(USE_DLOPEN)
7509 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007510 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511# else
7512 shl_t hinstLib;
7513# endif
7514 STRPROCSTR ProcAdd;
7515 INTPROCSTR ProcAddI;
7516 char_u *retval_str = NULL;
7517 int retval_int = 0;
7518 int success = FALSE;
7519
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007520 /*
7521 * Get a handle to the DLL module.
7522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007524 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007525 (void)dlerror();
7526
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 hinstLib = dlopen((char *)libname, RTLD_LAZY
7528# ifdef RTLD_LOCAL
7529 | RTLD_LOCAL
7530# endif
7531 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007532 if (hinstLib == NULL)
7533 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007534 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007535 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007536 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007537 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539# else
7540 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7541# endif
7542
Bram Moolenaar0f873732019-12-05 20:28:46 +01007543 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 if (hinstLib != NULL)
7545 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007546# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 /*
7548 * Catch a crash when calling the library function. For example when
7549 * using a number where a string pointer is expected.
7550 */
7551 mch_startjmp();
7552 if (SETJMP(lc_jump_env) != 0)
7553 {
7554 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007555# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007556 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 mch_didjmp();
7559 }
7560 else
7561# endif
7562 {
7563 retval_str = NULL;
7564 retval_int = 0;
7565
7566 if (argstring != NULL)
7567 {
7568# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007569 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007570 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571# else
7572 if (shl_findsym(&hinstLib, (const char *)funcname,
7573 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7574 ProcAdd = NULL;
7575# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007576 if ((success = (ProcAdd != NULL
7577# if defined(USE_DLOPEN)
7578 && dlerr == NULL
7579# endif
7580 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 {
7582 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007583 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 else
7585 retval_str = (ProcAdd)(argstring);
7586 }
7587 }
7588 else
7589 {
7590# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007591 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007592 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593# else
7594 if (shl_findsym(&hinstLib, (const char *)funcname,
7595 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7596 ProcAddI = NULL;
7597# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007598 if ((success = (ProcAddI != NULL
7599# if defined(USE_DLOPEN)
7600 && dlerr == NULL
7601# endif
7602 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 {
7604 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007605 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 else
7607 retval_str = (ProcAddI)(argint);
7608 }
7609 }
7610
Bram Moolenaar0f873732019-12-05 20:28:46 +01007611 // Save the string before we free the library.
7612 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 if (string_result == NULL)
7614 *number_result = retval_int;
7615 else if (retval_str != NULL
7616 && retval_str != (char_u *)1
7617 && retval_str != (char_u *)-1)
7618 *string_result = vim_strsave(retval_str);
7619 }
7620
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007621# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 mch_endjmp();
7623# ifdef SIGHASARG
7624 if (lc_signal != 0)
7625 {
7626 int i;
7627
Bram Moolenaar0f873732019-12-05 20:28:46 +01007628 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 for (i = 0; signal_info[i].sig != -1; i++)
7630 if (lc_signal == signal_info[i].sig)
7631 break;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00007632 semsg(e_got_sig_str_in_libcall, signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 }
7634# endif
7635# endif
7636
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007638 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007639 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007640 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007641
Bram Moolenaar0f873732019-12-05 20:28:46 +01007642 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 (void)dlclose(hinstLib);
7644# else
7645 (void)shl_unload(hinstLib);
7646# endif
7647 }
7648
7649 if (!success)
7650 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007651 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 return FAIL;
7653 }
7654
7655 return OK;
7656}
7657#endif
7658
7659#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007660static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661static int xterm_button;
7662
7663/*
7664 * Setup a dummy window for X selections in a terminal.
7665 */
7666 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007667setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668{
7669 int z = 0;
7670 char *strp = "";
7671 Widget AppShell;
7672
7673 if (!x_connect_to_server())
7674 return;
7675
7676 open_app_context();
7677 if (app_context != NULL && xterm_Shell == (Widget)0)
7678 {
7679 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007680# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007682# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007683# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007684 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685
7686 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007687 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688# endif
7689
Bram Moolenaar0f873732019-12-05 20:28:46 +01007690 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 oldhandler = XSetErrorHandler(x_error_check);
7692
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007693# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007694 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7696 mch_startjmp();
7697 if (SETJMP(lc_jump_env) != 0)
7698 {
7699 mch_didjmp();
7700 xterm_dpy = NULL;
7701 }
7702 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 {
7705 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7706 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007707 if (xterm_dpy != NULL)
7708 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007709# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007711# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 }
7713
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007714# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007715 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007717# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007718 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 (void)XSetErrorHandler(oldhandler);
7720
7721 if (xterm_dpy == NULL)
7722 {
7723 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007724 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 return;
7726 }
7727
Bram Moolenaar0f873732019-12-05 20:28:46 +01007728 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 (void)XSetIOErrorHandler(x_IOerror_handler);
7730
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007731# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007733 {
7734 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007735 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007736 verbose_leave();
7737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738# endif
7739
Bram Moolenaar0f873732019-12-05 20:28:46 +01007740 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7742 applicationShellWidgetClass, xterm_dpy,
7743 NULL);
7744 if (AppShell == (Widget)0)
7745 return;
7746 xterm_Shell = XtVaCreatePopupShell("VIM",
7747 topLevelShellWidgetClass, AppShell,
7748 XtNmappedWhenManaged, 0,
7749 XtNwidth, 1,
7750 XtNheight, 1,
7751 NULL);
7752 if (xterm_Shell == (Widget)0)
7753 return;
7754
7755 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007756 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 if (x11_display == NULL)
7758 x11_display = xterm_dpy;
7759
7760 XtRealizeWidget(xterm_Shell);
7761 XSync(xterm_dpy, False);
7762 xterm_update();
7763 }
7764 if (xterm_Shell != (Widget)0)
7765 {
7766 clip_init(TRUE);
7767 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7768 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007769 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 if (test_x11_window(xterm_dpy) == FAIL)
7771 x11_window = 0;
7772 if (x11_window != 0)
7773 xterm_trace = 0;
7774 }
7775}
7776
7777 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007778start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7781 return;
7782 xterm_trace = 1;
7783 xterm_button = button;
7784 do_xterm_trace();
7785}
7786
7787
7788 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007789stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790{
7791 if (xterm_trace < 0)
7792 return;
7793 xterm_trace = 0;
7794}
7795
7796/*
7797 * Query the xterm pointer and generate mouse termcodes if necessary
7798 * return TRUE if dragging is active, else FALSE
7799 */
7800 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007801do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802{
7803 Window root, child;
7804 int root_x, root_y;
7805 int win_x, win_y;
7806 int row, col;
7807 int_u mask_return;
7808 char_u buf[50];
7809 char_u *strp;
7810 long got_hints;
7811 static char_u *mouse_code;
7812 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7813 static int prev_row = 0, prev_col = 0;
7814 static XSizeHints xterm_hints;
7815
7816 if (xterm_trace <= 0)
7817 return FALSE;
7818
7819 if (xterm_trace == 1)
7820 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007821 // Get the hints just before tracking starts. The font size might
7822 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007823 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7824 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 || xterm_hints.width_inc <= 1
7826 || xterm_hints.height_inc <= 1)
7827 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007828 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 return FALSE;
7830 }
7831
Bram Moolenaar0f873732019-12-05 20:28:46 +01007832 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 mouse_code = find_termcode(mouse_name);
7834 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007835 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 xterm_trace = 2;
7837
Bram Moolenaar0f873732019-12-05 20:28:46 +01007838 // Find the offset of the chars, there might be a scrollbar on the
7839 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7841 &win_x, &win_y, &mask_return);
7842 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7843 - (xterm_hints.height_inc / 2);
7844 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7845 xterm_hints.y = 2;
7846 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7847 - (xterm_hints.width_inc / 2);
7848 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7849 xterm_hints.x = 2;
7850 return TRUE;
7851 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007852 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {
7854 xterm_trace = 0;
7855 return FALSE;
7856 }
7857
7858 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7859 &win_x, &win_y, &mask_return);
7860
7861 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7862 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7863 if (row == prev_row && col == prev_col)
7864 return TRUE;
7865
7866 STRCPY(buf, mouse_code);
7867 strp = buf + STRLEN(buf);
7868 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7869 *strp++ = (char_u)(col + ' ' + 1);
7870 *strp++ = (char_u)(row + ' ' + 1);
7871 *strp = 0;
7872 add_to_input_buf(buf, STRLEN(buf));
7873
7874 prev_row = row;
7875 prev_col = col;
7876 return TRUE;
7877}
7878
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007879# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880/*
7881 * Destroy the display, window and app_context. Required for GTK.
7882 */
7883 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007884clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885{
7886 if (xterm_Shell != (Widget)0)
7887 {
7888 XtDestroyWidget(xterm_Shell);
7889 xterm_Shell = (Widget)0;
7890 }
7891 if (xterm_dpy != NULL)
7892 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007893# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007894 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007896# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 if (x11_display == xterm_dpy)
7898 x11_display = NULL;
7899 xterm_dpy = NULL;
7900 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007901# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 if (app_context != (XtAppContext)NULL)
7903 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007904 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 XtDestroyApplicationContext(app_context);
7906 app_context = (XtAppContext)NULL;
7907 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909}
7910# endif
7911
7912/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007913 * Catch up with GUI or X events.
7914 */
7915 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007916clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007917{
7918# ifdef FEAT_GUI
7919 if (gui.in_use)
7920 gui_mch_update();
7921 else
7922# endif
7923 if (xterm_Shell != (Widget)0)
7924 xterm_update();
7925}
7926
7927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 * Catch up with any queued X events. This may put keyboard input into the
7929 * input buffer, call resize call-backs, trigger timers etc. If there is
7930 * nothing in the X event queue (& no timers pending), then we return
7931 * immediately.
7932 */
7933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007934xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
7936 XEvent event;
7937
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007938 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007940 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007942 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007943 break;
7944
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007945 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007946 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007947 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007948 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007949#ifdef FEAT_CLIENTSERVER
7950 {
7951 XPropertyEvent *e = (XPropertyEvent *)&event;
7952
7953 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007955 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007958 XtDispatchEvent(&event);
7959 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007960 else
7961 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007962 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007963 XtAppProcessEvent(app_context, mask);
7964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 }
7966}
7967
7968 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007969clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970{
7971 if (xterm_Shell != (Widget)0)
7972 return clip_x11_own_selection(xterm_Shell, cbd);
7973 return FAIL;
7974}
7975
7976 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007977clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
7979 if (xterm_Shell != (Widget)0)
7980 clip_x11_lose_selection(xterm_Shell, cbd);
7981}
7982
7983 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007984clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
7986 if (xterm_Shell != (Widget)0)
7987 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7988}
7989
7990 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007991clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992{
7993 clip_x11_set_selection(cbd);
7994}
7995#endif
7996
7997
7998#if defined(USE_XSMP) || defined(PROTO)
7999/*
8000 * Code for X Session Management Protocol.
8001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002
8003# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004/*
8005 * This is our chance to ask the user if they want to save,
8006 * or abort the logout
8007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008009xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010{
Bram Moolenaare1004402020-10-24 20:49:43 +02008011 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 int cancel_shutdown = False;
8013
Bram Moolenaare1004402020-10-24 20:49:43 +02008014 save_cmod_flags = cmdmod.cmod_flags;
8015 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01008016 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01008017 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02008019 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01008020 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 out_flush();
8022
Bram Moolenaar0f873732019-12-05 20:28:46 +01008023 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 SmcInteractDone(smc_conn, cancel_shutdown);
8025
Bram Moolenaar0f873732019-12-05 20:28:46 +01008026 // Finish off
8027 // Only end save-yourself here if we're not cancelling shutdown;
8028 // we'll get a cancelled callback later in which we'll end it.
8029 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 if (!cancel_shutdown)
8031 {
8032 xsmp.save_yourself = False;
8033 SmcSaveYourselfDone(smc_conn, True);
8034 }
8035}
8036# endif
8037
8038/*
8039 * Callback that starts save-yourself.
8040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008042xsmp_handle_save_yourself(
8043 SmcConn smc_conn,
8044 SmPointer client_data UNUSED,
8045 int save_type UNUSED,
8046 Bool shutdown,
8047 int interact_style UNUSED,
8048 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008050 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 if (xsmp.save_yourself)
8052 SmcSaveYourselfDone(smc_conn, True);
8053 xsmp.save_yourself = True;
8054 xsmp.shutdown = shutdown;
8055
Bram Moolenaar0f873732019-12-05 20:28:46 +01008056 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01008058 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059
8060 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008061 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062
8063# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008064 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008066 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 SmcInteractRequest(smc_conn, SmDialogError,
8068 xsmp_handle_interaction, client_data);
8069 else
8070# endif
8071 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008072 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 SmcSaveYourselfDone(smc_conn, True);
8074 xsmp.save_yourself = False;
8075 }
8076}
8077
8078
8079/*
8080 * Callback to warn us of imminent death.
8081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008083xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 xsmp_close();
8086
Bram Moolenaar0f873732019-12-05 20:28:46 +01008087 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 getout_preserve_modified(0);
8089}
8090
8091
8092/*
8093 * Callback to tell us that save-yourself has completed.
8094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008096xsmp_save_complete(
8097 SmcConn smc_conn UNUSED,
8098 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099{
8100 xsmp.save_yourself = False;
8101}
8102
8103
8104/*
8105 * Callback to tell us that an instigated shutdown was cancelled
8106 * (maybe even by us)
8107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008109xsmp_shutdown_cancelled(
8110 SmcConn smc_conn,
8111 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112{
8113 if (xsmp.save_yourself)
8114 SmcSaveYourselfDone(smc_conn, True);
8115 xsmp.save_yourself = False;
8116 xsmp.shutdown = False;
8117}
8118
8119
8120/*
8121 * Callback to tell us that a new ICE connection has been established.
8122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008124xsmp_ice_connection(
8125 IceConn iceConn,
8126 IcePointer clientData UNUSED,
8127 Bool opening,
8128 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008130 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 if (opening)
8132 {
8133 xsmp_icefd = IceConnectionNumber(iceConn);
8134 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8135 }
8136}
8137
8138
Bram Moolenaar0f873732019-12-05 20:28:46 +01008139// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008141xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142{
8143 Bool rep;
8144
8145 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8146 == IceProcessMessagesIOError)
8147 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008148 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008150 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 xsmp_close();
8152 return FAIL;
8153 }
8154 else
8155 return OK;
8156}
8157
8158static int dummy;
8159
Bram Moolenaar0f873732019-12-05 20:28:46 +01008160// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 void
8162xsmp_init(void)
8163{
8164 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 SmcCallbacks smcallbacks;
8166#if 0
8167 SmPropValue smname;
8168 SmProp smnameprop;
8169 SmProp *smprops[1];
8170#endif
8171
8172 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008173 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174
8175 xsmp.save_yourself = xsmp.shutdown = False;
8176
Bram Moolenaar0f873732019-12-05 20:28:46 +01008177 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8179 smcallbacks.save_yourself.client_data = NULL;
8180 smcallbacks.die.callback = xsmp_die;
8181 smcallbacks.die.client_data = NULL;
8182 smcallbacks.save_complete.callback = xsmp_save_complete;
8183 smcallbacks.save_complete.client_data = NULL;
8184 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8185 smcallbacks.shutdown_cancelled.client_data = NULL;
8186
Bram Moolenaar0f873732019-12-05 20:28:46 +01008187 // Set up a watch on ICE connection creations. The "dummy" argument is
8188 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8190 {
8191 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008192 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 return;
8194 }
8195
Bram Moolenaar0f873732019-12-05 20:28:46 +01008196 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 xsmp.smcconn = SmcOpenConnection(
8198 NULL,
8199 NULL,
8200 SmProtoMajor,
8201 SmProtoMinor,
8202 SmcSaveYourselfProcMask | SmcDieProcMask
8203 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8204 &smcallbacks,
8205 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008206 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008207 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 errorstring);
8209 if (xsmp.smcconn == NULL)
8210 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008212 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008213 char errorreport[132];
8214
8215 // If the message is too long it might not be NUL terminated. Add
8216 // a NUL at the end to make sure we don't go over the end.
8217 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008218 vim_snprintf(errorreport, sizeof(errorreport),
8219 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008220 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 return;
8223 }
8224 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8225
8226#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008227 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 smname.value = "vim";
8229 smname.length = 3;
8230 smnameprop.name = "SmProgram";
8231 smnameprop.type = "SmARRAY8";
8232 smnameprop.num_vals = 1;
8233 smnameprop.vals = &smname;
8234
8235 smprops[0] = &smnameprop;
8236 SmcSetProperties(xsmp.smcconn, 1, smprops);
8237#endif
8238}
8239
8240
Bram Moolenaar0f873732019-12-05 20:28:46 +01008241// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008243xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244{
8245 if (xsmp_icefd != -1)
8246 {
8247 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008248 if (xsmp.clientid != NULL)
8249 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008250 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 xsmp_icefd = -1;
8252 }
8253}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008254#endif // USE_XSMP