blob: f93bc958627f358e9b6132d4a6ca2b07a02b92f7 [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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121Window x11_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#endif
124
Bram Moolenaar5c3128e2020-05-11 20:54:42 +0200125static int ignore_sigtstp = FALSE;
126
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200130static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +0200131static int unix_did_set_title = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132static char_u *oldicon = NULL;
133static int did_set_icon = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaar205b8862011-09-07 15:04:31 +0200137#ifdef HAVE_UNION_WAIT
138typedef union wait waitstatus;
139#else
140typedef int waitstatus;
141#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200142static int WaitForChar(long msec, int *interrupted, int ignore_input);
143static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar041c7102020-05-30 18:14:57 +0200144#ifdef VMS
Bram Moolenaarcda77642016-06-04 13:32:35 +0200145int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200147static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
149
150#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static int do_xterm_trace(void);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100152# define XT_TRACE_DELAY 50 // delay for xterm tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#endif
154
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156
157#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100158static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
160#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100161static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200163#if defined(SIGUSR1)
164static RETSIGTYPE catch_sigusr1 SIGPROTOARG;
165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100167static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000169#if defined(SIGALRM) && defined(FEAT_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100171static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100172// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200173static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100175static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void catch_int_signal(void);
178static void set_signals(void);
179static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200180#ifdef HAVE_SIGPROCMASK
181# define SIGSET_DECL(set) sigset_t set;
182# define BLOCK_SIGNALS(set) block_signals(set)
183# define UNBLOCK_SIGNALS(set) unblock_signals(set)
184#else
185# define SIGSET_DECL(set)
186# define BLOCK_SIGNALS(set) do { /**/ } while (0)
187# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
188#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100189static int have_wildcard(int, char_u **);
190static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100192static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000195# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196#endif
197
Bram Moolenaar0f873732019-12-05 20:28:46 +0100198// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200199static volatile sig_atomic_t do_resize = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200static char_u *extra_shell_arg = NULL;
201static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100202// volatile because it is used in signal handler deathtrap().
203static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
204// volatile because it is used in signal handler deathtrap().
205static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100207#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
208static int dont_check_job_ended = 0;
209#endif
210
Bram Moolenaar26e86442020-05-17 14:06:16 +0200211// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
212static tmode_T mch_cur_tmode = TMODE_COOK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
214#ifdef USE_XSMP
215typedef struct
216{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100217 SmcConn smcconn; // The SM connection ID
218 IceConn iceconn; // The ICE connection ID
219 char *clientid; // The client ID for the current smc session
220 Bool save_yourself; // If we're in the middle of a save_yourself
221 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222} xsmp_config_T;
223
224static xsmp_config_T xsmp;
225#endif
226
227#ifdef SYS_SIGLIST_DECLARED
228/*
229 * I have seen
230 * extern char *_sys_siglist[NSIG];
231 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
232 * that describe the signals. That is nearly what we want here. But
233 * autoconf does only check for sys_siglist (without the underscore), I
234 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100235 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 */
237#endif
238
239static struct signalinfo
240{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100241 int sig; // Signal number, eg. SIGSEGV etc
242 char *name; // Signal name (not char_u!).
243 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244} signal_info[] =
245{
246#ifdef SIGHUP
247 {SIGHUP, "HUP", TRUE},
248#endif
249#ifdef SIGQUIT
250 {SIGQUIT, "QUIT", TRUE},
251#endif
252#ifdef SIGILL
253 {SIGILL, "ILL", TRUE},
254#endif
255#ifdef SIGTRAP
256 {SIGTRAP, "TRAP", TRUE},
257#endif
258#ifdef SIGABRT
259 {SIGABRT, "ABRT", TRUE},
260#endif
261#ifdef SIGEMT
262 {SIGEMT, "EMT", TRUE},
263#endif
264#ifdef SIGFPE
265 {SIGFPE, "FPE", TRUE},
266#endif
267#ifdef SIGBUS
268 {SIGBUS, "BUS", TRUE},
269#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100270#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100271 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 {SIGSEGV, "SEGV", TRUE},
273#endif
274#ifdef SIGSYS
275 {SIGSYS, "SYS", TRUE},
276#endif
277#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100278 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279#endif
280#ifdef SIGTERM
281 {SIGTERM, "TERM", TRUE},
282#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100283#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 {SIGVTALRM, "VTALRM", TRUE},
285#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000286#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100287 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
288 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 {SIGPROF, "PROF", TRUE},
290#endif
291#ifdef SIGXCPU
292 {SIGXCPU, "XCPU", TRUE},
293#endif
294#ifdef SIGXFSZ
295 {SIGXFSZ, "XFSZ", TRUE},
296#endif
297#ifdef SIGUSR1
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200298 {SIGUSR1, "USR1", FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000300#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100301 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 {SIGUSR2, "USR2", TRUE},
303#endif
304#ifdef SIGINT
305 {SIGINT, "INT", FALSE},
306#endif
307#ifdef SIGWINCH
308 {SIGWINCH, "WINCH", FALSE},
309#endif
310#ifdef SIGTSTP
311 {SIGTSTP, "TSTP", FALSE},
312#endif
313#ifdef SIGPIPE
314 {SIGPIPE, "PIPE", FALSE},
315#endif
316 {-1, "Unknown!", FALSE}
317};
318
Bram Moolenaar25724922009-07-14 15:38:41 +0000319 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100320mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000321{
322 if (p_verbose >= 5)
323 {
324 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100325 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000326 verbose_leave();
327 }
328# ifdef VMS
329 return chdir(vms_fixfilename(path));
330# else
331 return chdir(path);
332# endif
333}
334
Bram Moolenaar0f873732019-12-05 20:28:46 +0100335// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100336#if defined(ECHOE) && defined(ICANON) \
337 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
338 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200339# define NEW_TTY_SYSTEM
340#endif
341
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000342/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100343 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100346mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200348 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100349 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100350 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351}
352
353/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100354 * Function passed to inchar_loop() to handle window resizing.
355 * If "check_only" is TRUE: Return whether there was a resize.
356 * If "check_only" is FALSE: Deal with the window resized.
357 */
358 static int
359resize_func(int check_only)
360{
361 if (check_only)
362 return do_resize;
363 while (do_resize)
364 handle_resize();
365 return FALSE;
366}
367
368/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000369 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 * Get a characters from the keyboard.
371 * Return the number of characters that are available.
372 * If wtime == 0 do not wait for characters.
373 * If wtime == n wait a short time for characters.
374 * If wtime == -1 wait forever for characters.
375 */
376 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100377mch_inchar(
378 char_u *buf,
379 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100380 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100381 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100383 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
384 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385}
386
387 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100388handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
390 do_resize = FALSE;
391 shell_resized();
392}
393
394/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200395 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 */
397 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100398mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200400 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401}
402
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200403#if defined(FEAT_TERMINAL) || defined(PROTO)
404/*
405 * Check for any pending input or messages.
406 */
407 int
408mch_check_messages(void)
409{
410 return WaitForChar(0L, NULL, TRUE);
411}
412#endif
413
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
415# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000416# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417# endif
418# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
419# include <sys/sysctl.h>
420# endif
421# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
422# include <sys/sysinfo.h>
423# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100424# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100425# include <mach/mach_host.h>
426# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100427# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428
429/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000430 * Return total amount of memory available in Kbyte.
431 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100434mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100437 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438
Bram Moolenaar362dc332018-03-05 21:59:37 +0100439# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100440 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100441 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100442 mach_port_t host = mach_host_self();
443 kern_return_t kret;
444# ifdef HOST_VM_INFO64
445 struct vm_statistics64 vm_stat;
446 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447
Bram Moolenaar988615f2018-02-27 17:58:20 +0100448 kret = host_statistics64(host, HOST_VM_INFO64,
449 (host_info64_t)&vm_stat, &count);
450# else
451 struct vm_statistics vm_stat;
452 natural_t count = HOST_VM_INFO_COUNT;
453
454 kret = host_statistics(host, HOST_VM_INFO,
455 (host_info_t)&vm_stat, &count);
456# endif
457 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100458 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100459 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
460 + vm_stat.inactive_count
461# ifdef MAC_OS_X_VERSION_10_9
462 + vm_stat.compressor_page_count
463# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100464 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100465 mach_port_deallocate(mach_task_self(), host);
466 }
467# endif
468
469# ifdef HAVE_SYSCTL
470 if (mem == 0)
471 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100472 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100473 int mib[2];
474 size_t len = sizeof(long_u);
475# ifdef HW_USERMEM64
476 long_u physmem;
477# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100478 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100479 union {
480 int_u u32;
481 long_u u64;
482 } physmem;
483# endif
484
485 mib[0] = CTL_HW;
486# ifdef HW_USERMEM64
487 mib[1] = HW_USERMEM64;
488# else
489 mib[1] = HW_USERMEM;
490# endif
491 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
492 {
493# ifdef HW_USERMEM64
494 mem = (long_u)physmem;
495# else
496 if (len == sizeof(physmem.u64))
497 mem = (long_u)physmem.u64;
498 else
499 mem = (long_u)physmem.u32;
500# endif
501 }
502 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200503# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200505# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 if (mem == 0)
507 {
508 struct sysinfo sinfo;
509
Bram Moolenaar0f873732019-12-05 20:28:46 +0100510 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000512 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200513# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100514 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000515 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
516 {
517 sinfo.mem_unit = sinfo.mem_unit >> 1;
518 --shiftright;
519 }
520 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200521# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200523# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200528# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 if (mem == 0)
530 {
531 long pagesize, pagecount;
532
Bram Moolenaar0f873732019-12-05 20:28:46 +0100533 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 pagesize = sysconf(_SC_PAGESIZE);
535 pagecount = sysconf(_SC_PHYS_PAGES);
536 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000537 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100538 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000539 while (shiftright > 0 && (pagesize & 1) == 0)
540 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000541 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000542 --shiftright;
543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200547# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
Bram Moolenaar0f873732019-12-05 20:28:46 +0100549 // Return the minimum of the physical memory and the user limit, because
550 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200551# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 {
553 struct rlimit rlp;
554
555 if (getrlimit(RLIMIT_DATA, &rlp) == 0
556 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200557# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200559# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000560 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000562 {
563 mem = (long_u)rlp.rlim_cur;
564 shiftright = 10;
565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568
569 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000570 return mem >> shiftright;
571 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572}
573#endif
574
Bram Moolenaar0981c872020-08-23 14:28:37 +0200575/*
576 * "flags": MCH_DELAY_IGNOREINPUT - don't read input
577 * MCH_DELAY_SETTMODE - use settmode() even for short delays
578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200580mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581{
Bram Moolenaar26e86442020-05-17 14:06:16 +0200582 tmode_T old_tmode;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200583 int call_settmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000584#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100585 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
Bram Moolenaar0981c872020-08-23 14:28:37 +0200588 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100590 // Go to cooked mode without echo, to allow SIGINT interrupting us
591 // here. But we don't want QUIT to kill us (CTRL-\ used in a
592 // shell may produce SIGQUIT).
Bram Moolenaar26e86442020-05-17 14:06:16 +0200593 // Only do this if sleeping for more than half a second.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000594 in_mch_delay = TRUE;
Bram Moolenaar80361a52020-10-05 21:39:25 +0200595 call_settmode = mch_cur_tmode == TMODE_RAW
596 && (msec > 500 || (flags & MCH_DELAY_SETTMODE));
597 if (call_settmode)
598 {
599 old_tmode = mch_cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 settmode(TMODE_SLEEP);
Bram Moolenaar80361a52020-10-05 21:39:25 +0200601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602
603 /*
604 * Everybody sleeps in a different way...
605 * Prefer nanosleep(), some versions of usleep() can only sleep up to
606 * one second.
607 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000608#ifdef FEAT_MZSCHEME
609 do
610 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100611 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000612 if (total > p_mzq)
613 msec = p_mzq;
614 else
615 msec = total;
616 total -= msec;
617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#ifdef HAVE_NANOSLEEP
619 {
620 struct timespec ts;
621
622 ts.tv_sec = msec / 1000;
623 ts.tv_nsec = (msec % 1000) * 1000000;
624 (void)nanosleep(&ts, NULL);
625 }
626#else
627# ifdef HAVE_USLEEP
628 while (msec >= 1000)
629 {
630 usleep((unsigned int)(999 * 1000));
631 msec -= 999;
632 }
633 usleep((unsigned int)(msec * 1000));
634# else
635# ifndef HAVE_SELECT
636 poll(NULL, 0, (int)msec);
637# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 {
639 struct timeval tv;
640
641 tv.tv_sec = msec / 1000;
642 tv.tv_usec = (msec % 1000) * 1000;
Bram Moolenaar0981c872020-08-23 14:28:37 +0200643 // NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
644 // a patch from Sun to fix this. Reported by Gunnar Pedersen.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 select(0, NULL, NULL, NULL, &tv);
646 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100647# endif // HAVE_SELECT
648# endif // HAVE_NANOSLEEP
649#endif // HAVE_USLEEP
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000650#ifdef FEAT_MZSCHEME
651 }
652 while (total > 0);
653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaar80361a52020-10-05 21:39:25 +0200655 if (call_settmode)
Bram Moolenaar26e86442020-05-17 14:06:16 +0200656 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000657 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 }
659 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200660 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661}
662
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000663#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
665# define HAVE_CHECK_STACK_GROWTH
666/*
667 * Support for checking for an almost-out-of-stack-space situation.
668 */
669
670/*
671 * Return a pointer to an item on the stack. Used to find out if the stack
672 * grows up or down.
673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674static int stack_grows_downwards;
675
676/*
677 * Find out if the stack grows upwards or downwards.
678 * "p" points to a variable on the stack of the caller.
679 */
680 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100681check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682{
683 int i;
684
685 stack_grows_downwards = (p > (char *)&i);
686}
687#endif
688
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000689#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690static char *stack_limit = NULL;
691
692#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
693# include <pthread.h>
694# include <pthread_np.h>
695#endif
696
697/*
698 * Find out until how var the stack can grow without getting into trouble.
699 * Called when starting up and when switching to the signal stack in
700 * deathtrap().
701 */
702 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100703get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704{
705 struct rlimit rlp;
706 int i;
707 long lim;
708
Bram Moolenaar0f873732019-12-05 20:28:46 +0100709 // Set the stack limit to 15/16 of the allowable size. Skip this when the
710 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 if (getrlimit(RLIMIT_STACK, &rlp) == 0
712 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
713# ifdef RLIM_INFINITY
714 && rlp.rlim_cur != RLIM_INFINITY
715# endif
716 )
717 {
718 lim = (long)rlp.rlim_cur;
719#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
720 {
721 pthread_attr_t attr;
722 size_t size;
723
Bram Moolenaar0f873732019-12-05 20:28:46 +0100724 // On FreeBSD the initial thread always has a fixed stack size, no
725 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 pthread_attr_init(&attr);
727 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
728 {
729 pthread_attr_getstacksize(&attr, &size);
730 if (lim > (long)size)
731 lim = (long)size;
732 }
733 pthread_attr_destroy(&attr);
734 }
735#endif
736 if (stack_grows_downwards)
737 {
738 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
739 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100740 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 stack_limit = (char *)((long)&i / 16L);
742 }
743 else
744 {
745 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
746 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100747 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 }
749 }
750}
751
752/*
753 * Return FAIL when running out of stack space.
754 * "p" must point to any variable local to the caller that's on the stack.
755 */
756 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100757mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758{
759 if (stack_limit != NULL)
760 {
761 if (stack_grows_downwards)
762 {
763 if (p < stack_limit)
764 return FAIL;
765 }
766 else if (p > stack_limit)
767 return FAIL;
768 }
769 return OK;
770}
771#endif
772
773#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
774/*
775 * Support for using the signal stack.
776 * This helps when we run out of stack space, which causes a SIGSEGV. The
777 * signal handler then must run on another stack, since the normal stack is
778 * completely full.
779 */
780
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100782static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100784static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785# endif
786
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200787/*
788 * Get a size of signal stack.
789 * Preference (if available): sysconf > SIGSTKSZ > guessed size
790 */
791static long int get_signal_stack_size()
792{
793# ifdef HAVE_SYSCONF_SIGSTKSZ
794 long int size = -1;
795
796 // return size only if sysconf doesn't return an error
797 if ((size = sysconf(_SC_SIGSTKSZ)) > -1)
798 return size;
799# endif
800
801# ifdef SIGSTKSZ
802 // if sysconf() isn't available or gives error, return SIGSTKSZ
803 // if defined
804 return SIGSTKSZ;
805# endif
806
807 // otherwise guess the size
808 return 8000;
809}
810
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811static char *signal_stack;
812
813 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100814init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815{
816 if (signal_stack != NULL)
817 {
818# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819# ifdef HAVE_SS_BASE
820 sigstk.ss_base = signal_stack;
821# else
822 sigstk.ss_sp = signal_stack;
823# endif
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200824 sigstk.ss_size = get_signal_stack_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 sigstk.ss_flags = 0;
826 (void)sigaltstack(&sigstk, NULL);
827# else
828 sigstk.ss_sp = signal_stack;
829 if (stack_grows_downwards)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +0200830 sigstk.ss_sp += get_signal_stack_size() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 sigstk.ss_onstack = 0;
832 (void)sigstack(&sigstk, NULL);
833# endif
834 }
835}
836#endif
837
838/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000839 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 * will barf when the second argument to signal() is ``wrong''.
841 * Let me try it with a few tricky defines from my own osdef.h (jw).
842 */
843#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 static RETSIGTYPE
845sig_winch SIGDEFARG(sigarg)
846{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100847 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
849 do_resize = TRUE;
850 SIGRETURN;
851}
852#endif
853
854#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 static RETSIGTYPE
856catch_sigint SIGDEFARG(sigarg)
857{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100858 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
860 got_int = TRUE;
861 SIGRETURN;
862}
863#endif
864
Bram Moolenaarbe5ee862020-06-10 20:56:58 +0200865#if defined(SIGUSR1)
866 static RETSIGTYPE
867catch_sigusr1 SIGDEFARG(sigarg)
868{
869 // this is not required on all systems, but it doesn't hurt anybody
870 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
871 got_sigusr1 = TRUE;
872 SIGRETURN;
873}
874#endif
875
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 static RETSIGTYPE
878catch_sigpwr SIGDEFARG(sigarg)
879{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100880 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000881 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 /*
883 * I'm not sure we get the SIGPWR signal when the system is really going
884 * down or when the batteries are almost empty. Just preserve the swap
885 * files and don't exit, that can't do any harm.
886 */
887 ml_sync_all(FALSE, FALSE);
888 SIGRETURN;
889}
890#endif
891
892#ifdef SET_SIG_ALARM
893/*
894 * signal function for alarm().
895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 static RETSIGTYPE
897sig_alarm SIGDEFARG(sigarg)
898{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100899 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 sig_alarm_called = TRUE;
901 SIGRETURN;
902}
903#endif
904
Bram Moolenaaredce7422019-01-20 18:39:30 +0100905#if (defined(HAVE_SETJMP_H) \
906 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
907 || defined(FEAT_LIBCALL))) \
908 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100909# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100910
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100911// argument to SETJMP()
912static JMP_BUF lc_jump_env;
913
914# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100915// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100916// Volatile because it is used in signal handlers.
917static volatile sig_atomic_t lc_signal;
918# endif
919
920// TRUE when lc_jump_env is valid.
921// Volatile because it is used in signal handler deathtrap().
922static volatile sig_atomic_t lc_active INIT(= FALSE);
923
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924/*
925 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100926 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100928 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 * Usage:
930 * mch_startjmp();
931 * if (SETJMP(lc_jump_env) != 0)
932 * {
933 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100934 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 * }
936 * else
937 * {
938 * do_the_work;
939 * mch_endjmp();
940 * }
941 * Note: Can't move SETJMP() here, because a function calling setjmp() must
942 * not return before the saved environment is used.
943 * Returns OK for normal return, FAIL when the protected code caused a
944 * problem and LONGJMP() was used.
945 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100946 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100947mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948{
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100949# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 lc_active = TRUE;
953}
954
Bram Moolenaar113e1072019-01-20 15:30:40 +0100955 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100956mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957{
958 lc_active = FALSE;
959}
960
Bram Moolenaar113e1072019-01-20 15:30:40 +0100961 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100962mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963{
964# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100965 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
966 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 init_signal_stack();
968# endif
969}
970#endif
971
972/*
973 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200974 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200976 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
977 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 */
979 static RETSIGTYPE
980deathtrap SIGDEFARG(sigarg)
981{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100982 static int entered = 0; // count the number of times we got here.
983 // Note: when memory has been corrupted
984 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985#ifdef SIGHASARG
986 int i;
987#endif
988
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100989#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 /*
991 * Catch a crash in protected code.
992 * Restores the environment saved in lc_jump_env, which looks like
993 * SETJMP() returns 1.
994 */
995 if (lc_active)
996 {
997# if defined(SIGHASARG)
998 lc_signal = sigarg;
999# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001000 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001002 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 }
1004#endif
1005
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001006#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001007# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001008 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
1009 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1010 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001011 if (in_mch_delay && sigarg == SIGQUIT)
1012 SIGRETURN;
1013# endif
1014
Bram Moolenaar0f873732019-12-05 20:28:46 +01001015 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1016 // here. This avoids that a non-reentrant function is interrupted, e.g.,
1017 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001018 if (entered == 0
1019 && (0
1020# ifdef SIGHUP
1021 || sigarg == SIGHUP
1022# endif
1023# ifdef SIGQUIT
1024 || sigarg == SIGQUIT
1025# endif
1026# ifdef SIGTERM
1027 || sigarg == SIGTERM
1028# endif
1029# ifdef SIGPWR
1030 || sigarg == SIGPWR
1031# endif
1032# ifdef SIGUSR1
1033 || sigarg == SIGUSR1
1034# endif
1035# ifdef SIGUSR2
1036 || sigarg == SIGUSR2
1037# endif
1038 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001039 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001040 SIGRETURN;
1041#endif
1042
Bram Moolenaar0f873732019-12-05 20:28:46 +01001043 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 ++entered;
1045
Bram Moolenaar0f873732019-12-05 20:28:46 +01001046 // Executing autocommands is likely to use more stack space than we have
1047 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001048 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001051 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 set_vim_var_nr(VV_DYING, (long)entered);
1053#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001054 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001056#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001057 // Since we are now using the signal stack, need to reset the stack
1058 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 get_stack_limit();
1060#endif
1061
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001062#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001063 // This is for opening gdb the moment Vim crashes.
1064 // You need to manually adjust the file name and Vim executable name.
1065 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001066 {
1067# define VI_GDB_FILE "/tmp/vimgdb"
1068# define VIM_NAME "/usr/bin/vim"
1069 FILE *fp = fopen(VI_GDB_FILE, "w");
1070 if (fp)
1071 {
1072 fprintf(fp,
1073 "file %s\n"
1074 "attach %d\n"
1075 "set height 1000\n"
1076 "bt full\n"
1077 , VIM_NAME, getpid());
1078 fclose(fp);
1079 system("xterm -e gdb -x "VI_GDB_FILE);
1080 unlink(VI_GDB_FILE);
1081 }
1082 }
1083#endif
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001086 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 for (i = 0; signal_info[i].sig != -1; i++)
1088 if (sigarg == signal_info[i].sig)
1089 break;
1090 deadly_signal = sigarg;
1091#endif
1092
Bram Moolenaar0f873732019-12-05 20:28:46 +01001093 full_screen = FALSE; // don't write message to the GUI, it might be
1094 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 /*
1096 * If something goes wrong after entering here, we may get here again.
1097 * When this happens, give a message and try to exit nicely (resetting the
1098 * terminal mode, etc.)
1099 * When this happens twice, just exit, don't even try to give a message,
1100 * stack may be corrupt or something weird.
1101 * When this still happens again (or memory was corrupted in such a way
1102 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1103 */
1104 if (entered >= 3)
1105 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001106 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 may_core_dump();
1108 if (entered >= 4)
1109 _exit(8);
1110 exit(7);
1111 }
1112 if (entered == 2)
1113 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001114 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001115 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 out_flush();
1117 getout(1);
1118 }
1119
Bram Moolenaar0f873732019-12-05 20:28:46 +01001120 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001122 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 signal_info[i].name);
1124#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001125 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001127
Bram Moolenaar0f873732019-12-05 20:28:46 +01001128 // Preserve files and exit. This sets the really_exiting flag to prevent
1129 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001130 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131
Bram Moolenaar0f873732019-12-05 20:28:46 +01001132 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001133
Bram Moolenaar009b2592004-10-24 19:18:58 +00001134#ifdef NBDEBUG
1135 reset_signals();
1136 may_core_dump();
1137 abort();
1138#endif
1139
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 SIGRETURN;
1141}
1142
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001144 * Invoked after receiving SIGCONT. We don't know what happened while
1145 * sleeping, deal with part of that.
1146 */
1147 static void
1148after_sigcont(void)
1149{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001150 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1151 // again later.
1152 oldtitle_outdated = TRUE;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001153
Bram Moolenaar2e310482018-08-21 13:09:10 +02001154 settmode(TMODE_RAW);
1155 need_check_timestamps = TRUE;
1156 did_check_timestamps = FALSE;
1157}
1158
1159#if defined(SIGCONT)
1160static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001161static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001162
1163/*
1164 * With multi-threading, suspending might not work immediately. Catch the
1165 * SIGCONT signal, which will be used as an indication whether the suspending
1166 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001167 *
1168 * On Linux, signal is not always handled immediately either.
1169 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001170 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001171 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001172 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001174static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001175static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
1177/*
1178 * signal handler for SIGCONT
1179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 static RETSIGTYPE
1181sigcont_handler SIGDEFARG(sigarg)
1182{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001183 if (in_mch_suspend)
1184 {
1185 sigcont_received = TRUE;
1186 }
1187 else
1188 {
1189 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1190 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1191 // back to a sane mode. We should redraw, but we can't really do that
1192 // in a signal handler, do a redraw later.
1193 after_sigcont();
1194 redraw_later(CLEAR);
1195 cursor_on_force();
1196 out_flush();
1197 }
1198
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 SIGRETURN;
1200}
1201#endif
1202
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001203#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001204# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001205static void *clip_star_save = NULL;
1206static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001207# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001208
1209/*
1210 * Called when Vim is going to sleep or execute a shell command.
1211 * We can't respond to requests for the X selections. Lose them, otherwise
1212 * other applications will hang. But first copy the text to cut buffer 0.
1213 */
1214 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001215loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001216{
1217 if (clip_star.owned || clip_plus.owned)
1218 {
1219 x11_export_final_selection();
1220 if (clip_star.owned)
1221 clip_lose_selection(&clip_star);
1222 if (clip_plus.owned)
1223 clip_lose_selection(&clip_plus);
1224 if (x11_display != NULL)
1225 XFlush(x11_display);
1226 }
1227}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001228
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001229# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001230/*
1231 * Save clipboard text to restore later.
1232 */
1233 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001234save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001235{
1236 if (clip_star.owned)
1237 clip_star_save = get_register('*', TRUE);
1238 if (clip_plus.owned)
1239 clip_plus_save = get_register('+', TRUE);
1240}
1241
1242/*
1243 * Restore clipboard text if no one own the X selection.
1244 */
1245 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001246restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001247{
1248 if (clip_star_save != NULL)
1249 {
1250 if (!clip_gen_owner_exists(&clip_star))
1251 put_register('*', clip_star_save);
1252 else
1253 free_register(clip_star_save);
1254 clip_star_save = NULL;
1255 }
1256 if (clip_plus_save != NULL)
1257 {
1258 if (!clip_gen_owner_exists(&clip_plus))
1259 put_register('+', clip_plus_save);
1260 else
1261 free_register(clip_plus_save);
1262 clip_plus_save = NULL;
1263 }
1264}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001265# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001266#endif
1267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268/*
1269 * If the machine has job control, use it to suspend the program,
1270 * otherwise fake it by starting a new shell.
1271 */
1272 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001273mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001275 if (ignore_sigtstp)
1276 return;
1277
Bram Moolenaar041c7102020-05-30 18:14:57 +02001278#if defined(SIGTSTP)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001279 in_mch_suspend = TRUE;
1280
Bram Moolenaar0f873732019-12-05 20:28:46 +01001281 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001283 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284
1285# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001286 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001288# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 sigcont_received = FALSE;
1290# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001291
Bram Moolenaar0f873732019-12-05 20:28:46 +01001292 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001293
1294# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001295 /*
1296 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001297 * immediately, but somehow not all the time, probably because it's handled
1298 * in another thread. Do not call pause() because there would be race
1299 * condition which would hang Vim if signal happened in between the test of
1300 * sigcont_received and the call to pause(). If signal is not yet received,
1301 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1302 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001303 */
1304 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001305 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001306
Bram Moolenaar262735e2009-07-14 10:20:22 +00001307 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar0981c872020-08-23 14:28:37 +02001308 mch_delay(wait_time, 0);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001311 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312
Bram Moolenaar2e310482018-08-21 13:09:10 +02001313 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314#else
1315 suspend_shell();
1316#endif
1317}
1318
1319 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001320mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321{
1322 Columns = 80;
1323 Rows = 24;
1324
1325 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001326
1327#ifdef SIGTSTP
1328 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1329 // that indicates the shell (or program) that launched us does not support
1330 // tty job control and thus we should ignore that signal. If invoked as a
1331 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
1332 ignore_sigtstp = restricted || SIG_IGN == signal(SIGTSTP, SIG_ERR);
1333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001335
Bram Moolenaar56718732006-03-15 22:53:57 +00001336#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001337 mac_conv_init();
1338#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001339#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1340 win_clip_init();
1341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342}
1343
1344 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001345set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346{
1347#if defined(SIGWINCH)
1348 /*
1349 * WINDOW CHANGE signal is handled with sig_winch().
1350 */
1351 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1352#endif
1353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001355 // See mch_init() for the conditions under which we ignore SIGTSTP.
1356 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001358#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 signal(SIGCONT, sigcont_handler);
1360#endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001361#ifdef SIGPIPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 /*
1363 * We want to ignore breaking of PIPEs.
1364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 signal(SIGPIPE, SIG_IGN);
1366#endif
1367
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001369 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370#endif
1371
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001372#ifdef SIGUSR1
1373 /*
1374 * Call user's handler on SIGUSR1
1375 */
1376 signal(SIGUSR1, (RETSIGTYPE (*)())catch_sigusr1);
1377#endif
1378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 /*
1380 * Ignore alarm signals (Perl's alarm() generates it).
1381 */
1382#ifdef SIGALRM
1383 signal(SIGALRM, SIG_IGN);
1384#endif
1385
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02001386#ifdef SIGPWR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 /*
1388 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1389 * work will be lost.
1390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1392#endif
1393
1394 /*
1395 * Arrange for other signals to gracefully shutdown Vim.
1396 */
1397 catch_signals(deathtrap, SIG_ERR);
1398
1399#if defined(FEAT_GUI) && defined(SIGHUP)
1400 /*
1401 * When the GUI is running, ignore the hangup signal.
1402 */
1403 if (gui.in_use)
1404 signal(SIGHUP, SIG_IGN);
1405#endif
1406}
1407
Bram Moolenaardf177f62005-02-22 08:39:57 +00001408#if defined(SIGINT) || defined(PROTO)
1409/*
1410 * Catch CTRL-C (only works while in Cooked mode).
1411 */
1412 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001413catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001414{
1415 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1416}
1417#endif
1418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001420reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
1422 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001423#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001424 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 signal(SIGCONT, SIG_DFL);
1426#endif
1427}
1428
1429 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001430catch_signals(
1431 RETSIGTYPE (*func_deadly)(),
1432 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
1434 int i;
1435
1436 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 if (signal_info[i].deadly)
1439 {
1440#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1441 struct sigaction sa;
1442
Bram Moolenaar0f873732019-12-05 20:28:46 +01001443 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 sa.sa_handler = func_deadly;
1445 sigemptyset(&sa.sa_mask);
1446# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001447 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1448 // thread handling in combination with using the alternate stack:
1449 // pthread library functions try to use the stack pointer to
1450 // identify the current thread, causing a SEGV signal, which
1451 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 sa.sa_flags = 0;
1453# else
1454 sa.sa_flags = SA_ONSTACK;
1455# endif
1456 sigaction(signal_info[i].sig, &sa, NULL);
1457#else
1458# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1459 struct sigvec sv;
1460
Bram Moolenaar0f873732019-12-05 20:28:46 +01001461 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 sv.sv_handler = func_deadly;
1463 sv.sv_mask = 0;
1464 sv.sv_flags = SV_ONSTACK;
1465 sigvec(signal_info[i].sig, &sv, NULL);
1466# else
1467 signal(signal_info[i].sig, func_deadly);
1468# endif
1469#endif
1470 }
1471 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001472 {
1473 // Deal with non-deadly signals.
1474#ifdef SIGTSTP
1475 signal(signal_info[i].sig,
1476 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1477 ? SIG_IGN : func_other);
1478#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001480#endif
1481 }
1482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483}
1484
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001485#ifdef HAVE_SIGPROCMASK
1486 static void
1487block_signals(sigset_t *set)
1488{
1489 sigset_t newset;
1490 int i;
1491
1492 sigemptyset(&newset);
1493
1494 for (i = 0; signal_info[i].sig != -1; i++)
1495 sigaddset(&newset, signal_info[i].sig);
1496
Bram Moolenaar2e310482018-08-21 13:09:10 +02001497# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001498 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001499 sigaddset(&newset, SIGCONT);
1500# endif
1501
1502 sigprocmask(SIG_BLOCK, &newset, set);
1503}
1504
1505 static void
1506unblock_signals(sigset_t *set)
1507{
1508 sigprocmask(SIG_SETMASK, set, NULL);
1509}
1510#endif
1511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001513 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001514 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1515 * return TRUE
1516 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1517 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001518 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001519 * Returns TRUE when Vim should exit.
1520 */
1521 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001522vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001523{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001524 static int got_signal = 0;
1525 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001526
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001527 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001528 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001529 case SIGNAL_BLOCK: blocked = TRUE;
1530 break;
1531
1532 case SIGNAL_UNBLOCK: blocked = FALSE;
1533 if (got_signal != 0)
1534 {
1535 kill(getpid(), got_signal);
1536 got_signal = 0;
1537 }
1538 break;
1539
1540 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001541 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001542 got_signal = sig;
1543#ifdef SIGPWR
1544 if (sig != SIGPWR)
1545#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001546 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001547 break;
1548 }
1549 return FALSE;
1550}
1551
1552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 * Check_win checks whether we have an interactive stdout.
1554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001556mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 if (isatty(1))
1559 return OK;
1560 return FAIL;
1561}
1562
1563/*
1564 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1565 */
1566 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001567mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
1569 if (isatty(read_cmd_fd))
1570 return TRUE;
1571 return FALSE;
1572}
1573
1574#ifdef FEAT_X11
1575
Bram Moolenaar651fca82021-11-29 20:39:38 +00001576# if defined(ELAPSED_TIMEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578/*
1579 * Give a message about the elapsed time for opening the X window.
1580 */
1581 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001582xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001584 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585}
1586# endif
1587#endif
1588
Bram Moolenaar651fca82021-11-29 20:39:38 +00001589#if defined(FEAT_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590/*
1591 * A few functions shared by X11 title and clipboard code.
1592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594static int got_x_error = FALSE;
1595
1596/*
1597 * X Error handler, otherwise X just exits! (very rude) -- webb
1598 */
1599 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001600x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001602 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 STRCAT(IObuff, _("\nVim: Got X error\n"));
1604
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001605 // In the GUI we cannot print a message and continue, because no X calls
1606 // are allowed here (causes my system to hang). Silently continuing seems
1607 // like the best alternative. Do preserve files, in case we crash.
1608 ml_sync_all(FALSE, FALSE);
1609
1610#ifdef FEAT_GUI
1611 if (!gui.in_use)
1612#endif
1613 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
Bram Moolenaar0f873732019-12-05 20:28:46 +01001615 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616}
1617
1618/*
1619 * Another X Error handler, just used to check for errors.
1620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001622x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
1624 got_x_error = TRUE;
1625 return 0;
1626}
1627
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001628/*
1629 * Return TRUE when connection to the X server is desired.
1630 */
1631 static int
1632x_connect_to_server(void)
1633{
1634 // No point in connecting if we are exiting or dying.
1635 if (exiting || v_dying)
1636 return FALSE;
1637
1638#if defined(FEAT_CLIENTSERVER)
1639 if (x_force_connect)
1640 return TRUE;
1641#endif
1642 if (x_no_connect)
1643 return FALSE;
1644
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001645 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001646 if (clip_exclude_prog != NULL)
1647 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001648 // Just in case we get called recursively, return FALSE. This could
1649 // happen if vpeekc() is used while executing the prog and it causes a
1650 // related callback to be invoked.
1651 if (regprog_in_use(clip_exclude_prog))
1652 return FALSE;
1653
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001654 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1655 return FALSE;
1656 }
1657 return TRUE;
1658}
1659
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001661# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662/*
1663 * An X IO Error handler, used to catch error while opening the display.
1664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001666x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001668 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001670# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001671 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001672# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673}
1674# endif
1675
1676/*
1677 * An X IO Error handler, used to catch terminal errors.
1678 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001679static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001682x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683{
1684 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001685 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 x11_window = 0;
1687 x11_display = NULL;
1688 xterm_Shell = (Widget)0;
1689
Bram Moolenaar0f873732019-12-05 20:28:46 +01001690 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001692# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001693 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001696
1697/*
1698 * If the X11 connection was lost try to restore it.
1699 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001700 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001701 */
1702 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001703may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001704{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001705 // No point in restoring the connecting if we are exiting or dying.
1706 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001707 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001708 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001709
1710# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001711 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001712 if (app_context != (XtAppContext)NULL)
1713 {
1714 XtDestroyApplicationContext(app_context);
1715 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001716 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001717 }
1718# endif
1719
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001720 setup_term_clip();
1721 get_x11_title(FALSE);
1722 }
1723}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001724
1725 void
1726ex_xrestore(exarg_T *eap)
1727{
1728 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1729 {
1730 if (xterm_display_allocated)
1731 vim_free(xterm_display);
1732 xterm_display = (char *)vim_strsave(eap->arg);
1733 xterm_display_allocated = TRUE;
1734 }
1735 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001736 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001737
1738 clear_xterm_clip();
1739 x11_window = 0;
1740 xterm_dpy_retry_count = 5; // Try reconnecting five times
1741 may_restore_clipboard();
1742}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743#endif
1744
1745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 * Test if "dpy" and x11_window are valid by getting the window title.
1747 * I don't actually want it yet, so there may be a simpler call to use, but
1748 * this will cause the error handler x_error_check() to be called if anything
1749 * is wrong, such as the window pointer being invalid (as can happen when the
1750 * user changes his DISPLAY, but not his WINDOWID) -- webb
1751 */
1752 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001753test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 int (*old_handler)();
1756 XTextProperty text_prop;
1757
1758 old_handler = XSetErrorHandler(x_error_check);
1759 got_x_error = FALSE;
1760 if (XGetWMName(dpy, x11_window, &text_prop))
1761 XFree((void *)text_prop.value);
1762 XSync(dpy, False);
1763 (void)XSetErrorHandler(old_handler);
1764
1765 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001766 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
1768 return (got_x_error ? FAIL : OK);
1769}
1770#endif
1771
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
1773#ifdef FEAT_X11
1774
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001775static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777/*
1778 * try to get x11 window and display
1779 *
1780 * return FAIL for failure, OK otherwise
1781 */
1782 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001783get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
1785 char *winid;
1786 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001787#define XD_NONE 0 // x11_display not set here
1788#define XD_HERE 1 // x11_display opened here
1789#define XD_GUI 2 // x11_display used from gui.dpy
1790#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 static int x11_display_from = XD_NONE;
1792 static int did_set_error_handler = FALSE;
1793
1794 if (!did_set_error_handler)
1795 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001796 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 (void)XSetErrorHandler(x_error_handler);
1798 did_set_error_handler = TRUE;
1799 }
1800
Bram Moolenaar9372a112005-12-06 19:59:18 +00001801#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (gui.in_use)
1803 {
1804 /*
1805 * If the X11 display was opened here before, for the window where Vim
1806 * was started, close that one now to avoid a memory leak.
1807 */
1808 if (x11_display_from == XD_HERE && x11_display != NULL)
1809 {
1810 XCloseDisplay(x11_display);
1811 x11_display_from = XD_NONE;
1812 }
1813 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1814 {
1815 x11_display_from = XD_GUI;
1816 return OK;
1817 }
1818 x11_display = NULL;
1819 return FAIL;
1820 }
1821 else if (x11_display_from == XD_GUI)
1822 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001823 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 x11_window = 0;
1825 x11_display = NULL;
1826 x11_display_from = XD_NONE;
1827 }
1828#endif
1829
Bram Moolenaar0f873732019-12-05 20:28:46 +01001830 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (!x_connect_to_server())
1832 return FAIL;
1833
1834 /*
1835 * If WINDOWID not set, should try another method to find out
1836 * what the current window number is. The only code I know for
1837 * this is very complicated.
1838 * We assume that zero is invalid for WINDOWID.
1839 */
1840 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1841 x11_window = (Window)atol(winid);
1842
1843#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001844 if (xterm_dpy == x11_display)
1845 // x11_display may have been set to xterm_dpy elsewhere
1846 x11_display_from = XD_XTERM;
1847
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if (xterm_dpy != NULL && x11_window != 0)
1849 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001850 // We may have checked it already, but Gnome terminal can move us to
1851 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001852 if (x11_display_from != XD_XTERM)
1853 {
1854 /*
1855 * If the X11 display was opened here before, for the window where
1856 * Vim was started, close that one now to avoid a memory leak.
1857 */
1858 if (x11_display_from == XD_HERE && x11_display != NULL)
1859 XCloseDisplay(x11_display);
1860 x11_display = xterm_dpy;
1861 x11_display_from = XD_XTERM;
1862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 if (test_x11_window(x11_display) == FAIL)
1864 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001865 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 x11_window = 0;
1867 x11_display = NULL;
1868 x11_display_from = XD_NONE;
1869 return FAIL;
1870 }
1871 return OK;
1872 }
1873#endif
1874
1875 if (x11_window == 0 || x11_display == NULL)
1876 result = -1;
1877
Bram Moolenaar0f873732019-12-05 20:28:46 +01001878 if (result != -1) // Have already been here and set this
1879 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880
1881 if (x11_window != 0 && x11_display == NULL)
1882 {
1883#ifdef SET_SIG_ALARM
1884 RETSIGTYPE (*sig_save)();
1885#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001886#ifdef ELAPSED_FUNC
1887 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888
1889 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001890 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891#endif
1892
1893#ifdef SET_SIG_ALARM
1894 /*
1895 * Opening the Display may hang if the DISPLAY setting is wrong, or
1896 * the network connection is bad. Set an alarm timer to get out.
1897 */
1898 sig_alarm_called = FALSE;
1899 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1900 (RETSIGTYPE (*)())sig_alarm);
1901 alarm(2);
1902#endif
1903 x11_display = XOpenDisplay(NULL);
1904
1905#ifdef SET_SIG_ALARM
1906 alarm(0);
1907 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1908 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001909 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910#endif
1911 if (x11_display != NULL)
1912 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001913# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001915 {
1916 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001917 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001918 verbose_leave();
1919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920# endif
1921 if (test_x11_window(x11_display) == FAIL)
1922 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001923 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 x11_window = 0;
1925 XCloseDisplay(x11_display);
1926 x11_display = NULL;
1927 }
1928 else
1929 x11_display_from = XD_HERE;
1930 }
1931 }
1932 if (x11_window == 0 || x11_display == NULL)
1933 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001934
1935# ifdef FEAT_EVAL
1936 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1937# endif
1938
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 return (result = OK);
1940}
1941
1942/*
1943 * Determine original x11 Window Title
1944 */
1945 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001946get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001948 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949}
1950
1951/*
1952 * Determine original x11 Window icon
1953 */
1954 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001955get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956{
1957 int retval = FALSE;
1958
1959 retval = get_x11_thing(FALSE, test_only);
1960
Bram Moolenaar0f873732019-12-05 20:28:46 +01001961 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 if (oldicon == NULL && !test_only)
1963 {
1964 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001965 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001967 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 }
1969
1970 return retval;
1971}
1972
1973 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001974get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01001975 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01001976 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977{
1978 XTextProperty text_prop;
1979 int retval = FALSE;
1980 Status status;
1981
1982 if (get_x11_windis() == OK)
1983 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001984 // Get window/icon name if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 if (get_title)
1986 status = XGetWMName(x11_display, x11_window, &text_prop);
1987 else
1988 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1989
1990 /*
1991 * If terminal is xterm, then x11_window may be a child window of the
1992 * outer xterm window that actually contains the window/icon name, so
1993 * keep traversing up the tree until a window with a title/icon is
1994 * found.
1995 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001996 // Previously this was only done for xterm and alike. I don't see a
Bram Moolenaar0f873732019-12-05 20:28:46 +01001997 // reason why it would fail for other terminal emulators.
1998 // if (term_is_xterm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
2000 Window root;
2001 Window parent;
2002 Window win = x11_window;
2003 Window *children;
2004 unsigned int num_children;
2005
2006 while (!status || text_prop.value == NULL)
2007 {
2008 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2009 &num_children))
2010 break;
2011 if (children)
2012 XFree((void *)children);
2013 if (parent == root || parent == 0)
2014 break;
2015
2016 win = parent;
2017 if (get_title)
2018 status = XGetWMName(x11_display, win, &text_prop);
2019 else
2020 status = XGetWMIconName(x11_display, win, &text_prop);
2021 }
2022 }
2023 if (status && text_prop.value != NULL)
2024 {
2025 retval = TRUE;
2026 if (!test_only)
2027 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002028 if (get_title)
2029 vim_free(oldtitle);
2030 else
2031 vim_free(oldicon);
Bram Moolenaara12a1612019-01-24 16:39:02 +01002032 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (get_title)
2035 oldtitle = vim_strsave((char_u *)text_prop.value);
2036 else
2037 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 else
2040 {
2041 char **cl;
2042 Status transform_status;
2043 int n = 0;
2044
2045 transform_status = XmbTextPropertyToTextList(x11_display,
2046 &text_prop,
2047 &cl, &n);
2048 if (transform_status >= Success && n > 0 && cl[0])
2049 {
2050 if (get_title)
2051 oldtitle = vim_strsave((char_u *) cl[0]);
2052 else
2053 oldicon = vim_strsave((char_u *) cl[0]);
2054 XFreeStringList(cl);
2055 }
2056 else
2057 {
2058 if (get_title)
2059 oldtitle = vim_strsave((char_u *)text_prop.value);
2060 else
2061 oldicon = vim_strsave((char_u *)text_prop.value);
2062 }
2063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065 XFree((void *)text_prop.value);
2066 }
2067 }
2068 return retval;
2069}
2070
Bram Moolenaar0f873732019-12-05 20:28:46 +01002071// Xutf8 functions are not available on older systems. Note that on some
2072// systems X_HAVE_UTF8_STRING may be defined in a header file but
2073// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2074// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002075#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002076# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077# define USE_UTF8_STRING
2078# endif
2079#endif
2080
2081/*
2082 * Set x11 Window Title
2083 *
2084 * get_x11_windis() must be called before this and have returned OK
2085 */
2086 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002087set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002089 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2090 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2091 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092#ifdef USE_UTF8_STRING
2093 if (enc_utf8)
2094 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2095 NULL, NULL, 0, NULL, NULL, NULL);
2096 else
2097#endif
2098 {
2099#if XtSpecificationRelease >= 4
2100# ifdef FEAT_XFONTSET
2101 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2102 NULL, NULL, 0, NULL, NULL, NULL);
2103# else
2104 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002105 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
Bram Moolenaar0f873732019-12-05 20:28:46 +01002107 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002108 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 XSetWMProperties(x11_display, x11_window, &text_prop,
2110 NULL, NULL, 0, NULL, NULL, NULL);
2111# endif
2112#else
2113 XStoreName(x11_display, x11_window, (char *)title);
2114#endif
2115 }
2116 XFlush(x11_display);
2117}
2118
2119/*
2120 * Set x11 Window icon
2121 *
2122 * get_x11_windis() must be called before this and have returned OK
2123 */
2124 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002125set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002127 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128#ifdef USE_UTF8_STRING
2129 if (enc_utf8)
2130 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2131 NULL, 0, NULL, NULL, NULL);
2132 else
2133#endif
2134 {
2135#if XtSpecificationRelease >= 4
2136# ifdef FEAT_XFONTSET
2137 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2138 NULL, 0, NULL, NULL, NULL);
2139# else
2140 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002141 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002143 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2145 NULL, 0, NULL, NULL, NULL);
2146# endif
2147#else
2148 XSetIconName(x11_display, x11_window, (char *)icon);
2149#endif
2150 }
2151 XFlush(x11_display);
2152}
2153
Bram Moolenaar0f873732019-12-05 20:28:46 +01002154#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002157get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158{
2159 return FALSE;
2160}
2161
2162 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002163get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164{
2165 if (!test_only)
2166 {
2167 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002168 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002170 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
2172 return FALSE;
2173}
2174
Bram Moolenaar0f873732019-12-05 20:28:46 +01002175#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
2177 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002178mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
2180 return get_x11_title(TRUE);
2181}
2182
2183 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002184mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
2186 return get_x11_icon(TRUE);
2187}
2188
2189/*
2190 * Set the window title and icon.
2191 */
2192 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002193mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194{
2195 int type = 0;
2196 static int recursive = 0;
2197
Bram Moolenaar0f873732019-12-05 20:28:46 +01002198 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002200 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 return;
2202
Bram Moolenaar0f873732019-12-05 20:28:46 +01002203 // When one of the X11 functions causes a deadly signal, we get here again
2204 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 if (recursive)
2206 return;
2207 ++recursive;
2208
2209 /*
2210 * if the window ID and the display is known, we may use X11 calls
2211 */
2212#ifdef FEAT_X11
2213 if (get_x11_windis() == OK)
2214 type = 1;
2215#else
Bram Moolenaar097148e2020-08-11 21:58:20 +02002216# if defined(FEAT_GUI_PHOTON) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002217 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 if (gui.in_use)
2219 type = 1;
2220# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221#endif
2222
2223 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002224 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 * than x11 calls, because the x11 calls don't always work
2226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 if ((type || *T_TS != NUL) && title != NULL)
2228 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002229 if (oldtitle_outdated)
2230 {
2231 oldtitle_outdated = FALSE;
2232 VIM_CLEAR(oldtitle);
2233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 if (oldtitle == NULL
2235#ifdef FEAT_GUI
2236 && !gui.in_use
2237#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002238 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 (void)get_x11_title(FALSE);
2240
Bram Moolenaar0f873732019-12-05 20:28:46 +01002241 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 term_settitle(title);
2243#ifdef FEAT_X11
2244 else
2245# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002246 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002248 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002250#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002251 || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 else
2253 gui_mch_settitle(title, icon);
2254#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002255 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 }
2257
2258 if ((type || *T_CIS != NUL) && icon != NULL)
2259 {
2260 if (oldicon == NULL
2261#ifdef FEAT_GUI
2262 && !gui.in_use
2263#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002264 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 get_x11_icon(FALSE);
2266
2267 if (*T_CIS != NUL)
2268 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002269 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002271 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 out_flush();
2273 }
2274#ifdef FEAT_X11
2275 else
2276# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002277 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002279 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280#endif
2281 did_set_icon = TRUE;
2282 }
2283 --recursive;
2284}
2285
2286/*
2287 * Restore the window/icon title.
2288 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002289 * SAVE_RESTORE_TITLE only restore title
2290 * SAVE_RESTORE_ICON only restore icon
2291 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 */
2293 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002294mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
Bram Moolenaardac13472019-09-16 21:06:21 +02002296 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002297
Bram Moolenaar0f873732019-12-05 20:28:46 +01002298 // only restore the title or icon when it has been set
Bram Moolenaardac13472019-09-16 21:06:21 +02002299 mch_settitle(((which & SAVE_RESTORE_TITLE) && unix_did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002301 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2302
Bram Moolenaare5c83282019-05-03 23:15:37 +02002303 if (do_push_pop)
2304 {
2305 // pop and push from/to the stack
2306 term_pop_title(which);
2307 term_push_title(which);
2308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309}
2310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311
2312/*
2313 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002314 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 */
2316 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002317vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
2319 if (name == NULL)
2320 return FALSE;
2321 return (STRNICMP(name, "xterm", 5) == 0
2322 || STRNICMP(name, "nxterm", 6) == 0
2323 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002324 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002326 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 || STRCMP(name, "builtin_xterm") == 0);
2328}
2329
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002330#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2331/*
2332 * Return TRUE if "name" appears to be that of a terminal
2333 * known to support the xterm-style mouse protocol.
2334 * Relies on term_is_xterm having been set to its correct value.
2335 */
2336 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002337use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002338{
2339 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002340 && (term_is_xterm
2341 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002342 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002343 || STRICMP(name, "st") == 0
2344 || STRNICMP(name, "st-", 3) == 0
2345 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002346}
2347#endif
2348
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349/*
2350 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2351 * Return 1 for "xterm".
2352 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002353 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002354 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 */
2356 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002357use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002359 if (ttym_flags == TTYM_SGR)
2360 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002361 if (ttym_flags == TTYM_URXVT)
2362 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (ttym_flags == TTYM_XTERM2)
2364 return 2;
2365 if (ttym_flags == TTYM_XTERM)
2366 return 1;
2367 return 0;
2368}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002371vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 if (name == NULL)
2374 return FALSE;
2375 return (STRNICMP(name, "iris-ansi", 9) == 0
2376 || STRCMP(name, "builtin_iris-ansi") == 0);
2377}
2378
2379 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002380vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 if (name == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002383 return FALSE; // actually all ANSI comp. terminals should be here
2384 // catch VT100 - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002385 return ((STRNICMP(name, "vt", 2) == 0
2386 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 || STRCMP(name, "builtin_vt320") == 0);
2388}
2389
2390/*
2391 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2392 * This should include all windowed terminal emulators.
2393 */
2394 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002395vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
2397 if (name == NULL)
2398 return FALSE;
2399 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2400 return TRUE;
2401 return ( STRNICMP(name, "hpterm", 6) == 0
2402 || STRNICMP(name, "sun-cmd", 7) == 0
2403 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002404 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 || STRNICMP(name, "dtterm", 6) == 0);
2406}
2407
2408/*
2409 * Insert user name in s[len].
2410 * Return OK if a name found.
2411 */
2412 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002413mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414{
2415#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002416 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 return OK;
2418#else
2419 return mch_get_uname(getuid(), s, len);
2420#endif
2421}
2422
2423/*
2424 * Insert user name for "uid" in s[len].
2425 * Return OK if a name found.
2426 */
2427 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002428mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429{
2430#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2431 struct passwd *pw;
2432
2433 if ((pw = getpwuid(uid)) != NULL
2434 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2435 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002436 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 return OK;
2438 }
2439#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002440 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2441 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442}
2443
2444/*
2445 * Insert host name is s[len].
2446 */
2447
2448#ifdef HAVE_SYS_UTSNAME_H
2449 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002450mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
2452 struct utsname vutsname;
2453
2454 if (uname(&vutsname) < 0)
2455 *s = NUL;
2456 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002457 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002459#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460
2461# ifdef HAVE_SYS_SYSTEMINFO_H
2462# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2463# endif
2464
2465 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002466mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
2468# ifdef VAXC
2469 vaxc$gethostname((char *)s, len);
2470# else
2471 gethostname((char *)s, len);
2472# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002473 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002475#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
2477/*
2478 * return process ID
2479 */
2480 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002481mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483 return (long)getpid();
2484}
2485
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002486/*
2487 * return TRUE if process "pid" is still running
2488 */
2489 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002490mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002491{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002492 // If there is no error the process must be running.
2493 if (kill(pid, 0) == 0)
2494 return TRUE;
2495#ifdef ESRCH
2496 // If the error is ESRCH then the process is not running.
2497 if (errno == ESRCH)
2498 return FALSE;
2499#endif
2500 // If the process is running and owned by another user we get EPERM. With
2501 // other errors the process might be running, assuming it is then.
2502 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002503}
2504
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002507strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509 extern int sys_nerr;
2510 extern char *sys_errlist[];
2511 static char er[20];
2512
2513 if (err > 0 && err < sys_nerr)
2514 return (sys_errlist[err]);
2515 sprintf(er, "Error %d", err);
2516 return er;
2517}
2518#endif
2519
2520/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002521 * Get name of current directory into buffer "buf" of length "len" bytes.
2522 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 * Return OK for success, FAIL for failure.
2524 */
2525 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002526mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
2528#if defined(USE_GETCWD)
2529 if (getcwd((char *)buf, len) == NULL)
2530 {
2531 STRCPY(buf, strerror(errno));
2532 return FAIL;
2533 }
2534 return OK;
2535#else
2536 return (getwd((char *)buf) != NULL ? OK : FAIL);
2537#endif
2538}
2539
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002541 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 *
2543 * return FAIL for failure, OK for success
2544 */
2545 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002546mch_FullName(
2547 char_u *fname,
2548 char_u *buf,
2549 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002550 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002553#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002555 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 char_u olddir[MAXPATHL];
2558 char_u *p;
2559 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002560#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002561 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2562 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002563#endif
2564
Bram Moolenaar38323e42007-03-06 19:22:53 +00002565#ifdef VMS
2566 fname = vms_fixfilename(fname);
2567#endif
2568
Bram Moolenaara2442432007-04-26 14:26:37 +00002569#ifdef __CYGWIN__
2570 /*
2571 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2572 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002573# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002574 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2575 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002576 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2577 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002578# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002579 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002580# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002581 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002582#endif
2583
Bram Moolenaar0f873732019-12-05 20:28:46 +01002584 // Expand it if forced or not an absolute path.
2585 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002586 if ((force || !mch_isFullName(fname))
2587 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
2589 /*
2590 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002591 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 * This will get the correct path name with "../" things.
2593 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002594 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002596 if (STRCMP(p, "/..") == 0)
2597 // for "/path/dir/.." include the "/.."
2598 p += 3;
2599
Bram Moolenaar38323e42007-03-06 19:22:53 +00002600#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 /*
2602 * Use fchdir() if possible, it's said to be faster and more
2603 * reliable. But on SunOS 4 it might not work. Check this by
2604 * doing a fchdir() right now.
2605 */
2606 if (!dont_fchdir)
2607 {
2608 fd = open(".", O_RDONLY | O_EXTRA, 0);
2609 if (fd >= 0 && fchdir(fd) < 0)
2610 {
2611 close(fd);
2612 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002613 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 }
2615 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617
Bram Moolenaar0f873732019-12-05 20:28:46 +01002618 // Only change directory when we are sure we can return to where
2619 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002621#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 (mch_dirname(olddir, MAXPATHL) == FAIL
2625 || mch_chdir((char *)olddir) != 0))
2626 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002627 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 retval = FAIL;
2629 }
2630 else
2631 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002632 // The directory is copied into buf[], to be able to remove
2633 // the file name without changing it (could be a string in
2634 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 if (p - fname >= len)
2636 retval = FAIL;
2637 else
2638 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002639 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002641 {
2642 // Path does not exist (yet). For a full path fail,
2643 // will use the path as-is. For a relative path use
2644 // the current directory and append the file name.
2645 if (mch_isFullName(fname))
2646 retval = FAIL;
2647 else
2648 p = NULL;
2649 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002650 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002652 else
2653 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 *buf = NUL;
2655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 }
2657 }
2658 if (mch_dirname(buf, len) == FAIL)
2659 {
2660 retval = FAIL;
2661 *buf = NUL;
2662 }
2663 if (p != NULL)
2664 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002665#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 if (fd >= 0)
2667 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002668 if (p_verbose >= 5)
2669 {
2670 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002671 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002672 verbose_leave();
2673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
2676 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 l = mch_chdir((char *)olddir);
2679 if (l != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002680 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
itchyny051a40c2021-10-20 10:00:05 +01002682#ifdef HAVE_FCHDIR
2683 if (fd >= 0)
2684 close(fd);
2685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
2687 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002688 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002689 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002690#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002691 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002693 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002696
Bram Moolenaar0f873732019-12-05 20:28:46 +01002697 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002698 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 return FAIL;
2700
Bram Moolenaar0f873732019-12-05 20:28:46 +01002701 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (STRCMP(fname, ".") != 0)
2703 STRCAT(buf, fname);
2704
2705 return OK;
2706}
2707
2708/*
2709 * Return TRUE if "fname" does not depend on the current directory.
2710 */
2711 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002712mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002714#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 return ( fname[0] == '/' || fname[0] == '.' ||
2716 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2717 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2718 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002719#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721#endif
2722}
2723
Bram Moolenaar24552be2005-12-10 20:17:30 +00002724#if defined(USE_FNAME_CASE) || defined(PROTO)
2725/*
2726 * Set the case of the file name, if it already exists. This will cause the
2727 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002728 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002729 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002730 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002731fname_case(
2732 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002733 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002734{
2735 struct stat st;
2736 char_u *slash, *tail;
2737 DIR *dirp;
2738 struct dirent *dp;
2739
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002740 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002741 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002742 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002743 slash = vim_strrchr(name, '/');
2744 if (slash == NULL)
2745 {
2746 dirp = opendir(".");
2747 tail = name;
2748 }
2749 else
2750 {
2751 *slash = NUL;
2752 dirp = opendir((char *)name);
2753 *slash = '/';
2754 tail = slash + 1;
2755 }
2756
2757 if (dirp != NULL)
2758 {
2759 while ((dp = readdir(dirp)) != NULL)
2760 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002761 // Only accept names that differ in case and are the same byte
2762 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002763 if (STRICMP(tail, dp->d_name) == 0
2764 && STRLEN(tail) == STRLEN(dp->d_name))
2765 {
2766 char_u newname[MAXPATHL + 1];
2767 struct stat st2;
2768
Bram Moolenaar0f873732019-12-05 20:28:46 +01002769 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002770 vim_strncpy(newname, name, MAXPATHL);
2771 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2772 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002773 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002774 && st.st_ino == st2.st_ino
2775 && st.st_dev == st2.st_dev)
2776 {
2777 STRCPY(tail, dp->d_name);
2778 break;
2779 }
2780 }
2781 }
2782
2783 closedir(dirp);
2784 }
2785 }
2786}
2787#endif
2788
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789/*
2790 * Get file permissions for 'name'.
2791 * Returns -1 when it doesn't exist.
2792 */
2793 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002794mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 struct stat statb;
2797
Bram Moolenaar0f873732019-12-05 20:28:46 +01002798 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799#ifdef VMS
2800 if (stat((char *)vms_fixfilename(name), &statb))
2801#else
2802 if (stat((char *)name, &statb))
2803#endif
2804 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002805#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002806 // The top bit makes the value negative, which means the file doesn't
2807 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002808 return statb.st_mode & ~S_ADDACE;
2809#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812}
2813
2814/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002815 * Set file permission for "name" to "perm".
2816 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 */
2818 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002819mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
2821 return (chmod((char *)
2822#ifdef VMS
2823 vms_fixfilename(name),
2824#else
2825 name,
2826#endif
2827 (mode_t)perm) == 0 ? OK : FAIL);
2828}
2829
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002830#if defined(HAVE_FCHMOD) || defined(PROTO)
2831/*
2832 * Set file permission for open file "fd" to "perm".
2833 * Return FAIL for failure, OK otherwise.
2834 */
2835 int
2836mch_fsetperm(int fd, long perm)
2837{
2838 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2839}
2840#endif
2841
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842#if defined(HAVE_ACL) || defined(PROTO)
2843# ifdef HAVE_SYS_ACL_H
2844# include <sys/acl.h>
2845# endif
2846# ifdef HAVE_SYS_ACCESS_H
2847# include <sys/access.h>
2848# endif
2849
2850# ifdef HAVE_SOLARIS_ACL
2851typedef struct vim_acl_solaris_T {
2852 int acl_cnt;
2853 aclent_t *acl_entry;
2854} vim_acl_solaris_T;
2855# endif
2856
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002857#if defined(HAVE_SELINUX) || defined(PROTO)
2858/*
2859 * Copy security info from "from_file" to "to_file".
2860 */
2861 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002862mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002863{
2864 if (from_file == NULL)
2865 return;
2866
2867 if (selinux_enabled == -1)
2868 selinux_enabled = is_selinux_enabled();
2869
2870 if (selinux_enabled > 0)
2871 {
Bram Moolenaar89560232020-10-09 23:04:47 +02002872 // Use "char *" instead of "security_context_t" to avoid a deprecation
2873 // warning.
2874 char *from_context = NULL;
2875 char *to_context = NULL;
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002876
2877 if (getfilecon((char *)from_file, &from_context) < 0)
2878 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002879 // If the filesystem doesn't support extended attributes,
2880 // the original had no special security context and the
2881 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002882 if (errno == EOPNOTSUPP)
2883 return;
2884
Bram Moolenaar32526b32019-01-19 17:43:09 +01002885 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002886 msg_outtrans(from_file);
2887 msg_putchar('\n');
2888 return;
2889 }
2890 if (getfilecon((char *)to_file, &to_context) < 0)
2891 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002892 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002893 msg_outtrans(to_file);
2894 msg_putchar('\n');
2895 freecon (from_context);
2896 return ;
2897 }
2898 if (strcmp(from_context, to_context) != 0)
2899 {
2900 if (setfilecon((char *)to_file, from_context) < 0)
2901 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002902 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002903 msg_outtrans(to_file);
2904 msg_putchar('\n');
2905 }
2906 }
2907 freecon(to_context);
2908 freecon(from_context);
2909 }
2910}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002911#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002912
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002913#if defined(HAVE_SMACK) && !defined(PROTO)
2914/*
2915 * Copy security info from "from_file" to "to_file".
2916 */
2917 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002918mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002919{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002920 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002921 {
2922 XATTR_NAME_SMACK,
2923 XATTR_NAME_SMACKEXEC,
2924 XATTR_NAME_SMACKMMAP
2925 };
2926
2927 char buffer[SMACK_LABEL_LEN];
2928 const char *name;
2929 int index;
2930 int ret;
2931 ssize_t size;
2932
2933 if (from_file == NULL)
2934 return;
2935
2936 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2937 / sizeof(smack_copied_attributes)[0]) ; index++)
2938 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002939 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002940 name = smack_copied_attributes[index];
2941
Bram Moolenaar0f873732019-12-05 20:28:46 +01002942 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002943 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2944 if (size >= 0)
2945 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002946 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002947 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2948 if (ret < 0)
2949 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002950 vim_snprintf((char *)IObuff, IOSIZE,
2951 _("Could not set security context %s for %s"),
2952 name, to_file);
2953 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002954 msg_putchar('\n');
2955 }
2956 }
2957 else
2958 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002959 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002960 switch (errno)
2961 {
2962 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002963 // extended attributes aren't supported or enabled
2964 // should a message be echoed? not sure...
2965 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002966
2967 case ERANGE:
2968 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002969 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002970 vim_snprintf((char *)IObuff, IOSIZE,
2971 _("Could not get security context %s for %s. Removing it!"),
2972 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002973 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002974 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01002975 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002976
2977 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002978 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002979 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002980 // Silently ignore errors, apparently this happens when
2981 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002982 break;
2983 }
2984 }
2985 }
2986}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002987#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002988
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989/*
2990 * Return a pointer to the ACL of file "fname" in allocated memory.
2991 * Return NULL if the ACL is not available for whatever reason.
2992 */
2993 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002994mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
2996 vim_acl_T ret = NULL;
2997#ifdef HAVE_POSIX_ACL
2998 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2999#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003000#ifdef HAVE_SOLARIS_ZFS_ACL
3001 acl_t *aclent;
3002
3003 if (acl_get((char *)fname, 0, &aclent) < 0)
3004 return NULL;
3005 ret = (vim_acl_T)aclent;
3006#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007#ifdef HAVE_SOLARIS_ACL
3008 vim_acl_solaris_T *aclent;
3009
3010 aclent = malloc(sizeof(vim_acl_solaris_T));
3011 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3012 {
3013 free(aclent);
3014 return NULL;
3015 }
3016 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3017 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3018 {
3019 free(aclent->acl_entry);
3020 free(aclent);
3021 return NULL;
3022 }
3023 ret = (vim_acl_T)aclent;
3024#else
3025#if defined(HAVE_AIX_ACL)
3026 int aclsize;
3027 struct acl *aclent;
3028
3029 aclsize = sizeof(struct acl);
3030 aclent = malloc(aclsize);
3031 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3032 {
3033 if (errno == ENOSPC)
3034 {
3035 aclsize = aclent->acl_len;
3036 aclent = realloc(aclent, aclsize);
3037 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3038 {
3039 free(aclent);
3040 return NULL;
3041 }
3042 }
3043 else
3044 {
3045 free(aclent);
3046 return NULL;
3047 }
3048 }
3049 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003050#endif // HAVE_AIX_ACL
3051#endif // HAVE_SOLARIS_ACL
3052#endif // HAVE_SOLARIS_ZFS_ACL
3053#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 return ret;
3055}
3056
3057/*
3058 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3059 */
3060 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003061mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063 if (aclent == NULL)
3064 return;
3065#ifdef HAVE_POSIX_ACL
3066 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3067#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003068#ifdef HAVE_SOLARIS_ZFS_ACL
3069 acl_set((char *)fname, (acl_t *)aclent);
3070#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071#ifdef HAVE_SOLARIS_ACL
3072 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3073 ((vim_acl_solaris_T *)aclent)->acl_entry);
3074#else
3075#ifdef HAVE_AIX_ACL
3076 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003077#endif // HAVE_AIX_ACL
3078#endif // HAVE_SOLARIS_ACL
3079#endif // HAVE_SOLARIS_ZFS_ACL
3080#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081}
3082
3083 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003084mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
3086 if (aclent == NULL)
3087 return;
3088#ifdef HAVE_POSIX_ACL
3089 acl_free((acl_t)aclent);
3090#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003091#ifdef HAVE_SOLARIS_ZFS_ACL
3092 acl_free((acl_t *)aclent);
3093#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094#ifdef HAVE_SOLARIS_ACL
3095 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3096 free(aclent);
3097#else
3098#ifdef HAVE_AIX_ACL
3099 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003100#endif // HAVE_AIX_ACL
3101#endif // HAVE_SOLARIS_ACL
3102#endif // HAVE_SOLARIS_ZFS_ACL
3103#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104}
3105#endif
3106
3107/*
3108 * Set hidden flag for "name".
3109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003111mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003113 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114}
3115
3116/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003117 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 * return FALSE if "name" is not a directory
3119 * return FALSE for error
3120 */
3121 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003122mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123{
3124 struct stat statb;
3125
Bram Moolenaar0f873732019-12-05 20:28:46 +01003126 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 return FALSE;
3128 if (stat((char *)name, &statb))
3129 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131}
3132
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003133/*
3134 * return TRUE if "name" is a directory, NOT a symlink to a directory
3135 * return FALSE if "name" is not a directory
3136 * return FALSE for error
3137 */
3138 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003139mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003140{
3141 struct stat statb;
3142
Bram Moolenaar0f873732019-12-05 20:28:46 +01003143 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003144 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003145 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003146 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003147 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003148}
3149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150/*
3151 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3152 */
3153 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003154executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155{
3156 struct stat st;
3157
3158 if (stat((char *)name, &st))
3159 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003160#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003161 // Like on Unix system file can have executable rights but not necessarily
3162 // be an executable, but on Unix is not a default for an ordinary file to
3163 // have an executable flag - on VMS it is in most cases.
3164 // Therefore, this check does not have any sense - let keep us to the
3165 // conventions instead:
3166 // *.COM and *.EXE files are the executables - the rest are not. This is
3167 // not ideal but better then it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003168 int vms_executable = 0;
3169 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3170 {
3171 if (strstr(vms_tolower((char*)name),".exe") != NULL
3172 || strstr(vms_tolower((char*)name),".com")!= NULL)
3173 vms_executable = 1;
3174 }
3175 return vms_executable;
3176#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179}
3180
3181/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003182 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003183 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 * Return -1 if unknown.
3185 */
3186 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003187mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188{
3189 char_u *buf;
3190 char_u *p, *e;
3191 int retval;
3192
Bram Moolenaar0f873732019-12-05 20:28:46 +01003193 // When "use_path" is false and if it's an absolute or relative path don't
3194 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003195 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003196 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003197 // There must be a path separator, files in the current directory
3198 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003199 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003200 {
3201 if (path != NULL)
3202 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003203 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003204 *path = FullName_save(name, TRUE);
3205 else
3206 *path = vim_strsave(name);
3207 }
3208 return TRUE;
3209 }
3210 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
3213 p = (char_u *)getenv("PATH");
3214 if (p == NULL || *p == NUL)
3215 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003216 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 if (buf == NULL)
3218 return -1;
3219
3220 /*
3221 * Walk through all entries in $PATH to check if "name" exists there and
3222 * is an executable file.
3223 */
3224 for (;;)
3225 {
3226 e = (char_u *)strchr((char *)p, ':');
3227 if (e == NULL)
3228 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003229 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 STRCPY(buf, "./");
3231 else
3232 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003233 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 add_pathsep(buf);
3235 }
3236 STRCAT(buf, name);
3237 retval = executable_file(buf);
3238 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003239 {
3240 if (path != NULL)
3241 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003242 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003243 *path = FullName_save(buf, TRUE);
3244 else
3245 *path = vim_strsave(buf);
3246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
3250 if (*e != ':')
3251 break;
3252 p = e + 1;
3253 }
3254
3255 vim_free(buf);
3256 return retval;
3257}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
3259/*
3260 * Check what "name" is:
3261 * NODE_NORMAL: file or directory (or doesn't exist)
3262 * NODE_WRITABLE: writable device, socket, fifo, etc.
3263 * NODE_OTHER: non-writable things
3264 */
3265 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003266mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267{
3268 struct stat st;
3269
3270 if (stat((char *)name, &st))
3271 return NODE_NORMAL;
3272 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3273 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003274 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003276 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 return NODE_WRITABLE;
3278}
3279
3280 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003281mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283#ifdef HAVE_CHECK_STACK_GROWTH
3284 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 check_stack_growth((char *)&i);
3287
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003288# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 get_stack_limit();
3290# endif
3291
3292#endif
3293
3294 /*
3295 * Setup an alternative stack for signals. Helps to catch signals when
3296 * running out of stack space.
3297 * Use of sigaltstack() is preferred, it's more portable.
3298 * Ignore any errors.
3299 */
3300#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003301 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 init_signal_stack();
3303#endif
3304}
3305
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003306#if defined(EXITFREE) || defined(PROTO)
3307 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003308mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003309{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003310# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3311 if (clip_star.owned)
3312 clip_lose_selection(&clip_star);
3313 if (clip_plus.owned)
3314 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003315# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003316# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003317 if (xterm_Shell != (Widget)0)
3318 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003319# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003320 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003321 if (xterm_dpy != NULL)
3322 XtCloseDisplay(xterm_dpy);
3323 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003324 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003325 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003326# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003327 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003328# endif
3329 }
3330# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003331# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003332# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003333 if (x11_display != NULL
3334# ifdef FEAT_XCLIPBOARD
3335 && x11_display != xterm_dpy
3336# endif
3337 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003338 XCloseDisplay(x11_display);
3339# endif
3340# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003341 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003342# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003343 vim_free(oldtitle);
3344 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003345}
3346#endif
3347
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348/*
3349 * Output a newline when exiting.
3350 * Make sure the newline goes to the same stream as the text.
3351 */
3352 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003353exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003355 if (silent_mode)
3356 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 if (newline_on_exit || msg_didout)
3358 {
3359 if (msg_use_printf())
3360 {
3361 if (info_message)
3362 mch_msg("\n");
3363 else
3364 mch_errmsg("\r\n");
3365 }
3366 else
3367 out_char('\n');
3368 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003369 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003371 restore_cterm_colors(); // get original colors back
3372 msg_clr_eos_force(); // clear the rest of the display
3373 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
3375}
3376
Bram Moolenaarb4151682020-05-11 22:13:28 +02003377#ifdef USE_GCOV_FLUSH
3378extern void __gcov_flush();
3379#endif
3380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003382mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
3384 exiting = TRUE;
3385
3386#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3387 x11_export_final_selection();
3388#endif
3389
3390#ifdef FEAT_GUI
3391 if (!gui.in_use)
3392#endif
3393 {
3394 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003395 if (!is_not_a_term())
3396 {
3397 // restore xterm title and icon name
3398 mch_restore_title(SAVE_RESTORE_BOTH);
3399 term_pop_title(SAVE_RESTORE_BOTH);
3400 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003401
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 /*
3403 * When t_ti is not empty but it doesn't cause swapping terminal
3404 * pages, need to output a newline when msg_didout is set. But when
3405 * t_ti does swap pages it should not go to the shell page. Do this
3406 * before stoptermcap().
3407 */
3408 if (swapping_screen() && !newline_on_exit)
3409 exit_scroll();
3410
Bram Moolenaar0f873732019-12-05 20:28:46 +01003411 // Stop termcap: May need to check for T_CRV response, which
3412 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 stoptermcap();
3414
3415 /*
3416 * A newline is only required after a message in the alternate screen.
3417 * This is set to TRUE by wait_return().
3418 */
3419 if (!swapping_screen() || newline_on_exit)
3420 exit_scroll();
3421
Bram Moolenaar0f873732019-12-05 20:28:46 +01003422 // Cursor may have been switched off without calling starttermcap()
3423 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 if (full_screen)
3425 cursor_on();
3426 }
3427 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003428 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003429
3430#ifdef USE_GCOV_FLUSH
3431 // Flush coverage info before possibly being killed by a deadly signal.
3432 __gcov_flush();
3433#endif
3434
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 may_core_dump();
3436#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 gui_exit(r);
3439#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003440
Bram Moolenaar56718732006-03-15 22:53:57 +00003441#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003442 mac_conv_cleanup();
3443#endif
3444
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003446 // A core dump won't be created if the signal handler
3447 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 if (deadly_signal != 0)
3449 return;
3450#endif
3451
Bram Moolenaar009b2592004-10-24 19:18:58 +00003452#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003453 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003454#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003455
3456#ifdef EXITFREE
3457 free_all_mem();
3458#endif
3459
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 exit(r);
3461}
3462
3463 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003464may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465{
3466 if (deadly_signal != 0)
3467 {
3468 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003469 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
3471}
3472
3473#ifndef VMS
3474
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003475/*
3476 * Get the file descriptor to use for tty operations.
3477 */
3478 static int
3479get_tty_fd(int fd)
3480{
3481 int tty_fd = fd;
3482
3483#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3484 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3485 // "fd" when it is a master device.
3486 if (mch_isatty(fd) > 1)
3487 {
3488 char *name;
3489
3490 name = ptsname(fd);
3491 if (name == NULL)
3492 return -1;
3493
3494 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3495 if (tty_fd < 0)
3496 return -1;
3497 }
3498#endif
3499 return tty_fd;
3500}
3501
3502 static int
3503mch_tcgetattr(int fd, void *term)
3504{
3505 int tty_fd;
3506 int retval = -1;
3507
3508 tty_fd = get_tty_fd(fd);
3509 if (tty_fd >= 0)
3510 {
3511#ifdef NEW_TTY_SYSTEM
3512# ifdef HAVE_TERMIOS_H
3513 retval = tcgetattr(tty_fd, (struct termios *)term);
3514# else
3515 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3516# endif
3517#else
3518 // for "old" tty systems
3519 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3520#endif
3521 if (tty_fd != fd)
3522 close(tty_fd);
3523 }
3524 return retval;
3525}
3526
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003528mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529{
3530 static int first = TRUE;
3531
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003532#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533# ifdef HAVE_TERMIOS_H
3534 static struct termios told;
3535 struct termios tnew;
3536# else
3537 static struct termio told;
3538 struct termio tnew;
3539# endif
3540
3541 if (first)
3542 {
3543 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003544 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546
3547 tnew = told;
3548 if (tmode == TMODE_RAW)
3549 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003550 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003551 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3552 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003554# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003555 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556# endif
3557 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003558# ifdef ONLCR
3559 // Don't map NL -> CR NL, we do it ourselves.
3560 // Also disable expanding tabs if possible.
3561# ifdef XTABS
3562 tnew.c_oflag &= ~(ONLCR | XTABS);
3563# else
3564# ifdef TAB3
3565 tnew.c_oflag &= ~(ONLCR | TAB3);
3566# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003568# endif
3569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003571 tnew.c_cc[VMIN] = 1; // return after 1 char
3572 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
3574 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003575 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003576 // Also reset ICANON here, otherwise on Solaris select() won't see
3577 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003578 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003579 tnew.c_cc[VMIN] = 1; // return after 1 char
3580 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583# if defined(HAVE_TERMIOS_H)
3584 {
3585 int n = 10;
3586
Bram Moolenaar0f873732019-12-05 20:28:46 +01003587 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3588 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3590 && errno == EINTR && n > 0)
3591 --n;
3592 }
3593# else
3594 ioctl(read_cmd_fd, TCSETA, &tnew);
3595# endif
3596
3597#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 /*
3599 * for "old" tty systems
3600 */
3601# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003602# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603# endif
3604 static struct sgttyb ttybold;
3605 struct sgttyb ttybnew;
3606
3607 if (first)
3608 {
3609 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003610 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612
3613 ttybnew = ttybold;
3614 if (tmode == TMODE_RAW)
3615 {
3616 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3617 ttybnew.sg_flags |= RAW;
3618 }
3619 else if (tmode == TMODE_SLEEP)
3620 ttybnew.sg_flags &= ~(ECHO);
3621 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3622#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003623 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624}
3625
3626/*
3627 * Try to get the code for "t_kb" from the stty setting
3628 *
3629 * Even if termcap claims a backspace key, the user's setting *should*
3630 * prevail. stty knows more about reality than termcap does, and if
3631 * somebody's usual erase key is DEL (which, for most BSD users, it will
3632 * be), they're going to get really annoyed if their erase key starts
3633 * doing forward deletes for no reason. (Eric Fischer)
3634 */
3635 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003636get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003638 ttyinfo_T info;
3639 char_u buf[2];
3640 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003642 if (get_tty_info(read_cmd_fd, &info) == OK)
3643 {
3644 intr_char = info.interrupt;
3645 buf[0] = info.backspace;
3646 buf[1] = NUL;
3647 add_termcode((char_u *)"kb", buf, FALSE);
3648
Bram Moolenaar0f873732019-12-05 20:28:46 +01003649 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003650 p = find_termcode((char_u *)"kD");
3651 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3652 do_fixdel(NULL);
3653 }
3654}
3655
3656/*
3657 * Obtain the characters that Backspace and Enter produce on "fd".
3658 * Returns OK or FAIL.
3659 */
3660 int
3661get_tty_info(int fd, ttyinfo_T *info)
3662{
3663#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664# ifdef HAVE_TERMIOS_H
3665 struct termios keys;
3666# else
3667 struct termio keys;
3668# endif
3669
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003670 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003672 info->backspace = keys.c_cc[VERASE];
3673 info->interrupt = keys.c_cc[VINTR];
3674 if (keys.c_iflag & ICRNL)
3675 info->enter = NL;
3676 else
3677 info->enter = CAR;
3678 if (keys.c_oflag & ONLCR)
3679 info->nl_does_cr = TRUE;
3680 else
3681 info->nl_does_cr = FALSE;
3682 return OK;
3683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003685 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 struct sgttyb keys;
3687
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003688 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003690 info->backspace = keys.sg_erase;
3691 info->interrupt = keys.sg_kill;
3692 info->enter = CAR;
3693 info->nl_does_cr = TRUE;
3694 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003697 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698}
3699
Bram Moolenaar0f873732019-12-05 20:28:46 +01003700#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003702static int mouse_ison = FALSE;
3703
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704/*
3705 * Set mouse clicks on or off.
3706 */
3707 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003708mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003710#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003711 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 int xterm_mouse_vers;
3714
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003715#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003716 if (!on)
3717 // Make sure not tracing mouse movements. Important when a button-down
3718 // was received but no release yet.
3719 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003720#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003721
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003722 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003723#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003724 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003725#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003726 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003727 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 return;
3729
3730 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003731
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003732#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003733 if (ttym_flags == TTYM_URXVT)
3734 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003735 out_str_nf((char_u *)
3736 (on
3737 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3738 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003739 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003740 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003741#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003742
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003743 if (ttym_flags == TTYM_SGR)
3744 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003745 // SGR mode supports columns above 223
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003746 out_str_nf((char_u *)
3747 (on
3748 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3749 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003750 mouse_ison = on;
3751 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003752
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003753#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003754 if (bevalterm_ison != (p_bevalterm && on))
3755 {
3756 bevalterm_ison = (p_bevalterm && on);
3757 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003758 // disable mouse movement events, enabling is below
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003759 out_str_nf((char_u *)
3760 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003761 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003762#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003763
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 if (xterm_mouse_vers > 0)
3765 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003766 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 out_str_nf((char_u *)
3768 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003769 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003770#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003771 bevalterm_ison
3772 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003773#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003774 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003776 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 out_str_nf((char_u *)
3778 (xterm_mouse_vers > 1
3779 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3780 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003781 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
3783
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003784#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 else if (ttym_flags == TTYM_DEC)
3786 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003787 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003789 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003791 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003795#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 else
3797 {
3798 if (on)
3799 {
3800 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003801 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
3803 else
3804 {
3805 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003806 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
3808 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003811#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003812 else
3813 {
3814 if (on)
3815 {
3816 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003817 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003818 }
3819 else
3820 {
3821 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003822 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003823 }
3824 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003825#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003826
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003827#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 else
3829 {
3830 if (on)
3831 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003832 // D - Enable Mouse up/down messages
3833 // L - Enable Left Button Reporting
3834 // M - Enable Middle Button Reporting
3835 // R - Enable Right Button Reporting
3836 // K - Enable SHIFT and CTRL key Reporting
3837 // + - Enable Advanced messaging of mouse moves and up/down messages
3838 // Q - Quiet No Ack
3839 // # - Numeric value of mouse pointer required
3840 // 0 = Multiview 2000 cursor, used as standard
3841 // 1 = Windows Arrow
3842 // 2 = Windows I Beam
3843 // 3 = Windows Hour Glass
3844 // 4 = Windows Cross Hair
3845 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003846# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003847 // Disables full feedback of pointer movements
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3849 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003850# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3852 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003853# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003854 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
3856 else
3857 {
3858 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3859 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003860 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 }
3862 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003863#endif
3864#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 else
3866 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003867 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 if (on)
3869 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3870 else
3871 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003872 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875}
3876
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003877#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003878/*
3879 * Called when 'balloonevalterm' changed.
3880 */
3881 void
3882mch_bevalterm_changed(void)
3883{
3884 mch_setmouse(mouse_ison);
3885}
3886#endif
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888/*
3889 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3890 */
3891 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003892check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893{
3894# ifdef FEAT_MOUSE_XTERM
3895 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003896# ifdef FEAT_MOUSE_URXVT
3897 && use_xterm_mouse() != 3
3898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899# ifdef FEAT_GUI
3900 && !gui.in_use
3901# endif
3902 )
3903 {
3904 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003905 ? IF_EB("\233M", CSI_STR "M")
3906 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 if (*p_mouse != NUL)
3908 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003909 // force mouse off and maybe on to send possibly new mouse
3910 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 mch_setmouse(FALSE);
3912 setmouse();
3913 }
3914 }
3915 else
3916 del_mouse_termcode(KS_MOUSE);
3917# endif
3918
3919# ifdef FEAT_MOUSE_GPM
3920 if (!use_xterm_mouse()
3921# ifdef FEAT_GUI
3922 && !gui.in_use
3923# endif
3924 )
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003925 set_mouse_termcode(KS_GPM_MOUSE,
3926 (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3927 else
3928 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929# endif
3930
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003931# ifdef FEAT_SYSMOUSE
3932 if (!use_xterm_mouse()
3933# ifdef FEAT_GUI
3934 && !gui.in_use
3935# endif
3936 )
3937 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3938# endif
3939
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003941 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if (!use_xterm_mouse()
3943# ifdef FEAT_GUI
3944 && !gui.in_use
3945# endif
3946 )
3947 set_mouse_termcode(KS_JSBTERM_MOUSE,
3948 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3949 else
3950 del_mouse_termcode(KS_JSBTERM_MOUSE);
3951# endif
3952
3953# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003954 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3955 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if (!use_xterm_mouse()
3957# ifdef FEAT_GUI
3958 && !gui.in_use
3959# endif
3960 )
3961 set_mouse_termcode(KS_NETTERM_MOUSE,
3962 (char_u *)IF_EB("\033}", ESC_STR "}"));
3963 else
3964 del_mouse_termcode(KS_NETTERM_MOUSE);
3965# endif
3966
3967# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003968 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003969 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970# ifdef FEAT_GUI
3971 && !gui.in_use
3972# endif
3973 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003974 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3975 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 else
3977 del_mouse_termcode(KS_DEC_MOUSE);
3978# endif
3979# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01003980 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003981 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982# ifdef FEAT_GUI
3983 && !gui.in_use
3984# endif
3985 )
3986 set_mouse_termcode(KS_PTERM_MOUSE,
3987 (char_u *) IF_EB("\033[", ESC_STR "["));
3988 else
3989 del_mouse_termcode(KS_PTERM_MOUSE);
3990# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003991# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003992 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003993# ifdef FEAT_GUI
3994 && !gui.in_use
3995# endif
3996 )
3997 {
3998 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003999 ? IF_EB("\233*M", CSI_STR "*M")
4000 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004001
4002 if (*p_mouse != NUL)
4003 {
4004 mch_setmouse(FALSE);
4005 setmouse();
4006 }
4007 }
4008 else
4009 del_mouse_termcode(KS_URXVT_MOUSE);
4010# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004011 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004012# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004013 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004014# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004015 )
4016 {
4017 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02004018 ? IF_EB("\233<*M", CSI_STR "<*M")
4019 : IF_EB("\033[<*M", ESC_STR "[<*M")));
4020
4021 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
4022 ? IF_EB("\233<*m", CSI_STR "<*m")
4023 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004024
4025 if (*p_mouse != NUL)
4026 {
4027 mch_setmouse(FALSE);
4028 setmouse();
4029 }
4030 }
4031 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004032 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004033 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004034 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038#ifndef VMS
4039
4040/*
4041 * Try to get the current window size:
4042 * 1. with an ioctl(), most accurate method
4043 * 2. from the environment variables LINES and COLUMNS
4044 * 3. from the termcap
4045 * 4. keep using the old values
4046 * Return OK when size could be determined, FAIL otherwise.
4047 */
4048 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004049mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050{
4051 long rows = 0;
4052 long columns = 0;
4053 char_u *p;
4054
4055 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 * 1. try using an ioctl. It is the most accurate method.
4057 *
4058 * Try using TIOCGWINSZ first, some systems that have it also define
4059 * TIOCGSIZE but don't have a struct ttysize.
4060 */
4061# ifdef TIOCGWINSZ
4062 {
4063 struct winsize ws;
4064 int fd = 1;
4065
Bram Moolenaar0f873732019-12-05 20:28:46 +01004066 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 if (!isatty(fd) && isatty(read_cmd_fd))
4068 fd = read_cmd_fd;
4069 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4070 {
4071 columns = ws.ws_col;
4072 rows = ws.ws_row;
4073 }
4074 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004075# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076# ifdef TIOCGSIZE
4077 {
4078 struct ttysize ts;
4079 int fd = 1;
4080
Bram Moolenaar0f873732019-12-05 20:28:46 +01004081 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 if (!isatty(fd) && isatty(read_cmd_fd))
4083 fd = read_cmd_fd;
4084 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4085 {
4086 columns = ts.ts_cols;
4087 rows = ts.ts_lines;
4088 }
4089 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004090# endif // TIOCGSIZE
4091# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092
4093 /*
4094 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004095 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4096 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004098 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 {
4100 if ((p = (char_u *)getenv("LINES")))
4101 rows = atoi((char *)p);
4102 if ((p = (char_u *)getenv("COLUMNS")))
4103 columns = atoi((char *)p);
4104 }
4105
4106#ifdef HAVE_TGETENT
4107 /*
4108 * 3. try reading "co" and "li" entries from termcap
4109 */
4110 if (columns == 0 || rows == 0)
4111 getlinecol(&columns, &rows);
4112#endif
4113
4114 /*
4115 * 4. If everything fails, use the old values
4116 */
4117 if (columns <= 0 || rows <= 0)
4118 return FAIL;
4119
4120 Rows = rows;
4121 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004122 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return OK;
4124}
4125
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004126#if defined(FEAT_TERMINAL) || defined(PROTO)
4127/*
4128 * Report the windows size "rows" and "cols" to tty "fd".
4129 */
4130 int
4131mch_report_winsize(int fd, int rows, int cols)
4132{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004133 int tty_fd;
4134 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004135
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004136 tty_fd = get_tty_fd(fd);
4137 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004138 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004139# if defined(TIOCSWINSZ)
4140 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004141
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004142 ws.ws_col = cols;
4143 ws.ws_row = rows;
4144 ws.ws_xpixel = cols * 5;
4145 ws.ws_ypixel = rows * 10;
4146 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4147 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4148 retval == 0 ? "success" : "failed");
4149# elif defined(TIOCSSIZE)
4150 struct ttysize ts;
4151
4152 ts.ts_cols = cols;
4153 ts.ts_lines = rows;
4154 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4155 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4156 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004157# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004158 if (tty_fd != fd)
4159 close(tty_fd);
4160 }
4161 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004162}
4163#endif
4164
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165/*
4166 * Try to set the window size to Rows and Columns.
4167 */
4168 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004169mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
4171 if (*T_CWS)
4172 {
4173 /*
4174 * NOTE: if you get an error here that term_set_winsize() is
4175 * undefined, check the output of configure. It could probably not
4176 * find a ncurses, termcap or termlib library.
4177 */
4178 term_set_winsize((int)Rows, (int)Columns);
4179 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004180 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182}
4183
Bram Moolenaar0f873732019-12-05 20:28:46 +01004184#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185
4186/*
4187 * Rows and/or Columns has changed.
4188 */
4189 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004190mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004192 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193}
4194
Bram Moolenaar205b8862011-09-07 15:04:31 +02004195/*
4196 * Wait for process "child" to end.
4197 * Return "child" if it exited properly, <= 0 on error.
4198 */
4199 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004200wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004201{
4202 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004203 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004204
4205 while (wait_pid != child)
4206 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004207 // When compiled with Python threads are probably used, in which case
4208 // wait() sometimes hangs for no obvious reason. Use waitpid()
4209 // instead and loop (like the GUI). Also needed for other interfaces,
4210 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004211# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004212 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004213# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004214 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004215# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004216 if (wait_pid == 0)
4217 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004218 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004219 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004220 if (++delay_msec > 10)
4221 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004222 continue;
4223 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004224 if (wait_pid <= 0
4225# ifdef ECHILD
4226 && errno == ECHILD
4227# endif
4228 )
4229 break;
4230 }
4231 return wait_pid;
4232}
4233
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004234#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004235/*
4236 * Set the environment for a child process.
4237 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004238 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004239set_child_environment(
4240 long rows,
4241 long columns,
4242 char *term,
4243 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004244{
4245# ifdef HAVE_SETENV
4246 char envbuf[50];
4247# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004248 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004249 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004250 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004251 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004252 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004253# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004254 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004255# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004256# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004257 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004258# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004259# endif
4260
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004261# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004262 setenv("TERM", term, 1);
4263 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004264 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004265 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004266 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004267 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004268 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004269 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004270 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004271# ifdef FEAT_TERMINAL
4272 if (is_terminal)
4273 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004274 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004275 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4276 }
4277# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004278# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004279 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004280# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004281# else
4282 /*
4283 * Putenv does not copy the string, it has to remain valid.
4284 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004285 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004286 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004287 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4288 putenv(envbuf_Term);
4289 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004290 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004291 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4292 putenv(envbuf_Lines);
4293 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4294 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004295 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004296 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004297 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004298# ifdef FEAT_TERMINAL
4299 if (is_terminal)
4300 {
4301 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004302 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004303 putenv(envbuf_Version);
4304 }
4305# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004306# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004307 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4308 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4309 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004310# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004311# endif
4312}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004313
4314 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004315set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004316{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004317 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004318}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004319#endif
4320
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004321#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004322/*
4323 * Open a PTY, with FD for the master and slave side.
4324 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004325 * When successful both file descriptors are stored and the allocated pty name
4326 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004327 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004328 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004329open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004330{
4331 char *tty_name;
4332
Bram Moolenaar59386482019-02-10 22:43:46 +01004333 if (name1 != NULL)
4334 *name1 = NULL;
4335 if (name2 != NULL)
4336 *name2 = NULL;
4337
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004338 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004339 if (*pty_master_fd >= 0)
4340 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004341 // Leaving out O_NOCTTY may lead to waitpid() always returning
4342 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4343 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004344#ifdef O_NOCTTY
4345 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4346#else
4347 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4348#endif
4349 if (*pty_slave_fd < 0)
4350 {
4351 close(*pty_master_fd);
4352 *pty_master_fd = -1;
4353 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004354 else
4355 {
4356 if (name1 != NULL)
4357 *name1 = vim_strsave((char_u *)tty_name);
4358 if (name2 != NULL)
4359 *name2 = vim_strsave((char_u *)tty_name);
4360 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004361 }
4362}
4363#endif
4364
Bram Moolenaarfae42832017-08-01 22:24:26 +02004365/*
4366 * Send SIGINT to a child process if "c" is an interrupt character.
4367 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004368 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004369may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4370{
4371# ifdef SIGINT
4372 if (c == Ctrl_C || c == intr_char)
4373 {
4374# ifdef HAVE_SETSID
4375 kill(-pid, SIGINT);
4376# else
4377 kill(0, SIGINT);
4378# endif
4379 if (wpid > 0)
4380 kill(wpid, SIGINT);
4381 }
4382# endif
4383}
4384
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004385#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004386
Bram Moolenaar0f873732019-12-05 20:28:46 +01004387/*
4388 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4389 * pointers, the last one is NULL.
4390 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4391 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004392 int
4393unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004394 char_u *cmd,
4395 char ***argvp,
4396 char_u **sh_tofree,
4397 char_u **shcf_tofree)
4398{
4399 char **argv = NULL;
4400 int argc;
4401
4402 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004403 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004404 return FAIL;
4405
4406 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4407 return FAIL;
4408 *argvp = argv;
4409
4410 if (cmd != NULL)
4411 {
4412 char_u *s;
4413 char_u *p;
4414
4415 if (extra_shell_arg != NULL)
4416 argv[argc++] = (char *)extra_shell_arg;
4417
Bram Moolenaar0f873732019-12-05 20:28:46 +01004418 // Break 'shellcmdflag' into white separated parts. This doesn't
4419 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004420 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004421 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004422 return FAIL;
4423 s = *shcf_tofree;
4424 p = p_shcf;
4425 while (*p != NUL)
4426 {
4427 argv[argc++] = (char *)s;
4428 while (*p && *p != ' ' && *p != TAB)
4429 *s++ = *p++;
4430 *s++ = NUL;
4431 p = skipwhite(p);
4432 }
4433
4434 argv[argc++] = (char *)cmd;
4435 }
4436 argv[argc] = NULL;
4437 return OK;
4438}
4439#endif
4440
4441#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4442/*
4443 * Use a terminal window to run a shell command in.
4444 */
4445 static int
4446mch_call_shell_terminal(
4447 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004448 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004449{
4450 jobopt_T opt;
4451 char **argv = NULL;
4452 char_u *tofree1 = NULL;
4453 char_u *tofree2 = NULL;
4454 int retval = -1;
4455 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004456 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004457 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004458 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004459
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004460 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004461 goto theend;
4462
4463 init_job_options(&opt);
4464 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4465 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004466 if (buf == NULL)
4467 goto theend;
4468
4469 job = term_getjob(buf->b_term);
4470 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004471
Bram Moolenaar0f873732019-12-05 20:28:46 +01004472 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004473 aucmd_prepbuf(&aco, buf);
4474
4475 clear_oparg(&oa);
4476 while (term_use_loop())
4477 {
4478 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4479 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004480 // If terminal_loop() returns OK we got a key that is handled
4481 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004482 if (terminal_loop(TRUE) == OK)
4483 normal_cmd(&oa, TRUE);
4484 }
4485 else
4486 normal_cmd(&oa, TRUE);
4487 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004488 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004489 ch_log(NULL, "system command finished");
4490
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004491 job_unref(job);
4492
Bram Moolenaar0f873732019-12-05 20:28:46 +01004493 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004494 aucmd_restbuf(&aco);
4495
4496 wait_return(TRUE);
4497 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4498
4499theend:
4500 vim_free(argv);
4501 vim_free(tofree1);
4502 vim_free(tofree2);
4503 return retval;
4504}
4505#endif
4506
4507#ifdef USE_SYSTEM
4508/*
4509 * Use system() to start the shell: simple but slow.
4510 */
4511 static int
4512mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004513 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004514 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515{
4516#ifdef VMS
4517 char *ifn = NULL;
4518 char *ofn = NULL;
4519#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004520 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004521 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004522 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
4524 out_flush();
4525
4526 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004527 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
Bram Moolenaar62b42182010-09-21 22:09:37 +02004529# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004530 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004531 loose_clipboard();
4532# endif
4533
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 if (cmd == NULL)
4535 x = system((char *)p_sh);
4536 else
4537 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004538# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (ofn = strchr((char *)cmd, '>'))
4540 *ofn++ = '\0';
4541 if (ifn = strchr((char *)cmd, '<'))
4542 {
4543 char *p;
4544
4545 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004546 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (p)
4548 *p = '\0';
4549 }
4550 if (ofn)
4551 x = vms_sys((char *)cmd, ofn, ifn);
4552 else
4553 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004554# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004555 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004557 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 if (newcmd == NULL)
4559 x = 0;
4560 else
4561 {
4562 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4563 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4564 (char *)p_shcf,
4565 (char *)cmd);
4566 x = system((char *)newcmd);
4567 vim_free(newcmd);
4568 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 }
4571# ifdef VMS
4572 x = vms_sys_status(x);
4573# endif
4574 if (emsg_silent)
4575 ;
4576 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004577 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 else if (x && !(options & SHELL_SILENT))
4579 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004580 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 msg_outnum((long)x);
4582 msg_putchar('\n');
4583 }
4584
4585 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004586 {
4587 // The shell may have messed with the mode, always set it.
4588 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004589 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004592# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4593 restore_clipboard();
4594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004596}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
Bram Moolenaar0f873732019-12-05 20:28:46 +01004598#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599
Bram Moolenaar0f873732019-12-05 20:28:46 +01004600# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4601 // 127, some shells use that already
4602# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
Bram Moolenaar13568252018-03-16 20:46:58 +01004604/*
4605 * Don't use system(), use fork()/exec().
4606 */
4607 static int
4608mch_call_shell_fork(
4609 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004610 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004611{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004612 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004614 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 pid_t wait_pid = 0;
4616# ifdef HAVE_UNION_WAIT
4617 union wait status;
4618# else
4619 int status = -1;
4620# endif
4621 int retval = -1;
4622 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004623 char_u *tofree1 = NULL;
4624 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004626 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004627# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004629# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004630 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 int fd_fromshell[2];
4632 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004633 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634
4635 out_flush();
4636 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004637 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004638 if (tmode == TMODE_RAW)
4639 // The shell may have messed with the mode, always set it later.
4640 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004642 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004643 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004644
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004646 * For the GUI, when writing the output into the buffer and when reading
4647 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4648 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004650 if ((options & (SHELL_READ|SHELL_WRITE))
4651# ifdef FEAT_GUI
4652 || (gui.in_use && show_shell_mess)
4653# endif
4654 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004656# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 /*
4658 * Try to open a master pty.
4659 * If this works, open the slave pty.
4660 * If the slave can't be opened, close the master pty.
4661 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004662 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004663 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 /*
4665 * If not opening a pty or it didn't work, try using pipes.
4666 */
4667 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
4670 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004671 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 {
4673 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004674 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
4676 close(fd_toshell[0]);
4677 close(fd_toshell[1]);
4678 }
4679 }
4680 if (pipe_error)
4681 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004682 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 out_flush();
4684 }
4685 }
4686 }
4687
Bram Moolenaar0f873732019-12-05 20:28:46 +01004688 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004690 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004691 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004692 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004693 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004695 UNBLOCK_SIGNALS(&curset);
4696
Bram Moolenaar32526b32019-01-19 17:43:09 +01004697 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004698 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004700 || (gui.in_use && show_shell_mess)
4701# endif
4702 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004704# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004705 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
4707 close(pty_master_fd);
4708 close(pty_slave_fd);
4709 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004710 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004711# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
4713 close(fd_toshell[0]);
4714 close(fd_toshell[1]);
4715 close(fd_fromshell[0]);
4716 close(fd_fromshell[1]);
4717 }
4718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004720 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004722 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004723 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724
Bram Moolenaar819524702018-02-27 19:10:00 +01004725# ifdef FEAT_JOB_CHANNEL
4726 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004727 {
4728 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004729 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004730 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004731# endif
4732
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 if (!show_shell_mess || (options & SHELL_EXPAND))
4734 {
4735 int fd;
4736
4737 /*
4738 * Don't want to show any message from the shell. Can't just
4739 * close stdout and stderr though, because some systems will
4740 * break if you try to write to them after that, so we must
4741 * use dup() to replace them with something else -- webb
4742 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4743 * waiting for input.
4744 */
4745 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4746 fclose(stdin);
4747 fclose(stdout);
4748 fclose(stderr);
4749
4750 /*
4751 * If any of these open()'s and dup()'s fail, we just continue
4752 * anyway. It's not fatal, and on most systems it will make
4753 * no difference at all. On a few it will cause the execvp()
4754 * to exit with a non-zero status even when the completion
4755 * could be done, which is nothing too serious. If the open()
4756 * or dup() failed we'd just do the same thing ourselves
4757 * anyway -- webb
4758 */
4759 if (fd >= 0)
4760 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004761 vim_ignored = dup(fd); // To replace stdin (fd 0)
4762 vim_ignored = dup(fd); // To replace stdout (fd 1)
4763 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
Bram Moolenaar0f873732019-12-05 20:28:46 +01004765 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 close(fd);
4767 }
4768 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004769 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004771 || gui.in_use
4772# endif
4773 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
4775
Bram Moolenaardf177f62005-02-22 08:39:57 +00004776# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004777 // Create our own process group, so that the child and all its
4778 // children can be kill()ed. Don't do this when using pipes,
4779 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004780 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004781 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004782 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004783# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004784 // When doing "!xterm&" and 'shell' is bash: the shell
4785 // will exit and send SIGHUP to all processes in its
4786 // group, killing the just started process. Ignore SIGHUP
4787 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004788 signal(SIGHUP, SIG_IGN);
4789# endif
4790 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004791# endif
4792# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004793 if (pty_slave_fd >= 0)
4794 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004795 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004796 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004797 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004798# ifdef TIOCSCTTY
4799 // Try to become controlling tty (probably doesn't work,
4800 // unless run by root)
4801 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4802# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004803 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004804# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004805 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
Bram Moolenaara5792f52005-11-23 21:25:05 +00004807 /*
4808 * stderr is only redirected when using the GUI, so that a
4809 * program like gpg can still access the terminal to get a
4810 * passphrase using stderr.
4811 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004812# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 if (pty_master_fd >= 0)
4814 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004815 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816
Bram Moolenaar0f873732019-12-05 20:28:46 +01004817 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004819 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004821 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004822 if (gui.in_use)
4823 {
4824 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004825 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827
Bram Moolenaar0f873732019-12-05 20:28:46 +01004828 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 }
4830 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004833 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 close(fd_toshell[1]);
4835 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004836 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 close(fd_toshell[0]);
4838
Bram Moolenaar0f873732019-12-05 20:28:46 +01004839 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 close(fd_fromshell[0]);
4841 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004842 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 close(fd_fromshell[1]);
4844
Bram Moolenaara5792f52005-11-23 21:25:05 +00004845# ifdef FEAT_GUI
4846 if (gui.in_use)
4847 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004848 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004849 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004850 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004851 }
4852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004855
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 /*
4857 * There is no type cast for the argv, because the type may be
4858 * different on different machines. This may cause a warning
4859 * message with strict compilers, don't worry about it.
4860 * Call _exit() instead of exit() to avoid closing the connection
4861 * to the X server (esp. with GTK, which uses atexit()).
4862 */
4863 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004864 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004866 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
4868 /*
4869 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004870 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 */
4872 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004873 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004874 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004875# ifdef FEAT_JOB_CHANNEL
4876 ++dont_check_job_ended;
4877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 /*
4879 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004880 * This is also used to pipe stdin/stdout to/from the external
4881 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004883 if ((options & (SHELL_READ|SHELL_WRITE))
4884# ifdef FEAT_GUI
4885 || (gui.in_use && show_shell_mess)
4886# endif
4887 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004889# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004891 int buffer_off = 0; // valid bytes in buffer[]
4892 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4893 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 int len;
4895 int p_more_save;
4896 int old_State;
4897 int c;
4898 int toshell_fd;
4899 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004900 garray_T ga;
4901 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004902# ifdef ELAPSED_FUNC
4903 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905
Bram Moolenaardf177f62005-02-22 08:39:57 +00004906# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 if (pty_master_fd >= 0)
4908 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 fromshell_fd = pty_master_fd;
4910 toshell_fd = dup(pty_master_fd);
4911 }
4912 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
4915 close(fd_toshell[0]);
4916 close(fd_fromshell[1]);
4917 toshell_fd = fd_toshell[1];
4918 fromshell_fd = fd_fromshell[0];
4919 }
4920
4921 /*
4922 * Write to the child if there are typed characters.
4923 * Read from the child if there are characters available.
4924 * Repeat the reading a few times if more characters are
4925 * available. Need to check for typed keys now and then, but
4926 * not too often (delays when no chars are available).
4927 * This loop is quit if no characters can be read from the pty
4928 * (WaitForChar detected special condition), or there are no
4929 * characters available and the child has exited.
4930 * Only check if the child has exited when there is no more
4931 * output. The child may exit before all the output has
4932 * been printed.
4933 *
4934 * Currently this busy loops!
4935 * This can probably dead-lock when the write blocks!
4936 */
4937 p_more_save = p_more;
4938 p_more = FALSE;
4939 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004940 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004942 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004943 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004944 // Fork a process that will write the lines to the
4945 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004946 if ((wpid = fork()) == -1)
4947 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004948 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004949 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004950 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004951 {
4952 linenr_T lnum = curbuf->b_op_start.lnum;
4953 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004954 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004955 size_t l;
4956
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004957 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004958 for (;;)
4959 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004960 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004961 if (l == 0)
4962 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004963 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004964 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004965 len = write(toshell_fd, "", (size_t)1);
4966 else
4967 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004968 char_u *s = vim_strchr(lp + written, NL);
4969
Bram Moolenaar89d40322006-08-29 15:30:07 +00004970 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004971 s == NULL ? l
4972 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004973 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004974 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004975 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004976 // Finished a line, add a NL, unless this line
4977 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004978 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004979 || (!curbuf->b_p_bin
4980 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004981 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004982 && (lnum !=
4983 curbuf->b_ml.ml_line_count
4984 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004985 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004986 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004987 ++lnum;
4988 if (lnum > curbuf->b_op_end.lnum)
4989 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004990 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00004991 close(toshell_fd);
4992 toshell_fd = -1;
4993 break;
4994 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004995 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004996 written = 0;
4997 }
4998 else if (len > 0)
4999 written += len;
5000 }
5001 _exit(0);
5002 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005003 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005004 {
5005 close(toshell_fd);
5006 toshell_fd = -1;
5007 }
5008 }
5009
5010 if (options & SHELL_READ)
5011 ga_init2(&ga, 1, BUFLEN);
5012
5013 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005014# ifdef ELAPSED_FUNC
5015 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005016# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 for (;;)
5018 {
5019 /*
5020 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005021 * if there are any.
5022 * Don't do this if we are expanding wild cards (would eat
5023 * typeahead).
5024 * Don't do this when filtering and terminal is in cooked
5025 * mode, the shell command will handle the I/O. Avoids
5026 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005027 * Don't get characters when the child has already
5028 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005029 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005030 * while (noread_cnt > 4), avoids that ":r !ls" eats
5031 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 */
5033 len = 0;
5034 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005035 && ((options &
5036 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5037 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005038# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005039 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005040# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005041 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005042 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005043 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005045 if (ta_len == 0)
5046 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005047 // Get extra characters when we don't have any.
5048 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005049 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005050# ifdef ELAPSED_FUNC
5051 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005052# endif
5053 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5054 }
5055 if (ta_len > 0 || len > 0)
5056 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 /*
5058 * For pipes:
5059 * Check for CTRL-C: send interrupt signal to child.
5060 * Check for CTRL-D: EOF, close pipe to child.
5061 */
5062 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 /*
5065 * Send SIGINT to the child's group or all
5066 * processes in our group.
5067 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005068 may_send_sigint(ta_buf[ta_len], pid, wpid);
5069
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (pty_master_fd < 0 && toshell_fd >= 0
5071 && ta_buf[ta_len] == Ctrl_D)
5072 {
5073 close(toshell_fd);
5074 toshell_fd = -1;
5075 }
5076 }
5077
Bram Moolenaarf4140482020-02-15 23:06:45 +01005078 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079
5080 /*
5081 * For pipes: echo the typed characters.
5082 * For a pty this does not seem to work.
5083 */
5084 if (pty_master_fd < 0)
5085 {
5086 for (i = ta_len; i < ta_len + len; ++i)
5087 {
5088 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5089 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 else if (has_mbyte)
5091 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005092 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093
5094 msg_outtrans_len(ta_buf + i, l);
5095 i += l - 1;
5096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 else
5098 msg_outtrans_len(ta_buf + i, 1);
5099 }
5100 windgoto(msg_row, msg_col);
5101 out_flush();
5102 }
5103
5104 ta_len += len;
5105
5106 /*
5107 * Write the characters to the child, unless EOF has
5108 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005109 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005110 * When writing buffer lines, drop the typed
5111 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005113 if (options & SHELL_WRITE)
5114 ta_len = 0;
5115 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
5117 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5118 if (len > 0)
5119 {
5120 ta_len -= len;
5121 mch_memmove(ta_buf, ta_buf + len, ta_len);
5122 }
5123 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
5126
Bram Moolenaardf177f62005-02-22 08:39:57 +00005127 if (got_int)
5128 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005129 // CTRL-C sends a signal to the child, we ignore it
5130 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005131# ifdef HAVE_SETSID
5132 kill(-pid, SIGINT);
5133# else
5134 kill(0, SIGINT);
5135# endif
5136 if (wpid > 0)
5137 kill(wpid, SIGINT);
5138 got_int = FALSE;
5139 }
5140
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 /*
5142 * Check if the child has any characters to be printed.
5143 * Read them and write them to our window. Repeat this as
5144 * long as there is something to do, avoid the 10ms wait
5145 * for mch_inchar(), or sending typeahead characters to
5146 * the external process.
5147 * TODO: This should handle escape sequences, compatible
5148 * to some terminal (vt52?).
5149 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005150 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005151 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005153 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005156 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005158
5159 noread_cnt = 0;
5160 if (options & SHELL_READ)
5161 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005162 // Do NUL -> NL translation, append NL separated
5163 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005164 for (i = 0; i < len; ++i)
5165 {
5166 if (buffer[i] == NL)
5167 append_ga_line(&ga);
5168 else if (buffer[i] == NUL)
5169 ga_append(&ga, NL);
5170 else
5171 ga_append(&ga, buffer[i]);
5172 }
5173 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005174 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
5176 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005177 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178
Bram Moolenaardf177f62005-02-22 08:39:57 +00005179 len += buffer_off;
5180 buffer[len] = NUL;
5181
Bram Moolenaar0f873732019-12-05 20:28:46 +01005182 // Check if the last character in buffer[] is
5183 // incomplete, keep these bytes for the next
5184 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 for (p = buffer; p < buffer + len; p += l)
5186 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005187 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005189 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 else if (MB_BYTE2LEN(*p) != l)
5191 break;
5192 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005193 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005195 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 if (len >= 12)
5197 ++p;
5198 else
5199 {
5200 buffer_off = len;
5201 continue;
5202 }
5203 }
5204 c = *p;
5205 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005206 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if (p < buffer + len)
5208 {
5209 *p = c;
5210 buffer_off = (buffer + len) - p;
5211 mch_memmove(buffer, p, buffer_off);
5212 continue;
5213 }
5214 buffer_off = 0;
5215 }
5216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
5218 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005219 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
5221
5222 windgoto(msg_row, msg_col);
5223 cursor_on();
5224 out_flush();
5225 if (got_int)
5226 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005227
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005228# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005229 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005230 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005231 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005232
Bram Moolenaar0f873732019-12-05 20:28:46 +01005233 // Avoid that we keep looping here without
5234 // checking for a CTRL-C for a long time. Don't
5235 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005236 if (msec > 2000)
5237 {
5238 noread_cnt = 5;
5239 break;
5240 }
5241 }
5242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244
Bram Moolenaar0f873732019-12-05 20:28:46 +01005245 // If we already detected the child has finished, continue
5246 // reading output for a short while. Some text may be
5247 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005248 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005249 {
5250 if (noread_cnt < 5)
5251 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005252 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005253 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005254
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 /*
5256 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005257 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005259# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005260 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005261# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5265 || (wait_pid == pid && WIFEXITED(status)))
5266 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005267 // Don't break the loop yet, try reading more
5268 // characters from "fromshell_fd" first. When using
5269 // pipes there might still be something to read and
5270 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005273 else
5274 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005275
Bram Moolenaar95a51352013-03-21 22:53:50 +01005276# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005277 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005278 clip_update();
5279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
5281finished:
5282 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005283 if (options & SHELL_READ)
5284 {
5285 if (ga.ga_len > 0)
5286 {
5287 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005288 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005289 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005290 }
5291 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005292 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005293 ga_clear(&ga);
5294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 /*
5297 * Give all typeahead that wasn't used back to ui_inchar().
5298 */
5299 if (ta_len)
5300 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 State = old_State;
5302 if (toshell_fd >= 0)
5303 close(toshell_fd);
5304 close(fromshell_fd);
5305 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005306# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005307 else
5308 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005309 long delay_msec = 1;
5310
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005311 if (tmode == TMODE_RAW)
5312 // possibly disables modifyOtherKeys, so that the system
5313 // can recognize CTRL-C
5314 out_str(T_CTE);
Bram Moolenaar0981c872020-08-23 14:28:37 +02005315
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005316 /*
5317 * Similar to the loop above, but only handle X events, no
5318 * I/O.
5319 */
5320 for (;;)
5321 {
5322 if (got_int)
5323 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005324 // CTRL-C sends a signal to the child, we ignore it
5325 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005326# ifdef HAVE_SETSID
5327 kill(-pid, SIGINT);
5328# else
5329 kill(0, SIGINT);
5330# endif
5331 got_int = FALSE;
5332 }
5333# ifdef __NeXT__
5334 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5335# else
5336 wait_pid = waitpid(pid, &status, WNOHANG);
5337# endif
5338 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5339 || (wait_pid == pid && WIFEXITED(status)))
5340 {
5341 wait_pid = pid;
5342 break;
5343 }
5344
Bram Moolenaar0f873732019-12-05 20:28:46 +01005345 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005346 clip_update();
5347
Bram Moolenaar0f873732019-12-05 20:28:46 +01005348 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005349 // less time, gradually wait longer.
5350 mch_delay(delay_msec,
5351 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005352 if (++delay_msec > 10)
5353 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005354 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005355
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005356 if (tmode == TMODE_RAW)
5357 // possibly enables modifyOtherKeys again
5358 out_str(T_CTI);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005359 }
5360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361
5362 /*
5363 * Wait until our child has exited.
5364 * Ignore wait() returning pids of other children and returning
5365 * because of some signal like SIGWINCH.
5366 * Don't wait if wait_pid was already set above, indicating the
5367 * child already exited.
5368 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005369 if (wait_pid != pid)
5370 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
Bram Moolenaar624891f2010-10-13 16:22:09 +02005372# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005373 // Close slave side of pty. Only do this after the child has
5374 // exited, otherwise the child may hang when it tries to write on
5375 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005376 if (pty_master_fd >= 0)
5377 close(pty_slave_fd);
5378# endif
5379
Bram Moolenaar0f873732019-12-05 20:28:46 +01005380 // Make sure the child that writes to the external program is
5381 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005382 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005383 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005384 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005385 wait4pid(wpid, NULL);
5386 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005387
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005388# ifdef FEAT_JOB_CHANNEL
5389 --dont_check_job_ended;
5390# endif
5391
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 /*
5393 * Set to raw mode right now, otherwise a CTRL-C after
5394 * catch_signals() will kill Vim.
5395 */
5396 if (tmode == TMODE_RAW)
5397 settmode(TMODE_RAW);
5398 did_settmode = TRUE;
5399 set_signals();
5400
5401 if (WIFEXITED(status))
5402 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005403 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005405 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {
5407 if (retval == EXEC_FAILED)
5408 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005409 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 msg_outtrans(p_sh);
5411 msg_putchar('\n');
5412 }
5413 else if (!(options & SHELL_SILENT))
5414 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005415 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 msg_outnum((long)retval);
5417 msg_putchar('\n');
5418 }
5419 }
5420 }
5421 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005422 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 }
5424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425
5426error:
5427 if (!did_settmode)
5428 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005429 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005431 vim_free(argv);
5432 vim_free(tofree1);
5433 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434
5435 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005436}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005437#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005438
5439 int
5440mch_call_shell(
5441 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005442 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005443{
5444#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5445 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5446 return mch_call_shell_terminal(cmd, options);
5447#endif
5448#ifdef USE_SYSTEM
5449 return mch_call_shell_system(cmd, options);
5450#else
5451 return mch_call_shell_fork(cmd, options);
5452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453}
5454
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005455#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005456 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005457mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005458{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005459 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005460 int fd_in[2] = {-1, -1}; // for stdin
5461 int fd_out[2] = {-1, -1}; // for stdout
5462 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005463 int pty_master_fd = -1;
5464 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005465 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005466 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5467 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5468 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005469 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005470 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5471 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005472 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005473 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005474 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005475
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005476 if (use_out_for_err && use_null_for_out)
5477 use_null_for_err = TRUE;
5478
Bram Moolenaar0f873732019-12-05 20:28:46 +01005479 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005480 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005481
Bram Moolenaarb2412082017-08-20 18:09:14 +02005482 if (options->jo_pty
5483 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005484 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005485 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005486 open_pty(&pty_master_fd, &pty_slave_fd,
5487 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005488
Bram Moolenaar0f873732019-12-05 20:28:46 +01005489 // TODO: without the channel feature connect the child to /dev/null?
5490 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005491 if (use_file_for_in)
5492 {
5493 char_u *fname = options->jo_io_name[PART_IN];
5494
5495 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5496 if (fd_in[0] < 0)
5497 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005498 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005499 goto failed;
5500 }
5501 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005502 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005503 // When writing buffer lines to the input don't use the pty, so that
5504 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005505 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5506 && pipe(fd_in) < 0)
5507 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005508
5509 if (use_file_for_out)
5510 {
5511 char_u *fname = options->jo_io_name[PART_OUT];
5512
5513 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5514 if (fd_out[1] < 0)
5515 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005516 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005517 goto failed;
5518 }
5519 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005520 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005521 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005522
5523 if (use_file_for_err)
5524 {
5525 char_u *fname = options->jo_io_name[PART_ERR];
5526
5527 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5528 if (fd_err[1] < 0)
5529 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005530 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005531 goto failed;
5532 }
5533 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005534 else if (!use_out_for_err && !use_null_for_err
5535 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005536 goto failed;
5537
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005538 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5539 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005540 if (options->jo_set & JO_CHANNEL)
5541 {
5542 channel = options->jo_channel;
5543 if (channel != NULL)
5544 ++channel->ch_refcount;
5545 }
5546 else
5547 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005548 if (channel == NULL)
5549 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005550 if (job->jv_tty_out != NULL)
5551 ch_log(channel, "using pty %s on fd %d",
5552 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005553 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005554
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005555 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005556 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005557 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005558 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005559 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005560 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005561 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005562 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005563 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005564 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005565 int null_fd = -1;
5566 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005567
Bram Moolenaar0f873732019-12-05 20:28:46 +01005568 // child
5569 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005570 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005571
Bram Moolenaar819524702018-02-27 19:10:00 +01005572# ifdef FEAT_JOB_CHANNEL
5573 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005574 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005575 ch_logfile((char_u *)"", (char_u *)"");
5576# endif
5577
Bram Moolenaar835dc632016-02-07 14:27:38 +01005578# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005579 // Create our own process group, so that the child and all its
5580 // children can be kill()ed. Don't do this when using pipes,
5581 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005582 (void)setsid();
5583# endif
5584
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005585# ifdef FEAT_TERMINAL
5586 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005587 {
5588 char *term = (char *)T_NAME;
5589
5590#ifdef FEAT_GUI
5591 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005592 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005593 term = getenv("TERM");
5594#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005595 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005596 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005597 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005598 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005599 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005600 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005601 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005602 else if (t_colors > 16)
5603 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005604 else
5605 term = "xterm";
5606 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005607 set_child_environment(
5608 (long)options->jo_term_rows,
5609 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005610 term,
5611 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005612 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005613 else
5614# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005615 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005616
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005617 if (options->jo_env != NULL)
5618 {
5619 dict_T *dict = options->jo_env;
5620 hashitem_T *hi;
5621 int todo = (int)dict->dv_hashtab.ht_used;
5622
5623 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5624 if (!HASHITEM_EMPTY(hi))
5625 {
5626 typval_T *item = &dict_lookup(hi)->di_tv;
5627
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005628 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005629 --todo;
5630 }
5631 }
5632
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005633 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005634 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005635 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005636 if (null_fd < 0)
5637 {
5638 perror("opening /dev/null failed");
5639 _exit(OPEN_NULL_FAILED);
5640 }
5641 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005642
Bram Moolenaar223896d2017-08-02 22:33:28 +02005643 if (pty_slave_fd >= 0)
5644 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005645 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005646 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005647# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005648 // Try to become controlling tty (probably doesn't work,
5649 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005650 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5651# endif
5652 }
5653
Bram Moolenaar0f873732019-12-05 20:28:46 +01005654 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005655 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005656 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005657 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005658 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005659 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005660 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005661 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005662
Bram Moolenaar0f873732019-12-05 20:28:46 +01005663 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005664 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005665 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005666 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005667 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005668 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005669 }
5670 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005671 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005672 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005673 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005674 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005675 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005676
Bram Moolenaar0f873732019-12-05 20:28:46 +01005677 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005678 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005679 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005680 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005681 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005682 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005683 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005684 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005685
5686 if (fd_in[0] >= 0)
5687 close(fd_in[0]);
5688 if (fd_in[1] >= 0)
5689 close(fd_in[1]);
5690 if (fd_out[0] >= 0)
5691 close(fd_out[0]);
5692 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005693 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005694 if (fd_err[0] >= 0)
5695 close(fd_err[0]);
5696 if (fd_err[1] >= 0)
5697 close(fd_err[1]);
5698 if (pty_master_fd >= 0)
5699 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005700 close(pty_master_fd); // not used in the child
5701 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005702 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005703
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005704 if (null_fd >= 0)
5705 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005706
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005707 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5708 _exit(EXEC_FAILED);
5709
Bram Moolenaar0f873732019-12-05 20:28:46 +01005710 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005711 execvp(argv[0], argv);
5712
Bram Moolenaar4694a172016-04-21 14:05:23 +02005713 if (stderr_works)
5714 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005715# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005716 // calling free_all_mem() here causes problems. Ignore valgrind
5717 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005718# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005719 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005720 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005721
Bram Moolenaar0f873732019-12-05 20:28:46 +01005722 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005723 UNBLOCK_SIGNALS(&curset);
5724
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005725 job->jv_pid = pid;
5726 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005727 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005728
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005729 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005730 close(pty_slave_fd); // not used in the parent
5731 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005732 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005733 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005734 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005735 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005736 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005737 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005738 if (channel != NULL)
5739 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005740 int in_fd = INVALID_FD;
5741 int out_fd = INVALID_FD;
5742 int err_fd = INVALID_FD;
5743
5744 if (!(use_file_for_in || use_null_for_in))
5745 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5746
5747 if (!(use_file_for_out || use_null_for_out))
5748 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5749
5750 // When using pty_master_fd only set it for stdout, do not duplicate
5751 // it for stderr, it only needs to be read once.
5752 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5753 {
5754 if (fd_err[0] >= 0)
5755 err_fd = fd_err[0];
5756 else if (out_fd != pty_master_fd)
5757 err_fd = pty_master_fd;
5758 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005759
5760 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005761 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005762 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005763 else
5764 {
5765 if (fd_in[1] >= 0)
5766 close(fd_in[1]);
5767 if (fd_out[0] >= 0)
5768 close(fd_out[0]);
5769 if (fd_err[0] >= 0)
5770 close(fd_err[0]);
5771 if (pty_master_fd >= 0)
5772 close(pty_master_fd);
5773 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005774
Bram Moolenaar0f873732019-12-05 20:28:46 +01005775 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005776 return;
5777
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005778failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005779 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005780 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005781 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005782 if (fd_in[1] >= 0)
5783 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005784 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005785 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005786 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005787 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005788 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005789 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005790 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005791 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005792 if (pty_master_fd >= 0)
5793 close(pty_master_fd);
5794 if (pty_slave_fd >= 0)
5795 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005796}
5797
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005798 static char_u *
5799get_signal_name(int sig)
5800{
5801 int i;
5802 char_u numbuf[NUMBUFLEN];
5803
5804 if (sig == SIGKILL)
5805 return vim_strsave((char_u *)"kill");
5806
5807 for (i = 0; signal_info[i].sig != -1; i++)
5808 if (sig == signal_info[i].sig)
5809 return strlow_save((char_u *)signal_info[i].name);
5810
5811 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5812 return vim_strsave(numbuf);
5813}
5814
Bram Moolenaar835dc632016-02-07 14:27:38 +01005815 char *
5816mch_job_status(job_T *job)
5817{
5818# ifdef HAVE_UNION_WAIT
5819 union wait status;
5820# else
5821 int status = -1;
5822# endif
5823 pid_t wait_pid = 0;
5824
5825# ifdef __NeXT__
5826 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5827# else
5828 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5829# endif
5830 if (wait_pid == -1)
5831 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005832 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005833 if (job->jv_status < JOB_ENDED)
5834 ch_log(job->jv_channel, "Job no longer exists: %s",
5835 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005836 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005837 }
5838 if (wait_pid == 0)
5839 return "run";
5840 if (WIFEXITED(status))
5841 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005842 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005843 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005844 if (job->jv_status < JOB_ENDED)
5845 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005846 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005847 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005848 if (WIFSIGNALED(status))
5849 {
5850 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005851 job->jv_termsig = get_signal_name(WTERMSIG(status));
5852 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5853 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5854 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005855 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005856 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005857 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005858
5859return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005860 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005861 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005862 return "dead";
5863}
5864
5865 job_T *
5866mch_detect_ended_job(job_T *job_list)
5867{
5868# ifdef HAVE_UNION_WAIT
5869 union wait status;
5870# else
5871 int status = -1;
5872# endif
5873 pid_t wait_pid = 0;
5874 job_T *job;
5875
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005876# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005877 // Do not do this when waiting for a shell command to finish, we would get
5878 // the exit value here (and discard it), the exit value obtained there
5879 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005880 if (dont_check_job_ended > 0)
5881 return NULL;
5882# endif
5883
Bram Moolenaar97792de2016-10-15 18:36:49 +02005884# ifdef __NeXT__
5885 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5886# else
5887 wait_pid = waitpid(-1, &status, WNOHANG);
5888# endif
5889 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005890 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005891 return NULL;
5892 for (job = job_list; job != NULL; job = job->jv_next)
5893 {
5894 if (job->jv_pid == wait_pid)
5895 {
5896 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005897 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005898 job->jv_exitval = WEXITSTATUS(status);
5899 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005900 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005901 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005902 job->jv_termsig = get_signal_name(WTERMSIG(status));
5903 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005904 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005905 {
5906 ch_log(job->jv_channel, "Job ended");
5907 job->jv_status = JOB_ENDED;
5908 }
5909 return job;
5910 }
5911 }
5912 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005913}
5914
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005915/*
5916 * Send a (deadly) signal to "job".
5917 * Return FAIL if "how" is not a valid name.
5918 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005919 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005920mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005921{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005922 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005923
Bram Moolenaar923d9262016-02-25 20:56:01 +01005924 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005925 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005926 else if (STRCMP(how, "hup") == 0)
5927 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005928 else if (STRCMP(how, "quit") == 0)
5929 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005930 else if (STRCMP(how, "int") == 0)
5931 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005932 else if (STRCMP(how, "kill") == 0)
5933 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005934#ifdef SIGWINCH
5935 else if (STRCMP(how, "winch") == 0)
5936 sig = SIGWINCH;
5937#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005938 else if (isdigit(*how))
5939 sig = atoi((char *)how);
5940 else
5941 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005942
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005943 // Never kill ourselves!
5944 if (job->jv_pid != 0)
5945 {
5946 // TODO: have an option to only kill the process, not the group?
5947 kill(-job->jv_pid, sig);
5948 kill(job->jv_pid, sig);
5949 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005950
Bram Moolenaar835dc632016-02-07 14:27:38 +01005951 return OK;
5952}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005953
5954/*
5955 * Clear the data related to "job".
5956 */
5957 void
5958mch_clear_job(job_T *job)
5959{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005960 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005961# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005962 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005963# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005964 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005965# endif
5966}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005967#endif
5968
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005969#if defined(FEAT_TERMINAL) || defined(PROTO)
5970 int
5971mch_create_pty_channel(job_T *job, jobopt_T *options)
5972{
5973 int pty_master_fd = -1;
5974 int pty_slave_fd = -1;
5975 channel_T *channel;
5976
Bram Moolenaar59386482019-02-10 22:43:46 +01005977 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02005978 if (pty_master_fd < 0 || pty_slave_fd < 0)
5979 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005980 close(pty_slave_fd);
5981
5982 channel = add_channel();
5983 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005984 {
5985 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005986 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005987 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005988 if (job->jv_tty_out != NULL)
5989 ch_log(channel, "using pty %s on fd %d",
5990 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005991 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005992 channel->ch_keep_open = TRUE;
5993
Bram Moolenaar0f873732019-12-05 20:28:46 +01005994 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5995 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005996 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005997 channel_set_job(channel, job, options);
5998 return OK;
5999}
6000#endif
6001
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002/*
6003 * Check for CTRL-C typed by reading all available characters.
6004 * In cooked mode we should get SIGINT, no need to check.
6005 */
6006 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006007mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006009 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006010 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 fill_input_buf(FALSE);
6012}
6013
6014/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006015 * Wait "msec" msec until a character is available from the mouse, keyboard,
6016 * from inbuf[].
6017 * "msec" == -1 will block forever.
6018 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006019 * When "ignore_input" is TRUE even check for pending input when input is
6020 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006021 * "interrupted" (if not NULL) is set to TRUE when no character is available
6022 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006023 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006024 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 */
6026 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006027WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006029#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006030 return ui_wait_for_chars_or_timer(
6031 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006032#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006033 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006034#endif
6035}
6036
6037/*
6038 * Wait "msec" msec until a character is available from the mouse or keyboard
6039 * or from inbuf[].
6040 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006041 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006042 * "interrupted" (if not NULL) is set to TRUE when no character is available
6043 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006044 * When a GUI is being used, this will never get called -- webb
6045 */
6046 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006047WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006048{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049#ifdef FEAT_MOUSE_GPM
6050 int gpm_process_wanted;
6051#endif
6052#ifdef FEAT_XCLIPBOARD
6053 int rest;
6054#endif
6055 int avail;
6056
Bram Moolenaar0f873732019-12-05 20:28:46 +01006057 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 return 1;
6059
6060#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006061 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 if (WantQueryMouse)
6063 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006064 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006065 if (!no_query_mouse_for_testing)
6066 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 }
6068#endif
6069
6070 /*
6071 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6072 * events. This is a bit complicated, because they might both be defined.
6073 */
6074#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6075# ifdef FEAT_XCLIPBOARD
6076 rest = 0;
6077 if (do_xterm_trace())
6078 rest = msec;
6079# endif
6080 do
6081 {
6082# ifdef FEAT_XCLIPBOARD
6083 if (rest != 0)
6084 {
6085 msec = XT_TRACE_DELAY;
6086 if (rest >= 0 && rest < XT_TRACE_DELAY)
6087 msec = rest;
6088 if (rest >= 0)
6089 rest -= msec;
6090 }
6091# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006092# ifdef FEAT_SOUND_CANBERRA
6093 // Invoke any pending sound callbacks.
6094 if (has_sound_callback_in_queue())
6095 invoke_sound_callback();
6096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097# ifdef FEAT_MOUSE_GPM
6098 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006099 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006100 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006101 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006103 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006105# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006107 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 return 1;
6109# ifdef FEAT_XCLIPBOARD
6110 if (rest == 0 || !do_xterm_trace())
6111# endif
6112 break;
6113 }
6114 }
6115 while (FALSE
6116# ifdef FEAT_MOUSE_GPM
6117 || (gpm_process_wanted && mch_gpm_process() == 0)
6118# endif
6119# ifdef FEAT_XCLIPBOARD
6120 || (!avail && rest != 0)
6121# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006122 )
6123 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124
6125#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006126 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127#endif
6128 return avail;
6129}
6130
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006131#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132/*
6133 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006134 * "msec" == 0 will check for characters once.
6135 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006138 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006139 * "interrupted" (if not NULL) is set to TRUE when no character is available
6140 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006142 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006143RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
6145 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006146 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006147#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 static int busy = FALSE;
6149
Bram Moolenaar0f873732019-12-05 20:28:46 +01006150 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151# define MAY_LOOP
6152
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006153# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006154 // Remember at what time we started, so that we know how much longer we
6155 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006156 long start_msec = msec;
6157 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006159 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006160 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161# endif
6162
Bram Moolenaar0f873732019-12-05 20:28:46 +01006163 // Handle being called recursively. This may happen for the session
6164 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (busy)
6166 return 0;
6167#endif
6168
6169#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006170 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171#endif
6172 {
6173#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006174 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006175# ifdef FEAT_MZSCHEME
6176 int mzquantum_used = FALSE;
6177# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178#endif
6179#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006180 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006181 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 int nfd;
6183# ifdef FEAT_XCLIPBOARD
6184 int xterm_idx = -1;
6185# endif
6186# ifdef FEAT_MOUSE_GPM
6187 int gpm_idx = -1;
6188# endif
6189# ifdef USE_XSMP
6190 int xsmp_idx = -1;
6191# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006192 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006194# ifdef FEAT_MZSCHEME
6195 mzvim_check_threads();
6196 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6197 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006198 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006199 mzquantum_used = TRUE;
6200 }
6201# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 fds[0].fd = fd;
6203 fds[0].events = POLLIN;
6204 nfd = 1;
6205
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006207 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 if (xterm_Shell != (Widget)0)
6209 {
6210 xterm_idx = nfd;
6211 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6212 fds[nfd].events = POLLIN;
6213 nfd++;
6214 }
6215# endif
6216# ifdef FEAT_MOUSE_GPM
6217 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6218 {
6219 gpm_idx = nfd;
6220 fds[nfd].fd = gpm_fd;
6221 fds[nfd].events = POLLIN;
6222 nfd++;
6223 }
6224# endif
6225# ifdef USE_XSMP
6226 if (xsmp_icefd != -1)
6227 {
6228 xsmp_idx = nfd;
6229 fds[nfd].fd = xsmp_icefd;
6230 fds[nfd].events = POLLIN;
6231 nfd++;
6232 }
6233# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006234#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006235 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006236#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006237 if (interrupted != NULL)
6238 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006240 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006241
6242 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006243 if (result == 0 && interrupted != NULL && ret > 0)
6244 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006245
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006246# ifdef FEAT_MZSCHEME
6247 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006248 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006249 finished = FALSE;
6250# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252# ifdef FEAT_XCLIPBOARD
6253 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6254 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006255 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006257 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 finished = FALSE;
6259 }
6260# endif
6261# ifdef FEAT_MOUSE_GPM
6262 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264# endif
6265# ifdef USE_XSMP
6266 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6267 {
6268 if (fds[xsmp_idx].revents & POLLIN)
6269 {
6270 busy = TRUE;
6271 xsmp_handle_requests();
6272 busy = FALSE;
6273 }
6274 else if (fds[xsmp_idx].revents & POLLHUP)
6275 {
6276 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006277 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 xsmp_close();
6279 }
6280 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006281 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 }
6283# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006284#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006285 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006286 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006287 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289
Bram Moolenaar0f873732019-12-05 20:28:46 +01006290#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291
6292 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006293 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006294 // These are static because they can take 8 Kbyte each and cause the
6295 // signal stack to run out with -O3.
6296 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006298 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006300# ifdef FEAT_MZSCHEME
6301 mzvim_check_threads();
6302 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6303 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006304 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006305 mzquantum_used = TRUE;
6306 }
6307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006309 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006311 tv.tv_sec = towait / 1000;
6312 tv.tv_usec = (towait % 1000) * (1000000/1000);
6313 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006315 else
6316 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317
6318 /*
6319 * Select on ready for reading and exceptional condition (end of file).
6320 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006321select_eintr:
6322 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006323 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 FD_ZERO(&efds);
6325 FD_SET(fd, &rfds);
6326# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006327 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 FD_SET(fd, &efds);
6329# endif
6330 maxfd = fd;
6331
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006333 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 if (xterm_Shell != (Widget)0)
6335 {
6336 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6337 if (maxfd < ConnectionNumber(xterm_dpy))
6338 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006339
Bram Moolenaar0f873732019-12-05 20:28:46 +01006340 // An event may have already been read but not handled. In
6341 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006342 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 }
6344# endif
6345# ifdef FEAT_MOUSE_GPM
6346 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6347 {
6348 FD_SET(gpm_fd, &rfds);
6349 FD_SET(gpm_fd, &efds);
6350 if (maxfd < gpm_fd)
6351 maxfd = gpm_fd;
6352 }
6353# endif
6354# ifdef USE_XSMP
6355 if (xsmp_icefd != -1)
6356 {
6357 FD_SET(xsmp_icefd, &rfds);
6358 FD_SET(xsmp_icefd, &efds);
6359 if (maxfd < xsmp_icefd)
6360 maxfd = xsmp_icefd;
6361 }
6362# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006363# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006364 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006365# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006366 if (interrupted != NULL)
6367 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368
Bram Moolenaar643b6142018-09-12 20:29:09 +02006369 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6370 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006371 result = ret > 0 && FD_ISSET(fd, &rfds);
6372 if (result)
6373 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006374 else if (interrupted != NULL && ret > 0)
6375 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006376
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006377# ifdef EINTR
6378 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006379 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006380 // Check whether window has been resized, EINTR may be caused by
6381 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006382 if (do_resize)
6383 handle_resize();
6384
Bram Moolenaar0f873732019-12-05 20:28:46 +01006385 // Interrupted by a signal, need to try again. We ignore msec
6386 // here, because we do want to check even after a timeout if
6387 // characters are available. Needed for reading output of an
6388 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006389 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006390 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006391# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006392# ifdef __TANDEM
6393 if (ret == -1 && errno == ENOTSUP)
6394 {
6395 FD_ZERO(&rfds);
6396 FD_ZERO(&efds);
6397 ret = 0;
6398 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006399# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006400# ifdef FEAT_MZSCHEME
6401 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006402 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006403 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404# endif
6405
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406# ifdef FEAT_XCLIPBOARD
6407 if (ret > 0 && xterm_Shell != (Widget)0
6408 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6409 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006410 xterm_update(); // Maybe we should hand out clipboard
6411 // continue looping when we only got the X event and the input
6412 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 if (--ret == 0 && !input_available())
6414 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006415 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 finished = FALSE;
6417 }
6418 }
6419# endif
6420# ifdef FEAT_MOUSE_GPM
6421 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6422 {
6423 if (FD_ISSET(gpm_fd, &efds))
6424 gpm_close();
6425 else if (FD_ISSET(gpm_fd, &rfds))
6426 *check_for_gpm = 1;
6427 }
6428# endif
6429# ifdef USE_XSMP
6430 if (ret > 0 && xsmp_icefd != -1)
6431 {
6432 if (FD_ISSET(xsmp_icefd, &efds))
6433 {
6434 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006435 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 xsmp_close();
6437 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006438 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 }
6440 else if (FD_ISSET(xsmp_icefd, &rfds))
6441 {
6442 busy = TRUE;
6443 xsmp_handle_requests();
6444 busy = FALSE;
6445 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006446 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 }
6448 }
6449# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006450#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006451 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006452 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006453 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455
Bram Moolenaar0f873732019-12-05 20:28:46 +01006456#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457
6458#ifdef MAY_LOOP
6459 if (finished || msec == 0)
6460 break;
6461
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006462# ifdef FEAT_CLIENTSERVER
6463 if (server_waiting())
6464 break;
6465# endif
6466
Bram Moolenaar0f873732019-12-05 20:28:46 +01006467 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 if (msec > 0)
6469 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006470# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006471 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006472 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006474 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 msec = msec / 2;
6476# endif
6477 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006478 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 }
6480#endif
6481 }
6482
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006483 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484}
6485
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006487 * Expand a path into all matching files and/or directories. Handles "*",
6488 * "?", "[a-z]", "**", etc.
6489 * "path" has backslashes before chars that are not to be expanded.
6490 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 */
6492 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006493mch_expandpath(
6494 garray_T *gap,
6495 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006496 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497{
Bram Moolenaar02743632005-07-25 20:42:36 +00006498 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
6501/*
6502 * mch_expand_wildcards() - this code does wild-card pattern matching using
6503 * the shell
6504 *
6505 * return OK for success, FAIL for error (you may lose some memory) and put
6506 * an error message in *file.
6507 *
6508 * num_pat is number of input patterns
6509 * pat is array of pointers to input patterns
6510 * num_file is pointer to number of matched file names
6511 * file is pointer to array of pointers to matched file names
6512 */
6513
6514#ifndef SEEK_SET
6515# define SEEK_SET 0
6516#endif
6517#ifndef SEEK_END
6518# define SEEK_END 2
6519#endif
6520
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006521#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006522
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006524mch_expand_wildcards(
6525 int num_pat,
6526 char_u **pat,
6527 int *num_file,
6528 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006529 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530{
6531 int i;
6532 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006533 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 char_u *p;
6535 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536
Bram Moolenaarc7247912008-01-13 12:54:11 +00006537 /*
6538 * This is the non-OS/2 implementation (really Unix).
6539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 int j;
6541 char_u *tempname;
6542 char_u *command;
6543 FILE *fd;
6544 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006545#define STYLE_ECHO 0 // use "echo", the default
6546#define STYLE_GLOB 1 // use "glob", for csh
6547#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6548#define STYLE_PRINT 3 // use "print -N", for zsh
6549#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6550 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 int shell_style = STYLE_ECHO;
6552 int check_spaces;
6553 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006554 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006555 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006556 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557
Bram Moolenaar0f873732019-12-05 20:28:46 +01006558 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 *file = NULL;
6560
6561 /*
6562 * If there are no wildcards, just copy the names to allocated memory.
6563 * Saves a lot of time, because we don't have to start a new shell.
6564 */
6565 if (!have_wildcard(num_pat, pat))
6566 return save_patterns(num_pat, pat, num_file, file);
6567
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006568# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006569 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006570 if (sandbox != 0 && check_secure())
6571 return FAIL;
6572# endif
6573
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 /*
6575 * Don't allow the use of backticks in secure and restricted mode.
6576 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006577 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 for (i = 0; i < num_pat; ++i)
6579 if (vim_strchr(pat[i], '`') != NULL
6580 && (check_restricted() || check_secure()))
6581 return FAIL;
6582
6583 /*
6584 * get a name for the temp file
6585 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006586 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006588 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 return FAIL;
6590 }
6591
6592 /*
6593 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006594 * file.
6595 * STYLE_BT: NL separated
6596 * If expanding `cmd` execute it directly.
6597 * STYLE_GLOB: NUL separated
6598 * If we use *csh, "glob" will work better than "echo".
6599 * STYLE_PRINT: NL or NUL separated
6600 * If we use *zsh, "print -N" will work better than "glob".
6601 * STYLE_VIMGLOB: NL separated
6602 * If we use *sh*, we define "vimglob()".
6603 * STYLE_ECHO: space separated.
6604 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 */
6606 if (num_pat == 1 && *pat[0] == '`'
6607 && (len = STRLEN(pat[0])) > 2
6608 && *(pat[0] + len - 1) == '`')
6609 shell_style = STYLE_BT;
6610 else if ((len = STRLEN(p_sh)) >= 3)
6611 {
6612 if (STRCMP(p_sh + len - 3, "csh") == 0)
6613 shell_style = STYLE_GLOB;
6614 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6615 shell_style = STYLE_PRINT;
6616 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006617 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6618 "sh") != NULL)
6619 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620
Bram Moolenaar0f873732019-12-05 20:28:46 +01006621 // Compute the length of the command. We need 2 extra bytes: for the
6622 // optional '&' and for the NUL.
6623 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006625 if (shell_style == STYLE_VIMGLOB)
6626 len += STRLEN(sh_vimglob_func);
6627
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006628 for (i = 0; i < num_pat; ++i)
6629 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006630 // Count the length of the patterns in the same way as they are put in
6631 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006632#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006633 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006634#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006635 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006636 for (j = 0; pat[i][j] != NUL; ++j)
6637 {
6638 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006639 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006640 ++len;
6641 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006642#endif
6643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 command = alloc(len);
6645 if (command == NULL)
6646 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006647 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 vim_free(tempname);
6649 return FAIL;
6650 }
6651
6652 /*
6653 * Build the shell command:
6654 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6655 * recognizes this).
6656 * - Add the shell command to print the expanded names.
6657 * - Add the temp file name.
6658 * - Add the file name patterns.
6659 */
6660 if (shell_style == STYLE_BT)
6661 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006662 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006663 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006664 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006666 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006667 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006669 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006671 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 *p = ' ';
6673 }
6674 STRCAT(command, ">");
6675 }
6676 else
6677 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006678 STRCPY(command, "");
6679 if (shell_style == STYLE_GLOB)
6680 {
6681 // Assume the nonomatch option is valid only for csh like shells,
6682 // otherwise, this may set the positional parameters for the shell,
6683 // e.g. "$*".
6684 if (flags & EW_NOTFOUND)
6685 STRCAT(command, "set nonomatch; ");
6686 else
6687 STRCAT(command, "unset nonomatch; ");
6688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 if (shell_style == STYLE_GLOB)
6690 STRCAT(command, "glob >");
6691 else if (shell_style == STYLE_PRINT)
6692 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006693 else if (shell_style == STYLE_VIMGLOB)
6694 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 else
6696 STRCAT(command, "echo >");
6697 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006698
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006700
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 if (shell_style != STYLE_BT)
6702 for (i = 0; i < num_pat; ++i)
6703 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006704 // When using system() always add extra quotes, because the shell
6705 // is started twice. Otherwise put a backslash before special
6706 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707#ifdef USE_SYSTEM
6708 STRCAT(command, " \"");
6709 STRCAT(command, pat[i]);
6710 STRCAT(command, "\"");
6711#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006712 int intick = FALSE;
6713
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 p = command + STRLEN(command);
6715 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006716 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006717 {
6718 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006719 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006720 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6721 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006722 // Remove a backslash, take char literally. But keep
6723 // backslash inside backticks, before a special character
6724 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006725 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006726 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6727 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006728 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006729 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006730 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006731 else if (!intick
6732 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6733 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006734 // Put a backslash before a special character, but not
6735 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6736 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006737 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006738
Bram Moolenaar0f873732019-12-05 20:28:46 +01006739 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006740 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 *p = NUL;
6743#endif
6744 }
6745 if (flags & EW_SILENT)
6746 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006747 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006748 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749
6750 /*
6751 * Using zsh -G: If a pattern has no matches, it is just deleted from
6752 * the argument list, otherwise zsh gives an error message and doesn't
6753 * expand any other pattern.
6754 */
6755 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006756 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
6758 /*
6759 * If we use -f then shell variables set in .cshrc won't get expanded.
6760 * vi can do it, so we will too, but it is only necessary if there is a "$"
6761 * in one of the patterns, otherwise we can still use the fast option.
6762 */
6763 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006764 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
6766 /*
6767 * execute the shell command
6768 */
6769 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6770
Bram Moolenaar0f873732019-12-05 20:28:46 +01006771 // When running in the background, give it some time to create the temp
6772 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006773 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006774 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
Bram Moolenaar0f873732019-12-05 20:28:46 +01006776 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 show_shell_mess = TRUE;
6778 vim_free(command);
6779
Bram Moolenaar0f873732019-12-05 20:28:46 +01006780 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 {
6782 mch_remove(tempname);
6783 vim_free(tempname);
6784 /*
6785 * With interactive completion, the error message is not printed.
6786 * However with USE_SYSTEM, I don't know how to turn off error messages
6787 * from the shell, so screen may still get messed up -- webb.
6788 */
6789#ifndef USE_SYSTEM
6790 if (!(flags & EW_SILENT))
6791#endif
6792 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006793 redraw_later_clear(); // probably messed up screen
6794 msg_putchar('\n'); // clear bottom line quickly
6795 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796#ifdef USE_SYSTEM
6797 if (!(flags & EW_SILENT))
6798#endif
6799 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006800 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006801 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
6803 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006804 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6805 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (shell_style == STYLE_BT)
6807 return FAIL;
6808 goto notfound;
6809 }
6810
6811 /*
6812 * read the names from the file into memory
6813 */
6814 fd = fopen((char *)tempname, READBIN);
6815 if (fd == NULL)
6816 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006817 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 if (!(flags & EW_SILENT))
6819 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006820 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006821 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 }
6823 vim_free(tempname);
6824 goto notfound;
6825 }
6826 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006827 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006829 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006830 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006831 buffer = NULL;
6832 else
6833 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 if (buffer == NULL)
6835 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006836 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 mch_remove(tempname);
6838 vim_free(tempname);
6839 fclose(fd);
6840 return FAIL;
6841 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006842 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 i = fread((char *)buffer, 1, len, fd);
6844 fclose(fd);
6845 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006846 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006848 // unexpected read error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006849 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 vim_free(tempname);
6851 vim_free(buffer);
6852 return FAIL;
6853 }
6854 vim_free(tempname);
6855
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006856# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006857 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006859 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6861 *p++ = buffer[i];
6862 len = p - buffer;
6863# endif
6864
6865
Bram Moolenaar0f873732019-12-05 20:28:46 +01006866 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 if (shell_style == STYLE_ECHO)
6868 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006869 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006871 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {
6873 while (*p != ' ' && *p != '\n')
6874 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006875 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 }
6877 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006878 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006879 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006881 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006883 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {
6885 while (*p != '\n' && *p != NUL)
6886 ++p;
6887 if (*p != NUL)
6888 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006889 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 }
6891 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006892 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 else
6894 {
6895 /*
6896 * Some versions of zsh use spaces instead of NULs to separate
6897 * results. Only do this when there is no NUL before the end of the
6898 * buffer, otherwise we would never be able to use file names with
6899 * embedded spaces when zsh does use NULs.
6900 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6901 * don't check for spaces again.
6902 */
6903 check_spaces = FALSE;
6904 if (shell_style == STYLE_PRINT && !did_find_nul)
6905 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006906 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006907 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006908 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 did_find_nul = TRUE;
6910 else
6911 check_spaces = TRUE;
6912 }
6913
6914 /*
6915 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6916 * already is one, for STYLE_GLOB it needs to be added.
6917 */
6918 if (len && buffer[len - 1] == NUL)
6919 --len;
6920 else
6921 buffer[len] = NUL;
6922 i = 0;
6923 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006924 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {
6926 ++i;
6927 *p = NUL;
6928 }
6929 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006930 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 }
6932 if (i == 0)
6933 {
6934 /*
6935 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6936 * /bin/sh will happily expand it to nothing rather than returning an
6937 * error; and hey, it's good to check anyway -- webb.
6938 */
6939 vim_free(buffer);
6940 goto notfound;
6941 }
6942 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006943 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 if (*file == NULL)
6945 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006946 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 vim_free(buffer);
6948 return FAIL;
6949 }
6950
6951 /*
6952 * Isolate the individual file names.
6953 */
6954 p = buffer;
6955 for (i = 0; i < *num_file; ++i)
6956 {
6957 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006958 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006959 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6960 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006962 while (!(shell_style == STYLE_ECHO && *p == ' ')
6963 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006965 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 *p = NUL;
6967 else
6968 {
6969 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006970 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 }
6972 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006973 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006975 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006977 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 }
6979 }
6980
6981 /*
6982 * Move the file names to allocated memory.
6983 */
6984 for (j = 0, i = 0; i < *num_file; ++i)
6985 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006986 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6988 continue;
6989
Bram Moolenaar0f873732019-12-05 20:28:46 +01006990 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 dir = (mch_isdir((*file)[i]));
6992 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6993 continue;
6994
Bram Moolenaar0f873732019-12-05 20:28:46 +01006995 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01006996 if (!dir && (flags & EW_EXEC)
6997 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006998 continue;
6999
Bram Moolenaar964b3742019-05-24 18:54:09 +02007000 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 if (p)
7002 {
7003 STRCPY(p, (*file)[i]);
7004 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007005 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 (*file)[j++] = p;
7007 }
7008 }
7009 vim_free(buffer);
7010 *num_file = j;
7011
Bram Moolenaar0f873732019-12-05 20:28:46 +01007012 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007014 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 goto notfound;
7016 }
7017
7018 return OK;
7019
7020notfound:
7021 if (flags & EW_NOTFOUND)
7022 return save_patterns(num_pat, pat, num_file, file);
7023 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024}
7025
Bram Moolenaar0f873732019-12-05 20:28:46 +01007026#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007029save_patterns(
7030 int num_pat,
7031 char_u **pat,
7032 int *num_file,
7033 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
7035 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007036 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007038 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 if (*file == NULL)
7040 return FAIL;
7041 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007042 {
7043 s = vim_strsave(pat[i]);
7044 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007045 // Be compatible with expand_filename(): halve the number of
7046 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007047 backslash_halve(s);
7048 (*file)[i] = s;
7049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 *num_file = num_pat;
7051 return OK;
7052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054/*
7055 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7056 * expand.
7057 */
7058 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007059mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007061 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 if (*p == '\\' && p[1] != NUL)
7064 ++p;
7065 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 if (vim_strchr((char_u *)
7067#ifdef VMS
7068 "*?%"
7069#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071#endif
7072 , *p) != NULL)
7073 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 }
7075 return FALSE;
7076}
7077
7078/*
7079 * Return TRUE if the string "p" contains a wildcard.
7080 * Don't recognize '~' at the end as a wildcard.
7081 */
7082 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007083mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007085 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 if (*p == '\\' && p[1] != NUL)
7088 ++p;
7089 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 if (vim_strchr((char_u *)
7091#ifdef VMS
7092 "*?%$"
7093#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095#endif
7096 , *p) != NULL
7097 || (*p == '~' && p[1] != NUL))
7098 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 }
7100 return FALSE;
7101}
7102
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007104have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
7106 int i;
7107
7108 for (i = 0; i < num; i++)
7109 if (mch_has_wildcard(file[i]))
7110 return 1;
7111 return 0;
7112}
7113
7114 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007115have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116{
7117 int i;
7118
7119 for (i = 0; i < num; i++)
7120 if (vim_strchr(file[i], '$') != NULL)
7121 return TRUE;
7122 return FALSE;
7123}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007125#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126/*
7127 * Scaled-down version of rename(), which is missing in Xenix.
7128 * This version can only move regular files and will fail if the
7129 * destination exists.
7130 */
7131 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007132mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133{
7134 struct stat st;
7135
Bram Moolenaar0f873732019-12-05 20:28:46 +01007136 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007138 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007140 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 return 0;
7142 return -1;
7143}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007144#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007146#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147/*
7148 * Initializes connection with gpm (if it isn't already opened)
7149 * Return 1 if succeeded (or connection already opened), 0 if failed
7150 */
7151 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007152gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007154 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155
7156 if (!gpm_flag)
7157 {
7158 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7159 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007160 // Default handling for mouse move
7161 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 gpm_connect.maxMod = 0xffff;
7163 if (Gpm_Open(&gpm_connect, 0) > 0)
7164 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007165 // gpm library tries to handling TSTP causes
7166 // problems. Anyways, we close connection to Gpm whenever
7167 // we are going to suspend or starting an external process
7168 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007169# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007171# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007172 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 }
7174 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007175 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 return 0;
7177 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007178 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179}
7180
7181/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007182 * Returns TRUE if the GPM mouse is enabled.
7183 */
7184 int
7185gpm_enabled(void)
7186{
7187 return gpm_flag && gpm_fd >= 0;
7188}
7189
7190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 */
7193 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007194gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007196 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 Gpm_Close();
7198}
7199
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007200/*
7201 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007203 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 */
7205 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007206mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207{
7208 int button;
7209 static Gpm_Event gpm_event;
7210 char_u string[6];
7211 int_u vim_modifiers;
7212 int row,col;
7213 unsigned char buttons_mask;
7214 unsigned char gpm_modifiers;
7215 static unsigned char old_buttons = 0;
7216
7217 Gpm_GetEvent(&gpm_event);
7218
7219#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007220 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 if (hold_gui_events)
7222 return 0;
7223#endif
7224
7225 row = gpm_event.y - 1;
7226 col = gpm_event.x - 1;
7227
Bram Moolenaar0f873732019-12-05 20:28:46 +01007228 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 string[1] = 'M';
7230 string[2] = 'G';
7231 switch (GPM_BARE_EVENTS(gpm_event.type))
7232 {
7233 case GPM_DRAG:
7234 string[3] = MOUSE_DRAG;
7235 break;
7236 case GPM_DOWN:
7237 buttons_mask = gpm_event.buttons & ~old_buttons;
7238 old_buttons = gpm_event.buttons;
7239 switch (buttons_mask)
7240 {
7241 case GPM_B_LEFT:
7242 button = MOUSE_LEFT;
7243 break;
7244 case GPM_B_MIDDLE:
7245 button = MOUSE_MIDDLE;
7246 break;
7247 case GPM_B_RIGHT:
7248 button = MOUSE_RIGHT;
7249 break;
7250 default:
7251 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007252 // Don't know what to do. Can more than one button be
7253 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 }
7255 string[3] = (char_u)(button | 0x20);
7256 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7257 break;
7258 case GPM_UP:
7259 string[3] = MOUSE_RELEASE;
7260 old_buttons &= ~gpm_event.buttons;
7261 break;
7262 default:
7263 return 0;
7264 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007265 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 gpm_modifiers = gpm_event.modifiers;
7267 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007268 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7269 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7270 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7272 vim_modifiers |= MOUSE_SHIFT;
7273
7274 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7275 vim_modifiers |= MOUSE_CTRL;
7276 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7277 vim_modifiers |= MOUSE_ALT;
7278 string[3] |= vim_modifiers;
7279 string[4] = (char_u)(col + ' ' + 1);
7280 string[5] = (char_u)(row + ' ' + 1);
7281 add_to_input_buf(string, 6);
7282 return 6;
7283}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007284#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007286#ifdef FEAT_SYSMOUSE
7287/*
7288 * Initialize connection with sysmouse.
7289 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7290 * output, any sysmouse output than will be processed via sig_sysmouse().
7291 * Return OK if succeeded, FAIL if failed.
7292 */
7293 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007294sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007295{
7296 struct mouse_info mouse;
7297
7298 mouse.operation = MOUSE_MODE;
7299 mouse.u.mode.mode = 0;
7300 mouse.u.mode.signal = SIGUSR2;
7301 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7302 {
7303 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7304 mouse.operation = MOUSE_SHOW;
7305 ioctl(1, CONS_MOUSECTL, &mouse);
7306 return OK;
7307 }
7308 return FAIL;
7309}
7310
7311/*
7312 * Stop processing SIGUSR2 signals, and also make sure that
7313 * virtual console do not send us any sysmouse related signal.
7314 */
7315 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007316sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007317{
7318 struct mouse_info mouse;
7319
7320 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7321 mouse.operation = MOUSE_MODE;
7322 mouse.u.mode.mode = 0;
7323 mouse.u.mode.signal = 0;
7324 ioctl(1, CONS_MOUSECTL, &mouse);
7325}
7326
7327/*
7328 * Gets info from sysmouse and adds special keys to input buf.
7329 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007330 static RETSIGTYPE
7331sig_sysmouse SIGDEFARG(sigarg)
7332{
7333 struct mouse_info mouse;
7334 struct video_info video;
7335 char_u string[6];
7336 int row, col;
7337 int button;
7338 int buttons;
7339 static int oldbuttons = 0;
7340
7341#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007342 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007343 if (hold_gui_events)
7344 return;
7345#endif
7346
7347 mouse.operation = MOUSE_GETINFO;
7348 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7349 && ioctl(1, FBIO_MODEINFO, &video) != -1
7350 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7351 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7352 {
7353 row = mouse.u.data.y / video.vi_cheight;
7354 col = mouse.u.data.x / video.vi_cwidth;
7355 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007356 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007357 string[1] = 'M';
7358 string[2] = 'S';
7359 if (oldbuttons == buttons && buttons != 0)
7360 {
7361 button = MOUSE_DRAG;
7362 }
7363 else
7364 {
7365 switch (buttons)
7366 {
7367 case 0:
7368 button = MOUSE_RELEASE;
7369 break;
7370 case 1:
7371 button = MOUSE_LEFT;
7372 break;
7373 case 2:
7374 button = MOUSE_MIDDLE;
7375 break;
7376 case 4:
7377 button = MOUSE_RIGHT;
7378 break;
7379 default:
7380 return;
7381 }
7382 oldbuttons = buttons;
7383 }
7384 string[3] = (char_u)(button);
7385 string[4] = (char_u)(col + ' ' + 1);
7386 string[5] = (char_u)(row + ' ' + 1);
7387 add_to_input_buf(string, 6);
7388 }
7389 return;
7390}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007391#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007392
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007394typedef char_u * (*STRPROCSTR)(char_u *);
7395typedef char_u * (*INTPROCSTR)(int);
7396typedef int (*STRPROCINT)(char_u *);
7397typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398
7399/*
7400 * Call a DLL routine which takes either a string or int param
7401 * and returns an allocated string.
7402 */
7403 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007404mch_libcall(
7405 char_u *libname,
7406 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007407 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007408 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007409 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007410 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411{
7412# if defined(USE_DLOPEN)
7413 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007414 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415# else
7416 shl_t hinstLib;
7417# endif
7418 STRPROCSTR ProcAdd;
7419 INTPROCSTR ProcAddI;
7420 char_u *retval_str = NULL;
7421 int retval_int = 0;
7422 int success = FALSE;
7423
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007424 /*
7425 * Get a handle to the DLL module.
7426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007428 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007429 (void)dlerror();
7430
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 hinstLib = dlopen((char *)libname, RTLD_LAZY
7432# ifdef RTLD_LOCAL
7433 | RTLD_LOCAL
7434# endif
7435 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007436 if (hinstLib == NULL)
7437 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007438 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007439 dlerr = (char *)dlerror();
7440 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007441 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443# else
7444 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7445# endif
7446
Bram Moolenaar0f873732019-12-05 20:28:46 +01007447 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 if (hinstLib != NULL)
7449 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007450# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 /*
7452 * Catch a crash when calling the library function. For example when
7453 * using a number where a string pointer is expected.
7454 */
7455 mch_startjmp();
7456 if (SETJMP(lc_jump_env) != 0)
7457 {
7458 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007459# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007460 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007461# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 mch_didjmp();
7463 }
7464 else
7465# endif
7466 {
7467 retval_str = NULL;
7468 retval_int = 0;
7469
7470 if (argstring != NULL)
7471 {
7472# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007473 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007474 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475# else
7476 if (shl_findsym(&hinstLib, (const char *)funcname,
7477 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7478 ProcAdd = NULL;
7479# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007480 if ((success = (ProcAdd != NULL
7481# if defined(USE_DLOPEN)
7482 && dlerr == NULL
7483# endif
7484 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 {
7486 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007487 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 else
7489 retval_str = (ProcAdd)(argstring);
7490 }
7491 }
7492 else
7493 {
7494# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007495 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007496 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497# else
7498 if (shl_findsym(&hinstLib, (const char *)funcname,
7499 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7500 ProcAddI = NULL;
7501# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007502 if ((success = (ProcAddI != NULL
7503# if defined(USE_DLOPEN)
7504 && dlerr == NULL
7505# endif
7506 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 {
7508 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007509 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 else
7511 retval_str = (ProcAddI)(argint);
7512 }
7513 }
7514
Bram Moolenaar0f873732019-12-05 20:28:46 +01007515 // Save the string before we free the library.
7516 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 if (string_result == NULL)
7518 *number_result = retval_int;
7519 else if (retval_str != NULL
7520 && retval_str != (char_u *)1
7521 && retval_str != (char_u *)-1)
7522 *string_result = vim_strsave(retval_str);
7523 }
7524
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007525# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 mch_endjmp();
7527# ifdef SIGHASARG
7528 if (lc_signal != 0)
7529 {
7530 int i;
7531
Bram Moolenaar0f873732019-12-05 20:28:46 +01007532 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 for (i = 0; signal_info[i].sig != -1; i++)
7534 if (lc_signal == signal_info[i].sig)
7535 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007536 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 }
7538# endif
7539# endif
7540
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007542 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007543 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007544 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007545
Bram Moolenaar0f873732019-12-05 20:28:46 +01007546 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 (void)dlclose(hinstLib);
7548# else
7549 (void)shl_unload(hinstLib);
7550# endif
7551 }
7552
7553 if (!success)
7554 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007555 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 return FAIL;
7557 }
7558
7559 return OK;
7560}
7561#endif
7562
7563#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007564static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565static int xterm_button;
7566
7567/*
7568 * Setup a dummy window for X selections in a terminal.
7569 */
7570 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007571setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572{
7573 int z = 0;
7574 char *strp = "";
7575 Widget AppShell;
7576
7577 if (!x_connect_to_server())
7578 return;
7579
7580 open_app_context();
7581 if (app_context != NULL && xterm_Shell == (Widget)0)
7582 {
7583 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007584# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007586# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007587# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007588 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589
7590 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007591 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592# endif
7593
Bram Moolenaar0f873732019-12-05 20:28:46 +01007594 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 oldhandler = XSetErrorHandler(x_error_check);
7596
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007597# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007598 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7600 mch_startjmp();
7601 if (SETJMP(lc_jump_env) != 0)
7602 {
7603 mch_didjmp();
7604 xterm_dpy = NULL;
7605 }
7606 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 {
7609 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7610 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007611 if (xterm_dpy != NULL)
7612 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007613# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007615# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 }
7617
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007618# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007619 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007621# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007622 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 (void)XSetErrorHandler(oldhandler);
7624
7625 if (xterm_dpy == NULL)
7626 {
7627 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007628 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 return;
7630 }
7631
Bram Moolenaar0f873732019-12-05 20:28:46 +01007632 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 (void)XSetIOErrorHandler(x_IOerror_handler);
7634
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007635# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007637 {
7638 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007639 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007640 verbose_leave();
7641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642# endif
7643
Bram Moolenaar0f873732019-12-05 20:28:46 +01007644 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7646 applicationShellWidgetClass, xterm_dpy,
7647 NULL);
7648 if (AppShell == (Widget)0)
7649 return;
7650 xterm_Shell = XtVaCreatePopupShell("VIM",
7651 topLevelShellWidgetClass, AppShell,
7652 XtNmappedWhenManaged, 0,
7653 XtNwidth, 1,
7654 XtNheight, 1,
7655 NULL);
7656 if (xterm_Shell == (Widget)0)
7657 return;
7658
7659 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007660 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 if (x11_display == NULL)
7662 x11_display = xterm_dpy;
7663
7664 XtRealizeWidget(xterm_Shell);
7665 XSync(xterm_dpy, False);
7666 xterm_update();
7667 }
7668 if (xterm_Shell != (Widget)0)
7669 {
7670 clip_init(TRUE);
7671 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7672 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007673 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 if (test_x11_window(xterm_dpy) == FAIL)
7675 x11_window = 0;
7676 if (x11_window != 0)
7677 xterm_trace = 0;
7678 }
7679}
7680
7681 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007682start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683{
7684 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7685 return;
7686 xterm_trace = 1;
7687 xterm_button = button;
7688 do_xterm_trace();
7689}
7690
7691
7692 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007693stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694{
7695 if (xterm_trace < 0)
7696 return;
7697 xterm_trace = 0;
7698}
7699
7700/*
7701 * Query the xterm pointer and generate mouse termcodes if necessary
7702 * return TRUE if dragging is active, else FALSE
7703 */
7704 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007705do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
7707 Window root, child;
7708 int root_x, root_y;
7709 int win_x, win_y;
7710 int row, col;
7711 int_u mask_return;
7712 char_u buf[50];
7713 char_u *strp;
7714 long got_hints;
7715 static char_u *mouse_code;
7716 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7717 static int prev_row = 0, prev_col = 0;
7718 static XSizeHints xterm_hints;
7719
7720 if (xterm_trace <= 0)
7721 return FALSE;
7722
7723 if (xterm_trace == 1)
7724 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007725 // Get the hints just before tracking starts. The font size might
7726 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007727 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7728 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 || xterm_hints.width_inc <= 1
7730 || xterm_hints.height_inc <= 1)
7731 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007732 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 return FALSE;
7734 }
7735
Bram Moolenaar0f873732019-12-05 20:28:46 +01007736 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 mouse_code = find_termcode(mouse_name);
7738 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007739 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 xterm_trace = 2;
7741
Bram Moolenaar0f873732019-12-05 20:28:46 +01007742 // Find the offset of the chars, there might be a scrollbar on the
7743 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7745 &win_x, &win_y, &mask_return);
7746 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7747 - (xterm_hints.height_inc / 2);
7748 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7749 xterm_hints.y = 2;
7750 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7751 - (xterm_hints.width_inc / 2);
7752 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7753 xterm_hints.x = 2;
7754 return TRUE;
7755 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007756 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {
7758 xterm_trace = 0;
7759 return FALSE;
7760 }
7761
7762 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7763 &win_x, &win_y, &mask_return);
7764
7765 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7766 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7767 if (row == prev_row && col == prev_col)
7768 return TRUE;
7769
7770 STRCPY(buf, mouse_code);
7771 strp = buf + STRLEN(buf);
7772 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7773 *strp++ = (char_u)(col + ' ' + 1);
7774 *strp++ = (char_u)(row + ' ' + 1);
7775 *strp = 0;
7776 add_to_input_buf(buf, STRLEN(buf));
7777
7778 prev_row = row;
7779 prev_col = col;
7780 return TRUE;
7781}
7782
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007783# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784/*
7785 * Destroy the display, window and app_context. Required for GTK.
7786 */
7787 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007788clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789{
7790 if (xterm_Shell != (Widget)0)
7791 {
7792 XtDestroyWidget(xterm_Shell);
7793 xterm_Shell = (Widget)0;
7794 }
7795 if (xterm_dpy != NULL)
7796 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007797# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007798 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 if (x11_display == xterm_dpy)
7802 x11_display = NULL;
7803 xterm_dpy = NULL;
7804 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007805# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 if (app_context != (XtAppContext)NULL)
7807 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007808 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 XtDestroyApplicationContext(app_context);
7810 app_context = (XtAppContext)NULL;
7811 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813}
7814# endif
7815
7816/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007817 * Catch up with GUI or X events.
7818 */
7819 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007820clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007821{
7822# ifdef FEAT_GUI
7823 if (gui.in_use)
7824 gui_mch_update();
7825 else
7826# endif
7827 if (xterm_Shell != (Widget)0)
7828 xterm_update();
7829}
7830
7831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 * Catch up with any queued X events. This may put keyboard input into the
7833 * input buffer, call resize call-backs, trigger timers etc. If there is
7834 * nothing in the X event queue (& no timers pending), then we return
7835 * immediately.
7836 */
7837 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007838xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839{
7840 XEvent event;
7841
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007842 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007844 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007846 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007847 break;
7848
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007849 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007850 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007851 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007852 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007853#ifdef FEAT_CLIENTSERVER
7854 {
7855 XPropertyEvent *e = (XPropertyEvent *)&event;
7856
7857 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007859 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007862 XtDispatchEvent(&event);
7863 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007864 else
7865 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007866 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007867 XtAppProcessEvent(app_context, mask);
7868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 }
7870}
7871
7872 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007873clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
7875 if (xterm_Shell != (Widget)0)
7876 return clip_x11_own_selection(xterm_Shell, cbd);
7877 return FAIL;
7878}
7879
7880 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007881clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882{
7883 if (xterm_Shell != (Widget)0)
7884 clip_x11_lose_selection(xterm_Shell, cbd);
7885}
7886
7887 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007888clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889{
7890 if (xterm_Shell != (Widget)0)
7891 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7892}
7893
7894 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007895clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
7897 clip_x11_set_selection(cbd);
7898}
7899#endif
7900
7901
7902#if defined(USE_XSMP) || defined(PROTO)
7903/*
7904 * Code for X Session Management Protocol.
7905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906
7907# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908/*
7909 * This is our chance to ask the user if they want to save,
7910 * or abort the logout
7911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007913xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
Bram Moolenaare1004402020-10-24 20:49:43 +02007915 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 int cancel_shutdown = False;
7917
Bram Moolenaare1004402020-10-24 20:49:43 +02007918 save_cmod_flags = cmdmod.cmod_flags;
7919 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007920 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007921 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02007923 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007924 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 out_flush();
7926
Bram Moolenaar0f873732019-12-05 20:28:46 +01007927 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 SmcInteractDone(smc_conn, cancel_shutdown);
7929
Bram Moolenaar0f873732019-12-05 20:28:46 +01007930 // Finish off
7931 // Only end save-yourself here if we're not cancelling shutdown;
7932 // we'll get a cancelled callback later in which we'll end it.
7933 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 if (!cancel_shutdown)
7935 {
7936 xsmp.save_yourself = False;
7937 SmcSaveYourselfDone(smc_conn, True);
7938 }
7939}
7940# endif
7941
7942/*
7943 * Callback that starts save-yourself.
7944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007946xsmp_handle_save_yourself(
7947 SmcConn smc_conn,
7948 SmPointer client_data UNUSED,
7949 int save_type UNUSED,
7950 Bool shutdown,
7951 int interact_style UNUSED,
7952 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007954 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 if (xsmp.save_yourself)
7956 SmcSaveYourselfDone(smc_conn, True);
7957 xsmp.save_yourself = True;
7958 xsmp.shutdown = shutdown;
7959
Bram Moolenaar0f873732019-12-05 20:28:46 +01007960 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007962 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963
7964 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007965 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966
7967# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007968 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007970 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 SmcInteractRequest(smc_conn, SmDialogError,
7972 xsmp_handle_interaction, client_data);
7973 else
7974# endif
7975 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007976 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 SmcSaveYourselfDone(smc_conn, True);
7978 xsmp.save_yourself = False;
7979 }
7980}
7981
7982
7983/*
7984 * Callback to warn us of imminent death.
7985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007987xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988{
7989 xsmp_close();
7990
Bram Moolenaar0f873732019-12-05 20:28:46 +01007991 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 getout_preserve_modified(0);
7993}
7994
7995
7996/*
7997 * Callback to tell us that save-yourself has completed.
7998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008000xsmp_save_complete(
8001 SmcConn smc_conn UNUSED,
8002 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003{
8004 xsmp.save_yourself = False;
8005}
8006
8007
8008/*
8009 * Callback to tell us that an instigated shutdown was cancelled
8010 * (maybe even by us)
8011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008013xsmp_shutdown_cancelled(
8014 SmcConn smc_conn,
8015 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017 if (xsmp.save_yourself)
8018 SmcSaveYourselfDone(smc_conn, True);
8019 xsmp.save_yourself = False;
8020 xsmp.shutdown = False;
8021}
8022
8023
8024/*
8025 * Callback to tell us that a new ICE connection has been established.
8026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008028xsmp_ice_connection(
8029 IceConn iceConn,
8030 IcePointer clientData UNUSED,
8031 Bool opening,
8032 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008034 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 if (opening)
8036 {
8037 xsmp_icefd = IceConnectionNumber(iceConn);
8038 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8039 }
8040}
8041
8042
Bram Moolenaar0f873732019-12-05 20:28:46 +01008043// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008045xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046{
8047 Bool rep;
8048
8049 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8050 == IceProcessMessagesIOError)
8051 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008052 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008054 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 xsmp_close();
8056 return FAIL;
8057 }
8058 else
8059 return OK;
8060}
8061
8062static int dummy;
8063
Bram Moolenaar0f873732019-12-05 20:28:46 +01008064// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 void
8066xsmp_init(void)
8067{
8068 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 SmcCallbacks smcallbacks;
8070#if 0
8071 SmPropValue smname;
8072 SmProp smnameprop;
8073 SmProp *smprops[1];
8074#endif
8075
8076 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008077 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078
8079 xsmp.save_yourself = xsmp.shutdown = False;
8080
Bram Moolenaar0f873732019-12-05 20:28:46 +01008081 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8083 smcallbacks.save_yourself.client_data = NULL;
8084 smcallbacks.die.callback = xsmp_die;
8085 smcallbacks.die.client_data = NULL;
8086 smcallbacks.save_complete.callback = xsmp_save_complete;
8087 smcallbacks.save_complete.client_data = NULL;
8088 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8089 smcallbacks.shutdown_cancelled.client_data = NULL;
8090
Bram Moolenaar0f873732019-12-05 20:28:46 +01008091 // Set up a watch on ICE connection creations. The "dummy" argument is
8092 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8094 {
8095 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008096 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 return;
8098 }
8099
Bram Moolenaar0f873732019-12-05 20:28:46 +01008100 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 xsmp.smcconn = SmcOpenConnection(
8102 NULL,
8103 NULL,
8104 SmProtoMajor,
8105 SmProtoMinor,
8106 SmcSaveYourselfProcMask | SmcDieProcMask
8107 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8108 &smcallbacks,
8109 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008110 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008111 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 errorstring);
8113 if (xsmp.smcconn == NULL)
8114 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008116 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008117 char errorreport[132];
8118
8119 // If the message is too long it might not be NUL terminated. Add
8120 // a NUL at the end to make sure we don't go over the end.
8121 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008122 vim_snprintf(errorreport, sizeof(errorreport),
8123 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008124 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 return;
8127 }
8128 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8129
8130#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008131 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 smname.value = "vim";
8133 smname.length = 3;
8134 smnameprop.name = "SmProgram";
8135 smnameprop.type = "SmARRAY8";
8136 smnameprop.num_vals = 1;
8137 smnameprop.vals = &smname;
8138
8139 smprops[0] = &smnameprop;
8140 SmcSetProperties(xsmp.smcconn, 1, smprops);
8141#endif
8142}
8143
8144
Bram Moolenaar0f873732019-12-05 20:28:46 +01008145// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008147xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148{
8149 if (xsmp_icefd != -1)
8150 {
8151 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008152 if (xsmp.clientid != NULL)
8153 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008154 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 xsmp_icefd = -1;
8156 }
8157}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008158#endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159
8160
8161#ifdef EBCDIC
Bram Moolenaar0f873732019-12-05 20:28:46 +01008162// Translate character to its CTRL- value
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163char CtrlTable[] =
8164{
8165/* 00 - 5E */
8166 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8167 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8168 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8169 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8170 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8171 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8172/* ^ */ 0x1E,
8173/* - */ 0x1F,
8174/* 61 - 6C */
8175 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8176/* _ */ 0x1F,
8177/* 6E - 80 */
8178 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8179/* a */ 0x01,
8180/* b */ 0x02,
8181/* c */ 0x03,
8182/* d */ 0x37,
8183/* e */ 0x2D,
8184/* f */ 0x2E,
8185/* g */ 0x2F,
8186/* h */ 0x16,
8187/* i */ 0x05,
8188/* 8A - 90 */
8189 0, 0, 0, 0, 0, 0, 0,
8190/* j */ 0x15,
8191/* k */ 0x0B,
8192/* l */ 0x0C,
8193/* m */ 0x0D,
8194/* n */ 0x0E,
8195/* o */ 0x0F,
8196/* p */ 0x10,
8197/* q */ 0x11,
8198/* r */ 0x12,
8199/* 9A - A1 */
8200 0, 0, 0, 0, 0, 0, 0, 0,
8201/* s */ 0x13,
8202/* t */ 0x3C,
8203/* u */ 0x3D,
8204/* v */ 0x32,
8205/* w */ 0x26,
8206/* x */ 0x18,
8207/* y */ 0x19,
8208/* z */ 0x3F,
8209/* AA - AC */
8210 0, 0, 0,
8211/* [ */ 0x27,
8212/* AE - BC */
8213 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8214/* ] */ 0x1D,
8215/* BE - C0 */ 0, 0, 0,
8216/* A */ 0x01,
8217/* B */ 0x02,
8218/* C */ 0x03,
8219/* D */ 0x37,
8220/* E */ 0x2D,
8221/* F */ 0x2E,
8222/* G */ 0x2F,
8223/* H */ 0x16,
8224/* I */ 0x05,
8225/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8226/* J */ 0x15,
8227/* K */ 0x0B,
8228/* L */ 0x0C,
8229/* M */ 0x0D,
8230/* N */ 0x0E,
8231/* O */ 0x0F,
8232/* P */ 0x10,
8233/* Q */ 0x11,
8234/* R */ 0x12,
8235/* DA - DF */ 0, 0, 0, 0, 0, 0,
8236/* \ */ 0x1C,
8237/* E1 */ 0,
8238/* S */ 0x13,
8239/* T */ 0x3C,
8240/* U */ 0x3D,
8241/* V */ 0x32,
8242/* W */ 0x26,
8243/* X */ 0x18,
8244/* Y */ 0x19,
8245/* Z */ 0x3F,
8246/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8247 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8248};
8249
8250char MetaCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008251{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8253 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8254 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8255 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8256};
8257
8258
Bram Moolenaar0f873732019-12-05 20:28:46 +01008259// TODO: Use characters NOT numbers!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260char CtrlCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008261{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8263 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8264 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8265 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8266};
8267
8268
8269#endif