blob: c6ba2499bdabf04d3283085e151b36c3f312f093 [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 Moolenaar928eec62020-05-31 13:09:47 +02003484 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3485 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3487# if defined(IEXTEN) && !defined(__MINT__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003488 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
3489 // but it breaks function keys on MINT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490# endif
3491 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003492# ifdef ONLCR
3493 // Don't map NL -> CR NL, we do it ourselves.
3494 // Also disable expanding tabs if possible.
3495# ifdef XTABS
3496 tnew.c_oflag &= ~(ONLCR | XTABS);
3497# else
3498# ifdef TAB3
3499 tnew.c_oflag &= ~(ONLCR | TAB3);
3500# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003502# endif
3503# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003505 tnew.c_cc[VMIN] = 1; // return after 1 char
3506 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
3508 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003509 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003510 // Also reset ICANON here, otherwise on Solaris select() won't see
3511 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003512 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003513 tnew.c_cc[VMIN] = 1; // return after 1 char
3514 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
3517# if defined(HAVE_TERMIOS_H)
3518 {
3519 int n = 10;
3520
Bram Moolenaar0f873732019-12-05 20:28:46 +01003521 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3522 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3524 && errno == EINTR && n > 0)
3525 --n;
3526 }
3527# else
3528 ioctl(read_cmd_fd, TCSETA, &tnew);
3529# endif
3530
3531#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 /*
3533 * for "old" tty systems
3534 */
3535# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003536# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537# endif
3538 static struct sgttyb ttybold;
3539 struct sgttyb ttybnew;
3540
3541 if (first)
3542 {
3543 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003544 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546
3547 ttybnew = ttybold;
3548 if (tmode == TMODE_RAW)
3549 {
3550 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3551 ttybnew.sg_flags |= RAW;
3552 }
3553 else if (tmode == TMODE_SLEEP)
3554 ttybnew.sg_flags &= ~(ECHO);
3555 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3556#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003557 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558}
3559
3560/*
3561 * Try to get the code for "t_kb" from the stty setting
3562 *
3563 * Even if termcap claims a backspace key, the user's setting *should*
3564 * prevail. stty knows more about reality than termcap does, and if
3565 * somebody's usual erase key is DEL (which, for most BSD users, it will
3566 * be), they're going to get really annoyed if their erase key starts
3567 * doing forward deletes for no reason. (Eric Fischer)
3568 */
3569 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003570get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003572 ttyinfo_T info;
3573 char_u buf[2];
3574 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003576 if (get_tty_info(read_cmd_fd, &info) == OK)
3577 {
3578 intr_char = info.interrupt;
3579 buf[0] = info.backspace;
3580 buf[1] = NUL;
3581 add_termcode((char_u *)"kb", buf, FALSE);
3582
Bram Moolenaar0f873732019-12-05 20:28:46 +01003583 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003584 p = find_termcode((char_u *)"kD");
3585 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3586 do_fixdel(NULL);
3587 }
3588}
3589
3590/*
3591 * Obtain the characters that Backspace and Enter produce on "fd".
3592 * Returns OK or FAIL.
3593 */
3594 int
3595get_tty_info(int fd, ttyinfo_T *info)
3596{
3597#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598# ifdef HAVE_TERMIOS_H
3599 struct termios keys;
3600# else
3601 struct termio keys;
3602# endif
3603
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003604 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003606 info->backspace = keys.c_cc[VERASE];
3607 info->interrupt = keys.c_cc[VINTR];
3608 if (keys.c_iflag & ICRNL)
3609 info->enter = NL;
3610 else
3611 info->enter = CAR;
3612 if (keys.c_oflag & ONLCR)
3613 info->nl_does_cr = TRUE;
3614 else
3615 info->nl_does_cr = FALSE;
3616 return OK;
3617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003619 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 struct sgttyb keys;
3621
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003622 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003624 info->backspace = keys.sg_erase;
3625 info->interrupt = keys.sg_kill;
3626 info->enter = CAR;
3627 info->nl_does_cr = TRUE;
3628 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003631 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632}
3633
Bram Moolenaar0f873732019-12-05 20:28:46 +01003634#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003636static int mouse_ison = FALSE;
3637
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638/*
3639 * Set mouse clicks on or off.
3640 */
3641 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003642mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003644#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003645 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 int xterm_mouse_vers;
3648
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003649#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003650 if (!on)
3651 // Make sure not tracing mouse movements. Important when a button-down
3652 // was received but no release yet.
3653 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003654#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003655
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003656 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003657#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003658 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003659#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003660 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003661 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 return;
3663
3664 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003665
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003666#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003667 if (ttym_flags == TTYM_URXVT)
3668 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003669 out_str_nf((char_u *)
3670 (on
3671 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3672 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003673 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003674 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003675#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003676
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003677 if (ttym_flags == TTYM_SGR)
3678 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003679 // SGR mode supports columns above 223
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003680 out_str_nf((char_u *)
3681 (on
3682 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3683 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003684 mouse_ison = on;
3685 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003686
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003687#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003688 if (bevalterm_ison != (p_bevalterm && on))
3689 {
3690 bevalterm_ison = (p_bevalterm && on);
3691 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003692 // disable mouse movement events, enabling is below
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003693 out_str_nf((char_u *)
3694 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003695 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003696#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003697
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 if (xterm_mouse_vers > 0)
3699 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003700 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 out_str_nf((char_u *)
3702 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003703 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003704#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003705 bevalterm_ison
3706 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003707#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003708 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003710 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 out_str_nf((char_u *)
3712 (xterm_mouse_vers > 1
3713 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3714 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003715 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003718#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 else if (ttym_flags == TTYM_DEC)
3720 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003721 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003723 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003725 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003729#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 else
3731 {
3732 if (on)
3733 {
3734 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003735 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737 else
3738 {
3739 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003740 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
3742 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003745#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003746 else
3747 {
3748 if (on)
3749 {
3750 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003751 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003752 }
3753 else
3754 {
3755 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003756 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003757 }
3758 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003759#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003760
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003761#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 else
3763 {
3764 if (on)
3765 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003766 // D - Enable Mouse up/down messages
3767 // L - Enable Left Button Reporting
3768 // M - Enable Middle Button Reporting
3769 // R - Enable Right Button Reporting
3770 // K - Enable SHIFT and CTRL key Reporting
3771 // + - Enable Advanced messaging of mouse moves and up/down messages
3772 // Q - Quiet No Ack
3773 // # - Numeric value of mouse pointer required
3774 // 0 = Multiview 2000 cursor, used as standard
3775 // 1 = Windows Arrow
3776 // 2 = Windows I Beam
3777 // 3 = Windows Hour Glass
3778 // 4 = Windows Cross Hair
3779 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003780# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003781 // Disables full feedback of pointer movements
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3783 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003784# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3786 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003787# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003788 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 else
3791 {
3792 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3793 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003794 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 }
3796 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003797#endif
3798#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 else
3800 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003801 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 if (on)
3803 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3804 else
3805 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003806 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809}
3810
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003811#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003812/*
3813 * Called when 'balloonevalterm' changed.
3814 */
3815 void
3816mch_bevalterm_changed(void)
3817{
3818 mch_setmouse(mouse_ison);
3819}
3820#endif
3821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822/*
3823 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3824 */
3825 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003826check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827{
3828# ifdef FEAT_MOUSE_XTERM
3829 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003830# ifdef FEAT_MOUSE_URXVT
3831 && use_xterm_mouse() != 3
3832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833# ifdef FEAT_GUI
3834 && !gui.in_use
3835# endif
3836 )
3837 {
3838 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003839 ? IF_EB("\233M", CSI_STR "M")
3840 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 if (*p_mouse != NUL)
3842 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003843 // force mouse off and maybe on to send possibly new mouse
3844 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 mch_setmouse(FALSE);
3846 setmouse();
3847 }
3848 }
3849 else
3850 del_mouse_termcode(KS_MOUSE);
3851# endif
3852
3853# ifdef FEAT_MOUSE_GPM
3854 if (!use_xterm_mouse()
3855# ifdef FEAT_GUI
3856 && !gui.in_use
3857# endif
3858 )
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003859 set_mouse_termcode(KS_GPM_MOUSE,
3860 (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3861 else
3862 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863# endif
3864
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003865# ifdef FEAT_SYSMOUSE
3866 if (!use_xterm_mouse()
3867# ifdef FEAT_GUI
3868 && !gui.in_use
3869# endif
3870 )
3871 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3872# endif
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003875 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 if (!use_xterm_mouse()
3877# ifdef FEAT_GUI
3878 && !gui.in_use
3879# endif
3880 )
3881 set_mouse_termcode(KS_JSBTERM_MOUSE,
3882 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3883 else
3884 del_mouse_termcode(KS_JSBTERM_MOUSE);
3885# endif
3886
3887# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003888 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3889 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 if (!use_xterm_mouse()
3891# ifdef FEAT_GUI
3892 && !gui.in_use
3893# endif
3894 )
3895 set_mouse_termcode(KS_NETTERM_MOUSE,
3896 (char_u *)IF_EB("\033}", ESC_STR "}"));
3897 else
3898 del_mouse_termcode(KS_NETTERM_MOUSE);
3899# endif
3900
3901# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003902 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003903 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904# ifdef FEAT_GUI
3905 && !gui.in_use
3906# endif
3907 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003908 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3909 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 else
3911 del_mouse_termcode(KS_DEC_MOUSE);
3912# endif
3913# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01003914 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003915 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916# ifdef FEAT_GUI
3917 && !gui.in_use
3918# endif
3919 )
3920 set_mouse_termcode(KS_PTERM_MOUSE,
3921 (char_u *) IF_EB("\033[", ESC_STR "["));
3922 else
3923 del_mouse_termcode(KS_PTERM_MOUSE);
3924# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003925# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003926 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003927# ifdef FEAT_GUI
3928 && !gui.in_use
3929# endif
3930 )
3931 {
3932 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003933 ? IF_EB("\233*M", CSI_STR "*M")
3934 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003935
3936 if (*p_mouse != NUL)
3937 {
3938 mch_setmouse(FALSE);
3939 setmouse();
3940 }
3941 }
3942 else
3943 del_mouse_termcode(KS_URXVT_MOUSE);
3944# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003945 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003946# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003947 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003948# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003949 )
3950 {
3951 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003952 ? IF_EB("\233<*M", CSI_STR "<*M")
3953 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3954
3955 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3956 ? IF_EB("\233<*m", CSI_STR "<*m")
3957 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003958
3959 if (*p_mouse != NUL)
3960 {
3961 mch_setmouse(FALSE);
3962 setmouse();
3963 }
3964 }
3965 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003966 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003967 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003968 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972#ifndef VMS
3973
3974/*
3975 * Try to get the current window size:
3976 * 1. with an ioctl(), most accurate method
3977 * 2. from the environment variables LINES and COLUMNS
3978 * 3. from the termcap
3979 * 4. keep using the old values
3980 * Return OK when size could be determined, FAIL otherwise.
3981 */
3982 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003983mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984{
3985 long rows = 0;
3986 long columns = 0;
3987 char_u *p;
3988
3989 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 * 1. try using an ioctl. It is the most accurate method.
3991 *
3992 * Try using TIOCGWINSZ first, some systems that have it also define
3993 * TIOCGSIZE but don't have a struct ttysize.
3994 */
3995# ifdef TIOCGWINSZ
3996 {
3997 struct winsize ws;
3998 int fd = 1;
3999
Bram Moolenaar0f873732019-12-05 20:28:46 +01004000 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 if (!isatty(fd) && isatty(read_cmd_fd))
4002 fd = read_cmd_fd;
4003 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4004 {
4005 columns = ws.ws_col;
4006 rows = ws.ws_row;
4007 }
4008 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004009# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010# ifdef TIOCGSIZE
4011 {
4012 struct ttysize ts;
4013 int fd = 1;
4014
Bram Moolenaar0f873732019-12-05 20:28:46 +01004015 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 if (!isatty(fd) && isatty(read_cmd_fd))
4017 fd = read_cmd_fd;
4018 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4019 {
4020 columns = ts.ts_cols;
4021 rows = ts.ts_lines;
4022 }
4023 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004024# endif // TIOCGSIZE
4025# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
4027 /*
4028 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004029 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4030 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004032 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 {
4034 if ((p = (char_u *)getenv("LINES")))
4035 rows = atoi((char *)p);
4036 if ((p = (char_u *)getenv("COLUMNS")))
4037 columns = atoi((char *)p);
4038 }
4039
4040#ifdef HAVE_TGETENT
4041 /*
4042 * 3. try reading "co" and "li" entries from termcap
4043 */
4044 if (columns == 0 || rows == 0)
4045 getlinecol(&columns, &rows);
4046#endif
4047
4048 /*
4049 * 4. If everything fails, use the old values
4050 */
4051 if (columns <= 0 || rows <= 0)
4052 return FAIL;
4053
4054 Rows = rows;
4055 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004056 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 return OK;
4058}
4059
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004060#if defined(FEAT_TERMINAL) || defined(PROTO)
4061/*
4062 * Report the windows size "rows" and "cols" to tty "fd".
4063 */
4064 int
4065mch_report_winsize(int fd, int rows, int cols)
4066{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004067 int tty_fd;
4068 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004069
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004070 tty_fd = get_tty_fd(fd);
4071 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004072 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004073# if defined(TIOCSWINSZ)
4074 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004075
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004076 ws.ws_col = cols;
4077 ws.ws_row = rows;
4078 ws.ws_xpixel = cols * 5;
4079 ws.ws_ypixel = rows * 10;
4080 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4081 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4082 retval == 0 ? "success" : "failed");
4083# elif defined(TIOCSSIZE)
4084 struct ttysize ts;
4085
4086 ts.ts_cols = cols;
4087 ts.ts_lines = rows;
4088 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4089 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4090 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004091# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004092 if (tty_fd != fd)
4093 close(tty_fd);
4094 }
4095 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004096}
4097#endif
4098
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099/*
4100 * Try to set the window size to Rows and Columns.
4101 */
4102 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004103mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
4105 if (*T_CWS)
4106 {
4107 /*
4108 * NOTE: if you get an error here that term_set_winsize() is
4109 * undefined, check the output of configure. It could probably not
4110 * find a ncurses, termcap or termlib library.
4111 */
4112 term_set_winsize((int)Rows, (int)Columns);
4113 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004114 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116}
4117
Bram Moolenaar0f873732019-12-05 20:28:46 +01004118#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
4120/*
4121 * Rows and/or Columns has changed.
4122 */
4123 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004124mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004126 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127}
4128
Bram Moolenaar205b8862011-09-07 15:04:31 +02004129/*
4130 * Wait for process "child" to end.
4131 * Return "child" if it exited properly, <= 0 on error.
4132 */
4133 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004134wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004135{
4136 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004137 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004138
4139 while (wait_pid != child)
4140 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004141 // When compiled with Python threads are probably used, in which case
4142 // wait() sometimes hangs for no obvious reason. Use waitpid()
4143 // instead and loop (like the GUI). Also needed for other interfaces,
4144 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004145# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004146 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004147# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004148 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004149# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004150 if (wait_pid == 0)
4151 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004152 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004153 mch_delay(delay_msec, TRUE);
4154 if (++delay_msec > 10)
4155 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004156 continue;
4157 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004158 if (wait_pid <= 0
4159# ifdef ECHILD
4160 && errno == ECHILD
4161# endif
4162 )
4163 break;
4164 }
4165 return wait_pid;
4166}
4167
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004168#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004169/*
4170 * Set the environment for a child process.
4171 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004172 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004173set_child_environment(
4174 long rows,
4175 long columns,
4176 char *term,
4177 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004178{
4179# ifdef HAVE_SETENV
4180 char envbuf[50];
4181# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004182 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004183 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004184 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004185 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004186 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004187# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004188 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004189# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004190# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004191 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004192# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004193# endif
4194
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004195# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004196 setenv("TERM", term, 1);
4197 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004198 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004199 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004200 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004201 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004202 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004203 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004204 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004205# ifdef FEAT_TERMINAL
4206 if (is_terminal)
4207 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004208 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004209 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4210 }
4211# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004212# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004213 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004214# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004215# else
4216 /*
4217 * Putenv does not copy the string, it has to remain valid.
4218 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004219 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004220 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004221 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4222 putenv(envbuf_Term);
4223 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004224 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004225 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4226 putenv(envbuf_Lines);
4227 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4228 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004229 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004230 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004231 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004232# ifdef FEAT_TERMINAL
4233 if (is_terminal)
4234 {
4235 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004236 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004237 putenv(envbuf_Version);
4238 }
4239# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004240# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004241 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4242 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4243 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004244# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004245# endif
4246}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004247
4248 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004249set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004250{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004251 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004252}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004253#endif
4254
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004255#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004256/*
4257 * Open a PTY, with FD for the master and slave side.
4258 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004259 * When successful both file descriptors are stored and the allocated pty name
4260 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004261 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004262 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004263open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004264{
4265 char *tty_name;
4266
Bram Moolenaar59386482019-02-10 22:43:46 +01004267 if (name1 != NULL)
4268 *name1 = NULL;
4269 if (name2 != NULL)
4270 *name2 = NULL;
4271
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004272 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004273 if (*pty_master_fd >= 0)
4274 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004275 // Leaving out O_NOCTTY may lead to waitpid() always returning
4276 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4277 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004278#ifdef O_NOCTTY
4279 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4280#else
4281 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4282#endif
4283 if (*pty_slave_fd < 0)
4284 {
4285 close(*pty_master_fd);
4286 *pty_master_fd = -1;
4287 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004288 else
4289 {
4290 if (name1 != NULL)
4291 *name1 = vim_strsave((char_u *)tty_name);
4292 if (name2 != NULL)
4293 *name2 = vim_strsave((char_u *)tty_name);
4294 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004295 }
4296}
4297#endif
4298
Bram Moolenaarfae42832017-08-01 22:24:26 +02004299/*
4300 * Send SIGINT to a child process if "c" is an interrupt character.
4301 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004302 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004303may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4304{
4305# ifdef SIGINT
4306 if (c == Ctrl_C || c == intr_char)
4307 {
4308# ifdef HAVE_SETSID
4309 kill(-pid, SIGINT);
4310# else
4311 kill(0, SIGINT);
4312# endif
4313 if (wpid > 0)
4314 kill(wpid, SIGINT);
4315 }
4316# endif
4317}
4318
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004319#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004320
Bram Moolenaar0f873732019-12-05 20:28:46 +01004321/*
4322 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4323 * pointers, the last one is NULL.
4324 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4325 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004326 int
4327unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004328 char_u *cmd,
4329 char ***argvp,
4330 char_u **sh_tofree,
4331 char_u **shcf_tofree)
4332{
4333 char **argv = NULL;
4334 int argc;
4335
4336 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004337 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004338 return FAIL;
4339
4340 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4341 return FAIL;
4342 *argvp = argv;
4343
4344 if (cmd != NULL)
4345 {
4346 char_u *s;
4347 char_u *p;
4348
4349 if (extra_shell_arg != NULL)
4350 argv[argc++] = (char *)extra_shell_arg;
4351
Bram Moolenaar0f873732019-12-05 20:28:46 +01004352 // Break 'shellcmdflag' into white separated parts. This doesn't
4353 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004354 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004355 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004356 return FAIL;
4357 s = *shcf_tofree;
4358 p = p_shcf;
4359 while (*p != NUL)
4360 {
4361 argv[argc++] = (char *)s;
4362 while (*p && *p != ' ' && *p != TAB)
4363 *s++ = *p++;
4364 *s++ = NUL;
4365 p = skipwhite(p);
4366 }
4367
4368 argv[argc++] = (char *)cmd;
4369 }
4370 argv[argc] = NULL;
4371 return OK;
4372}
4373#endif
4374
4375#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4376/*
4377 * Use a terminal window to run a shell command in.
4378 */
4379 static int
4380mch_call_shell_terminal(
4381 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004382 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004383{
4384 jobopt_T opt;
4385 char **argv = NULL;
4386 char_u *tofree1 = NULL;
4387 char_u *tofree2 = NULL;
4388 int retval = -1;
4389 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004390 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004391 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004392 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004393
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004394 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004395 goto theend;
4396
4397 init_job_options(&opt);
4398 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4399 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004400 if (buf == NULL)
4401 goto theend;
4402
4403 job = term_getjob(buf->b_term);
4404 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004405
Bram Moolenaar0f873732019-12-05 20:28:46 +01004406 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004407 aucmd_prepbuf(&aco, buf);
4408
4409 clear_oparg(&oa);
4410 while (term_use_loop())
4411 {
4412 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4413 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004414 // If terminal_loop() returns OK we got a key that is handled
4415 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004416 if (terminal_loop(TRUE) == OK)
4417 normal_cmd(&oa, TRUE);
4418 }
4419 else
4420 normal_cmd(&oa, TRUE);
4421 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004422 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004423 ch_log(NULL, "system command finished");
4424
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004425 job_unref(job);
4426
Bram Moolenaar0f873732019-12-05 20:28:46 +01004427 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004428 aucmd_restbuf(&aco);
4429
4430 wait_return(TRUE);
4431 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4432
4433theend:
4434 vim_free(argv);
4435 vim_free(tofree1);
4436 vim_free(tofree2);
4437 return retval;
4438}
4439#endif
4440
4441#ifdef USE_SYSTEM
4442/*
4443 * Use system() to start the shell: simple but slow.
4444 */
4445 static int
4446mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004447 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004448 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449{
4450#ifdef VMS
4451 char *ifn = NULL;
4452 char *ofn = NULL;
4453#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004454 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004455 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004456 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457
4458 out_flush();
4459
4460 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004461 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
Bram Moolenaar62b42182010-09-21 22:09:37 +02004463# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004464 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004465 loose_clipboard();
4466# endif
4467
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 if (cmd == NULL)
4469 x = system((char *)p_sh);
4470 else
4471 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004472# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 if (ofn = strchr((char *)cmd, '>'))
4474 *ofn++ = '\0';
4475 if (ifn = strchr((char *)cmd, '<'))
4476 {
4477 char *p;
4478
4479 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004480 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 if (p)
4482 *p = '\0';
4483 }
4484 if (ofn)
4485 x = vms_sys((char *)cmd, ofn, ifn);
4486 else
4487 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004488# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004489 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004491 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 if (newcmd == NULL)
4493 x = 0;
4494 else
4495 {
4496 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4497 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4498 (char *)p_shcf,
4499 (char *)cmd);
4500 x = system((char *)newcmd);
4501 vim_free(newcmd);
4502 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004503# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505# ifdef VMS
4506 x = vms_sys_status(x);
4507# endif
4508 if (emsg_silent)
4509 ;
4510 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004511 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 else if (x && !(options & SHELL_SILENT))
4513 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004514 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 msg_outnum((long)x);
4516 msg_putchar('\n');
4517 }
4518
4519 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004520 {
4521 // The shell may have messed with the mode, always set it.
4522 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004523 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525# ifdef FEAT_TITLE
4526 resettitle();
4527# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004528# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4529 restore_clipboard();
4530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004532}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
Bram Moolenaar0f873732019-12-05 20:28:46 +01004534#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
Bram Moolenaar0f873732019-12-05 20:28:46 +01004536# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4537 // 127, some shells use that already
4538# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
Bram Moolenaar13568252018-03-16 20:46:58 +01004540/*
4541 * Don't use system(), use fork()/exec().
4542 */
4543 static int
4544mch_call_shell_fork(
4545 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004546 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004547{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004548 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004550 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 pid_t wait_pid = 0;
4552# ifdef HAVE_UNION_WAIT
4553 union wait status;
4554# else
4555 int status = -1;
4556# endif
4557 int retval = -1;
4558 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004559 char_u *tofree1 = NULL;
4560 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004562 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004563# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004565# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004566 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 int fd_fromshell[2];
4568 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004569 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
4571 out_flush();
4572 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004573 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004574 if (tmode == TMODE_RAW)
4575 // The shell may have messed with the mode, always set it later.
4576 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004578 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004579 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004580
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004582 * For the GUI, when writing the output into the buffer and when reading
4583 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4584 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004586 if ((options & (SHELL_READ|SHELL_WRITE))
4587# ifdef FEAT_GUI
4588 || (gui.in_use && show_shell_mess)
4589# endif
4590 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004592# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 /*
4594 * Try to open a master pty.
4595 * If this works, open the slave pty.
4596 * If the slave can't be opened, close the master pty.
4597 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004598 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004599 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 /*
4601 * If not opening a pty or it didn't work, try using pipes.
4602 */
4603 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004604# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004607 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
4609 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004610 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 {
4612 close(fd_toshell[0]);
4613 close(fd_toshell[1]);
4614 }
4615 }
4616 if (pipe_error)
4617 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004618 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 out_flush();
4620 }
4621 }
4622 }
4623
Bram Moolenaar0f873732019-12-05 20:28:46 +01004624 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004626 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004627 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004628 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004629 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004631 UNBLOCK_SIGNALS(&curset);
4632
Bram Moolenaar32526b32019-01-19 17:43:09 +01004633 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004634 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004636 || (gui.in_use && show_shell_mess)
4637# endif
4638 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004640# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004641 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
4643 close(pty_master_fd);
4644 close(pty_slave_fd);
4645 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004646 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004647# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 {
4649 close(fd_toshell[0]);
4650 close(fd_toshell[1]);
4651 close(fd_fromshell[0]);
4652 close(fd_fromshell[1]);
4653 }
4654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004656 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004658 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004659 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
Bram Moolenaar819524702018-02-27 19:10:00 +01004661# ifdef FEAT_JOB_CHANNEL
4662 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01004663 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01004664 ch_logfile((char_u *)"", (char_u *)"");
4665# endif
4666
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 if (!show_shell_mess || (options & SHELL_EXPAND))
4668 {
4669 int fd;
4670
4671 /*
4672 * Don't want to show any message from the shell. Can't just
4673 * close stdout and stderr though, because some systems will
4674 * break if you try to write to them after that, so we must
4675 * use dup() to replace them with something else -- webb
4676 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4677 * waiting for input.
4678 */
4679 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4680 fclose(stdin);
4681 fclose(stdout);
4682 fclose(stderr);
4683
4684 /*
4685 * If any of these open()'s and dup()'s fail, we just continue
4686 * anyway. It's not fatal, and on most systems it will make
4687 * no difference at all. On a few it will cause the execvp()
4688 * to exit with a non-zero status even when the completion
4689 * could be done, which is nothing too serious. If the open()
4690 * or dup() failed we'd just do the same thing ourselves
4691 * anyway -- webb
4692 */
4693 if (fd >= 0)
4694 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004695 vim_ignored = dup(fd); // To replace stdin (fd 0)
4696 vim_ignored = dup(fd); // To replace stdout (fd 1)
4697 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698
Bram Moolenaar0f873732019-12-05 20:28:46 +01004699 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 close(fd);
4701 }
4702 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004703 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004705 || gui.in_use
4706# endif
4707 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
4709
Bram Moolenaardf177f62005-02-22 08:39:57 +00004710# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004711 // Create our own process group, so that the child and all its
4712 // children can be kill()ed. Don't do this when using pipes,
4713 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004714 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004715 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004716 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004717# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004718 // When doing "!xterm&" and 'shell' is bash: the shell
4719 // will exit and send SIGHUP to all processes in its
4720 // group, killing the just started process. Ignore SIGHUP
4721 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004722 signal(SIGHUP, SIG_IGN);
4723# endif
4724 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004725# endif
4726# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004727 if (pty_slave_fd >= 0)
4728 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004729 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004730 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004731 setup_slavepty(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01004733 // Try to become controlling tty (probably doesn't work,
4734 // unless run by root)
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004735 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004737 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004738# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004739 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
Bram Moolenaara5792f52005-11-23 21:25:05 +00004741 /*
4742 * stderr is only redirected when using the GUI, so that a
4743 * program like gpg can still access the terminal to get a
4744 * passphrase using stderr.
4745 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004746# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (pty_master_fd >= 0)
4748 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004749 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
Bram Moolenaar0f873732019-12-05 20:28:46 +01004751 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004753 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004755 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004756 if (gui.in_use)
4757 {
4758 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004759 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761
Bram Moolenaar0f873732019-12-05 20:28:46 +01004762 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
4764 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004765# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004767 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 close(fd_toshell[1]);
4769 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004770 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 close(fd_toshell[0]);
4772
Bram Moolenaar0f873732019-12-05 20:28:46 +01004773 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 close(fd_fromshell[0]);
4775 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004776 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 close(fd_fromshell[1]);
4778
Bram Moolenaara5792f52005-11-23 21:25:05 +00004779# ifdef FEAT_GUI
4780 if (gui.in_use)
4781 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004782 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004783 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004784 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004785 }
4786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 }
4788 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004789
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 /*
4791 * There is no type cast for the argv, because the type may be
4792 * different on different machines. This may cause a warning
4793 * message with strict compilers, don't worry about it.
4794 * Call _exit() instead of exit() to avoid closing the connection
4795 * to the X server (esp. with GTK, which uses atexit()).
4796 */
4797 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004798 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004800 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 {
4802 /*
4803 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004804 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 */
4806 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004807 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004808 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004809# ifdef FEAT_JOB_CHANNEL
4810 ++dont_check_job_ended;
4811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 /*
4813 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004814 * This is also used to pipe stdin/stdout to/from the external
4815 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004817 if ((options & (SHELL_READ|SHELL_WRITE))
4818# ifdef FEAT_GUI
4819 || (gui.in_use && show_shell_mess)
4820# endif
4821 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004823# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004825 int buffer_off = 0; // valid bytes in buffer[]
4826 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4827 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 int len;
4829 int p_more_save;
4830 int old_State;
4831 int c;
4832 int toshell_fd;
4833 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004834 garray_T ga;
4835 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004836# ifdef ELAPSED_FUNC
4837 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
Bram Moolenaardf177f62005-02-22 08:39:57 +00004840# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 if (pty_master_fd >= 0)
4842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 fromshell_fd = pty_master_fd;
4844 toshell_fd = dup(pty_master_fd);
4845 }
4846 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 {
4849 close(fd_toshell[0]);
4850 close(fd_fromshell[1]);
4851 toshell_fd = fd_toshell[1];
4852 fromshell_fd = fd_fromshell[0];
4853 }
4854
4855 /*
4856 * Write to the child if there are typed characters.
4857 * Read from the child if there are characters available.
4858 * Repeat the reading a few times if more characters are
4859 * available. Need to check for typed keys now and then, but
4860 * not too often (delays when no chars are available).
4861 * This loop is quit if no characters can be read from the pty
4862 * (WaitForChar detected special condition), or there are no
4863 * characters available and the child has exited.
4864 * Only check if the child has exited when there is no more
4865 * output. The child may exit before all the output has
4866 * been printed.
4867 *
4868 * Currently this busy loops!
4869 * This can probably dead-lock when the write blocks!
4870 */
4871 p_more_save = p_more;
4872 p_more = FALSE;
4873 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004874 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004876 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004877 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004878 // Fork a process that will write the lines to the
4879 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004880 if ((wpid = fork()) == -1)
4881 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004882 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004883 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004884 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004885 {
4886 linenr_T lnum = curbuf->b_op_start.lnum;
4887 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004888 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004889 size_t l;
4890
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004891 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004892 for (;;)
4893 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004894 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004895 if (l == 0)
4896 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004897 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004898 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004899 len = write(toshell_fd, "", (size_t)1);
4900 else
4901 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004902 char_u *s = vim_strchr(lp + written, NL);
4903
Bram Moolenaar89d40322006-08-29 15:30:07 +00004904 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004905 s == NULL ? l
4906 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004907 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004908 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004909 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004910 // Finished a line, add a NL, unless this line
4911 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004912 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004913 || (!curbuf->b_p_bin
4914 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004915 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004916 && (lnum !=
4917 curbuf->b_ml.ml_line_count
4918 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004919 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004920 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004921 ++lnum;
4922 if (lnum > curbuf->b_op_end.lnum)
4923 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004924 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00004925 close(toshell_fd);
4926 toshell_fd = -1;
4927 break;
4928 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004929 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004930 written = 0;
4931 }
4932 else if (len > 0)
4933 written += len;
4934 }
4935 _exit(0);
4936 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004937 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00004938 {
4939 close(toshell_fd);
4940 toshell_fd = -1;
4941 }
4942 }
4943
4944 if (options & SHELL_READ)
4945 ga_init2(&ga, 1, BUFLEN);
4946
4947 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004948# ifdef ELAPSED_FUNC
4949 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 for (;;)
4952 {
4953 /*
4954 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004955 * if there are any.
4956 * Don't do this if we are expanding wild cards (would eat
4957 * typeahead).
4958 * Don't do this when filtering and terminal is in cooked
4959 * mode, the shell command will handle the I/O. Avoids
4960 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004961 * Don't get characters when the child has already
4962 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004963 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004964 * while (noread_cnt > 4), avoids that ":r !ls" eats
4965 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 */
4967 len = 0;
4968 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004969 && ((options &
4970 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4971 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004972# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004973 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004974# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004975 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004976 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004977 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004979 if (ta_len == 0)
4980 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004981 // Get extra characters when we don't have any.
4982 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004983 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004984# ifdef ELAPSED_FUNC
4985 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004986# endif
4987 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4988 }
4989 if (ta_len > 0 || len > 0)
4990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 /*
4992 * For pipes:
4993 * Check for CTRL-C: send interrupt signal to child.
4994 * Check for CTRL-D: EOF, close pipe to child.
4995 */
4996 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 /*
4999 * Send SIGINT to the child's group or all
5000 * processes in our group.
5001 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005002 may_send_sigint(ta_buf[ta_len], pid, wpid);
5003
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 if (pty_master_fd < 0 && toshell_fd >= 0
5005 && ta_buf[ta_len] == Ctrl_D)
5006 {
5007 close(toshell_fd);
5008 toshell_fd = -1;
5009 }
5010 }
5011
Bram Moolenaarf4140482020-02-15 23:06:45 +01005012 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
5014 /*
5015 * For pipes: echo the typed characters.
5016 * For a pty this does not seem to work.
5017 */
5018 if (pty_master_fd < 0)
5019 {
5020 for (i = ta_len; i < ta_len + len; ++i)
5021 {
5022 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5023 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 else if (has_mbyte)
5025 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005026 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027
5028 msg_outtrans_len(ta_buf + i, l);
5029 i += l - 1;
5030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 else
5032 msg_outtrans_len(ta_buf + i, 1);
5033 }
5034 windgoto(msg_row, msg_col);
5035 out_flush();
5036 }
5037
5038 ta_len += len;
5039
5040 /*
5041 * Write the characters to the child, unless EOF has
5042 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005043 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005044 * When writing buffer lines, drop the typed
5045 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005047 if (options & SHELL_WRITE)
5048 ta_len = 0;
5049 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 {
5051 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5052 if (len > 0)
5053 {
5054 ta_len -= len;
5055 mch_memmove(ta_buf, ta_buf + len, ta_len);
5056 }
5057 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060
Bram Moolenaardf177f62005-02-22 08:39:57 +00005061 if (got_int)
5062 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005063 // CTRL-C sends a signal to the child, we ignore it
5064 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005065# ifdef HAVE_SETSID
5066 kill(-pid, SIGINT);
5067# else
5068 kill(0, SIGINT);
5069# endif
5070 if (wpid > 0)
5071 kill(wpid, SIGINT);
5072 got_int = FALSE;
5073 }
5074
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 /*
5076 * Check if the child has any characters to be printed.
5077 * Read them and write them to our window. Repeat this as
5078 * long as there is something to do, avoid the 10ms wait
5079 * for mch_inchar(), or sending typeahead characters to
5080 * the external process.
5081 * TODO: This should handle escape sequences, compatible
5082 * to some terminal (vt52?).
5083 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005084 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005085 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005087 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005090 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005092
5093 noread_cnt = 0;
5094 if (options & SHELL_READ)
5095 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005096 // Do NUL -> NL translation, append NL separated
5097 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005098 for (i = 0; i < len; ++i)
5099 {
5100 if (buffer[i] == NL)
5101 append_ga_line(&ga);
5102 else if (buffer[i] == NUL)
5103 ga_append(&ga, NL);
5104 else
5105 ga_append(&ga, buffer[i]);
5106 }
5107 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005108 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 {
5110 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005111 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112
Bram Moolenaardf177f62005-02-22 08:39:57 +00005113 len += buffer_off;
5114 buffer[len] = NUL;
5115
Bram Moolenaar0f873732019-12-05 20:28:46 +01005116 // Check if the last character in buffer[] is
5117 // incomplete, keep these bytes for the next
5118 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 for (p = buffer; p < buffer + len; p += l)
5120 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005121 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005123 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 else if (MB_BYTE2LEN(*p) != l)
5125 break;
5126 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005127 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005129 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 if (len >= 12)
5131 ++p;
5132 else
5133 {
5134 buffer_off = len;
5135 continue;
5136 }
5137 }
5138 c = *p;
5139 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005140 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 if (p < buffer + len)
5142 {
5143 *p = c;
5144 buffer_off = (buffer + len) - p;
5145 mch_memmove(buffer, p, buffer_off);
5146 continue;
5147 }
5148 buffer_off = 0;
5149 }
5150 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 {
5152 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005153 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
5155
5156 windgoto(msg_row, msg_col);
5157 cursor_on();
5158 out_flush();
5159 if (got_int)
5160 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005161
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005162# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005163 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005164 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005165 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005166
Bram Moolenaar0f873732019-12-05 20:28:46 +01005167 // Avoid that we keep looping here without
5168 // checking for a CTRL-C for a long time. Don't
5169 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005170 if (msec > 2000)
5171 {
5172 noread_cnt = 5;
5173 break;
5174 }
5175 }
5176# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178
Bram Moolenaar0f873732019-12-05 20:28:46 +01005179 // If we already detected the child has finished, continue
5180 // reading output for a short while. Some text may be
5181 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005182 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005183 {
5184 if (noread_cnt < 5)
5185 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005186 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005187 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005188
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 /*
5190 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005191 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005193# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005194 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005195# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5199 || (wait_pid == pid && WIFEXITED(status)))
5200 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005201 // Don't break the loop yet, try reading more
5202 // characters from "fromshell_fd" first. When using
5203 // pipes there might still be something to read and
5204 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005207 else
5208 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005209
Bram Moolenaar95a51352013-03-21 22:53:50 +01005210# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005211 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005212 clip_update();
5213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
5215finished:
5216 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005217 if (options & SHELL_READ)
5218 {
5219 if (ga.ga_len > 0)
5220 {
5221 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005222 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005223 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005224 }
5225 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005226 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005227 ga_clear(&ga);
5228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 /*
5231 * Give all typeahead that wasn't used back to ui_inchar().
5232 */
5233 if (ta_len)
5234 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 State = old_State;
5236 if (toshell_fd >= 0)
5237 close(toshell_fd);
5238 close(fromshell_fd);
5239 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005240# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005241 else
5242 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005243 long delay_msec = 1;
5244
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005245 /*
5246 * Similar to the loop above, but only handle X events, no
5247 * I/O.
5248 */
5249 for (;;)
5250 {
5251 if (got_int)
5252 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005253 // CTRL-C sends a signal to the child, we ignore it
5254 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005255# ifdef HAVE_SETSID
5256 kill(-pid, SIGINT);
5257# else
5258 kill(0, SIGINT);
5259# endif
5260 got_int = FALSE;
5261 }
5262# ifdef __NeXT__
5263 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5264# else
5265 wait_pid = waitpid(pid, &status, WNOHANG);
5266# endif
5267 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5268 || (wait_pid == pid && WIFEXITED(status)))
5269 {
5270 wait_pid = pid;
5271 break;
5272 }
5273
Bram Moolenaar0f873732019-12-05 20:28:46 +01005274 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005275 clip_update();
5276
Bram Moolenaar0f873732019-12-05 20:28:46 +01005277 // Wait for 1 to 10 msec. 1 is faster but gives the child
5278 // less time.
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005279 mch_delay(delay_msec, TRUE);
5280 if (++delay_msec > 10)
5281 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005282 }
5283 }
5284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
5286 /*
5287 * Wait until our child has exited.
5288 * Ignore wait() returning pids of other children and returning
5289 * because of some signal like SIGWINCH.
5290 * Don't wait if wait_pid was already set above, indicating the
5291 * child already exited.
5292 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005293 if (wait_pid != pid)
5294 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295
Bram Moolenaar624891f2010-10-13 16:22:09 +02005296# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005297 // Close slave side of pty. Only do this after the child has
5298 // exited, otherwise the child may hang when it tries to write on
5299 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005300 if (pty_master_fd >= 0)
5301 close(pty_slave_fd);
5302# endif
5303
Bram Moolenaar0f873732019-12-05 20:28:46 +01005304 // Make sure the child that writes to the external program is
5305 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005306 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005307 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005308 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005309 wait4pid(wpid, NULL);
5310 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005311
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005312# ifdef FEAT_JOB_CHANNEL
5313 --dont_check_job_ended;
5314# endif
5315
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 /*
5317 * Set to raw mode right now, otherwise a CTRL-C after
5318 * catch_signals() will kill Vim.
5319 */
5320 if (tmode == TMODE_RAW)
5321 settmode(TMODE_RAW);
5322 did_settmode = TRUE;
5323 set_signals();
5324
5325 if (WIFEXITED(status))
5326 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005327 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005329 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
5331 if (retval == EXEC_FAILED)
5332 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005333 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 msg_outtrans(p_sh);
5335 msg_putchar('\n');
5336 }
5337 else if (!(options & SHELL_SILENT))
5338 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005339 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 msg_outnum((long)retval);
5341 msg_putchar('\n');
5342 }
5343 }
5344 }
5345 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005346 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
5348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349
5350error:
5351 if (!did_settmode)
5352 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005353 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354# ifdef FEAT_TITLE
5355 resettitle();
5356# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005357 vim_free(argv);
5358 vim_free(tofree1);
5359 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360
5361 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005362}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005363#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005364
5365 int
5366mch_call_shell(
5367 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005368 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005369{
5370#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5371 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5372 return mch_call_shell_terminal(cmd, options);
5373#endif
5374#ifdef USE_SYSTEM
5375 return mch_call_shell_system(cmd, options);
5376#else
5377 return mch_call_shell_fork(cmd, options);
5378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379}
5380
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005381#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005382 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005383mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005384{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005385 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005386 int fd_in[2] = {-1, -1}; // for stdin
5387 int fd_out[2] = {-1, -1}; // for stdout
5388 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005389 int pty_master_fd = -1;
5390 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005391 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005392 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5393 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5394 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005395 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005396 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5397 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005398 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005399 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005400 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005401
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005402 if (use_out_for_err && use_null_for_out)
5403 use_null_for_err = TRUE;
5404
Bram Moolenaar0f873732019-12-05 20:28:46 +01005405 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005406 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005407
Bram Moolenaarb2412082017-08-20 18:09:14 +02005408 if (options->jo_pty
5409 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005410 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005411 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005412 open_pty(&pty_master_fd, &pty_slave_fd,
5413 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005414
Bram Moolenaar0f873732019-12-05 20:28:46 +01005415 // TODO: without the channel feature connect the child to /dev/null?
5416 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005417 if (use_file_for_in)
5418 {
5419 char_u *fname = options->jo_io_name[PART_IN];
5420
5421 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5422 if (fd_in[0] < 0)
5423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005424 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005425 goto failed;
5426 }
5427 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005428 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005429 // When writing buffer lines to the input don't use the pty, so that
5430 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005431 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5432 && pipe(fd_in) < 0)
5433 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005434
5435 if (use_file_for_out)
5436 {
5437 char_u *fname = options->jo_io_name[PART_OUT];
5438
5439 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5440 if (fd_out[1] < 0)
5441 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005442 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005443 goto failed;
5444 }
5445 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005446 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005447 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005448
5449 if (use_file_for_err)
5450 {
5451 char_u *fname = options->jo_io_name[PART_ERR];
5452
5453 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5454 if (fd_err[1] < 0)
5455 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005456 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005457 goto failed;
5458 }
5459 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005460 else if (!use_out_for_err && !use_null_for_err
5461 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005462 goto failed;
5463
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005464 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5465 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005466 if (options->jo_set & JO_CHANNEL)
5467 {
5468 channel = options->jo_channel;
5469 if (channel != NULL)
5470 ++channel->ch_refcount;
5471 }
5472 else
5473 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005474 if (channel == NULL)
5475 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005476 if (job->jv_tty_out != NULL)
5477 ch_log(channel, "using pty %s on fd %d",
5478 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005479 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005480
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005481 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005482 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005483 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005484 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005485 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005486 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005487 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005488 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005489 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005490 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005491 int null_fd = -1;
5492 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005493
Bram Moolenaar0f873732019-12-05 20:28:46 +01005494 // child
5495 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005496 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005497
Bram Moolenaar819524702018-02-27 19:10:00 +01005498# ifdef FEAT_JOB_CHANNEL
5499 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005500 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005501 ch_logfile((char_u *)"", (char_u *)"");
5502# endif
5503
Bram Moolenaar835dc632016-02-07 14:27:38 +01005504# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005505 // Create our own process group, so that the child and all its
5506 // children can be kill()ed. Don't do this when using pipes,
5507 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005508 (void)setsid();
5509# endif
5510
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005511# ifdef FEAT_TERMINAL
5512 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005513 {
5514 char *term = (char *)T_NAME;
5515
5516#ifdef FEAT_GUI
5517 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005518 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005519 term = getenv("TERM");
5520#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005521 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005522 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005523 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005524 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005525 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005526 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005527 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005528 else if (t_colors > 16)
5529 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005530 else
5531 term = "xterm";
5532 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005533 set_child_environment(
5534 (long)options->jo_term_rows,
5535 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005536 term,
5537 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005538 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005539 else
5540# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005541 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005542
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005543 if (options->jo_env != NULL)
5544 {
5545 dict_T *dict = options->jo_env;
5546 hashitem_T *hi;
5547 int todo = (int)dict->dv_hashtab.ht_used;
5548
5549 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5550 if (!HASHITEM_EMPTY(hi))
5551 {
5552 typval_T *item = &dict_lookup(hi)->di_tv;
5553
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005554 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005555 --todo;
5556 }
5557 }
5558
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005559 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005560 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005561 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005562 if (null_fd < 0)
5563 {
5564 perror("opening /dev/null failed");
5565 _exit(OPEN_NULL_FAILED);
5566 }
5567 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005568
Bram Moolenaar223896d2017-08-02 22:33:28 +02005569 if (pty_slave_fd >= 0)
5570 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005571 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005572 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005573# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005574 // Try to become controlling tty (probably doesn't work,
5575 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005576 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5577# endif
5578 }
5579
Bram Moolenaar0f873732019-12-05 20:28:46 +01005580 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005581 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005582 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005583 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005584 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005585 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005586 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005587 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005588
Bram Moolenaar0f873732019-12-05 20:28:46 +01005589 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005590 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005591 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005592 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005593 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005594 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005595 }
5596 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005597 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005598 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005599 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005600 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005601 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005602
Bram Moolenaar0f873732019-12-05 20:28:46 +01005603 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005604 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005605 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005606 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005607 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005608 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005609 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005610 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005611
5612 if (fd_in[0] >= 0)
5613 close(fd_in[0]);
5614 if (fd_in[1] >= 0)
5615 close(fd_in[1]);
5616 if (fd_out[0] >= 0)
5617 close(fd_out[0]);
5618 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005619 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005620 if (fd_err[0] >= 0)
5621 close(fd_err[0]);
5622 if (fd_err[1] >= 0)
5623 close(fd_err[1]);
5624 if (pty_master_fd >= 0)
5625 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005626 close(pty_master_fd); // not used in the child
5627 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005628 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005629
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005630 if (null_fd >= 0)
5631 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005632
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005633 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5634 _exit(EXEC_FAILED);
5635
Bram Moolenaar0f873732019-12-05 20:28:46 +01005636 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005637 execvp(argv[0], argv);
5638
Bram Moolenaar4694a172016-04-21 14:05:23 +02005639 if (stderr_works)
5640 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005641# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005642 // calling free_all_mem() here causes problems. Ignore valgrind
5643 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005644# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005645 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005646 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005647
Bram Moolenaar0f873732019-12-05 20:28:46 +01005648 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005649 UNBLOCK_SIGNALS(&curset);
5650
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005651 job->jv_pid = pid;
5652 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005653 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005654
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005655 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005656 close(pty_slave_fd); // not used in the parent
5657 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005658 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005659 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005660 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005661 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005662 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005663 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005664 if (channel != NULL)
5665 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005666 int in_fd = INVALID_FD;
5667 int out_fd = INVALID_FD;
5668 int err_fd = INVALID_FD;
5669
5670 if (!(use_file_for_in || use_null_for_in))
5671 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5672
5673 if (!(use_file_for_out || use_null_for_out))
5674 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5675
5676 // When using pty_master_fd only set it for stdout, do not duplicate
5677 // it for stderr, it only needs to be read once.
5678 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5679 {
5680 if (fd_err[0] >= 0)
5681 err_fd = fd_err[0];
5682 else if (out_fd != pty_master_fd)
5683 err_fd = pty_master_fd;
5684 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005685
5686 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005687 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005688 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005689 else
5690 {
5691 if (fd_in[1] >= 0)
5692 close(fd_in[1]);
5693 if (fd_out[0] >= 0)
5694 close(fd_out[0]);
5695 if (fd_err[0] >= 0)
5696 close(fd_err[0]);
5697 if (pty_master_fd >= 0)
5698 close(pty_master_fd);
5699 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005700
Bram Moolenaar0f873732019-12-05 20:28:46 +01005701 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005702 return;
5703
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005704failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005705 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005706 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005707 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005708 if (fd_in[1] >= 0)
5709 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005710 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005711 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005712 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005713 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005714 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005715 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005716 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005717 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005718 if (pty_master_fd >= 0)
5719 close(pty_master_fd);
5720 if (pty_slave_fd >= 0)
5721 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005722}
5723
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005724 static char_u *
5725get_signal_name(int sig)
5726{
5727 int i;
5728 char_u numbuf[NUMBUFLEN];
5729
5730 if (sig == SIGKILL)
5731 return vim_strsave((char_u *)"kill");
5732
5733 for (i = 0; signal_info[i].sig != -1; i++)
5734 if (sig == signal_info[i].sig)
5735 return strlow_save((char_u *)signal_info[i].name);
5736
5737 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5738 return vim_strsave(numbuf);
5739}
5740
Bram Moolenaar835dc632016-02-07 14:27:38 +01005741 char *
5742mch_job_status(job_T *job)
5743{
5744# ifdef HAVE_UNION_WAIT
5745 union wait status;
5746# else
5747 int status = -1;
5748# endif
5749 pid_t wait_pid = 0;
5750
5751# ifdef __NeXT__
5752 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5753# else
5754 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5755# endif
5756 if (wait_pid == -1)
5757 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005758 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005759 if (job->jv_status < JOB_ENDED)
5760 ch_log(job->jv_channel, "Job no longer exists: %s",
5761 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005762 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005763 }
5764 if (wait_pid == 0)
5765 return "run";
5766 if (WIFEXITED(status))
5767 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005768 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005769 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005770 if (job->jv_status < JOB_ENDED)
5771 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005772 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005773 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005774 if (WIFSIGNALED(status))
5775 {
5776 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005777 job->jv_termsig = get_signal_name(WTERMSIG(status));
5778 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5779 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5780 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005781 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005782 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005783 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005784
5785return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005786 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005787 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005788 return "dead";
5789}
5790
5791 job_T *
5792mch_detect_ended_job(job_T *job_list)
5793{
5794# ifdef HAVE_UNION_WAIT
5795 union wait status;
5796# else
5797 int status = -1;
5798# endif
5799 pid_t wait_pid = 0;
5800 job_T *job;
5801
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005802# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005803 // Do not do this when waiting for a shell command to finish, we would get
5804 // the exit value here (and discard it), the exit value obtained there
5805 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005806 if (dont_check_job_ended > 0)
5807 return NULL;
5808# endif
5809
Bram Moolenaar97792de2016-10-15 18:36:49 +02005810# ifdef __NeXT__
5811 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5812# else
5813 wait_pid = waitpid(-1, &status, WNOHANG);
5814# endif
5815 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005816 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005817 return NULL;
5818 for (job = job_list; job != NULL; job = job->jv_next)
5819 {
5820 if (job->jv_pid == wait_pid)
5821 {
5822 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005823 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005824 job->jv_exitval = WEXITSTATUS(status);
5825 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005826 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005827 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005828 job->jv_termsig = get_signal_name(WTERMSIG(status));
5829 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005830 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005831 {
5832 ch_log(job->jv_channel, "Job ended");
5833 job->jv_status = JOB_ENDED;
5834 }
5835 return job;
5836 }
5837 }
5838 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005839}
5840
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005841/*
5842 * Send a (deadly) signal to "job".
5843 * Return FAIL if "how" is not a valid name.
5844 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005845 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005846mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005847{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005848 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005849
Bram Moolenaar923d9262016-02-25 20:56:01 +01005850 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005851 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005852 else if (STRCMP(how, "hup") == 0)
5853 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005854 else if (STRCMP(how, "quit") == 0)
5855 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005856 else if (STRCMP(how, "int") == 0)
5857 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005858 else if (STRCMP(how, "kill") == 0)
5859 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005860#ifdef SIGWINCH
5861 else if (STRCMP(how, "winch") == 0)
5862 sig = SIGWINCH;
5863#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005864 else if (isdigit(*how))
5865 sig = atoi((char *)how);
5866 else
5867 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005868
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005869 // Never kill ourselves!
5870 if (job->jv_pid != 0)
5871 {
5872 // TODO: have an option to only kill the process, not the group?
5873 kill(-job->jv_pid, sig);
5874 kill(job->jv_pid, sig);
5875 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005876
Bram Moolenaar835dc632016-02-07 14:27:38 +01005877 return OK;
5878}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005879
5880/*
5881 * Clear the data related to "job".
5882 */
5883 void
5884mch_clear_job(job_T *job)
5885{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005886 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005887# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005888 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005889# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005890 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005891# endif
5892}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005893#endif
5894
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005895#if defined(FEAT_TERMINAL) || defined(PROTO)
5896 int
5897mch_create_pty_channel(job_T *job, jobopt_T *options)
5898{
5899 int pty_master_fd = -1;
5900 int pty_slave_fd = -1;
5901 channel_T *channel;
5902
Bram Moolenaar59386482019-02-10 22:43:46 +01005903 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005904 close(pty_slave_fd);
5905
5906 channel = add_channel();
5907 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005908 {
5909 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005910 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005911 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005912 if (job->jv_tty_out != NULL)
5913 ch_log(channel, "using pty %s on fd %d",
5914 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005915 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005916 channel->ch_keep_open = TRUE;
5917
Bram Moolenaar0f873732019-12-05 20:28:46 +01005918 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5919 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005920 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005921 channel_set_job(channel, job, options);
5922 return OK;
5923}
5924#endif
5925
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926/*
5927 * Check for CTRL-C typed by reading all available characters.
5928 * In cooked mode we should get SIGINT, no need to check.
5929 */
5930 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005931mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932{
Bram Moolenaar26e86442020-05-17 14:06:16 +02005933 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005934 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 fill_input_buf(FALSE);
5936}
5937
5938/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005939 * Wait "msec" msec until a character is available from the mouse, keyboard,
5940 * from inbuf[].
5941 * "msec" == -1 will block forever.
5942 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005943 * When "ignore_input" is TRUE even check for pending input when input is
5944 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005945 * "interrupted" (if not NULL) is set to TRUE when no character is available
5946 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005947 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005948 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 */
5950 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005951WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005953#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005954 return ui_wait_for_chars_or_timer(
5955 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005956#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005957 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005958#endif
5959}
5960
5961/*
5962 * Wait "msec" msec until a character is available from the mouse or keyboard
5963 * or from inbuf[].
5964 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005965 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005966 * "interrupted" (if not NULL) is set to TRUE when no character is available
5967 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005968 * When a GUI is being used, this will never get called -- webb
5969 */
5970 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005971WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005972{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973#ifdef FEAT_MOUSE_GPM
5974 int gpm_process_wanted;
5975#endif
5976#ifdef FEAT_XCLIPBOARD
5977 int rest;
5978#endif
5979 int avail;
5980
Bram Moolenaar0f873732019-12-05 20:28:46 +01005981 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 return 1;
5983
5984#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005985 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 if (WantQueryMouse)
5987 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005988 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02005989 if (!no_query_mouse_for_testing)
5990 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 }
5992#endif
5993
5994 /*
5995 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5996 * events. This is a bit complicated, because they might both be defined.
5997 */
5998#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5999# ifdef FEAT_XCLIPBOARD
6000 rest = 0;
6001 if (do_xterm_trace())
6002 rest = msec;
6003# endif
6004 do
6005 {
6006# ifdef FEAT_XCLIPBOARD
6007 if (rest != 0)
6008 {
6009 msec = XT_TRACE_DELAY;
6010 if (rest >= 0 && rest < XT_TRACE_DELAY)
6011 msec = rest;
6012 if (rest >= 0)
6013 rest -= msec;
6014 }
6015# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006016# ifdef FEAT_SOUND_CANBERRA
6017 // Invoke any pending sound callbacks.
6018 if (has_sound_callback_in_queue())
6019 invoke_sound_callback();
6020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021# ifdef FEAT_MOUSE_GPM
6022 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006023 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006024 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006025 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006027 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006029# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006031 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 return 1;
6033# ifdef FEAT_XCLIPBOARD
6034 if (rest == 0 || !do_xterm_trace())
6035# endif
6036 break;
6037 }
6038 }
6039 while (FALSE
6040# ifdef FEAT_MOUSE_GPM
6041 || (gpm_process_wanted && mch_gpm_process() == 0)
6042# endif
6043# ifdef FEAT_XCLIPBOARD
6044 || (!avail && rest != 0)
6045# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006046 )
6047 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048
6049#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006050 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051#endif
6052 return avail;
6053}
6054
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006055#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056/*
6057 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006058 * "msec" == 0 will check for characters once.
6059 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006062 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006063 * "interrupted" (if not NULL) is set to TRUE when no character is available
6064 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006066 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006067RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
6069 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006070 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006071#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 static int busy = FALSE;
6073
Bram Moolenaar0f873732019-12-05 20:28:46 +01006074 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075# define MAY_LOOP
6076
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006077# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006078 // Remember at what time we started, so that we know how much longer we
6079 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006080 long start_msec = msec;
6081 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006083 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006084 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085# endif
6086
Bram Moolenaar0f873732019-12-05 20:28:46 +01006087 // Handle being called recursively. This may happen for the session
6088 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 if (busy)
6090 return 0;
6091#endif
6092
6093#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006094 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095#endif
6096 {
6097#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006098 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006099# ifdef FEAT_MZSCHEME
6100 int mzquantum_used = FALSE;
6101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102#endif
6103#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006104 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006105 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 int nfd;
6107# ifdef FEAT_XCLIPBOARD
6108 int xterm_idx = -1;
6109# endif
6110# ifdef FEAT_MOUSE_GPM
6111 int gpm_idx = -1;
6112# endif
6113# ifdef USE_XSMP
6114 int xsmp_idx = -1;
6115# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006116 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006118# ifdef FEAT_MZSCHEME
6119 mzvim_check_threads();
6120 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6121 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006122 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006123 mzquantum_used = TRUE;
6124 }
6125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 fds[0].fd = fd;
6127 fds[0].events = POLLIN;
6128 nfd = 1;
6129
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006131 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 if (xterm_Shell != (Widget)0)
6133 {
6134 xterm_idx = nfd;
6135 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6136 fds[nfd].events = POLLIN;
6137 nfd++;
6138 }
6139# endif
6140# ifdef FEAT_MOUSE_GPM
6141 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6142 {
6143 gpm_idx = nfd;
6144 fds[nfd].fd = gpm_fd;
6145 fds[nfd].events = POLLIN;
6146 nfd++;
6147 }
6148# endif
6149# ifdef USE_XSMP
6150 if (xsmp_icefd != -1)
6151 {
6152 xsmp_idx = nfd;
6153 fds[nfd].fd = xsmp_icefd;
6154 fds[nfd].events = POLLIN;
6155 nfd++;
6156 }
6157# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006158#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006159 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006160#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006161 if (interrupted != NULL)
6162 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006164 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006165
6166 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006167 if (result == 0 && interrupted != NULL && ret > 0)
6168 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006169
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006170# ifdef FEAT_MZSCHEME
6171 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006172 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006173 finished = FALSE;
6174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176# ifdef FEAT_XCLIPBOARD
6177 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6178 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006179 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006181 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 finished = FALSE;
6183 }
6184# endif
6185# ifdef FEAT_MOUSE_GPM
6186 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188# endif
6189# ifdef USE_XSMP
6190 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6191 {
6192 if (fds[xsmp_idx].revents & POLLIN)
6193 {
6194 busy = TRUE;
6195 xsmp_handle_requests();
6196 busy = FALSE;
6197 }
6198 else if (fds[xsmp_idx].revents & POLLHUP)
6199 {
6200 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006201 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 xsmp_close();
6203 }
6204 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006205 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 }
6207# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006208#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006209 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006210 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006211 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213
Bram Moolenaar0f873732019-12-05 20:28:46 +01006214#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
6216 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006217 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006218 // These are static because they can take 8 Kbyte each and cause the
6219 // signal stack to run out with -O3.
6220 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006222 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006224# ifdef FEAT_MZSCHEME
6225 mzvim_check_threads();
6226 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6227 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006228 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006229 mzquantum_used = TRUE;
6230 }
6231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006233 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006235 tv.tv_sec = towait / 1000;
6236 tv.tv_usec = (towait % 1000) * (1000000/1000);
6237 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006239 else
6240 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
6242 /*
6243 * Select on ready for reading and exceptional condition (end of file).
6244 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006245select_eintr:
6246 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006247 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 FD_ZERO(&efds);
6249 FD_SET(fd, &rfds);
6250# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006251 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 FD_SET(fd, &efds);
6253# endif
6254 maxfd = fd;
6255
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006257 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (xterm_Shell != (Widget)0)
6259 {
6260 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6261 if (maxfd < ConnectionNumber(xterm_dpy))
6262 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006263
Bram Moolenaar0f873732019-12-05 20:28:46 +01006264 // An event may have already been read but not handled. In
6265 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006266 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 }
6268# endif
6269# ifdef FEAT_MOUSE_GPM
6270 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6271 {
6272 FD_SET(gpm_fd, &rfds);
6273 FD_SET(gpm_fd, &efds);
6274 if (maxfd < gpm_fd)
6275 maxfd = gpm_fd;
6276 }
6277# endif
6278# ifdef USE_XSMP
6279 if (xsmp_icefd != -1)
6280 {
6281 FD_SET(xsmp_icefd, &rfds);
6282 FD_SET(xsmp_icefd, &efds);
6283 if (maxfd < xsmp_icefd)
6284 maxfd = xsmp_icefd;
6285 }
6286# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006287# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006288 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006289# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006290 if (interrupted != NULL)
6291 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292
Bram Moolenaar643b6142018-09-12 20:29:09 +02006293 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6294 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006295 result = ret > 0 && FD_ISSET(fd, &rfds);
6296 if (result)
6297 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006298 else if (interrupted != NULL && ret > 0)
6299 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006300
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006301# ifdef EINTR
6302 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006303 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006304 // Check whether window has been resized, EINTR may be caused by
6305 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006306 if (do_resize)
6307 handle_resize();
6308
Bram Moolenaar0f873732019-12-05 20:28:46 +01006309 // Interrupted by a signal, need to try again. We ignore msec
6310 // here, because we do want to check even after a timeout if
6311 // characters are available. Needed for reading output of an
6312 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006313 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006314 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006315# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006316# ifdef __TANDEM
6317 if (ret == -1 && errno == ENOTSUP)
6318 {
6319 FD_ZERO(&rfds);
6320 FD_ZERO(&efds);
6321 ret = 0;
6322 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006323# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006324# ifdef FEAT_MZSCHEME
6325 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006326 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006327 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328# endif
6329
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330# ifdef FEAT_XCLIPBOARD
6331 if (ret > 0 && xterm_Shell != (Widget)0
6332 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6333 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006334 xterm_update(); // Maybe we should hand out clipboard
6335 // continue looping when we only got the X event and the input
6336 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 if (--ret == 0 && !input_available())
6338 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006339 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 finished = FALSE;
6341 }
6342 }
6343# endif
6344# ifdef FEAT_MOUSE_GPM
6345 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6346 {
6347 if (FD_ISSET(gpm_fd, &efds))
6348 gpm_close();
6349 else if (FD_ISSET(gpm_fd, &rfds))
6350 *check_for_gpm = 1;
6351 }
6352# endif
6353# ifdef USE_XSMP
6354 if (ret > 0 && xsmp_icefd != -1)
6355 {
6356 if (FD_ISSET(xsmp_icefd, &efds))
6357 {
6358 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006359 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 xsmp_close();
6361 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006362 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 }
6364 else if (FD_ISSET(xsmp_icefd, &rfds))
6365 {
6366 busy = TRUE;
6367 xsmp_handle_requests();
6368 busy = FALSE;
6369 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006370 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 }
6372 }
6373# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006374#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006375 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006376 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006377 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
Bram Moolenaar0f873732019-12-05 20:28:46 +01006380#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381
6382#ifdef MAY_LOOP
6383 if (finished || msec == 0)
6384 break;
6385
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006386# ifdef FEAT_CLIENTSERVER
6387 if (server_waiting())
6388 break;
6389# endif
6390
Bram Moolenaar0f873732019-12-05 20:28:46 +01006391 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 if (msec > 0)
6393 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006394# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006395 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006396 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006398 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 msec = msec / 2;
6400# endif
6401 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006402 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 }
6404#endif
6405 }
6406
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006407 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408}
6409
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006411 * Expand a path into all matching files and/or directories. Handles "*",
6412 * "?", "[a-z]", "**", etc.
6413 * "path" has backslashes before chars that are not to be expanded.
6414 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 */
6416 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006417mch_expandpath(
6418 garray_T *gap,
6419 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006420 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421{
Bram Moolenaar02743632005-07-25 20:42:36 +00006422 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424
6425/*
6426 * mch_expand_wildcards() - this code does wild-card pattern matching using
6427 * the shell
6428 *
6429 * return OK for success, FAIL for error (you may lose some memory) and put
6430 * an error message in *file.
6431 *
6432 * num_pat is number of input patterns
6433 * pat is array of pointers to input patterns
6434 * num_file is pointer to number of matched file names
6435 * file is pointer to array of pointers to matched file names
6436 */
6437
6438#ifndef SEEK_SET
6439# define SEEK_SET 0
6440#endif
6441#ifndef SEEK_END
6442# define SEEK_END 2
6443#endif
6444
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006445#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006446
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006448mch_expand_wildcards(
6449 int num_pat,
6450 char_u **pat,
6451 int *num_file,
6452 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006453 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
6455 int i;
6456 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006457 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 char_u *p;
6459 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460
Bram Moolenaarc7247912008-01-13 12:54:11 +00006461 /*
6462 * This is the non-OS/2 implementation (really Unix).
6463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 int j;
6465 char_u *tempname;
6466 char_u *command;
6467 FILE *fd;
6468 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006469#define STYLE_ECHO 0 // use "echo", the default
6470#define STYLE_GLOB 1 // use "glob", for csh
6471#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6472#define STYLE_PRINT 3 // use "print -N", for zsh
6473#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6474 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 int shell_style = STYLE_ECHO;
6476 int check_spaces;
6477 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006478 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006479 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006480 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481
Bram Moolenaar0f873732019-12-05 20:28:46 +01006482 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 *file = NULL;
6484
6485 /*
6486 * If there are no wildcards, just copy the names to allocated memory.
6487 * Saves a lot of time, because we don't have to start a new shell.
6488 */
6489 if (!have_wildcard(num_pat, pat))
6490 return save_patterns(num_pat, pat, num_file, file);
6491
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006492# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006493 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006494 if (sandbox != 0 && check_secure())
6495 return FAIL;
6496# endif
6497
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 /*
6499 * Don't allow the use of backticks in secure and restricted mode.
6500 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006501 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 for (i = 0; i < num_pat; ++i)
6503 if (vim_strchr(pat[i], '`') != NULL
6504 && (check_restricted() || check_secure()))
6505 return FAIL;
6506
6507 /*
6508 * get a name for the temp file
6509 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006510 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006512 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 return FAIL;
6514 }
6515
6516 /*
6517 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006518 * file.
6519 * STYLE_BT: NL separated
6520 * If expanding `cmd` execute it directly.
6521 * STYLE_GLOB: NUL separated
6522 * If we use *csh, "glob" will work better than "echo".
6523 * STYLE_PRINT: NL or NUL separated
6524 * If we use *zsh, "print -N" will work better than "glob".
6525 * STYLE_VIMGLOB: NL separated
6526 * If we use *sh*, we define "vimglob()".
6527 * STYLE_ECHO: space separated.
6528 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 */
6530 if (num_pat == 1 && *pat[0] == '`'
6531 && (len = STRLEN(pat[0])) > 2
6532 && *(pat[0] + len - 1) == '`')
6533 shell_style = STYLE_BT;
6534 else if ((len = STRLEN(p_sh)) >= 3)
6535 {
6536 if (STRCMP(p_sh + len - 3, "csh") == 0)
6537 shell_style = STYLE_GLOB;
6538 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6539 shell_style = STYLE_PRINT;
6540 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006541 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6542 "sh") != NULL)
6543 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544
Bram Moolenaar0f873732019-12-05 20:28:46 +01006545 // Compute the length of the command. We need 2 extra bytes: for the
6546 // optional '&' and for the NUL.
6547 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006549 if (shell_style == STYLE_VIMGLOB)
6550 len += STRLEN(sh_vimglob_func);
6551
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006552 for (i = 0; i < num_pat; ++i)
6553 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006554 // Count the length of the patterns in the same way as they are put in
6555 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006556#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006557 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006558#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006559 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006560 for (j = 0; pat[i][j] != NUL; ++j)
6561 {
6562 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006563 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006564 ++len;
6565 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006566#endif
6567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 command = alloc(len);
6569 if (command == NULL)
6570 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006571 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 vim_free(tempname);
6573 return FAIL;
6574 }
6575
6576 /*
6577 * Build the shell command:
6578 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6579 * recognizes this).
6580 * - Add the shell command to print the expanded names.
6581 * - Add the temp file name.
6582 * - Add the file name patterns.
6583 */
6584 if (shell_style == STYLE_BT)
6585 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006586 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006587 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006588 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006590 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006591 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006593 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006595 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 *p = ' ';
6597 }
6598 STRCAT(command, ">");
6599 }
6600 else
6601 {
6602 if (flags & EW_NOTFOUND)
6603 STRCPY(command, "set nonomatch; ");
6604 else
6605 STRCPY(command, "unset nonomatch; ");
6606 if (shell_style == STYLE_GLOB)
6607 STRCAT(command, "glob >");
6608 else if (shell_style == STYLE_PRINT)
6609 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006610 else if (shell_style == STYLE_VIMGLOB)
6611 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 else
6613 STRCAT(command, "echo >");
6614 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006615
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006617
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 if (shell_style != STYLE_BT)
6619 for (i = 0; i < num_pat; ++i)
6620 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006621 // When using system() always add extra quotes, because the shell
6622 // is started twice. Otherwise put a backslash before special
6623 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624#ifdef USE_SYSTEM
6625 STRCAT(command, " \"");
6626 STRCAT(command, pat[i]);
6627 STRCAT(command, "\"");
6628#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006629 int intick = FALSE;
6630
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 p = command + STRLEN(command);
6632 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006633 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006634 {
6635 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006636 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006637 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6638 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006639 // Remove a backslash, take char literally. But keep
6640 // backslash inside backticks, before a special character
6641 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006642 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006643 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6644 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006645 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006646 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006647 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006648 else if (!intick
6649 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6650 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006651 // Put a backslash before a special character, but not
6652 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6653 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006654 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006655
Bram Moolenaar0f873732019-12-05 20:28:46 +01006656 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006657 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 *p = NUL;
6660#endif
6661 }
6662 if (flags & EW_SILENT)
6663 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006664 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006665 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
6667 /*
6668 * Using zsh -G: If a pattern has no matches, it is just deleted from
6669 * the argument list, otherwise zsh gives an error message and doesn't
6670 * expand any other pattern.
6671 */
6672 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006673 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674
6675 /*
6676 * If we use -f then shell variables set in .cshrc won't get expanded.
6677 * vi can do it, so we will too, but it is only necessary if there is a "$"
6678 * in one of the patterns, otherwise we can still use the fast option.
6679 */
6680 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006681 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
6683 /*
6684 * execute the shell command
6685 */
6686 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6687
Bram Moolenaar0f873732019-12-05 20:28:46 +01006688 // When running in the background, give it some time to create the temp
6689 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006690 if (ampersand)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 mch_delay(10L, TRUE);
6692
Bram Moolenaar0f873732019-12-05 20:28:46 +01006693 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 show_shell_mess = TRUE;
6695 vim_free(command);
6696
Bram Moolenaar0f873732019-12-05 20:28:46 +01006697 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 {
6699 mch_remove(tempname);
6700 vim_free(tempname);
6701 /*
6702 * With interactive completion, the error message is not printed.
6703 * However with USE_SYSTEM, I don't know how to turn off error messages
6704 * from the shell, so screen may still get messed up -- webb.
6705 */
6706#ifndef USE_SYSTEM
6707 if (!(flags & EW_SILENT))
6708#endif
6709 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006710 redraw_later_clear(); // probably messed up screen
6711 msg_putchar('\n'); // clear bottom line quickly
6712 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713#ifdef USE_SYSTEM
6714 if (!(flags & EW_SILENT))
6715#endif
6716 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006717 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006718 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 }
6720 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006721 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6722 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 if (shell_style == STYLE_BT)
6724 return FAIL;
6725 goto notfound;
6726 }
6727
6728 /*
6729 * read the names from the file into memory
6730 */
6731 fd = fopen((char *)tempname, READBIN);
6732 if (fd == NULL)
6733 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006734 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 if (!(flags & EW_SILENT))
6736 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006737 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006738 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 }
6740 vim_free(tempname);
6741 goto notfound;
6742 }
6743 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006744 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006746 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006747 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006748 buffer = NULL;
6749 else
6750 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 if (buffer == NULL)
6752 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006753 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 mch_remove(tempname);
6755 vim_free(tempname);
6756 fclose(fd);
6757 return FAIL;
6758 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006759 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 i = fread((char *)buffer, 1, len, fd);
6761 fclose(fd);
6762 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006763 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006765 // unexpected read error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006766 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 vim_free(tempname);
6768 vim_free(buffer);
6769 return FAIL;
6770 }
6771 vim_free(tempname);
6772
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006773# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006774 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006776 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6778 *p++ = buffer[i];
6779 len = p - buffer;
6780# endif
6781
6782
Bram Moolenaar0f873732019-12-05 20:28:46 +01006783 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 if (shell_style == STYLE_ECHO)
6785 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006786 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006788 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 {
6790 while (*p != ' ' && *p != '\n')
6791 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006792 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 }
6794 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006795 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006796 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006798 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006800 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {
6802 while (*p != '\n' && *p != NUL)
6803 ++p;
6804 if (*p != NUL)
6805 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006806 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 }
6808 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006809 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 else
6811 {
6812 /*
6813 * Some versions of zsh use spaces instead of NULs to separate
6814 * results. Only do this when there is no NUL before the end of the
6815 * buffer, otherwise we would never be able to use file names with
6816 * embedded spaces when zsh does use NULs.
6817 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6818 * don't check for spaces again.
6819 */
6820 check_spaces = FALSE;
6821 if (shell_style == STYLE_PRINT && !did_find_nul)
6822 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006823 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006824 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006825 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 did_find_nul = TRUE;
6827 else
6828 check_spaces = TRUE;
6829 }
6830
6831 /*
6832 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6833 * already is one, for STYLE_GLOB it needs to be added.
6834 */
6835 if (len && buffer[len - 1] == NUL)
6836 --len;
6837 else
6838 buffer[len] = NUL;
6839 i = 0;
6840 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006841 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 {
6843 ++i;
6844 *p = NUL;
6845 }
6846 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006847 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 }
6849 if (i == 0)
6850 {
6851 /*
6852 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6853 * /bin/sh will happily expand it to nothing rather than returning an
6854 * error; and hey, it's good to check anyway -- webb.
6855 */
6856 vim_free(buffer);
6857 goto notfound;
6858 }
6859 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006860 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 if (*file == NULL)
6862 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006863 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 vim_free(buffer);
6865 return FAIL;
6866 }
6867
6868 /*
6869 * Isolate the individual file names.
6870 */
6871 p = buffer;
6872 for (i = 0; i < *num_file; ++i)
6873 {
6874 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006875 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006876 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6877 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006879 while (!(shell_style == STYLE_ECHO && *p == ' ')
6880 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006882 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 *p = NUL;
6884 else
6885 {
6886 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006887 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 }
6889 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006890 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006892 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006894 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 }
6896 }
6897
6898 /*
6899 * Move the file names to allocated memory.
6900 */
6901 for (j = 0, i = 0; i < *num_file; ++i)
6902 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006903 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6905 continue;
6906
Bram Moolenaar0f873732019-12-05 20:28:46 +01006907 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 dir = (mch_isdir((*file)[i]));
6909 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6910 continue;
6911
Bram Moolenaar0f873732019-12-05 20:28:46 +01006912 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01006913 if (!dir && (flags & EW_EXEC)
6914 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006915 continue;
6916
Bram Moolenaar964b3742019-05-24 18:54:09 +02006917 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 if (p)
6919 {
6920 STRCPY(p, (*file)[i]);
6921 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006922 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 (*file)[j++] = p;
6924 }
6925 }
6926 vim_free(buffer);
6927 *num_file = j;
6928
Bram Moolenaar0f873732019-12-05 20:28:46 +01006929 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006931 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 goto notfound;
6933 }
6934
6935 return OK;
6936
6937notfound:
6938 if (flags & EW_NOTFOUND)
6939 return save_patterns(num_pat, pat, num_file, file);
6940 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941}
6942
Bram Moolenaar0f873732019-12-05 20:28:46 +01006943#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006946save_patterns(
6947 int num_pat,
6948 char_u **pat,
6949 int *num_file,
6950 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951{
6952 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006953 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006955 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 if (*file == NULL)
6957 return FAIL;
6958 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006959 {
6960 s = vim_strsave(pat[i]);
6961 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006962 // Be compatible with expand_filename(): halve the number of
6963 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00006964 backslash_halve(s);
6965 (*file)[i] = s;
6966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 *num_file = num_pat;
6968 return OK;
6969}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971/*
6972 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6973 * expand.
6974 */
6975 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006976mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006978 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 if (*p == '\\' && p[1] != NUL)
6981 ++p;
6982 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (vim_strchr((char_u *)
6984#ifdef VMS
6985 "*?%"
6986#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988#endif
6989 , *p) != NULL)
6990 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 }
6992 return FALSE;
6993}
6994
6995/*
6996 * Return TRUE if the string "p" contains a wildcard.
6997 * Don't recognize '~' at the end as a wildcard.
6998 */
6999 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007000mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007002 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 if (*p == '\\' && p[1] != NUL)
7005 ++p;
7006 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 if (vim_strchr((char_u *)
7008#ifdef VMS
7009 "*?%$"
7010#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012#endif
7013 , *p) != NULL
7014 || (*p == '~' && p[1] != NUL))
7015 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 }
7017 return FALSE;
7018}
7019
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007021have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
7023 int i;
7024
7025 for (i = 0; i < num; i++)
7026 if (mch_has_wildcard(file[i]))
7027 return 1;
7028 return 0;
7029}
7030
7031 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007032have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034 int i;
7035
7036 for (i = 0; i < num; i++)
7037 if (vim_strchr(file[i], '$') != NULL)
7038 return TRUE;
7039 return FALSE;
7040}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007042#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043/*
7044 * Scaled-down version of rename(), which is missing in Xenix.
7045 * This version can only move regular files and will fail if the
7046 * destination exists.
7047 */
7048 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007049mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050{
7051 struct stat st;
7052
Bram Moolenaar0f873732019-12-05 20:28:46 +01007053 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007055 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007057 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 return 0;
7059 return -1;
7060}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007061#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007063#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064/*
7065 * Initializes connection with gpm (if it isn't already opened)
7066 * Return 1 if succeeded (or connection already opened), 0 if failed
7067 */
7068 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007069gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007071 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072
7073 if (!gpm_flag)
7074 {
7075 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7076 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007077 // Default handling for mouse move
7078 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 gpm_connect.maxMod = 0xffff;
7080 if (Gpm_Open(&gpm_connect, 0) > 0)
7081 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007082 // gpm library tries to handling TSTP causes
7083 // problems. Anyways, we close connection to Gpm whenever
7084 // we are going to suspend or starting an external process
7085 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007086# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007088# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007089 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 }
7091 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007092 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 return 0;
7094 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007095 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096}
7097
7098/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007099 * Returns TRUE if the GPM mouse is enabled.
7100 */
7101 int
7102gpm_enabled(void)
7103{
7104 return gpm_flag && gpm_fd >= 0;
7105}
7106
7107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 */
7110 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007111gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007113 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 Gpm_Close();
7115}
7116
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007117/*
7118 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007120 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 */
7122 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007123mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124{
7125 int button;
7126 static Gpm_Event gpm_event;
7127 char_u string[6];
7128 int_u vim_modifiers;
7129 int row,col;
7130 unsigned char buttons_mask;
7131 unsigned char gpm_modifiers;
7132 static unsigned char old_buttons = 0;
7133
7134 Gpm_GetEvent(&gpm_event);
7135
7136#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007137 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 if (hold_gui_events)
7139 return 0;
7140#endif
7141
7142 row = gpm_event.y - 1;
7143 col = gpm_event.x - 1;
7144
Bram Moolenaar0f873732019-12-05 20:28:46 +01007145 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 string[1] = 'M';
7147 string[2] = 'G';
7148 switch (GPM_BARE_EVENTS(gpm_event.type))
7149 {
7150 case GPM_DRAG:
7151 string[3] = MOUSE_DRAG;
7152 break;
7153 case GPM_DOWN:
7154 buttons_mask = gpm_event.buttons & ~old_buttons;
7155 old_buttons = gpm_event.buttons;
7156 switch (buttons_mask)
7157 {
7158 case GPM_B_LEFT:
7159 button = MOUSE_LEFT;
7160 break;
7161 case GPM_B_MIDDLE:
7162 button = MOUSE_MIDDLE;
7163 break;
7164 case GPM_B_RIGHT:
7165 button = MOUSE_RIGHT;
7166 break;
7167 default:
7168 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007169 // Don't know what to do. Can more than one button be
7170 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 }
7172 string[3] = (char_u)(button | 0x20);
7173 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7174 break;
7175 case GPM_UP:
7176 string[3] = MOUSE_RELEASE;
7177 old_buttons &= ~gpm_event.buttons;
7178 break;
7179 default:
7180 return 0;
7181 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007182 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 gpm_modifiers = gpm_event.modifiers;
7184 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007185 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7186 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7187 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7189 vim_modifiers |= MOUSE_SHIFT;
7190
7191 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7192 vim_modifiers |= MOUSE_CTRL;
7193 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7194 vim_modifiers |= MOUSE_ALT;
7195 string[3] |= vim_modifiers;
7196 string[4] = (char_u)(col + ' ' + 1);
7197 string[5] = (char_u)(row + ' ' + 1);
7198 add_to_input_buf(string, 6);
7199 return 6;
7200}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007201#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007203#ifdef FEAT_SYSMOUSE
7204/*
7205 * Initialize connection with sysmouse.
7206 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7207 * output, any sysmouse output than will be processed via sig_sysmouse().
7208 * Return OK if succeeded, FAIL if failed.
7209 */
7210 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007211sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007212{
7213 struct mouse_info mouse;
7214
7215 mouse.operation = MOUSE_MODE;
7216 mouse.u.mode.mode = 0;
7217 mouse.u.mode.signal = SIGUSR2;
7218 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7219 {
7220 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7221 mouse.operation = MOUSE_SHOW;
7222 ioctl(1, CONS_MOUSECTL, &mouse);
7223 return OK;
7224 }
7225 return FAIL;
7226}
7227
7228/*
7229 * Stop processing SIGUSR2 signals, and also make sure that
7230 * virtual console do not send us any sysmouse related signal.
7231 */
7232 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007233sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007234{
7235 struct mouse_info mouse;
7236
7237 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7238 mouse.operation = MOUSE_MODE;
7239 mouse.u.mode.mode = 0;
7240 mouse.u.mode.signal = 0;
7241 ioctl(1, CONS_MOUSECTL, &mouse);
7242}
7243
7244/*
7245 * Gets info from sysmouse and adds special keys to input buf.
7246 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007247 static RETSIGTYPE
7248sig_sysmouse SIGDEFARG(sigarg)
7249{
7250 struct mouse_info mouse;
7251 struct video_info video;
7252 char_u string[6];
7253 int row, col;
7254 int button;
7255 int buttons;
7256 static int oldbuttons = 0;
7257
7258#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007259 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007260 if (hold_gui_events)
7261 return;
7262#endif
7263
7264 mouse.operation = MOUSE_GETINFO;
7265 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7266 && ioctl(1, FBIO_MODEINFO, &video) != -1
7267 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7268 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7269 {
7270 row = mouse.u.data.y / video.vi_cheight;
7271 col = mouse.u.data.x / video.vi_cwidth;
7272 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007273 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007274 string[1] = 'M';
7275 string[2] = 'S';
7276 if (oldbuttons == buttons && buttons != 0)
7277 {
7278 button = MOUSE_DRAG;
7279 }
7280 else
7281 {
7282 switch (buttons)
7283 {
7284 case 0:
7285 button = MOUSE_RELEASE;
7286 break;
7287 case 1:
7288 button = MOUSE_LEFT;
7289 break;
7290 case 2:
7291 button = MOUSE_MIDDLE;
7292 break;
7293 case 4:
7294 button = MOUSE_RIGHT;
7295 break;
7296 default:
7297 return;
7298 }
7299 oldbuttons = buttons;
7300 }
7301 string[3] = (char_u)(button);
7302 string[4] = (char_u)(col + ' ' + 1);
7303 string[5] = (char_u)(row + ' ' + 1);
7304 add_to_input_buf(string, 6);
7305 }
7306 return;
7307}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007308#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007309
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007311typedef char_u * (*STRPROCSTR)(char_u *);
7312typedef char_u * (*INTPROCSTR)(int);
7313typedef int (*STRPROCINT)(char_u *);
7314typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315
7316/*
7317 * Call a DLL routine which takes either a string or int param
7318 * and returns an allocated string.
7319 */
7320 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007321mch_libcall(
7322 char_u *libname,
7323 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007324 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007325 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007326 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007327 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328{
7329# if defined(USE_DLOPEN)
7330 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007331 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332# else
7333 shl_t hinstLib;
7334# endif
7335 STRPROCSTR ProcAdd;
7336 INTPROCSTR ProcAddI;
7337 char_u *retval_str = NULL;
7338 int retval_int = 0;
7339 int success = FALSE;
7340
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007341 /*
7342 * Get a handle to the DLL module.
7343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007345 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007346 (void)dlerror();
7347
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 hinstLib = dlopen((char *)libname, RTLD_LAZY
7349# ifdef RTLD_LOCAL
7350 | RTLD_LOCAL
7351# endif
7352 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007353 if (hinstLib == NULL)
7354 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007355 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007356 dlerr = (char *)dlerror();
7357 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007358 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360# else
7361 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7362# endif
7363
Bram Moolenaar0f873732019-12-05 20:28:46 +01007364 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 if (hinstLib != NULL)
7366 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007367# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 /*
7369 * Catch a crash when calling the library function. For example when
7370 * using a number where a string pointer is expected.
7371 */
7372 mch_startjmp();
7373 if (SETJMP(lc_jump_env) != 0)
7374 {
7375 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007376# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007377 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 mch_didjmp();
7380 }
7381 else
7382# endif
7383 {
7384 retval_str = NULL;
7385 retval_int = 0;
7386
7387 if (argstring != NULL)
7388 {
7389# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007390 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007391 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392# else
7393 if (shl_findsym(&hinstLib, (const char *)funcname,
7394 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7395 ProcAdd = NULL;
7396# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007397 if ((success = (ProcAdd != NULL
7398# if defined(USE_DLOPEN)
7399 && dlerr == NULL
7400# endif
7401 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 {
7403 if (string_result == NULL)
7404 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7405 else
7406 retval_str = (ProcAdd)(argstring);
7407 }
7408 }
7409 else
7410 {
7411# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007412 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007413 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414# else
7415 if (shl_findsym(&hinstLib, (const char *)funcname,
7416 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7417 ProcAddI = NULL;
7418# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007419 if ((success = (ProcAddI != NULL
7420# if defined(USE_DLOPEN)
7421 && dlerr == NULL
7422# endif
7423 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 {
7425 if (string_result == NULL)
7426 retval_int = ((INTPROCINT)ProcAddI)(argint);
7427 else
7428 retval_str = (ProcAddI)(argint);
7429 }
7430 }
7431
Bram Moolenaar0f873732019-12-05 20:28:46 +01007432 // Save the string before we free the library.
7433 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 if (string_result == NULL)
7435 *number_result = retval_int;
7436 else if (retval_str != NULL
7437 && retval_str != (char_u *)1
7438 && retval_str != (char_u *)-1)
7439 *string_result = vim_strsave(retval_str);
7440 }
7441
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007442# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 mch_endjmp();
7444# ifdef SIGHASARG
7445 if (lc_signal != 0)
7446 {
7447 int i;
7448
Bram Moolenaar0f873732019-12-05 20:28:46 +01007449 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 for (i = 0; signal_info[i].sig != -1; i++)
7451 if (lc_signal == signal_info[i].sig)
7452 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007453 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 }
7455# endif
7456# endif
7457
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007459 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007460 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007461 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007462
Bram Moolenaar0f873732019-12-05 20:28:46 +01007463 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 (void)dlclose(hinstLib);
7465# else
7466 (void)shl_unload(hinstLib);
7467# endif
7468 }
7469
7470 if (!success)
7471 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007472 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 return FAIL;
7474 }
7475
7476 return OK;
7477}
7478#endif
7479
7480#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007481static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482static int xterm_button;
7483
7484/*
7485 * Setup a dummy window for X selections in a terminal.
7486 */
7487 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007488setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489{
7490 int z = 0;
7491 char *strp = "";
7492 Widget AppShell;
7493
7494 if (!x_connect_to_server())
7495 return;
7496
7497 open_app_context();
7498 if (app_context != NULL && xterm_Shell == (Widget)0)
7499 {
7500 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007501# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007503# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007504# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007505 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
7507 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007508 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509# endif
7510
Bram Moolenaar0f873732019-12-05 20:28:46 +01007511 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 oldhandler = XSetErrorHandler(x_error_check);
7513
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007514# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007515 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7517 mch_startjmp();
7518 if (SETJMP(lc_jump_env) != 0)
7519 {
7520 mch_didjmp();
7521 xterm_dpy = NULL;
7522 }
7523 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007524# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 {
7526 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7527 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007528 if (xterm_dpy != NULL)
7529 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007530# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 }
7534
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007535# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007536 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007538# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007539 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 (void)XSetErrorHandler(oldhandler);
7541
7542 if (xterm_dpy == NULL)
7543 {
7544 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007545 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 return;
7547 }
7548
Bram Moolenaar0f873732019-12-05 20:28:46 +01007549 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 (void)XSetIOErrorHandler(x_IOerror_handler);
7551
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007552# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007554 {
7555 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007556 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007557 verbose_leave();
7558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559# endif
7560
Bram Moolenaar0f873732019-12-05 20:28:46 +01007561 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7563 applicationShellWidgetClass, xterm_dpy,
7564 NULL);
7565 if (AppShell == (Widget)0)
7566 return;
7567 xterm_Shell = XtVaCreatePopupShell("VIM",
7568 topLevelShellWidgetClass, AppShell,
7569 XtNmappedWhenManaged, 0,
7570 XtNwidth, 1,
7571 XtNheight, 1,
7572 NULL);
7573 if (xterm_Shell == (Widget)0)
7574 return;
7575
7576 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007577 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 if (x11_display == NULL)
7579 x11_display = xterm_dpy;
7580
7581 XtRealizeWidget(xterm_Shell);
7582 XSync(xterm_dpy, False);
7583 xterm_update();
7584 }
7585 if (xterm_Shell != (Widget)0)
7586 {
7587 clip_init(TRUE);
7588 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7589 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007590 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 if (test_x11_window(xterm_dpy) == FAIL)
7592 x11_window = 0;
7593 if (x11_window != 0)
7594 xterm_trace = 0;
7595 }
7596}
7597
7598 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007599start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600{
7601 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7602 return;
7603 xterm_trace = 1;
7604 xterm_button = button;
7605 do_xterm_trace();
7606}
7607
7608
7609 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007610stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611{
7612 if (xterm_trace < 0)
7613 return;
7614 xterm_trace = 0;
7615}
7616
7617/*
7618 * Query the xterm pointer and generate mouse termcodes if necessary
7619 * return TRUE if dragging is active, else FALSE
7620 */
7621 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007622do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623{
7624 Window root, child;
7625 int root_x, root_y;
7626 int win_x, win_y;
7627 int row, col;
7628 int_u mask_return;
7629 char_u buf[50];
7630 char_u *strp;
7631 long got_hints;
7632 static char_u *mouse_code;
7633 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7634 static int prev_row = 0, prev_col = 0;
7635 static XSizeHints xterm_hints;
7636
7637 if (xterm_trace <= 0)
7638 return FALSE;
7639
7640 if (xterm_trace == 1)
7641 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007642 // Get the hints just before tracking starts. The font size might
7643 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007644 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7645 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 || xterm_hints.width_inc <= 1
7647 || xterm_hints.height_inc <= 1)
7648 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007649 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 return FALSE;
7651 }
7652
Bram Moolenaar0f873732019-12-05 20:28:46 +01007653 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 mouse_code = find_termcode(mouse_name);
7655 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007656 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 xterm_trace = 2;
7658
Bram Moolenaar0f873732019-12-05 20:28:46 +01007659 // Find the offset of the chars, there might be a scrollbar on the
7660 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7662 &win_x, &win_y, &mask_return);
7663 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7664 - (xterm_hints.height_inc / 2);
7665 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7666 xterm_hints.y = 2;
7667 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7668 - (xterm_hints.width_inc / 2);
7669 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7670 xterm_hints.x = 2;
7671 return TRUE;
7672 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007673 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 {
7675 xterm_trace = 0;
7676 return FALSE;
7677 }
7678
7679 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7680 &win_x, &win_y, &mask_return);
7681
7682 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7683 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7684 if (row == prev_row && col == prev_col)
7685 return TRUE;
7686
7687 STRCPY(buf, mouse_code);
7688 strp = buf + STRLEN(buf);
7689 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7690 *strp++ = (char_u)(col + ' ' + 1);
7691 *strp++ = (char_u)(row + ' ' + 1);
7692 *strp = 0;
7693 add_to_input_buf(buf, STRLEN(buf));
7694
7695 prev_row = row;
7696 prev_col = col;
7697 return TRUE;
7698}
7699
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007700# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701/*
7702 * Destroy the display, window and app_context. Required for GTK.
7703 */
7704 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007705clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
7707 if (xterm_Shell != (Widget)0)
7708 {
7709 XtDestroyWidget(xterm_Shell);
7710 xterm_Shell = (Widget)0;
7711 }
7712 if (xterm_dpy != NULL)
7713 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007714# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007715 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007717# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 if (x11_display == xterm_dpy)
7719 x11_display = NULL;
7720 xterm_dpy = NULL;
7721 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007722# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 if (app_context != (XtAppContext)NULL)
7724 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007725 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 XtDestroyApplicationContext(app_context);
7727 app_context = (XtAppContext)NULL;
7728 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007729# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730}
7731# endif
7732
7733/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007734 * Catch up with GUI or X events.
7735 */
7736 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007737clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007738{
7739# ifdef FEAT_GUI
7740 if (gui.in_use)
7741 gui_mch_update();
7742 else
7743# endif
7744 if (xterm_Shell != (Widget)0)
7745 xterm_update();
7746}
7747
7748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 * Catch up with any queued X events. This may put keyboard input into the
7750 * input buffer, call resize call-backs, trigger timers etc. If there is
7751 * nothing in the X event queue (& no timers pending), then we return
7752 * immediately.
7753 */
7754 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007755xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
7757 XEvent event;
7758
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007759 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007761 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007763 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007764 break;
7765
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007766 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007767 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007768 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007769 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007770#ifdef FEAT_CLIENTSERVER
7771 {
7772 XPropertyEvent *e = (XPropertyEvent *)&event;
7773
7774 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007776 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007779 XtDispatchEvent(&event);
7780 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007781 else
7782 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007783 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007784 XtAppProcessEvent(app_context, mask);
7785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 }
7787}
7788
7789 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007790clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
7792 if (xterm_Shell != (Widget)0)
7793 return clip_x11_own_selection(xterm_Shell, cbd);
7794 return FAIL;
7795}
7796
7797 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007798clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799{
7800 if (xterm_Shell != (Widget)0)
7801 clip_x11_lose_selection(xterm_Shell, cbd);
7802}
7803
7804 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007805clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
7807 if (xterm_Shell != (Widget)0)
7808 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7809}
7810
7811 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007812clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813{
7814 clip_x11_set_selection(cbd);
7815}
7816#endif
7817
7818
7819#if defined(USE_XSMP) || defined(PROTO)
7820/*
7821 * Code for X Session Management Protocol.
7822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823
7824# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825/*
7826 * This is our chance to ask the user if they want to save,
7827 * or abort the logout
7828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007830xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831{
7832 cmdmod_T save_cmdmod;
7833 int cancel_shutdown = False;
7834
7835 save_cmdmod = cmdmod;
7836 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007837 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007838 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 cancel_shutdown = True;
7840 cmdmod = save_cmdmod;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007841 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 out_flush();
7843
Bram Moolenaar0f873732019-12-05 20:28:46 +01007844 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 SmcInteractDone(smc_conn, cancel_shutdown);
7846
Bram Moolenaar0f873732019-12-05 20:28:46 +01007847 // Finish off
7848 // Only end save-yourself here if we're not cancelling shutdown;
7849 // we'll get a cancelled callback later in which we'll end it.
7850 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 if (!cancel_shutdown)
7852 {
7853 xsmp.save_yourself = False;
7854 SmcSaveYourselfDone(smc_conn, True);
7855 }
7856}
7857# endif
7858
7859/*
7860 * Callback that starts save-yourself.
7861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007863xsmp_handle_save_yourself(
7864 SmcConn smc_conn,
7865 SmPointer client_data UNUSED,
7866 int save_type UNUSED,
7867 Bool shutdown,
7868 int interact_style UNUSED,
7869 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007871 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 if (xsmp.save_yourself)
7873 SmcSaveYourselfDone(smc_conn, True);
7874 xsmp.save_yourself = True;
7875 xsmp.shutdown = shutdown;
7876
Bram Moolenaar0f873732019-12-05 20:28:46 +01007877 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007879 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880
7881 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007882 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883
7884# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007885 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007887 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 SmcInteractRequest(smc_conn, SmDialogError,
7889 xsmp_handle_interaction, client_data);
7890 else
7891# endif
7892 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007893 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 SmcSaveYourselfDone(smc_conn, True);
7895 xsmp.save_yourself = False;
7896 }
7897}
7898
7899
7900/*
7901 * Callback to warn us of imminent death.
7902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007904xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 xsmp_close();
7907
Bram Moolenaar0f873732019-12-05 20:28:46 +01007908 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 getout_preserve_modified(0);
7910}
7911
7912
7913/*
7914 * Callback to tell us that save-yourself has completed.
7915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007917xsmp_save_complete(
7918 SmcConn smc_conn UNUSED,
7919 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920{
7921 xsmp.save_yourself = False;
7922}
7923
7924
7925/*
7926 * Callback to tell us that an instigated shutdown was cancelled
7927 * (maybe even by us)
7928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007930xsmp_shutdown_cancelled(
7931 SmcConn smc_conn,
7932 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934 if (xsmp.save_yourself)
7935 SmcSaveYourselfDone(smc_conn, True);
7936 xsmp.save_yourself = False;
7937 xsmp.shutdown = False;
7938}
7939
7940
7941/*
7942 * Callback to tell us that a new ICE connection has been established.
7943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007945xsmp_ice_connection(
7946 IceConn iceConn,
7947 IcePointer clientData UNUSED,
7948 Bool opening,
7949 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007951 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 if (opening)
7953 {
7954 xsmp_icefd = IceConnectionNumber(iceConn);
7955 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7956 }
7957}
7958
7959
Bram Moolenaar0f873732019-12-05 20:28:46 +01007960// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007962xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963{
7964 Bool rep;
7965
7966 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7967 == IceProcessMessagesIOError)
7968 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007969 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007971 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 xsmp_close();
7973 return FAIL;
7974 }
7975 else
7976 return OK;
7977}
7978
7979static int dummy;
7980
Bram Moolenaar0f873732019-12-05 20:28:46 +01007981// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 void
7983xsmp_init(void)
7984{
7985 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 SmcCallbacks smcallbacks;
7987#if 0
7988 SmPropValue smname;
7989 SmProp smnameprop;
7990 SmProp *smprops[1];
7991#endif
7992
7993 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007994 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995
7996 xsmp.save_yourself = xsmp.shutdown = False;
7997
Bram Moolenaar0f873732019-12-05 20:28:46 +01007998 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8000 smcallbacks.save_yourself.client_data = NULL;
8001 smcallbacks.die.callback = xsmp_die;
8002 smcallbacks.die.client_data = NULL;
8003 smcallbacks.save_complete.callback = xsmp_save_complete;
8004 smcallbacks.save_complete.client_data = NULL;
8005 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8006 smcallbacks.shutdown_cancelled.client_data = NULL;
8007
Bram Moolenaar0f873732019-12-05 20:28:46 +01008008 // Set up a watch on ICE connection creations. The "dummy" argument is
8009 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8011 {
8012 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008013 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 return;
8015 }
8016
Bram Moolenaar0f873732019-12-05 20:28:46 +01008017 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 xsmp.smcconn = SmcOpenConnection(
8019 NULL,
8020 NULL,
8021 SmProtoMajor,
8022 SmProtoMinor,
8023 SmcSaveYourselfProcMask | SmcDieProcMask
8024 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8025 &smcallbacks,
8026 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008027 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008028 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 errorstring);
8030 if (xsmp.smcconn == NULL)
8031 {
8032 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008033
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008035 {
8036 vim_snprintf(errorreport, sizeof(errorreport),
8037 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008038 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 return;
8041 }
8042 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8043
8044#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008045 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 smname.value = "vim";
8047 smname.length = 3;
8048 smnameprop.name = "SmProgram";
8049 smnameprop.type = "SmARRAY8";
8050 smnameprop.num_vals = 1;
8051 smnameprop.vals = &smname;
8052
8053 smprops[0] = &smnameprop;
8054 SmcSetProperties(xsmp.smcconn, 1, smprops);
8055#endif
8056}
8057
8058
Bram Moolenaar0f873732019-12-05 20:28:46 +01008059// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008061xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062{
8063 if (xsmp_icefd != -1)
8064 {
8065 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008066 if (xsmp.clientid != NULL)
8067 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008068 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 xsmp_icefd = -1;
8070 }
8071}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008072#endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073
8074
8075#ifdef EBCDIC
Bram Moolenaar0f873732019-12-05 20:28:46 +01008076// Translate character to its CTRL- value
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077char CtrlTable[] =
8078{
8079/* 00 - 5E */
8080 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8081 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8082 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8083 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8084 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8085 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8086/* ^ */ 0x1E,
8087/* - */ 0x1F,
8088/* 61 - 6C */
8089 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8090/* _ */ 0x1F,
8091/* 6E - 80 */
8092 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8093/* a */ 0x01,
8094/* b */ 0x02,
8095/* c */ 0x03,
8096/* d */ 0x37,
8097/* e */ 0x2D,
8098/* f */ 0x2E,
8099/* g */ 0x2F,
8100/* h */ 0x16,
8101/* i */ 0x05,
8102/* 8A - 90 */
8103 0, 0, 0, 0, 0, 0, 0,
8104/* j */ 0x15,
8105/* k */ 0x0B,
8106/* l */ 0x0C,
8107/* m */ 0x0D,
8108/* n */ 0x0E,
8109/* o */ 0x0F,
8110/* p */ 0x10,
8111/* q */ 0x11,
8112/* r */ 0x12,
8113/* 9A - A1 */
8114 0, 0, 0, 0, 0, 0, 0, 0,
8115/* s */ 0x13,
8116/* t */ 0x3C,
8117/* u */ 0x3D,
8118/* v */ 0x32,
8119/* w */ 0x26,
8120/* x */ 0x18,
8121/* y */ 0x19,
8122/* z */ 0x3F,
8123/* AA - AC */
8124 0, 0, 0,
8125/* [ */ 0x27,
8126/* AE - BC */
8127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8128/* ] */ 0x1D,
8129/* BE - C0 */ 0, 0, 0,
8130/* A */ 0x01,
8131/* B */ 0x02,
8132/* C */ 0x03,
8133/* D */ 0x37,
8134/* E */ 0x2D,
8135/* F */ 0x2E,
8136/* G */ 0x2F,
8137/* H */ 0x16,
8138/* I */ 0x05,
8139/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8140/* J */ 0x15,
8141/* K */ 0x0B,
8142/* L */ 0x0C,
8143/* M */ 0x0D,
8144/* N */ 0x0E,
8145/* O */ 0x0F,
8146/* P */ 0x10,
8147/* Q */ 0x11,
8148/* R */ 0x12,
8149/* DA - DF */ 0, 0, 0, 0, 0, 0,
8150/* \ */ 0x1C,
8151/* E1 */ 0,
8152/* S */ 0x13,
8153/* T */ 0x3C,
8154/* U */ 0x3D,
8155/* V */ 0x32,
8156/* W */ 0x26,
8157/* X */ 0x18,
8158/* Y */ 0x19,
8159/* Z */ 0x3F,
8160/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8161 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8162};
8163
8164char MetaCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008165{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8167 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8168 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8169 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8170};
8171
8172
Bram Moolenaar0f873732019-12-05 20:28:46 +01008173// TODO: Use characters NOT numbers!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174char CtrlCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008175{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8177 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8178 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8179 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8180};
8181
8182
8183#endif