blob: 9dde754f3ae40036aa272750105a916271956ea7 [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 Moolenaar071d4272004-06-13 20:20:40 +0000786# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100787static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100789static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790# endif
791
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200792/*
793 * Get a size of signal stack.
794 * Preference (if available): sysconf > SIGSTKSZ > guessed size
795 */
796static long int get_signal_stack_size()
797{
798# ifdef HAVE_SYSCONF_SIGSTKSZ
799 long int size = -1;
800
801 // return size only if sysconf doesn't return an error
802 if ((size = sysconf(_SC_SIGSTKSZ)) > -1)
803 return size;
804# endif
805
806# ifdef SIGSTKSZ
807 // if sysconf() isn't available or gives error, return SIGSTKSZ
808 // if defined
809 return SIGSTKSZ;
810# endif
811
812 // otherwise guess the size
813 return 8000;
814}
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816static char *signal_stack;
817
818 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100819init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821 if (signal_stack != NULL)
822 {
823# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824# ifdef HAVE_SS_BASE
825 sigstk.ss_base = signal_stack;
826# else
827 sigstk.ss_sp = signal_stack;
828# endif
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200829 sigstk.ss_size = get_signal_stack_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 sigstk.ss_flags = 0;
831 (void)sigaltstack(&sigstk, NULL);
832# else
833 sigstk.ss_sp = signal_stack;
834 if (stack_grows_downwards)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200835 sigstk.ss_sp += get_signal_stack_size() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 sigstk.ss_onstack = 0;
837 (void)sigstack(&sigstk, NULL);
838# endif
839 }
840}
841#endif
842
843/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000844 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 * will barf when the second argument to signal() is ``wrong''.
846 * Let me try it with a few tricky defines from my own osdef.h (jw).
847 */
848#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 static RETSIGTYPE
850sig_winch SIGDEFARG(sigarg)
851{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100852 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
854 do_resize = TRUE;
855 SIGRETURN;
856}
857#endif
858
859#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 static RETSIGTYPE
861catch_sigint SIGDEFARG(sigarg)
862{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100863 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
865 got_int = TRUE;
866 SIGRETURN;
867}
868#endif
869
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200870#if defined(SIGUSR1)
871 static RETSIGTYPE
872catch_sigusr1 SIGDEFARG(sigarg)
873{
874 // this is not required on all systems, but it doesn't hurt anybody
875 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
876 got_sigusr1 = TRUE;
877 SIGRETURN;
878}
879#endif
880
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 static RETSIGTYPE
883catch_sigpwr SIGDEFARG(sigarg)
884{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100885 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000886 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 /*
888 * I'm not sure we get the SIGPWR signal when the system is really going
889 * down or when the batteries are almost empty. Just preserve the swap
890 * files and don't exit, that can't do any harm.
891 */
892 ml_sync_all(FALSE, FALSE);
893 SIGRETURN;
894}
895#endif
896
897#ifdef SET_SIG_ALARM
898/*
899 * signal function for alarm().
900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 static RETSIGTYPE
902sig_alarm SIGDEFARG(sigarg)
903{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100904 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 sig_alarm_called = TRUE;
906 SIGRETURN;
907}
908#endif
909
Bram Moolenaaredce7422019-01-20 18:39:30 +0100910#if (defined(HAVE_SETJMP_H) \
911 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
912 || defined(FEAT_LIBCALL))) \
913 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100914# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100915
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100916// argument to SETJMP()
917static JMP_BUF lc_jump_env;
918
919# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100920// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100921// Volatile because it is used in signal handlers.
922static volatile sig_atomic_t lc_signal;
923# endif
924
925// TRUE when lc_jump_env is valid.
926// Volatile because it is used in signal handler deathtrap().
927static volatile sig_atomic_t lc_active INIT(= FALSE);
928
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929/*
930 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100931 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100933 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 * Usage:
935 * mch_startjmp();
936 * if (SETJMP(lc_jump_env) != 0)
937 * {
938 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100939 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 * }
941 * else
942 * {
943 * do_the_work;
944 * mch_endjmp();
945 * }
946 * Note: Can't move SETJMP() here, because a function calling setjmp() must
947 * not return before the saved environment is used.
948 * Returns OK for normal return, FAIL when the protected code caused a
949 * problem and LONGJMP() was used.
950 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100951 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100952mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953{
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100954# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 lc_active = TRUE;
958}
959
Bram Moolenaar113e1072019-01-20 15:30:40 +0100960 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100961mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962{
963 lc_active = FALSE;
964}
965
Bram Moolenaar113e1072019-01-20 15:30:40 +0100966 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100967mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968{
969# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100970 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
971 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 init_signal_stack();
973# endif
974}
975#endif
976
977/*
978 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200979 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200981 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
982 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 */
984 static RETSIGTYPE
985deathtrap SIGDEFARG(sigarg)
986{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100987 static int entered = 0; // count the number of times we got here.
988 // Note: when memory has been corrupted
989 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990#ifdef SIGHASARG
991 int i;
992#endif
993
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100994#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 /*
996 * Catch a crash in protected code.
997 * Restores the environment saved in lc_jump_env, which looks like
998 * SETJMP() returns 1.
999 */
1000 if (lc_active)
1001 {
1002# if defined(SIGHASARG)
1003 lc_signal = sigarg;
1004# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001005 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001007 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 }
1009#endif
1010
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001011#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001012# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001013 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1014 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1015 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001016 if (in_mch_delay && sigarg == SIGQUIT)
1017 SIGRETURN;
1018# endif
1019
Bram Moolenaar0f873732019-12-05 20:28:46 +01001020 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1021 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1022 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001023 if (entered == 0
1024 && (0
1025# ifdef SIGHUP
1026 || sigarg == SIGHUP
1027# endif
1028# ifdef SIGQUIT
1029 || sigarg == SIGQUIT
1030# endif
1031# ifdef SIGTERM
1032 || sigarg == SIGTERM
1033# endif
1034# ifdef SIGPWR
1035 || sigarg == SIGPWR
1036# endif
1037# ifdef SIGUSR1
1038 || sigarg == SIGUSR1
1039# endif
1040# ifdef SIGUSR2
1041 || sigarg == SIGUSR2
1042# endif
1043 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001044 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001045 SIGRETURN;
1046#endif
1047
Bram Moolenaar0f873732019-12-05 20:28:46 +01001048 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 ++entered;
1050
Bram Moolenaar0f873732019-12-05 20:28:46 +01001051 // Executing autocommands is likely to use more stack space than we have
1052 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001053 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001056 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 set_vim_var_nr(VV_DYING, (long)entered);
1058#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001059 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001061#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001062 // Since we are now using the signal stack, need to reset the stack
1063 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 get_stack_limit();
1065#endif
1066
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001067#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001068 // This is for opening gdb the moment Vim crashes.
1069 // You need to manually adjust the file name and Vim executable name.
1070 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001071 {
1072# define VI_GDB_FILE "/tmp/vimgdb"
1073# define VIM_NAME "/usr/bin/vim"
1074 FILE *fp = fopen(VI_GDB_FILE, "w");
1075 if (fp)
1076 {
1077 fprintf(fp,
1078 "file %s\n"
1079 "attach %d\n"
1080 "set height 1000\n"
1081 "bt full\n"
1082 , VIM_NAME, getpid());
1083 fclose(fp);
1084 system("xterm -e gdb -x "VI_GDB_FILE);
1085 unlink(VI_GDB_FILE);
1086 }
1087 }
1088#endif
1089
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001091 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 for (i = 0; signal_info[i].sig != -1; i++)
1093 if (sigarg == signal_info[i].sig)
1094 break;
1095 deadly_signal = sigarg;
1096#endif
1097
Bram Moolenaar0f873732019-12-05 20:28:46 +01001098 full_screen = FALSE; // don't write message to the GUI, it might be
1099 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 /*
1101 * If something goes wrong after entering here, we may get here again.
1102 * When this happens, give a message and try to exit nicely (resetting the
1103 * terminal mode, etc.)
1104 * When this happens twice, just exit, don't even try to give a message,
1105 * stack may be corrupt or something weird.
1106 * When this still happens again (or memory was corrupted in such a way
1107 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1108 */
1109 if (entered >= 3)
1110 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001111 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 may_core_dump();
1113 if (entered >= 4)
1114 _exit(8);
1115 exit(7);
1116 }
1117 if (entered == 2)
1118 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001119 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001120 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 out_flush();
1122 getout(1);
1123 }
1124
Bram Moolenaar0f873732019-12-05 20:28:46 +01001125 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001127 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 signal_info[i].name);
1129#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001130 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001132
Bram Moolenaar0f873732019-12-05 20:28:46 +01001133 // Preserve files and exit. This sets the really_exiting flag to prevent
1134 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001135 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
Bram Moolenaar0f873732019-12-05 20:28:46 +01001137 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001138
Bram Moolenaar009b2592004-10-24 19:18:58 +00001139#ifdef NBDEBUG
1140 reset_signals();
1141 may_core_dump();
1142 abort();
1143#endif
1144
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 SIGRETURN;
1146}
1147
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001149 * Invoked after receiving SIGCONT. We don't know what happened while
1150 * sleeping, deal with part of that.
1151 */
1152 static void
1153after_sigcont(void)
1154{
1155# ifdef FEAT_TITLE
1156 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1157 // again later.
1158 oldtitle_outdated = TRUE;
1159# endif
1160 settmode(TMODE_RAW);
1161 need_check_timestamps = TRUE;
1162 did_check_timestamps = FALSE;
1163}
1164
1165#if defined(SIGCONT)
1166static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001167static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001168
1169/*
1170 * With multi-threading, suspending might not work immediately. Catch the
1171 * SIGCONT signal, which will be used as an indication whether the suspending
1172 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001173 *
1174 * On Linux, signal is not always handled immediately either.
1175 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001176 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001177 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001178 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001180static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001181static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182
1183/*
1184 * signal handler for SIGCONT
1185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 static RETSIGTYPE
1187sigcont_handler SIGDEFARG(sigarg)
1188{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001189 if (in_mch_suspend)
1190 {
1191 sigcont_received = TRUE;
1192 }
1193 else
1194 {
1195 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1196 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1197 // back to a sane mode. We should redraw, but we can't really do that
1198 // in a signal handler, do a redraw later.
1199 after_sigcont();
1200 redraw_later(CLEAR);
1201 cursor_on_force();
1202 out_flush();
1203 }
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 SIGRETURN;
1206}
1207#endif
1208
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001209#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001210# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001211static void *clip_star_save = NULL;
1212static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001213# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001214
1215/*
1216 * Called when Vim is going to sleep or execute a shell command.
1217 * We can't respond to requests for the X selections. Lose them, otherwise
1218 * other applications will hang. But first copy the text to cut buffer 0.
1219 */
1220 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001221loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001222{
1223 if (clip_star.owned || clip_plus.owned)
1224 {
1225 x11_export_final_selection();
1226 if (clip_star.owned)
1227 clip_lose_selection(&clip_star);
1228 if (clip_plus.owned)
1229 clip_lose_selection(&clip_plus);
1230 if (x11_display != NULL)
1231 XFlush(x11_display);
1232 }
1233}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001234
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001235# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001236/*
1237 * Save clipboard text to restore later.
1238 */
1239 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001240save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001241{
1242 if (clip_star.owned)
1243 clip_star_save = get_register('*', TRUE);
1244 if (clip_plus.owned)
1245 clip_plus_save = get_register('+', TRUE);
1246}
1247
1248/*
1249 * Restore clipboard text if no one own the X selection.
1250 */
1251 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001252restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001253{
1254 if (clip_star_save != NULL)
1255 {
1256 if (!clip_gen_owner_exists(&clip_star))
1257 put_register('*', clip_star_save);
1258 else
1259 free_register(clip_star_save);
1260 clip_star_save = NULL;
1261 }
1262 if (clip_plus_save != NULL)
1263 {
1264 if (!clip_gen_owner_exists(&clip_plus))
1265 put_register('+', clip_plus_save);
1266 else
1267 free_register(clip_plus_save);
1268 clip_plus_save = NULL;
1269 }
1270}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001271# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001272#endif
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274/*
1275 * If the machine has job control, use it to suspend the program,
1276 * otherwise fake it by starting a new shell.
1277 */
1278 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001279mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001281 if (ignore_sigtstp)
1282 return;
1283
Bram Moolenaar041c7102020-05-30 18:14:57 +02001284#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001285 in_mch_suspend = TRUE;
1286
Bram Moolenaar0f873732019-12-05 20:28:46 +01001287 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001289 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
1291# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001292 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001294# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 sigcont_received = FALSE;
1296# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001297
Bram Moolenaar0f873732019-12-05 20:28:46 +01001298 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001299
1300# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001301 /*
1302 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001303 * immediately, but somehow not all the time, probably because it's handled
1304 * in another thread. Do not call pause() because there would be race
1305 * condition which would hang Vim if signal happened in between the test of
1306 * sigcont_received and the call to pause(). If signal is not yet received,
1307 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1308 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001309 */
1310 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001311 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001312
Bram Moolenaar262735e2009-07-14 10:20:22 +00001313 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001314 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001317 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
Bram Moolenaar2e310482018-08-21 13:09:10 +02001319 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320#else
1321 suspend_shell();
1322#endif
1323}
1324
1325 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001326mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
1328 Columns = 80;
1329 Rows = 24;
1330
1331 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001332
1333#ifdef SIGTSTP
1334 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1335 // that indicates the shell (or program) that launched us does not support
1336 // tty job control and thus we should ignore that signal. If invoked as a
1337 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
1338 ignore_sigtstp = restricted || SIG_IGN == signal(SIGTSTP, SIG_ERR);
1339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001341
Bram Moolenaar56718732006-03-15 22:53:57 +00001342#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001343 mac_conv_init();
1344#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001345#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1346 win_clip_init();
1347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348}
1349
1350 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001351set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353#if defined(SIGWINCH)
1354 /*
1355 * WINDOW CHANGE signal is handled with sig_winch().
1356 */
1357 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1358#endif
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001361 // See mch_init() for the conditions under which we ignore SIGTSTP.
1362 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001364#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 signal(SIGCONT, sigcont_handler);
1366#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001367#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 /*
1369 * We want to ignore breaking of PIPEs.
1370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 signal(SIGPIPE, SIG_IGN);
1372#endif
1373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001375 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376#endif
1377
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001378#ifdef SIGUSR1
1379 /*
1380 * Call user's handler on SIGUSR1
1381 */
1382 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
1383#endif
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 /*
1386 * Ignore alarm signals (Perl's alarm() generates it).
1387 */
1388#ifdef SIGALRM
1389 signal(SIGALRM, SIG_IGN);
1390#endif
1391
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001392#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 /*
1394 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1395 * work will be lost.
1396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1398#endif
1399
1400 /*
1401 * Arrange for other signals to gracefully shutdown Vim.
1402 */
1403 catch_signals(deathtrap, SIG_ERR);
1404
1405#if defined(FEAT_GUI) && defined(SIGHUP)
1406 /*
1407 * When the GUI is running, ignore the hangup signal.
1408 */
1409 if (gui.in_use)
1410 signal(SIGHUP, SIG_IGN);
1411#endif
1412}
1413
Bram Moolenaardf177f62005-02-22 08:39:57 +00001414#if defined(SIGINT) || defined(PROTO)
1415/*
1416 * Catch CTRL-C (only works while in Cooked mode).
1417 */
1418 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001419catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001420{
1421 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1422}
1423#endif
1424
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001426reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001429#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001430 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 signal(SIGCONT, SIG_DFL);
1432#endif
1433}
1434
1435 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001436catch_signals(
1437 RETSIGTYPE (*func_deadly)(),
1438 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
1440 int i;
1441
1442 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if (signal_info[i].deadly)
1445 {
1446#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1447 struct sigaction sa;
1448
Bram Moolenaar0f873732019-12-05 20:28:46 +01001449 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 sa.sa_handler = func_deadly;
1451 sigemptyset(&sa.sa_mask);
1452# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001453 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1454 // thread handling in combination with using the alternate stack:
1455 // pthread library functions try to use the stack pointer to
1456 // identify the current thread, causing a SEGV signal, which
1457 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 sa.sa_flags = 0;
1459# else
1460 sa.sa_flags = SA_ONSTACK;
1461# endif
1462 sigaction(signal_info[i].sig, &sa, NULL);
1463#else
1464# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1465 struct sigvec sv;
1466
Bram Moolenaar0f873732019-12-05 20:28:46 +01001467 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 sv.sv_handler = func_deadly;
1469 sv.sv_mask = 0;
1470 sv.sv_flags = SV_ONSTACK;
1471 sigvec(signal_info[i].sig, &sv, NULL);
1472# else
1473 signal(signal_info[i].sig, func_deadly);
1474# endif
1475#endif
1476 }
1477 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001478 {
1479 // Deal with non-deadly signals.
1480#ifdef SIGTSTP
1481 signal(signal_info[i].sig,
1482 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1483 ? SIG_IGN : func_other);
1484#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001486#endif
1487 }
1488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489}
1490
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001491#ifdef HAVE_SIGPROCMASK
1492 static void
1493block_signals(sigset_t *set)
1494{
1495 sigset_t newset;
1496 int i;
1497
1498 sigemptyset(&newset);
1499
1500 for (i = 0; signal_info[i].sig != -1; i++)
1501 sigaddset(&newset, signal_info[i].sig);
1502
Bram Moolenaar2e310482018-08-21 13:09:10 +02001503# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001504 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001505 sigaddset(&newset, SIGCONT);
1506# endif
1507
1508 sigprocmask(SIG_BLOCK, &newset, set);
1509}
1510
1511 static void
1512unblock_signals(sigset_t *set)
1513{
1514 sigprocmask(SIG_SETMASK, set, NULL);
1515}
1516#endif
1517
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001519 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001520 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1521 * return TRUE
1522 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1523 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001524 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001525 * Returns TRUE when Vim should exit.
1526 */
1527 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001528vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001529{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001530 static int got_signal = 0;
1531 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001532
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001533 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001534 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001535 case SIGNAL_BLOCK: blocked = TRUE;
1536 break;
1537
1538 case SIGNAL_UNBLOCK: blocked = FALSE;
1539 if (got_signal != 0)
1540 {
1541 kill(getpid(), got_signal);
1542 got_signal = 0;
1543 }
1544 break;
1545
1546 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001547 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001548 got_signal = sig;
1549#ifdef SIGPWR
1550 if (sig != SIGPWR)
1551#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001552 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001553 break;
1554 }
1555 return FALSE;
1556}
1557
1558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 * Check_win checks whether we have an interactive stdout.
1560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001562mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 if (isatty(1))
1565 return OK;
1566 return FAIL;
1567}
1568
1569/*
1570 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1571 */
1572 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001573mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
1575 if (isatty(read_cmd_fd))
1576 return TRUE;
1577 return FALSE;
1578}
1579
1580#ifdef FEAT_X11
1581
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001582# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585/*
1586 * Give a message about the elapsed time for opening the X window.
1587 */
1588 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001589xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001591 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592}
1593# endif
1594#endif
1595
1596#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1597/*
1598 * A few functions shared by X11 title and clipboard code.
1599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601static int got_x_error = FALSE;
1602
1603/*
1604 * X Error handler, otherwise X just exits! (very rude) -- webb
1605 */
1606 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001607x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001609 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 STRCAT(IObuff, _("\nVim: Got X error\n"));
1611
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001612 // In the GUI we cannot print a message and continue, because no X calls
1613 // are allowed here (causes my system to hang). Silently continuing seems
1614 // like the best alternative. Do preserve files, in case we crash.
1615 ml_sync_all(FALSE, FALSE);
1616
1617#ifdef FEAT_GUI
1618 if (!gui.in_use)
1619#endif
1620 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
Bram Moolenaar0f873732019-12-05 20:28:46 +01001622 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623}
1624
1625/*
1626 * Another X Error handler, just used to check for errors.
1627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001629x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
1631 got_x_error = TRUE;
1632 return 0;
1633}
1634
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001635/*
1636 * Return TRUE when connection to the X server is desired.
1637 */
1638 static int
1639x_connect_to_server(void)
1640{
1641 // No point in connecting if we are exiting or dying.
1642 if (exiting || v_dying)
1643 return FALSE;
1644
1645#if defined(FEAT_CLIENTSERVER)
1646 if (x_force_connect)
1647 return TRUE;
1648#endif
1649 if (x_no_connect)
1650 return FALSE;
1651
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001652 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001653 if (clip_exclude_prog != NULL)
1654 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001655 // Just in case we get called recursively, return FALSE. This could
1656 // happen if vpeekc() is used while executing the prog and it causes a
1657 // related callback to be invoked.
1658 if (regprog_in_use(clip_exclude_prog))
1659 return FALSE;
1660
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001661 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1662 return FALSE;
1663 }
1664 return TRUE;
1665}
1666
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001668# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669/*
1670 * An X IO Error handler, used to catch error while opening the display.
1671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001673x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001675 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001677# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001678 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001679# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680}
1681# endif
1682
1683/*
1684 * An X IO Error handler, used to catch terminal errors.
1685 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001686static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001689x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
1691 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001692 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 x11_window = 0;
1694 x11_display = NULL;
1695 xterm_Shell = (Widget)0;
1696
Bram Moolenaar0f873732019-12-05 20:28:46 +01001697 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001699# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001700 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001701# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001703
1704/*
1705 * If the X11 connection was lost try to restore it.
1706 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001707 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001708 */
1709 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001710may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001711{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001712 // No point in restoring the connecting if we are exiting or dying.
1713 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001714 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001715 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001716
1717# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001718 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001719 if (app_context != (XtAppContext)NULL)
1720 {
1721 XtDestroyApplicationContext(app_context);
1722 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001723 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001724 }
1725# endif
1726
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001727 setup_term_clip();
1728 get_x11_title(FALSE);
1729 }
1730}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001731
1732 void
1733ex_xrestore(exarg_T *eap)
1734{
1735 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1736 {
1737 if (xterm_display_allocated)
1738 vim_free(xterm_display);
1739 xterm_display = (char *)vim_strsave(eap->arg);
1740 xterm_display_allocated = TRUE;
1741 }
1742 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001743 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001744
1745 clear_xterm_clip();
1746 x11_window = 0;
1747 xterm_dpy_retry_count = 5; // Try reconnecting five times
1748 may_restore_clipboard();
1749}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750#endif
1751
1752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 * Test if "dpy" and x11_window are valid by getting the window title.
1754 * I don't actually want it yet, so there may be a simpler call to use, but
1755 * this will cause the error handler x_error_check() to be called if anything
1756 * is wrong, such as the window pointer being invalid (as can happen when the
1757 * user changes his DISPLAY, but not his WINDOWID) -- webb
1758 */
1759 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001760test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
1762 int (*old_handler)();
1763 XTextProperty text_prop;
1764
1765 old_handler = XSetErrorHandler(x_error_check);
1766 got_x_error = FALSE;
1767 if (XGetWMName(dpy, x11_window, &text_prop))
1768 XFree((void *)text_prop.value);
1769 XSync(dpy, False);
1770 (void)XSetErrorHandler(old_handler);
1771
1772 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001773 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
1775 return (got_x_error ? FAIL : OK);
1776}
1777#endif
1778
1779#ifdef FEAT_TITLE
1780
1781#ifdef FEAT_X11
1782
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001783static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
1785/*
1786 * try to get x11 window and display
1787 *
1788 * return FAIL for failure, OK otherwise
1789 */
1790 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001791get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792{
1793 char *winid;
1794 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001795#define XD_NONE 0 // x11_display not set here
1796#define XD_HERE 1 // x11_display opened here
1797#define XD_GUI 2 // x11_display used from gui.dpy
1798#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 static int x11_display_from = XD_NONE;
1800 static int did_set_error_handler = FALSE;
1801
1802 if (!did_set_error_handler)
1803 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001804 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 (void)XSetErrorHandler(x_error_handler);
1806 did_set_error_handler = TRUE;
1807 }
1808
Bram Moolenaar9372a112005-12-06 19:59:18 +00001809#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 if (gui.in_use)
1811 {
1812 /*
1813 * If the X11 display was opened here before, for the window where Vim
1814 * was started, close that one now to avoid a memory leak.
1815 */
1816 if (x11_display_from == XD_HERE && x11_display != NULL)
1817 {
1818 XCloseDisplay(x11_display);
1819 x11_display_from = XD_NONE;
1820 }
1821 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1822 {
1823 x11_display_from = XD_GUI;
1824 return OK;
1825 }
1826 x11_display = NULL;
1827 return FAIL;
1828 }
1829 else if (x11_display_from == XD_GUI)
1830 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001831 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 x11_window = 0;
1833 x11_display = NULL;
1834 x11_display_from = XD_NONE;
1835 }
1836#endif
1837
Bram Moolenaar0f873732019-12-05 20:28:46 +01001838 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 if (!x_connect_to_server())
1840 return FAIL;
1841
1842 /*
1843 * If WINDOWID not set, should try another method to find out
1844 * what the current window number is. The only code I know for
1845 * this is very complicated.
1846 * We assume that zero is invalid for WINDOWID.
1847 */
1848 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1849 x11_window = (Window)atol(winid);
1850
1851#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001852 if (xterm_dpy == x11_display)
1853 // x11_display may have been set to xterm_dpy elsewhere
1854 x11_display_from = XD_XTERM;
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (xterm_dpy != NULL && x11_window != 0)
1857 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001858 // We may have checked it already, but Gnome terminal can move us to
1859 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001860 if (x11_display_from != XD_XTERM)
1861 {
1862 /*
1863 * If the X11 display was opened here before, for the window where
1864 * Vim was started, close that one now to avoid a memory leak.
1865 */
1866 if (x11_display_from == XD_HERE && x11_display != NULL)
1867 XCloseDisplay(x11_display);
1868 x11_display = xterm_dpy;
1869 x11_display_from = XD_XTERM;
1870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (test_x11_window(x11_display) == FAIL)
1872 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001873 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 x11_window = 0;
1875 x11_display = NULL;
1876 x11_display_from = XD_NONE;
1877 return FAIL;
1878 }
1879 return OK;
1880 }
1881#endif
1882
1883 if (x11_window == 0 || x11_display == NULL)
1884 result = -1;
1885
Bram Moolenaar0f873732019-12-05 20:28:46 +01001886 if (result != -1) // Have already been here and set this
1887 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888
1889 if (x11_window != 0 && x11_display == NULL)
1890 {
1891#ifdef SET_SIG_ALARM
1892 RETSIGTYPE (*sig_save)();
1893#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001894#ifdef ELAPSED_FUNC
1895 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
1897 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001898 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899#endif
1900
1901#ifdef SET_SIG_ALARM
1902 /*
1903 * Opening the Display may hang if the DISPLAY setting is wrong, or
1904 * the network connection is bad. Set an alarm timer to get out.
1905 */
1906 sig_alarm_called = FALSE;
1907 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1908 (RETSIGTYPE (*)())sig_alarm);
1909 alarm(2);
1910#endif
1911 x11_display = XOpenDisplay(NULL);
1912
1913#ifdef SET_SIG_ALARM
1914 alarm(0);
1915 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1916 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001917 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918#endif
1919 if (x11_display != NULL)
1920 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001921# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001923 {
1924 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001925 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001926 verbose_leave();
1927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928# endif
1929 if (test_x11_window(x11_display) == FAIL)
1930 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001931 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 x11_window = 0;
1933 XCloseDisplay(x11_display);
1934 x11_display = NULL;
1935 }
1936 else
1937 x11_display_from = XD_HERE;
1938 }
1939 }
1940 if (x11_window == 0 || x11_display == NULL)
1941 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001942
1943# ifdef FEAT_EVAL
1944 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1945# endif
1946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 return (result = OK);
1948}
1949
1950/*
1951 * Determine original x11 Window Title
1952 */
1953 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001954get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001956 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957}
1958
1959/*
1960 * Determine original x11 Window icon
1961 */
1962 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001963get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964{
1965 int retval = FALSE;
1966
1967 retval = get_x11_thing(FALSE, test_only);
1968
Bram Moolenaar0f873732019-12-05 20:28:46 +01001969 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 if (oldicon == NULL && !test_only)
1971 {
1972 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001973 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001975 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 }
1977
1978 return retval;
1979}
1980
1981 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001982get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01001983 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01001984 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985{
1986 XTextProperty text_prop;
1987 int retval = FALSE;
1988 Status status;
1989
1990 if (get_x11_windis() == OK)
1991 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001992 // Get window/icon name if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 if (get_title)
1994 status = XGetWMName(x11_display, x11_window, &text_prop);
1995 else
1996 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1997
1998 /*
1999 * If terminal is xterm, then x11_window may be a child window of the
2000 * outer xterm window that actually contains the window/icon name, so
2001 * keep traversing up the tree until a window with a title/icon is
2002 * found.
2003 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002004 // Previously this was only done for xterm and alike. I don't see a
Bram Moolenaar0f873732019-12-05 20:28:46 +01002005 // reason why it would fail for other terminal emulators.
2006 // if (term_is_xterm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 Window root;
2009 Window parent;
2010 Window win = x11_window;
2011 Window *children;
2012 unsigned int num_children;
2013
2014 while (!status || text_prop.value == NULL)
2015 {
2016 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2017 &num_children))
2018 break;
2019 if (children)
2020 XFree((void *)children);
2021 if (parent == root || parent == 0)
2022 break;
2023
2024 win = parent;
2025 if (get_title)
2026 status = XGetWMName(x11_display, win, &text_prop);
2027 else
2028 status = XGetWMIconName(x11_display, win, &text_prop);
2029 }
2030 }
2031 if (status && text_prop.value != NULL)
2032 {
2033 retval = TRUE;
2034 if (!test_only)
2035 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002036 if (get_title)
2037 vim_free(oldtitle);
2038 else
2039 vim_free(oldicon);
Bram Moolenaara12a1612019-01-24 16:39:02 +01002040 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 if (get_title)
2043 oldtitle = vim_strsave((char_u *)text_prop.value);
2044 else
2045 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
2047 else
2048 {
2049 char **cl;
2050 Status transform_status;
2051 int n = 0;
2052
2053 transform_status = XmbTextPropertyToTextList(x11_display,
2054 &text_prop,
2055 &cl, &n);
2056 if (transform_status >= Success && n > 0 && cl[0])
2057 {
2058 if (get_title)
2059 oldtitle = vim_strsave((char_u *) cl[0]);
2060 else
2061 oldicon = vim_strsave((char_u *) cl[0]);
2062 XFreeStringList(cl);
2063 }
2064 else
2065 {
2066 if (get_title)
2067 oldtitle = vim_strsave((char_u *)text_prop.value);
2068 else
2069 oldicon = vim_strsave((char_u *)text_prop.value);
2070 }
2071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
2073 XFree((void *)text_prop.value);
2074 }
2075 }
2076 return retval;
2077}
2078
Bram Moolenaar0f873732019-12-05 20:28:46 +01002079// Xutf8 functions are not available on older systems. Note that on some
2080// systems X_HAVE_UTF8_STRING may be defined in a header file but
2081// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2082// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002083#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002084# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085# define USE_UTF8_STRING
2086# endif
2087#endif
2088
2089/*
2090 * Set x11 Window Title
2091 *
2092 * get_x11_windis() must be called before this and have returned OK
2093 */
2094 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002095set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002097 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2098 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2099 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100#ifdef USE_UTF8_STRING
2101 if (enc_utf8)
2102 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2103 NULL, NULL, 0, NULL, NULL, NULL);
2104 else
2105#endif
2106 {
2107#if XtSpecificationRelease >= 4
2108# ifdef FEAT_XFONTSET
2109 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2110 NULL, NULL, 0, NULL, NULL, NULL);
2111# else
2112 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002113 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
Bram Moolenaar0f873732019-12-05 20:28:46 +01002115 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002116 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 XSetWMProperties(x11_display, x11_window, &text_prop,
2118 NULL, NULL, 0, NULL, NULL, NULL);
2119# endif
2120#else
2121 XStoreName(x11_display, x11_window, (char *)title);
2122#endif
2123 }
2124 XFlush(x11_display);
2125}
2126
2127/*
2128 * Set x11 Window icon
2129 *
2130 * get_x11_windis() must be called before this and have returned OK
2131 */
2132 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002133set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002135 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136#ifdef USE_UTF8_STRING
2137 if (enc_utf8)
2138 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2139 NULL, 0, NULL, NULL, NULL);
2140 else
2141#endif
2142 {
2143#if XtSpecificationRelease >= 4
2144# ifdef FEAT_XFONTSET
2145 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2146 NULL, 0, NULL, NULL, NULL);
2147# else
2148 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002149 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002151 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2153 NULL, 0, NULL, NULL, NULL);
2154# endif
2155#else
2156 XSetIconName(x11_display, x11_window, (char *)icon);
2157#endif
2158 }
2159 XFlush(x11_display);
2160}
2161
Bram Moolenaar0f873732019-12-05 20:28:46 +01002162#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002165get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
2167 return FALSE;
2168}
2169
2170 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002171get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
2173 if (!test_only)
2174 {
2175 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002176 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002178 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180 return FALSE;
2181}
2182
Bram Moolenaar0f873732019-12-05 20:28:46 +01002183#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
2185 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002186mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187{
2188 return get_x11_title(TRUE);
2189}
2190
2191 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002192mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193{
2194 return get_x11_icon(TRUE);
2195}
2196
2197/*
2198 * Set the window title and icon.
2199 */
2200 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002201mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202{
2203 int type = 0;
2204 static int recursive = 0;
2205
Bram Moolenaar0f873732019-12-05 20:28:46 +01002206 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002208 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 return;
2210
Bram Moolenaar0f873732019-12-05 20:28:46 +01002211 // When one of the X11 functions causes a deadly signal, we get here again
2212 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 if (recursive)
2214 return;
2215 ++recursive;
2216
2217 /*
2218 * if the window ID and the display is known, we may use X11 calls
2219 */
2220#ifdef FEAT_X11
2221 if (get_x11_windis() == OK)
2222 type = 1;
2223#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002224# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002225 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 if (gui.in_use)
2227 type = 1;
2228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229#endif
2230
2231 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002232 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 * than x11 calls, because the x11 calls don't always work
2234 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 if ((type || *T_TS != NUL) && title != NULL)
2236 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002237 if (oldtitle_outdated)
2238 {
2239 oldtitle_outdated = FALSE;
2240 VIM_CLEAR(oldtitle);
2241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 if (oldtitle == NULL
2243#ifdef FEAT_GUI
2244 && !gui.in_use
2245#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002246 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 (void)get_x11_title(FALSE);
2248
Bram Moolenaar0f873732019-12-05 20:28:46 +01002249 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 term_settitle(title);
2251#ifdef FEAT_X11
2252 else
2253# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002254 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002256 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002258#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002259 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 else
2261 gui_mch_settitle(title, icon);
2262#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002263 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 }
2265
2266 if ((type || *T_CIS != NUL) && icon != NULL)
2267 {
2268 if (oldicon == NULL
2269#ifdef FEAT_GUI
2270 && !gui.in_use
2271#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002272 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 get_x11_icon(FALSE);
2274
2275 if (*T_CIS != NUL)
2276 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002277 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002279 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 out_flush();
2281 }
2282#ifdef FEAT_X11
2283 else
2284# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002285 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002287 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288#endif
2289 did_set_icon = TRUE;
2290 }
2291 --recursive;
2292}
2293
2294/*
2295 * Restore the window/icon title.
2296 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002297 * SAVE_RESTORE_TITLE only restore title
2298 * SAVE_RESTORE_ICON only restore icon
2299 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 */
2301 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002302mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303{
Bram Moolenaardac13472019-09-16 21:06:21 +02002304 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002305
Bram Moolenaar0f873732019-12-05 20:28:46 +01002306 // only restore the title or icon when it has been set
Bram Moolenaardac13472019-09-16 21:06:21 +02002307 mch_settitle(((which & SAVE_RESTORE_TITLE) && unix_did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002309 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2310
Bram Moolenaare5c83282019-05-03 23:15:37 +02002311 if (do_push_pop)
2312 {
2313 // pop and push from/to the stack
2314 term_pop_title(which);
2315 term_push_title(which);
2316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317}
2318
Bram Moolenaar0f873732019-12-05 20:28:46 +01002319#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
2321/*
2322 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002323 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 */
2325 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002326vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
2328 if (name == NULL)
2329 return FALSE;
2330 return (STRNICMP(name, "xterm", 5) == 0
2331 || STRNICMP(name, "nxterm", 6) == 0
2332 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002333 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002335 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 || STRCMP(name, "builtin_xterm") == 0);
2337}
2338
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002339#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2340/*
2341 * Return TRUE if "name" appears to be that of a terminal
2342 * known to support the xterm-style mouse protocol.
2343 * Relies on term_is_xterm having been set to its correct value.
2344 */
2345 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002346use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002347{
2348 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002349 && (term_is_xterm
2350 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002351 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002352 || STRICMP(name, "st") == 0
2353 || STRNICMP(name, "st-", 3) == 0
2354 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002355}
2356#endif
2357
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358/*
2359 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2360 * Return 1 for "xterm".
2361 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002362 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002363 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 */
2365 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002366use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002368 if (ttym_flags == TTYM_SGR)
2369 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002370 if (ttym_flags == TTYM_URXVT)
2371 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 if (ttym_flags == TTYM_XTERM2)
2373 return 2;
2374 if (ttym_flags == TTYM_XTERM)
2375 return 1;
2376 return 0;
2377}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002380vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 if (name == NULL)
2383 return FALSE;
2384 return (STRNICMP(name, "iris-ansi", 9) == 0
2385 || STRCMP(name, "builtin_iris-ansi") == 0);
2386}
2387
2388 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002389vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
2391 if (name == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002392 return FALSE; // actually all ANSI comp. terminals should be here
2393 // catch VT100 - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002394 return ((STRNICMP(name, "vt", 2) == 0
2395 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 || STRCMP(name, "builtin_vt320") == 0);
2397}
2398
2399/*
2400 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2401 * This should include all windowed terminal emulators.
2402 */
2403 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002404vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
2406 if (name == NULL)
2407 return FALSE;
2408 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2409 return TRUE;
2410 return ( STRNICMP(name, "hpterm", 6) == 0
2411 || STRNICMP(name, "sun-cmd", 7) == 0
2412 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002413 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 || STRNICMP(name, "dtterm", 6) == 0);
2415}
2416
2417/*
2418 * Insert user name in s[len].
2419 * Return OK if a name found.
2420 */
2421 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002422mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423{
2424#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002425 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 return OK;
2427#else
2428 return mch_get_uname(getuid(), s, len);
2429#endif
2430}
2431
2432/*
2433 * Insert user name for "uid" in s[len].
2434 * Return OK if a name found.
2435 */
2436 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002437mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2440 struct passwd *pw;
2441
2442 if ((pw = getpwuid(uid)) != NULL
2443 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2444 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002445 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 return OK;
2447 }
2448#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002449 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2450 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451}
2452
2453/*
2454 * Insert host name is s[len].
2455 */
2456
2457#ifdef HAVE_SYS_UTSNAME_H
2458 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002459mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460{
2461 struct utsname vutsname;
2462
2463 if (uname(&vutsname) < 0)
2464 *s = NUL;
2465 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002466 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002468#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
2470# ifdef HAVE_SYS_SYSTEMINFO_H
2471# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2472# endif
2473
2474 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002475mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476{
2477# ifdef VAXC
2478 vaxc$gethostname((char *)s, len);
2479# else
2480 gethostname((char *)s, len);
2481# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002482 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002484#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
2486/*
2487 * return process ID
2488 */
2489 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002490mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491{
2492 return (long)getpid();
2493}
2494
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002495/*
2496 * return TRUE if process "pid" is still running
2497 */
2498 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002499mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002500{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002501 // If there is no error the process must be running.
2502 if (kill(pid, 0) == 0)
2503 return TRUE;
2504#ifdef ESRCH
2505 // If the error is ESRCH then the process is not running.
2506 if (errno == ESRCH)
2507 return FALSE;
2508#endif
2509 // If the process is running and owned by another user we get EPERM. With
2510 // other errors the process might be running, assuming it is then.
2511 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002512}
2513
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002516strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517{
2518 extern int sys_nerr;
2519 extern char *sys_errlist[];
2520 static char er[20];
2521
2522 if (err > 0 && err < sys_nerr)
2523 return (sys_errlist[err]);
2524 sprintf(er, "Error %d", err);
2525 return er;
2526}
2527#endif
2528
2529/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002530 * Get name of current directory into buffer "buf" of length "len" bytes.
2531 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 * Return OK for success, FAIL for failure.
2533 */
2534 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002535mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
2537#if defined(USE_GETCWD)
2538 if (getcwd((char *)buf, len) == NULL)
2539 {
2540 STRCPY(buf, strerror(errno));
2541 return FAIL;
2542 }
2543 return OK;
2544#else
2545 return (getwd((char *)buf) != NULL ? OK : FAIL);
2546#endif
2547}
2548
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002550 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 *
2552 * return FAIL for failure, OK for success
2553 */
2554 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002555mch_FullName(
2556 char_u *fname,
2557 char_u *buf,
2558 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002559 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
2561 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002562#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002564 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 char_u olddir[MAXPATHL];
2567 char_u *p;
2568 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002569#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002570 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2571 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002572#endif
2573
Bram Moolenaar38323e42007-03-06 19:22:53 +00002574#ifdef VMS
2575 fname = vms_fixfilename(fname);
2576#endif
2577
Bram Moolenaara2442432007-04-26 14:26:37 +00002578#ifdef __CYGWIN__
2579 /*
2580 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2581 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002582# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002583 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2584 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002585 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2586 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002587# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002588 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002589# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002590 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002591#endif
2592
Bram Moolenaar0f873732019-12-05 20:28:46 +01002593 // Expand it if forced or not an absolute path.
2594 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002595 if ((force || !mch_isFullName(fname))
2596 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 {
2598 /*
2599 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002600 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 * This will get the correct path name with "../" things.
2602 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002603 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002605 if (STRCMP(p, "/..") == 0)
2606 // for "/path/dir/.." include the "/.."
2607 p += 3;
2608
Bram Moolenaar38323e42007-03-06 19:22:53 +00002609#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 /*
2611 * Use fchdir() if possible, it's said to be faster and more
2612 * reliable. But on SunOS 4 it might not work. Check this by
2613 * doing a fchdir() right now.
2614 */
2615 if (!dont_fchdir)
2616 {
2617 fd = open(".", O_RDONLY | O_EXTRA, 0);
2618 if (fd >= 0 && fchdir(fd) < 0)
2619 {
2620 close(fd);
2621 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002622 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
2624 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
Bram Moolenaar0f873732019-12-05 20:28:46 +01002627 // Only change directory when we are sure we can return to where
2628 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002630#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 (mch_dirname(olddir, MAXPATHL) == FAIL
2634 || mch_chdir((char *)olddir) != 0))
2635 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002636 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 retval = FAIL;
2638 }
2639 else
2640 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002641 // The directory is copied into buf[], to be able to remove
2642 // the file name without changing it (could be a string in
2643 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if (p - fname >= len)
2645 retval = FAIL;
2646 else
2647 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002648 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002650 {
2651 // Path does not exist (yet). For a full path fail,
2652 // will use the path as-is. For a relative path use
2653 // the current directory and append the file name.
2654 if (mch_isFullName(fname))
2655 retval = FAIL;
2656 else
2657 p = NULL;
2658 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002659 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002661 else
2662 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 *buf = NUL;
2664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
2666 }
2667 if (mch_dirname(buf, len) == FAIL)
2668 {
2669 retval = FAIL;
2670 *buf = NUL;
2671 }
2672 if (p != NULL)
2673 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002674#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 if (fd >= 0)
2676 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002677 if (p_verbose >= 5)
2678 {
2679 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002680 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002681 verbose_leave();
2682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 l = fchdir(fd);
2684 close(fd);
2685 }
2686 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 l = mch_chdir((char *)olddir);
2689 if (l != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002690 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692
2693 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002694 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002695 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002696#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002697 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002699 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002702
Bram Moolenaar0f873732019-12-05 20:28:46 +01002703 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002704 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 return FAIL;
2706
Bram Moolenaar0f873732019-12-05 20:28:46 +01002707 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (STRCMP(fname, ".") != 0)
2709 STRCAT(buf, fname);
2710
2711 return OK;
2712}
2713
2714/*
2715 * Return TRUE if "fname" does not depend on the current directory.
2716 */
2717 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002718mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002720#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 return ( fname[0] == '/' || fname[0] == '.' ||
2722 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2723 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2724 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002725#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727#endif
2728}
2729
Bram Moolenaar24552be2005-12-10 20:17:30 +00002730#if defined(USE_FNAME_CASE) || defined(PROTO)
2731/*
2732 * Set the case of the file name, if it already exists. This will cause the
2733 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002734 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002735 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002736 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002737fname_case(
2738 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002739 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002740{
2741 struct stat st;
2742 char_u *slash, *tail;
2743 DIR *dirp;
2744 struct dirent *dp;
2745
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002746 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002747 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002748 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002749 slash = vim_strrchr(name, '/');
2750 if (slash == NULL)
2751 {
2752 dirp = opendir(".");
2753 tail = name;
2754 }
2755 else
2756 {
2757 *slash = NUL;
2758 dirp = opendir((char *)name);
2759 *slash = '/';
2760 tail = slash + 1;
2761 }
2762
2763 if (dirp != NULL)
2764 {
2765 while ((dp = readdir(dirp)) != NULL)
2766 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002767 // Only accept names that differ in case and are the same byte
2768 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002769 if (STRICMP(tail, dp->d_name) == 0
2770 && STRLEN(tail) == STRLEN(dp->d_name))
2771 {
2772 char_u newname[MAXPATHL + 1];
2773 struct stat st2;
2774
Bram Moolenaar0f873732019-12-05 20:28:46 +01002775 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002776 vim_strncpy(newname, name, MAXPATHL);
2777 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2778 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002779 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002780 && st.st_ino == st2.st_ino
2781 && st.st_dev == st2.st_dev)
2782 {
2783 STRCPY(tail, dp->d_name);
2784 break;
2785 }
2786 }
2787 }
2788
2789 closedir(dirp);
2790 }
2791 }
2792}
2793#endif
2794
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795/*
2796 * Get file permissions for 'name'.
2797 * Returns -1 when it doesn't exist.
2798 */
2799 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002800mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
2802 struct stat statb;
2803
Bram Moolenaar0f873732019-12-05 20:28:46 +01002804 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805#ifdef VMS
2806 if (stat((char *)vms_fixfilename(name), &statb))
2807#else
2808 if (stat((char *)name, &statb))
2809#endif
2810 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002811#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002812 // The top bit makes the value negative, which means the file doesn't
2813 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002814 return statb.st_mode & ~S_ADDACE;
2815#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818}
2819
2820/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002821 * Set file permission for "name" to "perm".
2822 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 */
2824 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002825mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
2827 return (chmod((char *)
2828#ifdef VMS
2829 vms_fixfilename(name),
2830#else
2831 name,
2832#endif
2833 (mode_t)perm) == 0 ? OK : FAIL);
2834}
2835
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002836#if defined(HAVE_FCHMOD) || defined(PROTO)
2837/*
2838 * Set file permission for open file "fd" to "perm".
2839 * Return FAIL for failure, OK otherwise.
2840 */
2841 int
2842mch_fsetperm(int fd, long perm)
2843{
2844 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2845}
2846#endif
2847
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848#if defined(HAVE_ACL) || defined(PROTO)
2849# ifdef HAVE_SYS_ACL_H
2850# include <sys/acl.h>
2851# endif
2852# ifdef HAVE_SYS_ACCESS_H
2853# include <sys/access.h>
2854# endif
2855
2856# ifdef HAVE_SOLARIS_ACL
2857typedef struct vim_acl_solaris_T {
2858 int acl_cnt;
2859 aclent_t *acl_entry;
2860} vim_acl_solaris_T;
2861# endif
2862
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002863#if defined(HAVE_SELINUX) || defined(PROTO)
2864/*
2865 * Copy security info from "from_file" to "to_file".
2866 */
2867 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002868mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002869{
2870 if (from_file == NULL)
2871 return;
2872
2873 if (selinux_enabled == -1)
2874 selinux_enabled = is_selinux_enabled();
2875
2876 if (selinux_enabled > 0)
2877 {
Bram Moolenaar89560232020-10-09 23:04:47 +02002878 // Use "char *" instead of "security_context_t" to avoid a deprecation
2879 // warning.
2880 char *from_context = NULL;
2881 char *to_context = NULL;
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002882
2883 if (getfilecon((char *)from_file, &from_context) < 0)
2884 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002885 // If the filesystem doesn't support extended attributes,
2886 // the original had no special security context and the
2887 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002888 if (errno == EOPNOTSUPP)
2889 return;
2890
Bram Moolenaar32526b32019-01-19 17:43:09 +01002891 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002892 msg_outtrans(from_file);
2893 msg_putchar('\n');
2894 return;
2895 }
2896 if (getfilecon((char *)to_file, &to_context) < 0)
2897 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002898 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002899 msg_outtrans(to_file);
2900 msg_putchar('\n');
2901 freecon (from_context);
2902 return ;
2903 }
2904 if (strcmp(from_context, to_context) != 0)
2905 {
2906 if (setfilecon((char *)to_file, from_context) < 0)
2907 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002908 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002909 msg_outtrans(to_file);
2910 msg_putchar('\n');
2911 }
2912 }
2913 freecon(to_context);
2914 freecon(from_context);
2915 }
2916}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002917#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002918
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002919#if defined(HAVE_SMACK) && !defined(PROTO)
2920/*
2921 * Copy security info from "from_file" to "to_file".
2922 */
2923 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002924mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002925{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002926 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002927 {
2928 XATTR_NAME_SMACK,
2929 XATTR_NAME_SMACKEXEC,
2930 XATTR_NAME_SMACKMMAP
2931 };
2932
2933 char buffer[SMACK_LABEL_LEN];
2934 const char *name;
2935 int index;
2936 int ret;
2937 ssize_t size;
2938
2939 if (from_file == NULL)
2940 return;
2941
2942 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2943 / sizeof(smack_copied_attributes)[0]) ; index++)
2944 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002945 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002946 name = smack_copied_attributes[index];
2947
Bram Moolenaar0f873732019-12-05 20:28:46 +01002948 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002949 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2950 if (size >= 0)
2951 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002952 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002953 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2954 if (ret < 0)
2955 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002956 vim_snprintf((char *)IObuff, IOSIZE,
2957 _("Could not set security context %s for %s"),
2958 name, to_file);
2959 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002960 msg_putchar('\n');
2961 }
2962 }
2963 else
2964 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002965 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002966 switch (errno)
2967 {
2968 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002969 // extended attributes aren't supported or enabled
2970 // should a message be echoed? not sure...
2971 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002972
2973 case ERANGE:
2974 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002975 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002976 vim_snprintf((char *)IObuff, IOSIZE,
2977 _("Could not get security context %s for %s. Removing it!"),
2978 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002979 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002980 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01002981 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002982
2983 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002984 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002985 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002986 // Silently ignore errors, apparently this happens when
2987 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002988 break;
2989 }
2990 }
2991 }
2992}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002993#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002994
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995/*
2996 * Return a pointer to the ACL of file "fname" in allocated memory.
2997 * Return NULL if the ACL is not available for whatever reason.
2998 */
2999 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003000mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
3002 vim_acl_T ret = NULL;
3003#ifdef HAVE_POSIX_ACL
3004 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3005#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003006#ifdef HAVE_SOLARIS_ZFS_ACL
3007 acl_t *aclent;
3008
3009 if (acl_get((char *)fname, 0, &aclent) < 0)
3010 return NULL;
3011 ret = (vim_acl_T)aclent;
3012#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013#ifdef HAVE_SOLARIS_ACL
3014 vim_acl_solaris_T *aclent;
3015
3016 aclent = malloc(sizeof(vim_acl_solaris_T));
3017 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3018 {
3019 free(aclent);
3020 return NULL;
3021 }
3022 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3023 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3024 {
3025 free(aclent->acl_entry);
3026 free(aclent);
3027 return NULL;
3028 }
3029 ret = (vim_acl_T)aclent;
3030#else
3031#if defined(HAVE_AIX_ACL)
3032 int aclsize;
3033 struct acl *aclent;
3034
3035 aclsize = sizeof(struct acl);
3036 aclent = malloc(aclsize);
3037 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3038 {
3039 if (errno == ENOSPC)
3040 {
3041 aclsize = aclent->acl_len;
3042 aclent = realloc(aclent, aclsize);
3043 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3044 {
3045 free(aclent);
3046 return NULL;
3047 }
3048 }
3049 else
3050 {
3051 free(aclent);
3052 return NULL;
3053 }
3054 }
3055 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003056#endif // HAVE_AIX_ACL
3057#endif // HAVE_SOLARIS_ACL
3058#endif // HAVE_SOLARIS_ZFS_ACL
3059#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 return ret;
3061}
3062
3063/*
3064 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3065 */
3066 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003067mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069 if (aclent == NULL)
3070 return;
3071#ifdef HAVE_POSIX_ACL
3072 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3073#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003074#ifdef HAVE_SOLARIS_ZFS_ACL
3075 acl_set((char *)fname, (acl_t *)aclent);
3076#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077#ifdef HAVE_SOLARIS_ACL
3078 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3079 ((vim_acl_solaris_T *)aclent)->acl_entry);
3080#else
3081#ifdef HAVE_AIX_ACL
3082 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003083#endif // HAVE_AIX_ACL
3084#endif // HAVE_SOLARIS_ACL
3085#endif // HAVE_SOLARIS_ZFS_ACL
3086#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087}
3088
3089 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003090mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
3092 if (aclent == NULL)
3093 return;
3094#ifdef HAVE_POSIX_ACL
3095 acl_free((acl_t)aclent);
3096#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003097#ifdef HAVE_SOLARIS_ZFS_ACL
3098 acl_free((acl_t *)aclent);
3099#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100#ifdef HAVE_SOLARIS_ACL
3101 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3102 free(aclent);
3103#else
3104#ifdef HAVE_AIX_ACL
3105 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003106#endif // HAVE_AIX_ACL
3107#endif // HAVE_SOLARIS_ACL
3108#endif // HAVE_SOLARIS_ZFS_ACL
3109#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110}
3111#endif
3112
3113/*
3114 * Set hidden flag for "name".
3115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003117mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003119 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120}
3121
3122/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003123 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 * return FALSE if "name" is not a directory
3125 * return FALSE for error
3126 */
3127 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003128mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 struct stat statb;
3131
Bram Moolenaar0f873732019-12-05 20:28:46 +01003132 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 return FALSE;
3134 if (stat((char *)name, &statb))
3135 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137}
3138
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003139/*
3140 * return TRUE if "name" is a directory, NOT a symlink to a directory
3141 * return FALSE if "name" is not a directory
3142 * return FALSE for error
3143 */
3144 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003145mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003146{
3147 struct stat statb;
3148
Bram Moolenaar0f873732019-12-05 20:28:46 +01003149 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003150 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003151 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003152 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003153 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003154}
3155
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156/*
3157 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3158 */
3159 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003160executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161{
3162 struct stat st;
3163
3164 if (stat((char *)name, &st))
3165 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003166#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003167 // Like on Unix system file can have executable rights but not necessarily
3168 // be an executable, but on Unix is not a default for an ordinary file to
3169 // have an executable flag - on VMS it is in most cases.
3170 // Therefore, this check does not have any sense - let keep us to the
3171 // conventions instead:
3172 // *.COM and *.EXE files are the executables - the rest are not. This is
3173 // not ideal but better then it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003174 int vms_executable = 0;
3175 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3176 {
3177 if (strstr(vms_tolower((char*)name),".exe") != NULL
3178 || strstr(vms_tolower((char*)name),".com")!= NULL)
3179 vms_executable = 1;
3180 }
3181 return vms_executable;
3182#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185}
3186
3187/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003188 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003189 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 * Return -1 if unknown.
3191 */
3192 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003193mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194{
3195 char_u *buf;
3196 char_u *p, *e;
3197 int retval;
3198
Bram Moolenaar0f873732019-12-05 20:28:46 +01003199 // When "use_path" is false and if it's an absolute or relative path don't
3200 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003201 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003202 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003203 // There must be a path separator, files in the current directory
3204 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003205 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003206 {
3207 if (path != NULL)
3208 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003209 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003210 *path = FullName_save(name, TRUE);
3211 else
3212 *path = vim_strsave(name);
3213 }
3214 return TRUE;
3215 }
3216 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
3219 p = (char_u *)getenv("PATH");
3220 if (p == NULL || *p == NUL)
3221 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003222 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (buf == NULL)
3224 return -1;
3225
3226 /*
3227 * Walk through all entries in $PATH to check if "name" exists there and
3228 * is an executable file.
3229 */
3230 for (;;)
3231 {
3232 e = (char_u *)strchr((char *)p, ':');
3233 if (e == NULL)
3234 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003235 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 STRCPY(buf, "./");
3237 else
3238 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003239 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 add_pathsep(buf);
3241 }
3242 STRCAT(buf, name);
3243 retval = executable_file(buf);
3244 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003245 {
3246 if (path != NULL)
3247 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003248 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003249 *path = FullName_save(buf, TRUE);
3250 else
3251 *path = vim_strsave(buf);
3252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
3256 if (*e != ':')
3257 break;
3258 p = e + 1;
3259 }
3260
3261 vim_free(buf);
3262 return retval;
3263}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264
3265/*
3266 * Check what "name" is:
3267 * NODE_NORMAL: file or directory (or doesn't exist)
3268 * NODE_WRITABLE: writable device, socket, fifo, etc.
3269 * NODE_OTHER: non-writable things
3270 */
3271 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003272mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
3274 struct stat st;
3275
3276 if (stat((char *)name, &st))
3277 return NODE_NORMAL;
3278 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3279 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003280 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003282 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 return NODE_WRITABLE;
3284}
3285
3286 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003287mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288{
3289#ifdef HAVE_CHECK_STACK_GROWTH
3290 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 check_stack_growth((char *)&i);
3293
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003294# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 get_stack_limit();
3296# endif
3297
3298#endif
3299
3300 /*
3301 * Setup an alternative stack for signals. Helps to catch signals when
3302 * running out of stack space.
3303 * Use of sigaltstack() is preferred, it's more portable.
3304 * Ignore any errors.
3305 */
3306#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003307 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 init_signal_stack();
3309#endif
3310}
3311
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003312#if defined(EXITFREE) || defined(PROTO)
3313 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003314mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003315{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003316# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3317 if (clip_star.owned)
3318 clip_lose_selection(&clip_star);
3319 if (clip_plus.owned)
3320 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003321# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003322# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003323 if (xterm_Shell != (Widget)0)
3324 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003325# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003326 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003327 if (xterm_dpy != NULL)
3328 XtCloseDisplay(xterm_dpy);
3329 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003330 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003331 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003332# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003333 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003334# endif
3335 }
3336# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003337# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003338# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003339 if (x11_display != NULL
3340# ifdef FEAT_XCLIPBOARD
3341 && x11_display != xterm_dpy
3342# endif
3343 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003344 XCloseDisplay(x11_display);
3345# endif
3346# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003347 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003348# endif
3349# ifdef FEAT_TITLE
3350 vim_free(oldtitle);
3351 vim_free(oldicon);
3352# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003353}
3354#endif
3355
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356/*
3357 * Output a newline when exiting.
3358 * Make sure the newline goes to the same stream as the text.
3359 */
3360 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003361exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003363 if (silent_mode)
3364 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (newline_on_exit || msg_didout)
3366 {
3367 if (msg_use_printf())
3368 {
3369 if (info_message)
3370 mch_msg("\n");
3371 else
3372 mch_errmsg("\r\n");
3373 }
3374 else
3375 out_char('\n');
3376 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003377 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003379 restore_cterm_colors(); // get original colors back
3380 msg_clr_eos_force(); // clear the rest of the display
3381 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
3383}
3384
Bram Moolenaarb4151682020-05-11 22:13:28 +02003385#ifdef USE_GCOV_FLUSH
3386extern void __gcov_flush();
3387#endif
3388
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003390mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
3392 exiting = TRUE;
3393
3394#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3395 x11_export_final_selection();
3396#endif
3397
3398#ifdef FEAT_GUI
3399 if (!gui.in_use)
3400#endif
3401 {
3402 settmode(TMODE_COOK);
3403#ifdef FEAT_TITLE
Bram Moolenaar7007e312021-03-27 12:11:33 +01003404 if (!is_not_a_term())
3405 {
3406 // restore xterm title and icon name
3407 mch_restore_title(SAVE_RESTORE_BOTH);
3408 term_pop_title(SAVE_RESTORE_BOTH);
3409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410#endif
3411 /*
3412 * When t_ti is not empty but it doesn't cause swapping terminal
3413 * pages, need to output a newline when msg_didout is set. But when
3414 * t_ti does swap pages it should not go to the shell page. Do this
3415 * before stoptermcap().
3416 */
3417 if (swapping_screen() && !newline_on_exit)
3418 exit_scroll();
3419
Bram Moolenaar0f873732019-12-05 20:28:46 +01003420 // Stop termcap: May need to check for T_CRV response, which
3421 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 stoptermcap();
3423
3424 /*
3425 * A newline is only required after a message in the alternate screen.
3426 * This is set to TRUE by wait_return().
3427 */
3428 if (!swapping_screen() || newline_on_exit)
3429 exit_scroll();
3430
Bram Moolenaar0f873732019-12-05 20:28:46 +01003431 // Cursor may have been switched off without calling starttermcap()
3432 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 if (full_screen)
3434 cursor_on();
3435 }
3436 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003437 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003438
3439#ifdef USE_GCOV_FLUSH
3440 // Flush coverage info before possibly being killed by a deadly signal.
3441 __gcov_flush();
3442#endif
3443
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 may_core_dump();
3445#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 gui_exit(r);
3448#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003449
Bram Moolenaar56718732006-03-15 22:53:57 +00003450#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003451 mac_conv_cleanup();
3452#endif
3453
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003455 // A core dump won't be created if the signal handler
3456 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (deadly_signal != 0)
3458 return;
3459#endif
3460
Bram Moolenaar009b2592004-10-24 19:18:58 +00003461#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003462 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003463#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003464
3465#ifdef EXITFREE
3466 free_all_mem();
3467#endif
3468
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 exit(r);
3470}
3471
3472 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003473may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474{
3475 if (deadly_signal != 0)
3476 {
3477 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003478 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480}
3481
3482#ifndef VMS
3483
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003484/*
3485 * Get the file descriptor to use for tty operations.
3486 */
3487 static int
3488get_tty_fd(int fd)
3489{
3490 int tty_fd = fd;
3491
3492#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3493 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3494 // "fd" when it is a master device.
3495 if (mch_isatty(fd) > 1)
3496 {
3497 char *name;
3498
3499 name = ptsname(fd);
3500 if (name == NULL)
3501 return -1;
3502
3503 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3504 if (tty_fd < 0)
3505 return -1;
3506 }
3507#endif
3508 return tty_fd;
3509}
3510
3511 static int
3512mch_tcgetattr(int fd, void *term)
3513{
3514 int tty_fd;
3515 int retval = -1;
3516
3517 tty_fd = get_tty_fd(fd);
3518 if (tty_fd >= 0)
3519 {
3520#ifdef NEW_TTY_SYSTEM
3521# ifdef HAVE_TERMIOS_H
3522 retval = tcgetattr(tty_fd, (struct termios *)term);
3523# else
3524 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3525# endif
3526#else
3527 // for "old" tty systems
3528 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3529#endif
3530 if (tty_fd != fd)
3531 close(tty_fd);
3532 }
3533 return retval;
3534}
3535
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003537mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538{
3539 static int first = TRUE;
3540
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003541#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542# ifdef HAVE_TERMIOS_H
3543 static struct termios told;
3544 struct termios tnew;
3545# else
3546 static struct termio told;
3547 struct termio tnew;
3548# endif
3549
3550 if (first)
3551 {
3552 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003553 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555
3556 tnew = told;
3557 if (tmode == TMODE_RAW)
3558 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003559 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003560 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3561 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003563# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003564 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565# endif
3566 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003567# ifdef ONLCR
3568 // Don't map NL -> CR NL, we do it ourselves.
3569 // Also disable expanding tabs if possible.
3570# ifdef XTABS
3571 tnew.c_oflag &= ~(ONLCR | XTABS);
3572# else
3573# ifdef TAB3
3574 tnew.c_oflag &= ~(ONLCR | TAB3);
3575# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003577# endif
3578# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003580 tnew.c_cc[VMIN] = 1; // return after 1 char
3581 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
3583 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003584 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003585 // Also reset ICANON here, otherwise on Solaris select() won't see
3586 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003587 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003588 tnew.c_cc[VMIN] = 1; // return after 1 char
3589 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
3592# if defined(HAVE_TERMIOS_H)
3593 {
3594 int n = 10;
3595
Bram Moolenaar0f873732019-12-05 20:28:46 +01003596 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3597 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3599 && errno == EINTR && n > 0)
3600 --n;
3601 }
3602# else
3603 ioctl(read_cmd_fd, TCSETA, &tnew);
3604# endif
3605
3606#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 /*
3608 * for "old" tty systems
3609 */
3610# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003611# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612# endif
3613 static struct sgttyb ttybold;
3614 struct sgttyb ttybnew;
3615
3616 if (first)
3617 {
3618 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003619 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 }
3621
3622 ttybnew = ttybold;
3623 if (tmode == TMODE_RAW)
3624 {
3625 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3626 ttybnew.sg_flags |= RAW;
3627 }
3628 else if (tmode == TMODE_SLEEP)
3629 ttybnew.sg_flags &= ~(ECHO);
3630 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3631#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003632 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633}
3634
3635/*
3636 * Try to get the code for "t_kb" from the stty setting
3637 *
3638 * Even if termcap claims a backspace key, the user's setting *should*
3639 * prevail. stty knows more about reality than termcap does, and if
3640 * somebody's usual erase key is DEL (which, for most BSD users, it will
3641 * be), they're going to get really annoyed if their erase key starts
3642 * doing forward deletes for no reason. (Eric Fischer)
3643 */
3644 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003645get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003647 ttyinfo_T info;
3648 char_u buf[2];
3649 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003651 if (get_tty_info(read_cmd_fd, &info) == OK)
3652 {
3653 intr_char = info.interrupt;
3654 buf[0] = info.backspace;
3655 buf[1] = NUL;
3656 add_termcode((char_u *)"kb", buf, FALSE);
3657
Bram Moolenaar0f873732019-12-05 20:28:46 +01003658 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003659 p = find_termcode((char_u *)"kD");
3660 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3661 do_fixdel(NULL);
3662 }
3663}
3664
3665/*
3666 * Obtain the characters that Backspace and Enter produce on "fd".
3667 * Returns OK or FAIL.
3668 */
3669 int
3670get_tty_info(int fd, ttyinfo_T *info)
3671{
3672#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673# ifdef HAVE_TERMIOS_H
3674 struct termios keys;
3675# else
3676 struct termio keys;
3677# endif
3678
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003679 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003681 info->backspace = keys.c_cc[VERASE];
3682 info->interrupt = keys.c_cc[VINTR];
3683 if (keys.c_iflag & ICRNL)
3684 info->enter = NL;
3685 else
3686 info->enter = CAR;
3687 if (keys.c_oflag & ONLCR)
3688 info->nl_does_cr = TRUE;
3689 else
3690 info->nl_does_cr = FALSE;
3691 return OK;
3692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003694 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 struct sgttyb keys;
3696
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003697 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003699 info->backspace = keys.sg_erase;
3700 info->interrupt = keys.sg_kill;
3701 info->enter = CAR;
3702 info->nl_does_cr = TRUE;
3703 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003706 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707}
3708
Bram Moolenaar0f873732019-12-05 20:28:46 +01003709#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003711static int mouse_ison = FALSE;
3712
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713/*
3714 * Set mouse clicks on or off.
3715 */
3716 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003717mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003719#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003720 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 int xterm_mouse_vers;
3723
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003724#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003725 if (!on)
3726 // Make sure not tracing mouse movements. Important when a button-down
3727 // was received but no release yet.
3728 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003729#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003730
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003731 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003732#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003733 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003734#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003735 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003736 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 return;
3738
3739 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003740
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003741#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003742 if (ttym_flags == TTYM_URXVT)
3743 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003744 out_str_nf((char_u *)
3745 (on
3746 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3747 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003748 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003749 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003750#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003751
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003752 if (ttym_flags == TTYM_SGR)
3753 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003754 // SGR mode supports columns above 223
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003755 out_str_nf((char_u *)
3756 (on
3757 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3758 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003759 mouse_ison = on;
3760 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003761
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003762#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003763 if (bevalterm_ison != (p_bevalterm && on))
3764 {
3765 bevalterm_ison = (p_bevalterm && on);
3766 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003767 // disable mouse movement events, enabling is below
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003768 out_str_nf((char_u *)
3769 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003770 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003771#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003772
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 if (xterm_mouse_vers > 0)
3774 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003775 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 out_str_nf((char_u *)
3777 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003778 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003779#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003780 bevalterm_ison
3781 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003782#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003783 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003785 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 out_str_nf((char_u *)
3787 (xterm_mouse_vers > 1
3788 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3789 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003790 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003793#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 else if (ttym_flags == TTYM_DEC)
3795 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003796 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003798 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003800 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003804#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 else
3806 {
3807 if (on)
3808 {
3809 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003810 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812 else
3813 {
3814 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003815 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 }
3817 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003820#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003821 else
3822 {
3823 if (on)
3824 {
3825 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003826 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003827 }
3828 else
3829 {
3830 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003831 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003832 }
3833 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003834#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003835
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003836#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 else
3838 {
3839 if (on)
3840 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003841 // D - Enable Mouse up/down messages
3842 // L - Enable Left Button Reporting
3843 // M - Enable Middle Button Reporting
3844 // R - Enable Right Button Reporting
3845 // K - Enable SHIFT and CTRL key Reporting
3846 // + - Enable Advanced messaging of mouse moves and up/down messages
3847 // Q - Quiet No Ack
3848 // # - Numeric value of mouse pointer required
3849 // 0 = Multiview 2000 cursor, used as standard
3850 // 1 = Windows Arrow
3851 // 2 = Windows I Beam
3852 // 3 = Windows Hour Glass
3853 // 4 = Windows Cross Hair
3854 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003855# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003856 // Disables full feedback of pointer movements
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3858 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003859# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3861 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003862# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003863 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
3865 else
3866 {
3867 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3868 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003869 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 }
3871 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003872#endif
3873#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 else
3875 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003876 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 if (on)
3878 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3879 else
3880 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003881 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884}
3885
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003886#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003887/*
3888 * Called when 'balloonevalterm' changed.
3889 */
3890 void
3891mch_bevalterm_changed(void)
3892{
3893 mch_setmouse(mouse_ison);
3894}
3895#endif
3896
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897/*
3898 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3899 */
3900 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003901check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902{
3903# ifdef FEAT_MOUSE_XTERM
3904 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003905# ifdef FEAT_MOUSE_URXVT
3906 && use_xterm_mouse() != 3
3907# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908# ifdef FEAT_GUI
3909 && !gui.in_use
3910# endif
3911 )
3912 {
3913 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003914 ? IF_EB("\233M", CSI_STR "M")
3915 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 if (*p_mouse != NUL)
3917 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003918 // force mouse off and maybe on to send possibly new mouse
3919 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 mch_setmouse(FALSE);
3921 setmouse();
3922 }
3923 }
3924 else
3925 del_mouse_termcode(KS_MOUSE);
3926# endif
3927
3928# ifdef FEAT_MOUSE_GPM
3929 if (!use_xterm_mouse()
3930# ifdef FEAT_GUI
3931 && !gui.in_use
3932# endif
3933 )
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003934 set_mouse_termcode(KS_GPM_MOUSE,
3935 (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3936 else
3937 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938# endif
3939
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003940# ifdef FEAT_SYSMOUSE
3941 if (!use_xterm_mouse()
3942# ifdef FEAT_GUI
3943 && !gui.in_use
3944# endif
3945 )
3946 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3947# endif
3948
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003950 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 if (!use_xterm_mouse()
3952# ifdef FEAT_GUI
3953 && !gui.in_use
3954# endif
3955 )
3956 set_mouse_termcode(KS_JSBTERM_MOUSE,
3957 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3958 else
3959 del_mouse_termcode(KS_JSBTERM_MOUSE);
3960# endif
3961
3962# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003963 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3964 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 if (!use_xterm_mouse()
3966# ifdef FEAT_GUI
3967 && !gui.in_use
3968# endif
3969 )
3970 set_mouse_termcode(KS_NETTERM_MOUSE,
3971 (char_u *)IF_EB("\033}", ESC_STR "}"));
3972 else
3973 del_mouse_termcode(KS_NETTERM_MOUSE);
3974# endif
3975
3976# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003977 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003978 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979# ifdef FEAT_GUI
3980 && !gui.in_use
3981# endif
3982 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003983 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3984 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 else
3986 del_mouse_termcode(KS_DEC_MOUSE);
3987# endif
3988# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01003989 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003990 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991# ifdef FEAT_GUI
3992 && !gui.in_use
3993# endif
3994 )
3995 set_mouse_termcode(KS_PTERM_MOUSE,
3996 (char_u *) IF_EB("\033[", ESC_STR "["));
3997 else
3998 del_mouse_termcode(KS_PTERM_MOUSE);
3999# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004000# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004001 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004002# ifdef FEAT_GUI
4003 && !gui.in_use
4004# endif
4005 )
4006 {
4007 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02004008 ? IF_EB("\233*M", CSI_STR "*M")
4009 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004010
4011 if (*p_mouse != NUL)
4012 {
4013 mch_setmouse(FALSE);
4014 setmouse();
4015 }
4016 }
4017 else
4018 del_mouse_termcode(KS_URXVT_MOUSE);
4019# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004020 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004021# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004022 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004023# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004024 )
4025 {
4026 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02004027 ? IF_EB("\233<*M", CSI_STR "<*M")
4028 : IF_EB("\033[<*M", ESC_STR "[<*M")));
4029
4030 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
4031 ? IF_EB("\233<*m", CSI_STR "<*m")
4032 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004033
4034 if (*p_mouse != NUL)
4035 {
4036 mch_setmouse(FALSE);
4037 setmouse();
4038 }
4039 }
4040 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004041 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004042 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004043 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047#ifndef VMS
4048
4049/*
4050 * Try to get the current window size:
4051 * 1. with an ioctl(), most accurate method
4052 * 2. from the environment variables LINES and COLUMNS
4053 * 3. from the termcap
4054 * 4. keep using the old values
4055 * Return OK when size could be determined, FAIL otherwise.
4056 */
4057 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004058mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059{
4060 long rows = 0;
4061 long columns = 0;
4062 char_u *p;
4063
4064 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 * 1. try using an ioctl. It is the most accurate method.
4066 *
4067 * Try using TIOCGWINSZ first, some systems that have it also define
4068 * TIOCGSIZE but don't have a struct ttysize.
4069 */
4070# ifdef TIOCGWINSZ
4071 {
4072 struct winsize ws;
4073 int fd = 1;
4074
Bram Moolenaar0f873732019-12-05 20:28:46 +01004075 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 if (!isatty(fd) && isatty(read_cmd_fd))
4077 fd = read_cmd_fd;
4078 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4079 {
4080 columns = ws.ws_col;
4081 rows = ws.ws_row;
4082 }
4083 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004084# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085# ifdef TIOCGSIZE
4086 {
4087 struct ttysize ts;
4088 int fd = 1;
4089
Bram Moolenaar0f873732019-12-05 20:28:46 +01004090 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 if (!isatty(fd) && isatty(read_cmd_fd))
4092 fd = read_cmd_fd;
4093 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4094 {
4095 columns = ts.ts_cols;
4096 rows = ts.ts_lines;
4097 }
4098 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004099# endif // TIOCGSIZE
4100# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101
4102 /*
4103 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004104 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4105 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004107 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 {
4109 if ((p = (char_u *)getenv("LINES")))
4110 rows = atoi((char *)p);
4111 if ((p = (char_u *)getenv("COLUMNS")))
4112 columns = atoi((char *)p);
4113 }
4114
4115#ifdef HAVE_TGETENT
4116 /*
4117 * 3. try reading "co" and "li" entries from termcap
4118 */
4119 if (columns == 0 || rows == 0)
4120 getlinecol(&columns, &rows);
4121#endif
4122
4123 /*
4124 * 4. If everything fails, use the old values
4125 */
4126 if (columns <= 0 || rows <= 0)
4127 return FAIL;
4128
4129 Rows = rows;
4130 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004131 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 return OK;
4133}
4134
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004135#if defined(FEAT_TERMINAL) || defined(PROTO)
4136/*
4137 * Report the windows size "rows" and "cols" to tty "fd".
4138 */
4139 int
4140mch_report_winsize(int fd, int rows, int cols)
4141{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004142 int tty_fd;
4143 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004144
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004145 tty_fd = get_tty_fd(fd);
4146 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004147 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004148# if defined(TIOCSWINSZ)
4149 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004150
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004151 ws.ws_col = cols;
4152 ws.ws_row = rows;
4153 ws.ws_xpixel = cols * 5;
4154 ws.ws_ypixel = rows * 10;
4155 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4156 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4157 retval == 0 ? "success" : "failed");
4158# elif defined(TIOCSSIZE)
4159 struct ttysize ts;
4160
4161 ts.ts_cols = cols;
4162 ts.ts_lines = rows;
4163 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4164 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4165 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004166# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004167 if (tty_fd != fd)
4168 close(tty_fd);
4169 }
4170 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004171}
4172#endif
4173
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174/*
4175 * Try to set the window size to Rows and Columns.
4176 */
4177 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004178mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179{
4180 if (*T_CWS)
4181 {
4182 /*
4183 * NOTE: if you get an error here that term_set_winsize() is
4184 * undefined, check the output of configure. It could probably not
4185 * find a ncurses, termcap or termlib library.
4186 */
4187 term_set_winsize((int)Rows, (int)Columns);
4188 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004189 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191}
4192
Bram Moolenaar0f873732019-12-05 20:28:46 +01004193#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
4195/*
4196 * Rows and/or Columns has changed.
4197 */
4198 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004199mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004201 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202}
4203
Bram Moolenaar205b8862011-09-07 15:04:31 +02004204/*
4205 * Wait for process "child" to end.
4206 * Return "child" if it exited properly, <= 0 on error.
4207 */
4208 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004209wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004210{
4211 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004212 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004213
4214 while (wait_pid != child)
4215 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004216 // When compiled with Python threads are probably used, in which case
4217 // wait() sometimes hangs for no obvious reason. Use waitpid()
4218 // instead and loop (like the GUI). Also needed for other interfaces,
4219 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004220# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004221 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004222# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004223 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004224# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004225 if (wait_pid == 0)
4226 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004227 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004228 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004229 if (++delay_msec > 10)
4230 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004231 continue;
4232 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004233 if (wait_pid <= 0
4234# ifdef ECHILD
4235 && errno == ECHILD
4236# endif
4237 )
4238 break;
4239 }
4240 return wait_pid;
4241}
4242
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004243#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004244/*
4245 * Set the environment for a child process.
4246 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004247 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004248set_child_environment(
4249 long rows,
4250 long columns,
4251 char *term,
4252 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004253{
4254# ifdef HAVE_SETENV
4255 char envbuf[50];
4256# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004257 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004258 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004259 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004260 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004261 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004262# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004263 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004264# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004265# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004266 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004267# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004268# endif
4269
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004270# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004271 setenv("TERM", term, 1);
4272 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004273 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004274 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004275 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004276 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004277 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004278 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004279 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004280# ifdef FEAT_TERMINAL
4281 if (is_terminal)
4282 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004283 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004284 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4285 }
4286# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004287# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004288 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004289# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004290# else
4291 /*
4292 * Putenv does not copy the string, it has to remain valid.
4293 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004294 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004295 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004296 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4297 putenv(envbuf_Term);
4298 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004299 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004300 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4301 putenv(envbuf_Lines);
4302 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4303 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004304 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004305 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004306 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004307# ifdef FEAT_TERMINAL
4308 if (is_terminal)
4309 {
4310 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004311 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004312 putenv(envbuf_Version);
4313 }
4314# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004315# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004316 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4317 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4318 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004319# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004320# endif
4321}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004322
4323 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004324set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004325{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004326 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004327}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004328#endif
4329
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004330#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004331/*
4332 * Open a PTY, with FD for the master and slave side.
4333 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004334 * When successful both file descriptors are stored and the allocated pty name
4335 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004336 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004337 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004338open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004339{
4340 char *tty_name;
4341
Bram Moolenaar59386482019-02-10 22:43:46 +01004342 if (name1 != NULL)
4343 *name1 = NULL;
4344 if (name2 != NULL)
4345 *name2 = NULL;
4346
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004347 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004348 if (*pty_master_fd >= 0)
4349 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004350 // Leaving out O_NOCTTY may lead to waitpid() always returning
4351 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4352 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004353#ifdef O_NOCTTY
4354 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4355#else
4356 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4357#endif
4358 if (*pty_slave_fd < 0)
4359 {
4360 close(*pty_master_fd);
4361 *pty_master_fd = -1;
4362 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004363 else
4364 {
4365 if (name1 != NULL)
4366 *name1 = vim_strsave((char_u *)tty_name);
4367 if (name2 != NULL)
4368 *name2 = vim_strsave((char_u *)tty_name);
4369 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004370 }
4371}
4372#endif
4373
Bram Moolenaarfae42832017-08-01 22:24:26 +02004374/*
4375 * Send SIGINT to a child process if "c" is an interrupt character.
4376 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004377 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004378may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4379{
4380# ifdef SIGINT
4381 if (c == Ctrl_C || c == intr_char)
4382 {
4383# ifdef HAVE_SETSID
4384 kill(-pid, SIGINT);
4385# else
4386 kill(0, SIGINT);
4387# endif
4388 if (wpid > 0)
4389 kill(wpid, SIGINT);
4390 }
4391# endif
4392}
4393
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004394#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004395
Bram Moolenaar0f873732019-12-05 20:28:46 +01004396/*
4397 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4398 * pointers, the last one is NULL.
4399 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4400 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004401 int
4402unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004403 char_u *cmd,
4404 char ***argvp,
4405 char_u **sh_tofree,
4406 char_u **shcf_tofree)
4407{
4408 char **argv = NULL;
4409 int argc;
4410
4411 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004412 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004413 return FAIL;
4414
4415 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4416 return FAIL;
4417 *argvp = argv;
4418
4419 if (cmd != NULL)
4420 {
4421 char_u *s;
4422 char_u *p;
4423
4424 if (extra_shell_arg != NULL)
4425 argv[argc++] = (char *)extra_shell_arg;
4426
Bram Moolenaar0f873732019-12-05 20:28:46 +01004427 // Break 'shellcmdflag' into white separated parts. This doesn't
4428 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004429 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004430 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004431 return FAIL;
4432 s = *shcf_tofree;
4433 p = p_shcf;
4434 while (*p != NUL)
4435 {
4436 argv[argc++] = (char *)s;
4437 while (*p && *p != ' ' && *p != TAB)
4438 *s++ = *p++;
4439 *s++ = NUL;
4440 p = skipwhite(p);
4441 }
4442
4443 argv[argc++] = (char *)cmd;
4444 }
4445 argv[argc] = NULL;
4446 return OK;
4447}
4448#endif
4449
4450#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4451/*
4452 * Use a terminal window to run a shell command in.
4453 */
4454 static int
4455mch_call_shell_terminal(
4456 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004457 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004458{
4459 jobopt_T opt;
4460 char **argv = NULL;
4461 char_u *tofree1 = NULL;
4462 char_u *tofree2 = NULL;
4463 int retval = -1;
4464 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004465 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004466 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004467 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004468
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004469 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004470 goto theend;
4471
4472 init_job_options(&opt);
4473 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4474 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004475 if (buf == NULL)
4476 goto theend;
4477
4478 job = term_getjob(buf->b_term);
4479 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004480
Bram Moolenaar0f873732019-12-05 20:28:46 +01004481 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004482 aucmd_prepbuf(&aco, buf);
4483
4484 clear_oparg(&oa);
4485 while (term_use_loop())
4486 {
4487 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4488 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004489 // If terminal_loop() returns OK we got a key that is handled
4490 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004491 if (terminal_loop(TRUE) == OK)
4492 normal_cmd(&oa, TRUE);
4493 }
4494 else
4495 normal_cmd(&oa, TRUE);
4496 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004497 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004498 ch_log(NULL, "system command finished");
4499
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004500 job_unref(job);
4501
Bram Moolenaar0f873732019-12-05 20:28:46 +01004502 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004503 aucmd_restbuf(&aco);
4504
4505 wait_return(TRUE);
4506 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4507
4508theend:
4509 vim_free(argv);
4510 vim_free(tofree1);
4511 vim_free(tofree2);
4512 return retval;
4513}
4514#endif
4515
4516#ifdef USE_SYSTEM
4517/*
4518 * Use system() to start the shell: simple but slow.
4519 */
4520 static int
4521mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004522 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004523 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
4525#ifdef VMS
4526 char *ifn = NULL;
4527 char *ofn = NULL;
4528#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004529 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004530 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004531 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533 out_flush();
4534
4535 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004536 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
Bram Moolenaar62b42182010-09-21 22:09:37 +02004538# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004539 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004540 loose_clipboard();
4541# endif
4542
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 if (cmd == NULL)
4544 x = system((char *)p_sh);
4545 else
4546 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004547# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (ofn = strchr((char *)cmd, '>'))
4549 *ofn++ = '\0';
4550 if (ifn = strchr((char *)cmd, '<'))
4551 {
4552 char *p;
4553
4554 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004555 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (p)
4557 *p = '\0';
4558 }
4559 if (ofn)
4560 x = vms_sys((char *)cmd, ofn, ifn);
4561 else
4562 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004563# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004564 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004566 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (newcmd == NULL)
4568 x = 0;
4569 else
4570 {
4571 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4572 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4573 (char *)p_shcf,
4574 (char *)cmd);
4575 x = system((char *)newcmd);
4576 vim_free(newcmd);
4577 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004578# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580# ifdef VMS
4581 x = vms_sys_status(x);
4582# endif
4583 if (emsg_silent)
4584 ;
4585 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004586 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 else if (x && !(options & SHELL_SILENT))
4588 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004589 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 msg_outnum((long)x);
4591 msg_putchar('\n');
4592 }
4593
4594 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004595 {
4596 // The shell may have messed with the mode, always set it.
4597 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004598 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600# ifdef FEAT_TITLE
4601 resettitle();
4602# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004603# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4604 restore_clipboard();
4605# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004607}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
Bram Moolenaar0f873732019-12-05 20:28:46 +01004609#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
Bram Moolenaar0f873732019-12-05 20:28:46 +01004611# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4612 // 127, some shells use that already
4613# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
Bram Moolenaar13568252018-03-16 20:46:58 +01004615/*
4616 * Don't use system(), use fork()/exec().
4617 */
4618 static int
4619mch_call_shell_fork(
4620 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004621 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004622{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004623 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004625 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 pid_t wait_pid = 0;
4627# ifdef HAVE_UNION_WAIT
4628 union wait status;
4629# else
4630 int status = -1;
4631# endif
4632 int retval = -1;
4633 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004634 char_u *tofree1 = NULL;
4635 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004637 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004638# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004640# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004641 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 int fd_fromshell[2];
4643 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004644 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
4646 out_flush();
4647 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004648 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004649 if (tmode == TMODE_RAW)
4650 // The shell may have messed with the mode, always set it later.
4651 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004653 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004654 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004655
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004657 * For the GUI, when writing the output into the buffer and when reading
4658 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4659 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004661 if ((options & (SHELL_READ|SHELL_WRITE))
4662# ifdef FEAT_GUI
4663 || (gui.in_use && show_shell_mess)
4664# endif
4665 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004667# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 /*
4669 * Try to open a master pty.
4670 * If this works, open the slave pty.
4671 * If the slave can't be opened, close the master pty.
4672 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004673 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004674 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 /*
4676 * If not opening a pty or it didn't work, try using pipes.
4677 */
4678 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004679# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 {
4681 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004682 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 {
4684 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004685 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {
4687 close(fd_toshell[0]);
4688 close(fd_toshell[1]);
4689 }
4690 }
4691 if (pipe_error)
4692 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004693 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 out_flush();
4695 }
4696 }
4697 }
4698
Bram Moolenaar0f873732019-12-05 20:28:46 +01004699 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004701 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004702 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004703 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004704 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004706 UNBLOCK_SIGNALS(&curset);
4707
Bram Moolenaar32526b32019-01-19 17:43:09 +01004708 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004709 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004711 || (gui.in_use && show_shell_mess)
4712# endif
4713 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004715# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004716 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
4718 close(pty_master_fd);
4719 close(pty_slave_fd);
4720 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004721 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 {
4724 close(fd_toshell[0]);
4725 close(fd_toshell[1]);
4726 close(fd_fromshell[0]);
4727 close(fd_fromshell[1]);
4728 }
4729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004731 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004733 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004734 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735
Bram Moolenaar819524702018-02-27 19:10:00 +01004736# ifdef FEAT_JOB_CHANNEL
4737 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004738 {
4739 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004740 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004741 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004742# endif
4743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 if (!show_shell_mess || (options & SHELL_EXPAND))
4745 {
4746 int fd;
4747
4748 /*
4749 * Don't want to show any message from the shell. Can't just
4750 * close stdout and stderr though, because some systems will
4751 * break if you try to write to them after that, so we must
4752 * use dup() to replace them with something else -- webb
4753 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4754 * waiting for input.
4755 */
4756 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4757 fclose(stdin);
4758 fclose(stdout);
4759 fclose(stderr);
4760
4761 /*
4762 * If any of these open()'s and dup()'s fail, we just continue
4763 * anyway. It's not fatal, and on most systems it will make
4764 * no difference at all. On a few it will cause the execvp()
4765 * to exit with a non-zero status even when the completion
4766 * could be done, which is nothing too serious. If the open()
4767 * or dup() failed we'd just do the same thing ourselves
4768 * anyway -- webb
4769 */
4770 if (fd >= 0)
4771 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004772 vim_ignored = dup(fd); // To replace stdin (fd 0)
4773 vim_ignored = dup(fd); // To replace stdout (fd 1)
4774 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
Bram Moolenaar0f873732019-12-05 20:28:46 +01004776 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 close(fd);
4778 }
4779 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004780 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004782 || gui.in_use
4783# endif
4784 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
4786
Bram Moolenaardf177f62005-02-22 08:39:57 +00004787# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004788 // Create our own process group, so that the child and all its
4789 // children can be kill()ed. Don't do this when using pipes,
4790 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004791 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004792 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004793 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004794# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004795 // When doing "!xterm&" and 'shell' is bash: the shell
4796 // will exit and send SIGHUP to all processes in its
4797 // group, killing the just started process. Ignore SIGHUP
4798 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004799 signal(SIGHUP, SIG_IGN);
4800# endif
4801 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004802# endif
4803# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004804 if (pty_slave_fd >= 0)
4805 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004806 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004807 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004808 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004809# ifdef TIOCSCTTY
4810 // Try to become controlling tty (probably doesn't work,
4811 // unless run by root)
4812 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4813# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004814 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004815# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004816 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817
Bram Moolenaara5792f52005-11-23 21:25:05 +00004818 /*
4819 * stderr is only redirected when using the GUI, so that a
4820 * program like gpg can still access the terminal to get a
4821 * passphrase using stderr.
4822 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004823# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 if (pty_master_fd >= 0)
4825 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004826 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827
Bram Moolenaar0f873732019-12-05 20:28:46 +01004828 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004830 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004832 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004833 if (gui.in_use)
4834 {
4835 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004836 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838
Bram Moolenaar0f873732019-12-05 20:28:46 +01004839 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
4841 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004844 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 close(fd_toshell[1]);
4846 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004847 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 close(fd_toshell[0]);
4849
Bram Moolenaar0f873732019-12-05 20:28:46 +01004850 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 close(fd_fromshell[0]);
4852 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004853 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 close(fd_fromshell[1]);
4855
Bram Moolenaara5792f52005-11-23 21:25:05 +00004856# ifdef FEAT_GUI
4857 if (gui.in_use)
4858 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004859 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004860 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004861 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004862 }
4863# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 }
4865 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004866
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 /*
4868 * There is no type cast for the argv, because the type may be
4869 * different on different machines. This may cause a warning
4870 * message with strict compilers, don't worry about it.
4871 * Call _exit() instead of exit() to avoid closing the connection
4872 * to the X server (esp. with GTK, which uses atexit()).
4873 */
4874 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004875 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004877 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
4879 /*
4880 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004881 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 */
4883 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004884 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004885 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004886# ifdef FEAT_JOB_CHANNEL
4887 ++dont_check_job_ended;
4888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 /*
4890 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004891 * This is also used to pipe stdin/stdout to/from the external
4892 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004894 if ((options & (SHELL_READ|SHELL_WRITE))
4895# ifdef FEAT_GUI
4896 || (gui.in_use && show_shell_mess)
4897# endif
4898 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004900# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004902 int buffer_off = 0; // valid bytes in buffer[]
4903 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4904 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 int len;
4906 int p_more_save;
4907 int old_State;
4908 int c;
4909 int toshell_fd;
4910 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004911 garray_T ga;
4912 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004913# ifdef ELAPSED_FUNC
4914 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004915# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
Bram Moolenaardf177f62005-02-22 08:39:57 +00004917# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 if (pty_master_fd >= 0)
4919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 fromshell_fd = pty_master_fd;
4921 toshell_fd = dup(pty_master_fd);
4922 }
4923 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004924# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
4926 close(fd_toshell[0]);
4927 close(fd_fromshell[1]);
4928 toshell_fd = fd_toshell[1];
4929 fromshell_fd = fd_fromshell[0];
4930 }
4931
4932 /*
4933 * Write to the child if there are typed characters.
4934 * Read from the child if there are characters available.
4935 * Repeat the reading a few times if more characters are
4936 * available. Need to check for typed keys now and then, but
4937 * not too often (delays when no chars are available).
4938 * This loop is quit if no characters can be read from the pty
4939 * (WaitForChar detected special condition), or there are no
4940 * characters available and the child has exited.
4941 * Only check if the child has exited when there is no more
4942 * output. The child may exit before all the output has
4943 * been printed.
4944 *
4945 * Currently this busy loops!
4946 * This can probably dead-lock when the write blocks!
4947 */
4948 p_more_save = p_more;
4949 p_more = FALSE;
4950 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004951 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004953 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004954 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004955 // Fork a process that will write the lines to the
4956 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004957 if ((wpid = fork()) == -1)
4958 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004959 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004960 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004961 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004962 {
4963 linenr_T lnum = curbuf->b_op_start.lnum;
4964 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004965 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004966 size_t l;
4967
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004968 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004969 for (;;)
4970 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004971 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004972 if (l == 0)
4973 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004974 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004975 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004976 len = write(toshell_fd, "", (size_t)1);
4977 else
4978 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004979 char_u *s = vim_strchr(lp + written, NL);
4980
Bram Moolenaar89d40322006-08-29 15:30:07 +00004981 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004982 s == NULL ? l
4983 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004984 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004985 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004986 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004987 // Finished a line, add a NL, unless this line
4988 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004989 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004990 || (!curbuf->b_p_bin
4991 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004992 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004993 && (lnum !=
4994 curbuf->b_ml.ml_line_count
4995 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004996 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004997 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004998 ++lnum;
4999 if (lnum > curbuf->b_op_end.lnum)
5000 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005001 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005002 close(toshell_fd);
5003 toshell_fd = -1;
5004 break;
5005 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005006 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005007 written = 0;
5008 }
5009 else if (len > 0)
5010 written += len;
5011 }
5012 _exit(0);
5013 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005014 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005015 {
5016 close(toshell_fd);
5017 toshell_fd = -1;
5018 }
5019 }
5020
5021 if (options & SHELL_READ)
5022 ga_init2(&ga, 1, BUFLEN);
5023
5024 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005025# ifdef ELAPSED_FUNC
5026 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 for (;;)
5029 {
5030 /*
5031 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005032 * if there are any.
5033 * Don't do this if we are expanding wild cards (would eat
5034 * typeahead).
5035 * Don't do this when filtering and terminal is in cooked
5036 * mode, the shell command will handle the I/O. Avoids
5037 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005038 * Don't get characters when the child has already
5039 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005040 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005041 * while (noread_cnt > 4), avoids that ":r !ls" eats
5042 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 */
5044 len = 0;
5045 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005046 && ((options &
5047 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5048 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005049# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005050 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005051# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005052 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005053 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005054 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005056 if (ta_len == 0)
5057 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005058 // Get extra characters when we don't have any.
5059 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005060 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005061# ifdef ELAPSED_FUNC
5062 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005063# endif
5064 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5065 }
5066 if (ta_len > 0 || len > 0)
5067 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 /*
5069 * For pipes:
5070 * Check for CTRL-C: send interrupt signal to child.
5071 * Check for CTRL-D: EOF, close pipe to child.
5072 */
5073 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 /*
5076 * Send SIGINT to the child's group or all
5077 * processes in our group.
5078 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005079 may_send_sigint(ta_buf[ta_len], pid, wpid);
5080
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 if (pty_master_fd < 0 && toshell_fd >= 0
5082 && ta_buf[ta_len] == Ctrl_D)
5083 {
5084 close(toshell_fd);
5085 toshell_fd = -1;
5086 }
5087 }
5088
Bram Moolenaarf4140482020-02-15 23:06:45 +01005089 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090
5091 /*
5092 * For pipes: echo the typed characters.
5093 * For a pty this does not seem to work.
5094 */
5095 if (pty_master_fd < 0)
5096 {
5097 for (i = ta_len; i < ta_len + len; ++i)
5098 {
5099 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5100 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 else if (has_mbyte)
5102 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005103 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104
5105 msg_outtrans_len(ta_buf + i, l);
5106 i += l - 1;
5107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 else
5109 msg_outtrans_len(ta_buf + i, 1);
5110 }
5111 windgoto(msg_row, msg_col);
5112 out_flush();
5113 }
5114
5115 ta_len += len;
5116
5117 /*
5118 * Write the characters to the child, unless EOF has
5119 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005120 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005121 * When writing buffer lines, drop the typed
5122 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005124 if (options & SHELL_WRITE)
5125 ta_len = 0;
5126 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 {
5128 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5129 if (len > 0)
5130 {
5131 ta_len -= len;
5132 mch_memmove(ta_buf, ta_buf + len, ta_len);
5133 }
5134 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 }
5137
Bram Moolenaardf177f62005-02-22 08:39:57 +00005138 if (got_int)
5139 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005140 // CTRL-C sends a signal to the child, we ignore it
5141 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005142# ifdef HAVE_SETSID
5143 kill(-pid, SIGINT);
5144# else
5145 kill(0, SIGINT);
5146# endif
5147 if (wpid > 0)
5148 kill(wpid, SIGINT);
5149 got_int = FALSE;
5150 }
5151
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 /*
5153 * Check if the child has any characters to be printed.
5154 * Read them and write them to our window. Repeat this as
5155 * long as there is something to do, avoid the 10ms wait
5156 * for mch_inchar(), or sending typeahead characters to
5157 * the external process.
5158 * TODO: This should handle escape sequences, compatible
5159 * to some terminal (vt52?).
5160 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005161 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005162 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005164 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005167 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005169
5170 noread_cnt = 0;
5171 if (options & SHELL_READ)
5172 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005173 // Do NUL -> NL translation, append NL separated
5174 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005175 for (i = 0; i < len; ++i)
5176 {
5177 if (buffer[i] == NL)
5178 append_ga_line(&ga);
5179 else if (buffer[i] == NUL)
5180 ga_append(&ga, NL);
5181 else
5182 ga_append(&ga, buffer[i]);
5183 }
5184 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005185 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 {
5187 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005188 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189
Bram Moolenaardf177f62005-02-22 08:39:57 +00005190 len += buffer_off;
5191 buffer[len] = NUL;
5192
Bram Moolenaar0f873732019-12-05 20:28:46 +01005193 // Check if the last character in buffer[] is
5194 // incomplete, keep these bytes for the next
5195 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 for (p = buffer; p < buffer + len; p += l)
5197 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005198 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005200 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 else if (MB_BYTE2LEN(*p) != l)
5202 break;
5203 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005204 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005206 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if (len >= 12)
5208 ++p;
5209 else
5210 {
5211 buffer_off = len;
5212 continue;
5213 }
5214 }
5215 c = *p;
5216 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005217 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 if (p < buffer + len)
5219 {
5220 *p = c;
5221 buffer_off = (buffer + len) - p;
5222 mch_memmove(buffer, p, buffer_off);
5223 continue;
5224 }
5225 buffer_off = 0;
5226 }
5227 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 {
5229 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005230 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
5232
5233 windgoto(msg_row, msg_col);
5234 cursor_on();
5235 out_flush();
5236 if (got_int)
5237 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005238
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005239# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005240 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005241 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005242 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005243
Bram Moolenaar0f873732019-12-05 20:28:46 +01005244 // Avoid that we keep looping here without
5245 // checking for a CTRL-C for a long time. Don't
5246 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005247 if (msec > 2000)
5248 {
5249 noread_cnt = 5;
5250 break;
5251 }
5252 }
5253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 }
5255
Bram Moolenaar0f873732019-12-05 20:28:46 +01005256 // If we already detected the child has finished, continue
5257 // reading output for a short while. Some text may be
5258 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005259 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005260 {
5261 if (noread_cnt < 5)
5262 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005263 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005264 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 /*
5267 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005268 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005270# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005271 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005272# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005274# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5276 || (wait_pid == pid && WIFEXITED(status)))
5277 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005278 // Don't break the loop yet, try reading more
5279 // characters from "fromshell_fd" first. When using
5280 // pipes there might still be something to read and
5281 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005284 else
5285 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005286
Bram Moolenaar95a51352013-03-21 22:53:50 +01005287# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005288 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005289 clip_update();
5290# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292finished:
5293 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005294 if (options & SHELL_READ)
5295 {
5296 if (ga.ga_len > 0)
5297 {
5298 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005299 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005300 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005301 }
5302 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005303 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005304 ga_clear(&ga);
5305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 /*
5308 * Give all typeahead that wasn't used back to ui_inchar().
5309 */
5310 if (ta_len)
5311 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 State = old_State;
5313 if (toshell_fd >= 0)
5314 close(toshell_fd);
5315 close(fromshell_fd);
5316 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005317# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005318 else
5319 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005320 long delay_msec = 1;
5321
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005322 if (tmode == TMODE_RAW)
5323 // possibly disables modifyOtherKeys, so that the system
5324 // can recognize CTRL-C
5325 out_str(T_CTE);
Bram Moolenaar0981c872020-08-23 14:28:37 +02005326
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005327 /*
5328 * Similar to the loop above, but only handle X events, no
5329 * I/O.
5330 */
5331 for (;;)
5332 {
5333 if (got_int)
5334 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005335 // CTRL-C sends a signal to the child, we ignore it
5336 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005337# ifdef HAVE_SETSID
5338 kill(-pid, SIGINT);
5339# else
5340 kill(0, SIGINT);
5341# endif
5342 got_int = FALSE;
5343 }
5344# ifdef __NeXT__
5345 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5346# else
5347 wait_pid = waitpid(pid, &status, WNOHANG);
5348# endif
5349 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5350 || (wait_pid == pid && WIFEXITED(status)))
5351 {
5352 wait_pid = pid;
5353 break;
5354 }
5355
Bram Moolenaar0f873732019-12-05 20:28:46 +01005356 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005357 clip_update();
5358
Bram Moolenaar0f873732019-12-05 20:28:46 +01005359 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005360 // less time, gradually wait longer.
5361 mch_delay(delay_msec,
5362 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005363 if (++delay_msec > 10)
5364 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005365 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005366
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005367 if (tmode == TMODE_RAW)
5368 // possibly enables modifyOtherKeys again
5369 out_str(T_CTI);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005370 }
5371# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
5373 /*
5374 * Wait until our child has exited.
5375 * Ignore wait() returning pids of other children and returning
5376 * because of some signal like SIGWINCH.
5377 * Don't wait if wait_pid was already set above, indicating the
5378 * child already exited.
5379 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005380 if (wait_pid != pid)
5381 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
Bram Moolenaar624891f2010-10-13 16:22:09 +02005383# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005384 // Close slave side of pty. Only do this after the child has
5385 // exited, otherwise the child may hang when it tries to write on
5386 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005387 if (pty_master_fd >= 0)
5388 close(pty_slave_fd);
5389# endif
5390
Bram Moolenaar0f873732019-12-05 20:28:46 +01005391 // Make sure the child that writes to the external program is
5392 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005393 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005394 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005395 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005396 wait4pid(wpid, NULL);
5397 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005398
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005399# ifdef FEAT_JOB_CHANNEL
5400 --dont_check_job_ended;
5401# endif
5402
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 /*
5404 * Set to raw mode right now, otherwise a CTRL-C after
5405 * catch_signals() will kill Vim.
5406 */
5407 if (tmode == TMODE_RAW)
5408 settmode(TMODE_RAW);
5409 did_settmode = TRUE;
5410 set_signals();
5411
5412 if (WIFEXITED(status))
5413 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005414 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005416 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
5418 if (retval == EXEC_FAILED)
5419 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005420 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 msg_outtrans(p_sh);
5422 msg_putchar('\n');
5423 }
5424 else if (!(options & SHELL_SILENT))
5425 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005426 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 msg_outnum((long)retval);
5428 msg_putchar('\n');
5429 }
5430 }
5431 }
5432 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005433 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
5435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436
5437error:
5438 if (!did_settmode)
5439 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005440 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441# ifdef FEAT_TITLE
5442 resettitle();
5443# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005444 vim_free(argv);
5445 vim_free(tofree1);
5446 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447
5448 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005449}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005450#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005451
5452 int
5453mch_call_shell(
5454 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005455 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005456{
5457#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5458 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5459 return mch_call_shell_terminal(cmd, options);
5460#endif
5461#ifdef USE_SYSTEM
5462 return mch_call_shell_system(cmd, options);
5463#else
5464 return mch_call_shell_fork(cmd, options);
5465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466}
5467
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005468#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005469 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005470mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005471{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005472 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005473 int fd_in[2] = {-1, -1}; // for stdin
5474 int fd_out[2] = {-1, -1}; // for stdout
5475 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005476 int pty_master_fd = -1;
5477 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005478 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005479 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5480 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5481 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005482 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005483 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5484 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005485 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005486 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005487 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005488
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005489 if (use_out_for_err && use_null_for_out)
5490 use_null_for_err = TRUE;
5491
Bram Moolenaar0f873732019-12-05 20:28:46 +01005492 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005493 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005494
Bram Moolenaarb2412082017-08-20 18:09:14 +02005495 if (options->jo_pty
5496 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005497 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005498 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005499 open_pty(&pty_master_fd, &pty_slave_fd,
5500 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005501
Bram Moolenaar0f873732019-12-05 20:28:46 +01005502 // TODO: without the channel feature connect the child to /dev/null?
5503 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005504 if (use_file_for_in)
5505 {
5506 char_u *fname = options->jo_io_name[PART_IN];
5507
5508 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5509 if (fd_in[0] < 0)
5510 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005511 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005512 goto failed;
5513 }
5514 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005515 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005516 // When writing buffer lines to the input don't use the pty, so that
5517 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005518 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5519 && pipe(fd_in) < 0)
5520 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005521
5522 if (use_file_for_out)
5523 {
5524 char_u *fname = options->jo_io_name[PART_OUT];
5525
5526 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5527 if (fd_out[1] < 0)
5528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005529 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005530 goto failed;
5531 }
5532 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005533 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005534 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005535
5536 if (use_file_for_err)
5537 {
5538 char_u *fname = options->jo_io_name[PART_ERR];
5539
5540 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5541 if (fd_err[1] < 0)
5542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005543 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005544 goto failed;
5545 }
5546 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005547 else if (!use_out_for_err && !use_null_for_err
5548 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005549 goto failed;
5550
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005551 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5552 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005553 if (options->jo_set & JO_CHANNEL)
5554 {
5555 channel = options->jo_channel;
5556 if (channel != NULL)
5557 ++channel->ch_refcount;
5558 }
5559 else
5560 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005561 if (channel == NULL)
5562 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005563 if (job->jv_tty_out != NULL)
5564 ch_log(channel, "using pty %s on fd %d",
5565 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005566 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005567
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005568 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005569 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005570 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005571 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005572 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005573 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005574 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005575 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005576 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005577 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005578 int null_fd = -1;
5579 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005580
Bram Moolenaar0f873732019-12-05 20:28:46 +01005581 // child
5582 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005583 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005584
Bram Moolenaar819524702018-02-27 19:10:00 +01005585# ifdef FEAT_JOB_CHANNEL
5586 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005587 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005588 ch_logfile((char_u *)"", (char_u *)"");
5589# endif
5590
Bram Moolenaar835dc632016-02-07 14:27:38 +01005591# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005592 // Create our own process group, so that the child and all its
5593 // children can be kill()ed. Don't do this when using pipes,
5594 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005595 (void)setsid();
5596# endif
5597
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005598# ifdef FEAT_TERMINAL
5599 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005600 {
5601 char *term = (char *)T_NAME;
5602
5603#ifdef FEAT_GUI
5604 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005605 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005606 term = getenv("TERM");
5607#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005608 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005609 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005610 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005611 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005612 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005613 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005614 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005615 else if (t_colors > 16)
5616 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005617 else
5618 term = "xterm";
5619 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005620 set_child_environment(
5621 (long)options->jo_term_rows,
5622 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005623 term,
5624 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005625 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005626 else
5627# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005628 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005629
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005630 if (options->jo_env != NULL)
5631 {
5632 dict_T *dict = options->jo_env;
5633 hashitem_T *hi;
5634 int todo = (int)dict->dv_hashtab.ht_used;
5635
5636 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5637 if (!HASHITEM_EMPTY(hi))
5638 {
5639 typval_T *item = &dict_lookup(hi)->di_tv;
5640
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005641 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005642 --todo;
5643 }
5644 }
5645
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005646 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005647 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005648 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005649 if (null_fd < 0)
5650 {
5651 perror("opening /dev/null failed");
5652 _exit(OPEN_NULL_FAILED);
5653 }
5654 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005655
Bram Moolenaar223896d2017-08-02 22:33:28 +02005656 if (pty_slave_fd >= 0)
5657 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005658 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005659 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005660# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005661 // Try to become controlling tty (probably doesn't work,
5662 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005663 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5664# endif
5665 }
5666
Bram Moolenaar0f873732019-12-05 20:28:46 +01005667 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005668 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005669 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005670 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005671 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005672 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005673 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005674 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005675
Bram Moolenaar0f873732019-12-05 20:28:46 +01005676 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005677 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005678 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005679 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005680 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005681 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005682 }
5683 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005684 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005685 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005686 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005687 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005688 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005689
Bram Moolenaar0f873732019-12-05 20:28:46 +01005690 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005691 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005692 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005693 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005694 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005695 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005696 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005697 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005698
5699 if (fd_in[0] >= 0)
5700 close(fd_in[0]);
5701 if (fd_in[1] >= 0)
5702 close(fd_in[1]);
5703 if (fd_out[0] >= 0)
5704 close(fd_out[0]);
5705 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005706 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005707 if (fd_err[0] >= 0)
5708 close(fd_err[0]);
5709 if (fd_err[1] >= 0)
5710 close(fd_err[1]);
5711 if (pty_master_fd >= 0)
5712 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005713 close(pty_master_fd); // not used in the child
5714 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005715 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005716
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005717 if (null_fd >= 0)
5718 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005719
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005720 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5721 _exit(EXEC_FAILED);
5722
Bram Moolenaar0f873732019-12-05 20:28:46 +01005723 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005724 execvp(argv[0], argv);
5725
Bram Moolenaar4694a172016-04-21 14:05:23 +02005726 if (stderr_works)
5727 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005728# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005729 // calling free_all_mem() here causes problems. Ignore valgrind
5730 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005731# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005732 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005733 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005734
Bram Moolenaar0f873732019-12-05 20:28:46 +01005735 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005736 UNBLOCK_SIGNALS(&curset);
5737
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005738 job->jv_pid = pid;
5739 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005740 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005741
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005742 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005743 close(pty_slave_fd); // not used in the parent
5744 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005745 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005746 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005747 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005748 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005749 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005750 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005751 if (channel != NULL)
5752 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005753 int in_fd = INVALID_FD;
5754 int out_fd = INVALID_FD;
5755 int err_fd = INVALID_FD;
5756
5757 if (!(use_file_for_in || use_null_for_in))
5758 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5759
5760 if (!(use_file_for_out || use_null_for_out))
5761 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5762
5763 // When using pty_master_fd only set it for stdout, do not duplicate
5764 // it for stderr, it only needs to be read once.
5765 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5766 {
5767 if (fd_err[0] >= 0)
5768 err_fd = fd_err[0];
5769 else if (out_fd != pty_master_fd)
5770 err_fd = pty_master_fd;
5771 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005772
5773 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005774 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005775 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005776 else
5777 {
5778 if (fd_in[1] >= 0)
5779 close(fd_in[1]);
5780 if (fd_out[0] >= 0)
5781 close(fd_out[0]);
5782 if (fd_err[0] >= 0)
5783 close(fd_err[0]);
5784 if (pty_master_fd >= 0)
5785 close(pty_master_fd);
5786 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005787
Bram Moolenaar0f873732019-12-05 20:28:46 +01005788 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005789 return;
5790
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005791failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005792 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005793 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005794 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005795 if (fd_in[1] >= 0)
5796 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005797 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005798 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005799 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005800 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005801 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005802 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005803 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005804 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005805 if (pty_master_fd >= 0)
5806 close(pty_master_fd);
5807 if (pty_slave_fd >= 0)
5808 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005809}
5810
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005811 static char_u *
5812get_signal_name(int sig)
5813{
5814 int i;
5815 char_u numbuf[NUMBUFLEN];
5816
5817 if (sig == SIGKILL)
5818 return vim_strsave((char_u *)"kill");
5819
5820 for (i = 0; signal_info[i].sig != -1; i++)
5821 if (sig == signal_info[i].sig)
5822 return strlow_save((char_u *)signal_info[i].name);
5823
5824 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5825 return vim_strsave(numbuf);
5826}
5827
Bram Moolenaar835dc632016-02-07 14:27:38 +01005828 char *
5829mch_job_status(job_T *job)
5830{
5831# ifdef HAVE_UNION_WAIT
5832 union wait status;
5833# else
5834 int status = -1;
5835# endif
5836 pid_t wait_pid = 0;
5837
5838# ifdef __NeXT__
5839 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5840# else
5841 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5842# endif
5843 if (wait_pid == -1)
5844 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005845 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005846 if (job->jv_status < JOB_ENDED)
5847 ch_log(job->jv_channel, "Job no longer exists: %s",
5848 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005849 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005850 }
5851 if (wait_pid == 0)
5852 return "run";
5853 if (WIFEXITED(status))
5854 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005855 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005856 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005857 if (job->jv_status < JOB_ENDED)
5858 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005859 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005860 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005861 if (WIFSIGNALED(status))
5862 {
5863 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005864 job->jv_termsig = get_signal_name(WTERMSIG(status));
5865 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5866 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5867 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005868 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005869 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005870 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005871
5872return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005873 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005874 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005875 return "dead";
5876}
5877
5878 job_T *
5879mch_detect_ended_job(job_T *job_list)
5880{
5881# ifdef HAVE_UNION_WAIT
5882 union wait status;
5883# else
5884 int status = -1;
5885# endif
5886 pid_t wait_pid = 0;
5887 job_T *job;
5888
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005889# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005890 // Do not do this when waiting for a shell command to finish, we would get
5891 // the exit value here (and discard it), the exit value obtained there
5892 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005893 if (dont_check_job_ended > 0)
5894 return NULL;
5895# endif
5896
Bram Moolenaar97792de2016-10-15 18:36:49 +02005897# ifdef __NeXT__
5898 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5899# else
5900 wait_pid = waitpid(-1, &status, WNOHANG);
5901# endif
5902 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005903 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005904 return NULL;
5905 for (job = job_list; job != NULL; job = job->jv_next)
5906 {
5907 if (job->jv_pid == wait_pid)
5908 {
5909 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005910 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005911 job->jv_exitval = WEXITSTATUS(status);
5912 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005913 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005914 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005915 job->jv_termsig = get_signal_name(WTERMSIG(status));
5916 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005917 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005918 {
5919 ch_log(job->jv_channel, "Job ended");
5920 job->jv_status = JOB_ENDED;
5921 }
5922 return job;
5923 }
5924 }
5925 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005926}
5927
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005928/*
5929 * Send a (deadly) signal to "job".
5930 * Return FAIL if "how" is not a valid name.
5931 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005932 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005933mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005934{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005935 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005936
Bram Moolenaar923d9262016-02-25 20:56:01 +01005937 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005938 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005939 else if (STRCMP(how, "hup") == 0)
5940 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005941 else if (STRCMP(how, "quit") == 0)
5942 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005943 else if (STRCMP(how, "int") == 0)
5944 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005945 else if (STRCMP(how, "kill") == 0)
5946 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005947#ifdef SIGWINCH
5948 else if (STRCMP(how, "winch") == 0)
5949 sig = SIGWINCH;
5950#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005951 else if (isdigit(*how))
5952 sig = atoi((char *)how);
5953 else
5954 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005955
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005956 // Never kill ourselves!
5957 if (job->jv_pid != 0)
5958 {
5959 // TODO: have an option to only kill the process, not the group?
5960 kill(-job->jv_pid, sig);
5961 kill(job->jv_pid, sig);
5962 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005963
Bram Moolenaar835dc632016-02-07 14:27:38 +01005964 return OK;
5965}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005966
5967/*
5968 * Clear the data related to "job".
5969 */
5970 void
5971mch_clear_job(job_T *job)
5972{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005973 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005974# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005975 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005976# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005977 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005978# endif
5979}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005980#endif
5981
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005982#if defined(FEAT_TERMINAL) || defined(PROTO)
5983 int
5984mch_create_pty_channel(job_T *job, jobopt_T *options)
5985{
5986 int pty_master_fd = -1;
5987 int pty_slave_fd = -1;
5988 channel_T *channel;
5989
Bram Moolenaar59386482019-02-10 22:43:46 +01005990 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02005991 if (pty_master_fd < 0 || pty_slave_fd < 0)
5992 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005993 close(pty_slave_fd);
5994
5995 channel = add_channel();
5996 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005997 {
5998 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005999 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02006000 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02006001 if (job->jv_tty_out != NULL)
6002 ch_log(channel, "using pty %s on fd %d",
6003 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006004 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006005 channel->ch_keep_open = TRUE;
6006
Bram Moolenaar0f873732019-12-05 20:28:46 +01006007 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6008 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006009 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006010 channel_set_job(channel, job, options);
6011 return OK;
6012}
6013#endif
6014
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015/*
6016 * Check for CTRL-C typed by reading all available characters.
6017 * In cooked mode we should get SIGINT, no need to check.
6018 */
6019 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006020mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006022 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006023 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 fill_input_buf(FALSE);
6025}
6026
6027/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006028 * Wait "msec" msec until a character is available from the mouse, keyboard,
6029 * from inbuf[].
6030 * "msec" == -1 will block forever.
6031 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006032 * When "ignore_input" is TRUE even check for pending input when input is
6033 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006034 * "interrupted" (if not NULL) is set to TRUE when no character is available
6035 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006036 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006037 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 */
6039 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006040WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006042#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006043 return ui_wait_for_chars_or_timer(
6044 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006045#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006046 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006047#endif
6048}
6049
6050/*
6051 * Wait "msec" msec until a character is available from the mouse or keyboard
6052 * or from inbuf[].
6053 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006054 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006055 * "interrupted" (if not NULL) is set to TRUE when no character is available
6056 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006057 * When a GUI is being used, this will never get called -- webb
6058 */
6059 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006060WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006061{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062#ifdef FEAT_MOUSE_GPM
6063 int gpm_process_wanted;
6064#endif
6065#ifdef FEAT_XCLIPBOARD
6066 int rest;
6067#endif
6068 int avail;
6069
Bram Moolenaar0f873732019-12-05 20:28:46 +01006070 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 return 1;
6072
6073#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006074 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 if (WantQueryMouse)
6076 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006077 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006078 if (!no_query_mouse_for_testing)
6079 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 }
6081#endif
6082
6083 /*
6084 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6085 * events. This is a bit complicated, because they might both be defined.
6086 */
6087#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6088# ifdef FEAT_XCLIPBOARD
6089 rest = 0;
6090 if (do_xterm_trace())
6091 rest = msec;
6092# endif
6093 do
6094 {
6095# ifdef FEAT_XCLIPBOARD
6096 if (rest != 0)
6097 {
6098 msec = XT_TRACE_DELAY;
6099 if (rest >= 0 && rest < XT_TRACE_DELAY)
6100 msec = rest;
6101 if (rest >= 0)
6102 rest -= msec;
6103 }
6104# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006105# ifdef FEAT_SOUND_CANBERRA
6106 // Invoke any pending sound callbacks.
6107 if (has_sound_callback_in_queue())
6108 invoke_sound_callback();
6109# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110# ifdef FEAT_MOUSE_GPM
6111 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006112 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006113 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006114 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006116 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006118# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006120 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 return 1;
6122# ifdef FEAT_XCLIPBOARD
6123 if (rest == 0 || !do_xterm_trace())
6124# endif
6125 break;
6126 }
6127 }
6128 while (FALSE
6129# ifdef FEAT_MOUSE_GPM
6130 || (gpm_process_wanted && mch_gpm_process() == 0)
6131# endif
6132# ifdef FEAT_XCLIPBOARD
6133 || (!avail && rest != 0)
6134# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006135 )
6136 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137
6138#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006139 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140#endif
6141 return avail;
6142}
6143
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006144#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145/*
6146 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006147 * "msec" == 0 will check for characters once.
6148 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006151 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006152 * "interrupted" (if not NULL) is set to TRUE when no character is available
6153 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006155 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006156RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157{
6158 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006159 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006160#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 static int busy = FALSE;
6162
Bram Moolenaar0f873732019-12-05 20:28:46 +01006163 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164# define MAY_LOOP
6165
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006166# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006167 // Remember at what time we started, so that we know how much longer we
6168 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006169 long start_msec = msec;
6170 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006172 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006173 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174# endif
6175
Bram Moolenaar0f873732019-12-05 20:28:46 +01006176 // Handle being called recursively. This may happen for the session
6177 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 if (busy)
6179 return 0;
6180#endif
6181
6182#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006183 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184#endif
6185 {
6186#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006187 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006188# ifdef FEAT_MZSCHEME
6189 int mzquantum_used = FALSE;
6190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191#endif
6192#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006193 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006194 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 int nfd;
6196# ifdef FEAT_XCLIPBOARD
6197 int xterm_idx = -1;
6198# endif
6199# ifdef FEAT_MOUSE_GPM
6200 int gpm_idx = -1;
6201# endif
6202# ifdef USE_XSMP
6203 int xsmp_idx = -1;
6204# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006205 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006207# ifdef FEAT_MZSCHEME
6208 mzvim_check_threads();
6209 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6210 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006211 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006212 mzquantum_used = TRUE;
6213 }
6214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 fds[0].fd = fd;
6216 fds[0].events = POLLIN;
6217 nfd = 1;
6218
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006220 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 if (xterm_Shell != (Widget)0)
6222 {
6223 xterm_idx = nfd;
6224 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6225 fds[nfd].events = POLLIN;
6226 nfd++;
6227 }
6228# endif
6229# ifdef FEAT_MOUSE_GPM
6230 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6231 {
6232 gpm_idx = nfd;
6233 fds[nfd].fd = gpm_fd;
6234 fds[nfd].events = POLLIN;
6235 nfd++;
6236 }
6237# endif
6238# ifdef USE_XSMP
6239 if (xsmp_icefd != -1)
6240 {
6241 xsmp_idx = nfd;
6242 fds[nfd].fd = xsmp_icefd;
6243 fds[nfd].events = POLLIN;
6244 nfd++;
6245 }
6246# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006247#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006248 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006249#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006250 if (interrupted != NULL)
6251 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006253 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006254
6255 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006256 if (result == 0 && interrupted != NULL && ret > 0)
6257 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006258
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006259# ifdef FEAT_MZSCHEME
6260 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006261 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006262 finished = FALSE;
6263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265# ifdef FEAT_XCLIPBOARD
6266 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6267 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006268 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006270 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 finished = FALSE;
6272 }
6273# endif
6274# ifdef FEAT_MOUSE_GPM
6275 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277# endif
6278# ifdef USE_XSMP
6279 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6280 {
6281 if (fds[xsmp_idx].revents & POLLIN)
6282 {
6283 busy = TRUE;
6284 xsmp_handle_requests();
6285 busy = FALSE;
6286 }
6287 else if (fds[xsmp_idx].revents & POLLHUP)
6288 {
6289 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006290 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 xsmp_close();
6292 }
6293 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006294 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 }
6296# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006297#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006298 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006299 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006300 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
Bram Moolenaar0f873732019-12-05 20:28:46 +01006303#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304
6305 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006306 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006307 // These are static because they can take 8 Kbyte each and cause the
6308 // signal stack to run out with -O3.
6309 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006311 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006313# ifdef FEAT_MZSCHEME
6314 mzvim_check_threads();
6315 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6316 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006317 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006318 mzquantum_used = TRUE;
6319 }
6320# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006322 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006324 tv.tv_sec = towait / 1000;
6325 tv.tv_usec = (towait % 1000) * (1000000/1000);
6326 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006328 else
6329 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331 /*
6332 * Select on ready for reading and exceptional condition (end of file).
6333 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006334select_eintr:
6335 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006336 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 FD_ZERO(&efds);
6338 FD_SET(fd, &rfds);
6339# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006340 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 FD_SET(fd, &efds);
6342# endif
6343 maxfd = fd;
6344
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006346 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 if (xterm_Shell != (Widget)0)
6348 {
6349 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6350 if (maxfd < ConnectionNumber(xterm_dpy))
6351 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006352
Bram Moolenaar0f873732019-12-05 20:28:46 +01006353 // An event may have already been read but not handled. In
6354 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006355 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 }
6357# endif
6358# ifdef FEAT_MOUSE_GPM
6359 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6360 {
6361 FD_SET(gpm_fd, &rfds);
6362 FD_SET(gpm_fd, &efds);
6363 if (maxfd < gpm_fd)
6364 maxfd = gpm_fd;
6365 }
6366# endif
6367# ifdef USE_XSMP
6368 if (xsmp_icefd != -1)
6369 {
6370 FD_SET(xsmp_icefd, &rfds);
6371 FD_SET(xsmp_icefd, &efds);
6372 if (maxfd < xsmp_icefd)
6373 maxfd = xsmp_icefd;
6374 }
6375# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006376# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006377 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006378# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006379 if (interrupted != NULL)
6380 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381
Bram Moolenaar643b6142018-09-12 20:29:09 +02006382 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6383 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006384 result = ret > 0 && FD_ISSET(fd, &rfds);
6385 if (result)
6386 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006387 else if (interrupted != NULL && ret > 0)
6388 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006389
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006390# ifdef EINTR
6391 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006392 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006393 // Check whether window has been resized, EINTR may be caused by
6394 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006395 if (do_resize)
6396 handle_resize();
6397
Bram Moolenaar0f873732019-12-05 20:28:46 +01006398 // Interrupted by a signal, need to try again. We ignore msec
6399 // here, because we do want to check even after a timeout if
6400 // characters are available. Needed for reading output of an
6401 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006402 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006403 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006404# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006405# ifdef __TANDEM
6406 if (ret == -1 && errno == ENOTSUP)
6407 {
6408 FD_ZERO(&rfds);
6409 FD_ZERO(&efds);
6410 ret = 0;
6411 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006412# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006413# ifdef FEAT_MZSCHEME
6414 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006415 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006416 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417# endif
6418
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419# ifdef FEAT_XCLIPBOARD
6420 if (ret > 0 && xterm_Shell != (Widget)0
6421 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6422 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006423 xterm_update(); // Maybe we should hand out clipboard
6424 // continue looping when we only got the X event and the input
6425 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 if (--ret == 0 && !input_available())
6427 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006428 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 finished = FALSE;
6430 }
6431 }
6432# endif
6433# ifdef FEAT_MOUSE_GPM
6434 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6435 {
6436 if (FD_ISSET(gpm_fd, &efds))
6437 gpm_close();
6438 else if (FD_ISSET(gpm_fd, &rfds))
6439 *check_for_gpm = 1;
6440 }
6441# endif
6442# ifdef USE_XSMP
6443 if (ret > 0 && xsmp_icefd != -1)
6444 {
6445 if (FD_ISSET(xsmp_icefd, &efds))
6446 {
6447 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006448 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 xsmp_close();
6450 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006451 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 }
6453 else if (FD_ISSET(xsmp_icefd, &rfds))
6454 {
6455 busy = TRUE;
6456 xsmp_handle_requests();
6457 busy = FALSE;
6458 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006459 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 }
6461 }
6462# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006463#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006464 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006465 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006466 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468
Bram Moolenaar0f873732019-12-05 20:28:46 +01006469#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470
6471#ifdef MAY_LOOP
6472 if (finished || msec == 0)
6473 break;
6474
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006475# ifdef FEAT_CLIENTSERVER
6476 if (server_waiting())
6477 break;
6478# endif
6479
Bram Moolenaar0f873732019-12-05 20:28:46 +01006480 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 if (msec > 0)
6482 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006483# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006484 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006485 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006487 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 msec = msec / 2;
6489# endif
6490 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006491 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 }
6493#endif
6494 }
6495
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006496 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497}
6498
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006500 * Expand a path into all matching files and/or directories. Handles "*",
6501 * "?", "[a-z]", "**", etc.
6502 * "path" has backslashes before chars that are not to be expanded.
6503 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 */
6505 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006506mch_expandpath(
6507 garray_T *gap,
6508 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006509 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
Bram Moolenaar02743632005-07-25 20:42:36 +00006511 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513
6514/*
6515 * mch_expand_wildcards() - this code does wild-card pattern matching using
6516 * the shell
6517 *
6518 * return OK for success, FAIL for error (you may lose some memory) and put
6519 * an error message in *file.
6520 *
6521 * num_pat is number of input patterns
6522 * pat is array of pointers to input patterns
6523 * num_file is pointer to number of matched file names
6524 * file is pointer to array of pointers to matched file names
6525 */
6526
6527#ifndef SEEK_SET
6528# define SEEK_SET 0
6529#endif
6530#ifndef SEEK_END
6531# define SEEK_END 2
6532#endif
6533
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006534#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006535
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006537mch_expand_wildcards(
6538 int num_pat,
6539 char_u **pat,
6540 int *num_file,
6541 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006542 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543{
6544 int i;
6545 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006546 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 char_u *p;
6548 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549
Bram Moolenaarc7247912008-01-13 12:54:11 +00006550 /*
6551 * This is the non-OS/2 implementation (really Unix).
6552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 int j;
6554 char_u *tempname;
6555 char_u *command;
6556 FILE *fd;
6557 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006558#define STYLE_ECHO 0 // use "echo", the default
6559#define STYLE_GLOB 1 // use "glob", for csh
6560#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6561#define STYLE_PRINT 3 // use "print -N", for zsh
6562#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6563 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 int shell_style = STYLE_ECHO;
6565 int check_spaces;
6566 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006567 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006568 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006569 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
Bram Moolenaar0f873732019-12-05 20:28:46 +01006571 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 *file = NULL;
6573
6574 /*
6575 * If there are no wildcards, just copy the names to allocated memory.
6576 * Saves a lot of time, because we don't have to start a new shell.
6577 */
6578 if (!have_wildcard(num_pat, pat))
6579 return save_patterns(num_pat, pat, num_file, file);
6580
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006581# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006582 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006583 if (sandbox != 0 && check_secure())
6584 return FAIL;
6585# endif
6586
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 /*
6588 * Don't allow the use of backticks in secure and restricted mode.
6589 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006590 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 for (i = 0; i < num_pat; ++i)
6592 if (vim_strchr(pat[i], '`') != NULL
6593 && (check_restricted() || check_secure()))
6594 return FAIL;
6595
6596 /*
6597 * get a name for the temp file
6598 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006599 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006601 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 return FAIL;
6603 }
6604
6605 /*
6606 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006607 * file.
6608 * STYLE_BT: NL separated
6609 * If expanding `cmd` execute it directly.
6610 * STYLE_GLOB: NUL separated
6611 * If we use *csh, "glob" will work better than "echo".
6612 * STYLE_PRINT: NL or NUL separated
6613 * If we use *zsh, "print -N" will work better than "glob".
6614 * STYLE_VIMGLOB: NL separated
6615 * If we use *sh*, we define "vimglob()".
6616 * STYLE_ECHO: space separated.
6617 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 */
6619 if (num_pat == 1 && *pat[0] == '`'
6620 && (len = STRLEN(pat[0])) > 2
6621 && *(pat[0] + len - 1) == '`')
6622 shell_style = STYLE_BT;
6623 else if ((len = STRLEN(p_sh)) >= 3)
6624 {
6625 if (STRCMP(p_sh + len - 3, "csh") == 0)
6626 shell_style = STYLE_GLOB;
6627 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6628 shell_style = STYLE_PRINT;
6629 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006630 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6631 "sh") != NULL)
6632 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633
Bram Moolenaar0f873732019-12-05 20:28:46 +01006634 // Compute the length of the command. We need 2 extra bytes: for the
6635 // optional '&' and for the NUL.
6636 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006638 if (shell_style == STYLE_VIMGLOB)
6639 len += STRLEN(sh_vimglob_func);
6640
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006641 for (i = 0; i < num_pat; ++i)
6642 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006643 // Count the length of the patterns in the same way as they are put in
6644 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006645#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006646 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006647#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006648 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006649 for (j = 0; pat[i][j] != NUL; ++j)
6650 {
6651 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006652 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006653 ++len;
6654 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006655#endif
6656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 command = alloc(len);
6658 if (command == NULL)
6659 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006660 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 vim_free(tempname);
6662 return FAIL;
6663 }
6664
6665 /*
6666 * Build the shell command:
6667 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6668 * recognizes this).
6669 * - Add the shell command to print the expanded names.
6670 * - Add the temp file name.
6671 * - Add the file name patterns.
6672 */
6673 if (shell_style == STYLE_BT)
6674 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006675 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006676 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006677 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006679 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006680 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006682 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006684 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 *p = ' ';
6686 }
6687 STRCAT(command, ">");
6688 }
6689 else
6690 {
6691 if (flags & EW_NOTFOUND)
6692 STRCPY(command, "set nonomatch; ");
6693 else
6694 STRCPY(command, "unset nonomatch; ");
6695 if (shell_style == STYLE_GLOB)
6696 STRCAT(command, "glob >");
6697 else if (shell_style == STYLE_PRINT)
6698 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006699 else if (shell_style == STYLE_VIMGLOB)
6700 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 else
6702 STRCAT(command, "echo >");
6703 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006704
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006706
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 if (shell_style != STYLE_BT)
6708 for (i = 0; i < num_pat; ++i)
6709 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006710 // When using system() always add extra quotes, because the shell
6711 // is started twice. Otherwise put a backslash before special
6712 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713#ifdef USE_SYSTEM
6714 STRCAT(command, " \"");
6715 STRCAT(command, pat[i]);
6716 STRCAT(command, "\"");
6717#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006718 int intick = FALSE;
6719
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 p = command + STRLEN(command);
6721 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006722 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006723 {
6724 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006725 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006726 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6727 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006728 // Remove a backslash, take char literally. But keep
6729 // backslash inside backticks, before a special character
6730 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006731 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006732 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6733 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006734 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006735 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006736 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006737 else if (!intick
6738 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6739 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006740 // Put a backslash before a special character, but not
6741 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6742 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006743 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006744
Bram Moolenaar0f873732019-12-05 20:28:46 +01006745 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006746 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 *p = NUL;
6749#endif
6750 }
6751 if (flags & EW_SILENT)
6752 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006753 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006754 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
6756 /*
6757 * Using zsh -G: If a pattern has no matches, it is just deleted from
6758 * the argument list, otherwise zsh gives an error message and doesn't
6759 * expand any other pattern.
6760 */
6761 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006762 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763
6764 /*
6765 * If we use -f then shell variables set in .cshrc won't get expanded.
6766 * vi can do it, so we will too, but it is only necessary if there is a "$"
6767 * in one of the patterns, otherwise we can still use the fast option.
6768 */
6769 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006770 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771
6772 /*
6773 * execute the shell command
6774 */
6775 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6776
Bram Moolenaar0f873732019-12-05 20:28:46 +01006777 // When running in the background, give it some time to create the temp
6778 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006779 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006780 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781
Bram Moolenaar0f873732019-12-05 20:28:46 +01006782 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 show_shell_mess = TRUE;
6784 vim_free(command);
6785
Bram Moolenaar0f873732019-12-05 20:28:46 +01006786 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 {
6788 mch_remove(tempname);
6789 vim_free(tempname);
6790 /*
6791 * With interactive completion, the error message is not printed.
6792 * However with USE_SYSTEM, I don't know how to turn off error messages
6793 * from the shell, so screen may still get messed up -- webb.
6794 */
6795#ifndef USE_SYSTEM
6796 if (!(flags & EW_SILENT))
6797#endif
6798 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006799 redraw_later_clear(); // probably messed up screen
6800 msg_putchar('\n'); // clear bottom line quickly
6801 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802#ifdef USE_SYSTEM
6803 if (!(flags & EW_SILENT))
6804#endif
6805 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006806 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006807 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 }
6809 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006810 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6811 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (shell_style == STYLE_BT)
6813 return FAIL;
6814 goto notfound;
6815 }
6816
6817 /*
6818 * read the names from the file into memory
6819 */
6820 fd = fopen((char *)tempname, READBIN);
6821 if (fd == NULL)
6822 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006823 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 if (!(flags & EW_SILENT))
6825 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006826 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006827 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 }
6829 vim_free(tempname);
6830 goto notfound;
6831 }
6832 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006833 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006835 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006836 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006837 buffer = NULL;
6838 else
6839 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 if (buffer == NULL)
6841 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006842 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 mch_remove(tempname);
6844 vim_free(tempname);
6845 fclose(fd);
6846 return FAIL;
6847 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006848 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 i = fread((char *)buffer, 1, len, fd);
6850 fclose(fd);
6851 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006852 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006854 // unexpected read error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006855 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 vim_free(tempname);
6857 vim_free(buffer);
6858 return FAIL;
6859 }
6860 vim_free(tempname);
6861
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006862# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006863 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006865 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6867 *p++ = buffer[i];
6868 len = p - buffer;
6869# endif
6870
6871
Bram Moolenaar0f873732019-12-05 20:28:46 +01006872 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 if (shell_style == STYLE_ECHO)
6874 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006875 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006877 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {
6879 while (*p != ' ' && *p != '\n')
6880 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006881 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 }
6883 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006884 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006885 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006887 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006889 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 {
6891 while (*p != '\n' && *p != NUL)
6892 ++p;
6893 if (*p != NUL)
6894 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006895 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 }
6897 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006898 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 else
6900 {
6901 /*
6902 * Some versions of zsh use spaces instead of NULs to separate
6903 * results. Only do this when there is no NUL before the end of the
6904 * buffer, otherwise we would never be able to use file names with
6905 * embedded spaces when zsh does use NULs.
6906 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6907 * don't check for spaces again.
6908 */
6909 check_spaces = FALSE;
6910 if (shell_style == STYLE_PRINT && !did_find_nul)
6911 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006912 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006913 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006914 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 did_find_nul = TRUE;
6916 else
6917 check_spaces = TRUE;
6918 }
6919
6920 /*
6921 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6922 * already is one, for STYLE_GLOB it needs to be added.
6923 */
6924 if (len && buffer[len - 1] == NUL)
6925 --len;
6926 else
6927 buffer[len] = NUL;
6928 i = 0;
6929 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006930 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {
6932 ++i;
6933 *p = NUL;
6934 }
6935 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006936 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 }
6938 if (i == 0)
6939 {
6940 /*
6941 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6942 * /bin/sh will happily expand it to nothing rather than returning an
6943 * error; and hey, it's good to check anyway -- webb.
6944 */
6945 vim_free(buffer);
6946 goto notfound;
6947 }
6948 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006949 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 if (*file == NULL)
6951 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006952 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 vim_free(buffer);
6954 return FAIL;
6955 }
6956
6957 /*
6958 * Isolate the individual file names.
6959 */
6960 p = buffer;
6961 for (i = 0; i < *num_file; ++i)
6962 {
6963 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006964 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006965 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6966 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006968 while (!(shell_style == STYLE_ECHO && *p == ' ')
6969 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006971 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 *p = NUL;
6973 else
6974 {
6975 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006976 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 }
6978 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006979 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006981 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006983 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 }
6985 }
6986
6987 /*
6988 * Move the file names to allocated memory.
6989 */
6990 for (j = 0, i = 0; i < *num_file; ++i)
6991 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006992 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6994 continue;
6995
Bram Moolenaar0f873732019-12-05 20:28:46 +01006996 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 dir = (mch_isdir((*file)[i]));
6998 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6999 continue;
7000
Bram Moolenaar0f873732019-12-05 20:28:46 +01007001 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007002 if (!dir && (flags & EW_EXEC)
7003 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007004 continue;
7005
Bram Moolenaar964b3742019-05-24 18:54:09 +02007006 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 if (p)
7008 {
7009 STRCPY(p, (*file)[i]);
7010 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007011 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 (*file)[j++] = p;
7013 }
7014 }
7015 vim_free(buffer);
7016 *num_file = j;
7017
Bram Moolenaar0f873732019-12-05 20:28:46 +01007018 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007020 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 goto notfound;
7022 }
7023
7024 return OK;
7025
7026notfound:
7027 if (flags & EW_NOTFOUND)
7028 return save_patterns(num_pat, pat, num_file, file);
7029 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030}
7031
Bram Moolenaar0f873732019-12-05 20:28:46 +01007032#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007035save_patterns(
7036 int num_pat,
7037 char_u **pat,
7038 int *num_file,
7039 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040{
7041 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007042 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007044 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 if (*file == NULL)
7046 return FAIL;
7047 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007048 {
7049 s = vim_strsave(pat[i]);
7050 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007051 // Be compatible with expand_filename(): halve the number of
7052 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007053 backslash_halve(s);
7054 (*file)[i] = s;
7055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 *num_file = num_pat;
7057 return OK;
7058}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060/*
7061 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7062 * expand.
7063 */
7064 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007065mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007067 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 if (*p == '\\' && p[1] != NUL)
7070 ++p;
7071 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 if (vim_strchr((char_u *)
7073#ifdef VMS
7074 "*?%"
7075#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077#endif
7078 , *p) != NULL)
7079 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 }
7081 return FALSE;
7082}
7083
7084/*
7085 * Return TRUE if the string "p" contains a wildcard.
7086 * Don't recognize '~' at the end as a wildcard.
7087 */
7088 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007089mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007091 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 if (*p == '\\' && p[1] != NUL)
7094 ++p;
7095 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 if (vim_strchr((char_u *)
7097#ifdef VMS
7098 "*?%$"
7099#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101#endif
7102 , *p) != NULL
7103 || (*p == '~' && p[1] != NUL))
7104 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 }
7106 return FALSE;
7107}
7108
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007110have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111{
7112 int i;
7113
7114 for (i = 0; i < num; i++)
7115 if (mch_has_wildcard(file[i]))
7116 return 1;
7117 return 0;
7118}
7119
7120 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007121have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
7123 int i;
7124
7125 for (i = 0; i < num; i++)
7126 if (vim_strchr(file[i], '$') != NULL)
7127 return TRUE;
7128 return FALSE;
7129}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007131#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132/*
7133 * Scaled-down version of rename(), which is missing in Xenix.
7134 * This version can only move regular files and will fail if the
7135 * destination exists.
7136 */
7137 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007138mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
7140 struct stat st;
7141
Bram Moolenaar0f873732019-12-05 20:28:46 +01007142 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007144 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007146 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 return 0;
7148 return -1;
7149}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007150#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007152#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153/*
7154 * Initializes connection with gpm (if it isn't already opened)
7155 * Return 1 if succeeded (or connection already opened), 0 if failed
7156 */
7157 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007158gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007160 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161
7162 if (!gpm_flag)
7163 {
7164 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7165 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007166 // Default handling for mouse move
7167 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 gpm_connect.maxMod = 0xffff;
7169 if (Gpm_Open(&gpm_connect, 0) > 0)
7170 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007171 // gpm library tries to handling TSTP causes
7172 // problems. Anyways, we close connection to Gpm whenever
7173 // we are going to suspend or starting an external process
7174 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007175# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007177# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007178 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 }
7180 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007181 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 return 0;
7183 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007184 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185}
7186
7187/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007188 * Returns TRUE if the GPM mouse is enabled.
7189 */
7190 int
7191gpm_enabled(void)
7192{
7193 return gpm_flag && gpm_fd >= 0;
7194}
7195
7196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 */
7199 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007200gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007202 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 Gpm_Close();
7204}
7205
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007206/*
7207 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007209 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 */
7211 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007212mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213{
7214 int button;
7215 static Gpm_Event gpm_event;
7216 char_u string[6];
7217 int_u vim_modifiers;
7218 int row,col;
7219 unsigned char buttons_mask;
7220 unsigned char gpm_modifiers;
7221 static unsigned char old_buttons = 0;
7222
7223 Gpm_GetEvent(&gpm_event);
7224
7225#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007226 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 if (hold_gui_events)
7228 return 0;
7229#endif
7230
7231 row = gpm_event.y - 1;
7232 col = gpm_event.x - 1;
7233
Bram Moolenaar0f873732019-12-05 20:28:46 +01007234 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 string[1] = 'M';
7236 string[2] = 'G';
7237 switch (GPM_BARE_EVENTS(gpm_event.type))
7238 {
7239 case GPM_DRAG:
7240 string[3] = MOUSE_DRAG;
7241 break;
7242 case GPM_DOWN:
7243 buttons_mask = gpm_event.buttons & ~old_buttons;
7244 old_buttons = gpm_event.buttons;
7245 switch (buttons_mask)
7246 {
7247 case GPM_B_LEFT:
7248 button = MOUSE_LEFT;
7249 break;
7250 case GPM_B_MIDDLE:
7251 button = MOUSE_MIDDLE;
7252 break;
7253 case GPM_B_RIGHT:
7254 button = MOUSE_RIGHT;
7255 break;
7256 default:
7257 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007258 // Don't know what to do. Can more than one button be
7259 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 }
7261 string[3] = (char_u)(button | 0x20);
7262 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7263 break;
7264 case GPM_UP:
7265 string[3] = MOUSE_RELEASE;
7266 old_buttons &= ~gpm_event.buttons;
7267 break;
7268 default:
7269 return 0;
7270 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007271 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 gpm_modifiers = gpm_event.modifiers;
7273 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007274 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7275 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7276 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7278 vim_modifiers |= MOUSE_SHIFT;
7279
7280 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7281 vim_modifiers |= MOUSE_CTRL;
7282 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7283 vim_modifiers |= MOUSE_ALT;
7284 string[3] |= vim_modifiers;
7285 string[4] = (char_u)(col + ' ' + 1);
7286 string[5] = (char_u)(row + ' ' + 1);
7287 add_to_input_buf(string, 6);
7288 return 6;
7289}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007290#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007292#ifdef FEAT_SYSMOUSE
7293/*
7294 * Initialize connection with sysmouse.
7295 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7296 * output, any sysmouse output than will be processed via sig_sysmouse().
7297 * Return OK if succeeded, FAIL if failed.
7298 */
7299 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007300sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007301{
7302 struct mouse_info mouse;
7303
7304 mouse.operation = MOUSE_MODE;
7305 mouse.u.mode.mode = 0;
7306 mouse.u.mode.signal = SIGUSR2;
7307 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7308 {
7309 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7310 mouse.operation = MOUSE_SHOW;
7311 ioctl(1, CONS_MOUSECTL, &mouse);
7312 return OK;
7313 }
7314 return FAIL;
7315}
7316
7317/*
7318 * Stop processing SIGUSR2 signals, and also make sure that
7319 * virtual console do not send us any sysmouse related signal.
7320 */
7321 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007322sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007323{
7324 struct mouse_info mouse;
7325
7326 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7327 mouse.operation = MOUSE_MODE;
7328 mouse.u.mode.mode = 0;
7329 mouse.u.mode.signal = 0;
7330 ioctl(1, CONS_MOUSECTL, &mouse);
7331}
7332
7333/*
7334 * Gets info from sysmouse and adds special keys to input buf.
7335 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007336 static RETSIGTYPE
7337sig_sysmouse SIGDEFARG(sigarg)
7338{
7339 struct mouse_info mouse;
7340 struct video_info video;
7341 char_u string[6];
7342 int row, col;
7343 int button;
7344 int buttons;
7345 static int oldbuttons = 0;
7346
7347#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007348 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007349 if (hold_gui_events)
7350 return;
7351#endif
7352
7353 mouse.operation = MOUSE_GETINFO;
7354 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7355 && ioctl(1, FBIO_MODEINFO, &video) != -1
7356 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7357 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7358 {
7359 row = mouse.u.data.y / video.vi_cheight;
7360 col = mouse.u.data.x / video.vi_cwidth;
7361 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007362 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007363 string[1] = 'M';
7364 string[2] = 'S';
7365 if (oldbuttons == buttons && buttons != 0)
7366 {
7367 button = MOUSE_DRAG;
7368 }
7369 else
7370 {
7371 switch (buttons)
7372 {
7373 case 0:
7374 button = MOUSE_RELEASE;
7375 break;
7376 case 1:
7377 button = MOUSE_LEFT;
7378 break;
7379 case 2:
7380 button = MOUSE_MIDDLE;
7381 break;
7382 case 4:
7383 button = MOUSE_RIGHT;
7384 break;
7385 default:
7386 return;
7387 }
7388 oldbuttons = buttons;
7389 }
7390 string[3] = (char_u)(button);
7391 string[4] = (char_u)(col + ' ' + 1);
7392 string[5] = (char_u)(row + ' ' + 1);
7393 add_to_input_buf(string, 6);
7394 }
7395 return;
7396}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007397#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007398
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007400typedef char_u * (*STRPROCSTR)(char_u *);
7401typedef char_u * (*INTPROCSTR)(int);
7402typedef int (*STRPROCINT)(char_u *);
7403typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404
7405/*
7406 * Call a DLL routine which takes either a string or int param
7407 * and returns an allocated string.
7408 */
7409 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007410mch_libcall(
7411 char_u *libname,
7412 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007413 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007414 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007415 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007416 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417{
7418# if defined(USE_DLOPEN)
7419 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007420 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421# else
7422 shl_t hinstLib;
7423# endif
7424 STRPROCSTR ProcAdd;
7425 INTPROCSTR ProcAddI;
7426 char_u *retval_str = NULL;
7427 int retval_int = 0;
7428 int success = FALSE;
7429
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007430 /*
7431 * Get a handle to the DLL module.
7432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007434 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007435 (void)dlerror();
7436
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 hinstLib = dlopen((char *)libname, RTLD_LAZY
7438# ifdef RTLD_LOCAL
7439 | RTLD_LOCAL
7440# endif
7441 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007442 if (hinstLib == NULL)
7443 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007444 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007445 dlerr = (char *)dlerror();
7446 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007447 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449# else
7450 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7451# endif
7452
Bram Moolenaar0f873732019-12-05 20:28:46 +01007453 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 if (hinstLib != NULL)
7455 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007456# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 /*
7458 * Catch a crash when calling the library function. For example when
7459 * using a number where a string pointer is expected.
7460 */
7461 mch_startjmp();
7462 if (SETJMP(lc_jump_env) != 0)
7463 {
7464 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007465# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007466 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 mch_didjmp();
7469 }
7470 else
7471# endif
7472 {
7473 retval_str = NULL;
7474 retval_int = 0;
7475
7476 if (argstring != NULL)
7477 {
7478# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007479 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007480 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481# else
7482 if (shl_findsym(&hinstLib, (const char *)funcname,
7483 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7484 ProcAdd = NULL;
7485# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007486 if ((success = (ProcAdd != NULL
7487# if defined(USE_DLOPEN)
7488 && dlerr == NULL
7489# endif
7490 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 {
7492 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007493 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 else
7495 retval_str = (ProcAdd)(argstring);
7496 }
7497 }
7498 else
7499 {
7500# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007501 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007502 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503# else
7504 if (shl_findsym(&hinstLib, (const char *)funcname,
7505 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7506 ProcAddI = NULL;
7507# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007508 if ((success = (ProcAddI != NULL
7509# if defined(USE_DLOPEN)
7510 && dlerr == NULL
7511# endif
7512 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 {
7514 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007515 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 else
7517 retval_str = (ProcAddI)(argint);
7518 }
7519 }
7520
Bram Moolenaar0f873732019-12-05 20:28:46 +01007521 // Save the string before we free the library.
7522 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 if (string_result == NULL)
7524 *number_result = retval_int;
7525 else if (retval_str != NULL
7526 && retval_str != (char_u *)1
7527 && retval_str != (char_u *)-1)
7528 *string_result = vim_strsave(retval_str);
7529 }
7530
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007531# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 mch_endjmp();
7533# ifdef SIGHASARG
7534 if (lc_signal != 0)
7535 {
7536 int i;
7537
Bram Moolenaar0f873732019-12-05 20:28:46 +01007538 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 for (i = 0; signal_info[i].sig != -1; i++)
7540 if (lc_signal == signal_info[i].sig)
7541 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007542 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 }
7544# endif
7545# endif
7546
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007548 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007549 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007550 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007551
Bram Moolenaar0f873732019-12-05 20:28:46 +01007552 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 (void)dlclose(hinstLib);
7554# else
7555 (void)shl_unload(hinstLib);
7556# endif
7557 }
7558
7559 if (!success)
7560 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007561 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 return FAIL;
7563 }
7564
7565 return OK;
7566}
7567#endif
7568
7569#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007570static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571static int xterm_button;
7572
7573/*
7574 * Setup a dummy window for X selections in a terminal.
7575 */
7576 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007577setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578{
7579 int z = 0;
7580 char *strp = "";
7581 Widget AppShell;
7582
7583 if (!x_connect_to_server())
7584 return;
7585
7586 open_app_context();
7587 if (app_context != NULL && xterm_Shell == (Widget)0)
7588 {
7589 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007590# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007592# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007593# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007594 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595
7596 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007597 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598# endif
7599
Bram Moolenaar0f873732019-12-05 20:28:46 +01007600 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 oldhandler = XSetErrorHandler(x_error_check);
7602
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007603# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007604 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7606 mch_startjmp();
7607 if (SETJMP(lc_jump_env) != 0)
7608 {
7609 mch_didjmp();
7610 xterm_dpy = NULL;
7611 }
7612 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007613# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 {
7615 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7616 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007617 if (xterm_dpy != NULL)
7618 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007619# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 }
7623
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007624# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007625 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007627# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007628 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 (void)XSetErrorHandler(oldhandler);
7630
7631 if (xterm_dpy == NULL)
7632 {
7633 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007634 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 return;
7636 }
7637
Bram Moolenaar0f873732019-12-05 20:28:46 +01007638 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 (void)XSetIOErrorHandler(x_IOerror_handler);
7640
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007641# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007643 {
7644 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007645 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007646 verbose_leave();
7647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648# endif
7649
Bram Moolenaar0f873732019-12-05 20:28:46 +01007650 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7652 applicationShellWidgetClass, xterm_dpy,
7653 NULL);
7654 if (AppShell == (Widget)0)
7655 return;
7656 xterm_Shell = XtVaCreatePopupShell("VIM",
7657 topLevelShellWidgetClass, AppShell,
7658 XtNmappedWhenManaged, 0,
7659 XtNwidth, 1,
7660 XtNheight, 1,
7661 NULL);
7662 if (xterm_Shell == (Widget)0)
7663 return;
7664
7665 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007666 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 if (x11_display == NULL)
7668 x11_display = xterm_dpy;
7669
7670 XtRealizeWidget(xterm_Shell);
7671 XSync(xterm_dpy, False);
7672 xterm_update();
7673 }
7674 if (xterm_Shell != (Widget)0)
7675 {
7676 clip_init(TRUE);
7677 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7678 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007679 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 if (test_x11_window(xterm_dpy) == FAIL)
7681 x11_window = 0;
7682 if (x11_window != 0)
7683 xterm_trace = 0;
7684 }
7685}
7686
7687 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007688start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689{
7690 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7691 return;
7692 xterm_trace = 1;
7693 xterm_button = button;
7694 do_xterm_trace();
7695}
7696
7697
7698 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007699stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
7701 if (xterm_trace < 0)
7702 return;
7703 xterm_trace = 0;
7704}
7705
7706/*
7707 * Query the xterm pointer and generate mouse termcodes if necessary
7708 * return TRUE if dragging is active, else FALSE
7709 */
7710 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007711do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
7713 Window root, child;
7714 int root_x, root_y;
7715 int win_x, win_y;
7716 int row, col;
7717 int_u mask_return;
7718 char_u buf[50];
7719 char_u *strp;
7720 long got_hints;
7721 static char_u *mouse_code;
7722 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7723 static int prev_row = 0, prev_col = 0;
7724 static XSizeHints xterm_hints;
7725
7726 if (xterm_trace <= 0)
7727 return FALSE;
7728
7729 if (xterm_trace == 1)
7730 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007731 // Get the hints just before tracking starts. The font size might
7732 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007733 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7734 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 || xterm_hints.width_inc <= 1
7736 || xterm_hints.height_inc <= 1)
7737 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007738 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 return FALSE;
7740 }
7741
Bram Moolenaar0f873732019-12-05 20:28:46 +01007742 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 mouse_code = find_termcode(mouse_name);
7744 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007745 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 xterm_trace = 2;
7747
Bram Moolenaar0f873732019-12-05 20:28:46 +01007748 // Find the offset of the chars, there might be a scrollbar on the
7749 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7751 &win_x, &win_y, &mask_return);
7752 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7753 - (xterm_hints.height_inc / 2);
7754 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7755 xterm_hints.y = 2;
7756 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7757 - (xterm_hints.width_inc / 2);
7758 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7759 xterm_hints.x = 2;
7760 return TRUE;
7761 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007762 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 {
7764 xterm_trace = 0;
7765 return FALSE;
7766 }
7767
7768 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7769 &win_x, &win_y, &mask_return);
7770
7771 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7772 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7773 if (row == prev_row && col == prev_col)
7774 return TRUE;
7775
7776 STRCPY(buf, mouse_code);
7777 strp = buf + STRLEN(buf);
7778 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7779 *strp++ = (char_u)(col + ' ' + 1);
7780 *strp++ = (char_u)(row + ' ' + 1);
7781 *strp = 0;
7782 add_to_input_buf(buf, STRLEN(buf));
7783
7784 prev_row = row;
7785 prev_col = col;
7786 return TRUE;
7787}
7788
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007789# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790/*
7791 * Destroy the display, window and app_context. Required for GTK.
7792 */
7793 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007794clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
7796 if (xterm_Shell != (Widget)0)
7797 {
7798 XtDestroyWidget(xterm_Shell);
7799 xterm_Shell = (Widget)0;
7800 }
7801 if (xterm_dpy != NULL)
7802 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007803# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007804 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 if (x11_display == xterm_dpy)
7808 x11_display = NULL;
7809 xterm_dpy = NULL;
7810 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007811# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 if (app_context != (XtAppContext)NULL)
7813 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007814 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 XtDestroyApplicationContext(app_context);
7816 app_context = (XtAppContext)NULL;
7817 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819}
7820# endif
7821
7822/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007823 * Catch up with GUI or X events.
7824 */
7825 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007826clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007827{
7828# ifdef FEAT_GUI
7829 if (gui.in_use)
7830 gui_mch_update();
7831 else
7832# endif
7833 if (xterm_Shell != (Widget)0)
7834 xterm_update();
7835}
7836
7837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 * Catch up with any queued X events. This may put keyboard input into the
7839 * input buffer, call resize call-backs, trigger timers etc. If there is
7840 * nothing in the X event queue (& no timers pending), then we return
7841 * immediately.
7842 */
7843 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007844xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
7846 XEvent event;
7847
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007848 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007850 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007852 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007853 break;
7854
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007855 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007856 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007857 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007858 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007859#ifdef FEAT_CLIENTSERVER
7860 {
7861 XPropertyEvent *e = (XPropertyEvent *)&event;
7862
7863 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007865 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007868 XtDispatchEvent(&event);
7869 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007870 else
7871 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007872 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007873 XtAppProcessEvent(app_context, mask);
7874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 }
7876}
7877
7878 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007879clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880{
7881 if (xterm_Shell != (Widget)0)
7882 return clip_x11_own_selection(xterm_Shell, cbd);
7883 return FAIL;
7884}
7885
7886 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007887clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888{
7889 if (xterm_Shell != (Widget)0)
7890 clip_x11_lose_selection(xterm_Shell, cbd);
7891}
7892
7893 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007894clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
7896 if (xterm_Shell != (Widget)0)
7897 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7898}
7899
7900 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007901clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
7903 clip_x11_set_selection(cbd);
7904}
7905#endif
7906
7907
7908#if defined(USE_XSMP) || defined(PROTO)
7909/*
7910 * Code for X Session Management Protocol.
7911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912
7913# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914/*
7915 * This is our chance to ask the user if they want to save,
7916 * or abort the logout
7917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007919xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920{
Bram Moolenaare1004402020-10-24 20:49:43 +02007921 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 int cancel_shutdown = False;
7923
Bram Moolenaare1004402020-10-24 20:49:43 +02007924 save_cmod_flags = cmdmod.cmod_flags;
7925 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007926 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007927 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02007929 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007930 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 out_flush();
7932
Bram Moolenaar0f873732019-12-05 20:28:46 +01007933 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 SmcInteractDone(smc_conn, cancel_shutdown);
7935
Bram Moolenaar0f873732019-12-05 20:28:46 +01007936 // Finish off
7937 // Only end save-yourself here if we're not cancelling shutdown;
7938 // we'll get a cancelled callback later in which we'll end it.
7939 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 if (!cancel_shutdown)
7941 {
7942 xsmp.save_yourself = False;
7943 SmcSaveYourselfDone(smc_conn, True);
7944 }
7945}
7946# endif
7947
7948/*
7949 * Callback that starts save-yourself.
7950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007952xsmp_handle_save_yourself(
7953 SmcConn smc_conn,
7954 SmPointer client_data UNUSED,
7955 int save_type UNUSED,
7956 Bool shutdown,
7957 int interact_style UNUSED,
7958 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007960 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 if (xsmp.save_yourself)
7962 SmcSaveYourselfDone(smc_conn, True);
7963 xsmp.save_yourself = True;
7964 xsmp.shutdown = shutdown;
7965
Bram Moolenaar0f873732019-12-05 20:28:46 +01007966 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007968 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969
7970 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007971 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972
7973# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007974 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007976 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 SmcInteractRequest(smc_conn, SmDialogError,
7978 xsmp_handle_interaction, client_data);
7979 else
7980# endif
7981 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007982 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 SmcSaveYourselfDone(smc_conn, True);
7984 xsmp.save_yourself = False;
7985 }
7986}
7987
7988
7989/*
7990 * Callback to warn us of imminent death.
7991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007993xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994{
7995 xsmp_close();
7996
Bram Moolenaar0f873732019-12-05 20:28:46 +01007997 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 getout_preserve_modified(0);
7999}
8000
8001
8002/*
8003 * Callback to tell us that save-yourself has completed.
8004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008006xsmp_save_complete(
8007 SmcConn smc_conn UNUSED,
8008 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009{
8010 xsmp.save_yourself = False;
8011}
8012
8013
8014/*
8015 * Callback to tell us that an instigated shutdown was cancelled
8016 * (maybe even by us)
8017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008019xsmp_shutdown_cancelled(
8020 SmcConn smc_conn,
8021 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022{
8023 if (xsmp.save_yourself)
8024 SmcSaveYourselfDone(smc_conn, True);
8025 xsmp.save_yourself = False;
8026 xsmp.shutdown = False;
8027}
8028
8029
8030/*
8031 * Callback to tell us that a new ICE connection has been established.
8032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008034xsmp_ice_connection(
8035 IceConn iceConn,
8036 IcePointer clientData UNUSED,
8037 Bool opening,
8038 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008040 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 if (opening)
8042 {
8043 xsmp_icefd = IceConnectionNumber(iceConn);
8044 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8045 }
8046}
8047
8048
Bram Moolenaar0f873732019-12-05 20:28:46 +01008049// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008051xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052{
8053 Bool rep;
8054
8055 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8056 == IceProcessMessagesIOError)
8057 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008058 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008060 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 xsmp_close();
8062 return FAIL;
8063 }
8064 else
8065 return OK;
8066}
8067
8068static int dummy;
8069
Bram Moolenaar0f873732019-12-05 20:28:46 +01008070// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 void
8072xsmp_init(void)
8073{
8074 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 SmcCallbacks smcallbacks;
8076#if 0
8077 SmPropValue smname;
8078 SmProp smnameprop;
8079 SmProp *smprops[1];
8080#endif
8081
8082 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008083 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084
8085 xsmp.save_yourself = xsmp.shutdown = False;
8086
Bram Moolenaar0f873732019-12-05 20:28:46 +01008087 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8089 smcallbacks.save_yourself.client_data = NULL;
8090 smcallbacks.die.callback = xsmp_die;
8091 smcallbacks.die.client_data = NULL;
8092 smcallbacks.save_complete.callback = xsmp_save_complete;
8093 smcallbacks.save_complete.client_data = NULL;
8094 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8095 smcallbacks.shutdown_cancelled.client_data = NULL;
8096
Bram Moolenaar0f873732019-12-05 20:28:46 +01008097 // Set up a watch on ICE connection creations. The "dummy" argument is
8098 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8100 {
8101 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008102 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 return;
8104 }
8105
Bram Moolenaar0f873732019-12-05 20:28:46 +01008106 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 xsmp.smcconn = SmcOpenConnection(
8108 NULL,
8109 NULL,
8110 SmProtoMajor,
8111 SmProtoMinor,
8112 SmcSaveYourselfProcMask | SmcDieProcMask
8113 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8114 &smcallbacks,
8115 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008116 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008117 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 errorstring);
8119 if (xsmp.smcconn == NULL)
8120 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008122 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008123 char errorreport[132];
8124
8125 // If the message is too long it might not be NUL terminated. Add
8126 // a NUL at the end to make sure we don't go over the end.
8127 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008128 vim_snprintf(errorreport, sizeof(errorreport),
8129 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008130 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 return;
8133 }
8134 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8135
8136#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008137 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 smname.value = "vim";
8139 smname.length = 3;
8140 smnameprop.name = "SmProgram";
8141 smnameprop.type = "SmARRAY8";
8142 smnameprop.num_vals = 1;
8143 smnameprop.vals = &smname;
8144
8145 smprops[0] = &smnameprop;
8146 SmcSetProperties(xsmp.smcconn, 1, smprops);
8147#endif
8148}
8149
8150
Bram Moolenaar0f873732019-12-05 20:28:46 +01008151// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008153xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154{
8155 if (xsmp_icefd != -1)
8156 {
8157 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008158 if (xsmp.clientid != NULL)
8159 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008160 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 xsmp_icefd = -1;
8162 }
8163}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008164#endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165
8166
8167#ifdef EBCDIC
Bram Moolenaar0f873732019-12-05 20:28:46 +01008168// Translate character to its CTRL- value
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169char CtrlTable[] =
8170{
8171/* 00 - 5E */
8172 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8173 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8174 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8175 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8177 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8178/* ^ */ 0x1E,
8179/* - */ 0x1F,
8180/* 61 - 6C */
8181 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8182/* _ */ 0x1F,
8183/* 6E - 80 */
8184 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8185/* a */ 0x01,
8186/* b */ 0x02,
8187/* c */ 0x03,
8188/* d */ 0x37,
8189/* e */ 0x2D,
8190/* f */ 0x2E,
8191/* g */ 0x2F,
8192/* h */ 0x16,
8193/* i */ 0x05,
8194/* 8A - 90 */
8195 0, 0, 0, 0, 0, 0, 0,
8196/* j */ 0x15,
8197/* k */ 0x0B,
8198/* l */ 0x0C,
8199/* m */ 0x0D,
8200/* n */ 0x0E,
8201/* o */ 0x0F,
8202/* p */ 0x10,
8203/* q */ 0x11,
8204/* r */ 0x12,
8205/* 9A - A1 */
8206 0, 0, 0, 0, 0, 0, 0, 0,
8207/* s */ 0x13,
8208/* t */ 0x3C,
8209/* u */ 0x3D,
8210/* v */ 0x32,
8211/* w */ 0x26,
8212/* x */ 0x18,
8213/* y */ 0x19,
8214/* z */ 0x3F,
8215/* AA - AC */
8216 0, 0, 0,
8217/* [ */ 0x27,
8218/* AE - BC */
8219 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8220/* ] */ 0x1D,
8221/* BE - C0 */ 0, 0, 0,
8222/* A */ 0x01,
8223/* B */ 0x02,
8224/* C */ 0x03,
8225/* D */ 0x37,
8226/* E */ 0x2D,
8227/* F */ 0x2E,
8228/* G */ 0x2F,
8229/* H */ 0x16,
8230/* I */ 0x05,
8231/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8232/* J */ 0x15,
8233/* K */ 0x0B,
8234/* L */ 0x0C,
8235/* M */ 0x0D,
8236/* N */ 0x0E,
8237/* O */ 0x0F,
8238/* P */ 0x10,
8239/* Q */ 0x11,
8240/* R */ 0x12,
8241/* DA - DF */ 0, 0, 0, 0, 0, 0,
8242/* \ */ 0x1C,
8243/* E1 */ 0,
8244/* S */ 0x13,
8245/* T */ 0x3C,
8246/* U */ 0x3D,
8247/* V */ 0x32,
8248/* W */ 0x26,
8249/* X */ 0x18,
8250/* Y */ 0x19,
8251/* Z */ 0x3F,
8252/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8253 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8254};
8255
8256char MetaCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008257{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8259 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8260 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8261 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8262};
8263
8264
Bram Moolenaar0f873732019-12-05 20:28:46 +01008265// TODO: Use characters NOT numbers!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266char CtrlCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008267{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8269 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8270 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8271 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8272};
8273
8274
8275#endif