blob: 3464426c3d19f61e93123d96877f0291f36c73c4 [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# ifdef IXON_NOT_USED
3486 // Do not make CTRL-S stop output, for most users it is unexpected and
3487 // is hardly ever useful.
3488 tnew.c_iflag |= IXON;
3489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3491# if defined(IEXTEN) && !defined(__MINT__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003492 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
3493 // but it breaks function keys on MINT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494# endif
3495 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003496# ifdef ONLCR
3497 // Don't map NL -> CR NL, we do it ourselves.
3498 // Also disable expanding tabs if possible.
3499# ifdef XTABS
3500 tnew.c_oflag &= ~(ONLCR | XTABS);
3501# else
3502# ifdef TAB3
3503 tnew.c_oflag &= ~(ONLCR | TAB3);
3504# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003506# endif
3507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003509 tnew.c_cc[VMIN] = 1; // return after 1 char
3510 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 }
3512 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003513 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003514 // Also reset ICANON here, otherwise on Solaris select() won't see
3515 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003516 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003517 tnew.c_cc[VMIN] = 1; // return after 1 char
3518 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
3521# if defined(HAVE_TERMIOS_H)
3522 {
3523 int n = 10;
3524
Bram Moolenaar0f873732019-12-05 20:28:46 +01003525 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3526 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3528 && errno == EINTR && n > 0)
3529 --n;
3530 }
3531# else
3532 ioctl(read_cmd_fd, TCSETA, &tnew);
3533# endif
3534
3535#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 /*
3537 * for "old" tty systems
3538 */
3539# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003540# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541# endif
3542 static struct sgttyb ttybold;
3543 struct sgttyb ttybnew;
3544
3545 if (first)
3546 {
3547 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003548 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550
3551 ttybnew = ttybold;
3552 if (tmode == TMODE_RAW)
3553 {
3554 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3555 ttybnew.sg_flags |= RAW;
3556 }
3557 else if (tmode == TMODE_SLEEP)
3558 ttybnew.sg_flags &= ~(ECHO);
3559 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3560#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003561 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562}
3563
3564/*
3565 * Try to get the code for "t_kb" from the stty setting
3566 *
3567 * Even if termcap claims a backspace key, the user's setting *should*
3568 * prevail. stty knows more about reality than termcap does, and if
3569 * somebody's usual erase key is DEL (which, for most BSD users, it will
3570 * be), they're going to get really annoyed if their erase key starts
3571 * doing forward deletes for no reason. (Eric Fischer)
3572 */
3573 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003574get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003576 ttyinfo_T info;
3577 char_u buf[2];
3578 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003580 if (get_tty_info(read_cmd_fd, &info) == OK)
3581 {
3582 intr_char = info.interrupt;
3583 buf[0] = info.backspace;
3584 buf[1] = NUL;
3585 add_termcode((char_u *)"kb", buf, FALSE);
3586
Bram Moolenaar0f873732019-12-05 20:28:46 +01003587 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003588 p = find_termcode((char_u *)"kD");
3589 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3590 do_fixdel(NULL);
3591 }
3592}
3593
3594/*
3595 * Obtain the characters that Backspace and Enter produce on "fd".
3596 * Returns OK or FAIL.
3597 */
3598 int
3599get_tty_info(int fd, ttyinfo_T *info)
3600{
3601#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602# ifdef HAVE_TERMIOS_H
3603 struct termios keys;
3604# else
3605 struct termio keys;
3606# endif
3607
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003608 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003610 info->backspace = keys.c_cc[VERASE];
3611 info->interrupt = keys.c_cc[VINTR];
3612 if (keys.c_iflag & ICRNL)
3613 info->enter = NL;
3614 else
3615 info->enter = CAR;
3616 if (keys.c_oflag & ONLCR)
3617 info->nl_does_cr = TRUE;
3618 else
3619 info->nl_does_cr = FALSE;
3620 return OK;
3621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003623 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 struct sgttyb keys;
3625
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003626 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003628 info->backspace = keys.sg_erase;
3629 info->interrupt = keys.sg_kill;
3630 info->enter = CAR;
3631 info->nl_does_cr = TRUE;
3632 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003635 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636}
3637
Bram Moolenaar0f873732019-12-05 20:28:46 +01003638#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003640static int mouse_ison = FALSE;
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642/*
3643 * Set mouse clicks on or off.
3644 */
3645 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003646mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003648#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003649 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 int xterm_mouse_vers;
3652
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003653#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003654 if (!on)
3655 // Make sure not tracing mouse movements. Important when a button-down
3656 // was received but no release yet.
3657 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003658#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003659
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003660 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003661#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003662 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003663#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003664 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003665 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 return;
3667
3668 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003669
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003670#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003671 if (ttym_flags == TTYM_URXVT)
3672 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003673 out_str_nf((char_u *)
3674 (on
3675 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3676 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003677 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003678 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003679#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003680
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003681 if (ttym_flags == TTYM_SGR)
3682 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003683 // SGR mode supports columns above 223
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003684 out_str_nf((char_u *)
3685 (on
3686 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3687 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003688 mouse_ison = on;
3689 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003690
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003691#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003692 if (bevalterm_ison != (p_bevalterm && on))
3693 {
3694 bevalterm_ison = (p_bevalterm && on);
3695 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003696 // disable mouse movement events, enabling is below
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003697 out_str_nf((char_u *)
3698 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003699 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003700#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 if (xterm_mouse_vers > 0)
3703 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003704 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 out_str_nf((char_u *)
3706 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003707 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003708#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003709 bevalterm_ison
3710 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003711#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003712 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003714 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 out_str_nf((char_u *)
3716 (xterm_mouse_vers > 1
3717 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3718 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003719 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
3721
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003722#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 else if (ttym_flags == TTYM_DEC)
3724 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003725 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003727 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003729 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003733#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 else
3735 {
3736 if (on)
3737 {
3738 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003739 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
3741 else
3742 {
3743 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003744 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
3746 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003749#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003750 else
3751 {
3752 if (on)
3753 {
3754 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003755 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003756 }
3757 else
3758 {
3759 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003760 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003761 }
3762 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003763#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003764
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003765#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 else
3767 {
3768 if (on)
3769 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003770 // D - Enable Mouse up/down messages
3771 // L - Enable Left Button Reporting
3772 // M - Enable Middle Button Reporting
3773 // R - Enable Right Button Reporting
3774 // K - Enable SHIFT and CTRL key Reporting
3775 // + - Enable Advanced messaging of mouse moves and up/down messages
3776 // Q - Quiet No Ack
3777 // # - Numeric value of mouse pointer required
3778 // 0 = Multiview 2000 cursor, used as standard
3779 // 1 = Windows Arrow
3780 // 2 = Windows I Beam
3781 // 3 = Windows Hour Glass
3782 // 4 = Windows Cross Hair
3783 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003784# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003785 // Disables full feedback of pointer movements
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3787 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003788# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3790 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003791# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003792 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
3794 else
3795 {
3796 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3797 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003798 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
3800 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003801#endif
3802#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 else
3804 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003805 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (on)
3807 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3808 else
3809 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003810 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813}
3814
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003815#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003816/*
3817 * Called when 'balloonevalterm' changed.
3818 */
3819 void
3820mch_bevalterm_changed(void)
3821{
3822 mch_setmouse(mouse_ison);
3823}
3824#endif
3825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826/*
3827 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3828 */
3829 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003830check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831{
3832# ifdef FEAT_MOUSE_XTERM
3833 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003834# ifdef FEAT_MOUSE_URXVT
3835 && use_xterm_mouse() != 3
3836# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837# ifdef FEAT_GUI
3838 && !gui.in_use
3839# endif
3840 )
3841 {
3842 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003843 ? IF_EB("\233M", CSI_STR "M")
3844 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 if (*p_mouse != NUL)
3846 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003847 // force mouse off and maybe on to send possibly new mouse
3848 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 mch_setmouse(FALSE);
3850 setmouse();
3851 }
3852 }
3853 else
3854 del_mouse_termcode(KS_MOUSE);
3855# endif
3856
3857# ifdef FEAT_MOUSE_GPM
3858 if (!use_xterm_mouse()
3859# ifdef FEAT_GUI
3860 && !gui.in_use
3861# endif
3862 )
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003863 set_mouse_termcode(KS_GPM_MOUSE,
3864 (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3865 else
3866 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867# endif
3868
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003869# ifdef FEAT_SYSMOUSE
3870 if (!use_xterm_mouse()
3871# ifdef FEAT_GUI
3872 && !gui.in_use
3873# endif
3874 )
3875 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3876# endif
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003879 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 if (!use_xterm_mouse()
3881# ifdef FEAT_GUI
3882 && !gui.in_use
3883# endif
3884 )
3885 set_mouse_termcode(KS_JSBTERM_MOUSE,
3886 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3887 else
3888 del_mouse_termcode(KS_JSBTERM_MOUSE);
3889# endif
3890
3891# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003892 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3893 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (!use_xterm_mouse()
3895# ifdef FEAT_GUI
3896 && !gui.in_use
3897# endif
3898 )
3899 set_mouse_termcode(KS_NETTERM_MOUSE,
3900 (char_u *)IF_EB("\033}", ESC_STR "}"));
3901 else
3902 del_mouse_termcode(KS_NETTERM_MOUSE);
3903# endif
3904
3905# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003906 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003907 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908# ifdef FEAT_GUI
3909 && !gui.in_use
3910# endif
3911 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003912 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3913 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 else
3915 del_mouse_termcode(KS_DEC_MOUSE);
3916# endif
3917# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01003918 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003919 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920# ifdef FEAT_GUI
3921 && !gui.in_use
3922# endif
3923 )
3924 set_mouse_termcode(KS_PTERM_MOUSE,
3925 (char_u *) IF_EB("\033[", ESC_STR "["));
3926 else
3927 del_mouse_termcode(KS_PTERM_MOUSE);
3928# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003929# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003930 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003931# ifdef FEAT_GUI
3932 && !gui.in_use
3933# endif
3934 )
3935 {
3936 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003937 ? IF_EB("\233*M", CSI_STR "*M")
3938 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003939
3940 if (*p_mouse != NUL)
3941 {
3942 mch_setmouse(FALSE);
3943 setmouse();
3944 }
3945 }
3946 else
3947 del_mouse_termcode(KS_URXVT_MOUSE);
3948# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003949 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003950# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003951 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003952# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003953 )
3954 {
3955 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003956 ? IF_EB("\233<*M", CSI_STR "<*M")
3957 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3958
3959 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3960 ? IF_EB("\233<*m", CSI_STR "<*m")
3961 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003962
3963 if (*p_mouse != NUL)
3964 {
3965 mch_setmouse(FALSE);
3966 setmouse();
3967 }
3968 }
3969 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003970 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003971 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003972 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976#ifndef VMS
3977
3978/*
3979 * Try to get the current window size:
3980 * 1. with an ioctl(), most accurate method
3981 * 2. from the environment variables LINES and COLUMNS
3982 * 3. from the termcap
3983 * 4. keep using the old values
3984 * Return OK when size could be determined, FAIL otherwise.
3985 */
3986 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003987mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988{
3989 long rows = 0;
3990 long columns = 0;
3991 char_u *p;
3992
3993 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 * 1. try using an ioctl. It is the most accurate method.
3995 *
3996 * Try using TIOCGWINSZ first, some systems that have it also define
3997 * TIOCGSIZE but don't have a struct ttysize.
3998 */
3999# ifdef TIOCGWINSZ
4000 {
4001 struct winsize ws;
4002 int fd = 1;
4003
Bram Moolenaar0f873732019-12-05 20:28:46 +01004004 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 if (!isatty(fd) && isatty(read_cmd_fd))
4006 fd = read_cmd_fd;
4007 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4008 {
4009 columns = ws.ws_col;
4010 rows = ws.ws_row;
4011 }
4012 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004013# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014# ifdef TIOCGSIZE
4015 {
4016 struct ttysize ts;
4017 int fd = 1;
4018
Bram Moolenaar0f873732019-12-05 20:28:46 +01004019 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 if (!isatty(fd) && isatty(read_cmd_fd))
4021 fd = read_cmd_fd;
4022 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4023 {
4024 columns = ts.ts_cols;
4025 rows = ts.ts_lines;
4026 }
4027 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004028# endif // TIOCGSIZE
4029# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030
4031 /*
4032 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004033 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4034 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004036 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
4038 if ((p = (char_u *)getenv("LINES")))
4039 rows = atoi((char *)p);
4040 if ((p = (char_u *)getenv("COLUMNS")))
4041 columns = atoi((char *)p);
4042 }
4043
4044#ifdef HAVE_TGETENT
4045 /*
4046 * 3. try reading "co" and "li" entries from termcap
4047 */
4048 if (columns == 0 || rows == 0)
4049 getlinecol(&columns, &rows);
4050#endif
4051
4052 /*
4053 * 4. If everything fails, use the old values
4054 */
4055 if (columns <= 0 || rows <= 0)
4056 return FAIL;
4057
4058 Rows = rows;
4059 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004060 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 return OK;
4062}
4063
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004064#if defined(FEAT_TERMINAL) || defined(PROTO)
4065/*
4066 * Report the windows size "rows" and "cols" to tty "fd".
4067 */
4068 int
4069mch_report_winsize(int fd, int rows, int cols)
4070{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004071 int tty_fd;
4072 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004073
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004074 tty_fd = get_tty_fd(fd);
4075 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004076 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004077# if defined(TIOCSWINSZ)
4078 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004079
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004080 ws.ws_col = cols;
4081 ws.ws_row = rows;
4082 ws.ws_xpixel = cols * 5;
4083 ws.ws_ypixel = rows * 10;
4084 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4085 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4086 retval == 0 ? "success" : "failed");
4087# elif defined(TIOCSSIZE)
4088 struct ttysize ts;
4089
4090 ts.ts_cols = cols;
4091 ts.ts_lines = rows;
4092 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4093 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4094 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004095# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004096 if (tty_fd != fd)
4097 close(tty_fd);
4098 }
4099 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004100}
4101#endif
4102
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103/*
4104 * Try to set the window size to Rows and Columns.
4105 */
4106 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004107mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108{
4109 if (*T_CWS)
4110 {
4111 /*
4112 * NOTE: if you get an error here that term_set_winsize() is
4113 * undefined, check the output of configure. It could probably not
4114 * find a ncurses, termcap or termlib library.
4115 */
4116 term_set_winsize((int)Rows, (int)Columns);
4117 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004118 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120}
4121
Bram Moolenaar0f873732019-12-05 20:28:46 +01004122#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
4124/*
4125 * Rows and/or Columns has changed.
4126 */
4127 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004128mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004130 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131}
4132
Bram Moolenaar205b8862011-09-07 15:04:31 +02004133/*
4134 * Wait for process "child" to end.
4135 * Return "child" if it exited properly, <= 0 on error.
4136 */
4137 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004138wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004139{
4140 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004141 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004142
4143 while (wait_pid != child)
4144 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004145 // When compiled with Python threads are probably used, in which case
4146 // wait() sometimes hangs for no obvious reason. Use waitpid()
4147 // instead and loop (like the GUI). Also needed for other interfaces,
4148 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004149# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004150 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004151# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004152 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004153# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004154 if (wait_pid == 0)
4155 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004156 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004157 mch_delay(delay_msec, TRUE);
4158 if (++delay_msec > 10)
4159 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004160 continue;
4161 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004162 if (wait_pid <= 0
4163# ifdef ECHILD
4164 && errno == ECHILD
4165# endif
4166 )
4167 break;
4168 }
4169 return wait_pid;
4170}
4171
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004172#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004173/*
4174 * Set the environment for a child process.
4175 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004176 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004177set_child_environment(
4178 long rows,
4179 long columns,
4180 char *term,
4181 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004182{
4183# ifdef HAVE_SETENV
4184 char envbuf[50];
4185# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004186 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004187 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004188 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004189 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004190 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004191# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004192 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004193# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004194# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004195 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004196# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004197# endif
4198
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004199# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004200 setenv("TERM", term, 1);
4201 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004202 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004203 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004204 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004205 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004206 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004207 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004208 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004209# ifdef FEAT_TERMINAL
4210 if (is_terminal)
4211 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004212 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004213 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4214 }
4215# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004216# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004217 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004218# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004219# else
4220 /*
4221 * Putenv does not copy the string, it has to remain valid.
4222 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004223 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004224 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004225 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4226 putenv(envbuf_Term);
4227 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004228 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004229 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4230 putenv(envbuf_Lines);
4231 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4232 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004233 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004234 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004235 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004236# ifdef FEAT_TERMINAL
4237 if (is_terminal)
4238 {
4239 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004240 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004241 putenv(envbuf_Version);
4242 }
4243# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004244# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004245 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4246 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4247 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004248# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004249# endif
4250}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004251
4252 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004253set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004254{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004255 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004256}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004257#endif
4258
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004259#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004260/*
4261 * Open a PTY, with FD for the master and slave side.
4262 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004263 * When successful both file descriptors are stored and the allocated pty name
4264 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004265 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004266 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004267open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004268{
4269 char *tty_name;
4270
Bram Moolenaar59386482019-02-10 22:43:46 +01004271 if (name1 != NULL)
4272 *name1 = NULL;
4273 if (name2 != NULL)
4274 *name2 = NULL;
4275
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004276 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004277 if (*pty_master_fd >= 0)
4278 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004279 // Leaving out O_NOCTTY may lead to waitpid() always returning
4280 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4281 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004282#ifdef O_NOCTTY
4283 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4284#else
4285 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4286#endif
4287 if (*pty_slave_fd < 0)
4288 {
4289 close(*pty_master_fd);
4290 *pty_master_fd = -1;
4291 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004292 else
4293 {
4294 if (name1 != NULL)
4295 *name1 = vim_strsave((char_u *)tty_name);
4296 if (name2 != NULL)
4297 *name2 = vim_strsave((char_u *)tty_name);
4298 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004299 }
4300}
4301#endif
4302
Bram Moolenaarfae42832017-08-01 22:24:26 +02004303/*
4304 * Send SIGINT to a child process if "c" is an interrupt character.
4305 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004306 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004307may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4308{
4309# ifdef SIGINT
4310 if (c == Ctrl_C || c == intr_char)
4311 {
4312# ifdef HAVE_SETSID
4313 kill(-pid, SIGINT);
4314# else
4315 kill(0, SIGINT);
4316# endif
4317 if (wpid > 0)
4318 kill(wpid, SIGINT);
4319 }
4320# endif
4321}
4322
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004323#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004324
Bram Moolenaar0f873732019-12-05 20:28:46 +01004325/*
4326 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4327 * pointers, the last one is NULL.
4328 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4329 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004330 int
4331unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004332 char_u *cmd,
4333 char ***argvp,
4334 char_u **sh_tofree,
4335 char_u **shcf_tofree)
4336{
4337 char **argv = NULL;
4338 int argc;
4339
4340 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004341 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004342 return FAIL;
4343
4344 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4345 return FAIL;
4346 *argvp = argv;
4347
4348 if (cmd != NULL)
4349 {
4350 char_u *s;
4351 char_u *p;
4352
4353 if (extra_shell_arg != NULL)
4354 argv[argc++] = (char *)extra_shell_arg;
4355
Bram Moolenaar0f873732019-12-05 20:28:46 +01004356 // Break 'shellcmdflag' into white separated parts. This doesn't
4357 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004358 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004359 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004360 return FAIL;
4361 s = *shcf_tofree;
4362 p = p_shcf;
4363 while (*p != NUL)
4364 {
4365 argv[argc++] = (char *)s;
4366 while (*p && *p != ' ' && *p != TAB)
4367 *s++ = *p++;
4368 *s++ = NUL;
4369 p = skipwhite(p);
4370 }
4371
4372 argv[argc++] = (char *)cmd;
4373 }
4374 argv[argc] = NULL;
4375 return OK;
4376}
4377#endif
4378
4379#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4380/*
4381 * Use a terminal window to run a shell command in.
4382 */
4383 static int
4384mch_call_shell_terminal(
4385 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004386 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004387{
4388 jobopt_T opt;
4389 char **argv = NULL;
4390 char_u *tofree1 = NULL;
4391 char_u *tofree2 = NULL;
4392 int retval = -1;
4393 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004394 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004395 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004396 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004397
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004398 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004399 goto theend;
4400
4401 init_job_options(&opt);
4402 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4403 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004404 if (buf == NULL)
4405 goto theend;
4406
4407 job = term_getjob(buf->b_term);
4408 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004409
Bram Moolenaar0f873732019-12-05 20:28:46 +01004410 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004411 aucmd_prepbuf(&aco, buf);
4412
4413 clear_oparg(&oa);
4414 while (term_use_loop())
4415 {
4416 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4417 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004418 // If terminal_loop() returns OK we got a key that is handled
4419 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004420 if (terminal_loop(TRUE) == OK)
4421 normal_cmd(&oa, TRUE);
4422 }
4423 else
4424 normal_cmd(&oa, TRUE);
4425 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004426 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004427 ch_log(NULL, "system command finished");
4428
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004429 job_unref(job);
4430
Bram Moolenaar0f873732019-12-05 20:28:46 +01004431 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004432 aucmd_restbuf(&aco);
4433
4434 wait_return(TRUE);
4435 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4436
4437theend:
4438 vim_free(argv);
4439 vim_free(tofree1);
4440 vim_free(tofree2);
4441 return retval;
4442}
4443#endif
4444
4445#ifdef USE_SYSTEM
4446/*
4447 * Use system() to start the shell: simple but slow.
4448 */
4449 static int
4450mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004451 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004452 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453{
4454#ifdef VMS
4455 char *ifn = NULL;
4456 char *ofn = NULL;
4457#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004458 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004459 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004460 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
4462 out_flush();
4463
4464 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004465 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
Bram Moolenaar62b42182010-09-21 22:09:37 +02004467# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004468 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004469 loose_clipboard();
4470# endif
4471
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 if (cmd == NULL)
4473 x = system((char *)p_sh);
4474 else
4475 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004476# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 if (ofn = strchr((char *)cmd, '>'))
4478 *ofn++ = '\0';
4479 if (ifn = strchr((char *)cmd, '<'))
4480 {
4481 char *p;
4482
4483 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004484 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 if (p)
4486 *p = '\0';
4487 }
4488 if (ofn)
4489 x = vms_sys((char *)cmd, ofn, ifn);
4490 else
4491 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004492# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004493 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004495 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 if (newcmd == NULL)
4497 x = 0;
4498 else
4499 {
4500 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4501 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4502 (char *)p_shcf,
4503 (char *)cmd);
4504 x = system((char *)newcmd);
4505 vim_free(newcmd);
4506 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509# ifdef VMS
4510 x = vms_sys_status(x);
4511# endif
4512 if (emsg_silent)
4513 ;
4514 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004515 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 else if (x && !(options & SHELL_SILENT))
4517 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004518 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 msg_outnum((long)x);
4520 msg_putchar('\n');
4521 }
4522
4523 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004524 {
4525 // The shell may have messed with the mode, always set it.
4526 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004527 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529# ifdef FEAT_TITLE
4530 resettitle();
4531# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004532# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4533 restore_clipboard();
4534# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004536}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
Bram Moolenaar0f873732019-12-05 20:28:46 +01004538#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
Bram Moolenaar0f873732019-12-05 20:28:46 +01004540# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4541 // 127, some shells use that already
4542# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
Bram Moolenaar13568252018-03-16 20:46:58 +01004544/*
4545 * Don't use system(), use fork()/exec().
4546 */
4547 static int
4548mch_call_shell_fork(
4549 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004550 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004551{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004552 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004554 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 pid_t wait_pid = 0;
4556# ifdef HAVE_UNION_WAIT
4557 union wait status;
4558# else
4559 int status = -1;
4560# endif
4561 int retval = -1;
4562 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004563 char_u *tofree1 = NULL;
4564 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004566 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004567# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004569# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004570 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 int fd_fromshell[2];
4572 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004573 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
4575 out_flush();
4576 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004577 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004578 if (tmode == TMODE_RAW)
4579 // The shell may have messed with the mode, always set it later.
4580 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004582 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004583 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004584
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004586 * For the GUI, when writing the output into the buffer and when reading
4587 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4588 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004590 if ((options & (SHELL_READ|SHELL_WRITE))
4591# ifdef FEAT_GUI
4592 || (gui.in_use && show_shell_mess)
4593# endif
4594 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004596# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 /*
4598 * Try to open a master pty.
4599 * If this works, open the slave pty.
4600 * If the slave can't be opened, close the master pty.
4601 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004602 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004603 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 /*
4605 * If not opening a pty or it didn't work, try using pipes.
4606 */
4607 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 {
4610 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004611 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 {
4613 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004614 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
4616 close(fd_toshell[0]);
4617 close(fd_toshell[1]);
4618 }
4619 }
4620 if (pipe_error)
4621 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004622 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 out_flush();
4624 }
4625 }
4626 }
4627
Bram Moolenaar0f873732019-12-05 20:28:46 +01004628 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004630 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004631 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004632 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004633 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004635 UNBLOCK_SIGNALS(&curset);
4636
Bram Moolenaar32526b32019-01-19 17:43:09 +01004637 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004638 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004640 || (gui.in_use && show_shell_mess)
4641# endif
4642 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004644# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004645 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
4647 close(pty_master_fd);
4648 close(pty_slave_fd);
4649 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004650 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004651# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 {
4653 close(fd_toshell[0]);
4654 close(fd_toshell[1]);
4655 close(fd_fromshell[0]);
4656 close(fd_fromshell[1]);
4657 }
4658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004660 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004662 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004663 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
Bram Moolenaar819524702018-02-27 19:10:00 +01004665# ifdef FEAT_JOB_CHANNEL
4666 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01004667 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01004668 ch_logfile((char_u *)"", (char_u *)"");
4669# endif
4670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 if (!show_shell_mess || (options & SHELL_EXPAND))
4672 {
4673 int fd;
4674
4675 /*
4676 * Don't want to show any message from the shell. Can't just
4677 * close stdout and stderr though, because some systems will
4678 * break if you try to write to them after that, so we must
4679 * use dup() to replace them with something else -- webb
4680 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4681 * waiting for input.
4682 */
4683 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4684 fclose(stdin);
4685 fclose(stdout);
4686 fclose(stderr);
4687
4688 /*
4689 * If any of these open()'s and dup()'s fail, we just continue
4690 * anyway. It's not fatal, and on most systems it will make
4691 * no difference at all. On a few it will cause the execvp()
4692 * to exit with a non-zero status even when the completion
4693 * could be done, which is nothing too serious. If the open()
4694 * or dup() failed we'd just do the same thing ourselves
4695 * anyway -- webb
4696 */
4697 if (fd >= 0)
4698 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004699 vim_ignored = dup(fd); // To replace stdin (fd 0)
4700 vim_ignored = dup(fd); // To replace stdout (fd 1)
4701 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
Bram Moolenaar0f873732019-12-05 20:28:46 +01004703 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 close(fd);
4705 }
4706 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004707 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004709 || gui.in_use
4710# endif
4711 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
4713
Bram Moolenaardf177f62005-02-22 08:39:57 +00004714# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004715 // Create our own process group, so that the child and all its
4716 // children can be kill()ed. Don't do this when using pipes,
4717 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004718 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004719 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004720 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004721# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004722 // When doing "!xterm&" and 'shell' is bash: the shell
4723 // will exit and send SIGHUP to all processes in its
4724 // group, killing the just started process. Ignore SIGHUP
4725 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004726 signal(SIGHUP, SIG_IGN);
4727# endif
4728 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004729# endif
4730# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004731 if (pty_slave_fd >= 0)
4732 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004733 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004734 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004735 setup_slavepty(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01004737 // Try to become controlling tty (probably doesn't work,
4738 // unless run by root)
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004739 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004741 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004742# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004743 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744
Bram Moolenaara5792f52005-11-23 21:25:05 +00004745 /*
4746 * stderr is only redirected when using the GUI, so that a
4747 * program like gpg can still access the terminal to get a
4748 * passphrase using stderr.
4749 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004750# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 if (pty_master_fd >= 0)
4752 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004753 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
Bram Moolenaar0f873732019-12-05 20:28:46 +01004755 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004757 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004759 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004760 if (gui.in_use)
4761 {
4762 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004763 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
Bram Moolenaar0f873732019-12-05 20:28:46 +01004766 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004771 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 close(fd_toshell[1]);
4773 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004774 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 close(fd_toshell[0]);
4776
Bram Moolenaar0f873732019-12-05 20:28:46 +01004777 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 close(fd_fromshell[0]);
4779 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004780 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 close(fd_fromshell[1]);
4782
Bram Moolenaara5792f52005-11-23 21:25:05 +00004783# ifdef FEAT_GUI
4784 if (gui.in_use)
4785 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004786 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004787 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004788 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004789 }
4790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004793
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 /*
4795 * There is no type cast for the argv, because the type may be
4796 * different on different machines. This may cause a warning
4797 * message with strict compilers, don't worry about it.
4798 * Call _exit() instead of exit() to avoid closing the connection
4799 * to the X server (esp. with GTK, which uses atexit()).
4800 */
4801 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004802 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004804 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
4806 /*
4807 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004808 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 */
4810 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004811 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004812 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004813# ifdef FEAT_JOB_CHANNEL
4814 ++dont_check_job_ended;
4815# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 /*
4817 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004818 * This is also used to pipe stdin/stdout to/from the external
4819 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004821 if ((options & (SHELL_READ|SHELL_WRITE))
4822# ifdef FEAT_GUI
4823 || (gui.in_use && show_shell_mess)
4824# endif
4825 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004827# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004829 int buffer_off = 0; // valid bytes in buffer[]
4830 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4831 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 int len;
4833 int p_more_save;
4834 int old_State;
4835 int c;
4836 int toshell_fd;
4837 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004838 garray_T ga;
4839 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004840# ifdef ELAPSED_FUNC
4841 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
Bram Moolenaardf177f62005-02-22 08:39:57 +00004844# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 if (pty_master_fd >= 0)
4846 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 fromshell_fd = pty_master_fd;
4848 toshell_fd = dup(pty_master_fd);
4849 }
4850 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 {
4853 close(fd_toshell[0]);
4854 close(fd_fromshell[1]);
4855 toshell_fd = fd_toshell[1];
4856 fromshell_fd = fd_fromshell[0];
4857 }
4858
4859 /*
4860 * Write to the child if there are typed characters.
4861 * Read from the child if there are characters available.
4862 * Repeat the reading a few times if more characters are
4863 * available. Need to check for typed keys now and then, but
4864 * not too often (delays when no chars are available).
4865 * This loop is quit if no characters can be read from the pty
4866 * (WaitForChar detected special condition), or there are no
4867 * characters available and the child has exited.
4868 * Only check if the child has exited when there is no more
4869 * output. The child may exit before all the output has
4870 * been printed.
4871 *
4872 * Currently this busy loops!
4873 * This can probably dead-lock when the write blocks!
4874 */
4875 p_more_save = p_more;
4876 p_more = FALSE;
4877 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004878 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004880 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004881 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004882 // Fork a process that will write the lines to the
4883 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004884 if ((wpid = fork()) == -1)
4885 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004886 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004887 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004888 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004889 {
4890 linenr_T lnum = curbuf->b_op_start.lnum;
4891 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004892 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004893 size_t l;
4894
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004895 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004896 for (;;)
4897 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004898 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004899 if (l == 0)
4900 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004901 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004902 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004903 len = write(toshell_fd, "", (size_t)1);
4904 else
4905 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004906 char_u *s = vim_strchr(lp + written, NL);
4907
Bram Moolenaar89d40322006-08-29 15:30:07 +00004908 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004909 s == NULL ? l
4910 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004911 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004912 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004913 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004914 // Finished a line, add a NL, unless this line
4915 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004916 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004917 || (!curbuf->b_p_bin
4918 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004919 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004920 && (lnum !=
4921 curbuf->b_ml.ml_line_count
4922 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004923 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004924 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004925 ++lnum;
4926 if (lnum > curbuf->b_op_end.lnum)
4927 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004928 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00004929 close(toshell_fd);
4930 toshell_fd = -1;
4931 break;
4932 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004933 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004934 written = 0;
4935 }
4936 else if (len > 0)
4937 written += len;
4938 }
4939 _exit(0);
4940 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004941 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00004942 {
4943 close(toshell_fd);
4944 toshell_fd = -1;
4945 }
4946 }
4947
4948 if (options & SHELL_READ)
4949 ga_init2(&ga, 1, BUFLEN);
4950
4951 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004952# ifdef ELAPSED_FUNC
4953 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 for (;;)
4956 {
4957 /*
4958 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004959 * if there are any.
4960 * Don't do this if we are expanding wild cards (would eat
4961 * typeahead).
4962 * Don't do this when filtering and terminal is in cooked
4963 * mode, the shell command will handle the I/O. Avoids
4964 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004965 * Don't get characters when the child has already
4966 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004967 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004968 * while (noread_cnt > 4), avoids that ":r !ls" eats
4969 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 */
4971 len = 0;
4972 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004973 && ((options &
4974 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4975 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004976# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004977 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004978# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004979 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004980 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004981 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004983 if (ta_len == 0)
4984 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004985 // Get extra characters when we don't have any.
4986 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004987 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004988# ifdef ELAPSED_FUNC
4989 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004990# endif
4991 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4992 }
4993 if (ta_len > 0 || len > 0)
4994 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 /*
4996 * For pipes:
4997 * Check for CTRL-C: send interrupt signal to child.
4998 * Check for CTRL-D: EOF, close pipe to child.
4999 */
5000 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 /*
5003 * Send SIGINT to the child's group or all
5004 * processes in our group.
5005 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005006 may_send_sigint(ta_buf[ta_len], pid, wpid);
5007
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 if (pty_master_fd < 0 && toshell_fd >= 0
5009 && ta_buf[ta_len] == Ctrl_D)
5010 {
5011 close(toshell_fd);
5012 toshell_fd = -1;
5013 }
5014 }
5015
Bram Moolenaarf4140482020-02-15 23:06:45 +01005016 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
5018 /*
5019 * For pipes: echo the typed characters.
5020 * For a pty this does not seem to work.
5021 */
5022 if (pty_master_fd < 0)
5023 {
5024 for (i = ta_len; i < ta_len + len; ++i)
5025 {
5026 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5027 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 else if (has_mbyte)
5029 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005030 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031
5032 msg_outtrans_len(ta_buf + i, l);
5033 i += l - 1;
5034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 else
5036 msg_outtrans_len(ta_buf + i, 1);
5037 }
5038 windgoto(msg_row, msg_col);
5039 out_flush();
5040 }
5041
5042 ta_len += len;
5043
5044 /*
5045 * Write the characters to the child, unless EOF has
5046 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005047 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005048 * When writing buffer lines, drop the typed
5049 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005051 if (options & SHELL_WRITE)
5052 ta_len = 0;
5053 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 {
5055 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5056 if (len > 0)
5057 {
5058 ta_len -= len;
5059 mch_memmove(ta_buf, ta_buf + len, ta_len);
5060 }
5061 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
5064
Bram Moolenaardf177f62005-02-22 08:39:57 +00005065 if (got_int)
5066 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005067 // CTRL-C sends a signal to the child, we ignore it
5068 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005069# ifdef HAVE_SETSID
5070 kill(-pid, SIGINT);
5071# else
5072 kill(0, SIGINT);
5073# endif
5074 if (wpid > 0)
5075 kill(wpid, SIGINT);
5076 got_int = FALSE;
5077 }
5078
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 /*
5080 * Check if the child has any characters to be printed.
5081 * Read them and write them to our window. Repeat this as
5082 * long as there is something to do, avoid the 10ms wait
5083 * for mch_inchar(), or sending typeahead characters to
5084 * the external process.
5085 * TODO: This should handle escape sequences, compatible
5086 * to some terminal (vt52?).
5087 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005088 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005089 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005091 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005094 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005096
5097 noread_cnt = 0;
5098 if (options & SHELL_READ)
5099 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005100 // Do NUL -> NL translation, append NL separated
5101 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005102 for (i = 0; i < len; ++i)
5103 {
5104 if (buffer[i] == NL)
5105 append_ga_line(&ga);
5106 else if (buffer[i] == NUL)
5107 ga_append(&ga, NL);
5108 else
5109 ga_append(&ga, buffer[i]);
5110 }
5111 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005112 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
5114 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005115 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116
Bram Moolenaardf177f62005-02-22 08:39:57 +00005117 len += buffer_off;
5118 buffer[len] = NUL;
5119
Bram Moolenaar0f873732019-12-05 20:28:46 +01005120 // Check if the last character in buffer[] is
5121 // incomplete, keep these bytes for the next
5122 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 for (p = buffer; p < buffer + len; p += l)
5124 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005125 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005127 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 else if (MB_BYTE2LEN(*p) != l)
5129 break;
5130 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005131 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005133 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 if (len >= 12)
5135 ++p;
5136 else
5137 {
5138 buffer_off = len;
5139 continue;
5140 }
5141 }
5142 c = *p;
5143 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005144 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (p < buffer + len)
5146 {
5147 *p = c;
5148 buffer_off = (buffer + len) - p;
5149 mch_memmove(buffer, p, buffer_off);
5150 continue;
5151 }
5152 buffer_off = 0;
5153 }
5154 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 {
5156 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005157 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
5159
5160 windgoto(msg_row, msg_col);
5161 cursor_on();
5162 out_flush();
5163 if (got_int)
5164 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005165
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005166# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005167 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005168 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005169 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005170
Bram Moolenaar0f873732019-12-05 20:28:46 +01005171 // Avoid that we keep looping here without
5172 // checking for a CTRL-C for a long time. Don't
5173 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005174 if (msec > 2000)
5175 {
5176 noread_cnt = 5;
5177 break;
5178 }
5179 }
5180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182
Bram Moolenaar0f873732019-12-05 20:28:46 +01005183 // If we already detected the child has finished, continue
5184 // reading output for a short while. Some text may be
5185 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005186 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005187 {
5188 if (noread_cnt < 5)
5189 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005190 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005191 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005192
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 /*
5194 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005195 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005197# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005198 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005199# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005201# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5203 || (wait_pid == pid && WIFEXITED(status)))
5204 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005205 // Don't break the loop yet, try reading more
5206 // characters from "fromshell_fd" first. When using
5207 // pipes there might still be something to read and
5208 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005211 else
5212 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005213
Bram Moolenaar95a51352013-03-21 22:53:50 +01005214# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005215 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005216 clip_update();
5217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219finished:
5220 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005221 if (options & SHELL_READ)
5222 {
5223 if (ga.ga_len > 0)
5224 {
5225 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005226 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005227 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005228 }
5229 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005230 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005231 ga_clear(&ga);
5232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 /*
5235 * Give all typeahead that wasn't used back to ui_inchar().
5236 */
5237 if (ta_len)
5238 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 State = old_State;
5240 if (toshell_fd >= 0)
5241 close(toshell_fd);
5242 close(fromshell_fd);
5243 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005244# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005245 else
5246 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005247 long delay_msec = 1;
5248
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005249 /*
5250 * Similar to the loop above, but only handle X events, no
5251 * I/O.
5252 */
5253 for (;;)
5254 {
5255 if (got_int)
5256 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005257 // CTRL-C sends a signal to the child, we ignore it
5258 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005259# ifdef HAVE_SETSID
5260 kill(-pid, SIGINT);
5261# else
5262 kill(0, SIGINT);
5263# endif
5264 got_int = FALSE;
5265 }
5266# ifdef __NeXT__
5267 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5268# else
5269 wait_pid = waitpid(pid, &status, WNOHANG);
5270# endif
5271 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5272 || (wait_pid == pid && WIFEXITED(status)))
5273 {
5274 wait_pid = pid;
5275 break;
5276 }
5277
Bram Moolenaar0f873732019-12-05 20:28:46 +01005278 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005279 clip_update();
5280
Bram Moolenaar0f873732019-12-05 20:28:46 +01005281 // Wait for 1 to 10 msec. 1 is faster but gives the child
5282 // less time.
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005283 mch_delay(delay_msec, TRUE);
5284 if (++delay_msec > 10)
5285 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005286 }
5287 }
5288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289
5290 /*
5291 * Wait until our child has exited.
5292 * Ignore wait() returning pids of other children and returning
5293 * because of some signal like SIGWINCH.
5294 * Don't wait if wait_pid was already set above, indicating the
5295 * child already exited.
5296 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005297 if (wait_pid != pid)
5298 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
Bram Moolenaar624891f2010-10-13 16:22:09 +02005300# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005301 // Close slave side of pty. Only do this after the child has
5302 // exited, otherwise the child may hang when it tries to write on
5303 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005304 if (pty_master_fd >= 0)
5305 close(pty_slave_fd);
5306# endif
5307
Bram Moolenaar0f873732019-12-05 20:28:46 +01005308 // Make sure the child that writes to the external program is
5309 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005310 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005311 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005312 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005313 wait4pid(wpid, NULL);
5314 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005315
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005316# ifdef FEAT_JOB_CHANNEL
5317 --dont_check_job_ended;
5318# endif
5319
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 /*
5321 * Set to raw mode right now, otherwise a CTRL-C after
5322 * catch_signals() will kill Vim.
5323 */
5324 if (tmode == TMODE_RAW)
5325 settmode(TMODE_RAW);
5326 did_settmode = TRUE;
5327 set_signals();
5328
5329 if (WIFEXITED(status))
5330 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005331 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005333 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 {
5335 if (retval == EXEC_FAILED)
5336 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005337 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 msg_outtrans(p_sh);
5339 msg_putchar('\n');
5340 }
5341 else if (!(options & SHELL_SILENT))
5342 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005343 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 msg_outnum((long)retval);
5345 msg_putchar('\n');
5346 }
5347 }
5348 }
5349 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005350 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
5352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353
5354error:
5355 if (!did_settmode)
5356 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005357 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358# ifdef FEAT_TITLE
5359 resettitle();
5360# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005361 vim_free(argv);
5362 vim_free(tofree1);
5363 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
5365 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005366}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005367#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005368
5369 int
5370mch_call_shell(
5371 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005372 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005373{
5374#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5375 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5376 return mch_call_shell_terminal(cmd, options);
5377#endif
5378#ifdef USE_SYSTEM
5379 return mch_call_shell_system(cmd, options);
5380#else
5381 return mch_call_shell_fork(cmd, options);
5382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383}
5384
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005385#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005386 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005387mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005388{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005389 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005390 int fd_in[2] = {-1, -1}; // for stdin
5391 int fd_out[2] = {-1, -1}; // for stdout
5392 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005393 int pty_master_fd = -1;
5394 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005395 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005396 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5397 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5398 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005399 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005400 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5401 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005402 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005403 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005404 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005405
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005406 if (use_out_for_err && use_null_for_out)
5407 use_null_for_err = TRUE;
5408
Bram Moolenaar0f873732019-12-05 20:28:46 +01005409 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005410 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005411
Bram Moolenaarb2412082017-08-20 18:09:14 +02005412 if (options->jo_pty
5413 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005414 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005415 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005416 open_pty(&pty_master_fd, &pty_slave_fd,
5417 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005418
Bram Moolenaar0f873732019-12-05 20:28:46 +01005419 // TODO: without the channel feature connect the child to /dev/null?
5420 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005421 if (use_file_for_in)
5422 {
5423 char_u *fname = options->jo_io_name[PART_IN];
5424
5425 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5426 if (fd_in[0] < 0)
5427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005428 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005429 goto failed;
5430 }
5431 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005432 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005433 // When writing buffer lines to the input don't use the pty, so that
5434 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005435 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5436 && pipe(fd_in) < 0)
5437 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005438
5439 if (use_file_for_out)
5440 {
5441 char_u *fname = options->jo_io_name[PART_OUT];
5442
5443 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5444 if (fd_out[1] < 0)
5445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005446 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005447 goto failed;
5448 }
5449 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005450 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005451 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005452
5453 if (use_file_for_err)
5454 {
5455 char_u *fname = options->jo_io_name[PART_ERR];
5456
5457 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5458 if (fd_err[1] < 0)
5459 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005460 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005461 goto failed;
5462 }
5463 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005464 else if (!use_out_for_err && !use_null_for_err
5465 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005466 goto failed;
5467
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005468 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5469 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005470 if (options->jo_set & JO_CHANNEL)
5471 {
5472 channel = options->jo_channel;
5473 if (channel != NULL)
5474 ++channel->ch_refcount;
5475 }
5476 else
5477 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005478 if (channel == NULL)
5479 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005480 if (job->jv_tty_out != NULL)
5481 ch_log(channel, "using pty %s on fd %d",
5482 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005483 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005484
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005485 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005486 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005487 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005488 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005489 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005490 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005491 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005492 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005493 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005494 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005495 int null_fd = -1;
5496 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005497
Bram Moolenaar0f873732019-12-05 20:28:46 +01005498 // child
5499 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005500 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005501
Bram Moolenaar819524702018-02-27 19:10:00 +01005502# ifdef FEAT_JOB_CHANNEL
5503 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005504 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005505 ch_logfile((char_u *)"", (char_u *)"");
5506# endif
5507
Bram Moolenaar835dc632016-02-07 14:27:38 +01005508# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005509 // Create our own process group, so that the child and all its
5510 // children can be kill()ed. Don't do this when using pipes,
5511 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005512 (void)setsid();
5513# endif
5514
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005515# ifdef FEAT_TERMINAL
5516 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005517 {
5518 char *term = (char *)T_NAME;
5519
5520#ifdef FEAT_GUI
5521 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005522 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005523 term = getenv("TERM");
5524#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005525 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005526 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005527 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005528 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005529 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005530 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005531 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005532 else if (t_colors > 16)
5533 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005534 else
5535 term = "xterm";
5536 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005537 set_child_environment(
5538 (long)options->jo_term_rows,
5539 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005540 term,
5541 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005542 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005543 else
5544# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005545 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005546
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005547 if (options->jo_env != NULL)
5548 {
5549 dict_T *dict = options->jo_env;
5550 hashitem_T *hi;
5551 int todo = (int)dict->dv_hashtab.ht_used;
5552
5553 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5554 if (!HASHITEM_EMPTY(hi))
5555 {
5556 typval_T *item = &dict_lookup(hi)->di_tv;
5557
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005558 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005559 --todo;
5560 }
5561 }
5562
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005563 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005564 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005565 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005566 if (null_fd < 0)
5567 {
5568 perror("opening /dev/null failed");
5569 _exit(OPEN_NULL_FAILED);
5570 }
5571 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005572
Bram Moolenaar223896d2017-08-02 22:33:28 +02005573 if (pty_slave_fd >= 0)
5574 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005575 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005576 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005577# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005578 // Try to become controlling tty (probably doesn't work,
5579 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005580 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5581# endif
5582 }
5583
Bram Moolenaar0f873732019-12-05 20:28:46 +01005584 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005585 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005586 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005587 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005588 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005589 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005590 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005591 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005592
Bram Moolenaar0f873732019-12-05 20:28:46 +01005593 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005594 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005595 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005596 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005597 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005598 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005599 }
5600 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005601 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005602 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005603 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005604 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005605 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005606
Bram Moolenaar0f873732019-12-05 20:28:46 +01005607 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005608 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005609 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005610 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005611 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005612 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005613 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005614 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005615
5616 if (fd_in[0] >= 0)
5617 close(fd_in[0]);
5618 if (fd_in[1] >= 0)
5619 close(fd_in[1]);
5620 if (fd_out[0] >= 0)
5621 close(fd_out[0]);
5622 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005623 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005624 if (fd_err[0] >= 0)
5625 close(fd_err[0]);
5626 if (fd_err[1] >= 0)
5627 close(fd_err[1]);
5628 if (pty_master_fd >= 0)
5629 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005630 close(pty_master_fd); // not used in the child
5631 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005632 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005633
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005634 if (null_fd >= 0)
5635 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005636
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005637 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5638 _exit(EXEC_FAILED);
5639
Bram Moolenaar0f873732019-12-05 20:28:46 +01005640 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005641 execvp(argv[0], argv);
5642
Bram Moolenaar4694a172016-04-21 14:05:23 +02005643 if (stderr_works)
5644 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005645# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005646 // calling free_all_mem() here causes problems. Ignore valgrind
5647 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005648# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005649 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005650 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005651
Bram Moolenaar0f873732019-12-05 20:28:46 +01005652 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005653 UNBLOCK_SIGNALS(&curset);
5654
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005655 job->jv_pid = pid;
5656 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005657 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005658
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005659 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005660 close(pty_slave_fd); // not used in the parent
5661 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005662 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005663 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005664 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005665 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005666 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005667 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005668 if (channel != NULL)
5669 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005670 int in_fd = INVALID_FD;
5671 int out_fd = INVALID_FD;
5672 int err_fd = INVALID_FD;
5673
5674 if (!(use_file_for_in || use_null_for_in))
5675 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5676
5677 if (!(use_file_for_out || use_null_for_out))
5678 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5679
5680 // When using pty_master_fd only set it for stdout, do not duplicate
5681 // it for stderr, it only needs to be read once.
5682 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5683 {
5684 if (fd_err[0] >= 0)
5685 err_fd = fd_err[0];
5686 else if (out_fd != pty_master_fd)
5687 err_fd = pty_master_fd;
5688 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005689
5690 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005691 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005692 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005693 else
5694 {
5695 if (fd_in[1] >= 0)
5696 close(fd_in[1]);
5697 if (fd_out[0] >= 0)
5698 close(fd_out[0]);
5699 if (fd_err[0] >= 0)
5700 close(fd_err[0]);
5701 if (pty_master_fd >= 0)
5702 close(pty_master_fd);
5703 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005704
Bram Moolenaar0f873732019-12-05 20:28:46 +01005705 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005706 return;
5707
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005708failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005709 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005710 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005711 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005712 if (fd_in[1] >= 0)
5713 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005714 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005715 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005716 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005717 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005718 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005719 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005720 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005721 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005722 if (pty_master_fd >= 0)
5723 close(pty_master_fd);
5724 if (pty_slave_fd >= 0)
5725 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005726}
5727
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005728 static char_u *
5729get_signal_name(int sig)
5730{
5731 int i;
5732 char_u numbuf[NUMBUFLEN];
5733
5734 if (sig == SIGKILL)
5735 return vim_strsave((char_u *)"kill");
5736
5737 for (i = 0; signal_info[i].sig != -1; i++)
5738 if (sig == signal_info[i].sig)
5739 return strlow_save((char_u *)signal_info[i].name);
5740
5741 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5742 return vim_strsave(numbuf);
5743}
5744
Bram Moolenaar835dc632016-02-07 14:27:38 +01005745 char *
5746mch_job_status(job_T *job)
5747{
5748# ifdef HAVE_UNION_WAIT
5749 union wait status;
5750# else
5751 int status = -1;
5752# endif
5753 pid_t wait_pid = 0;
5754
5755# ifdef __NeXT__
5756 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5757# else
5758 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5759# endif
5760 if (wait_pid == -1)
5761 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005762 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005763 if (job->jv_status < JOB_ENDED)
5764 ch_log(job->jv_channel, "Job no longer exists: %s",
5765 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005766 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005767 }
5768 if (wait_pid == 0)
5769 return "run";
5770 if (WIFEXITED(status))
5771 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005772 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005773 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005774 if (job->jv_status < JOB_ENDED)
5775 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005776 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005777 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005778 if (WIFSIGNALED(status))
5779 {
5780 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005781 job->jv_termsig = get_signal_name(WTERMSIG(status));
5782 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5783 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5784 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005785 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005786 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005787 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005788
5789return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005790 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005791 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005792 return "dead";
5793}
5794
5795 job_T *
5796mch_detect_ended_job(job_T *job_list)
5797{
5798# ifdef HAVE_UNION_WAIT
5799 union wait status;
5800# else
5801 int status = -1;
5802# endif
5803 pid_t wait_pid = 0;
5804 job_T *job;
5805
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005806# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005807 // Do not do this when waiting for a shell command to finish, we would get
5808 // the exit value here (and discard it), the exit value obtained there
5809 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005810 if (dont_check_job_ended > 0)
5811 return NULL;
5812# endif
5813
Bram Moolenaar97792de2016-10-15 18:36:49 +02005814# ifdef __NeXT__
5815 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5816# else
5817 wait_pid = waitpid(-1, &status, WNOHANG);
5818# endif
5819 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005820 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005821 return NULL;
5822 for (job = job_list; job != NULL; job = job->jv_next)
5823 {
5824 if (job->jv_pid == wait_pid)
5825 {
5826 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005827 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005828 job->jv_exitval = WEXITSTATUS(status);
5829 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005830 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005831 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005832 job->jv_termsig = get_signal_name(WTERMSIG(status));
5833 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005834 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005835 {
5836 ch_log(job->jv_channel, "Job ended");
5837 job->jv_status = JOB_ENDED;
5838 }
5839 return job;
5840 }
5841 }
5842 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005843}
5844
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005845/*
5846 * Send a (deadly) signal to "job".
5847 * Return FAIL if "how" is not a valid name.
5848 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005849 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005850mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005851{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005852 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005853
Bram Moolenaar923d9262016-02-25 20:56:01 +01005854 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005855 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005856 else if (STRCMP(how, "hup") == 0)
5857 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005858 else if (STRCMP(how, "quit") == 0)
5859 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005860 else if (STRCMP(how, "int") == 0)
5861 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005862 else if (STRCMP(how, "kill") == 0)
5863 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005864#ifdef SIGWINCH
5865 else if (STRCMP(how, "winch") == 0)
5866 sig = SIGWINCH;
5867#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005868 else if (isdigit(*how))
5869 sig = atoi((char *)how);
5870 else
5871 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005872
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005873 // Never kill ourselves!
5874 if (job->jv_pid != 0)
5875 {
5876 // TODO: have an option to only kill the process, not the group?
5877 kill(-job->jv_pid, sig);
5878 kill(job->jv_pid, sig);
5879 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005880
Bram Moolenaar835dc632016-02-07 14:27:38 +01005881 return OK;
5882}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005883
5884/*
5885 * Clear the data related to "job".
5886 */
5887 void
5888mch_clear_job(job_T *job)
5889{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005890 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005891# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005892 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005893# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005894 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005895# endif
5896}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005897#endif
5898
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005899#if defined(FEAT_TERMINAL) || defined(PROTO)
5900 int
5901mch_create_pty_channel(job_T *job, jobopt_T *options)
5902{
5903 int pty_master_fd = -1;
5904 int pty_slave_fd = -1;
5905 channel_T *channel;
5906
Bram Moolenaar59386482019-02-10 22:43:46 +01005907 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005908 close(pty_slave_fd);
5909
5910 channel = add_channel();
5911 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005912 {
5913 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005914 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005915 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005916 if (job->jv_tty_out != NULL)
5917 ch_log(channel, "using pty %s on fd %d",
5918 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005919 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005920 channel->ch_keep_open = TRUE;
5921
Bram Moolenaar0f873732019-12-05 20:28:46 +01005922 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5923 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005924 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005925 channel_set_job(channel, job, options);
5926 return OK;
5927}
5928#endif
5929
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930/*
5931 * Check for CTRL-C typed by reading all available characters.
5932 * In cooked mode we should get SIGINT, no need to check.
5933 */
5934 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005935mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
Bram Moolenaar26e86442020-05-17 14:06:16 +02005937 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005938 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 fill_input_buf(FALSE);
5940}
5941
5942/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005943 * Wait "msec" msec until a character is available from the mouse, keyboard,
5944 * from inbuf[].
5945 * "msec" == -1 will block forever.
5946 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005947 * When "ignore_input" is TRUE even check for pending input when input is
5948 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005949 * "interrupted" (if not NULL) is set to TRUE when no character is available
5950 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005951 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005952 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 */
5954 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005955WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005957#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005958 return ui_wait_for_chars_or_timer(
5959 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005960#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005961 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005962#endif
5963}
5964
5965/*
5966 * Wait "msec" msec until a character is available from the mouse or keyboard
5967 * or from inbuf[].
5968 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005969 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005970 * "interrupted" (if not NULL) is set to TRUE when no character is available
5971 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005972 * When a GUI is being used, this will never get called -- webb
5973 */
5974 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005975WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005976{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977#ifdef FEAT_MOUSE_GPM
5978 int gpm_process_wanted;
5979#endif
5980#ifdef FEAT_XCLIPBOARD
5981 int rest;
5982#endif
5983 int avail;
5984
Bram Moolenaar0f873732019-12-05 20:28:46 +01005985 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 return 1;
5987
5988#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005989 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 if (WantQueryMouse)
5991 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005992 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02005993 if (!no_query_mouse_for_testing)
5994 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 }
5996#endif
5997
5998 /*
5999 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6000 * events. This is a bit complicated, because they might both be defined.
6001 */
6002#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6003# ifdef FEAT_XCLIPBOARD
6004 rest = 0;
6005 if (do_xterm_trace())
6006 rest = msec;
6007# endif
6008 do
6009 {
6010# ifdef FEAT_XCLIPBOARD
6011 if (rest != 0)
6012 {
6013 msec = XT_TRACE_DELAY;
6014 if (rest >= 0 && rest < XT_TRACE_DELAY)
6015 msec = rest;
6016 if (rest >= 0)
6017 rest -= msec;
6018 }
6019# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006020# ifdef FEAT_SOUND_CANBERRA
6021 // Invoke any pending sound callbacks.
6022 if (has_sound_callback_in_queue())
6023 invoke_sound_callback();
6024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025# ifdef FEAT_MOUSE_GPM
6026 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006027 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006028 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006029 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006031 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006033# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006035 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 return 1;
6037# ifdef FEAT_XCLIPBOARD
6038 if (rest == 0 || !do_xterm_trace())
6039# endif
6040 break;
6041 }
6042 }
6043 while (FALSE
6044# ifdef FEAT_MOUSE_GPM
6045 || (gpm_process_wanted && mch_gpm_process() == 0)
6046# endif
6047# ifdef FEAT_XCLIPBOARD
6048 || (!avail && rest != 0)
6049# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006050 )
6051 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052
6053#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006054 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055#endif
6056 return avail;
6057}
6058
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006059#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060/*
6061 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006062 * "msec" == 0 will check for characters once.
6063 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006066 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006067 * "interrupted" (if not NULL) is set to TRUE when no character is available
6068 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006070 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006071RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
6073 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006074 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006075#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 static int busy = FALSE;
6077
Bram Moolenaar0f873732019-12-05 20:28:46 +01006078 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079# define MAY_LOOP
6080
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006081# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006082 // Remember at what time we started, so that we know how much longer we
6083 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006084 long start_msec = msec;
6085 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006087 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006088 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089# endif
6090
Bram Moolenaar0f873732019-12-05 20:28:46 +01006091 // Handle being called recursively. This may happen for the session
6092 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (busy)
6094 return 0;
6095#endif
6096
6097#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006098 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099#endif
6100 {
6101#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006102 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006103# ifdef FEAT_MZSCHEME
6104 int mzquantum_used = FALSE;
6105# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106#endif
6107#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006108 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006109 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 int nfd;
6111# ifdef FEAT_XCLIPBOARD
6112 int xterm_idx = -1;
6113# endif
6114# ifdef FEAT_MOUSE_GPM
6115 int gpm_idx = -1;
6116# endif
6117# ifdef USE_XSMP
6118 int xsmp_idx = -1;
6119# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006120 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006122# ifdef FEAT_MZSCHEME
6123 mzvim_check_threads();
6124 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6125 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006126 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006127 mzquantum_used = TRUE;
6128 }
6129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 fds[0].fd = fd;
6131 fds[0].events = POLLIN;
6132 nfd = 1;
6133
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006135 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 if (xterm_Shell != (Widget)0)
6137 {
6138 xterm_idx = nfd;
6139 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6140 fds[nfd].events = POLLIN;
6141 nfd++;
6142 }
6143# endif
6144# ifdef FEAT_MOUSE_GPM
6145 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6146 {
6147 gpm_idx = nfd;
6148 fds[nfd].fd = gpm_fd;
6149 fds[nfd].events = POLLIN;
6150 nfd++;
6151 }
6152# endif
6153# ifdef USE_XSMP
6154 if (xsmp_icefd != -1)
6155 {
6156 xsmp_idx = nfd;
6157 fds[nfd].fd = xsmp_icefd;
6158 fds[nfd].events = POLLIN;
6159 nfd++;
6160 }
6161# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006162#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006163 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006164#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006165 if (interrupted != NULL)
6166 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006168 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006169
6170 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006171 if (result == 0 && interrupted != NULL && ret > 0)
6172 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006173
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006174# ifdef FEAT_MZSCHEME
6175 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006176 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006177 finished = FALSE;
6178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180# ifdef FEAT_XCLIPBOARD
6181 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6182 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006183 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006185 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 finished = FALSE;
6187 }
6188# endif
6189# ifdef FEAT_MOUSE_GPM
6190 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192# endif
6193# ifdef USE_XSMP
6194 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6195 {
6196 if (fds[xsmp_idx].revents & POLLIN)
6197 {
6198 busy = TRUE;
6199 xsmp_handle_requests();
6200 busy = FALSE;
6201 }
6202 else if (fds[xsmp_idx].revents & POLLHUP)
6203 {
6204 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006205 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 xsmp_close();
6207 }
6208 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006209 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 }
6211# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006212#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006213 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006214 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006215 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
Bram Moolenaar0f873732019-12-05 20:28:46 +01006218#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219
6220 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006221 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006222 // These are static because they can take 8 Kbyte each and cause the
6223 // signal stack to run out with -O3.
6224 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006226 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006228# ifdef FEAT_MZSCHEME
6229 mzvim_check_threads();
6230 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6231 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006232 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006233 mzquantum_used = TRUE;
6234 }
6235# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006237 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006239 tv.tv_sec = towait / 1000;
6240 tv.tv_usec = (towait % 1000) * (1000000/1000);
6241 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006243 else
6244 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
6246 /*
6247 * Select on ready for reading and exceptional condition (end of file).
6248 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006249select_eintr:
6250 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006251 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 FD_ZERO(&efds);
6253 FD_SET(fd, &rfds);
6254# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006255 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 FD_SET(fd, &efds);
6257# endif
6258 maxfd = fd;
6259
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006261 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 if (xterm_Shell != (Widget)0)
6263 {
6264 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6265 if (maxfd < ConnectionNumber(xterm_dpy))
6266 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006267
Bram Moolenaar0f873732019-12-05 20:28:46 +01006268 // An event may have already been read but not handled. In
6269 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006270 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 }
6272# endif
6273# ifdef FEAT_MOUSE_GPM
6274 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6275 {
6276 FD_SET(gpm_fd, &rfds);
6277 FD_SET(gpm_fd, &efds);
6278 if (maxfd < gpm_fd)
6279 maxfd = gpm_fd;
6280 }
6281# endif
6282# ifdef USE_XSMP
6283 if (xsmp_icefd != -1)
6284 {
6285 FD_SET(xsmp_icefd, &rfds);
6286 FD_SET(xsmp_icefd, &efds);
6287 if (maxfd < xsmp_icefd)
6288 maxfd = xsmp_icefd;
6289 }
6290# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006291# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006292 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006293# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006294 if (interrupted != NULL)
6295 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296
Bram Moolenaar643b6142018-09-12 20:29:09 +02006297 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6298 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006299 result = ret > 0 && FD_ISSET(fd, &rfds);
6300 if (result)
6301 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006302 else if (interrupted != NULL && ret > 0)
6303 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006304
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006305# ifdef EINTR
6306 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006307 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006308 // Check whether window has been resized, EINTR may be caused by
6309 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006310 if (do_resize)
6311 handle_resize();
6312
Bram Moolenaar0f873732019-12-05 20:28:46 +01006313 // Interrupted by a signal, need to try again. We ignore msec
6314 // here, because we do want to check even after a timeout if
6315 // characters are available. Needed for reading output of an
6316 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006317 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006318 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006319# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006320# ifdef __TANDEM
6321 if (ret == -1 && errno == ENOTSUP)
6322 {
6323 FD_ZERO(&rfds);
6324 FD_ZERO(&efds);
6325 ret = 0;
6326 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006327# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006328# ifdef FEAT_MZSCHEME
6329 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006330 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006331 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332# endif
6333
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334# ifdef FEAT_XCLIPBOARD
6335 if (ret > 0 && xterm_Shell != (Widget)0
6336 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6337 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006338 xterm_update(); // Maybe we should hand out clipboard
6339 // continue looping when we only got the X event and the input
6340 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 if (--ret == 0 && !input_available())
6342 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006343 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 finished = FALSE;
6345 }
6346 }
6347# endif
6348# ifdef FEAT_MOUSE_GPM
6349 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6350 {
6351 if (FD_ISSET(gpm_fd, &efds))
6352 gpm_close();
6353 else if (FD_ISSET(gpm_fd, &rfds))
6354 *check_for_gpm = 1;
6355 }
6356# endif
6357# ifdef USE_XSMP
6358 if (ret > 0 && xsmp_icefd != -1)
6359 {
6360 if (FD_ISSET(xsmp_icefd, &efds))
6361 {
6362 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006363 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 xsmp_close();
6365 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006366 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 }
6368 else if (FD_ISSET(xsmp_icefd, &rfds))
6369 {
6370 busy = TRUE;
6371 xsmp_handle_requests();
6372 busy = FALSE;
6373 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006374 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 }
6376 }
6377# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006378#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006379 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006380 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006381 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383
Bram Moolenaar0f873732019-12-05 20:28:46 +01006384#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
6386#ifdef MAY_LOOP
6387 if (finished || msec == 0)
6388 break;
6389
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006390# ifdef FEAT_CLIENTSERVER
6391 if (server_waiting())
6392 break;
6393# endif
6394
Bram Moolenaar0f873732019-12-05 20:28:46 +01006395 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 if (msec > 0)
6397 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006398# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006399 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006400 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006402 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 msec = msec / 2;
6404# endif
6405 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006406 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 }
6408#endif
6409 }
6410
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006411 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412}
6413
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006415 * Expand a path into all matching files and/or directories. Handles "*",
6416 * "?", "[a-z]", "**", etc.
6417 * "path" has backslashes before chars that are not to be expanded.
6418 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 */
6420 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006421mch_expandpath(
6422 garray_T *gap,
6423 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006424 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425{
Bram Moolenaar02743632005-07-25 20:42:36 +00006426 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428
6429/*
6430 * mch_expand_wildcards() - this code does wild-card pattern matching using
6431 * the shell
6432 *
6433 * return OK for success, FAIL for error (you may lose some memory) and put
6434 * an error message in *file.
6435 *
6436 * num_pat is number of input patterns
6437 * pat is array of pointers to input patterns
6438 * num_file is pointer to number of matched file names
6439 * file is pointer to array of pointers to matched file names
6440 */
6441
6442#ifndef SEEK_SET
6443# define SEEK_SET 0
6444#endif
6445#ifndef SEEK_END
6446# define SEEK_END 2
6447#endif
6448
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006449#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006450
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006452mch_expand_wildcards(
6453 int num_pat,
6454 char_u **pat,
6455 int *num_file,
6456 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006457 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458{
6459 int i;
6460 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006461 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 char_u *p;
6463 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464
Bram Moolenaarc7247912008-01-13 12:54:11 +00006465 /*
6466 * This is the non-OS/2 implementation (really Unix).
6467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 int j;
6469 char_u *tempname;
6470 char_u *command;
6471 FILE *fd;
6472 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006473#define STYLE_ECHO 0 // use "echo", the default
6474#define STYLE_GLOB 1 // use "glob", for csh
6475#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6476#define STYLE_PRINT 3 // use "print -N", for zsh
6477#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6478 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 int shell_style = STYLE_ECHO;
6480 int check_spaces;
6481 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006482 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006483 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006484 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
Bram Moolenaar0f873732019-12-05 20:28:46 +01006486 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 *file = NULL;
6488
6489 /*
6490 * If there are no wildcards, just copy the names to allocated memory.
6491 * Saves a lot of time, because we don't have to start a new shell.
6492 */
6493 if (!have_wildcard(num_pat, pat))
6494 return save_patterns(num_pat, pat, num_file, file);
6495
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006496# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006497 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006498 if (sandbox != 0 && check_secure())
6499 return FAIL;
6500# endif
6501
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 /*
6503 * Don't allow the use of backticks in secure and restricted mode.
6504 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006505 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 for (i = 0; i < num_pat; ++i)
6507 if (vim_strchr(pat[i], '`') != NULL
6508 && (check_restricted() || check_secure()))
6509 return FAIL;
6510
6511 /*
6512 * get a name for the temp file
6513 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006514 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006516 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 return FAIL;
6518 }
6519
6520 /*
6521 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006522 * file.
6523 * STYLE_BT: NL separated
6524 * If expanding `cmd` execute it directly.
6525 * STYLE_GLOB: NUL separated
6526 * If we use *csh, "glob" will work better than "echo".
6527 * STYLE_PRINT: NL or NUL separated
6528 * If we use *zsh, "print -N" will work better than "glob".
6529 * STYLE_VIMGLOB: NL separated
6530 * If we use *sh*, we define "vimglob()".
6531 * STYLE_ECHO: space separated.
6532 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 */
6534 if (num_pat == 1 && *pat[0] == '`'
6535 && (len = STRLEN(pat[0])) > 2
6536 && *(pat[0] + len - 1) == '`')
6537 shell_style = STYLE_BT;
6538 else if ((len = STRLEN(p_sh)) >= 3)
6539 {
6540 if (STRCMP(p_sh + len - 3, "csh") == 0)
6541 shell_style = STYLE_GLOB;
6542 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6543 shell_style = STYLE_PRINT;
6544 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006545 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6546 "sh") != NULL)
6547 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
Bram Moolenaar0f873732019-12-05 20:28:46 +01006549 // Compute the length of the command. We need 2 extra bytes: for the
6550 // optional '&' and for the NUL.
6551 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006553 if (shell_style == STYLE_VIMGLOB)
6554 len += STRLEN(sh_vimglob_func);
6555
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006556 for (i = 0; i < num_pat; ++i)
6557 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006558 // Count the length of the patterns in the same way as they are put in
6559 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006560#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006561 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006562#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006563 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006564 for (j = 0; pat[i][j] != NUL; ++j)
6565 {
6566 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006567 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006568 ++len;
6569 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006570#endif
6571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 command = alloc(len);
6573 if (command == NULL)
6574 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006575 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 vim_free(tempname);
6577 return FAIL;
6578 }
6579
6580 /*
6581 * Build the shell command:
6582 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6583 * recognizes this).
6584 * - Add the shell command to print the expanded names.
6585 * - Add the temp file name.
6586 * - Add the file name patterns.
6587 */
6588 if (shell_style == STYLE_BT)
6589 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006590 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006591 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006592 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006594 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006595 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006597 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006599 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 *p = ' ';
6601 }
6602 STRCAT(command, ">");
6603 }
6604 else
6605 {
6606 if (flags & EW_NOTFOUND)
6607 STRCPY(command, "set nonomatch; ");
6608 else
6609 STRCPY(command, "unset nonomatch; ");
6610 if (shell_style == STYLE_GLOB)
6611 STRCAT(command, "glob >");
6612 else if (shell_style == STYLE_PRINT)
6613 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006614 else if (shell_style == STYLE_VIMGLOB)
6615 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 else
6617 STRCAT(command, "echo >");
6618 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006621
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 if (shell_style != STYLE_BT)
6623 for (i = 0; i < num_pat; ++i)
6624 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006625 // When using system() always add extra quotes, because the shell
6626 // is started twice. Otherwise put a backslash before special
6627 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628#ifdef USE_SYSTEM
6629 STRCAT(command, " \"");
6630 STRCAT(command, pat[i]);
6631 STRCAT(command, "\"");
6632#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006633 int intick = FALSE;
6634
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 p = command + STRLEN(command);
6636 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006637 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006638 {
6639 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006640 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006641 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6642 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006643 // Remove a backslash, take char literally. But keep
6644 // backslash inside backticks, before a special character
6645 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006646 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006647 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6648 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006649 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006650 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006651 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006652 else if (!intick
6653 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6654 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006655 // Put a backslash before a special character, but not
6656 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6657 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006658 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006659
Bram Moolenaar0f873732019-12-05 20:28:46 +01006660 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006661 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 *p = NUL;
6664#endif
6665 }
6666 if (flags & EW_SILENT)
6667 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006668 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006669 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670
6671 /*
6672 * Using zsh -G: If a pattern has no matches, it is just deleted from
6673 * the argument list, otherwise zsh gives an error message and doesn't
6674 * expand any other pattern.
6675 */
6676 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006677 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679 /*
6680 * If we use -f then shell variables set in .cshrc won't get expanded.
6681 * vi can do it, so we will too, but it is only necessary if there is a "$"
6682 * in one of the patterns, otherwise we can still use the fast option.
6683 */
6684 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006685 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686
6687 /*
6688 * execute the shell command
6689 */
6690 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6691
Bram Moolenaar0f873732019-12-05 20:28:46 +01006692 // When running in the background, give it some time to create the temp
6693 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006694 if (ampersand)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 mch_delay(10L, TRUE);
6696
Bram Moolenaar0f873732019-12-05 20:28:46 +01006697 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 show_shell_mess = TRUE;
6699 vim_free(command);
6700
Bram Moolenaar0f873732019-12-05 20:28:46 +01006701 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 {
6703 mch_remove(tempname);
6704 vim_free(tempname);
6705 /*
6706 * With interactive completion, the error message is not printed.
6707 * However with USE_SYSTEM, I don't know how to turn off error messages
6708 * from the shell, so screen may still get messed up -- webb.
6709 */
6710#ifndef USE_SYSTEM
6711 if (!(flags & EW_SILENT))
6712#endif
6713 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006714 redraw_later_clear(); // probably messed up screen
6715 msg_putchar('\n'); // clear bottom line quickly
6716 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717#ifdef USE_SYSTEM
6718 if (!(flags & EW_SILENT))
6719#endif
6720 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006721 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006722 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 }
6724 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006725 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6726 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 if (shell_style == STYLE_BT)
6728 return FAIL;
6729 goto notfound;
6730 }
6731
6732 /*
6733 * read the names from the file into memory
6734 */
6735 fd = fopen((char *)tempname, READBIN);
6736 if (fd == NULL)
6737 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006738 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 if (!(flags & EW_SILENT))
6740 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006741 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006742 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 }
6744 vim_free(tempname);
6745 goto notfound;
6746 }
6747 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006748 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006750 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006751 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006752 buffer = NULL;
6753 else
6754 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 if (buffer == NULL)
6756 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006757 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 mch_remove(tempname);
6759 vim_free(tempname);
6760 fclose(fd);
6761 return FAIL;
6762 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006763 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 i = fread((char *)buffer, 1, len, fd);
6765 fclose(fd);
6766 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006767 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006769 // unexpected read error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006770 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 vim_free(tempname);
6772 vim_free(buffer);
6773 return FAIL;
6774 }
6775 vim_free(tempname);
6776
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006777# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006778 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006780 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6782 *p++ = buffer[i];
6783 len = p - buffer;
6784# endif
6785
6786
Bram Moolenaar0f873732019-12-05 20:28:46 +01006787 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 if (shell_style == STYLE_ECHO)
6789 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006790 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006792 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
6794 while (*p != ' ' && *p != '\n')
6795 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006796 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 }
6798 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006799 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006800 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006802 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006804 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 {
6806 while (*p != '\n' && *p != NUL)
6807 ++p;
6808 if (*p != NUL)
6809 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006810 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 }
6812 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006813 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 else
6815 {
6816 /*
6817 * Some versions of zsh use spaces instead of NULs to separate
6818 * results. Only do this when there is no NUL before the end of the
6819 * buffer, otherwise we would never be able to use file names with
6820 * embedded spaces when zsh does use NULs.
6821 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6822 * don't check for spaces again.
6823 */
6824 check_spaces = FALSE;
6825 if (shell_style == STYLE_PRINT && !did_find_nul)
6826 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006827 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006828 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006829 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 did_find_nul = TRUE;
6831 else
6832 check_spaces = TRUE;
6833 }
6834
6835 /*
6836 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6837 * already is one, for STYLE_GLOB it needs to be added.
6838 */
6839 if (len && buffer[len - 1] == NUL)
6840 --len;
6841 else
6842 buffer[len] = NUL;
6843 i = 0;
6844 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006845 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 {
6847 ++i;
6848 *p = NUL;
6849 }
6850 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006851 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 }
6853 if (i == 0)
6854 {
6855 /*
6856 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6857 * /bin/sh will happily expand it to nothing rather than returning an
6858 * error; and hey, it's good to check anyway -- webb.
6859 */
6860 vim_free(buffer);
6861 goto notfound;
6862 }
6863 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006864 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 if (*file == NULL)
6866 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006867 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 vim_free(buffer);
6869 return FAIL;
6870 }
6871
6872 /*
6873 * Isolate the individual file names.
6874 */
6875 p = buffer;
6876 for (i = 0; i < *num_file; ++i)
6877 {
6878 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006879 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006880 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6881 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006883 while (!(shell_style == STYLE_ECHO && *p == ' ')
6884 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006886 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 *p = NUL;
6888 else
6889 {
6890 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006891 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 }
6893 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006894 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006896 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006898 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 }
6900 }
6901
6902 /*
6903 * Move the file names to allocated memory.
6904 */
6905 for (j = 0, i = 0; i < *num_file; ++i)
6906 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006907 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6909 continue;
6910
Bram Moolenaar0f873732019-12-05 20:28:46 +01006911 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 dir = (mch_isdir((*file)[i]));
6913 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6914 continue;
6915
Bram Moolenaar0f873732019-12-05 20:28:46 +01006916 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01006917 if (!dir && (flags & EW_EXEC)
6918 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006919 continue;
6920
Bram Moolenaar964b3742019-05-24 18:54:09 +02006921 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 if (p)
6923 {
6924 STRCPY(p, (*file)[i]);
6925 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006926 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 (*file)[j++] = p;
6928 }
6929 }
6930 vim_free(buffer);
6931 *num_file = j;
6932
Bram Moolenaar0f873732019-12-05 20:28:46 +01006933 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006935 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 goto notfound;
6937 }
6938
6939 return OK;
6940
6941notfound:
6942 if (flags & EW_NOTFOUND)
6943 return save_patterns(num_pat, pat, num_file, file);
6944 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945}
6946
Bram Moolenaar0f873732019-12-05 20:28:46 +01006947#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006950save_patterns(
6951 int num_pat,
6952 char_u **pat,
6953 int *num_file,
6954 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955{
6956 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006957 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006959 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 if (*file == NULL)
6961 return FAIL;
6962 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006963 {
6964 s = vim_strsave(pat[i]);
6965 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006966 // Be compatible with expand_filename(): halve the number of
6967 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00006968 backslash_halve(s);
6969 (*file)[i] = s;
6970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 *num_file = num_pat;
6972 return OK;
6973}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975/*
6976 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6977 * expand.
6978 */
6979 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006980mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006982 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 if (*p == '\\' && p[1] != NUL)
6985 ++p;
6986 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (vim_strchr((char_u *)
6988#ifdef VMS
6989 "*?%"
6990#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992#endif
6993 , *p) != NULL)
6994 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 }
6996 return FALSE;
6997}
6998
6999/*
7000 * Return TRUE if the string "p" contains a wildcard.
7001 * Don't recognize '~' at the end as a wildcard.
7002 */
7003 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007004mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007006 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 if (*p == '\\' && p[1] != NUL)
7009 ++p;
7010 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 if (vim_strchr((char_u *)
7012#ifdef VMS
7013 "*?%$"
7014#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016#endif
7017 , *p) != NULL
7018 || (*p == '~' && p[1] != NUL))
7019 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 }
7021 return FALSE;
7022}
7023
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007025have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026{
7027 int i;
7028
7029 for (i = 0; i < num; i++)
7030 if (mch_has_wildcard(file[i]))
7031 return 1;
7032 return 0;
7033}
7034
7035 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007036have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037{
7038 int i;
7039
7040 for (i = 0; i < num; i++)
7041 if (vim_strchr(file[i], '$') != NULL)
7042 return TRUE;
7043 return FALSE;
7044}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007046#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047/*
7048 * Scaled-down version of rename(), which is missing in Xenix.
7049 * This version can only move regular files and will fail if the
7050 * destination exists.
7051 */
7052 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007053mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054{
7055 struct stat st;
7056
Bram Moolenaar0f873732019-12-05 20:28:46 +01007057 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007059 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007061 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 return 0;
7063 return -1;
7064}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007065#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007067#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068/*
7069 * Initializes connection with gpm (if it isn't already opened)
7070 * Return 1 if succeeded (or connection already opened), 0 if failed
7071 */
7072 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007073gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007075 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076
7077 if (!gpm_flag)
7078 {
7079 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7080 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007081 // Default handling for mouse move
7082 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 gpm_connect.maxMod = 0xffff;
7084 if (Gpm_Open(&gpm_connect, 0) > 0)
7085 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007086 // gpm library tries to handling TSTP causes
7087 // problems. Anyways, we close connection to Gpm whenever
7088 // we are going to suspend or starting an external process
7089 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007090# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007092# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007093 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 }
7095 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007096 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 return 0;
7098 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007099 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100}
7101
7102/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007103 * Returns TRUE if the GPM mouse is enabled.
7104 */
7105 int
7106gpm_enabled(void)
7107{
7108 return gpm_flag && gpm_fd >= 0;
7109}
7110
7111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 */
7114 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007115gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007117 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 Gpm_Close();
7119}
7120
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007121/*
7122 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007124 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 */
7126 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007127mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 int button;
7130 static Gpm_Event gpm_event;
7131 char_u string[6];
7132 int_u vim_modifiers;
7133 int row,col;
7134 unsigned char buttons_mask;
7135 unsigned char gpm_modifiers;
7136 static unsigned char old_buttons = 0;
7137
7138 Gpm_GetEvent(&gpm_event);
7139
7140#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007141 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 if (hold_gui_events)
7143 return 0;
7144#endif
7145
7146 row = gpm_event.y - 1;
7147 col = gpm_event.x - 1;
7148
Bram Moolenaar0f873732019-12-05 20:28:46 +01007149 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 string[1] = 'M';
7151 string[2] = 'G';
7152 switch (GPM_BARE_EVENTS(gpm_event.type))
7153 {
7154 case GPM_DRAG:
7155 string[3] = MOUSE_DRAG;
7156 break;
7157 case GPM_DOWN:
7158 buttons_mask = gpm_event.buttons & ~old_buttons;
7159 old_buttons = gpm_event.buttons;
7160 switch (buttons_mask)
7161 {
7162 case GPM_B_LEFT:
7163 button = MOUSE_LEFT;
7164 break;
7165 case GPM_B_MIDDLE:
7166 button = MOUSE_MIDDLE;
7167 break;
7168 case GPM_B_RIGHT:
7169 button = MOUSE_RIGHT;
7170 break;
7171 default:
7172 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007173 // Don't know what to do. Can more than one button be
7174 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 }
7176 string[3] = (char_u)(button | 0x20);
7177 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7178 break;
7179 case GPM_UP:
7180 string[3] = MOUSE_RELEASE;
7181 old_buttons &= ~gpm_event.buttons;
7182 break;
7183 default:
7184 return 0;
7185 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007186 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 gpm_modifiers = gpm_event.modifiers;
7188 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007189 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7190 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7191 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7193 vim_modifiers |= MOUSE_SHIFT;
7194
7195 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7196 vim_modifiers |= MOUSE_CTRL;
7197 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7198 vim_modifiers |= MOUSE_ALT;
7199 string[3] |= vim_modifiers;
7200 string[4] = (char_u)(col + ' ' + 1);
7201 string[5] = (char_u)(row + ' ' + 1);
7202 add_to_input_buf(string, 6);
7203 return 6;
7204}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007205#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007207#ifdef FEAT_SYSMOUSE
7208/*
7209 * Initialize connection with sysmouse.
7210 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7211 * output, any sysmouse output than will be processed via sig_sysmouse().
7212 * Return OK if succeeded, FAIL if failed.
7213 */
7214 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007215sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007216{
7217 struct mouse_info mouse;
7218
7219 mouse.operation = MOUSE_MODE;
7220 mouse.u.mode.mode = 0;
7221 mouse.u.mode.signal = SIGUSR2;
7222 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7223 {
7224 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7225 mouse.operation = MOUSE_SHOW;
7226 ioctl(1, CONS_MOUSECTL, &mouse);
7227 return OK;
7228 }
7229 return FAIL;
7230}
7231
7232/*
7233 * Stop processing SIGUSR2 signals, and also make sure that
7234 * virtual console do not send us any sysmouse related signal.
7235 */
7236 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007237sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007238{
7239 struct mouse_info mouse;
7240
7241 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7242 mouse.operation = MOUSE_MODE;
7243 mouse.u.mode.mode = 0;
7244 mouse.u.mode.signal = 0;
7245 ioctl(1, CONS_MOUSECTL, &mouse);
7246}
7247
7248/*
7249 * Gets info from sysmouse and adds special keys to input buf.
7250 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007251 static RETSIGTYPE
7252sig_sysmouse SIGDEFARG(sigarg)
7253{
7254 struct mouse_info mouse;
7255 struct video_info video;
7256 char_u string[6];
7257 int row, col;
7258 int button;
7259 int buttons;
7260 static int oldbuttons = 0;
7261
7262#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007263 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007264 if (hold_gui_events)
7265 return;
7266#endif
7267
7268 mouse.operation = MOUSE_GETINFO;
7269 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7270 && ioctl(1, FBIO_MODEINFO, &video) != -1
7271 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7272 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7273 {
7274 row = mouse.u.data.y / video.vi_cheight;
7275 col = mouse.u.data.x / video.vi_cwidth;
7276 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007277 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007278 string[1] = 'M';
7279 string[2] = 'S';
7280 if (oldbuttons == buttons && buttons != 0)
7281 {
7282 button = MOUSE_DRAG;
7283 }
7284 else
7285 {
7286 switch (buttons)
7287 {
7288 case 0:
7289 button = MOUSE_RELEASE;
7290 break;
7291 case 1:
7292 button = MOUSE_LEFT;
7293 break;
7294 case 2:
7295 button = MOUSE_MIDDLE;
7296 break;
7297 case 4:
7298 button = MOUSE_RIGHT;
7299 break;
7300 default:
7301 return;
7302 }
7303 oldbuttons = buttons;
7304 }
7305 string[3] = (char_u)(button);
7306 string[4] = (char_u)(col + ' ' + 1);
7307 string[5] = (char_u)(row + ' ' + 1);
7308 add_to_input_buf(string, 6);
7309 }
7310 return;
7311}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007312#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007313
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007315typedef char_u * (*STRPROCSTR)(char_u *);
7316typedef char_u * (*INTPROCSTR)(int);
7317typedef int (*STRPROCINT)(char_u *);
7318typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
7320/*
7321 * Call a DLL routine which takes either a string or int param
7322 * and returns an allocated string.
7323 */
7324 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007325mch_libcall(
7326 char_u *libname,
7327 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007328 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007329 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007330 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007331 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
7333# if defined(USE_DLOPEN)
7334 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007335 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336# else
7337 shl_t hinstLib;
7338# endif
7339 STRPROCSTR ProcAdd;
7340 INTPROCSTR ProcAddI;
7341 char_u *retval_str = NULL;
7342 int retval_int = 0;
7343 int success = FALSE;
7344
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007345 /*
7346 * Get a handle to the DLL module.
7347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007349 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007350 (void)dlerror();
7351
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 hinstLib = dlopen((char *)libname, RTLD_LAZY
7353# ifdef RTLD_LOCAL
7354 | RTLD_LOCAL
7355# endif
7356 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007357 if (hinstLib == NULL)
7358 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007359 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007360 dlerr = (char *)dlerror();
7361 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007362 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364# else
7365 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7366# endif
7367
Bram Moolenaar0f873732019-12-05 20:28:46 +01007368 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 if (hinstLib != NULL)
7370 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007371# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 /*
7373 * Catch a crash when calling the library function. For example when
7374 * using a number where a string pointer is expected.
7375 */
7376 mch_startjmp();
7377 if (SETJMP(lc_jump_env) != 0)
7378 {
7379 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007380# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007381 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 mch_didjmp();
7384 }
7385 else
7386# endif
7387 {
7388 retval_str = NULL;
7389 retval_int = 0;
7390
7391 if (argstring != NULL)
7392 {
7393# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007394 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007395 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396# else
7397 if (shl_findsym(&hinstLib, (const char *)funcname,
7398 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7399 ProcAdd = NULL;
7400# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007401 if ((success = (ProcAdd != NULL
7402# if defined(USE_DLOPEN)
7403 && dlerr == NULL
7404# endif
7405 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 {
7407 if (string_result == NULL)
7408 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7409 else
7410 retval_str = (ProcAdd)(argstring);
7411 }
7412 }
7413 else
7414 {
7415# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007416 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007417 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418# else
7419 if (shl_findsym(&hinstLib, (const char *)funcname,
7420 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7421 ProcAddI = NULL;
7422# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007423 if ((success = (ProcAddI != NULL
7424# if defined(USE_DLOPEN)
7425 && dlerr == NULL
7426# endif
7427 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 {
7429 if (string_result == NULL)
7430 retval_int = ((INTPROCINT)ProcAddI)(argint);
7431 else
7432 retval_str = (ProcAddI)(argint);
7433 }
7434 }
7435
Bram Moolenaar0f873732019-12-05 20:28:46 +01007436 // Save the string before we free the library.
7437 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 if (string_result == NULL)
7439 *number_result = retval_int;
7440 else if (retval_str != NULL
7441 && retval_str != (char_u *)1
7442 && retval_str != (char_u *)-1)
7443 *string_result = vim_strsave(retval_str);
7444 }
7445
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007446# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 mch_endjmp();
7448# ifdef SIGHASARG
7449 if (lc_signal != 0)
7450 {
7451 int i;
7452
Bram Moolenaar0f873732019-12-05 20:28:46 +01007453 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 for (i = 0; signal_info[i].sig != -1; i++)
7455 if (lc_signal == signal_info[i].sig)
7456 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007457 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 }
7459# endif
7460# endif
7461
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007463 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007464 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007465 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007466
Bram Moolenaar0f873732019-12-05 20:28:46 +01007467 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 (void)dlclose(hinstLib);
7469# else
7470 (void)shl_unload(hinstLib);
7471# endif
7472 }
7473
7474 if (!success)
7475 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007476 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 return FAIL;
7478 }
7479
7480 return OK;
7481}
7482#endif
7483
7484#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007485static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486static int xterm_button;
7487
7488/*
7489 * Setup a dummy window for X selections in a terminal.
7490 */
7491 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007492setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493{
7494 int z = 0;
7495 char *strp = "";
7496 Widget AppShell;
7497
7498 if (!x_connect_to_server())
7499 return;
7500
7501 open_app_context();
7502 if (app_context != NULL && xterm_Shell == (Widget)0)
7503 {
7504 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007505# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007507# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007508# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007509 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510
7511 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007512 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513# endif
7514
Bram Moolenaar0f873732019-12-05 20:28:46 +01007515 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 oldhandler = XSetErrorHandler(x_error_check);
7517
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007518# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007519 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7521 mch_startjmp();
7522 if (SETJMP(lc_jump_env) != 0)
7523 {
7524 mch_didjmp();
7525 xterm_dpy = NULL;
7526 }
7527 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007528# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 {
7530 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7531 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007532 if (xterm_dpy != NULL)
7533 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007534# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007536# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 }
7538
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007539# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007540 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007542# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007543 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 (void)XSetErrorHandler(oldhandler);
7545
7546 if (xterm_dpy == NULL)
7547 {
7548 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007549 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 return;
7551 }
7552
Bram Moolenaar0f873732019-12-05 20:28:46 +01007553 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 (void)XSetIOErrorHandler(x_IOerror_handler);
7555
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007556# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007558 {
7559 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007560 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007561 verbose_leave();
7562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563# endif
7564
Bram Moolenaar0f873732019-12-05 20:28:46 +01007565 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7567 applicationShellWidgetClass, xterm_dpy,
7568 NULL);
7569 if (AppShell == (Widget)0)
7570 return;
7571 xterm_Shell = XtVaCreatePopupShell("VIM",
7572 topLevelShellWidgetClass, AppShell,
7573 XtNmappedWhenManaged, 0,
7574 XtNwidth, 1,
7575 XtNheight, 1,
7576 NULL);
7577 if (xterm_Shell == (Widget)0)
7578 return;
7579
7580 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007581 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 if (x11_display == NULL)
7583 x11_display = xterm_dpy;
7584
7585 XtRealizeWidget(xterm_Shell);
7586 XSync(xterm_dpy, False);
7587 xterm_update();
7588 }
7589 if (xterm_Shell != (Widget)0)
7590 {
7591 clip_init(TRUE);
7592 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7593 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007594 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 if (test_x11_window(xterm_dpy) == FAIL)
7596 x11_window = 0;
7597 if (x11_window != 0)
7598 xterm_trace = 0;
7599 }
7600}
7601
7602 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007603start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604{
7605 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7606 return;
7607 xterm_trace = 1;
7608 xterm_button = button;
7609 do_xterm_trace();
7610}
7611
7612
7613 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007614stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615{
7616 if (xterm_trace < 0)
7617 return;
7618 xterm_trace = 0;
7619}
7620
7621/*
7622 * Query the xterm pointer and generate mouse termcodes if necessary
7623 * return TRUE if dragging is active, else FALSE
7624 */
7625 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007626do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627{
7628 Window root, child;
7629 int root_x, root_y;
7630 int win_x, win_y;
7631 int row, col;
7632 int_u mask_return;
7633 char_u buf[50];
7634 char_u *strp;
7635 long got_hints;
7636 static char_u *mouse_code;
7637 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7638 static int prev_row = 0, prev_col = 0;
7639 static XSizeHints xterm_hints;
7640
7641 if (xterm_trace <= 0)
7642 return FALSE;
7643
7644 if (xterm_trace == 1)
7645 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007646 // Get the hints just before tracking starts. The font size might
7647 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007648 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7649 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 || xterm_hints.width_inc <= 1
7651 || xterm_hints.height_inc <= 1)
7652 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007653 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 return FALSE;
7655 }
7656
Bram Moolenaar0f873732019-12-05 20:28:46 +01007657 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 mouse_code = find_termcode(mouse_name);
7659 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007660 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 xterm_trace = 2;
7662
Bram Moolenaar0f873732019-12-05 20:28:46 +01007663 // Find the offset of the chars, there might be a scrollbar on the
7664 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7666 &win_x, &win_y, &mask_return);
7667 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7668 - (xterm_hints.height_inc / 2);
7669 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7670 xterm_hints.y = 2;
7671 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7672 - (xterm_hints.width_inc / 2);
7673 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7674 xterm_hints.x = 2;
7675 return TRUE;
7676 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007677 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 {
7679 xterm_trace = 0;
7680 return FALSE;
7681 }
7682
7683 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7684 &win_x, &win_y, &mask_return);
7685
7686 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7687 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7688 if (row == prev_row && col == prev_col)
7689 return TRUE;
7690
7691 STRCPY(buf, mouse_code);
7692 strp = buf + STRLEN(buf);
7693 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7694 *strp++ = (char_u)(col + ' ' + 1);
7695 *strp++ = (char_u)(row + ' ' + 1);
7696 *strp = 0;
7697 add_to_input_buf(buf, STRLEN(buf));
7698
7699 prev_row = row;
7700 prev_col = col;
7701 return TRUE;
7702}
7703
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007704# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705/*
7706 * Destroy the display, window and app_context. Required for GTK.
7707 */
7708 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007709clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710{
7711 if (xterm_Shell != (Widget)0)
7712 {
7713 XtDestroyWidget(xterm_Shell);
7714 xterm_Shell = (Widget)0;
7715 }
7716 if (xterm_dpy != NULL)
7717 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007718# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007719 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007721# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 if (x11_display == xterm_dpy)
7723 x11_display = NULL;
7724 xterm_dpy = NULL;
7725 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007726# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 if (app_context != (XtAppContext)NULL)
7728 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007729 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 XtDestroyApplicationContext(app_context);
7731 app_context = (XtAppContext)NULL;
7732 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007733# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734}
7735# endif
7736
7737/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007738 * Catch up with GUI or X events.
7739 */
7740 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007741clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007742{
7743# ifdef FEAT_GUI
7744 if (gui.in_use)
7745 gui_mch_update();
7746 else
7747# endif
7748 if (xterm_Shell != (Widget)0)
7749 xterm_update();
7750}
7751
7752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 * Catch up with any queued X events. This may put keyboard input into the
7754 * input buffer, call resize call-backs, trigger timers etc. If there is
7755 * nothing in the X event queue (& no timers pending), then we return
7756 * immediately.
7757 */
7758 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007759xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760{
7761 XEvent event;
7762
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007763 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007765 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007767 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007768 break;
7769
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007770 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007771 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007772 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007773 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007774#ifdef FEAT_CLIENTSERVER
7775 {
7776 XPropertyEvent *e = (XPropertyEvent *)&event;
7777
7778 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007780 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007783 XtDispatchEvent(&event);
7784 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007785 else
7786 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007787 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007788 XtAppProcessEvent(app_context, mask);
7789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 }
7791}
7792
7793 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007794clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
7796 if (xterm_Shell != (Widget)0)
7797 return clip_x11_own_selection(xterm_Shell, cbd);
7798 return FAIL;
7799}
7800
7801 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007802clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
7804 if (xterm_Shell != (Widget)0)
7805 clip_x11_lose_selection(xterm_Shell, cbd);
7806}
7807
7808 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007809clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811 if (xterm_Shell != (Widget)0)
7812 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7813}
7814
7815 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007816clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817{
7818 clip_x11_set_selection(cbd);
7819}
7820#endif
7821
7822
7823#if defined(USE_XSMP) || defined(PROTO)
7824/*
7825 * Code for X Session Management Protocol.
7826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827
7828# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829/*
7830 * This is our chance to ask the user if they want to save,
7831 * or abort the logout
7832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007834xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835{
7836 cmdmod_T save_cmdmod;
7837 int cancel_shutdown = False;
7838
7839 save_cmdmod = cmdmod;
7840 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007841 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007842 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 cancel_shutdown = True;
7844 cmdmod = save_cmdmod;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007845 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 out_flush();
7847
Bram Moolenaar0f873732019-12-05 20:28:46 +01007848 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 SmcInteractDone(smc_conn, cancel_shutdown);
7850
Bram Moolenaar0f873732019-12-05 20:28:46 +01007851 // Finish off
7852 // Only end save-yourself here if we're not cancelling shutdown;
7853 // we'll get a cancelled callback later in which we'll end it.
7854 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 if (!cancel_shutdown)
7856 {
7857 xsmp.save_yourself = False;
7858 SmcSaveYourselfDone(smc_conn, True);
7859 }
7860}
7861# endif
7862
7863/*
7864 * Callback that starts save-yourself.
7865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007867xsmp_handle_save_yourself(
7868 SmcConn smc_conn,
7869 SmPointer client_data UNUSED,
7870 int save_type UNUSED,
7871 Bool shutdown,
7872 int interact_style UNUSED,
7873 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007875 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 if (xsmp.save_yourself)
7877 SmcSaveYourselfDone(smc_conn, True);
7878 xsmp.save_yourself = True;
7879 xsmp.shutdown = shutdown;
7880
Bram Moolenaar0f873732019-12-05 20:28:46 +01007881 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007883 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884
7885 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007886 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887
7888# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007889 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007891 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 SmcInteractRequest(smc_conn, SmDialogError,
7893 xsmp_handle_interaction, client_data);
7894 else
7895# endif
7896 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007897 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 SmcSaveYourselfDone(smc_conn, True);
7899 xsmp.save_yourself = False;
7900 }
7901}
7902
7903
7904/*
7905 * Callback to warn us of imminent death.
7906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007908xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909{
7910 xsmp_close();
7911
Bram Moolenaar0f873732019-12-05 20:28:46 +01007912 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 getout_preserve_modified(0);
7914}
7915
7916
7917/*
7918 * Callback to tell us that save-yourself has completed.
7919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007921xsmp_save_complete(
7922 SmcConn smc_conn UNUSED,
7923 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924{
7925 xsmp.save_yourself = False;
7926}
7927
7928
7929/*
7930 * Callback to tell us that an instigated shutdown was cancelled
7931 * (maybe even by us)
7932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007934xsmp_shutdown_cancelled(
7935 SmcConn smc_conn,
7936 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
7938 if (xsmp.save_yourself)
7939 SmcSaveYourselfDone(smc_conn, True);
7940 xsmp.save_yourself = False;
7941 xsmp.shutdown = False;
7942}
7943
7944
7945/*
7946 * Callback to tell us that a new ICE connection has been established.
7947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007949xsmp_ice_connection(
7950 IceConn iceConn,
7951 IcePointer clientData UNUSED,
7952 Bool opening,
7953 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007955 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 if (opening)
7957 {
7958 xsmp_icefd = IceConnectionNumber(iceConn);
7959 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7960 }
7961}
7962
7963
Bram Moolenaar0f873732019-12-05 20:28:46 +01007964// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007966xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967{
7968 Bool rep;
7969
7970 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7971 == IceProcessMessagesIOError)
7972 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007973 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007975 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 xsmp_close();
7977 return FAIL;
7978 }
7979 else
7980 return OK;
7981}
7982
7983static int dummy;
7984
Bram Moolenaar0f873732019-12-05 20:28:46 +01007985// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 void
7987xsmp_init(void)
7988{
7989 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 SmcCallbacks smcallbacks;
7991#if 0
7992 SmPropValue smname;
7993 SmProp smnameprop;
7994 SmProp *smprops[1];
7995#endif
7996
7997 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007998 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999
8000 xsmp.save_yourself = xsmp.shutdown = False;
8001
Bram Moolenaar0f873732019-12-05 20:28:46 +01008002 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8004 smcallbacks.save_yourself.client_data = NULL;
8005 smcallbacks.die.callback = xsmp_die;
8006 smcallbacks.die.client_data = NULL;
8007 smcallbacks.save_complete.callback = xsmp_save_complete;
8008 smcallbacks.save_complete.client_data = NULL;
8009 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8010 smcallbacks.shutdown_cancelled.client_data = NULL;
8011
Bram Moolenaar0f873732019-12-05 20:28:46 +01008012 // Set up a watch on ICE connection creations. The "dummy" argument is
8013 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8015 {
8016 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008017 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 return;
8019 }
8020
Bram Moolenaar0f873732019-12-05 20:28:46 +01008021 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 xsmp.smcconn = SmcOpenConnection(
8023 NULL,
8024 NULL,
8025 SmProtoMajor,
8026 SmProtoMinor,
8027 SmcSaveYourselfProcMask | SmcDieProcMask
8028 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8029 &smcallbacks,
8030 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008031 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008032 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 errorstring);
8034 if (xsmp.smcconn == NULL)
8035 {
8036 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008037
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008039 {
8040 vim_snprintf(errorreport, sizeof(errorreport),
8041 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008042 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 return;
8045 }
8046 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8047
8048#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008049 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 smname.value = "vim";
8051 smname.length = 3;
8052 smnameprop.name = "SmProgram";
8053 smnameprop.type = "SmARRAY8";
8054 smnameprop.num_vals = 1;
8055 smnameprop.vals = &smname;
8056
8057 smprops[0] = &smnameprop;
8058 SmcSetProperties(xsmp.smcconn, 1, smprops);
8059#endif
8060}
8061
8062
Bram Moolenaar0f873732019-12-05 20:28:46 +01008063// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008065xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066{
8067 if (xsmp_icefd != -1)
8068 {
8069 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008070 if (xsmp.clientid != NULL)
8071 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008072 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 xsmp_icefd = -1;
8074 }
8075}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008076#endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077
8078
8079#ifdef EBCDIC
Bram Moolenaar0f873732019-12-05 20:28:46 +01008080// Translate character to its CTRL- value
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081char CtrlTable[] =
8082{
8083/* 00 - 5E */
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, 0,
8088 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8089 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8090/* ^ */ 0x1E,
8091/* - */ 0x1F,
8092/* 61 - 6C */
8093 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8094/* _ */ 0x1F,
8095/* 6E - 80 */
8096 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8097/* a */ 0x01,
8098/* b */ 0x02,
8099/* c */ 0x03,
8100/* d */ 0x37,
8101/* e */ 0x2D,
8102/* f */ 0x2E,
8103/* g */ 0x2F,
8104/* h */ 0x16,
8105/* i */ 0x05,
8106/* 8A - 90 */
8107 0, 0, 0, 0, 0, 0, 0,
8108/* j */ 0x15,
8109/* k */ 0x0B,
8110/* l */ 0x0C,
8111/* m */ 0x0D,
8112/* n */ 0x0E,
8113/* o */ 0x0F,
8114/* p */ 0x10,
8115/* q */ 0x11,
8116/* r */ 0x12,
8117/* 9A - A1 */
8118 0, 0, 0, 0, 0, 0, 0, 0,
8119/* s */ 0x13,
8120/* t */ 0x3C,
8121/* u */ 0x3D,
8122/* v */ 0x32,
8123/* w */ 0x26,
8124/* x */ 0x18,
8125/* y */ 0x19,
8126/* z */ 0x3F,
8127/* AA - AC */
8128 0, 0, 0,
8129/* [ */ 0x27,
8130/* AE - BC */
8131 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8132/* ] */ 0x1D,
8133/* BE - C0 */ 0, 0, 0,
8134/* A */ 0x01,
8135/* B */ 0x02,
8136/* C */ 0x03,
8137/* D */ 0x37,
8138/* E */ 0x2D,
8139/* F */ 0x2E,
8140/* G */ 0x2F,
8141/* H */ 0x16,
8142/* I */ 0x05,
8143/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8144/* J */ 0x15,
8145/* K */ 0x0B,
8146/* L */ 0x0C,
8147/* M */ 0x0D,
8148/* N */ 0x0E,
8149/* O */ 0x0F,
8150/* P */ 0x10,
8151/* Q */ 0x11,
8152/* R */ 0x12,
8153/* DA - DF */ 0, 0, 0, 0, 0, 0,
8154/* \ */ 0x1C,
8155/* E1 */ 0,
8156/* S */ 0x13,
8157/* T */ 0x3C,
8158/* U */ 0x3D,
8159/* V */ 0x32,
8160/* W */ 0x26,
8161/* X */ 0x18,
8162/* Y */ 0x19,
8163/* Z */ 0x3F,
8164/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8165 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8166};
8167
8168char MetaCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008169{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8171 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8172 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8173 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8174};
8175
8176
Bram Moolenaar0f873732019-12-05 20:28:46 +01008177// TODO: Use characters NOT numbers!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178char CtrlCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008179{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8181 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8182 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8183 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8184};
8185
8186
8187#endif