blob: 32850f3bf2f2f71de06c178ded807d811b8497cb [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 Moolenaar48873ae2021-12-08 21:00:24 +00002343 || STRNICMP(name, "gnome", 5) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002344 || STRICMP(name, "st") == 0
2345 || STRNICMP(name, "st-", 3) == 0
2346 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002347}
2348#endif
2349
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350/*
2351 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2352 * Return 1 for "xterm".
2353 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002354 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002355 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 */
2357 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002358use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002360 if (ttym_flags == TTYM_SGR)
2361 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002362 if (ttym_flags == TTYM_URXVT)
2363 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 if (ttym_flags == TTYM_XTERM2)
2365 return 2;
2366 if (ttym_flags == TTYM_XTERM)
2367 return 1;
2368 return 0;
2369}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
2371 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002372vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373{
2374 if (name == NULL)
2375 return FALSE;
2376 return (STRNICMP(name, "iris-ansi", 9) == 0
2377 || STRCMP(name, "builtin_iris-ansi") == 0);
2378}
2379
2380 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002381vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382{
2383 if (name == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002384 return FALSE; // actually all ANSI comp. terminals should be here
2385 // catch VT100 - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002386 return ((STRNICMP(name, "vt", 2) == 0
2387 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 || STRCMP(name, "builtin_vt320") == 0);
2389}
2390
2391/*
2392 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2393 * This should include all windowed terminal emulators.
2394 */
2395 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002396vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
2398 if (name == NULL)
2399 return FALSE;
2400 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2401 return TRUE;
2402 return ( STRNICMP(name, "hpterm", 6) == 0
2403 || STRNICMP(name, "sun-cmd", 7) == 0
2404 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002405 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 || STRNICMP(name, "dtterm", 6) == 0);
2407}
2408
2409/*
2410 * Insert user name in s[len].
2411 * Return OK if a name found.
2412 */
2413 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002414mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002417 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 return OK;
2419#else
2420 return mch_get_uname(getuid(), s, len);
2421#endif
2422}
2423
2424/*
2425 * Insert user name for "uid" in s[len].
2426 * Return OK if a name found.
2427 */
2428 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002429mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
2431#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2432 struct passwd *pw;
2433
2434 if ((pw = getpwuid(uid)) != NULL
2435 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2436 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002437 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 return OK;
2439 }
2440#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002441 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2442 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443}
2444
2445/*
2446 * Insert host name is s[len].
2447 */
2448
2449#ifdef HAVE_SYS_UTSNAME_H
2450 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002451mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452{
2453 struct utsname vutsname;
2454
2455 if (uname(&vutsname) < 0)
2456 *s = NUL;
2457 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002458 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002460#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462# ifdef HAVE_SYS_SYSTEMINFO_H
2463# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2464# endif
2465
2466 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002467mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469# ifdef VAXC
2470 vaxc$gethostname((char *)s, len);
2471# else
2472 gethostname((char *)s, len);
2473# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002474 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002476#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478/*
2479 * return process ID
2480 */
2481 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002482mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483{
2484 return (long)getpid();
2485}
2486
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002487/*
2488 * return TRUE if process "pid" is still running
2489 */
2490 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002491mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002492{
Bram Moolenaar44dea9d2021-06-23 21:13:20 +02002493 // If there is no error the process must be running.
2494 if (kill(pid, 0) == 0)
2495 return TRUE;
2496#ifdef ESRCH
2497 // If the error is ESRCH then the process is not running.
2498 if (errno == ESRCH)
2499 return FALSE;
2500#endif
2501 // If the process is running and owned by another user we get EPERM. With
2502 // other errors the process might be running, assuming it is then.
2503 return TRUE;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002504}
2505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002508strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510 extern int sys_nerr;
2511 extern char *sys_errlist[];
2512 static char er[20];
2513
2514 if (err > 0 && err < sys_nerr)
2515 return (sys_errlist[err]);
2516 sprintf(er, "Error %d", err);
2517 return er;
2518}
2519#endif
2520
2521/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002522 * Get name of current directory into buffer "buf" of length "len" bytes.
2523 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 * Return OK for success, FAIL for failure.
2525 */
2526 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002527mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528{
2529#if defined(USE_GETCWD)
2530 if (getcwd((char *)buf, len) == NULL)
2531 {
2532 STRCPY(buf, strerror(errno));
2533 return FAIL;
2534 }
2535 return OK;
2536#else
2537 return (getwd((char *)buf) != NULL ? OK : FAIL);
2538#endif
2539}
2540
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002542 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 *
2544 * return FAIL for failure, OK for success
2545 */
2546 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002547mch_FullName(
2548 char_u *fname,
2549 char_u *buf,
2550 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002551 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
2553 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002554#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002556 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 char_u olddir[MAXPATHL];
2559 char_u *p;
2560 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002561#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002562 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2563 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002564#endif
2565
Bram Moolenaar38323e42007-03-06 19:22:53 +00002566#ifdef VMS
2567 fname = vms_fixfilename(fname);
2568#endif
2569
Bram Moolenaara2442432007-04-26 14:26:37 +00002570#ifdef __CYGWIN__
2571 /*
2572 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2573 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002574# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002575 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2576 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002577 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2578 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002579# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002580 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002581# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002582 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002583#endif
2584
Bram Moolenaar0f873732019-12-05 20:28:46 +01002585 // Expand it if forced or not an absolute path.
2586 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002587 if ((force || !mch_isFullName(fname))
2588 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 {
2590 /*
2591 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002592 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 * This will get the correct path name with "../" things.
2594 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002595 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002597 if (STRCMP(p, "/..") == 0)
2598 // for "/path/dir/.." include the "/.."
2599 p += 3;
2600
Bram Moolenaar38323e42007-03-06 19:22:53 +00002601#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 /*
2603 * Use fchdir() if possible, it's said to be faster and more
2604 * reliable. But on SunOS 4 it might not work. Check this by
2605 * doing a fchdir() right now.
2606 */
2607 if (!dont_fchdir)
2608 {
2609 fd = open(".", O_RDONLY | O_EXTRA, 0);
2610 if (fd >= 0 && fchdir(fd) < 0)
2611 {
2612 close(fd);
2613 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002614 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 }
2616 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
Bram Moolenaar0f873732019-12-05 20:28:46 +01002619 // Only change directory when we are sure we can return to where
2620 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002622#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 (mch_dirname(olddir, MAXPATHL) == FAIL
2626 || mch_chdir((char *)olddir) != 0))
2627 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002628 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 retval = FAIL;
2630 }
2631 else
2632 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002633 // The directory is copied into buf[], to be able to remove
2634 // the file name without changing it (could be a string in
2635 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 if (p - fname >= len)
2637 retval = FAIL;
2638 else
2639 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002640 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (mch_chdir((char *)buf))
Bram Moolenaarc6376c72021-10-03 19:29:48 +01002642 {
2643 // Path does not exist (yet). For a full path fail,
2644 // will use the path as-is. For a relative path use
2645 // the current directory and append the file name.
2646 if (mch_isFullName(fname))
2647 retval = FAIL;
2648 else
2649 p = NULL;
2650 }
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002651 else if (*p == '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 fname = p + 1;
Bram Moolenaar4eaef992021-08-30 21:26:16 +02002653 else
2654 fname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 *buf = NUL;
2656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
2658 }
2659 if (mch_dirname(buf, len) == FAIL)
2660 {
2661 retval = FAIL;
2662 *buf = NUL;
2663 }
2664 if (p != NULL)
2665 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002666#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (fd >= 0)
2668 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002669 if (p_verbose >= 5)
2670 {
2671 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002672 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002673 verbose_leave();
2674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 l = fchdir(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
2677 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 l = mch_chdir((char *)olddir);
2680 if (l != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002681 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 }
itchyny051a40c2021-10-20 10:00:05 +01002683#ifdef HAVE_FCHDIR
2684 if (fd >= 0)
2685 close(fd);
2686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
2688 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002689 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002690 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002691#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002692 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002694 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002697
Bram Moolenaar0f873732019-12-05 20:28:46 +01002698 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002699 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 return FAIL;
2701
Bram Moolenaar0f873732019-12-05 20:28:46 +01002702 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 if (STRCMP(fname, ".") != 0)
2704 STRCAT(buf, fname);
2705
2706 return OK;
2707}
2708
2709/*
2710 * Return TRUE if "fname" does not depend on the current directory.
2711 */
2712 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002713mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002715#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 return ( fname[0] == '/' || fname[0] == '.' ||
2717 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2718 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2719 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002720#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722#endif
2723}
2724
Bram Moolenaar24552be2005-12-10 20:17:30 +00002725#if defined(USE_FNAME_CASE) || defined(PROTO)
2726/*
2727 * Set the case of the file name, if it already exists. This will cause the
2728 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002729 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002730 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002731 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002732fname_case(
2733 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002734 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002735{
2736 struct stat st;
2737 char_u *slash, *tail;
2738 DIR *dirp;
2739 struct dirent *dp;
2740
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002741 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002742 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002743 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002744 slash = vim_strrchr(name, '/');
2745 if (slash == NULL)
2746 {
2747 dirp = opendir(".");
2748 tail = name;
2749 }
2750 else
2751 {
2752 *slash = NUL;
2753 dirp = opendir((char *)name);
2754 *slash = '/';
2755 tail = slash + 1;
2756 }
2757
2758 if (dirp != NULL)
2759 {
2760 while ((dp = readdir(dirp)) != NULL)
2761 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002762 // Only accept names that differ in case and are the same byte
2763 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002764 if (STRICMP(tail, dp->d_name) == 0
2765 && STRLEN(tail) == STRLEN(dp->d_name))
2766 {
2767 char_u newname[MAXPATHL + 1];
2768 struct stat st2;
2769
Bram Moolenaar0f873732019-12-05 20:28:46 +01002770 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002771 vim_strncpy(newname, name, MAXPATHL);
2772 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2773 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002774 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002775 && st.st_ino == st2.st_ino
2776 && st.st_dev == st2.st_dev)
2777 {
2778 STRCPY(tail, dp->d_name);
2779 break;
2780 }
2781 }
2782 }
2783
2784 closedir(dirp);
2785 }
2786 }
2787}
2788#endif
2789
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790/*
2791 * Get file permissions for 'name'.
2792 * Returns -1 when it doesn't exist.
2793 */
2794 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002795mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796{
2797 struct stat statb;
2798
Bram Moolenaar0f873732019-12-05 20:28:46 +01002799 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800#ifdef VMS
2801 if (stat((char *)vms_fixfilename(name), &statb))
2802#else
2803 if (stat((char *)name, &statb))
2804#endif
2805 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002806#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002807 // The top bit makes the value negative, which means the file doesn't
2808 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002809 return statb.st_mode & ~S_ADDACE;
2810#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813}
2814
2815/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002816 * Set file permission for "name" to "perm".
2817 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 */
2819 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002820mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821{
2822 return (chmod((char *)
2823#ifdef VMS
2824 vms_fixfilename(name),
2825#else
2826 name,
2827#endif
2828 (mode_t)perm) == 0 ? OK : FAIL);
2829}
2830
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002831#if defined(HAVE_FCHMOD) || defined(PROTO)
2832/*
2833 * Set file permission for open file "fd" to "perm".
2834 * Return FAIL for failure, OK otherwise.
2835 */
2836 int
2837mch_fsetperm(int fd, long perm)
2838{
2839 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2840}
2841#endif
2842
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843#if defined(HAVE_ACL) || defined(PROTO)
2844# ifdef HAVE_SYS_ACL_H
2845# include <sys/acl.h>
2846# endif
2847# ifdef HAVE_SYS_ACCESS_H
2848# include <sys/access.h>
2849# endif
2850
2851# ifdef HAVE_SOLARIS_ACL
2852typedef struct vim_acl_solaris_T {
2853 int acl_cnt;
2854 aclent_t *acl_entry;
2855} vim_acl_solaris_T;
2856# endif
2857
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002858#if defined(HAVE_SELINUX) || defined(PROTO)
2859/*
2860 * Copy security info from "from_file" to "to_file".
2861 */
2862 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002863mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002864{
2865 if (from_file == NULL)
2866 return;
2867
2868 if (selinux_enabled == -1)
2869 selinux_enabled = is_selinux_enabled();
2870
2871 if (selinux_enabled > 0)
2872 {
Bram Moolenaar89560232020-10-09 23:04:47 +02002873 // Use "char *" instead of "security_context_t" to avoid a deprecation
2874 // warning.
2875 char *from_context = NULL;
2876 char *to_context = NULL;
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002877
2878 if (getfilecon((char *)from_file, &from_context) < 0)
2879 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002880 // If the filesystem doesn't support extended attributes,
2881 // the original had no special security context and the
2882 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002883 if (errno == EOPNOTSUPP)
2884 return;
2885
Bram Moolenaar32526b32019-01-19 17:43:09 +01002886 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002887 msg_outtrans(from_file);
2888 msg_putchar('\n');
2889 return;
2890 }
2891 if (getfilecon((char *)to_file, &to_context) < 0)
2892 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002893 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002894 msg_outtrans(to_file);
2895 msg_putchar('\n');
2896 freecon (from_context);
2897 return ;
2898 }
2899 if (strcmp(from_context, to_context) != 0)
2900 {
2901 if (setfilecon((char *)to_file, from_context) < 0)
2902 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002903 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002904 msg_outtrans(to_file);
2905 msg_putchar('\n');
2906 }
2907 }
2908 freecon(to_context);
2909 freecon(from_context);
2910 }
2911}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002912#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002913
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002914#if defined(HAVE_SMACK) && !defined(PROTO)
2915/*
2916 * Copy security info from "from_file" to "to_file".
2917 */
2918 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002919mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002920{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002921 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002922 {
2923 XATTR_NAME_SMACK,
2924 XATTR_NAME_SMACKEXEC,
2925 XATTR_NAME_SMACKMMAP
2926 };
2927
2928 char buffer[SMACK_LABEL_LEN];
2929 const char *name;
2930 int index;
2931 int ret;
2932 ssize_t size;
2933
2934 if (from_file == NULL)
2935 return;
2936
2937 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2938 / sizeof(smack_copied_attributes)[0]) ; index++)
2939 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002940 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002941 name = smack_copied_attributes[index];
2942
Bram Moolenaar0f873732019-12-05 20:28:46 +01002943 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002944 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2945 if (size >= 0)
2946 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002947 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002948 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2949 if (ret < 0)
2950 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002951 vim_snprintf((char *)IObuff, IOSIZE,
2952 _("Could not set security context %s for %s"),
2953 name, to_file);
2954 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002955 msg_putchar('\n');
2956 }
2957 }
2958 else
2959 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002960 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002961 switch (errno)
2962 {
2963 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002964 // extended attributes aren't supported or enabled
2965 // should a message be echoed? not sure...
2966 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002967
2968 case ERANGE:
2969 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002970 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002971 vim_snprintf((char *)IObuff, IOSIZE,
2972 _("Could not get security context %s for %s. Removing it!"),
2973 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002974 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002975 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01002976 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002977
2978 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002979 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002980 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002981 // Silently ignore errors, apparently this happens when
2982 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002983 break;
2984 }
2985 }
2986 }
2987}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002988#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002989
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990/*
2991 * Return a pointer to the ACL of file "fname" in allocated memory.
2992 * Return NULL if the ACL is not available for whatever reason.
2993 */
2994 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002995mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 vim_acl_T ret = NULL;
2998#ifdef HAVE_POSIX_ACL
2999 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3000#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003001#ifdef HAVE_SOLARIS_ZFS_ACL
3002 acl_t *aclent;
3003
3004 if (acl_get((char *)fname, 0, &aclent) < 0)
3005 return NULL;
3006 ret = (vim_acl_T)aclent;
3007#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008#ifdef HAVE_SOLARIS_ACL
3009 vim_acl_solaris_T *aclent;
3010
3011 aclent = malloc(sizeof(vim_acl_solaris_T));
3012 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3013 {
3014 free(aclent);
3015 return NULL;
3016 }
3017 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3018 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3019 {
3020 free(aclent->acl_entry);
3021 free(aclent);
3022 return NULL;
3023 }
3024 ret = (vim_acl_T)aclent;
3025#else
3026#if defined(HAVE_AIX_ACL)
3027 int aclsize;
3028 struct acl *aclent;
3029
3030 aclsize = sizeof(struct acl);
3031 aclent = malloc(aclsize);
3032 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3033 {
3034 if (errno == ENOSPC)
3035 {
3036 aclsize = aclent->acl_len;
3037 aclent = realloc(aclent, aclsize);
3038 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3039 {
3040 free(aclent);
3041 return NULL;
3042 }
3043 }
3044 else
3045 {
3046 free(aclent);
3047 return NULL;
3048 }
3049 }
3050 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003051#endif // HAVE_AIX_ACL
3052#endif // HAVE_SOLARIS_ACL
3053#endif // HAVE_SOLARIS_ZFS_ACL
3054#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 return ret;
3056}
3057
3058/*
3059 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3060 */
3061 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003062mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 if (aclent == NULL)
3065 return;
3066#ifdef HAVE_POSIX_ACL
3067 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3068#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003069#ifdef HAVE_SOLARIS_ZFS_ACL
3070 acl_set((char *)fname, (acl_t *)aclent);
3071#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072#ifdef HAVE_SOLARIS_ACL
3073 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3074 ((vim_acl_solaris_T *)aclent)->acl_entry);
3075#else
3076#ifdef HAVE_AIX_ACL
3077 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003078#endif // HAVE_AIX_ACL
3079#endif // HAVE_SOLARIS_ACL
3080#endif // HAVE_SOLARIS_ZFS_ACL
3081#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082}
3083
3084 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003085mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
3087 if (aclent == NULL)
3088 return;
3089#ifdef HAVE_POSIX_ACL
3090 acl_free((acl_t)aclent);
3091#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003092#ifdef HAVE_SOLARIS_ZFS_ACL
3093 acl_free((acl_t *)aclent);
3094#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#ifdef HAVE_SOLARIS_ACL
3096 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3097 free(aclent);
3098#else
3099#ifdef HAVE_AIX_ACL
3100 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003101#endif // HAVE_AIX_ACL
3102#endif // HAVE_SOLARIS_ACL
3103#endif // HAVE_SOLARIS_ZFS_ACL
3104#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105}
3106#endif
3107
3108/*
3109 * Set hidden flag for "name".
3110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003112mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003114 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115}
3116
3117/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003118 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 * return FALSE if "name" is not a directory
3120 * return FALSE for error
3121 */
3122 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003123mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
3125 struct stat statb;
3126
Bram Moolenaar0f873732019-12-05 20:28:46 +01003127 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 return FALSE;
3129 if (stat((char *)name, &statb))
3130 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132}
3133
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003134/*
3135 * return TRUE if "name" is a directory, NOT a symlink to a directory
3136 * return FALSE if "name" is not a directory
3137 * return FALSE for error
3138 */
3139 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003140mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003141{
3142 struct stat statb;
3143
Bram Moolenaar0f873732019-12-05 20:28:46 +01003144 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003145 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003146 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003147 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003148 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003149}
3150
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151/*
3152 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3153 */
3154 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003155executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
3157 struct stat st;
3158
3159 if (stat((char *)name, &st))
3160 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003161#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003162 // Like on Unix system file can have executable rights but not necessarily
3163 // be an executable, but on Unix is not a default for an ordinary file to
3164 // have an executable flag - on VMS it is in most cases.
3165 // Therefore, this check does not have any sense - let keep us to the
3166 // conventions instead:
3167 // *.COM and *.EXE files are the executables - the rest are not. This is
3168 // not ideal but better then it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003169 int vms_executable = 0;
3170 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3171 {
3172 if (strstr(vms_tolower((char*)name),".exe") != NULL
3173 || strstr(vms_tolower((char*)name),".com")!= NULL)
3174 vms_executable = 1;
3175 }
3176 return vms_executable;
3177#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180}
3181
3182/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003183 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003184 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 * Return -1 if unknown.
3186 */
3187 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003188mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189{
3190 char_u *buf;
3191 char_u *p, *e;
3192 int retval;
3193
Bram Moolenaar0f873732019-12-05 20:28:46 +01003194 // When "use_path" is false and if it's an absolute or relative path don't
3195 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003196 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003197 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003198 // There must be a path separator, files in the current directory
3199 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003200 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003201 {
3202 if (path != NULL)
3203 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003204 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003205 *path = FullName_save(name, TRUE);
3206 else
3207 *path = vim_strsave(name);
3208 }
3209 return TRUE;
3210 }
3211 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
3214 p = (char_u *)getenv("PATH");
3215 if (p == NULL || *p == NUL)
3216 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003217 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 if (buf == NULL)
3219 return -1;
3220
3221 /*
3222 * Walk through all entries in $PATH to check if "name" exists there and
3223 * is an executable file.
3224 */
3225 for (;;)
3226 {
3227 e = (char_u *)strchr((char *)p, ':');
3228 if (e == NULL)
3229 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003230 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 STRCPY(buf, "./");
3232 else
3233 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003234 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 add_pathsep(buf);
3236 }
3237 STRCAT(buf, name);
3238 retval = executable_file(buf);
3239 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003240 {
3241 if (path != NULL)
3242 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003243 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003244 *path = FullName_save(buf, TRUE);
3245 else
3246 *path = vim_strsave(buf);
3247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
3251 if (*e != ':')
3252 break;
3253 p = e + 1;
3254 }
3255
3256 vim_free(buf);
3257 return retval;
3258}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
3260/*
3261 * Check what "name" is:
3262 * NODE_NORMAL: file or directory (or doesn't exist)
3263 * NODE_WRITABLE: writable device, socket, fifo, etc.
3264 * NODE_OTHER: non-writable things
3265 */
3266 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003267mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
3269 struct stat st;
3270
3271 if (stat((char *)name, &st))
3272 return NODE_NORMAL;
3273 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3274 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003275 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003277 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 return NODE_WRITABLE;
3279}
3280
3281 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003282mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
3284#ifdef HAVE_CHECK_STACK_GROWTH
3285 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 check_stack_growth((char *)&i);
3288
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003289# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 get_stack_limit();
3291# endif
3292
3293#endif
3294
3295 /*
3296 * Setup an alternative stack for signals. Helps to catch signals when
3297 * running out of stack space.
3298 * Use of sigaltstack() is preferred, it's more portable.
3299 * Ignore any errors.
3300 */
3301#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Zdenek Dohnalba9c23e2021-08-11 14:20:05 +02003302 signal_stack = alloc(get_signal_stack_size());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 init_signal_stack();
3304#endif
3305}
3306
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003307#if defined(EXITFREE) || defined(PROTO)
3308 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003309mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003310{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003311# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3312 if (clip_star.owned)
3313 clip_lose_selection(&clip_star);
3314 if (clip_plus.owned)
3315 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003316# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003317# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003318 if (xterm_Shell != (Widget)0)
3319 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003320# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003321 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003322 if (xterm_dpy != NULL)
3323 XtCloseDisplay(xterm_dpy);
3324 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003325 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003326 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003327# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003328 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003329# endif
3330 }
3331# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003332# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003333# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003334 if (x11_display != NULL
3335# ifdef FEAT_XCLIPBOARD
3336 && x11_display != xterm_dpy
3337# endif
3338 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003339 XCloseDisplay(x11_display);
3340# endif
3341# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003342 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003343# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003344 vim_free(oldtitle);
3345 vim_free(oldicon);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003346}
3347#endif
3348
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349/*
3350 * Output a newline when exiting.
3351 * Make sure the newline goes to the same stream as the text.
3352 */
3353 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003354exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003356 if (silent_mode)
3357 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 if (newline_on_exit || msg_didout)
3359 {
3360 if (msg_use_printf())
3361 {
3362 if (info_message)
3363 mch_msg("\n");
3364 else
3365 mch_errmsg("\r\n");
3366 }
3367 else
3368 out_char('\n');
3369 }
Bram Moolenaar7007e312021-03-27 12:11:33 +01003370 else if (!is_not_a_term())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003372 restore_cterm_colors(); // get original colors back
3373 msg_clr_eos_force(); // clear the rest of the display
3374 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
3376}
3377
Bram Moolenaarb4151682020-05-11 22:13:28 +02003378#ifdef USE_GCOV_FLUSH
3379extern void __gcov_flush();
3380#endif
3381
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003383mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384{
3385 exiting = TRUE;
3386
3387#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3388 x11_export_final_selection();
3389#endif
3390
3391#ifdef FEAT_GUI
3392 if (!gui.in_use)
3393#endif
3394 {
3395 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003396 if (!is_not_a_term())
3397 {
3398 // restore xterm title and icon name
3399 mch_restore_title(SAVE_RESTORE_BOTH);
3400 term_pop_title(SAVE_RESTORE_BOTH);
3401 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003402
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 /*
3404 * When t_ti is not empty but it doesn't cause swapping terminal
3405 * pages, need to output a newline when msg_didout is set. But when
3406 * t_ti does swap pages it should not go to the shell page. Do this
3407 * before stoptermcap().
3408 */
3409 if (swapping_screen() && !newline_on_exit)
3410 exit_scroll();
3411
Bram Moolenaar0f873732019-12-05 20:28:46 +01003412 // Stop termcap: May need to check for T_CRV response, which
3413 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 stoptermcap();
3415
3416 /*
3417 * A newline is only required after a message in the alternate screen.
3418 * This is set to TRUE by wait_return().
3419 */
3420 if (!swapping_screen() || newline_on_exit)
3421 exit_scroll();
3422
Bram Moolenaar0f873732019-12-05 20:28:46 +01003423 // Cursor may have been switched off without calling starttermcap()
3424 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 if (full_screen)
3426 cursor_on();
3427 }
3428 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003429 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003430
3431#ifdef USE_GCOV_FLUSH
3432 // Flush coverage info before possibly being killed by a deadly signal.
3433 __gcov_flush();
3434#endif
3435
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 may_core_dump();
3437#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 gui_exit(r);
3440#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003441
Bram Moolenaar56718732006-03-15 22:53:57 +00003442#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003443 mac_conv_cleanup();
3444#endif
3445
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003447 // A core dump won't be created if the signal handler
3448 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 if (deadly_signal != 0)
3450 return;
3451#endif
3452
Bram Moolenaar009b2592004-10-24 19:18:58 +00003453#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003454 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003455#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003456
3457#ifdef EXITFREE
3458 free_all_mem();
3459#endif
3460
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 exit(r);
3462}
3463
3464 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003465may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 if (deadly_signal != 0)
3468 {
3469 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003470 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
3472}
3473
3474#ifndef VMS
3475
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003476/*
3477 * Get the file descriptor to use for tty operations.
3478 */
3479 static int
3480get_tty_fd(int fd)
3481{
3482 int tty_fd = fd;
3483
3484#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3485 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3486 // "fd" when it is a master device.
3487 if (mch_isatty(fd) > 1)
3488 {
3489 char *name;
3490
3491 name = ptsname(fd);
3492 if (name == NULL)
3493 return -1;
3494
3495 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3496 if (tty_fd < 0)
3497 return -1;
3498 }
3499#endif
3500 return tty_fd;
3501}
3502
3503 static int
3504mch_tcgetattr(int fd, void *term)
3505{
3506 int tty_fd;
3507 int retval = -1;
3508
3509 tty_fd = get_tty_fd(fd);
3510 if (tty_fd >= 0)
3511 {
3512#ifdef NEW_TTY_SYSTEM
3513# ifdef HAVE_TERMIOS_H
3514 retval = tcgetattr(tty_fd, (struct termios *)term);
3515# else
3516 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3517# endif
3518#else
3519 // for "old" tty systems
3520 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3521#endif
3522 if (tty_fd != fd)
3523 close(tty_fd);
3524 }
3525 return retval;
3526}
3527
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003529mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530{
3531 static int first = TRUE;
3532
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003533#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534# ifdef HAVE_TERMIOS_H
3535 static struct termios told;
3536 struct termios tnew;
3537# else
3538 static struct termio told;
3539 struct termio tnew;
3540# endif
3541
3542 if (first)
3543 {
3544 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003545 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547
3548 tnew = told;
3549 if (tmode == TMODE_RAW)
3550 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003551 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003552 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3553 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003555# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003556 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557# endif
3558 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003559# ifdef ONLCR
3560 // Don't map NL -> CR NL, we do it ourselves.
3561 // Also disable expanding tabs if possible.
3562# ifdef XTABS
3563 tnew.c_oflag &= ~(ONLCR | XTABS);
3564# else
3565# ifdef TAB3
3566 tnew.c_oflag &= ~(ONLCR | TAB3);
3567# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003569# endif
3570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003572 tnew.c_cc[VMIN] = 1; // return after 1 char
3573 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003576 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003577 // Also reset ICANON here, otherwise on Solaris select() won't see
3578 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003579 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003580 tnew.c_cc[VMIN] = 1; // return after 1 char
3581 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
3584# if defined(HAVE_TERMIOS_H)
3585 {
3586 int n = 10;
3587
Bram Moolenaar0f873732019-12-05 20:28:46 +01003588 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3589 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3591 && errno == EINTR && n > 0)
3592 --n;
3593 }
3594# else
3595 ioctl(read_cmd_fd, TCSETA, &tnew);
3596# endif
3597
3598#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 /*
3600 * for "old" tty systems
3601 */
3602# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003603# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604# endif
3605 static struct sgttyb ttybold;
3606 struct sgttyb ttybnew;
3607
3608 if (first)
3609 {
3610 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003611 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613
3614 ttybnew = ttybold;
3615 if (tmode == TMODE_RAW)
3616 {
3617 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3618 ttybnew.sg_flags |= RAW;
3619 }
3620 else if (tmode == TMODE_SLEEP)
3621 ttybnew.sg_flags &= ~(ECHO);
3622 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3623#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003624 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625}
3626
3627/*
3628 * Try to get the code for "t_kb" from the stty setting
3629 *
3630 * Even if termcap claims a backspace key, the user's setting *should*
3631 * prevail. stty knows more about reality than termcap does, and if
3632 * somebody's usual erase key is DEL (which, for most BSD users, it will
3633 * be), they're going to get really annoyed if their erase key starts
3634 * doing forward deletes for no reason. (Eric Fischer)
3635 */
3636 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003637get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003639 ttyinfo_T info;
3640 char_u buf[2];
3641 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003643 if (get_tty_info(read_cmd_fd, &info) == OK)
3644 {
3645 intr_char = info.interrupt;
3646 buf[0] = info.backspace;
3647 buf[1] = NUL;
3648 add_termcode((char_u *)"kb", buf, FALSE);
3649
Bram Moolenaar0f873732019-12-05 20:28:46 +01003650 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003651 p = find_termcode((char_u *)"kD");
3652 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3653 do_fixdel(NULL);
3654 }
3655}
3656
3657/*
3658 * Obtain the characters that Backspace and Enter produce on "fd".
3659 * Returns OK or FAIL.
3660 */
3661 int
3662get_tty_info(int fd, ttyinfo_T *info)
3663{
3664#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665# ifdef HAVE_TERMIOS_H
3666 struct termios keys;
3667# else
3668 struct termio keys;
3669# endif
3670
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003671 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003673 info->backspace = keys.c_cc[VERASE];
3674 info->interrupt = keys.c_cc[VINTR];
3675 if (keys.c_iflag & ICRNL)
3676 info->enter = NL;
3677 else
3678 info->enter = CAR;
3679 if (keys.c_oflag & ONLCR)
3680 info->nl_does_cr = TRUE;
3681 else
3682 info->nl_does_cr = FALSE;
3683 return OK;
3684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003686 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 struct sgttyb keys;
3688
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003689 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003691 info->backspace = keys.sg_erase;
3692 info->interrupt = keys.sg_kill;
3693 info->enter = CAR;
3694 info->nl_does_cr = TRUE;
3695 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003698 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699}
3700
Bram Moolenaar0f873732019-12-05 20:28:46 +01003701#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003703static int mouse_ison = FALSE;
3704
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705/*
3706 * Set mouse clicks on or off.
3707 */
3708 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003709mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003711#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003712 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 int xterm_mouse_vers;
3715
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003716#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003717 if (!on)
3718 // Make sure not tracing mouse movements. Important when a button-down
3719 // was received but no release yet.
3720 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003721#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003722
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003723 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003724#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003725 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003726#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003727 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003728 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 return;
3730
3731 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003732
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003733#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003734 if (ttym_flags == TTYM_URXVT)
3735 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003736 out_str_nf((char_u *)
3737 (on
3738 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3739 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003740 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003741 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003742#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003743
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003744 if (ttym_flags == TTYM_SGR)
3745 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003746 // SGR mode supports columns above 223
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003747 out_str_nf((char_u *)
3748 (on
3749 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3750 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003751 mouse_ison = on;
3752 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003753
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003754#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003755 if (bevalterm_ison != (p_bevalterm && on))
3756 {
3757 bevalterm_ison = (p_bevalterm && on);
3758 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003759 // disable mouse movement events, enabling is below
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003760 out_str_nf((char_u *)
3761 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003762 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003763#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003764
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 if (xterm_mouse_vers > 0)
3766 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003767 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 out_str_nf((char_u *)
3769 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003770 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003771#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003772 bevalterm_ison
3773 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003774#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003775 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003777 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 out_str_nf((char_u *)
3779 (xterm_mouse_vers > 1
3780 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3781 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003782 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003785#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 else if (ttym_flags == TTYM_DEC)
3787 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003788 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003790 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003792 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003796#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 else
3798 {
3799 if (on)
3800 {
3801 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003802 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804 else
3805 {
3806 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003807 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003812#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003813 else
3814 {
3815 if (on)
3816 {
3817 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003818 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003819 }
3820 else
3821 {
3822 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003823 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003824 }
3825 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003826#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003827
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003828#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 else
3830 {
3831 if (on)
3832 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003833 // D - Enable Mouse up/down messages
3834 // L - Enable Left Button Reporting
3835 // M - Enable Middle Button Reporting
3836 // R - Enable Right Button Reporting
3837 // K - Enable SHIFT and CTRL key Reporting
3838 // + - Enable Advanced messaging of mouse moves and up/down messages
3839 // Q - Quiet No Ack
3840 // # - Numeric value of mouse pointer required
3841 // 0 = Multiview 2000 cursor, used as standard
3842 // 1 = Windows Arrow
3843 // 2 = Windows I Beam
3844 // 3 = Windows Hour Glass
3845 // 4 = Windows Cross Hair
3846 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003847# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003848 // Disables full feedback of pointer movements
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3850 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003851# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3853 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003854# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003855 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
3857 else
3858 {
3859 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3860 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003861 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 }
3863 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003864#endif
3865#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 else
3867 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003868 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (on)
3870 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3871 else
3872 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003873 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876}
3877
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003878#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003879/*
3880 * Called when 'balloonevalterm' changed.
3881 */
3882 void
3883mch_bevalterm_changed(void)
3884{
3885 mch_setmouse(mouse_ison);
3886}
3887#endif
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889/*
3890 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3891 */
3892 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003893check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894{
3895# ifdef FEAT_MOUSE_XTERM
3896 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003897# ifdef FEAT_MOUSE_URXVT
3898 && use_xterm_mouse() != 3
3899# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900# ifdef FEAT_GUI
3901 && !gui.in_use
3902# endif
3903 )
3904 {
3905 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003906 ? IF_EB("\233M", CSI_STR "M")
3907 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 if (*p_mouse != NUL)
3909 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003910 // force mouse off and maybe on to send possibly new mouse
3911 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 mch_setmouse(FALSE);
3913 setmouse();
3914 }
3915 }
3916 else
3917 del_mouse_termcode(KS_MOUSE);
3918# endif
3919
3920# ifdef FEAT_MOUSE_GPM
3921 if (!use_xterm_mouse()
3922# ifdef FEAT_GUI
3923 && !gui.in_use
3924# endif
3925 )
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003926 set_mouse_termcode(KS_GPM_MOUSE,
3927 (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3928 else
3929 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930# endif
3931
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003932# ifdef FEAT_SYSMOUSE
3933 if (!use_xterm_mouse()
3934# ifdef FEAT_GUI
3935 && !gui.in_use
3936# endif
3937 )
3938 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3939# endif
3940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003942 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 if (!use_xterm_mouse()
3944# ifdef FEAT_GUI
3945 && !gui.in_use
3946# endif
3947 )
3948 set_mouse_termcode(KS_JSBTERM_MOUSE,
3949 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3950 else
3951 del_mouse_termcode(KS_JSBTERM_MOUSE);
3952# endif
3953
3954# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003955 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3956 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 if (!use_xterm_mouse()
3958# ifdef FEAT_GUI
3959 && !gui.in_use
3960# endif
3961 )
3962 set_mouse_termcode(KS_NETTERM_MOUSE,
3963 (char_u *)IF_EB("\033}", ESC_STR "}"));
3964 else
3965 del_mouse_termcode(KS_NETTERM_MOUSE);
3966# endif
3967
3968# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003969 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003970 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971# ifdef FEAT_GUI
3972 && !gui.in_use
3973# endif
3974 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003975 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3976 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 else
3978 del_mouse_termcode(KS_DEC_MOUSE);
3979# endif
3980# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01003981 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003982 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983# ifdef FEAT_GUI
3984 && !gui.in_use
3985# endif
3986 )
3987 set_mouse_termcode(KS_PTERM_MOUSE,
3988 (char_u *) IF_EB("\033[", ESC_STR "["));
3989 else
3990 del_mouse_termcode(KS_PTERM_MOUSE);
3991# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003992# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003993 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003994# ifdef FEAT_GUI
3995 && !gui.in_use
3996# endif
3997 )
3998 {
3999 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02004000 ? IF_EB("\233*M", CSI_STR "*M")
4001 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004002
4003 if (*p_mouse != NUL)
4004 {
4005 mch_setmouse(FALSE);
4006 setmouse();
4007 }
4008 }
4009 else
4010 del_mouse_termcode(KS_URXVT_MOUSE);
4011# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004012 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004013# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004014 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004015# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004016 )
4017 {
4018 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02004019 ? IF_EB("\233<*M", CSI_STR "<*M")
4020 : IF_EB("\033[<*M", ESC_STR "[<*M")));
4021
4022 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
4023 ? IF_EB("\233<*m", CSI_STR "<*m")
4024 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004025
4026 if (*p_mouse != NUL)
4027 {
4028 mch_setmouse(FALSE);
4029 setmouse();
4030 }
4031 }
4032 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004033 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004034 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004035 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039#ifndef VMS
4040
4041/*
4042 * Try to get the current window size:
4043 * 1. with an ioctl(), most accurate method
4044 * 2. from the environment variables LINES and COLUMNS
4045 * 3. from the termcap
4046 * 4. keep using the old values
4047 * Return OK when size could be determined, FAIL otherwise.
4048 */
4049 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004050mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051{
4052 long rows = 0;
4053 long columns = 0;
4054 char_u *p;
4055
4056 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 * 1. try using an ioctl. It is the most accurate method.
4058 *
4059 * Try using TIOCGWINSZ first, some systems that have it also define
4060 * TIOCGSIZE but don't have a struct ttysize.
4061 */
4062# ifdef TIOCGWINSZ
4063 {
4064 struct winsize ws;
4065 int fd = 1;
4066
Bram Moolenaar0f873732019-12-05 20:28:46 +01004067 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 if (!isatty(fd) && isatty(read_cmd_fd))
4069 fd = read_cmd_fd;
4070 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4071 {
4072 columns = ws.ws_col;
4073 rows = ws.ws_row;
4074 }
4075 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004076# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077# ifdef TIOCGSIZE
4078 {
4079 struct ttysize ts;
4080 int fd = 1;
4081
Bram Moolenaar0f873732019-12-05 20:28:46 +01004082 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 if (!isatty(fd) && isatty(read_cmd_fd))
4084 fd = read_cmd_fd;
4085 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4086 {
4087 columns = ts.ts_cols;
4088 rows = ts.ts_lines;
4089 }
4090 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004091# endif // TIOCGSIZE
4092# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093
4094 /*
4095 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004096 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4097 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004099 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 {
4101 if ((p = (char_u *)getenv("LINES")))
4102 rows = atoi((char *)p);
4103 if ((p = (char_u *)getenv("COLUMNS")))
4104 columns = atoi((char *)p);
4105 }
4106
4107#ifdef HAVE_TGETENT
4108 /*
4109 * 3. try reading "co" and "li" entries from termcap
4110 */
4111 if (columns == 0 || rows == 0)
4112 getlinecol(&columns, &rows);
4113#endif
4114
4115 /*
4116 * 4. If everything fails, use the old values
4117 */
4118 if (columns <= 0 || rows <= 0)
4119 return FAIL;
4120
4121 Rows = rows;
4122 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004123 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 return OK;
4125}
4126
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004127#if defined(FEAT_TERMINAL) || defined(PROTO)
4128/*
4129 * Report the windows size "rows" and "cols" to tty "fd".
4130 */
4131 int
4132mch_report_winsize(int fd, int rows, int cols)
4133{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004134 int tty_fd;
4135 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004136
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004137 tty_fd = get_tty_fd(fd);
4138 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004139 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004140# if defined(TIOCSWINSZ)
4141 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004142
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004143 ws.ws_col = cols;
4144 ws.ws_row = rows;
4145 ws.ws_xpixel = cols * 5;
4146 ws.ws_ypixel = rows * 10;
4147 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4148 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4149 retval == 0 ? "success" : "failed");
4150# elif defined(TIOCSSIZE)
4151 struct ttysize ts;
4152
4153 ts.ts_cols = cols;
4154 ts.ts_lines = rows;
4155 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4156 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4157 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004158# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004159 if (tty_fd != fd)
4160 close(tty_fd);
4161 }
4162 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004163}
4164#endif
4165
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166/*
4167 * Try to set the window size to Rows and Columns.
4168 */
4169 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004170mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171{
4172 if (*T_CWS)
4173 {
4174 /*
4175 * NOTE: if you get an error here that term_set_winsize() is
4176 * undefined, check the output of configure. It could probably not
4177 * find a ncurses, termcap or termlib library.
4178 */
4179 term_set_winsize((int)Rows, (int)Columns);
4180 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004181 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 }
4183}
4184
Bram Moolenaar0f873732019-12-05 20:28:46 +01004185#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186
4187/*
4188 * Rows and/or Columns has changed.
4189 */
4190 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004191mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004193 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194}
4195
Bram Moolenaar205b8862011-09-07 15:04:31 +02004196/*
4197 * Wait for process "child" to end.
4198 * Return "child" if it exited properly, <= 0 on error.
4199 */
4200 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004201wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004202{
4203 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004204 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004205
4206 while (wait_pid != child)
4207 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004208 // When compiled with Python threads are probably used, in which case
4209 // wait() sometimes hangs for no obvious reason. Use waitpid()
4210 // instead and loop (like the GUI). Also needed for other interfaces,
4211 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004212# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004213 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004214# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004215 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004216# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004217 if (wait_pid == 0)
4218 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004219 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004220 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004221 if (++delay_msec > 10)
4222 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004223 continue;
4224 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004225 if (wait_pid <= 0
4226# ifdef ECHILD
4227 && errno == ECHILD
4228# endif
4229 )
4230 break;
4231 }
4232 return wait_pid;
4233}
4234
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004235#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004236/*
4237 * Set the environment for a child process.
4238 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004239 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004240set_child_environment(
4241 long rows,
4242 long columns,
4243 char *term,
4244 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004245{
4246# ifdef HAVE_SETENV
4247 char envbuf[50];
4248# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004249 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004250 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004251 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004252 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004253 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004254# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004255 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004256# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004257# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004258 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004259# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004260# endif
4261
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004262# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004263 setenv("TERM", term, 1);
4264 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004265 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004266 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004267 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004268 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004269 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004270 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004271 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004272# ifdef FEAT_TERMINAL
4273 if (is_terminal)
4274 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004275 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004276 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4277 }
4278# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004279# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004280 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004281# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004282# else
4283 /*
4284 * Putenv does not copy the string, it has to remain valid.
4285 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004286 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004287 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004288 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4289 putenv(envbuf_Term);
4290 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004291 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004292 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4293 putenv(envbuf_Lines);
4294 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4295 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004296 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004297 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004298 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004299# ifdef FEAT_TERMINAL
4300 if (is_terminal)
4301 {
4302 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004303 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004304 putenv(envbuf_Version);
4305 }
4306# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004307# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004308 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4309 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4310 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004311# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004312# endif
4313}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004314
4315 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004316set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004317{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004318 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004319}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004320#endif
4321
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004322#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004323/*
4324 * Open a PTY, with FD for the master and slave side.
4325 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004326 * When successful both file descriptors are stored and the allocated pty name
4327 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004328 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004329 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004330open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004331{
4332 char *tty_name;
4333
Bram Moolenaar59386482019-02-10 22:43:46 +01004334 if (name1 != NULL)
4335 *name1 = NULL;
4336 if (name2 != NULL)
4337 *name2 = NULL;
4338
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004339 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004340 if (*pty_master_fd >= 0)
4341 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004342 // Leaving out O_NOCTTY may lead to waitpid() always returning
4343 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4344 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004345#ifdef O_NOCTTY
4346 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4347#else
4348 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4349#endif
4350 if (*pty_slave_fd < 0)
4351 {
4352 close(*pty_master_fd);
4353 *pty_master_fd = -1;
4354 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004355 else
4356 {
4357 if (name1 != NULL)
4358 *name1 = vim_strsave((char_u *)tty_name);
4359 if (name2 != NULL)
4360 *name2 = vim_strsave((char_u *)tty_name);
4361 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004362 }
4363}
4364#endif
4365
Bram Moolenaarfae42832017-08-01 22:24:26 +02004366/*
4367 * Send SIGINT to a child process if "c" is an interrupt character.
4368 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004369 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004370may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4371{
4372# ifdef SIGINT
4373 if (c == Ctrl_C || c == intr_char)
4374 {
4375# ifdef HAVE_SETSID
4376 kill(-pid, SIGINT);
4377# else
4378 kill(0, SIGINT);
4379# endif
4380 if (wpid > 0)
4381 kill(wpid, SIGINT);
4382 }
4383# endif
4384}
4385
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004386#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004387
Bram Moolenaar0f873732019-12-05 20:28:46 +01004388/*
4389 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4390 * pointers, the last one is NULL.
4391 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4392 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004393 int
4394unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004395 char_u *cmd,
4396 char ***argvp,
4397 char_u **sh_tofree,
4398 char_u **shcf_tofree)
4399{
4400 char **argv = NULL;
4401 int argc;
4402
4403 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004404 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004405 return FAIL;
4406
4407 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4408 return FAIL;
4409 *argvp = argv;
4410
4411 if (cmd != NULL)
4412 {
4413 char_u *s;
4414 char_u *p;
4415
4416 if (extra_shell_arg != NULL)
4417 argv[argc++] = (char *)extra_shell_arg;
4418
Bram Moolenaar0f873732019-12-05 20:28:46 +01004419 // Break 'shellcmdflag' into white separated parts. This doesn't
4420 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004421 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004422 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004423 return FAIL;
4424 s = *shcf_tofree;
4425 p = p_shcf;
4426 while (*p != NUL)
4427 {
4428 argv[argc++] = (char *)s;
4429 while (*p && *p != ' ' && *p != TAB)
4430 *s++ = *p++;
4431 *s++ = NUL;
4432 p = skipwhite(p);
4433 }
4434
4435 argv[argc++] = (char *)cmd;
4436 }
4437 argv[argc] = NULL;
4438 return OK;
4439}
4440#endif
4441
4442#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4443/*
4444 * Use a terminal window to run a shell command in.
4445 */
4446 static int
4447mch_call_shell_terminal(
4448 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004449 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004450{
4451 jobopt_T opt;
4452 char **argv = NULL;
4453 char_u *tofree1 = NULL;
4454 char_u *tofree2 = NULL;
4455 int retval = -1;
4456 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004457 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004458 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004459 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004460
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004461 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004462 goto theend;
4463
4464 init_job_options(&opt);
4465 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4466 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004467 if (buf == NULL)
4468 goto theend;
4469
4470 job = term_getjob(buf->b_term);
4471 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004472
Bram Moolenaar0f873732019-12-05 20:28:46 +01004473 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004474 aucmd_prepbuf(&aco, buf);
4475
4476 clear_oparg(&oa);
4477 while (term_use_loop())
4478 {
4479 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4480 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004481 // If terminal_loop() returns OK we got a key that is handled
4482 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004483 if (terminal_loop(TRUE) == OK)
4484 normal_cmd(&oa, TRUE);
4485 }
4486 else
4487 normal_cmd(&oa, TRUE);
4488 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004489 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004490 ch_log(NULL, "system command finished");
4491
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004492 job_unref(job);
4493
Bram Moolenaar0f873732019-12-05 20:28:46 +01004494 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004495 aucmd_restbuf(&aco);
4496
4497 wait_return(TRUE);
4498 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4499
4500theend:
4501 vim_free(argv);
4502 vim_free(tofree1);
4503 vim_free(tofree2);
4504 return retval;
4505}
4506#endif
4507
4508#ifdef USE_SYSTEM
4509/*
4510 * Use system() to start the shell: simple but slow.
4511 */
4512 static int
4513mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004514 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004515 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516{
4517#ifdef VMS
4518 char *ifn = NULL;
4519 char *ofn = NULL;
4520#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004521 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004522 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004523 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524
4525 out_flush();
4526
4527 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004528 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529
Bram Moolenaar62b42182010-09-21 22:09:37 +02004530# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004531 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004532 loose_clipboard();
4533# endif
4534
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (cmd == NULL)
4536 x = system((char *)p_sh);
4537 else
4538 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004539# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 if (ofn = strchr((char *)cmd, '>'))
4541 *ofn++ = '\0';
4542 if (ifn = strchr((char *)cmd, '<'))
4543 {
4544 char *p;
4545
4546 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004547 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (p)
4549 *p = '\0';
4550 }
4551 if (ofn)
4552 x = vms_sys((char *)cmd, ofn, ifn);
4553 else
4554 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004555# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004556 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004558 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 if (newcmd == NULL)
4560 x = 0;
4561 else
4562 {
4563 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4564 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4565 (char *)p_shcf,
4566 (char *)cmd);
4567 x = system((char *)newcmd);
4568 vim_free(newcmd);
4569 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 }
4572# ifdef VMS
4573 x = vms_sys_status(x);
4574# endif
4575 if (emsg_silent)
4576 ;
4577 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004578 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 else if (x && !(options & SHELL_SILENT))
4580 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004581 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 msg_outnum((long)x);
4583 msg_putchar('\n');
4584 }
4585
4586 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004587 {
4588 // The shell may have messed with the mode, always set it.
4589 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004590 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004593# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4594 restore_clipboard();
4595# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004597}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598
Bram Moolenaar0f873732019-12-05 20:28:46 +01004599#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
Bram Moolenaar0f873732019-12-05 20:28:46 +01004601# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4602 // 127, some shells use that already
4603# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
Bram Moolenaar13568252018-03-16 20:46:58 +01004605/*
4606 * Don't use system(), use fork()/exec().
4607 */
4608 static int
4609mch_call_shell_fork(
4610 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004611 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004612{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004613 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004615 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 pid_t wait_pid = 0;
4617# ifdef HAVE_UNION_WAIT
4618 union wait status;
4619# else
4620 int status = -1;
4621# endif
4622 int retval = -1;
4623 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004624 char_u *tofree1 = NULL;
4625 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004627 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004628# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004630# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004631 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 int fd_fromshell[2];
4633 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004634 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635
4636 out_flush();
4637 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004638 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004639 if (tmode == TMODE_RAW)
4640 // The shell may have messed with the mode, always set it later.
4641 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004643 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004644 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004645
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004647 * For the GUI, when writing the output into the buffer and when reading
4648 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4649 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004651 if ((options & (SHELL_READ|SHELL_WRITE))
4652# ifdef FEAT_GUI
4653 || (gui.in_use && show_shell_mess)
4654# endif
4655 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004657# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 /*
4659 * Try to open a master pty.
4660 * If this works, open the slave pty.
4661 * If the slave can't be opened, close the master pty.
4662 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004663 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004664 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 /*
4666 * If not opening a pty or it didn't work, try using pipes.
4667 */
4668 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
4671 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004672 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
4674 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004675 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 {
4677 close(fd_toshell[0]);
4678 close(fd_toshell[1]);
4679 }
4680 }
4681 if (pipe_error)
4682 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004683 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 out_flush();
4685 }
4686 }
4687 }
4688
Bram Moolenaar0f873732019-12-05 20:28:46 +01004689 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004691 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004692 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004693 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004694 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004696 UNBLOCK_SIGNALS(&curset);
4697
Bram Moolenaar32526b32019-01-19 17:43:09 +01004698 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004699 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004701 || (gui.in_use && show_shell_mess)
4702# endif
4703 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004705# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004706 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 {
4708 close(pty_master_fd);
4709 close(pty_slave_fd);
4710 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004711 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004712# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
4714 close(fd_toshell[0]);
4715 close(fd_toshell[1]);
4716 close(fd_fromshell[0]);
4717 close(fd_fromshell[1]);
4718 }
4719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004721 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004723 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004724 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
Bram Moolenaar819524702018-02-27 19:10:00 +01004726# ifdef FEAT_JOB_CHANNEL
4727 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004728 {
4729 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004730 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004731 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004732# endif
4733
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 if (!show_shell_mess || (options & SHELL_EXPAND))
4735 {
4736 int fd;
4737
4738 /*
4739 * Don't want to show any message from the shell. Can't just
4740 * close stdout and stderr though, because some systems will
4741 * break if you try to write to them after that, so we must
4742 * use dup() to replace them with something else -- webb
4743 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4744 * waiting for input.
4745 */
4746 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4747 fclose(stdin);
4748 fclose(stdout);
4749 fclose(stderr);
4750
4751 /*
4752 * If any of these open()'s and dup()'s fail, we just continue
4753 * anyway. It's not fatal, and on most systems it will make
4754 * no difference at all. On a few it will cause the execvp()
4755 * to exit with a non-zero status even when the completion
4756 * could be done, which is nothing too serious. If the open()
4757 * or dup() failed we'd just do the same thing ourselves
4758 * anyway -- webb
4759 */
4760 if (fd >= 0)
4761 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004762 vim_ignored = dup(fd); // To replace stdin (fd 0)
4763 vim_ignored = dup(fd); // To replace stdout (fd 1)
4764 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
Bram Moolenaar0f873732019-12-05 20:28:46 +01004766 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 close(fd);
4768 }
4769 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004770 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004772 || gui.in_use
4773# endif
4774 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
4776
Bram Moolenaardf177f62005-02-22 08:39:57 +00004777# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004778 // Create our own process group, so that the child and all its
4779 // children can be kill()ed. Don't do this when using pipes,
4780 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004781 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004782 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004783 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004784# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004785 // When doing "!xterm&" and 'shell' is bash: the shell
4786 // will exit and send SIGHUP to all processes in its
4787 // group, killing the just started process. Ignore SIGHUP
4788 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004789 signal(SIGHUP, SIG_IGN);
4790# endif
4791 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004792# endif
4793# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004794 if (pty_slave_fd >= 0)
4795 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004796 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004797 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004798 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004799# ifdef TIOCSCTTY
4800 // Try to become controlling tty (probably doesn't work,
4801 // unless run by root)
4802 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4803# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004804 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004805# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004806 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807
Bram Moolenaara5792f52005-11-23 21:25:05 +00004808 /*
4809 * stderr is only redirected when using the GUI, so that a
4810 * program like gpg can still access the terminal to get a
4811 * passphrase using stderr.
4812 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004813# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 if (pty_master_fd >= 0)
4815 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004816 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817
Bram Moolenaar0f873732019-12-05 20:28:46 +01004818 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004820 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004822 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004823 if (gui.in_use)
4824 {
4825 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004826 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828
Bram Moolenaar0f873732019-12-05 20:28:46 +01004829 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 }
4831 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004834 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 close(fd_toshell[1]);
4836 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004837 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 close(fd_toshell[0]);
4839
Bram Moolenaar0f873732019-12-05 20:28:46 +01004840 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 close(fd_fromshell[0]);
4842 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004843 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 close(fd_fromshell[1]);
4845
Bram Moolenaara5792f52005-11-23 21:25:05 +00004846# ifdef FEAT_GUI
4847 if (gui.in_use)
4848 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004849 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004850 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004851 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004852 }
4853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004856
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 /*
4858 * There is no type cast for the argv, because the type may be
4859 * different on different machines. This may cause a warning
4860 * message with strict compilers, don't worry about it.
4861 * Call _exit() instead of exit() to avoid closing the connection
4862 * to the X server (esp. with GTK, which uses atexit()).
4863 */
4864 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004865 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004867 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
4869 /*
4870 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004871 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 */
4873 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004874 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004875 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004876# ifdef FEAT_JOB_CHANNEL
4877 ++dont_check_job_ended;
4878# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 /*
4880 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004881 * This is also used to pipe stdin/stdout to/from the external
4882 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004884 if ((options & (SHELL_READ|SHELL_WRITE))
4885# ifdef FEAT_GUI
4886 || (gui.in_use && show_shell_mess)
4887# endif
4888 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004890# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004892 int buffer_off = 0; // valid bytes in buffer[]
4893 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4894 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 int len;
4896 int p_more_save;
4897 int old_State;
4898 int c;
4899 int toshell_fd;
4900 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004901 garray_T ga;
4902 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004903# ifdef ELAPSED_FUNC
4904 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906
Bram Moolenaardf177f62005-02-22 08:39:57 +00004907# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 if (pty_master_fd >= 0)
4909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 fromshell_fd = pty_master_fd;
4911 toshell_fd = dup(pty_master_fd);
4912 }
4913 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004914# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
4916 close(fd_toshell[0]);
4917 close(fd_fromshell[1]);
4918 toshell_fd = fd_toshell[1];
4919 fromshell_fd = fd_fromshell[0];
4920 }
4921
4922 /*
4923 * Write to the child if there are typed characters.
4924 * Read from the child if there are characters available.
4925 * Repeat the reading a few times if more characters are
4926 * available. Need to check for typed keys now and then, but
4927 * not too often (delays when no chars are available).
4928 * This loop is quit if no characters can be read from the pty
4929 * (WaitForChar detected special condition), or there are no
4930 * characters available and the child has exited.
4931 * Only check if the child has exited when there is no more
4932 * output. The child may exit before all the output has
4933 * been printed.
4934 *
4935 * Currently this busy loops!
4936 * This can probably dead-lock when the write blocks!
4937 */
4938 p_more_save = p_more;
4939 p_more = FALSE;
4940 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004941 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004943 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004944 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004945 // Fork a process that will write the lines to the
4946 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004947 if ((wpid = fork()) == -1)
4948 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004949 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004950 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004951 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004952 {
4953 linenr_T lnum = curbuf->b_op_start.lnum;
4954 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004955 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004956 size_t l;
4957
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004958 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004959 for (;;)
4960 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004961 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004962 if (l == 0)
4963 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004964 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004965 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004966 len = write(toshell_fd, "", (size_t)1);
4967 else
4968 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004969 char_u *s = vim_strchr(lp + written, NL);
4970
Bram Moolenaar89d40322006-08-29 15:30:07 +00004971 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004972 s == NULL ? l
4973 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004974 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004975 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004976 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004977 // Finished a line, add a NL, unless this line
4978 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004979 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004980 || (!curbuf->b_p_bin
4981 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004982 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004983 && (lnum !=
4984 curbuf->b_ml.ml_line_count
4985 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004986 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004987 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004988 ++lnum;
4989 if (lnum > curbuf->b_op_end.lnum)
4990 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004991 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00004992 close(toshell_fd);
4993 toshell_fd = -1;
4994 break;
4995 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004996 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004997 written = 0;
4998 }
4999 else if (len > 0)
5000 written += len;
5001 }
5002 _exit(0);
5003 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005004 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005005 {
5006 close(toshell_fd);
5007 toshell_fd = -1;
5008 }
5009 }
5010
5011 if (options & SHELL_READ)
5012 ga_init2(&ga, 1, BUFLEN);
5013
5014 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005015# ifdef ELAPSED_FUNC
5016 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005017# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 for (;;)
5019 {
5020 /*
5021 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005022 * if there are any.
5023 * Don't do this if we are expanding wild cards (would eat
5024 * typeahead).
5025 * Don't do this when filtering and terminal is in cooked
5026 * mode, the shell command will handle the I/O. Avoids
5027 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005028 * Don't get characters when the child has already
5029 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005030 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005031 * while (noread_cnt > 4), avoids that ":r !ls" eats
5032 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 */
5034 len = 0;
5035 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005036 && ((options &
5037 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5038 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005039# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005040 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005041# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005042 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005043 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005044 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005046 if (ta_len == 0)
5047 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005048 // Get extra characters when we don't have any.
5049 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005050 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005051# ifdef ELAPSED_FUNC
5052 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005053# endif
5054 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5055 }
5056 if (ta_len > 0 || len > 0)
5057 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 /*
5059 * For pipes:
5060 * Check for CTRL-C: send interrupt signal to child.
5061 * Check for CTRL-D: EOF, close pipe to child.
5062 */
5063 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5064 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 /*
5066 * Send SIGINT to the child's group or all
5067 * processes in our group.
5068 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005069 may_send_sigint(ta_buf[ta_len], pid, wpid);
5070
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 if (pty_master_fd < 0 && toshell_fd >= 0
5072 && ta_buf[ta_len] == Ctrl_D)
5073 {
5074 close(toshell_fd);
5075 toshell_fd = -1;
5076 }
5077 }
5078
Bram Moolenaarf4140482020-02-15 23:06:45 +01005079 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080
5081 /*
5082 * For pipes: echo the typed characters.
5083 * For a pty this does not seem to work.
5084 */
5085 if (pty_master_fd < 0)
5086 {
5087 for (i = ta_len; i < ta_len + len; ++i)
5088 {
5089 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5090 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 else if (has_mbyte)
5092 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005093 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094
5095 msg_outtrans_len(ta_buf + i, l);
5096 i += l - 1;
5097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 else
5099 msg_outtrans_len(ta_buf + i, 1);
5100 }
5101 windgoto(msg_row, msg_col);
5102 out_flush();
5103 }
5104
5105 ta_len += len;
5106
5107 /*
5108 * Write the characters to the child, unless EOF has
5109 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005110 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005111 * When writing buffer lines, drop the typed
5112 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005114 if (options & SHELL_WRITE)
5115 ta_len = 0;
5116 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
5118 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5119 if (len > 0)
5120 {
5121 ta_len -= len;
5122 mch_memmove(ta_buf, ta_buf + len, ta_len);
5123 }
5124 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127
Bram Moolenaardf177f62005-02-22 08:39:57 +00005128 if (got_int)
5129 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005130 // CTRL-C sends a signal to the child, we ignore it
5131 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005132# ifdef HAVE_SETSID
5133 kill(-pid, SIGINT);
5134# else
5135 kill(0, SIGINT);
5136# endif
5137 if (wpid > 0)
5138 kill(wpid, SIGINT);
5139 got_int = FALSE;
5140 }
5141
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 /*
5143 * Check if the child has any characters to be printed.
5144 * Read them and write them to our window. Repeat this as
5145 * long as there is something to do, avoid the 10ms wait
5146 * for mch_inchar(), or sending typeahead characters to
5147 * the external process.
5148 * TODO: This should handle escape sequences, compatible
5149 * to some terminal (vt52?).
5150 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005151 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005152 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005154 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005157 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005159
5160 noread_cnt = 0;
5161 if (options & SHELL_READ)
5162 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005163 // Do NUL -> NL translation, append NL separated
5164 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005165 for (i = 0; i < len; ++i)
5166 {
5167 if (buffer[i] == NL)
5168 append_ga_line(&ga);
5169 else if (buffer[i] == NUL)
5170 ga_append(&ga, NL);
5171 else
5172 ga_append(&ga, buffer[i]);
5173 }
5174 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005175 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
5177 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005178 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
Bram Moolenaardf177f62005-02-22 08:39:57 +00005180 len += buffer_off;
5181 buffer[len] = NUL;
5182
Bram Moolenaar0f873732019-12-05 20:28:46 +01005183 // Check if the last character in buffer[] is
5184 // incomplete, keep these bytes for the next
5185 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 for (p = buffer; p < buffer + len; p += l)
5187 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005188 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005190 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 else if (MB_BYTE2LEN(*p) != l)
5192 break;
5193 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005194 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005196 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if (len >= 12)
5198 ++p;
5199 else
5200 {
5201 buffer_off = len;
5202 continue;
5203 }
5204 }
5205 c = *p;
5206 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005207 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (p < buffer + len)
5209 {
5210 *p = c;
5211 buffer_off = (buffer + len) - p;
5212 mch_memmove(buffer, p, buffer_off);
5213 continue;
5214 }
5215 buffer_off = 0;
5216 }
5217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
5219 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005220 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
5222
5223 windgoto(msg_row, msg_col);
5224 cursor_on();
5225 out_flush();
5226 if (got_int)
5227 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005228
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005229# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005230 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005231 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005232 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005233
Bram Moolenaar0f873732019-12-05 20:28:46 +01005234 // Avoid that we keep looping here without
5235 // checking for a CTRL-C for a long time. Don't
5236 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005237 if (msec > 2000)
5238 {
5239 noread_cnt = 5;
5240 break;
5241 }
5242 }
5243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
5245
Bram Moolenaar0f873732019-12-05 20:28:46 +01005246 // If we already detected the child has finished, continue
5247 // reading output for a short while. Some text may be
5248 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005249 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005250 {
5251 if (noread_cnt < 5)
5252 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005253 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005254 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005255
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 /*
5257 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005258 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005260# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005261 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005262# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5266 || (wait_pid == pid && WIFEXITED(status)))
5267 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005268 // Don't break the loop yet, try reading more
5269 // characters from "fromshell_fd" first. When using
5270 // pipes there might still be something to read and
5271 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005274 else
5275 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005276
Bram Moolenaar95a51352013-03-21 22:53:50 +01005277# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005278 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005279 clip_update();
5280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
5282finished:
5283 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005284 if (options & SHELL_READ)
5285 {
5286 if (ga.ga_len > 0)
5287 {
5288 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005289 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005290 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005291 }
5292 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005293 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005294 ga_clear(&ga);
5295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 /*
5298 * Give all typeahead that wasn't used back to ui_inchar().
5299 */
5300 if (ta_len)
5301 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 State = old_State;
5303 if (toshell_fd >= 0)
5304 close(toshell_fd);
5305 close(fromshell_fd);
5306 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005307# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005308 else
5309 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005310 long delay_msec = 1;
5311
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005312 if (tmode == TMODE_RAW)
5313 // possibly disables modifyOtherKeys, so that the system
5314 // can recognize CTRL-C
5315 out_str(T_CTE);
Bram Moolenaar0981c872020-08-23 14:28:37 +02005316
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005317 /*
5318 * Similar to the loop above, but only handle X events, no
5319 * I/O.
5320 */
5321 for (;;)
5322 {
5323 if (got_int)
5324 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005325 // CTRL-C sends a signal to the child, we ignore it
5326 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005327# ifdef HAVE_SETSID
5328 kill(-pid, SIGINT);
5329# else
5330 kill(0, SIGINT);
5331# endif
5332 got_int = FALSE;
5333 }
5334# ifdef __NeXT__
5335 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5336# else
5337 wait_pid = waitpid(pid, &status, WNOHANG);
5338# endif
5339 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5340 || (wait_pid == pid && WIFEXITED(status)))
5341 {
5342 wait_pid = pid;
5343 break;
5344 }
5345
Bram Moolenaar0f873732019-12-05 20:28:46 +01005346 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005347 clip_update();
5348
Bram Moolenaar0f873732019-12-05 20:28:46 +01005349 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005350 // less time, gradually wait longer.
5351 mch_delay(delay_msec,
5352 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005353 if (++delay_msec > 10)
5354 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005355 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005356
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005357 if (tmode == TMODE_RAW)
5358 // possibly enables modifyOtherKeys again
5359 out_str(T_CTI);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005360 }
5361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
5363 /*
5364 * Wait until our child has exited.
5365 * Ignore wait() returning pids of other children and returning
5366 * because of some signal like SIGWINCH.
5367 * Don't wait if wait_pid was already set above, indicating the
5368 * child already exited.
5369 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005370 if (wait_pid != pid)
5371 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
Bram Moolenaar624891f2010-10-13 16:22:09 +02005373# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005374 // Close slave side of pty. Only do this after the child has
5375 // exited, otherwise the child may hang when it tries to write on
5376 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005377 if (pty_master_fd >= 0)
5378 close(pty_slave_fd);
5379# endif
5380
Bram Moolenaar0f873732019-12-05 20:28:46 +01005381 // Make sure the child that writes to the external program is
5382 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005383 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005384 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005385 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005386 wait4pid(wpid, NULL);
5387 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005388
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005389# ifdef FEAT_JOB_CHANNEL
5390 --dont_check_job_ended;
5391# endif
5392
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 /*
5394 * Set to raw mode right now, otherwise a CTRL-C after
5395 * catch_signals() will kill Vim.
5396 */
5397 if (tmode == TMODE_RAW)
5398 settmode(TMODE_RAW);
5399 did_settmode = TRUE;
5400 set_signals();
5401
5402 if (WIFEXITED(status))
5403 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005404 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005406 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
5408 if (retval == EXEC_FAILED)
5409 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005410 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 msg_outtrans(p_sh);
5412 msg_putchar('\n');
5413 }
5414 else if (!(options & SHELL_SILENT))
5415 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005416 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 msg_outnum((long)retval);
5418 msg_putchar('\n');
5419 }
5420 }
5421 }
5422 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005423 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 }
5425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
5427error:
5428 if (!did_settmode)
5429 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005430 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005432 vim_free(argv);
5433 vim_free(tofree1);
5434 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
5436 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005437}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005438#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005439
5440 int
5441mch_call_shell(
5442 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005443 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005444{
5445#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5446 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5447 return mch_call_shell_terminal(cmd, options);
5448#endif
5449#ifdef USE_SYSTEM
5450 return mch_call_shell_system(cmd, options);
5451#else
5452 return mch_call_shell_fork(cmd, options);
5453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454}
5455
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005456#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005457 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005458mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005459{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005460 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005461 int fd_in[2] = {-1, -1}; // for stdin
5462 int fd_out[2] = {-1, -1}; // for stdout
5463 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005464 int pty_master_fd = -1;
5465 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005466 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005467 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5468 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5469 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005470 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005471 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5472 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005473 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005474 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005475 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005476
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005477 if (use_out_for_err && use_null_for_out)
5478 use_null_for_err = TRUE;
5479
Bram Moolenaar0f873732019-12-05 20:28:46 +01005480 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005481 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005482
Bram Moolenaarb2412082017-08-20 18:09:14 +02005483 if (options->jo_pty
5484 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005485 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005486 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005487 open_pty(&pty_master_fd, &pty_slave_fd,
5488 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005489
Bram Moolenaar0f873732019-12-05 20:28:46 +01005490 // TODO: without the channel feature connect the child to /dev/null?
5491 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005492 if (use_file_for_in)
5493 {
5494 char_u *fname = options->jo_io_name[PART_IN];
5495
5496 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5497 if (fd_in[0] < 0)
5498 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005499 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005500 goto failed;
5501 }
5502 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005503 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005504 // When writing buffer lines to the input don't use the pty, so that
5505 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005506 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5507 && pipe(fd_in) < 0)
5508 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005509
5510 if (use_file_for_out)
5511 {
5512 char_u *fname = options->jo_io_name[PART_OUT];
5513
5514 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5515 if (fd_out[1] < 0)
5516 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005517 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005518 goto failed;
5519 }
5520 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005521 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005522 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005523
5524 if (use_file_for_err)
5525 {
5526 char_u *fname = options->jo_io_name[PART_ERR];
5527
5528 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5529 if (fd_err[1] < 0)
5530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005531 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005532 goto failed;
5533 }
5534 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005535 else if (!use_out_for_err && !use_null_for_err
5536 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005537 goto failed;
5538
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005539 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5540 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005541 if (options->jo_set & JO_CHANNEL)
5542 {
5543 channel = options->jo_channel;
5544 if (channel != NULL)
5545 ++channel->ch_refcount;
5546 }
5547 else
5548 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005549 if (channel == NULL)
5550 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005551 if (job->jv_tty_out != NULL)
5552 ch_log(channel, "using pty %s on fd %d",
5553 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005554 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005555
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005556 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005557 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005558 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005559 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005560 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005561 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005562 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005563 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005564 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005565 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005566 int null_fd = -1;
5567 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005568
Bram Moolenaar0f873732019-12-05 20:28:46 +01005569 // child
5570 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005571 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005572
Bram Moolenaar819524702018-02-27 19:10:00 +01005573# ifdef FEAT_JOB_CHANNEL
5574 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005575 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005576 ch_logfile((char_u *)"", (char_u *)"");
5577# endif
5578
Bram Moolenaar835dc632016-02-07 14:27:38 +01005579# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005580 // Create our own process group, so that the child and all its
5581 // children can be kill()ed. Don't do this when using pipes,
5582 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005583 (void)setsid();
5584# endif
5585
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005586# ifdef FEAT_TERMINAL
5587 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005588 {
5589 char *term = (char *)T_NAME;
5590
5591#ifdef FEAT_GUI
5592 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005593 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005594 term = getenv("TERM");
5595#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005596 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005597 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005598 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005599 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005600 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005601 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005602 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005603 else if (t_colors > 16)
5604 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005605 else
5606 term = "xterm";
5607 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005608 set_child_environment(
5609 (long)options->jo_term_rows,
5610 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005611 term,
5612 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005613 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005614 else
5615# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005616 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005617
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005618 if (options->jo_env != NULL)
5619 {
5620 dict_T *dict = options->jo_env;
5621 hashitem_T *hi;
5622 int todo = (int)dict->dv_hashtab.ht_used;
5623
5624 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5625 if (!HASHITEM_EMPTY(hi))
5626 {
5627 typval_T *item = &dict_lookup(hi)->di_tv;
5628
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005629 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005630 --todo;
5631 }
5632 }
5633
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005634 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005635 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005636 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005637 if (null_fd < 0)
5638 {
5639 perror("opening /dev/null failed");
5640 _exit(OPEN_NULL_FAILED);
5641 }
5642 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005643
Bram Moolenaar223896d2017-08-02 22:33:28 +02005644 if (pty_slave_fd >= 0)
5645 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005646 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005647 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005648# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005649 // Try to become controlling tty (probably doesn't work,
5650 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005651 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5652# endif
5653 }
5654
Bram Moolenaar0f873732019-12-05 20:28:46 +01005655 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005656 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005657 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005658 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005659 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005660 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005661 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005662 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005663
Bram Moolenaar0f873732019-12-05 20:28:46 +01005664 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005665 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005666 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005667 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005668 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005669 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005670 }
5671 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005672 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005673 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005674 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005675 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005676 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005677
Bram Moolenaar0f873732019-12-05 20:28:46 +01005678 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005679 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005680 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005681 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005682 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005683 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005684 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005685 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005686
5687 if (fd_in[0] >= 0)
5688 close(fd_in[0]);
5689 if (fd_in[1] >= 0)
5690 close(fd_in[1]);
5691 if (fd_out[0] >= 0)
5692 close(fd_out[0]);
5693 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005694 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005695 if (fd_err[0] >= 0)
5696 close(fd_err[0]);
5697 if (fd_err[1] >= 0)
5698 close(fd_err[1]);
5699 if (pty_master_fd >= 0)
5700 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005701 close(pty_master_fd); // not used in the child
5702 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005703 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005704
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005705 if (null_fd >= 0)
5706 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005707
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005708 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5709 _exit(EXEC_FAILED);
5710
Bram Moolenaar0f873732019-12-05 20:28:46 +01005711 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005712 execvp(argv[0], argv);
5713
Bram Moolenaar4694a172016-04-21 14:05:23 +02005714 if (stderr_works)
5715 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005716# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005717 // calling free_all_mem() here causes problems. Ignore valgrind
5718 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005719# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005720 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005721 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005722
Bram Moolenaar0f873732019-12-05 20:28:46 +01005723 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005724 UNBLOCK_SIGNALS(&curset);
5725
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005726 job->jv_pid = pid;
5727 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005728 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005729
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005730 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005731 close(pty_slave_fd); // not used in the parent
5732 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005733 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005734 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005735 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005736 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005737 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005738 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005739 if (channel != NULL)
5740 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005741 int in_fd = INVALID_FD;
5742 int out_fd = INVALID_FD;
5743 int err_fd = INVALID_FD;
5744
5745 if (!(use_file_for_in || use_null_for_in))
5746 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5747
5748 if (!(use_file_for_out || use_null_for_out))
5749 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5750
5751 // When using pty_master_fd only set it for stdout, do not duplicate
5752 // it for stderr, it only needs to be read once.
5753 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5754 {
5755 if (fd_err[0] >= 0)
5756 err_fd = fd_err[0];
5757 else if (out_fd != pty_master_fd)
5758 err_fd = pty_master_fd;
5759 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005760
5761 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005762 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005763 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005764 else
5765 {
5766 if (fd_in[1] >= 0)
5767 close(fd_in[1]);
5768 if (fd_out[0] >= 0)
5769 close(fd_out[0]);
5770 if (fd_err[0] >= 0)
5771 close(fd_err[0]);
5772 if (pty_master_fd >= 0)
5773 close(pty_master_fd);
5774 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005775
Bram Moolenaar0f873732019-12-05 20:28:46 +01005776 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005777 return;
5778
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005779failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005780 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005781 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005782 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005783 if (fd_in[1] >= 0)
5784 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005785 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005786 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005787 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005788 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005789 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005790 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005791 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005792 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005793 if (pty_master_fd >= 0)
5794 close(pty_master_fd);
5795 if (pty_slave_fd >= 0)
5796 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005797}
5798
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005799 static char_u *
5800get_signal_name(int sig)
5801{
5802 int i;
5803 char_u numbuf[NUMBUFLEN];
5804
5805 if (sig == SIGKILL)
5806 return vim_strsave((char_u *)"kill");
5807
5808 for (i = 0; signal_info[i].sig != -1; i++)
5809 if (sig == signal_info[i].sig)
5810 return strlow_save((char_u *)signal_info[i].name);
5811
5812 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5813 return vim_strsave(numbuf);
5814}
5815
Bram Moolenaar835dc632016-02-07 14:27:38 +01005816 char *
5817mch_job_status(job_T *job)
5818{
5819# ifdef HAVE_UNION_WAIT
5820 union wait status;
5821# else
5822 int status = -1;
5823# endif
5824 pid_t wait_pid = 0;
5825
5826# ifdef __NeXT__
5827 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5828# else
5829 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5830# endif
5831 if (wait_pid == -1)
5832 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005833 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005834 if (job->jv_status < JOB_ENDED)
5835 ch_log(job->jv_channel, "Job no longer exists: %s",
5836 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005837 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005838 }
5839 if (wait_pid == 0)
5840 return "run";
5841 if (WIFEXITED(status))
5842 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005843 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005844 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005845 if (job->jv_status < JOB_ENDED)
5846 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005847 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005848 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005849 if (WIFSIGNALED(status))
5850 {
5851 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005852 job->jv_termsig = get_signal_name(WTERMSIG(status));
5853 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5854 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5855 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005856 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005857 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005858 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005859
5860return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005861 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005862 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005863 return "dead";
5864}
5865
5866 job_T *
5867mch_detect_ended_job(job_T *job_list)
5868{
5869# ifdef HAVE_UNION_WAIT
5870 union wait status;
5871# else
5872 int status = -1;
5873# endif
5874 pid_t wait_pid = 0;
5875 job_T *job;
5876
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005877# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005878 // Do not do this when waiting for a shell command to finish, we would get
5879 // the exit value here (and discard it), the exit value obtained there
5880 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005881 if (dont_check_job_ended > 0)
5882 return NULL;
5883# endif
5884
Bram Moolenaar97792de2016-10-15 18:36:49 +02005885# ifdef __NeXT__
5886 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5887# else
5888 wait_pid = waitpid(-1, &status, WNOHANG);
5889# endif
5890 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005891 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005892 return NULL;
5893 for (job = job_list; job != NULL; job = job->jv_next)
5894 {
5895 if (job->jv_pid == wait_pid)
5896 {
5897 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005898 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005899 job->jv_exitval = WEXITSTATUS(status);
5900 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005901 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005902 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005903 job->jv_termsig = get_signal_name(WTERMSIG(status));
5904 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005905 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005906 {
5907 ch_log(job->jv_channel, "Job ended");
5908 job->jv_status = JOB_ENDED;
5909 }
5910 return job;
5911 }
5912 }
5913 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005914}
5915
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005916/*
5917 * Send a (deadly) signal to "job".
5918 * Return FAIL if "how" is not a valid name.
5919 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005920 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005921mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005922{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005923 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005924
Bram Moolenaar923d9262016-02-25 20:56:01 +01005925 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005926 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005927 else if (STRCMP(how, "hup") == 0)
5928 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005929 else if (STRCMP(how, "quit") == 0)
5930 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005931 else if (STRCMP(how, "int") == 0)
5932 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005933 else if (STRCMP(how, "kill") == 0)
5934 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005935#ifdef SIGWINCH
5936 else if (STRCMP(how, "winch") == 0)
5937 sig = SIGWINCH;
5938#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005939 else if (isdigit(*how))
5940 sig = atoi((char *)how);
5941 else
5942 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005943
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005944 // Never kill ourselves!
5945 if (job->jv_pid != 0)
5946 {
5947 // TODO: have an option to only kill the process, not the group?
5948 kill(-job->jv_pid, sig);
5949 kill(job->jv_pid, sig);
5950 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005951
Bram Moolenaar835dc632016-02-07 14:27:38 +01005952 return OK;
5953}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005954
5955/*
5956 * Clear the data related to "job".
5957 */
5958 void
5959mch_clear_job(job_T *job)
5960{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005961 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005962# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005963 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005964# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005965 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005966# endif
5967}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005968#endif
5969
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005970#if defined(FEAT_TERMINAL) || defined(PROTO)
5971 int
5972mch_create_pty_channel(job_T *job, jobopt_T *options)
5973{
5974 int pty_master_fd = -1;
5975 int pty_slave_fd = -1;
5976 channel_T *channel;
5977
Bram Moolenaar59386482019-02-10 22:43:46 +01005978 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02005979 if (pty_master_fd < 0 || pty_slave_fd < 0)
5980 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005981 close(pty_slave_fd);
5982
5983 channel = add_channel();
5984 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005985 {
5986 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005987 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005988 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005989 if (job->jv_tty_out != NULL)
5990 ch_log(channel, "using pty %s on fd %d",
5991 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005992 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005993 channel->ch_keep_open = TRUE;
5994
Bram Moolenaar0f873732019-12-05 20:28:46 +01005995 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5996 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005997 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005998 channel_set_job(channel, job, options);
5999 return OK;
6000}
6001#endif
6002
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003/*
6004 * Check for CTRL-C typed by reading all available characters.
6005 * In cooked mode we should get SIGINT, no need to check.
6006 */
6007 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006008mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006010 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006011 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 fill_input_buf(FALSE);
6013}
6014
6015/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006016 * Wait "msec" msec until a character is available from the mouse, keyboard,
6017 * from inbuf[].
6018 * "msec" == -1 will block forever.
6019 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006020 * When "ignore_input" is TRUE even check for pending input when input is
6021 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006022 * "interrupted" (if not NULL) is set to TRUE when no character is available
6023 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006024 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006025 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 */
6027 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006028WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006030#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006031 return ui_wait_for_chars_or_timer(
6032 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006033#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006034 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006035#endif
6036}
6037
6038/*
6039 * Wait "msec" msec until a character is available from the mouse or keyboard
6040 * or from inbuf[].
6041 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006042 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006043 * "interrupted" (if not NULL) is set to TRUE when no character is available
6044 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006045 * When a GUI is being used, this will never get called -- webb
6046 */
6047 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006048WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006049{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050#ifdef FEAT_MOUSE_GPM
6051 int gpm_process_wanted;
6052#endif
6053#ifdef FEAT_XCLIPBOARD
6054 int rest;
6055#endif
6056 int avail;
6057
Bram Moolenaar0f873732019-12-05 20:28:46 +01006058 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 return 1;
6060
6061#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006062 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 if (WantQueryMouse)
6064 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006065 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006066 if (!no_query_mouse_for_testing)
6067 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 }
6069#endif
6070
6071 /*
6072 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6073 * events. This is a bit complicated, because they might both be defined.
6074 */
6075#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6076# ifdef FEAT_XCLIPBOARD
6077 rest = 0;
6078 if (do_xterm_trace())
6079 rest = msec;
6080# endif
6081 do
6082 {
6083# ifdef FEAT_XCLIPBOARD
6084 if (rest != 0)
6085 {
6086 msec = XT_TRACE_DELAY;
6087 if (rest >= 0 && rest < XT_TRACE_DELAY)
6088 msec = rest;
6089 if (rest >= 0)
6090 rest -= msec;
6091 }
6092# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006093# ifdef FEAT_SOUND_CANBERRA
6094 // Invoke any pending sound callbacks.
6095 if (has_sound_callback_in_queue())
6096 invoke_sound_callback();
6097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098# ifdef FEAT_MOUSE_GPM
6099 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006100 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006101 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006102 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006104 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006106# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006108 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 return 1;
6110# ifdef FEAT_XCLIPBOARD
6111 if (rest == 0 || !do_xterm_trace())
6112# endif
6113 break;
6114 }
6115 }
6116 while (FALSE
6117# ifdef FEAT_MOUSE_GPM
6118 || (gpm_process_wanted && mch_gpm_process() == 0)
6119# endif
6120# ifdef FEAT_XCLIPBOARD
6121 || (!avail && rest != 0)
6122# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006123 )
6124 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125
6126#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006127 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128#endif
6129 return avail;
6130}
6131
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006132#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133/*
6134 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006135 * "msec" == 0 will check for characters once.
6136 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006139 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006140 * "interrupted" (if not NULL) is set to TRUE when no character is available
6141 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006143 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006144RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145{
6146 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006147 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006148#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 static int busy = FALSE;
6150
Bram Moolenaar0f873732019-12-05 20:28:46 +01006151 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152# define MAY_LOOP
6153
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006154# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006155 // Remember at what time we started, so that we know how much longer we
6156 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006157 long start_msec = msec;
6158 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006160 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006161 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162# endif
6163
Bram Moolenaar0f873732019-12-05 20:28:46 +01006164 // Handle being called recursively. This may happen for the session
6165 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 if (busy)
6167 return 0;
6168#endif
6169
6170#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006171 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172#endif
6173 {
6174#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006175 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006176# ifdef FEAT_MZSCHEME
6177 int mzquantum_used = FALSE;
6178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179#endif
6180#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006181 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006182 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 int nfd;
6184# ifdef FEAT_XCLIPBOARD
6185 int xterm_idx = -1;
6186# endif
6187# ifdef FEAT_MOUSE_GPM
6188 int gpm_idx = -1;
6189# endif
6190# ifdef USE_XSMP
6191 int xsmp_idx = -1;
6192# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006193 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006195# ifdef FEAT_MZSCHEME
6196 mzvim_check_threads();
6197 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6198 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006199 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006200 mzquantum_used = TRUE;
6201 }
6202# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 fds[0].fd = fd;
6204 fds[0].events = POLLIN;
6205 nfd = 1;
6206
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006208 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 if (xterm_Shell != (Widget)0)
6210 {
6211 xterm_idx = nfd;
6212 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6213 fds[nfd].events = POLLIN;
6214 nfd++;
6215 }
6216# endif
6217# ifdef FEAT_MOUSE_GPM
6218 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6219 {
6220 gpm_idx = nfd;
6221 fds[nfd].fd = gpm_fd;
6222 fds[nfd].events = POLLIN;
6223 nfd++;
6224 }
6225# endif
6226# ifdef USE_XSMP
6227 if (xsmp_icefd != -1)
6228 {
6229 xsmp_idx = nfd;
6230 fds[nfd].fd = xsmp_icefd;
6231 fds[nfd].events = POLLIN;
6232 nfd++;
6233 }
6234# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006235#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006236 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006237#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006238 if (interrupted != NULL)
6239 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006241 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006242
6243 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006244 if (result == 0 && interrupted != NULL && ret > 0)
6245 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006246
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006247# ifdef FEAT_MZSCHEME
6248 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006249 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006250 finished = FALSE;
6251# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253# ifdef FEAT_XCLIPBOARD
6254 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6255 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006256 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006258 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 finished = FALSE;
6260 }
6261# endif
6262# ifdef FEAT_MOUSE_GPM
6263 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265# endif
6266# ifdef USE_XSMP
6267 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6268 {
6269 if (fds[xsmp_idx].revents & POLLIN)
6270 {
6271 busy = TRUE;
6272 xsmp_handle_requests();
6273 busy = FALSE;
6274 }
6275 else if (fds[xsmp_idx].revents & POLLHUP)
6276 {
6277 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006278 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 xsmp_close();
6280 }
6281 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006282 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 }
6284# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006285#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006286 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006287 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006288 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290
Bram Moolenaar0f873732019-12-05 20:28:46 +01006291#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292
6293 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006294 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006295 // These are static because they can take 8 Kbyte each and cause the
6296 // signal stack to run out with -O3.
6297 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006299 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006301# ifdef FEAT_MZSCHEME
6302 mzvim_check_threads();
6303 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6304 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006305 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006306 mzquantum_used = TRUE;
6307 }
6308# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006310 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006312 tv.tv_sec = towait / 1000;
6313 tv.tv_usec = (towait % 1000) * (1000000/1000);
6314 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006316 else
6317 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318
6319 /*
6320 * Select on ready for reading and exceptional condition (end of file).
6321 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006322select_eintr:
6323 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006324 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 FD_ZERO(&efds);
6326 FD_SET(fd, &rfds);
6327# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006328 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 FD_SET(fd, &efds);
6330# endif
6331 maxfd = fd;
6332
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006334 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 if (xterm_Shell != (Widget)0)
6336 {
6337 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6338 if (maxfd < ConnectionNumber(xterm_dpy))
6339 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006340
Bram Moolenaar0f873732019-12-05 20:28:46 +01006341 // An event may have already been read but not handled. In
6342 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006343 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 }
6345# endif
6346# ifdef FEAT_MOUSE_GPM
6347 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6348 {
6349 FD_SET(gpm_fd, &rfds);
6350 FD_SET(gpm_fd, &efds);
6351 if (maxfd < gpm_fd)
6352 maxfd = gpm_fd;
6353 }
6354# endif
6355# ifdef USE_XSMP
6356 if (xsmp_icefd != -1)
6357 {
6358 FD_SET(xsmp_icefd, &rfds);
6359 FD_SET(xsmp_icefd, &efds);
6360 if (maxfd < xsmp_icefd)
6361 maxfd = xsmp_icefd;
6362 }
6363# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006364# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006365 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006366# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006367 if (interrupted != NULL)
6368 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369
Bram Moolenaar643b6142018-09-12 20:29:09 +02006370 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6371 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006372 result = ret > 0 && FD_ISSET(fd, &rfds);
6373 if (result)
6374 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006375 else if (interrupted != NULL && ret > 0)
6376 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006377
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006378# ifdef EINTR
6379 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006380 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006381 // Check whether window has been resized, EINTR may be caused by
6382 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006383 if (do_resize)
6384 handle_resize();
6385
Bram Moolenaar0f873732019-12-05 20:28:46 +01006386 // Interrupted by a signal, need to try again. We ignore msec
6387 // here, because we do want to check even after a timeout if
6388 // characters are available. Needed for reading output of an
6389 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006390 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006391 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006392# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006393# ifdef __TANDEM
6394 if (ret == -1 && errno == ENOTSUP)
6395 {
6396 FD_ZERO(&rfds);
6397 FD_ZERO(&efds);
6398 ret = 0;
6399 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006400# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006401# ifdef FEAT_MZSCHEME
6402 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006403 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006404 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405# endif
6406
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407# ifdef FEAT_XCLIPBOARD
6408 if (ret > 0 && xterm_Shell != (Widget)0
6409 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6410 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006411 xterm_update(); // Maybe we should hand out clipboard
6412 // continue looping when we only got the X event and the input
6413 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 if (--ret == 0 && !input_available())
6415 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006416 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 finished = FALSE;
6418 }
6419 }
6420# endif
6421# ifdef FEAT_MOUSE_GPM
6422 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6423 {
6424 if (FD_ISSET(gpm_fd, &efds))
6425 gpm_close();
6426 else if (FD_ISSET(gpm_fd, &rfds))
6427 *check_for_gpm = 1;
6428 }
6429# endif
6430# ifdef USE_XSMP
6431 if (ret > 0 && xsmp_icefd != -1)
6432 {
6433 if (FD_ISSET(xsmp_icefd, &efds))
6434 {
6435 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006436 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 xsmp_close();
6438 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006439 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 }
6441 else if (FD_ISSET(xsmp_icefd, &rfds))
6442 {
6443 busy = TRUE;
6444 xsmp_handle_requests();
6445 busy = FALSE;
6446 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006447 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 }
6449 }
6450# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006451#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006452 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006453 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006454 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456
Bram Moolenaar0f873732019-12-05 20:28:46 +01006457#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
6459#ifdef MAY_LOOP
6460 if (finished || msec == 0)
6461 break;
6462
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006463# ifdef FEAT_CLIENTSERVER
6464 if (server_waiting())
6465 break;
6466# endif
6467
Bram Moolenaar0f873732019-12-05 20:28:46 +01006468 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 if (msec > 0)
6470 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006471# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006472 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006473 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006475 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 msec = msec / 2;
6477# endif
6478 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006479 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 }
6481#endif
6482 }
6483
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006484 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485}
6486
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006488 * Expand a path into all matching files and/or directories. Handles "*",
6489 * "?", "[a-z]", "**", etc.
6490 * "path" has backslashes before chars that are not to be expanded.
6491 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 */
6493 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006494mch_expandpath(
6495 garray_T *gap,
6496 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006497 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
Bram Moolenaar02743632005-07-25 20:42:36 +00006499 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501
6502/*
6503 * mch_expand_wildcards() - this code does wild-card pattern matching using
6504 * the shell
6505 *
6506 * return OK for success, FAIL for error (you may lose some memory) and put
6507 * an error message in *file.
6508 *
6509 * num_pat is number of input patterns
6510 * pat is array of pointers to input patterns
6511 * num_file is pointer to number of matched file names
6512 * file is pointer to array of pointers to matched file names
6513 */
6514
6515#ifndef SEEK_SET
6516# define SEEK_SET 0
6517#endif
6518#ifndef SEEK_END
6519# define SEEK_END 2
6520#endif
6521
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006522#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006523
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006525mch_expand_wildcards(
6526 int num_pat,
6527 char_u **pat,
6528 int *num_file,
6529 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006530 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531{
6532 int i;
6533 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006534 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 char_u *p;
6536 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537
Bram Moolenaarc7247912008-01-13 12:54:11 +00006538 /*
6539 * This is the non-OS/2 implementation (really Unix).
6540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 int j;
6542 char_u *tempname;
6543 char_u *command;
6544 FILE *fd;
6545 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006546#define STYLE_ECHO 0 // use "echo", the default
6547#define STYLE_GLOB 1 // use "glob", for csh
6548#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6549#define STYLE_PRINT 3 // use "print -N", for zsh
6550#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6551 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 int shell_style = STYLE_ECHO;
6553 int check_spaces;
6554 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006555 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006556 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006557 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558
Bram Moolenaar0f873732019-12-05 20:28:46 +01006559 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 *file = NULL;
6561
6562 /*
6563 * If there are no wildcards, just copy the names to allocated memory.
6564 * Saves a lot of time, because we don't have to start a new shell.
6565 */
6566 if (!have_wildcard(num_pat, pat))
6567 return save_patterns(num_pat, pat, num_file, file);
6568
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006569# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006570 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006571 if (sandbox != 0 && check_secure())
6572 return FAIL;
6573# endif
6574
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 /*
6576 * Don't allow the use of backticks in secure and restricted mode.
6577 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006578 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 for (i = 0; i < num_pat; ++i)
6580 if (vim_strchr(pat[i], '`') != NULL
6581 && (check_restricted() || check_secure()))
6582 return FAIL;
6583
6584 /*
6585 * get a name for the temp file
6586 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006587 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006589 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 return FAIL;
6591 }
6592
6593 /*
6594 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006595 * file.
6596 * STYLE_BT: NL separated
6597 * If expanding `cmd` execute it directly.
6598 * STYLE_GLOB: NUL separated
6599 * If we use *csh, "glob" will work better than "echo".
6600 * STYLE_PRINT: NL or NUL separated
6601 * If we use *zsh, "print -N" will work better than "glob".
6602 * STYLE_VIMGLOB: NL separated
6603 * If we use *sh*, we define "vimglob()".
6604 * STYLE_ECHO: space separated.
6605 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 */
6607 if (num_pat == 1 && *pat[0] == '`'
6608 && (len = STRLEN(pat[0])) > 2
6609 && *(pat[0] + len - 1) == '`')
6610 shell_style = STYLE_BT;
6611 else if ((len = STRLEN(p_sh)) >= 3)
6612 {
6613 if (STRCMP(p_sh + len - 3, "csh") == 0)
6614 shell_style = STYLE_GLOB;
6615 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6616 shell_style = STYLE_PRINT;
6617 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006618 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6619 "sh") != NULL)
6620 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621
Bram Moolenaar0f873732019-12-05 20:28:46 +01006622 // Compute the length of the command. We need 2 extra bytes: for the
6623 // optional '&' and for the NUL.
6624 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006626 if (shell_style == STYLE_VIMGLOB)
6627 len += STRLEN(sh_vimglob_func);
6628
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006629 for (i = 0; i < num_pat; ++i)
6630 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006631 // Count the length of the patterns in the same way as they are put in
6632 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006633#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006634 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006635#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006636 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006637 for (j = 0; pat[i][j] != NUL; ++j)
6638 {
6639 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006640 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006641 ++len;
6642 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006643#endif
6644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 command = alloc(len);
6646 if (command == NULL)
6647 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006648 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 vim_free(tempname);
6650 return FAIL;
6651 }
6652
6653 /*
6654 * Build the shell command:
6655 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6656 * recognizes this).
6657 * - Add the shell command to print the expanded names.
6658 * - Add the temp file name.
6659 * - Add the file name patterns.
6660 */
6661 if (shell_style == STYLE_BT)
6662 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006663 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006664 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006665 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006667 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006668 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006670 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006672 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 *p = ' ';
6674 }
6675 STRCAT(command, ">");
6676 }
6677 else
6678 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006679 STRCPY(command, "");
6680 if (shell_style == STYLE_GLOB)
6681 {
6682 // Assume the nonomatch option is valid only for csh like shells,
6683 // otherwise, this may set the positional parameters for the shell,
6684 // e.g. "$*".
6685 if (flags & EW_NOTFOUND)
6686 STRCAT(command, "set nonomatch; ");
6687 else
6688 STRCAT(command, "unset nonomatch; ");
6689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 if (shell_style == STYLE_GLOB)
6691 STRCAT(command, "glob >");
6692 else if (shell_style == STYLE_PRINT)
6693 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006694 else if (shell_style == STYLE_VIMGLOB)
6695 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 else
6697 STRCAT(command, "echo >");
6698 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006701
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 if (shell_style != STYLE_BT)
6703 for (i = 0; i < num_pat; ++i)
6704 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006705 // When using system() always add extra quotes, because the shell
6706 // is started twice. Otherwise put a backslash before special
6707 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708#ifdef USE_SYSTEM
6709 STRCAT(command, " \"");
6710 STRCAT(command, pat[i]);
6711 STRCAT(command, "\"");
6712#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006713 int intick = FALSE;
6714
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 p = command + STRLEN(command);
6716 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006717 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006718 {
6719 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006720 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006721 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6722 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006723 // Remove a backslash, take char literally. But keep
6724 // backslash inside backticks, before a special character
6725 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006726 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006727 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6728 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006729 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006730 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006731 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006732 else if (!intick
6733 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6734 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006735 // Put a backslash before a special character, but not
6736 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6737 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006738 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006739
Bram Moolenaar0f873732019-12-05 20:28:46 +01006740 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006741 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 *p = NUL;
6744#endif
6745 }
6746 if (flags & EW_SILENT)
6747 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006748 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006749 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
6751 /*
6752 * Using zsh -G: If a pattern has no matches, it is just deleted from
6753 * the argument list, otherwise zsh gives an error message and doesn't
6754 * expand any other pattern.
6755 */
6756 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006757 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
6759 /*
6760 * If we use -f then shell variables set in .cshrc won't get expanded.
6761 * vi can do it, so we will too, but it is only necessary if there is a "$"
6762 * in one of the patterns, otherwise we can still use the fast option.
6763 */
6764 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006765 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766
6767 /*
6768 * execute the shell command
6769 */
6770 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6771
Bram Moolenaar0f873732019-12-05 20:28:46 +01006772 // When running in the background, give it some time to create the temp
6773 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006774 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006775 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776
Bram Moolenaar0f873732019-12-05 20:28:46 +01006777 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 show_shell_mess = TRUE;
6779 vim_free(command);
6780
Bram Moolenaar0f873732019-12-05 20:28:46 +01006781 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {
6783 mch_remove(tempname);
6784 vim_free(tempname);
6785 /*
6786 * With interactive completion, the error message is not printed.
6787 * However with USE_SYSTEM, I don't know how to turn off error messages
6788 * from the shell, so screen may still get messed up -- webb.
6789 */
6790#ifndef USE_SYSTEM
6791 if (!(flags & EW_SILENT))
6792#endif
6793 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006794 redraw_later_clear(); // probably messed up screen
6795 msg_putchar('\n'); // clear bottom line quickly
6796 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797#ifdef USE_SYSTEM
6798 if (!(flags & EW_SILENT))
6799#endif
6800 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006801 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006802 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 }
6804 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006805 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6806 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 if (shell_style == STYLE_BT)
6808 return FAIL;
6809 goto notfound;
6810 }
6811
6812 /*
6813 * read the names from the file into memory
6814 */
6815 fd = fopen((char *)tempname, READBIN);
6816 if (fd == NULL)
6817 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006818 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (!(flags & EW_SILENT))
6820 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006821 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006822 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 }
6824 vim_free(tempname);
6825 goto notfound;
6826 }
6827 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006828 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006830 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006831 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006832 buffer = NULL;
6833 else
6834 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 if (buffer == NULL)
6836 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006837 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 mch_remove(tempname);
6839 vim_free(tempname);
6840 fclose(fd);
6841 return FAIL;
6842 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006843 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 i = fread((char *)buffer, 1, len, fd);
6845 fclose(fd);
6846 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006847 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006849 // unexpected read error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006850 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 vim_free(tempname);
6852 vim_free(buffer);
6853 return FAIL;
6854 }
6855 vim_free(tempname);
6856
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006857# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006858 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006860 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6862 *p++ = buffer[i];
6863 len = p - buffer;
6864# endif
6865
6866
Bram Moolenaar0f873732019-12-05 20:28:46 +01006867 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 if (shell_style == STYLE_ECHO)
6869 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006870 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006872 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 {
6874 while (*p != ' ' && *p != '\n')
6875 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006876 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 }
6878 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006879 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006880 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006882 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006884 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {
6886 while (*p != '\n' && *p != NUL)
6887 ++p;
6888 if (*p != NUL)
6889 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006890 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
6892 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006893 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 else
6895 {
6896 /*
6897 * Some versions of zsh use spaces instead of NULs to separate
6898 * results. Only do this when there is no NUL before the end of the
6899 * buffer, otherwise we would never be able to use file names with
6900 * embedded spaces when zsh does use NULs.
6901 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6902 * don't check for spaces again.
6903 */
6904 check_spaces = FALSE;
6905 if (shell_style == STYLE_PRINT && !did_find_nul)
6906 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006907 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006908 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006909 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 did_find_nul = TRUE;
6911 else
6912 check_spaces = TRUE;
6913 }
6914
6915 /*
6916 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6917 * already is one, for STYLE_GLOB it needs to be added.
6918 */
6919 if (len && buffer[len - 1] == NUL)
6920 --len;
6921 else
6922 buffer[len] = NUL;
6923 i = 0;
6924 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006925 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 {
6927 ++i;
6928 *p = NUL;
6929 }
6930 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006931 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 }
6933 if (i == 0)
6934 {
6935 /*
6936 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6937 * /bin/sh will happily expand it to nothing rather than returning an
6938 * error; and hey, it's good to check anyway -- webb.
6939 */
6940 vim_free(buffer);
6941 goto notfound;
6942 }
6943 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006944 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 if (*file == NULL)
6946 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006947 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 vim_free(buffer);
6949 return FAIL;
6950 }
6951
6952 /*
6953 * Isolate the individual file names.
6954 */
6955 p = buffer;
6956 for (i = 0; i < *num_file; ++i)
6957 {
6958 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006959 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006960 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6961 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006963 while (!(shell_style == STYLE_ECHO && *p == ' ')
6964 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006966 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 *p = NUL;
6968 else
6969 {
6970 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006971 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 }
6973 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006974 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006976 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006978 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 }
6980 }
6981
6982 /*
6983 * Move the file names to allocated memory.
6984 */
6985 for (j = 0, i = 0; i < *num_file; ++i)
6986 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006987 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6989 continue;
6990
Bram Moolenaar0f873732019-12-05 20:28:46 +01006991 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 dir = (mch_isdir((*file)[i]));
6993 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6994 continue;
6995
Bram Moolenaar0f873732019-12-05 20:28:46 +01006996 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01006997 if (!dir && (flags & EW_EXEC)
6998 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006999 continue;
7000
Bram Moolenaar964b3742019-05-24 18:54:09 +02007001 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 if (p)
7003 {
7004 STRCPY(p, (*file)[i]);
7005 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007006 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 (*file)[j++] = p;
7008 }
7009 }
7010 vim_free(buffer);
7011 *num_file = j;
7012
Bram Moolenaar0f873732019-12-05 20:28:46 +01007013 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007015 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 goto notfound;
7017 }
7018
7019 return OK;
7020
7021notfound:
7022 if (flags & EW_NOTFOUND)
7023 return save_patterns(num_pat, pat, num_file, file);
7024 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025}
7026
Bram Moolenaar0f873732019-12-05 20:28:46 +01007027#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007030save_patterns(
7031 int num_pat,
7032 char_u **pat,
7033 int *num_file,
7034 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035{
7036 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007037 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007039 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 if (*file == NULL)
7041 return FAIL;
7042 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007043 {
7044 s = vim_strsave(pat[i]);
7045 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007046 // Be compatible with expand_filename(): halve the number of
7047 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007048 backslash_halve(s);
7049 (*file)[i] = s;
7050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 *num_file = num_pat;
7052 return OK;
7053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055/*
7056 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7057 * expand.
7058 */
7059 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007060mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007062 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 if (*p == '\\' && p[1] != NUL)
7065 ++p;
7066 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 if (vim_strchr((char_u *)
7068#ifdef VMS
7069 "*?%"
7070#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072#endif
7073 , *p) != NULL)
7074 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 }
7076 return FALSE;
7077}
7078
7079/*
7080 * Return TRUE if the string "p" contains a wildcard.
7081 * Don't recognize '~' at the end as a wildcard.
7082 */
7083 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007084mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007086 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 if (*p == '\\' && p[1] != NUL)
7089 ++p;
7090 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 if (vim_strchr((char_u *)
7092#ifdef VMS
7093 "*?%$"
7094#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096#endif
7097 , *p) != NULL
7098 || (*p == '~' && p[1] != NUL))
7099 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 }
7101 return FALSE;
7102}
7103
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007105have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106{
7107 int i;
7108
7109 for (i = 0; i < num; i++)
7110 if (mch_has_wildcard(file[i]))
7111 return 1;
7112 return 0;
7113}
7114
7115 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007116have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117{
7118 int i;
7119
7120 for (i = 0; i < num; i++)
7121 if (vim_strchr(file[i], '$') != NULL)
7122 return TRUE;
7123 return FALSE;
7124}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007126#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127/*
7128 * Scaled-down version of rename(), which is missing in Xenix.
7129 * This version can only move regular files and will fail if the
7130 * destination exists.
7131 */
7132 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007133mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134{
7135 struct stat st;
7136
Bram Moolenaar0f873732019-12-05 20:28:46 +01007137 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007139 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007141 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 return 0;
7143 return -1;
7144}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007145#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007147#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148/*
7149 * Initializes connection with gpm (if it isn't already opened)
7150 * Return 1 if succeeded (or connection already opened), 0 if failed
7151 */
7152 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007153gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007155 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156
7157 if (!gpm_flag)
7158 {
7159 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7160 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007161 // Default handling for mouse move
7162 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 gpm_connect.maxMod = 0xffff;
7164 if (Gpm_Open(&gpm_connect, 0) > 0)
7165 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007166 // gpm library tries to handling TSTP causes
7167 // problems. Anyways, we close connection to Gpm whenever
7168 // we are going to suspend or starting an external process
7169 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007170# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007172# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007173 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 }
7175 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007176 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 return 0;
7178 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007179 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180}
7181
7182/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007183 * Returns TRUE if the GPM mouse is enabled.
7184 */
7185 int
7186gpm_enabled(void)
7187{
7188 return gpm_flag && gpm_fd >= 0;
7189}
7190
7191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 */
7194 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007195gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007197 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 Gpm_Close();
7199}
7200
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007201/*
7202 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007204 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 */
7206 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007207mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208{
7209 int button;
7210 static Gpm_Event gpm_event;
7211 char_u string[6];
7212 int_u vim_modifiers;
7213 int row,col;
7214 unsigned char buttons_mask;
7215 unsigned char gpm_modifiers;
7216 static unsigned char old_buttons = 0;
7217
7218 Gpm_GetEvent(&gpm_event);
7219
7220#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007221 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 if (hold_gui_events)
7223 return 0;
7224#endif
7225
7226 row = gpm_event.y - 1;
7227 col = gpm_event.x - 1;
7228
Bram Moolenaar0f873732019-12-05 20:28:46 +01007229 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 string[1] = 'M';
7231 string[2] = 'G';
7232 switch (GPM_BARE_EVENTS(gpm_event.type))
7233 {
7234 case GPM_DRAG:
7235 string[3] = MOUSE_DRAG;
7236 break;
7237 case GPM_DOWN:
7238 buttons_mask = gpm_event.buttons & ~old_buttons;
7239 old_buttons = gpm_event.buttons;
7240 switch (buttons_mask)
7241 {
7242 case GPM_B_LEFT:
7243 button = MOUSE_LEFT;
7244 break;
7245 case GPM_B_MIDDLE:
7246 button = MOUSE_MIDDLE;
7247 break;
7248 case GPM_B_RIGHT:
7249 button = MOUSE_RIGHT;
7250 break;
7251 default:
7252 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007253 // Don't know what to do. Can more than one button be
7254 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 }
7256 string[3] = (char_u)(button | 0x20);
7257 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7258 break;
7259 case GPM_UP:
7260 string[3] = MOUSE_RELEASE;
7261 old_buttons &= ~gpm_event.buttons;
7262 break;
7263 default:
7264 return 0;
7265 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007266 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 gpm_modifiers = gpm_event.modifiers;
7268 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007269 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7270 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7271 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7273 vim_modifiers |= MOUSE_SHIFT;
7274
7275 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7276 vim_modifiers |= MOUSE_CTRL;
7277 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7278 vim_modifiers |= MOUSE_ALT;
7279 string[3] |= vim_modifiers;
7280 string[4] = (char_u)(col + ' ' + 1);
7281 string[5] = (char_u)(row + ' ' + 1);
7282 add_to_input_buf(string, 6);
7283 return 6;
7284}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007285#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007287#ifdef FEAT_SYSMOUSE
7288/*
7289 * Initialize connection with sysmouse.
7290 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7291 * output, any sysmouse output than will be processed via sig_sysmouse().
7292 * Return OK if succeeded, FAIL if failed.
7293 */
7294 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007295sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007296{
7297 struct mouse_info mouse;
7298
7299 mouse.operation = MOUSE_MODE;
7300 mouse.u.mode.mode = 0;
7301 mouse.u.mode.signal = SIGUSR2;
7302 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7303 {
7304 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7305 mouse.operation = MOUSE_SHOW;
7306 ioctl(1, CONS_MOUSECTL, &mouse);
7307 return OK;
7308 }
7309 return FAIL;
7310}
7311
7312/*
7313 * Stop processing SIGUSR2 signals, and also make sure that
7314 * virtual console do not send us any sysmouse related signal.
7315 */
7316 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007317sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007318{
7319 struct mouse_info mouse;
7320
7321 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7322 mouse.operation = MOUSE_MODE;
7323 mouse.u.mode.mode = 0;
7324 mouse.u.mode.signal = 0;
7325 ioctl(1, CONS_MOUSECTL, &mouse);
7326}
7327
7328/*
7329 * Gets info from sysmouse and adds special keys to input buf.
7330 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007331 static RETSIGTYPE
7332sig_sysmouse SIGDEFARG(sigarg)
7333{
7334 struct mouse_info mouse;
7335 struct video_info video;
7336 char_u string[6];
7337 int row, col;
7338 int button;
7339 int buttons;
7340 static int oldbuttons = 0;
7341
7342#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007343 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007344 if (hold_gui_events)
7345 return;
7346#endif
7347
7348 mouse.operation = MOUSE_GETINFO;
7349 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7350 && ioctl(1, FBIO_MODEINFO, &video) != -1
7351 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7352 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7353 {
7354 row = mouse.u.data.y / video.vi_cheight;
7355 col = mouse.u.data.x / video.vi_cwidth;
7356 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007357 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007358 string[1] = 'M';
7359 string[2] = 'S';
7360 if (oldbuttons == buttons && buttons != 0)
7361 {
7362 button = MOUSE_DRAG;
7363 }
7364 else
7365 {
7366 switch (buttons)
7367 {
7368 case 0:
7369 button = MOUSE_RELEASE;
7370 break;
7371 case 1:
7372 button = MOUSE_LEFT;
7373 break;
7374 case 2:
7375 button = MOUSE_MIDDLE;
7376 break;
7377 case 4:
7378 button = MOUSE_RIGHT;
7379 break;
7380 default:
7381 return;
7382 }
7383 oldbuttons = buttons;
7384 }
7385 string[3] = (char_u)(button);
7386 string[4] = (char_u)(col + ' ' + 1);
7387 string[5] = (char_u)(row + ' ' + 1);
7388 add_to_input_buf(string, 6);
7389 }
7390 return;
7391}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007392#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007393
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007395typedef char_u * (*STRPROCSTR)(char_u *);
7396typedef char_u * (*INTPROCSTR)(int);
7397typedef int (*STRPROCINT)(char_u *);
7398typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399
7400/*
7401 * Call a DLL routine which takes either a string or int param
7402 * and returns an allocated string.
7403 */
7404 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007405mch_libcall(
7406 char_u *libname,
7407 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007408 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007409 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007410 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007411 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
7413# if defined(USE_DLOPEN)
7414 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007415 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416# else
7417 shl_t hinstLib;
7418# endif
7419 STRPROCSTR ProcAdd;
7420 INTPROCSTR ProcAddI;
7421 char_u *retval_str = NULL;
7422 int retval_int = 0;
7423 int success = FALSE;
7424
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007425 /*
7426 * Get a handle to the DLL module.
7427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007429 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007430 (void)dlerror();
7431
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 hinstLib = dlopen((char *)libname, RTLD_LAZY
7433# ifdef RTLD_LOCAL
7434 | RTLD_LOCAL
7435# endif
7436 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007437 if (hinstLib == NULL)
7438 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007439 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007440 dlerr = (char *)dlerror();
7441 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007442 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444# else
7445 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7446# endif
7447
Bram Moolenaar0f873732019-12-05 20:28:46 +01007448 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if (hinstLib != NULL)
7450 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007451# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 /*
7453 * Catch a crash when calling the library function. For example when
7454 * using a number where a string pointer is expected.
7455 */
7456 mch_startjmp();
7457 if (SETJMP(lc_jump_env) != 0)
7458 {
7459 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007460# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007461 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 mch_didjmp();
7464 }
7465 else
7466# endif
7467 {
7468 retval_str = NULL;
7469 retval_int = 0;
7470
7471 if (argstring != NULL)
7472 {
7473# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007474 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007475 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476# else
7477 if (shl_findsym(&hinstLib, (const char *)funcname,
7478 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7479 ProcAdd = NULL;
7480# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007481 if ((success = (ProcAdd != NULL
7482# if defined(USE_DLOPEN)
7483 && dlerr == NULL
7484# endif
7485 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 {
7487 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007488 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 else
7490 retval_str = (ProcAdd)(argstring);
7491 }
7492 }
7493 else
7494 {
7495# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007496 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007497 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498# else
7499 if (shl_findsym(&hinstLib, (const char *)funcname,
7500 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7501 ProcAddI = NULL;
7502# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007503 if ((success = (ProcAddI != NULL
7504# if defined(USE_DLOPEN)
7505 && dlerr == NULL
7506# endif
7507 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 {
7509 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007510 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 else
7512 retval_str = (ProcAddI)(argint);
7513 }
7514 }
7515
Bram Moolenaar0f873732019-12-05 20:28:46 +01007516 // Save the string before we free the library.
7517 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 if (string_result == NULL)
7519 *number_result = retval_int;
7520 else if (retval_str != NULL
7521 && retval_str != (char_u *)1
7522 && retval_str != (char_u *)-1)
7523 *string_result = vim_strsave(retval_str);
7524 }
7525
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007526# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 mch_endjmp();
7528# ifdef SIGHASARG
7529 if (lc_signal != 0)
7530 {
7531 int i;
7532
Bram Moolenaar0f873732019-12-05 20:28:46 +01007533 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 for (i = 0; signal_info[i].sig != -1; i++)
7535 if (lc_signal == signal_info[i].sig)
7536 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007537 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 }
7539# endif
7540# endif
7541
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007543 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007544 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007545 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007546
Bram Moolenaar0f873732019-12-05 20:28:46 +01007547 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 (void)dlclose(hinstLib);
7549# else
7550 (void)shl_unload(hinstLib);
7551# endif
7552 }
7553
7554 if (!success)
7555 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007556 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 return FAIL;
7558 }
7559
7560 return OK;
7561}
7562#endif
7563
7564#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007565static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566static int xterm_button;
7567
7568/*
7569 * Setup a dummy window for X selections in a terminal.
7570 */
7571 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007572setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573{
7574 int z = 0;
7575 char *strp = "";
7576 Widget AppShell;
7577
7578 if (!x_connect_to_server())
7579 return;
7580
7581 open_app_context();
7582 if (app_context != NULL && xterm_Shell == (Widget)0)
7583 {
7584 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007585# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007587# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007588# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007589 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590
7591 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007592 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593# endif
7594
Bram Moolenaar0f873732019-12-05 20:28:46 +01007595 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 oldhandler = XSetErrorHandler(x_error_check);
7597
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007598# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007599 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7601 mch_startjmp();
7602 if (SETJMP(lc_jump_env) != 0)
7603 {
7604 mch_didjmp();
7605 xterm_dpy = NULL;
7606 }
7607 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 {
7610 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7611 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007612 if (xterm_dpy != NULL)
7613 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007614# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 }
7618
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007619# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007620 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007622# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007623 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 (void)XSetErrorHandler(oldhandler);
7625
7626 if (xterm_dpy == NULL)
7627 {
7628 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007629 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 return;
7631 }
7632
Bram Moolenaar0f873732019-12-05 20:28:46 +01007633 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 (void)XSetIOErrorHandler(x_IOerror_handler);
7635
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007636# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007638 {
7639 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007640 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007641 verbose_leave();
7642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643# endif
7644
Bram Moolenaar0f873732019-12-05 20:28:46 +01007645 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7647 applicationShellWidgetClass, xterm_dpy,
7648 NULL);
7649 if (AppShell == (Widget)0)
7650 return;
7651 xterm_Shell = XtVaCreatePopupShell("VIM",
7652 topLevelShellWidgetClass, AppShell,
7653 XtNmappedWhenManaged, 0,
7654 XtNwidth, 1,
7655 XtNheight, 1,
7656 NULL);
7657 if (xterm_Shell == (Widget)0)
7658 return;
7659
7660 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007661 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 if (x11_display == NULL)
7663 x11_display = xterm_dpy;
7664
7665 XtRealizeWidget(xterm_Shell);
7666 XSync(xterm_dpy, False);
7667 xterm_update();
7668 }
7669 if (xterm_Shell != (Widget)0)
7670 {
7671 clip_init(TRUE);
7672 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7673 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007674 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 if (test_x11_window(xterm_dpy) == FAIL)
7676 x11_window = 0;
7677 if (x11_window != 0)
7678 xterm_trace = 0;
7679 }
7680}
7681
7682 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007683start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684{
7685 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7686 return;
7687 xterm_trace = 1;
7688 xterm_button = button;
7689 do_xterm_trace();
7690}
7691
7692
7693 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007694stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
7696 if (xterm_trace < 0)
7697 return;
7698 xterm_trace = 0;
7699}
7700
7701/*
7702 * Query the xterm pointer and generate mouse termcodes if necessary
7703 * return TRUE if dragging is active, else FALSE
7704 */
7705 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007706do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707{
7708 Window root, child;
7709 int root_x, root_y;
7710 int win_x, win_y;
7711 int row, col;
7712 int_u mask_return;
7713 char_u buf[50];
7714 char_u *strp;
7715 long got_hints;
7716 static char_u *mouse_code;
7717 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7718 static int prev_row = 0, prev_col = 0;
7719 static XSizeHints xterm_hints;
7720
7721 if (xterm_trace <= 0)
7722 return FALSE;
7723
7724 if (xterm_trace == 1)
7725 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007726 // Get the hints just before tracking starts. The font size might
7727 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007728 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7729 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 || xterm_hints.width_inc <= 1
7731 || xterm_hints.height_inc <= 1)
7732 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007733 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 return FALSE;
7735 }
7736
Bram Moolenaar0f873732019-12-05 20:28:46 +01007737 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 mouse_code = find_termcode(mouse_name);
7739 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007740 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 xterm_trace = 2;
7742
Bram Moolenaar0f873732019-12-05 20:28:46 +01007743 // Find the offset of the chars, there might be a scrollbar on the
7744 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7746 &win_x, &win_y, &mask_return);
7747 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7748 - (xterm_hints.height_inc / 2);
7749 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7750 xterm_hints.y = 2;
7751 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7752 - (xterm_hints.width_inc / 2);
7753 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7754 xterm_hints.x = 2;
7755 return TRUE;
7756 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007757 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 {
7759 xterm_trace = 0;
7760 return FALSE;
7761 }
7762
7763 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7764 &win_x, &win_y, &mask_return);
7765
7766 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7767 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7768 if (row == prev_row && col == prev_col)
7769 return TRUE;
7770
7771 STRCPY(buf, mouse_code);
7772 strp = buf + STRLEN(buf);
7773 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7774 *strp++ = (char_u)(col + ' ' + 1);
7775 *strp++ = (char_u)(row + ' ' + 1);
7776 *strp = 0;
7777 add_to_input_buf(buf, STRLEN(buf));
7778
7779 prev_row = row;
7780 prev_col = col;
7781 return TRUE;
7782}
7783
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007784# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785/*
7786 * Destroy the display, window and app_context. Required for GTK.
7787 */
7788 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007789clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790{
7791 if (xterm_Shell != (Widget)0)
7792 {
7793 XtDestroyWidget(xterm_Shell);
7794 xterm_Shell = (Widget)0;
7795 }
7796 if (xterm_dpy != NULL)
7797 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007798# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007799 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007801# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 if (x11_display == xterm_dpy)
7803 x11_display = NULL;
7804 xterm_dpy = NULL;
7805 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007806# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 if (app_context != (XtAppContext)NULL)
7808 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007809 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 XtDestroyApplicationContext(app_context);
7811 app_context = (XtAppContext)NULL;
7812 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007813# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814}
7815# endif
7816
7817/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007818 * Catch up with GUI or X events.
7819 */
7820 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007821clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007822{
7823# ifdef FEAT_GUI
7824 if (gui.in_use)
7825 gui_mch_update();
7826 else
7827# endif
7828 if (xterm_Shell != (Widget)0)
7829 xterm_update();
7830}
7831
7832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 * Catch up with any queued X events. This may put keyboard input into the
7834 * input buffer, call resize call-backs, trigger timers etc. If there is
7835 * nothing in the X event queue (& no timers pending), then we return
7836 * immediately.
7837 */
7838 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007839xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840{
7841 XEvent event;
7842
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007843 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007845 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007847 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007848 break;
7849
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007850 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007851 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007852 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007853 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007854#ifdef FEAT_CLIENTSERVER
7855 {
7856 XPropertyEvent *e = (XPropertyEvent *)&event;
7857
7858 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007860 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007863 XtDispatchEvent(&event);
7864 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007865 else
7866 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007867 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007868 XtAppProcessEvent(app_context, mask);
7869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 }
7871}
7872
7873 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007874clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875{
7876 if (xterm_Shell != (Widget)0)
7877 return clip_x11_own_selection(xterm_Shell, cbd);
7878 return FAIL;
7879}
7880
7881 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007882clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883{
7884 if (xterm_Shell != (Widget)0)
7885 clip_x11_lose_selection(xterm_Shell, cbd);
7886}
7887
7888 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007889clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890{
7891 if (xterm_Shell != (Widget)0)
7892 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7893}
7894
7895 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007896clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 clip_x11_set_selection(cbd);
7899}
7900#endif
7901
7902
7903#if defined(USE_XSMP) || defined(PROTO)
7904/*
7905 * Code for X Session Management Protocol.
7906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907
7908# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909/*
7910 * This is our chance to ask the user if they want to save,
7911 * or abort the logout
7912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007914xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915{
Bram Moolenaare1004402020-10-24 20:49:43 +02007916 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 int cancel_shutdown = False;
7918
Bram Moolenaare1004402020-10-24 20:49:43 +02007919 save_cmod_flags = cmdmod.cmod_flags;
7920 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007921 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007922 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02007924 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007925 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 out_flush();
7927
Bram Moolenaar0f873732019-12-05 20:28:46 +01007928 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 SmcInteractDone(smc_conn, cancel_shutdown);
7930
Bram Moolenaar0f873732019-12-05 20:28:46 +01007931 // Finish off
7932 // Only end save-yourself here if we're not cancelling shutdown;
7933 // we'll get a cancelled callback later in which we'll end it.
7934 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 if (!cancel_shutdown)
7936 {
7937 xsmp.save_yourself = False;
7938 SmcSaveYourselfDone(smc_conn, True);
7939 }
7940}
7941# endif
7942
7943/*
7944 * Callback that starts save-yourself.
7945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007947xsmp_handle_save_yourself(
7948 SmcConn smc_conn,
7949 SmPointer client_data UNUSED,
7950 int save_type UNUSED,
7951 Bool shutdown,
7952 int interact_style UNUSED,
7953 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007955 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 if (xsmp.save_yourself)
7957 SmcSaveYourselfDone(smc_conn, True);
7958 xsmp.save_yourself = True;
7959 xsmp.shutdown = shutdown;
7960
Bram Moolenaar0f873732019-12-05 20:28:46 +01007961 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007963 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964
7965 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007966 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967
7968# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007969 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007971 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 SmcInteractRequest(smc_conn, SmDialogError,
7973 xsmp_handle_interaction, client_data);
7974 else
7975# endif
7976 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007977 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 SmcSaveYourselfDone(smc_conn, True);
7979 xsmp.save_yourself = False;
7980 }
7981}
7982
7983
7984/*
7985 * Callback to warn us of imminent death.
7986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007988xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
7990 xsmp_close();
7991
Bram Moolenaar0f873732019-12-05 20:28:46 +01007992 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 getout_preserve_modified(0);
7994}
7995
7996
7997/*
7998 * Callback to tell us that save-yourself has completed.
7999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008001xsmp_save_complete(
8002 SmcConn smc_conn UNUSED,
8003 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004{
8005 xsmp.save_yourself = False;
8006}
8007
8008
8009/*
8010 * Callback to tell us that an instigated shutdown was cancelled
8011 * (maybe even by us)
8012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008014xsmp_shutdown_cancelled(
8015 SmcConn smc_conn,
8016 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017{
8018 if (xsmp.save_yourself)
8019 SmcSaveYourselfDone(smc_conn, True);
8020 xsmp.save_yourself = False;
8021 xsmp.shutdown = False;
8022}
8023
8024
8025/*
8026 * Callback to tell us that a new ICE connection has been established.
8027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008029xsmp_ice_connection(
8030 IceConn iceConn,
8031 IcePointer clientData UNUSED,
8032 Bool opening,
8033 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008035 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 if (opening)
8037 {
8038 xsmp_icefd = IceConnectionNumber(iceConn);
8039 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8040 }
8041}
8042
8043
Bram Moolenaar0f873732019-12-05 20:28:46 +01008044// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008046xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047{
8048 Bool rep;
8049
8050 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8051 == IceProcessMessagesIOError)
8052 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008053 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008055 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 xsmp_close();
8057 return FAIL;
8058 }
8059 else
8060 return OK;
8061}
8062
8063static int dummy;
8064
Bram Moolenaar0f873732019-12-05 20:28:46 +01008065// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 void
8067xsmp_init(void)
8068{
8069 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 SmcCallbacks smcallbacks;
8071#if 0
8072 SmPropValue smname;
8073 SmProp smnameprop;
8074 SmProp *smprops[1];
8075#endif
8076
8077 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008078 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079
8080 xsmp.save_yourself = xsmp.shutdown = False;
8081
Bram Moolenaar0f873732019-12-05 20:28:46 +01008082 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8084 smcallbacks.save_yourself.client_data = NULL;
8085 smcallbacks.die.callback = xsmp_die;
8086 smcallbacks.die.client_data = NULL;
8087 smcallbacks.save_complete.callback = xsmp_save_complete;
8088 smcallbacks.save_complete.client_data = NULL;
8089 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8090 smcallbacks.shutdown_cancelled.client_data = NULL;
8091
Bram Moolenaar0f873732019-12-05 20:28:46 +01008092 // Set up a watch on ICE connection creations. The "dummy" argument is
8093 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8095 {
8096 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008097 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 return;
8099 }
8100
Bram Moolenaar0f873732019-12-05 20:28:46 +01008101 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 xsmp.smcconn = SmcOpenConnection(
8103 NULL,
8104 NULL,
8105 SmProtoMajor,
8106 SmProtoMinor,
8107 SmcSaveYourselfProcMask | SmcDieProcMask
8108 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8109 &smcallbacks,
8110 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008111 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008112 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 errorstring);
8114 if (xsmp.smcconn == NULL)
8115 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008117 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008118 char errorreport[132];
8119
8120 // If the message is too long it might not be NUL terminated. Add
8121 // a NUL at the end to make sure we don't go over the end.
8122 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008123 vim_snprintf(errorreport, sizeof(errorreport),
8124 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008125 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 return;
8128 }
8129 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8130
8131#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008132 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 smname.value = "vim";
8134 smname.length = 3;
8135 smnameprop.name = "SmProgram";
8136 smnameprop.type = "SmARRAY8";
8137 smnameprop.num_vals = 1;
8138 smnameprop.vals = &smname;
8139
8140 smprops[0] = &smnameprop;
8141 SmcSetProperties(xsmp.smcconn, 1, smprops);
8142#endif
8143}
8144
8145
Bram Moolenaar0f873732019-12-05 20:28:46 +01008146// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008148xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149{
8150 if (xsmp_icefd != -1)
8151 {
8152 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008153 if (xsmp.clientid != NULL)
8154 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008155 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 xsmp_icefd = -1;
8157 }
8158}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008159#endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160
8161
8162#ifdef EBCDIC
Bram Moolenaar0f873732019-12-05 20:28:46 +01008163// Translate character to its CTRL- value
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164char CtrlTable[] =
8165{
8166/* 00 - 5E */
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, 0,
8172 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8173/* ^ */ 0x1E,
8174/* - */ 0x1F,
8175/* 61 - 6C */
8176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8177/* _ */ 0x1F,
8178/* 6E - 80 */
8179 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8180/* a */ 0x01,
8181/* b */ 0x02,
8182/* c */ 0x03,
8183/* d */ 0x37,
8184/* e */ 0x2D,
8185/* f */ 0x2E,
8186/* g */ 0x2F,
8187/* h */ 0x16,
8188/* i */ 0x05,
8189/* 8A - 90 */
8190 0, 0, 0, 0, 0, 0, 0,
8191/* j */ 0x15,
8192/* k */ 0x0B,
8193/* l */ 0x0C,
8194/* m */ 0x0D,
8195/* n */ 0x0E,
8196/* o */ 0x0F,
8197/* p */ 0x10,
8198/* q */ 0x11,
8199/* r */ 0x12,
8200/* 9A - A1 */
8201 0, 0, 0, 0, 0, 0, 0, 0,
8202/* s */ 0x13,
8203/* t */ 0x3C,
8204/* u */ 0x3D,
8205/* v */ 0x32,
8206/* w */ 0x26,
8207/* x */ 0x18,
8208/* y */ 0x19,
8209/* z */ 0x3F,
8210/* AA - AC */
8211 0, 0, 0,
8212/* [ */ 0x27,
8213/* AE - BC */
8214 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8215/* ] */ 0x1D,
8216/* BE - C0 */ 0, 0, 0,
8217/* A */ 0x01,
8218/* B */ 0x02,
8219/* C */ 0x03,
8220/* D */ 0x37,
8221/* E */ 0x2D,
8222/* F */ 0x2E,
8223/* G */ 0x2F,
8224/* H */ 0x16,
8225/* I */ 0x05,
8226/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8227/* J */ 0x15,
8228/* K */ 0x0B,
8229/* L */ 0x0C,
8230/* M */ 0x0D,
8231/* N */ 0x0E,
8232/* O */ 0x0F,
8233/* P */ 0x10,
8234/* Q */ 0x11,
8235/* R */ 0x12,
8236/* DA - DF */ 0, 0, 0, 0, 0, 0,
8237/* \ */ 0x1C,
8238/* E1 */ 0,
8239/* S */ 0x13,
8240/* T */ 0x3C,
8241/* U */ 0x3D,
8242/* V */ 0x32,
8243/* W */ 0x26,
8244/* X */ 0x18,
8245/* Y */ 0x19,
8246/* Z */ 0x3F,
8247/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8248 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8249};
8250
8251char MetaCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008252{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8254 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8255 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8256 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8257};
8258
8259
Bram Moolenaar0f873732019-12-05 20:28:46 +01008260// TODO: Use characters NOT numbers!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261char CtrlCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008262{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8264 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8265 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8266 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8267};
8268
8269
8270#endif