blob: e1296c4b5504e1b34918922d0a439042a7150264 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * OS/2 port by Paul Slootman
5 * VMS merge by Zoltan Arpadffy
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code.
10 */
11
12/*
13 * os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
14 * Also for OS/2, using the excellent EMX package!!!
Bram Moolenaar041c7102020-05-30 18:14:57 +020015 * Also for Atari MiNT.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016 *
17 * A lot of this file was originally written by Juergen Weigert and later
18 * changed beyond recognition.
19 */
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#include "vim.h"
22
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023#ifdef FEAT_MZSCHEME
24# include "if_mzsch.h"
25#endif
26
Bram Moolenaar0f873732019-12-05 20:28:46 +010027#include "os_unixx.h" // unix includes for os_unix.c only
Bram Moolenaar071d4272004-06-13 20:20:40 +000028
29#ifdef USE_XSMP
30# include <X11/SM/SMlib.h>
31#endif
32
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000033#ifdef HAVE_SELINUX
34# include <selinux/selinux.h>
35static int selinux_enabled = -1;
36#endif
37
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020038#ifdef HAVE_SMACK
39# include <attr/xattr.h>
40# include <linux/xattr.h>
41# ifndef SMACK_LABEL_LEN
42# define SMACK_LABEL_LEN 1024
43# endif
44#endif
45
Bram Moolenaara2442432007-04-26 14:26:37 +000046#ifdef __CYGWIN__
Bram Moolenaar4f974752019-02-17 17:44:42 +010047# ifndef MSWIN
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000048# include <cygwin/version.h>
Bram Moolenaar0f873732019-12-05 20:28:46 +010049# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
50 // for cygwin_conv_path()
Bram Moolenaar693e40c2013-02-26 14:56:42 +010051# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
52# define WIN32_LEAN_AND_MEAN
53# include <windows.h>
54# include "winclip.pro"
55# endif
Bram Moolenaara2442432007-04-26 14:26:37 +000056# endif
57#endif
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#ifdef FEAT_MOUSE_GPM
60# include <gpm.h>
Bram Moolenaar0f873732019-12-05 20:28:46 +010061// <linux/keyboard.h> contains defines conflicting with "keymap.h",
62// I just copied relevant defines here. A cleaner solution would be to put gpm
63// code into separate file and include there linux/keyboard.h
64// #include <linux/keyboard.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000065# define KG_SHIFT 0
66# define KG_CTRL 2
67# define KG_ALT 3
68# define KG_ALTGR 1
69# define KG_SHIFTL 4
70# define KG_SHIFTR 5
71# define KG_CTRLL 6
72# define KG_CTRLR 7
73# define KG_CAPSSHIFT 8
74
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010075static void gpm_close(void);
76static int gpm_open(void);
77static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000078#endif
79
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000080#ifdef FEAT_SYSMOUSE
81# include <sys/consio.h>
82# include <sys/fbio.h>
83
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010084static int sysmouse_open(void);
85static void sysmouse_close(void);
Bram Moolenaard99df422016-01-29 23:20:40 +010086static RETSIGTYPE sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000087#endif
88
Bram Moolenaar071d4272004-06-13 20:20:40 +000089/*
90 * end of autoconf section. To be extended...
91 */
92
Bram Moolenaar0f873732019-12-05 20:28:46 +010093// Are the following #ifdefs still required? And why? Is that for X11?
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
95#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
96# ifdef SIGWINCH
97# undef SIGWINCH
98# endif
99# ifdef TIOCGWINSZ
100# undef TIOCGWINSZ
101# endif
102#endif
103
Bram Moolenaar0f873732019-12-05 20:28:46 +0100104#if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105# define SIGWINCH SIGWINDOW
106#endif
107
108#ifdef FEAT_X11
109# include <X11/Xlib.h>
110# include <X11/Xutil.h>
111# include <X11/Xatom.h>
112# ifdef FEAT_XCLIPBOARD
113# include <X11/Intrinsic.h>
114# include <X11/Shell.h>
115# include <X11/StringDefs.h>
116static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static void clip_update(void);
118static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119# endif
120
121# if defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE)
122Window x11_window = 0;
123# endif
124Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
126
Bram Moolenaar5c3128e2020-05-11 20:54:42 +0200127static int ignore_sigtstp = FALSE;
128
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifdef FEAT_TITLE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
132static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200133static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +0200134static int unix_did_set_title = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135static char_u *oldicon = NULL;
136static int did_set_icon = FALSE;
137#endif
138
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140
Bram Moolenaar205b8862011-09-07 15:04:31 +0200141#ifdef HAVE_UNION_WAIT
142typedef union wait waitstatus;
143#else
144typedef int waitstatus;
145#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200146static int WaitForChar(long msec, int *interrupted, int ignore_input);
147static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar041c7102020-05-30 18:14:57 +0200148#ifdef VMS
Bram Moolenaarcda77642016-06-04 13:32:35 +0200149int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200151static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#endif
153
154#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static int do_xterm_trace(void);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100156# define XT_TRACE_DELAY 50 // delay for xterm tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160
161#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100162static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#endif
164#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100165static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#endif
167#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100168static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
170#if defined(SIGALRM) && defined(FEAT_X11) \
171 && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
172# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100173static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100174// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200175static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100177static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100179static void catch_int_signal(void);
180static void set_signals(void);
181static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200182#ifdef HAVE_SIGPROCMASK
183# define SIGSET_DECL(set) sigset_t set;
184# define BLOCK_SIGNALS(set) block_signals(set)
185# define UNBLOCK_SIGNALS(set) unblock_signals(set)
186#else
187# define SIGSET_DECL(set)
188# define BLOCK_SIGNALS(set) do { /**/ } while (0)
189# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
190#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100191static int have_wildcard(int, char_u **);
192static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100194static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195
196#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000197# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198#endif
199
Bram Moolenaar0f873732019-12-05 20:28:46 +0100200// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200201static volatile sig_atomic_t do_resize = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202static char_u *extra_shell_arg = NULL;
203static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100204// volatile because it is used in signal handler deathtrap().
205static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
206// volatile because it is used in signal handler deathtrap().
207static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100209#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
210static int dont_check_job_ended = 0;
211#endif
212
Bram Moolenaar26e86442020-05-17 14:06:16 +0200213// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
214static tmode_T mch_cur_tmode = TMODE_COOK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215
216#ifdef USE_XSMP
217typedef struct
218{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100219 SmcConn smcconn; // The SM connection ID
220 IceConn iceconn; // The ICE connection ID
221 char *clientid; // The client ID for the current smc session
222 Bool save_yourself; // If we're in the middle of a save_yourself
223 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224} xsmp_config_T;
225
226static xsmp_config_T xsmp;
227#endif
228
229#ifdef SYS_SIGLIST_DECLARED
230/*
231 * I have seen
232 * extern char *_sys_siglist[NSIG];
233 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
234 * that describe the signals. That is nearly what we want here. But
235 * autoconf does only check for sys_siglist (without the underscore), I
236 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100237 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 */
239#endif
240
241static struct signalinfo
242{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100243 int sig; // Signal number, eg. SIGSEGV etc
244 char *name; // Signal name (not char_u!).
245 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246} signal_info[] =
247{
248#ifdef SIGHUP
249 {SIGHUP, "HUP", TRUE},
250#endif
251#ifdef SIGQUIT
252 {SIGQUIT, "QUIT", TRUE},
253#endif
254#ifdef SIGILL
255 {SIGILL, "ILL", TRUE},
256#endif
257#ifdef SIGTRAP
258 {SIGTRAP, "TRAP", TRUE},
259#endif
260#ifdef SIGABRT
261 {SIGABRT, "ABRT", TRUE},
262#endif
263#ifdef SIGEMT
264 {SIGEMT, "EMT", TRUE},
265#endif
266#ifdef SIGFPE
267 {SIGFPE, "FPE", TRUE},
268#endif
269#ifdef SIGBUS
270 {SIGBUS, "BUS", TRUE},
271#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100272#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100273 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 {SIGSEGV, "SEGV", TRUE},
275#endif
276#ifdef SIGSYS
277 {SIGSYS, "SYS", TRUE},
278#endif
279#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100280 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281#endif
282#ifdef SIGTERM
283 {SIGTERM, "TERM", TRUE},
284#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100285#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 {SIGVTALRM, "VTALRM", TRUE},
287#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000288#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100289 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
290 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 {SIGPROF, "PROF", TRUE},
292#endif
293#ifdef SIGXCPU
294 {SIGXCPU, "XCPU", TRUE},
295#endif
296#ifdef SIGXFSZ
297 {SIGXFSZ, "XFSZ", TRUE},
298#endif
299#ifdef SIGUSR1
300 {SIGUSR1, "USR1", TRUE},
301#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000302#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100303 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 {SIGUSR2, "USR2", TRUE},
305#endif
306#ifdef SIGINT
307 {SIGINT, "INT", FALSE},
308#endif
309#ifdef SIGWINCH
310 {SIGWINCH, "WINCH", FALSE},
311#endif
312#ifdef SIGTSTP
313 {SIGTSTP, "TSTP", FALSE},
314#endif
315#ifdef SIGPIPE
316 {SIGPIPE, "PIPE", FALSE},
317#endif
318 {-1, "Unknown!", FALSE}
319};
320
Bram Moolenaar25724922009-07-14 15:38:41 +0000321 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100322mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000323{
324 if (p_verbose >= 5)
325 {
326 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100327 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000328 verbose_leave();
329 }
330# ifdef VMS
331 return chdir(vms_fixfilename(path));
332# else
333 return chdir(path);
334# endif
335}
336
Bram Moolenaar0f873732019-12-05 20:28:46 +0100337// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100338#if defined(ECHOE) && defined(ICANON) \
339 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
340 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200341# define NEW_TTY_SYSTEM
342#endif
343
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000344/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100345 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100348mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200350 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100351 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100352 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353}
354
355/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100356 * Function passed to inchar_loop() to handle window resizing.
357 * If "check_only" is TRUE: Return whether there was a resize.
358 * If "check_only" is FALSE: Deal with the window resized.
359 */
360 static int
361resize_func(int check_only)
362{
363 if (check_only)
364 return do_resize;
365 while (do_resize)
366 handle_resize();
367 return FALSE;
368}
369
370/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000371 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 * Get a characters from the keyboard.
373 * Return the number of characters that are available.
374 * If wtime == 0 do not wait for characters.
375 * If wtime == n wait a short time for characters.
376 * If wtime == -1 wait forever for characters.
377 */
378 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100379mch_inchar(
380 char_u *buf,
381 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100382 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100383 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100385 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
386 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387}
388
389 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100390handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391{
392 do_resize = FALSE;
393 shell_resized();
394}
395
396/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200397 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 */
399 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100400mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200402 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403}
404
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200405#if defined(FEAT_TERMINAL) || defined(PROTO)
406/*
407 * Check for any pending input or messages.
408 */
409 int
410mch_check_messages(void)
411{
412 return WaitForChar(0L, NULL, TRUE);
413}
414#endif
415
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
417# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000418# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419# endif
420# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
421# include <sys/sysctl.h>
422# endif
423# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
424# include <sys/sysinfo.h>
425# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100426# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100427# include <mach/mach_host.h>
428# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100429# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430
431/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000432 * Return total amount of memory available in Kbyte.
433 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100436mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100439 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440
Bram Moolenaar362dc332018-03-05 21:59:37 +0100441# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100442 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100443 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100444 mach_port_t host = mach_host_self();
445 kern_return_t kret;
446# ifdef HOST_VM_INFO64
447 struct vm_statistics64 vm_stat;
448 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449
Bram Moolenaar988615f2018-02-27 17:58:20 +0100450 kret = host_statistics64(host, HOST_VM_INFO64,
451 (host_info64_t)&vm_stat, &count);
452# else
453 struct vm_statistics vm_stat;
454 natural_t count = HOST_VM_INFO_COUNT;
455
456 kret = host_statistics(host, HOST_VM_INFO,
457 (host_info_t)&vm_stat, &count);
458# endif
459 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100460 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100461 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
462 + vm_stat.inactive_count
463# ifdef MAC_OS_X_VERSION_10_9
464 + vm_stat.compressor_page_count
465# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100466 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100467 mach_port_deallocate(mach_task_self(), host);
468 }
469# endif
470
471# ifdef HAVE_SYSCTL
472 if (mem == 0)
473 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100474 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100475 int mib[2];
476 size_t len = sizeof(long_u);
477# ifdef HW_USERMEM64
478 long_u physmem;
479# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100480 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100481 union {
482 int_u u32;
483 long_u u64;
484 } physmem;
485# endif
486
487 mib[0] = CTL_HW;
488# ifdef HW_USERMEM64
489 mib[1] = HW_USERMEM64;
490# else
491 mib[1] = HW_USERMEM;
492# endif
493 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
494 {
495# ifdef HW_USERMEM64
496 mem = (long_u)physmem;
497# else
498 if (len == sizeof(physmem.u64))
499 mem = (long_u)physmem.u64;
500 else
501 mem = (long_u)physmem.u32;
502# endif
503 }
504 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200505# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200507# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 if (mem == 0)
509 {
510 struct sysinfo sinfo;
511
Bram Moolenaar0f873732019-12-05 20:28:46 +0100512 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000514 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200515# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100516 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000517 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
518 {
519 sinfo.mem_unit = sinfo.mem_unit >> 1;
520 --shiftright;
521 }
522 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200523# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200525# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200528# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200530# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 if (mem == 0)
532 {
533 long pagesize, pagecount;
534
Bram Moolenaar0f873732019-12-05 20:28:46 +0100535 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 pagesize = sysconf(_SC_PAGESIZE);
537 pagecount = sysconf(_SC_PHYS_PAGES);
538 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000539 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100540 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000541 while (shiftright > 0 && (pagesize & 1) == 0)
542 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000543 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000544 --shiftright;
545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 mem = (long_u)pagesize * pagecount;
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 Moolenaar0f873732019-12-05 20:28:46 +0100551 // Return the minimum of the physical memory and the user limit, because
552 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200553# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 {
555 struct rlimit rlp;
556
557 if (getrlimit(RLIMIT_DATA, &rlp) == 0
558 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200559# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200561# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000562 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000564 {
565 mem = (long_u)rlp.rlim_cur;
566 shiftright = 10;
567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
571 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000572 return mem >> shiftright;
573 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574}
575#endif
576
577 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100578mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200580 tmode_T old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000581#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100582 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
585 if (ignoreinput)
586 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100587 // Go to cooked mode without echo, to allow SIGINT interrupting us
588 // here. But we don't want QUIT to kill us (CTRL-\ used in a
589 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200590 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000591 in_mch_delay = TRUE;
Bram Moolenaar26e86442020-05-17 14:06:16 +0200592 old_tmode = mch_cur_tmode;
593 if (mch_cur_tmode == TMODE_RAW && msec > 500)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 settmode(TMODE_SLEEP);
595
596 /*
597 * Everybody sleeps in a different way...
598 * Prefer nanosleep(), some versions of usleep() can only sleep up to
599 * one second.
600 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000601#ifdef FEAT_MZSCHEME
602 do
603 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100604 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000605 if (total > p_mzq)
606 msec = p_mzq;
607 else
608 msec = total;
609 total -= msec;
610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611#ifdef HAVE_NANOSLEEP
612 {
613 struct timespec ts;
614
615 ts.tv_sec = msec / 1000;
616 ts.tv_nsec = (msec % 1000) * 1000000;
617 (void)nanosleep(&ts, NULL);
618 }
619#else
620# ifdef HAVE_USLEEP
621 while (msec >= 1000)
622 {
623 usleep((unsigned int)(999 * 1000));
624 msec -= 999;
625 }
626 usleep((unsigned int)(msec * 1000));
627# else
628# ifndef HAVE_SELECT
629 poll(NULL, 0, (int)msec);
630# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 {
632 struct timeval tv;
633
634 tv.tv_sec = msec / 1000;
635 tv.tv_usec = (msec % 1000) * 1000;
636 /*
637 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
638 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
639 */
640 select(0, NULL, NULL, NULL, &tv);
641 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100642# endif // HAVE_SELECT
643# endif // HAVE_NANOSLEEP
644#endif // HAVE_USLEEP
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000645#ifdef FEAT_MZSCHEME
646 }
647 while (total > 0);
648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaar26e86442020-05-17 14:06:16 +0200650 if (msec > 500)
651 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000652 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
654 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200655 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656}
657
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000658#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
660# define HAVE_CHECK_STACK_GROWTH
661/*
662 * Support for checking for an almost-out-of-stack-space situation.
663 */
664
665/*
666 * Return a pointer to an item on the stack. Used to find out if the stack
667 * grows up or down.
668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669static int stack_grows_downwards;
670
671/*
672 * Find out if the stack grows upwards or downwards.
673 * "p" points to a variable on the stack of the caller.
674 */
675 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100676check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677{
678 int i;
679
680 stack_grows_downwards = (p > (char *)&i);
681}
682#endif
683
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000684#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685static char *stack_limit = NULL;
686
687#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
688# include <pthread.h>
689# include <pthread_np.h>
690#endif
691
692/*
693 * Find out until how var the stack can grow without getting into trouble.
694 * Called when starting up and when switching to the signal stack in
695 * deathtrap().
696 */
697 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100698get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699{
700 struct rlimit rlp;
701 int i;
702 long lim;
703
Bram Moolenaar0f873732019-12-05 20:28:46 +0100704 // Set the stack limit to 15/16 of the allowable size. Skip this when the
705 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (getrlimit(RLIMIT_STACK, &rlp) == 0
707 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
708# ifdef RLIM_INFINITY
709 && rlp.rlim_cur != RLIM_INFINITY
710# endif
711 )
712 {
713 lim = (long)rlp.rlim_cur;
714#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
715 {
716 pthread_attr_t attr;
717 size_t size;
718
Bram Moolenaar0f873732019-12-05 20:28:46 +0100719 // On FreeBSD the initial thread always has a fixed stack size, no
720 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 pthread_attr_init(&attr);
722 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
723 {
724 pthread_attr_getstacksize(&attr, &size);
725 if (lim > (long)size)
726 lim = (long)size;
727 }
728 pthread_attr_destroy(&attr);
729 }
730#endif
731 if (stack_grows_downwards)
732 {
733 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
734 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100735 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 stack_limit = (char *)((long)&i / 16L);
737 }
738 else
739 {
740 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
741 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100742 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 }
744 }
745}
746
747/*
748 * Return FAIL when running out of stack space.
749 * "p" must point to any variable local to the caller that's on the stack.
750 */
751 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100752mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753{
754 if (stack_limit != NULL)
755 {
756 if (stack_grows_downwards)
757 {
758 if (p < stack_limit)
759 return FAIL;
760 }
761 else if (p > stack_limit)
762 return FAIL;
763 }
764 return OK;
765}
766#endif
767
768#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
769/*
770 * Support for using the signal stack.
771 * This helps when we run out of stack space, which causes a SIGSEGV. The
772 * signal handler then must run on another stack, since the normal stack is
773 * completely full.
774 */
775
776#ifndef SIGSTKSZ
Bram Moolenaar0f873732019-12-05 20:28:46 +0100777# define SIGSTKSZ 8000 // just a guess of how much stack is needed...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#endif
779
780# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100781static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100783static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784# endif
785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786static char *signal_stack;
787
788 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100789init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790{
791 if (signal_stack != NULL)
792 {
793# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794# ifdef HAVE_SS_BASE
795 sigstk.ss_base = signal_stack;
796# else
797 sigstk.ss_sp = signal_stack;
798# endif
799 sigstk.ss_size = SIGSTKSZ;
800 sigstk.ss_flags = 0;
801 (void)sigaltstack(&sigstk, NULL);
802# else
803 sigstk.ss_sp = signal_stack;
804 if (stack_grows_downwards)
805 sigstk.ss_sp += SIGSTKSZ - 1;
806 sigstk.ss_onstack = 0;
807 (void)sigstack(&sigstk, NULL);
808# endif
809 }
810}
811#endif
812
813/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000814 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 * will barf when the second argument to signal() is ``wrong''.
816 * Let me try it with a few tricky defines from my own osdef.h (jw).
817 */
818#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 static RETSIGTYPE
820sig_winch SIGDEFARG(sigarg)
821{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100822 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
824 do_resize = TRUE;
825 SIGRETURN;
826}
827#endif
828
829#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 static RETSIGTYPE
831catch_sigint SIGDEFARG(sigarg)
832{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100833 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
835 got_int = TRUE;
836 SIGRETURN;
837}
838#endif
839
840#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 static RETSIGTYPE
842catch_sigpwr SIGDEFARG(sigarg)
843{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100844 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000845 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 /*
847 * I'm not sure we get the SIGPWR signal when the system is really going
848 * down or when the batteries are almost empty. Just preserve the swap
849 * files and don't exit, that can't do any harm.
850 */
851 ml_sync_all(FALSE, FALSE);
852 SIGRETURN;
853}
854#endif
855
856#ifdef SET_SIG_ALARM
857/*
858 * signal function for alarm().
859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 static RETSIGTYPE
861sig_alarm SIGDEFARG(sigarg)
862{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100863 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 sig_alarm_called = TRUE;
865 SIGRETURN;
866}
867#endif
868
Bram Moolenaaredce7422019-01-20 18:39:30 +0100869#if (defined(HAVE_SETJMP_H) \
870 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
871 || defined(FEAT_LIBCALL))) \
872 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100873# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100874
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100875// argument to SETJMP()
876static JMP_BUF lc_jump_env;
877
878# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100879// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100880// Volatile because it is used in signal handlers.
881static volatile sig_atomic_t lc_signal;
882# endif
883
884// TRUE when lc_jump_env is valid.
885// Volatile because it is used in signal handler deathtrap().
886static volatile sig_atomic_t lc_active INIT(= FALSE);
887
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888/*
889 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100890 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100892 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 * Usage:
894 * mch_startjmp();
895 * if (SETJMP(lc_jump_env) != 0)
896 * {
897 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100898 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 * }
900 * else
901 * {
902 * do_the_work;
903 * mch_endjmp();
904 * }
905 * Note: Can't move SETJMP() here, because a function calling setjmp() must
906 * not return before the saved environment is used.
907 * Returns OK for normal return, FAIL when the protected code caused a
908 * problem and LONGJMP() was used.
909 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100910 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100911mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912{
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100913# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100915# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 lc_active = TRUE;
917}
918
Bram Moolenaar113e1072019-01-20 15:30:40 +0100919 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100920mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921{
922 lc_active = FALSE;
923}
924
Bram Moolenaar113e1072019-01-20 15:30:40 +0100925 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100926mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100929 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
930 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 init_signal_stack();
932# endif
933}
934#endif
935
936/*
937 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200938 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200940 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
941 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 */
943 static RETSIGTYPE
944deathtrap SIGDEFARG(sigarg)
945{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100946 static int entered = 0; // count the number of times we got here.
947 // Note: when memory has been corrupted
948 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949#ifdef SIGHASARG
950 int i;
951#endif
952
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100953#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 /*
955 * Catch a crash in protected code.
956 * Restores the environment saved in lc_jump_env, which looks like
957 * SETJMP() returns 1.
958 */
959 if (lc_active)
960 {
961# if defined(SIGHASARG)
962 lc_signal = sigarg;
963# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100964 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100966 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968#endif
969
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000970#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000971# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100972 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
973 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
974 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000975 if (in_mch_delay && sigarg == SIGQUIT)
976 SIGRETURN;
977# endif
978
Bram Moolenaar0f873732019-12-05 20:28:46 +0100979 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
980 // here. This avoids that a non-reentrant function is interrupted, e.g.,
981 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000982 if (entered == 0
983 && (0
984# ifdef SIGHUP
985 || sigarg == SIGHUP
986# endif
987# ifdef SIGQUIT
988 || sigarg == SIGQUIT
989# endif
990# ifdef SIGTERM
991 || sigarg == SIGTERM
992# endif
993# ifdef SIGPWR
994 || sigarg == SIGPWR
995# endif
996# ifdef SIGUSR1
997 || sigarg == SIGUSR1
998# endif
999# ifdef SIGUSR2
1000 || sigarg == SIGUSR2
1001# endif
1002 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001003 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001004 SIGRETURN;
1005#endif
1006
Bram Moolenaar0f873732019-12-05 20:28:46 +01001007 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 ++entered;
1009
Bram Moolenaar0f873732019-12-05 20:28:46 +01001010 // Executing autocommands is likely to use more stack space than we have
1011 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001012 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001015 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 set_vim_var_nr(VV_DYING, (long)entered);
1017#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001018 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001020#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001021 // Since we are now using the signal stack, need to reset the stack
1022 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 get_stack_limit();
1024#endif
1025
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001026#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001027 // This is for opening gdb the moment Vim crashes.
1028 // You need to manually adjust the file name and Vim executable name.
1029 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001030 {
1031# define VI_GDB_FILE "/tmp/vimgdb"
1032# define VIM_NAME "/usr/bin/vim"
1033 FILE *fp = fopen(VI_GDB_FILE, "w");
1034 if (fp)
1035 {
1036 fprintf(fp,
1037 "file %s\n"
1038 "attach %d\n"
1039 "set height 1000\n"
1040 "bt full\n"
1041 , VIM_NAME, getpid());
1042 fclose(fp);
1043 system("xterm -e gdb -x "VI_GDB_FILE);
1044 unlink(VI_GDB_FILE);
1045 }
1046 }
1047#endif
1048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001050 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 for (i = 0; signal_info[i].sig != -1; i++)
1052 if (sigarg == signal_info[i].sig)
1053 break;
1054 deadly_signal = sigarg;
1055#endif
1056
Bram Moolenaar0f873732019-12-05 20:28:46 +01001057 full_screen = FALSE; // don't write message to the GUI, it might be
1058 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 /*
1060 * If something goes wrong after entering here, we may get here again.
1061 * When this happens, give a message and try to exit nicely (resetting the
1062 * terminal mode, etc.)
1063 * When this happens twice, just exit, don't even try to give a message,
1064 * stack may be corrupt or something weird.
1065 * When this still happens again (or memory was corrupted in such a way
1066 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1067 */
1068 if (entered >= 3)
1069 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001070 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 may_core_dump();
1072 if (entered >= 4)
1073 _exit(8);
1074 exit(7);
1075 }
1076 if (entered == 2)
1077 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001078 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001079 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 out_flush();
1081 getout(1);
1082 }
1083
Bram Moolenaar0f873732019-12-05 20:28:46 +01001084 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001086 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 signal_info[i].name);
1088#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001089 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001091
Bram Moolenaar0f873732019-12-05 20:28:46 +01001092 // Preserve files and exit. This sets the really_exiting flag to prevent
1093 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001094 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
Bram Moolenaar0f873732019-12-05 20:28:46 +01001096 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001097
Bram Moolenaar009b2592004-10-24 19:18:58 +00001098#ifdef NBDEBUG
1099 reset_signals();
1100 may_core_dump();
1101 abort();
1102#endif
1103
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 SIGRETURN;
1105}
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001108 * Invoked after receiving SIGCONT. We don't know what happened while
1109 * sleeping, deal with part of that.
1110 */
1111 static void
1112after_sigcont(void)
1113{
1114# ifdef FEAT_TITLE
1115 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1116 // again later.
1117 oldtitle_outdated = TRUE;
1118# endif
1119 settmode(TMODE_RAW);
1120 need_check_timestamps = TRUE;
1121 did_check_timestamps = FALSE;
1122}
1123
1124#if defined(SIGCONT)
1125static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001126static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001127
1128/*
1129 * With multi-threading, suspending might not work immediately. Catch the
1130 * SIGCONT signal, which will be used as an indication whether the suspending
1131 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001132 *
1133 * On Linux, signal is not always handled immediately either.
1134 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001135 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001136 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001137 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001139static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001140static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141
1142/*
1143 * signal handler for SIGCONT
1144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 static RETSIGTYPE
1146sigcont_handler SIGDEFARG(sigarg)
1147{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001148 if (in_mch_suspend)
1149 {
1150 sigcont_received = TRUE;
1151 }
1152 else
1153 {
1154 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1155 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1156 // back to a sane mode. We should redraw, but we can't really do that
1157 // in a signal handler, do a redraw later.
1158 after_sigcont();
1159 redraw_later(CLEAR);
1160 cursor_on_force();
1161 out_flush();
1162 }
1163
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 SIGRETURN;
1165}
1166#endif
1167
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001168#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001169# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001170static void *clip_star_save = NULL;
1171static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001172# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001173
1174/*
1175 * Called when Vim is going to sleep or execute a shell command.
1176 * We can't respond to requests for the X selections. Lose them, otherwise
1177 * other applications will hang. But first copy the text to cut buffer 0.
1178 */
1179 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001180loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001181{
1182 if (clip_star.owned || clip_plus.owned)
1183 {
1184 x11_export_final_selection();
1185 if (clip_star.owned)
1186 clip_lose_selection(&clip_star);
1187 if (clip_plus.owned)
1188 clip_lose_selection(&clip_plus);
1189 if (x11_display != NULL)
1190 XFlush(x11_display);
1191 }
1192}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001193
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001194# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001195/*
1196 * Save clipboard text to restore later.
1197 */
1198 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001199save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001200{
1201 if (clip_star.owned)
1202 clip_star_save = get_register('*', TRUE);
1203 if (clip_plus.owned)
1204 clip_plus_save = get_register('+', TRUE);
1205}
1206
1207/*
1208 * Restore clipboard text if no one own the X selection.
1209 */
1210 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001211restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001212{
1213 if (clip_star_save != NULL)
1214 {
1215 if (!clip_gen_owner_exists(&clip_star))
1216 put_register('*', clip_star_save);
1217 else
1218 free_register(clip_star_save);
1219 clip_star_save = NULL;
1220 }
1221 if (clip_plus_save != NULL)
1222 {
1223 if (!clip_gen_owner_exists(&clip_plus))
1224 put_register('+', clip_plus_save);
1225 else
1226 free_register(clip_plus_save);
1227 clip_plus_save = NULL;
1228 }
1229}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001230# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001231#endif
1232
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233/*
1234 * If the machine has job control, use it to suspend the program,
1235 * otherwise fake it by starting a new shell.
1236 */
1237 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001238mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001240 if (ignore_sigtstp)
1241 return;
1242
Bram Moolenaar041c7102020-05-30 18:14:57 +02001243#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001244 in_mch_suspend = TRUE;
1245
Bram Moolenaar0f873732019-12-05 20:28:46 +01001246 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001248 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249
1250# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001251 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001253# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 sigcont_received = FALSE;
1255# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001256
Bram Moolenaar0f873732019-12-05 20:28:46 +01001257 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001258
1259# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001260 /*
1261 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001262 * immediately, but somehow not all the time, probably because it's handled
1263 * in another thread. Do not call pause() because there would be race
1264 * condition which would hang Vim if signal happened in between the test of
1265 * sigcont_received and the call to pause(). If signal is not yet received,
1266 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1267 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001268 */
1269 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001270 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001271
Bram Moolenaar262735e2009-07-14 10:20:22 +00001272 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar262735e2009-07-14 10:20:22 +00001273 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001276 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277
Bram Moolenaar2e310482018-08-21 13:09:10 +02001278 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279#else
1280 suspend_shell();
1281#endif
1282}
1283
1284 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001285mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 Columns = 80;
1288 Rows = 24;
1289
1290 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001291
1292#ifdef SIGTSTP
1293 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1294 // that indicates the shell (or program) that launched us does not support
1295 // tty job control and thus we should ignore that signal. If invoked as a
1296 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
1297 ignore_sigtstp = restricted || SIG_IGN == signal(SIGTSTP, SIG_ERR);
1298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001300
Bram Moolenaar56718732006-03-15 22:53:57 +00001301#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001302 mac_conv_init();
1303#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001304#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1305 win_clip_init();
1306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307}
1308
1309 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001310set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311{
1312#if defined(SIGWINCH)
1313 /*
1314 * WINDOW CHANGE signal is handled with sig_winch().
1315 */
1316 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1317#endif
1318
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001320 // See mch_init() for the conditions under which we ignore SIGTSTP.
1321 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001323#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 signal(SIGCONT, sigcont_handler);
1325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 /*
1327 * We want to ignore breaking of PIPEs.
1328 */
1329#ifdef SIGPIPE
1330 signal(SIGPIPE, SIG_IGN);
1331#endif
1332
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001334 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335#endif
1336
1337 /*
1338 * Ignore alarm signals (Perl's alarm() generates it).
1339 */
1340#ifdef SIGALRM
1341 signal(SIGALRM, SIG_IGN);
1342#endif
1343
1344 /*
1345 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1346 * work will be lost.
1347 */
1348#ifdef SIGPWR
1349 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1350#endif
1351
1352 /*
1353 * Arrange for other signals to gracefully shutdown Vim.
1354 */
1355 catch_signals(deathtrap, SIG_ERR);
1356
1357#if defined(FEAT_GUI) && defined(SIGHUP)
1358 /*
1359 * When the GUI is running, ignore the hangup signal.
1360 */
1361 if (gui.in_use)
1362 signal(SIGHUP, SIG_IGN);
1363#endif
1364}
1365
Bram Moolenaardf177f62005-02-22 08:39:57 +00001366#if defined(SIGINT) || defined(PROTO)
1367/*
1368 * Catch CTRL-C (only works while in Cooked mode).
1369 */
1370 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001371catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001372{
1373 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1374}
1375#endif
1376
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001378reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379{
1380 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001381#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001382 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 signal(SIGCONT, SIG_DFL);
1384#endif
1385}
1386
1387 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001388catch_signals(
1389 RETSIGTYPE (*func_deadly)(),
1390 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 int i;
1393
1394 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 if (signal_info[i].deadly)
1397 {
1398#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1399 struct sigaction sa;
1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01001401 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 sa.sa_handler = func_deadly;
1403 sigemptyset(&sa.sa_mask);
1404# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001405 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1406 // thread handling in combination with using the alternate stack:
1407 // pthread library functions try to use the stack pointer to
1408 // identify the current thread, causing a SEGV signal, which
1409 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 sa.sa_flags = 0;
1411# else
1412 sa.sa_flags = SA_ONSTACK;
1413# endif
1414 sigaction(signal_info[i].sig, &sa, NULL);
1415#else
1416# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1417 struct sigvec sv;
1418
Bram Moolenaar0f873732019-12-05 20:28:46 +01001419 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 sv.sv_handler = func_deadly;
1421 sv.sv_mask = 0;
1422 sv.sv_flags = SV_ONSTACK;
1423 sigvec(signal_info[i].sig, &sv, NULL);
1424# else
1425 signal(signal_info[i].sig, func_deadly);
1426# endif
1427#endif
1428 }
1429 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001430 {
1431 // Deal with non-deadly signals.
1432#ifdef SIGTSTP
1433 signal(signal_info[i].sig,
1434 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1435 ? SIG_IGN : func_other);
1436#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001438#endif
1439 }
1440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441}
1442
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001443#ifdef HAVE_SIGPROCMASK
1444 static void
1445block_signals(sigset_t *set)
1446{
1447 sigset_t newset;
1448 int i;
1449
1450 sigemptyset(&newset);
1451
1452 for (i = 0; signal_info[i].sig != -1; i++)
1453 sigaddset(&newset, signal_info[i].sig);
1454
Bram Moolenaar2e310482018-08-21 13:09:10 +02001455# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001456 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001457 sigaddset(&newset, SIGCONT);
1458# endif
1459
1460 sigprocmask(SIG_BLOCK, &newset, set);
1461}
1462
1463 static void
1464unblock_signals(sigset_t *set)
1465{
1466 sigprocmask(SIG_SETMASK, set, NULL);
1467}
1468#endif
1469
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001471 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001472 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1473 * return TRUE
1474 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1475 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001476 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001477 * Returns TRUE when Vim should exit.
1478 */
1479 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001480vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001481{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001482 static int got_signal = 0;
1483 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001484
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001485 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001486 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001487 case SIGNAL_BLOCK: blocked = TRUE;
1488 break;
1489
1490 case SIGNAL_UNBLOCK: blocked = FALSE;
1491 if (got_signal != 0)
1492 {
1493 kill(getpid(), got_signal);
1494 got_signal = 0;
1495 }
1496 break;
1497
1498 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001499 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001500 got_signal = sig;
1501#ifdef SIGPWR
1502 if (sig != SIGPWR)
1503#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001504 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001505 break;
1506 }
1507 return FALSE;
1508}
1509
1510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 * Check_win checks whether we have an interactive stdout.
1512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001514mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 if (isatty(1))
1517 return OK;
1518 return FAIL;
1519}
1520
1521/*
1522 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1523 */
1524 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001525mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
1527 if (isatty(read_cmd_fd))
1528 return TRUE;
1529 return FALSE;
1530}
1531
1532#ifdef FEAT_X11
1533
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001534# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1536
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537/*
1538 * Give a message about the elapsed time for opening the X window.
1539 */
1540 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001541xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001543 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544}
1545# endif
1546#endif
1547
1548#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1549/*
1550 * A few functions shared by X11 title and clipboard code.
1551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552
1553static int got_x_error = FALSE;
1554
1555/*
1556 * X Error handler, otherwise X just exits! (very rude) -- webb
1557 */
1558 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001559x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001561 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 STRCAT(IObuff, _("\nVim: Got X error\n"));
1563
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001564 // In the GUI we cannot print a message and continue, because no X calls
1565 // are allowed here (causes my system to hang). Silently continuing seems
1566 // like the best alternative. Do preserve files, in case we crash.
1567 ml_sync_all(FALSE, FALSE);
1568
1569#ifdef FEAT_GUI
1570 if (!gui.in_use)
1571#endif
1572 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaar0f873732019-12-05 20:28:46 +01001574 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575}
1576
1577/*
1578 * Another X Error handler, just used to check for errors.
1579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001581x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582{
1583 got_x_error = TRUE;
1584 return 0;
1585}
1586
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001587/*
1588 * Return TRUE when connection to the X server is desired.
1589 */
1590 static int
1591x_connect_to_server(void)
1592{
1593 // No point in connecting if we are exiting or dying.
1594 if (exiting || v_dying)
1595 return FALSE;
1596
1597#if defined(FEAT_CLIENTSERVER)
1598 if (x_force_connect)
1599 return TRUE;
1600#endif
1601 if (x_no_connect)
1602 return FALSE;
1603
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001604 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001605 if (clip_exclude_prog != NULL)
1606 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001607 // Just in case we get called recursively, return FALSE. This could
1608 // happen if vpeekc() is used while executing the prog and it causes a
1609 // related callback to be invoked.
1610 if (regprog_in_use(clip_exclude_prog))
1611 return FALSE;
1612
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001613 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1614 return FALSE;
1615 }
1616 return TRUE;
1617}
1618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001620# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621/*
1622 * An X IO Error handler, used to catch error while opening the display.
1623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001625x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001627 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001629# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001630 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632}
1633# endif
1634
1635/*
1636 * An X IO Error handler, used to catch terminal errors.
1637 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001638static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001641x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642{
1643 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001644 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 x11_window = 0;
1646 x11_display = NULL;
1647 xterm_Shell = (Widget)0;
1648
Bram Moolenaar0f873732019-12-05 20:28:46 +01001649 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001651# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001652 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001653# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001655
1656/*
1657 * If the X11 connection was lost try to restore it.
1658 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001659 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001660 */
1661 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001662may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001663{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001664 // No point in restoring the connecting if we are exiting or dying.
1665 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001666 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001667 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001668
1669# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001670 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001671 if (app_context != (XtAppContext)NULL)
1672 {
1673 XtDestroyApplicationContext(app_context);
1674 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001675 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001676 }
1677# endif
1678
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001679 setup_term_clip();
1680 get_x11_title(FALSE);
1681 }
1682}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001683
1684 void
1685ex_xrestore(exarg_T *eap)
1686{
1687 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1688 {
1689 if (xterm_display_allocated)
1690 vim_free(xterm_display);
1691 xterm_display = (char *)vim_strsave(eap->arg);
1692 xterm_display_allocated = TRUE;
1693 }
1694 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001695 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001696
1697 clear_xterm_clip();
1698 x11_window = 0;
1699 xterm_dpy_retry_count = 5; // Try reconnecting five times
1700 may_restore_clipboard();
1701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#endif
1703
1704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 * Test if "dpy" and x11_window are valid by getting the window title.
1706 * I don't actually want it yet, so there may be a simpler call to use, but
1707 * this will cause the error handler x_error_check() to be called if anything
1708 * is wrong, such as the window pointer being invalid (as can happen when the
1709 * user changes his DISPLAY, but not his WINDOWID) -- webb
1710 */
1711 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001712test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 int (*old_handler)();
1715 XTextProperty text_prop;
1716
1717 old_handler = XSetErrorHandler(x_error_check);
1718 got_x_error = FALSE;
1719 if (XGetWMName(dpy, x11_window, &text_prop))
1720 XFree((void *)text_prop.value);
1721 XSync(dpy, False);
1722 (void)XSetErrorHandler(old_handler);
1723
1724 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001725 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
1727 return (got_x_error ? FAIL : OK);
1728}
1729#endif
1730
1731#ifdef FEAT_TITLE
1732
1733#ifdef FEAT_X11
1734
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001735static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736
1737/*
1738 * try to get x11 window and display
1739 *
1740 * return FAIL for failure, OK otherwise
1741 */
1742 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001743get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744{
1745 char *winid;
1746 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001747#define XD_NONE 0 // x11_display not set here
1748#define XD_HERE 1 // x11_display opened here
1749#define XD_GUI 2 // x11_display used from gui.dpy
1750#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 static int x11_display_from = XD_NONE;
1752 static int did_set_error_handler = FALSE;
1753
1754 if (!did_set_error_handler)
1755 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001756 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 (void)XSetErrorHandler(x_error_handler);
1758 did_set_error_handler = TRUE;
1759 }
1760
Bram Moolenaar9372a112005-12-06 19:59:18 +00001761#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 if (gui.in_use)
1763 {
1764 /*
1765 * If the X11 display was opened here before, for the window where Vim
1766 * was started, close that one now to avoid a memory leak.
1767 */
1768 if (x11_display_from == XD_HERE && x11_display != NULL)
1769 {
1770 XCloseDisplay(x11_display);
1771 x11_display_from = XD_NONE;
1772 }
1773 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1774 {
1775 x11_display_from = XD_GUI;
1776 return OK;
1777 }
1778 x11_display = NULL;
1779 return FAIL;
1780 }
1781 else if (x11_display_from == XD_GUI)
1782 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001783 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 x11_window = 0;
1785 x11_display = NULL;
1786 x11_display_from = XD_NONE;
1787 }
1788#endif
1789
Bram Moolenaar0f873732019-12-05 20:28:46 +01001790 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 if (!x_connect_to_server())
1792 return FAIL;
1793
1794 /*
1795 * If WINDOWID not set, should try another method to find out
1796 * what the current window number is. The only code I know for
1797 * this is very complicated.
1798 * We assume that zero is invalid for WINDOWID.
1799 */
1800 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1801 x11_window = (Window)atol(winid);
1802
1803#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001804 if (xterm_dpy == x11_display)
1805 // x11_display may have been set to xterm_dpy elsewhere
1806 x11_display_from = XD_XTERM;
1807
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (xterm_dpy != NULL && x11_window != 0)
1809 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001810 // We may have checked it already, but Gnome terminal can move us to
1811 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001812 if (x11_display_from != XD_XTERM)
1813 {
1814 /*
1815 * If the X11 display was opened here before, for the window where
1816 * Vim was started, close that one now to avoid a memory leak.
1817 */
1818 if (x11_display_from == XD_HERE && x11_display != NULL)
1819 XCloseDisplay(x11_display);
1820 x11_display = xterm_dpy;
1821 x11_display_from = XD_XTERM;
1822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 if (test_x11_window(x11_display) == FAIL)
1824 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001825 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 x11_window = 0;
1827 x11_display = NULL;
1828 x11_display_from = XD_NONE;
1829 return FAIL;
1830 }
1831 return OK;
1832 }
1833#endif
1834
1835 if (x11_window == 0 || x11_display == NULL)
1836 result = -1;
1837
Bram Moolenaar0f873732019-12-05 20:28:46 +01001838 if (result != -1) // Have already been here and set this
1839 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
1841 if (x11_window != 0 && x11_display == NULL)
1842 {
1843#ifdef SET_SIG_ALARM
1844 RETSIGTYPE (*sig_save)();
1845#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001846#ifdef ELAPSED_FUNC
1847 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
1849 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001850 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851#endif
1852
1853#ifdef SET_SIG_ALARM
1854 /*
1855 * Opening the Display may hang if the DISPLAY setting is wrong, or
1856 * the network connection is bad. Set an alarm timer to get out.
1857 */
1858 sig_alarm_called = FALSE;
1859 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1860 (RETSIGTYPE (*)())sig_alarm);
1861 alarm(2);
1862#endif
1863 x11_display = XOpenDisplay(NULL);
1864
1865#ifdef SET_SIG_ALARM
1866 alarm(0);
1867 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1868 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001869 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870#endif
1871 if (x11_display != NULL)
1872 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001873# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001875 {
1876 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001877 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001878 verbose_leave();
1879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880# endif
1881 if (test_x11_window(x11_display) == FAIL)
1882 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001883 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 x11_window = 0;
1885 XCloseDisplay(x11_display);
1886 x11_display = NULL;
1887 }
1888 else
1889 x11_display_from = XD_HERE;
1890 }
1891 }
1892 if (x11_window == 0 || x11_display == NULL)
1893 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001894
1895# ifdef FEAT_EVAL
1896 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1897# endif
1898
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 return (result = OK);
1900}
1901
1902/*
1903 * Determine original x11 Window Title
1904 */
1905 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001906get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001908 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909}
1910
1911/*
1912 * Determine original x11 Window icon
1913 */
1914 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001915get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916{
1917 int retval = FALSE;
1918
1919 retval = get_x11_thing(FALSE, test_only);
1920
Bram Moolenaar0f873732019-12-05 20:28:46 +01001921 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (oldicon == NULL && !test_only)
1923 {
1924 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001925 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001927 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 }
1929
1930 return retval;
1931}
1932
1933 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001934get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01001935 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01001936 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937{
1938 XTextProperty text_prop;
1939 int retval = FALSE;
1940 Status status;
1941
1942 if (get_x11_windis() == OK)
1943 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001944 // Get window/icon name if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 if (get_title)
1946 status = XGetWMName(x11_display, x11_window, &text_prop);
1947 else
1948 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1949
1950 /*
1951 * If terminal is xterm, then x11_window may be a child window of the
1952 * outer xterm window that actually contains the window/icon name, so
1953 * keep traversing up the tree until a window with a title/icon is
1954 * found.
1955 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001956 // Previously this was only done for xterm and alike. I don't see a
Bram Moolenaar0f873732019-12-05 20:28:46 +01001957 // reason why it would fail for other terminal emulators.
1958 // if (term_is_xterm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 {
1960 Window root;
1961 Window parent;
1962 Window win = x11_window;
1963 Window *children;
1964 unsigned int num_children;
1965
1966 while (!status || text_prop.value == NULL)
1967 {
1968 if (!XQueryTree(x11_display, win, &root, &parent, &children,
1969 &num_children))
1970 break;
1971 if (children)
1972 XFree((void *)children);
1973 if (parent == root || parent == 0)
1974 break;
1975
1976 win = parent;
1977 if (get_title)
1978 status = XGetWMName(x11_display, win, &text_prop);
1979 else
1980 status = XGetWMIconName(x11_display, win, &text_prop);
1981 }
1982 }
1983 if (status && text_prop.value != NULL)
1984 {
1985 retval = TRUE;
1986 if (!test_only)
1987 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01001988 if (get_title)
1989 vim_free(oldtitle);
1990 else
1991 vim_free(oldicon);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001992 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 if (get_title)
1995 oldtitle = vim_strsave((char_u *)text_prop.value);
1996 else
1997 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 else
2000 {
2001 char **cl;
2002 Status transform_status;
2003 int n = 0;
2004
2005 transform_status = XmbTextPropertyToTextList(x11_display,
2006 &text_prop,
2007 &cl, &n);
2008 if (transform_status >= Success && n > 0 && cl[0])
2009 {
2010 if (get_title)
2011 oldtitle = vim_strsave((char_u *) cl[0]);
2012 else
2013 oldicon = vim_strsave((char_u *) cl[0]);
2014 XFreeStringList(cl);
2015 }
2016 else
2017 {
2018 if (get_title)
2019 oldtitle = vim_strsave((char_u *)text_prop.value);
2020 else
2021 oldicon = vim_strsave((char_u *)text_prop.value);
2022 }
2023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
2025 XFree((void *)text_prop.value);
2026 }
2027 }
2028 return retval;
2029}
2030
Bram Moolenaar0f873732019-12-05 20:28:46 +01002031// Xutf8 functions are not available on older systems. Note that on some
2032// systems X_HAVE_UTF8_STRING may be defined in a header file but
2033// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2034// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002035#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002036# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037# define USE_UTF8_STRING
2038# endif
2039#endif
2040
2041/*
2042 * Set x11 Window Title
2043 *
2044 * get_x11_windis() must be called before this and have returned OK
2045 */
2046 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002047set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002049 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2050 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2051 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052#ifdef USE_UTF8_STRING
2053 if (enc_utf8)
2054 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2055 NULL, NULL, 0, NULL, NULL, NULL);
2056 else
2057#endif
2058 {
2059#if XtSpecificationRelease >= 4
2060# ifdef FEAT_XFONTSET
2061 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2062 NULL, NULL, 0, NULL, NULL, NULL);
2063# else
2064 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002065 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
Bram Moolenaar0f873732019-12-05 20:28:46 +01002067 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002068 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 XSetWMProperties(x11_display, x11_window, &text_prop,
2070 NULL, NULL, 0, NULL, NULL, NULL);
2071# endif
2072#else
2073 XStoreName(x11_display, x11_window, (char *)title);
2074#endif
2075 }
2076 XFlush(x11_display);
2077}
2078
2079/*
2080 * Set x11 Window icon
2081 *
2082 * get_x11_windis() must be called before this and have returned OK
2083 */
2084 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002085set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002087 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088#ifdef USE_UTF8_STRING
2089 if (enc_utf8)
2090 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2091 NULL, 0, NULL, NULL, NULL);
2092 else
2093#endif
2094 {
2095#if XtSpecificationRelease >= 4
2096# ifdef FEAT_XFONTSET
2097 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2098 NULL, 0, NULL, NULL, NULL);
2099# else
2100 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002101 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002103 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2105 NULL, 0, NULL, NULL, NULL);
2106# endif
2107#else
2108 XSetIconName(x11_display, x11_window, (char *)icon);
2109#endif
2110 }
2111 XFlush(x11_display);
2112}
2113
Bram Moolenaar0f873732019-12-05 20:28:46 +01002114#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002117get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118{
2119 return FALSE;
2120}
2121
2122 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002123get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
2125 if (!test_only)
2126 {
2127 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002128 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002130 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
2132 return FALSE;
2133}
2134
Bram Moolenaar0f873732019-12-05 20:28:46 +01002135#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
2137 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002138mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139{
2140 return get_x11_title(TRUE);
2141}
2142
2143 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002144mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145{
2146 return get_x11_icon(TRUE);
2147}
2148
2149/*
2150 * Set the window title and icon.
2151 */
2152 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002153mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154{
2155 int type = 0;
2156 static int recursive = 0;
2157
Bram Moolenaar0f873732019-12-05 20:28:46 +01002158 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002160 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 return;
2162
Bram Moolenaar0f873732019-12-05 20:28:46 +01002163 // When one of the X11 functions causes a deadly signal, we get here again
2164 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 if (recursive)
2166 return;
2167 ++recursive;
2168
2169 /*
2170 * if the window ID and the display is known, we may use X11 calls
2171 */
2172#ifdef FEAT_X11
2173 if (get_x11_windis() == OK)
2174 type = 1;
2175#else
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002176# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) \
2177 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 if (gui.in_use)
2179 type = 1;
2180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181#endif
2182
2183 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002184 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 * than x11 calls, because the x11 calls don't always work
2186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if ((type || *T_TS != NUL) && title != NULL)
2188 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002189 if (oldtitle_outdated)
2190 {
2191 oldtitle_outdated = FALSE;
2192 VIM_CLEAR(oldtitle);
2193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 if (oldtitle == NULL
2195#ifdef FEAT_GUI
2196 && !gui.in_use
2197#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002198 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 (void)get_x11_title(FALSE);
2200
Bram Moolenaar0f873732019-12-05 20:28:46 +01002201 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 term_settitle(title);
2203#ifdef FEAT_X11
2204 else
2205# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002206 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002208 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002210#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2212 else
2213 gui_mch_settitle(title, icon);
2214#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002215 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
2217
2218 if ((type || *T_CIS != NUL) && icon != NULL)
2219 {
2220 if (oldicon == NULL
2221#ifdef FEAT_GUI
2222 && !gui.in_use
2223#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002224 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 get_x11_icon(FALSE);
2226
2227 if (*T_CIS != NUL)
2228 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002229 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002231 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 out_flush();
2233 }
2234#ifdef FEAT_X11
2235 else
2236# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002237 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002239 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240#endif
2241 did_set_icon = TRUE;
2242 }
2243 --recursive;
2244}
2245
2246/*
2247 * Restore the window/icon title.
2248 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002249 * SAVE_RESTORE_TITLE only restore title
2250 * SAVE_RESTORE_ICON only restore icon
2251 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 */
2253 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002254mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255{
Bram Moolenaardac13472019-09-16 21:06:21 +02002256 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002257
Bram Moolenaar0f873732019-12-05 20:28:46 +01002258 // only restore the title or icon when it has been set
Bram Moolenaardac13472019-09-16 21:06:21 +02002259 mch_settitle(((which & SAVE_RESTORE_TITLE) && unix_did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002261 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2262
Bram Moolenaare5c83282019-05-03 23:15:37 +02002263 if (do_push_pop)
2264 {
2265 // pop and push from/to the stack
2266 term_pop_title(which);
2267 term_push_title(which);
2268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269}
2270
Bram Moolenaar0f873732019-12-05 20:28:46 +01002271#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
2273/*
2274 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002275 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 */
2277 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002278vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 if (name == NULL)
2281 return FALSE;
2282 return (STRNICMP(name, "xterm", 5) == 0
2283 || STRNICMP(name, "nxterm", 6) == 0
2284 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002285 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002287 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 || STRCMP(name, "builtin_xterm") == 0);
2289}
2290
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002291#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2292/*
2293 * Return TRUE if "name" appears to be that of a terminal
2294 * known to support the xterm-style mouse protocol.
2295 * Relies on term_is_xterm having been set to its correct value.
2296 */
2297 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002298use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002299{
2300 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002301 && (term_is_xterm
2302 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002303 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002304 || STRICMP(name, "st") == 0
2305 || STRNICMP(name, "st-", 3) == 0
2306 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002307}
2308#endif
2309
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310/*
2311 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2312 * Return 1 for "xterm".
2313 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002314 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002315 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 */
2317 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002318use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002320 if (ttym_flags == TTYM_SGR)
2321 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002322 if (ttym_flags == TTYM_URXVT)
2323 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (ttym_flags == TTYM_XTERM2)
2325 return 2;
2326 if (ttym_flags == TTYM_XTERM)
2327 return 1;
2328 return 0;
2329}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
2331 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002332vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334 if (name == NULL)
2335 return FALSE;
2336 return (STRNICMP(name, "iris-ansi", 9) == 0
2337 || STRCMP(name, "builtin_iris-ansi") == 0);
2338}
2339
2340 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002341vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
2343 if (name == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002344 return FALSE; // actually all ANSI comp. terminals should be here
2345 // catch VT100 - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002346 return ((STRNICMP(name, "vt", 2) == 0
2347 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 || STRCMP(name, "builtin_vt320") == 0);
2349}
2350
2351/*
2352 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2353 * This should include all windowed terminal emulators.
2354 */
2355 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002356vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357{
2358 if (name == NULL)
2359 return FALSE;
2360 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2361 return TRUE;
2362 return ( STRNICMP(name, "hpterm", 6) == 0
2363 || STRNICMP(name, "sun-cmd", 7) == 0
2364 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002365 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 || STRNICMP(name, "dtterm", 6) == 0);
2367}
2368
2369/*
2370 * Insert user name in s[len].
2371 * Return OK if a name found.
2372 */
2373 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002374mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
2376#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002377 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 return OK;
2379#else
2380 return mch_get_uname(getuid(), s, len);
2381#endif
2382}
2383
2384/*
2385 * Insert user name for "uid" in s[len].
2386 * Return OK if a name found.
2387 */
2388 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002389mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
2391#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2392 struct passwd *pw;
2393
2394 if ((pw = getpwuid(uid)) != NULL
2395 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2396 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002397 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 return OK;
2399 }
2400#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002401 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2402 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403}
2404
2405/*
2406 * Insert host name is s[len].
2407 */
2408
2409#ifdef HAVE_SYS_UTSNAME_H
2410 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002411mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412{
2413 struct utsname vutsname;
2414
2415 if (uname(&vutsname) < 0)
2416 *s = NUL;
2417 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002418 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002420#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422# ifdef HAVE_SYS_SYSTEMINFO_H
2423# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2424# endif
2425
2426 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002427mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428{
2429# ifdef VAXC
2430 vaxc$gethostname((char *)s, len);
2431# else
2432 gethostname((char *)s, len);
2433# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002434 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002436#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438/*
2439 * return process ID
2440 */
2441 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002442mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443{
2444 return (long)getpid();
2445}
2446
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002447/*
2448 * return TRUE if process "pid" is still running
2449 */
2450 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002451mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002452{
2453 // EMX kill() not working correctly, it seems
2454 return kill(pid, 0) == 0;
2455}
2456
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002459strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460{
2461 extern int sys_nerr;
2462 extern char *sys_errlist[];
2463 static char er[20];
2464
2465 if (err > 0 && err < sys_nerr)
2466 return (sys_errlist[err]);
2467 sprintf(er, "Error %d", err);
2468 return er;
2469}
2470#endif
2471
2472/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002473 * Get name of current directory into buffer "buf" of length "len" bytes.
2474 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 * Return OK for success, FAIL for failure.
2476 */
2477 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002478mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479{
2480#if defined(USE_GETCWD)
2481 if (getcwd((char *)buf, len) == NULL)
2482 {
2483 STRCPY(buf, strerror(errno));
2484 return FAIL;
2485 }
2486 return OK;
2487#else
2488 return (getwd((char *)buf) != NULL ? OK : FAIL);
2489#endif
2490}
2491
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002493 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 *
2495 * return FAIL for failure, OK for success
2496 */
2497 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002498mch_FullName(
2499 char_u *fname,
2500 char_u *buf,
2501 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002502 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503{
2504 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002505#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002507 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 char_u olddir[MAXPATHL];
2510 char_u *p;
2511 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002512#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002513 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2514 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002515#endif
2516
Bram Moolenaar38323e42007-03-06 19:22:53 +00002517#ifdef VMS
2518 fname = vms_fixfilename(fname);
2519#endif
2520
Bram Moolenaara2442432007-04-26 14:26:37 +00002521#ifdef __CYGWIN__
2522 /*
2523 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2524 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002525# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002526 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2527 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002528 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2529 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002530# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002531 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002532# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002533 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002534#endif
2535
Bram Moolenaar0f873732019-12-05 20:28:46 +01002536 // Expand it if forced or not an absolute path.
2537 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002538 if ((force || !mch_isFullName(fname))
2539 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 {
2541 /*
2542 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002543 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 * This will get the correct path name with "../" things.
2545 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002546 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002548#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 /*
2550 * Use fchdir() if possible, it's said to be faster and more
2551 * reliable. But on SunOS 4 it might not work. Check this by
2552 * doing a fchdir() right now.
2553 */
2554 if (!dont_fchdir)
2555 {
2556 fd = open(".", O_RDONLY | O_EXTRA, 0);
2557 if (fd >= 0 && fchdir(fd) < 0)
2558 {
2559 close(fd);
2560 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002561 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
2563 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
Bram Moolenaar0f873732019-12-05 20:28:46 +01002566 // Only change directory when we are sure we can return to where
2567 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002569#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 (mch_dirname(olddir, MAXPATHL) == FAIL
2573 || mch_chdir((char *)olddir) != 0))
2574 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002575 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 retval = FAIL;
2577 }
2578 else
2579 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002580 // The directory is copied into buf[], to be able to remove
2581 // the file name without changing it (could be a string in
2582 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (p - fname >= len)
2584 retval = FAIL;
2585 else
2586 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002587 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 if (mch_chdir((char *)buf))
2589 retval = FAIL;
2590 else
2591 fname = p + 1;
2592 *buf = NUL;
2593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
2595 }
2596 if (mch_dirname(buf, len) == FAIL)
2597 {
2598 retval = FAIL;
2599 *buf = NUL;
2600 }
2601 if (p != NULL)
2602 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002603#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (fd >= 0)
2605 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002606 if (p_verbose >= 5)
2607 {
2608 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002609 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002610 verbose_leave();
2611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 l = fchdir(fd);
2613 close(fd);
2614 }
2615 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 l = mch_chdir((char *)olddir);
2618 if (l != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002619 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 }
2621
2622 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002623 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002624 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002625#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002626 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002628 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002631
Bram Moolenaar0f873732019-12-05 20:28:46 +01002632 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002633 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 return FAIL;
2635
Bram Moolenaar0f873732019-12-05 20:28:46 +01002636 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 if (STRCMP(fname, ".") != 0)
2638 STRCAT(buf, fname);
2639
2640 return OK;
2641}
2642
2643/*
2644 * Return TRUE if "fname" does not depend on the current directory.
2645 */
2646 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002647mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002649#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 return ( fname[0] == '/' || fname[0] == '.' ||
2651 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2652 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2653 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002654#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656#endif
2657}
2658
Bram Moolenaar24552be2005-12-10 20:17:30 +00002659#if defined(USE_FNAME_CASE) || defined(PROTO)
2660/*
2661 * Set the case of the file name, if it already exists. This will cause the
2662 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002663 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002664 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002665 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002666fname_case(
2667 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002668 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002669{
2670 struct stat st;
2671 char_u *slash, *tail;
2672 DIR *dirp;
2673 struct dirent *dp;
2674
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002675 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002676 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002677 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002678 slash = vim_strrchr(name, '/');
2679 if (slash == NULL)
2680 {
2681 dirp = opendir(".");
2682 tail = name;
2683 }
2684 else
2685 {
2686 *slash = NUL;
2687 dirp = opendir((char *)name);
2688 *slash = '/';
2689 tail = slash + 1;
2690 }
2691
2692 if (dirp != NULL)
2693 {
2694 while ((dp = readdir(dirp)) != NULL)
2695 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002696 // Only accept names that differ in case and are the same byte
2697 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002698 if (STRICMP(tail, dp->d_name) == 0
2699 && STRLEN(tail) == STRLEN(dp->d_name))
2700 {
2701 char_u newname[MAXPATHL + 1];
2702 struct stat st2;
2703
Bram Moolenaar0f873732019-12-05 20:28:46 +01002704 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002705 vim_strncpy(newname, name, MAXPATHL);
2706 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2707 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002708 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002709 && st.st_ino == st2.st_ino
2710 && st.st_dev == st2.st_dev)
2711 {
2712 STRCPY(tail, dp->d_name);
2713 break;
2714 }
2715 }
2716 }
2717
2718 closedir(dirp);
2719 }
2720 }
2721}
2722#endif
2723
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724/*
2725 * Get file permissions for 'name'.
2726 * Returns -1 when it doesn't exist.
2727 */
2728 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002729mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730{
2731 struct stat statb;
2732
Bram Moolenaar0f873732019-12-05 20:28:46 +01002733 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734#ifdef VMS
2735 if (stat((char *)vms_fixfilename(name), &statb))
2736#else
2737 if (stat((char *)name, &statb))
2738#endif
2739 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002740#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002741 // The top bit makes the value negative, which means the file doesn't
2742 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002743 return statb.st_mode & ~S_ADDACE;
2744#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747}
2748
2749/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002750 * Set file permission for "name" to "perm".
2751 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 */
2753 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002754mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 return (chmod((char *)
2757#ifdef VMS
2758 vms_fixfilename(name),
2759#else
2760 name,
2761#endif
2762 (mode_t)perm) == 0 ? OK : FAIL);
2763}
2764
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002765#if defined(HAVE_FCHMOD) || defined(PROTO)
2766/*
2767 * Set file permission for open file "fd" to "perm".
2768 * Return FAIL for failure, OK otherwise.
2769 */
2770 int
2771mch_fsetperm(int fd, long perm)
2772{
2773 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2774}
2775#endif
2776
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777#if defined(HAVE_ACL) || defined(PROTO)
2778# ifdef HAVE_SYS_ACL_H
2779# include <sys/acl.h>
2780# endif
2781# ifdef HAVE_SYS_ACCESS_H
2782# include <sys/access.h>
2783# endif
2784
2785# ifdef HAVE_SOLARIS_ACL
2786typedef struct vim_acl_solaris_T {
2787 int acl_cnt;
2788 aclent_t *acl_entry;
2789} vim_acl_solaris_T;
2790# endif
2791
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002792#if defined(HAVE_SELINUX) || defined(PROTO)
2793/*
2794 * Copy security info from "from_file" to "to_file".
2795 */
2796 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002797mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002798{
2799 if (from_file == NULL)
2800 return;
2801
2802 if (selinux_enabled == -1)
2803 selinux_enabled = is_selinux_enabled();
2804
2805 if (selinux_enabled > 0)
2806 {
2807 security_context_t from_context = NULL;
2808 security_context_t to_context = NULL;
2809
2810 if (getfilecon((char *)from_file, &from_context) < 0)
2811 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002812 // If the filesystem doesn't support extended attributes,
2813 // the original had no special security context and the
2814 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002815 if (errno == EOPNOTSUPP)
2816 return;
2817
Bram Moolenaar32526b32019-01-19 17:43:09 +01002818 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002819 msg_outtrans(from_file);
2820 msg_putchar('\n');
2821 return;
2822 }
2823 if (getfilecon((char *)to_file, &to_context) < 0)
2824 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002825 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002826 msg_outtrans(to_file);
2827 msg_putchar('\n');
2828 freecon (from_context);
2829 return ;
2830 }
2831 if (strcmp(from_context, to_context) != 0)
2832 {
2833 if (setfilecon((char *)to_file, from_context) < 0)
2834 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002835 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002836 msg_outtrans(to_file);
2837 msg_putchar('\n');
2838 }
2839 }
2840 freecon(to_context);
2841 freecon(from_context);
2842 }
2843}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002844#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002845
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002846#if defined(HAVE_SMACK) && !defined(PROTO)
2847/*
2848 * Copy security info from "from_file" to "to_file".
2849 */
2850 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002851mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002852{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002853 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002854 {
2855 XATTR_NAME_SMACK,
2856 XATTR_NAME_SMACKEXEC,
2857 XATTR_NAME_SMACKMMAP
2858 };
2859
2860 char buffer[SMACK_LABEL_LEN];
2861 const char *name;
2862 int index;
2863 int ret;
2864 ssize_t size;
2865
2866 if (from_file == NULL)
2867 return;
2868
2869 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2870 / sizeof(smack_copied_attributes)[0]) ; index++)
2871 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002872 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002873 name = smack_copied_attributes[index];
2874
Bram Moolenaar0f873732019-12-05 20:28:46 +01002875 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002876 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2877 if (size >= 0)
2878 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002879 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002880 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2881 if (ret < 0)
2882 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002883 vim_snprintf((char *)IObuff, IOSIZE,
2884 _("Could not set security context %s for %s"),
2885 name, to_file);
2886 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002887 msg_putchar('\n');
2888 }
2889 }
2890 else
2891 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002892 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002893 switch (errno)
2894 {
2895 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002896 // extended attributes aren't supported or enabled
2897 // should a message be echoed? not sure...
2898 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002899
2900 case ERANGE:
2901 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002902 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002903 vim_snprintf((char *)IObuff, IOSIZE,
2904 _("Could not get security context %s for %s. Removing it!"),
2905 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002906 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002907 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01002908 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002909
2910 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002911 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002912 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002913 // Silently ignore errors, apparently this happens when
2914 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002915 break;
2916 }
2917 }
2918 }
2919}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002920#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002921
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922/*
2923 * Return a pointer to the ACL of file "fname" in allocated memory.
2924 * Return NULL if the ACL is not available for whatever reason.
2925 */
2926 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002927mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
2929 vim_acl_T ret = NULL;
2930#ifdef HAVE_POSIX_ACL
2931 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2932#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002933#ifdef HAVE_SOLARIS_ZFS_ACL
2934 acl_t *aclent;
2935
2936 if (acl_get((char *)fname, 0, &aclent) < 0)
2937 return NULL;
2938 ret = (vim_acl_T)aclent;
2939#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940#ifdef HAVE_SOLARIS_ACL
2941 vim_acl_solaris_T *aclent;
2942
2943 aclent = malloc(sizeof(vim_acl_solaris_T));
2944 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2945 {
2946 free(aclent);
2947 return NULL;
2948 }
2949 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2950 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2951 {
2952 free(aclent->acl_entry);
2953 free(aclent);
2954 return NULL;
2955 }
2956 ret = (vim_acl_T)aclent;
2957#else
2958#if defined(HAVE_AIX_ACL)
2959 int aclsize;
2960 struct acl *aclent;
2961
2962 aclsize = sizeof(struct acl);
2963 aclent = malloc(aclsize);
2964 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2965 {
2966 if (errno == ENOSPC)
2967 {
2968 aclsize = aclent->acl_len;
2969 aclent = realloc(aclent, aclsize);
2970 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2971 {
2972 free(aclent);
2973 return NULL;
2974 }
2975 }
2976 else
2977 {
2978 free(aclent);
2979 return NULL;
2980 }
2981 }
2982 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002983#endif // HAVE_AIX_ACL
2984#endif // HAVE_SOLARIS_ACL
2985#endif // HAVE_SOLARIS_ZFS_ACL
2986#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 return ret;
2988}
2989
2990/*
2991 * Set the ACL of file "fname" to "acl" (unless it's NULL).
2992 */
2993 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002994mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
2996 if (aclent == NULL)
2997 return;
2998#ifdef HAVE_POSIX_ACL
2999 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3000#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003001#ifdef HAVE_SOLARIS_ZFS_ACL
3002 acl_set((char *)fname, (acl_t *)aclent);
3003#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004#ifdef HAVE_SOLARIS_ACL
3005 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3006 ((vim_acl_solaris_T *)aclent)->acl_entry);
3007#else
3008#ifdef HAVE_AIX_ACL
3009 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003010#endif // HAVE_AIX_ACL
3011#endif // HAVE_SOLARIS_ACL
3012#endif // HAVE_SOLARIS_ZFS_ACL
3013#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014}
3015
3016 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003017mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018{
3019 if (aclent == NULL)
3020 return;
3021#ifdef HAVE_POSIX_ACL
3022 acl_free((acl_t)aclent);
3023#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003024#ifdef HAVE_SOLARIS_ZFS_ACL
3025 acl_free((acl_t *)aclent);
3026#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027#ifdef HAVE_SOLARIS_ACL
3028 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3029 free(aclent);
3030#else
3031#ifdef HAVE_AIX_ACL
3032 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003033#endif // HAVE_AIX_ACL
3034#endif // HAVE_SOLARIS_ACL
3035#endif // HAVE_SOLARIS_ZFS_ACL
3036#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037}
3038#endif
3039
3040/*
3041 * Set hidden flag for "name".
3042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003044mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003046 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047}
3048
3049/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003050 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 * return FALSE if "name" is not a directory
3052 * return FALSE for error
3053 */
3054 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003055mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
3057 struct stat statb;
3058
Bram Moolenaar0f873732019-12-05 20:28:46 +01003059 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 return FALSE;
3061 if (stat((char *)name, &statb))
3062 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064}
3065
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003066/*
3067 * return TRUE if "name" is a directory, NOT a symlink to a directory
3068 * return FALSE if "name" is not a directory
3069 * return FALSE for error
3070 */
3071 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003072mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003073{
3074 struct stat statb;
3075
Bram Moolenaar0f873732019-12-05 20:28:46 +01003076 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003077 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003078 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003079 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003080 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003081}
3082
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083/*
3084 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3085 */
3086 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003087executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
3089 struct stat st;
3090
3091 if (stat((char *)name, &st))
3092 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003093#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003094 // Like on Unix system file can have executable rights but not necessarily
3095 // be an executable, but on Unix is not a default for an ordinary file to
3096 // have an executable flag - on VMS it is in most cases.
3097 // Therefore, this check does not have any sense - let keep us to the
3098 // conventions instead:
3099 // *.COM and *.EXE files are the executables - the rest are not. This is
3100 // not ideal but better then it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003101 int vms_executable = 0;
3102 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3103 {
3104 if (strstr(vms_tolower((char*)name),".exe") != NULL
3105 || strstr(vms_tolower((char*)name),".com")!= NULL)
3106 vms_executable = 1;
3107 }
3108 return vms_executable;
3109#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112}
3113
3114/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003115 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003116 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 * Return -1 if unknown.
3118 */
3119 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003120mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
3122 char_u *buf;
3123 char_u *p, *e;
3124 int retval;
3125
Bram Moolenaar0f873732019-12-05 20:28:46 +01003126 // When "use_path" is false and if it's an absolute or relative path don't
3127 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003128 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003129 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003130 // There must be a path separator, files in the current directory
3131 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003132 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003133 {
3134 if (path != NULL)
3135 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003136 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003137 *path = FullName_save(name, TRUE);
3138 else
3139 *path = vim_strsave(name);
3140 }
3141 return TRUE;
3142 }
3143 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
3146 p = (char_u *)getenv("PATH");
3147 if (p == NULL || *p == NUL)
3148 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003149 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (buf == NULL)
3151 return -1;
3152
3153 /*
3154 * Walk through all entries in $PATH to check if "name" exists there and
3155 * is an executable file.
3156 */
3157 for (;;)
3158 {
3159 e = (char_u *)strchr((char *)p, ':');
3160 if (e == NULL)
3161 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003162 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 STRCPY(buf, "./");
3164 else
3165 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003166 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 add_pathsep(buf);
3168 }
3169 STRCAT(buf, name);
3170 retval = executable_file(buf);
3171 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003172 {
3173 if (path != NULL)
3174 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003175 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003176 *path = FullName_save(buf, TRUE);
3177 else
3178 *path = vim_strsave(buf);
3179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
3183 if (*e != ':')
3184 break;
3185 p = e + 1;
3186 }
3187
3188 vim_free(buf);
3189 return retval;
3190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
3192/*
3193 * Check what "name" is:
3194 * NODE_NORMAL: file or directory (or doesn't exist)
3195 * NODE_WRITABLE: writable device, socket, fifo, etc.
3196 * NODE_OTHER: non-writable things
3197 */
3198 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003199mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
3201 struct stat st;
3202
3203 if (stat((char *)name, &st))
3204 return NODE_NORMAL;
3205 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3206 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003207 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003209 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 return NODE_WRITABLE;
3211}
3212
3213 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003214mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
3216#ifdef HAVE_CHECK_STACK_GROWTH
3217 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 check_stack_growth((char *)&i);
3220
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003221# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 get_stack_limit();
3223# endif
3224
3225#endif
3226
3227 /*
3228 * Setup an alternative stack for signals. Helps to catch signals when
3229 * running out of stack space.
3230 * Use of sigaltstack() is preferred, it's more portable.
3231 * Ignore any errors.
3232 */
3233#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003234 signal_stack = alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 init_signal_stack();
3236#endif
3237}
3238
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003239#if defined(EXITFREE) || defined(PROTO)
3240 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003241mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003242{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003243# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3244 if (clip_star.owned)
3245 clip_lose_selection(&clip_star);
3246 if (clip_plus.owned)
3247 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003248# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003249# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003250 if (xterm_Shell != (Widget)0)
3251 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003252# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003253 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003254 if (xterm_dpy != NULL)
3255 XtCloseDisplay(xterm_dpy);
3256 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003257 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003258 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003259# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003260 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003261# endif
3262 }
3263# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003264# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003265# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003266 if (x11_display != NULL
3267# ifdef FEAT_XCLIPBOARD
3268 && x11_display != xterm_dpy
3269# endif
3270 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 XCloseDisplay(x11_display);
3272# endif
3273# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003274 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003275# endif
3276# ifdef FEAT_TITLE
3277 vim_free(oldtitle);
3278 vim_free(oldicon);
3279# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003280}
3281#endif
3282
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283/*
3284 * Output a newline when exiting.
3285 * Make sure the newline goes to the same stream as the text.
3286 */
3287 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003288exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003290 if (silent_mode)
3291 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 if (newline_on_exit || msg_didout)
3293 {
3294 if (msg_use_printf())
3295 {
3296 if (info_message)
3297 mch_msg("\n");
3298 else
3299 mch_errmsg("\r\n");
3300 }
3301 else
3302 out_char('\n');
3303 }
3304 else
3305 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003306 restore_cterm_colors(); // get original colors back
3307 msg_clr_eos_force(); // clear the rest of the display
3308 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 }
3310}
3311
Bram Moolenaarb4151682020-05-11 22:13:28 +02003312#ifdef USE_GCOV_FLUSH
3313extern void __gcov_flush();
3314#endif
3315
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003317mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
3319 exiting = TRUE;
3320
3321#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3322 x11_export_final_selection();
3323#endif
3324
3325#ifdef FEAT_GUI
3326 if (!gui.in_use)
3327#endif
3328 {
3329 settmode(TMODE_COOK);
3330#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003331 // restore xterm title and icon name
3332 mch_restore_title(SAVE_RESTORE_BOTH);
3333 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334#endif
3335 /*
3336 * When t_ti is not empty but it doesn't cause swapping terminal
3337 * pages, need to output a newline when msg_didout is set. But when
3338 * t_ti does swap pages it should not go to the shell page. Do this
3339 * before stoptermcap().
3340 */
3341 if (swapping_screen() && !newline_on_exit)
3342 exit_scroll();
3343
Bram Moolenaar0f873732019-12-05 20:28:46 +01003344 // Stop termcap: May need to check for T_CRV response, which
3345 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 stoptermcap();
3347
3348 /*
3349 * A newline is only required after a message in the alternate screen.
3350 * This is set to TRUE by wait_return().
3351 */
3352 if (!swapping_screen() || newline_on_exit)
3353 exit_scroll();
3354
Bram Moolenaar0f873732019-12-05 20:28:46 +01003355 // Cursor may have been switched off without calling starttermcap()
3356 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 if (full_screen)
3358 cursor_on();
3359 }
3360 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003361 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003362
3363#ifdef USE_GCOV_FLUSH
3364 // Flush coverage info before possibly being killed by a deadly signal.
3365 __gcov_flush();
3366#endif
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 may_core_dump();
3369#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 gui_exit(r);
3372#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003373
Bram Moolenaar56718732006-03-15 22:53:57 +00003374#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003375 mac_conv_cleanup();
3376#endif
3377
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003379 // A core dump won't be created if the signal handler
3380 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 if (deadly_signal != 0)
3382 return;
3383#endif
3384
Bram Moolenaar009b2592004-10-24 19:18:58 +00003385#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003386 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003387#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003388
3389#ifdef EXITFREE
3390 free_all_mem();
3391#endif
3392
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 exit(r);
3394}
3395
3396 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003397may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
3399 if (deadly_signal != 0)
3400 {
3401 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003402 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 }
3404}
3405
3406#ifndef VMS
3407
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003408/*
3409 * Get the file descriptor to use for tty operations.
3410 */
3411 static int
3412get_tty_fd(int fd)
3413{
3414 int tty_fd = fd;
3415
3416#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3417 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3418 // "fd" when it is a master device.
3419 if (mch_isatty(fd) > 1)
3420 {
3421 char *name;
3422
3423 name = ptsname(fd);
3424 if (name == NULL)
3425 return -1;
3426
3427 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3428 if (tty_fd < 0)
3429 return -1;
3430 }
3431#endif
3432 return tty_fd;
3433}
3434
3435 static int
3436mch_tcgetattr(int fd, void *term)
3437{
3438 int tty_fd;
3439 int retval = -1;
3440
3441 tty_fd = get_tty_fd(fd);
3442 if (tty_fd >= 0)
3443 {
3444#ifdef NEW_TTY_SYSTEM
3445# ifdef HAVE_TERMIOS_H
3446 retval = tcgetattr(tty_fd, (struct termios *)term);
3447# else
3448 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3449# endif
3450#else
3451 // for "old" tty systems
3452 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3453#endif
3454 if (tty_fd != fd)
3455 close(tty_fd);
3456 }
3457 return retval;
3458}
3459
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003461mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
3463 static int first = TRUE;
3464
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003465#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466# ifdef HAVE_TERMIOS_H
3467 static struct termios told;
3468 struct termios tnew;
3469# else
3470 static struct termio told;
3471 struct termio tnew;
3472# endif
3473
3474 if (first)
3475 {
3476 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003477 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
3479
3480 tnew = told;
3481 if (tmode == TMODE_RAW)
3482 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003483 // ~ICRNL enables typing ^V^M
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 tnew.c_iflag &= ~ICRNL;
Bram Moolenaar041c7102020-05-30 18:14:57 +02003485 // Do not make CTRL-S stop output, for most users it is unexpected and
3486 // is hardly ever useful.
3487 tnew.c_iflag |= IXON;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3489# if defined(IEXTEN) && !defined(__MINT__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003490 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
3491 // but it breaks function keys on MINT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492# endif
3493 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003494# ifdef ONLCR
3495 // Don't map NL -> CR NL, we do it ourselves.
3496 // Also disable expanding tabs if possible.
3497# ifdef XTABS
3498 tnew.c_oflag &= ~(ONLCR | XTABS);
3499# else
3500# ifdef TAB3
3501 tnew.c_oflag &= ~(ONLCR | TAB3);
3502# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003504# endif
3505# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003507 tnew.c_cc[VMIN] = 1; // return after 1 char
3508 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003511 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003512 // Also reset ICANON here, otherwise on Solaris select() won't see
3513 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003514 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003515 tnew.c_cc[VMIN] = 1; // return after 1 char
3516 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
3519# if defined(HAVE_TERMIOS_H)
3520 {
3521 int n = 10;
3522
Bram Moolenaar0f873732019-12-05 20:28:46 +01003523 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3524 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3526 && errno == EINTR && n > 0)
3527 --n;
3528 }
3529# else
3530 ioctl(read_cmd_fd, TCSETA, &tnew);
3531# endif
3532
3533#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 /*
3535 * for "old" tty systems
3536 */
3537# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003538# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539# endif
3540 static struct sgttyb ttybold;
3541 struct sgttyb ttybnew;
3542
3543 if (first)
3544 {
3545 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003546 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
3548
3549 ttybnew = ttybold;
3550 if (tmode == TMODE_RAW)
3551 {
3552 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3553 ttybnew.sg_flags |= RAW;
3554 }
3555 else if (tmode == TMODE_SLEEP)
3556 ttybnew.sg_flags &= ~(ECHO);
3557 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3558#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003559 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560}
3561
3562/*
3563 * Try to get the code for "t_kb" from the stty setting
3564 *
3565 * Even if termcap claims a backspace key, the user's setting *should*
3566 * prevail. stty knows more about reality than termcap does, and if
3567 * somebody's usual erase key is DEL (which, for most BSD users, it will
3568 * be), they're going to get really annoyed if their erase key starts
3569 * doing forward deletes for no reason. (Eric Fischer)
3570 */
3571 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003572get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003574 ttyinfo_T info;
3575 char_u buf[2];
3576 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003578 if (get_tty_info(read_cmd_fd, &info) == OK)
3579 {
3580 intr_char = info.interrupt;
3581 buf[0] = info.backspace;
3582 buf[1] = NUL;
3583 add_termcode((char_u *)"kb", buf, FALSE);
3584
Bram Moolenaar0f873732019-12-05 20:28:46 +01003585 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003586 p = find_termcode((char_u *)"kD");
3587 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3588 do_fixdel(NULL);
3589 }
3590}
3591
3592/*
3593 * Obtain the characters that Backspace and Enter produce on "fd".
3594 * Returns OK or FAIL.
3595 */
3596 int
3597get_tty_info(int fd, ttyinfo_T *info)
3598{
3599#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600# ifdef HAVE_TERMIOS_H
3601 struct termios keys;
3602# else
3603 struct termio keys;
3604# endif
3605
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003606 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003608 info->backspace = keys.c_cc[VERASE];
3609 info->interrupt = keys.c_cc[VINTR];
3610 if (keys.c_iflag & ICRNL)
3611 info->enter = NL;
3612 else
3613 info->enter = CAR;
3614 if (keys.c_oflag & ONLCR)
3615 info->nl_does_cr = TRUE;
3616 else
3617 info->nl_does_cr = FALSE;
3618 return OK;
3619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003621 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 struct sgttyb keys;
3623
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003624 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003626 info->backspace = keys.sg_erase;
3627 info->interrupt = keys.sg_kill;
3628 info->enter = CAR;
3629 info->nl_does_cr = TRUE;
3630 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003633 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634}
3635
Bram Moolenaar0f873732019-12-05 20:28:46 +01003636#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003638static int mouse_ison = FALSE;
3639
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640/*
3641 * Set mouse clicks on or off.
3642 */
3643 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003644mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003646#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003647 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 int xterm_mouse_vers;
3650
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003651#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003652 if (!on)
3653 // Make sure not tracing mouse movements. Important when a button-down
3654 // was received but no release yet.
3655 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003656#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003657
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003658 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003659#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003660 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003661#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003662 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003663 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 return;
3665
3666 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003667
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003668#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003669 if (ttym_flags == TTYM_URXVT)
3670 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003671 out_str_nf((char_u *)
3672 (on
3673 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3674 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003675 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003676 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003677#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003678
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003679 if (ttym_flags == TTYM_SGR)
3680 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003681 // SGR mode supports columns above 223
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003682 out_str_nf((char_u *)
3683 (on
3684 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3685 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003686 mouse_ison = on;
3687 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003688
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003689#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003690 if (bevalterm_ison != (p_bevalterm && on))
3691 {
3692 bevalterm_ison = (p_bevalterm && on);
3693 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003694 // disable mouse movement events, enabling is below
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003695 out_str_nf((char_u *)
3696 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003697 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003698#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 if (xterm_mouse_vers > 0)
3701 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003702 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 out_str_nf((char_u *)
3704 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003705 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003706#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003707 bevalterm_ison
3708 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003709#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003710 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003712 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 out_str_nf((char_u *)
3714 (xterm_mouse_vers > 1
3715 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3716 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003717 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
3719
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003720#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 else if (ttym_flags == TTYM_DEC)
3722 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003723 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003725 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003727 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003731#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 else
3733 {
3734 if (on)
3735 {
3736 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003737 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
3739 else
3740 {
3741 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003742 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
3744 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003747#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003748 else
3749 {
3750 if (on)
3751 {
3752 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003753 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003754 }
3755 else
3756 {
3757 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003758 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003759 }
3760 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003761#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003762
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003763#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 else
3765 {
3766 if (on)
3767 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003768 // D - Enable Mouse up/down messages
3769 // L - Enable Left Button Reporting
3770 // M - Enable Middle Button Reporting
3771 // R - Enable Right Button Reporting
3772 // K - Enable SHIFT and CTRL key Reporting
3773 // + - Enable Advanced messaging of mouse moves and up/down messages
3774 // Q - Quiet No Ack
3775 // # - Numeric value of mouse pointer required
3776 // 0 = Multiview 2000 cursor, used as standard
3777 // 1 = Windows Arrow
3778 // 2 = Windows I Beam
3779 // 3 = Windows Hour Glass
3780 // 4 = Windows Cross Hair
3781 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003782# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003783 // Disables full feedback of pointer movements
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3785 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003786# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3788 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003789# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003790 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792 else
3793 {
3794 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3795 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003796 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
3798 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003799#endif
3800#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 else
3802 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003803 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (on)
3805 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3806 else
3807 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003808 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811}
3812
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003813#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003814/*
3815 * Called when 'balloonevalterm' changed.
3816 */
3817 void
3818mch_bevalterm_changed(void)
3819{
3820 mch_setmouse(mouse_ison);
3821}
3822#endif
3823
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824/*
3825 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3826 */
3827 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003828check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829{
3830# ifdef FEAT_MOUSE_XTERM
3831 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003832# ifdef FEAT_MOUSE_URXVT
3833 && use_xterm_mouse() != 3
3834# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835# ifdef FEAT_GUI
3836 && !gui.in_use
3837# endif
3838 )
3839 {
3840 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003841 ? IF_EB("\233M", CSI_STR "M")
3842 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if (*p_mouse != NUL)
3844 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003845 // force mouse off and maybe on to send possibly new mouse
3846 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 mch_setmouse(FALSE);
3848 setmouse();
3849 }
3850 }
3851 else
3852 del_mouse_termcode(KS_MOUSE);
3853# endif
3854
3855# ifdef FEAT_MOUSE_GPM
3856 if (!use_xterm_mouse()
3857# ifdef FEAT_GUI
3858 && !gui.in_use
3859# endif
3860 )
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003861 set_mouse_termcode(KS_GPM_MOUSE,
3862 (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3863 else
3864 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865# endif
3866
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003867# ifdef FEAT_SYSMOUSE
3868 if (!use_xterm_mouse()
3869# ifdef FEAT_GUI
3870 && !gui.in_use
3871# endif
3872 )
3873 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3874# endif
3875
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003877 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (!use_xterm_mouse()
3879# ifdef FEAT_GUI
3880 && !gui.in_use
3881# endif
3882 )
3883 set_mouse_termcode(KS_JSBTERM_MOUSE,
3884 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3885 else
3886 del_mouse_termcode(KS_JSBTERM_MOUSE);
3887# endif
3888
3889# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003890 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3891 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 if (!use_xterm_mouse()
3893# ifdef FEAT_GUI
3894 && !gui.in_use
3895# endif
3896 )
3897 set_mouse_termcode(KS_NETTERM_MOUSE,
3898 (char_u *)IF_EB("\033}", ESC_STR "}"));
3899 else
3900 del_mouse_termcode(KS_NETTERM_MOUSE);
3901# endif
3902
3903# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003904 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003905 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906# ifdef FEAT_GUI
3907 && !gui.in_use
3908# endif
3909 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003910 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3911 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 else
3913 del_mouse_termcode(KS_DEC_MOUSE);
3914# endif
3915# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01003916 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003917 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918# ifdef FEAT_GUI
3919 && !gui.in_use
3920# endif
3921 )
3922 set_mouse_termcode(KS_PTERM_MOUSE,
3923 (char_u *) IF_EB("\033[", ESC_STR "["));
3924 else
3925 del_mouse_termcode(KS_PTERM_MOUSE);
3926# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003927# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003928 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003929# ifdef FEAT_GUI
3930 && !gui.in_use
3931# endif
3932 )
3933 {
3934 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003935 ? IF_EB("\233*M", CSI_STR "*M")
3936 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003937
3938 if (*p_mouse != NUL)
3939 {
3940 mch_setmouse(FALSE);
3941 setmouse();
3942 }
3943 }
3944 else
3945 del_mouse_termcode(KS_URXVT_MOUSE);
3946# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003947 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003948# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003949 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003950# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003951 )
3952 {
3953 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003954 ? IF_EB("\233<*M", CSI_STR "<*M")
3955 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3956
3957 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3958 ? IF_EB("\233<*m", CSI_STR "<*m")
3959 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003960
3961 if (*p_mouse != NUL)
3962 {
3963 mch_setmouse(FALSE);
3964 setmouse();
3965 }
3966 }
3967 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003968 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003969 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003970 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974#ifndef VMS
3975
3976/*
3977 * Try to get the current window size:
3978 * 1. with an ioctl(), most accurate method
3979 * 2. from the environment variables LINES and COLUMNS
3980 * 3. from the termcap
3981 * 4. keep using the old values
3982 * Return OK when size could be determined, FAIL otherwise.
3983 */
3984 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003985mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
3987 long rows = 0;
3988 long columns = 0;
3989 char_u *p;
3990
3991 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 * 1. try using an ioctl. It is the most accurate method.
3993 *
3994 * Try using TIOCGWINSZ first, some systems that have it also define
3995 * TIOCGSIZE but don't have a struct ttysize.
3996 */
3997# ifdef TIOCGWINSZ
3998 {
3999 struct winsize ws;
4000 int fd = 1;
4001
Bram Moolenaar0f873732019-12-05 20:28:46 +01004002 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 if (!isatty(fd) && isatty(read_cmd_fd))
4004 fd = read_cmd_fd;
4005 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4006 {
4007 columns = ws.ws_col;
4008 rows = ws.ws_row;
4009 }
4010 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004011# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012# ifdef TIOCGSIZE
4013 {
4014 struct ttysize ts;
4015 int fd = 1;
4016
Bram Moolenaar0f873732019-12-05 20:28:46 +01004017 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 if (!isatty(fd) && isatty(read_cmd_fd))
4019 fd = read_cmd_fd;
4020 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4021 {
4022 columns = ts.ts_cols;
4023 rows = ts.ts_lines;
4024 }
4025 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004026# endif // TIOCGSIZE
4027# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
4029 /*
4030 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004031 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4032 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004034 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
4036 if ((p = (char_u *)getenv("LINES")))
4037 rows = atoi((char *)p);
4038 if ((p = (char_u *)getenv("COLUMNS")))
4039 columns = atoi((char *)p);
4040 }
4041
4042#ifdef HAVE_TGETENT
4043 /*
4044 * 3. try reading "co" and "li" entries from termcap
4045 */
4046 if (columns == 0 || rows == 0)
4047 getlinecol(&columns, &rows);
4048#endif
4049
4050 /*
4051 * 4. If everything fails, use the old values
4052 */
4053 if (columns <= 0 || rows <= 0)
4054 return FAIL;
4055
4056 Rows = rows;
4057 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004058 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 return OK;
4060}
4061
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004062#if defined(FEAT_TERMINAL) || defined(PROTO)
4063/*
4064 * Report the windows size "rows" and "cols" to tty "fd".
4065 */
4066 int
4067mch_report_winsize(int fd, int rows, int cols)
4068{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004069 int tty_fd;
4070 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004071
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004072 tty_fd = get_tty_fd(fd);
4073 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004074 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004075# if defined(TIOCSWINSZ)
4076 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004077
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004078 ws.ws_col = cols;
4079 ws.ws_row = rows;
4080 ws.ws_xpixel = cols * 5;
4081 ws.ws_ypixel = rows * 10;
4082 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4083 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4084 retval == 0 ? "success" : "failed");
4085# elif defined(TIOCSSIZE)
4086 struct ttysize ts;
4087
4088 ts.ts_cols = cols;
4089 ts.ts_lines = rows;
4090 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4091 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4092 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004093# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004094 if (tty_fd != fd)
4095 close(tty_fd);
4096 }
4097 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004098}
4099#endif
4100
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101/*
4102 * Try to set the window size to Rows and Columns.
4103 */
4104 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004105mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
4107 if (*T_CWS)
4108 {
4109 /*
4110 * NOTE: if you get an error here that term_set_winsize() is
4111 * undefined, check the output of configure. It could probably not
4112 * find a ncurses, termcap or termlib library.
4113 */
4114 term_set_winsize((int)Rows, (int)Columns);
4115 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004116 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118}
4119
Bram Moolenaar0f873732019-12-05 20:28:46 +01004120#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
4122/*
4123 * Rows and/or Columns has changed.
4124 */
4125 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004126mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004128 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129}
4130
Bram Moolenaar205b8862011-09-07 15:04:31 +02004131/*
4132 * Wait for process "child" to end.
4133 * Return "child" if it exited properly, <= 0 on error.
4134 */
4135 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004136wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004137{
4138 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004139 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004140
4141 while (wait_pid != child)
4142 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004143 // When compiled with Python threads are probably used, in which case
4144 // wait() sometimes hangs for no obvious reason. Use waitpid()
4145 // instead and loop (like the GUI). Also needed for other interfaces,
4146 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004147# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004148 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004149# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004150 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004151# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004152 if (wait_pid == 0)
4153 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004154 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004155 mch_delay(delay_msec, TRUE);
4156 if (++delay_msec > 10)
4157 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004158 continue;
4159 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004160 if (wait_pid <= 0
4161# ifdef ECHILD
4162 && errno == ECHILD
4163# endif
4164 )
4165 break;
4166 }
4167 return wait_pid;
4168}
4169
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004170#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004171/*
4172 * Set the environment for a child process.
4173 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004174 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004175set_child_environment(
4176 long rows,
4177 long columns,
4178 char *term,
4179 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004180{
4181# ifdef HAVE_SETENV
4182 char envbuf[50];
4183# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004184 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004185 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004186 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004187 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004188 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004189# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004190 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004191# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004192# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004193 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004194# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004195# endif
4196
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004197# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004198 setenv("TERM", term, 1);
4199 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004200 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004201 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004202 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004203 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004204 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004205 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004206 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004207# ifdef FEAT_TERMINAL
4208 if (is_terminal)
4209 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004210 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004211 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4212 }
4213# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004214# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004215 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004216# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004217# else
4218 /*
4219 * Putenv does not copy the string, it has to remain valid.
4220 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004221 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004222 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004223 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4224 putenv(envbuf_Term);
4225 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004226 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004227 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4228 putenv(envbuf_Lines);
4229 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4230 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004231 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004232 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004233 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004234# ifdef FEAT_TERMINAL
4235 if (is_terminal)
4236 {
4237 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004238 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004239 putenv(envbuf_Version);
4240 }
4241# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004242# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004243 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4244 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4245 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004246# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004247# endif
4248}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004249
4250 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004251set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004252{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004253 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004254}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004255#endif
4256
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004257#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004258/*
4259 * Open a PTY, with FD for the master and slave side.
4260 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004261 * When successful both file descriptors are stored and the allocated pty name
4262 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004263 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004264 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004265open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004266{
4267 char *tty_name;
4268
Bram Moolenaar59386482019-02-10 22:43:46 +01004269 if (name1 != NULL)
4270 *name1 = NULL;
4271 if (name2 != NULL)
4272 *name2 = NULL;
4273
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004274 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004275 if (*pty_master_fd >= 0)
4276 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004277 // Leaving out O_NOCTTY may lead to waitpid() always returning
4278 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4279 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004280#ifdef O_NOCTTY
4281 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4282#else
4283 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4284#endif
4285 if (*pty_slave_fd < 0)
4286 {
4287 close(*pty_master_fd);
4288 *pty_master_fd = -1;
4289 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004290 else
4291 {
4292 if (name1 != NULL)
4293 *name1 = vim_strsave((char_u *)tty_name);
4294 if (name2 != NULL)
4295 *name2 = vim_strsave((char_u *)tty_name);
4296 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004297 }
4298}
4299#endif
4300
Bram Moolenaarfae42832017-08-01 22:24:26 +02004301/*
4302 * Send SIGINT to a child process if "c" is an interrupt character.
4303 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004304 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004305may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4306{
4307# ifdef SIGINT
4308 if (c == Ctrl_C || c == intr_char)
4309 {
4310# ifdef HAVE_SETSID
4311 kill(-pid, SIGINT);
4312# else
4313 kill(0, SIGINT);
4314# endif
4315 if (wpid > 0)
4316 kill(wpid, SIGINT);
4317 }
4318# endif
4319}
4320
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004321#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004322
Bram Moolenaar0f873732019-12-05 20:28:46 +01004323/*
4324 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4325 * pointers, the last one is NULL.
4326 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4327 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004328 int
4329unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004330 char_u *cmd,
4331 char ***argvp,
4332 char_u **sh_tofree,
4333 char_u **shcf_tofree)
4334{
4335 char **argv = NULL;
4336 int argc;
4337
4338 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004339 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004340 return FAIL;
4341
4342 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4343 return FAIL;
4344 *argvp = argv;
4345
4346 if (cmd != NULL)
4347 {
4348 char_u *s;
4349 char_u *p;
4350
4351 if (extra_shell_arg != NULL)
4352 argv[argc++] = (char *)extra_shell_arg;
4353
Bram Moolenaar0f873732019-12-05 20:28:46 +01004354 // Break 'shellcmdflag' into white separated parts. This doesn't
4355 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004356 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004357 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004358 return FAIL;
4359 s = *shcf_tofree;
4360 p = p_shcf;
4361 while (*p != NUL)
4362 {
4363 argv[argc++] = (char *)s;
4364 while (*p && *p != ' ' && *p != TAB)
4365 *s++ = *p++;
4366 *s++ = NUL;
4367 p = skipwhite(p);
4368 }
4369
4370 argv[argc++] = (char *)cmd;
4371 }
4372 argv[argc] = NULL;
4373 return OK;
4374}
4375#endif
4376
4377#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4378/*
4379 * Use a terminal window to run a shell command in.
4380 */
4381 static int
4382mch_call_shell_terminal(
4383 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004384 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004385{
4386 jobopt_T opt;
4387 char **argv = NULL;
4388 char_u *tofree1 = NULL;
4389 char_u *tofree2 = NULL;
4390 int retval = -1;
4391 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004392 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004393 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004394 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004395
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004396 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004397 goto theend;
4398
4399 init_job_options(&opt);
4400 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4401 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004402 if (buf == NULL)
4403 goto theend;
4404
4405 job = term_getjob(buf->b_term);
4406 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004407
Bram Moolenaar0f873732019-12-05 20:28:46 +01004408 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004409 aucmd_prepbuf(&aco, buf);
4410
4411 clear_oparg(&oa);
4412 while (term_use_loop())
4413 {
4414 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4415 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004416 // If terminal_loop() returns OK we got a key that is handled
4417 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004418 if (terminal_loop(TRUE) == OK)
4419 normal_cmd(&oa, TRUE);
4420 }
4421 else
4422 normal_cmd(&oa, TRUE);
4423 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004424 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004425 ch_log(NULL, "system command finished");
4426
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004427 job_unref(job);
4428
Bram Moolenaar0f873732019-12-05 20:28:46 +01004429 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004430 aucmd_restbuf(&aco);
4431
4432 wait_return(TRUE);
4433 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4434
4435theend:
4436 vim_free(argv);
4437 vim_free(tofree1);
4438 vim_free(tofree2);
4439 return retval;
4440}
4441#endif
4442
4443#ifdef USE_SYSTEM
4444/*
4445 * Use system() to start the shell: simple but slow.
4446 */
4447 static int
4448mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004449 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004450 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
4452#ifdef VMS
4453 char *ifn = NULL;
4454 char *ofn = NULL;
4455#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004456 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004457 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004458 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459
4460 out_flush();
4461
4462 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004463 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
Bram Moolenaar62b42182010-09-21 22:09:37 +02004465# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004466 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004467 loose_clipboard();
4468# endif
4469
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 if (cmd == NULL)
4471 x = system((char *)p_sh);
4472 else
4473 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004474# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 if (ofn = strchr((char *)cmd, '>'))
4476 *ofn++ = '\0';
4477 if (ifn = strchr((char *)cmd, '<'))
4478 {
4479 char *p;
4480
4481 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004482 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (p)
4484 *p = '\0';
4485 }
4486 if (ofn)
4487 x = vms_sys((char *)cmd, ofn, ifn);
4488 else
4489 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004490# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004491 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004493 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 if (newcmd == NULL)
4495 x = 0;
4496 else
4497 {
4498 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4499 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4500 (char *)p_shcf,
4501 (char *)cmd);
4502 x = system((char *)newcmd);
4503 vim_free(newcmd);
4504 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004505# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507# ifdef VMS
4508 x = vms_sys_status(x);
4509# endif
4510 if (emsg_silent)
4511 ;
4512 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004513 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 else if (x && !(options & SHELL_SILENT))
4515 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004516 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 msg_outnum((long)x);
4518 msg_putchar('\n');
4519 }
4520
4521 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004522 {
4523 // The shell may have messed with the mode, always set it.
4524 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004525 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527# ifdef FEAT_TITLE
4528 resettitle();
4529# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004530# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4531 restore_clipboard();
4532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004534}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
Bram Moolenaar0f873732019-12-05 20:28:46 +01004536#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
Bram Moolenaar0f873732019-12-05 20:28:46 +01004538# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4539 // 127, some shells use that already
4540# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
Bram Moolenaar13568252018-03-16 20:46:58 +01004542/*
4543 * Don't use system(), use fork()/exec().
4544 */
4545 static int
4546mch_call_shell_fork(
4547 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004548 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004549{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004550 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004552 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 pid_t wait_pid = 0;
4554# ifdef HAVE_UNION_WAIT
4555 union wait status;
4556# else
4557 int status = -1;
4558# endif
4559 int retval = -1;
4560 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004561 char_u *tofree1 = NULL;
4562 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004564 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004565# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004567# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004568 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 int fd_fromshell[2];
4570 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004571 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572
4573 out_flush();
4574 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004575 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004576 if (tmode == TMODE_RAW)
4577 // The shell may have messed with the mode, always set it later.
4578 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004580 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004581 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004582
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004584 * For the GUI, when writing the output into the buffer and when reading
4585 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4586 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004588 if ((options & (SHELL_READ|SHELL_WRITE))
4589# ifdef FEAT_GUI
4590 || (gui.in_use && show_shell_mess)
4591# endif
4592 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004594# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 /*
4596 * Try to open a master pty.
4597 * If this works, open the slave pty.
4598 * If the slave can't be opened, close the master pty.
4599 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004600 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004601 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 /*
4603 * If not opening a pty or it didn't work, try using pipes.
4604 */
4605 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
4608 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004609 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {
4611 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004612 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
4614 close(fd_toshell[0]);
4615 close(fd_toshell[1]);
4616 }
4617 }
4618 if (pipe_error)
4619 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004620 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 out_flush();
4622 }
4623 }
4624 }
4625
Bram Moolenaar0f873732019-12-05 20:28:46 +01004626 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004628 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004629 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004630 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004631 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004633 UNBLOCK_SIGNALS(&curset);
4634
Bram Moolenaar32526b32019-01-19 17:43:09 +01004635 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004636 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004638 || (gui.in_use && show_shell_mess)
4639# endif
4640 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004642# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004643 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 {
4645 close(pty_master_fd);
4646 close(pty_slave_fd);
4647 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004648 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
4651 close(fd_toshell[0]);
4652 close(fd_toshell[1]);
4653 close(fd_fromshell[0]);
4654 close(fd_fromshell[1]);
4655 }
4656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004658 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004660 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004661 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662
Bram Moolenaar819524702018-02-27 19:10:00 +01004663# ifdef FEAT_JOB_CHANNEL
4664 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01004665 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01004666 ch_logfile((char_u *)"", (char_u *)"");
4667# endif
4668
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (!show_shell_mess || (options & SHELL_EXPAND))
4670 {
4671 int fd;
4672
4673 /*
4674 * Don't want to show any message from the shell. Can't just
4675 * close stdout and stderr though, because some systems will
4676 * break if you try to write to them after that, so we must
4677 * use dup() to replace them with something else -- webb
4678 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4679 * waiting for input.
4680 */
4681 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4682 fclose(stdin);
4683 fclose(stdout);
4684 fclose(stderr);
4685
4686 /*
4687 * If any of these open()'s and dup()'s fail, we just continue
4688 * anyway. It's not fatal, and on most systems it will make
4689 * no difference at all. On a few it will cause the execvp()
4690 * to exit with a non-zero status even when the completion
4691 * could be done, which is nothing too serious. If the open()
4692 * or dup() failed we'd just do the same thing ourselves
4693 * anyway -- webb
4694 */
4695 if (fd >= 0)
4696 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004697 vim_ignored = dup(fd); // To replace stdin (fd 0)
4698 vim_ignored = dup(fd); // To replace stdout (fd 1)
4699 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
Bram Moolenaar0f873732019-12-05 20:28:46 +01004701 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 close(fd);
4703 }
4704 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004705 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004707 || gui.in_use
4708# endif
4709 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
4711
Bram Moolenaardf177f62005-02-22 08:39:57 +00004712# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004713 // Create our own process group, so that the child and all its
4714 // children can be kill()ed. Don't do this when using pipes,
4715 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004716 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004717 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004718 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004719# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004720 // When doing "!xterm&" and 'shell' is bash: the shell
4721 // will exit and send SIGHUP to all processes in its
4722 // group, killing the just started process. Ignore SIGHUP
4723 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004724 signal(SIGHUP, SIG_IGN);
4725# endif
4726 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004727# endif
4728# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004729 if (pty_slave_fd >= 0)
4730 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004731 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004732 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004733 setup_slavepty(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01004735 // Try to become controlling tty (probably doesn't work,
4736 // unless run by root)
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004737 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004739 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004740# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004741 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
Bram Moolenaara5792f52005-11-23 21:25:05 +00004743 /*
4744 * stderr is only redirected when using the GUI, so that a
4745 * program like gpg can still access the terminal to get a
4746 * passphrase using stderr.
4747 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004748# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 if (pty_master_fd >= 0)
4750 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004751 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
Bram Moolenaar0f873732019-12-05 20:28:46 +01004753 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004755 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004757 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004758 if (gui.in_use)
4759 {
4760 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004761 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
Bram Moolenaar0f873732019-12-05 20:28:46 +01004764 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
4766 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004767# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004769 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 close(fd_toshell[1]);
4771 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004772 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 close(fd_toshell[0]);
4774
Bram Moolenaar0f873732019-12-05 20:28:46 +01004775 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 close(fd_fromshell[0]);
4777 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004778 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 close(fd_fromshell[1]);
4780
Bram Moolenaara5792f52005-11-23 21:25:05 +00004781# ifdef FEAT_GUI
4782 if (gui.in_use)
4783 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004784 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004785 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004786 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004787 }
4788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
4790 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004791
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 /*
4793 * There is no type cast for the argv, because the type may be
4794 * different on different machines. This may cause a warning
4795 * message with strict compilers, don't worry about it.
4796 * Call _exit() instead of exit() to avoid closing the connection
4797 * to the X server (esp. with GTK, which uses atexit()).
4798 */
4799 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004800 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004802 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
4804 /*
4805 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004806 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 */
4808 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004809 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004810 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004811# ifdef FEAT_JOB_CHANNEL
4812 ++dont_check_job_ended;
4813# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 /*
4815 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004816 * This is also used to pipe stdin/stdout to/from the external
4817 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004819 if ((options & (SHELL_READ|SHELL_WRITE))
4820# ifdef FEAT_GUI
4821 || (gui.in_use && show_shell_mess)
4822# endif
4823 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004825# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004827 int buffer_off = 0; // valid bytes in buffer[]
4828 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4829 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 int len;
4831 int p_more_save;
4832 int old_State;
4833 int c;
4834 int toshell_fd;
4835 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004836 garray_T ga;
4837 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004838# ifdef ELAPSED_FUNC
4839 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841
Bram Moolenaardf177f62005-02-22 08:39:57 +00004842# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 if (pty_master_fd >= 0)
4844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 fromshell_fd = pty_master_fd;
4846 toshell_fd = dup(pty_master_fd);
4847 }
4848 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
4851 close(fd_toshell[0]);
4852 close(fd_fromshell[1]);
4853 toshell_fd = fd_toshell[1];
4854 fromshell_fd = fd_fromshell[0];
4855 }
4856
4857 /*
4858 * Write to the child if there are typed characters.
4859 * Read from the child if there are characters available.
4860 * Repeat the reading a few times if more characters are
4861 * available. Need to check for typed keys now and then, but
4862 * not too often (delays when no chars are available).
4863 * This loop is quit if no characters can be read from the pty
4864 * (WaitForChar detected special condition), or there are no
4865 * characters available and the child has exited.
4866 * Only check if the child has exited when there is no more
4867 * output. The child may exit before all the output has
4868 * been printed.
4869 *
4870 * Currently this busy loops!
4871 * This can probably dead-lock when the write blocks!
4872 */
4873 p_more_save = p_more;
4874 p_more = FALSE;
4875 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004876 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004878 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004879 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004880 // Fork a process that will write the lines to the
4881 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004882 if ((wpid = fork()) == -1)
4883 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004884 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004885 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004886 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004887 {
4888 linenr_T lnum = curbuf->b_op_start.lnum;
4889 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004890 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004891 size_t l;
4892
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004893 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004894 for (;;)
4895 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004896 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004897 if (l == 0)
4898 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004899 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004900 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004901 len = write(toshell_fd, "", (size_t)1);
4902 else
4903 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004904 char_u *s = vim_strchr(lp + written, NL);
4905
Bram Moolenaar89d40322006-08-29 15:30:07 +00004906 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004907 s == NULL ? l
4908 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004909 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004910 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004911 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004912 // Finished a line, add a NL, unless this line
4913 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004914 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004915 || (!curbuf->b_p_bin
4916 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004917 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004918 && (lnum !=
4919 curbuf->b_ml.ml_line_count
4920 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004921 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004922 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004923 ++lnum;
4924 if (lnum > curbuf->b_op_end.lnum)
4925 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004926 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00004927 close(toshell_fd);
4928 toshell_fd = -1;
4929 break;
4930 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004931 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004932 written = 0;
4933 }
4934 else if (len > 0)
4935 written += len;
4936 }
4937 _exit(0);
4938 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004939 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00004940 {
4941 close(toshell_fd);
4942 toshell_fd = -1;
4943 }
4944 }
4945
4946 if (options & SHELL_READ)
4947 ga_init2(&ga, 1, BUFLEN);
4948
4949 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004950# ifdef ELAPSED_FUNC
4951 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 for (;;)
4954 {
4955 /*
4956 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004957 * if there are any.
4958 * Don't do this if we are expanding wild cards (would eat
4959 * typeahead).
4960 * Don't do this when filtering and terminal is in cooked
4961 * mode, the shell command will handle the I/O. Avoids
4962 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004963 * Don't get characters when the child has already
4964 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004965 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004966 * while (noread_cnt > 4), avoids that ":r !ls" eats
4967 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 */
4969 len = 0;
4970 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004971 && ((options &
4972 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4973 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004974# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004975 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004976# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004977 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004978 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004979 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004981 if (ta_len == 0)
4982 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004983 // Get extra characters when we don't have any.
4984 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004985 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004986# ifdef ELAPSED_FUNC
4987 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004988# endif
4989 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4990 }
4991 if (ta_len > 0 || len > 0)
4992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 /*
4994 * For pipes:
4995 * Check for CTRL-C: send interrupt signal to child.
4996 * Check for CTRL-D: EOF, close pipe to child.
4997 */
4998 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4999 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 /*
5001 * Send SIGINT to the child's group or all
5002 * processes in our group.
5003 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005004 may_send_sigint(ta_buf[ta_len], pid, wpid);
5005
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 if (pty_master_fd < 0 && toshell_fd >= 0
5007 && ta_buf[ta_len] == Ctrl_D)
5008 {
5009 close(toshell_fd);
5010 toshell_fd = -1;
5011 }
5012 }
5013
Bram Moolenaarf4140482020-02-15 23:06:45 +01005014 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
5016 /*
5017 * For pipes: echo the typed characters.
5018 * For a pty this does not seem to work.
5019 */
5020 if (pty_master_fd < 0)
5021 {
5022 for (i = ta_len; i < ta_len + len; ++i)
5023 {
5024 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5025 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 else if (has_mbyte)
5027 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005028 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029
5030 msg_outtrans_len(ta_buf + i, l);
5031 i += l - 1;
5032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 else
5034 msg_outtrans_len(ta_buf + i, 1);
5035 }
5036 windgoto(msg_row, msg_col);
5037 out_flush();
5038 }
5039
5040 ta_len += len;
5041
5042 /*
5043 * Write the characters to the child, unless EOF has
5044 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005045 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005046 * When writing buffer lines, drop the typed
5047 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005049 if (options & SHELL_WRITE)
5050 ta_len = 0;
5051 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
5053 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5054 if (len > 0)
5055 {
5056 ta_len -= len;
5057 mch_memmove(ta_buf, ta_buf + len, ta_len);
5058 }
5059 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
5062
Bram Moolenaardf177f62005-02-22 08:39:57 +00005063 if (got_int)
5064 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005065 // CTRL-C sends a signal to the child, we ignore it
5066 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005067# ifdef HAVE_SETSID
5068 kill(-pid, SIGINT);
5069# else
5070 kill(0, SIGINT);
5071# endif
5072 if (wpid > 0)
5073 kill(wpid, SIGINT);
5074 got_int = FALSE;
5075 }
5076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 /*
5078 * Check if the child has any characters to be printed.
5079 * Read them and write them to our window. Repeat this as
5080 * long as there is something to do, avoid the 10ms wait
5081 * for mch_inchar(), or sending typeahead characters to
5082 * the external process.
5083 * TODO: This should handle escape sequences, compatible
5084 * to some terminal (vt52?).
5085 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005086 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005087 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005089 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005092 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005094
5095 noread_cnt = 0;
5096 if (options & SHELL_READ)
5097 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005098 // Do NUL -> NL translation, append NL separated
5099 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005100 for (i = 0; i < len; ++i)
5101 {
5102 if (buffer[i] == NL)
5103 append_ga_line(&ga);
5104 else if (buffer[i] == NUL)
5105 ga_append(&ga, NL);
5106 else
5107 ga_append(&ga, buffer[i]);
5108 }
5109 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005110 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
5112 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005113 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
Bram Moolenaardf177f62005-02-22 08:39:57 +00005115 len += buffer_off;
5116 buffer[len] = NUL;
5117
Bram Moolenaar0f873732019-12-05 20:28:46 +01005118 // Check if the last character in buffer[] is
5119 // incomplete, keep these bytes for the next
5120 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 for (p = buffer; p < buffer + len; p += l)
5122 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005123 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005125 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 else if (MB_BYTE2LEN(*p) != l)
5127 break;
5128 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005129 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005131 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 if (len >= 12)
5133 ++p;
5134 else
5135 {
5136 buffer_off = len;
5137 continue;
5138 }
5139 }
5140 c = *p;
5141 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005142 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 if (p < buffer + len)
5144 {
5145 *p = c;
5146 buffer_off = (buffer + len) - p;
5147 mch_memmove(buffer, p, buffer_off);
5148 continue;
5149 }
5150 buffer_off = 0;
5151 }
5152 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
5154 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005155 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
5157
5158 windgoto(msg_row, msg_col);
5159 cursor_on();
5160 out_flush();
5161 if (got_int)
5162 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005163
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005164# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005165 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005166 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005167 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005168
Bram Moolenaar0f873732019-12-05 20:28:46 +01005169 // Avoid that we keep looping here without
5170 // checking for a CTRL-C for a long time. Don't
5171 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005172 if (msec > 2000)
5173 {
5174 noread_cnt = 5;
5175 break;
5176 }
5177 }
5178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180
Bram Moolenaar0f873732019-12-05 20:28:46 +01005181 // If we already detected the child has finished, continue
5182 // reading output for a short while. Some text may be
5183 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005184 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005185 {
5186 if (noread_cnt < 5)
5187 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005188 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005189 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005190
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 /*
5192 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005193 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005195# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005196 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005197# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5201 || (wait_pid == pid && WIFEXITED(status)))
5202 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005203 // Don't break the loop yet, try reading more
5204 // characters from "fromshell_fd" first. When using
5205 // pipes there might still be something to read and
5206 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005209 else
5210 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005211
Bram Moolenaar95a51352013-03-21 22:53:50 +01005212# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005213 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005214 clip_update();
5215# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217finished:
5218 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005219 if (options & SHELL_READ)
5220 {
5221 if (ga.ga_len > 0)
5222 {
5223 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005224 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005225 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005226 }
5227 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005228 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005229 ga_clear(&ga);
5230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 /*
5233 * Give all typeahead that wasn't used back to ui_inchar().
5234 */
5235 if (ta_len)
5236 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 State = old_State;
5238 if (toshell_fd >= 0)
5239 close(toshell_fd);
5240 close(fromshell_fd);
5241 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005242# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005243 else
5244 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005245 long delay_msec = 1;
5246
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005247 /*
5248 * Similar to the loop above, but only handle X events, no
5249 * I/O.
5250 */
5251 for (;;)
5252 {
5253 if (got_int)
5254 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005255 // CTRL-C sends a signal to the child, we ignore it
5256 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005257# ifdef HAVE_SETSID
5258 kill(-pid, SIGINT);
5259# else
5260 kill(0, SIGINT);
5261# endif
5262 got_int = FALSE;
5263 }
5264# ifdef __NeXT__
5265 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5266# else
5267 wait_pid = waitpid(pid, &status, WNOHANG);
5268# endif
5269 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5270 || (wait_pid == pid && WIFEXITED(status)))
5271 {
5272 wait_pid = pid;
5273 break;
5274 }
5275
Bram Moolenaar0f873732019-12-05 20:28:46 +01005276 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005277 clip_update();
5278
Bram Moolenaar0f873732019-12-05 20:28:46 +01005279 // Wait for 1 to 10 msec. 1 is faster but gives the child
5280 // less time.
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005281 mch_delay(delay_msec, TRUE);
5282 if (++delay_msec > 10)
5283 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005284 }
5285 }
5286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
5288 /*
5289 * Wait until our child has exited.
5290 * Ignore wait() returning pids of other children and returning
5291 * because of some signal like SIGWINCH.
5292 * Don't wait if wait_pid was already set above, indicating the
5293 * child already exited.
5294 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005295 if (wait_pid != pid)
5296 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297
Bram Moolenaar624891f2010-10-13 16:22:09 +02005298# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005299 // Close slave side of pty. Only do this after the child has
5300 // exited, otherwise the child may hang when it tries to write on
5301 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005302 if (pty_master_fd >= 0)
5303 close(pty_slave_fd);
5304# endif
5305
Bram Moolenaar0f873732019-12-05 20:28:46 +01005306 // Make sure the child that writes to the external program is
5307 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005308 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005309 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005310 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005311 wait4pid(wpid, NULL);
5312 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005313
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005314# ifdef FEAT_JOB_CHANNEL
5315 --dont_check_job_ended;
5316# endif
5317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 /*
5319 * Set to raw mode right now, otherwise a CTRL-C after
5320 * catch_signals() will kill Vim.
5321 */
5322 if (tmode == TMODE_RAW)
5323 settmode(TMODE_RAW);
5324 did_settmode = TRUE;
5325 set_signals();
5326
5327 if (WIFEXITED(status))
5328 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005329 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005331 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
5333 if (retval == EXEC_FAILED)
5334 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005335 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 msg_outtrans(p_sh);
5337 msg_putchar('\n');
5338 }
5339 else if (!(options & SHELL_SILENT))
5340 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005341 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 msg_outnum((long)retval);
5343 msg_putchar('\n');
5344 }
5345 }
5346 }
5347 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005348 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
5350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
5352error:
5353 if (!did_settmode)
5354 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005355 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356# ifdef FEAT_TITLE
5357 resettitle();
5358# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005359 vim_free(argv);
5360 vim_free(tofree1);
5361 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
5363 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005364}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005365#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005366
5367 int
5368mch_call_shell(
5369 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005370 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005371{
5372#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5373 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5374 return mch_call_shell_terminal(cmd, options);
5375#endif
5376#ifdef USE_SYSTEM
5377 return mch_call_shell_system(cmd, options);
5378#else
5379 return mch_call_shell_fork(cmd, options);
5380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381}
5382
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005383#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005384 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005385mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005386{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005387 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005388 int fd_in[2] = {-1, -1}; // for stdin
5389 int fd_out[2] = {-1, -1}; // for stdout
5390 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005391 int pty_master_fd = -1;
5392 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005393 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005394 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5395 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5396 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005397 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005398 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5399 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005400 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005401 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005402 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005403
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005404 if (use_out_for_err && use_null_for_out)
5405 use_null_for_err = TRUE;
5406
Bram Moolenaar0f873732019-12-05 20:28:46 +01005407 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005408 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005409
Bram Moolenaarb2412082017-08-20 18:09:14 +02005410 if (options->jo_pty
5411 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005412 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005413 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005414 open_pty(&pty_master_fd, &pty_slave_fd,
5415 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005416
Bram Moolenaar0f873732019-12-05 20:28:46 +01005417 // TODO: without the channel feature connect the child to /dev/null?
5418 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005419 if (use_file_for_in)
5420 {
5421 char_u *fname = options->jo_io_name[PART_IN];
5422
5423 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5424 if (fd_in[0] < 0)
5425 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005426 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005427 goto failed;
5428 }
5429 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005430 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005431 // When writing buffer lines to the input don't use the pty, so that
5432 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005433 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5434 && pipe(fd_in) < 0)
5435 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005436
5437 if (use_file_for_out)
5438 {
5439 char_u *fname = options->jo_io_name[PART_OUT];
5440
5441 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5442 if (fd_out[1] < 0)
5443 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005444 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005445 goto failed;
5446 }
5447 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005448 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005449 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005450
5451 if (use_file_for_err)
5452 {
5453 char_u *fname = options->jo_io_name[PART_ERR];
5454
5455 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5456 if (fd_err[1] < 0)
5457 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005458 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005459 goto failed;
5460 }
5461 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005462 else if (!use_out_for_err && !use_null_for_err
5463 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005464 goto failed;
5465
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005466 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5467 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005468 if (options->jo_set & JO_CHANNEL)
5469 {
5470 channel = options->jo_channel;
5471 if (channel != NULL)
5472 ++channel->ch_refcount;
5473 }
5474 else
5475 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005476 if (channel == NULL)
5477 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005478 if (job->jv_tty_out != NULL)
5479 ch_log(channel, "using pty %s on fd %d",
5480 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005481 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005482
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005483 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005484 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005485 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005486 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005487 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005488 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005489 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005490 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005491 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005492 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005493 int null_fd = -1;
5494 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005495
Bram Moolenaar0f873732019-12-05 20:28:46 +01005496 // child
5497 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005498 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005499
Bram Moolenaar819524702018-02-27 19:10:00 +01005500# ifdef FEAT_JOB_CHANNEL
5501 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005502 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005503 ch_logfile((char_u *)"", (char_u *)"");
5504# endif
5505
Bram Moolenaar835dc632016-02-07 14:27:38 +01005506# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005507 // Create our own process group, so that the child and all its
5508 // children can be kill()ed. Don't do this when using pipes,
5509 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005510 (void)setsid();
5511# endif
5512
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005513# ifdef FEAT_TERMINAL
5514 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005515 {
5516 char *term = (char *)T_NAME;
5517
5518#ifdef FEAT_GUI
5519 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005520 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005521 term = getenv("TERM");
5522#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005523 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005524 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005525 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005526 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005527 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005528 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005529 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005530 else if (t_colors > 16)
5531 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005532 else
5533 term = "xterm";
5534 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005535 set_child_environment(
5536 (long)options->jo_term_rows,
5537 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005538 term,
5539 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005540 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005541 else
5542# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005543 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005544
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005545 if (options->jo_env != NULL)
5546 {
5547 dict_T *dict = options->jo_env;
5548 hashitem_T *hi;
5549 int todo = (int)dict->dv_hashtab.ht_used;
5550
5551 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5552 if (!HASHITEM_EMPTY(hi))
5553 {
5554 typval_T *item = &dict_lookup(hi)->di_tv;
5555
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005556 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005557 --todo;
5558 }
5559 }
5560
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005561 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005562 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005563 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005564 if (null_fd < 0)
5565 {
5566 perror("opening /dev/null failed");
5567 _exit(OPEN_NULL_FAILED);
5568 }
5569 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005570
Bram Moolenaar223896d2017-08-02 22:33:28 +02005571 if (pty_slave_fd >= 0)
5572 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005573 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005574 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005575# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005576 // Try to become controlling tty (probably doesn't work,
5577 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005578 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5579# endif
5580 }
5581
Bram Moolenaar0f873732019-12-05 20:28:46 +01005582 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005583 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005584 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005585 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005586 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005587 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005588 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005589 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005590
Bram Moolenaar0f873732019-12-05 20:28:46 +01005591 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005592 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005593 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005594 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005595 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005596 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005597 }
5598 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005599 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005600 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005601 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005602 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005603 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005604
Bram Moolenaar0f873732019-12-05 20:28:46 +01005605 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005606 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005607 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005608 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005609 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005610 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005611 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005612 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005613
5614 if (fd_in[0] >= 0)
5615 close(fd_in[0]);
5616 if (fd_in[1] >= 0)
5617 close(fd_in[1]);
5618 if (fd_out[0] >= 0)
5619 close(fd_out[0]);
5620 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005621 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005622 if (fd_err[0] >= 0)
5623 close(fd_err[0]);
5624 if (fd_err[1] >= 0)
5625 close(fd_err[1]);
5626 if (pty_master_fd >= 0)
5627 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005628 close(pty_master_fd); // not used in the child
5629 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005630 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005631
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005632 if (null_fd >= 0)
5633 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005634
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005635 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5636 _exit(EXEC_FAILED);
5637
Bram Moolenaar0f873732019-12-05 20:28:46 +01005638 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005639 execvp(argv[0], argv);
5640
Bram Moolenaar4694a172016-04-21 14:05:23 +02005641 if (stderr_works)
5642 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005643# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005644 // calling free_all_mem() here causes problems. Ignore valgrind
5645 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005646# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005647 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005648 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005649
Bram Moolenaar0f873732019-12-05 20:28:46 +01005650 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005651 UNBLOCK_SIGNALS(&curset);
5652
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005653 job->jv_pid = pid;
5654 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005655 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005656
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005657 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005658 close(pty_slave_fd); // not used in the parent
5659 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005660 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005661 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005662 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005663 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005664 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005665 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005666 if (channel != NULL)
5667 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005668 int in_fd = INVALID_FD;
5669 int out_fd = INVALID_FD;
5670 int err_fd = INVALID_FD;
5671
5672 if (!(use_file_for_in || use_null_for_in))
5673 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5674
5675 if (!(use_file_for_out || use_null_for_out))
5676 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5677
5678 // When using pty_master_fd only set it for stdout, do not duplicate
5679 // it for stderr, it only needs to be read once.
5680 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5681 {
5682 if (fd_err[0] >= 0)
5683 err_fd = fd_err[0];
5684 else if (out_fd != pty_master_fd)
5685 err_fd = pty_master_fd;
5686 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005687
5688 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005689 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005690 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005691 else
5692 {
5693 if (fd_in[1] >= 0)
5694 close(fd_in[1]);
5695 if (fd_out[0] >= 0)
5696 close(fd_out[0]);
5697 if (fd_err[0] >= 0)
5698 close(fd_err[0]);
5699 if (pty_master_fd >= 0)
5700 close(pty_master_fd);
5701 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005702
Bram Moolenaar0f873732019-12-05 20:28:46 +01005703 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005704 return;
5705
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005706failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005707 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005708 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005709 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005710 if (fd_in[1] >= 0)
5711 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005712 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005713 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005714 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005715 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005716 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005717 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005718 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005719 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005720 if (pty_master_fd >= 0)
5721 close(pty_master_fd);
5722 if (pty_slave_fd >= 0)
5723 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005724}
5725
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005726 static char_u *
5727get_signal_name(int sig)
5728{
5729 int i;
5730 char_u numbuf[NUMBUFLEN];
5731
5732 if (sig == SIGKILL)
5733 return vim_strsave((char_u *)"kill");
5734
5735 for (i = 0; signal_info[i].sig != -1; i++)
5736 if (sig == signal_info[i].sig)
5737 return strlow_save((char_u *)signal_info[i].name);
5738
5739 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5740 return vim_strsave(numbuf);
5741}
5742
Bram Moolenaar835dc632016-02-07 14:27:38 +01005743 char *
5744mch_job_status(job_T *job)
5745{
5746# ifdef HAVE_UNION_WAIT
5747 union wait status;
5748# else
5749 int status = -1;
5750# endif
5751 pid_t wait_pid = 0;
5752
5753# ifdef __NeXT__
5754 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5755# else
5756 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5757# endif
5758 if (wait_pid == -1)
5759 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005760 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005761 if (job->jv_status < JOB_ENDED)
5762 ch_log(job->jv_channel, "Job no longer exists: %s",
5763 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005764 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005765 }
5766 if (wait_pid == 0)
5767 return "run";
5768 if (WIFEXITED(status))
5769 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005770 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005771 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005772 if (job->jv_status < JOB_ENDED)
5773 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005774 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005775 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005776 if (WIFSIGNALED(status))
5777 {
5778 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005779 job->jv_termsig = get_signal_name(WTERMSIG(status));
5780 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5781 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5782 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005783 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005784 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005785 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005786
5787return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005788 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005789 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005790 return "dead";
5791}
5792
5793 job_T *
5794mch_detect_ended_job(job_T *job_list)
5795{
5796# ifdef HAVE_UNION_WAIT
5797 union wait status;
5798# else
5799 int status = -1;
5800# endif
5801 pid_t wait_pid = 0;
5802 job_T *job;
5803
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005804# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005805 // Do not do this when waiting for a shell command to finish, we would get
5806 // the exit value here (and discard it), the exit value obtained there
5807 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005808 if (dont_check_job_ended > 0)
5809 return NULL;
5810# endif
5811
Bram Moolenaar97792de2016-10-15 18:36:49 +02005812# ifdef __NeXT__
5813 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5814# else
5815 wait_pid = waitpid(-1, &status, WNOHANG);
5816# endif
5817 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005818 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005819 return NULL;
5820 for (job = job_list; job != NULL; job = job->jv_next)
5821 {
5822 if (job->jv_pid == wait_pid)
5823 {
5824 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005825 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005826 job->jv_exitval = WEXITSTATUS(status);
5827 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005828 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005829 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005830 job->jv_termsig = get_signal_name(WTERMSIG(status));
5831 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005832 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005833 {
5834 ch_log(job->jv_channel, "Job ended");
5835 job->jv_status = JOB_ENDED;
5836 }
5837 return job;
5838 }
5839 }
5840 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005841}
5842
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005843/*
5844 * Send a (deadly) signal to "job".
5845 * Return FAIL if "how" is not a valid name.
5846 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005847 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005848mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005849{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005850 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005851
Bram Moolenaar923d9262016-02-25 20:56:01 +01005852 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005853 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005854 else if (STRCMP(how, "hup") == 0)
5855 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005856 else if (STRCMP(how, "quit") == 0)
5857 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005858 else if (STRCMP(how, "int") == 0)
5859 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005860 else if (STRCMP(how, "kill") == 0)
5861 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005862#ifdef SIGWINCH
5863 else if (STRCMP(how, "winch") == 0)
5864 sig = SIGWINCH;
5865#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005866 else if (isdigit(*how))
5867 sig = atoi((char *)how);
5868 else
5869 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005870
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005871 // Never kill ourselves!
5872 if (job->jv_pid != 0)
5873 {
5874 // TODO: have an option to only kill the process, not the group?
5875 kill(-job->jv_pid, sig);
5876 kill(job->jv_pid, sig);
5877 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005878
Bram Moolenaar835dc632016-02-07 14:27:38 +01005879 return OK;
5880}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005881
5882/*
5883 * Clear the data related to "job".
5884 */
5885 void
5886mch_clear_job(job_T *job)
5887{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005888 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005889# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005890 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005891# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005892 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005893# endif
5894}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005895#endif
5896
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005897#if defined(FEAT_TERMINAL) || defined(PROTO)
5898 int
5899mch_create_pty_channel(job_T *job, jobopt_T *options)
5900{
5901 int pty_master_fd = -1;
5902 int pty_slave_fd = -1;
5903 channel_T *channel;
5904
Bram Moolenaar59386482019-02-10 22:43:46 +01005905 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005906 close(pty_slave_fd);
5907
5908 channel = add_channel();
5909 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005910 {
5911 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005912 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005913 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005914 if (job->jv_tty_out != NULL)
5915 ch_log(channel, "using pty %s on fd %d",
5916 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005917 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005918 channel->ch_keep_open = TRUE;
5919
Bram Moolenaar0f873732019-12-05 20:28:46 +01005920 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5921 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005922 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005923 channel_set_job(channel, job, options);
5924 return OK;
5925}
5926#endif
5927
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928/*
5929 * Check for CTRL-C typed by reading all available characters.
5930 * In cooked mode we should get SIGINT, no need to check.
5931 */
5932 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005933mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
Bram Moolenaar26e86442020-05-17 14:06:16 +02005935 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005936 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 fill_input_buf(FALSE);
5938}
5939
5940/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005941 * Wait "msec" msec until a character is available from the mouse, keyboard,
5942 * from inbuf[].
5943 * "msec" == -1 will block forever.
5944 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005945 * When "ignore_input" is TRUE even check for pending input when input is
5946 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005947 * "interrupted" (if not NULL) is set to TRUE when no character is available
5948 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005949 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005950 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 */
5952 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005953WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005955#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005956 return ui_wait_for_chars_or_timer(
5957 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005958#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005959 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005960#endif
5961}
5962
5963/*
5964 * Wait "msec" msec until a character is available from the mouse or keyboard
5965 * or from inbuf[].
5966 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005967 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005968 * "interrupted" (if not NULL) is set to TRUE when no character is available
5969 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005970 * When a GUI is being used, this will never get called -- webb
5971 */
5972 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005973WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005974{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975#ifdef FEAT_MOUSE_GPM
5976 int gpm_process_wanted;
5977#endif
5978#ifdef FEAT_XCLIPBOARD
5979 int rest;
5980#endif
5981 int avail;
5982
Bram Moolenaar0f873732019-12-05 20:28:46 +01005983 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 return 1;
5985
5986#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005987 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 if (WantQueryMouse)
5989 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005990 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02005991 if (!no_query_mouse_for_testing)
5992 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 }
5994#endif
5995
5996 /*
5997 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5998 * events. This is a bit complicated, because they might both be defined.
5999 */
6000#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6001# ifdef FEAT_XCLIPBOARD
6002 rest = 0;
6003 if (do_xterm_trace())
6004 rest = msec;
6005# endif
6006 do
6007 {
6008# ifdef FEAT_XCLIPBOARD
6009 if (rest != 0)
6010 {
6011 msec = XT_TRACE_DELAY;
6012 if (rest >= 0 && rest < XT_TRACE_DELAY)
6013 msec = rest;
6014 if (rest >= 0)
6015 rest -= msec;
6016 }
6017# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006018# ifdef FEAT_SOUND_CANBERRA
6019 // Invoke any pending sound callbacks.
6020 if (has_sound_callback_in_queue())
6021 invoke_sound_callback();
6022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023# ifdef FEAT_MOUSE_GPM
6024 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006025 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006026 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006027 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006029 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006033 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 return 1;
6035# ifdef FEAT_XCLIPBOARD
6036 if (rest == 0 || !do_xterm_trace())
6037# endif
6038 break;
6039 }
6040 }
6041 while (FALSE
6042# ifdef FEAT_MOUSE_GPM
6043 || (gpm_process_wanted && mch_gpm_process() == 0)
6044# endif
6045# ifdef FEAT_XCLIPBOARD
6046 || (!avail && rest != 0)
6047# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006048 )
6049 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050
6051#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006052 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053#endif
6054 return avail;
6055}
6056
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006057#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058/*
6059 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006060 * "msec" == 0 will check for characters once.
6061 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006064 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006065 * "interrupted" (if not NULL) is set to TRUE when no character is available
6066 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006068 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006069RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
6071 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006072 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006073#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 static int busy = FALSE;
6075
Bram Moolenaar0f873732019-12-05 20:28:46 +01006076 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077# define MAY_LOOP
6078
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006079# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006080 // Remember at what time we started, so that we know how much longer we
6081 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006082 long start_msec = msec;
6083 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006085 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006086 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087# endif
6088
Bram Moolenaar0f873732019-12-05 20:28:46 +01006089 // Handle being called recursively. This may happen for the session
6090 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 if (busy)
6092 return 0;
6093#endif
6094
6095#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006096 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097#endif
6098 {
6099#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006100 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006101# ifdef FEAT_MZSCHEME
6102 int mzquantum_used = FALSE;
6103# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104#endif
6105#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006106 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006107 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 int nfd;
6109# ifdef FEAT_XCLIPBOARD
6110 int xterm_idx = -1;
6111# endif
6112# ifdef FEAT_MOUSE_GPM
6113 int gpm_idx = -1;
6114# endif
6115# ifdef USE_XSMP
6116 int xsmp_idx = -1;
6117# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006118 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006120# ifdef FEAT_MZSCHEME
6121 mzvim_check_threads();
6122 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6123 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006124 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006125 mzquantum_used = TRUE;
6126 }
6127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 fds[0].fd = fd;
6129 fds[0].events = POLLIN;
6130 nfd = 1;
6131
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006133 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 if (xterm_Shell != (Widget)0)
6135 {
6136 xterm_idx = nfd;
6137 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6138 fds[nfd].events = POLLIN;
6139 nfd++;
6140 }
6141# endif
6142# ifdef FEAT_MOUSE_GPM
6143 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6144 {
6145 gpm_idx = nfd;
6146 fds[nfd].fd = gpm_fd;
6147 fds[nfd].events = POLLIN;
6148 nfd++;
6149 }
6150# endif
6151# ifdef USE_XSMP
6152 if (xsmp_icefd != -1)
6153 {
6154 xsmp_idx = nfd;
6155 fds[nfd].fd = xsmp_icefd;
6156 fds[nfd].events = POLLIN;
6157 nfd++;
6158 }
6159# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006160#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006161 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006162#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006163 if (interrupted != NULL)
6164 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006166 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006167
6168 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006169 if (result == 0 && interrupted != NULL && ret > 0)
6170 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006171
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006172# ifdef FEAT_MZSCHEME
6173 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006174 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006175 finished = FALSE;
6176# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178# ifdef FEAT_XCLIPBOARD
6179 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6180 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006181 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006183 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 finished = FALSE;
6185 }
6186# endif
6187# ifdef FEAT_MOUSE_GPM
6188 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190# endif
6191# ifdef USE_XSMP
6192 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6193 {
6194 if (fds[xsmp_idx].revents & POLLIN)
6195 {
6196 busy = TRUE;
6197 xsmp_handle_requests();
6198 busy = FALSE;
6199 }
6200 else if (fds[xsmp_idx].revents & POLLHUP)
6201 {
6202 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006203 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 xsmp_close();
6205 }
6206 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006207 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 }
6209# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006210#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006211 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006212 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006213 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
Bram Moolenaar0f873732019-12-05 20:28:46 +01006216#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
6218 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006219 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006220 // These are static because they can take 8 Kbyte each and cause the
6221 // signal stack to run out with -O3.
6222 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006224 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006226# ifdef FEAT_MZSCHEME
6227 mzvim_check_threads();
6228 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6229 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006230 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006231 mzquantum_used = TRUE;
6232 }
6233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006235 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006237 tv.tv_sec = towait / 1000;
6238 tv.tv_usec = (towait % 1000) * (1000000/1000);
6239 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006241 else
6242 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243
6244 /*
6245 * Select on ready for reading and exceptional condition (end of file).
6246 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006247select_eintr:
6248 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006249 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 FD_ZERO(&efds);
6251 FD_SET(fd, &rfds);
6252# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006253 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 FD_SET(fd, &efds);
6255# endif
6256 maxfd = fd;
6257
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006259 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 if (xterm_Shell != (Widget)0)
6261 {
6262 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6263 if (maxfd < ConnectionNumber(xterm_dpy))
6264 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006265
Bram Moolenaar0f873732019-12-05 20:28:46 +01006266 // An event may have already been read but not handled. In
6267 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006268 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 }
6270# endif
6271# ifdef FEAT_MOUSE_GPM
6272 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6273 {
6274 FD_SET(gpm_fd, &rfds);
6275 FD_SET(gpm_fd, &efds);
6276 if (maxfd < gpm_fd)
6277 maxfd = gpm_fd;
6278 }
6279# endif
6280# ifdef USE_XSMP
6281 if (xsmp_icefd != -1)
6282 {
6283 FD_SET(xsmp_icefd, &rfds);
6284 FD_SET(xsmp_icefd, &efds);
6285 if (maxfd < xsmp_icefd)
6286 maxfd = xsmp_icefd;
6287 }
6288# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006289# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006290 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006291# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006292 if (interrupted != NULL)
6293 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
Bram Moolenaar643b6142018-09-12 20:29:09 +02006295 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6296 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006297 result = ret > 0 && FD_ISSET(fd, &rfds);
6298 if (result)
6299 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006300 else if (interrupted != NULL && ret > 0)
6301 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006302
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006303# ifdef EINTR
6304 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006305 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006306 // Check whether window has been resized, EINTR may be caused by
6307 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006308 if (do_resize)
6309 handle_resize();
6310
Bram Moolenaar0f873732019-12-05 20:28:46 +01006311 // Interrupted by a signal, need to try again. We ignore msec
6312 // here, because we do want to check even after a timeout if
6313 // characters are available. Needed for reading output of an
6314 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006315 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006316 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006317# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006318# ifdef __TANDEM
6319 if (ret == -1 && errno == ENOTSUP)
6320 {
6321 FD_ZERO(&rfds);
6322 FD_ZERO(&efds);
6323 ret = 0;
6324 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006325# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006326# ifdef FEAT_MZSCHEME
6327 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006328 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006329 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330# endif
6331
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332# ifdef FEAT_XCLIPBOARD
6333 if (ret > 0 && xterm_Shell != (Widget)0
6334 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6335 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006336 xterm_update(); // Maybe we should hand out clipboard
6337 // continue looping when we only got the X event and the input
6338 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 if (--ret == 0 && !input_available())
6340 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006341 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 finished = FALSE;
6343 }
6344 }
6345# endif
6346# ifdef FEAT_MOUSE_GPM
6347 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6348 {
6349 if (FD_ISSET(gpm_fd, &efds))
6350 gpm_close();
6351 else if (FD_ISSET(gpm_fd, &rfds))
6352 *check_for_gpm = 1;
6353 }
6354# endif
6355# ifdef USE_XSMP
6356 if (ret > 0 && xsmp_icefd != -1)
6357 {
6358 if (FD_ISSET(xsmp_icefd, &efds))
6359 {
6360 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006361 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 xsmp_close();
6363 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006364 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 }
6366 else if (FD_ISSET(xsmp_icefd, &rfds))
6367 {
6368 busy = TRUE;
6369 xsmp_handle_requests();
6370 busy = FALSE;
6371 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006372 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 }
6374 }
6375# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006376#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006377 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006378 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006379 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381
Bram Moolenaar0f873732019-12-05 20:28:46 +01006382#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383
6384#ifdef MAY_LOOP
6385 if (finished || msec == 0)
6386 break;
6387
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006388# ifdef FEAT_CLIENTSERVER
6389 if (server_waiting())
6390 break;
6391# endif
6392
Bram Moolenaar0f873732019-12-05 20:28:46 +01006393 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 if (msec > 0)
6395 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006396# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006397 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006398 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006400 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 msec = msec / 2;
6402# endif
6403 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006404 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 }
6406#endif
6407 }
6408
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006409 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410}
6411
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006413 * Expand a path into all matching files and/or directories. Handles "*",
6414 * "?", "[a-z]", "**", etc.
6415 * "path" has backslashes before chars that are not to be expanded.
6416 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 */
6418 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006419mch_expandpath(
6420 garray_T *gap,
6421 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006422 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423{
Bram Moolenaar02743632005-07-25 20:42:36 +00006424 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426
6427/*
6428 * mch_expand_wildcards() - this code does wild-card pattern matching using
6429 * the shell
6430 *
6431 * return OK for success, FAIL for error (you may lose some memory) and put
6432 * an error message in *file.
6433 *
6434 * num_pat is number of input patterns
6435 * pat is array of pointers to input patterns
6436 * num_file is pointer to number of matched file names
6437 * file is pointer to array of pointers to matched file names
6438 */
6439
6440#ifndef SEEK_SET
6441# define SEEK_SET 0
6442#endif
6443#ifndef SEEK_END
6444# define SEEK_END 2
6445#endif
6446
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006447#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006448
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006450mch_expand_wildcards(
6451 int num_pat,
6452 char_u **pat,
6453 int *num_file,
6454 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006455 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456{
6457 int i;
6458 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006459 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 char_u *p;
6461 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462
Bram Moolenaarc7247912008-01-13 12:54:11 +00006463 /*
6464 * This is the non-OS/2 implementation (really Unix).
6465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 int j;
6467 char_u *tempname;
6468 char_u *command;
6469 FILE *fd;
6470 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006471#define STYLE_ECHO 0 // use "echo", the default
6472#define STYLE_GLOB 1 // use "glob", for csh
6473#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6474#define STYLE_PRINT 3 // use "print -N", for zsh
6475#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6476 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 int shell_style = STYLE_ECHO;
6478 int check_spaces;
6479 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006480 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006481 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006482 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483
Bram Moolenaar0f873732019-12-05 20:28:46 +01006484 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 *file = NULL;
6486
6487 /*
6488 * If there are no wildcards, just copy the names to allocated memory.
6489 * Saves a lot of time, because we don't have to start a new shell.
6490 */
6491 if (!have_wildcard(num_pat, pat))
6492 return save_patterns(num_pat, pat, num_file, file);
6493
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006494# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006495 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006496 if (sandbox != 0 && check_secure())
6497 return FAIL;
6498# endif
6499
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 /*
6501 * Don't allow the use of backticks in secure and restricted mode.
6502 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006503 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 for (i = 0; i < num_pat; ++i)
6505 if (vim_strchr(pat[i], '`') != NULL
6506 && (check_restricted() || check_secure()))
6507 return FAIL;
6508
6509 /*
6510 * get a name for the temp file
6511 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006512 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006514 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 return FAIL;
6516 }
6517
6518 /*
6519 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006520 * file.
6521 * STYLE_BT: NL separated
6522 * If expanding `cmd` execute it directly.
6523 * STYLE_GLOB: NUL separated
6524 * If we use *csh, "glob" will work better than "echo".
6525 * STYLE_PRINT: NL or NUL separated
6526 * If we use *zsh, "print -N" will work better than "glob".
6527 * STYLE_VIMGLOB: NL separated
6528 * If we use *sh*, we define "vimglob()".
6529 * STYLE_ECHO: space separated.
6530 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 */
6532 if (num_pat == 1 && *pat[0] == '`'
6533 && (len = STRLEN(pat[0])) > 2
6534 && *(pat[0] + len - 1) == '`')
6535 shell_style = STYLE_BT;
6536 else if ((len = STRLEN(p_sh)) >= 3)
6537 {
6538 if (STRCMP(p_sh + len - 3, "csh") == 0)
6539 shell_style = STYLE_GLOB;
6540 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6541 shell_style = STYLE_PRINT;
6542 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006543 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6544 "sh") != NULL)
6545 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546
Bram Moolenaar0f873732019-12-05 20:28:46 +01006547 // Compute the length of the command. We need 2 extra bytes: for the
6548 // optional '&' and for the NUL.
6549 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006551 if (shell_style == STYLE_VIMGLOB)
6552 len += STRLEN(sh_vimglob_func);
6553
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006554 for (i = 0; i < num_pat; ++i)
6555 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006556 // Count the length of the patterns in the same way as they are put in
6557 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006558#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006559 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006560#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006561 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006562 for (j = 0; pat[i][j] != NUL; ++j)
6563 {
6564 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006565 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006566 ++len;
6567 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006568#endif
6569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 command = alloc(len);
6571 if (command == NULL)
6572 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006573 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 vim_free(tempname);
6575 return FAIL;
6576 }
6577
6578 /*
6579 * Build the shell command:
6580 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6581 * recognizes this).
6582 * - Add the shell command to print the expanded names.
6583 * - Add the temp file name.
6584 * - Add the file name patterns.
6585 */
6586 if (shell_style == STYLE_BT)
6587 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006588 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006589 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006590 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006592 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006593 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006595 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006597 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 *p = ' ';
6599 }
6600 STRCAT(command, ">");
6601 }
6602 else
6603 {
6604 if (flags & EW_NOTFOUND)
6605 STRCPY(command, "set nonomatch; ");
6606 else
6607 STRCPY(command, "unset nonomatch; ");
6608 if (shell_style == STYLE_GLOB)
6609 STRCAT(command, "glob >");
6610 else if (shell_style == STYLE_PRINT)
6611 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006612 else if (shell_style == STYLE_VIMGLOB)
6613 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 else
6615 STRCAT(command, "echo >");
6616 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006617
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 if (shell_style != STYLE_BT)
6621 for (i = 0; i < num_pat; ++i)
6622 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006623 // When using system() always add extra quotes, because the shell
6624 // is started twice. Otherwise put a backslash before special
6625 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626#ifdef USE_SYSTEM
6627 STRCAT(command, " \"");
6628 STRCAT(command, pat[i]);
6629 STRCAT(command, "\"");
6630#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006631 int intick = FALSE;
6632
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 p = command + STRLEN(command);
6634 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006635 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006636 {
6637 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006638 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006639 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6640 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006641 // Remove a backslash, take char literally. But keep
6642 // backslash inside backticks, before a special character
6643 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006644 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006645 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6646 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006647 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006648 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006649 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006650 else if (!intick
6651 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6652 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006653 // Put a backslash before a special character, but not
6654 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6655 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006656 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006657
Bram Moolenaar0f873732019-12-05 20:28:46 +01006658 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006659 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 *p = NUL;
6662#endif
6663 }
6664 if (flags & EW_SILENT)
6665 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006666 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006667 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668
6669 /*
6670 * Using zsh -G: If a pattern has no matches, it is just deleted from
6671 * the argument list, otherwise zsh gives an error message and doesn't
6672 * expand any other pattern.
6673 */
6674 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006675 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676
6677 /*
6678 * If we use -f then shell variables set in .cshrc won't get expanded.
6679 * vi can do it, so we will too, but it is only necessary if there is a "$"
6680 * in one of the patterns, otherwise we can still use the fast option.
6681 */
6682 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006683 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684
6685 /*
6686 * execute the shell command
6687 */
6688 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6689
Bram Moolenaar0f873732019-12-05 20:28:46 +01006690 // When running in the background, give it some time to create the temp
6691 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006692 if (ampersand)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 mch_delay(10L, TRUE);
6694
Bram Moolenaar0f873732019-12-05 20:28:46 +01006695 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 show_shell_mess = TRUE;
6697 vim_free(command);
6698
Bram Moolenaar0f873732019-12-05 20:28:46 +01006699 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 {
6701 mch_remove(tempname);
6702 vim_free(tempname);
6703 /*
6704 * With interactive completion, the error message is not printed.
6705 * However with USE_SYSTEM, I don't know how to turn off error messages
6706 * from the shell, so screen may still get messed up -- webb.
6707 */
6708#ifndef USE_SYSTEM
6709 if (!(flags & EW_SILENT))
6710#endif
6711 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006712 redraw_later_clear(); // probably messed up screen
6713 msg_putchar('\n'); // clear bottom line quickly
6714 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715#ifdef USE_SYSTEM
6716 if (!(flags & EW_SILENT))
6717#endif
6718 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006719 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006720 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 }
6722 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006723 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6724 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 if (shell_style == STYLE_BT)
6726 return FAIL;
6727 goto notfound;
6728 }
6729
6730 /*
6731 * read the names from the file into memory
6732 */
6733 fd = fopen((char *)tempname, READBIN);
6734 if (fd == NULL)
6735 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006736 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 if (!(flags & EW_SILENT))
6738 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006739 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006740 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 }
6742 vim_free(tempname);
6743 goto notfound;
6744 }
6745 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006746 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006748 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006749 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006750 buffer = NULL;
6751 else
6752 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 if (buffer == NULL)
6754 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006755 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 mch_remove(tempname);
6757 vim_free(tempname);
6758 fclose(fd);
6759 return FAIL;
6760 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006761 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 i = fread((char *)buffer, 1, len, fd);
6763 fclose(fd);
6764 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006765 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006767 // unexpected read error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006768 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 vim_free(tempname);
6770 vim_free(buffer);
6771 return FAIL;
6772 }
6773 vim_free(tempname);
6774
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006775# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006776 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006778 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6780 *p++ = buffer[i];
6781 len = p - buffer;
6782# endif
6783
6784
Bram Moolenaar0f873732019-12-05 20:28:46 +01006785 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 if (shell_style == STYLE_ECHO)
6787 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006788 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006790 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
6792 while (*p != ' ' && *p != '\n')
6793 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006794 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 }
6796 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006797 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006798 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006800 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006802 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {
6804 while (*p != '\n' && *p != NUL)
6805 ++p;
6806 if (*p != NUL)
6807 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006808 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
6810 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006811 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 else
6813 {
6814 /*
6815 * Some versions of zsh use spaces instead of NULs to separate
6816 * results. Only do this when there is no NUL before the end of the
6817 * buffer, otherwise we would never be able to use file names with
6818 * embedded spaces when zsh does use NULs.
6819 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6820 * don't check for spaces again.
6821 */
6822 check_spaces = FALSE;
6823 if (shell_style == STYLE_PRINT && !did_find_nul)
6824 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006825 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006826 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006827 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 did_find_nul = TRUE;
6829 else
6830 check_spaces = TRUE;
6831 }
6832
6833 /*
6834 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6835 * already is one, for STYLE_GLOB it needs to be added.
6836 */
6837 if (len && buffer[len - 1] == NUL)
6838 --len;
6839 else
6840 buffer[len] = NUL;
6841 i = 0;
6842 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006843 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 {
6845 ++i;
6846 *p = NUL;
6847 }
6848 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006849 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 }
6851 if (i == 0)
6852 {
6853 /*
6854 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6855 * /bin/sh will happily expand it to nothing rather than returning an
6856 * error; and hey, it's good to check anyway -- webb.
6857 */
6858 vim_free(buffer);
6859 goto notfound;
6860 }
6861 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006862 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 if (*file == NULL)
6864 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006865 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 vim_free(buffer);
6867 return FAIL;
6868 }
6869
6870 /*
6871 * Isolate the individual file names.
6872 */
6873 p = buffer;
6874 for (i = 0; i < *num_file; ++i)
6875 {
6876 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006877 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006878 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6879 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006881 while (!(shell_style == STYLE_ECHO && *p == ' ')
6882 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006884 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 *p = NUL;
6886 else
6887 {
6888 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006889 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 }
6891 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006892 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006894 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006896 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 }
6898 }
6899
6900 /*
6901 * Move the file names to allocated memory.
6902 */
6903 for (j = 0, i = 0; i < *num_file; ++i)
6904 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006905 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6907 continue;
6908
Bram Moolenaar0f873732019-12-05 20:28:46 +01006909 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 dir = (mch_isdir((*file)[i]));
6911 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6912 continue;
6913
Bram Moolenaar0f873732019-12-05 20:28:46 +01006914 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01006915 if (!dir && (flags & EW_EXEC)
6916 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006917 continue;
6918
Bram Moolenaar964b3742019-05-24 18:54:09 +02006919 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 if (p)
6921 {
6922 STRCPY(p, (*file)[i]);
6923 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006924 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 (*file)[j++] = p;
6926 }
6927 }
6928 vim_free(buffer);
6929 *num_file = j;
6930
Bram Moolenaar0f873732019-12-05 20:28:46 +01006931 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006933 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 goto notfound;
6935 }
6936
6937 return OK;
6938
6939notfound:
6940 if (flags & EW_NOTFOUND)
6941 return save_patterns(num_pat, pat, num_file, file);
6942 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943}
6944
Bram Moolenaar0f873732019-12-05 20:28:46 +01006945#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006948save_patterns(
6949 int num_pat,
6950 char_u **pat,
6951 int *num_file,
6952 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953{
6954 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006955 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006957 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 if (*file == NULL)
6959 return FAIL;
6960 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006961 {
6962 s = vim_strsave(pat[i]);
6963 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006964 // Be compatible with expand_filename(): halve the number of
6965 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00006966 backslash_halve(s);
6967 (*file)[i] = s;
6968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 *num_file = num_pat;
6970 return OK;
6971}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973/*
6974 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6975 * expand.
6976 */
6977 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006978mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006980 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 if (*p == '\\' && p[1] != NUL)
6983 ++p;
6984 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 if (vim_strchr((char_u *)
6986#ifdef VMS
6987 "*?%"
6988#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990#endif
6991 , *p) != NULL)
6992 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 }
6994 return FALSE;
6995}
6996
6997/*
6998 * Return TRUE if the string "p" contains a wildcard.
6999 * Don't recognize '~' at the end as a wildcard.
7000 */
7001 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007002mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007004 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 if (*p == '\\' && p[1] != NUL)
7007 ++p;
7008 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 if (vim_strchr((char_u *)
7010#ifdef VMS
7011 "*?%$"
7012#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014#endif
7015 , *p) != NULL
7016 || (*p == '~' && p[1] != NUL))
7017 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 }
7019 return FALSE;
7020}
7021
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007023have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
7025 int i;
7026
7027 for (i = 0; i < num; i++)
7028 if (mch_has_wildcard(file[i]))
7029 return 1;
7030 return 0;
7031}
7032
7033 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007034have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035{
7036 int i;
7037
7038 for (i = 0; i < num; i++)
7039 if (vim_strchr(file[i], '$') != NULL)
7040 return TRUE;
7041 return FALSE;
7042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007044#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045/*
7046 * Scaled-down version of rename(), which is missing in Xenix.
7047 * This version can only move regular files and will fail if the
7048 * destination exists.
7049 */
7050 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007051mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
7053 struct stat st;
7054
Bram Moolenaar0f873732019-12-05 20:28:46 +01007055 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007057 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007059 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 return 0;
7061 return -1;
7062}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007063#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007065#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066/*
7067 * Initializes connection with gpm (if it isn't already opened)
7068 * Return 1 if succeeded (or connection already opened), 0 if failed
7069 */
7070 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007071gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007073 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074
7075 if (!gpm_flag)
7076 {
7077 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7078 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007079 // Default handling for mouse move
7080 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 gpm_connect.maxMod = 0xffff;
7082 if (Gpm_Open(&gpm_connect, 0) > 0)
7083 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007084 // gpm library tries to handling TSTP causes
7085 // problems. Anyways, we close connection to Gpm whenever
7086 // we are going to suspend or starting an external process
7087 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007088# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007090# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007091 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 }
7093 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007094 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 return 0;
7096 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007097 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098}
7099
7100/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007101 * Returns TRUE if the GPM mouse is enabled.
7102 */
7103 int
7104gpm_enabled(void)
7105{
7106 return gpm_flag && gpm_fd >= 0;
7107}
7108
7109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 */
7112 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007113gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007115 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 Gpm_Close();
7117}
7118
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007119/*
7120 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007122 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 */
7124 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007125mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126{
7127 int button;
7128 static Gpm_Event gpm_event;
7129 char_u string[6];
7130 int_u vim_modifiers;
7131 int row,col;
7132 unsigned char buttons_mask;
7133 unsigned char gpm_modifiers;
7134 static unsigned char old_buttons = 0;
7135
7136 Gpm_GetEvent(&gpm_event);
7137
7138#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007139 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 if (hold_gui_events)
7141 return 0;
7142#endif
7143
7144 row = gpm_event.y - 1;
7145 col = gpm_event.x - 1;
7146
Bram Moolenaar0f873732019-12-05 20:28:46 +01007147 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 string[1] = 'M';
7149 string[2] = 'G';
7150 switch (GPM_BARE_EVENTS(gpm_event.type))
7151 {
7152 case GPM_DRAG:
7153 string[3] = MOUSE_DRAG;
7154 break;
7155 case GPM_DOWN:
7156 buttons_mask = gpm_event.buttons & ~old_buttons;
7157 old_buttons = gpm_event.buttons;
7158 switch (buttons_mask)
7159 {
7160 case GPM_B_LEFT:
7161 button = MOUSE_LEFT;
7162 break;
7163 case GPM_B_MIDDLE:
7164 button = MOUSE_MIDDLE;
7165 break;
7166 case GPM_B_RIGHT:
7167 button = MOUSE_RIGHT;
7168 break;
7169 default:
7170 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007171 // Don't know what to do. Can more than one button be
7172 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 }
7174 string[3] = (char_u)(button | 0x20);
7175 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7176 break;
7177 case GPM_UP:
7178 string[3] = MOUSE_RELEASE;
7179 old_buttons &= ~gpm_event.buttons;
7180 break;
7181 default:
7182 return 0;
7183 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007184 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 gpm_modifiers = gpm_event.modifiers;
7186 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007187 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7188 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7189 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7191 vim_modifiers |= MOUSE_SHIFT;
7192
7193 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7194 vim_modifiers |= MOUSE_CTRL;
7195 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7196 vim_modifiers |= MOUSE_ALT;
7197 string[3] |= vim_modifiers;
7198 string[4] = (char_u)(col + ' ' + 1);
7199 string[5] = (char_u)(row + ' ' + 1);
7200 add_to_input_buf(string, 6);
7201 return 6;
7202}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007203#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007205#ifdef FEAT_SYSMOUSE
7206/*
7207 * Initialize connection with sysmouse.
7208 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7209 * output, any sysmouse output than will be processed via sig_sysmouse().
7210 * Return OK if succeeded, FAIL if failed.
7211 */
7212 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007213sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007214{
7215 struct mouse_info mouse;
7216
7217 mouse.operation = MOUSE_MODE;
7218 mouse.u.mode.mode = 0;
7219 mouse.u.mode.signal = SIGUSR2;
7220 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7221 {
7222 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7223 mouse.operation = MOUSE_SHOW;
7224 ioctl(1, CONS_MOUSECTL, &mouse);
7225 return OK;
7226 }
7227 return FAIL;
7228}
7229
7230/*
7231 * Stop processing SIGUSR2 signals, and also make sure that
7232 * virtual console do not send us any sysmouse related signal.
7233 */
7234 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007235sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007236{
7237 struct mouse_info mouse;
7238
7239 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7240 mouse.operation = MOUSE_MODE;
7241 mouse.u.mode.mode = 0;
7242 mouse.u.mode.signal = 0;
7243 ioctl(1, CONS_MOUSECTL, &mouse);
7244}
7245
7246/*
7247 * Gets info from sysmouse and adds special keys to input buf.
7248 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007249 static RETSIGTYPE
7250sig_sysmouse SIGDEFARG(sigarg)
7251{
7252 struct mouse_info mouse;
7253 struct video_info video;
7254 char_u string[6];
7255 int row, col;
7256 int button;
7257 int buttons;
7258 static int oldbuttons = 0;
7259
7260#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007261 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007262 if (hold_gui_events)
7263 return;
7264#endif
7265
7266 mouse.operation = MOUSE_GETINFO;
7267 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7268 && ioctl(1, FBIO_MODEINFO, &video) != -1
7269 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7270 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7271 {
7272 row = mouse.u.data.y / video.vi_cheight;
7273 col = mouse.u.data.x / video.vi_cwidth;
7274 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007275 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007276 string[1] = 'M';
7277 string[2] = 'S';
7278 if (oldbuttons == buttons && buttons != 0)
7279 {
7280 button = MOUSE_DRAG;
7281 }
7282 else
7283 {
7284 switch (buttons)
7285 {
7286 case 0:
7287 button = MOUSE_RELEASE;
7288 break;
7289 case 1:
7290 button = MOUSE_LEFT;
7291 break;
7292 case 2:
7293 button = MOUSE_MIDDLE;
7294 break;
7295 case 4:
7296 button = MOUSE_RIGHT;
7297 break;
7298 default:
7299 return;
7300 }
7301 oldbuttons = buttons;
7302 }
7303 string[3] = (char_u)(button);
7304 string[4] = (char_u)(col + ' ' + 1);
7305 string[5] = (char_u)(row + ' ' + 1);
7306 add_to_input_buf(string, 6);
7307 }
7308 return;
7309}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007310#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007311
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007313typedef char_u * (*STRPROCSTR)(char_u *);
7314typedef char_u * (*INTPROCSTR)(int);
7315typedef int (*STRPROCINT)(char_u *);
7316typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317
7318/*
7319 * Call a DLL routine which takes either a string or int param
7320 * and returns an allocated string.
7321 */
7322 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007323mch_libcall(
7324 char_u *libname,
7325 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007326 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007327 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007328 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007329 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330{
7331# if defined(USE_DLOPEN)
7332 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007333 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334# else
7335 shl_t hinstLib;
7336# endif
7337 STRPROCSTR ProcAdd;
7338 INTPROCSTR ProcAddI;
7339 char_u *retval_str = NULL;
7340 int retval_int = 0;
7341 int success = FALSE;
7342
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007343 /*
7344 * Get a handle to the DLL module.
7345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007347 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007348 (void)dlerror();
7349
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 hinstLib = dlopen((char *)libname, RTLD_LAZY
7351# ifdef RTLD_LOCAL
7352 | RTLD_LOCAL
7353# endif
7354 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007355 if (hinstLib == NULL)
7356 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007357 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007358 dlerr = (char *)dlerror();
7359 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007360 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362# else
7363 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7364# endif
7365
Bram Moolenaar0f873732019-12-05 20:28:46 +01007366 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 if (hinstLib != NULL)
7368 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007369# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 /*
7371 * Catch a crash when calling the library function. For example when
7372 * using a number where a string pointer is expected.
7373 */
7374 mch_startjmp();
7375 if (SETJMP(lc_jump_env) != 0)
7376 {
7377 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007378# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007379 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 mch_didjmp();
7382 }
7383 else
7384# endif
7385 {
7386 retval_str = NULL;
7387 retval_int = 0;
7388
7389 if (argstring != NULL)
7390 {
7391# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007392 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007393 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394# else
7395 if (shl_findsym(&hinstLib, (const char *)funcname,
7396 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7397 ProcAdd = NULL;
7398# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007399 if ((success = (ProcAdd != NULL
7400# if defined(USE_DLOPEN)
7401 && dlerr == NULL
7402# endif
7403 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 {
7405 if (string_result == NULL)
7406 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7407 else
7408 retval_str = (ProcAdd)(argstring);
7409 }
7410 }
7411 else
7412 {
7413# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007414 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007415 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416# else
7417 if (shl_findsym(&hinstLib, (const char *)funcname,
7418 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7419 ProcAddI = NULL;
7420# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007421 if ((success = (ProcAddI != NULL
7422# if defined(USE_DLOPEN)
7423 && dlerr == NULL
7424# endif
7425 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 {
7427 if (string_result == NULL)
7428 retval_int = ((INTPROCINT)ProcAddI)(argint);
7429 else
7430 retval_str = (ProcAddI)(argint);
7431 }
7432 }
7433
Bram Moolenaar0f873732019-12-05 20:28:46 +01007434 // Save the string before we free the library.
7435 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 if (string_result == NULL)
7437 *number_result = retval_int;
7438 else if (retval_str != NULL
7439 && retval_str != (char_u *)1
7440 && retval_str != (char_u *)-1)
7441 *string_result = vim_strsave(retval_str);
7442 }
7443
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007444# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 mch_endjmp();
7446# ifdef SIGHASARG
7447 if (lc_signal != 0)
7448 {
7449 int i;
7450
Bram Moolenaar0f873732019-12-05 20:28:46 +01007451 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 for (i = 0; signal_info[i].sig != -1; i++)
7453 if (lc_signal == signal_info[i].sig)
7454 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007455 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 }
7457# endif
7458# endif
7459
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007461 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007462 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007463 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007464
Bram Moolenaar0f873732019-12-05 20:28:46 +01007465 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 (void)dlclose(hinstLib);
7467# else
7468 (void)shl_unload(hinstLib);
7469# endif
7470 }
7471
7472 if (!success)
7473 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007474 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 return FAIL;
7476 }
7477
7478 return OK;
7479}
7480#endif
7481
7482#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007483static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484static int xterm_button;
7485
7486/*
7487 * Setup a dummy window for X selections in a terminal.
7488 */
7489 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007490setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
7492 int z = 0;
7493 char *strp = "";
7494 Widget AppShell;
7495
7496 if (!x_connect_to_server())
7497 return;
7498
7499 open_app_context();
7500 if (app_context != NULL && xterm_Shell == (Widget)0)
7501 {
7502 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007503# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007505# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007506# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007507 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508
7509 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007510 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511# endif
7512
Bram Moolenaar0f873732019-12-05 20:28:46 +01007513 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 oldhandler = XSetErrorHandler(x_error_check);
7515
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007516# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007517 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7519 mch_startjmp();
7520 if (SETJMP(lc_jump_env) != 0)
7521 {
7522 mch_didjmp();
7523 xterm_dpy = NULL;
7524 }
7525 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 {
7528 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7529 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007530 if (xterm_dpy != NULL)
7531 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007532# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007534# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 }
7536
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007537# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007538 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007540# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007541 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 (void)XSetErrorHandler(oldhandler);
7543
7544 if (xterm_dpy == NULL)
7545 {
7546 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007547 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 return;
7549 }
7550
Bram Moolenaar0f873732019-12-05 20:28:46 +01007551 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 (void)XSetIOErrorHandler(x_IOerror_handler);
7553
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007554# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007556 {
7557 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007558 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007559 verbose_leave();
7560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561# endif
7562
Bram Moolenaar0f873732019-12-05 20:28:46 +01007563 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7565 applicationShellWidgetClass, xterm_dpy,
7566 NULL);
7567 if (AppShell == (Widget)0)
7568 return;
7569 xterm_Shell = XtVaCreatePopupShell("VIM",
7570 topLevelShellWidgetClass, AppShell,
7571 XtNmappedWhenManaged, 0,
7572 XtNwidth, 1,
7573 XtNheight, 1,
7574 NULL);
7575 if (xterm_Shell == (Widget)0)
7576 return;
7577
7578 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007579 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 if (x11_display == NULL)
7581 x11_display = xterm_dpy;
7582
7583 XtRealizeWidget(xterm_Shell);
7584 XSync(xterm_dpy, False);
7585 xterm_update();
7586 }
7587 if (xterm_Shell != (Widget)0)
7588 {
7589 clip_init(TRUE);
7590 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7591 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007592 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 if (test_x11_window(xterm_dpy) == FAIL)
7594 x11_window = 0;
7595 if (x11_window != 0)
7596 xterm_trace = 0;
7597 }
7598}
7599
7600 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007601start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602{
7603 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7604 return;
7605 xterm_trace = 1;
7606 xterm_button = button;
7607 do_xterm_trace();
7608}
7609
7610
7611 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007612stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613{
7614 if (xterm_trace < 0)
7615 return;
7616 xterm_trace = 0;
7617}
7618
7619/*
7620 * Query the xterm pointer and generate mouse termcodes if necessary
7621 * return TRUE if dragging is active, else FALSE
7622 */
7623 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007624do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625{
7626 Window root, child;
7627 int root_x, root_y;
7628 int win_x, win_y;
7629 int row, col;
7630 int_u mask_return;
7631 char_u buf[50];
7632 char_u *strp;
7633 long got_hints;
7634 static char_u *mouse_code;
7635 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7636 static int prev_row = 0, prev_col = 0;
7637 static XSizeHints xterm_hints;
7638
7639 if (xterm_trace <= 0)
7640 return FALSE;
7641
7642 if (xterm_trace == 1)
7643 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007644 // Get the hints just before tracking starts. The font size might
7645 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007646 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7647 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 || xterm_hints.width_inc <= 1
7649 || xterm_hints.height_inc <= 1)
7650 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007651 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 return FALSE;
7653 }
7654
Bram Moolenaar0f873732019-12-05 20:28:46 +01007655 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 mouse_code = find_termcode(mouse_name);
7657 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007658 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 xterm_trace = 2;
7660
Bram Moolenaar0f873732019-12-05 20:28:46 +01007661 // Find the offset of the chars, there might be a scrollbar on the
7662 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7664 &win_x, &win_y, &mask_return);
7665 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7666 - (xterm_hints.height_inc / 2);
7667 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7668 xterm_hints.y = 2;
7669 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7670 - (xterm_hints.width_inc / 2);
7671 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7672 xterm_hints.x = 2;
7673 return TRUE;
7674 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007675 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 {
7677 xterm_trace = 0;
7678 return FALSE;
7679 }
7680
7681 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7682 &win_x, &win_y, &mask_return);
7683
7684 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7685 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7686 if (row == prev_row && col == prev_col)
7687 return TRUE;
7688
7689 STRCPY(buf, mouse_code);
7690 strp = buf + STRLEN(buf);
7691 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7692 *strp++ = (char_u)(col + ' ' + 1);
7693 *strp++ = (char_u)(row + ' ' + 1);
7694 *strp = 0;
7695 add_to_input_buf(buf, STRLEN(buf));
7696
7697 prev_row = row;
7698 prev_col = col;
7699 return TRUE;
7700}
7701
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007702# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703/*
7704 * Destroy the display, window and app_context. Required for GTK.
7705 */
7706 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007707clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708{
7709 if (xterm_Shell != (Widget)0)
7710 {
7711 XtDestroyWidget(xterm_Shell);
7712 xterm_Shell = (Widget)0;
7713 }
7714 if (xterm_dpy != NULL)
7715 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007716# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007717 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007719# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 if (x11_display == xterm_dpy)
7721 x11_display = NULL;
7722 xterm_dpy = NULL;
7723 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007724# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 if (app_context != (XtAppContext)NULL)
7726 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007727 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 XtDestroyApplicationContext(app_context);
7729 app_context = (XtAppContext)NULL;
7730 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007731# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732}
7733# endif
7734
7735/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007736 * Catch up with GUI or X events.
7737 */
7738 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007739clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007740{
7741# ifdef FEAT_GUI
7742 if (gui.in_use)
7743 gui_mch_update();
7744 else
7745# endif
7746 if (xterm_Shell != (Widget)0)
7747 xterm_update();
7748}
7749
7750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 * Catch up with any queued X events. This may put keyboard input into the
7752 * input buffer, call resize call-backs, trigger timers etc. If there is
7753 * nothing in the X event queue (& no timers pending), then we return
7754 * immediately.
7755 */
7756 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007757xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758{
7759 XEvent event;
7760
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007761 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007763 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007765 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007766 break;
7767
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007768 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007769 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007770 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007771 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007772#ifdef FEAT_CLIENTSERVER
7773 {
7774 XPropertyEvent *e = (XPropertyEvent *)&event;
7775
7776 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007778 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007781 XtDispatchEvent(&event);
7782 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007783 else
7784 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007785 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007786 XtAppProcessEvent(app_context, mask);
7787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 }
7789}
7790
7791 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007792clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
7794 if (xterm_Shell != (Widget)0)
7795 return clip_x11_own_selection(xterm_Shell, cbd);
7796 return FAIL;
7797}
7798
7799 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007800clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801{
7802 if (xterm_Shell != (Widget)0)
7803 clip_x11_lose_selection(xterm_Shell, cbd);
7804}
7805
7806 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007807clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808{
7809 if (xterm_Shell != (Widget)0)
7810 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7811}
7812
7813 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007814clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815{
7816 clip_x11_set_selection(cbd);
7817}
7818#endif
7819
7820
7821#if defined(USE_XSMP) || defined(PROTO)
7822/*
7823 * Code for X Session Management Protocol.
7824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825
7826# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827/*
7828 * This is our chance to ask the user if they want to save,
7829 * or abort the logout
7830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007832xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833{
7834 cmdmod_T save_cmdmod;
7835 int cancel_shutdown = False;
7836
7837 save_cmdmod = cmdmod;
7838 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007839 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007840 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 cancel_shutdown = True;
7842 cmdmod = save_cmdmod;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007843 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 out_flush();
7845
Bram Moolenaar0f873732019-12-05 20:28:46 +01007846 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 SmcInteractDone(smc_conn, cancel_shutdown);
7848
Bram Moolenaar0f873732019-12-05 20:28:46 +01007849 // Finish off
7850 // Only end save-yourself here if we're not cancelling shutdown;
7851 // we'll get a cancelled callback later in which we'll end it.
7852 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 if (!cancel_shutdown)
7854 {
7855 xsmp.save_yourself = False;
7856 SmcSaveYourselfDone(smc_conn, True);
7857 }
7858}
7859# endif
7860
7861/*
7862 * Callback that starts save-yourself.
7863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007865xsmp_handle_save_yourself(
7866 SmcConn smc_conn,
7867 SmPointer client_data UNUSED,
7868 int save_type UNUSED,
7869 Bool shutdown,
7870 int interact_style UNUSED,
7871 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007873 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 if (xsmp.save_yourself)
7875 SmcSaveYourselfDone(smc_conn, True);
7876 xsmp.save_yourself = True;
7877 xsmp.shutdown = shutdown;
7878
Bram Moolenaar0f873732019-12-05 20:28:46 +01007879 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007881 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882
7883 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007884 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885
7886# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007887 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007889 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 SmcInteractRequest(smc_conn, SmDialogError,
7891 xsmp_handle_interaction, client_data);
7892 else
7893# endif
7894 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007895 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 SmcSaveYourselfDone(smc_conn, True);
7897 xsmp.save_yourself = False;
7898 }
7899}
7900
7901
7902/*
7903 * Callback to warn us of imminent death.
7904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007906xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
7908 xsmp_close();
7909
Bram Moolenaar0f873732019-12-05 20:28:46 +01007910 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 getout_preserve_modified(0);
7912}
7913
7914
7915/*
7916 * Callback to tell us that save-yourself has completed.
7917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007919xsmp_save_complete(
7920 SmcConn smc_conn UNUSED,
7921 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922{
7923 xsmp.save_yourself = False;
7924}
7925
7926
7927/*
7928 * Callback to tell us that an instigated shutdown was cancelled
7929 * (maybe even by us)
7930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007932xsmp_shutdown_cancelled(
7933 SmcConn smc_conn,
7934 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
7936 if (xsmp.save_yourself)
7937 SmcSaveYourselfDone(smc_conn, True);
7938 xsmp.save_yourself = False;
7939 xsmp.shutdown = False;
7940}
7941
7942
7943/*
7944 * Callback to tell us that a new ICE connection has been established.
7945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007947xsmp_ice_connection(
7948 IceConn iceConn,
7949 IcePointer clientData UNUSED,
7950 Bool opening,
7951 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007953 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 if (opening)
7955 {
7956 xsmp_icefd = IceConnectionNumber(iceConn);
7957 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7958 }
7959}
7960
7961
Bram Moolenaar0f873732019-12-05 20:28:46 +01007962// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007964xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
7966 Bool rep;
7967
7968 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7969 == IceProcessMessagesIOError)
7970 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007971 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007973 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 xsmp_close();
7975 return FAIL;
7976 }
7977 else
7978 return OK;
7979}
7980
7981static int dummy;
7982
Bram Moolenaar0f873732019-12-05 20:28:46 +01007983// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 void
7985xsmp_init(void)
7986{
7987 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 SmcCallbacks smcallbacks;
7989#if 0
7990 SmPropValue smname;
7991 SmProp smnameprop;
7992 SmProp *smprops[1];
7993#endif
7994
7995 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007996 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997
7998 xsmp.save_yourself = xsmp.shutdown = False;
7999
Bram Moolenaar0f873732019-12-05 20:28:46 +01008000 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8002 smcallbacks.save_yourself.client_data = NULL;
8003 smcallbacks.die.callback = xsmp_die;
8004 smcallbacks.die.client_data = NULL;
8005 smcallbacks.save_complete.callback = xsmp_save_complete;
8006 smcallbacks.save_complete.client_data = NULL;
8007 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8008 smcallbacks.shutdown_cancelled.client_data = NULL;
8009
Bram Moolenaar0f873732019-12-05 20:28:46 +01008010 // Set up a watch on ICE connection creations. The "dummy" argument is
8011 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8013 {
8014 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008015 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 return;
8017 }
8018
Bram Moolenaar0f873732019-12-05 20:28:46 +01008019 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 xsmp.smcconn = SmcOpenConnection(
8021 NULL,
8022 NULL,
8023 SmProtoMajor,
8024 SmProtoMinor,
8025 SmcSaveYourselfProcMask | SmcDieProcMask
8026 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8027 &smcallbacks,
8028 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008029 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008030 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 errorstring);
8032 if (xsmp.smcconn == NULL)
8033 {
8034 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008035
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008037 {
8038 vim_snprintf(errorreport, sizeof(errorreport),
8039 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008040 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 return;
8043 }
8044 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8045
8046#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008047 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 smname.value = "vim";
8049 smname.length = 3;
8050 smnameprop.name = "SmProgram";
8051 smnameprop.type = "SmARRAY8";
8052 smnameprop.num_vals = 1;
8053 smnameprop.vals = &smname;
8054
8055 smprops[0] = &smnameprop;
8056 SmcSetProperties(xsmp.smcconn, 1, smprops);
8057#endif
8058}
8059
8060
Bram Moolenaar0f873732019-12-05 20:28:46 +01008061// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008063xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064{
8065 if (xsmp_icefd != -1)
8066 {
8067 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008068 if (xsmp.clientid != NULL)
8069 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008070 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 xsmp_icefd = -1;
8072 }
8073}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008074#endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075
8076
8077#ifdef EBCDIC
Bram Moolenaar0f873732019-12-05 20:28:46 +01008078// Translate character to its CTRL- value
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079char CtrlTable[] =
8080{
8081/* 00 - 5E */
8082 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8083 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8084 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8085 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8086 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8087 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8088/* ^ */ 0x1E,
8089/* - */ 0x1F,
8090/* 61 - 6C */
8091 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8092/* _ */ 0x1F,
8093/* 6E - 80 */
8094 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8095/* a */ 0x01,
8096/* b */ 0x02,
8097/* c */ 0x03,
8098/* d */ 0x37,
8099/* e */ 0x2D,
8100/* f */ 0x2E,
8101/* g */ 0x2F,
8102/* h */ 0x16,
8103/* i */ 0x05,
8104/* 8A - 90 */
8105 0, 0, 0, 0, 0, 0, 0,
8106/* j */ 0x15,
8107/* k */ 0x0B,
8108/* l */ 0x0C,
8109/* m */ 0x0D,
8110/* n */ 0x0E,
8111/* o */ 0x0F,
8112/* p */ 0x10,
8113/* q */ 0x11,
8114/* r */ 0x12,
8115/* 9A - A1 */
8116 0, 0, 0, 0, 0, 0, 0, 0,
8117/* s */ 0x13,
8118/* t */ 0x3C,
8119/* u */ 0x3D,
8120/* v */ 0x32,
8121/* w */ 0x26,
8122/* x */ 0x18,
8123/* y */ 0x19,
8124/* z */ 0x3F,
8125/* AA - AC */
8126 0, 0, 0,
8127/* [ */ 0x27,
8128/* AE - BC */
8129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8130/* ] */ 0x1D,
8131/* BE - C0 */ 0, 0, 0,
8132/* A */ 0x01,
8133/* B */ 0x02,
8134/* C */ 0x03,
8135/* D */ 0x37,
8136/* E */ 0x2D,
8137/* F */ 0x2E,
8138/* G */ 0x2F,
8139/* H */ 0x16,
8140/* I */ 0x05,
8141/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8142/* J */ 0x15,
8143/* K */ 0x0B,
8144/* L */ 0x0C,
8145/* M */ 0x0D,
8146/* N */ 0x0E,
8147/* O */ 0x0F,
8148/* P */ 0x10,
8149/* Q */ 0x11,
8150/* R */ 0x12,
8151/* DA - DF */ 0, 0, 0, 0, 0, 0,
8152/* \ */ 0x1C,
8153/* E1 */ 0,
8154/* S */ 0x13,
8155/* T */ 0x3C,
8156/* U */ 0x3D,
8157/* V */ 0x32,
8158/* W */ 0x26,
8159/* X */ 0x18,
8160/* Y */ 0x19,
8161/* Z */ 0x3F,
8162/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8163 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8164};
8165
8166char MetaCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008167{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8169 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8170 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8171 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8172};
8173
8174
Bram Moolenaar0f873732019-12-05 20:28:46 +01008175// TODO: Use characters NOT numbers!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176char CtrlCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008177{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8179 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8180 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8181 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8182};
8183
8184
8185#endif