blob: 1c807991741c254c9878e985090ee920b9747f0d [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 }
3346 else
3347 {
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 Moolenaar40385db2018-08-07 22:31:44 +02003373 // restore xterm title and icon name
3374 mch_restore_title(SAVE_RESTORE_BOTH);
3375 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376#endif
3377 /*
3378 * When t_ti is not empty but it doesn't cause swapping terminal
3379 * pages, need to output a newline when msg_didout is set. But when
3380 * t_ti does swap pages it should not go to the shell page. Do this
3381 * before stoptermcap().
3382 */
3383 if (swapping_screen() && !newline_on_exit)
3384 exit_scroll();
3385
Bram Moolenaar0f873732019-12-05 20:28:46 +01003386 // Stop termcap: May need to check for T_CRV response, which
3387 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 stoptermcap();
3389
3390 /*
3391 * A newline is only required after a message in the alternate screen.
3392 * This is set to TRUE by wait_return().
3393 */
3394 if (!swapping_screen() || newline_on_exit)
3395 exit_scroll();
3396
Bram Moolenaar0f873732019-12-05 20:28:46 +01003397 // Cursor may have been switched off without calling starttermcap()
3398 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 if (full_screen)
3400 cursor_on();
3401 }
3402 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003403 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003404
3405#ifdef USE_GCOV_FLUSH
3406 // Flush coverage info before possibly being killed by a deadly signal.
3407 __gcov_flush();
3408#endif
3409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 may_core_dump();
3411#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 gui_exit(r);
3414#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003415
Bram Moolenaar56718732006-03-15 22:53:57 +00003416#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003417 mac_conv_cleanup();
3418#endif
3419
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003421 // A core dump won't be created if the signal handler
3422 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 if (deadly_signal != 0)
3424 return;
3425#endif
3426
Bram Moolenaar009b2592004-10-24 19:18:58 +00003427#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003428 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003429#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003430
3431#ifdef EXITFREE
3432 free_all_mem();
3433#endif
3434
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 exit(r);
3436}
3437
3438 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003439may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
3441 if (deadly_signal != 0)
3442 {
3443 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003444 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
3446}
3447
3448#ifndef VMS
3449
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003450/*
3451 * Get the file descriptor to use for tty operations.
3452 */
3453 static int
3454get_tty_fd(int fd)
3455{
3456 int tty_fd = fd;
3457
3458#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3459 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3460 // "fd" when it is a master device.
3461 if (mch_isatty(fd) > 1)
3462 {
3463 char *name;
3464
3465 name = ptsname(fd);
3466 if (name == NULL)
3467 return -1;
3468
3469 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3470 if (tty_fd < 0)
3471 return -1;
3472 }
3473#endif
3474 return tty_fd;
3475}
3476
3477 static int
3478mch_tcgetattr(int fd, void *term)
3479{
3480 int tty_fd;
3481 int retval = -1;
3482
3483 tty_fd = get_tty_fd(fd);
3484 if (tty_fd >= 0)
3485 {
3486#ifdef NEW_TTY_SYSTEM
3487# ifdef HAVE_TERMIOS_H
3488 retval = tcgetattr(tty_fd, (struct termios *)term);
3489# else
3490 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3491# endif
3492#else
3493 // for "old" tty systems
3494 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3495#endif
3496 if (tty_fd != fd)
3497 close(tty_fd);
3498 }
3499 return retval;
3500}
3501
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003503mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
3505 static int first = TRUE;
3506
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003507#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508# ifdef HAVE_TERMIOS_H
3509 static struct termios told;
3510 struct termios tnew;
3511# else
3512 static struct termio told;
3513 struct termio tnew;
3514# endif
3515
3516 if (first)
3517 {
3518 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003519 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521
3522 tnew = told;
3523 if (tmode == TMODE_RAW)
3524 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003525 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003526 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3527 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003529# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003530 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531# endif
3532 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003533# ifdef ONLCR
3534 // Don't map NL -> CR NL, we do it ourselves.
3535 // Also disable expanding tabs if possible.
3536# ifdef XTABS
3537 tnew.c_oflag &= ~(ONLCR | XTABS);
3538# else
3539# ifdef TAB3
3540 tnew.c_oflag &= ~(ONLCR | TAB3);
3541# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003543# endif
3544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003546 tnew.c_cc[VMIN] = 1; // return after 1 char
3547 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
3549 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003550 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003551 // Also reset ICANON here, otherwise on Solaris select() won't see
3552 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003553 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003554 tnew.c_cc[VMIN] = 1; // return after 1 char
3555 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
3558# if defined(HAVE_TERMIOS_H)
3559 {
3560 int n = 10;
3561
Bram Moolenaar0f873732019-12-05 20:28:46 +01003562 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3563 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3565 && errno == EINTR && n > 0)
3566 --n;
3567 }
3568# else
3569 ioctl(read_cmd_fd, TCSETA, &tnew);
3570# endif
3571
3572#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 /*
3574 * for "old" tty systems
3575 */
3576# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003577# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578# endif
3579 static struct sgttyb ttybold;
3580 struct sgttyb ttybnew;
3581
3582 if (first)
3583 {
3584 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003585 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
3587
3588 ttybnew = ttybold;
3589 if (tmode == TMODE_RAW)
3590 {
3591 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3592 ttybnew.sg_flags |= RAW;
3593 }
3594 else if (tmode == TMODE_SLEEP)
3595 ttybnew.sg_flags &= ~(ECHO);
3596 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3597#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003598 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599}
3600
3601/*
3602 * Try to get the code for "t_kb" from the stty setting
3603 *
3604 * Even if termcap claims a backspace key, the user's setting *should*
3605 * prevail. stty knows more about reality than termcap does, and if
3606 * somebody's usual erase key is DEL (which, for most BSD users, it will
3607 * be), they're going to get really annoyed if their erase key starts
3608 * doing forward deletes for no reason. (Eric Fischer)
3609 */
3610 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003611get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003613 ttyinfo_T info;
3614 char_u buf[2];
3615 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003617 if (get_tty_info(read_cmd_fd, &info) == OK)
3618 {
3619 intr_char = info.interrupt;
3620 buf[0] = info.backspace;
3621 buf[1] = NUL;
3622 add_termcode((char_u *)"kb", buf, FALSE);
3623
Bram Moolenaar0f873732019-12-05 20:28:46 +01003624 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003625 p = find_termcode((char_u *)"kD");
3626 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3627 do_fixdel(NULL);
3628 }
3629}
3630
3631/*
3632 * Obtain the characters that Backspace and Enter produce on "fd".
3633 * Returns OK or FAIL.
3634 */
3635 int
3636get_tty_info(int fd, ttyinfo_T *info)
3637{
3638#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639# ifdef HAVE_TERMIOS_H
3640 struct termios keys;
3641# else
3642 struct termio keys;
3643# endif
3644
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003645 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003647 info->backspace = keys.c_cc[VERASE];
3648 info->interrupt = keys.c_cc[VINTR];
3649 if (keys.c_iflag & ICRNL)
3650 info->enter = NL;
3651 else
3652 info->enter = CAR;
3653 if (keys.c_oflag & ONLCR)
3654 info->nl_does_cr = TRUE;
3655 else
3656 info->nl_does_cr = FALSE;
3657 return OK;
3658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003660 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 struct sgttyb keys;
3662
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003663 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003665 info->backspace = keys.sg_erase;
3666 info->interrupt = keys.sg_kill;
3667 info->enter = CAR;
3668 info->nl_does_cr = TRUE;
3669 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003672 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673}
3674
Bram Moolenaar0f873732019-12-05 20:28:46 +01003675#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003677static int mouse_ison = FALSE;
3678
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679/*
3680 * Set mouse clicks on or off.
3681 */
3682 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003683mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003685#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003686 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 int xterm_mouse_vers;
3689
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003690#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003691 if (!on)
3692 // Make sure not tracing mouse movements. Important when a button-down
3693 // was received but no release yet.
3694 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003695#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003696
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003697 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003698#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003699 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003700#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003701 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003702 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 return;
3704
3705 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003706
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003707#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003708 if (ttym_flags == TTYM_URXVT)
3709 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003710 out_str_nf((char_u *)
3711 (on
3712 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3713 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003714 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003715 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003716#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003717
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003718 if (ttym_flags == TTYM_SGR)
3719 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003720 // SGR mode supports columns above 223
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003721 out_str_nf((char_u *)
3722 (on
3723 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3724 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003725 mouse_ison = on;
3726 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003727
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003728#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003729 if (bevalterm_ison != (p_bevalterm && on))
3730 {
3731 bevalterm_ison = (p_bevalterm && on);
3732 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003733 // disable mouse movement events, enabling is below
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003734 out_str_nf((char_u *)
3735 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003736 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003737#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003738
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 if (xterm_mouse_vers > 0)
3740 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003741 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 out_str_nf((char_u *)
3743 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003744 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003745#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003746 bevalterm_ison
3747 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003748#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003749 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003751 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 out_str_nf((char_u *)
3753 (xterm_mouse_vers > 1
3754 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3755 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003756 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
3758
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003759#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 else if (ttym_flags == TTYM_DEC)
3761 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003762 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003764 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003766 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003770#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 else
3772 {
3773 if (on)
3774 {
3775 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003776 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 else
3779 {
3780 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003781 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
3783 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003786#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003787 else
3788 {
3789 if (on)
3790 {
3791 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003792 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003793 }
3794 else
3795 {
3796 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003797 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003798 }
3799 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003800#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003801
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003802#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 else
3804 {
3805 if (on)
3806 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003807 // D - Enable Mouse up/down messages
3808 // L - Enable Left Button Reporting
3809 // M - Enable Middle Button Reporting
3810 // R - Enable Right Button Reporting
3811 // K - Enable SHIFT and CTRL key Reporting
3812 // + - Enable Advanced messaging of mouse moves and up/down messages
3813 // Q - Quiet No Ack
3814 // # - Numeric value of mouse pointer required
3815 // 0 = Multiview 2000 cursor, used as standard
3816 // 1 = Windows Arrow
3817 // 2 = Windows I Beam
3818 // 3 = Windows Hour Glass
3819 // 4 = Windows Cross Hair
3820 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003821# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003822 // Disables full feedback of pointer movements
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3824 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003825# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3827 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003828# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003829 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831 else
3832 {
3833 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3834 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003835 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003838#endif
3839#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 else
3841 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003842 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if (on)
3844 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3845 else
3846 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003847 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850}
3851
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003852#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003853/*
3854 * Called when 'balloonevalterm' changed.
3855 */
3856 void
3857mch_bevalterm_changed(void)
3858{
3859 mch_setmouse(mouse_ison);
3860}
3861#endif
3862
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863/*
3864 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3865 */
3866 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003867check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868{
3869# ifdef FEAT_MOUSE_XTERM
3870 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003871# ifdef FEAT_MOUSE_URXVT
3872 && use_xterm_mouse() != 3
3873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874# ifdef FEAT_GUI
3875 && !gui.in_use
3876# endif
3877 )
3878 {
3879 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003880 ? IF_EB("\233M", CSI_STR "M")
3881 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (*p_mouse != NUL)
3883 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003884 // force mouse off and maybe on to send possibly new mouse
3885 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 mch_setmouse(FALSE);
3887 setmouse();
3888 }
3889 }
3890 else
3891 del_mouse_termcode(KS_MOUSE);
3892# endif
3893
3894# ifdef FEAT_MOUSE_GPM
3895 if (!use_xterm_mouse()
3896# ifdef FEAT_GUI
3897 && !gui.in_use
3898# endif
3899 )
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003900 set_mouse_termcode(KS_GPM_MOUSE,
3901 (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3902 else
3903 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904# endif
3905
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003906# ifdef FEAT_SYSMOUSE
3907 if (!use_xterm_mouse()
3908# ifdef FEAT_GUI
3909 && !gui.in_use
3910# endif
3911 )
3912 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3913# endif
3914
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003916 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 if (!use_xterm_mouse()
3918# ifdef FEAT_GUI
3919 && !gui.in_use
3920# endif
3921 )
3922 set_mouse_termcode(KS_JSBTERM_MOUSE,
3923 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3924 else
3925 del_mouse_termcode(KS_JSBTERM_MOUSE);
3926# endif
3927
3928# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003929 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3930 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 if (!use_xterm_mouse()
3932# ifdef FEAT_GUI
3933 && !gui.in_use
3934# endif
3935 )
3936 set_mouse_termcode(KS_NETTERM_MOUSE,
3937 (char_u *)IF_EB("\033}", ESC_STR "}"));
3938 else
3939 del_mouse_termcode(KS_NETTERM_MOUSE);
3940# endif
3941
3942# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003943 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003944 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945# ifdef FEAT_GUI
3946 && !gui.in_use
3947# endif
3948 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003949 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3950 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 else
3952 del_mouse_termcode(KS_DEC_MOUSE);
3953# endif
3954# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01003955 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003956 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957# ifdef FEAT_GUI
3958 && !gui.in_use
3959# endif
3960 )
3961 set_mouse_termcode(KS_PTERM_MOUSE,
3962 (char_u *) IF_EB("\033[", ESC_STR "["));
3963 else
3964 del_mouse_termcode(KS_PTERM_MOUSE);
3965# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003966# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003967 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003968# ifdef FEAT_GUI
3969 && !gui.in_use
3970# endif
3971 )
3972 {
3973 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003974 ? IF_EB("\233*M", CSI_STR "*M")
3975 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003976
3977 if (*p_mouse != NUL)
3978 {
3979 mch_setmouse(FALSE);
3980 setmouse();
3981 }
3982 }
3983 else
3984 del_mouse_termcode(KS_URXVT_MOUSE);
3985# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003986 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003987# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003988 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003989# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003990 )
3991 {
3992 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003993 ? IF_EB("\233<*M", CSI_STR "<*M")
3994 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3995
3996 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3997 ? IF_EB("\233<*m", CSI_STR "<*m")
3998 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003999
4000 if (*p_mouse != NUL)
4001 {
4002 mch_setmouse(FALSE);
4003 setmouse();
4004 }
4005 }
4006 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004007 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004008 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004009 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013#ifndef VMS
4014
4015/*
4016 * Try to get the current window size:
4017 * 1. with an ioctl(), most accurate method
4018 * 2. from the environment variables LINES and COLUMNS
4019 * 3. from the termcap
4020 * 4. keep using the old values
4021 * Return OK when size could be determined, FAIL otherwise.
4022 */
4023 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004024mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025{
4026 long rows = 0;
4027 long columns = 0;
4028 char_u *p;
4029
4030 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 * 1. try using an ioctl. It is the most accurate method.
4032 *
4033 * Try using TIOCGWINSZ first, some systems that have it also define
4034 * TIOCGSIZE but don't have a struct ttysize.
4035 */
4036# ifdef TIOCGWINSZ
4037 {
4038 struct winsize ws;
4039 int fd = 1;
4040
Bram Moolenaar0f873732019-12-05 20:28:46 +01004041 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 if (!isatty(fd) && isatty(read_cmd_fd))
4043 fd = read_cmd_fd;
4044 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4045 {
4046 columns = ws.ws_col;
4047 rows = ws.ws_row;
4048 }
4049 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004050# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051# ifdef TIOCGSIZE
4052 {
4053 struct ttysize ts;
4054 int fd = 1;
4055
Bram Moolenaar0f873732019-12-05 20:28:46 +01004056 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 if (!isatty(fd) && isatty(read_cmd_fd))
4058 fd = read_cmd_fd;
4059 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4060 {
4061 columns = ts.ts_cols;
4062 rows = ts.ts_lines;
4063 }
4064 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004065# endif // TIOCGSIZE
4066# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068 /*
4069 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004070 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4071 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004073 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
4075 if ((p = (char_u *)getenv("LINES")))
4076 rows = atoi((char *)p);
4077 if ((p = (char_u *)getenv("COLUMNS")))
4078 columns = atoi((char *)p);
4079 }
4080
4081#ifdef HAVE_TGETENT
4082 /*
4083 * 3. try reading "co" and "li" entries from termcap
4084 */
4085 if (columns == 0 || rows == 0)
4086 getlinecol(&columns, &rows);
4087#endif
4088
4089 /*
4090 * 4. If everything fails, use the old values
4091 */
4092 if (columns <= 0 || rows <= 0)
4093 return FAIL;
4094
4095 Rows = rows;
4096 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004097 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 return OK;
4099}
4100
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004101#if defined(FEAT_TERMINAL) || defined(PROTO)
4102/*
4103 * Report the windows size "rows" and "cols" to tty "fd".
4104 */
4105 int
4106mch_report_winsize(int fd, int rows, int cols)
4107{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004108 int tty_fd;
4109 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004110
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004111 tty_fd = get_tty_fd(fd);
4112 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004113 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004114# if defined(TIOCSWINSZ)
4115 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004116
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004117 ws.ws_col = cols;
4118 ws.ws_row = rows;
4119 ws.ws_xpixel = cols * 5;
4120 ws.ws_ypixel = rows * 10;
4121 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4122 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4123 retval == 0 ? "success" : "failed");
4124# elif defined(TIOCSSIZE)
4125 struct ttysize ts;
4126
4127 ts.ts_cols = cols;
4128 ts.ts_lines = rows;
4129 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4130 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4131 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004132# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004133 if (tty_fd != fd)
4134 close(tty_fd);
4135 }
4136 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004137}
4138#endif
4139
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140/*
4141 * Try to set the window size to Rows and Columns.
4142 */
4143 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004144mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145{
4146 if (*T_CWS)
4147 {
4148 /*
4149 * NOTE: if you get an error here that term_set_winsize() is
4150 * undefined, check the output of configure. It could probably not
4151 * find a ncurses, termcap or termlib library.
4152 */
4153 term_set_winsize((int)Rows, (int)Columns);
4154 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004155 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 }
4157}
4158
Bram Moolenaar0f873732019-12-05 20:28:46 +01004159#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161/*
4162 * Rows and/or Columns has changed.
4163 */
4164 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004165mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004167 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168}
4169
Bram Moolenaar205b8862011-09-07 15:04:31 +02004170/*
4171 * Wait for process "child" to end.
4172 * Return "child" if it exited properly, <= 0 on error.
4173 */
4174 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004175wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004176{
4177 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004178 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004179
4180 while (wait_pid != child)
4181 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004182 // When compiled with Python threads are probably used, in which case
4183 // wait() sometimes hangs for no obvious reason. Use waitpid()
4184 // instead and loop (like the GUI). Also needed for other interfaces,
4185 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004186# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004187 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004188# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004189 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004190# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004191 if (wait_pid == 0)
4192 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004193 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004194 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004195 if (++delay_msec > 10)
4196 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004197 continue;
4198 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004199 if (wait_pid <= 0
4200# ifdef ECHILD
4201 && errno == ECHILD
4202# endif
4203 )
4204 break;
4205 }
4206 return wait_pid;
4207}
4208
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004209#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004210/*
4211 * Set the environment for a child process.
4212 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004213 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004214set_child_environment(
4215 long rows,
4216 long columns,
4217 char *term,
4218 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004219{
4220# ifdef HAVE_SETENV
4221 char envbuf[50];
4222# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004223 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004224 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004225 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004226 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004227 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004228# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004229 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004230# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004231# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004232 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004233# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004234# endif
4235
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004236# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004237 setenv("TERM", term, 1);
4238 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004239 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004240 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004241 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004242 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004243 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004244 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004245 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004246# ifdef FEAT_TERMINAL
4247 if (is_terminal)
4248 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004249 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004250 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4251 }
4252# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004253# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004254 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004255# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004256# else
4257 /*
4258 * Putenv does not copy the string, it has to remain valid.
4259 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004260 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004261 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004262 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4263 putenv(envbuf_Term);
4264 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004265 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004266 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4267 putenv(envbuf_Lines);
4268 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4269 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004270 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004271 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004272 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004273# ifdef FEAT_TERMINAL
4274 if (is_terminal)
4275 {
4276 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004277 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004278 putenv(envbuf_Version);
4279 }
4280# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004281# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004282 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4283 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4284 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004285# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004286# endif
4287}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004288
4289 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004290set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004291{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004292 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004293}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004294#endif
4295
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004296#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004297/*
4298 * Open a PTY, with FD for the master and slave side.
4299 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004300 * When successful both file descriptors are stored and the allocated pty name
4301 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004302 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004303 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004304open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004305{
4306 char *tty_name;
4307
Bram Moolenaar59386482019-02-10 22:43:46 +01004308 if (name1 != NULL)
4309 *name1 = NULL;
4310 if (name2 != NULL)
4311 *name2 = NULL;
4312
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004313 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004314 if (*pty_master_fd >= 0)
4315 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004316 // Leaving out O_NOCTTY may lead to waitpid() always returning
4317 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4318 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004319#ifdef O_NOCTTY
4320 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4321#else
4322 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4323#endif
4324 if (*pty_slave_fd < 0)
4325 {
4326 close(*pty_master_fd);
4327 *pty_master_fd = -1;
4328 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004329 else
4330 {
4331 if (name1 != NULL)
4332 *name1 = vim_strsave((char_u *)tty_name);
4333 if (name2 != NULL)
4334 *name2 = vim_strsave((char_u *)tty_name);
4335 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004336 }
4337}
4338#endif
4339
Bram Moolenaarfae42832017-08-01 22:24:26 +02004340/*
4341 * Send SIGINT to a child process if "c" is an interrupt character.
4342 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004343 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004344may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4345{
4346# ifdef SIGINT
4347 if (c == Ctrl_C || c == intr_char)
4348 {
4349# ifdef HAVE_SETSID
4350 kill(-pid, SIGINT);
4351# else
4352 kill(0, SIGINT);
4353# endif
4354 if (wpid > 0)
4355 kill(wpid, SIGINT);
4356 }
4357# endif
4358}
4359
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004360#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004361
Bram Moolenaar0f873732019-12-05 20:28:46 +01004362/*
4363 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4364 * pointers, the last one is NULL.
4365 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4366 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004367 int
4368unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004369 char_u *cmd,
4370 char ***argvp,
4371 char_u **sh_tofree,
4372 char_u **shcf_tofree)
4373{
4374 char **argv = NULL;
4375 int argc;
4376
4377 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004378 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004379 return FAIL;
4380
4381 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4382 return FAIL;
4383 *argvp = argv;
4384
4385 if (cmd != NULL)
4386 {
4387 char_u *s;
4388 char_u *p;
4389
4390 if (extra_shell_arg != NULL)
4391 argv[argc++] = (char *)extra_shell_arg;
4392
Bram Moolenaar0f873732019-12-05 20:28:46 +01004393 // Break 'shellcmdflag' into white separated parts. This doesn't
4394 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004395 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004396 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004397 return FAIL;
4398 s = *shcf_tofree;
4399 p = p_shcf;
4400 while (*p != NUL)
4401 {
4402 argv[argc++] = (char *)s;
4403 while (*p && *p != ' ' && *p != TAB)
4404 *s++ = *p++;
4405 *s++ = NUL;
4406 p = skipwhite(p);
4407 }
4408
4409 argv[argc++] = (char *)cmd;
4410 }
4411 argv[argc] = NULL;
4412 return OK;
4413}
4414#endif
4415
4416#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4417/*
4418 * Use a terminal window to run a shell command in.
4419 */
4420 static int
4421mch_call_shell_terminal(
4422 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004423 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004424{
4425 jobopt_T opt;
4426 char **argv = NULL;
4427 char_u *tofree1 = NULL;
4428 char_u *tofree2 = NULL;
4429 int retval = -1;
4430 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004431 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004432 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004433 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004434
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004435 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004436 goto theend;
4437
4438 init_job_options(&opt);
4439 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4440 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004441 if (buf == NULL)
4442 goto theend;
4443
4444 job = term_getjob(buf->b_term);
4445 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004446
Bram Moolenaar0f873732019-12-05 20:28:46 +01004447 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004448 aucmd_prepbuf(&aco, buf);
4449
4450 clear_oparg(&oa);
4451 while (term_use_loop())
4452 {
4453 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4454 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004455 // If terminal_loop() returns OK we got a key that is handled
4456 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004457 if (terminal_loop(TRUE) == OK)
4458 normal_cmd(&oa, TRUE);
4459 }
4460 else
4461 normal_cmd(&oa, TRUE);
4462 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004463 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004464 ch_log(NULL, "system command finished");
4465
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004466 job_unref(job);
4467
Bram Moolenaar0f873732019-12-05 20:28:46 +01004468 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004469 aucmd_restbuf(&aco);
4470
4471 wait_return(TRUE);
4472 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4473
4474theend:
4475 vim_free(argv);
4476 vim_free(tofree1);
4477 vim_free(tofree2);
4478 return retval;
4479}
4480#endif
4481
4482#ifdef USE_SYSTEM
4483/*
4484 * Use system() to start the shell: simple but slow.
4485 */
4486 static int
4487mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004488 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004489 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490{
4491#ifdef VMS
4492 char *ifn = NULL;
4493 char *ofn = NULL;
4494#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004495 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004496 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004497 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
4499 out_flush();
4500
4501 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004502 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
Bram Moolenaar62b42182010-09-21 22:09:37 +02004504# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004505 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004506 loose_clipboard();
4507# endif
4508
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 if (cmd == NULL)
4510 x = system((char *)p_sh);
4511 else
4512 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004513# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 if (ofn = strchr((char *)cmd, '>'))
4515 *ofn++ = '\0';
4516 if (ifn = strchr((char *)cmd, '<'))
4517 {
4518 char *p;
4519
4520 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004521 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 if (p)
4523 *p = '\0';
4524 }
4525 if (ofn)
4526 x = vms_sys((char *)cmd, ofn, ifn);
4527 else
4528 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004529# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004530 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004532 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 if (newcmd == NULL)
4534 x = 0;
4535 else
4536 {
4537 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4538 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4539 (char *)p_shcf,
4540 (char *)cmd);
4541 x = system((char *)newcmd);
4542 vim_free(newcmd);
4543 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 }
4546# ifdef VMS
4547 x = vms_sys_status(x);
4548# endif
4549 if (emsg_silent)
4550 ;
4551 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004552 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 else if (x && !(options & SHELL_SILENT))
4554 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004555 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 msg_outnum((long)x);
4557 msg_putchar('\n');
4558 }
4559
4560 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004561 {
4562 // The shell may have messed with the mode, always set it.
4563 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004564 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566# ifdef FEAT_TITLE
4567 resettitle();
4568# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004569# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4570 restore_clipboard();
4571# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004573}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
Bram Moolenaar0f873732019-12-05 20:28:46 +01004575#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576
Bram Moolenaar0f873732019-12-05 20:28:46 +01004577# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4578 // 127, some shells use that already
4579# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
Bram Moolenaar13568252018-03-16 20:46:58 +01004581/*
4582 * Don't use system(), use fork()/exec().
4583 */
4584 static int
4585mch_call_shell_fork(
4586 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004587 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004588{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004589 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004591 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 pid_t wait_pid = 0;
4593# ifdef HAVE_UNION_WAIT
4594 union wait status;
4595# else
4596 int status = -1;
4597# endif
4598 int retval = -1;
4599 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004600 char_u *tofree1 = NULL;
4601 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004603 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004604# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004606# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004607 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 int fd_fromshell[2];
4609 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004610 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611
4612 out_flush();
4613 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004614 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004615 if (tmode == TMODE_RAW)
4616 // The shell may have messed with the mode, always set it later.
4617 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004619 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004620 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004621
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004623 * For the GUI, when writing the output into the buffer and when reading
4624 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4625 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004627 if ((options & (SHELL_READ|SHELL_WRITE))
4628# ifdef FEAT_GUI
4629 || (gui.in_use && show_shell_mess)
4630# endif
4631 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004633# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 /*
4635 * Try to open a master pty.
4636 * If this works, open the slave pty.
4637 * If the slave can't be opened, close the master pty.
4638 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004639 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004640 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 /*
4642 * If not opening a pty or it didn't work, try using pipes.
4643 */
4644 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
4647 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004648 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 {
4650 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004651 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 {
4653 close(fd_toshell[0]);
4654 close(fd_toshell[1]);
4655 }
4656 }
4657 if (pipe_error)
4658 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004659 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 out_flush();
4661 }
4662 }
4663 }
4664
Bram Moolenaar0f873732019-12-05 20:28:46 +01004665 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004667 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004668 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004669 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004670 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004672 UNBLOCK_SIGNALS(&curset);
4673
Bram Moolenaar32526b32019-01-19 17:43:09 +01004674 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004675 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004677 || (gui.in_use && show_shell_mess)
4678# endif
4679 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004681# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004682 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 {
4684 close(pty_master_fd);
4685 close(pty_slave_fd);
4686 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004687 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 {
4690 close(fd_toshell[0]);
4691 close(fd_toshell[1]);
4692 close(fd_fromshell[0]);
4693 close(fd_fromshell[1]);
4694 }
4695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004697 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004699 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004700 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701
Bram Moolenaar819524702018-02-27 19:10:00 +01004702# ifdef FEAT_JOB_CHANNEL
4703 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004704 {
4705 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004706 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004707 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004708# endif
4709
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 if (!show_shell_mess || (options & SHELL_EXPAND))
4711 {
4712 int fd;
4713
4714 /*
4715 * Don't want to show any message from the shell. Can't just
4716 * close stdout and stderr though, because some systems will
4717 * break if you try to write to them after that, so we must
4718 * use dup() to replace them with something else -- webb
4719 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4720 * waiting for input.
4721 */
4722 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4723 fclose(stdin);
4724 fclose(stdout);
4725 fclose(stderr);
4726
4727 /*
4728 * If any of these open()'s and dup()'s fail, we just continue
4729 * anyway. It's not fatal, and on most systems it will make
4730 * no difference at all. On a few it will cause the execvp()
4731 * to exit with a non-zero status even when the completion
4732 * could be done, which is nothing too serious. If the open()
4733 * or dup() failed we'd just do the same thing ourselves
4734 * anyway -- webb
4735 */
4736 if (fd >= 0)
4737 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004738 vim_ignored = dup(fd); // To replace stdin (fd 0)
4739 vim_ignored = dup(fd); // To replace stdout (fd 1)
4740 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741
Bram Moolenaar0f873732019-12-05 20:28:46 +01004742 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 close(fd);
4744 }
4745 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004746 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004748 || gui.in_use
4749# endif
4750 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
4752
Bram Moolenaardf177f62005-02-22 08:39:57 +00004753# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004754 // Create our own process group, so that the child and all its
4755 // children can be kill()ed. Don't do this when using pipes,
4756 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004757 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004758 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004759 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004760# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004761 // When doing "!xterm&" and 'shell' is bash: the shell
4762 // will exit and send SIGHUP to all processes in its
4763 // group, killing the just started process. Ignore SIGHUP
4764 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004765 signal(SIGHUP, SIG_IGN);
4766# endif
4767 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004768# endif
4769# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004770 if (pty_slave_fd >= 0)
4771 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004772 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004773 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004774 setup_slavepty(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01004776 // Try to become controlling tty (probably doesn't work,
4777 // unless run by root)
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004778 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004780 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004781# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004782 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
Bram Moolenaara5792f52005-11-23 21:25:05 +00004784 /*
4785 * stderr is only redirected when using the GUI, so that a
4786 * program like gpg can still access the terminal to get a
4787 * passphrase using stderr.
4788 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004789# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 if (pty_master_fd >= 0)
4791 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004792 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
Bram Moolenaar0f873732019-12-05 20:28:46 +01004794 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004796 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004798 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004799 if (gui.in_use)
4800 {
4801 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004802 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804
Bram Moolenaar0f873732019-12-05 20:28:46 +01004805 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004810 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 close(fd_toshell[1]);
4812 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004813 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 close(fd_toshell[0]);
4815
Bram Moolenaar0f873732019-12-05 20:28:46 +01004816 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 close(fd_fromshell[0]);
4818 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004819 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 close(fd_fromshell[1]);
4821
Bram Moolenaara5792f52005-11-23 21:25:05 +00004822# ifdef FEAT_GUI
4823 if (gui.in_use)
4824 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004825 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004826 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004827 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004828 }
4829# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 }
4831 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004832
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 /*
4834 * There is no type cast for the argv, because the type may be
4835 * different on different machines. This may cause a warning
4836 * message with strict compilers, don't worry about it.
4837 * Call _exit() instead of exit() to avoid closing the connection
4838 * to the X server (esp. with GTK, which uses atexit()).
4839 */
4840 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004841 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004843 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 {
4845 /*
4846 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004847 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 */
4849 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004850 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004851 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004852# ifdef FEAT_JOB_CHANNEL
4853 ++dont_check_job_ended;
4854# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 /*
4856 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004857 * This is also used to pipe stdin/stdout to/from the external
4858 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004860 if ((options & (SHELL_READ|SHELL_WRITE))
4861# ifdef FEAT_GUI
4862 || (gui.in_use && show_shell_mess)
4863# endif
4864 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004866# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004868 int buffer_off = 0; // valid bytes in buffer[]
4869 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4870 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 int len;
4872 int p_more_save;
4873 int old_State;
4874 int c;
4875 int toshell_fd;
4876 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004877 garray_T ga;
4878 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004879# ifdef ELAPSED_FUNC
4880 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882
Bram Moolenaardf177f62005-02-22 08:39:57 +00004883# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 if (pty_master_fd >= 0)
4885 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 fromshell_fd = pty_master_fd;
4887 toshell_fd = dup(pty_master_fd);
4888 }
4889 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 {
4892 close(fd_toshell[0]);
4893 close(fd_fromshell[1]);
4894 toshell_fd = fd_toshell[1];
4895 fromshell_fd = fd_fromshell[0];
4896 }
4897
4898 /*
4899 * Write to the child if there are typed characters.
4900 * Read from the child if there are characters available.
4901 * Repeat the reading a few times if more characters are
4902 * available. Need to check for typed keys now and then, but
4903 * not too often (delays when no chars are available).
4904 * This loop is quit if no characters can be read from the pty
4905 * (WaitForChar detected special condition), or there are no
4906 * characters available and the child has exited.
4907 * Only check if the child has exited when there is no more
4908 * output. The child may exit before all the output has
4909 * been printed.
4910 *
4911 * Currently this busy loops!
4912 * This can probably dead-lock when the write blocks!
4913 */
4914 p_more_save = p_more;
4915 p_more = FALSE;
4916 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004917 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004919 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004920 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004921 // Fork a process that will write the lines to the
4922 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004923 if ((wpid = fork()) == -1)
4924 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004925 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004926 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004927 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004928 {
4929 linenr_T lnum = curbuf->b_op_start.lnum;
4930 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004931 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004932 size_t l;
4933
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004934 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004935 for (;;)
4936 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004937 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004938 if (l == 0)
4939 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004940 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004941 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004942 len = write(toshell_fd, "", (size_t)1);
4943 else
4944 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004945 char_u *s = vim_strchr(lp + written, NL);
4946
Bram Moolenaar89d40322006-08-29 15:30:07 +00004947 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004948 s == NULL ? l
4949 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004950 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004951 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004952 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004953 // Finished a line, add a NL, unless this line
4954 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004955 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004956 || (!curbuf->b_p_bin
4957 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004958 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004959 && (lnum !=
4960 curbuf->b_ml.ml_line_count
4961 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004962 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004963 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004964 ++lnum;
4965 if (lnum > curbuf->b_op_end.lnum)
4966 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004967 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00004968 close(toshell_fd);
4969 toshell_fd = -1;
4970 break;
4971 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004972 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004973 written = 0;
4974 }
4975 else if (len > 0)
4976 written += len;
4977 }
4978 _exit(0);
4979 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004980 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00004981 {
4982 close(toshell_fd);
4983 toshell_fd = -1;
4984 }
4985 }
4986
4987 if (options & SHELL_READ)
4988 ga_init2(&ga, 1, BUFLEN);
4989
4990 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004991# ifdef ELAPSED_FUNC
4992 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 for (;;)
4995 {
4996 /*
4997 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004998 * if there are any.
4999 * Don't do this if we are expanding wild cards (would eat
5000 * typeahead).
5001 * Don't do this when filtering and terminal is in cooked
5002 * mode, the shell command will handle the I/O. Avoids
5003 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005004 * Don't get characters when the child has already
5005 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005006 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005007 * while (noread_cnt > 4), avoids that ":r !ls" eats
5008 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 */
5010 len = 0;
5011 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005012 && ((options &
5013 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5014 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005015# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005016 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005017# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005018 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005019 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005020 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005022 if (ta_len == 0)
5023 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005024 // Get extra characters when we don't have any.
5025 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005026 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005027# ifdef ELAPSED_FUNC
5028 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005029# endif
5030 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5031 }
5032 if (ta_len > 0 || len > 0)
5033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 /*
5035 * For pipes:
5036 * Check for CTRL-C: send interrupt signal to child.
5037 * Check for CTRL-D: EOF, close pipe to child.
5038 */
5039 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5040 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 /*
5042 * Send SIGINT to the child's group or all
5043 * processes in our group.
5044 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005045 may_send_sigint(ta_buf[ta_len], pid, wpid);
5046
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (pty_master_fd < 0 && toshell_fd >= 0
5048 && ta_buf[ta_len] == Ctrl_D)
5049 {
5050 close(toshell_fd);
5051 toshell_fd = -1;
5052 }
5053 }
5054
Bram Moolenaarf4140482020-02-15 23:06:45 +01005055 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056
5057 /*
5058 * For pipes: echo the typed characters.
5059 * For a pty this does not seem to work.
5060 */
5061 if (pty_master_fd < 0)
5062 {
5063 for (i = ta_len; i < ta_len + len; ++i)
5064 {
5065 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5066 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 else if (has_mbyte)
5068 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005069 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
5071 msg_outtrans_len(ta_buf + i, l);
5072 i += l - 1;
5073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 else
5075 msg_outtrans_len(ta_buf + i, 1);
5076 }
5077 windgoto(msg_row, msg_col);
5078 out_flush();
5079 }
5080
5081 ta_len += len;
5082
5083 /*
5084 * Write the characters to the child, unless EOF has
5085 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005086 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005087 * When writing buffer lines, drop the typed
5088 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005090 if (options & SHELL_WRITE)
5091 ta_len = 0;
5092 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
5094 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5095 if (len > 0)
5096 {
5097 ta_len -= len;
5098 mch_memmove(ta_buf, ta_buf + len, ta_len);
5099 }
5100 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 }
5103
Bram Moolenaardf177f62005-02-22 08:39:57 +00005104 if (got_int)
5105 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005106 // CTRL-C sends a signal to the child, we ignore it
5107 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005108# ifdef HAVE_SETSID
5109 kill(-pid, SIGINT);
5110# else
5111 kill(0, SIGINT);
5112# endif
5113 if (wpid > 0)
5114 kill(wpid, SIGINT);
5115 got_int = FALSE;
5116 }
5117
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 /*
5119 * Check if the child has any characters to be printed.
5120 * Read them and write them to our window. Repeat this as
5121 * long as there is something to do, avoid the 10ms wait
5122 * for mch_inchar(), or sending typeahead characters to
5123 * the external process.
5124 * TODO: This should handle escape sequences, compatible
5125 * to some terminal (vt52?).
5126 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005127 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005128 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005130 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005133 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005135
5136 noread_cnt = 0;
5137 if (options & SHELL_READ)
5138 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005139 // Do NUL -> NL translation, append NL separated
5140 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005141 for (i = 0; i < len; ++i)
5142 {
5143 if (buffer[i] == NL)
5144 append_ga_line(&ga);
5145 else if (buffer[i] == NUL)
5146 ga_append(&ga, NL);
5147 else
5148 ga_append(&ga, buffer[i]);
5149 }
5150 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005151 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 {
5153 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005154 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155
Bram Moolenaardf177f62005-02-22 08:39:57 +00005156 len += buffer_off;
5157 buffer[len] = NUL;
5158
Bram Moolenaar0f873732019-12-05 20:28:46 +01005159 // Check if the last character in buffer[] is
5160 // incomplete, keep these bytes for the next
5161 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 for (p = buffer; p < buffer + len; p += l)
5163 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005164 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005166 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 else if (MB_BYTE2LEN(*p) != l)
5168 break;
5169 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005170 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005172 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 if (len >= 12)
5174 ++p;
5175 else
5176 {
5177 buffer_off = len;
5178 continue;
5179 }
5180 }
5181 c = *p;
5182 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005183 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (p < buffer + len)
5185 {
5186 *p = c;
5187 buffer_off = (buffer + len) - p;
5188 mch_memmove(buffer, p, buffer_off);
5189 continue;
5190 }
5191 buffer_off = 0;
5192 }
5193 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
5195 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005196 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
5198
5199 windgoto(msg_row, msg_col);
5200 cursor_on();
5201 out_flush();
5202 if (got_int)
5203 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005204
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005205# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005206 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005207 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005208 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005209
Bram Moolenaar0f873732019-12-05 20:28:46 +01005210 // Avoid that we keep looping here without
5211 // checking for a CTRL-C for a long time. Don't
5212 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005213 if (msec > 2000)
5214 {
5215 noread_cnt = 5;
5216 break;
5217 }
5218 }
5219# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
5221
Bram Moolenaar0f873732019-12-05 20:28:46 +01005222 // If we already detected the child has finished, continue
5223 // reading output for a short while. Some text may be
5224 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005225 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005226 {
5227 if (noread_cnt < 5)
5228 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005229 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005230 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005231
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 /*
5233 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005234 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005236# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005237 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005238# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005240# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5242 || (wait_pid == pid && WIFEXITED(status)))
5243 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005244 // Don't break the loop yet, try reading more
5245 // characters from "fromshell_fd" first. When using
5246 // pipes there might still be something to read and
5247 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005250 else
5251 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005252
Bram Moolenaar95a51352013-03-21 22:53:50 +01005253# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005254 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005255 clip_update();
5256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 }
5258finished:
5259 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005260 if (options & SHELL_READ)
5261 {
5262 if (ga.ga_len > 0)
5263 {
5264 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005265 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005266 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005267 }
5268 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005269 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005270 ga_clear(&ga);
5271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 /*
5274 * Give all typeahead that wasn't used back to ui_inchar().
5275 */
5276 if (ta_len)
5277 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 State = old_State;
5279 if (toshell_fd >= 0)
5280 close(toshell_fd);
5281 close(fromshell_fd);
5282 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005283# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005284 else
5285 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005286 long delay_msec = 1;
5287
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005288 if (tmode == TMODE_RAW)
5289 // possibly disables modifyOtherKeys, so that the system
5290 // can recognize CTRL-C
5291 out_str(T_CTE);
Bram Moolenaar0981c872020-08-23 14:28:37 +02005292
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005293 /*
5294 * Similar to the loop above, but only handle X events, no
5295 * I/O.
5296 */
5297 for (;;)
5298 {
5299 if (got_int)
5300 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005301 // CTRL-C sends a signal to the child, we ignore it
5302 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005303# ifdef HAVE_SETSID
5304 kill(-pid, SIGINT);
5305# else
5306 kill(0, SIGINT);
5307# endif
5308 got_int = FALSE;
5309 }
5310# ifdef __NeXT__
5311 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5312# else
5313 wait_pid = waitpid(pid, &status, WNOHANG);
5314# endif
5315 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5316 || (wait_pid == pid && WIFEXITED(status)))
5317 {
5318 wait_pid = pid;
5319 break;
5320 }
5321
Bram Moolenaar0f873732019-12-05 20:28:46 +01005322 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005323 clip_update();
5324
Bram Moolenaar0f873732019-12-05 20:28:46 +01005325 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005326 // less time, gradually wait longer.
5327 mch_delay(delay_msec,
5328 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005329 if (++delay_msec > 10)
5330 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005331 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005332
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005333 if (tmode == TMODE_RAW)
5334 // possibly enables modifyOtherKeys again
5335 out_str(T_CTI);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005336 }
5337# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338
5339 /*
5340 * Wait until our child has exited.
5341 * Ignore wait() returning pids of other children and returning
5342 * because of some signal like SIGWINCH.
5343 * Don't wait if wait_pid was already set above, indicating the
5344 * child already exited.
5345 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005346 if (wait_pid != pid)
5347 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348
Bram Moolenaar624891f2010-10-13 16:22:09 +02005349# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005350 // Close slave side of pty. Only do this after the child has
5351 // exited, otherwise the child may hang when it tries to write on
5352 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005353 if (pty_master_fd >= 0)
5354 close(pty_slave_fd);
5355# endif
5356
Bram Moolenaar0f873732019-12-05 20:28:46 +01005357 // Make sure the child that writes to the external program is
5358 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005359 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005360 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005361 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005362 wait4pid(wpid, NULL);
5363 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005364
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005365# ifdef FEAT_JOB_CHANNEL
5366 --dont_check_job_ended;
5367# endif
5368
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 /*
5370 * Set to raw mode right now, otherwise a CTRL-C after
5371 * catch_signals() will kill Vim.
5372 */
5373 if (tmode == TMODE_RAW)
5374 settmode(TMODE_RAW);
5375 did_settmode = TRUE;
5376 set_signals();
5377
5378 if (WIFEXITED(status))
5379 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005380 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005382 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 {
5384 if (retval == EXEC_FAILED)
5385 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005386 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 msg_outtrans(p_sh);
5388 msg_putchar('\n');
5389 }
5390 else if (!(options & SHELL_SILENT))
5391 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005392 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 msg_outnum((long)retval);
5394 msg_putchar('\n');
5395 }
5396 }
5397 }
5398 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005399 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 }
5401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
5403error:
5404 if (!did_settmode)
5405 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005406 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407# ifdef FEAT_TITLE
5408 resettitle();
5409# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005410 vim_free(argv);
5411 vim_free(tofree1);
5412 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413
5414 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005415}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005416#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005417
5418 int
5419mch_call_shell(
5420 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005421 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005422{
5423#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5424 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5425 return mch_call_shell_terminal(cmd, options);
5426#endif
5427#ifdef USE_SYSTEM
5428 return mch_call_shell_system(cmd, options);
5429#else
5430 return mch_call_shell_fork(cmd, options);
5431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432}
5433
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005434#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005435 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005436mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005437{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005438 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005439 int fd_in[2] = {-1, -1}; // for stdin
5440 int fd_out[2] = {-1, -1}; // for stdout
5441 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005442 int pty_master_fd = -1;
5443 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005444 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005445 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5446 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5447 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005448 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005449 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5450 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005451 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005452 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005453 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005454
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005455 if (use_out_for_err && use_null_for_out)
5456 use_null_for_err = TRUE;
5457
Bram Moolenaar0f873732019-12-05 20:28:46 +01005458 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005459 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005460
Bram Moolenaarb2412082017-08-20 18:09:14 +02005461 if (options->jo_pty
5462 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005463 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005464 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005465 open_pty(&pty_master_fd, &pty_slave_fd,
5466 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005467
Bram Moolenaar0f873732019-12-05 20:28:46 +01005468 // TODO: without the channel feature connect the child to /dev/null?
5469 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005470 if (use_file_for_in)
5471 {
5472 char_u *fname = options->jo_io_name[PART_IN];
5473
5474 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5475 if (fd_in[0] < 0)
5476 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005477 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005478 goto failed;
5479 }
5480 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005481 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005482 // When writing buffer lines to the input don't use the pty, so that
5483 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005484 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5485 && pipe(fd_in) < 0)
5486 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005487
5488 if (use_file_for_out)
5489 {
5490 char_u *fname = options->jo_io_name[PART_OUT];
5491
5492 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5493 if (fd_out[1] < 0)
5494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005495 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005496 goto failed;
5497 }
5498 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005499 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005500 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005501
5502 if (use_file_for_err)
5503 {
5504 char_u *fname = options->jo_io_name[PART_ERR];
5505
5506 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5507 if (fd_err[1] < 0)
5508 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005509 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005510 goto failed;
5511 }
5512 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005513 else if (!use_out_for_err && !use_null_for_err
5514 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005515 goto failed;
5516
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005517 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5518 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005519 if (options->jo_set & JO_CHANNEL)
5520 {
5521 channel = options->jo_channel;
5522 if (channel != NULL)
5523 ++channel->ch_refcount;
5524 }
5525 else
5526 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005527 if (channel == NULL)
5528 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005529 if (job->jv_tty_out != NULL)
5530 ch_log(channel, "using pty %s on fd %d",
5531 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005532 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005533
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005534 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005535 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005536 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005537 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005538 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005539 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005540 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005541 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005542 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005543 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005544 int null_fd = -1;
5545 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005546
Bram Moolenaar0f873732019-12-05 20:28:46 +01005547 // child
5548 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005549 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005550
Bram Moolenaar819524702018-02-27 19:10:00 +01005551# ifdef FEAT_JOB_CHANNEL
5552 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005553 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005554 ch_logfile((char_u *)"", (char_u *)"");
5555# endif
5556
Bram Moolenaar835dc632016-02-07 14:27:38 +01005557# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005558 // Create our own process group, so that the child and all its
5559 // children can be kill()ed. Don't do this when using pipes,
5560 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005561 (void)setsid();
5562# endif
5563
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005564# ifdef FEAT_TERMINAL
5565 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005566 {
5567 char *term = (char *)T_NAME;
5568
5569#ifdef FEAT_GUI
5570 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005571 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005572 term = getenv("TERM");
5573#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005574 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005575 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005576 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005577 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005578 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005579 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005580 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005581 else if (t_colors > 16)
5582 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005583 else
5584 term = "xterm";
5585 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005586 set_child_environment(
5587 (long)options->jo_term_rows,
5588 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005589 term,
5590 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005591 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005592 else
5593# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005594 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005595
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005596 if (options->jo_env != NULL)
5597 {
5598 dict_T *dict = options->jo_env;
5599 hashitem_T *hi;
5600 int todo = (int)dict->dv_hashtab.ht_used;
5601
5602 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5603 if (!HASHITEM_EMPTY(hi))
5604 {
5605 typval_T *item = &dict_lookup(hi)->di_tv;
5606
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005607 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005608 --todo;
5609 }
5610 }
5611
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005612 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005613 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005614 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005615 if (null_fd < 0)
5616 {
5617 perror("opening /dev/null failed");
5618 _exit(OPEN_NULL_FAILED);
5619 }
5620 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005621
Bram Moolenaar223896d2017-08-02 22:33:28 +02005622 if (pty_slave_fd >= 0)
5623 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005624 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005625 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005626# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005627 // Try to become controlling tty (probably doesn't work,
5628 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005629 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5630# endif
5631 }
5632
Bram Moolenaar0f873732019-12-05 20:28:46 +01005633 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005634 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005635 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005636 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005637 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005638 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005639 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005640 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005641
Bram Moolenaar0f873732019-12-05 20:28:46 +01005642 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005643 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005644 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005645 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005646 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005647 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005648 }
5649 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005650 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005651 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005652 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005653 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005654 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005655
Bram Moolenaar0f873732019-12-05 20:28:46 +01005656 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005657 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005658 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005659 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005660 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005661 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005662 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005663 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005664
5665 if (fd_in[0] >= 0)
5666 close(fd_in[0]);
5667 if (fd_in[1] >= 0)
5668 close(fd_in[1]);
5669 if (fd_out[0] >= 0)
5670 close(fd_out[0]);
5671 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005672 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005673 if (fd_err[0] >= 0)
5674 close(fd_err[0]);
5675 if (fd_err[1] >= 0)
5676 close(fd_err[1]);
5677 if (pty_master_fd >= 0)
5678 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005679 close(pty_master_fd); // not used in the child
5680 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005681 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005682
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005683 if (null_fd >= 0)
5684 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005685
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005686 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5687 _exit(EXEC_FAILED);
5688
Bram Moolenaar0f873732019-12-05 20:28:46 +01005689 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005690 execvp(argv[0], argv);
5691
Bram Moolenaar4694a172016-04-21 14:05:23 +02005692 if (stderr_works)
5693 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005694# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005695 // calling free_all_mem() here causes problems. Ignore valgrind
5696 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005697# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005698 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005699 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005700
Bram Moolenaar0f873732019-12-05 20:28:46 +01005701 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005702 UNBLOCK_SIGNALS(&curset);
5703
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005704 job->jv_pid = pid;
5705 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005706 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005707
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005708 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005709 close(pty_slave_fd); // not used in the parent
5710 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005711 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005712 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005713 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005714 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005715 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005716 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005717 if (channel != NULL)
5718 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005719 int in_fd = INVALID_FD;
5720 int out_fd = INVALID_FD;
5721 int err_fd = INVALID_FD;
5722
5723 if (!(use_file_for_in || use_null_for_in))
5724 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5725
5726 if (!(use_file_for_out || use_null_for_out))
5727 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5728
5729 // When using pty_master_fd only set it for stdout, do not duplicate
5730 // it for stderr, it only needs to be read once.
5731 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5732 {
5733 if (fd_err[0] >= 0)
5734 err_fd = fd_err[0];
5735 else if (out_fd != pty_master_fd)
5736 err_fd = pty_master_fd;
5737 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005738
5739 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005740 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005741 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005742 else
5743 {
5744 if (fd_in[1] >= 0)
5745 close(fd_in[1]);
5746 if (fd_out[0] >= 0)
5747 close(fd_out[0]);
5748 if (fd_err[0] >= 0)
5749 close(fd_err[0]);
5750 if (pty_master_fd >= 0)
5751 close(pty_master_fd);
5752 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005753
Bram Moolenaar0f873732019-12-05 20:28:46 +01005754 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005755 return;
5756
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005757failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005758 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005759 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005760 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005761 if (fd_in[1] >= 0)
5762 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005763 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005764 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005765 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005766 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005767 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005768 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005769 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005770 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005771 if (pty_master_fd >= 0)
5772 close(pty_master_fd);
5773 if (pty_slave_fd >= 0)
5774 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005775}
5776
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005777 static char_u *
5778get_signal_name(int sig)
5779{
5780 int i;
5781 char_u numbuf[NUMBUFLEN];
5782
5783 if (sig == SIGKILL)
5784 return vim_strsave((char_u *)"kill");
5785
5786 for (i = 0; signal_info[i].sig != -1; i++)
5787 if (sig == signal_info[i].sig)
5788 return strlow_save((char_u *)signal_info[i].name);
5789
5790 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5791 return vim_strsave(numbuf);
5792}
5793
Bram Moolenaar835dc632016-02-07 14:27:38 +01005794 char *
5795mch_job_status(job_T *job)
5796{
5797# ifdef HAVE_UNION_WAIT
5798 union wait status;
5799# else
5800 int status = -1;
5801# endif
5802 pid_t wait_pid = 0;
5803
5804# ifdef __NeXT__
5805 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5806# else
5807 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5808# endif
5809 if (wait_pid == -1)
5810 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005811 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005812 if (job->jv_status < JOB_ENDED)
5813 ch_log(job->jv_channel, "Job no longer exists: %s",
5814 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005815 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005816 }
5817 if (wait_pid == 0)
5818 return "run";
5819 if (WIFEXITED(status))
5820 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005821 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005822 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005823 if (job->jv_status < JOB_ENDED)
5824 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005825 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005826 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005827 if (WIFSIGNALED(status))
5828 {
5829 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005830 job->jv_termsig = get_signal_name(WTERMSIG(status));
5831 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5832 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5833 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005834 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005835 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005836 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005837
5838return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005839 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005840 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005841 return "dead";
5842}
5843
5844 job_T *
5845mch_detect_ended_job(job_T *job_list)
5846{
5847# ifdef HAVE_UNION_WAIT
5848 union wait status;
5849# else
5850 int status = -1;
5851# endif
5852 pid_t wait_pid = 0;
5853 job_T *job;
5854
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005855# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005856 // Do not do this when waiting for a shell command to finish, we would get
5857 // the exit value here (and discard it), the exit value obtained there
5858 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005859 if (dont_check_job_ended > 0)
5860 return NULL;
5861# endif
5862
Bram Moolenaar97792de2016-10-15 18:36:49 +02005863# ifdef __NeXT__
5864 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5865# else
5866 wait_pid = waitpid(-1, &status, WNOHANG);
5867# endif
5868 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005869 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005870 return NULL;
5871 for (job = job_list; job != NULL; job = job->jv_next)
5872 {
5873 if (job->jv_pid == wait_pid)
5874 {
5875 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005876 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005877 job->jv_exitval = WEXITSTATUS(status);
5878 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005879 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005880 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005881 job->jv_termsig = get_signal_name(WTERMSIG(status));
5882 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005883 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005884 {
5885 ch_log(job->jv_channel, "Job ended");
5886 job->jv_status = JOB_ENDED;
5887 }
5888 return job;
5889 }
5890 }
5891 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005892}
5893
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005894/*
5895 * Send a (deadly) signal to "job".
5896 * Return FAIL if "how" is not a valid name.
5897 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005898 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005899mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005900{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005901 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005902
Bram Moolenaar923d9262016-02-25 20:56:01 +01005903 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005904 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005905 else if (STRCMP(how, "hup") == 0)
5906 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005907 else if (STRCMP(how, "quit") == 0)
5908 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005909 else if (STRCMP(how, "int") == 0)
5910 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005911 else if (STRCMP(how, "kill") == 0)
5912 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005913#ifdef SIGWINCH
5914 else if (STRCMP(how, "winch") == 0)
5915 sig = SIGWINCH;
5916#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005917 else if (isdigit(*how))
5918 sig = atoi((char *)how);
5919 else
5920 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005921
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005922 // Never kill ourselves!
5923 if (job->jv_pid != 0)
5924 {
5925 // TODO: have an option to only kill the process, not the group?
5926 kill(-job->jv_pid, sig);
5927 kill(job->jv_pid, sig);
5928 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005929
Bram Moolenaar835dc632016-02-07 14:27:38 +01005930 return OK;
5931}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005932
5933/*
5934 * Clear the data related to "job".
5935 */
5936 void
5937mch_clear_job(job_T *job)
5938{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005939 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005940# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005941 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005942# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005943 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005944# endif
5945}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005946#endif
5947
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005948#if defined(FEAT_TERMINAL) || defined(PROTO)
5949 int
5950mch_create_pty_channel(job_T *job, jobopt_T *options)
5951{
5952 int pty_master_fd = -1;
5953 int pty_slave_fd = -1;
5954 channel_T *channel;
5955
Bram Moolenaar59386482019-02-10 22:43:46 +01005956 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02005957 if (pty_master_fd < 0 || pty_slave_fd < 0)
5958 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005959 close(pty_slave_fd);
5960
5961 channel = add_channel();
5962 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005963 {
5964 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005965 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005966 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005967 if (job->jv_tty_out != NULL)
5968 ch_log(channel, "using pty %s on fd %d",
5969 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005970 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005971 channel->ch_keep_open = TRUE;
5972
Bram Moolenaar0f873732019-12-05 20:28:46 +01005973 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5974 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005975 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005976 channel_set_job(channel, job, options);
5977 return OK;
5978}
5979#endif
5980
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981/*
5982 * Check for CTRL-C typed by reading all available characters.
5983 * In cooked mode we should get SIGINT, no need to check.
5984 */
5985 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005986mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987{
Bram Moolenaar26e86442020-05-17 14:06:16 +02005988 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005989 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 fill_input_buf(FALSE);
5991}
5992
5993/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005994 * Wait "msec" msec until a character is available from the mouse, keyboard,
5995 * from inbuf[].
5996 * "msec" == -1 will block forever.
5997 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005998 * When "ignore_input" is TRUE even check for pending input when input is
5999 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006000 * "interrupted" (if not NULL) is set to TRUE when no character is available
6001 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006002 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006003 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 */
6005 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006006WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006008#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006009 return ui_wait_for_chars_or_timer(
6010 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006011#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006012 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006013#endif
6014}
6015
6016/*
6017 * Wait "msec" msec until a character is available from the mouse or keyboard
6018 * or from inbuf[].
6019 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006020 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006021 * "interrupted" (if not NULL) is set to TRUE when no character is available
6022 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006023 * When a GUI is being used, this will never get called -- webb
6024 */
6025 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006026WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006027{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028#ifdef FEAT_MOUSE_GPM
6029 int gpm_process_wanted;
6030#endif
6031#ifdef FEAT_XCLIPBOARD
6032 int rest;
6033#endif
6034 int avail;
6035
Bram Moolenaar0f873732019-12-05 20:28:46 +01006036 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 return 1;
6038
6039#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006040 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 if (WantQueryMouse)
6042 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006043 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006044 if (!no_query_mouse_for_testing)
6045 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 }
6047#endif
6048
6049 /*
6050 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6051 * events. This is a bit complicated, because they might both be defined.
6052 */
6053#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6054# ifdef FEAT_XCLIPBOARD
6055 rest = 0;
6056 if (do_xterm_trace())
6057 rest = msec;
6058# endif
6059 do
6060 {
6061# ifdef FEAT_XCLIPBOARD
6062 if (rest != 0)
6063 {
6064 msec = XT_TRACE_DELAY;
6065 if (rest >= 0 && rest < XT_TRACE_DELAY)
6066 msec = rest;
6067 if (rest >= 0)
6068 rest -= msec;
6069 }
6070# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006071# ifdef FEAT_SOUND_CANBERRA
6072 // Invoke any pending sound callbacks.
6073 if (has_sound_callback_in_queue())
6074 invoke_sound_callback();
6075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076# ifdef FEAT_MOUSE_GPM
6077 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006078 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006079 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006080 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006082 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006084# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006086 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 return 1;
6088# ifdef FEAT_XCLIPBOARD
6089 if (rest == 0 || !do_xterm_trace())
6090# endif
6091 break;
6092 }
6093 }
6094 while (FALSE
6095# ifdef FEAT_MOUSE_GPM
6096 || (gpm_process_wanted && mch_gpm_process() == 0)
6097# endif
6098# ifdef FEAT_XCLIPBOARD
6099 || (!avail && rest != 0)
6100# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006101 )
6102 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103
6104#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006105 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106#endif
6107 return avail;
6108}
6109
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006110#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111/*
6112 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006113 * "msec" == 0 will check for characters once.
6114 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006117 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006118 * "interrupted" (if not NULL) is set to TRUE when no character is available
6119 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006121 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006122RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123{
6124 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006125 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006126#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 static int busy = FALSE;
6128
Bram Moolenaar0f873732019-12-05 20:28:46 +01006129 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130# define MAY_LOOP
6131
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006132# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006133 // Remember at what time we started, so that we know how much longer we
6134 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006135 long start_msec = msec;
6136 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006138 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006139 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140# endif
6141
Bram Moolenaar0f873732019-12-05 20:28:46 +01006142 // Handle being called recursively. This may happen for the session
6143 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (busy)
6145 return 0;
6146#endif
6147
6148#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006149 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150#endif
6151 {
6152#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006153 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006154# ifdef FEAT_MZSCHEME
6155 int mzquantum_used = FALSE;
6156# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157#endif
6158#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006159 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006160 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 int nfd;
6162# ifdef FEAT_XCLIPBOARD
6163 int xterm_idx = -1;
6164# endif
6165# ifdef FEAT_MOUSE_GPM
6166 int gpm_idx = -1;
6167# endif
6168# ifdef USE_XSMP
6169 int xsmp_idx = -1;
6170# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006171 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006173# ifdef FEAT_MZSCHEME
6174 mzvim_check_threads();
6175 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6176 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006177 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006178 mzquantum_used = TRUE;
6179 }
6180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 fds[0].fd = fd;
6182 fds[0].events = POLLIN;
6183 nfd = 1;
6184
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006186 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 if (xterm_Shell != (Widget)0)
6188 {
6189 xterm_idx = nfd;
6190 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6191 fds[nfd].events = POLLIN;
6192 nfd++;
6193 }
6194# endif
6195# ifdef FEAT_MOUSE_GPM
6196 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6197 {
6198 gpm_idx = nfd;
6199 fds[nfd].fd = gpm_fd;
6200 fds[nfd].events = POLLIN;
6201 nfd++;
6202 }
6203# endif
6204# ifdef USE_XSMP
6205 if (xsmp_icefd != -1)
6206 {
6207 xsmp_idx = nfd;
6208 fds[nfd].fd = xsmp_icefd;
6209 fds[nfd].events = POLLIN;
6210 nfd++;
6211 }
6212# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006213#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006214 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006215#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006216 if (interrupted != NULL)
6217 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006219 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006220
6221 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006222 if (result == 0 && interrupted != NULL && ret > 0)
6223 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006224
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006225# ifdef FEAT_MZSCHEME
6226 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006227 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006228 finished = FALSE;
6229# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231# ifdef FEAT_XCLIPBOARD
6232 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6233 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006234 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006236 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 finished = FALSE;
6238 }
6239# endif
6240# ifdef FEAT_MOUSE_GPM
6241 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243# endif
6244# ifdef USE_XSMP
6245 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6246 {
6247 if (fds[xsmp_idx].revents & POLLIN)
6248 {
6249 busy = TRUE;
6250 xsmp_handle_requests();
6251 busy = FALSE;
6252 }
6253 else if (fds[xsmp_idx].revents & POLLHUP)
6254 {
6255 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006256 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 xsmp_close();
6258 }
6259 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006260 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 }
6262# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006263#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006264 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006265 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006266 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268
Bram Moolenaar0f873732019-12-05 20:28:46 +01006269#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
6271 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006272 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006273 // These are static because they can take 8 Kbyte each and cause the
6274 // signal stack to run out with -O3.
6275 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006277 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006279# ifdef FEAT_MZSCHEME
6280 mzvim_check_threads();
6281 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6282 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006283 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006284 mzquantum_used = TRUE;
6285 }
6286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006288 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006290 tv.tv_sec = towait / 1000;
6291 tv.tv_usec = (towait % 1000) * (1000000/1000);
6292 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006294 else
6295 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296
6297 /*
6298 * Select on ready for reading and exceptional condition (end of file).
6299 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006300select_eintr:
6301 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006302 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 FD_ZERO(&efds);
6304 FD_SET(fd, &rfds);
6305# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006306 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 FD_SET(fd, &efds);
6308# endif
6309 maxfd = fd;
6310
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006312 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 if (xterm_Shell != (Widget)0)
6314 {
6315 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6316 if (maxfd < ConnectionNumber(xterm_dpy))
6317 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006318
Bram Moolenaar0f873732019-12-05 20:28:46 +01006319 // An event may have already been read but not handled. In
6320 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006321 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 }
6323# endif
6324# ifdef FEAT_MOUSE_GPM
6325 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6326 {
6327 FD_SET(gpm_fd, &rfds);
6328 FD_SET(gpm_fd, &efds);
6329 if (maxfd < gpm_fd)
6330 maxfd = gpm_fd;
6331 }
6332# endif
6333# ifdef USE_XSMP
6334 if (xsmp_icefd != -1)
6335 {
6336 FD_SET(xsmp_icefd, &rfds);
6337 FD_SET(xsmp_icefd, &efds);
6338 if (maxfd < xsmp_icefd)
6339 maxfd = xsmp_icefd;
6340 }
6341# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006342# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006343 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006344# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006345 if (interrupted != NULL)
6346 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347
Bram Moolenaar643b6142018-09-12 20:29:09 +02006348 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6349 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006350 result = ret > 0 && FD_ISSET(fd, &rfds);
6351 if (result)
6352 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006353 else if (interrupted != NULL && ret > 0)
6354 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006355
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006356# ifdef EINTR
6357 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006358 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006359 // Check whether window has been resized, EINTR may be caused by
6360 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006361 if (do_resize)
6362 handle_resize();
6363
Bram Moolenaar0f873732019-12-05 20:28:46 +01006364 // Interrupted by a signal, need to try again. We ignore msec
6365 // here, because we do want to check even after a timeout if
6366 // characters are available. Needed for reading output of an
6367 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006368 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006369 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006370# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006371# ifdef __TANDEM
6372 if (ret == -1 && errno == ENOTSUP)
6373 {
6374 FD_ZERO(&rfds);
6375 FD_ZERO(&efds);
6376 ret = 0;
6377 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006378# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006379# ifdef FEAT_MZSCHEME
6380 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006381 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006382 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383# endif
6384
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385# ifdef FEAT_XCLIPBOARD
6386 if (ret > 0 && xterm_Shell != (Widget)0
6387 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6388 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006389 xterm_update(); // Maybe we should hand out clipboard
6390 // continue looping when we only got the X event and the input
6391 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 if (--ret == 0 && !input_available())
6393 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006394 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 finished = FALSE;
6396 }
6397 }
6398# endif
6399# ifdef FEAT_MOUSE_GPM
6400 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6401 {
6402 if (FD_ISSET(gpm_fd, &efds))
6403 gpm_close();
6404 else if (FD_ISSET(gpm_fd, &rfds))
6405 *check_for_gpm = 1;
6406 }
6407# endif
6408# ifdef USE_XSMP
6409 if (ret > 0 && xsmp_icefd != -1)
6410 {
6411 if (FD_ISSET(xsmp_icefd, &efds))
6412 {
6413 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006414 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 xsmp_close();
6416 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006417 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419 else if (FD_ISSET(xsmp_icefd, &rfds))
6420 {
6421 busy = TRUE;
6422 xsmp_handle_requests();
6423 busy = FALSE;
6424 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006425 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 }
6427 }
6428# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006429#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006430 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006431 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006432 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434
Bram Moolenaar0f873732019-12-05 20:28:46 +01006435#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436
6437#ifdef MAY_LOOP
6438 if (finished || msec == 0)
6439 break;
6440
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006441# ifdef FEAT_CLIENTSERVER
6442 if (server_waiting())
6443 break;
6444# endif
6445
Bram Moolenaar0f873732019-12-05 20:28:46 +01006446 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 if (msec > 0)
6448 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006449# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006450 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006451 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006453 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 msec = msec / 2;
6455# endif
6456 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006457 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 }
6459#endif
6460 }
6461
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006462 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463}
6464
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006466 * Expand a path into all matching files and/or directories. Handles "*",
6467 * "?", "[a-z]", "**", etc.
6468 * "path" has backslashes before chars that are not to be expanded.
6469 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 */
6471 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006472mch_expandpath(
6473 garray_T *gap,
6474 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006475 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476{
Bram Moolenaar02743632005-07-25 20:42:36 +00006477 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479
6480/*
6481 * mch_expand_wildcards() - this code does wild-card pattern matching using
6482 * the shell
6483 *
6484 * return OK for success, FAIL for error (you may lose some memory) and put
6485 * an error message in *file.
6486 *
6487 * num_pat is number of input patterns
6488 * pat is array of pointers to input patterns
6489 * num_file is pointer to number of matched file names
6490 * file is pointer to array of pointers to matched file names
6491 */
6492
6493#ifndef SEEK_SET
6494# define SEEK_SET 0
6495#endif
6496#ifndef SEEK_END
6497# define SEEK_END 2
6498#endif
6499
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006500#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006501
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006503mch_expand_wildcards(
6504 int num_pat,
6505 char_u **pat,
6506 int *num_file,
6507 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006508 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
6510 int i;
6511 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006512 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 char_u *p;
6514 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
Bram Moolenaarc7247912008-01-13 12:54:11 +00006516 /*
6517 * This is the non-OS/2 implementation (really Unix).
6518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 int j;
6520 char_u *tempname;
6521 char_u *command;
6522 FILE *fd;
6523 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006524#define STYLE_ECHO 0 // use "echo", the default
6525#define STYLE_GLOB 1 // use "glob", for csh
6526#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6527#define STYLE_PRINT 3 // use "print -N", for zsh
6528#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6529 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 int shell_style = STYLE_ECHO;
6531 int check_spaces;
6532 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006533 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006534 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006535 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536
Bram Moolenaar0f873732019-12-05 20:28:46 +01006537 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 *file = NULL;
6539
6540 /*
6541 * If there are no wildcards, just copy the names to allocated memory.
6542 * Saves a lot of time, because we don't have to start a new shell.
6543 */
6544 if (!have_wildcard(num_pat, pat))
6545 return save_patterns(num_pat, pat, num_file, file);
6546
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006547# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006548 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006549 if (sandbox != 0 && check_secure())
6550 return FAIL;
6551# endif
6552
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 /*
6554 * Don't allow the use of backticks in secure and restricted mode.
6555 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006556 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 for (i = 0; i < num_pat; ++i)
6558 if (vim_strchr(pat[i], '`') != NULL
6559 && (check_restricted() || check_secure()))
6560 return FAIL;
6561
6562 /*
6563 * get a name for the temp file
6564 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006565 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006567 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 return FAIL;
6569 }
6570
6571 /*
6572 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006573 * file.
6574 * STYLE_BT: NL separated
6575 * If expanding `cmd` execute it directly.
6576 * STYLE_GLOB: NUL separated
6577 * If we use *csh, "glob" will work better than "echo".
6578 * STYLE_PRINT: NL or NUL separated
6579 * If we use *zsh, "print -N" will work better than "glob".
6580 * STYLE_VIMGLOB: NL separated
6581 * If we use *sh*, we define "vimglob()".
6582 * STYLE_ECHO: space separated.
6583 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 */
6585 if (num_pat == 1 && *pat[0] == '`'
6586 && (len = STRLEN(pat[0])) > 2
6587 && *(pat[0] + len - 1) == '`')
6588 shell_style = STYLE_BT;
6589 else if ((len = STRLEN(p_sh)) >= 3)
6590 {
6591 if (STRCMP(p_sh + len - 3, "csh") == 0)
6592 shell_style = STYLE_GLOB;
6593 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6594 shell_style = STYLE_PRINT;
6595 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006596 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6597 "sh") != NULL)
6598 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599
Bram Moolenaar0f873732019-12-05 20:28:46 +01006600 // Compute the length of the command. We need 2 extra bytes: for the
6601 // optional '&' and for the NUL.
6602 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006604 if (shell_style == STYLE_VIMGLOB)
6605 len += STRLEN(sh_vimglob_func);
6606
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006607 for (i = 0; i < num_pat; ++i)
6608 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006609 // Count the length of the patterns in the same way as they are put in
6610 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006611#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006612 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006613#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006614 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006615 for (j = 0; pat[i][j] != NUL; ++j)
6616 {
6617 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006618 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006619 ++len;
6620 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006621#endif
6622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 command = alloc(len);
6624 if (command == NULL)
6625 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006626 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 vim_free(tempname);
6628 return FAIL;
6629 }
6630
6631 /*
6632 * Build the shell command:
6633 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6634 * recognizes this).
6635 * - Add the shell command to print the expanded names.
6636 * - Add the temp file name.
6637 * - Add the file name patterns.
6638 */
6639 if (shell_style == STYLE_BT)
6640 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006641 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006642 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006643 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006645 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006646 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006648 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006650 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 *p = ' ';
6652 }
6653 STRCAT(command, ">");
6654 }
6655 else
6656 {
6657 if (flags & EW_NOTFOUND)
6658 STRCPY(command, "set nonomatch; ");
6659 else
6660 STRCPY(command, "unset nonomatch; ");
6661 if (shell_style == STYLE_GLOB)
6662 STRCAT(command, "glob >");
6663 else if (shell_style == STYLE_PRINT)
6664 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006665 else if (shell_style == STYLE_VIMGLOB)
6666 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 else
6668 STRCAT(command, "echo >");
6669 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006670
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006672
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 if (shell_style != STYLE_BT)
6674 for (i = 0; i < num_pat; ++i)
6675 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006676 // When using system() always add extra quotes, because the shell
6677 // is started twice. Otherwise put a backslash before special
6678 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679#ifdef USE_SYSTEM
6680 STRCAT(command, " \"");
6681 STRCAT(command, pat[i]);
6682 STRCAT(command, "\"");
6683#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006684 int intick = FALSE;
6685
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 p = command + STRLEN(command);
6687 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006688 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006689 {
6690 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006691 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006692 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6693 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006694 // Remove a backslash, take char literally. But keep
6695 // backslash inside backticks, before a special character
6696 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006697 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006698 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6699 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006700 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006701 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006702 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006703 else if (!intick
6704 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6705 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006706 // Put a backslash before a special character, but not
6707 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6708 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006709 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006710
Bram Moolenaar0f873732019-12-05 20:28:46 +01006711 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006712 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 *p = NUL;
6715#endif
6716 }
6717 if (flags & EW_SILENT)
6718 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006719 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006720 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721
6722 /*
6723 * Using zsh -G: If a pattern has no matches, it is just deleted from
6724 * the argument list, otherwise zsh gives an error message and doesn't
6725 * expand any other pattern.
6726 */
6727 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006728 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
6730 /*
6731 * If we use -f then shell variables set in .cshrc won't get expanded.
6732 * vi can do it, so we will too, but it is only necessary if there is a "$"
6733 * in one of the patterns, otherwise we can still use the fast option.
6734 */
6735 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006736 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737
6738 /*
6739 * execute the shell command
6740 */
6741 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6742
Bram Moolenaar0f873732019-12-05 20:28:46 +01006743 // When running in the background, give it some time to create the temp
6744 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006745 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006746 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747
Bram Moolenaar0f873732019-12-05 20:28:46 +01006748 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 show_shell_mess = TRUE;
6750 vim_free(command);
6751
Bram Moolenaar0f873732019-12-05 20:28:46 +01006752 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 {
6754 mch_remove(tempname);
6755 vim_free(tempname);
6756 /*
6757 * With interactive completion, the error message is not printed.
6758 * However with USE_SYSTEM, I don't know how to turn off error messages
6759 * from the shell, so screen may still get messed up -- webb.
6760 */
6761#ifndef USE_SYSTEM
6762 if (!(flags & EW_SILENT))
6763#endif
6764 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006765 redraw_later_clear(); // probably messed up screen
6766 msg_putchar('\n'); // clear bottom line quickly
6767 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768#ifdef USE_SYSTEM
6769 if (!(flags & EW_SILENT))
6770#endif
6771 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006772 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006773 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 }
6775 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006776 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6777 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 if (shell_style == STYLE_BT)
6779 return FAIL;
6780 goto notfound;
6781 }
6782
6783 /*
6784 * read the names from the file into memory
6785 */
6786 fd = fopen((char *)tempname, READBIN);
6787 if (fd == NULL)
6788 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006789 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 if (!(flags & EW_SILENT))
6791 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006792 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006793 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 }
6795 vim_free(tempname);
6796 goto notfound;
6797 }
6798 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006799 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006801 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006802 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006803 buffer = NULL;
6804 else
6805 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (buffer == NULL)
6807 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006808 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 mch_remove(tempname);
6810 vim_free(tempname);
6811 fclose(fd);
6812 return FAIL;
6813 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006814 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 i = fread((char *)buffer, 1, len, fd);
6816 fclose(fd);
6817 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006818 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006820 // unexpected read error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006821 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 vim_free(tempname);
6823 vim_free(buffer);
6824 return FAIL;
6825 }
6826 vim_free(tempname);
6827
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006828# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006829 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006831 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6833 *p++ = buffer[i];
6834 len = p - buffer;
6835# endif
6836
6837
Bram Moolenaar0f873732019-12-05 20:28:46 +01006838 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 if (shell_style == STYLE_ECHO)
6840 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006841 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006843 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 {
6845 while (*p != ' ' && *p != '\n')
6846 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006847 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 }
6849 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006850 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006851 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006853 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006855 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 {
6857 while (*p != '\n' && *p != NUL)
6858 ++p;
6859 if (*p != NUL)
6860 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006861 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 }
6863 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006864 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 else
6866 {
6867 /*
6868 * Some versions of zsh use spaces instead of NULs to separate
6869 * results. Only do this when there is no NUL before the end of the
6870 * buffer, otherwise we would never be able to use file names with
6871 * embedded spaces when zsh does use NULs.
6872 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6873 * don't check for spaces again.
6874 */
6875 check_spaces = FALSE;
6876 if (shell_style == STYLE_PRINT && !did_find_nul)
6877 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006878 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006879 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006880 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 did_find_nul = TRUE;
6882 else
6883 check_spaces = TRUE;
6884 }
6885
6886 /*
6887 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6888 * already is one, for STYLE_GLOB it needs to be added.
6889 */
6890 if (len && buffer[len - 1] == NUL)
6891 --len;
6892 else
6893 buffer[len] = NUL;
6894 i = 0;
6895 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006896 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 {
6898 ++i;
6899 *p = NUL;
6900 }
6901 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006902 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 }
6904 if (i == 0)
6905 {
6906 /*
6907 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6908 * /bin/sh will happily expand it to nothing rather than returning an
6909 * error; and hey, it's good to check anyway -- webb.
6910 */
6911 vim_free(buffer);
6912 goto notfound;
6913 }
6914 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006915 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 if (*file == NULL)
6917 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006918 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 vim_free(buffer);
6920 return FAIL;
6921 }
6922
6923 /*
6924 * Isolate the individual file names.
6925 */
6926 p = buffer;
6927 for (i = 0; i < *num_file; ++i)
6928 {
6929 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006930 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006931 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6932 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006934 while (!(shell_style == STYLE_ECHO && *p == ' ')
6935 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006937 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 *p = NUL;
6939 else
6940 {
6941 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006942 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
6944 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006945 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006947 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006949 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 }
6951 }
6952
6953 /*
6954 * Move the file names to allocated memory.
6955 */
6956 for (j = 0, i = 0; i < *num_file; ++i)
6957 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006958 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6960 continue;
6961
Bram Moolenaar0f873732019-12-05 20:28:46 +01006962 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 dir = (mch_isdir((*file)[i]));
6964 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6965 continue;
6966
Bram Moolenaar0f873732019-12-05 20:28:46 +01006967 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01006968 if (!dir && (flags & EW_EXEC)
6969 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006970 continue;
6971
Bram Moolenaar964b3742019-05-24 18:54:09 +02006972 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 if (p)
6974 {
6975 STRCPY(p, (*file)[i]);
6976 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006977 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 (*file)[j++] = p;
6979 }
6980 }
6981 vim_free(buffer);
6982 *num_file = j;
6983
Bram Moolenaar0f873732019-12-05 20:28:46 +01006984 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006986 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 goto notfound;
6988 }
6989
6990 return OK;
6991
6992notfound:
6993 if (flags & EW_NOTFOUND)
6994 return save_patterns(num_pat, pat, num_file, file);
6995 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996}
6997
Bram Moolenaar0f873732019-12-05 20:28:46 +01006998#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007001save_patterns(
7002 int num_pat,
7003 char_u **pat,
7004 int *num_file,
7005 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
7007 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007008 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007010 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 if (*file == NULL)
7012 return FAIL;
7013 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007014 {
7015 s = vim_strsave(pat[i]);
7016 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007017 // Be compatible with expand_filename(): halve the number of
7018 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007019 backslash_halve(s);
7020 (*file)[i] = s;
7021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 *num_file = num_pat;
7023 return OK;
7024}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026/*
7027 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7028 * expand.
7029 */
7030 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007031mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007033 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 if (*p == '\\' && p[1] != NUL)
7036 ++p;
7037 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 if (vim_strchr((char_u *)
7039#ifdef VMS
7040 "*?%"
7041#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043#endif
7044 , *p) != NULL)
7045 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 }
7047 return FALSE;
7048}
7049
7050/*
7051 * Return TRUE if the string "p" contains a wildcard.
7052 * Don't recognize '~' at the end as a wildcard.
7053 */
7054 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007055mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007057 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 if (*p == '\\' && p[1] != NUL)
7060 ++p;
7061 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 if (vim_strchr((char_u *)
7063#ifdef VMS
7064 "*?%$"
7065#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067#endif
7068 , *p) != NULL
7069 || (*p == '~' && p[1] != NUL))
7070 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 }
7072 return FALSE;
7073}
7074
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007076have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077{
7078 int i;
7079
7080 for (i = 0; i < num; i++)
7081 if (mch_has_wildcard(file[i]))
7082 return 1;
7083 return 0;
7084}
7085
7086 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007087have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088{
7089 int i;
7090
7091 for (i = 0; i < num; i++)
7092 if (vim_strchr(file[i], '$') != NULL)
7093 return TRUE;
7094 return FALSE;
7095}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007097#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098/*
7099 * Scaled-down version of rename(), which is missing in Xenix.
7100 * This version can only move regular files and will fail if the
7101 * destination exists.
7102 */
7103 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007104mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
7106 struct stat st;
7107
Bram Moolenaar0f873732019-12-05 20:28:46 +01007108 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007110 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007112 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 return 0;
7114 return -1;
7115}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007116#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007118#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119/*
7120 * Initializes connection with gpm (if it isn't already opened)
7121 * Return 1 if succeeded (or connection already opened), 0 if failed
7122 */
7123 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007124gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007126 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127
7128 if (!gpm_flag)
7129 {
7130 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7131 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007132 // Default handling for mouse move
7133 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 gpm_connect.maxMod = 0xffff;
7135 if (Gpm_Open(&gpm_connect, 0) > 0)
7136 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007137 // gpm library tries to handling TSTP causes
7138 // problems. Anyways, we close connection to Gpm whenever
7139 // we are going to suspend or starting an external process
7140 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007141# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007143# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007144 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 }
7146 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007147 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 return 0;
7149 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007150 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151}
7152
7153/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007154 * Returns TRUE if the GPM mouse is enabled.
7155 */
7156 int
7157gpm_enabled(void)
7158{
7159 return gpm_flag && gpm_fd >= 0;
7160}
7161
7162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 */
7165 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007166gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007168 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 Gpm_Close();
7170}
7171
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007172/*
7173 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007175 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 */
7177 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007178mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179{
7180 int button;
7181 static Gpm_Event gpm_event;
7182 char_u string[6];
7183 int_u vim_modifiers;
7184 int row,col;
7185 unsigned char buttons_mask;
7186 unsigned char gpm_modifiers;
7187 static unsigned char old_buttons = 0;
7188
7189 Gpm_GetEvent(&gpm_event);
7190
7191#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007192 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 if (hold_gui_events)
7194 return 0;
7195#endif
7196
7197 row = gpm_event.y - 1;
7198 col = gpm_event.x - 1;
7199
Bram Moolenaar0f873732019-12-05 20:28:46 +01007200 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 string[1] = 'M';
7202 string[2] = 'G';
7203 switch (GPM_BARE_EVENTS(gpm_event.type))
7204 {
7205 case GPM_DRAG:
7206 string[3] = MOUSE_DRAG;
7207 break;
7208 case GPM_DOWN:
7209 buttons_mask = gpm_event.buttons & ~old_buttons;
7210 old_buttons = gpm_event.buttons;
7211 switch (buttons_mask)
7212 {
7213 case GPM_B_LEFT:
7214 button = MOUSE_LEFT;
7215 break;
7216 case GPM_B_MIDDLE:
7217 button = MOUSE_MIDDLE;
7218 break;
7219 case GPM_B_RIGHT:
7220 button = MOUSE_RIGHT;
7221 break;
7222 default:
7223 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007224 // Don't know what to do. Can more than one button be
7225 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 }
7227 string[3] = (char_u)(button | 0x20);
7228 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7229 break;
7230 case GPM_UP:
7231 string[3] = MOUSE_RELEASE;
7232 old_buttons &= ~gpm_event.buttons;
7233 break;
7234 default:
7235 return 0;
7236 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007237 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 gpm_modifiers = gpm_event.modifiers;
7239 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007240 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7241 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7242 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7244 vim_modifiers |= MOUSE_SHIFT;
7245
7246 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7247 vim_modifiers |= MOUSE_CTRL;
7248 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7249 vim_modifiers |= MOUSE_ALT;
7250 string[3] |= vim_modifiers;
7251 string[4] = (char_u)(col + ' ' + 1);
7252 string[5] = (char_u)(row + ' ' + 1);
7253 add_to_input_buf(string, 6);
7254 return 6;
7255}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007256#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007258#ifdef FEAT_SYSMOUSE
7259/*
7260 * Initialize connection with sysmouse.
7261 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7262 * output, any sysmouse output than will be processed via sig_sysmouse().
7263 * Return OK if succeeded, FAIL if failed.
7264 */
7265 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007266sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007267{
7268 struct mouse_info mouse;
7269
7270 mouse.operation = MOUSE_MODE;
7271 mouse.u.mode.mode = 0;
7272 mouse.u.mode.signal = SIGUSR2;
7273 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7274 {
7275 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7276 mouse.operation = MOUSE_SHOW;
7277 ioctl(1, CONS_MOUSECTL, &mouse);
7278 return OK;
7279 }
7280 return FAIL;
7281}
7282
7283/*
7284 * Stop processing SIGUSR2 signals, and also make sure that
7285 * virtual console do not send us any sysmouse related signal.
7286 */
7287 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007288sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007289{
7290 struct mouse_info mouse;
7291
7292 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7293 mouse.operation = MOUSE_MODE;
7294 mouse.u.mode.mode = 0;
7295 mouse.u.mode.signal = 0;
7296 ioctl(1, CONS_MOUSECTL, &mouse);
7297}
7298
7299/*
7300 * Gets info from sysmouse and adds special keys to input buf.
7301 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007302 static RETSIGTYPE
7303sig_sysmouse SIGDEFARG(sigarg)
7304{
7305 struct mouse_info mouse;
7306 struct video_info video;
7307 char_u string[6];
7308 int row, col;
7309 int button;
7310 int buttons;
7311 static int oldbuttons = 0;
7312
7313#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007314 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007315 if (hold_gui_events)
7316 return;
7317#endif
7318
7319 mouse.operation = MOUSE_GETINFO;
7320 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7321 && ioctl(1, FBIO_MODEINFO, &video) != -1
7322 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7323 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7324 {
7325 row = mouse.u.data.y / video.vi_cheight;
7326 col = mouse.u.data.x / video.vi_cwidth;
7327 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007328 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007329 string[1] = 'M';
7330 string[2] = 'S';
7331 if (oldbuttons == buttons && buttons != 0)
7332 {
7333 button = MOUSE_DRAG;
7334 }
7335 else
7336 {
7337 switch (buttons)
7338 {
7339 case 0:
7340 button = MOUSE_RELEASE;
7341 break;
7342 case 1:
7343 button = MOUSE_LEFT;
7344 break;
7345 case 2:
7346 button = MOUSE_MIDDLE;
7347 break;
7348 case 4:
7349 button = MOUSE_RIGHT;
7350 break;
7351 default:
7352 return;
7353 }
7354 oldbuttons = buttons;
7355 }
7356 string[3] = (char_u)(button);
7357 string[4] = (char_u)(col + ' ' + 1);
7358 string[5] = (char_u)(row + ' ' + 1);
7359 add_to_input_buf(string, 6);
7360 }
7361 return;
7362}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007363#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007364
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007366typedef char_u * (*STRPROCSTR)(char_u *);
7367typedef char_u * (*INTPROCSTR)(int);
7368typedef int (*STRPROCINT)(char_u *);
7369typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370
7371/*
7372 * Call a DLL routine which takes either a string or int param
7373 * and returns an allocated string.
7374 */
7375 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007376mch_libcall(
7377 char_u *libname,
7378 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007379 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007380 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007381 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007382 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383{
7384# if defined(USE_DLOPEN)
7385 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007386 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387# else
7388 shl_t hinstLib;
7389# endif
7390 STRPROCSTR ProcAdd;
7391 INTPROCSTR ProcAddI;
7392 char_u *retval_str = NULL;
7393 int retval_int = 0;
7394 int success = FALSE;
7395
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007396 /*
7397 * Get a handle to the DLL module.
7398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007400 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007401 (void)dlerror();
7402
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 hinstLib = dlopen((char *)libname, RTLD_LAZY
7404# ifdef RTLD_LOCAL
7405 | RTLD_LOCAL
7406# endif
7407 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007408 if (hinstLib == NULL)
7409 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007410 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007411 dlerr = (char *)dlerror();
7412 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007413 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415# else
7416 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7417# endif
7418
Bram Moolenaar0f873732019-12-05 20:28:46 +01007419 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 if (hinstLib != NULL)
7421 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007422# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 /*
7424 * Catch a crash when calling the library function. For example when
7425 * using a number where a string pointer is expected.
7426 */
7427 mch_startjmp();
7428 if (SETJMP(lc_jump_env) != 0)
7429 {
7430 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007431# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007432 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 mch_didjmp();
7435 }
7436 else
7437# endif
7438 {
7439 retval_str = NULL;
7440 retval_int = 0;
7441
7442 if (argstring != NULL)
7443 {
7444# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007445 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007446 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447# else
7448 if (shl_findsym(&hinstLib, (const char *)funcname,
7449 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7450 ProcAdd = NULL;
7451# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007452 if ((success = (ProcAdd != NULL
7453# if defined(USE_DLOPEN)
7454 && dlerr == NULL
7455# endif
7456 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 {
7458 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007459 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 else
7461 retval_str = (ProcAdd)(argstring);
7462 }
7463 }
7464 else
7465 {
7466# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007467 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007468 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469# else
7470 if (shl_findsym(&hinstLib, (const char *)funcname,
7471 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7472 ProcAddI = NULL;
7473# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007474 if ((success = (ProcAddI != NULL
7475# if defined(USE_DLOPEN)
7476 && dlerr == NULL
7477# endif
7478 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 {
7480 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007481 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 else
7483 retval_str = (ProcAddI)(argint);
7484 }
7485 }
7486
Bram Moolenaar0f873732019-12-05 20:28:46 +01007487 // Save the string before we free the library.
7488 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 if (string_result == NULL)
7490 *number_result = retval_int;
7491 else if (retval_str != NULL
7492 && retval_str != (char_u *)1
7493 && retval_str != (char_u *)-1)
7494 *string_result = vim_strsave(retval_str);
7495 }
7496
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007497# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 mch_endjmp();
7499# ifdef SIGHASARG
7500 if (lc_signal != 0)
7501 {
7502 int i;
7503
Bram Moolenaar0f873732019-12-05 20:28:46 +01007504 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 for (i = 0; signal_info[i].sig != -1; i++)
7506 if (lc_signal == signal_info[i].sig)
7507 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007508 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 }
7510# endif
7511# endif
7512
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007514 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007515 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007516 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007517
Bram Moolenaar0f873732019-12-05 20:28:46 +01007518 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 (void)dlclose(hinstLib);
7520# else
7521 (void)shl_unload(hinstLib);
7522# endif
7523 }
7524
7525 if (!success)
7526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007527 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 return FAIL;
7529 }
7530
7531 return OK;
7532}
7533#endif
7534
7535#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007536static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537static int xterm_button;
7538
7539/*
7540 * Setup a dummy window for X selections in a terminal.
7541 */
7542 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007543setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544{
7545 int z = 0;
7546 char *strp = "";
7547 Widget AppShell;
7548
7549 if (!x_connect_to_server())
7550 return;
7551
7552 open_app_context();
7553 if (app_context != NULL && xterm_Shell == (Widget)0)
7554 {
7555 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007556# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007558# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007559# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007560 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561
7562 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007563 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564# endif
7565
Bram Moolenaar0f873732019-12-05 20:28:46 +01007566 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 oldhandler = XSetErrorHandler(x_error_check);
7568
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007569# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007570 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7572 mch_startjmp();
7573 if (SETJMP(lc_jump_env) != 0)
7574 {
7575 mch_didjmp();
7576 xterm_dpy = NULL;
7577 }
7578 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007579# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 {
7581 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7582 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007583 if (xterm_dpy != NULL)
7584 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007585# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 }
7589
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007590# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007591 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007593# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007594 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 (void)XSetErrorHandler(oldhandler);
7596
7597 if (xterm_dpy == NULL)
7598 {
7599 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007600 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 return;
7602 }
7603
Bram Moolenaar0f873732019-12-05 20:28:46 +01007604 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 (void)XSetIOErrorHandler(x_IOerror_handler);
7606
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007607# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007609 {
7610 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007611 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007612 verbose_leave();
7613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614# endif
7615
Bram Moolenaar0f873732019-12-05 20:28:46 +01007616 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7618 applicationShellWidgetClass, xterm_dpy,
7619 NULL);
7620 if (AppShell == (Widget)0)
7621 return;
7622 xterm_Shell = XtVaCreatePopupShell("VIM",
7623 topLevelShellWidgetClass, AppShell,
7624 XtNmappedWhenManaged, 0,
7625 XtNwidth, 1,
7626 XtNheight, 1,
7627 NULL);
7628 if (xterm_Shell == (Widget)0)
7629 return;
7630
7631 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007632 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 if (x11_display == NULL)
7634 x11_display = xterm_dpy;
7635
7636 XtRealizeWidget(xterm_Shell);
7637 XSync(xterm_dpy, False);
7638 xterm_update();
7639 }
7640 if (xterm_Shell != (Widget)0)
7641 {
7642 clip_init(TRUE);
7643 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7644 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007645 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 if (test_x11_window(xterm_dpy) == FAIL)
7647 x11_window = 0;
7648 if (x11_window != 0)
7649 xterm_trace = 0;
7650 }
7651}
7652
7653 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007654start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655{
7656 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7657 return;
7658 xterm_trace = 1;
7659 xterm_button = button;
7660 do_xterm_trace();
7661}
7662
7663
7664 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007665stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666{
7667 if (xterm_trace < 0)
7668 return;
7669 xterm_trace = 0;
7670}
7671
7672/*
7673 * Query the xterm pointer and generate mouse termcodes if necessary
7674 * return TRUE if dragging is active, else FALSE
7675 */
7676 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007677do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
7679 Window root, child;
7680 int root_x, root_y;
7681 int win_x, win_y;
7682 int row, col;
7683 int_u mask_return;
7684 char_u buf[50];
7685 char_u *strp;
7686 long got_hints;
7687 static char_u *mouse_code;
7688 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7689 static int prev_row = 0, prev_col = 0;
7690 static XSizeHints xterm_hints;
7691
7692 if (xterm_trace <= 0)
7693 return FALSE;
7694
7695 if (xterm_trace == 1)
7696 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007697 // Get the hints just before tracking starts. The font size might
7698 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007699 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7700 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 || xterm_hints.width_inc <= 1
7702 || xterm_hints.height_inc <= 1)
7703 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007704 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 return FALSE;
7706 }
7707
Bram Moolenaar0f873732019-12-05 20:28:46 +01007708 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 mouse_code = find_termcode(mouse_name);
7710 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007711 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 xterm_trace = 2;
7713
Bram Moolenaar0f873732019-12-05 20:28:46 +01007714 // Find the offset of the chars, there might be a scrollbar on the
7715 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7717 &win_x, &win_y, &mask_return);
7718 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7719 - (xterm_hints.height_inc / 2);
7720 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7721 xterm_hints.y = 2;
7722 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7723 - (xterm_hints.width_inc / 2);
7724 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7725 xterm_hints.x = 2;
7726 return TRUE;
7727 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007728 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 {
7730 xterm_trace = 0;
7731 return FALSE;
7732 }
7733
7734 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7735 &win_x, &win_y, &mask_return);
7736
7737 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7738 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7739 if (row == prev_row && col == prev_col)
7740 return TRUE;
7741
7742 STRCPY(buf, mouse_code);
7743 strp = buf + STRLEN(buf);
7744 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7745 *strp++ = (char_u)(col + ' ' + 1);
7746 *strp++ = (char_u)(row + ' ' + 1);
7747 *strp = 0;
7748 add_to_input_buf(buf, STRLEN(buf));
7749
7750 prev_row = row;
7751 prev_col = col;
7752 return TRUE;
7753}
7754
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007755# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756/*
7757 * Destroy the display, window and app_context. Required for GTK.
7758 */
7759 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007760clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761{
7762 if (xterm_Shell != (Widget)0)
7763 {
7764 XtDestroyWidget(xterm_Shell);
7765 xterm_Shell = (Widget)0;
7766 }
7767 if (xterm_dpy != NULL)
7768 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007769# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007770 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007772# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 if (x11_display == xterm_dpy)
7774 x11_display = NULL;
7775 xterm_dpy = NULL;
7776 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007777# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 if (app_context != (XtAppContext)NULL)
7779 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007780 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 XtDestroyApplicationContext(app_context);
7782 app_context = (XtAppContext)NULL;
7783 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785}
7786# endif
7787
7788/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007789 * Catch up with GUI or X events.
7790 */
7791 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007792clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007793{
7794# ifdef FEAT_GUI
7795 if (gui.in_use)
7796 gui_mch_update();
7797 else
7798# endif
7799 if (xterm_Shell != (Widget)0)
7800 xterm_update();
7801}
7802
7803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 * Catch up with any queued X events. This may put keyboard input into the
7805 * input buffer, call resize call-backs, trigger timers etc. If there is
7806 * nothing in the X event queue (& no timers pending), then we return
7807 * immediately.
7808 */
7809 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007810xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811{
7812 XEvent event;
7813
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007814 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007816 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007818 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007819 break;
7820
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007821 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007822 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007823 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007824 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007825#ifdef FEAT_CLIENTSERVER
7826 {
7827 XPropertyEvent *e = (XPropertyEvent *)&event;
7828
7829 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007831 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007834 XtDispatchEvent(&event);
7835 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007836 else
7837 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007838 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007839 XtAppProcessEvent(app_context, mask);
7840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 }
7842}
7843
7844 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007845clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846{
7847 if (xterm_Shell != (Widget)0)
7848 return clip_x11_own_selection(xterm_Shell, cbd);
7849 return FAIL;
7850}
7851
7852 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007853clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854{
7855 if (xterm_Shell != (Widget)0)
7856 clip_x11_lose_selection(xterm_Shell, cbd);
7857}
7858
7859 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007860clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
7862 if (xterm_Shell != (Widget)0)
7863 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7864}
7865
7866 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007867clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869 clip_x11_set_selection(cbd);
7870}
7871#endif
7872
7873
7874#if defined(USE_XSMP) || defined(PROTO)
7875/*
7876 * Code for X Session Management Protocol.
7877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878
7879# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880/*
7881 * This is our chance to ask the user if they want to save,
7882 * or abort the logout
7883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007885xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
Bram Moolenaare1004402020-10-24 20:49:43 +02007887 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 int cancel_shutdown = False;
7889
Bram Moolenaare1004402020-10-24 20:49:43 +02007890 save_cmod_flags = cmdmod.cmod_flags;
7891 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007892 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007893 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02007895 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007896 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 out_flush();
7898
Bram Moolenaar0f873732019-12-05 20:28:46 +01007899 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 SmcInteractDone(smc_conn, cancel_shutdown);
7901
Bram Moolenaar0f873732019-12-05 20:28:46 +01007902 // Finish off
7903 // Only end save-yourself here if we're not cancelling shutdown;
7904 // we'll get a cancelled callback later in which we'll end it.
7905 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 if (!cancel_shutdown)
7907 {
7908 xsmp.save_yourself = False;
7909 SmcSaveYourselfDone(smc_conn, True);
7910 }
7911}
7912# endif
7913
7914/*
7915 * Callback that starts save-yourself.
7916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007918xsmp_handle_save_yourself(
7919 SmcConn smc_conn,
7920 SmPointer client_data UNUSED,
7921 int save_type UNUSED,
7922 Bool shutdown,
7923 int interact_style UNUSED,
7924 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007926 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 if (xsmp.save_yourself)
7928 SmcSaveYourselfDone(smc_conn, True);
7929 xsmp.save_yourself = True;
7930 xsmp.shutdown = shutdown;
7931
Bram Moolenaar0f873732019-12-05 20:28:46 +01007932 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007934 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935
7936 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007937 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938
7939# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007940 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007942 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 SmcInteractRequest(smc_conn, SmDialogError,
7944 xsmp_handle_interaction, client_data);
7945 else
7946# endif
7947 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007948 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 SmcSaveYourselfDone(smc_conn, True);
7950 xsmp.save_yourself = False;
7951 }
7952}
7953
7954
7955/*
7956 * Callback to warn us of imminent death.
7957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007959xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960{
7961 xsmp_close();
7962
Bram Moolenaar0f873732019-12-05 20:28:46 +01007963 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 getout_preserve_modified(0);
7965}
7966
7967
7968/*
7969 * Callback to tell us that save-yourself has completed.
7970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007972xsmp_save_complete(
7973 SmcConn smc_conn UNUSED,
7974 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975{
7976 xsmp.save_yourself = False;
7977}
7978
7979
7980/*
7981 * Callback to tell us that an instigated shutdown was cancelled
7982 * (maybe even by us)
7983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007985xsmp_shutdown_cancelled(
7986 SmcConn smc_conn,
7987 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988{
7989 if (xsmp.save_yourself)
7990 SmcSaveYourselfDone(smc_conn, True);
7991 xsmp.save_yourself = False;
7992 xsmp.shutdown = False;
7993}
7994
7995
7996/*
7997 * Callback to tell us that a new ICE connection has been established.
7998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008000xsmp_ice_connection(
8001 IceConn iceConn,
8002 IcePointer clientData UNUSED,
8003 Bool opening,
8004 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008006 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 if (opening)
8008 {
8009 xsmp_icefd = IceConnectionNumber(iceConn);
8010 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8011 }
8012}
8013
8014
Bram Moolenaar0f873732019-12-05 20:28:46 +01008015// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008017xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018{
8019 Bool rep;
8020
8021 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8022 == IceProcessMessagesIOError)
8023 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008024 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008026 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 xsmp_close();
8028 return FAIL;
8029 }
8030 else
8031 return OK;
8032}
8033
8034static int dummy;
8035
Bram Moolenaar0f873732019-12-05 20:28:46 +01008036// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 void
8038xsmp_init(void)
8039{
8040 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 SmcCallbacks smcallbacks;
8042#if 0
8043 SmPropValue smname;
8044 SmProp smnameprop;
8045 SmProp *smprops[1];
8046#endif
8047
8048 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008049 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050
8051 xsmp.save_yourself = xsmp.shutdown = False;
8052
Bram Moolenaar0f873732019-12-05 20:28:46 +01008053 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8055 smcallbacks.save_yourself.client_data = NULL;
8056 smcallbacks.die.callback = xsmp_die;
8057 smcallbacks.die.client_data = NULL;
8058 smcallbacks.save_complete.callback = xsmp_save_complete;
8059 smcallbacks.save_complete.client_data = NULL;
8060 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8061 smcallbacks.shutdown_cancelled.client_data = NULL;
8062
Bram Moolenaar0f873732019-12-05 20:28:46 +01008063 // Set up a watch on ICE connection creations. The "dummy" argument is
8064 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8066 {
8067 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008068 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 return;
8070 }
8071
Bram Moolenaar0f873732019-12-05 20:28:46 +01008072 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 xsmp.smcconn = SmcOpenConnection(
8074 NULL,
8075 NULL,
8076 SmProtoMajor,
8077 SmProtoMinor,
8078 SmcSaveYourselfProcMask | SmcDieProcMask
8079 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8080 &smcallbacks,
8081 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008082 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008083 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 errorstring);
8085 if (xsmp.smcconn == NULL)
8086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008088 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008089 char errorreport[132];
8090
8091 // If the message is too long it might not be NUL terminated. Add
8092 // a NUL at the end to make sure we don't go over the end.
8093 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008094 vim_snprintf(errorreport, sizeof(errorreport),
8095 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008096 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 return;
8099 }
8100 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8101
8102#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008103 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 smname.value = "vim";
8105 smname.length = 3;
8106 smnameprop.name = "SmProgram";
8107 smnameprop.type = "SmARRAY8";
8108 smnameprop.num_vals = 1;
8109 smnameprop.vals = &smname;
8110
8111 smprops[0] = &smnameprop;
8112 SmcSetProperties(xsmp.smcconn, 1, smprops);
8113#endif
8114}
8115
8116
Bram Moolenaar0f873732019-12-05 20:28:46 +01008117// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008119xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120{
8121 if (xsmp_icefd != -1)
8122 {
8123 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008124 if (xsmp.clientid != NULL)
8125 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008126 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 xsmp_icefd = -1;
8128 }
8129}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008130#endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131
8132
8133#ifdef EBCDIC
Bram Moolenaar0f873732019-12-05 20:28:46 +01008134// Translate character to its CTRL- value
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135char CtrlTable[] =
8136{
8137/* 00 - 5E */
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, 0,
8142 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8143 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8144/* ^ */ 0x1E,
8145/* - */ 0x1F,
8146/* 61 - 6C */
8147 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8148/* _ */ 0x1F,
8149/* 6E - 80 */
8150 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8151/* a */ 0x01,
8152/* b */ 0x02,
8153/* c */ 0x03,
8154/* d */ 0x37,
8155/* e */ 0x2D,
8156/* f */ 0x2E,
8157/* g */ 0x2F,
8158/* h */ 0x16,
8159/* i */ 0x05,
8160/* 8A - 90 */
8161 0, 0, 0, 0, 0, 0, 0,
8162/* j */ 0x15,
8163/* k */ 0x0B,
8164/* l */ 0x0C,
8165/* m */ 0x0D,
8166/* n */ 0x0E,
8167/* o */ 0x0F,
8168/* p */ 0x10,
8169/* q */ 0x11,
8170/* r */ 0x12,
8171/* 9A - A1 */
8172 0, 0, 0, 0, 0, 0, 0, 0,
8173/* s */ 0x13,
8174/* t */ 0x3C,
8175/* u */ 0x3D,
8176/* v */ 0x32,
8177/* w */ 0x26,
8178/* x */ 0x18,
8179/* y */ 0x19,
8180/* z */ 0x3F,
8181/* AA - AC */
8182 0, 0, 0,
8183/* [ */ 0x27,
8184/* AE - BC */
8185 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8186/* ] */ 0x1D,
8187/* BE - C0 */ 0, 0, 0,
8188/* A */ 0x01,
8189/* B */ 0x02,
8190/* C */ 0x03,
8191/* D */ 0x37,
8192/* E */ 0x2D,
8193/* F */ 0x2E,
8194/* G */ 0x2F,
8195/* H */ 0x16,
8196/* I */ 0x05,
8197/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8198/* J */ 0x15,
8199/* K */ 0x0B,
8200/* L */ 0x0C,
8201/* M */ 0x0D,
8202/* N */ 0x0E,
8203/* O */ 0x0F,
8204/* P */ 0x10,
8205/* Q */ 0x11,
8206/* R */ 0x12,
8207/* DA - DF */ 0, 0, 0, 0, 0, 0,
8208/* \ */ 0x1C,
8209/* E1 */ 0,
8210/* S */ 0x13,
8211/* T */ 0x3C,
8212/* U */ 0x3D,
8213/* V */ 0x32,
8214/* W */ 0x26,
8215/* X */ 0x18,
8216/* Y */ 0x19,
8217/* Z */ 0x3F,
8218/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8219 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8220};
8221
8222char MetaCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008223{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8225 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8226 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8227 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8228};
8229
8230
Bram Moolenaar0f873732019-12-05 20:28:46 +01008231// TODO: Use characters NOT numbers!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232char CtrlCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008233{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8235 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8236 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8237 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8238};
8239
8240
8241#endif