blob: b23adb710f69e8e2227d11ea48e849c912b9f96d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * OS/2 port by Paul Slootman
5 * VMS merge by Zoltan Arpadffy
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code.
10 */
11
12/*
13 * os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
14 * Also for OS/2, using the excellent EMX package!!!
Bram Moolenaar041c7102020-05-30 18:14:57 +020015 * Also for Atari MiNT.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016 *
17 * A lot of this file was originally written by Juergen Weigert and later
18 * changed beyond recognition.
19 */
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#include "vim.h"
22
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023#ifdef FEAT_MZSCHEME
24# include "if_mzsch.h"
25#endif
26
Bram Moolenaar0f873732019-12-05 20:28:46 +010027#include "os_unixx.h" // unix includes for os_unix.c only
Bram Moolenaar071d4272004-06-13 20:20:40 +000028
29#ifdef USE_XSMP
30# include <X11/SM/SMlib.h>
31#endif
32
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000033#ifdef HAVE_SELINUX
34# include <selinux/selinux.h>
35static int selinux_enabled = -1;
36#endif
37
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020038#ifdef HAVE_SMACK
39# include <attr/xattr.h>
40# include <linux/xattr.h>
41# ifndef SMACK_LABEL_LEN
42# define SMACK_LABEL_LEN 1024
43# endif
44#endif
45
Bram Moolenaara2442432007-04-26 14:26:37 +000046#ifdef __CYGWIN__
K.Takata972db232022-02-04 10:45:38 +000047# include <cygwin/version.h>
48# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
Bram Moolenaar0f873732019-12-05 20:28:46 +010049 // for cygwin_conv_path()
K.Takata972db232022-02-04 10:45:38 +000050# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
51# define WIN32_LEAN_AND_MEAN
52# include <windows.h>
53# include "winclip.pro"
Bram Moolenaara2442432007-04-26 14:26:37 +000054# endif
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000058
Bram Moolenaar071d4272004-06-13 20:20:40 +000059# include <gpm.h>
Bram Moolenaar33fc4a62022-02-23 18:07:38 +000060
61# ifdef DYNAMIC_GPM
62# define Gpm_Open (*dll_Gpm_Open)
63# define Gpm_Close (*dll_Gpm_Close)
64# define Gpm_GetEvent (*dll_Gpm_GetEvent)
65# define gpm_flag (dll_gpm_flag != NULL ? *dll_gpm_flag : 0)
66# define gpm_fd (dll_gpm_fd != NULL ? *dll_gpm_fd : -1)
67
68static int (*dll_Gpm_Open) (Gpm_Connect *, int);
69static int (*dll_Gpm_Close) (void);
70static int (*dll_Gpm_GetEvent) (Gpm_Event *);
71static int *dll_gpm_flag;
72static int *dll_gpm_fd;
73
74static void *libgpm_hinst;
75# endif
76
Bram Moolenaar0f873732019-12-05 20:28:46 +010077// <linux/keyboard.h> contains defines conflicting with "keymap.h",
78// I just copied relevant defines here. A cleaner solution would be to put gpm
79// code into separate file and include there linux/keyboard.h
80// #include <linux/keyboard.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000081# define KG_SHIFT 0
82# define KG_CTRL 2
83# define KG_ALT 3
84# define KG_ALTGR 1
85# define KG_SHIFTL 4
86# define KG_SHIFTR 5
87# define KG_CTRLL 6
88# define KG_CTRLR 7
89# define KG_CAPSSHIFT 8
90
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010091static void gpm_close(void);
92static int gpm_open(void);
93static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#endif
95
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000096#ifdef FEAT_SYSMOUSE
97# include <sys/consio.h>
98# include <sys/fbio.h>
99
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100100static int sysmouse_open(void);
101static void sysmouse_close(void);
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100102static void sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000103#endif
104
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105/*
106 * end of autoconf section. To be extended...
107 */
108
Bram Moolenaar0f873732019-12-05 20:28:46 +0100109// Are the following #ifdefs still required? And why? Is that for X11?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
111#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
112# ifdef SIGWINCH
113# undef SIGWINCH
114# endif
115# ifdef TIOCGWINSZ
116# undef TIOCGWINSZ
117# endif
118#endif
119
Bram Moolenaar0f873732019-12-05 20:28:46 +0100120#if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121# define SIGWINCH SIGWINDOW
122#endif
123
124#ifdef FEAT_X11
125# include <X11/Xlib.h>
126# include <X11/Xutil.h>
127# include <X11/Xatom.h>
128# ifdef FEAT_XCLIPBOARD
129# include <X11/Intrinsic.h>
130# include <X11/Shell.h>
131# include <X11/StringDefs.h>
132static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void clip_update(void);
134static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135# endif
136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137Window x11_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
140
Bram Moolenaar5c3128e2020-05-11 20:54:42 +0200141static int ignore_sigtstp = FALSE;
142
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200146static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +0200147static int unix_did_set_title = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static char_u *oldicon = NULL;
149static int did_set_icon = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
Bram Moolenaar205b8862011-09-07 15:04:31 +0200153#ifdef HAVE_UNION_WAIT
154typedef union wait waitstatus;
155#else
156typedef int waitstatus;
157#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200158static int WaitForChar(long msec, int *interrupted, int ignore_input);
159static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar041c7102020-05-30 18:14:57 +0200160#ifdef VMS
Bram Moolenaarcda77642016-06-04 13:32:35 +0200161int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200163static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#endif
165
166#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100167static int do_xterm_trace(void);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100168# define XT_TRACE_DELAY 50 // delay for xterm tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
170
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
173#if defined(SIGWINCH)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100174static void sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000176#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100177static void sig_tstp SIGPROTOARG;
dbivolaruab16ad32021-12-29 19:41:47 +0000178// 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 Moolenaar99c48fe2022-06-05 22:05:19 +0100182static void catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200184#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100185static void catch_sigusr1 SIGPROTOARG;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100188static void 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 Moolenaar99c48fe2022-06-05 22:05:19 +0100192static void 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 Moolenaar99c48fe2022-06-05 22:05:19 +0100196static void 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);
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100200static void catch_signals(void (*func_deadly)(), void (*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 Moolenaar99c48fe2022-06-05 22:05:19 +0100216# define SIG_ERR ((void (*)())-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 Moolenaar99c48fe2022-06-05 22:05:19 +0100867 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868sig_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 Moolenaar99c48fe2022-06-05 22:05:19 +0100871 signal(SIGWINCH, (void (*)())sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 do_resize = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873}
874#endif
875
dbivolaruab16ad32021-12-29 19:41:47 +0000876#if defined(SIGTSTP)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100877 static void
dbivolaruab16ad32021-12-29 19:41:47 +0000878sig_tstp SIGDEFARG(sigarg)
879{
880 // Second time we get called we actually need to suspend
881 if (in_mch_suspend)
882 {
883 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
884 raise(sigarg);
885 }
dbivolaru79a6e252022-01-23 16:41:14 +0000886 else
887 got_tstp = TRUE;
dbivolaruab16ad32021-12-29 19:41:47 +0000888
ichizok5f823d12022-03-13 17:27:38 +0000889#if !defined(__ANDROID__) && !defined(__OpenBSD__) && !defined(__DragonFly__)
890 // This is not required on all systems. On some systems (at least Android,
891 // OpenBSD, and DragonFlyBSD) this breaks suspending with CTRL-Z.
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100892 signal(SIGTSTP, (void (*)())sig_tstp);
xtkobacbef12e2022-02-27 12:31:52 +0000893#endif
dbivolaruab16ad32021-12-29 19:41:47 +0000894}
895#endif
896
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897#if defined(SIGINT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100898 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899catch_sigint SIGDEFARG(sigarg)
900{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100901 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100902 signal(SIGINT, (void (*)())catch_sigint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 got_int = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904}
905#endif
906
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200907#if defined(SIGUSR1)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100908 static void
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200909catch_sigusr1 SIGDEFARG(sigarg)
910{
911 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100912 signal(SIGUSR1, (void (*)())catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200913 got_sigusr1 = TRUE;
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200914}
915#endif
916
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917#if defined(SIGPWR)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100918 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919catch_sigpwr SIGDEFARG(sigarg)
920{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100921 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100922 signal(SIGPWR, (void (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 /*
924 * I'm not sure we get the SIGPWR signal when the system is really going
925 * down or when the batteries are almost empty. Just preserve the swap
926 * files and don't exit, that can't do any harm.
927 */
928 ml_sync_all(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929}
930#endif
931
932#ifdef SET_SIG_ALARM
933/*
934 * signal function for alarm().
935 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +0100936 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937sig_alarm SIGDEFARG(sigarg)
938{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100939 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 sig_alarm_called = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941}
942#endif
943
Bram Moolenaaredce7422019-01-20 18:39:30 +0100944#if (defined(HAVE_SETJMP_H) \
945 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
946 || defined(FEAT_LIBCALL))) \
947 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100948# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100949
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100950// argument to SETJMP()
951static JMP_BUF lc_jump_env;
952
953# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100954// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100955// Volatile because it is used in signal handlers.
956static volatile sig_atomic_t lc_signal;
957# endif
958
959// TRUE when lc_jump_env is valid.
960// Volatile because it is used in signal handler deathtrap().
961static volatile sig_atomic_t lc_active INIT(= FALSE);
962
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963/*
964 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100965 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100967 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 * Usage:
969 * mch_startjmp();
970 * if (SETJMP(lc_jump_env) != 0)
971 * {
972 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100973 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 * }
975 * else
976 * {
977 * do_the_work;
978 * mch_endjmp();
979 * }
980 * Note: Can't move SETJMP() here, because a function calling setjmp() must
981 * not return before the saved environment is used.
982 * Returns OK for normal return, FAIL when the protected code caused a
983 * problem and LONGJMP() was used.
984 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100985 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100986mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987{
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100988# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100990# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 lc_active = TRUE;
992}
993
Bram Moolenaar113e1072019-01-20 15:30:40 +0100994 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100995mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996{
997 lc_active = FALSE;
998}
999
Bram Moolenaar113e1072019-01-20 15:30:40 +01001000 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001001mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001004 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1005 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 init_signal_stack();
1007# endif
1008}
1009#endif
1010
1011/*
1012 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001013 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001015 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1016 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001018 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019deathtrap SIGDEFARG(sigarg)
1020{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001021 static int entered = 0; // count the number of times we got here.
1022 // Note: when memory has been corrupted
1023 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024#ifdef SIGHASARG
1025 int i;
1026#endif
1027
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001028#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 /*
1030 * Catch a crash in protected code.
1031 * Restores the environment saved in lc_jump_env, which looks like
1032 * SETJMP() returns 1.
1033 */
1034 if (lc_active)
1035 {
1036# if defined(SIGHASARG)
1037 lc_signal = sigarg;
1038# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001039 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001041 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 }
1043#endif
1044
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001045#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001046# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001047 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1048 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1049 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001050 if (in_mch_delay && sigarg == SIGQUIT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001051 return;
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001052# endif
1053
Bram Moolenaar0f873732019-12-05 20:28:46 +01001054 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1055 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1056 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001057 if (entered == 0
1058 && (0
1059# ifdef SIGHUP
1060 || sigarg == SIGHUP
1061# endif
1062# ifdef SIGQUIT
1063 || sigarg == SIGQUIT
1064# endif
1065# ifdef SIGTERM
1066 || sigarg == SIGTERM
1067# endif
1068# ifdef SIGPWR
1069 || sigarg == SIGPWR
1070# endif
1071# ifdef SIGUSR1
1072 || sigarg == SIGUSR1
1073# endif
1074# ifdef SIGUSR2
1075 || sigarg == SIGUSR2
1076# endif
1077 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001078 && !vim_handle_signal(sigarg))
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001079 return;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001080#endif
1081
Bram Moolenaar0f873732019-12-05 20:28:46 +01001082 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 ++entered;
1084
Bram Moolenaar0f873732019-12-05 20:28:46 +01001085 // Executing autocommands is likely to use more stack space than we have
1086 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001087 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001090 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 set_vim_var_nr(VV_DYING, (long)entered);
1092#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001093 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001095#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001096 // Since we are now using the signal stack, need to reset the stack
1097 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 get_stack_limit();
1099#endif
1100
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001101#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001102 // This is for opening gdb the moment Vim crashes.
1103 // You need to manually adjust the file name and Vim executable name.
1104 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001105 {
1106# define VI_GDB_FILE "/tmp/vimgdb"
1107# define VIM_NAME "/usr/bin/vim"
1108 FILE *fp = fopen(VI_GDB_FILE, "w");
1109 if (fp)
1110 {
1111 fprintf(fp,
1112 "file %s\n"
1113 "attach %d\n"
1114 "set height 1000\n"
1115 "bt full\n"
1116 , VIM_NAME, getpid());
1117 fclose(fp);
1118 system("xterm -e gdb -x "VI_GDB_FILE);
1119 unlink(VI_GDB_FILE);
1120 }
1121 }
1122#endif
1123
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001125 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 for (i = 0; signal_info[i].sig != -1; i++)
1127 if (sigarg == signal_info[i].sig)
1128 break;
1129 deadly_signal = sigarg;
1130#endif
1131
Bram Moolenaar0f873732019-12-05 20:28:46 +01001132 full_screen = FALSE; // don't write message to the GUI, it might be
1133 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 /*
1135 * If something goes wrong after entering here, we may get here again.
1136 * When this happens, give a message and try to exit nicely (resetting the
1137 * terminal mode, etc.)
1138 * When this happens twice, just exit, don't even try to give a message,
1139 * stack may be corrupt or something weird.
1140 * When this still happens again (or memory was corrupted in such a way
1141 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1142 */
1143 if (entered >= 3)
1144 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001145 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 may_core_dump();
1147 if (entered >= 4)
1148 _exit(8);
1149 exit(7);
1150 }
1151 if (entered == 2)
1152 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001153 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001154 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 out_flush();
1156 getout(1);
1157 }
1158
Bram Moolenaar0f873732019-12-05 20:28:46 +01001159 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001161 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 signal_info[i].name);
1163#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001164 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001166
Bram Moolenaar0f873732019-12-05 20:28:46 +01001167 // Preserve files and exit. This sets the really_exiting flag to prevent
1168 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001169 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
Bram Moolenaar0f873732019-12-05 20:28:46 +01001171 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001172
Bram Moolenaar009b2592004-10-24 19:18:58 +00001173#ifdef NBDEBUG
1174 reset_signals();
1175 may_core_dump();
1176 abort();
1177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178}
1179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001181 * Invoked after receiving SIGCONT. We don't know what happened while
1182 * sleeping, deal with part of that.
1183 */
1184 static void
1185after_sigcont(void)
1186{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001187 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1188 // again later.
1189 oldtitle_outdated = TRUE;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001190
Bram Moolenaar2e310482018-08-21 13:09:10 +02001191 settmode(TMODE_RAW);
1192 need_check_timestamps = TRUE;
1193 did_check_timestamps = FALSE;
1194}
1195
1196#if defined(SIGCONT)
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001197static void sigcont_handler SIGPROTOARG;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001198
1199/*
1200 * With multi-threading, suspending might not work immediately. Catch the
1201 * SIGCONT signal, which will be used as an indication whether the suspending
1202 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001203 *
1204 * On Linux, signal is not always handled immediately either.
1205 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001206 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001207 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001208 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001210static volatile sig_atomic_t sigcont_received;
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001211static void sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
1213/*
1214 * signal handler for SIGCONT
1215 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001216 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217sigcont_handler SIGDEFARG(sigarg)
1218{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001219 if (in_mch_suspend)
1220 {
1221 sigcont_received = TRUE;
1222 }
1223 else
1224 {
1225 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1226 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1227 // back to a sane mode. We should redraw, but we can't really do that
1228 // in a signal handler, do a redraw later.
1229 after_sigcont();
1230 redraw_later(CLEAR);
1231 cursor_on_force();
1232 out_flush();
1233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234}
1235#endif
1236
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001237#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001238# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001239static void *clip_star_save = NULL;
1240static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001241# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001242
1243/*
1244 * Called when Vim is going to sleep or execute a shell command.
1245 * We can't respond to requests for the X selections. Lose them, otherwise
1246 * other applications will hang. But first copy the text to cut buffer 0.
1247 */
1248 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001249loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001250{
1251 if (clip_star.owned || clip_plus.owned)
1252 {
1253 x11_export_final_selection();
1254 if (clip_star.owned)
1255 clip_lose_selection(&clip_star);
1256 if (clip_plus.owned)
1257 clip_lose_selection(&clip_plus);
1258 if (x11_display != NULL)
1259 XFlush(x11_display);
1260 }
1261}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001262
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001263# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001264/*
1265 * Save clipboard text to restore later.
1266 */
1267 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001268save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001269{
1270 if (clip_star.owned)
1271 clip_star_save = get_register('*', TRUE);
1272 if (clip_plus.owned)
1273 clip_plus_save = get_register('+', TRUE);
1274}
1275
1276/*
1277 * Restore clipboard text if no one own the X selection.
1278 */
1279 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001280restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001281{
1282 if (clip_star_save != NULL)
1283 {
1284 if (!clip_gen_owner_exists(&clip_star))
1285 put_register('*', clip_star_save);
1286 else
1287 free_register(clip_star_save);
1288 clip_star_save = NULL;
1289 }
1290 if (clip_plus_save != NULL)
1291 {
1292 if (!clip_gen_owner_exists(&clip_plus))
1293 put_register('+', clip_plus_save);
1294 else
1295 free_register(clip_plus_save);
1296 clip_plus_save = NULL;
1297 }
1298}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001299# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001300#endif
1301
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302/*
1303 * If the machine has job control, use it to suspend the program,
1304 * otherwise fake it by starting a new shell.
1305 */
1306 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001307mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001309 if (ignore_sigtstp)
1310 return;
1311
Bram Moolenaar041c7102020-05-30 18:14:57 +02001312#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001313 in_mch_suspend = TRUE;
1314
Bram Moolenaar0f873732019-12-05 20:28:46 +01001315 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001317 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
1319# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001320 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001322# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 sigcont_received = FALSE;
1324# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001325
Bram Moolenaar0f873732019-12-05 20:28:46 +01001326 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001327
1328# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001329 /*
1330 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001331 * immediately, but somehow not all the time, probably because it's handled
1332 * in another thread. Do not call pause() because there would be race
1333 * condition which would hang Vim if signal happened in between the test of
1334 * sigcont_received and the call to pause(). If signal is not yet received,
1335 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1336 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001337 */
1338 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001339 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001340
Bram Moolenaar262735e2009-07-14 10:20:22 +00001341 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001342 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001345 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
Bram Moolenaar2e310482018-08-21 13:09:10 +02001347 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348#else
1349 suspend_shell();
1350#endif
1351}
1352
1353 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001354mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355{
1356 Columns = 80;
1357 Rows = 24;
1358
1359 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001360
1361#ifdef SIGTSTP
1362 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1363 // that indicates the shell (or program) that launched us does not support
1364 // tty job control and thus we should ignore that signal. If invoked as a
1365 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
1366 ignore_sigtstp = restricted || SIG_IGN == signal(SIGTSTP, SIG_ERR);
1367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001369
Bram Moolenaar56718732006-03-15 22:53:57 +00001370#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001371 mac_conv_init();
1372#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001373#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1374 win_clip_init();
1375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376}
1377
1378 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001379set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380{
1381#if defined(SIGWINCH)
1382 /*
1383 * WINDOW CHANGE signal is handled with sig_winch().
1384 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001385 signal(SIGWINCH, (void (*)())sig_winch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386#endif
1387
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001389 // See mch_init() for the conditions under which we ignore SIGTSTP.
Bram Moolenaar8e4af852022-01-24 12:20:45 +00001390 // In the GUI default TSTP processing is OK.
1391 // Checking both gui.in_use and gui.starting because gui.in_use is not set
1392 // at this point (set after menus are displayed), but gui.starting is set.
1393 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN
1394# ifdef FEAT_GUI
1395 : gui.in_use || gui.starting ? SIG_DFL
1396# endif
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001397 : (void (*)())sig_tstp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001399#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 signal(SIGCONT, sigcont_handler);
1401#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001402#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 /*
1404 * We want to ignore breaking of PIPEs.
1405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 signal(SIGPIPE, SIG_IGN);
1407#endif
1408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001410 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411#endif
1412
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001413#ifdef SIGUSR1
1414 /*
1415 * Call user's handler on SIGUSR1
1416 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001417 signal(SIGUSR1, (void (*)())catch_sigusr1);
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001418#endif
1419
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 /*
1421 * Ignore alarm signals (Perl's alarm() generates it).
1422 */
1423#ifdef SIGALRM
1424 signal(SIGALRM, SIG_IGN);
1425#endif
1426
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001427#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 /*
1429 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1430 * work will be lost.
1431 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001432 signal(SIGPWR, (void (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433#endif
1434
1435 /*
1436 * Arrange for other signals to gracefully shutdown Vim.
1437 */
1438 catch_signals(deathtrap, SIG_ERR);
1439
1440#if defined(FEAT_GUI) && defined(SIGHUP)
1441 /*
1442 * When the GUI is running, ignore the hangup signal.
1443 */
1444 if (gui.in_use)
1445 signal(SIGHUP, SIG_IGN);
1446#endif
1447}
1448
Bram Moolenaardf177f62005-02-22 08:39:57 +00001449#if defined(SIGINT) || defined(PROTO)
1450/*
1451 * Catch CTRL-C (only works while in Cooked mode).
1452 */
1453 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001454catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001455{
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001456 signal(SIGINT, (void (*)())catch_sigint);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001457}
1458#endif
1459
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001461reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462{
1463 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001464#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001465 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 signal(SIGCONT, SIG_DFL);
1467#endif
1468}
1469
1470 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001471catch_signals(
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001472 void (*func_deadly)(),
1473 void (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
1475 int i;
1476
1477 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001478 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (signal_info[i].deadly)
1480 {
1481#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1482 struct sigaction sa;
1483
Bram Moolenaar0f873732019-12-05 20:28:46 +01001484 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 sa.sa_handler = func_deadly;
1486 sigemptyset(&sa.sa_mask);
1487# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001488 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1489 // thread handling in combination with using the alternate stack:
1490 // pthread library functions try to use the stack pointer to
1491 // identify the current thread, causing a SEGV signal, which
1492 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 sa.sa_flags = 0;
1494# else
1495 sa.sa_flags = SA_ONSTACK;
1496# endif
1497 sigaction(signal_info[i].sig, &sa, NULL);
1498#else
1499# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1500 struct sigvec sv;
1501
Bram Moolenaar0f873732019-12-05 20:28:46 +01001502 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 sv.sv_handler = func_deadly;
1504 sv.sv_mask = 0;
1505 sv.sv_flags = SV_ONSTACK;
1506 sigvec(signal_info[i].sig, &sv, NULL);
1507# else
1508 signal(signal_info[i].sig, func_deadly);
1509# endif
1510#endif
1511 }
1512 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001513 {
1514 // Deal with non-deadly signals.
1515#ifdef SIGTSTP
1516 signal(signal_info[i].sig,
1517 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1518 ? SIG_IGN : func_other);
1519#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001521#endif
1522 }
1523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524}
1525
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001526#ifdef HAVE_SIGPROCMASK
1527 static void
1528block_signals(sigset_t *set)
1529{
1530 sigset_t newset;
1531 int i;
1532
1533 sigemptyset(&newset);
1534
1535 for (i = 0; signal_info[i].sig != -1; i++)
1536 sigaddset(&newset, signal_info[i].sig);
1537
Bram Moolenaar2e310482018-08-21 13:09:10 +02001538# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001539 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001540 sigaddset(&newset, SIGCONT);
1541# endif
1542
1543 sigprocmask(SIG_BLOCK, &newset, set);
1544}
1545
1546 static void
1547unblock_signals(sigset_t *set)
1548{
1549 sigprocmask(SIG_SETMASK, set, NULL);
1550}
1551#endif
1552
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001554 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001555 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1556 * return TRUE
1557 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1558 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001559 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001560 * Returns TRUE when Vim should exit.
1561 */
1562 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001563vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001564{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001565 static int got_signal = 0;
1566 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001567
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001568 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001569 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001570 case SIGNAL_BLOCK: blocked = TRUE;
1571 break;
1572
1573 case SIGNAL_UNBLOCK: blocked = FALSE;
1574 if (got_signal != 0)
1575 {
1576 kill(getpid(), got_signal);
1577 got_signal = 0;
1578 }
1579 break;
1580
1581 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001582 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001583 got_signal = sig;
1584#ifdef SIGPWR
1585 if (sig != SIGPWR)
1586#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001587 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001588 break;
1589 }
1590 return FALSE;
1591}
1592
1593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 * Check_win checks whether we have an interactive stdout.
1595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001597mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (isatty(1))
1600 return OK;
1601 return FAIL;
1602}
1603
1604/*
1605 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1606 */
1607 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001608mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609{
1610 if (isatty(read_cmd_fd))
1611 return TRUE;
1612 return FALSE;
1613}
1614
1615#ifdef FEAT_X11
1616
Bram Moolenaar651fca82021-11-29 20:39:38 +00001617# if defined(ELAPSED_TIMEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619/*
1620 * Give a message about the elapsed time for opening the X window.
1621 */
1622 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001623xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001625 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626}
1627# endif
1628#endif
1629
Bram Moolenaar651fca82021-11-29 20:39:38 +00001630#if defined(FEAT_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631/*
1632 * A few functions shared by X11 title and clipboard code.
1633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634
1635static int got_x_error = FALSE;
1636
1637/*
1638 * X Error handler, otherwise X just exits! (very rude) -- webb
1639 */
1640 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001641x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001643 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 STRCAT(IObuff, _("\nVim: Got X error\n"));
1645
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001646 // In the GUI we cannot print a message and continue, because no X calls
1647 // are allowed here (causes my system to hang). Silently continuing seems
1648 // like the best alternative. Do preserve files, in case we crash.
1649 ml_sync_all(FALSE, FALSE);
1650
1651#ifdef FEAT_GUI
1652 if (!gui.in_use)
1653#endif
1654 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
Bram Moolenaar0f873732019-12-05 20:28:46 +01001656 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657}
1658
1659/*
1660 * Another X Error handler, just used to check for errors.
1661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001663x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
1665 got_x_error = TRUE;
1666 return 0;
1667}
1668
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001669/*
1670 * Return TRUE when connection to the X server is desired.
1671 */
1672 static int
1673x_connect_to_server(void)
1674{
1675 // No point in connecting if we are exiting or dying.
1676 if (exiting || v_dying)
1677 return FALSE;
1678
1679#if defined(FEAT_CLIENTSERVER)
1680 if (x_force_connect)
1681 return TRUE;
1682#endif
1683 if (x_no_connect)
1684 return FALSE;
1685
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001686 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001687 if (clip_exclude_prog != NULL)
1688 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001689 // Just in case we get called recursively, return FALSE. This could
1690 // happen if vpeekc() is used while executing the prog and it causes a
1691 // related callback to be invoked.
1692 if (regprog_in_use(clip_exclude_prog))
1693 return FALSE;
1694
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001695 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1696 return FALSE;
1697 }
1698 return TRUE;
1699}
1700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001702# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703/*
1704 * An X IO Error handler, used to catch error while opening the display.
1705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001707x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001709 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001711# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001712 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001713# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714}
1715# endif
1716
1717/*
1718 * An X IO Error handler, used to catch terminal errors.
1719 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001720static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001723x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001726 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 x11_window = 0;
1728 x11_display = NULL;
1729 xterm_Shell = (Widget)0;
1730
Bram Moolenaar0f873732019-12-05 20:28:46 +01001731 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001733# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001734 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001735# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001737
1738/*
1739 * If the X11 connection was lost try to restore it.
1740 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001741 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001742 */
1743 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001744may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001745{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001746 // No point in restoring the connecting if we are exiting or dying.
1747 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001748 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001749 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001750
1751# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001752 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001753 if (app_context != (XtAppContext)NULL)
1754 {
1755 XtDestroyApplicationContext(app_context);
1756 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001757 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001758 }
1759# endif
1760
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001761 setup_term_clip();
1762 get_x11_title(FALSE);
1763 }
1764}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001765
1766 void
1767ex_xrestore(exarg_T *eap)
1768{
1769 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1770 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001771 if (xterm_display_allocated)
1772 vim_free(xterm_display);
1773 xterm_display = (char *)vim_strsave(eap->arg);
1774 xterm_display_allocated = TRUE;
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001775 }
1776 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001777 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001778
1779 clear_xterm_clip();
1780 x11_window = 0;
1781 xterm_dpy_retry_count = 5; // Try reconnecting five times
1782 may_restore_clipboard();
1783}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784#endif
1785
1786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 * Test if "dpy" and x11_window are valid by getting the window title.
1788 * I don't actually want it yet, so there may be a simpler call to use, but
1789 * this will cause the error handler x_error_check() to be called if anything
1790 * is wrong, such as the window pointer being invalid (as can happen when the
1791 * user changes his DISPLAY, but not his WINDOWID) -- webb
1792 */
1793 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001794test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
1796 int (*old_handler)();
1797 XTextProperty text_prop;
1798
1799 old_handler = XSetErrorHandler(x_error_check);
1800 got_x_error = FALSE;
1801 if (XGetWMName(dpy, x11_window, &text_prop))
1802 XFree((void *)text_prop.value);
1803 XSync(dpy, False);
1804 (void)XSetErrorHandler(old_handler);
1805
1806 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001807 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809 return (got_x_error ? FAIL : OK);
1810}
1811#endif
1812
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
1814#ifdef FEAT_X11
1815
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001816static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818/*
1819 * try to get x11 window and display
1820 *
1821 * return FAIL for failure, OK otherwise
1822 */
1823 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001824get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
1826 char *winid;
1827 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001828#define XD_NONE 0 // x11_display not set here
1829#define XD_HERE 1 // x11_display opened here
1830#define XD_GUI 2 // x11_display used from gui.dpy
1831#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 static int x11_display_from = XD_NONE;
1833 static int did_set_error_handler = FALSE;
1834
1835 if (!did_set_error_handler)
1836 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001837 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 (void)XSetErrorHandler(x_error_handler);
1839 did_set_error_handler = TRUE;
1840 }
1841
Bram Moolenaar9372a112005-12-06 19:59:18 +00001842#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (gui.in_use)
1844 {
1845 /*
1846 * If the X11 display was opened here before, for the window where Vim
1847 * was started, close that one now to avoid a memory leak.
1848 */
1849 if (x11_display_from == XD_HERE && x11_display != NULL)
1850 {
1851 XCloseDisplay(x11_display);
1852 x11_display_from = XD_NONE;
1853 }
1854 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1855 {
1856 x11_display_from = XD_GUI;
1857 return OK;
1858 }
1859 x11_display = NULL;
1860 return FAIL;
1861 }
1862 else if (x11_display_from == XD_GUI)
1863 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001864 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 x11_window = 0;
1866 x11_display = NULL;
1867 x11_display_from = XD_NONE;
1868 }
1869#endif
1870
Bram Moolenaar0f873732019-12-05 20:28:46 +01001871 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (!x_connect_to_server())
1873 return FAIL;
1874
1875 /*
1876 * If WINDOWID not set, should try another method to find out
1877 * what the current window number is. The only code I know for
1878 * this is very complicated.
1879 * We assume that zero is invalid for WINDOWID.
1880 */
1881 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1882 x11_window = (Window)atol(winid);
1883
1884#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001885 if (xterm_dpy == x11_display)
1886 // x11_display may have been set to xterm_dpy elsewhere
1887 x11_display_from = XD_XTERM;
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (xterm_dpy != NULL && x11_window != 0)
1890 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001891 // We may have checked it already, but Gnome terminal can move us to
1892 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001893 if (x11_display_from != XD_XTERM)
1894 {
1895 /*
1896 * If the X11 display was opened here before, for the window where
1897 * Vim was started, close that one now to avoid a memory leak.
1898 */
1899 if (x11_display_from == XD_HERE && x11_display != NULL)
1900 XCloseDisplay(x11_display);
1901 x11_display = xterm_dpy;
1902 x11_display_from = XD_XTERM;
1903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (test_x11_window(x11_display) == FAIL)
1905 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001906 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 x11_window = 0;
1908 x11_display = NULL;
1909 x11_display_from = XD_NONE;
1910 return FAIL;
1911 }
1912 return OK;
1913 }
1914#endif
1915
1916 if (x11_window == 0 || x11_display == NULL)
1917 result = -1;
1918
Bram Moolenaar0f873732019-12-05 20:28:46 +01001919 if (result != -1) // Have already been here and set this
1920 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921
1922 if (x11_window != 0 && x11_display == NULL)
1923 {
1924#ifdef SET_SIG_ALARM
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001925 void (*sig_save)();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001927#ifdef ELAPSED_FUNC
1928 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
1930 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001931 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932#endif
1933
1934#ifdef SET_SIG_ALARM
1935 /*
1936 * Opening the Display may hang if the DISPLAY setting is wrong, or
1937 * the network connection is bad. Set an alarm timer to get out.
1938 */
1939 sig_alarm_called = FALSE;
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001940 sig_save = (void (*)())signal(SIGALRM, (void (*)())sig_alarm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 alarm(2);
1942#endif
1943 x11_display = XOpenDisplay(NULL);
1944
1945#ifdef SET_SIG_ALARM
1946 alarm(0);
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01001947 signal(SIGALRM, (void (*)())sig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001949 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950#endif
1951 if (x11_display != NULL)
1952 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001953# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001955 {
1956 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001957 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001958 verbose_leave();
1959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960# endif
1961 if (test_x11_window(x11_display) == FAIL)
1962 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001963 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 x11_window = 0;
1965 XCloseDisplay(x11_display);
1966 x11_display = NULL;
1967 }
1968 else
1969 x11_display_from = XD_HERE;
1970 }
1971 }
1972 if (x11_window == 0 || x11_display == NULL)
1973 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001974
1975# ifdef FEAT_EVAL
1976 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1977# endif
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 return (result = OK);
1980}
1981
1982/*
1983 * Determine original x11 Window Title
1984 */
1985 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001986get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001988 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989}
1990
1991/*
1992 * Determine original x11 Window icon
1993 */
1994 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001995get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996{
1997 int retval = FALSE;
1998
1999 retval = get_x11_thing(FALSE, test_only);
2000
Bram Moolenaar0f873732019-12-05 20:28:46 +01002001 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 if (oldicon == NULL && !test_only)
2003 {
2004 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002005 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002007 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 }
2009
2010 return retval;
2011}
2012
2013 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002014get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002015 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01002016 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017{
2018 XTextProperty text_prop;
2019 int retval = FALSE;
2020 Status status;
2021
2022 if (get_x11_windis() == OK)
2023 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002024 // Get window/icon name if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 if (get_title)
2026 status = XGetWMName(x11_display, x11_window, &text_prop);
2027 else
2028 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2029
2030 /*
2031 * If terminal is xterm, then x11_window may be a child window of the
2032 * outer xterm window that actually contains the window/icon name, so
2033 * keep traversing up the tree until a window with a title/icon is
2034 * found.
2035 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002036 // Previously this was only done for xterm and alike. I don't see a
Bram Moolenaar0f873732019-12-05 20:28:46 +01002037 // reason why it would fail for other terminal emulators.
2038 // if (term_is_xterm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
2040 Window root;
2041 Window parent;
2042 Window win = x11_window;
2043 Window *children;
2044 unsigned int num_children;
2045
2046 while (!status || text_prop.value == NULL)
2047 {
2048 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2049 &num_children))
2050 break;
2051 if (children)
2052 XFree((void *)children);
2053 if (parent == root || parent == 0)
2054 break;
2055
2056 win = parent;
2057 if (get_title)
2058 status = XGetWMName(x11_display, win, &text_prop);
2059 else
2060 status = XGetWMIconName(x11_display, win, &text_prop);
2061 }
2062 }
2063 if (status && text_prop.value != NULL)
2064 {
2065 retval = TRUE;
2066 if (!test_only)
2067 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002068 if (get_title)
2069 vim_free(oldtitle);
2070 else
2071 vim_free(oldicon);
Bram Moolenaara12a1612019-01-24 16:39:02 +01002072 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 if (get_title)
2075 oldtitle = vim_strsave((char_u *)text_prop.value);
2076 else
2077 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
2079 else
2080 {
2081 char **cl;
2082 Status transform_status;
2083 int n = 0;
2084
2085 transform_status = XmbTextPropertyToTextList(x11_display,
2086 &text_prop,
2087 &cl, &n);
2088 if (transform_status >= Success && n > 0 && cl[0])
2089 {
2090 if (get_title)
2091 oldtitle = vim_strsave((char_u *) cl[0]);
2092 else
2093 oldicon = vim_strsave((char_u *) cl[0]);
2094 XFreeStringList(cl);
2095 }
2096 else
2097 {
2098 if (get_title)
2099 oldtitle = vim_strsave((char_u *)text_prop.value);
2100 else
2101 oldicon = vim_strsave((char_u *)text_prop.value);
2102 }
2103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
2105 XFree((void *)text_prop.value);
2106 }
2107 }
2108 return retval;
2109}
2110
Bram Moolenaar0f873732019-12-05 20:28:46 +01002111// Xutf8 functions are not available on older systems. Note that on some
2112// systems X_HAVE_UTF8_STRING may be defined in a header file but
2113// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2114// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002115#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002116# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117# define USE_UTF8_STRING
2118# endif
2119#endif
2120
2121/*
2122 * Set x11 Window Title
2123 *
2124 * get_x11_windis() must be called before this and have returned OK
2125 */
2126 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002127set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002129 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2130 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2131 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132#ifdef USE_UTF8_STRING
2133 if (enc_utf8)
2134 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2135 NULL, NULL, 0, NULL, NULL, NULL);
2136 else
2137#endif
2138 {
2139#if XtSpecificationRelease >= 4
2140# ifdef FEAT_XFONTSET
2141 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2142 NULL, NULL, 0, NULL, NULL, NULL);
2143# else
2144 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002145 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
Bram Moolenaar0f873732019-12-05 20:28:46 +01002147 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002148 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 XSetWMProperties(x11_display, x11_window, &text_prop,
2150 NULL, NULL, 0, NULL, NULL, NULL);
2151# endif
2152#else
2153 XStoreName(x11_display, x11_window, (char *)title);
2154#endif
2155 }
2156 XFlush(x11_display);
2157}
2158
2159/*
2160 * Set x11 Window icon
2161 *
2162 * get_x11_windis() must be called before this and have returned OK
2163 */
2164 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002165set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002167 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168#ifdef USE_UTF8_STRING
2169 if (enc_utf8)
2170 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2171 NULL, 0, NULL, NULL, NULL);
2172 else
2173#endif
2174 {
2175#if XtSpecificationRelease >= 4
2176# ifdef FEAT_XFONTSET
2177 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2178 NULL, 0, NULL, NULL, NULL);
2179# else
2180 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002181 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002183 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2185 NULL, 0, NULL, NULL, NULL);
2186# endif
2187#else
2188 XSetIconName(x11_display, x11_window, (char *)icon);
2189#endif
2190 }
2191 XFlush(x11_display);
2192}
2193
Bram Moolenaar0f873732019-12-05 20:28:46 +01002194#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002197get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198{
2199 return FALSE;
2200}
2201
2202 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002203get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204{
2205 if (!test_only)
2206 {
2207 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002208 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002210 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
2212 return FALSE;
2213}
2214
Bram Moolenaar0f873732019-12-05 20:28:46 +01002215#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
2217 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002218mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219{
2220 return get_x11_title(TRUE);
2221}
2222
2223 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002224mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
2226 return get_x11_icon(TRUE);
2227}
2228
2229/*
2230 * Set the window title and icon.
2231 */
2232 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002233mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
2235 int type = 0;
2236 static int recursive = 0;
2237
Bram Moolenaar0f873732019-12-05 20:28:46 +01002238 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002240 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 return;
2242
Bram Moolenaar0f873732019-12-05 20:28:46 +01002243 // When one of the X11 functions causes a deadly signal, we get here again
2244 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 if (recursive)
2246 return;
2247 ++recursive;
2248
2249 /*
2250 * if the window ID and the display is known, we may use X11 calls
2251 */
2252#ifdef FEAT_X11
2253 if (get_x11_windis() == OK)
2254 type = 1;
2255#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002256# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002257 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (gui.in_use)
2259 type = 1;
2260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261#endif
2262
2263 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002264 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 * than x11 calls, because the x11 calls don't always work
2266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if ((type || *T_TS != NUL) && title != NULL)
2268 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002269 if (oldtitle_outdated)
2270 {
2271 oldtitle_outdated = FALSE;
2272 VIM_CLEAR(oldtitle);
2273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 if (oldtitle == NULL
2275#ifdef FEAT_GUI
2276 && !gui.in_use
2277#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002278 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 (void)get_x11_title(FALSE);
2280
Bram Moolenaar0f873732019-12-05 20:28:46 +01002281 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 term_settitle(title);
2283#ifdef FEAT_X11
2284 else
2285# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002286 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002288 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002290#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002291 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 else
2293 gui_mch_settitle(title, icon);
2294#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002295 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297
2298 if ((type || *T_CIS != NUL) && icon != NULL)
2299 {
2300 if (oldicon == NULL
2301#ifdef FEAT_GUI
2302 && !gui.in_use
2303#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002304 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 get_x11_icon(FALSE);
2306
2307 if (*T_CIS != NUL)
2308 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002309 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002311 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 out_flush();
2313 }
2314#ifdef FEAT_X11
2315 else
2316# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002317 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002319 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320#endif
2321 did_set_icon = TRUE;
2322 }
2323 --recursive;
2324}
2325
2326/*
2327 * Restore the window/icon title.
2328 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002329 * SAVE_RESTORE_TITLE only restore title
2330 * SAVE_RESTORE_ICON only restore icon
2331 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 */
2333 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002334mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
Bram Moolenaardac13472019-09-16 21:06:21 +02002336 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002337
Bram Moolenaar0f873732019-12-05 20:28:46 +01002338 // only restore the title or icon when it has been set
Bram Moolenaardac13472019-09-16 21:06:21 +02002339 mch_settitle(((which & SAVE_RESTORE_TITLE) && unix_did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002341 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2342
Bram Moolenaare5c83282019-05-03 23:15:37 +02002343 if (do_push_pop)
2344 {
2345 // pop and push from/to the stack
2346 term_pop_title(which);
2347 term_push_title(which);
2348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349}
2350
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352/*
2353 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002354 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 */
2356 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002357vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
2359 if (name == NULL)
2360 return FALSE;
2361 return (STRNICMP(name, "xterm", 5) == 0
2362 || STRNICMP(name, "nxterm", 6) == 0
2363 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002364 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002366 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 || STRCMP(name, "builtin_xterm") == 0);
2368}
2369
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002370#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2371/*
2372 * Return TRUE if "name" appears to be that of a terminal
2373 * known to support the xterm-style mouse protocol.
2374 * Relies on term_is_xterm having been set to its correct value.
2375 */
2376 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002377use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002378{
2379 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002380 && (term_is_xterm
2381 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002382 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar48873ae2021-12-08 21:00:24 +00002383 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002384 || STRICMP(name, "st") == 0
2385 || STRNICMP(name, "st-", 3) == 0
2386 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002387}
2388#endif
2389
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390/*
2391 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2392 * Return 1 for "xterm".
2393 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002394 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002395 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
2397 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002398use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002400 if (ttym_flags == TTYM_SGR)
2401 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002402 if (ttym_flags == TTYM_URXVT)
2403 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 if (ttym_flags == TTYM_XTERM2)
2405 return 2;
2406 if (ttym_flags == TTYM_XTERM)
2407 return 1;
2408 return 0;
2409}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
2411 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002412vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
2414 if (name == NULL)
2415 return FALSE;
2416 return (STRNICMP(name, "iris-ansi", 9) == 0
2417 || STRCMP(name, "builtin_iris-ansi") == 0);
2418}
2419
2420 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002421vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
2423 if (name == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002424 return FALSE; // actually all ANSI comp. terminals should be here
2425 // catch VT100 - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002426 return ((STRNICMP(name, "vt", 2) == 0
2427 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 || STRCMP(name, "builtin_vt320") == 0);
2429}
2430
2431/*
2432 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2433 * This should include all windowed terminal emulators.
2434 */
2435 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002436vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437{
2438 if (name == NULL)
2439 return FALSE;
2440 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2441 return TRUE;
2442 return ( STRNICMP(name, "hpterm", 6) == 0
2443 || STRNICMP(name, "sun-cmd", 7) == 0
2444 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002445 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 || STRNICMP(name, "dtterm", 6) == 0);
2447}
2448
2449/*
2450 * Insert user name in s[len].
2451 * Return OK if a name found.
2452 */
2453 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002454mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455{
2456#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002457 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 return OK;
2459#else
2460 return mch_get_uname(getuid(), s, len);
2461#endif
2462}
2463
2464/*
2465 * Insert user name for "uid" in s[len].
2466 * Return OK if a name found.
2467 */
2468 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002469mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470{
2471#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2472 struct passwd *pw;
2473
2474 if ((pw = getpwuid(uid)) != NULL
2475 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2476 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002477 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 return OK;
2479 }
2480#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002481 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2482 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483}
2484
2485/*
2486 * Insert host name is s[len].
2487 */
2488
2489#ifdef HAVE_SYS_UTSNAME_H
2490 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002491mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
2493 struct utsname vutsname;
2494
2495 if (uname(&vutsname) < 0)
2496 *s = NUL;
2497 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002498 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002500#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502# ifdef HAVE_SYS_SYSTEMINFO_H
2503# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2504# endif
2505
2506 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002507mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509# ifdef VAXC
2510 vaxc$gethostname((char *)s, len);
2511# else
2512 gethostname((char *)s, len);
2513# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002514 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002516#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
2518/*
2519 * return process ID
2520 */
2521 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002522mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524 return (long)getpid();
2525}
2526
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002527/*
2528 * return TRUE if process "pid" is still running
2529 */
2530 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002531mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002532{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002533 // If there is no error the process must be running.
2534 if (kill(pid, 0) == 0)
2535 return TRUE;
2536#ifdef ESRCH
2537 // If the error is ESRCH then the process is not running.
2538 if (errno == ESRCH)
2539 return FALSE;
2540#endif
2541 // If the process is running and owned by another user we get EPERM. With
2542 // other errors the process might be running, assuming it is then.
2543 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002544}
2545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002548strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549{
2550 extern int sys_nerr;
2551 extern char *sys_errlist[];
2552 static char er[20];
2553
2554 if (err > 0 && err < sys_nerr)
2555 return (sys_errlist[err]);
2556 sprintf(er, "Error %d", err);
2557 return er;
2558}
2559#endif
2560
2561/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002562 * Get name of current directory into buffer "buf" of length "len" bytes.
2563 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 * Return OK for success, FAIL for failure.
2565 */
2566 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002567mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568{
2569#if defined(USE_GETCWD)
2570 if (getcwd((char *)buf, len) == NULL)
2571 {
2572 STRCPY(buf, strerror(errno));
2573 return FAIL;
2574 }
2575 return OK;
2576#else
2577 return (getwd((char *)buf) != NULL ? OK : FAIL);
2578#endif
2579}
2580
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002582 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 *
2584 * return FAIL for failure, OK for success
2585 */
2586 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002587mch_FullName(
2588 char_u *fname,
2589 char_u *buf,
2590 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002591 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
2593 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002594#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002596 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 char_u olddir[MAXPATHL];
2599 char_u *p;
2600 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002601#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002602 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2603 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002604#endif
2605
Bram Moolenaar38323e42007-03-06 19:22:53 +00002606#ifdef VMS
2607 fname = vms_fixfilename(fname);
2608#endif
2609
Bram Moolenaara2442432007-04-26 14:26:37 +00002610#ifdef __CYGWIN__
2611 /*
2612 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2613 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002614# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002615 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2616 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002617 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2618 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002619# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002620 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002621# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002622 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002623#endif
2624
Bram Moolenaar0f873732019-12-05 20:28:46 +01002625 // Expand it if forced or not an absolute path.
2626 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002627 if ((force || !mch_isFullName(fname))
2628 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 {
2630 /*
2631 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002632 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 * This will get the correct path name with "../" things.
2634 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002635 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002637 if (STRCMP(p, "/..") == 0)
2638 // for "/path/dir/.." include the "/.."
2639 p += 3;
2640
Bram Moolenaar38323e42007-03-06 19:22:53 +00002641#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 /*
2643 * Use fchdir() if possible, it's said to be faster and more
2644 * reliable. But on SunOS 4 it might not work. Check this by
2645 * doing a fchdir() right now.
2646 */
2647 if (!dont_fchdir)
2648 {
2649 fd = open(".", O_RDONLY | O_EXTRA, 0);
2650 if (fd >= 0 && fchdir(fd) < 0)
2651 {
2652 close(fd);
2653 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002654 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
2656 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
Bram Moolenaar0f873732019-12-05 20:28:46 +01002659 // Only change directory when we are sure we can return to where
2660 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002662#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 (mch_dirname(olddir, MAXPATHL) == FAIL
2666 || mch_chdir((char *)olddir) != 0))
2667 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002668 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 retval = FAIL;
2670 }
2671 else
2672 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002673 // The directory is copied into buf[], to be able to remove
2674 // the file name without changing it (could be a string in
2675 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (p - fname >= len)
2677 retval = FAIL;
2678 else
2679 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002680 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002682 {
2683 // Path does not exist (yet). For a full path fail,
2684 // will use the path as-is. For a relative path use
2685 // the current directory and append the file name.
2686 if (mch_isFullName(fname))
2687 retval = FAIL;
2688 else
2689 p = NULL;
2690 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002691 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002693 else
2694 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 *buf = NUL;
2696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 }
2698 }
2699 if (mch_dirname(buf, len) == FAIL)
2700 {
2701 retval = FAIL;
2702 *buf = NUL;
2703 }
2704 if (p != NULL)
2705 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002706#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 if (fd >= 0)
2708 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002709 if (p_verbose >= 5)
2710 {
2711 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002712 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002713 verbose_leave();
2714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
2717 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 l = mch_chdir((char *)olddir);
2720 if (l != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002721 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
itchyny051a40c2021-10-20 10:00:05 +01002723#ifdef HAVE_FCHDIR
2724 if (fd >= 0)
2725 close(fd);
2726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
2728 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002729 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002730 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002731#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002732 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002734 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002737
Bram Moolenaar0f873732019-12-05 20:28:46 +01002738 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002739 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 return FAIL;
2741
Bram Moolenaar0f873732019-12-05 20:28:46 +01002742 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 if (STRCMP(fname, ".") != 0)
2744 STRCAT(buf, fname);
2745
2746 return OK;
2747}
2748
2749/*
2750 * Return TRUE if "fname" does not depend on the current directory.
2751 */
2752 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002753mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002755#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 return ( fname[0] == '/' || fname[0] == '.' ||
2757 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2758 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2759 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002760#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762#endif
2763}
2764
Bram Moolenaar24552be2005-12-10 20:17:30 +00002765#if defined(USE_FNAME_CASE) || defined(PROTO)
2766/*
2767 * Set the case of the file name, if it already exists. This will cause the
2768 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002769 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002770 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002771 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002772fname_case(
2773 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002774 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002775{
2776 struct stat st;
2777 char_u *slash, *tail;
2778 DIR *dirp;
2779 struct dirent *dp;
2780
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002781 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002782 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002783 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002784 slash = vim_strrchr(name, '/');
2785 if (slash == NULL)
2786 {
2787 dirp = opendir(".");
2788 tail = name;
2789 }
2790 else
2791 {
2792 *slash = NUL;
2793 dirp = opendir((char *)name);
2794 *slash = '/';
2795 tail = slash + 1;
2796 }
2797
2798 if (dirp != NULL)
2799 {
2800 while ((dp = readdir(dirp)) != NULL)
2801 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002802 // Only accept names that differ in case and are the same byte
2803 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002804 if (STRICMP(tail, dp->d_name) == 0
2805 && STRLEN(tail) == STRLEN(dp->d_name))
2806 {
2807 char_u newname[MAXPATHL + 1];
2808 struct stat st2;
2809
Bram Moolenaar0f873732019-12-05 20:28:46 +01002810 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002811 vim_strncpy(newname, name, MAXPATHL);
2812 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2813 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002814 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002815 && st.st_ino == st2.st_ino
2816 && st.st_dev == st2.st_dev)
2817 {
2818 STRCPY(tail, dp->d_name);
2819 break;
2820 }
2821 }
2822 }
2823
2824 closedir(dirp);
2825 }
2826 }
2827}
2828#endif
2829
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830/*
2831 * Get file permissions for 'name'.
2832 * Returns -1 when it doesn't exist.
2833 */
2834 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002835mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
2837 struct stat statb;
2838
Bram Moolenaar0f873732019-12-05 20:28:46 +01002839 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840#ifdef VMS
2841 if (stat((char *)vms_fixfilename(name), &statb))
2842#else
2843 if (stat((char *)name, &statb))
2844#endif
2845 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002846#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002847 // The top bit makes the value negative, which means the file doesn't
2848 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002849 return statb.st_mode & ~S_ADDACE;
2850#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853}
2854
2855/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002856 * Set file permission for "name" to "perm".
2857 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 */
2859 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002860mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861{
2862 return (chmod((char *)
2863#ifdef VMS
2864 vms_fixfilename(name),
2865#else
2866 name,
2867#endif
2868 (mode_t)perm) == 0 ? OK : FAIL);
2869}
2870
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002871#if defined(HAVE_FCHMOD) || defined(PROTO)
2872/*
2873 * Set file permission for open file "fd" to "perm".
2874 * Return FAIL for failure, OK otherwise.
2875 */
2876 int
2877mch_fsetperm(int fd, long perm)
2878{
2879 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2880}
2881#endif
2882
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883#if defined(HAVE_ACL) || defined(PROTO)
2884# ifdef HAVE_SYS_ACL_H
2885# include <sys/acl.h>
2886# endif
2887# ifdef HAVE_SYS_ACCESS_H
2888# include <sys/access.h>
2889# endif
2890
2891# ifdef HAVE_SOLARIS_ACL
2892typedef struct vim_acl_solaris_T {
2893 int acl_cnt;
2894 aclent_t *acl_entry;
2895} vim_acl_solaris_T;
2896# endif
2897
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002898#if defined(HAVE_SELINUX) || defined(PROTO)
2899/*
2900 * Copy security info from "from_file" to "to_file".
2901 */
2902 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002903mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002904{
2905 if (from_file == NULL)
2906 return;
2907
2908 if (selinux_enabled == -1)
2909 selinux_enabled = is_selinux_enabled();
2910
2911 if (selinux_enabled > 0)
2912 {
Bram Moolenaar89560232020-10-09 23:04:47 +02002913 // Use "char *" instead of "security_context_t" to avoid a deprecation
2914 // warning.
2915 char *from_context = NULL;
2916 char *to_context = NULL;
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002917
2918 if (getfilecon((char *)from_file, &from_context) < 0)
2919 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002920 // If the filesystem doesn't support extended attributes,
2921 // the original had no special security context and the
2922 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002923 if (errno == EOPNOTSUPP)
2924 return;
2925
Bram Moolenaar32526b32019-01-19 17:43:09 +01002926 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002927 msg_outtrans(from_file);
2928 msg_putchar('\n');
2929 return;
2930 }
2931 if (getfilecon((char *)to_file, &to_context) < 0)
2932 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002933 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002934 msg_outtrans(to_file);
2935 msg_putchar('\n');
2936 freecon (from_context);
2937 return ;
2938 }
2939 if (strcmp(from_context, to_context) != 0)
2940 {
2941 if (setfilecon((char *)to_file, from_context) < 0)
2942 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002943 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002944 msg_outtrans(to_file);
2945 msg_putchar('\n');
2946 }
2947 }
2948 freecon(to_context);
2949 freecon(from_context);
2950 }
2951}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002952#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002953
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002954#if defined(HAVE_SMACK) && !defined(PROTO)
2955/*
2956 * Copy security info from "from_file" to "to_file".
2957 */
2958 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002959mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002960{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002961 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002962 {
2963 XATTR_NAME_SMACK,
2964 XATTR_NAME_SMACKEXEC,
2965 XATTR_NAME_SMACKMMAP
2966 };
2967
2968 char buffer[SMACK_LABEL_LEN];
2969 const char *name;
2970 int index;
2971 int ret;
2972 ssize_t size;
2973
2974 if (from_file == NULL)
2975 return;
2976
2977 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2978 / sizeof(smack_copied_attributes)[0]) ; index++)
2979 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002980 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002981 name = smack_copied_attributes[index];
2982
Bram Moolenaar0f873732019-12-05 20:28:46 +01002983 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002984 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2985 if (size >= 0)
2986 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002987 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002988 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2989 if (ret < 0)
2990 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002991 vim_snprintf((char *)IObuff, IOSIZE,
2992 _("Could not set security context %s for %s"),
2993 name, to_file);
2994 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002995 msg_putchar('\n');
2996 }
2997 }
2998 else
2999 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003000 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003001 switch (errno)
3002 {
3003 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003004 // extended attributes aren't supported or enabled
3005 // should a message be echoed? not sure...
3006 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003007
3008 case ERANGE:
3009 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003010 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003011 vim_snprintf((char *)IObuff, IOSIZE,
3012 _("Could not get security context %s for %s. Removing it!"),
3013 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003014 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01003015 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01003016 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003017
3018 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003019 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003020 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003021 // Silently ignore errors, apparently this happens when
3022 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003023 break;
3024 }
3025 }
3026 }
3027}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003028#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003029
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030/*
3031 * Return a pointer to the ACL of file "fname" in allocated memory.
3032 * Return NULL if the ACL is not available for whatever reason.
3033 */
3034 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003035mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036{
3037 vim_acl_T ret = NULL;
3038#ifdef HAVE_POSIX_ACL
3039 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3040#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003041#ifdef HAVE_SOLARIS_ZFS_ACL
3042 acl_t *aclent;
3043
3044 if (acl_get((char *)fname, 0, &aclent) < 0)
3045 return NULL;
3046 ret = (vim_acl_T)aclent;
3047#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048#ifdef HAVE_SOLARIS_ACL
3049 vim_acl_solaris_T *aclent;
3050
3051 aclent = malloc(sizeof(vim_acl_solaris_T));
3052 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3053 {
3054 free(aclent);
3055 return NULL;
3056 }
3057 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3058 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3059 {
3060 free(aclent->acl_entry);
3061 free(aclent);
3062 return NULL;
3063 }
3064 ret = (vim_acl_T)aclent;
3065#else
3066#if defined(HAVE_AIX_ACL)
3067 int aclsize;
3068 struct acl *aclent;
3069
3070 aclsize = sizeof(struct acl);
3071 aclent = malloc(aclsize);
3072 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3073 {
3074 if (errno == ENOSPC)
3075 {
3076 aclsize = aclent->acl_len;
3077 aclent = realloc(aclent, aclsize);
3078 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3079 {
3080 free(aclent);
3081 return NULL;
3082 }
3083 }
3084 else
3085 {
3086 free(aclent);
3087 return NULL;
3088 }
3089 }
3090 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003091#endif // HAVE_AIX_ACL
3092#endif // HAVE_SOLARIS_ACL
3093#endif // HAVE_SOLARIS_ZFS_ACL
3094#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 return ret;
3096}
3097
3098/*
3099 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3100 */
3101 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003102mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103{
3104 if (aclent == NULL)
3105 return;
3106#ifdef HAVE_POSIX_ACL
3107 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3108#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003109#ifdef HAVE_SOLARIS_ZFS_ACL
3110 acl_set((char *)fname, (acl_t *)aclent);
3111#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112#ifdef HAVE_SOLARIS_ACL
3113 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3114 ((vim_acl_solaris_T *)aclent)->acl_entry);
3115#else
3116#ifdef HAVE_AIX_ACL
3117 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003118#endif // HAVE_AIX_ACL
3119#endif // HAVE_SOLARIS_ACL
3120#endif // HAVE_SOLARIS_ZFS_ACL
3121#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122}
3123
3124 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003125mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126{
3127 if (aclent == NULL)
3128 return;
3129#ifdef HAVE_POSIX_ACL
3130 acl_free((acl_t)aclent);
3131#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003132#ifdef HAVE_SOLARIS_ZFS_ACL
3133 acl_free((acl_t *)aclent);
3134#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135#ifdef HAVE_SOLARIS_ACL
3136 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3137 free(aclent);
3138#else
3139#ifdef HAVE_AIX_ACL
3140 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003141#endif // HAVE_AIX_ACL
3142#endif // HAVE_SOLARIS_ACL
3143#endif // HAVE_SOLARIS_ZFS_ACL
3144#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145}
3146#endif
3147
3148/*
3149 * Set hidden flag for "name".
3150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003152mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003154 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155}
3156
3157/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003158 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 * return FALSE if "name" is not a directory
3160 * return FALSE for error
3161 */
3162 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003163mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164{
3165 struct stat statb;
3166
Bram Moolenaar0f873732019-12-05 20:28:46 +01003167 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 return FALSE;
3169 if (stat((char *)name, &statb))
3170 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172}
3173
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003174/*
3175 * return TRUE if "name" is a directory, NOT a symlink to a directory
3176 * return FALSE if "name" is not a directory
3177 * return FALSE for error
3178 */
3179 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003180mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003181{
3182 struct stat statb;
3183
Bram Moolenaar0f873732019-12-05 20:28:46 +01003184 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003185 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003186 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003187 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003188 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003189}
3190
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191/*
3192 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3193 */
3194 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003195executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196{
3197 struct stat st;
3198
3199 if (stat((char *)name, &st))
3200 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003201#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003202 // Like on Unix system file can have executable rights but not necessarily
3203 // be an executable, but on Unix is not a default for an ordinary file to
3204 // have an executable flag - on VMS it is in most cases.
3205 // Therefore, this check does not have any sense - let keep us to the
3206 // conventions instead:
3207 // *.COM and *.EXE files are the executables - the rest are not. This is
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003208 // not ideal but better than it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003209 int vms_executable = 0;
3210 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3211 {
3212 if (strstr(vms_tolower((char*)name),".exe") != NULL
3213 || strstr(vms_tolower((char*)name),".com")!= NULL)
3214 vms_executable = 1;
3215 }
3216 return vms_executable;
3217#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220}
3221
3222/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003223 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003224 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 * Return -1 if unknown.
3226 */
3227 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003228mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
3230 char_u *buf;
3231 char_u *p, *e;
3232 int retval;
3233
Bram Moolenaar0f873732019-12-05 20:28:46 +01003234 // When "use_path" is false and if it's an absolute or relative path don't
3235 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003236 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003237 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003238 // There must be a path separator, files in the current directory
3239 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003240 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003241 {
3242 if (path != NULL)
3243 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003244 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003245 *path = FullName_save(name, TRUE);
3246 else
3247 *path = vim_strsave(name);
3248 }
3249 return TRUE;
3250 }
3251 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
3254 p = (char_u *)getenv("PATH");
3255 if (p == NULL || *p == NUL)
3256 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003257 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 if (buf == NULL)
3259 return -1;
3260
3261 /*
3262 * Walk through all entries in $PATH to check if "name" exists there and
3263 * is an executable file.
3264 */
3265 for (;;)
3266 {
3267 e = (char_u *)strchr((char *)p, ':');
3268 if (e == NULL)
3269 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003270 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 STRCPY(buf, "./");
3272 else
3273 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003274 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 add_pathsep(buf);
3276 }
3277 STRCAT(buf, name);
3278 retval = executable_file(buf);
3279 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003280 {
3281 if (path != NULL)
3282 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003283 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003284 *path = FullName_save(buf, TRUE);
3285 else
3286 *path = vim_strsave(buf);
3287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
3291 if (*e != ':')
3292 break;
3293 p = e + 1;
3294 }
3295
3296 vim_free(buf);
3297 return retval;
3298}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299
3300/*
3301 * Check what "name" is:
3302 * NODE_NORMAL: file or directory (or doesn't exist)
3303 * NODE_WRITABLE: writable device, socket, fifo, etc.
3304 * NODE_OTHER: non-writable things
3305 */
3306 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003307mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
3309 struct stat st;
3310
3311 if (stat((char *)name, &st))
3312 return NODE_NORMAL;
3313 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3314 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003315 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003317 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 return NODE_WRITABLE;
3319}
3320
3321 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003322mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
3324#ifdef HAVE_CHECK_STACK_GROWTH
3325 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 check_stack_growth((char *)&i);
3328
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003329# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 get_stack_limit();
3331# endif
3332
3333#endif
3334
3335 /*
3336 * Setup an alternative stack for signals. Helps to catch signals when
3337 * running out of stack space.
3338 * Use of sigaltstack() is preferred, it's more portable.
3339 * Ignore any errors.
3340 */
3341#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003342 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 init_signal_stack();
3344#endif
3345}
3346
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003347#if defined(EXITFREE) || defined(PROTO)
3348 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003349mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003350{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003351# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3352 if (clip_star.owned)
3353 clip_lose_selection(&clip_star);
3354 if (clip_plus.owned)
3355 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003356# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003357# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003358 if (xterm_Shell != (Widget)0)
3359 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003360# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003361 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003362 if (xterm_dpy != NULL)
3363 XtCloseDisplay(xterm_dpy);
3364 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003365 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003366 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003367# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003368 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003369# endif
3370 }
3371# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003372# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003373# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003374 if (x11_display != NULL
3375# ifdef FEAT_XCLIPBOARD
3376 && x11_display != xterm_dpy
3377# endif
3378 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003379 XCloseDisplay(x11_display);
3380# endif
3381# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003382 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003383# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003384 vim_free(oldtitle);
3385 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003386}
3387#endif
3388
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389/*
3390 * Output a newline when exiting.
3391 * Make sure the newline goes to the same stream as the text.
3392 */
3393 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003394exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003396 if (silent_mode)
3397 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 if (newline_on_exit || msg_didout)
3399 {
3400 if (msg_use_printf())
3401 {
3402 if (info_message)
3403 mch_msg("\n");
3404 else
3405 mch_errmsg("\r\n");
3406 }
3407 else
3408 out_char('\n');
3409 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003410 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003412 restore_cterm_colors(); // get original colors back
3413 msg_clr_eos_force(); // clear the rest of the display
3414 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 }
3416}
3417
Bram Moolenaarb4151682020-05-11 22:13:28 +02003418#ifdef USE_GCOV_FLUSH
ichizokdee78e12021-12-09 21:08:01 +00003419# if (defined(__GNUC__) \
3420 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3421 || (defined(__clang__) && (__clang_major__ >= 12))
3422extern void __gcov_dump(void);
3423extern void __gcov_reset(void);
3424# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3425# else
3426extern void __gcov_flush(void);
3427# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003428#endif
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003431mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432{
3433 exiting = TRUE;
3434
3435#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3436 x11_export_final_selection();
3437#endif
3438
3439#ifdef FEAT_GUI
3440 if (!gui.in_use)
3441#endif
3442 {
3443 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003444 if (!is_not_a_term())
3445 {
3446 // restore xterm title and icon name
3447 mch_restore_title(SAVE_RESTORE_BOTH);
3448 term_pop_title(SAVE_RESTORE_BOTH);
3449 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003450
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 /*
3452 * When t_ti is not empty but it doesn't cause swapping terminal
3453 * pages, need to output a newline when msg_didout is set. But when
3454 * t_ti does swap pages it should not go to the shell page. Do this
3455 * before stoptermcap().
3456 */
3457 if (swapping_screen() && !newline_on_exit)
3458 exit_scroll();
3459
Bram Moolenaar0f873732019-12-05 20:28:46 +01003460 // Stop termcap: May need to check for T_CRV response, which
3461 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 stoptermcap();
3463
3464 /*
3465 * A newline is only required after a message in the alternate screen.
3466 * This is set to TRUE by wait_return().
3467 */
3468 if (!swapping_screen() || newline_on_exit)
3469 exit_scroll();
3470
Bram Moolenaar0f873732019-12-05 20:28:46 +01003471 // Cursor may have been switched off without calling starttermcap()
3472 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (full_screen)
3474 cursor_on();
3475 }
3476 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003477 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003478
3479#ifdef USE_GCOV_FLUSH
3480 // Flush coverage info before possibly being killed by a deadly signal.
3481 __gcov_flush();
3482#endif
3483
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 may_core_dump();
3485#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 gui_exit(r);
3488#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003489
Bram Moolenaar56718732006-03-15 22:53:57 +00003490#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003491 mac_conv_cleanup();
3492#endif
3493
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003495 // A core dump won't be created if the signal handler
3496 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (deadly_signal != 0)
3498 return;
3499#endif
3500
Bram Moolenaar009b2592004-10-24 19:18:58 +00003501#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003502 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003503#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003504
3505#ifdef EXITFREE
3506 free_all_mem();
3507#endif
3508
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 exit(r);
3510}
3511
3512 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003513may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
3515 if (deadly_signal != 0)
3516 {
3517 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003518 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
3520}
3521
3522#ifndef VMS
3523
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003524/*
3525 * Get the file descriptor to use for tty operations.
3526 */
3527 static int
3528get_tty_fd(int fd)
3529{
3530 int tty_fd = fd;
3531
3532#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3533 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3534 // "fd" when it is a master device.
3535 if (mch_isatty(fd) > 1)
3536 {
3537 char *name;
3538
3539 name = ptsname(fd);
3540 if (name == NULL)
3541 return -1;
3542
3543 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3544 if (tty_fd < 0)
3545 return -1;
3546 }
3547#endif
3548 return tty_fd;
3549}
3550
3551 static int
3552mch_tcgetattr(int fd, void *term)
3553{
3554 int tty_fd;
3555 int retval = -1;
3556
3557 tty_fd = get_tty_fd(fd);
3558 if (tty_fd >= 0)
3559 {
3560#ifdef NEW_TTY_SYSTEM
3561# ifdef HAVE_TERMIOS_H
3562 retval = tcgetattr(tty_fd, (struct termios *)term);
3563# else
3564 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3565# endif
3566#else
3567 // for "old" tty systems
3568 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3569#endif
3570 if (tty_fd != fd)
3571 close(tty_fd);
3572 }
3573 return retval;
3574}
3575
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003577mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
3579 static int first = TRUE;
3580
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003581#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582# ifdef HAVE_TERMIOS_H
3583 static struct termios told;
3584 struct termios tnew;
3585# else
3586 static struct termio told;
3587 struct termio tnew;
3588# endif
3589
3590 if (first)
3591 {
3592 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003593 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
3595
3596 tnew = told;
3597 if (tmode == TMODE_RAW)
3598 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003599 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003600 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3601 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003603# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003604 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605# endif
3606 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003607# ifdef ONLCR
3608 // Don't map NL -> CR NL, we do it ourselves.
3609 // Also disable expanding tabs if possible.
3610# ifdef XTABS
3611 tnew.c_oflag &= ~(ONLCR | XTABS);
3612# else
3613# ifdef TAB3
3614 tnew.c_oflag &= ~(ONLCR | TAB3);
3615# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003617# endif
3618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003620 tnew.c_cc[VMIN] = 1; // return after 1 char
3621 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 }
3623 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003624 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003625 // Also reset ICANON here, otherwise on Solaris select() won't see
3626 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003627 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003628 tnew.c_cc[VMIN] = 1; // return after 1 char
3629 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
3632# if defined(HAVE_TERMIOS_H)
3633 {
3634 int n = 10;
3635
Bram Moolenaar0f873732019-12-05 20:28:46 +01003636 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3637 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3639 && errno == EINTR && n > 0)
3640 --n;
3641 }
3642# else
3643 ioctl(read_cmd_fd, TCSETA, &tnew);
3644# endif
3645
3646#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 /*
3648 * for "old" tty systems
3649 */
3650# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003651# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652# endif
3653 static struct sgttyb ttybold;
3654 struct sgttyb ttybnew;
3655
3656 if (first)
3657 {
3658 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003659 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 }
3661
3662 ttybnew = ttybold;
3663 if (tmode == TMODE_RAW)
3664 {
3665 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3666 ttybnew.sg_flags |= RAW;
3667 }
3668 else if (tmode == TMODE_SLEEP)
3669 ttybnew.sg_flags &= ~(ECHO);
3670 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3671#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003672 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673}
3674
3675/*
3676 * Try to get the code for "t_kb" from the stty setting
3677 *
3678 * Even if termcap claims a backspace key, the user's setting *should*
3679 * prevail. stty knows more about reality than termcap does, and if
3680 * somebody's usual erase key is DEL (which, for most BSD users, it will
3681 * be), they're going to get really annoyed if their erase key starts
3682 * doing forward deletes for no reason. (Eric Fischer)
3683 */
3684 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003685get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003687 ttyinfo_T info;
3688 char_u buf[2];
3689 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003691 if (get_tty_info(read_cmd_fd, &info) == OK)
3692 {
3693 intr_char = info.interrupt;
3694 buf[0] = info.backspace;
3695 buf[1] = NUL;
3696 add_termcode((char_u *)"kb", buf, FALSE);
3697
Bram Moolenaar0f873732019-12-05 20:28:46 +01003698 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003699 p = find_termcode((char_u *)"kD");
3700 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3701 do_fixdel(NULL);
3702 }
3703}
3704
3705/*
3706 * Obtain the characters that Backspace and Enter produce on "fd".
3707 * Returns OK or FAIL.
3708 */
3709 int
3710get_tty_info(int fd, ttyinfo_T *info)
3711{
3712#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713# ifdef HAVE_TERMIOS_H
3714 struct termios keys;
3715# else
3716 struct termio keys;
3717# endif
3718
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003719 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003721 info->backspace = keys.c_cc[VERASE];
3722 info->interrupt = keys.c_cc[VINTR];
3723 if (keys.c_iflag & ICRNL)
3724 info->enter = NL;
3725 else
3726 info->enter = CAR;
3727 if (keys.c_oflag & ONLCR)
3728 info->nl_does_cr = TRUE;
3729 else
3730 info->nl_does_cr = FALSE;
3731 return OK;
3732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003734 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 struct sgttyb keys;
3736
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003737 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003739 info->backspace = keys.sg_erase;
3740 info->interrupt = keys.sg_kill;
3741 info->enter = CAR;
3742 info->nl_does_cr = TRUE;
3743 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003746 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747}
3748
Bram Moolenaar0f873732019-12-05 20:28:46 +01003749#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003751static int mouse_ison = FALSE;
3752
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753/*
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003754 * Set mouse clicks on or off and possible enable mouse movement events.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 */
3756 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003757mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003759#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003760 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 int xterm_mouse_vers;
3763
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003764#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003765 if (!on)
3766 // Make sure not tracing mouse movements. Important when a button-down
3767 // was received but no release yet.
3768 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003769#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003770
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003771 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003772#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003773 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003774#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003775 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003776 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 return;
3778
3779 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003780
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003781#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003782 if (ttym_flags == TTYM_URXVT)
3783 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003784 out_str_nf((char_u *)(on ? "\033[?1015h" : "\033[?1015l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003785 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003786 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003787#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003788
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003789 if (ttym_flags == TTYM_SGR)
3790 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003791 // SGR mode supports columns above 223
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003792 out_str_nf((char_u *)(on ? "\033[?1006h" : "\033[?1006l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003793 mouse_ison = on;
3794 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003795
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003796#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003797 if (bevalterm_ison != (p_bevalterm && on))
3798 {
3799 bevalterm_ison = (p_bevalterm && on);
3800 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003801 // disable mouse movement events, enabling is below
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003802 out_str_nf((char_u *)("\033[?1003l"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003803 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003804#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (xterm_mouse_vers > 0)
3807 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003808 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 out_str_nf((char_u *)
3810 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003811 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003812#ifdef FEAT_BEVAL_TERM
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003813 bevalterm_ison ? "\033[?1003h" :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003814#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003815 "\033[?1002h")
3816 : "\033[?1000h"));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003817 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 out_str_nf((char_u *)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003819 (xterm_mouse_vers > 1 ? "\033[?1002l" : "\033[?1000l"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003820 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003823#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 else if (ttym_flags == TTYM_DEC)
3825 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003826 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003828 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003830 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003834#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 else
3836 {
3837 if (on)
3838 {
3839 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003840 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 else
3843 {
3844 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003845 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
3847 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003850#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003851 else
3852 {
3853 if (on)
3854 {
3855 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003856 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003857 }
3858 else
3859 {
3860 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003861 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003862 }
3863 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003864#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003865
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003866#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 else
3868 {
3869 if (on)
3870 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003871 // D - Enable Mouse up/down messages
3872 // L - Enable Left Button Reporting
3873 // M - Enable Middle Button Reporting
3874 // R - Enable Right Button Reporting
3875 // K - Enable SHIFT and CTRL key Reporting
3876 // + - Enable Advanced messaging of mouse moves and up/down messages
3877 // Q - Quiet No Ack
3878 // # - Numeric value of mouse pointer required
3879 // 0 = Multiview 2000 cursor, used as standard
3880 // 1 = Windows Arrow
3881 // 2 = Windows I Beam
3882 // 3 = Windows Hour Glass
3883 // 4 = Windows Cross Hair
3884 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003885# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003886 // Disables full feedback of pointer movements
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003887 out_str_nf((char_u *)"\033[0~ZwLMRK1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003888# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003889 out_str_nf((char_u *)"\033[0~ZwLMRK+1Q\033\\");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003890# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003891 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893 else
3894 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003895 out_str_nf((char_u *)"\033[0~ZwQ\033\\");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003896 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
3898 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003899#endif
3900#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 else
3902 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003903 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 if (on)
3905 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3906 else
3907 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003908 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911}
3912
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003913#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003914/*
3915 * Called when 'balloonevalterm' changed.
3916 */
3917 void
3918mch_bevalterm_changed(void)
3919{
3920 mch_setmouse(mouse_ison);
3921}
3922#endif
3923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924/*
3925 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3926 */
3927 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003928check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929{
3930# ifdef FEAT_MOUSE_XTERM
3931 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003932# ifdef FEAT_MOUSE_URXVT
3933 && use_xterm_mouse() != 3
3934# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935# ifdef FEAT_GUI
3936 && !gui.in_use
3937# endif
3938 )
3939 {
3940 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003941 ? "\233M" : "\033[M"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if (*p_mouse != NUL)
3943 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003944 // force mouse off and maybe on to send possibly new mouse
3945 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 mch_setmouse(FALSE);
3947 setmouse();
3948 }
3949 }
3950 else
3951 del_mouse_termcode(KS_MOUSE);
3952# endif
3953
3954# ifdef FEAT_MOUSE_GPM
3955 if (!use_xterm_mouse()
3956# ifdef FEAT_GUI
3957 && !gui.in_use
3958# endif
3959 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003960 set_mouse_termcode(KS_GPM_MOUSE, (char_u *)"\033MG");
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003961 else
3962 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963# endif
3964
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003965# ifdef FEAT_SYSMOUSE
3966 if (!use_xterm_mouse()
3967# ifdef FEAT_GUI
3968 && !gui.in_use
3969# endif
3970 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003971 set_mouse_termcode(KS_MOUSE, (char_u *)"\033MS");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003972# endif
3973
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003975 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 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_JSBTERM_MOUSE, (char_u *)"\033[0~zw");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 else
3983 del_mouse_termcode(KS_JSBTERM_MOUSE);
3984# endif
3985
3986# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003987 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3988 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 if (!use_xterm_mouse()
3990# ifdef FEAT_GUI
3991 && !gui.in_use
3992# endif
3993 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003994 set_mouse_termcode(KS_NETTERM_MOUSE, (char_u *)"\033}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 else
3996 del_mouse_termcode(KS_NETTERM_MOUSE);
3997# endif
3998
3999# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01004000 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004001 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002# ifdef FEAT_GUI
4003 && !gui.in_use
4004# endif
4005 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004006 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004007 ? "\233" : "\033["));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 else
4009 del_mouse_termcode(KS_DEC_MOUSE);
4010# endif
4011# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01004012 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004013 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014# ifdef FEAT_GUI
4015 && !gui.in_use
4016# endif
4017 )
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004018 set_mouse_termcode(KS_PTERM_MOUSE, (char_u *)"\033[");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 else
4020 del_mouse_termcode(KS_PTERM_MOUSE);
4021# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004022# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004023 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004024# ifdef FEAT_GUI
4025 && !gui.in_use
4026# endif
4027 )
4028 {
4029 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004030 ? "\233*M" : "\033[*M"));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004031
4032 if (*p_mouse != NUL)
4033 {
4034 mch_setmouse(FALSE);
4035 setmouse();
4036 }
4037 }
4038 else
4039 del_mouse_termcode(KS_URXVT_MOUSE);
4040# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004041 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004042# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004043 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004044# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004045 )
4046 {
4047 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004048 ? "\233<*M" : "\033[<*M"));
Bram Moolenaara529ce02017-06-22 22:37:57 +02004049
4050 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004051 ? "\233<*m" : "\033[<*m"));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004052
4053 if (*p_mouse != NUL)
4054 {
4055 mch_setmouse(FALSE);
4056 setmouse();
4057 }
4058 }
4059 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004060 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004061 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004062 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066#ifndef VMS
4067
4068/*
4069 * Try to get the current window size:
4070 * 1. with an ioctl(), most accurate method
4071 * 2. from the environment variables LINES and COLUMNS
4072 * 3. from the termcap
4073 * 4. keep using the old values
4074 * Return OK when size could be determined, FAIL otherwise.
4075 */
4076 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004077mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078{
4079 long rows = 0;
4080 long columns = 0;
4081 char_u *p;
4082
4083 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 * 1. try using an ioctl. It is the most accurate method.
4085 *
4086 * Try using TIOCGWINSZ first, some systems that have it also define
4087 * TIOCGSIZE but don't have a struct ttysize.
4088 */
4089# ifdef TIOCGWINSZ
4090 {
4091 struct winsize ws;
4092 int fd = 1;
4093
Bram Moolenaar0f873732019-12-05 20:28:46 +01004094 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 if (!isatty(fd) && isatty(read_cmd_fd))
4096 fd = read_cmd_fd;
4097 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4098 {
4099 columns = ws.ws_col;
4100 rows = ws.ws_row;
4101 }
4102 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004103# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104# ifdef TIOCGSIZE
4105 {
4106 struct ttysize ts;
4107 int fd = 1;
4108
Bram Moolenaar0f873732019-12-05 20:28:46 +01004109 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 if (!isatty(fd) && isatty(read_cmd_fd))
4111 fd = read_cmd_fd;
4112 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4113 {
4114 columns = ts.ts_cols;
4115 rows = ts.ts_lines;
4116 }
4117 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004118# endif // TIOCGSIZE
4119# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
4121 /*
4122 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004123 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4124 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004126 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 {
4128 if ((p = (char_u *)getenv("LINES")))
4129 rows = atoi((char *)p);
4130 if ((p = (char_u *)getenv("COLUMNS")))
4131 columns = atoi((char *)p);
4132 }
4133
4134#ifdef HAVE_TGETENT
4135 /*
4136 * 3. try reading "co" and "li" entries from termcap
4137 */
4138 if (columns == 0 || rows == 0)
4139 getlinecol(&columns, &rows);
4140#endif
4141
4142 /*
4143 * 4. If everything fails, use the old values
4144 */
4145 if (columns <= 0 || rows <= 0)
4146 return FAIL;
4147
4148 Rows = rows;
4149 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004150 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 return OK;
4152}
4153
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004154#if defined(FEAT_TERMINAL) || defined(PROTO)
4155/*
4156 * Report the windows size "rows" and "cols" to tty "fd".
4157 */
4158 int
4159mch_report_winsize(int fd, int rows, int cols)
4160{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004161 int tty_fd;
4162 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004163
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004164 tty_fd = get_tty_fd(fd);
4165 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004166 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004167# if defined(TIOCSWINSZ)
4168 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004169
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004170 ws.ws_col = cols;
4171 ws.ws_row = rows;
4172 ws.ws_xpixel = cols * 5;
4173 ws.ws_ypixel = rows * 10;
4174 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4175 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4176 retval == 0 ? "success" : "failed");
4177# elif defined(TIOCSSIZE)
4178 struct ttysize ts;
4179
4180 ts.ts_cols = cols;
4181 ts.ts_lines = rows;
4182 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4183 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4184 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004185# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004186 if (tty_fd != fd)
4187 close(tty_fd);
4188 }
4189 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004190}
4191#endif
4192
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193/*
4194 * Try to set the window size to Rows and Columns.
4195 */
4196 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004197mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198{
4199 if (*T_CWS)
4200 {
4201 /*
4202 * NOTE: if you get an error here that term_set_winsize() is
4203 * undefined, check the output of configure. It could probably not
4204 * find a ncurses, termcap or termlib library.
4205 */
4206 term_set_winsize((int)Rows, (int)Columns);
4207 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004208 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
4210}
4211
Bram Moolenaar0f873732019-12-05 20:28:46 +01004212#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
4214/*
4215 * Rows and/or Columns has changed.
4216 */
4217 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004218mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004220 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221}
4222
Bram Moolenaar205b8862011-09-07 15:04:31 +02004223/*
4224 * Wait for process "child" to end.
4225 * Return "child" if it exited properly, <= 0 on error.
4226 */
4227 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004228wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004229{
4230 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004231 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004232
4233 while (wait_pid != child)
4234 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004235 // When compiled with Python threads are probably used, in which case
4236 // wait() sometimes hangs for no obvious reason. Use waitpid()
4237 // instead and loop (like the GUI). Also needed for other interfaces,
4238 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004239# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004240 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004241# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004242 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004243# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004244 if (wait_pid == 0)
4245 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004246 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004247 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004248 if (++delay_msec > 10)
4249 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004250 continue;
4251 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004252 if (wait_pid <= 0
4253# ifdef ECHILD
4254 && errno == ECHILD
4255# endif
4256 )
4257 break;
4258 }
4259 return wait_pid;
4260}
4261
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004262#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004263/*
4264 * Set the environment for a child process.
4265 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004266 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004267set_child_environment(
4268 long rows,
4269 long columns,
4270 char *term,
4271 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004272{
4273# ifdef HAVE_SETENV
4274 char envbuf[50];
4275# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004276 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004277 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004278 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004279 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004280 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004281# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004282 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004283# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004284# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004285 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004286# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004287# endif
4288
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004289# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004290 setenv("TERM", term, 1);
4291 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004292 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004293 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004294 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004295 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004296 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004297 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004298 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004299# ifdef FEAT_TERMINAL
4300 if (is_terminal)
4301 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004302 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004303 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4304 }
4305# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004306# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004307 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004308# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004309# else
4310 /*
4311 * Putenv does not copy the string, it has to remain valid.
4312 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004313 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004314 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004315 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4316 putenv(envbuf_Term);
4317 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004318 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004319 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4320 putenv(envbuf_Lines);
4321 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4322 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004323 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004324 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004325 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004326# ifdef FEAT_TERMINAL
4327 if (is_terminal)
4328 {
4329 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004330 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004331 putenv(envbuf_Version);
4332 }
4333# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004334# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004335 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4336 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4337 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004338# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004339# endif
4340}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004341
4342 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004343set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004344{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004345 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004346}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004347#endif
4348
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004349#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004350/*
4351 * Open a PTY, with FD for the master and slave side.
4352 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004353 * When successful both file descriptors are stored and the allocated pty name
4354 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004355 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004356 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004357open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004358{
4359 char *tty_name;
4360
Bram Moolenaar59386482019-02-10 22:43:46 +01004361 if (name1 != NULL)
4362 *name1 = NULL;
4363 if (name2 != NULL)
4364 *name2 = NULL;
4365
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004366 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004367 if (*pty_master_fd >= 0)
4368 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004369 // Leaving out O_NOCTTY may lead to waitpid() always returning
4370 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4371 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004372#ifdef O_NOCTTY
4373 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4374#else
4375 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4376#endif
4377 if (*pty_slave_fd < 0)
4378 {
4379 close(*pty_master_fd);
4380 *pty_master_fd = -1;
4381 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004382 else
4383 {
4384 if (name1 != NULL)
4385 *name1 = vim_strsave((char_u *)tty_name);
4386 if (name2 != NULL)
4387 *name2 = vim_strsave((char_u *)tty_name);
4388 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004389 }
4390}
4391#endif
4392
Bram Moolenaarfae42832017-08-01 22:24:26 +02004393/*
4394 * Send SIGINT to a child process if "c" is an interrupt character.
4395 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004396 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004397may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4398{
4399# ifdef SIGINT
4400 if (c == Ctrl_C || c == intr_char)
4401 {
4402# ifdef HAVE_SETSID
4403 kill(-pid, SIGINT);
4404# else
4405 kill(0, SIGINT);
4406# endif
4407 if (wpid > 0)
4408 kill(wpid, SIGINT);
4409 }
4410# endif
4411}
4412
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004413#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004414
Bram Moolenaar0f873732019-12-05 20:28:46 +01004415/*
4416 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4417 * pointers, the last one is NULL.
4418 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4419 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004420 int
4421unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004422 char_u *cmd,
4423 char ***argvp,
4424 char_u **sh_tofree,
4425 char_u **shcf_tofree)
4426{
4427 char **argv = NULL;
4428 int argc;
4429
4430 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004431 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004432 return FAIL;
4433
4434 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4435 return FAIL;
4436 *argvp = argv;
4437
4438 if (cmd != NULL)
4439 {
4440 char_u *s;
4441 char_u *p;
4442
4443 if (extra_shell_arg != NULL)
4444 argv[argc++] = (char *)extra_shell_arg;
4445
Bram Moolenaar0f873732019-12-05 20:28:46 +01004446 // Break 'shellcmdflag' into white separated parts. This doesn't
4447 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004448 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004449 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004450 return FAIL;
4451 s = *shcf_tofree;
4452 p = p_shcf;
4453 while (*p != NUL)
4454 {
4455 argv[argc++] = (char *)s;
4456 while (*p && *p != ' ' && *p != TAB)
4457 *s++ = *p++;
4458 *s++ = NUL;
4459 p = skipwhite(p);
4460 }
4461
4462 argv[argc++] = (char *)cmd;
4463 }
4464 argv[argc] = NULL;
4465 return OK;
4466}
4467#endif
4468
4469#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4470/*
4471 * Use a terminal window to run a shell command in.
4472 */
4473 static int
4474mch_call_shell_terminal(
4475 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004476 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004477{
4478 jobopt_T opt;
4479 char **argv = NULL;
4480 char_u *tofree1 = NULL;
4481 char_u *tofree2 = NULL;
4482 int retval = -1;
4483 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004484 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004485 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004486 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004487
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004488 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004489 goto theend;
4490
4491 init_job_options(&opt);
4492 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4493 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004494 if (buf == NULL)
4495 goto theend;
4496
4497 job = term_getjob(buf->b_term);
4498 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004499
Bram Moolenaar0f873732019-12-05 20:28:46 +01004500 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004501 aucmd_prepbuf(&aco, buf);
4502
4503 clear_oparg(&oa);
4504 while (term_use_loop())
4505 {
4506 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4507 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004508 // If terminal_loop() returns OK we got a key that is handled
4509 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004510 if (terminal_loop(TRUE) == OK)
4511 normal_cmd(&oa, TRUE);
4512 }
4513 else
4514 normal_cmd(&oa, TRUE);
4515 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004516 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004517 ch_log(NULL, "system command finished");
4518
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004519 job_unref(job);
4520
Bram Moolenaar0f873732019-12-05 20:28:46 +01004521 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004522 aucmd_restbuf(&aco);
4523
4524 wait_return(TRUE);
4525 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4526
4527theend:
4528 vim_free(argv);
4529 vim_free(tofree1);
4530 vim_free(tofree2);
4531 return retval;
4532}
4533#endif
4534
4535#ifdef USE_SYSTEM
4536/*
4537 * Use system() to start the shell: simple but slow.
4538 */
4539 static int
4540mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004541 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004542 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543{
4544#ifdef VMS
4545 char *ifn = NULL;
4546 char *ofn = NULL;
4547#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004548 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004549 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004550 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
4552 out_flush();
4553
4554 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004555 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
Bram Moolenaar62b42182010-09-21 22:09:37 +02004557# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004558 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004559 loose_clipboard();
4560# endif
4561
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 if (cmd == NULL)
4563 x = system((char *)p_sh);
4564 else
4565 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004566# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (ofn = strchr((char *)cmd, '>'))
4568 *ofn++ = '\0';
4569 if (ifn = strchr((char *)cmd, '<'))
4570 {
4571 char *p;
4572
4573 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004574 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 if (p)
4576 *p = '\0';
4577 }
4578 if (ofn)
4579 x = vms_sys((char *)cmd, ofn, ifn);
4580 else
4581 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004582# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004583 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004585 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 if (newcmd == NULL)
4587 x = 0;
4588 else
4589 {
4590 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4591 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4592 (char *)p_shcf,
4593 (char *)cmd);
4594 x = system((char *)newcmd);
4595 vim_free(newcmd);
4596 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004597# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
4599# ifdef VMS
4600 x = vms_sys_status(x);
4601# endif
4602 if (emsg_silent)
4603 ;
4604 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004605 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 else if (x && !(options & SHELL_SILENT))
4607 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004608 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 msg_outnum((long)x);
4610 msg_putchar('\n');
4611 }
4612
4613 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004614 {
4615 // The shell may have messed with the mode, always set it.
4616 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004617 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004620# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4621 restore_clipboard();
4622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004624}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
Bram Moolenaar0f873732019-12-05 20:28:46 +01004626#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
Bram Moolenaar0f873732019-12-05 20:28:46 +01004628# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4629 // 127, some shells use that already
4630# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
Bram Moolenaar13568252018-03-16 20:46:58 +01004632/*
4633 * Don't use system(), use fork()/exec().
4634 */
4635 static int
4636mch_call_shell_fork(
4637 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004638 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004639{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004640 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004642 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 pid_t wait_pid = 0;
4644# ifdef HAVE_UNION_WAIT
4645 union wait status;
4646# else
4647 int status = -1;
4648# endif
4649 int retval = -1;
4650 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004651 char_u *tofree1 = NULL;
4652 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004654 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004655# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004657# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004658 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 int fd_fromshell[2];
4660 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004661 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662
4663 out_flush();
4664 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004665 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004666 if (tmode == TMODE_RAW)
4667 // The shell may have messed with the mode, always set it later.
4668 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004670 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004671 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004672
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004674 * For the GUI, when writing the output into the buffer and when reading
4675 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4676 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004678 if ((options & (SHELL_READ|SHELL_WRITE))
4679# ifdef FEAT_GUI
4680 || (gui.in_use && show_shell_mess)
4681# endif
4682 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004684# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 /*
4686 * Try to open a master pty.
4687 * If this works, open the slave pty.
4688 * If the slave can't be opened, close the master pty.
4689 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004690 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004691 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 /*
4693 * If not opening a pty or it didn't work, try using pipes.
4694 */
4695 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
4698 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004699 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
4701 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004702 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
4704 close(fd_toshell[0]);
4705 close(fd_toshell[1]);
4706 }
4707 }
4708 if (pipe_error)
4709 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004710 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 out_flush();
4712 }
4713 }
4714 }
4715
Bram Moolenaar0f873732019-12-05 20:28:46 +01004716 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004718 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004719 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004720 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004721 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004723 UNBLOCK_SIGNALS(&curset);
4724
Bram Moolenaar32526b32019-01-19 17:43:09 +01004725 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004726 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004728 || (gui.in_use && show_shell_mess)
4729# endif
4730 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004732# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004733 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
4735 close(pty_master_fd);
4736 close(pty_slave_fd);
4737 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004738 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004739# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
4741 close(fd_toshell[0]);
4742 close(fd_toshell[1]);
4743 close(fd_fromshell[0]);
4744 close(fd_fromshell[1]);
4745 }
4746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004748 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004750 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004751 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
Bram Moolenaar819524702018-02-27 19:10:00 +01004753# ifdef FEAT_JOB_CHANNEL
4754 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004755 {
4756 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004757 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004758 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004759# endif
4760
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (!show_shell_mess || (options & SHELL_EXPAND))
4762 {
4763 int fd;
4764
4765 /*
4766 * Don't want to show any message from the shell. Can't just
4767 * close stdout and stderr though, because some systems will
4768 * break if you try to write to them after that, so we must
4769 * use dup() to replace them with something else -- webb
4770 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4771 * waiting for input.
4772 */
4773 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4774 fclose(stdin);
4775 fclose(stdout);
4776 fclose(stderr);
4777
4778 /*
4779 * If any of these open()'s and dup()'s fail, we just continue
4780 * anyway. It's not fatal, and on most systems it will make
4781 * no difference at all. On a few it will cause the execvp()
4782 * to exit with a non-zero status even when the completion
4783 * could be done, which is nothing too serious. If the open()
4784 * or dup() failed we'd just do the same thing ourselves
4785 * anyway -- webb
4786 */
4787 if (fd >= 0)
4788 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004789 vim_ignored = dup(fd); // To replace stdin (fd 0)
4790 vim_ignored = dup(fd); // To replace stdout (fd 1)
4791 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792
Bram Moolenaar0f873732019-12-05 20:28:46 +01004793 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 close(fd);
4795 }
4796 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004797 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004799 || gui.in_use
4800# endif
4801 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 {
4803
Bram Moolenaardf177f62005-02-22 08:39:57 +00004804# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004805 // Create our own process group, so that the child and all its
4806 // children can be kill()ed. Don't do this when using pipes,
4807 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004808 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004809 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004810 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004811# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004812 // When doing "!xterm&" and 'shell' is bash: the shell
4813 // will exit and send SIGHUP to all processes in its
4814 // group, killing the just started process. Ignore SIGHUP
4815 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004816 signal(SIGHUP, SIG_IGN);
4817# endif
4818 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004819# endif
4820# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004821 if (pty_slave_fd >= 0)
4822 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004823 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004824 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004825 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004826# ifdef TIOCSCTTY
4827 // Try to become controlling tty (probably doesn't work,
4828 // unless run by root)
4829 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4830# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004831 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004832# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004833 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
Bram Moolenaara5792f52005-11-23 21:25:05 +00004835 /*
4836 * stderr is only redirected when using the GUI, so that a
4837 * program like gpg can still access the terminal to get a
4838 * passphrase using stderr.
4839 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004840# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 if (pty_master_fd >= 0)
4842 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004843 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
Bram Moolenaar0f873732019-12-05 20:28:46 +01004845 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004847 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004849 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004850 if (gui.in_use)
4851 {
4852 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004853 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
Bram Moolenaar0f873732019-12-05 20:28:46 +01004856 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
4858 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004859# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004861 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 close(fd_toshell[1]);
4863 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004864 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 close(fd_toshell[0]);
4866
Bram Moolenaar0f873732019-12-05 20:28:46 +01004867 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 close(fd_fromshell[0]);
4869 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004870 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 close(fd_fromshell[1]);
4872
Bram Moolenaara5792f52005-11-23 21:25:05 +00004873# ifdef FEAT_GUI
4874 if (gui.in_use)
4875 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004876 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004877 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004878 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004879 }
4880# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
4882 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004883
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 /*
4885 * There is no type cast for the argv, because the type may be
4886 * different on different machines. This may cause a warning
4887 * message with strict compilers, don't worry about it.
4888 * Call _exit() instead of exit() to avoid closing the connection
4889 * to the X server (esp. with GTK, which uses atexit()).
4890 */
4891 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004892 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004894 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 {
4896 /*
4897 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004898 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 */
4900 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004901 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004902 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004903# ifdef FEAT_JOB_CHANNEL
4904 ++dont_check_job_ended;
4905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 /*
4907 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004908 * This is also used to pipe stdin/stdout to/from the external
4909 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004911 if ((options & (SHELL_READ|SHELL_WRITE))
4912# ifdef FEAT_GUI
4913 || (gui.in_use && show_shell_mess)
4914# endif
4915 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004917# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004919 int buffer_off = 0; // valid bytes in buffer[]
4920 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4921 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 int len;
4923 int p_more_save;
4924 int old_State;
4925 int c;
4926 int toshell_fd;
4927 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004928 garray_T ga;
4929 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004930# ifdef ELAPSED_FUNC
4931 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
Bram Moolenaardf177f62005-02-22 08:39:57 +00004934# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 if (pty_master_fd >= 0)
4936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 fromshell_fd = pty_master_fd;
4938 toshell_fd = dup(pty_master_fd);
4939 }
4940 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
4943 close(fd_toshell[0]);
4944 close(fd_fromshell[1]);
4945 toshell_fd = fd_toshell[1];
4946 fromshell_fd = fd_fromshell[0];
4947 }
4948
4949 /*
4950 * Write to the child if there are typed characters.
4951 * Read from the child if there are characters available.
4952 * Repeat the reading a few times if more characters are
4953 * available. Need to check for typed keys now and then, but
4954 * not too often (delays when no chars are available).
4955 * This loop is quit if no characters can be read from the pty
4956 * (WaitForChar detected special condition), or there are no
4957 * characters available and the child has exited.
4958 * Only check if the child has exited when there is no more
4959 * output. The child may exit before all the output has
4960 * been printed.
4961 *
4962 * Currently this busy loops!
4963 * This can probably dead-lock when the write blocks!
4964 */
4965 p_more_save = p_more;
4966 p_more = FALSE;
4967 old_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004968 State = MODE_EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004970 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004971 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004972 // Fork a process that will write the lines to the
4973 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004974 if ((wpid = fork()) == -1)
4975 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004976 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004977 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004978 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004979 {
4980 linenr_T lnum = curbuf->b_op_start.lnum;
4981 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004982 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004983 size_t l;
4984
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004985 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004986 for (;;)
4987 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004988 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004989 if (l == 0)
4990 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004991 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004992 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004993 len = write(toshell_fd, "", (size_t)1);
4994 else
4995 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004996 char_u *s = vim_strchr(lp + written, NL);
4997
Bram Moolenaar89d40322006-08-29 15:30:07 +00004998 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004999 s == NULL ? l
5000 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005001 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00005002 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005003 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005004 // Finished a line, add a NL, unless this line
5005 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005006 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005007 || (!curbuf->b_p_bin
5008 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005009 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00005010 && (lnum !=
5011 curbuf->b_ml.ml_line_count
5012 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02005013 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005014 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005015 ++lnum;
5016 if (lnum > curbuf->b_op_end.lnum)
5017 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005018 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005019 close(toshell_fd);
5020 toshell_fd = -1;
5021 break;
5022 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005023 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005024 written = 0;
5025 }
5026 else if (len > 0)
5027 written += len;
5028 }
5029 _exit(0);
5030 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005031 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005032 {
5033 close(toshell_fd);
5034 toshell_fd = -1;
5035 }
5036 }
5037
5038 if (options & SHELL_READ)
5039 ga_init2(&ga, 1, BUFLEN);
5040
5041 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005042# ifdef ELAPSED_FUNC
5043 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 for (;;)
5046 {
5047 /*
5048 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005049 * if there are any.
5050 * Don't do this if we are expanding wild cards (would eat
5051 * typeahead).
5052 * Don't do this when filtering and terminal is in cooked
5053 * mode, the shell command will handle the I/O. Avoids
5054 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005055 * Don't get characters when the child has already
5056 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005057 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005058 * while (noread_cnt > 4), avoids that ":r !ls" eats
5059 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 */
5061 len = 0;
5062 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005063 && ((options &
5064 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5065 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005066# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005067 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005068# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005069 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005070 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005071 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005073 if (ta_len == 0)
5074 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005075 // Get extra characters when we don't have any.
5076 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005077 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005078# ifdef ELAPSED_FUNC
5079 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005080# endif
5081 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5082 }
5083 if (ta_len > 0 || len > 0)
5084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 /*
5086 * For pipes:
5087 * Check for CTRL-C: send interrupt signal to child.
5088 * Check for CTRL-D: EOF, close pipe to child.
5089 */
5090 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 /*
5093 * Send SIGINT to the child's group or all
5094 * processes in our group.
5095 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005096 may_send_sigint(ta_buf[ta_len], pid, wpid);
5097
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 if (pty_master_fd < 0 && toshell_fd >= 0
5099 && ta_buf[ta_len] == Ctrl_D)
5100 {
5101 close(toshell_fd);
5102 toshell_fd = -1;
5103 }
5104 }
5105
Bram Moolenaarf4140482020-02-15 23:06:45 +01005106 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
5108 /*
5109 * For pipes: echo the typed characters.
5110 * For a pty this does not seem to work.
5111 */
5112 if (pty_master_fd < 0)
5113 {
5114 for (i = ta_len; i < ta_len + len; ++i)
5115 {
5116 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5117 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 else if (has_mbyte)
5119 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005120 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121
5122 msg_outtrans_len(ta_buf + i, l);
5123 i += l - 1;
5124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 else
5126 msg_outtrans_len(ta_buf + i, 1);
5127 }
5128 windgoto(msg_row, msg_col);
5129 out_flush();
5130 }
5131
5132 ta_len += len;
5133
5134 /*
5135 * Write the characters to the child, unless EOF has
5136 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005137 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005138 * When writing buffer lines, drop the typed
5139 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005141 if (options & SHELL_WRITE)
5142 ta_len = 0;
5143 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
5145 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5146 if (len > 0)
5147 {
5148 ta_len -= len;
5149 mch_memmove(ta_buf, ta_buf + len, ta_len);
5150 }
5151 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154
Bram Moolenaardf177f62005-02-22 08:39:57 +00005155 if (got_int)
5156 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005157 // CTRL-C sends a signal to the child, we ignore it
5158 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005159# ifdef HAVE_SETSID
5160 kill(-pid, SIGINT);
5161# else
5162 kill(0, SIGINT);
5163# endif
5164 if (wpid > 0)
5165 kill(wpid, SIGINT);
5166 got_int = FALSE;
5167 }
5168
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 /*
5170 * Check if the child has any characters to be printed.
5171 * Read them and write them to our window. Repeat this as
5172 * long as there is something to do, avoid the 10ms wait
5173 * for mch_inchar(), or sending typeahead characters to
5174 * the external process.
5175 * TODO: This should handle escape sequences, compatible
5176 * to some terminal (vt52?).
5177 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005178 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005179 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005181 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005184 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005186
5187 noread_cnt = 0;
5188 if (options & SHELL_READ)
5189 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005190 // Do NUL -> NL translation, append NL separated
5191 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005192 for (i = 0; i < len; ++i)
5193 {
5194 if (buffer[i] == NL)
5195 append_ga_line(&ga);
5196 else if (buffer[i] == NUL)
5197 ga_append(&ga, NL);
5198 else
5199 ga_append(&ga, buffer[i]);
5200 }
5201 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005202 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
5204 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005205 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
Bram Moolenaardf177f62005-02-22 08:39:57 +00005207 len += buffer_off;
5208 buffer[len] = NUL;
5209
Bram Moolenaar0f873732019-12-05 20:28:46 +01005210 // Check if the last character in buffer[] is
5211 // incomplete, keep these bytes for the next
5212 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 for (p = buffer; p < buffer + len; p += l)
5214 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005215 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005217 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 else if (MB_BYTE2LEN(*p) != l)
5219 break;
5220 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005221 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005223 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 if (len >= 12)
5225 ++p;
5226 else
5227 {
5228 buffer_off = len;
5229 continue;
5230 }
5231 }
5232 c = *p;
5233 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005234 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 if (p < buffer + len)
5236 {
5237 *p = c;
5238 buffer_off = (buffer + len) - p;
5239 mch_memmove(buffer, p, buffer_off);
5240 continue;
5241 }
5242 buffer_off = 0;
5243 }
5244 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 {
5246 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005247 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 }
5249
5250 windgoto(msg_row, msg_col);
5251 cursor_on();
5252 out_flush();
5253 if (got_int)
5254 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005255
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005256# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005257 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005258 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005259 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005260
Bram Moolenaar0f873732019-12-05 20:28:46 +01005261 // Avoid that we keep looping here without
5262 // checking for a CTRL-C for a long time. Don't
5263 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005264 if (msec > 2000)
5265 {
5266 noread_cnt = 5;
5267 break;
5268 }
5269 }
5270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
5272
Bram Moolenaar0f873732019-12-05 20:28:46 +01005273 // If we already detected the child has finished, continue
5274 // reading output for a short while. Some text may be
5275 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005276 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005277 {
5278 if (noread_cnt < 5)
5279 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005280 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005281 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005282
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 /*
5284 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005285 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005287# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005288 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005289# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005291# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5293 || (wait_pid == pid && WIFEXITED(status)))
5294 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005295 // Don't break the loop yet, try reading more
5296 // characters from "fromshell_fd" first. When using
5297 // pipes there might still be something to read and
5298 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005301 else
5302 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005303
Bram Moolenaar95a51352013-03-21 22:53:50 +01005304# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005305 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005306 clip_update();
5307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
5309finished:
5310 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005311 if (options & SHELL_READ)
5312 {
5313 if (ga.ga_len > 0)
5314 {
5315 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005316 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005317 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005318 }
5319 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005320 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005321 ga_clear(&ga);
5322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 /*
5325 * Give all typeahead that wasn't used back to ui_inchar().
5326 */
5327 if (ta_len)
5328 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 State = old_State;
5330 if (toshell_fd >= 0)
5331 close(toshell_fd);
5332 close(fromshell_fd);
5333 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005334# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005335 else
5336 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005337 long delay_msec = 1;
5338
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005339 if (tmode == TMODE_RAW)
5340 // possibly disables modifyOtherKeys, so that the system
5341 // can recognize CTRL-C
5342 out_str(T_CTE);
Bram Moolenaar0981c872020-08-23 14:28:37 +02005343
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005344 /*
5345 * Similar to the loop above, but only handle X events, no
5346 * I/O.
5347 */
5348 for (;;)
5349 {
5350 if (got_int)
5351 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005352 // CTRL-C sends a signal to the child, we ignore it
5353 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005354# ifdef HAVE_SETSID
5355 kill(-pid, SIGINT);
5356# else
5357 kill(0, SIGINT);
5358# endif
5359 got_int = FALSE;
5360 }
5361# ifdef __NeXT__
5362 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5363# else
5364 wait_pid = waitpid(pid, &status, WNOHANG);
5365# endif
5366 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5367 || (wait_pid == pid && WIFEXITED(status)))
5368 {
5369 wait_pid = pid;
5370 break;
5371 }
5372
Bram Moolenaar0f873732019-12-05 20:28:46 +01005373 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005374 clip_update();
5375
Bram Moolenaar0f873732019-12-05 20:28:46 +01005376 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005377 // less time, gradually wait longer.
5378 mch_delay(delay_msec,
5379 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005380 if (++delay_msec > 10)
5381 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005382 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005383
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005384 if (tmode == TMODE_RAW)
5385 // possibly enables modifyOtherKeys again
5386 out_str(T_CTI);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005387 }
5388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389
5390 /*
5391 * Wait until our child has exited.
5392 * Ignore wait() returning pids of other children and returning
5393 * because of some signal like SIGWINCH.
5394 * Don't wait if wait_pid was already set above, indicating the
5395 * child already exited.
5396 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005397 if (wait_pid != pid)
5398 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399
Bram Moolenaar624891f2010-10-13 16:22:09 +02005400# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005401 // Close slave side of pty. Only do this after the child has
5402 // exited, otherwise the child may hang when it tries to write on
5403 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005404 if (pty_master_fd >= 0)
5405 close(pty_slave_fd);
5406# endif
5407
Bram Moolenaar0f873732019-12-05 20:28:46 +01005408 // Make sure the child that writes to the external program is
5409 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005410 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005411 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005412 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005413 wait4pid(wpid, NULL);
5414 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005415
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005416# ifdef FEAT_JOB_CHANNEL
5417 --dont_check_job_ended;
5418# endif
5419
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 /*
5421 * Set to raw mode right now, otherwise a CTRL-C after
5422 * catch_signals() will kill Vim.
5423 */
5424 if (tmode == TMODE_RAW)
5425 settmode(TMODE_RAW);
5426 did_settmode = TRUE;
5427 set_signals();
5428
5429 if (WIFEXITED(status))
5430 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005431 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005433 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 {
5435 if (retval == EXEC_FAILED)
5436 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005437 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 msg_outtrans(p_sh);
5439 msg_putchar('\n');
5440 }
5441 else if (!(options & SHELL_SILENT))
5442 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005443 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 msg_outnum((long)retval);
5445 msg_putchar('\n');
5446 }
5447 }
5448 }
5449 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005450 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 }
5452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
5454error:
5455 if (!did_settmode)
5456 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005457 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005459 vim_free(argv);
5460 vim_free(tofree1);
5461 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005464}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005465#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005466
5467 int
5468mch_call_shell(
5469 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005470 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005471{
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01005472#ifdef FEAT_JOB_CHANNEL
5473 ch_log(NULL, "executing shell command: %s", cmd);
5474#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005475#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5476 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5477 return mch_call_shell_terminal(cmd, options);
5478#endif
5479#ifdef USE_SYSTEM
5480 return mch_call_shell_system(cmd, options);
5481#else
5482 return mch_call_shell_fork(cmd, options);
5483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484}
5485
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005486#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005487 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005488mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005489{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005490 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005491 int fd_in[2] = {-1, -1}; // for stdin
5492 int fd_out[2] = {-1, -1}; // for stdout
5493 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005494 int pty_master_fd = -1;
5495 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005496 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005497 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5498 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5499 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005500 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005501 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5502 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005503 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005504 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005505 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005506
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005507 if (use_out_for_err && use_null_for_out)
5508 use_null_for_err = TRUE;
5509
Bram Moolenaar0f873732019-12-05 20:28:46 +01005510 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005511 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005512
Bram Moolenaarb2412082017-08-20 18:09:14 +02005513 if (options->jo_pty
5514 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005515 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005516 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005517 open_pty(&pty_master_fd, &pty_slave_fd,
5518 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005519
Bram Moolenaar0f873732019-12-05 20:28:46 +01005520 // TODO: without the channel feature connect the child to /dev/null?
5521 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005522 if (use_file_for_in)
5523 {
5524 char_u *fname = options->jo_io_name[PART_IN];
5525
5526 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5527 if (fd_in[0] < 0)
5528 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005529 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005530 goto failed;
5531 }
5532 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005533 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005534 // When writing buffer lines to the input don't use the pty, so that
5535 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005536 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5537 && pipe(fd_in) < 0)
5538 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005539
5540 if (use_file_for_out)
5541 {
5542 char_u *fname = options->jo_io_name[PART_OUT];
5543
5544 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5545 if (fd_out[1] < 0)
5546 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005547 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005548 goto failed;
5549 }
5550 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005551 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005552 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005553
5554 if (use_file_for_err)
5555 {
5556 char_u *fname = options->jo_io_name[PART_ERR];
5557
5558 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5559 if (fd_err[1] < 0)
5560 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005561 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005562 goto failed;
5563 }
5564 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005565 else if (!use_out_for_err && !use_null_for_err
5566 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005567 goto failed;
5568
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005569 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5570 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005571 if (options->jo_set & JO_CHANNEL)
5572 {
5573 channel = options->jo_channel;
5574 if (channel != NULL)
5575 ++channel->ch_refcount;
5576 }
5577 else
5578 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005579 if (channel == NULL)
5580 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005581 if (job->jv_tty_out != NULL)
5582 ch_log(channel, "using pty %s on fd %d",
5583 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005584 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005585
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005586 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005587 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005588 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005589 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005590 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005591 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005592 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005593 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005594 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005595 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005596 int null_fd = -1;
5597 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005598
Bram Moolenaar0f873732019-12-05 20:28:46 +01005599 // child
5600 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005601 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005602
Bram Moolenaar819524702018-02-27 19:10:00 +01005603# ifdef FEAT_JOB_CHANNEL
5604 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005605 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005606 ch_logfile((char_u *)"", (char_u *)"");
5607# endif
5608
Bram Moolenaar835dc632016-02-07 14:27:38 +01005609# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005610 // Create our own process group, so that the child and all its
5611 // children can be kill()ed. Don't do this when using pipes,
5612 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005613 (void)setsid();
5614# endif
5615
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005616# ifdef FEAT_TERMINAL
5617 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005618 {
5619 char *term = (char *)T_NAME;
5620
5621#ifdef FEAT_GUI
5622 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005623 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005624 term = getenv("TERM");
5625#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005626 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005627 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005628 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005629 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005630 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005631 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005632 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005633 else if (t_colors > 16)
5634 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005635 else
5636 term = "xterm";
5637 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005638 set_child_environment(
5639 (long)options->jo_term_rows,
5640 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005641 term,
5642 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005643 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005644 else
5645# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005646 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005647
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005648 if (options->jo_env != NULL)
5649 {
5650 dict_T *dict = options->jo_env;
5651 hashitem_T *hi;
5652 int todo = (int)dict->dv_hashtab.ht_used;
5653
5654 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5655 if (!HASHITEM_EMPTY(hi))
5656 {
5657 typval_T *item = &dict_lookup(hi)->di_tv;
5658
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00005659 vim_setenv(hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005660 --todo;
5661 }
5662 }
5663
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005664 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005665 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005666 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005667 if (null_fd < 0)
5668 {
5669 perror("opening /dev/null failed");
5670 _exit(OPEN_NULL_FAILED);
5671 }
5672 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005673
Bram Moolenaar223896d2017-08-02 22:33:28 +02005674 if (pty_slave_fd >= 0)
5675 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005676 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005677 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005678# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005679 // Try to become controlling tty (probably doesn't work,
5680 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005681 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5682# endif
5683 }
5684
Bram Moolenaar0f873732019-12-05 20:28:46 +01005685 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005686 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005687 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005688 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005689 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005690 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005691 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005692 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005693
Bram Moolenaar0f873732019-12-05 20:28:46 +01005694 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005695 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005696 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005697 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005698 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005699 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005700 }
5701 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005702 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005703 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005704 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005705 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005706 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005707
Bram Moolenaar0f873732019-12-05 20:28:46 +01005708 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005709 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005710 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005711 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005712 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005713 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005714 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005715 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005716
5717 if (fd_in[0] >= 0)
5718 close(fd_in[0]);
5719 if (fd_in[1] >= 0)
5720 close(fd_in[1]);
5721 if (fd_out[0] >= 0)
5722 close(fd_out[0]);
5723 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005724 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005725 if (fd_err[0] >= 0)
5726 close(fd_err[0]);
5727 if (fd_err[1] >= 0)
5728 close(fd_err[1]);
5729 if (pty_master_fd >= 0)
5730 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005731 close(pty_master_fd); // not used in the child
5732 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005733 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005734
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005735 if (null_fd >= 0)
5736 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005737
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005738 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5739 _exit(EXEC_FAILED);
5740
Bram Moolenaar0f873732019-12-05 20:28:46 +01005741 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005742 execvp(argv[0], argv);
5743
Bram Moolenaar4694a172016-04-21 14:05:23 +02005744 if (stderr_works)
5745 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005746# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005747 // calling free_all_mem() here causes problems. Ignore valgrind
5748 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005749# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005750 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005751 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005752
Bram Moolenaar0f873732019-12-05 20:28:46 +01005753 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005754 UNBLOCK_SIGNALS(&curset);
5755
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005756 job->jv_pid = pid;
5757 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005758 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005759
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005760 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005761 close(pty_slave_fd); // not used in the parent
5762 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005763 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005764 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005765 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005766 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005767 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005768 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005769 if (channel != NULL)
5770 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005771 int in_fd = INVALID_FD;
5772 int out_fd = INVALID_FD;
5773 int err_fd = INVALID_FD;
5774
5775 if (!(use_file_for_in || use_null_for_in))
5776 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5777
5778 if (!(use_file_for_out || use_null_for_out))
5779 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5780
5781 // When using pty_master_fd only set it for stdout, do not duplicate
5782 // it for stderr, it only needs to be read once.
5783 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5784 {
5785 if (fd_err[0] >= 0)
5786 err_fd = fd_err[0];
5787 else if (out_fd != pty_master_fd)
5788 err_fd = pty_master_fd;
5789 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005790
5791 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005792 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005793 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005794 else
5795 {
5796 if (fd_in[1] >= 0)
5797 close(fd_in[1]);
5798 if (fd_out[0] >= 0)
5799 close(fd_out[0]);
5800 if (fd_err[0] >= 0)
5801 close(fd_err[0]);
5802 if (pty_master_fd >= 0)
5803 close(pty_master_fd);
5804 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005805
Bram Moolenaar0f873732019-12-05 20:28:46 +01005806 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005807 return;
5808
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005809failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005810 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005811 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005812 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005813 if (fd_in[1] >= 0)
5814 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005815 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005816 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005817 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005818 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005819 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005820 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005821 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005822 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005823 if (pty_master_fd >= 0)
5824 close(pty_master_fd);
5825 if (pty_slave_fd >= 0)
5826 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005827}
5828
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005829 static char_u *
5830get_signal_name(int sig)
5831{
5832 int i;
5833 char_u numbuf[NUMBUFLEN];
5834
5835 if (sig == SIGKILL)
5836 return vim_strsave((char_u *)"kill");
5837
5838 for (i = 0; signal_info[i].sig != -1; i++)
5839 if (sig == signal_info[i].sig)
5840 return strlow_save((char_u *)signal_info[i].name);
5841
5842 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5843 return vim_strsave(numbuf);
5844}
5845
Bram Moolenaar835dc632016-02-07 14:27:38 +01005846 char *
5847mch_job_status(job_T *job)
5848{
5849# ifdef HAVE_UNION_WAIT
5850 union wait status;
5851# else
5852 int status = -1;
5853# endif
5854 pid_t wait_pid = 0;
5855
5856# ifdef __NeXT__
5857 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5858# else
5859 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5860# endif
5861 if (wait_pid == -1)
5862 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005863 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005864 if (job->jv_status < JOB_ENDED)
5865 ch_log(job->jv_channel, "Job no longer exists: %s",
5866 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005867 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005868 }
5869 if (wait_pid == 0)
5870 return "run";
5871 if (WIFEXITED(status))
5872 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005873 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005874 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005875 if (job->jv_status < JOB_ENDED)
5876 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005877 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005878 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005879 if (WIFSIGNALED(status))
5880 {
5881 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005882 job->jv_termsig = get_signal_name(WTERMSIG(status));
5883 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5884 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5885 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005886 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005887 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005888 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005889
5890return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005891 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005892 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005893 return "dead";
5894}
5895
5896 job_T *
5897mch_detect_ended_job(job_T *job_list)
5898{
5899# ifdef HAVE_UNION_WAIT
5900 union wait status;
5901# else
5902 int status = -1;
5903# endif
5904 pid_t wait_pid = 0;
5905 job_T *job;
5906
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005907# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005908 // Do not do this when waiting for a shell command to finish, we would get
5909 // the exit value here (and discard it), the exit value obtained there
5910 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005911 if (dont_check_job_ended > 0)
5912 return NULL;
5913# endif
5914
Bram Moolenaar97792de2016-10-15 18:36:49 +02005915# ifdef __NeXT__
5916 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5917# else
5918 wait_pid = waitpid(-1, &status, WNOHANG);
5919# endif
5920 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005921 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005922 return NULL;
5923 for (job = job_list; job != NULL; job = job->jv_next)
5924 {
5925 if (job->jv_pid == wait_pid)
5926 {
5927 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005928 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005929 job->jv_exitval = WEXITSTATUS(status);
5930 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005931 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005932 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005933 job->jv_termsig = get_signal_name(WTERMSIG(status));
5934 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005935 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005936 {
5937 ch_log(job->jv_channel, "Job ended");
5938 job->jv_status = JOB_ENDED;
5939 }
5940 return job;
5941 }
5942 }
5943 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005944}
5945
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005946/*
5947 * Send a (deadly) signal to "job".
5948 * Return FAIL if "how" is not a valid name.
5949 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005950 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005951mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005952{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005953 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005954
Bram Moolenaar923d9262016-02-25 20:56:01 +01005955 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005956 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005957 else if (STRCMP(how, "hup") == 0)
5958 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005959 else if (STRCMP(how, "quit") == 0)
5960 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005961 else if (STRCMP(how, "int") == 0)
5962 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005963 else if (STRCMP(how, "kill") == 0)
5964 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005965#ifdef SIGWINCH
5966 else if (STRCMP(how, "winch") == 0)
5967 sig = SIGWINCH;
5968#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005969 else if (isdigit(*how))
5970 sig = atoi((char *)how);
5971 else
5972 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005973
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005974 // Never kill ourselves!
5975 if (job->jv_pid != 0)
5976 {
5977 // TODO: have an option to only kill the process, not the group?
5978 kill(-job->jv_pid, sig);
5979 kill(job->jv_pid, sig);
5980 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005981
Bram Moolenaar835dc632016-02-07 14:27:38 +01005982 return OK;
5983}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005984
5985/*
5986 * Clear the data related to "job".
5987 */
5988 void
5989mch_clear_job(job_T *job)
5990{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005991 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005992# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005993 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005994# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005995 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005996# endif
5997}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005998#endif
5999
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006000#if defined(FEAT_TERMINAL) || defined(PROTO)
6001 int
6002mch_create_pty_channel(job_T *job, jobopt_T *options)
6003{
6004 int pty_master_fd = -1;
6005 int pty_slave_fd = -1;
6006 channel_T *channel;
6007
Bram Moolenaar59386482019-02-10 22:43:46 +01006008 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02006009 if (pty_master_fd < 0 || pty_slave_fd < 0)
6010 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006011 close(pty_slave_fd);
6012
6013 channel = add_channel();
6014 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006015 {
6016 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006017 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006018 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006019 if (job->jv_tty_out != NULL)
6020 ch_log(channel, "using pty %s on fd %d",
6021 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006022 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006023 channel->ch_keep_open = TRUE;
6024
Bram Moolenaar0f873732019-12-05 20:28:46 +01006025 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6026 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006027 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006028 channel_set_job(channel, job, options);
6029 return OK;
6030}
6031#endif
6032
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033/*
6034 * Check for CTRL-C typed by reading all available characters.
6035 * In cooked mode we should get SIGINT, no need to check.
6036 */
6037 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006038mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006040 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006041 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 fill_input_buf(FALSE);
6043}
6044
6045/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006046 * Wait "msec" msec until a character is available from the mouse, keyboard,
6047 * from inbuf[].
6048 * "msec" == -1 will block forever.
6049 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006050 * When "ignore_input" is TRUE even check for pending input when input is
6051 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006052 * "interrupted" (if not NULL) is set to TRUE when no character is available
6053 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006054 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006055 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 */
6057 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006058WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006060#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006061 return ui_wait_for_chars_or_timer(
6062 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006063#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006064 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006065#endif
6066}
6067
6068/*
6069 * Wait "msec" msec until a character is available from the mouse or keyboard
6070 * or from inbuf[].
6071 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006072 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006073 * "interrupted" (if not NULL) is set to TRUE when no character is available
6074 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006075 * When a GUI is being used, this will never get called -- webb
6076 */
6077 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006078WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006079{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080#ifdef FEAT_MOUSE_GPM
6081 int gpm_process_wanted;
6082#endif
6083#ifdef FEAT_XCLIPBOARD
6084 int rest;
6085#endif
6086 int avail;
6087
Bram Moolenaar0f873732019-12-05 20:28:46 +01006088 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 return 1;
6090
6091#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006092 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (WantQueryMouse)
6094 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006095 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006096 if (!no_query_mouse_for_testing)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006097 mch_write((char_u *)"\033[1'|", 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 }
6099#endif
6100
6101 /*
6102 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6103 * events. This is a bit complicated, because they might both be defined.
6104 */
6105#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6106# ifdef FEAT_XCLIPBOARD
6107 rest = 0;
6108 if (do_xterm_trace())
6109 rest = msec;
6110# endif
6111 do
6112 {
6113# ifdef FEAT_XCLIPBOARD
6114 if (rest != 0)
6115 {
6116 msec = XT_TRACE_DELAY;
6117 if (rest >= 0 && rest < XT_TRACE_DELAY)
6118 msec = rest;
6119 if (rest >= 0)
6120 rest -= msec;
6121 }
6122# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006123# ifdef FEAT_SOUND_CANBERRA
6124 // Invoke any pending sound callbacks.
6125 if (has_sound_callback_in_queue())
6126 invoke_sound_callback();
6127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128# ifdef FEAT_MOUSE_GPM
6129 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006130 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006131 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006132 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006134 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006136# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006138 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 return 1;
6140# ifdef FEAT_XCLIPBOARD
6141 if (rest == 0 || !do_xterm_trace())
6142# endif
6143 break;
6144 }
6145 }
6146 while (FALSE
6147# ifdef FEAT_MOUSE_GPM
6148 || (gpm_process_wanted && mch_gpm_process() == 0)
6149# endif
6150# ifdef FEAT_XCLIPBOARD
6151 || (!avail && rest != 0)
6152# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006153 )
6154 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155
6156#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006157 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158#endif
6159 return avail;
6160}
6161
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006162#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163/*
6164 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006165 * "msec" == 0 will check for characters once.
6166 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006169 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006170 * "interrupted" (if not NULL) is set to TRUE when no character is available
6171 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006173 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006174RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175{
6176 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006177 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006178#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 static int busy = FALSE;
6180
Bram Moolenaar0f873732019-12-05 20:28:46 +01006181 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182# define MAY_LOOP
6183
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006184# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006185 // Remember at what time we started, so that we know how much longer we
6186 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006187 long start_msec = msec;
6188 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006190 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006191 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192# endif
6193
Bram Moolenaar0f873732019-12-05 20:28:46 +01006194 // Handle being called recursively. This may happen for the session
6195 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 if (busy)
6197 return 0;
6198#endif
6199
6200#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006201 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202#endif
6203 {
6204#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006205 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006206# ifdef FEAT_MZSCHEME
6207 int mzquantum_used = FALSE;
6208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209#endif
6210#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006211 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006212 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 int nfd;
6214# ifdef FEAT_XCLIPBOARD
6215 int xterm_idx = -1;
6216# endif
6217# ifdef FEAT_MOUSE_GPM
6218 int gpm_idx = -1;
6219# endif
6220# ifdef USE_XSMP
6221 int xsmp_idx = -1;
6222# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006223 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006225# ifdef FEAT_MZSCHEME
6226 mzvim_check_threads();
6227 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6228 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006229 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006230 mzquantum_used = TRUE;
6231 }
6232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 fds[0].fd = fd;
6234 fds[0].events = POLLIN;
6235 nfd = 1;
6236
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006238 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 if (xterm_Shell != (Widget)0)
6240 {
6241 xterm_idx = nfd;
6242 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6243 fds[nfd].events = POLLIN;
6244 nfd++;
6245 }
6246# endif
6247# ifdef FEAT_MOUSE_GPM
6248 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6249 {
6250 gpm_idx = nfd;
6251 fds[nfd].fd = gpm_fd;
6252 fds[nfd].events = POLLIN;
6253 nfd++;
6254 }
6255# endif
6256# ifdef USE_XSMP
6257 if (xsmp_icefd != -1)
6258 {
6259 xsmp_idx = nfd;
6260 fds[nfd].fd = xsmp_icefd;
6261 fds[nfd].events = POLLIN;
6262 nfd++;
6263 }
6264# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006265#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006266 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006267#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006268 if (interrupted != NULL)
6269 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006271 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006272
6273 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006274 if (result == 0 && interrupted != NULL && ret > 0)
6275 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006276
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006277# ifdef FEAT_MZSCHEME
6278 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006279 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006280 finished = FALSE;
6281# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283# ifdef FEAT_XCLIPBOARD
6284 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6285 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006286 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006288 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 finished = FALSE;
6290 }
6291# endif
6292# ifdef FEAT_MOUSE_GPM
6293 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295# endif
6296# ifdef USE_XSMP
6297 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6298 {
6299 if (fds[xsmp_idx].revents & POLLIN)
6300 {
6301 busy = TRUE;
6302 xsmp_handle_requests();
6303 busy = FALSE;
6304 }
6305 else if (fds[xsmp_idx].revents & POLLHUP)
6306 {
6307 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006308 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 xsmp_close();
6310 }
6311 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006312 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 }
6314# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006315#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006316 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006317 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006318 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320
Bram Moolenaar0f873732019-12-05 20:28:46 +01006321#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322
6323 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006324 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006325 // These are static because they can take 8 Kbyte each and cause the
6326 // signal stack to run out with -O3.
6327 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006329 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006331# ifdef FEAT_MZSCHEME
6332 mzvim_check_threads();
6333 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6334 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006335 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006336 mzquantum_used = TRUE;
6337 }
6338# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006340 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006342 tv.tv_sec = towait / 1000;
6343 tv.tv_usec = (towait % 1000) * (1000000/1000);
6344 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006346 else
6347 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348
6349 /*
6350 * Select on ready for reading and exceptional condition (end of file).
6351 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006352select_eintr:
6353 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006354 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 FD_ZERO(&efds);
6356 FD_SET(fd, &rfds);
K.Takatab247e062022-02-07 10:45:23 +00006357# ifndef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006358 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 FD_SET(fd, &efds);
6360# endif
6361 maxfd = fd;
6362
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006364 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 if (xterm_Shell != (Widget)0)
6366 {
6367 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6368 if (maxfd < ConnectionNumber(xterm_dpy))
6369 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006370
Bram Moolenaar0f873732019-12-05 20:28:46 +01006371 // An event may have already been read but not handled. In
6372 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006373 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 }
6375# endif
6376# ifdef FEAT_MOUSE_GPM
6377 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6378 {
6379 FD_SET(gpm_fd, &rfds);
6380 FD_SET(gpm_fd, &efds);
6381 if (maxfd < gpm_fd)
6382 maxfd = gpm_fd;
6383 }
6384# endif
6385# ifdef USE_XSMP
6386 if (xsmp_icefd != -1)
6387 {
6388 FD_SET(xsmp_icefd, &rfds);
6389 FD_SET(xsmp_icefd, &efds);
6390 if (maxfd < xsmp_icefd)
6391 maxfd = xsmp_icefd;
6392 }
6393# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006394# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006395 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006396# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006397 if (interrupted != NULL)
6398 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399
Bram Moolenaar643b6142018-09-12 20:29:09 +02006400 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6401 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006402 result = ret > 0 && FD_ISSET(fd, &rfds);
6403 if (result)
6404 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006405 else if (interrupted != NULL && ret > 0)
6406 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006407
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006408# ifdef EINTR
6409 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006410 {
dbivolaruab16ad32021-12-29 19:41:47 +00006411 // Check whether the EINTR is caused by SIGTSTP
6412 if (got_tstp && !in_mch_suspend)
6413 {
6414 exarg_T ea;
dbivolaru79a6e252022-01-23 16:41:14 +00006415
dbivolaruab16ad32021-12-29 19:41:47 +00006416 ea.forceit = TRUE;
6417 ex_stop(&ea);
6418 got_tstp = FALSE;
6419 }
6420
Bram Moolenaar0f873732019-12-05 20:28:46 +01006421 // Check whether window has been resized, EINTR may be caused by
6422 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006423 if (do_resize)
6424 handle_resize();
6425
Bram Moolenaar0f873732019-12-05 20:28:46 +01006426 // Interrupted by a signal, need to try again. We ignore msec
6427 // here, because we do want to check even after a timeout if
6428 // characters are available. Needed for reading output of an
6429 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006430 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006431 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006432# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006433# ifdef __TANDEM
6434 if (ret == -1 && errno == ENOTSUP)
6435 {
6436 FD_ZERO(&rfds);
6437 FD_ZERO(&efds);
6438 ret = 0;
6439 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006440# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006441# ifdef FEAT_MZSCHEME
6442 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006443 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006444 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445# endif
6446
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447# ifdef FEAT_XCLIPBOARD
6448 if (ret > 0 && xterm_Shell != (Widget)0
6449 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6450 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006451 xterm_update(); // Maybe we should hand out clipboard
6452 // continue looping when we only got the X event and the input
6453 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 if (--ret == 0 && !input_available())
6455 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006456 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 finished = FALSE;
6458 }
6459 }
6460# endif
6461# ifdef FEAT_MOUSE_GPM
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00006462 if (ret > 0 && check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 {
6464 if (FD_ISSET(gpm_fd, &efds))
6465 gpm_close();
6466 else if (FD_ISSET(gpm_fd, &rfds))
6467 *check_for_gpm = 1;
6468 }
6469# endif
6470# ifdef USE_XSMP
6471 if (ret > 0 && xsmp_icefd != -1)
6472 {
6473 if (FD_ISSET(xsmp_icefd, &efds))
6474 {
6475 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006476 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 xsmp_close();
6478 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006479 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 }
6481 else if (FD_ISSET(xsmp_icefd, &rfds))
6482 {
6483 busy = TRUE;
6484 xsmp_handle_requests();
6485 busy = FALSE;
6486 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006487 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 }
6489 }
6490# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006491#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006492 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006493 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006494 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496
Bram Moolenaar0f873732019-12-05 20:28:46 +01006497#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498
6499#ifdef MAY_LOOP
6500 if (finished || msec == 0)
6501 break;
6502
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006503# ifdef FEAT_CLIENTSERVER
6504 if (server_waiting())
6505 break;
6506# endif
6507
Bram Moolenaar0f873732019-12-05 20:28:46 +01006508 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (msec > 0)
6510 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006511# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006512 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006513 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006515 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 msec = msec / 2;
6517# endif
6518 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006519 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 }
6521#endif
6522 }
6523
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006524 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525}
6526
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006528 * Expand a path into all matching files and/or directories. Handles "*",
6529 * "?", "[a-z]", "**", etc.
6530 * "path" has backslashes before chars that are not to be expanded.
6531 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 */
6533 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006534mch_expandpath(
6535 garray_T *gap,
6536 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006537 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538{
Bram Moolenaar02743632005-07-25 20:42:36 +00006539 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541
6542/*
6543 * mch_expand_wildcards() - this code does wild-card pattern matching using
6544 * the shell
6545 *
6546 * return OK for success, FAIL for error (you may lose some memory) and put
6547 * an error message in *file.
6548 *
6549 * num_pat is number of input patterns
6550 * pat is array of pointers to input patterns
6551 * num_file is pointer to number of matched file names
6552 * file is pointer to array of pointers to matched file names
6553 */
6554
6555#ifndef SEEK_SET
6556# define SEEK_SET 0
6557#endif
6558#ifndef SEEK_END
6559# define SEEK_END 2
6560#endif
6561
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006562#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006563
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006565mch_expand_wildcards(
6566 int num_pat,
6567 char_u **pat,
6568 int *num_file,
6569 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006570 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571{
6572 int i;
6573 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006574 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 char_u *p;
6576 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
Bram Moolenaarc7247912008-01-13 12:54:11 +00006578 /*
6579 * This is the non-OS/2 implementation (really Unix).
6580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 int j;
6582 char_u *tempname;
6583 char_u *command;
6584 FILE *fd;
6585 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006586#define STYLE_ECHO 0 // use "echo", the default
6587#define STYLE_GLOB 1 // use "glob", for csh
6588#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6589#define STYLE_PRINT 3 // use "print -N", for zsh
6590#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6591 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 int shell_style = STYLE_ECHO;
6593 int check_spaces;
6594 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006595 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006596 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006597 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598
Bram Moolenaar0f873732019-12-05 20:28:46 +01006599 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 *file = NULL;
6601
6602 /*
6603 * If there are no wildcards, just copy the names to allocated memory.
6604 * Saves a lot of time, because we don't have to start a new shell.
6605 */
6606 if (!have_wildcard(num_pat, pat))
6607 return save_patterns(num_pat, pat, num_file, file);
6608
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006609# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006610 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006611 if (sandbox != 0 && check_secure())
6612 return FAIL;
6613# endif
6614
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 /*
6616 * Don't allow the use of backticks in secure and restricted mode.
6617 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006618 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 for (i = 0; i < num_pat; ++i)
6620 if (vim_strchr(pat[i], '`') != NULL
6621 && (check_restricted() || check_secure()))
6622 return FAIL;
6623
6624 /*
6625 * get a name for the temp file
6626 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006627 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006629 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 return FAIL;
6631 }
6632
6633 /*
6634 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006635 * file.
6636 * STYLE_BT: NL separated
6637 * If expanding `cmd` execute it directly.
6638 * STYLE_GLOB: NUL separated
6639 * If we use *csh, "glob" will work better than "echo".
6640 * STYLE_PRINT: NL or NUL separated
6641 * If we use *zsh, "print -N" will work better than "glob".
6642 * STYLE_VIMGLOB: NL separated
6643 * If we use *sh*, we define "vimglob()".
6644 * STYLE_ECHO: space separated.
6645 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 */
6647 if (num_pat == 1 && *pat[0] == '`'
6648 && (len = STRLEN(pat[0])) > 2
6649 && *(pat[0] + len - 1) == '`')
6650 shell_style = STYLE_BT;
6651 else if ((len = STRLEN(p_sh)) >= 3)
6652 {
6653 if (STRCMP(p_sh + len - 3, "csh") == 0)
6654 shell_style = STYLE_GLOB;
6655 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6656 shell_style = STYLE_PRINT;
6657 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006658 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6659 "sh") != NULL)
6660 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661
Bram Moolenaar0f873732019-12-05 20:28:46 +01006662 // Compute the length of the command. We need 2 extra bytes: for the
6663 // optional '&' and for the NUL.
6664 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006666 if (shell_style == STYLE_VIMGLOB)
6667 len += STRLEN(sh_vimglob_func);
6668
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006669 for (i = 0; i < num_pat; ++i)
6670 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006671 // Count the length of the patterns in the same way as they are put in
6672 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006673#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006674 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006675#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006676 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006677 for (j = 0; pat[i][j] != NUL; ++j)
6678 {
6679 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006680 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006681 ++len;
6682 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006683#endif
6684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 command = alloc(len);
6686 if (command == NULL)
6687 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006688 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 vim_free(tempname);
6690 return FAIL;
6691 }
6692
6693 /*
6694 * Build the shell command:
6695 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6696 * recognizes this).
6697 * - Add the shell command to print the expanded names.
6698 * - Add the temp file name.
6699 * - Add the file name patterns.
6700 */
6701 if (shell_style == STYLE_BT)
6702 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006703 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006704 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006705 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006707 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006708 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006710 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006712 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 *p = ' ';
6714 }
6715 STRCAT(command, ">");
6716 }
6717 else
6718 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006719 STRCPY(command, "");
6720 if (shell_style == STYLE_GLOB)
6721 {
6722 // Assume the nonomatch option is valid only for csh like shells,
6723 // otherwise, this may set the positional parameters for the shell,
6724 // e.g. "$*".
6725 if (flags & EW_NOTFOUND)
6726 STRCAT(command, "set nonomatch; ");
6727 else
6728 STRCAT(command, "unset nonomatch; ");
6729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 if (shell_style == STYLE_GLOB)
6731 STRCAT(command, "glob >");
6732 else if (shell_style == STYLE_PRINT)
6733 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006734 else if (shell_style == STYLE_VIMGLOB)
6735 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 else
6737 STRCAT(command, "echo >");
6738 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006739
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006741
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 if (shell_style != STYLE_BT)
6743 for (i = 0; i < num_pat; ++i)
6744 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006745 // When using system() always add extra quotes, because the shell
6746 // is started twice. Otherwise put a backslash before special
6747 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748#ifdef USE_SYSTEM
6749 STRCAT(command, " \"");
6750 STRCAT(command, pat[i]);
6751 STRCAT(command, "\"");
6752#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006753 int intick = FALSE;
6754
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 p = command + STRLEN(command);
6756 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006757 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006758 {
6759 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006760 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006761 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6762 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006763 // Remove a backslash, take char literally. But keep
6764 // backslash inside backticks, before a special character
6765 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006766 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006767 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6768 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006769 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006770 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006771 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006772 else if (!intick
6773 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6774 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006775 // Put a backslash before a special character, but not
6776 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6777 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006778 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006779
Bram Moolenaar0f873732019-12-05 20:28:46 +01006780 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006781 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 *p = NUL;
6784#endif
6785 }
6786 if (flags & EW_SILENT)
6787 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006788 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006789 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790
6791 /*
6792 * Using zsh -G: If a pattern has no matches, it is just deleted from
6793 * the argument list, otherwise zsh gives an error message and doesn't
6794 * expand any other pattern.
6795 */
6796 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006797 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
6799 /*
6800 * If we use -f then shell variables set in .cshrc won't get expanded.
6801 * vi can do it, so we will too, but it is only necessary if there is a "$"
6802 * in one of the patterns, otherwise we can still use the fast option.
6803 */
6804 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006805 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806
6807 /*
6808 * execute the shell command
6809 */
6810 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6811
Bram Moolenaar0f873732019-12-05 20:28:46 +01006812 // When running in the background, give it some time to create the temp
6813 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006814 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006815 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
Bram Moolenaar0f873732019-12-05 20:28:46 +01006817 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 show_shell_mess = TRUE;
6819 vim_free(command);
6820
Bram Moolenaar0f873732019-12-05 20:28:46 +01006821 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 {
6823 mch_remove(tempname);
6824 vim_free(tempname);
6825 /*
6826 * With interactive completion, the error message is not printed.
6827 * However with USE_SYSTEM, I don't know how to turn off error messages
6828 * from the shell, so screen may still get messed up -- webb.
6829 */
6830#ifndef USE_SYSTEM
6831 if (!(flags & EW_SILENT))
6832#endif
6833 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006834 redraw_later_clear(); // probably messed up screen
6835 msg_putchar('\n'); // clear bottom line quickly
6836 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837#ifdef USE_SYSTEM
6838 if (!(flags & EW_SILENT))
6839#endif
6840 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006841 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006842 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 }
6844 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006845 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6846 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 if (shell_style == STYLE_BT)
6848 return FAIL;
6849 goto notfound;
6850 }
6851
6852 /*
6853 * read the names from the file into memory
6854 */
6855 fd = fopen((char *)tempname, READBIN);
6856 if (fd == NULL)
6857 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006858 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 if (!(flags & EW_SILENT))
6860 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006861 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006862 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 }
6864 vim_free(tempname);
6865 goto notfound;
6866 }
6867 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006868 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006870 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006871 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006872 buffer = NULL;
6873 else
6874 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 if (buffer == NULL)
6876 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006877 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 mch_remove(tempname);
6879 vim_free(tempname);
6880 fclose(fd);
6881 return FAIL;
6882 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006883 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 i = fread((char *)buffer, 1, len, fd);
6885 fclose(fd);
6886 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006887 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006889 // unexpected read error
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006890 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 vim_free(tempname);
6892 vim_free(buffer);
6893 return FAIL;
6894 }
6895 vim_free(tempname);
6896
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006897# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006898 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006900 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6902 *p++ = buffer[i];
6903 len = p - buffer;
6904# endif
6905
6906
Bram Moolenaar0f873732019-12-05 20:28:46 +01006907 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 if (shell_style == STYLE_ECHO)
6909 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006910 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006912 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {
6914 while (*p != ' ' && *p != '\n')
6915 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006916 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 }
6918 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006919 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006920 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006922 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006924 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {
6926 while (*p != '\n' && *p != NUL)
6927 ++p;
6928 if (*p != NUL)
6929 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006930 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 }
6932 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006933 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 else
6935 {
6936 /*
6937 * Some versions of zsh use spaces instead of NULs to separate
6938 * results. Only do this when there is no NUL before the end of the
6939 * buffer, otherwise we would never be able to use file names with
6940 * embedded spaces when zsh does use NULs.
6941 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6942 * don't check for spaces again.
6943 */
6944 check_spaces = FALSE;
6945 if (shell_style == STYLE_PRINT && !did_find_nul)
6946 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006947 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006948 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006949 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 did_find_nul = TRUE;
6951 else
6952 check_spaces = TRUE;
6953 }
6954
6955 /*
6956 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6957 * already is one, for STYLE_GLOB it needs to be added.
6958 */
6959 if (len && buffer[len - 1] == NUL)
6960 --len;
6961 else
6962 buffer[len] = NUL;
6963 i = 0;
6964 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006965 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 {
6967 ++i;
6968 *p = NUL;
6969 }
6970 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006971 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 }
6973 if (i == 0)
6974 {
6975 /*
6976 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6977 * /bin/sh will happily expand it to nothing rather than returning an
6978 * error; and hey, it's good to check anyway -- webb.
6979 */
6980 vim_free(buffer);
6981 goto notfound;
6982 }
6983 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006984 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 if (*file == NULL)
6986 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006987 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 vim_free(buffer);
6989 return FAIL;
6990 }
6991
6992 /*
6993 * Isolate the individual file names.
6994 */
6995 p = buffer;
6996 for (i = 0; i < *num_file; ++i)
6997 {
6998 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006999 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00007000 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
7001 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00007003 while (!(shell_style == STYLE_ECHO && *p == ' ')
7004 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007006 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 *p = NUL;
7008 else
7009 {
7010 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007011 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 }
7013 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007014 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007016 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007018 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 }
7020 }
7021
7022 /*
7023 * Move the file names to allocated memory.
7024 */
7025 for (j = 0, i = 0; i < *num_file; ++i)
7026 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007027 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
7029 continue;
7030
Bram Moolenaar0f873732019-12-05 20:28:46 +01007031 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 dir = (mch_isdir((*file)[i]));
7033 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7034 continue;
7035
Bram Moolenaar0f873732019-12-05 20:28:46 +01007036 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007037 if (!dir && (flags & EW_EXEC)
7038 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007039 continue;
7040
Bram Moolenaar964b3742019-05-24 18:54:09 +02007041 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 if (p)
7043 {
7044 STRCPY(p, (*file)[i]);
7045 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007046 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 (*file)[j++] = p;
7048 }
7049 }
7050 vim_free(buffer);
7051 *num_file = j;
7052
Bram Moolenaar0f873732019-12-05 20:28:46 +01007053 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007055 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 goto notfound;
7057 }
7058
7059 return OK;
7060
7061notfound:
7062 if (flags & EW_NOTFOUND)
7063 return save_patterns(num_pat, pat, num_file, file);
7064 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065}
7066
Bram Moolenaar0f873732019-12-05 20:28:46 +01007067#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007070save_patterns(
7071 int num_pat,
7072 char_u **pat,
7073 int *num_file,
7074 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
7076 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007077 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007079 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 if (*file == NULL)
7081 return FAIL;
7082 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007083 {
7084 s = vim_strsave(pat[i]);
7085 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007086 // Be compatible with expand_filename(): halve the number of
7087 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007088 backslash_halve(s);
7089 (*file)[i] = s;
7090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 *num_file = num_pat;
7092 return OK;
7093}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095/*
7096 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7097 * expand.
7098 */
7099 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007100mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007102 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 if (*p == '\\' && p[1] != NUL)
7105 ++p;
7106 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 if (vim_strchr((char_u *)
7108#ifdef VMS
7109 "*?%"
7110#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112#endif
7113 , *p) != NULL)
7114 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 }
7116 return FALSE;
7117}
7118
7119/*
7120 * Return TRUE if the string "p" contains a wildcard.
7121 * Don't recognize '~' at the end as a wildcard.
7122 */
7123 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007124mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007126 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 if (*p == '\\' && p[1] != NUL)
7129 ++p;
7130 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 if (vim_strchr((char_u *)
7132#ifdef VMS
7133 "*?%$"
7134#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136#endif
7137 , *p) != NULL
7138 || (*p == '~' && p[1] != NUL))
7139 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 }
7141 return FALSE;
7142}
7143
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007145have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
7147 int i;
7148
7149 for (i = 0; i < num; i++)
7150 if (mch_has_wildcard(file[i]))
7151 return 1;
7152 return 0;
7153}
7154
7155 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007156have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157{
7158 int i;
7159
7160 for (i = 0; i < num; i++)
7161 if (vim_strchr(file[i], '$') != NULL)
7162 return TRUE;
7163 return FALSE;
7164}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007166#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167/*
7168 * Scaled-down version of rename(), which is missing in Xenix.
7169 * This version can only move regular files and will fail if the
7170 * destination exists.
7171 */
7172 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007173mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174{
7175 struct stat st;
7176
Bram Moolenaar0f873732019-12-05 20:28:46 +01007177 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007179 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007181 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 return 0;
7183 return -1;
7184}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007185#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007187#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007188# if defined(DYNAMIC_GPM) || defined(PROTO)
7189/*
7190 * Initialize Gpm's symbols for dynamic linking.
7191 * Must be called only if libgpm_hinst is NULL.
7192 */
7193 static int
7194load_libgpm(void)
7195{
7196 libgpm_hinst = dlopen("libgpm.so", RTLD_LAZY|RTLD_GLOBAL);
7197
7198 if (libgpm_hinst == NULL)
7199 {
7200 if (p_verbose > 0)
7201 smsg_attr(HL_ATTR(HLF_W),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007202 _("Could not load gpm library: %s"), dlerror());
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007203 return FAIL;
7204 }
7205
7206 if (
7207 (dll_Gpm_Open = dlsym(libgpm_hinst, "Gpm_Open")) == NULL
7208 || (dll_Gpm_Close = dlsym(libgpm_hinst, "Gpm_Close")) == NULL
7209 || (dll_Gpm_GetEvent = dlsym(libgpm_hinst, "Gpm_GetEvent")) == NULL
7210 || (dll_gpm_flag = dlsym(libgpm_hinst, "gpm_flag")) == NULL
7211 || (dll_gpm_fd = dlsym(libgpm_hinst, "gpm_fd")) == NULL
7212 )
7213 {
7214 semsg(_(e_could_not_load_library_str_str), "gpm", dlerror());
7215 dlclose(libgpm_hinst);
7216 libgpm_hinst = NULL;
7217 dll_gpm_flag = NULL;
7218 dll_gpm_fd = NULL;
7219 return FAIL;
7220 }
7221 return OK;
7222}
7223
7224 int
7225gpm_available(void)
7226{
7227 return libgpm_hinst != NULL || load_libgpm() == OK;
7228}
7229# endif // DYNAMIC_GPM
7230
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231/*
7232 * Initializes connection with gpm (if it isn't already opened)
7233 * Return 1 if succeeded (or connection already opened), 0 if failed
7234 */
7235 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007236gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007238 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
Bram Moolenaar33fc4a62022-02-23 18:07:38 +00007240#ifdef DYNAMIC_GPM
7241 if (!gpm_available())
7242 return 0;
7243#endif
7244
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 if (!gpm_flag)
7246 {
7247 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7248 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007249 // Default handling for mouse move
7250 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 gpm_connect.maxMod = 0xffff;
7252 if (Gpm_Open(&gpm_connect, 0) > 0)
7253 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007254 // gpm library tries to handling TSTP causes
7255 // problems. Anyways, we close connection to Gpm whenever
7256 // we are going to suspend or starting an external process
7257 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007258# ifdef SIGTSTP
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007259 signal(SIGTSTP, restricted ? SIG_IGN : (void (*)())sig_tstp);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007260# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007261 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 }
7263 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007264 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 return 0;
7266 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007267 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268}
7269
7270/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007271 * Returns TRUE if the GPM mouse is enabled.
7272 */
7273 int
7274gpm_enabled(void)
7275{
7276 return gpm_flag && gpm_fd >= 0;
7277}
7278
7279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 */
7282 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007283gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007285 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 Gpm_Close();
7287}
7288
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007289/*
7290 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007292 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 */
7294 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007295mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296{
7297 int button;
7298 static Gpm_Event gpm_event;
7299 char_u string[6];
7300 int_u vim_modifiers;
7301 int row,col;
7302 unsigned char buttons_mask;
7303 unsigned char gpm_modifiers;
7304 static unsigned char old_buttons = 0;
7305
7306 Gpm_GetEvent(&gpm_event);
7307
7308#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007309 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 if (hold_gui_events)
7311 return 0;
7312#endif
7313
7314 row = gpm_event.y - 1;
7315 col = gpm_event.x - 1;
7316
Bram Moolenaar0f873732019-12-05 20:28:46 +01007317 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 string[1] = 'M';
7319 string[2] = 'G';
7320 switch (GPM_BARE_EVENTS(gpm_event.type))
7321 {
7322 case GPM_DRAG:
7323 string[3] = MOUSE_DRAG;
7324 break;
7325 case GPM_DOWN:
7326 buttons_mask = gpm_event.buttons & ~old_buttons;
7327 old_buttons = gpm_event.buttons;
7328 switch (buttons_mask)
7329 {
7330 case GPM_B_LEFT:
7331 button = MOUSE_LEFT;
7332 break;
7333 case GPM_B_MIDDLE:
7334 button = MOUSE_MIDDLE;
7335 break;
7336 case GPM_B_RIGHT:
7337 button = MOUSE_RIGHT;
7338 break;
7339 default:
7340 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007341 // Don't know what to do. Can more than one button be
7342 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 }
7344 string[3] = (char_u)(button | 0x20);
7345 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7346 break;
7347 case GPM_UP:
7348 string[3] = MOUSE_RELEASE;
7349 old_buttons &= ~gpm_event.buttons;
7350 break;
7351 default:
7352 return 0;
7353 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007354 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 gpm_modifiers = gpm_event.modifiers;
7356 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007357 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7358 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7359 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7361 vim_modifiers |= MOUSE_SHIFT;
7362
7363 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7364 vim_modifiers |= MOUSE_CTRL;
7365 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7366 vim_modifiers |= MOUSE_ALT;
7367 string[3] |= vim_modifiers;
7368 string[4] = (char_u)(col + ' ' + 1);
7369 string[5] = (char_u)(row + ' ' + 1);
7370 add_to_input_buf(string, 6);
7371 return 6;
7372}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007373#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007375#ifdef FEAT_SYSMOUSE
7376/*
7377 * Initialize connection with sysmouse.
7378 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7379 * output, any sysmouse output than will be processed via sig_sysmouse().
7380 * Return OK if succeeded, FAIL if failed.
7381 */
7382 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007383sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007384{
7385 struct mouse_info mouse;
7386
7387 mouse.operation = MOUSE_MODE;
7388 mouse.u.mode.mode = 0;
7389 mouse.u.mode.signal = SIGUSR2;
7390 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7391 {
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007392 signal(SIGUSR2, (void (*)())sig_sysmouse);
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007393 mouse.operation = MOUSE_SHOW;
7394 ioctl(1, CONS_MOUSECTL, &mouse);
7395 return OK;
7396 }
7397 return FAIL;
7398}
7399
7400/*
7401 * Stop processing SIGUSR2 signals, and also make sure that
7402 * virtual console do not send us any sysmouse related signal.
7403 */
7404 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007405sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007406{
7407 struct mouse_info mouse;
7408
7409 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7410 mouse.operation = MOUSE_MODE;
7411 mouse.u.mode.mode = 0;
7412 mouse.u.mode.signal = 0;
7413 ioctl(1, CONS_MOUSECTL, &mouse);
7414}
7415
7416/*
7417 * Gets info from sysmouse and adds special keys to input buf.
7418 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01007419 static void
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007420sig_sysmouse SIGDEFARG(sigarg)
7421{
7422 struct mouse_info mouse;
7423 struct video_info video;
7424 char_u string[6];
7425 int row, col;
7426 int button;
7427 int buttons;
7428 static int oldbuttons = 0;
7429
7430#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007431 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007432 if (hold_gui_events)
7433 return;
7434#endif
7435
7436 mouse.operation = MOUSE_GETINFO;
7437 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7438 && ioctl(1, FBIO_MODEINFO, &video) != -1
7439 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7440 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7441 {
7442 row = mouse.u.data.y / video.vi_cheight;
7443 col = mouse.u.data.x / video.vi_cwidth;
7444 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007445 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007446 string[1] = 'M';
7447 string[2] = 'S';
7448 if (oldbuttons == buttons && buttons != 0)
7449 {
7450 button = MOUSE_DRAG;
7451 }
7452 else
7453 {
7454 switch (buttons)
7455 {
7456 case 0:
7457 button = MOUSE_RELEASE;
7458 break;
7459 case 1:
7460 button = MOUSE_LEFT;
7461 break;
7462 case 2:
7463 button = MOUSE_MIDDLE;
7464 break;
7465 case 4:
7466 button = MOUSE_RIGHT;
7467 break;
7468 default:
7469 return;
7470 }
7471 oldbuttons = buttons;
7472 }
7473 string[3] = (char_u)(button);
7474 string[4] = (char_u)(col + ' ' + 1);
7475 string[5] = (char_u)(row + ' ' + 1);
7476 add_to_input_buf(string, 6);
7477 }
7478 return;
7479}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007480#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007481
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007483typedef char_u * (*STRPROCSTR)(char_u *);
7484typedef char_u * (*INTPROCSTR)(int);
7485typedef int (*STRPROCINT)(char_u *);
7486typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487
7488/*
7489 * Call a DLL routine which takes either a string or int param
7490 * and returns an allocated string.
7491 */
7492 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007493mch_libcall(
7494 char_u *libname,
7495 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007496 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007497 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007498 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007499 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500{
7501# if defined(USE_DLOPEN)
7502 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007503 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504# else
7505 shl_t hinstLib;
7506# endif
7507 STRPROCSTR ProcAdd;
7508 INTPROCSTR ProcAddI;
7509 char_u *retval_str = NULL;
7510 int retval_int = 0;
7511 int success = FALSE;
7512
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007513 /*
7514 * Get a handle to the DLL module.
7515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007517 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007518 (void)dlerror();
7519
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 hinstLib = dlopen((char *)libname, RTLD_LAZY
7521# ifdef RTLD_LOCAL
7522 | RTLD_LOCAL
7523# endif
7524 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007525 if (hinstLib == NULL)
7526 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007527 // "dlerr" must be used before dlclose()
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007528 dlerr = dlerror();
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007529 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007530 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532# else
7533 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7534# endif
7535
Bram Moolenaar0f873732019-12-05 20:28:46 +01007536 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 if (hinstLib != NULL)
7538 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007539# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 /*
7541 * Catch a crash when calling the library function. For example when
7542 * using a number where a string pointer is expected.
7543 */
7544 mch_startjmp();
7545 if (SETJMP(lc_jump_env) != 0)
7546 {
7547 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007548# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007549 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 mch_didjmp();
7552 }
7553 else
7554# endif
7555 {
7556 retval_str = NULL;
7557 retval_int = 0;
7558
7559 if (argstring != NULL)
7560 {
7561# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007562 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007563 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564# else
7565 if (shl_findsym(&hinstLib, (const char *)funcname,
7566 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7567 ProcAdd = NULL;
7568# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007569 if ((success = (ProcAdd != NULL
7570# if defined(USE_DLOPEN)
7571 && dlerr == NULL
7572# endif
7573 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 {
7575 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007576 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 else
7578 retval_str = (ProcAdd)(argstring);
7579 }
7580 }
7581 else
7582 {
7583# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007584 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007585 dlerr = dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586# else
7587 if (shl_findsym(&hinstLib, (const char *)funcname,
7588 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7589 ProcAddI = NULL;
7590# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007591 if ((success = (ProcAddI != NULL
7592# if defined(USE_DLOPEN)
7593 && dlerr == NULL
7594# endif
7595 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 {
7597 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007598 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 else
7600 retval_str = (ProcAddI)(argint);
7601 }
7602 }
7603
Bram Moolenaar0f873732019-12-05 20:28:46 +01007604 // Save the string before we free the library.
7605 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 if (string_result == NULL)
7607 *number_result = retval_int;
7608 else if (retval_str != NULL
7609 && retval_str != (char_u *)1
7610 && retval_str != (char_u *)-1)
7611 *string_result = vim_strsave(retval_str);
7612 }
7613
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007614# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 mch_endjmp();
7616# ifdef SIGHASARG
7617 if (lc_signal != 0)
7618 {
7619 int i;
7620
Bram Moolenaar0f873732019-12-05 20:28:46 +01007621 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 for (i = 0; signal_info[i].sig != -1; i++)
7623 if (lc_signal == signal_info[i].sig)
7624 break;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00007625 semsg(e_got_sig_str_in_libcall, signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 }
7627# endif
7628# endif
7629
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007631 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007632 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007633 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007634
Bram Moolenaar0f873732019-12-05 20:28:46 +01007635 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 (void)dlclose(hinstLib);
7637# else
7638 (void)shl_unload(hinstLib);
7639# endif
7640 }
7641
7642 if (!success)
7643 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007644 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 return FAIL;
7646 }
7647
7648 return OK;
7649}
7650#endif
7651
7652#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007653static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654static int xterm_button;
7655
7656/*
7657 * Setup a dummy window for X selections in a terminal.
7658 */
7659 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007660setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661{
7662 int z = 0;
7663 char *strp = "";
7664 Widget AppShell;
7665
7666 if (!x_connect_to_server())
7667 return;
7668
7669 open_app_context();
7670 if (app_context != NULL && xterm_Shell == (Widget)0)
7671 {
7672 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007673# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007675# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007676# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007677 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678
7679 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007680 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681# endif
7682
Bram Moolenaar0f873732019-12-05 20:28:46 +01007683 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 oldhandler = XSetErrorHandler(x_error_check);
7685
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007686# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007687 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7689 mch_startjmp();
7690 if (SETJMP(lc_jump_env) != 0)
7691 {
7692 mch_didjmp();
7693 xterm_dpy = NULL;
7694 }
7695 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 {
7698 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7699 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007700 if (xterm_dpy != NULL)
7701 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007702# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 }
7706
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007707# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007708 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007710# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007711 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 (void)XSetErrorHandler(oldhandler);
7713
7714 if (xterm_dpy == NULL)
7715 {
7716 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007717 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 return;
7719 }
7720
Bram Moolenaar0f873732019-12-05 20:28:46 +01007721 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 (void)XSetIOErrorHandler(x_IOerror_handler);
7723
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007724# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007726 {
7727 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007728 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007729 verbose_leave();
7730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731# endif
7732
Bram Moolenaar0f873732019-12-05 20:28:46 +01007733 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7735 applicationShellWidgetClass, xterm_dpy,
7736 NULL);
7737 if (AppShell == (Widget)0)
7738 return;
7739 xterm_Shell = XtVaCreatePopupShell("VIM",
7740 topLevelShellWidgetClass, AppShell,
7741 XtNmappedWhenManaged, 0,
7742 XtNwidth, 1,
7743 XtNheight, 1,
7744 NULL);
7745 if (xterm_Shell == (Widget)0)
7746 return;
7747
7748 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007749 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 if (x11_display == NULL)
7751 x11_display = xterm_dpy;
7752
7753 XtRealizeWidget(xterm_Shell);
7754 XSync(xterm_dpy, False);
7755 xterm_update();
7756 }
7757 if (xterm_Shell != (Widget)0)
7758 {
7759 clip_init(TRUE);
7760 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7761 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007762 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 if (test_x11_window(xterm_dpy) == FAIL)
7764 x11_window = 0;
7765 if (x11_window != 0)
7766 xterm_trace = 0;
7767 }
7768}
7769
7770 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007771start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
7773 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7774 return;
7775 xterm_trace = 1;
7776 xterm_button = button;
7777 do_xterm_trace();
7778}
7779
7780
7781 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007782stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
7784 if (xterm_trace < 0)
7785 return;
7786 xterm_trace = 0;
7787}
7788
7789/*
7790 * Query the xterm pointer and generate mouse termcodes if necessary
7791 * return TRUE if dragging is active, else FALSE
7792 */
7793 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007794do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
7796 Window root, child;
7797 int root_x, root_y;
7798 int win_x, win_y;
7799 int row, col;
7800 int_u mask_return;
7801 char_u buf[50];
7802 char_u *strp;
7803 long got_hints;
7804 static char_u *mouse_code;
7805 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7806 static int prev_row = 0, prev_col = 0;
7807 static XSizeHints xterm_hints;
7808
7809 if (xterm_trace <= 0)
7810 return FALSE;
7811
7812 if (xterm_trace == 1)
7813 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007814 // Get the hints just before tracking starts. The font size might
7815 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007816 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7817 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 || xterm_hints.width_inc <= 1
7819 || xterm_hints.height_inc <= 1)
7820 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007821 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 return FALSE;
7823 }
7824
Bram Moolenaar0f873732019-12-05 20:28:46 +01007825 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 mouse_code = find_termcode(mouse_name);
7827 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007828 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 xterm_trace = 2;
7830
Bram Moolenaar0f873732019-12-05 20:28:46 +01007831 // Find the offset of the chars, there might be a scrollbar on the
7832 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7834 &win_x, &win_y, &mask_return);
7835 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7836 - (xterm_hints.height_inc / 2);
7837 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7838 xterm_hints.y = 2;
7839 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7840 - (xterm_hints.width_inc / 2);
7841 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7842 xterm_hints.x = 2;
7843 return TRUE;
7844 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007845 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {
7847 xterm_trace = 0;
7848 return FALSE;
7849 }
7850
7851 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7852 &win_x, &win_y, &mask_return);
7853
7854 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7855 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7856 if (row == prev_row && col == prev_col)
7857 return TRUE;
7858
7859 STRCPY(buf, mouse_code);
7860 strp = buf + STRLEN(buf);
7861 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7862 *strp++ = (char_u)(col + ' ' + 1);
7863 *strp++ = (char_u)(row + ' ' + 1);
7864 *strp = 0;
7865 add_to_input_buf(buf, STRLEN(buf));
7866
7867 prev_row = row;
7868 prev_col = col;
7869 return TRUE;
7870}
7871
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007872# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873/*
7874 * Destroy the display, window and app_context. Required for GTK.
7875 */
7876 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007877clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878{
7879 if (xterm_Shell != (Widget)0)
7880 {
7881 XtDestroyWidget(xterm_Shell);
7882 xterm_Shell = (Widget)0;
7883 }
7884 if (xterm_dpy != NULL)
7885 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007886# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007887 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 if (x11_display == xterm_dpy)
7891 x11_display = NULL;
7892 xterm_dpy = NULL;
7893 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007894# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 if (app_context != (XtAppContext)NULL)
7896 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007897 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 XtDestroyApplicationContext(app_context);
7899 app_context = (XtAppContext)NULL;
7900 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007901# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902}
7903# endif
7904
7905/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007906 * Catch up with GUI or X events.
7907 */
7908 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007909clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007910{
7911# ifdef FEAT_GUI
7912 if (gui.in_use)
7913 gui_mch_update();
7914 else
7915# endif
7916 if (xterm_Shell != (Widget)0)
7917 xterm_update();
7918}
7919
7920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 * Catch up with any queued X events. This may put keyboard input into the
7922 * input buffer, call resize call-backs, trigger timers etc. If there is
7923 * nothing in the X event queue (& no timers pending), then we return
7924 * immediately.
7925 */
7926 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007927xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928{
7929 XEvent event;
7930
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007931 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007933 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007935 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007936 break;
7937
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007938 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007939 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007940 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007941 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007942#ifdef FEAT_CLIENTSERVER
7943 {
7944 XPropertyEvent *e = (XPropertyEvent *)&event;
7945
7946 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007948 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007951 XtDispatchEvent(&event);
7952 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007953 else
7954 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007955 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007956 XtAppProcessEvent(app_context, mask);
7957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 }
7959}
7960
7961 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007962clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963{
7964 if (xterm_Shell != (Widget)0)
7965 return clip_x11_own_selection(xterm_Shell, cbd);
7966 return FAIL;
7967}
7968
7969 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007970clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971{
7972 if (xterm_Shell != (Widget)0)
7973 clip_x11_lose_selection(xterm_Shell, cbd);
7974}
7975
7976 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007977clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
7979 if (xterm_Shell != (Widget)0)
7980 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7981}
7982
7983 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007984clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
7986 clip_x11_set_selection(cbd);
7987}
7988#endif
7989
7990
7991#if defined(USE_XSMP) || defined(PROTO)
7992/*
7993 * Code for X Session Management Protocol.
7994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995
7996# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997/*
7998 * This is our chance to ask the user if they want to save,
7999 * or abort the logout
8000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008002xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003{
Bram Moolenaare1004402020-10-24 20:49:43 +02008004 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 int cancel_shutdown = False;
8006
Bram Moolenaare1004402020-10-24 20:49:43 +02008007 save_cmod_flags = cmdmod.cmod_flags;
8008 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01008009 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01008010 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02008012 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01008013 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 out_flush();
8015
Bram Moolenaar0f873732019-12-05 20:28:46 +01008016 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 SmcInteractDone(smc_conn, cancel_shutdown);
8018
Bram Moolenaar0f873732019-12-05 20:28:46 +01008019 // Finish off
8020 // Only end save-yourself here if we're not cancelling shutdown;
8021 // we'll get a cancelled callback later in which we'll end it.
8022 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 if (!cancel_shutdown)
8024 {
8025 xsmp.save_yourself = False;
8026 SmcSaveYourselfDone(smc_conn, True);
8027 }
8028}
8029# endif
8030
8031/*
8032 * Callback that starts save-yourself.
8033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008035xsmp_handle_save_yourself(
8036 SmcConn smc_conn,
8037 SmPointer client_data UNUSED,
8038 int save_type UNUSED,
8039 Bool shutdown,
8040 int interact_style UNUSED,
8041 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008043 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 if (xsmp.save_yourself)
8045 SmcSaveYourselfDone(smc_conn, True);
8046 xsmp.save_yourself = True;
8047 xsmp.shutdown = shutdown;
8048
Bram Moolenaar0f873732019-12-05 20:28:46 +01008049 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01008051 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052
8053 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008054 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055
8056# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008057 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008059 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 SmcInteractRequest(smc_conn, SmDialogError,
8061 xsmp_handle_interaction, client_data);
8062 else
8063# endif
8064 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008065 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 SmcSaveYourselfDone(smc_conn, True);
8067 xsmp.save_yourself = False;
8068 }
8069}
8070
8071
8072/*
8073 * Callback to warn us of imminent death.
8074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008076xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077{
8078 xsmp_close();
8079
Bram Moolenaar0f873732019-12-05 20:28:46 +01008080 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 getout_preserve_modified(0);
8082}
8083
8084
8085/*
8086 * Callback to tell us that save-yourself has completed.
8087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008089xsmp_save_complete(
8090 SmcConn smc_conn UNUSED,
8091 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092{
8093 xsmp.save_yourself = False;
8094}
8095
8096
8097/*
8098 * Callback to tell us that an instigated shutdown was cancelled
8099 * (maybe even by us)
8100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008102xsmp_shutdown_cancelled(
8103 SmcConn smc_conn,
8104 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106 if (xsmp.save_yourself)
8107 SmcSaveYourselfDone(smc_conn, True);
8108 xsmp.save_yourself = False;
8109 xsmp.shutdown = False;
8110}
8111
8112
8113/*
8114 * Callback to tell us that a new ICE connection has been established.
8115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008117xsmp_ice_connection(
8118 IceConn iceConn,
8119 IcePointer clientData UNUSED,
8120 Bool opening,
8121 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008123 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 if (opening)
8125 {
8126 xsmp_icefd = IceConnectionNumber(iceConn);
8127 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8128 }
8129}
8130
8131
Bram Moolenaar0f873732019-12-05 20:28:46 +01008132// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008134xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135{
8136 Bool rep;
8137
8138 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8139 == IceProcessMessagesIOError)
8140 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008141 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008143 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 xsmp_close();
8145 return FAIL;
8146 }
8147 else
8148 return OK;
8149}
8150
8151static int dummy;
8152
Bram Moolenaar0f873732019-12-05 20:28:46 +01008153// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 void
8155xsmp_init(void)
8156{
8157 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 SmcCallbacks smcallbacks;
8159#if 0
8160 SmPropValue smname;
8161 SmProp smnameprop;
8162 SmProp *smprops[1];
8163#endif
8164
8165 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008166 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167
8168 xsmp.save_yourself = xsmp.shutdown = False;
8169
Bram Moolenaar0f873732019-12-05 20:28:46 +01008170 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8172 smcallbacks.save_yourself.client_data = NULL;
8173 smcallbacks.die.callback = xsmp_die;
8174 smcallbacks.die.client_data = NULL;
8175 smcallbacks.save_complete.callback = xsmp_save_complete;
8176 smcallbacks.save_complete.client_data = NULL;
8177 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8178 smcallbacks.shutdown_cancelled.client_data = NULL;
8179
Bram Moolenaar0f873732019-12-05 20:28:46 +01008180 // Set up a watch on ICE connection creations. The "dummy" argument is
8181 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8183 {
8184 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008185 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 return;
8187 }
8188
Bram Moolenaar0f873732019-12-05 20:28:46 +01008189 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 xsmp.smcconn = SmcOpenConnection(
8191 NULL,
8192 NULL,
8193 SmProtoMajor,
8194 SmProtoMinor,
8195 SmcSaveYourselfProcMask | SmcDieProcMask
8196 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8197 &smcallbacks,
8198 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008199 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008200 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 errorstring);
8202 if (xsmp.smcconn == NULL)
8203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008205 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008206 char errorreport[132];
8207
8208 // If the message is too long it might not be NUL terminated. Add
8209 // a NUL at the end to make sure we don't go over the end.
8210 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008211 vim_snprintf(errorreport, sizeof(errorreport),
8212 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008213 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 return;
8216 }
8217 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8218
8219#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008220 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 smname.value = "vim";
8222 smname.length = 3;
8223 smnameprop.name = "SmProgram";
8224 smnameprop.type = "SmARRAY8";
8225 smnameprop.num_vals = 1;
8226 smnameprop.vals = &smname;
8227
8228 smprops[0] = &smnameprop;
8229 SmcSetProperties(xsmp.smcconn, 1, smprops);
8230#endif
8231}
8232
8233
Bram Moolenaar0f873732019-12-05 20:28:46 +01008234// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008236xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237{
8238 if (xsmp_icefd != -1)
8239 {
8240 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008241 if (xsmp.clientid != NULL)
8242 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008243 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 xsmp_icefd = -1;
8245 }
8246}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008247#endif // USE_XSMP
Paul Ollis65745772022-06-05 16:55:54 +01008248
8249#if defined(FEAT_RELTIME) || defined(PROTO)
8250# if defined(HAVE_TIMER_CREATE) || defined(MACOS_X)
8251/*
8252 * Implement timeout with timer_create() and timer_settime().
8253 */
8254static int timeout_flag = FALSE;
8255static timer_t timer_id;
8256static int timer_created = FALSE;
8257
8258/*
8259 * Callback for when the timer expires.
8260 */
8261 static void
8262set_flag(union sigval _unused UNUSED)
8263{
8264 timeout_flag = TRUE;
8265}
8266
8267/*
8268 * Stop any active timeout.
8269 */
8270 void
8271stop_timeout(void)
8272{
8273 static struct itimerspec disarm = {{0, 0}, {0, 0}};
8274
8275 if (timer_created)
8276 {
8277 int ret = timer_settime(timer_id, 0, &disarm, NULL);
8278
8279 if (ret < 0)
8280 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8281 }
8282
8283 // Clear the current timeout flag; any previous timeout should be
8284 // considered _not_ triggered.
8285 timeout_flag = FALSE;
8286}
8287
8288/*
8289 * Start the timeout timer.
8290 *
8291 * The return value is a pointer to a flag that is initialised to FALSE. If the
8292 * timeout expires, the flag is set to TRUE. This will only return pointers to
8293 * static memory; i.e. any pointer returned by this function may always be
8294 * safely dereferenced.
8295 *
8296 * This function is not expected to fail, but if it does it will still return a
8297 * valid flag pointer; the flag will remain stuck as FALSE .
8298 */
8299 const int *
8300start_timeout(long msec)
8301{
8302 struct itimerspec interval = {
8303 {0, 0}, // Do not repeat.
8304 {msec / 1000, (msec % 1000) * 1000000}}; // Timeout interval
8305 int ret;
8306
8307 // This is really the caller's responsibility, but let's make sure the
8308 // previous timer has been stopped.
8309 stop_timeout();
8310 timeout_flag = FALSE;
8311
8312 if (!timer_created)
8313 {
8314 struct sigevent action = {0};
8315
8316 action.sigev_notify = SIGEV_THREAD;
8317 action.sigev_notify_function = set_flag;
8318 ret = timer_create(CLOCK_MONOTONIC, &action, &timer_id);
8319 if (ret < 0)
8320 {
8321 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8322 return &timeout_flag;
8323 }
8324 timer_created = TRUE;
8325 }
8326
8327 ret = timer_settime(timer_id, 0, &interval, NULL);
8328 if (ret < 0)
8329 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8330
8331 return &timeout_flag;
8332}
8333
8334# else
8335
8336/*
8337 * Implement timeout with setitimer()
8338 */
8339static struct itimerval prev_interval;
8340static struct sigaction prev_sigaction;
8341static int timeout_flag = FALSE;
8342static int timer_active = FALSE;
8343static int timer_handler_active = FALSE;
8344static int alarm_pending = FALSE;
8345
8346/*
8347 * Handle SIGALRM for a timeout.
8348 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01008349 static void
Paul Ollis65745772022-06-05 16:55:54 +01008350set_flag SIGDEFARG(sigarg)
8351{
8352 if (alarm_pending)
8353 alarm_pending = FALSE;
8354 else
8355 timeout_flag = TRUE;
8356}
8357
8358/*
8359 * Stop any active timeout.
8360 */
8361 void
8362stop_timeout(void)
8363{
8364 static struct itimerval disarm = {{0, 0}, {0, 0}};
8365 int ret;
8366
8367 if (timer_active)
8368 {
8369 timer_active = FALSE;
8370 ret = setitimer(ITIMER_REAL, &disarm, &prev_interval);
8371 if (ret < 0)
8372 // Should only get here as a result of coding errors.
8373 semsg(_(e_could_not_clear_timeout_str), strerror(errno));
8374 }
8375
8376 if (timer_handler_active)
8377 {
8378 timer_handler_active = FALSE;
8379 ret = sigaction(SIGALRM, &prev_sigaction, NULL);
8380 if (ret < 0)
8381 // Should only get here as a result of coding errors.
8382 semsg(_(e_could_not_reset_handler_for_timeout_str),
8383 strerror(errno));
8384 }
8385 timeout_flag = 0;
8386}
8387
8388/*
8389 * Start the timeout timer.
8390 *
8391 * The return value is a pointer to a flag that is initialised to FALSE. If the
8392 * timeout expires, the flag is set to TRUE. This will only return pointers to
8393 * static memory; i.e. any pointer returned by this function may always be
8394 * safely dereferenced.
8395 *
8396 * This function is not expected to fail, but if it does it will still return a
8397 * valid flag pointer; the flag will remain stuck as FALSE .
8398 */
8399 const int *
8400start_timeout(long msec)
8401{
8402 struct itimerval interval = {
8403 {0, 0}, // Do not repeat.
8404 {msec / 1000, (msec % 1000) * 1000}}; // Timeout interval
8405 struct sigaction handle_alarm;
8406 int ret;
8407 sigset_t sigs;
8408 sigset_t saved_sigs;
8409
8410 // This is really the caller's responsibility, but let's make sure the
8411 // previous timer has been stopped.
8412 stop_timeout();
8413
8414 // There is a small chance that SIGALRM is pending and so the handler must
8415 // ignore it on the first call.
8416 alarm_pending = FALSE;
8417 ret = sigemptyset(&sigs);
8418 ret = ret == 0 ? sigaddset(&sigs, SIGALRM) : ret;
8419 ret = ret == 0 ? sigprocmask(SIG_BLOCK, &sigs, &saved_sigs) : ret;
8420 timeout_flag = FALSE;
8421 ret = ret == 0 ? sigpending(&sigs) : ret;
8422 if (ret == 0)
8423 {
8424 alarm_pending = sigismember(&sigs, SIGALRM);
8425 ret = ret == 0 ? sigprocmask(SIG_SETMASK, &saved_sigs, NULL) : ret;
8426 }
8427 if (unlikely(ret != 0 || alarm_pending < 0))
8428 {
8429 // Just catching coding errors. Write an error message, but carry on.
8430 semsg(_(e_could_not_check_for_pending_sigalrm_str), strerror(errno));
8431 alarm_pending = FALSE;
8432 }
8433
8434 // Set up the alarm handler first.
8435 ret = sigemptyset(&handle_alarm.sa_mask);
8436 handle_alarm.sa_handler = set_flag;
8437 handle_alarm.sa_flags = 0;
8438 ret = ret == 0 ? sigaction(SIGALRM, &handle_alarm, &prev_sigaction) : ret;
8439 if (ret < 0)
8440 {
8441 // Should only get here as a result of coding errors.
8442 semsg(_(e_could_not_set_handler_for_timeout_str), strerror(errno));
8443 return &timeout_flag;
8444 }
8445 timer_handler_active = TRUE;
8446
8447 // Set up the interval timer once the alarm handler is in place.
8448 ret = setitimer(ITIMER_REAL, &interval, &prev_interval);
8449 if (ret < 0)
8450 {
8451 // Should only get here as a result of coding errors.
8452 semsg(_(e_could_not_set_timeout_str), strerror(errno));
8453 stop_timeout();
8454 return &timeout_flag;
8455 }
8456
8457 timer_active = TRUE;
8458 return &timeout_flag;
8459}
8460# endif // HAVE_TIMER_CREATE
8461#endif // FEAT_RELTIME