blob: 0a4f0e6981cc4bc440e1f2f6c46f0fae8cdb3a54 [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
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200167#if defined(SIGUSR1)
168static RETSIGTYPE catch_sigusr1 SIGPROTOARG;
169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100171static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#if defined(SIGALRM) && defined(FEAT_X11) \
174 && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
175# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100176static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100177// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200178static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100180static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100182static void catch_int_signal(void);
183static void set_signals(void);
184static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200185#ifdef HAVE_SIGPROCMASK
186# define SIGSET_DECL(set) sigset_t set;
187# define BLOCK_SIGNALS(set) block_signals(set)
188# define UNBLOCK_SIGNALS(set) unblock_signals(set)
189#else
190# define SIGSET_DECL(set)
191# define BLOCK_SIGNALS(set) do { /**/ } while (0)
192# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
193#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100194static int have_wildcard(int, char_u **);
195static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100197static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000200# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#endif
202
Bram Moolenaar0f873732019-12-05 20:28:46 +0100203// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200204static volatile sig_atomic_t do_resize = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205static char_u *extra_shell_arg = NULL;
206static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100207// volatile because it is used in signal handler deathtrap().
208static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
209// volatile because it is used in signal handler deathtrap().
210static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100212#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
213static int dont_check_job_ended = 0;
214#endif
215
Bram Moolenaar26e86442020-05-17 14:06:16 +0200216// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
217static tmode_T mch_cur_tmode = TMODE_COOK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
219#ifdef USE_XSMP
220typedef struct
221{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100222 SmcConn smcconn; // The SM connection ID
223 IceConn iceconn; // The ICE connection ID
224 char *clientid; // The client ID for the current smc session
225 Bool save_yourself; // If we're in the middle of a save_yourself
226 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227} xsmp_config_T;
228
229static xsmp_config_T xsmp;
230#endif
231
232#ifdef SYS_SIGLIST_DECLARED
233/*
234 * I have seen
235 * extern char *_sys_siglist[NSIG];
236 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
237 * that describe the signals. That is nearly what we want here. But
238 * autoconf does only check for sys_siglist (without the underscore), I
239 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100240 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 */
242#endif
243
244static struct signalinfo
245{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100246 int sig; // Signal number, eg. SIGSEGV etc
247 char *name; // Signal name (not char_u!).
248 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249} signal_info[] =
250{
251#ifdef SIGHUP
252 {SIGHUP, "HUP", TRUE},
253#endif
254#ifdef SIGQUIT
255 {SIGQUIT, "QUIT", TRUE},
256#endif
257#ifdef SIGILL
258 {SIGILL, "ILL", TRUE},
259#endif
260#ifdef SIGTRAP
261 {SIGTRAP, "TRAP", TRUE},
262#endif
263#ifdef SIGABRT
264 {SIGABRT, "ABRT", TRUE},
265#endif
266#ifdef SIGEMT
267 {SIGEMT, "EMT", TRUE},
268#endif
269#ifdef SIGFPE
270 {SIGFPE, "FPE", TRUE},
271#endif
272#ifdef SIGBUS
273 {SIGBUS, "BUS", TRUE},
274#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100275#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100276 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {SIGSEGV, "SEGV", TRUE},
278#endif
279#ifdef SIGSYS
280 {SIGSYS, "SYS", TRUE},
281#endif
282#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100283 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284#endif
285#ifdef SIGTERM
286 {SIGTERM, "TERM", TRUE},
287#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100288#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 {SIGVTALRM, "VTALRM", TRUE},
290#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000291#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100292 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
293 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 {SIGPROF, "PROF", TRUE},
295#endif
296#ifdef SIGXCPU
297 {SIGXCPU, "XCPU", TRUE},
298#endif
299#ifdef SIGXFSZ
300 {SIGXFSZ, "XFSZ", TRUE},
301#endif
302#ifdef SIGUSR1
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200303 {SIGUSR1, "USR1", FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000305#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100306 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {SIGUSR2, "USR2", TRUE},
308#endif
309#ifdef SIGINT
310 {SIGINT, "INT", FALSE},
311#endif
312#ifdef SIGWINCH
313 {SIGWINCH, "WINCH", FALSE},
314#endif
315#ifdef SIGTSTP
316 {SIGTSTP, "TSTP", FALSE},
317#endif
318#ifdef SIGPIPE
319 {SIGPIPE, "PIPE", FALSE},
320#endif
321 {-1, "Unknown!", FALSE}
322};
323
Bram Moolenaar25724922009-07-14 15:38:41 +0000324 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100325mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000326{
327 if (p_verbose >= 5)
328 {
329 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100330 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000331 verbose_leave();
332 }
333# ifdef VMS
334 return chdir(vms_fixfilename(path));
335# else
336 return chdir(path);
337# endif
338}
339
Bram Moolenaar0f873732019-12-05 20:28:46 +0100340// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100341#if defined(ECHOE) && defined(ICANON) \
342 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
343 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200344# define NEW_TTY_SYSTEM
345#endif
346
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000347/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100348 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100351mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200353 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100354 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100355 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356}
357
358/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100359 * Function passed to inchar_loop() to handle window resizing.
360 * If "check_only" is TRUE: Return whether there was a resize.
361 * If "check_only" is FALSE: Deal with the window resized.
362 */
363 static int
364resize_func(int check_only)
365{
366 if (check_only)
367 return do_resize;
368 while (do_resize)
369 handle_resize();
370 return FALSE;
371}
372
373/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000374 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 * Get a characters from the keyboard.
376 * Return the number of characters that are available.
377 * If wtime == 0 do not wait for characters.
378 * If wtime == n wait a short time for characters.
379 * If wtime == -1 wait forever for characters.
380 */
381 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100382mch_inchar(
383 char_u *buf,
384 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100385 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100386 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100388 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
389 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390}
391
392 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100393handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394{
395 do_resize = FALSE;
396 shell_resized();
397}
398
399/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200400 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 */
402 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100403mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200405 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406}
407
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200408#if defined(FEAT_TERMINAL) || defined(PROTO)
409/*
410 * Check for any pending input or messages.
411 */
412 int
413mch_check_messages(void)
414{
415 return WaitForChar(0L, NULL, TRUE);
416}
417#endif
418
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
420# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000421# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422# endif
423# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
424# include <sys/sysctl.h>
425# endif
426# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
427# include <sys/sysinfo.h>
428# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100429# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100430# include <mach/mach_host.h>
431# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433
434/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000435 * Return total amount of memory available in Kbyte.
436 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100439mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100442 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
Bram Moolenaar362dc332018-03-05 21:59:37 +0100444# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100445 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100446 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100447 mach_port_t host = mach_host_self();
448 kern_return_t kret;
449# ifdef HOST_VM_INFO64
450 struct vm_statistics64 vm_stat;
451 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
Bram Moolenaar988615f2018-02-27 17:58:20 +0100453 kret = host_statistics64(host, HOST_VM_INFO64,
454 (host_info64_t)&vm_stat, &count);
455# else
456 struct vm_statistics vm_stat;
457 natural_t count = HOST_VM_INFO_COUNT;
458
459 kret = host_statistics(host, HOST_VM_INFO,
460 (host_info_t)&vm_stat, &count);
461# endif
462 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100463 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100464 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
465 + vm_stat.inactive_count
466# ifdef MAC_OS_X_VERSION_10_9
467 + vm_stat.compressor_page_count
468# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100469 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100470 mach_port_deallocate(mach_task_self(), host);
471 }
472# endif
473
474# ifdef HAVE_SYSCTL
475 if (mem == 0)
476 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100477 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100478 int mib[2];
479 size_t len = sizeof(long_u);
480# ifdef HW_USERMEM64
481 long_u physmem;
482# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100483 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100484 union {
485 int_u u32;
486 long_u u64;
487 } physmem;
488# endif
489
490 mib[0] = CTL_HW;
491# ifdef HW_USERMEM64
492 mib[1] = HW_USERMEM64;
493# else
494 mib[1] = HW_USERMEM;
495# endif
496 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
497 {
498# ifdef HW_USERMEM64
499 mem = (long_u)physmem;
500# else
501 if (len == sizeof(physmem.u64))
502 mem = (long_u)physmem.u64;
503 else
504 mem = (long_u)physmem.u32;
505# endif
506 }
507 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200510# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (mem == 0)
512 {
513 struct sysinfo sinfo;
514
Bram Moolenaar0f873732019-12-05 20:28:46 +0100515 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000517 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200518# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100519 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000520 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
521 {
522 sinfo.mem_unit = sinfo.mem_unit >> 1;
523 --shiftright;
524 }
525 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200526# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200528# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200531# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200533# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (mem == 0)
535 {
536 long pagesize, pagecount;
537
Bram Moolenaar0f873732019-12-05 20:28:46 +0100538 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 pagesize = sysconf(_SC_PAGESIZE);
540 pagecount = sysconf(_SC_PHYS_PAGES);
541 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000542 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100543 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000544 while (shiftright > 0 && (pagesize & 1) == 0)
545 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000546 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000547 --shiftright;
548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200552# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
Bram Moolenaar0f873732019-12-05 20:28:46 +0100554 // Return the minimum of the physical memory and the user limit, because
555 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200556# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 {
558 struct rlimit rlp;
559
560 if (getrlimit(RLIMIT_DATA, &rlp) == 0
561 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200562# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200564# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000565 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000567 {
568 mem = (long_u)rlp.rlim_cur;
569 shiftright = 10;
570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000575 return mem >> shiftright;
576 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577}
578#endif
579
Bram Moolenaar0981c872020-08-23 14:28:37 +0200580/*
581 * "flags": MCH_DELAY_IGNOREINPUT - don't read input
582 * MCH_DELAY_SETTMODE - use settmode() even for short delays
583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200585mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200587 tmode_T old_tmode;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200588 int call_settmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000589#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100590 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
Bram Moolenaar0981c872020-08-23 14:28:37 +0200593 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100595 // Go to cooked mode without echo, to allow SIGINT interrupting us
596 // here. But we don't want QUIT to kill us (CTRL-\ used in a
597 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200598 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000599 in_mch_delay = TRUE;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200600 call_settmode = mch_cur_tmode == TMODE_RAW
601 && (msec > 500 || (flags & MCH_DELAY_SETTMODE));
602 if (call_settmode)
603 {
604 old_tmode = mch_cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 settmode(TMODE_SLEEP);
Bram Moolenaar80361a52020-10-05 21:39:25 +0200606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608 /*
609 * Everybody sleeps in a different way...
610 * Prefer nanosleep(), some versions of usleep() can only sleep up to
611 * one second.
612 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000613#ifdef FEAT_MZSCHEME
614 do
615 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100616 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000617 if (total > p_mzq)
618 msec = p_mzq;
619 else
620 msec = total;
621 total -= msec;
622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623#ifdef HAVE_NANOSLEEP
624 {
625 struct timespec ts;
626
627 ts.tv_sec = msec / 1000;
628 ts.tv_nsec = (msec % 1000) * 1000000;
629 (void)nanosleep(&ts, NULL);
630 }
631#else
632# ifdef HAVE_USLEEP
633 while (msec >= 1000)
634 {
635 usleep((unsigned int)(999 * 1000));
636 msec -= 999;
637 }
638 usleep((unsigned int)(msec * 1000));
639# else
640# ifndef HAVE_SELECT
641 poll(NULL, 0, (int)msec);
642# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {
644 struct timeval tv;
645
646 tv.tv_sec = msec / 1000;
647 tv.tv_usec = (msec % 1000) * 1000;
Bram Moolenaar0981c872020-08-23 14:28:37 +0200648 // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
649 // a patch from Sun to fix this. Reported by Gunnar Pedersen.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 select(0, NULL, NULL, NULL, &tv);
651 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100652# endif // HAVE_SELECT
653# endif // HAVE_NANOSLEEP
654#endif // HAVE_USLEEP
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000655#ifdef FEAT_MZSCHEME
656 }
657 while (total > 0);
658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659
Bram Moolenaar80361a52020-10-05 21:39:25 +0200660 if (call_settmode)
Bram Moolenaar26e86442020-05-17 14:06:16 +0200661 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000662 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200665 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666}
667
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000668#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
670# define HAVE_CHECK_STACK_GROWTH
671/*
672 * Support for checking for an almost-out-of-stack-space situation.
673 */
674
675/*
676 * Return a pointer to an item on the stack. Used to find out if the stack
677 * grows up or down.
678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679static int stack_grows_downwards;
680
681/*
682 * Find out if the stack grows upwards or downwards.
683 * "p" points to a variable on the stack of the caller.
684 */
685 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100686check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
688 int i;
689
690 stack_grows_downwards = (p > (char *)&i);
691}
692#endif
693
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000694#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695static char *stack_limit = NULL;
696
697#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
698# include <pthread.h>
699# include <pthread_np.h>
700#endif
701
702/*
703 * Find out until how var the stack can grow without getting into trouble.
704 * Called when starting up and when switching to the signal stack in
705 * deathtrap().
706 */
707 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100708get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709{
710 struct rlimit rlp;
711 int i;
712 long lim;
713
Bram Moolenaar0f873732019-12-05 20:28:46 +0100714 // Set the stack limit to 15/16 of the allowable size. Skip this when the
715 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 if (getrlimit(RLIMIT_STACK, &rlp) == 0
717 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
718# ifdef RLIM_INFINITY
719 && rlp.rlim_cur != RLIM_INFINITY
720# endif
721 )
722 {
723 lim = (long)rlp.rlim_cur;
724#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
725 {
726 pthread_attr_t attr;
727 size_t size;
728
Bram Moolenaar0f873732019-12-05 20:28:46 +0100729 // On FreeBSD the initial thread always has a fixed stack size, no
730 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 pthread_attr_init(&attr);
732 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
733 {
734 pthread_attr_getstacksize(&attr, &size);
735 if (lim > (long)size)
736 lim = (long)size;
737 }
738 pthread_attr_destroy(&attr);
739 }
740#endif
741 if (stack_grows_downwards)
742 {
743 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
744 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100745 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 stack_limit = (char *)((long)&i / 16L);
747 }
748 else
749 {
750 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
751 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100752 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 }
754 }
755}
756
757/*
758 * Return FAIL when running out of stack space.
759 * "p" must point to any variable local to the caller that's on the stack.
760 */
761 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100762mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
764 if (stack_limit != NULL)
765 {
766 if (stack_grows_downwards)
767 {
768 if (p < stack_limit)
769 return FAIL;
770 }
771 else if (p > stack_limit)
772 return FAIL;
773 }
774 return OK;
775}
776#endif
777
778#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
779/*
780 * Support for using the signal stack.
781 * This helps when we run out of stack space, which causes a SIGSEGV. The
782 * signal handler then must run on another stack, since the normal stack is
783 * completely full.
784 */
785
Bram Moolenaar0e62a672021-02-25 17:17:56 +0100786#if !defined SIGSTKSZ && !defined(HAVE_SYSCONF_SIGSTKSZ)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100787# define SIGSTKSZ 8000 // just a guess of how much stack is needed...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#endif
789
790# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100791static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100793static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794# endif
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796static char *signal_stack;
797
798 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100799init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 if (signal_stack != NULL)
802 {
803# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804# ifdef HAVE_SS_BASE
805 sigstk.ss_base = signal_stack;
806# else
807 sigstk.ss_sp = signal_stack;
808# endif
Bram Moolenaar0e62a672021-02-25 17:17:56 +0100809# ifdef HAVE_SYSCONF_SIGSTKSZ
810 sigstk.ss_size = sysconf(_SC_SIGSTKSZ);
811# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 sigstk.ss_size = SIGSTKSZ;
Bram Moolenaar0e62a672021-02-25 17:17:56 +0100813# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 sigstk.ss_flags = 0;
815 (void)sigaltstack(&sigstk, NULL);
816# else
817 sigstk.ss_sp = signal_stack;
818 if (stack_grows_downwards)
Bram Moolenaar0e62a672021-02-25 17:17:56 +0100819# ifdef HAVE_SYSCONF_SIGSTKSZ
820 sigstk.ss_sp += sysconf(_SC_SIGSTKSZ) - 1;
821# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 sigstk.ss_sp += SIGSTKSZ - 1;
Bram Moolenaar0e62a672021-02-25 17:17:56 +0100823# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 sigstk.ss_onstack = 0;
825 (void)sigstack(&sigstk, NULL);
826# endif
827 }
828}
829#endif
830
831/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000832 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * will barf when the second argument to signal() is ``wrong''.
834 * Let me try it with a few tricky defines from my own osdef.h (jw).
835 */
836#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 static RETSIGTYPE
838sig_winch SIGDEFARG(sigarg)
839{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100840 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
842 do_resize = TRUE;
843 SIGRETURN;
844}
845#endif
846
847#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 static RETSIGTYPE
849catch_sigint SIGDEFARG(sigarg)
850{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100851 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
853 got_int = TRUE;
854 SIGRETURN;
855}
856#endif
857
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200858#if defined(SIGUSR1)
859 static RETSIGTYPE
860catch_sigusr1 SIGDEFARG(sigarg)
861{
862 // this is not required on all systems, but it doesn't hurt anybody
863 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
864 got_sigusr1 = TRUE;
865 SIGRETURN;
866}
867#endif
868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 static RETSIGTYPE
871catch_sigpwr SIGDEFARG(sigarg)
872{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100873 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000874 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 /*
876 * I'm not sure we get the SIGPWR signal when the system is really going
877 * down or when the batteries are almost empty. Just preserve the swap
878 * files and don't exit, that can't do any harm.
879 */
880 ml_sync_all(FALSE, FALSE);
881 SIGRETURN;
882}
883#endif
884
885#ifdef SET_SIG_ALARM
886/*
887 * signal function for alarm().
888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 static RETSIGTYPE
890sig_alarm SIGDEFARG(sigarg)
891{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100892 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 sig_alarm_called = TRUE;
894 SIGRETURN;
895}
896#endif
897
Bram Moolenaaredce7422019-01-20 18:39:30 +0100898#if (defined(HAVE_SETJMP_H) \
899 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
900 || defined(FEAT_LIBCALL))) \
901 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100902# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100903
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100904// argument to SETJMP()
905static JMP_BUF lc_jump_env;
906
907# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100908// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100909// Volatile because it is used in signal handlers.
910static volatile sig_atomic_t lc_signal;
911# endif
912
913// TRUE when lc_jump_env is valid.
914// Volatile because it is used in signal handler deathtrap().
915static volatile sig_atomic_t lc_active INIT(= FALSE);
916
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917/*
918 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100919 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100921 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 * Usage:
923 * mch_startjmp();
924 * if (SETJMP(lc_jump_env) != 0)
925 * {
926 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100927 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 * }
929 * else
930 * {
931 * do_the_work;
932 * mch_endjmp();
933 * }
934 * Note: Can't move SETJMP() here, because a function calling setjmp() must
935 * not return before the saved environment is used.
936 * Returns OK for normal return, FAIL when the protected code caused a
937 * problem and LONGJMP() was used.
938 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100939 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100940mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941{
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100942# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100944# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 lc_active = TRUE;
946}
947
Bram Moolenaar113e1072019-01-20 15:30:40 +0100948 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100949mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950{
951 lc_active = FALSE;
952}
953
Bram Moolenaar113e1072019-01-20 15:30:40 +0100954 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100955mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
957# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100958 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
959 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 init_signal_stack();
961# endif
962}
963#endif
964
965/*
966 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200967 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200969 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
970 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 */
972 static RETSIGTYPE
973deathtrap SIGDEFARG(sigarg)
974{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100975 static int entered = 0; // count the number of times we got here.
976 // Note: when memory has been corrupted
977 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978#ifdef SIGHASARG
979 int i;
980#endif
981
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100982#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 /*
984 * Catch a crash in protected code.
985 * Restores the environment saved in lc_jump_env, which looks like
986 * SETJMP() returns 1.
987 */
988 if (lc_active)
989 {
990# if defined(SIGHASARG)
991 lc_signal = sigarg;
992# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100993 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100995 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 }
997#endif
998
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000999#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001000# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001001 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1002 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1003 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001004 if (in_mch_delay && sigarg == SIGQUIT)
1005 SIGRETURN;
1006# endif
1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01001008 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1009 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1010 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001011 if (entered == 0
1012 && (0
1013# ifdef SIGHUP
1014 || sigarg == SIGHUP
1015# endif
1016# ifdef SIGQUIT
1017 || sigarg == SIGQUIT
1018# endif
1019# ifdef SIGTERM
1020 || sigarg == SIGTERM
1021# endif
1022# ifdef SIGPWR
1023 || sigarg == SIGPWR
1024# endif
1025# ifdef SIGUSR1
1026 || sigarg == SIGUSR1
1027# endif
1028# ifdef SIGUSR2
1029 || sigarg == SIGUSR2
1030# endif
1031 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001032 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001033 SIGRETURN;
1034#endif
1035
Bram Moolenaar0f873732019-12-05 20:28:46 +01001036 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 ++entered;
1038
Bram Moolenaar0f873732019-12-05 20:28:46 +01001039 // Executing autocommands is likely to use more stack space than we have
1040 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001041 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001042
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001044 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 set_vim_var_nr(VV_DYING, (long)entered);
1046#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001047 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001049#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001050 // Since we are now using the signal stack, need to reset the stack
1051 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 get_stack_limit();
1053#endif
1054
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001055#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001056 // This is for opening gdb the moment Vim crashes.
1057 // You need to manually adjust the file name and Vim executable name.
1058 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001059 {
1060# define VI_GDB_FILE "/tmp/vimgdb"
1061# define VIM_NAME "/usr/bin/vim"
1062 FILE *fp = fopen(VI_GDB_FILE, "w");
1063 if (fp)
1064 {
1065 fprintf(fp,
1066 "file %s\n"
1067 "attach %d\n"
1068 "set height 1000\n"
1069 "bt full\n"
1070 , VIM_NAME, getpid());
1071 fclose(fp);
1072 system("xterm -e gdb -x "VI_GDB_FILE);
1073 unlink(VI_GDB_FILE);
1074 }
1075 }
1076#endif
1077
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001079 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 for (i = 0; signal_info[i].sig != -1; i++)
1081 if (sigarg == signal_info[i].sig)
1082 break;
1083 deadly_signal = sigarg;
1084#endif
1085
Bram Moolenaar0f873732019-12-05 20:28:46 +01001086 full_screen = FALSE; // don't write message to the GUI, it might be
1087 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 /*
1089 * If something goes wrong after entering here, we may get here again.
1090 * When this happens, give a message and try to exit nicely (resetting the
1091 * terminal mode, etc.)
1092 * When this happens twice, just exit, don't even try to give a message,
1093 * stack may be corrupt or something weird.
1094 * When this still happens again (or memory was corrupted in such a way
1095 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1096 */
1097 if (entered >= 3)
1098 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001099 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 may_core_dump();
1101 if (entered >= 4)
1102 _exit(8);
1103 exit(7);
1104 }
1105 if (entered == 2)
1106 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001107 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001108 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 out_flush();
1110 getout(1);
1111 }
1112
Bram Moolenaar0f873732019-12-05 20:28:46 +01001113 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001115 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 signal_info[i].name);
1117#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001118 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001120
Bram Moolenaar0f873732019-12-05 20:28:46 +01001121 // Preserve files and exit. This sets the really_exiting flag to prevent
1122 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001123 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
Bram Moolenaar0f873732019-12-05 20:28:46 +01001125 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001126
Bram Moolenaar009b2592004-10-24 19:18:58 +00001127#ifdef NBDEBUG
1128 reset_signals();
1129 may_core_dump();
1130 abort();
1131#endif
1132
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 SIGRETURN;
1134}
1135
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001137 * Invoked after receiving SIGCONT. We don't know what happened while
1138 * sleeping, deal with part of that.
1139 */
1140 static void
1141after_sigcont(void)
1142{
1143# ifdef FEAT_TITLE
1144 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1145 // again later.
1146 oldtitle_outdated = TRUE;
1147# endif
1148 settmode(TMODE_RAW);
1149 need_check_timestamps = TRUE;
1150 did_check_timestamps = FALSE;
1151}
1152
1153#if defined(SIGCONT)
1154static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001155static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001156
1157/*
1158 * With multi-threading, suspending might not work immediately. Catch the
1159 * SIGCONT signal, which will be used as an indication whether the suspending
1160 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001161 *
1162 * On Linux, signal is not always handled immediately either.
1163 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001164 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001165 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001166 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001168static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001169static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
1171/*
1172 * signal handler for SIGCONT
1173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 static RETSIGTYPE
1175sigcont_handler SIGDEFARG(sigarg)
1176{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001177 if (in_mch_suspend)
1178 {
1179 sigcont_received = TRUE;
1180 }
1181 else
1182 {
1183 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1184 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1185 // back to a sane mode. We should redraw, but we can't really do that
1186 // in a signal handler, do a redraw later.
1187 after_sigcont();
1188 redraw_later(CLEAR);
1189 cursor_on_force();
1190 out_flush();
1191 }
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 SIGRETURN;
1194}
1195#endif
1196
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001197#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001198# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001199static void *clip_star_save = NULL;
1200static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001201# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001202
1203/*
1204 * Called when Vim is going to sleep or execute a shell command.
1205 * We can't respond to requests for the X selections. Lose them, otherwise
1206 * other applications will hang. But first copy the text to cut buffer 0.
1207 */
1208 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001209loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001210{
1211 if (clip_star.owned || clip_plus.owned)
1212 {
1213 x11_export_final_selection();
1214 if (clip_star.owned)
1215 clip_lose_selection(&clip_star);
1216 if (clip_plus.owned)
1217 clip_lose_selection(&clip_plus);
1218 if (x11_display != NULL)
1219 XFlush(x11_display);
1220 }
1221}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001222
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001223# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001224/*
1225 * Save clipboard text to restore later.
1226 */
1227 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001228save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001229{
1230 if (clip_star.owned)
1231 clip_star_save = get_register('*', TRUE);
1232 if (clip_plus.owned)
1233 clip_plus_save = get_register('+', TRUE);
1234}
1235
1236/*
1237 * Restore clipboard text if no one own the X selection.
1238 */
1239 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001240restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001241{
1242 if (clip_star_save != NULL)
1243 {
1244 if (!clip_gen_owner_exists(&clip_star))
1245 put_register('*', clip_star_save);
1246 else
1247 free_register(clip_star_save);
1248 clip_star_save = NULL;
1249 }
1250 if (clip_plus_save != NULL)
1251 {
1252 if (!clip_gen_owner_exists(&clip_plus))
1253 put_register('+', clip_plus_save);
1254 else
1255 free_register(clip_plus_save);
1256 clip_plus_save = NULL;
1257 }
1258}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001259# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001260#endif
1261
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262/*
1263 * If the machine has job control, use it to suspend the program,
1264 * otherwise fake it by starting a new shell.
1265 */
1266 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001267mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001269 if (ignore_sigtstp)
1270 return;
1271
Bram Moolenaar041c7102020-05-30 18:14:57 +02001272#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001273 in_mch_suspend = TRUE;
1274
Bram Moolenaar0f873732019-12-05 20:28:46 +01001275 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001277 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278
1279# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001280 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001282# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 sigcont_received = FALSE;
1284# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001285
Bram Moolenaar0f873732019-12-05 20:28:46 +01001286 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001287
1288# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001289 /*
1290 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001291 * immediately, but somehow not all the time, probably because it's handled
1292 * in another thread. Do not call pause() because there would be race
1293 * condition which would hang Vim if signal happened in between the test of
1294 * sigcont_received and the call to pause(). If signal is not yet received,
1295 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1296 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001297 */
1298 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001299 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001300
Bram Moolenaar262735e2009-07-14 10:20:22 +00001301 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001302 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001305 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306
Bram Moolenaar2e310482018-08-21 13:09:10 +02001307 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308#else
1309 suspend_shell();
1310#endif
1311}
1312
1313 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001314mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315{
1316 Columns = 80;
1317 Rows = 24;
1318
1319 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001320
1321#ifdef SIGTSTP
1322 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1323 // that indicates the shell (or program) that launched us does not support
1324 // tty job control and thus we should ignore that signal. If invoked as a
1325 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
1326 ignore_sigtstp = restricted || SIG_IGN == signal(SIGTSTP, SIG_ERR);
1327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001329
Bram Moolenaar56718732006-03-15 22:53:57 +00001330#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001331 mac_conv_init();
1332#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001333#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1334 win_clip_init();
1335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336}
1337
1338 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001339set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
1341#if defined(SIGWINCH)
1342 /*
1343 * WINDOW CHANGE signal is handled with sig_winch().
1344 */
1345 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1346#endif
1347
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001349 // See mch_init() for the conditions under which we ignore SIGTSTP.
1350 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001352#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 signal(SIGCONT, sigcont_handler);
1354#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001355#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 /*
1357 * We want to ignore breaking of PIPEs.
1358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 signal(SIGPIPE, SIG_IGN);
1360#endif
1361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001363 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364#endif
1365
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001366#ifdef SIGUSR1
1367 /*
1368 * Call user's handler on SIGUSR1
1369 */
1370 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
1371#endif
1372
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 /*
1374 * Ignore alarm signals (Perl's alarm() generates it).
1375 */
1376#ifdef SIGALRM
1377 signal(SIGALRM, SIG_IGN);
1378#endif
1379
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001380#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 /*
1382 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1383 * work will be lost.
1384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1386#endif
1387
1388 /*
1389 * Arrange for other signals to gracefully shutdown Vim.
1390 */
1391 catch_signals(deathtrap, SIG_ERR);
1392
1393#if defined(FEAT_GUI) && defined(SIGHUP)
1394 /*
1395 * When the GUI is running, ignore the hangup signal.
1396 */
1397 if (gui.in_use)
1398 signal(SIGHUP, SIG_IGN);
1399#endif
1400}
1401
Bram Moolenaardf177f62005-02-22 08:39:57 +00001402#if defined(SIGINT) || defined(PROTO)
1403/*
1404 * Catch CTRL-C (only works while in Cooked mode).
1405 */
1406 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001407catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001408{
1409 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1410}
1411#endif
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001414reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
1416 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001417#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001418 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 signal(SIGCONT, SIG_DFL);
1420#endif
1421}
1422
1423 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001424catch_signals(
1425 RETSIGTYPE (*func_deadly)(),
1426 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428 int i;
1429
1430 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if (signal_info[i].deadly)
1433 {
1434#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1435 struct sigaction sa;
1436
Bram Moolenaar0f873732019-12-05 20:28:46 +01001437 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 sa.sa_handler = func_deadly;
1439 sigemptyset(&sa.sa_mask);
1440# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001441 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1442 // thread handling in combination with using the alternate stack:
1443 // pthread library functions try to use the stack pointer to
1444 // identify the current thread, causing a SEGV signal, which
1445 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 sa.sa_flags = 0;
1447# else
1448 sa.sa_flags = SA_ONSTACK;
1449# endif
1450 sigaction(signal_info[i].sig, &sa, NULL);
1451#else
1452# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1453 struct sigvec sv;
1454
Bram Moolenaar0f873732019-12-05 20:28:46 +01001455 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 sv.sv_handler = func_deadly;
1457 sv.sv_mask = 0;
1458 sv.sv_flags = SV_ONSTACK;
1459 sigvec(signal_info[i].sig, &sv, NULL);
1460# else
1461 signal(signal_info[i].sig, func_deadly);
1462# endif
1463#endif
1464 }
1465 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001466 {
1467 // Deal with non-deadly signals.
1468#ifdef SIGTSTP
1469 signal(signal_info[i].sig,
1470 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1471 ? SIG_IGN : func_other);
1472#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001474#endif
1475 }
1476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477}
1478
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001479#ifdef HAVE_SIGPROCMASK
1480 static void
1481block_signals(sigset_t *set)
1482{
1483 sigset_t newset;
1484 int i;
1485
1486 sigemptyset(&newset);
1487
1488 for (i = 0; signal_info[i].sig != -1; i++)
1489 sigaddset(&newset, signal_info[i].sig);
1490
Bram Moolenaar2e310482018-08-21 13:09:10 +02001491# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001492 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001493 sigaddset(&newset, SIGCONT);
1494# endif
1495
1496 sigprocmask(SIG_BLOCK, &newset, set);
1497}
1498
1499 static void
1500unblock_signals(sigset_t *set)
1501{
1502 sigprocmask(SIG_SETMASK, set, NULL);
1503}
1504#endif
1505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001507 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001508 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1509 * return TRUE
1510 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1511 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001512 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001513 * Returns TRUE when Vim should exit.
1514 */
1515 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001516vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001517{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001518 static int got_signal = 0;
1519 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001520
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001521 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001522 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001523 case SIGNAL_BLOCK: blocked = TRUE;
1524 break;
1525
1526 case SIGNAL_UNBLOCK: blocked = FALSE;
1527 if (got_signal != 0)
1528 {
1529 kill(getpid(), got_signal);
1530 got_signal = 0;
1531 }
1532 break;
1533
1534 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001535 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001536 got_signal = sig;
1537#ifdef SIGPWR
1538 if (sig != SIGPWR)
1539#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001540 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001541 break;
1542 }
1543 return FALSE;
1544}
1545
1546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 * Check_win checks whether we have an interactive stdout.
1548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001550mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 if (isatty(1))
1553 return OK;
1554 return FAIL;
1555}
1556
1557/*
1558 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1559 */
1560 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001561mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562{
1563 if (isatty(read_cmd_fd))
1564 return TRUE;
1565 return FALSE;
1566}
1567
1568#ifdef FEAT_X11
1569
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001570# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1572
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573/*
1574 * Give a message about the elapsed time for opening the X window.
1575 */
1576 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001577xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001579 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580}
1581# endif
1582#endif
1583
1584#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1585/*
1586 * A few functions shared by X11 title and clipboard code.
1587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
1589static int got_x_error = FALSE;
1590
1591/*
1592 * X Error handler, otherwise X just exits! (very rude) -- webb
1593 */
1594 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001595x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001597 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 STRCAT(IObuff, _("\nVim: Got X error\n"));
1599
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001600 // In the GUI we cannot print a message and continue, because no X calls
1601 // are allowed here (causes my system to hang). Silently continuing seems
1602 // like the best alternative. Do preserve files, in case we crash.
1603 ml_sync_all(FALSE, FALSE);
1604
1605#ifdef FEAT_GUI
1606 if (!gui.in_use)
1607#endif
1608 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
Bram Moolenaar0f873732019-12-05 20:28:46 +01001610 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611}
1612
1613/*
1614 * Another X Error handler, just used to check for errors.
1615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001617x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
1619 got_x_error = TRUE;
1620 return 0;
1621}
1622
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001623/*
1624 * Return TRUE when connection to the X server is desired.
1625 */
1626 static int
1627x_connect_to_server(void)
1628{
1629 // No point in connecting if we are exiting or dying.
1630 if (exiting || v_dying)
1631 return FALSE;
1632
1633#if defined(FEAT_CLIENTSERVER)
1634 if (x_force_connect)
1635 return TRUE;
1636#endif
1637 if (x_no_connect)
1638 return FALSE;
1639
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001640 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001641 if (clip_exclude_prog != NULL)
1642 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001643 // Just in case we get called recursively, return FALSE. This could
1644 // happen if vpeekc() is used while executing the prog and it causes a
1645 // related callback to be invoked.
1646 if (regprog_in_use(clip_exclude_prog))
1647 return FALSE;
1648
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001649 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1650 return FALSE;
1651 }
1652 return TRUE;
1653}
1654
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001656# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657/*
1658 * An X IO Error handler, used to catch error while opening the display.
1659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001661x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001663 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001665# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001666 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001667# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668}
1669# endif
1670
1671/*
1672 * An X IO Error handler, used to catch terminal errors.
1673 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001674static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001677x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
1679 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001680 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 x11_window = 0;
1682 x11_display = NULL;
1683 xterm_Shell = (Widget)0;
1684
Bram Moolenaar0f873732019-12-05 20:28:46 +01001685 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001687# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001688 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001689# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001691
1692/*
1693 * If the X11 connection was lost try to restore it.
1694 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001695 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001696 */
1697 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001698may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001699{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001700 // No point in restoring the connecting if we are exiting or dying.
1701 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001702 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001703 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001704
1705# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001706 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001707 if (app_context != (XtAppContext)NULL)
1708 {
1709 XtDestroyApplicationContext(app_context);
1710 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001711 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001712 }
1713# endif
1714
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001715 setup_term_clip();
1716 get_x11_title(FALSE);
1717 }
1718}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001719
1720 void
1721ex_xrestore(exarg_T *eap)
1722{
1723 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1724 {
1725 if (xterm_display_allocated)
1726 vim_free(xterm_display);
1727 xterm_display = (char *)vim_strsave(eap->arg);
1728 xterm_display_allocated = TRUE;
1729 }
1730 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001731 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001732
1733 clear_xterm_clip();
1734 x11_window = 0;
1735 xterm_dpy_retry_count = 5; // Try reconnecting five times
1736 may_restore_clipboard();
1737}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#endif
1739
1740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 * Test if "dpy" and x11_window are valid by getting the window title.
1742 * I don't actually want it yet, so there may be a simpler call to use, but
1743 * this will cause the error handler x_error_check() to be called if anything
1744 * is wrong, such as the window pointer being invalid (as can happen when the
1745 * user changes his DISPLAY, but not his WINDOWID) -- webb
1746 */
1747 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001748test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 int (*old_handler)();
1751 XTextProperty text_prop;
1752
1753 old_handler = XSetErrorHandler(x_error_check);
1754 got_x_error = FALSE;
1755 if (XGetWMName(dpy, x11_window, &text_prop))
1756 XFree((void *)text_prop.value);
1757 XSync(dpy, False);
1758 (void)XSetErrorHandler(old_handler);
1759
1760 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001761 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762
1763 return (got_x_error ? FAIL : OK);
1764}
1765#endif
1766
1767#ifdef FEAT_TITLE
1768
1769#ifdef FEAT_X11
1770
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001771static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
1773/*
1774 * try to get x11 window and display
1775 *
1776 * return FAIL for failure, OK otherwise
1777 */
1778 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001779get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
1781 char *winid;
1782 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001783#define XD_NONE 0 // x11_display not set here
1784#define XD_HERE 1 // x11_display opened here
1785#define XD_GUI 2 // x11_display used from gui.dpy
1786#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 static int x11_display_from = XD_NONE;
1788 static int did_set_error_handler = FALSE;
1789
1790 if (!did_set_error_handler)
1791 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001792 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 (void)XSetErrorHandler(x_error_handler);
1794 did_set_error_handler = TRUE;
1795 }
1796
Bram Moolenaar9372a112005-12-06 19:59:18 +00001797#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 if (gui.in_use)
1799 {
1800 /*
1801 * If the X11 display was opened here before, for the window where Vim
1802 * was started, close that one now to avoid a memory leak.
1803 */
1804 if (x11_display_from == XD_HERE && x11_display != NULL)
1805 {
1806 XCloseDisplay(x11_display);
1807 x11_display_from = XD_NONE;
1808 }
1809 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1810 {
1811 x11_display_from = XD_GUI;
1812 return OK;
1813 }
1814 x11_display = NULL;
1815 return FAIL;
1816 }
1817 else if (x11_display_from == XD_GUI)
1818 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001819 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 x11_window = 0;
1821 x11_display = NULL;
1822 x11_display_from = XD_NONE;
1823 }
1824#endif
1825
Bram Moolenaar0f873732019-12-05 20:28:46 +01001826 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (!x_connect_to_server())
1828 return FAIL;
1829
1830 /*
1831 * If WINDOWID not set, should try another method to find out
1832 * what the current window number is. The only code I know for
1833 * this is very complicated.
1834 * We assume that zero is invalid for WINDOWID.
1835 */
1836 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1837 x11_window = (Window)atol(winid);
1838
1839#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001840 if (xterm_dpy == x11_display)
1841 // x11_display may have been set to xterm_dpy elsewhere
1842 x11_display_from = XD_XTERM;
1843
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 if (xterm_dpy != NULL && x11_window != 0)
1845 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001846 // We may have checked it already, but Gnome terminal can move us to
1847 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001848 if (x11_display_from != XD_XTERM)
1849 {
1850 /*
1851 * If the X11 display was opened here before, for the window where
1852 * Vim was started, close that one now to avoid a memory leak.
1853 */
1854 if (x11_display_from == XD_HERE && x11_display != NULL)
1855 XCloseDisplay(x11_display);
1856 x11_display = xterm_dpy;
1857 x11_display_from = XD_XTERM;
1858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (test_x11_window(x11_display) == FAIL)
1860 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001861 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 x11_window = 0;
1863 x11_display = NULL;
1864 x11_display_from = XD_NONE;
1865 return FAIL;
1866 }
1867 return OK;
1868 }
1869#endif
1870
1871 if (x11_window == 0 || x11_display == NULL)
1872 result = -1;
1873
Bram Moolenaar0f873732019-12-05 20:28:46 +01001874 if (result != -1) // Have already been here and set this
1875 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
1877 if (x11_window != 0 && x11_display == NULL)
1878 {
1879#ifdef SET_SIG_ALARM
1880 RETSIGTYPE (*sig_save)();
1881#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001882#ifdef ELAPSED_FUNC
1883 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884
1885 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001886 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887#endif
1888
1889#ifdef SET_SIG_ALARM
1890 /*
1891 * Opening the Display may hang if the DISPLAY setting is wrong, or
1892 * the network connection is bad. Set an alarm timer to get out.
1893 */
1894 sig_alarm_called = FALSE;
1895 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1896 (RETSIGTYPE (*)())sig_alarm);
1897 alarm(2);
1898#endif
1899 x11_display = XOpenDisplay(NULL);
1900
1901#ifdef SET_SIG_ALARM
1902 alarm(0);
1903 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1904 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001905 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906#endif
1907 if (x11_display != NULL)
1908 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001909# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001911 {
1912 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001913 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001914 verbose_leave();
1915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916# endif
1917 if (test_x11_window(x11_display) == FAIL)
1918 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001919 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 x11_window = 0;
1921 XCloseDisplay(x11_display);
1922 x11_display = NULL;
1923 }
1924 else
1925 x11_display_from = XD_HERE;
1926 }
1927 }
1928 if (x11_window == 0 || x11_display == NULL)
1929 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001930
1931# ifdef FEAT_EVAL
1932 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1933# endif
1934
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 return (result = OK);
1936}
1937
1938/*
1939 * Determine original x11 Window Title
1940 */
1941 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001942get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001944 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945}
1946
1947/*
1948 * Determine original x11 Window icon
1949 */
1950 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001951get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
1953 int retval = FALSE;
1954
1955 retval = get_x11_thing(FALSE, test_only);
1956
Bram Moolenaar0f873732019-12-05 20:28:46 +01001957 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 if (oldicon == NULL && !test_only)
1959 {
1960 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001961 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001963 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
1965
1966 return retval;
1967}
1968
1969 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001970get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01001971 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01001972 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
1974 XTextProperty text_prop;
1975 int retval = FALSE;
1976 Status status;
1977
1978 if (get_x11_windis() == OK)
1979 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001980 // Get window/icon name if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 if (get_title)
1982 status = XGetWMName(x11_display, x11_window, &text_prop);
1983 else
1984 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1985
1986 /*
1987 * If terminal is xterm, then x11_window may be a child window of the
1988 * outer xterm window that actually contains the window/icon name, so
1989 * keep traversing up the tree until a window with a title/icon is
1990 * found.
1991 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001992 // Previously this was only done for xterm and alike. I don't see a
Bram Moolenaar0f873732019-12-05 20:28:46 +01001993 // reason why it would fail for other terminal emulators.
1994 // if (term_is_xterm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 {
1996 Window root;
1997 Window parent;
1998 Window win = x11_window;
1999 Window *children;
2000 unsigned int num_children;
2001
2002 while (!status || text_prop.value == NULL)
2003 {
2004 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2005 &num_children))
2006 break;
2007 if (children)
2008 XFree((void *)children);
2009 if (parent == root || parent == 0)
2010 break;
2011
2012 win = parent;
2013 if (get_title)
2014 status = XGetWMName(x11_display, win, &text_prop);
2015 else
2016 status = XGetWMIconName(x11_display, win, &text_prop);
2017 }
2018 }
2019 if (status && text_prop.value != NULL)
2020 {
2021 retval = TRUE;
2022 if (!test_only)
2023 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002024 if (get_title)
2025 vim_free(oldtitle);
2026 else
2027 vim_free(oldicon);
Bram Moolenaara12a1612019-01-24 16:39:02 +01002028 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (get_title)
2031 oldtitle = vim_strsave((char_u *)text_prop.value);
2032 else
2033 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 }
2035 else
2036 {
2037 char **cl;
2038 Status transform_status;
2039 int n = 0;
2040
2041 transform_status = XmbTextPropertyToTextList(x11_display,
2042 &text_prop,
2043 &cl, &n);
2044 if (transform_status >= Success && n > 0 && cl[0])
2045 {
2046 if (get_title)
2047 oldtitle = vim_strsave((char_u *) cl[0]);
2048 else
2049 oldicon = vim_strsave((char_u *) cl[0]);
2050 XFreeStringList(cl);
2051 }
2052 else
2053 {
2054 if (get_title)
2055 oldtitle = vim_strsave((char_u *)text_prop.value);
2056 else
2057 oldicon = vim_strsave((char_u *)text_prop.value);
2058 }
2059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 }
2061 XFree((void *)text_prop.value);
2062 }
2063 }
2064 return retval;
2065}
2066
Bram Moolenaar0f873732019-12-05 20:28:46 +01002067// Xutf8 functions are not available on older systems. Note that on some
2068// systems X_HAVE_UTF8_STRING may be defined in a header file but
2069// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2070// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002071#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002072# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073# define USE_UTF8_STRING
2074# endif
2075#endif
2076
2077/*
2078 * Set x11 Window Title
2079 *
2080 * get_x11_windis() must be called before this and have returned OK
2081 */
2082 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002083set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002085 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2086 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2087 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088#ifdef USE_UTF8_STRING
2089 if (enc_utf8)
2090 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2091 NULL, NULL, 0, NULL, NULL, NULL);
2092 else
2093#endif
2094 {
2095#if XtSpecificationRelease >= 4
2096# ifdef FEAT_XFONTSET
2097 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2098 NULL, NULL, 0, NULL, NULL, NULL);
2099# else
2100 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002101 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102
Bram Moolenaar0f873732019-12-05 20:28:46 +01002103 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002104 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 XSetWMProperties(x11_display, x11_window, &text_prop,
2106 NULL, NULL, 0, NULL, NULL, NULL);
2107# endif
2108#else
2109 XStoreName(x11_display, x11_window, (char *)title);
2110#endif
2111 }
2112 XFlush(x11_display);
2113}
2114
2115/*
2116 * Set x11 Window icon
2117 *
2118 * get_x11_windis() must be called before this and have returned OK
2119 */
2120 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002121set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002123 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124#ifdef USE_UTF8_STRING
2125 if (enc_utf8)
2126 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2127 NULL, 0, NULL, NULL, NULL);
2128 else
2129#endif
2130 {
2131#if XtSpecificationRelease >= 4
2132# ifdef FEAT_XFONTSET
2133 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2134 NULL, 0, NULL, NULL, NULL);
2135# else
2136 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002137 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002139 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2141 NULL, 0, NULL, NULL, NULL);
2142# endif
2143#else
2144 XSetIconName(x11_display, x11_window, (char *)icon);
2145#endif
2146 }
2147 XFlush(x11_display);
2148}
2149
Bram Moolenaar0f873732019-12-05 20:28:46 +01002150#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002153get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154{
2155 return FALSE;
2156}
2157
2158 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002159get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160{
2161 if (!test_only)
2162 {
2163 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002164 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002166 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 }
2168 return FALSE;
2169}
2170
Bram Moolenaar0f873732019-12-05 20:28:46 +01002171#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
2173 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002174mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
2176 return get_x11_title(TRUE);
2177}
2178
2179 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002180mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181{
2182 return get_x11_icon(TRUE);
2183}
2184
2185/*
2186 * Set the window title and icon.
2187 */
2188 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002189mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
2191 int type = 0;
2192 static int recursive = 0;
2193
Bram Moolenaar0f873732019-12-05 20:28:46 +01002194 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002196 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 return;
2198
Bram Moolenaar0f873732019-12-05 20:28:46 +01002199 // When one of the X11 functions causes a deadly signal, we get here again
2200 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (recursive)
2202 return;
2203 ++recursive;
2204
2205 /*
2206 * if the window ID and the display is known, we may use X11 calls
2207 */
2208#ifdef FEAT_X11
2209 if (get_x11_windis() == OK)
2210 type = 1;
2211#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002212# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002213 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (gui.in_use)
2215 type = 1;
2216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217#endif
2218
2219 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002220 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 * than x11 calls, because the x11 calls don't always work
2222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 if ((type || *T_TS != NUL) && title != NULL)
2224 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002225 if (oldtitle_outdated)
2226 {
2227 oldtitle_outdated = FALSE;
2228 VIM_CLEAR(oldtitle);
2229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 if (oldtitle == NULL
2231#ifdef FEAT_GUI
2232 && !gui.in_use
2233#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002234 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 (void)get_x11_title(FALSE);
2236
Bram Moolenaar0f873732019-12-05 20:28:46 +01002237 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 term_settitle(title);
2239#ifdef FEAT_X11
2240 else
2241# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002242 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002244 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002246#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002247 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 else
2249 gui_mch_settitle(title, icon);
2250#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002251 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 }
2253
2254 if ((type || *T_CIS != NUL) && icon != NULL)
2255 {
2256 if (oldicon == NULL
2257#ifdef FEAT_GUI
2258 && !gui.in_use
2259#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002260 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 get_x11_icon(FALSE);
2262
2263 if (*T_CIS != NUL)
2264 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002265 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002267 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 out_flush();
2269 }
2270#ifdef FEAT_X11
2271 else
2272# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002273 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002275 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276#endif
2277 did_set_icon = TRUE;
2278 }
2279 --recursive;
2280}
2281
2282/*
2283 * Restore the window/icon title.
2284 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002285 * SAVE_RESTORE_TITLE only restore title
2286 * SAVE_RESTORE_ICON only restore icon
2287 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 */
2289 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002290mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
Bram Moolenaardac13472019-09-16 21:06:21 +02002292 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002293
Bram Moolenaar0f873732019-12-05 20:28:46 +01002294 // only restore the title or icon when it has been set
Bram Moolenaardac13472019-09-16 21:06:21 +02002295 mch_settitle(((which & SAVE_RESTORE_TITLE) && unix_did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002297 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2298
Bram Moolenaare5c83282019-05-03 23:15:37 +02002299 if (do_push_pop)
2300 {
2301 // pop and push from/to the stack
2302 term_pop_title(which);
2303 term_push_title(which);
2304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305}
2306
Bram Moolenaar0f873732019-12-05 20:28:46 +01002307#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
2309/*
2310 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002311 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 */
2313 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002314vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315{
2316 if (name == NULL)
2317 return FALSE;
2318 return (STRNICMP(name, "xterm", 5) == 0
2319 || STRNICMP(name, "nxterm", 6) == 0
2320 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002321 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002323 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 || STRCMP(name, "builtin_xterm") == 0);
2325}
2326
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002327#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2328/*
2329 * Return TRUE if "name" appears to be that of a terminal
2330 * known to support the xterm-style mouse protocol.
2331 * Relies on term_is_xterm having been set to its correct value.
2332 */
2333 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002334use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002335{
2336 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002337 && (term_is_xterm
2338 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002339 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002340 || STRICMP(name, "st") == 0
2341 || STRNICMP(name, "st-", 3) == 0
2342 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002343}
2344#endif
2345
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346/*
2347 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2348 * Return 1 for "xterm".
2349 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002350 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002351 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 */
2353 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002354use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002356 if (ttym_flags == TTYM_SGR)
2357 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002358 if (ttym_flags == TTYM_URXVT)
2359 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 if (ttym_flags == TTYM_XTERM2)
2361 return 2;
2362 if (ttym_flags == TTYM_XTERM)
2363 return 1;
2364 return 0;
2365}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
2367 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002368vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370 if (name == NULL)
2371 return FALSE;
2372 return (STRNICMP(name, "iris-ansi", 9) == 0
2373 || STRCMP(name, "builtin_iris-ansi") == 0);
2374}
2375
2376 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002377vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
2379 if (name == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002380 return FALSE; // actually all ANSI comp. terminals should be here
2381 // catch VT100 - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002382 return ((STRNICMP(name, "vt", 2) == 0
2383 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 || STRCMP(name, "builtin_vt320") == 0);
2385}
2386
2387/*
2388 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2389 * This should include all windowed terminal emulators.
2390 */
2391 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002392vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393{
2394 if (name == NULL)
2395 return FALSE;
2396 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2397 return TRUE;
2398 return ( STRNICMP(name, "hpterm", 6) == 0
2399 || STRNICMP(name, "sun-cmd", 7) == 0
2400 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002401 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 || STRNICMP(name, "dtterm", 6) == 0);
2403}
2404
2405/*
2406 * Insert user name in s[len].
2407 * Return OK if a name found.
2408 */
2409 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002410mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
2412#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002413 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 return OK;
2415#else
2416 return mch_get_uname(getuid(), s, len);
2417#endif
2418}
2419
2420/*
2421 * Insert user name for "uid" in s[len].
2422 * Return OK if a name found.
2423 */
2424 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002425mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426{
2427#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2428 struct passwd *pw;
2429
2430 if ((pw = getpwuid(uid)) != NULL
2431 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2432 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002433 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 return OK;
2435 }
2436#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002437 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2438 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439}
2440
2441/*
2442 * Insert host name is s[len].
2443 */
2444
2445#ifdef HAVE_SYS_UTSNAME_H
2446 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002447mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448{
2449 struct utsname vutsname;
2450
2451 if (uname(&vutsname) < 0)
2452 *s = NUL;
2453 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002454 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002456#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
2458# ifdef HAVE_SYS_SYSTEMINFO_H
2459# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2460# endif
2461
2462 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002463mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465# ifdef VAXC
2466 vaxc$gethostname((char *)s, len);
2467# else
2468 gethostname((char *)s, len);
2469# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002470 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002472#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474/*
2475 * return process ID
2476 */
2477 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002478mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479{
2480 return (long)getpid();
2481}
2482
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002483/*
2484 * return TRUE if process "pid" is still running
2485 */
2486 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002487mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002488{
2489 // EMX kill() not working correctly, it seems
2490 return kill(pid, 0) == 0;
2491}
2492
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002495strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
2497 extern int sys_nerr;
2498 extern char *sys_errlist[];
2499 static char er[20];
2500
2501 if (err > 0 && err < sys_nerr)
2502 return (sys_errlist[err]);
2503 sprintf(er, "Error %d", err);
2504 return er;
2505}
2506#endif
2507
2508/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002509 * Get name of current directory into buffer "buf" of length "len" bytes.
2510 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 * Return OK for success, FAIL for failure.
2512 */
2513 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002514mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516#if defined(USE_GETCWD)
2517 if (getcwd((char *)buf, len) == NULL)
2518 {
2519 STRCPY(buf, strerror(errno));
2520 return FAIL;
2521 }
2522 return OK;
2523#else
2524 return (getwd((char *)buf) != NULL ? OK : FAIL);
2525#endif
2526}
2527
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002529 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 *
2531 * return FAIL for failure, OK for success
2532 */
2533 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002534mch_FullName(
2535 char_u *fname,
2536 char_u *buf,
2537 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002538 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
2540 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002541#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002543 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 char_u olddir[MAXPATHL];
2546 char_u *p;
2547 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002548#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002549 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2550 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002551#endif
2552
Bram Moolenaar38323e42007-03-06 19:22:53 +00002553#ifdef VMS
2554 fname = vms_fixfilename(fname);
2555#endif
2556
Bram Moolenaara2442432007-04-26 14:26:37 +00002557#ifdef __CYGWIN__
2558 /*
2559 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2560 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002561# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002562 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2563 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002564 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2565 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002566# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002567 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002568# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002569 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002570#endif
2571
Bram Moolenaar0f873732019-12-05 20:28:46 +01002572 // Expand it if forced or not an absolute path.
2573 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002574 if ((force || !mch_isFullName(fname))
2575 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
2577 /*
2578 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002579 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 * This will get the correct path name with "../" things.
2581 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002582 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002584#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 /*
2586 * Use fchdir() if possible, it's said to be faster and more
2587 * reliable. But on SunOS 4 it might not work. Check this by
2588 * doing a fchdir() right now.
2589 */
2590 if (!dont_fchdir)
2591 {
2592 fd = open(".", O_RDONLY | O_EXTRA, 0);
2593 if (fd >= 0 && fchdir(fd) < 0)
2594 {
2595 close(fd);
2596 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002597 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
2599 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601
Bram Moolenaar0f873732019-12-05 20:28:46 +01002602 // Only change directory when we are sure we can return to where
2603 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002605#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 (mch_dirname(olddir, MAXPATHL) == FAIL
2609 || mch_chdir((char *)olddir) != 0))
2610 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002611 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 retval = FAIL;
2613 }
2614 else
2615 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002616 // The directory is copied into buf[], to be able to remove
2617 // the file name without changing it (could be a string in
2618 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 if (p - fname >= len)
2620 retval = FAIL;
2621 else
2622 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002623 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if (mch_chdir((char *)buf))
2625 retval = FAIL;
2626 else
2627 fname = p + 1;
2628 *buf = NUL;
2629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
2631 }
2632 if (mch_dirname(buf, len) == FAIL)
2633 {
2634 retval = FAIL;
2635 *buf = NUL;
2636 }
2637 if (p != NULL)
2638 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002639#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (fd >= 0)
2641 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002642 if (p_verbose >= 5)
2643 {
2644 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002645 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002646 verbose_leave();
2647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 l = fchdir(fd);
2649 close(fd);
2650 }
2651 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 l = mch_chdir((char *)olddir);
2654 if (l != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002655 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 }
2657
2658 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002659 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002660 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002661#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002662 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002664 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002667
Bram Moolenaar0f873732019-12-05 20:28:46 +01002668 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002669 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 return FAIL;
2671
Bram Moolenaar0f873732019-12-05 20:28:46 +01002672 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (STRCMP(fname, ".") != 0)
2674 STRCAT(buf, fname);
2675
2676 return OK;
2677}
2678
2679/*
2680 * Return TRUE if "fname" does not depend on the current directory.
2681 */
2682 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002683mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002685#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 return ( fname[0] == '/' || fname[0] == '.' ||
2687 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2688 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2689 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002690#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692#endif
2693}
2694
Bram Moolenaar24552be2005-12-10 20:17:30 +00002695#if defined(USE_FNAME_CASE) || defined(PROTO)
2696/*
2697 * Set the case of the file name, if it already exists. This will cause the
2698 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002699 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002700 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002701 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002702fname_case(
2703 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002704 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002705{
2706 struct stat st;
2707 char_u *slash, *tail;
2708 DIR *dirp;
2709 struct dirent *dp;
2710
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002711 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002712 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002713 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002714 slash = vim_strrchr(name, '/');
2715 if (slash == NULL)
2716 {
2717 dirp = opendir(".");
2718 tail = name;
2719 }
2720 else
2721 {
2722 *slash = NUL;
2723 dirp = opendir((char *)name);
2724 *slash = '/';
2725 tail = slash + 1;
2726 }
2727
2728 if (dirp != NULL)
2729 {
2730 while ((dp = readdir(dirp)) != NULL)
2731 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002732 // Only accept names that differ in case and are the same byte
2733 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002734 if (STRICMP(tail, dp->d_name) == 0
2735 && STRLEN(tail) == STRLEN(dp->d_name))
2736 {
2737 char_u newname[MAXPATHL + 1];
2738 struct stat st2;
2739
Bram Moolenaar0f873732019-12-05 20:28:46 +01002740 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002741 vim_strncpy(newname, name, MAXPATHL);
2742 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2743 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002744 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002745 && st.st_ino == st2.st_ino
2746 && st.st_dev == st2.st_dev)
2747 {
2748 STRCPY(tail, dp->d_name);
2749 break;
2750 }
2751 }
2752 }
2753
2754 closedir(dirp);
2755 }
2756 }
2757}
2758#endif
2759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760/*
2761 * Get file permissions for 'name'.
2762 * Returns -1 when it doesn't exist.
2763 */
2764 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002765mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766{
2767 struct stat statb;
2768
Bram Moolenaar0f873732019-12-05 20:28:46 +01002769 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770#ifdef VMS
2771 if (stat((char *)vms_fixfilename(name), &statb))
2772#else
2773 if (stat((char *)name, &statb))
2774#endif
2775 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002776#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002777 // The top bit makes the value negative, which means the file doesn't
2778 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002779 return statb.st_mode & ~S_ADDACE;
2780#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783}
2784
2785/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002786 * Set file permission for "name" to "perm".
2787 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 */
2789 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002790mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791{
2792 return (chmod((char *)
2793#ifdef VMS
2794 vms_fixfilename(name),
2795#else
2796 name,
2797#endif
2798 (mode_t)perm) == 0 ? OK : FAIL);
2799}
2800
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002801#if defined(HAVE_FCHMOD) || defined(PROTO)
2802/*
2803 * Set file permission for open file "fd" to "perm".
2804 * Return FAIL for failure, OK otherwise.
2805 */
2806 int
2807mch_fsetperm(int fd, long perm)
2808{
2809 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2810}
2811#endif
2812
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813#if defined(HAVE_ACL) || defined(PROTO)
2814# ifdef HAVE_SYS_ACL_H
2815# include <sys/acl.h>
2816# endif
2817# ifdef HAVE_SYS_ACCESS_H
2818# include <sys/access.h>
2819# endif
2820
2821# ifdef HAVE_SOLARIS_ACL
2822typedef struct vim_acl_solaris_T {
2823 int acl_cnt;
2824 aclent_t *acl_entry;
2825} vim_acl_solaris_T;
2826# endif
2827
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002828#if defined(HAVE_SELINUX) || defined(PROTO)
2829/*
2830 * Copy security info from "from_file" to "to_file".
2831 */
2832 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002833mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002834{
2835 if (from_file == NULL)
2836 return;
2837
2838 if (selinux_enabled == -1)
2839 selinux_enabled = is_selinux_enabled();
2840
2841 if (selinux_enabled > 0)
2842 {
Bram Moolenaar89560232020-10-09 23:04:47 +02002843 // Use "char *" instead of "security_context_t" to avoid a deprecation
2844 // warning.
2845 char *from_context = NULL;
2846 char *to_context = NULL;
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002847
2848 if (getfilecon((char *)from_file, &from_context) < 0)
2849 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002850 // If the filesystem doesn't support extended attributes,
2851 // the original had no special security context and the
2852 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002853 if (errno == EOPNOTSUPP)
2854 return;
2855
Bram Moolenaar32526b32019-01-19 17:43:09 +01002856 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002857 msg_outtrans(from_file);
2858 msg_putchar('\n');
2859 return;
2860 }
2861 if (getfilecon((char *)to_file, &to_context) < 0)
2862 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002863 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002864 msg_outtrans(to_file);
2865 msg_putchar('\n');
2866 freecon (from_context);
2867 return ;
2868 }
2869 if (strcmp(from_context, to_context) != 0)
2870 {
2871 if (setfilecon((char *)to_file, from_context) < 0)
2872 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002873 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002874 msg_outtrans(to_file);
2875 msg_putchar('\n');
2876 }
2877 }
2878 freecon(to_context);
2879 freecon(from_context);
2880 }
2881}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002882#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002883
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002884#if defined(HAVE_SMACK) && !defined(PROTO)
2885/*
2886 * Copy security info from "from_file" to "to_file".
2887 */
2888 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002889mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002890{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002891 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002892 {
2893 XATTR_NAME_SMACK,
2894 XATTR_NAME_SMACKEXEC,
2895 XATTR_NAME_SMACKMMAP
2896 };
2897
2898 char buffer[SMACK_LABEL_LEN];
2899 const char *name;
2900 int index;
2901 int ret;
2902 ssize_t size;
2903
2904 if (from_file == NULL)
2905 return;
2906
2907 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2908 / sizeof(smack_copied_attributes)[0]) ; index++)
2909 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002910 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002911 name = smack_copied_attributes[index];
2912
Bram Moolenaar0f873732019-12-05 20:28:46 +01002913 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002914 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2915 if (size >= 0)
2916 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002917 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002918 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2919 if (ret < 0)
2920 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002921 vim_snprintf((char *)IObuff, IOSIZE,
2922 _("Could not set security context %s for %s"),
2923 name, to_file);
2924 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002925 msg_putchar('\n');
2926 }
2927 }
2928 else
2929 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002930 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002931 switch (errno)
2932 {
2933 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002934 // extended attributes aren't supported or enabled
2935 // should a message be echoed? not sure...
2936 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002937
2938 case ERANGE:
2939 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002940 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002941 vim_snprintf((char *)IObuff, IOSIZE,
2942 _("Could not get security context %s for %s. Removing it!"),
2943 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002944 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002945 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01002946 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002947
2948 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002949 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002950 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002951 // Silently ignore errors, apparently this happens when
2952 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002953 break;
2954 }
2955 }
2956 }
2957}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002958#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002959
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960/*
2961 * Return a pointer to the ACL of file "fname" in allocated memory.
2962 * Return NULL if the ACL is not available for whatever reason.
2963 */
2964 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002965mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966{
2967 vim_acl_T ret = NULL;
2968#ifdef HAVE_POSIX_ACL
2969 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2970#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002971#ifdef HAVE_SOLARIS_ZFS_ACL
2972 acl_t *aclent;
2973
2974 if (acl_get((char *)fname, 0, &aclent) < 0)
2975 return NULL;
2976 ret = (vim_acl_T)aclent;
2977#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978#ifdef HAVE_SOLARIS_ACL
2979 vim_acl_solaris_T *aclent;
2980
2981 aclent = malloc(sizeof(vim_acl_solaris_T));
2982 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2983 {
2984 free(aclent);
2985 return NULL;
2986 }
2987 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2988 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2989 {
2990 free(aclent->acl_entry);
2991 free(aclent);
2992 return NULL;
2993 }
2994 ret = (vim_acl_T)aclent;
2995#else
2996#if defined(HAVE_AIX_ACL)
2997 int aclsize;
2998 struct acl *aclent;
2999
3000 aclsize = sizeof(struct acl);
3001 aclent = malloc(aclsize);
3002 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3003 {
3004 if (errno == ENOSPC)
3005 {
3006 aclsize = aclent->acl_len;
3007 aclent = realloc(aclent, aclsize);
3008 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3009 {
3010 free(aclent);
3011 return NULL;
3012 }
3013 }
3014 else
3015 {
3016 free(aclent);
3017 return NULL;
3018 }
3019 }
3020 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003021#endif // HAVE_AIX_ACL
3022#endif // HAVE_SOLARIS_ACL
3023#endif // HAVE_SOLARIS_ZFS_ACL
3024#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 return ret;
3026}
3027
3028/*
3029 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3030 */
3031 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003032mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
3034 if (aclent == NULL)
3035 return;
3036#ifdef HAVE_POSIX_ACL
3037 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3038#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003039#ifdef HAVE_SOLARIS_ZFS_ACL
3040 acl_set((char *)fname, (acl_t *)aclent);
3041#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042#ifdef HAVE_SOLARIS_ACL
3043 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3044 ((vim_acl_solaris_T *)aclent)->acl_entry);
3045#else
3046#ifdef HAVE_AIX_ACL
3047 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003048#endif // HAVE_AIX_ACL
3049#endif // HAVE_SOLARIS_ACL
3050#endif // HAVE_SOLARIS_ZFS_ACL
3051#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052}
3053
3054 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003055mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
3057 if (aclent == NULL)
3058 return;
3059#ifdef HAVE_POSIX_ACL
3060 acl_free((acl_t)aclent);
3061#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003062#ifdef HAVE_SOLARIS_ZFS_ACL
3063 acl_free((acl_t *)aclent);
3064#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065#ifdef HAVE_SOLARIS_ACL
3066 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3067 free(aclent);
3068#else
3069#ifdef HAVE_AIX_ACL
3070 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003071#endif // HAVE_AIX_ACL
3072#endif // HAVE_SOLARIS_ACL
3073#endif // HAVE_SOLARIS_ZFS_ACL
3074#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075}
3076#endif
3077
3078/*
3079 * Set hidden flag for "name".
3080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003082mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003084 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085}
3086
3087/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003088 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 * return FALSE if "name" is not a directory
3090 * return FALSE for error
3091 */
3092 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003093mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094{
3095 struct stat statb;
3096
Bram Moolenaar0f873732019-12-05 20:28:46 +01003097 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 return FALSE;
3099 if (stat((char *)name, &statb))
3100 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102}
3103
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003104/*
3105 * return TRUE if "name" is a directory, NOT a symlink to a directory
3106 * return FALSE if "name" is not a directory
3107 * return FALSE for error
3108 */
3109 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003110mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003111{
3112 struct stat statb;
3113
Bram Moolenaar0f873732019-12-05 20:28:46 +01003114 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003115 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003116 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003117 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003118 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003119}
3120
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121/*
3122 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3123 */
3124 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003125executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126{
3127 struct stat st;
3128
3129 if (stat((char *)name, &st))
3130 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003131#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003132 // Like on Unix system file can have executable rights but not necessarily
3133 // be an executable, but on Unix is not a default for an ordinary file to
3134 // have an executable flag - on VMS it is in most cases.
3135 // Therefore, this check does not have any sense - let keep us to the
3136 // conventions instead:
3137 // *.COM and *.EXE files are the executables - the rest are not. This is
3138 // not ideal but better then it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003139 int vms_executable = 0;
3140 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3141 {
3142 if (strstr(vms_tolower((char*)name),".exe") != NULL
3143 || strstr(vms_tolower((char*)name),".com")!= NULL)
3144 vms_executable = 1;
3145 }
3146 return vms_executable;
3147#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150}
3151
3152/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003153 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003154 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 * Return -1 if unknown.
3156 */
3157 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003158mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159{
3160 char_u *buf;
3161 char_u *p, *e;
3162 int retval;
3163
Bram Moolenaar0f873732019-12-05 20:28:46 +01003164 // When "use_path" is false and if it's an absolute or relative path don't
3165 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003166 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003167 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003168 // There must be a path separator, files in the current directory
3169 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003170 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003171 {
3172 if (path != NULL)
3173 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003174 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003175 *path = FullName_save(name, TRUE);
3176 else
3177 *path = vim_strsave(name);
3178 }
3179 return TRUE;
3180 }
3181 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
3184 p = (char_u *)getenv("PATH");
3185 if (p == NULL || *p == NUL)
3186 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003187 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (buf == NULL)
3189 return -1;
3190
3191 /*
3192 * Walk through all entries in $PATH to check if "name" exists there and
3193 * is an executable file.
3194 */
3195 for (;;)
3196 {
3197 e = (char_u *)strchr((char *)p, ':');
3198 if (e == NULL)
3199 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003200 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 STRCPY(buf, "./");
3202 else
3203 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003204 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 add_pathsep(buf);
3206 }
3207 STRCAT(buf, name);
3208 retval = executable_file(buf);
3209 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003210 {
3211 if (path != NULL)
3212 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003213 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003214 *path = FullName_save(buf, TRUE);
3215 else
3216 *path = vim_strsave(buf);
3217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220
3221 if (*e != ':')
3222 break;
3223 p = e + 1;
3224 }
3225
3226 vim_free(buf);
3227 return retval;
3228}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
3230/*
3231 * Check what "name" is:
3232 * NODE_NORMAL: file or directory (or doesn't exist)
3233 * NODE_WRITABLE: writable device, socket, fifo, etc.
3234 * NODE_OTHER: non-writable things
3235 */
3236 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003237mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
3239 struct stat st;
3240
3241 if (stat((char *)name, &st))
3242 return NODE_NORMAL;
3243 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3244 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003245 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003247 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 return NODE_WRITABLE;
3249}
3250
3251 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003252mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
3254#ifdef HAVE_CHECK_STACK_GROWTH
3255 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 check_stack_growth((char *)&i);
3258
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003259# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 get_stack_limit();
3261# endif
3262
3263#endif
3264
3265 /*
3266 * Setup an alternative stack for signals. Helps to catch signals when
3267 * running out of stack space.
3268 * Use of sigaltstack() is preferred, it's more portable.
3269 * Ignore any errors.
3270 */
3271#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar0e62a672021-02-25 17:17:56 +01003272# ifdef HAVE_SYSCONF_SIGSTKSZ
3273 signal_stack = alloc(sysconf(_SC_SIGSTKSZ));
3274# else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003275 signal_stack = alloc(SIGSTKSZ);
Bram Moolenaar0e62a672021-02-25 17:17:56 +01003276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 init_signal_stack();
3278#endif
3279}
3280
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003281#if defined(EXITFREE) || defined(PROTO)
3282 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003283mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003284{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003285# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3286 if (clip_star.owned)
3287 clip_lose_selection(&clip_star);
3288 if (clip_plus.owned)
3289 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003290# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003291# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003292 if (xterm_Shell != (Widget)0)
3293 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003294# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003295 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003296 if (xterm_dpy != NULL)
3297 XtCloseDisplay(xterm_dpy);
3298 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003299 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003300 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003301# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003302 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003303# endif
3304 }
3305# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003306# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003307# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003308 if (x11_display != NULL
3309# ifdef FEAT_XCLIPBOARD
3310 && x11_display != xterm_dpy
3311# endif
3312 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003313 XCloseDisplay(x11_display);
3314# endif
3315# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003316 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003317# endif
3318# ifdef FEAT_TITLE
3319 vim_free(oldtitle);
3320 vim_free(oldicon);
3321# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003322}
3323#endif
3324
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325/*
3326 * Output a newline when exiting.
3327 * Make sure the newline goes to the same stream as the text.
3328 */
3329 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003330exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003332 if (silent_mode)
3333 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 if (newline_on_exit || msg_didout)
3335 {
3336 if (msg_use_printf())
3337 {
3338 if (info_message)
3339 mch_msg("\n");
3340 else
3341 mch_errmsg("\r\n");
3342 }
3343 else
3344 out_char('\n');
3345 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003346 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003348 restore_cterm_colors(); // get original colors back
3349 msg_clr_eos_force(); // clear the rest of the display
3350 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 }
3352}
3353
Bram Moolenaarb4151682020-05-11 22:13:28 +02003354#ifdef USE_GCOV_FLUSH
3355extern void __gcov_flush();
3356#endif
3357
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003359mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 exiting = TRUE;
3362
3363#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3364 x11_export_final_selection();
3365#endif
3366
3367#ifdef FEAT_GUI
3368 if (!gui.in_use)
3369#endif
3370 {
3371 settmode(TMODE_COOK);
3372#ifdef FEAT_TITLE
Bram Moolenaar7007e312021-03-27 12:11:33 +01003373 if (!is_not_a_term())
3374 {
3375 // restore xterm title and icon name
3376 mch_restore_title(SAVE_RESTORE_BOTH);
3377 term_pop_title(SAVE_RESTORE_BOTH);
3378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379#endif
3380 /*
3381 * When t_ti is not empty but it doesn't cause swapping terminal
3382 * pages, need to output a newline when msg_didout is set. But when
3383 * t_ti does swap pages it should not go to the shell page. Do this
3384 * before stoptermcap().
3385 */
3386 if (swapping_screen() && !newline_on_exit)
3387 exit_scroll();
3388
Bram Moolenaar0f873732019-12-05 20:28:46 +01003389 // Stop termcap: May need to check for T_CRV response, which
3390 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 stoptermcap();
3392
3393 /*
3394 * A newline is only required after a message in the alternate screen.
3395 * This is set to TRUE by wait_return().
3396 */
3397 if (!swapping_screen() || newline_on_exit)
3398 exit_scroll();
3399
Bram Moolenaar0f873732019-12-05 20:28:46 +01003400 // Cursor may have been switched off without calling starttermcap()
3401 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (full_screen)
3403 cursor_on();
3404 }
3405 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003406 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003407
3408#ifdef USE_GCOV_FLUSH
3409 // Flush coverage info before possibly being killed by a deadly signal.
3410 __gcov_flush();
3411#endif
3412
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 may_core_dump();
3414#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 gui_exit(r);
3417#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003418
Bram Moolenaar56718732006-03-15 22:53:57 +00003419#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003420 mac_conv_cleanup();
3421#endif
3422
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003424 // A core dump won't be created if the signal handler
3425 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (deadly_signal != 0)
3427 return;
3428#endif
3429
Bram Moolenaar009b2592004-10-24 19:18:58 +00003430#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003431 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003432#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003433
3434#ifdef EXITFREE
3435 free_all_mem();
3436#endif
3437
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 exit(r);
3439}
3440
3441 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003442may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
3444 if (deadly_signal != 0)
3445 {
3446 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003447 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 }
3449}
3450
3451#ifndef VMS
3452
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003453/*
3454 * Get the file descriptor to use for tty operations.
3455 */
3456 static int
3457get_tty_fd(int fd)
3458{
3459 int tty_fd = fd;
3460
3461#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3462 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3463 // "fd" when it is a master device.
3464 if (mch_isatty(fd) > 1)
3465 {
3466 char *name;
3467
3468 name = ptsname(fd);
3469 if (name == NULL)
3470 return -1;
3471
3472 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3473 if (tty_fd < 0)
3474 return -1;
3475 }
3476#endif
3477 return tty_fd;
3478}
3479
3480 static int
3481mch_tcgetattr(int fd, void *term)
3482{
3483 int tty_fd;
3484 int retval = -1;
3485
3486 tty_fd = get_tty_fd(fd);
3487 if (tty_fd >= 0)
3488 {
3489#ifdef NEW_TTY_SYSTEM
3490# ifdef HAVE_TERMIOS_H
3491 retval = tcgetattr(tty_fd, (struct termios *)term);
3492# else
3493 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3494# endif
3495#else
3496 // for "old" tty systems
3497 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3498#endif
3499 if (tty_fd != fd)
3500 close(tty_fd);
3501 }
3502 return retval;
3503}
3504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003506mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
3508 static int first = TRUE;
3509
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003510#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511# ifdef HAVE_TERMIOS_H
3512 static struct termios told;
3513 struct termios tnew;
3514# else
3515 static struct termio told;
3516 struct termio tnew;
3517# endif
3518
3519 if (first)
3520 {
3521 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003522 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524
3525 tnew = told;
3526 if (tmode == TMODE_RAW)
3527 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003528 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003529 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3530 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003532# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003533 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534# endif
3535 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003536# ifdef ONLCR
3537 // Don't map NL -> CR NL, we do it ourselves.
3538 // Also disable expanding tabs if possible.
3539# ifdef XTABS
3540 tnew.c_oflag &= ~(ONLCR | XTABS);
3541# else
3542# ifdef TAB3
3543 tnew.c_oflag &= ~(ONLCR | TAB3);
3544# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003546# endif
3547# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003549 tnew.c_cc[VMIN] = 1; // return after 1 char
3550 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003553 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003554 // Also reset ICANON here, otherwise on Solaris select() won't see
3555 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003556 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003557 tnew.c_cc[VMIN] = 1; // return after 1 char
3558 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
3561# if defined(HAVE_TERMIOS_H)
3562 {
3563 int n = 10;
3564
Bram Moolenaar0f873732019-12-05 20:28:46 +01003565 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3566 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3568 && errno == EINTR && n > 0)
3569 --n;
3570 }
3571# else
3572 ioctl(read_cmd_fd, TCSETA, &tnew);
3573# endif
3574
3575#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 /*
3577 * for "old" tty systems
3578 */
3579# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003580# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581# endif
3582 static struct sgttyb ttybold;
3583 struct sgttyb ttybnew;
3584
3585 if (first)
3586 {
3587 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003588 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
3590
3591 ttybnew = ttybold;
3592 if (tmode == TMODE_RAW)
3593 {
3594 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3595 ttybnew.sg_flags |= RAW;
3596 }
3597 else if (tmode == TMODE_SLEEP)
3598 ttybnew.sg_flags &= ~(ECHO);
3599 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3600#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003601 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602}
3603
3604/*
3605 * Try to get the code for "t_kb" from the stty setting
3606 *
3607 * Even if termcap claims a backspace key, the user's setting *should*
3608 * prevail. stty knows more about reality than termcap does, and if
3609 * somebody's usual erase key is DEL (which, for most BSD users, it will
3610 * be), they're going to get really annoyed if their erase key starts
3611 * doing forward deletes for no reason. (Eric Fischer)
3612 */
3613 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003614get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003616 ttyinfo_T info;
3617 char_u buf[2];
3618 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003620 if (get_tty_info(read_cmd_fd, &info) == OK)
3621 {
3622 intr_char = info.interrupt;
3623 buf[0] = info.backspace;
3624 buf[1] = NUL;
3625 add_termcode((char_u *)"kb", buf, FALSE);
3626
Bram Moolenaar0f873732019-12-05 20:28:46 +01003627 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003628 p = find_termcode((char_u *)"kD");
3629 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3630 do_fixdel(NULL);
3631 }
3632}
3633
3634/*
3635 * Obtain the characters that Backspace and Enter produce on "fd".
3636 * Returns OK or FAIL.
3637 */
3638 int
3639get_tty_info(int fd, ttyinfo_T *info)
3640{
3641#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642# ifdef HAVE_TERMIOS_H
3643 struct termios keys;
3644# else
3645 struct termio keys;
3646# endif
3647
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003648 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003650 info->backspace = keys.c_cc[VERASE];
3651 info->interrupt = keys.c_cc[VINTR];
3652 if (keys.c_iflag & ICRNL)
3653 info->enter = NL;
3654 else
3655 info->enter = CAR;
3656 if (keys.c_oflag & ONLCR)
3657 info->nl_does_cr = TRUE;
3658 else
3659 info->nl_does_cr = FALSE;
3660 return OK;
3661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003663 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 struct sgttyb keys;
3665
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003666 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003668 info->backspace = keys.sg_erase;
3669 info->interrupt = keys.sg_kill;
3670 info->enter = CAR;
3671 info->nl_does_cr = TRUE;
3672 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003675 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676}
3677
Bram Moolenaar0f873732019-12-05 20:28:46 +01003678#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003680static int mouse_ison = FALSE;
3681
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682/*
3683 * Set mouse clicks on or off.
3684 */
3685 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003686mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003688#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003689 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 int xterm_mouse_vers;
3692
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003693#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003694 if (!on)
3695 // Make sure not tracing mouse movements. Important when a button-down
3696 // was received but no release yet.
3697 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003698#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003699
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003700 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003701#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003702 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003703#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003704 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003705 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 return;
3707
3708 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003709
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003710#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003711 if (ttym_flags == TTYM_URXVT)
3712 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003713 out_str_nf((char_u *)
3714 (on
3715 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3716 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003717 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003718 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003719#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003720
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003721 if (ttym_flags == TTYM_SGR)
3722 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003723 // SGR mode supports columns above 223
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003724 out_str_nf((char_u *)
3725 (on
3726 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3727 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003728 mouse_ison = on;
3729 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003730
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003731#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003732 if (bevalterm_ison != (p_bevalterm && on))
3733 {
3734 bevalterm_ison = (p_bevalterm && on);
3735 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003736 // disable mouse movement events, enabling is below
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003737 out_str_nf((char_u *)
3738 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003739 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003740#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003741
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 if (xterm_mouse_vers > 0)
3743 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003744 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 out_str_nf((char_u *)
3746 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003747 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003748#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003749 bevalterm_ison
3750 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003751#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003752 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003754 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 out_str_nf((char_u *)
3756 (xterm_mouse_vers > 1
3757 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3758 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003759 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003762#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 else if (ttym_flags == TTYM_DEC)
3764 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003765 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003767 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003769 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003773#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 else
3775 {
3776 if (on)
3777 {
3778 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003779 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 }
3781 else
3782 {
3783 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003784 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
3786 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003789#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003790 else
3791 {
3792 if (on)
3793 {
3794 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003795 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003796 }
3797 else
3798 {
3799 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003800 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003801 }
3802 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003803#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003804
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003805#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 else
3807 {
3808 if (on)
3809 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003810 // D - Enable Mouse up/down messages
3811 // L - Enable Left Button Reporting
3812 // M - Enable Middle Button Reporting
3813 // R - Enable Right Button Reporting
3814 // K - Enable SHIFT and CTRL key Reporting
3815 // + - Enable Advanced messaging of mouse moves and up/down messages
3816 // Q - Quiet No Ack
3817 // # - Numeric value of mouse pointer required
3818 // 0 = Multiview 2000 cursor, used as standard
3819 // 1 = Windows Arrow
3820 // 2 = Windows I Beam
3821 // 3 = Windows Hour Glass
3822 // 4 = Windows Cross Hair
3823 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003824# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003825 // Disables full feedback of pointer movements
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3827 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003828# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3830 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003831# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003832 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834 else
3835 {
3836 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3837 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003838 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003841#endif
3842#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 else
3844 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003845 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 if (on)
3847 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3848 else
3849 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003850 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853}
3854
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003855#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003856/*
3857 * Called when 'balloonevalterm' changed.
3858 */
3859 void
3860mch_bevalterm_changed(void)
3861{
3862 mch_setmouse(mouse_ison);
3863}
3864#endif
3865
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866/*
3867 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3868 */
3869 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003870check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871{
3872# ifdef FEAT_MOUSE_XTERM
3873 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003874# ifdef FEAT_MOUSE_URXVT
3875 && use_xterm_mouse() != 3
3876# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877# ifdef FEAT_GUI
3878 && !gui.in_use
3879# endif
3880 )
3881 {
3882 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003883 ? IF_EB("\233M", CSI_STR "M")
3884 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 if (*p_mouse != NUL)
3886 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003887 // force mouse off and maybe on to send possibly new mouse
3888 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 mch_setmouse(FALSE);
3890 setmouse();
3891 }
3892 }
3893 else
3894 del_mouse_termcode(KS_MOUSE);
3895# endif
3896
3897# ifdef FEAT_MOUSE_GPM
3898 if (!use_xterm_mouse()
3899# ifdef FEAT_GUI
3900 && !gui.in_use
3901# endif
3902 )
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003903 set_mouse_termcode(KS_GPM_MOUSE,
3904 (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3905 else
3906 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907# endif
3908
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003909# ifdef FEAT_SYSMOUSE
3910 if (!use_xterm_mouse()
3911# ifdef FEAT_GUI
3912 && !gui.in_use
3913# endif
3914 )
3915 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3916# endif
3917
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003919 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 if (!use_xterm_mouse()
3921# ifdef FEAT_GUI
3922 && !gui.in_use
3923# endif
3924 )
3925 set_mouse_termcode(KS_JSBTERM_MOUSE,
3926 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3927 else
3928 del_mouse_termcode(KS_JSBTERM_MOUSE);
3929# endif
3930
3931# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003932 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3933 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 if (!use_xterm_mouse()
3935# ifdef FEAT_GUI
3936 && !gui.in_use
3937# endif
3938 )
3939 set_mouse_termcode(KS_NETTERM_MOUSE,
3940 (char_u *)IF_EB("\033}", ESC_STR "}"));
3941 else
3942 del_mouse_termcode(KS_NETTERM_MOUSE);
3943# endif
3944
3945# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003946 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003947 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948# ifdef FEAT_GUI
3949 && !gui.in_use
3950# endif
3951 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003952 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3953 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 else
3955 del_mouse_termcode(KS_DEC_MOUSE);
3956# endif
3957# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01003958 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003959 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960# ifdef FEAT_GUI
3961 && !gui.in_use
3962# endif
3963 )
3964 set_mouse_termcode(KS_PTERM_MOUSE,
3965 (char_u *) IF_EB("\033[", ESC_STR "["));
3966 else
3967 del_mouse_termcode(KS_PTERM_MOUSE);
3968# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003969# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003970 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003971# ifdef FEAT_GUI
3972 && !gui.in_use
3973# endif
3974 )
3975 {
3976 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003977 ? IF_EB("\233*M", CSI_STR "*M")
3978 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003979
3980 if (*p_mouse != NUL)
3981 {
3982 mch_setmouse(FALSE);
3983 setmouse();
3984 }
3985 }
3986 else
3987 del_mouse_termcode(KS_URXVT_MOUSE);
3988# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003989 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003990# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003991 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003992# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003993 )
3994 {
3995 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003996 ? IF_EB("\233<*M", CSI_STR "<*M")
3997 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3998
3999 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
4000 ? IF_EB("\233<*m", CSI_STR "<*m")
4001 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004002
4003 if (*p_mouse != NUL)
4004 {
4005 mch_setmouse(FALSE);
4006 setmouse();
4007 }
4008 }
4009 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004010 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004011 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004012 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016#ifndef VMS
4017
4018/*
4019 * Try to get the current window size:
4020 * 1. with an ioctl(), most accurate method
4021 * 2. from the environment variables LINES and COLUMNS
4022 * 3. from the termcap
4023 * 4. keep using the old values
4024 * Return OK when size could be determined, FAIL otherwise.
4025 */
4026 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004027mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028{
4029 long rows = 0;
4030 long columns = 0;
4031 char_u *p;
4032
4033 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 * 1. try using an ioctl. It is the most accurate method.
4035 *
4036 * Try using TIOCGWINSZ first, some systems that have it also define
4037 * TIOCGSIZE but don't have a struct ttysize.
4038 */
4039# ifdef TIOCGWINSZ
4040 {
4041 struct winsize ws;
4042 int fd = 1;
4043
Bram Moolenaar0f873732019-12-05 20:28:46 +01004044 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 if (!isatty(fd) && isatty(read_cmd_fd))
4046 fd = read_cmd_fd;
4047 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4048 {
4049 columns = ws.ws_col;
4050 rows = ws.ws_row;
4051 }
4052 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004053# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054# ifdef TIOCGSIZE
4055 {
4056 struct ttysize ts;
4057 int fd = 1;
4058
Bram Moolenaar0f873732019-12-05 20:28:46 +01004059 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 if (!isatty(fd) && isatty(read_cmd_fd))
4061 fd = read_cmd_fd;
4062 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4063 {
4064 columns = ts.ts_cols;
4065 rows = ts.ts_lines;
4066 }
4067 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004068# endif // TIOCGSIZE
4069# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
4071 /*
4072 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004073 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4074 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004076 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
4078 if ((p = (char_u *)getenv("LINES")))
4079 rows = atoi((char *)p);
4080 if ((p = (char_u *)getenv("COLUMNS")))
4081 columns = atoi((char *)p);
4082 }
4083
4084#ifdef HAVE_TGETENT
4085 /*
4086 * 3. try reading "co" and "li" entries from termcap
4087 */
4088 if (columns == 0 || rows == 0)
4089 getlinecol(&columns, &rows);
4090#endif
4091
4092 /*
4093 * 4. If everything fails, use the old values
4094 */
4095 if (columns <= 0 || rows <= 0)
4096 return FAIL;
4097
4098 Rows = rows;
4099 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004100 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 return OK;
4102}
4103
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004104#if defined(FEAT_TERMINAL) || defined(PROTO)
4105/*
4106 * Report the windows size "rows" and "cols" to tty "fd".
4107 */
4108 int
4109mch_report_winsize(int fd, int rows, int cols)
4110{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004111 int tty_fd;
4112 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004113
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004114 tty_fd = get_tty_fd(fd);
4115 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004116 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004117# if defined(TIOCSWINSZ)
4118 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004119
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004120 ws.ws_col = cols;
4121 ws.ws_row = rows;
4122 ws.ws_xpixel = cols * 5;
4123 ws.ws_ypixel = rows * 10;
4124 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4125 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4126 retval == 0 ? "success" : "failed");
4127# elif defined(TIOCSSIZE)
4128 struct ttysize ts;
4129
4130 ts.ts_cols = cols;
4131 ts.ts_lines = rows;
4132 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4133 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4134 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004135# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004136 if (tty_fd != fd)
4137 close(tty_fd);
4138 }
4139 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004140}
4141#endif
4142
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143/*
4144 * Try to set the window size to Rows and Columns.
4145 */
4146 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004147mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148{
4149 if (*T_CWS)
4150 {
4151 /*
4152 * NOTE: if you get an error here that term_set_winsize() is
4153 * undefined, check the output of configure. It could probably not
4154 * find a ncurses, termcap or termlib library.
4155 */
4156 term_set_winsize((int)Rows, (int)Columns);
4157 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004158 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 }
4160}
4161
Bram Moolenaar0f873732019-12-05 20:28:46 +01004162#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
4164/*
4165 * Rows and/or Columns has changed.
4166 */
4167 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004168mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004170 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171}
4172
Bram Moolenaar205b8862011-09-07 15:04:31 +02004173/*
4174 * Wait for process "child" to end.
4175 * Return "child" if it exited properly, <= 0 on error.
4176 */
4177 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004178wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004179{
4180 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004181 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004182
4183 while (wait_pid != child)
4184 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004185 // When compiled with Python threads are probably used, in which case
4186 // wait() sometimes hangs for no obvious reason. Use waitpid()
4187 // instead and loop (like the GUI). Also needed for other interfaces,
4188 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004189# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004190 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004191# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004192 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004193# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004194 if (wait_pid == 0)
4195 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004196 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004197 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004198 if (++delay_msec > 10)
4199 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004200 continue;
4201 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004202 if (wait_pid <= 0
4203# ifdef ECHILD
4204 && errno == ECHILD
4205# endif
4206 )
4207 break;
4208 }
4209 return wait_pid;
4210}
4211
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004212#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004213/*
4214 * Set the environment for a child process.
4215 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004216 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004217set_child_environment(
4218 long rows,
4219 long columns,
4220 char *term,
4221 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004222{
4223# ifdef HAVE_SETENV
4224 char envbuf[50];
4225# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004226 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004227 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004228 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004229 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004230 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004231# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004232 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004233# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004234# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004235 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004236# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004237# endif
4238
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004239# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004240 setenv("TERM", term, 1);
4241 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004242 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004243 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004244 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004245 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004246 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004247 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004248 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004249# ifdef FEAT_TERMINAL
4250 if (is_terminal)
4251 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004252 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004253 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4254 }
4255# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004256# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004257 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004258# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004259# else
4260 /*
4261 * Putenv does not copy the string, it has to remain valid.
4262 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004263 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004264 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004265 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4266 putenv(envbuf_Term);
4267 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004268 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004269 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4270 putenv(envbuf_Lines);
4271 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4272 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004273 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004274 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004275 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004276# ifdef FEAT_TERMINAL
4277 if (is_terminal)
4278 {
4279 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004280 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004281 putenv(envbuf_Version);
4282 }
4283# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004284# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004285 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4286 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4287 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004288# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004289# endif
4290}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004291
4292 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004293set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004294{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004295 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004296}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004297#endif
4298
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004299#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004300/*
4301 * Open a PTY, with FD for the master and slave side.
4302 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004303 * When successful both file descriptors are stored and the allocated pty name
4304 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004305 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004306 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004307open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004308{
4309 char *tty_name;
4310
Bram Moolenaar59386482019-02-10 22:43:46 +01004311 if (name1 != NULL)
4312 *name1 = NULL;
4313 if (name2 != NULL)
4314 *name2 = NULL;
4315
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004316 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004317 if (*pty_master_fd >= 0)
4318 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004319 // Leaving out O_NOCTTY may lead to waitpid() always returning
4320 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4321 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004322#ifdef O_NOCTTY
4323 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4324#else
4325 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4326#endif
4327 if (*pty_slave_fd < 0)
4328 {
4329 close(*pty_master_fd);
4330 *pty_master_fd = -1;
4331 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004332 else
4333 {
4334 if (name1 != NULL)
4335 *name1 = vim_strsave((char_u *)tty_name);
4336 if (name2 != NULL)
4337 *name2 = vim_strsave((char_u *)tty_name);
4338 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004339 }
4340}
4341#endif
4342
Bram Moolenaarfae42832017-08-01 22:24:26 +02004343/*
4344 * Send SIGINT to a child process if "c" is an interrupt character.
4345 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004346 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004347may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4348{
4349# ifdef SIGINT
4350 if (c == Ctrl_C || c == intr_char)
4351 {
4352# ifdef HAVE_SETSID
4353 kill(-pid, SIGINT);
4354# else
4355 kill(0, SIGINT);
4356# endif
4357 if (wpid > 0)
4358 kill(wpid, SIGINT);
4359 }
4360# endif
4361}
4362
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004363#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004364
Bram Moolenaar0f873732019-12-05 20:28:46 +01004365/*
4366 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4367 * pointers, the last one is NULL.
4368 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4369 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004370 int
4371unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004372 char_u *cmd,
4373 char ***argvp,
4374 char_u **sh_tofree,
4375 char_u **shcf_tofree)
4376{
4377 char **argv = NULL;
4378 int argc;
4379
4380 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004381 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004382 return FAIL;
4383
4384 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4385 return FAIL;
4386 *argvp = argv;
4387
4388 if (cmd != NULL)
4389 {
4390 char_u *s;
4391 char_u *p;
4392
4393 if (extra_shell_arg != NULL)
4394 argv[argc++] = (char *)extra_shell_arg;
4395
Bram Moolenaar0f873732019-12-05 20:28:46 +01004396 // Break 'shellcmdflag' into white separated parts. This doesn't
4397 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004398 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004399 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004400 return FAIL;
4401 s = *shcf_tofree;
4402 p = p_shcf;
4403 while (*p != NUL)
4404 {
4405 argv[argc++] = (char *)s;
4406 while (*p && *p != ' ' && *p != TAB)
4407 *s++ = *p++;
4408 *s++ = NUL;
4409 p = skipwhite(p);
4410 }
4411
4412 argv[argc++] = (char *)cmd;
4413 }
4414 argv[argc] = NULL;
4415 return OK;
4416}
4417#endif
4418
4419#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4420/*
4421 * Use a terminal window to run a shell command in.
4422 */
4423 static int
4424mch_call_shell_terminal(
4425 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004426 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004427{
4428 jobopt_T opt;
4429 char **argv = NULL;
4430 char_u *tofree1 = NULL;
4431 char_u *tofree2 = NULL;
4432 int retval = -1;
4433 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004434 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004435 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004436 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004437
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004438 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004439 goto theend;
4440
4441 init_job_options(&opt);
4442 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4443 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004444 if (buf == NULL)
4445 goto theend;
4446
4447 job = term_getjob(buf->b_term);
4448 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004449
Bram Moolenaar0f873732019-12-05 20:28:46 +01004450 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004451 aucmd_prepbuf(&aco, buf);
4452
4453 clear_oparg(&oa);
4454 while (term_use_loop())
4455 {
4456 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4457 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004458 // If terminal_loop() returns OK we got a key that is handled
4459 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004460 if (terminal_loop(TRUE) == OK)
4461 normal_cmd(&oa, TRUE);
4462 }
4463 else
4464 normal_cmd(&oa, TRUE);
4465 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004466 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004467 ch_log(NULL, "system command finished");
4468
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004469 job_unref(job);
4470
Bram Moolenaar0f873732019-12-05 20:28:46 +01004471 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004472 aucmd_restbuf(&aco);
4473
4474 wait_return(TRUE);
4475 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4476
4477theend:
4478 vim_free(argv);
4479 vim_free(tofree1);
4480 vim_free(tofree2);
4481 return retval;
4482}
4483#endif
4484
4485#ifdef USE_SYSTEM
4486/*
4487 * Use system() to start the shell: simple but slow.
4488 */
4489 static int
4490mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004491 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004492 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493{
4494#ifdef VMS
4495 char *ifn = NULL;
4496 char *ofn = NULL;
4497#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004498 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004499 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004500 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502 out_flush();
4503
4504 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004505 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
Bram Moolenaar62b42182010-09-21 22:09:37 +02004507# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004508 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004509 loose_clipboard();
4510# endif
4511
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 if (cmd == NULL)
4513 x = system((char *)p_sh);
4514 else
4515 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004516# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 if (ofn = strchr((char *)cmd, '>'))
4518 *ofn++ = '\0';
4519 if (ifn = strchr((char *)cmd, '<'))
4520 {
4521 char *p;
4522
4523 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004524 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 if (p)
4526 *p = '\0';
4527 }
4528 if (ofn)
4529 x = vms_sys((char *)cmd, ofn, ifn);
4530 else
4531 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004532# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004533 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004535 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 if (newcmd == NULL)
4537 x = 0;
4538 else
4539 {
4540 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4541 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4542 (char *)p_shcf,
4543 (char *)cmd);
4544 x = system((char *)newcmd);
4545 vim_free(newcmd);
4546 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004547# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549# ifdef VMS
4550 x = vms_sys_status(x);
4551# endif
4552 if (emsg_silent)
4553 ;
4554 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004555 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 else if (x && !(options & SHELL_SILENT))
4557 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004558 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 msg_outnum((long)x);
4560 msg_putchar('\n');
4561 }
4562
4563 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004564 {
4565 // The shell may have messed with the mode, always set it.
4566 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004567 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569# ifdef FEAT_TITLE
4570 resettitle();
4571# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004572# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4573 restore_clipboard();
4574# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004576}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
Bram Moolenaar0f873732019-12-05 20:28:46 +01004578#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
Bram Moolenaar0f873732019-12-05 20:28:46 +01004580# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4581 // 127, some shells use that already
4582# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583
Bram Moolenaar13568252018-03-16 20:46:58 +01004584/*
4585 * Don't use system(), use fork()/exec().
4586 */
4587 static int
4588mch_call_shell_fork(
4589 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004590 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004591{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004592 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004594 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 pid_t wait_pid = 0;
4596# ifdef HAVE_UNION_WAIT
4597 union wait status;
4598# else
4599 int status = -1;
4600# endif
4601 int retval = -1;
4602 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004603 char_u *tofree1 = NULL;
4604 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004606 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004607# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004609# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004610 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 int fd_fromshell[2];
4612 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004613 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615 out_flush();
4616 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004617 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004618 if (tmode == TMODE_RAW)
4619 // The shell may have messed with the mode, always set it later.
4620 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004622 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004623 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004624
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004626 * For the GUI, when writing the output into the buffer and when reading
4627 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4628 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004630 if ((options & (SHELL_READ|SHELL_WRITE))
4631# ifdef FEAT_GUI
4632 || (gui.in_use && show_shell_mess)
4633# endif
4634 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004636# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 /*
4638 * Try to open a master pty.
4639 * If this works, open the slave pty.
4640 * If the slave can't be opened, close the master pty.
4641 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004642 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004643 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 /*
4645 * If not opening a pty or it didn't work, try using pipes.
4646 */
4647 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004648# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 {
4650 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004651 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 {
4653 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004654 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
4656 close(fd_toshell[0]);
4657 close(fd_toshell[1]);
4658 }
4659 }
4660 if (pipe_error)
4661 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004662 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 out_flush();
4664 }
4665 }
4666 }
4667
Bram Moolenaar0f873732019-12-05 20:28:46 +01004668 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004670 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004671 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004672 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004673 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004675 UNBLOCK_SIGNALS(&curset);
4676
Bram Moolenaar32526b32019-01-19 17:43:09 +01004677 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004678 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004680 || (gui.in_use && show_shell_mess)
4681# endif
4682 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004684# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004685 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {
4687 close(pty_master_fd);
4688 close(pty_slave_fd);
4689 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004690 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004691# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
4693 close(fd_toshell[0]);
4694 close(fd_toshell[1]);
4695 close(fd_fromshell[0]);
4696 close(fd_fromshell[1]);
4697 }
4698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004700 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004702 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004703 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704
Bram Moolenaar819524702018-02-27 19:10:00 +01004705# ifdef FEAT_JOB_CHANNEL
4706 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004707 {
4708 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004709 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004710 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004711# endif
4712
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 if (!show_shell_mess || (options & SHELL_EXPAND))
4714 {
4715 int fd;
4716
4717 /*
4718 * Don't want to show any message from the shell. Can't just
4719 * close stdout and stderr though, because some systems will
4720 * break if you try to write to them after that, so we must
4721 * use dup() to replace them with something else -- webb
4722 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4723 * waiting for input.
4724 */
4725 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4726 fclose(stdin);
4727 fclose(stdout);
4728 fclose(stderr);
4729
4730 /*
4731 * If any of these open()'s and dup()'s fail, we just continue
4732 * anyway. It's not fatal, and on most systems it will make
4733 * no difference at all. On a few it will cause the execvp()
4734 * to exit with a non-zero status even when the completion
4735 * could be done, which is nothing too serious. If the open()
4736 * or dup() failed we'd just do the same thing ourselves
4737 * anyway -- webb
4738 */
4739 if (fd >= 0)
4740 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004741 vim_ignored = dup(fd); // To replace stdin (fd 0)
4742 vim_ignored = dup(fd); // To replace stdout (fd 1)
4743 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744
Bram Moolenaar0f873732019-12-05 20:28:46 +01004745 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 close(fd);
4747 }
4748 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004749 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004751 || gui.in_use
4752# endif
4753 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
4755
Bram Moolenaardf177f62005-02-22 08:39:57 +00004756# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004757 // Create our own process group, so that the child and all its
4758 // children can be kill()ed. Don't do this when using pipes,
4759 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004760 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004761 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004762 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004763# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004764 // When doing "!xterm&" and 'shell' is bash: the shell
4765 // will exit and send SIGHUP to all processes in its
4766 // group, killing the just started process. Ignore SIGHUP
4767 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004768 signal(SIGHUP, SIG_IGN);
4769# endif
4770 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004771# endif
4772# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004773 if (pty_slave_fd >= 0)
4774 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004775 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004776 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004777 setup_slavepty(pty_slave_fd);
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004778 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004779# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004780 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
Bram Moolenaara5792f52005-11-23 21:25:05 +00004782 /*
4783 * stderr is only redirected when using the GUI, so that a
4784 * program like gpg can still access the terminal to get a
4785 * passphrase using stderr.
4786 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004787# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 if (pty_master_fd >= 0)
4789 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004790 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
Bram Moolenaar0f873732019-12-05 20:28:46 +01004792 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004794 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004796 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004797 if (gui.in_use)
4798 {
4799 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004800 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
Bram Moolenaar0f873732019-12-05 20:28:46 +01004803 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004808 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 close(fd_toshell[1]);
4810 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004811 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 close(fd_toshell[0]);
4813
Bram Moolenaar0f873732019-12-05 20:28:46 +01004814 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 close(fd_fromshell[0]);
4816 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004817 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 close(fd_fromshell[1]);
4819
Bram Moolenaara5792f52005-11-23 21:25:05 +00004820# ifdef FEAT_GUI
4821 if (gui.in_use)
4822 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004823 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004824 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004825 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004826 }
4827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
4829 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004830
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 /*
4832 * There is no type cast for the argv, because the type may be
4833 * different on different machines. This may cause a warning
4834 * message with strict compilers, don't worry about it.
4835 * Call _exit() instead of exit() to avoid closing the connection
4836 * to the X server (esp. with GTK, which uses atexit()).
4837 */
4838 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004839 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004841 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 {
4843 /*
4844 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004845 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 */
4847 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004848 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004849 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004850# ifdef FEAT_JOB_CHANNEL
4851 ++dont_check_job_ended;
4852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 /*
4854 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004855 * This is also used to pipe stdin/stdout to/from the external
4856 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004858 if ((options & (SHELL_READ|SHELL_WRITE))
4859# ifdef FEAT_GUI
4860 || (gui.in_use && show_shell_mess)
4861# endif
4862 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004864# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004866 int buffer_off = 0; // valid bytes in buffer[]
4867 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4868 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 int len;
4870 int p_more_save;
4871 int old_State;
4872 int c;
4873 int toshell_fd;
4874 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004875 garray_T ga;
4876 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004877# ifdef ELAPSED_FUNC
4878 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
Bram Moolenaardf177f62005-02-22 08:39:57 +00004881# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 if (pty_master_fd >= 0)
4883 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 fromshell_fd = pty_master_fd;
4885 toshell_fd = dup(pty_master_fd);
4886 }
4887 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 {
4890 close(fd_toshell[0]);
4891 close(fd_fromshell[1]);
4892 toshell_fd = fd_toshell[1];
4893 fromshell_fd = fd_fromshell[0];
4894 }
4895
4896 /*
4897 * Write to the child if there are typed characters.
4898 * Read from the child if there are characters available.
4899 * Repeat the reading a few times if more characters are
4900 * available. Need to check for typed keys now and then, but
4901 * not too often (delays when no chars are available).
4902 * This loop is quit if no characters can be read from the pty
4903 * (WaitForChar detected special condition), or there are no
4904 * characters available and the child has exited.
4905 * Only check if the child has exited when there is no more
4906 * output. The child may exit before all the output has
4907 * been printed.
4908 *
4909 * Currently this busy loops!
4910 * This can probably dead-lock when the write blocks!
4911 */
4912 p_more_save = p_more;
4913 p_more = FALSE;
4914 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004915 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004917 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004918 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004919 // Fork a process that will write the lines to the
4920 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004921 if ((wpid = fork()) == -1)
4922 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004923 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004924 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004925 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004926 {
4927 linenr_T lnum = curbuf->b_op_start.lnum;
4928 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004929 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004930 size_t l;
4931
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004932 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004933 for (;;)
4934 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004935 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004936 if (l == 0)
4937 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004938 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004939 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004940 len = write(toshell_fd, "", (size_t)1);
4941 else
4942 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004943 char_u *s = vim_strchr(lp + written, NL);
4944
Bram Moolenaar89d40322006-08-29 15:30:07 +00004945 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004946 s == NULL ? l
4947 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004948 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004949 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004950 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004951 // Finished a line, add a NL, unless this line
4952 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004953 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004954 || (!curbuf->b_p_bin
4955 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004956 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004957 && (lnum !=
4958 curbuf->b_ml.ml_line_count
4959 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004960 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004961 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004962 ++lnum;
4963 if (lnum > curbuf->b_op_end.lnum)
4964 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004965 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00004966 close(toshell_fd);
4967 toshell_fd = -1;
4968 break;
4969 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004970 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004971 written = 0;
4972 }
4973 else if (len > 0)
4974 written += len;
4975 }
4976 _exit(0);
4977 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004978 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00004979 {
4980 close(toshell_fd);
4981 toshell_fd = -1;
4982 }
4983 }
4984
4985 if (options & SHELL_READ)
4986 ga_init2(&ga, 1, BUFLEN);
4987
4988 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004989# ifdef ELAPSED_FUNC
4990 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004991# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 for (;;)
4993 {
4994 /*
4995 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004996 * if there are any.
4997 * Don't do this if we are expanding wild cards (would eat
4998 * typeahead).
4999 * Don't do this when filtering and terminal is in cooked
5000 * mode, the shell command will handle the I/O. Avoids
5001 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005002 * Don't get characters when the child has already
5003 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005004 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005005 * while (noread_cnt > 4), avoids that ":r !ls" eats
5006 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 */
5008 len = 0;
5009 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005010 && ((options &
5011 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5012 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005013# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005014 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005015# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005016 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005017 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005018 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005020 if (ta_len == 0)
5021 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005022 // Get extra characters when we don't have any.
5023 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005024 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005025# ifdef ELAPSED_FUNC
5026 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005027# endif
5028 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5029 }
5030 if (ta_len > 0 || len > 0)
5031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 /*
5033 * For pipes:
5034 * Check for CTRL-C: send interrupt signal to child.
5035 * Check for CTRL-D: EOF, close pipe to child.
5036 */
5037 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5038 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 /*
5040 * Send SIGINT to the child's group or all
5041 * processes in our group.
5042 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005043 may_send_sigint(ta_buf[ta_len], pid, wpid);
5044
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (pty_master_fd < 0 && toshell_fd >= 0
5046 && ta_buf[ta_len] == Ctrl_D)
5047 {
5048 close(toshell_fd);
5049 toshell_fd = -1;
5050 }
5051 }
5052
Bram Moolenaarf4140482020-02-15 23:06:45 +01005053 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
5055 /*
5056 * For pipes: echo the typed characters.
5057 * For a pty this does not seem to work.
5058 */
5059 if (pty_master_fd < 0)
5060 {
5061 for (i = ta_len; i < ta_len + len; ++i)
5062 {
5063 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5064 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 else if (has_mbyte)
5066 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005067 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
5069 msg_outtrans_len(ta_buf + i, l);
5070 i += l - 1;
5071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 else
5073 msg_outtrans_len(ta_buf + i, 1);
5074 }
5075 windgoto(msg_row, msg_col);
5076 out_flush();
5077 }
5078
5079 ta_len += len;
5080
5081 /*
5082 * Write the characters to the child, unless EOF has
5083 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005084 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005085 * When writing buffer lines, drop the typed
5086 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005088 if (options & SHELL_WRITE)
5089 ta_len = 0;
5090 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
5092 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5093 if (len > 0)
5094 {
5095 ta_len -= len;
5096 mch_memmove(ta_buf, ta_buf + len, ta_len);
5097 }
5098 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
5101
Bram Moolenaardf177f62005-02-22 08:39:57 +00005102 if (got_int)
5103 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005104 // CTRL-C sends a signal to the child, we ignore it
5105 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005106# ifdef HAVE_SETSID
5107 kill(-pid, SIGINT);
5108# else
5109 kill(0, SIGINT);
5110# endif
5111 if (wpid > 0)
5112 kill(wpid, SIGINT);
5113 got_int = FALSE;
5114 }
5115
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 /*
5117 * Check if the child has any characters to be printed.
5118 * Read them and write them to our window. Repeat this as
5119 * long as there is something to do, avoid the 10ms wait
5120 * for mch_inchar(), or sending typeahead characters to
5121 * the external process.
5122 * TODO: This should handle escape sequences, compatible
5123 * to some terminal (vt52?).
5124 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005125 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005126 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005128 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005131 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005133
5134 noread_cnt = 0;
5135 if (options & SHELL_READ)
5136 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005137 // Do NUL -> NL translation, append NL separated
5138 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005139 for (i = 0; i < len; ++i)
5140 {
5141 if (buffer[i] == NL)
5142 append_ga_line(&ga);
5143 else if (buffer[i] == NUL)
5144 ga_append(&ga, NL);
5145 else
5146 ga_append(&ga, buffer[i]);
5147 }
5148 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005149 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
5151 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005152 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153
Bram Moolenaardf177f62005-02-22 08:39:57 +00005154 len += buffer_off;
5155 buffer[len] = NUL;
5156
Bram Moolenaar0f873732019-12-05 20:28:46 +01005157 // Check if the last character in buffer[] is
5158 // incomplete, keep these bytes for the next
5159 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 for (p = buffer; p < buffer + len; p += l)
5161 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005162 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005164 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 else if (MB_BYTE2LEN(*p) != l)
5166 break;
5167 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005168 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005170 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if (len >= 12)
5172 ++p;
5173 else
5174 {
5175 buffer_off = len;
5176 continue;
5177 }
5178 }
5179 c = *p;
5180 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005181 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 if (p < buffer + len)
5183 {
5184 *p = c;
5185 buffer_off = (buffer + len) - p;
5186 mch_memmove(buffer, p, buffer_off);
5187 continue;
5188 }
5189 buffer_off = 0;
5190 }
5191 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 {
5193 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005194 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
5196
5197 windgoto(msg_row, msg_col);
5198 cursor_on();
5199 out_flush();
5200 if (got_int)
5201 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005202
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005203# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005204 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005205 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005206 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005207
Bram Moolenaar0f873732019-12-05 20:28:46 +01005208 // Avoid that we keep looping here without
5209 // checking for a CTRL-C for a long time. Don't
5210 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005211 if (msec > 2000)
5212 {
5213 noread_cnt = 5;
5214 break;
5215 }
5216 }
5217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219
Bram Moolenaar0f873732019-12-05 20:28:46 +01005220 // If we already detected the child has finished, continue
5221 // reading output for a short while. Some text may be
5222 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005223 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005224 {
5225 if (noread_cnt < 5)
5226 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005227 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005228 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005229
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 /*
5231 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005232 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005234# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005235 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005236# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005238# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5240 || (wait_pid == pid && WIFEXITED(status)))
5241 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005242 // Don't break the loop yet, try reading more
5243 // characters from "fromshell_fd" first. When using
5244 // pipes there might still be something to read and
5245 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005248 else
5249 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005250
Bram Moolenaar95a51352013-03-21 22:53:50 +01005251# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005252 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005253 clip_update();
5254# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 }
5256finished:
5257 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005258 if (options & SHELL_READ)
5259 {
5260 if (ga.ga_len > 0)
5261 {
5262 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005263 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005264 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005265 }
5266 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005267 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005268 ga_clear(&ga);
5269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 /*
5272 * Give all typeahead that wasn't used back to ui_inchar().
5273 */
5274 if (ta_len)
5275 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 State = old_State;
5277 if (toshell_fd >= 0)
5278 close(toshell_fd);
5279 close(fromshell_fd);
5280 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005281# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005282 else
5283 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005284 long delay_msec = 1;
5285
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005286 if (tmode == TMODE_RAW)
5287 // possibly disables modifyOtherKeys, so that the system
5288 // can recognize CTRL-C
5289 out_str(T_CTE);
Bram Moolenaar0981c872020-08-23 14:28:37 +02005290
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005291 /*
5292 * Similar to the loop above, but only handle X events, no
5293 * I/O.
5294 */
5295 for (;;)
5296 {
5297 if (got_int)
5298 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005299 // CTRL-C sends a signal to the child, we ignore it
5300 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005301# ifdef HAVE_SETSID
5302 kill(-pid, SIGINT);
5303# else
5304 kill(0, SIGINT);
5305# endif
5306 got_int = FALSE;
5307 }
5308# ifdef __NeXT__
5309 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5310# else
5311 wait_pid = waitpid(pid, &status, WNOHANG);
5312# endif
5313 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5314 || (wait_pid == pid && WIFEXITED(status)))
5315 {
5316 wait_pid = pid;
5317 break;
5318 }
5319
Bram Moolenaar0f873732019-12-05 20:28:46 +01005320 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005321 clip_update();
5322
Bram Moolenaar0f873732019-12-05 20:28:46 +01005323 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005324 // less time, gradually wait longer.
5325 mch_delay(delay_msec,
5326 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005327 if (++delay_msec > 10)
5328 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005329 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005330
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005331 if (tmode == TMODE_RAW)
5332 // possibly enables modifyOtherKeys again
5333 out_str(T_CTI);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005334 }
5335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
5337 /*
5338 * Wait until our child has exited.
5339 * Ignore wait() returning pids of other children and returning
5340 * because of some signal like SIGWINCH.
5341 * Don't wait if wait_pid was already set above, indicating the
5342 * child already exited.
5343 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005344 if (wait_pid != pid)
5345 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346
Bram Moolenaar624891f2010-10-13 16:22:09 +02005347# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005348 // Close slave side of pty. Only do this after the child has
5349 // exited, otherwise the child may hang when it tries to write on
5350 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005351 if (pty_master_fd >= 0)
5352 close(pty_slave_fd);
5353# endif
5354
Bram Moolenaar0f873732019-12-05 20:28:46 +01005355 // Make sure the child that writes to the external program is
5356 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005357 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005358 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005359 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005360 wait4pid(wpid, NULL);
5361 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005362
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005363# ifdef FEAT_JOB_CHANNEL
5364 --dont_check_job_ended;
5365# endif
5366
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 /*
5368 * Set to raw mode right now, otherwise a CTRL-C after
5369 * catch_signals() will kill Vim.
5370 */
5371 if (tmode == TMODE_RAW)
5372 settmode(TMODE_RAW);
5373 did_settmode = TRUE;
5374 set_signals();
5375
5376 if (WIFEXITED(status))
5377 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005378 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005380 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
5382 if (retval == EXEC_FAILED)
5383 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005384 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 msg_outtrans(p_sh);
5386 msg_putchar('\n');
5387 }
5388 else if (!(options & SHELL_SILENT))
5389 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005390 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 msg_outnum((long)retval);
5392 msg_putchar('\n');
5393 }
5394 }
5395 }
5396 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005397 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 }
5399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
5401error:
5402 if (!did_settmode)
5403 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005404 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405# ifdef FEAT_TITLE
5406 resettitle();
5407# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005408 vim_free(argv);
5409 vim_free(tofree1);
5410 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
5412 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005413}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005414#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005415
5416 int
5417mch_call_shell(
5418 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005419 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005420{
5421#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5422 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5423 return mch_call_shell_terminal(cmd, options);
5424#endif
5425#ifdef USE_SYSTEM
5426 return mch_call_shell_system(cmd, options);
5427#else
5428 return mch_call_shell_fork(cmd, options);
5429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430}
5431
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005432#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005433 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005434mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005435{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005436 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005437 int fd_in[2] = {-1, -1}; // for stdin
5438 int fd_out[2] = {-1, -1}; // for stdout
5439 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005440 int pty_master_fd = -1;
5441 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005442 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005443 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5444 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5445 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005446 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005447 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5448 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005449 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005450 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005451 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005452
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005453 if (use_out_for_err && use_null_for_out)
5454 use_null_for_err = TRUE;
5455
Bram Moolenaar0f873732019-12-05 20:28:46 +01005456 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005457 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005458
Bram Moolenaarb2412082017-08-20 18:09:14 +02005459 if (options->jo_pty
5460 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005461 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005462 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005463 open_pty(&pty_master_fd, &pty_slave_fd,
5464 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005465
Bram Moolenaar0f873732019-12-05 20:28:46 +01005466 // TODO: without the channel feature connect the child to /dev/null?
5467 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005468 if (use_file_for_in)
5469 {
5470 char_u *fname = options->jo_io_name[PART_IN];
5471
5472 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5473 if (fd_in[0] < 0)
5474 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005475 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005476 goto failed;
5477 }
5478 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005479 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005480 // When writing buffer lines to the input don't use the pty, so that
5481 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005482 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5483 && pipe(fd_in) < 0)
5484 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005485
5486 if (use_file_for_out)
5487 {
5488 char_u *fname = options->jo_io_name[PART_OUT];
5489
5490 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5491 if (fd_out[1] < 0)
5492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005493 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005494 goto failed;
5495 }
5496 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005497 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005498 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005499
5500 if (use_file_for_err)
5501 {
5502 char_u *fname = options->jo_io_name[PART_ERR];
5503
5504 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5505 if (fd_err[1] < 0)
5506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005507 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005508 goto failed;
5509 }
5510 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005511 else if (!use_out_for_err && !use_null_for_err
5512 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005513 goto failed;
5514
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005515 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5516 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005517 if (options->jo_set & JO_CHANNEL)
5518 {
5519 channel = options->jo_channel;
5520 if (channel != NULL)
5521 ++channel->ch_refcount;
5522 }
5523 else
5524 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005525 if (channel == NULL)
5526 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005527 if (job->jv_tty_out != NULL)
5528 ch_log(channel, "using pty %s on fd %d",
5529 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005530 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005531
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005532 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005533 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005534 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005535 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005536 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005537 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005538 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005539 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005540 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005541 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005542 int null_fd = -1;
5543 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005544
Bram Moolenaar0f873732019-12-05 20:28:46 +01005545 // child
5546 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005547 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005548
Bram Moolenaar819524702018-02-27 19:10:00 +01005549# ifdef FEAT_JOB_CHANNEL
5550 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005551 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005552 ch_logfile((char_u *)"", (char_u *)"");
5553# endif
5554
Bram Moolenaar835dc632016-02-07 14:27:38 +01005555# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005556 // Create our own process group, so that the child and all its
5557 // children can be kill()ed. Don't do this when using pipes,
5558 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005559 (void)setsid();
5560# endif
5561
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005562# ifdef FEAT_TERMINAL
5563 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005564 {
5565 char *term = (char *)T_NAME;
5566
5567#ifdef FEAT_GUI
5568 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005569 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005570 term = getenv("TERM");
5571#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005572 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005573 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005574 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005575 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005576 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005577 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005578 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005579 else if (t_colors > 16)
5580 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005581 else
5582 term = "xterm";
5583 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005584 set_child_environment(
5585 (long)options->jo_term_rows,
5586 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005587 term,
5588 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005589 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005590 else
5591# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005592 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005593
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005594 if (options->jo_env != NULL)
5595 {
5596 dict_T *dict = options->jo_env;
5597 hashitem_T *hi;
5598 int todo = (int)dict->dv_hashtab.ht_used;
5599
5600 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5601 if (!HASHITEM_EMPTY(hi))
5602 {
5603 typval_T *item = &dict_lookup(hi)->di_tv;
5604
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005605 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005606 --todo;
5607 }
5608 }
5609
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005610 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005611 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005612 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005613 if (null_fd < 0)
5614 {
5615 perror("opening /dev/null failed");
5616 _exit(OPEN_NULL_FAILED);
5617 }
5618 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005619
Bram Moolenaar223896d2017-08-02 22:33:28 +02005620 if (pty_slave_fd >= 0)
5621 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005622 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005623 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005624# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005625 // Try to become controlling tty (probably doesn't work,
5626 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005627 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5628# endif
5629 }
5630
Bram Moolenaar0f873732019-12-05 20:28:46 +01005631 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005632 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005633 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005634 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005635 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005636 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005637 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005638 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005639
Bram Moolenaar0f873732019-12-05 20:28:46 +01005640 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005641 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005642 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005643 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005644 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005645 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005646 }
5647 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005648 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005649 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005650 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005651 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005652 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005653
Bram Moolenaar0f873732019-12-05 20:28:46 +01005654 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005655 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005656 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005657 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005658 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005659 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005660 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005661 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005662
5663 if (fd_in[0] >= 0)
5664 close(fd_in[0]);
5665 if (fd_in[1] >= 0)
5666 close(fd_in[1]);
5667 if (fd_out[0] >= 0)
5668 close(fd_out[0]);
5669 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005670 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005671 if (fd_err[0] >= 0)
5672 close(fd_err[0]);
5673 if (fd_err[1] >= 0)
5674 close(fd_err[1]);
5675 if (pty_master_fd >= 0)
5676 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005677 close(pty_master_fd); // not used in the child
5678 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005679 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005680
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005681 if (null_fd >= 0)
5682 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005683
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005684 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5685 _exit(EXEC_FAILED);
5686
Bram Moolenaar0f873732019-12-05 20:28:46 +01005687 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005688 execvp(argv[0], argv);
5689
Bram Moolenaar4694a172016-04-21 14:05:23 +02005690 if (stderr_works)
5691 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005692# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005693 // calling free_all_mem() here causes problems. Ignore valgrind
5694 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005695# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005696 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005697 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005698
Bram Moolenaar0f873732019-12-05 20:28:46 +01005699 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005700 UNBLOCK_SIGNALS(&curset);
5701
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005702 job->jv_pid = pid;
5703 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005704 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005705
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005706 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005707 close(pty_slave_fd); // not used in the parent
5708 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005709 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005710 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005711 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005712 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005713 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005714 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005715 if (channel != NULL)
5716 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005717 int in_fd = INVALID_FD;
5718 int out_fd = INVALID_FD;
5719 int err_fd = INVALID_FD;
5720
5721 if (!(use_file_for_in || use_null_for_in))
5722 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5723
5724 if (!(use_file_for_out || use_null_for_out))
5725 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5726
5727 // When using pty_master_fd only set it for stdout, do not duplicate
5728 // it for stderr, it only needs to be read once.
5729 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5730 {
5731 if (fd_err[0] >= 0)
5732 err_fd = fd_err[0];
5733 else if (out_fd != pty_master_fd)
5734 err_fd = pty_master_fd;
5735 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005736
5737 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005738 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005739 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005740 else
5741 {
5742 if (fd_in[1] >= 0)
5743 close(fd_in[1]);
5744 if (fd_out[0] >= 0)
5745 close(fd_out[0]);
5746 if (fd_err[0] >= 0)
5747 close(fd_err[0]);
5748 if (pty_master_fd >= 0)
5749 close(pty_master_fd);
5750 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005751
Bram Moolenaar0f873732019-12-05 20:28:46 +01005752 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005753 return;
5754
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005755failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005756 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005757 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005758 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005759 if (fd_in[1] >= 0)
5760 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005761 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005762 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005763 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005764 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005765 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005766 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005767 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005768 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005769 if (pty_master_fd >= 0)
5770 close(pty_master_fd);
5771 if (pty_slave_fd >= 0)
5772 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005773}
5774
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005775 static char_u *
5776get_signal_name(int sig)
5777{
5778 int i;
5779 char_u numbuf[NUMBUFLEN];
5780
5781 if (sig == SIGKILL)
5782 return vim_strsave((char_u *)"kill");
5783
5784 for (i = 0; signal_info[i].sig != -1; i++)
5785 if (sig == signal_info[i].sig)
5786 return strlow_save((char_u *)signal_info[i].name);
5787
5788 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5789 return vim_strsave(numbuf);
5790}
5791
Bram Moolenaar835dc632016-02-07 14:27:38 +01005792 char *
5793mch_job_status(job_T *job)
5794{
5795# ifdef HAVE_UNION_WAIT
5796 union wait status;
5797# else
5798 int status = -1;
5799# endif
5800 pid_t wait_pid = 0;
5801
5802# ifdef __NeXT__
5803 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5804# else
5805 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5806# endif
5807 if (wait_pid == -1)
5808 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005809 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005810 if (job->jv_status < JOB_ENDED)
5811 ch_log(job->jv_channel, "Job no longer exists: %s",
5812 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005813 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005814 }
5815 if (wait_pid == 0)
5816 return "run";
5817 if (WIFEXITED(status))
5818 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005819 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005820 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005821 if (job->jv_status < JOB_ENDED)
5822 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005823 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005824 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005825 if (WIFSIGNALED(status))
5826 {
5827 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005828 job->jv_termsig = get_signal_name(WTERMSIG(status));
5829 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5830 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5831 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005832 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005833 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005834 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005835
5836return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005837 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005838 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005839 return "dead";
5840}
5841
5842 job_T *
5843mch_detect_ended_job(job_T *job_list)
5844{
5845# ifdef HAVE_UNION_WAIT
5846 union wait status;
5847# else
5848 int status = -1;
5849# endif
5850 pid_t wait_pid = 0;
5851 job_T *job;
5852
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005853# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005854 // Do not do this when waiting for a shell command to finish, we would get
5855 // the exit value here (and discard it), the exit value obtained there
5856 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005857 if (dont_check_job_ended > 0)
5858 return NULL;
5859# endif
5860
Bram Moolenaar97792de2016-10-15 18:36:49 +02005861# ifdef __NeXT__
5862 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5863# else
5864 wait_pid = waitpid(-1, &status, WNOHANG);
5865# endif
5866 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005867 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005868 return NULL;
5869 for (job = job_list; job != NULL; job = job->jv_next)
5870 {
5871 if (job->jv_pid == wait_pid)
5872 {
5873 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005874 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005875 job->jv_exitval = WEXITSTATUS(status);
5876 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005877 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005878 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005879 job->jv_termsig = get_signal_name(WTERMSIG(status));
5880 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005881 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005882 {
5883 ch_log(job->jv_channel, "Job ended");
5884 job->jv_status = JOB_ENDED;
5885 }
5886 return job;
5887 }
5888 }
5889 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005890}
5891
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005892/*
5893 * Send a (deadly) signal to "job".
5894 * Return FAIL if "how" is not a valid name.
5895 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005896 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005897mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005898{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005899 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005900
Bram Moolenaar923d9262016-02-25 20:56:01 +01005901 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005902 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005903 else if (STRCMP(how, "hup") == 0)
5904 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005905 else if (STRCMP(how, "quit") == 0)
5906 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005907 else if (STRCMP(how, "int") == 0)
5908 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005909 else if (STRCMP(how, "kill") == 0)
5910 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005911#ifdef SIGWINCH
5912 else if (STRCMP(how, "winch") == 0)
5913 sig = SIGWINCH;
5914#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005915 else if (isdigit(*how))
5916 sig = atoi((char *)how);
5917 else
5918 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005919
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005920 // Never kill ourselves!
5921 if (job->jv_pid != 0)
5922 {
5923 // TODO: have an option to only kill the process, not the group?
5924 kill(-job->jv_pid, sig);
5925 kill(job->jv_pid, sig);
5926 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005927
Bram Moolenaar835dc632016-02-07 14:27:38 +01005928 return OK;
5929}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005930
5931/*
5932 * Clear the data related to "job".
5933 */
5934 void
5935mch_clear_job(job_T *job)
5936{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005937 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005938# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005939 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005940# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005941 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005942# endif
5943}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005944#endif
5945
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005946#if defined(FEAT_TERMINAL) || defined(PROTO)
5947 int
5948mch_create_pty_channel(job_T *job, jobopt_T *options)
5949{
5950 int pty_master_fd = -1;
5951 int pty_slave_fd = -1;
5952 channel_T *channel;
5953
Bram Moolenaar59386482019-02-10 22:43:46 +01005954 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02005955 if (pty_master_fd < 0 || pty_slave_fd < 0)
5956 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005957 close(pty_slave_fd);
5958
5959 channel = add_channel();
5960 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005961 {
5962 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005963 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005964 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005965 if (job->jv_tty_out != NULL)
5966 ch_log(channel, "using pty %s on fd %d",
5967 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005968 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005969 channel->ch_keep_open = TRUE;
5970
Bram Moolenaar0f873732019-12-05 20:28:46 +01005971 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5972 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005973 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005974 channel_set_job(channel, job, options);
5975 return OK;
5976}
5977#endif
5978
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979/*
5980 * Check for CTRL-C typed by reading all available characters.
5981 * In cooked mode we should get SIGINT, no need to check.
5982 */
5983 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005984mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985{
Bram Moolenaar26e86442020-05-17 14:06:16 +02005986 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005987 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 fill_input_buf(FALSE);
5989}
5990
5991/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005992 * Wait "msec" msec until a character is available from the mouse, keyboard,
5993 * from inbuf[].
5994 * "msec" == -1 will block forever.
5995 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005996 * When "ignore_input" is TRUE even check for pending input when input is
5997 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005998 * "interrupted" (if not NULL) is set to TRUE when no character is available
5999 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006000 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006001 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 */
6003 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006004WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006006#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006007 return ui_wait_for_chars_or_timer(
6008 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006009#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006010 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006011#endif
6012}
6013
6014/*
6015 * Wait "msec" msec until a character is available from the mouse or keyboard
6016 * or from inbuf[].
6017 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006018 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006019 * "interrupted" (if not NULL) is set to TRUE when no character is available
6020 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006021 * When a GUI is being used, this will never get called -- webb
6022 */
6023 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006024WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006025{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026#ifdef FEAT_MOUSE_GPM
6027 int gpm_process_wanted;
6028#endif
6029#ifdef FEAT_XCLIPBOARD
6030 int rest;
6031#endif
6032 int avail;
6033
Bram Moolenaar0f873732019-12-05 20:28:46 +01006034 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 return 1;
6036
6037#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006038 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 if (WantQueryMouse)
6040 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006041 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006042 if (!no_query_mouse_for_testing)
6043 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 }
6045#endif
6046
6047 /*
6048 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6049 * events. This is a bit complicated, because they might both be defined.
6050 */
6051#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6052# ifdef FEAT_XCLIPBOARD
6053 rest = 0;
6054 if (do_xterm_trace())
6055 rest = msec;
6056# endif
6057 do
6058 {
6059# ifdef FEAT_XCLIPBOARD
6060 if (rest != 0)
6061 {
6062 msec = XT_TRACE_DELAY;
6063 if (rest >= 0 && rest < XT_TRACE_DELAY)
6064 msec = rest;
6065 if (rest >= 0)
6066 rest -= msec;
6067 }
6068# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006069# ifdef FEAT_SOUND_CANBERRA
6070 // Invoke any pending sound callbacks.
6071 if (has_sound_callback_in_queue())
6072 invoke_sound_callback();
6073# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074# ifdef FEAT_MOUSE_GPM
6075 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006076 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006077 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006078 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006080 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006082# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006084 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 return 1;
6086# ifdef FEAT_XCLIPBOARD
6087 if (rest == 0 || !do_xterm_trace())
6088# endif
6089 break;
6090 }
6091 }
6092 while (FALSE
6093# ifdef FEAT_MOUSE_GPM
6094 || (gpm_process_wanted && mch_gpm_process() == 0)
6095# endif
6096# ifdef FEAT_XCLIPBOARD
6097 || (!avail && rest != 0)
6098# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006099 )
6100 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101
6102#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006103 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104#endif
6105 return avail;
6106}
6107
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006108#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109/*
6110 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006111 * "msec" == 0 will check for characters once.
6112 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006115 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006116 * "interrupted" (if not NULL) is set to TRUE when no character is available
6117 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006119 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006120RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006123 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006124#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 static int busy = FALSE;
6126
Bram Moolenaar0f873732019-12-05 20:28:46 +01006127 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128# define MAY_LOOP
6129
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006130# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006131 // Remember at what time we started, so that we know how much longer we
6132 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006133 long start_msec = msec;
6134 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006136 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006137 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138# endif
6139
Bram Moolenaar0f873732019-12-05 20:28:46 +01006140 // Handle being called recursively. This may happen for the session
6141 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 if (busy)
6143 return 0;
6144#endif
6145
6146#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006147 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148#endif
6149 {
6150#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006151 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006152# ifdef FEAT_MZSCHEME
6153 int mzquantum_used = FALSE;
6154# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155#endif
6156#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006157 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006158 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 int nfd;
6160# ifdef FEAT_XCLIPBOARD
6161 int xterm_idx = -1;
6162# endif
6163# ifdef FEAT_MOUSE_GPM
6164 int gpm_idx = -1;
6165# endif
6166# ifdef USE_XSMP
6167 int xsmp_idx = -1;
6168# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006169 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006171# ifdef FEAT_MZSCHEME
6172 mzvim_check_threads();
6173 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6174 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006175 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006176 mzquantum_used = TRUE;
6177 }
6178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 fds[0].fd = fd;
6180 fds[0].events = POLLIN;
6181 nfd = 1;
6182
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006184 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (xterm_Shell != (Widget)0)
6186 {
6187 xterm_idx = nfd;
6188 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6189 fds[nfd].events = POLLIN;
6190 nfd++;
6191 }
6192# endif
6193# ifdef FEAT_MOUSE_GPM
6194 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6195 {
6196 gpm_idx = nfd;
6197 fds[nfd].fd = gpm_fd;
6198 fds[nfd].events = POLLIN;
6199 nfd++;
6200 }
6201# endif
6202# ifdef USE_XSMP
6203 if (xsmp_icefd != -1)
6204 {
6205 xsmp_idx = nfd;
6206 fds[nfd].fd = xsmp_icefd;
6207 fds[nfd].events = POLLIN;
6208 nfd++;
6209 }
6210# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006211#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006212 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006213#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006214 if (interrupted != NULL)
6215 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006217 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006218
6219 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006220 if (result == 0 && interrupted != NULL && ret > 0)
6221 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006222
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006223# ifdef FEAT_MZSCHEME
6224 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006225 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006226 finished = FALSE;
6227# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229# ifdef FEAT_XCLIPBOARD
6230 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6231 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006232 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006234 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 finished = FALSE;
6236 }
6237# endif
6238# ifdef FEAT_MOUSE_GPM
6239 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241# endif
6242# ifdef USE_XSMP
6243 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6244 {
6245 if (fds[xsmp_idx].revents & POLLIN)
6246 {
6247 busy = TRUE;
6248 xsmp_handle_requests();
6249 busy = FALSE;
6250 }
6251 else if (fds[xsmp_idx].revents & POLLHUP)
6252 {
6253 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006254 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 xsmp_close();
6256 }
6257 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006258 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 }
6260# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006261#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006262 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006263 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006264 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
Bram Moolenaar0f873732019-12-05 20:28:46 +01006267#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268
6269 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006270 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006271 // These are static because they can take 8 Kbyte each and cause the
6272 // signal stack to run out with -O3.
6273 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006275 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006277# ifdef FEAT_MZSCHEME
6278 mzvim_check_threads();
6279 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6280 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006281 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006282 mzquantum_used = TRUE;
6283 }
6284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006286 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006288 tv.tv_sec = towait / 1000;
6289 tv.tv_usec = (towait % 1000) * (1000000/1000);
6290 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006292 else
6293 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
6295 /*
6296 * Select on ready for reading and exceptional condition (end of file).
6297 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006298select_eintr:
6299 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006300 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 FD_ZERO(&efds);
6302 FD_SET(fd, &rfds);
6303# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006304 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 FD_SET(fd, &efds);
6306# endif
6307 maxfd = fd;
6308
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006310 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 if (xterm_Shell != (Widget)0)
6312 {
6313 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6314 if (maxfd < ConnectionNumber(xterm_dpy))
6315 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006316
Bram Moolenaar0f873732019-12-05 20:28:46 +01006317 // An event may have already been read but not handled. In
6318 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006319 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 }
6321# endif
6322# ifdef FEAT_MOUSE_GPM
6323 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6324 {
6325 FD_SET(gpm_fd, &rfds);
6326 FD_SET(gpm_fd, &efds);
6327 if (maxfd < gpm_fd)
6328 maxfd = gpm_fd;
6329 }
6330# endif
6331# ifdef USE_XSMP
6332 if (xsmp_icefd != -1)
6333 {
6334 FD_SET(xsmp_icefd, &rfds);
6335 FD_SET(xsmp_icefd, &efds);
6336 if (maxfd < xsmp_icefd)
6337 maxfd = xsmp_icefd;
6338 }
6339# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006340# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006341 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006342# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006343 if (interrupted != NULL)
6344 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345
Bram Moolenaar643b6142018-09-12 20:29:09 +02006346 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6347 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006348 result = ret > 0 && FD_ISSET(fd, &rfds);
6349 if (result)
6350 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006351 else if (interrupted != NULL && ret > 0)
6352 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006353
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006354# ifdef EINTR
6355 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006356 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006357 // Check whether window has been resized, EINTR may be caused by
6358 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006359 if (do_resize)
6360 handle_resize();
6361
Bram Moolenaar0f873732019-12-05 20:28:46 +01006362 // Interrupted by a signal, need to try again. We ignore msec
6363 // here, because we do want to check even after a timeout if
6364 // characters are available. Needed for reading output of an
6365 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006366 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006367 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006368# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006369# ifdef __TANDEM
6370 if (ret == -1 && errno == ENOTSUP)
6371 {
6372 FD_ZERO(&rfds);
6373 FD_ZERO(&efds);
6374 ret = 0;
6375 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006376# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006377# ifdef FEAT_MZSCHEME
6378 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006379 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006380 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381# endif
6382
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383# ifdef FEAT_XCLIPBOARD
6384 if (ret > 0 && xterm_Shell != (Widget)0
6385 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6386 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006387 xterm_update(); // Maybe we should hand out clipboard
6388 // continue looping when we only got the X event and the input
6389 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (--ret == 0 && !input_available())
6391 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006392 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 finished = FALSE;
6394 }
6395 }
6396# endif
6397# ifdef FEAT_MOUSE_GPM
6398 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6399 {
6400 if (FD_ISSET(gpm_fd, &efds))
6401 gpm_close();
6402 else if (FD_ISSET(gpm_fd, &rfds))
6403 *check_for_gpm = 1;
6404 }
6405# endif
6406# ifdef USE_XSMP
6407 if (ret > 0 && xsmp_icefd != -1)
6408 {
6409 if (FD_ISSET(xsmp_icefd, &efds))
6410 {
6411 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006412 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 xsmp_close();
6414 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006415 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 }
6417 else if (FD_ISSET(xsmp_icefd, &rfds))
6418 {
6419 busy = TRUE;
6420 xsmp_handle_requests();
6421 busy = FALSE;
6422 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006423 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 }
6425 }
6426# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006427#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006428 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006429 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006430 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432
Bram Moolenaar0f873732019-12-05 20:28:46 +01006433#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434
6435#ifdef MAY_LOOP
6436 if (finished || msec == 0)
6437 break;
6438
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006439# ifdef FEAT_CLIENTSERVER
6440 if (server_waiting())
6441 break;
6442# endif
6443
Bram Moolenaar0f873732019-12-05 20:28:46 +01006444 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 if (msec > 0)
6446 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006447# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006448 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006449 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006451 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 msec = msec / 2;
6453# endif
6454 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006455 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 }
6457#endif
6458 }
6459
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006460 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461}
6462
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006464 * Expand a path into all matching files and/or directories. Handles "*",
6465 * "?", "[a-z]", "**", etc.
6466 * "path" has backslashes before chars that are not to be expanded.
6467 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 */
6469 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006470mch_expandpath(
6471 garray_T *gap,
6472 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006473 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474{
Bram Moolenaar02743632005-07-25 20:42:36 +00006475 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477
6478/*
6479 * mch_expand_wildcards() - this code does wild-card pattern matching using
6480 * the shell
6481 *
6482 * return OK for success, FAIL for error (you may lose some memory) and put
6483 * an error message in *file.
6484 *
6485 * num_pat is number of input patterns
6486 * pat is array of pointers to input patterns
6487 * num_file is pointer to number of matched file names
6488 * file is pointer to array of pointers to matched file names
6489 */
6490
6491#ifndef SEEK_SET
6492# define SEEK_SET 0
6493#endif
6494#ifndef SEEK_END
6495# define SEEK_END 2
6496#endif
6497
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006498#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006499
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006501mch_expand_wildcards(
6502 int num_pat,
6503 char_u **pat,
6504 int *num_file,
6505 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006506 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
6508 int i;
6509 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006510 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 char_u *p;
6512 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513
Bram Moolenaarc7247912008-01-13 12:54:11 +00006514 /*
6515 * This is the non-OS/2 implementation (really Unix).
6516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 int j;
6518 char_u *tempname;
6519 char_u *command;
6520 FILE *fd;
6521 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006522#define STYLE_ECHO 0 // use "echo", the default
6523#define STYLE_GLOB 1 // use "glob", for csh
6524#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6525#define STYLE_PRINT 3 // use "print -N", for zsh
6526#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6527 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 int shell_style = STYLE_ECHO;
6529 int check_spaces;
6530 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006531 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006532 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006533 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534
Bram Moolenaar0f873732019-12-05 20:28:46 +01006535 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 *file = NULL;
6537
6538 /*
6539 * If there are no wildcards, just copy the names to allocated memory.
6540 * Saves a lot of time, because we don't have to start a new shell.
6541 */
6542 if (!have_wildcard(num_pat, pat))
6543 return save_patterns(num_pat, pat, num_file, file);
6544
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006545# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006546 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006547 if (sandbox != 0 && check_secure())
6548 return FAIL;
6549# endif
6550
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 /*
6552 * Don't allow the use of backticks in secure and restricted mode.
6553 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006554 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 for (i = 0; i < num_pat; ++i)
6556 if (vim_strchr(pat[i], '`') != NULL
6557 && (check_restricted() || check_secure()))
6558 return FAIL;
6559
6560 /*
6561 * get a name for the temp file
6562 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006563 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006565 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 return FAIL;
6567 }
6568
6569 /*
6570 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006571 * file.
6572 * STYLE_BT: NL separated
6573 * If expanding `cmd` execute it directly.
6574 * STYLE_GLOB: NUL separated
6575 * If we use *csh, "glob" will work better than "echo".
6576 * STYLE_PRINT: NL or NUL separated
6577 * If we use *zsh, "print -N" will work better than "glob".
6578 * STYLE_VIMGLOB: NL separated
6579 * If we use *sh*, we define "vimglob()".
6580 * STYLE_ECHO: space separated.
6581 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 */
6583 if (num_pat == 1 && *pat[0] == '`'
6584 && (len = STRLEN(pat[0])) > 2
6585 && *(pat[0] + len - 1) == '`')
6586 shell_style = STYLE_BT;
6587 else if ((len = STRLEN(p_sh)) >= 3)
6588 {
6589 if (STRCMP(p_sh + len - 3, "csh") == 0)
6590 shell_style = STYLE_GLOB;
6591 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6592 shell_style = STYLE_PRINT;
6593 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006594 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6595 "sh") != NULL)
6596 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597
Bram Moolenaar0f873732019-12-05 20:28:46 +01006598 // Compute the length of the command. We need 2 extra bytes: for the
6599 // optional '&' and for the NUL.
6600 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006602 if (shell_style == STYLE_VIMGLOB)
6603 len += STRLEN(sh_vimglob_func);
6604
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006605 for (i = 0; i < num_pat; ++i)
6606 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006607 // Count the length of the patterns in the same way as they are put in
6608 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006609#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006610 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006611#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006612 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006613 for (j = 0; pat[i][j] != NUL; ++j)
6614 {
6615 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006616 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006617 ++len;
6618 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006619#endif
6620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 command = alloc(len);
6622 if (command == NULL)
6623 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006624 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 vim_free(tempname);
6626 return FAIL;
6627 }
6628
6629 /*
6630 * Build the shell command:
6631 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6632 * recognizes this).
6633 * - Add the shell command to print the expanded names.
6634 * - Add the temp file name.
6635 * - Add the file name patterns.
6636 */
6637 if (shell_style == STYLE_BT)
6638 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006639 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006640 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006641 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006643 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006644 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006646 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006648 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 *p = ' ';
6650 }
6651 STRCAT(command, ">");
6652 }
6653 else
6654 {
6655 if (flags & EW_NOTFOUND)
6656 STRCPY(command, "set nonomatch; ");
6657 else
6658 STRCPY(command, "unset nonomatch; ");
6659 if (shell_style == STYLE_GLOB)
6660 STRCAT(command, "glob >");
6661 else if (shell_style == STYLE_PRINT)
6662 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006663 else if (shell_style == STYLE_VIMGLOB)
6664 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 else
6666 STRCAT(command, "echo >");
6667 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006668
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006670
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 if (shell_style != STYLE_BT)
6672 for (i = 0; i < num_pat; ++i)
6673 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006674 // When using system() always add extra quotes, because the shell
6675 // is started twice. Otherwise put a backslash before special
6676 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677#ifdef USE_SYSTEM
6678 STRCAT(command, " \"");
6679 STRCAT(command, pat[i]);
6680 STRCAT(command, "\"");
6681#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006682 int intick = FALSE;
6683
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 p = command + STRLEN(command);
6685 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006686 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006687 {
6688 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006689 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006690 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6691 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006692 // Remove a backslash, take char literally. But keep
6693 // backslash inside backticks, before a special character
6694 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006695 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006696 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6697 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006698 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006699 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006700 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006701 else if (!intick
6702 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6703 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006704 // Put a backslash before a special character, but not
6705 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6706 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006707 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006708
Bram Moolenaar0f873732019-12-05 20:28:46 +01006709 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006710 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 *p = NUL;
6713#endif
6714 }
6715 if (flags & EW_SILENT)
6716 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006717 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006718 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719
6720 /*
6721 * Using zsh -G: If a pattern has no matches, it is just deleted from
6722 * the argument list, otherwise zsh gives an error message and doesn't
6723 * expand any other pattern.
6724 */
6725 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006726 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727
6728 /*
6729 * If we use -f then shell variables set in .cshrc won't get expanded.
6730 * vi can do it, so we will too, but it is only necessary if there is a "$"
6731 * in one of the patterns, otherwise we can still use the fast option.
6732 */
6733 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006734 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
6736 /*
6737 * execute the shell command
6738 */
6739 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6740
Bram Moolenaar0f873732019-12-05 20:28:46 +01006741 // When running in the background, give it some time to create the temp
6742 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006743 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006744 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745
Bram Moolenaar0f873732019-12-05 20:28:46 +01006746 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 show_shell_mess = TRUE;
6748 vim_free(command);
6749
Bram Moolenaar0f873732019-12-05 20:28:46 +01006750 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 {
6752 mch_remove(tempname);
6753 vim_free(tempname);
6754 /*
6755 * With interactive completion, the error message is not printed.
6756 * However with USE_SYSTEM, I don't know how to turn off error messages
6757 * from the shell, so screen may still get messed up -- webb.
6758 */
6759#ifndef USE_SYSTEM
6760 if (!(flags & EW_SILENT))
6761#endif
6762 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006763 redraw_later_clear(); // probably messed up screen
6764 msg_putchar('\n'); // clear bottom line quickly
6765 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766#ifdef USE_SYSTEM
6767 if (!(flags & EW_SILENT))
6768#endif
6769 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006770 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006771 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 }
6773 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006774 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6775 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 if (shell_style == STYLE_BT)
6777 return FAIL;
6778 goto notfound;
6779 }
6780
6781 /*
6782 * read the names from the file into memory
6783 */
6784 fd = fopen((char *)tempname, READBIN);
6785 if (fd == NULL)
6786 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006787 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 if (!(flags & EW_SILENT))
6789 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006790 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006791 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 }
6793 vim_free(tempname);
6794 goto notfound;
6795 }
6796 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006797 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006799 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006800 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006801 buffer = NULL;
6802 else
6803 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 if (buffer == NULL)
6805 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006806 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 mch_remove(tempname);
6808 vim_free(tempname);
6809 fclose(fd);
6810 return FAIL;
6811 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006812 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 i = fread((char *)buffer, 1, len, fd);
6814 fclose(fd);
6815 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006816 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006818 // unexpected read error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006819 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 vim_free(tempname);
6821 vim_free(buffer);
6822 return FAIL;
6823 }
6824 vim_free(tempname);
6825
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006826# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006827 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006829 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6831 *p++ = buffer[i];
6832 len = p - buffer;
6833# endif
6834
6835
Bram Moolenaar0f873732019-12-05 20:28:46 +01006836 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 if (shell_style == STYLE_ECHO)
6838 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006839 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006841 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 {
6843 while (*p != ' ' && *p != '\n')
6844 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006845 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 }
6847 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006848 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006849 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006851 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006853 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 {
6855 while (*p != '\n' && *p != NUL)
6856 ++p;
6857 if (*p != NUL)
6858 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006859 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 }
6861 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006862 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 else
6864 {
6865 /*
6866 * Some versions of zsh use spaces instead of NULs to separate
6867 * results. Only do this when there is no NUL before the end of the
6868 * buffer, otherwise we would never be able to use file names with
6869 * embedded spaces when zsh does use NULs.
6870 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6871 * don't check for spaces again.
6872 */
6873 check_spaces = FALSE;
6874 if (shell_style == STYLE_PRINT && !did_find_nul)
6875 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006876 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006877 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006878 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 did_find_nul = TRUE;
6880 else
6881 check_spaces = TRUE;
6882 }
6883
6884 /*
6885 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6886 * already is one, for STYLE_GLOB it needs to be added.
6887 */
6888 if (len && buffer[len - 1] == NUL)
6889 --len;
6890 else
6891 buffer[len] = NUL;
6892 i = 0;
6893 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006894 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 {
6896 ++i;
6897 *p = NUL;
6898 }
6899 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006900 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 }
6902 if (i == 0)
6903 {
6904 /*
6905 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6906 * /bin/sh will happily expand it to nothing rather than returning an
6907 * error; and hey, it's good to check anyway -- webb.
6908 */
6909 vim_free(buffer);
6910 goto notfound;
6911 }
6912 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006913 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 if (*file == NULL)
6915 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006916 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 vim_free(buffer);
6918 return FAIL;
6919 }
6920
6921 /*
6922 * Isolate the individual file names.
6923 */
6924 p = buffer;
6925 for (i = 0; i < *num_file; ++i)
6926 {
6927 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006928 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006929 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6930 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006932 while (!(shell_style == STYLE_ECHO && *p == ' ')
6933 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006935 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 *p = NUL;
6937 else
6938 {
6939 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006940 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 }
6942 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006943 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006945 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006947 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 }
6949 }
6950
6951 /*
6952 * Move the file names to allocated memory.
6953 */
6954 for (j = 0, i = 0; i < *num_file; ++i)
6955 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006956 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6958 continue;
6959
Bram Moolenaar0f873732019-12-05 20:28:46 +01006960 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 dir = (mch_isdir((*file)[i]));
6962 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6963 continue;
6964
Bram Moolenaar0f873732019-12-05 20:28:46 +01006965 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01006966 if (!dir && (flags & EW_EXEC)
6967 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006968 continue;
6969
Bram Moolenaar964b3742019-05-24 18:54:09 +02006970 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 if (p)
6972 {
6973 STRCPY(p, (*file)[i]);
6974 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006975 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 (*file)[j++] = p;
6977 }
6978 }
6979 vim_free(buffer);
6980 *num_file = j;
6981
Bram Moolenaar0f873732019-12-05 20:28:46 +01006982 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006984 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 goto notfound;
6986 }
6987
6988 return OK;
6989
6990notfound:
6991 if (flags & EW_NOTFOUND)
6992 return save_patterns(num_pat, pat, num_file, file);
6993 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994}
6995
Bram Moolenaar0f873732019-12-05 20:28:46 +01006996#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006999save_patterns(
7000 int num_pat,
7001 char_u **pat,
7002 int *num_file,
7003 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004{
7005 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007006 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007008 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 if (*file == NULL)
7010 return FAIL;
7011 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007012 {
7013 s = vim_strsave(pat[i]);
7014 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007015 // Be compatible with expand_filename(): halve the number of
7016 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007017 backslash_halve(s);
7018 (*file)[i] = s;
7019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 *num_file = num_pat;
7021 return OK;
7022}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024/*
7025 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7026 * expand.
7027 */
7028 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007029mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007031 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 if (*p == '\\' && p[1] != NUL)
7034 ++p;
7035 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 if (vim_strchr((char_u *)
7037#ifdef VMS
7038 "*?%"
7039#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041#endif
7042 , *p) != NULL)
7043 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 }
7045 return FALSE;
7046}
7047
7048/*
7049 * Return TRUE if the string "p" contains a wildcard.
7050 * Don't recognize '~' at the end as a wildcard.
7051 */
7052 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007053mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007055 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 if (*p == '\\' && p[1] != NUL)
7058 ++p;
7059 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 if (vim_strchr((char_u *)
7061#ifdef VMS
7062 "*?%$"
7063#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065#endif
7066 , *p) != NULL
7067 || (*p == '~' && p[1] != NUL))
7068 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 }
7070 return FALSE;
7071}
7072
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007074have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
7076 int i;
7077
7078 for (i = 0; i < num; i++)
7079 if (mch_has_wildcard(file[i]))
7080 return 1;
7081 return 0;
7082}
7083
7084 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007085have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
7087 int i;
7088
7089 for (i = 0; i < num; i++)
7090 if (vim_strchr(file[i], '$') != NULL)
7091 return TRUE;
7092 return FALSE;
7093}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007095#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096/*
7097 * Scaled-down version of rename(), which is missing in Xenix.
7098 * This version can only move regular files and will fail if the
7099 * destination exists.
7100 */
7101 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007102mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
7104 struct stat st;
7105
Bram Moolenaar0f873732019-12-05 20:28:46 +01007106 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007108 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007110 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 return 0;
7112 return -1;
7113}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007114#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007116#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117/*
7118 * Initializes connection with gpm (if it isn't already opened)
7119 * Return 1 if succeeded (or connection already opened), 0 if failed
7120 */
7121 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007122gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007124 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
7126 if (!gpm_flag)
7127 {
7128 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7129 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007130 // Default handling for mouse move
7131 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 gpm_connect.maxMod = 0xffff;
7133 if (Gpm_Open(&gpm_connect, 0) > 0)
7134 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007135 // gpm library tries to handling TSTP causes
7136 // problems. Anyways, we close connection to Gpm whenever
7137 // we are going to suspend or starting an external process
7138 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007139# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007141# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007142 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 }
7144 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007145 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 return 0;
7147 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007148 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149}
7150
7151/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007152 * Returns TRUE if the GPM mouse is enabled.
7153 */
7154 int
7155gpm_enabled(void)
7156{
7157 return gpm_flag && gpm_fd >= 0;
7158}
7159
7160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 */
7163 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007164gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007166 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 Gpm_Close();
7168}
7169
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007170/*
7171 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007173 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 */
7175 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007176mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
7178 int button;
7179 static Gpm_Event gpm_event;
7180 char_u string[6];
7181 int_u vim_modifiers;
7182 int row,col;
7183 unsigned char buttons_mask;
7184 unsigned char gpm_modifiers;
7185 static unsigned char old_buttons = 0;
7186
7187 Gpm_GetEvent(&gpm_event);
7188
7189#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007190 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 if (hold_gui_events)
7192 return 0;
7193#endif
7194
7195 row = gpm_event.y - 1;
7196 col = gpm_event.x - 1;
7197
Bram Moolenaar0f873732019-12-05 20:28:46 +01007198 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 string[1] = 'M';
7200 string[2] = 'G';
7201 switch (GPM_BARE_EVENTS(gpm_event.type))
7202 {
7203 case GPM_DRAG:
7204 string[3] = MOUSE_DRAG;
7205 break;
7206 case GPM_DOWN:
7207 buttons_mask = gpm_event.buttons & ~old_buttons;
7208 old_buttons = gpm_event.buttons;
7209 switch (buttons_mask)
7210 {
7211 case GPM_B_LEFT:
7212 button = MOUSE_LEFT;
7213 break;
7214 case GPM_B_MIDDLE:
7215 button = MOUSE_MIDDLE;
7216 break;
7217 case GPM_B_RIGHT:
7218 button = MOUSE_RIGHT;
7219 break;
7220 default:
7221 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007222 // Don't know what to do. Can more than one button be
7223 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 }
7225 string[3] = (char_u)(button | 0x20);
7226 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7227 break;
7228 case GPM_UP:
7229 string[3] = MOUSE_RELEASE;
7230 old_buttons &= ~gpm_event.buttons;
7231 break;
7232 default:
7233 return 0;
7234 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007235 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 gpm_modifiers = gpm_event.modifiers;
7237 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007238 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7239 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7240 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7242 vim_modifiers |= MOUSE_SHIFT;
7243
7244 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7245 vim_modifiers |= MOUSE_CTRL;
7246 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7247 vim_modifiers |= MOUSE_ALT;
7248 string[3] |= vim_modifiers;
7249 string[4] = (char_u)(col + ' ' + 1);
7250 string[5] = (char_u)(row + ' ' + 1);
7251 add_to_input_buf(string, 6);
7252 return 6;
7253}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007254#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007256#ifdef FEAT_SYSMOUSE
7257/*
7258 * Initialize connection with sysmouse.
7259 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7260 * output, any sysmouse output than will be processed via sig_sysmouse().
7261 * Return OK if succeeded, FAIL if failed.
7262 */
7263 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007264sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007265{
7266 struct mouse_info mouse;
7267
7268 mouse.operation = MOUSE_MODE;
7269 mouse.u.mode.mode = 0;
7270 mouse.u.mode.signal = SIGUSR2;
7271 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7272 {
7273 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7274 mouse.operation = MOUSE_SHOW;
7275 ioctl(1, CONS_MOUSECTL, &mouse);
7276 return OK;
7277 }
7278 return FAIL;
7279}
7280
7281/*
7282 * Stop processing SIGUSR2 signals, and also make sure that
7283 * virtual console do not send us any sysmouse related signal.
7284 */
7285 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007286sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007287{
7288 struct mouse_info mouse;
7289
7290 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7291 mouse.operation = MOUSE_MODE;
7292 mouse.u.mode.mode = 0;
7293 mouse.u.mode.signal = 0;
7294 ioctl(1, CONS_MOUSECTL, &mouse);
7295}
7296
7297/*
7298 * Gets info from sysmouse and adds special keys to input buf.
7299 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007300 static RETSIGTYPE
7301sig_sysmouse SIGDEFARG(sigarg)
7302{
7303 struct mouse_info mouse;
7304 struct video_info video;
7305 char_u string[6];
7306 int row, col;
7307 int button;
7308 int buttons;
7309 static int oldbuttons = 0;
7310
7311#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007312 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007313 if (hold_gui_events)
7314 return;
7315#endif
7316
7317 mouse.operation = MOUSE_GETINFO;
7318 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7319 && ioctl(1, FBIO_MODEINFO, &video) != -1
7320 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7321 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7322 {
7323 row = mouse.u.data.y / video.vi_cheight;
7324 col = mouse.u.data.x / video.vi_cwidth;
7325 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007326 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007327 string[1] = 'M';
7328 string[2] = 'S';
7329 if (oldbuttons == buttons && buttons != 0)
7330 {
7331 button = MOUSE_DRAG;
7332 }
7333 else
7334 {
7335 switch (buttons)
7336 {
7337 case 0:
7338 button = MOUSE_RELEASE;
7339 break;
7340 case 1:
7341 button = MOUSE_LEFT;
7342 break;
7343 case 2:
7344 button = MOUSE_MIDDLE;
7345 break;
7346 case 4:
7347 button = MOUSE_RIGHT;
7348 break;
7349 default:
7350 return;
7351 }
7352 oldbuttons = buttons;
7353 }
7354 string[3] = (char_u)(button);
7355 string[4] = (char_u)(col + ' ' + 1);
7356 string[5] = (char_u)(row + ' ' + 1);
7357 add_to_input_buf(string, 6);
7358 }
7359 return;
7360}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007361#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007362
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007364typedef char_u * (*STRPROCSTR)(char_u *);
7365typedef char_u * (*INTPROCSTR)(int);
7366typedef int (*STRPROCINT)(char_u *);
7367typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368
7369/*
7370 * Call a DLL routine which takes either a string or int param
7371 * and returns an allocated string.
7372 */
7373 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007374mch_libcall(
7375 char_u *libname,
7376 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007377 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007378 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007379 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007380 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381{
7382# if defined(USE_DLOPEN)
7383 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007384 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385# else
7386 shl_t hinstLib;
7387# endif
7388 STRPROCSTR ProcAdd;
7389 INTPROCSTR ProcAddI;
7390 char_u *retval_str = NULL;
7391 int retval_int = 0;
7392 int success = FALSE;
7393
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007394 /*
7395 * Get a handle to the DLL module.
7396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007398 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007399 (void)dlerror();
7400
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 hinstLib = dlopen((char *)libname, RTLD_LAZY
7402# ifdef RTLD_LOCAL
7403 | RTLD_LOCAL
7404# endif
7405 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007406 if (hinstLib == NULL)
7407 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007408 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007409 dlerr = (char *)dlerror();
7410 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007411 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413# else
7414 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7415# endif
7416
Bram Moolenaar0f873732019-12-05 20:28:46 +01007417 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 if (hinstLib != NULL)
7419 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007420# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 /*
7422 * Catch a crash when calling the library function. For example when
7423 * using a number where a string pointer is expected.
7424 */
7425 mch_startjmp();
7426 if (SETJMP(lc_jump_env) != 0)
7427 {
7428 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007429# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007430 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 mch_didjmp();
7433 }
7434 else
7435# endif
7436 {
7437 retval_str = NULL;
7438 retval_int = 0;
7439
7440 if (argstring != NULL)
7441 {
7442# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007443 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007444 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445# else
7446 if (shl_findsym(&hinstLib, (const char *)funcname,
7447 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7448 ProcAdd = NULL;
7449# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007450 if ((success = (ProcAdd != NULL
7451# if defined(USE_DLOPEN)
7452 && dlerr == NULL
7453# endif
7454 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 {
7456 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007457 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 else
7459 retval_str = (ProcAdd)(argstring);
7460 }
7461 }
7462 else
7463 {
7464# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007465 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007466 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467# else
7468 if (shl_findsym(&hinstLib, (const char *)funcname,
7469 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7470 ProcAddI = NULL;
7471# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007472 if ((success = (ProcAddI != NULL
7473# if defined(USE_DLOPEN)
7474 && dlerr == NULL
7475# endif
7476 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 {
7478 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007479 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 else
7481 retval_str = (ProcAddI)(argint);
7482 }
7483 }
7484
Bram Moolenaar0f873732019-12-05 20:28:46 +01007485 // Save the string before we free the library.
7486 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 if (string_result == NULL)
7488 *number_result = retval_int;
7489 else if (retval_str != NULL
7490 && retval_str != (char_u *)1
7491 && retval_str != (char_u *)-1)
7492 *string_result = vim_strsave(retval_str);
7493 }
7494
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007495# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 mch_endjmp();
7497# ifdef SIGHASARG
7498 if (lc_signal != 0)
7499 {
7500 int i;
7501
Bram Moolenaar0f873732019-12-05 20:28:46 +01007502 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 for (i = 0; signal_info[i].sig != -1; i++)
7504 if (lc_signal == signal_info[i].sig)
7505 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007506 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 }
7508# endif
7509# endif
7510
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007512 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007513 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007514 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007515
Bram Moolenaar0f873732019-12-05 20:28:46 +01007516 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 (void)dlclose(hinstLib);
7518# else
7519 (void)shl_unload(hinstLib);
7520# endif
7521 }
7522
7523 if (!success)
7524 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007525 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 return FAIL;
7527 }
7528
7529 return OK;
7530}
7531#endif
7532
7533#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007534static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535static int xterm_button;
7536
7537/*
7538 * Setup a dummy window for X selections in a terminal.
7539 */
7540 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007541setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542{
7543 int z = 0;
7544 char *strp = "";
7545 Widget AppShell;
7546
7547 if (!x_connect_to_server())
7548 return;
7549
7550 open_app_context();
7551 if (app_context != NULL && xterm_Shell == (Widget)0)
7552 {
7553 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007554# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007556# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007557# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007558 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559
7560 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007561 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562# endif
7563
Bram Moolenaar0f873732019-12-05 20:28:46 +01007564 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 oldhandler = XSetErrorHandler(x_error_check);
7566
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007567# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007568 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7570 mch_startjmp();
7571 if (SETJMP(lc_jump_env) != 0)
7572 {
7573 mch_didjmp();
7574 xterm_dpy = NULL;
7575 }
7576 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007577# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 {
7579 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7580 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007581 if (xterm_dpy != NULL)
7582 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007583# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007585# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 }
7587
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007588# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007589 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007591# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007592 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 (void)XSetErrorHandler(oldhandler);
7594
7595 if (xterm_dpy == NULL)
7596 {
7597 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007598 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 return;
7600 }
7601
Bram Moolenaar0f873732019-12-05 20:28:46 +01007602 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 (void)XSetIOErrorHandler(x_IOerror_handler);
7604
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007605# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007607 {
7608 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007609 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007610 verbose_leave();
7611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612# endif
7613
Bram Moolenaar0f873732019-12-05 20:28:46 +01007614 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7616 applicationShellWidgetClass, xterm_dpy,
7617 NULL);
7618 if (AppShell == (Widget)0)
7619 return;
7620 xterm_Shell = XtVaCreatePopupShell("VIM",
7621 topLevelShellWidgetClass, AppShell,
7622 XtNmappedWhenManaged, 0,
7623 XtNwidth, 1,
7624 XtNheight, 1,
7625 NULL);
7626 if (xterm_Shell == (Widget)0)
7627 return;
7628
7629 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007630 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 if (x11_display == NULL)
7632 x11_display = xterm_dpy;
7633
7634 XtRealizeWidget(xterm_Shell);
7635 XSync(xterm_dpy, False);
7636 xterm_update();
7637 }
7638 if (xterm_Shell != (Widget)0)
7639 {
7640 clip_init(TRUE);
7641 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7642 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007643 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 if (test_x11_window(xterm_dpy) == FAIL)
7645 x11_window = 0;
7646 if (x11_window != 0)
7647 xterm_trace = 0;
7648 }
7649}
7650
7651 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007652start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653{
7654 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7655 return;
7656 xterm_trace = 1;
7657 xterm_button = button;
7658 do_xterm_trace();
7659}
7660
7661
7662 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007663stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664{
7665 if (xterm_trace < 0)
7666 return;
7667 xterm_trace = 0;
7668}
7669
7670/*
7671 * Query the xterm pointer and generate mouse termcodes if necessary
7672 * return TRUE if dragging is active, else FALSE
7673 */
7674 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007675do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676{
7677 Window root, child;
7678 int root_x, root_y;
7679 int win_x, win_y;
7680 int row, col;
7681 int_u mask_return;
7682 char_u buf[50];
7683 char_u *strp;
7684 long got_hints;
7685 static char_u *mouse_code;
7686 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7687 static int prev_row = 0, prev_col = 0;
7688 static XSizeHints xterm_hints;
7689
7690 if (xterm_trace <= 0)
7691 return FALSE;
7692
7693 if (xterm_trace == 1)
7694 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007695 // Get the hints just before tracking starts. The font size might
7696 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007697 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7698 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 || xterm_hints.width_inc <= 1
7700 || xterm_hints.height_inc <= 1)
7701 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007702 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 return FALSE;
7704 }
7705
Bram Moolenaar0f873732019-12-05 20:28:46 +01007706 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 mouse_code = find_termcode(mouse_name);
7708 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007709 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 xterm_trace = 2;
7711
Bram Moolenaar0f873732019-12-05 20:28:46 +01007712 // Find the offset of the chars, there might be a scrollbar on the
7713 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7715 &win_x, &win_y, &mask_return);
7716 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7717 - (xterm_hints.height_inc / 2);
7718 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7719 xterm_hints.y = 2;
7720 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7721 - (xterm_hints.width_inc / 2);
7722 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7723 xterm_hints.x = 2;
7724 return TRUE;
7725 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007726 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 {
7728 xterm_trace = 0;
7729 return FALSE;
7730 }
7731
7732 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7733 &win_x, &win_y, &mask_return);
7734
7735 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7736 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7737 if (row == prev_row && col == prev_col)
7738 return TRUE;
7739
7740 STRCPY(buf, mouse_code);
7741 strp = buf + STRLEN(buf);
7742 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7743 *strp++ = (char_u)(col + ' ' + 1);
7744 *strp++ = (char_u)(row + ' ' + 1);
7745 *strp = 0;
7746 add_to_input_buf(buf, STRLEN(buf));
7747
7748 prev_row = row;
7749 prev_col = col;
7750 return TRUE;
7751}
7752
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007753# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754/*
7755 * Destroy the display, window and app_context. Required for GTK.
7756 */
7757 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007758clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759{
7760 if (xterm_Shell != (Widget)0)
7761 {
7762 XtDestroyWidget(xterm_Shell);
7763 xterm_Shell = (Widget)0;
7764 }
7765 if (xterm_dpy != NULL)
7766 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007767# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007768 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 if (x11_display == xterm_dpy)
7772 x11_display = NULL;
7773 xterm_dpy = NULL;
7774 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007775# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 if (app_context != (XtAppContext)NULL)
7777 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007778 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 XtDestroyApplicationContext(app_context);
7780 app_context = (XtAppContext)NULL;
7781 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783}
7784# endif
7785
7786/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007787 * Catch up with GUI or X events.
7788 */
7789 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007790clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007791{
7792# ifdef FEAT_GUI
7793 if (gui.in_use)
7794 gui_mch_update();
7795 else
7796# endif
7797 if (xterm_Shell != (Widget)0)
7798 xterm_update();
7799}
7800
7801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 * Catch up with any queued X events. This may put keyboard input into the
7803 * input buffer, call resize call-backs, trigger timers etc. If there is
7804 * nothing in the X event queue (& no timers pending), then we return
7805 * immediately.
7806 */
7807 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007808xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809{
7810 XEvent event;
7811
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007812 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007814 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007816 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007817 break;
7818
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007819 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007820 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007821 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007822 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007823#ifdef FEAT_CLIENTSERVER
7824 {
7825 XPropertyEvent *e = (XPropertyEvent *)&event;
7826
7827 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007829 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007832 XtDispatchEvent(&event);
7833 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007834 else
7835 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007836 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007837 XtAppProcessEvent(app_context, mask);
7838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 }
7840}
7841
7842 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007843clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844{
7845 if (xterm_Shell != (Widget)0)
7846 return clip_x11_own_selection(xterm_Shell, cbd);
7847 return FAIL;
7848}
7849
7850 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007851clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
7853 if (xterm_Shell != (Widget)0)
7854 clip_x11_lose_selection(xterm_Shell, cbd);
7855}
7856
7857 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007858clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860 if (xterm_Shell != (Widget)0)
7861 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7862}
7863
7864 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007865clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866{
7867 clip_x11_set_selection(cbd);
7868}
7869#endif
7870
7871
7872#if defined(USE_XSMP) || defined(PROTO)
7873/*
7874 * Code for X Session Management Protocol.
7875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876
7877# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878/*
7879 * This is our chance to ask the user if they want to save,
7880 * or abort the logout
7881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007883xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884{
Bram Moolenaare1004402020-10-24 20:49:43 +02007885 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 int cancel_shutdown = False;
7887
Bram Moolenaare1004402020-10-24 20:49:43 +02007888 save_cmod_flags = cmdmod.cmod_flags;
7889 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007890 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007891 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02007893 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007894 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 out_flush();
7896
Bram Moolenaar0f873732019-12-05 20:28:46 +01007897 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 SmcInteractDone(smc_conn, cancel_shutdown);
7899
Bram Moolenaar0f873732019-12-05 20:28:46 +01007900 // Finish off
7901 // Only end save-yourself here if we're not cancelling shutdown;
7902 // we'll get a cancelled callback later in which we'll end it.
7903 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 if (!cancel_shutdown)
7905 {
7906 xsmp.save_yourself = False;
7907 SmcSaveYourselfDone(smc_conn, True);
7908 }
7909}
7910# endif
7911
7912/*
7913 * Callback that starts save-yourself.
7914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007916xsmp_handle_save_yourself(
7917 SmcConn smc_conn,
7918 SmPointer client_data UNUSED,
7919 int save_type UNUSED,
7920 Bool shutdown,
7921 int interact_style UNUSED,
7922 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007924 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 if (xsmp.save_yourself)
7926 SmcSaveYourselfDone(smc_conn, True);
7927 xsmp.save_yourself = True;
7928 xsmp.shutdown = shutdown;
7929
Bram Moolenaar0f873732019-12-05 20:28:46 +01007930 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007932 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933
7934 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007935 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936
7937# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007938 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007940 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 SmcInteractRequest(smc_conn, SmDialogError,
7942 xsmp_handle_interaction, client_data);
7943 else
7944# endif
7945 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007946 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 SmcSaveYourselfDone(smc_conn, True);
7948 xsmp.save_yourself = False;
7949 }
7950}
7951
7952
7953/*
7954 * Callback to warn us of imminent death.
7955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007957xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958{
7959 xsmp_close();
7960
Bram Moolenaar0f873732019-12-05 20:28:46 +01007961 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 getout_preserve_modified(0);
7963}
7964
7965
7966/*
7967 * Callback to tell us that save-yourself has completed.
7968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007970xsmp_save_complete(
7971 SmcConn smc_conn UNUSED,
7972 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973{
7974 xsmp.save_yourself = False;
7975}
7976
7977
7978/*
7979 * Callback to tell us that an instigated shutdown was cancelled
7980 * (maybe even by us)
7981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007983xsmp_shutdown_cancelled(
7984 SmcConn smc_conn,
7985 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986{
7987 if (xsmp.save_yourself)
7988 SmcSaveYourselfDone(smc_conn, True);
7989 xsmp.save_yourself = False;
7990 xsmp.shutdown = False;
7991}
7992
7993
7994/*
7995 * Callback to tell us that a new ICE connection has been established.
7996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007998xsmp_ice_connection(
7999 IceConn iceConn,
8000 IcePointer clientData UNUSED,
8001 Bool opening,
8002 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008004 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 if (opening)
8006 {
8007 xsmp_icefd = IceConnectionNumber(iceConn);
8008 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8009 }
8010}
8011
8012
Bram Moolenaar0f873732019-12-05 20:28:46 +01008013// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008015xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017 Bool rep;
8018
8019 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8020 == IceProcessMessagesIOError)
8021 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008022 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008024 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 xsmp_close();
8026 return FAIL;
8027 }
8028 else
8029 return OK;
8030}
8031
8032static int dummy;
8033
Bram Moolenaar0f873732019-12-05 20:28:46 +01008034// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 void
8036xsmp_init(void)
8037{
8038 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 SmcCallbacks smcallbacks;
8040#if 0
8041 SmPropValue smname;
8042 SmProp smnameprop;
8043 SmProp *smprops[1];
8044#endif
8045
8046 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008047 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048
8049 xsmp.save_yourself = xsmp.shutdown = False;
8050
Bram Moolenaar0f873732019-12-05 20:28:46 +01008051 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8053 smcallbacks.save_yourself.client_data = NULL;
8054 smcallbacks.die.callback = xsmp_die;
8055 smcallbacks.die.client_data = NULL;
8056 smcallbacks.save_complete.callback = xsmp_save_complete;
8057 smcallbacks.save_complete.client_data = NULL;
8058 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8059 smcallbacks.shutdown_cancelled.client_data = NULL;
8060
Bram Moolenaar0f873732019-12-05 20:28:46 +01008061 // Set up a watch on ICE connection creations. The "dummy" argument is
8062 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8064 {
8065 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008066 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 return;
8068 }
8069
Bram Moolenaar0f873732019-12-05 20:28:46 +01008070 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 xsmp.smcconn = SmcOpenConnection(
8072 NULL,
8073 NULL,
8074 SmProtoMajor,
8075 SmProtoMinor,
8076 SmcSaveYourselfProcMask | SmcDieProcMask
8077 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8078 &smcallbacks,
8079 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008080 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008081 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 errorstring);
8083 if (xsmp.smcconn == NULL)
8084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008086 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008087 char errorreport[132];
8088
8089 // If the message is too long it might not be NUL terminated. Add
8090 // a NUL at the end to make sure we don't go over the end.
8091 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008092 vim_snprintf(errorreport, sizeof(errorreport),
8093 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008094 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 return;
8097 }
8098 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8099
8100#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008101 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 smname.value = "vim";
8103 smname.length = 3;
8104 smnameprop.name = "SmProgram";
8105 smnameprop.type = "SmARRAY8";
8106 smnameprop.num_vals = 1;
8107 smnameprop.vals = &smname;
8108
8109 smprops[0] = &smnameprop;
8110 SmcSetProperties(xsmp.smcconn, 1, smprops);
8111#endif
8112}
8113
8114
Bram Moolenaar0f873732019-12-05 20:28:46 +01008115// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008117xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118{
8119 if (xsmp_icefd != -1)
8120 {
8121 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008122 if (xsmp.clientid != NULL)
8123 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008124 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 xsmp_icefd = -1;
8126 }
8127}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008128#endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129
8130
8131#ifdef EBCDIC
Bram Moolenaar0f873732019-12-05 20:28:46 +01008132// Translate character to its CTRL- value
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133char CtrlTable[] =
8134{
8135/* 00 - 5E */
8136 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8137 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8138 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8139 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8140 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8141 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8142/* ^ */ 0x1E,
8143/* - */ 0x1F,
8144/* 61 - 6C */
8145 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8146/* _ */ 0x1F,
8147/* 6E - 80 */
8148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8149/* a */ 0x01,
8150/* b */ 0x02,
8151/* c */ 0x03,
8152/* d */ 0x37,
8153/* e */ 0x2D,
8154/* f */ 0x2E,
8155/* g */ 0x2F,
8156/* h */ 0x16,
8157/* i */ 0x05,
8158/* 8A - 90 */
8159 0, 0, 0, 0, 0, 0, 0,
8160/* j */ 0x15,
8161/* k */ 0x0B,
8162/* l */ 0x0C,
8163/* m */ 0x0D,
8164/* n */ 0x0E,
8165/* o */ 0x0F,
8166/* p */ 0x10,
8167/* q */ 0x11,
8168/* r */ 0x12,
8169/* 9A - A1 */
8170 0, 0, 0, 0, 0, 0, 0, 0,
8171/* s */ 0x13,
8172/* t */ 0x3C,
8173/* u */ 0x3D,
8174/* v */ 0x32,
8175/* w */ 0x26,
8176/* x */ 0x18,
8177/* y */ 0x19,
8178/* z */ 0x3F,
8179/* AA - AC */
8180 0, 0, 0,
8181/* [ */ 0x27,
8182/* AE - BC */
8183 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8184/* ] */ 0x1D,
8185/* BE - C0 */ 0, 0, 0,
8186/* A */ 0x01,
8187/* B */ 0x02,
8188/* C */ 0x03,
8189/* D */ 0x37,
8190/* E */ 0x2D,
8191/* F */ 0x2E,
8192/* G */ 0x2F,
8193/* H */ 0x16,
8194/* I */ 0x05,
8195/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8196/* J */ 0x15,
8197/* K */ 0x0B,
8198/* L */ 0x0C,
8199/* M */ 0x0D,
8200/* N */ 0x0E,
8201/* O */ 0x0F,
8202/* P */ 0x10,
8203/* Q */ 0x11,
8204/* R */ 0x12,
8205/* DA - DF */ 0, 0, 0, 0, 0, 0,
8206/* \ */ 0x1C,
8207/* E1 */ 0,
8208/* S */ 0x13,
8209/* T */ 0x3C,
8210/* U */ 0x3D,
8211/* V */ 0x32,
8212/* W */ 0x26,
8213/* X */ 0x18,
8214/* Y */ 0x19,
8215/* Z */ 0x3F,
8216/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8217 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8218};
8219
8220char MetaCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008221{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8223 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8224 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8225 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8226};
8227
8228
Bram Moolenaar0f873732019-12-05 20:28:46 +01008229// TODO: Use characters NOT numbers!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230char CtrlCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008231{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8233 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8234 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8235 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8236};
8237
8238
8239#endif