blob: 77f35db95836dac871f7d9d471cac58ade38cbd5 [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
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003168 // not ideal but better than 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
ichizokdee78e12021-12-09 21:08:01 +00003379# if (defined(__GNUC__) \
3380 && ((__GNUC__ == 11 && __GNUC_MINOR__ >= 1) || (__GNUC__ >= 12))) \
3381 || (defined(__clang__) && (__clang_major__ >= 12))
3382extern void __gcov_dump(void);
3383extern void __gcov_reset(void);
3384# define __gcov_flush() do { __gcov_dump(); __gcov_reset(); } while (0)
3385# else
3386extern void __gcov_flush(void);
3387# endif
Bram Moolenaarb4151682020-05-11 22:13:28 +02003388#endif
3389
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003391mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
3393 exiting = TRUE;
3394
3395#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3396 x11_export_final_selection();
3397#endif
3398
3399#ifdef FEAT_GUI
3400 if (!gui.in_use)
3401#endif
3402 {
3403 settmode(TMODE_COOK);
Bram Moolenaar7007e312021-03-27 12:11:33 +01003404 if (!is_not_a_term())
3405 {
3406 // restore xterm title and icon name
3407 mch_restore_title(SAVE_RESTORE_BOTH);
3408 term_pop_title(SAVE_RESTORE_BOTH);
3409 }
Bram Moolenaar651fca82021-11-29 20:39:38 +00003410
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 /*
3412 * When t_ti is not empty but it doesn't cause swapping terminal
3413 * pages, need to output a newline when msg_didout is set. But when
3414 * t_ti does swap pages it should not go to the shell page. Do this
3415 * before stoptermcap().
3416 */
3417 if (swapping_screen() && !newline_on_exit)
3418 exit_scroll();
3419
Bram Moolenaar0f873732019-12-05 20:28:46 +01003420 // Stop termcap: May need to check for T_CRV response, which
3421 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 stoptermcap();
3423
3424 /*
3425 * A newline is only required after a message in the alternate screen.
3426 * This is set to TRUE by wait_return().
3427 */
3428 if (!swapping_screen() || newline_on_exit)
3429 exit_scroll();
3430
Bram Moolenaar0f873732019-12-05 20:28:46 +01003431 // Cursor may have been switched off without calling starttermcap()
3432 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 if (full_screen)
3434 cursor_on();
3435 }
3436 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003437 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003438
3439#ifdef USE_GCOV_FLUSH
3440 // Flush coverage info before possibly being killed by a deadly signal.
3441 __gcov_flush();
3442#endif
3443
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 may_core_dump();
3445#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 gui_exit(r);
3448#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003449
Bram Moolenaar56718732006-03-15 22:53:57 +00003450#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003451 mac_conv_cleanup();
3452#endif
3453
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003455 // A core dump won't be created if the signal handler
3456 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (deadly_signal != 0)
3458 return;
3459#endif
3460
Bram Moolenaar009b2592004-10-24 19:18:58 +00003461#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003462 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003463#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003464
3465#ifdef EXITFREE
3466 free_all_mem();
3467#endif
3468
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 exit(r);
3470}
3471
3472 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003473may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474{
3475 if (deadly_signal != 0)
3476 {
3477 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003478 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480}
3481
3482#ifndef VMS
3483
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003484/*
3485 * Get the file descriptor to use for tty operations.
3486 */
3487 static int
3488get_tty_fd(int fd)
3489{
3490 int tty_fd = fd;
3491
3492#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3493 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3494 // "fd" when it is a master device.
3495 if (mch_isatty(fd) > 1)
3496 {
3497 char *name;
3498
3499 name = ptsname(fd);
3500 if (name == NULL)
3501 return -1;
3502
3503 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3504 if (tty_fd < 0)
3505 return -1;
3506 }
3507#endif
3508 return tty_fd;
3509}
3510
3511 static int
3512mch_tcgetattr(int fd, void *term)
3513{
3514 int tty_fd;
3515 int retval = -1;
3516
3517 tty_fd = get_tty_fd(fd);
3518 if (tty_fd >= 0)
3519 {
3520#ifdef NEW_TTY_SYSTEM
3521# ifdef HAVE_TERMIOS_H
3522 retval = tcgetattr(tty_fd, (struct termios *)term);
3523# else
3524 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3525# endif
3526#else
3527 // for "old" tty systems
3528 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3529#endif
3530 if (tty_fd != fd)
3531 close(tty_fd);
3532 }
3533 return retval;
3534}
3535
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003537mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538{
3539 static int first = TRUE;
3540
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003541#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542# ifdef HAVE_TERMIOS_H
3543 static struct termios told;
3544 struct termios tnew;
3545# else
3546 static struct termio told;
3547 struct termio tnew;
3548# endif
3549
3550 if (first)
3551 {
3552 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003553 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555
3556 tnew = told;
3557 if (tmode == TMODE_RAW)
3558 {
Bram Moolenaar041c7102020-05-30 18:14:57 +02003559 // ~ICRNL enables typing ^V^M
Bram Moolenaar928eec62020-05-31 13:09:47 +02003560 // ~IXON disables CTRL-S stopping output, so that it can be mapped.
3561 tnew.c_iflag &= ~(ICRNL | IXON);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
Bram Moolenaare3f915d2020-07-14 23:02:44 +02003563# if defined(IEXTEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003564 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565# endif
3566 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003567# ifdef ONLCR
3568 // Don't map NL -> CR NL, we do it ourselves.
3569 // Also disable expanding tabs if possible.
3570# ifdef XTABS
3571 tnew.c_oflag &= ~(ONLCR | XTABS);
3572# else
3573# ifdef TAB3
3574 tnew.c_oflag &= ~(ONLCR | TAB3);
3575# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003577# endif
3578# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003580 tnew.c_cc[VMIN] = 1; // return after 1 char
3581 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
3583 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003584 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003585 // Also reset ICANON here, otherwise on Solaris select() won't see
3586 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003587 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003588 tnew.c_cc[VMIN] = 1; // return after 1 char
3589 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
3592# if defined(HAVE_TERMIOS_H)
3593 {
3594 int n = 10;
3595
Bram Moolenaar0f873732019-12-05 20:28:46 +01003596 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3597 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3599 && errno == EINTR && n > 0)
3600 --n;
3601 }
3602# else
3603 ioctl(read_cmd_fd, TCSETA, &tnew);
3604# endif
3605
3606#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 /*
3608 * for "old" tty systems
3609 */
3610# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003611# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612# endif
3613 static struct sgttyb ttybold;
3614 struct sgttyb ttybnew;
3615
3616 if (first)
3617 {
3618 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003619 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 }
3621
3622 ttybnew = ttybold;
3623 if (tmode == TMODE_RAW)
3624 {
3625 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3626 ttybnew.sg_flags |= RAW;
3627 }
3628 else if (tmode == TMODE_SLEEP)
3629 ttybnew.sg_flags &= ~(ECHO);
3630 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3631#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02003632 mch_cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633}
3634
3635/*
3636 * Try to get the code for "t_kb" from the stty setting
3637 *
3638 * Even if termcap claims a backspace key, the user's setting *should*
3639 * prevail. stty knows more about reality than termcap does, and if
3640 * somebody's usual erase key is DEL (which, for most BSD users, it will
3641 * be), they're going to get really annoyed if their erase key starts
3642 * doing forward deletes for no reason. (Eric Fischer)
3643 */
3644 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003645get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003647 ttyinfo_T info;
3648 char_u buf[2];
3649 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003651 if (get_tty_info(read_cmd_fd, &info) == OK)
3652 {
3653 intr_char = info.interrupt;
3654 buf[0] = info.backspace;
3655 buf[1] = NUL;
3656 add_termcode((char_u *)"kb", buf, FALSE);
3657
Bram Moolenaar0f873732019-12-05 20:28:46 +01003658 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003659 p = find_termcode((char_u *)"kD");
3660 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3661 do_fixdel(NULL);
3662 }
3663}
3664
3665/*
3666 * Obtain the characters that Backspace and Enter produce on "fd".
3667 * Returns OK or FAIL.
3668 */
3669 int
3670get_tty_info(int fd, ttyinfo_T *info)
3671{
3672#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673# ifdef HAVE_TERMIOS_H
3674 struct termios keys;
3675# else
3676 struct termio keys;
3677# endif
3678
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003679 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003681 info->backspace = keys.c_cc[VERASE];
3682 info->interrupt = keys.c_cc[VINTR];
3683 if (keys.c_iflag & ICRNL)
3684 info->enter = NL;
3685 else
3686 info->enter = CAR;
3687 if (keys.c_oflag & ONLCR)
3688 info->nl_does_cr = TRUE;
3689 else
3690 info->nl_does_cr = FALSE;
3691 return OK;
3692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003694 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 struct sgttyb keys;
3696
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003697 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003699 info->backspace = keys.sg_erase;
3700 info->interrupt = keys.sg_kill;
3701 info->enter = CAR;
3702 info->nl_does_cr = TRUE;
3703 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003706 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707}
3708
Bram Moolenaar0f873732019-12-05 20:28:46 +01003709#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003711static int mouse_ison = FALSE;
3712
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713/*
3714 * Set mouse clicks on or off.
3715 */
3716 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003717mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003719#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003720 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 int xterm_mouse_vers;
3723
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003724#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003725 if (!on)
3726 // Make sure not tracing mouse movements. Important when a button-down
3727 // was received but no release yet.
3728 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003729#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003730
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003731 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003732#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003733 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003734#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003735 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003736 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 return;
3738
3739 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003740
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003741#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003742 if (ttym_flags == TTYM_URXVT)
3743 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003744 out_str_nf((char_u *)
3745 (on
3746 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3747 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003748 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003749 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003750#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003751
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003752 if (ttym_flags == TTYM_SGR)
3753 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003754 // SGR mode supports columns above 223
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003755 out_str_nf((char_u *)
3756 (on
3757 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3758 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003759 mouse_ison = on;
3760 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003761
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003762#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003763 if (bevalterm_ison != (p_bevalterm && on))
3764 {
3765 bevalterm_ison = (p_bevalterm && on);
3766 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003767 // disable mouse movement events, enabling is below
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003768 out_str_nf((char_u *)
3769 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003770 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003771#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003772
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 if (xterm_mouse_vers > 0)
3774 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003775 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 out_str_nf((char_u *)
3777 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003778 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003779#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003780 bevalterm_ison
3781 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003782#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003783 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003785 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 out_str_nf((char_u *)
3787 (xterm_mouse_vers > 1
3788 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3789 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003790 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003793#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 else if (ttym_flags == TTYM_DEC)
3795 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003796 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003798 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003800 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003804#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 else
3806 {
3807 if (on)
3808 {
3809 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003810 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812 else
3813 {
3814 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003815 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 }
3817 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003820#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003821 else
3822 {
3823 if (on)
3824 {
3825 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003826 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003827 }
3828 else
3829 {
3830 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003831 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003832 }
3833 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003834#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003835
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003836#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 else
3838 {
3839 if (on)
3840 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003841 // D - Enable Mouse up/down messages
3842 // L - Enable Left Button Reporting
3843 // M - Enable Middle Button Reporting
3844 // R - Enable Right Button Reporting
3845 // K - Enable SHIFT and CTRL key Reporting
3846 // + - Enable Advanced messaging of mouse moves and up/down messages
3847 // Q - Quiet No Ack
3848 // # - Numeric value of mouse pointer required
3849 // 0 = Multiview 2000 cursor, used as standard
3850 // 1 = Windows Arrow
3851 // 2 = Windows I Beam
3852 // 3 = Windows Hour Glass
3853 // 4 = Windows Cross Hair
3854 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003855# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003856 // Disables full feedback of pointer movements
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3858 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003859# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3861 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003862# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003863 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
3865 else
3866 {
3867 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3868 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003869 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 }
3871 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003872#endif
3873#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 else
3875 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003876 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 if (on)
3878 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3879 else
3880 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003881 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884}
3885
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003886#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003887/*
3888 * Called when 'balloonevalterm' changed.
3889 */
3890 void
3891mch_bevalterm_changed(void)
3892{
3893 mch_setmouse(mouse_ison);
3894}
3895#endif
3896
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897/*
3898 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3899 */
3900 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003901check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902{
3903# ifdef FEAT_MOUSE_XTERM
3904 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003905# ifdef FEAT_MOUSE_URXVT
3906 && use_xterm_mouse() != 3
3907# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908# ifdef FEAT_GUI
3909 && !gui.in_use
3910# endif
3911 )
3912 {
3913 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003914 ? IF_EB("\233M", CSI_STR "M")
3915 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 if (*p_mouse != NUL)
3917 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003918 // force mouse off and maybe on to send possibly new mouse
3919 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 mch_setmouse(FALSE);
3921 setmouse();
3922 }
3923 }
3924 else
3925 del_mouse_termcode(KS_MOUSE);
3926# endif
3927
3928# ifdef FEAT_MOUSE_GPM
3929 if (!use_xterm_mouse()
3930# ifdef FEAT_GUI
3931 && !gui.in_use
3932# endif
3933 )
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003934 set_mouse_termcode(KS_GPM_MOUSE,
3935 (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3936 else
3937 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938# endif
3939
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003940# ifdef FEAT_SYSMOUSE
3941 if (!use_xterm_mouse()
3942# ifdef FEAT_GUI
3943 && !gui.in_use
3944# endif
3945 )
3946 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3947# endif
3948
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003950 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 if (!use_xterm_mouse()
3952# ifdef FEAT_GUI
3953 && !gui.in_use
3954# endif
3955 )
3956 set_mouse_termcode(KS_JSBTERM_MOUSE,
3957 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3958 else
3959 del_mouse_termcode(KS_JSBTERM_MOUSE);
3960# endif
3961
3962# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003963 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3964 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 if (!use_xterm_mouse()
3966# ifdef FEAT_GUI
3967 && !gui.in_use
3968# endif
3969 )
3970 set_mouse_termcode(KS_NETTERM_MOUSE,
3971 (char_u *)IF_EB("\033}", ESC_STR "}"));
3972 else
3973 del_mouse_termcode(KS_NETTERM_MOUSE);
3974# endif
3975
3976# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003977 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003978 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979# ifdef FEAT_GUI
3980 && !gui.in_use
3981# endif
3982 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003983 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3984 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 else
3986 del_mouse_termcode(KS_DEC_MOUSE);
3987# endif
3988# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01003989 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003990 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991# ifdef FEAT_GUI
3992 && !gui.in_use
3993# endif
3994 )
3995 set_mouse_termcode(KS_PTERM_MOUSE,
3996 (char_u *) IF_EB("\033[", ESC_STR "["));
3997 else
3998 del_mouse_termcode(KS_PTERM_MOUSE);
3999# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02004000# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004001 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02004002# ifdef FEAT_GUI
4003 && !gui.in_use
4004# endif
4005 )
4006 {
4007 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02004008 ? IF_EB("\233*M", CSI_STR "*M")
4009 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02004010
4011 if (*p_mouse != NUL)
4012 {
4013 mch_setmouse(FALSE);
4014 setmouse();
4015 }
4016 }
4017 else
4018 del_mouse_termcode(KS_URXVT_MOUSE);
4019# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004020 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004021# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004022 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004023# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004024 )
4025 {
4026 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02004027 ? IF_EB("\233<*M", CSI_STR "<*M")
4028 : IF_EB("\033[<*M", ESC_STR "[<*M")));
4029
4030 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
4031 ? IF_EB("\233<*m", CSI_STR "<*m")
4032 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004033
4034 if (*p_mouse != NUL)
4035 {
4036 mch_setmouse(FALSE);
4037 setmouse();
4038 }
4039 }
4040 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004041 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004042 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004043 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047#ifndef VMS
4048
4049/*
4050 * Try to get the current window size:
4051 * 1. with an ioctl(), most accurate method
4052 * 2. from the environment variables LINES and COLUMNS
4053 * 3. from the termcap
4054 * 4. keep using the old values
4055 * Return OK when size could be determined, FAIL otherwise.
4056 */
4057 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004058mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059{
4060 long rows = 0;
4061 long columns = 0;
4062 char_u *p;
4063
4064 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 * 1. try using an ioctl. It is the most accurate method.
4066 *
4067 * Try using TIOCGWINSZ first, some systems that have it also define
4068 * TIOCGSIZE but don't have a struct ttysize.
4069 */
4070# ifdef TIOCGWINSZ
4071 {
4072 struct winsize ws;
4073 int fd = 1;
4074
Bram Moolenaar0f873732019-12-05 20:28:46 +01004075 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 if (!isatty(fd) && isatty(read_cmd_fd))
4077 fd = read_cmd_fd;
4078 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4079 {
4080 columns = ws.ws_col;
4081 rows = ws.ws_row;
4082 }
4083 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004084# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085# ifdef TIOCGSIZE
4086 {
4087 struct ttysize ts;
4088 int fd = 1;
4089
Bram Moolenaar0f873732019-12-05 20:28:46 +01004090 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 if (!isatty(fd) && isatty(read_cmd_fd))
4092 fd = read_cmd_fd;
4093 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4094 {
4095 columns = ts.ts_cols;
4096 rows = ts.ts_lines;
4097 }
4098 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004099# endif // TIOCGSIZE
4100# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101
4102 /*
4103 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004104 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4105 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004107 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 {
4109 if ((p = (char_u *)getenv("LINES")))
4110 rows = atoi((char *)p);
4111 if ((p = (char_u *)getenv("COLUMNS")))
4112 columns = atoi((char *)p);
4113 }
4114
4115#ifdef HAVE_TGETENT
4116 /*
4117 * 3. try reading "co" and "li" entries from termcap
4118 */
4119 if (columns == 0 || rows == 0)
4120 getlinecol(&columns, &rows);
4121#endif
4122
4123 /*
4124 * 4. If everything fails, use the old values
4125 */
4126 if (columns <= 0 || rows <= 0)
4127 return FAIL;
4128
4129 Rows = rows;
4130 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004131 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 return OK;
4133}
4134
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004135#if defined(FEAT_TERMINAL) || defined(PROTO)
4136/*
4137 * Report the windows size "rows" and "cols" to tty "fd".
4138 */
4139 int
4140mch_report_winsize(int fd, int rows, int cols)
4141{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004142 int tty_fd;
4143 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004144
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004145 tty_fd = get_tty_fd(fd);
4146 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004147 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004148# if defined(TIOCSWINSZ)
4149 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004150
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004151 ws.ws_col = cols;
4152 ws.ws_row = rows;
4153 ws.ws_xpixel = cols * 5;
4154 ws.ws_ypixel = rows * 10;
4155 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4156 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4157 retval == 0 ? "success" : "failed");
4158# elif defined(TIOCSSIZE)
4159 struct ttysize ts;
4160
4161 ts.ts_cols = cols;
4162 ts.ts_lines = rows;
4163 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4164 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4165 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004166# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004167 if (tty_fd != fd)
4168 close(tty_fd);
4169 }
4170 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004171}
4172#endif
4173
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174/*
4175 * Try to set the window size to Rows and Columns.
4176 */
4177 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004178mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179{
4180 if (*T_CWS)
4181 {
4182 /*
4183 * NOTE: if you get an error here that term_set_winsize() is
4184 * undefined, check the output of configure. It could probably not
4185 * find a ncurses, termcap or termlib library.
4186 */
4187 term_set_winsize((int)Rows, (int)Columns);
4188 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004189 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191}
4192
Bram Moolenaar0f873732019-12-05 20:28:46 +01004193#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
4195/*
4196 * Rows and/or Columns has changed.
4197 */
4198 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004199mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004201 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202}
4203
Bram Moolenaar205b8862011-09-07 15:04:31 +02004204/*
4205 * Wait for process "child" to end.
4206 * Return "child" if it exited properly, <= 0 on error.
4207 */
4208 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004209wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004210{
4211 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004212 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004213
4214 while (wait_pid != child)
4215 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004216 // When compiled with Python threads are probably used, in which case
4217 // wait() sometimes hangs for no obvious reason. Use waitpid()
4218 // instead and loop (like the GUI). Also needed for other interfaces,
4219 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004220# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004221 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004222# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004223 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004224# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004225 if (wait_pid == 0)
4226 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004227 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0981c872020-08-23 14:28:37 +02004228 mch_delay(delay_msec, MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004229 if (++delay_msec > 10)
4230 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004231 continue;
4232 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004233 if (wait_pid <= 0
4234# ifdef ECHILD
4235 && errno == ECHILD
4236# endif
4237 )
4238 break;
4239 }
4240 return wait_pid;
4241}
4242
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004243#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004244/*
4245 * Set the environment for a child process.
4246 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004247 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004248set_child_environment(
4249 long rows,
4250 long columns,
4251 char *term,
4252 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004253{
4254# ifdef HAVE_SETENV
4255 char envbuf[50];
4256# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004257 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004258 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004259 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004260 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004261 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004262# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004263 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004264# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004265# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004266 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004267# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004268# endif
4269
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004270# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004271 setenv("TERM", term, 1);
4272 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004273 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004274 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004275 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004276 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004277 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004278 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004279 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004280# ifdef FEAT_TERMINAL
4281 if (is_terminal)
4282 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004283 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004284 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4285 }
4286# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004287# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004288 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004289# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004290# else
4291 /*
4292 * Putenv does not copy the string, it has to remain valid.
4293 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004294 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004295 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004296 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4297 putenv(envbuf_Term);
4298 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004299 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004300 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4301 putenv(envbuf_Lines);
4302 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4303 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004304 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004305 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004306 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004307# ifdef FEAT_TERMINAL
4308 if (is_terminal)
4309 {
4310 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004311 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004312 putenv(envbuf_Version);
4313 }
4314# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004315# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004316 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4317 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4318 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004319# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004320# endif
4321}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004322
4323 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004324set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004325{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004326 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004327}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004328#endif
4329
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004330#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004331/*
4332 * Open a PTY, with FD for the master and slave side.
4333 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004334 * When successful both file descriptors are stored and the allocated pty name
4335 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004336 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004337 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004338open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004339{
4340 char *tty_name;
4341
Bram Moolenaar59386482019-02-10 22:43:46 +01004342 if (name1 != NULL)
4343 *name1 = NULL;
4344 if (name2 != NULL)
4345 *name2 = NULL;
4346
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004347 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004348 if (*pty_master_fd >= 0)
4349 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004350 // Leaving out O_NOCTTY may lead to waitpid() always returning
4351 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4352 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004353#ifdef O_NOCTTY
4354 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4355#else
4356 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4357#endif
4358 if (*pty_slave_fd < 0)
4359 {
4360 close(*pty_master_fd);
4361 *pty_master_fd = -1;
4362 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004363 else
4364 {
4365 if (name1 != NULL)
4366 *name1 = vim_strsave((char_u *)tty_name);
4367 if (name2 != NULL)
4368 *name2 = vim_strsave((char_u *)tty_name);
4369 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004370 }
4371}
4372#endif
4373
Bram Moolenaarfae42832017-08-01 22:24:26 +02004374/*
4375 * Send SIGINT to a child process if "c" is an interrupt character.
4376 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004377 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004378may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4379{
4380# ifdef SIGINT
4381 if (c == Ctrl_C || c == intr_char)
4382 {
4383# ifdef HAVE_SETSID
4384 kill(-pid, SIGINT);
4385# else
4386 kill(0, SIGINT);
4387# endif
4388 if (wpid > 0)
4389 kill(wpid, SIGINT);
4390 }
4391# endif
4392}
4393
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004394#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004395
Bram Moolenaar0f873732019-12-05 20:28:46 +01004396/*
4397 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4398 * pointers, the last one is NULL.
4399 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4400 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004401 int
4402unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004403 char_u *cmd,
4404 char ***argvp,
4405 char_u **sh_tofree,
4406 char_u **shcf_tofree)
4407{
4408 char **argv = NULL;
4409 int argc;
4410
4411 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004412 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004413 return FAIL;
4414
4415 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4416 return FAIL;
4417 *argvp = argv;
4418
4419 if (cmd != NULL)
4420 {
4421 char_u *s;
4422 char_u *p;
4423
4424 if (extra_shell_arg != NULL)
4425 argv[argc++] = (char *)extra_shell_arg;
4426
Bram Moolenaar0f873732019-12-05 20:28:46 +01004427 // Break 'shellcmdflag' into white separated parts. This doesn't
4428 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004429 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004430 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004431 return FAIL;
4432 s = *shcf_tofree;
4433 p = p_shcf;
4434 while (*p != NUL)
4435 {
4436 argv[argc++] = (char *)s;
4437 while (*p && *p != ' ' && *p != TAB)
4438 *s++ = *p++;
4439 *s++ = NUL;
4440 p = skipwhite(p);
4441 }
4442
4443 argv[argc++] = (char *)cmd;
4444 }
4445 argv[argc] = NULL;
4446 return OK;
4447}
4448#endif
4449
4450#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4451/*
4452 * Use a terminal window to run a shell command in.
4453 */
4454 static int
4455mch_call_shell_terminal(
4456 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004457 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004458{
4459 jobopt_T opt;
4460 char **argv = NULL;
4461 char_u *tofree1 = NULL;
4462 char_u *tofree2 = NULL;
4463 int retval = -1;
4464 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004465 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004466 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004467 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004468
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004469 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004470 goto theend;
4471
4472 init_job_options(&opt);
4473 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4474 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004475 if (buf == NULL)
4476 goto theend;
4477
4478 job = term_getjob(buf->b_term);
4479 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004480
Bram Moolenaar0f873732019-12-05 20:28:46 +01004481 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004482 aucmd_prepbuf(&aco, buf);
4483
4484 clear_oparg(&oa);
4485 while (term_use_loop())
4486 {
4487 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4488 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004489 // If terminal_loop() returns OK we got a key that is handled
4490 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004491 if (terminal_loop(TRUE) == OK)
4492 normal_cmd(&oa, TRUE);
4493 }
4494 else
4495 normal_cmd(&oa, TRUE);
4496 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004497 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004498 ch_log(NULL, "system command finished");
4499
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004500 job_unref(job);
4501
Bram Moolenaar0f873732019-12-05 20:28:46 +01004502 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004503 aucmd_restbuf(&aco);
4504
4505 wait_return(TRUE);
4506 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4507
4508theend:
4509 vim_free(argv);
4510 vim_free(tofree1);
4511 vim_free(tofree2);
4512 return retval;
4513}
4514#endif
4515
4516#ifdef USE_SYSTEM
4517/*
4518 * Use system() to start the shell: simple but slow.
4519 */
4520 static int
4521mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004522 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004523 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
4525#ifdef VMS
4526 char *ifn = NULL;
4527 char *ofn = NULL;
4528#endif
Bram Moolenaar26e86442020-05-17 14:06:16 +02004529 tmode_T tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004530 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004531 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533 out_flush();
4534
4535 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004536 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
Bram Moolenaar62b42182010-09-21 22:09:37 +02004538# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004539 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004540 loose_clipboard();
4541# endif
4542
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 if (cmd == NULL)
4544 x = system((char *)p_sh);
4545 else
4546 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004547# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (ofn = strchr((char *)cmd, '>'))
4549 *ofn++ = '\0';
4550 if (ifn = strchr((char *)cmd, '<'))
4551 {
4552 char *p;
4553
4554 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004555 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (p)
4557 *p = '\0';
4558 }
4559 if (ofn)
4560 x = vms_sys((char *)cmd, ofn, ifn);
4561 else
4562 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004563# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004564 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004566 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (newcmd == NULL)
4568 x = 0;
4569 else
4570 {
4571 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4572 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4573 (char *)p_shcf,
4574 (char *)cmd);
4575 x = system((char *)newcmd);
4576 vim_free(newcmd);
4577 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004578# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580# ifdef VMS
4581 x = vms_sys_status(x);
4582# endif
4583 if (emsg_silent)
4584 ;
4585 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004586 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 else if (x && !(options & SHELL_SILENT))
4588 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004589 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 msg_outnum((long)x);
4591 msg_putchar('\n');
4592 }
4593
4594 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004595 {
4596 // The shell may have messed with the mode, always set it.
4597 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004598 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 resettitle();
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004601# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4602 restore_clipboard();
4603# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004605}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
Bram Moolenaar0f873732019-12-05 20:28:46 +01004607#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
Bram Moolenaar0f873732019-12-05 20:28:46 +01004609# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4610 // 127, some shells use that already
4611# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
Bram Moolenaar13568252018-03-16 20:46:58 +01004613/*
4614 * Don't use system(), use fork()/exec().
4615 */
4616 static int
4617mch_call_shell_fork(
4618 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004619 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004620{
Bram Moolenaar26e86442020-05-17 14:06:16 +02004621 tmode_T tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004623 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 pid_t wait_pid = 0;
4625# ifdef HAVE_UNION_WAIT
4626 union wait status;
4627# else
4628 int status = -1;
4629# endif
4630 int retval = -1;
4631 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004632 char_u *tofree1 = NULL;
4633 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004635 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004636# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004638# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004639 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 int fd_fromshell[2];
4641 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004642 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
4644 out_flush();
4645 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004646 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004647 if (tmode == TMODE_RAW)
4648 // The shell may have messed with the mode, always set it later.
4649 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004651 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004652 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004655 * For the GUI, when writing the output into the buffer and when reading
4656 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4657 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004659 if ((options & (SHELL_READ|SHELL_WRITE))
4660# ifdef FEAT_GUI
4661 || (gui.in_use && show_shell_mess)
4662# endif
4663 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004665# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 /*
4667 * Try to open a master pty.
4668 * If this works, open the slave pty.
4669 * If the slave can't be opened, close the master pty.
4670 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004671 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004672 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 /*
4674 * If not opening a pty or it didn't work, try using pipes.
4675 */
4676 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004677# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 {
4679 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004680 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
4682 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004683 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 {
4685 close(fd_toshell[0]);
4686 close(fd_toshell[1]);
4687 }
4688 }
4689 if (pipe_error)
4690 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004691 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 out_flush();
4693 }
4694 }
4695 }
4696
Bram Moolenaar0f873732019-12-05 20:28:46 +01004697 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004699 SIGSET_DECL(curset)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004700 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004701 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004702 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004704 UNBLOCK_SIGNALS(&curset);
4705
Bram Moolenaar32526b32019-01-19 17:43:09 +01004706 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004707 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004709 || (gui.in_use && show_shell_mess)
4710# endif
4711 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004713# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004714 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
4716 close(pty_master_fd);
4717 close(pty_slave_fd);
4718 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004719 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
4722 close(fd_toshell[0]);
4723 close(fd_toshell[1]);
4724 close(fd_fromshell[0]);
4725 close(fd_fromshell[1]);
4726 }
4727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004729 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004731 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004732 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733
Bram Moolenaar819524702018-02-27 19:10:00 +01004734# ifdef FEAT_JOB_CHANNEL
4735 if (ch_log_active())
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004736 {
4737 ch_log(NULL, "closing channel log in the child process");
Bram Moolenaar819524702018-02-27 19:10:00 +01004738 ch_logfile((char_u *)"", (char_u *)"");
Bram Moolenaar76603ba2020-08-30 17:24:37 +02004739 }
Bram Moolenaar819524702018-02-27 19:10:00 +01004740# endif
4741
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 if (!show_shell_mess || (options & SHELL_EXPAND))
4743 {
4744 int fd;
4745
4746 /*
4747 * Don't want to show any message from the shell. Can't just
4748 * close stdout and stderr though, because some systems will
4749 * break if you try to write to them after that, so we must
4750 * use dup() to replace them with something else -- webb
4751 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4752 * waiting for input.
4753 */
4754 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4755 fclose(stdin);
4756 fclose(stdout);
4757 fclose(stderr);
4758
4759 /*
4760 * If any of these open()'s and dup()'s fail, we just continue
4761 * anyway. It's not fatal, and on most systems it will make
4762 * no difference at all. On a few it will cause the execvp()
4763 * to exit with a non-zero status even when the completion
4764 * could be done, which is nothing too serious. If the open()
4765 * or dup() failed we'd just do the same thing ourselves
4766 * anyway -- webb
4767 */
4768 if (fd >= 0)
4769 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004770 vim_ignored = dup(fd); // To replace stdin (fd 0)
4771 vim_ignored = dup(fd); // To replace stdout (fd 1)
4772 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773
Bram Moolenaar0f873732019-12-05 20:28:46 +01004774 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 close(fd);
4776 }
4777 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004778 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004780 || gui.in_use
4781# endif
4782 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 {
4784
Bram Moolenaardf177f62005-02-22 08:39:57 +00004785# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004786 // Create our own process group, so that the child and all its
4787 // children can be kill()ed. Don't do this when using pipes,
4788 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004789 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004790 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004791 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004792# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004793 // When doing "!xterm&" and 'shell' is bash: the shell
4794 // will exit and send SIGHUP to all processes in its
4795 // group, killing the just started process. Ignore SIGHUP
4796 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004797 signal(SIGHUP, SIG_IGN);
4798# endif
4799 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004800# endif
4801# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004802 if (pty_slave_fd >= 0)
4803 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004804 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004805 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004806 setup_slavepty(pty_slave_fd);
Bram Moolenaarfff10d92021-10-13 10:05:30 +01004807# ifdef TIOCSCTTY
4808 // Try to become controlling tty (probably doesn't work,
4809 // unless run by root)
4810 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
4811# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004812 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004813# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004814 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
Bram Moolenaara5792f52005-11-23 21:25:05 +00004816 /*
4817 * stderr is only redirected when using the GUI, so that a
4818 * program like gpg can still access the terminal to get a
4819 * passphrase using stderr.
4820 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004821# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 if (pty_master_fd >= 0)
4823 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004824 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825
Bram Moolenaar0f873732019-12-05 20:28:46 +01004826 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004828 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004830 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004831 if (gui.in_use)
4832 {
4833 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004834 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
Bram Moolenaar0f873732019-12-05 20:28:46 +01004837 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 }
4839 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004842 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 close(fd_toshell[1]);
4844 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004845 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 close(fd_toshell[0]);
4847
Bram Moolenaar0f873732019-12-05 20:28:46 +01004848 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 close(fd_fromshell[0]);
4850 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004851 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 close(fd_fromshell[1]);
4853
Bram Moolenaara5792f52005-11-23 21:25:05 +00004854# ifdef FEAT_GUI
4855 if (gui.in_use)
4856 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004857 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004858 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004859 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004860 }
4861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 }
4863 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004864
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 /*
4866 * There is no type cast for the argv, because the type may be
4867 * different on different machines. This may cause a warning
4868 * message with strict compilers, don't worry about it.
4869 * Call _exit() instead of exit() to avoid closing the connection
4870 * to the X server (esp. with GTK, which uses atexit()).
4871 */
4872 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004873 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004875 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
4877 /*
4878 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004879 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 */
4881 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004882 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004883 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004884# ifdef FEAT_JOB_CHANNEL
4885 ++dont_check_job_ended;
4886# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 /*
4888 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004889 * This is also used to pipe stdin/stdout to/from the external
4890 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004892 if ((options & (SHELL_READ|SHELL_WRITE))
4893# ifdef FEAT_GUI
4894 || (gui.in_use && show_shell_mess)
4895# endif
4896 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004898# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004900 int buffer_off = 0; // valid bytes in buffer[]
4901 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4902 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 int len;
4904 int p_more_save;
4905 int old_State;
4906 int c;
4907 int toshell_fd;
4908 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004909 garray_T ga;
4910 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004911# ifdef ELAPSED_FUNC
4912 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914
Bram Moolenaardf177f62005-02-22 08:39:57 +00004915# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (pty_master_fd >= 0)
4917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 fromshell_fd = pty_master_fd;
4919 toshell_fd = dup(pty_master_fd);
4920 }
4921 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
4924 close(fd_toshell[0]);
4925 close(fd_fromshell[1]);
4926 toshell_fd = fd_toshell[1];
4927 fromshell_fd = fd_fromshell[0];
4928 }
4929
4930 /*
4931 * Write to the child if there are typed characters.
4932 * Read from the child if there are characters available.
4933 * Repeat the reading a few times if more characters are
4934 * available. Need to check for typed keys now and then, but
4935 * not too often (delays when no chars are available).
4936 * This loop is quit if no characters can be read from the pty
4937 * (WaitForChar detected special condition), or there are no
4938 * characters available and the child has exited.
4939 * Only check if the child has exited when there is no more
4940 * output. The child may exit before all the output has
4941 * been printed.
4942 *
4943 * Currently this busy loops!
4944 * This can probably dead-lock when the write blocks!
4945 */
4946 p_more_save = p_more;
4947 p_more = FALSE;
4948 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004949 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004951 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004952 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004953 // Fork a process that will write the lines to the
4954 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004955 if ((wpid = fork()) == -1)
4956 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004957 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004958 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004959 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004960 {
4961 linenr_T lnum = curbuf->b_op_start.lnum;
4962 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004963 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004964 size_t l;
4965
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004966 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004967 for (;;)
4968 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004969 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004970 if (l == 0)
4971 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004972 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004973 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004974 len = write(toshell_fd, "", (size_t)1);
4975 else
4976 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004977 char_u *s = vim_strchr(lp + written, NL);
4978
Bram Moolenaar89d40322006-08-29 15:30:07 +00004979 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004980 s == NULL ? l
4981 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004982 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004983 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004984 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004985 // Finished a line, add a NL, unless this line
4986 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004987 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004988 || (!curbuf->b_p_bin
4989 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004990 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004991 && (lnum !=
4992 curbuf->b_ml.ml_line_count
4993 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004994 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004995 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004996 ++lnum;
4997 if (lnum > curbuf->b_op_end.lnum)
4998 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004999 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00005000 close(toshell_fd);
5001 toshell_fd = -1;
5002 break;
5003 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005004 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005005 written = 0;
5006 }
5007 else if (len > 0)
5008 written += len;
5009 }
5010 _exit(0);
5011 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005012 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00005013 {
5014 close(toshell_fd);
5015 toshell_fd = -1;
5016 }
5017 }
5018
5019 if (options & SHELL_READ)
5020 ga_init2(&ga, 1, BUFLEN);
5021
5022 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005023# ifdef ELAPSED_FUNC
5024 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005025# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 for (;;)
5027 {
5028 /*
5029 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005030 * if there are any.
5031 * Don't do this if we are expanding wild cards (would eat
5032 * typeahead).
5033 * Don't do this when filtering and terminal is in cooked
5034 * mode, the shell command will handle the I/O. Avoids
5035 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005036 * Don't get characters when the child has already
5037 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005038 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005039 * while (noread_cnt > 4), avoids that ":r !ls" eats
5040 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 */
5042 len = 0;
5043 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005044 && ((options &
5045 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5046 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005047# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005048 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005049# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005050 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005051 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005052 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005054 if (ta_len == 0)
5055 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005056 // Get extra characters when we don't have any.
5057 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005058 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005059# ifdef ELAPSED_FUNC
5060 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005061# endif
5062 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5063 }
5064 if (ta_len > 0 || len > 0)
5065 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 /*
5067 * For pipes:
5068 * Check for CTRL-C: send interrupt signal to child.
5069 * Check for CTRL-D: EOF, close pipe to child.
5070 */
5071 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5072 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 /*
5074 * Send SIGINT to the child's group or all
5075 * processes in our group.
5076 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005077 may_send_sigint(ta_buf[ta_len], pid, wpid);
5078
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 if (pty_master_fd < 0 && toshell_fd >= 0
5080 && ta_buf[ta_len] == Ctrl_D)
5081 {
5082 close(toshell_fd);
5083 toshell_fd = -1;
5084 }
5085 }
5086
Bram Moolenaarf4140482020-02-15 23:06:45 +01005087 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088
5089 /*
5090 * For pipes: echo the typed characters.
5091 * For a pty this does not seem to work.
5092 */
5093 if (pty_master_fd < 0)
5094 {
5095 for (i = ta_len; i < ta_len + len; ++i)
5096 {
5097 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5098 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 else if (has_mbyte)
5100 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005101 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102
5103 msg_outtrans_len(ta_buf + i, l);
5104 i += l - 1;
5105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 else
5107 msg_outtrans_len(ta_buf + i, 1);
5108 }
5109 windgoto(msg_row, msg_col);
5110 out_flush();
5111 }
5112
5113 ta_len += len;
5114
5115 /*
5116 * Write the characters to the child, unless EOF has
5117 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005118 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005119 * When writing buffer lines, drop the typed
5120 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005122 if (options & SHELL_WRITE)
5123 ta_len = 0;
5124 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 {
5126 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5127 if (len > 0)
5128 {
5129 ta_len -= len;
5130 mch_memmove(ta_buf, ta_buf + len, ta_len);
5131 }
5132 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
5135
Bram Moolenaardf177f62005-02-22 08:39:57 +00005136 if (got_int)
5137 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005138 // CTRL-C sends a signal to the child, we ignore it
5139 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005140# ifdef HAVE_SETSID
5141 kill(-pid, SIGINT);
5142# else
5143 kill(0, SIGINT);
5144# endif
5145 if (wpid > 0)
5146 kill(wpid, SIGINT);
5147 got_int = FALSE;
5148 }
5149
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 /*
5151 * Check if the child has any characters to be printed.
5152 * Read them and write them to our window. Repeat this as
5153 * long as there is something to do, avoid the 10ms wait
5154 * for mch_inchar(), or sending typeahead characters to
5155 * the external process.
5156 * TODO: This should handle escape sequences, compatible
5157 * to some terminal (vt52?).
5158 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005159 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005160 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005162 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005165 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005167
5168 noread_cnt = 0;
5169 if (options & SHELL_READ)
5170 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005171 // Do NUL -> NL translation, append NL separated
5172 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005173 for (i = 0; i < len; ++i)
5174 {
5175 if (buffer[i] == NL)
5176 append_ga_line(&ga);
5177 else if (buffer[i] == NUL)
5178 ga_append(&ga, NL);
5179 else
5180 ga_append(&ga, buffer[i]);
5181 }
5182 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005183 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
5185 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005186 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187
Bram Moolenaardf177f62005-02-22 08:39:57 +00005188 len += buffer_off;
5189 buffer[len] = NUL;
5190
Bram Moolenaar0f873732019-12-05 20:28:46 +01005191 // Check if the last character in buffer[] is
5192 // incomplete, keep these bytes for the next
5193 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 for (p = buffer; p < buffer + len; p += l)
5195 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005196 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005198 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 else if (MB_BYTE2LEN(*p) != l)
5200 break;
5201 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005202 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005204 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 if (len >= 12)
5206 ++p;
5207 else
5208 {
5209 buffer_off = len;
5210 continue;
5211 }
5212 }
5213 c = *p;
5214 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005215 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 if (p < buffer + len)
5217 {
5218 *p = c;
5219 buffer_off = (buffer + len) - p;
5220 mch_memmove(buffer, p, buffer_off);
5221 continue;
5222 }
5223 buffer_off = 0;
5224 }
5225 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
5227 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005228 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230
5231 windgoto(msg_row, msg_col);
5232 cursor_on();
5233 out_flush();
5234 if (got_int)
5235 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005236
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005237# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005238 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005239 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005240 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005241
Bram Moolenaar0f873732019-12-05 20:28:46 +01005242 // Avoid that we keep looping here without
5243 // checking for a CTRL-C for a long time. Don't
5244 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005245 if (msec > 2000)
5246 {
5247 noread_cnt = 5;
5248 break;
5249 }
5250 }
5251# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 }
5253
Bram Moolenaar0f873732019-12-05 20:28:46 +01005254 // If we already detected the child has finished, continue
5255 // reading output for a short while. Some text may be
5256 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005257 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005258 {
5259 if (noread_cnt < 5)
5260 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005261 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005262 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005263
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 /*
5265 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005266 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005268# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005269 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005270# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5274 || (wait_pid == pid && WIFEXITED(status)))
5275 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005276 // Don't break the loop yet, try reading more
5277 // characters from "fromshell_fd" first. When using
5278 // pipes there might still be something to read and
5279 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005282 else
5283 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005284
Bram Moolenaar95a51352013-03-21 22:53:50 +01005285# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005286 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005287 clip_update();
5288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 }
5290finished:
5291 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005292 if (options & SHELL_READ)
5293 {
5294 if (ga.ga_len > 0)
5295 {
5296 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005297 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005298 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005299 }
5300 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005301 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005302 ga_clear(&ga);
5303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 /*
5306 * Give all typeahead that wasn't used back to ui_inchar().
5307 */
5308 if (ta_len)
5309 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 State = old_State;
5311 if (toshell_fd >= 0)
5312 close(toshell_fd);
5313 close(fromshell_fd);
5314 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005315# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005316 else
5317 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005318 long delay_msec = 1;
5319
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005320 if (tmode == TMODE_RAW)
5321 // possibly disables modifyOtherKeys, so that the system
5322 // can recognize CTRL-C
5323 out_str(T_CTE);
Bram Moolenaar0981c872020-08-23 14:28:37 +02005324
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005325 /*
5326 * Similar to the loop above, but only handle X events, no
5327 * I/O.
5328 */
5329 for (;;)
5330 {
5331 if (got_int)
5332 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005333 // CTRL-C sends a signal to the child, we ignore it
5334 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005335# ifdef HAVE_SETSID
5336 kill(-pid, SIGINT);
5337# else
5338 kill(0, SIGINT);
5339# endif
5340 got_int = FALSE;
5341 }
5342# ifdef __NeXT__
5343 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5344# else
5345 wait_pid = waitpid(pid, &status, WNOHANG);
5346# endif
5347 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5348 || (wait_pid == pid && WIFEXITED(status)))
5349 {
5350 wait_pid = pid;
5351 break;
5352 }
5353
Bram Moolenaar0f873732019-12-05 20:28:46 +01005354 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005355 clip_update();
5356
Bram Moolenaar0f873732019-12-05 20:28:46 +01005357 // Wait for 1 to 10 msec. 1 is faster but gives the child
Bram Moolenaar0981c872020-08-23 14:28:37 +02005358 // less time, gradually wait longer.
5359 mch_delay(delay_msec,
5360 MCH_DELAY_IGNOREINPUT | MCH_DELAY_SETTMODE);
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005361 if (++delay_msec > 10)
5362 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005363 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02005364
Bram Moolenaar8a3da6a2020-12-08 19:18:37 +01005365 if (tmode == TMODE_RAW)
5366 // possibly enables modifyOtherKeys again
5367 out_str(T_CTI);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005368 }
5369# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
5371 /*
5372 * Wait until our child has exited.
5373 * Ignore wait() returning pids of other children and returning
5374 * because of some signal like SIGWINCH.
5375 * Don't wait if wait_pid was already set above, indicating the
5376 * child already exited.
5377 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005378 if (wait_pid != pid)
5379 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380
Bram Moolenaar624891f2010-10-13 16:22:09 +02005381# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005382 // Close slave side of pty. Only do this after the child has
5383 // exited, otherwise the child may hang when it tries to write on
5384 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005385 if (pty_master_fd >= 0)
5386 close(pty_slave_fd);
5387# endif
5388
Bram Moolenaar0f873732019-12-05 20:28:46 +01005389 // Make sure the child that writes to the external program is
5390 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005391 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005392 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005393 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005394 wait4pid(wpid, NULL);
5395 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005396
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005397# ifdef FEAT_JOB_CHANNEL
5398 --dont_check_job_ended;
5399# endif
5400
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 /*
5402 * Set to raw mode right now, otherwise a CTRL-C after
5403 * catch_signals() will kill Vim.
5404 */
5405 if (tmode == TMODE_RAW)
5406 settmode(TMODE_RAW);
5407 did_settmode = TRUE;
5408 set_signals();
5409
5410 if (WIFEXITED(status))
5411 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005412 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005414 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 {
5416 if (retval == EXEC_FAILED)
5417 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005418 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 msg_outtrans(p_sh);
5420 msg_putchar('\n');
5421 }
5422 else if (!(options & SHELL_SILENT))
5423 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005424 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 msg_outnum((long)retval);
5426 msg_putchar('\n');
5427 }
5428 }
5429 }
5430 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005431 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 }
5433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434
5435error:
5436 if (!did_settmode)
5437 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005438 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 resettitle();
Bram Moolenaar13568252018-03-16 20:46:58 +01005440 vim_free(argv);
5441 vim_free(tofree1);
5442 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
5444 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005445}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005446#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005447
5448 int
5449mch_call_shell(
5450 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005451 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005452{
5453#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5454 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5455 return mch_call_shell_terminal(cmd, options);
5456#endif
5457#ifdef USE_SYSTEM
5458 return mch_call_shell_system(cmd, options);
5459#else
5460 return mch_call_shell_fork(cmd, options);
5461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462}
5463
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005464#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005465 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005466mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005467{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005468 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005469 int fd_in[2] = {-1, -1}; // for stdin
5470 int fd_out[2] = {-1, -1}; // for stdout
5471 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005472 int pty_master_fd = -1;
5473 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005474 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005475 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5476 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5477 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005478 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005479 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5480 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005481 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005482 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005483 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005484
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005485 if (use_out_for_err && use_null_for_out)
5486 use_null_for_err = TRUE;
5487
Bram Moolenaar0f873732019-12-05 20:28:46 +01005488 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005489 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005490
Bram Moolenaarb2412082017-08-20 18:09:14 +02005491 if (options->jo_pty
5492 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005493 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005494 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005495 open_pty(&pty_master_fd, &pty_slave_fd,
5496 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005497
Bram Moolenaar0f873732019-12-05 20:28:46 +01005498 // TODO: without the channel feature connect the child to /dev/null?
5499 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005500 if (use_file_for_in)
5501 {
5502 char_u *fname = options->jo_io_name[PART_IN];
5503
5504 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5505 if (fd_in[0] < 0)
5506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005507 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005508 goto failed;
5509 }
5510 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005511 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005512 // When writing buffer lines to the input don't use the pty, so that
5513 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005514 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5515 && pipe(fd_in) < 0)
5516 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005517
5518 if (use_file_for_out)
5519 {
5520 char_u *fname = options->jo_io_name[PART_OUT];
5521
5522 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5523 if (fd_out[1] < 0)
5524 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005525 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005526 goto failed;
5527 }
5528 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005529 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005530 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005531
5532 if (use_file_for_err)
5533 {
5534 char_u *fname = options->jo_io_name[PART_ERR];
5535
5536 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5537 if (fd_err[1] < 0)
5538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005539 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005540 goto failed;
5541 }
5542 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005543 else if (!use_out_for_err && !use_null_for_err
5544 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005545 goto failed;
5546
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005547 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5548 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005549 if (options->jo_set & JO_CHANNEL)
5550 {
5551 channel = options->jo_channel;
5552 if (channel != NULL)
5553 ++channel->ch_refcount;
5554 }
5555 else
5556 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005557 if (channel == NULL)
5558 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005559 if (job->jv_tty_out != NULL)
5560 ch_log(channel, "using pty %s on fd %d",
5561 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005562 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005563
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005564 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005565 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005566 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005567 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005568 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005569 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005570 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005571 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005572 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005573 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005574 int null_fd = -1;
5575 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005576
Bram Moolenaar0f873732019-12-05 20:28:46 +01005577 // child
5578 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005579 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005580
Bram Moolenaar819524702018-02-27 19:10:00 +01005581# ifdef FEAT_JOB_CHANNEL
5582 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005583 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005584 ch_logfile((char_u *)"", (char_u *)"");
5585# endif
5586
Bram Moolenaar835dc632016-02-07 14:27:38 +01005587# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005588 // Create our own process group, so that the child and all its
5589 // children can be kill()ed. Don't do this when using pipes,
5590 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005591 (void)setsid();
5592# endif
5593
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005594# ifdef FEAT_TERMINAL
5595 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005596 {
5597 char *term = (char *)T_NAME;
5598
5599#ifdef FEAT_GUI
5600 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005601 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005602 term = getenv("TERM");
5603#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005604 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005605 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005606 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005607 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005608 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005609 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005610 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005611 else if (t_colors > 16)
5612 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005613 else
5614 term = "xterm";
5615 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005616 set_child_environment(
5617 (long)options->jo_term_rows,
5618 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005619 term,
5620 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005621 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005622 else
5623# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005624 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005625
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005626 if (options->jo_env != NULL)
5627 {
5628 dict_T *dict = options->jo_env;
5629 hashitem_T *hi;
5630 int todo = (int)dict->dv_hashtab.ht_used;
5631
5632 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5633 if (!HASHITEM_EMPTY(hi))
5634 {
5635 typval_T *item = &dict_lookup(hi)->di_tv;
5636
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005637 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005638 --todo;
5639 }
5640 }
5641
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005642 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005643 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005644 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005645 if (null_fd < 0)
5646 {
5647 perror("opening /dev/null failed");
5648 _exit(OPEN_NULL_FAILED);
5649 }
5650 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005651
Bram Moolenaar223896d2017-08-02 22:33:28 +02005652 if (pty_slave_fd >= 0)
5653 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005654 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005655 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005656# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005657 // Try to become controlling tty (probably doesn't work,
5658 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005659 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5660# endif
5661 }
5662
Bram Moolenaar0f873732019-12-05 20:28:46 +01005663 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005664 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005665 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005666 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005667 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005668 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005669 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005670 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005671
Bram Moolenaar0f873732019-12-05 20:28:46 +01005672 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005673 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005674 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005675 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005676 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005677 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005678 }
5679 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005680 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005681 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005682 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005683 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005684 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005685
Bram Moolenaar0f873732019-12-05 20:28:46 +01005686 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005687 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005688 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005689 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005690 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005691 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005692 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005693 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005694
5695 if (fd_in[0] >= 0)
5696 close(fd_in[0]);
5697 if (fd_in[1] >= 0)
5698 close(fd_in[1]);
5699 if (fd_out[0] >= 0)
5700 close(fd_out[0]);
5701 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005702 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005703 if (fd_err[0] >= 0)
5704 close(fd_err[0]);
5705 if (fd_err[1] >= 0)
5706 close(fd_err[1]);
5707 if (pty_master_fd >= 0)
5708 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005709 close(pty_master_fd); // not used in the child
5710 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005711 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005712
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005713 if (null_fd >= 0)
5714 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005715
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005716 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5717 _exit(EXEC_FAILED);
5718
Bram Moolenaar0f873732019-12-05 20:28:46 +01005719 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005720 execvp(argv[0], argv);
5721
Bram Moolenaar4694a172016-04-21 14:05:23 +02005722 if (stderr_works)
5723 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005724# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005725 // calling free_all_mem() here causes problems. Ignore valgrind
5726 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005727# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005728 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005729 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005730
Bram Moolenaar0f873732019-12-05 20:28:46 +01005731 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005732 UNBLOCK_SIGNALS(&curset);
5733
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005734 job->jv_pid = pid;
5735 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005736 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005737
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005738 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005739 close(pty_slave_fd); // not used in the parent
5740 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005741 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005742 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005743 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005744 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005745 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005746 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005747 if (channel != NULL)
5748 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005749 int in_fd = INVALID_FD;
5750 int out_fd = INVALID_FD;
5751 int err_fd = INVALID_FD;
5752
5753 if (!(use_file_for_in || use_null_for_in))
5754 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5755
5756 if (!(use_file_for_out || use_null_for_out))
5757 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5758
5759 // When using pty_master_fd only set it for stdout, do not duplicate
5760 // it for stderr, it only needs to be read once.
5761 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5762 {
5763 if (fd_err[0] >= 0)
5764 err_fd = fd_err[0];
5765 else if (out_fd != pty_master_fd)
5766 err_fd = pty_master_fd;
5767 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005768
5769 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005770 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005771 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005772 else
5773 {
5774 if (fd_in[1] >= 0)
5775 close(fd_in[1]);
5776 if (fd_out[0] >= 0)
5777 close(fd_out[0]);
5778 if (fd_err[0] >= 0)
5779 close(fd_err[0]);
5780 if (pty_master_fd >= 0)
5781 close(pty_master_fd);
5782 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005783
Bram Moolenaar0f873732019-12-05 20:28:46 +01005784 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005785 return;
5786
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005787failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005788 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005789 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005790 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005791 if (fd_in[1] >= 0)
5792 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005793 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005794 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005795 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005796 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005797 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005798 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005799 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005800 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005801 if (pty_master_fd >= 0)
5802 close(pty_master_fd);
5803 if (pty_slave_fd >= 0)
5804 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005805}
5806
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005807 static char_u *
5808get_signal_name(int sig)
5809{
5810 int i;
5811 char_u numbuf[NUMBUFLEN];
5812
5813 if (sig == SIGKILL)
5814 return vim_strsave((char_u *)"kill");
5815
5816 for (i = 0; signal_info[i].sig != -1; i++)
5817 if (sig == signal_info[i].sig)
5818 return strlow_save((char_u *)signal_info[i].name);
5819
5820 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5821 return vim_strsave(numbuf);
5822}
5823
Bram Moolenaar835dc632016-02-07 14:27:38 +01005824 char *
5825mch_job_status(job_T *job)
5826{
5827# ifdef HAVE_UNION_WAIT
5828 union wait status;
5829# else
5830 int status = -1;
5831# endif
5832 pid_t wait_pid = 0;
5833
5834# ifdef __NeXT__
5835 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5836# else
5837 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5838# endif
5839 if (wait_pid == -1)
5840 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005841 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005842 if (job->jv_status < JOB_ENDED)
5843 ch_log(job->jv_channel, "Job no longer exists: %s",
5844 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005845 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005846 }
5847 if (wait_pid == 0)
5848 return "run";
5849 if (WIFEXITED(status))
5850 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005851 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005852 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005853 if (job->jv_status < JOB_ENDED)
5854 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005855 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005856 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005857 if (WIFSIGNALED(status))
5858 {
5859 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005860 job->jv_termsig = get_signal_name(WTERMSIG(status));
5861 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5862 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5863 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005864 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005865 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005866 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005867
5868return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005869 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005870 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005871 return "dead";
5872}
5873
5874 job_T *
5875mch_detect_ended_job(job_T *job_list)
5876{
5877# ifdef HAVE_UNION_WAIT
5878 union wait status;
5879# else
5880 int status = -1;
5881# endif
5882 pid_t wait_pid = 0;
5883 job_T *job;
5884
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005885# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005886 // Do not do this when waiting for a shell command to finish, we would get
5887 // the exit value here (and discard it), the exit value obtained there
5888 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005889 if (dont_check_job_ended > 0)
5890 return NULL;
5891# endif
5892
Bram Moolenaar97792de2016-10-15 18:36:49 +02005893# ifdef __NeXT__
5894 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5895# else
5896 wait_pid = waitpid(-1, &status, WNOHANG);
5897# endif
5898 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005899 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005900 return NULL;
5901 for (job = job_list; job != NULL; job = job->jv_next)
5902 {
5903 if (job->jv_pid == wait_pid)
5904 {
5905 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005906 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005907 job->jv_exitval = WEXITSTATUS(status);
5908 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005909 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005910 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005911 job->jv_termsig = get_signal_name(WTERMSIG(status));
5912 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005913 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005914 {
5915 ch_log(job->jv_channel, "Job ended");
5916 job->jv_status = JOB_ENDED;
5917 }
5918 return job;
5919 }
5920 }
5921 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005922}
5923
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005924/*
5925 * Send a (deadly) signal to "job".
5926 * Return FAIL if "how" is not a valid name.
5927 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005928 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005929mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005930{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005931 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005932
Bram Moolenaar923d9262016-02-25 20:56:01 +01005933 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005934 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005935 else if (STRCMP(how, "hup") == 0)
5936 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005937 else if (STRCMP(how, "quit") == 0)
5938 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005939 else if (STRCMP(how, "int") == 0)
5940 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005941 else if (STRCMP(how, "kill") == 0)
5942 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005943#ifdef SIGWINCH
5944 else if (STRCMP(how, "winch") == 0)
5945 sig = SIGWINCH;
5946#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005947 else if (isdigit(*how))
5948 sig = atoi((char *)how);
5949 else
5950 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005951
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005952 // Never kill ourselves!
5953 if (job->jv_pid != 0)
5954 {
5955 // TODO: have an option to only kill the process, not the group?
5956 kill(-job->jv_pid, sig);
5957 kill(job->jv_pid, sig);
5958 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005959
Bram Moolenaar835dc632016-02-07 14:27:38 +01005960 return OK;
5961}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005962
5963/*
5964 * Clear the data related to "job".
5965 */
5966 void
5967mch_clear_job(job_T *job)
5968{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005969 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005970# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005971 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005972# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005973 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005974# endif
5975}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005976#endif
5977
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005978#if defined(FEAT_TERMINAL) || defined(PROTO)
5979 int
5980mch_create_pty_channel(job_T *job, jobopt_T *options)
5981{
5982 int pty_master_fd = -1;
5983 int pty_slave_fd = -1;
5984 channel_T *channel;
5985
Bram Moolenaar59386482019-02-10 22:43:46 +01005986 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaard0342202020-06-29 22:40:42 +02005987 if (pty_master_fd < 0 || pty_slave_fd < 0)
5988 return FAIL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005989 close(pty_slave_fd);
5990
5991 channel = add_channel();
5992 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005993 {
5994 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005995 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005996 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005997 if (job->jv_tty_out != NULL)
5998 ch_log(channel, "using pty %s on fd %d",
5999 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006000 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006001 channel->ch_keep_open = TRUE;
6002
Bram Moolenaar0f873732019-12-05 20:28:46 +01006003 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
6004 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02006005 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006006 channel_set_job(channel, job, options);
6007 return OK;
6008}
6009#endif
6010
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011/*
6012 * Check for CTRL-C typed by reading all available characters.
6013 * In cooked mode we should get SIGINT, no need to check.
6014 */
6015 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006016mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017{
Bram Moolenaar26e86442020-05-17 14:06:16 +02006018 if ((mch_cur_tmode == TMODE_RAW || force)
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006019 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 fill_input_buf(FALSE);
6021}
6022
6023/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006024 * Wait "msec" msec until a character is available from the mouse, keyboard,
6025 * from inbuf[].
6026 * "msec" == -1 will block forever.
6027 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006028 * When "ignore_input" is TRUE even check for pending input when input is
6029 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006030 * "interrupted" (if not NULL) is set to TRUE when no character is available
6031 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02006032 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006033 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 */
6035 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006036WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006038#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01006039 return ui_wait_for_chars_or_timer(
6040 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006041#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006042 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006043#endif
6044}
6045
6046/*
6047 * Wait "msec" msec until a character is available from the mouse or keyboard
6048 * or from inbuf[].
6049 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006050 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02006051 * "interrupted" (if not NULL) is set to TRUE when no character is available
6052 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006053 * When a GUI is being used, this will never get called -- webb
6054 */
6055 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006056WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006057{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058#ifdef FEAT_MOUSE_GPM
6059 int gpm_process_wanted;
6060#endif
6061#ifdef FEAT_XCLIPBOARD
6062 int rest;
6063#endif
6064 int avail;
6065
Bram Moolenaar0f873732019-12-05 20:28:46 +01006066 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 return 1;
6068
6069#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006070 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 if (WantQueryMouse)
6072 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006073 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02006074 if (!no_query_mouse_for_testing)
6075 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
6077#endif
6078
6079 /*
6080 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6081 * events. This is a bit complicated, because they might both be defined.
6082 */
6083#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6084# ifdef FEAT_XCLIPBOARD
6085 rest = 0;
6086 if (do_xterm_trace())
6087 rest = msec;
6088# endif
6089 do
6090 {
6091# ifdef FEAT_XCLIPBOARD
6092 if (rest != 0)
6093 {
6094 msec = XT_TRACE_DELAY;
6095 if (rest >= 0 && rest < XT_TRACE_DELAY)
6096 msec = rest;
6097 if (rest >= 0)
6098 rest -= msec;
6099 }
6100# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006101# ifdef FEAT_SOUND_CANBERRA
6102 // Invoke any pending sound callbacks.
6103 if (has_sound_callback_in_queue())
6104 invoke_sound_callback();
6105# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106# ifdef FEAT_MOUSE_GPM
6107 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006108 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006109 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006110 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006112 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006114# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006116 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 return 1;
6118# ifdef FEAT_XCLIPBOARD
6119 if (rest == 0 || !do_xterm_trace())
6120# endif
6121 break;
6122 }
6123 }
6124 while (FALSE
6125# ifdef FEAT_MOUSE_GPM
6126 || (gpm_process_wanted && mch_gpm_process() == 0)
6127# endif
6128# ifdef FEAT_XCLIPBOARD
6129 || (!avail && rest != 0)
6130# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006131 )
6132 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133
6134#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006135 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136#endif
6137 return avail;
6138}
6139
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006140#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141/*
6142 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006143 * "msec" == 0 will check for characters once.
6144 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006147 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006148 * "interrupted" (if not NULL) is set to TRUE when no character is available
6149 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 */
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006151 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02006152RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153{
6154 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006155 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006156#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 static int busy = FALSE;
6158
Bram Moolenaar0f873732019-12-05 20:28:46 +01006159 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160# define MAY_LOOP
6161
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006162# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006163 // Remember at what time we started, so that we know how much longer we
6164 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006165 long start_msec = msec;
6166 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006168 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006169 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170# endif
6171
Bram Moolenaar0f873732019-12-05 20:28:46 +01006172 // Handle being called recursively. This may happen for the session
6173 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 if (busy)
6175 return 0;
6176#endif
6177
6178#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006179 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180#endif
6181 {
6182#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006183 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006184# ifdef FEAT_MZSCHEME
6185 int mzquantum_used = FALSE;
6186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187#endif
6188#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006189 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006190 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 int nfd;
6192# ifdef FEAT_XCLIPBOARD
6193 int xterm_idx = -1;
6194# endif
6195# ifdef FEAT_MOUSE_GPM
6196 int gpm_idx = -1;
6197# endif
6198# ifdef USE_XSMP
6199 int xsmp_idx = -1;
6200# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006201 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006203# ifdef FEAT_MZSCHEME
6204 mzvim_check_threads();
6205 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6206 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006207 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006208 mzquantum_used = TRUE;
6209 }
6210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 fds[0].fd = fd;
6212 fds[0].events = POLLIN;
6213 nfd = 1;
6214
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006216 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 if (xterm_Shell != (Widget)0)
6218 {
6219 xterm_idx = nfd;
6220 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6221 fds[nfd].events = POLLIN;
6222 nfd++;
6223 }
6224# endif
6225# ifdef FEAT_MOUSE_GPM
6226 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6227 {
6228 gpm_idx = nfd;
6229 fds[nfd].fd = gpm_fd;
6230 fds[nfd].events = POLLIN;
6231 nfd++;
6232 }
6233# endif
6234# ifdef USE_XSMP
6235 if (xsmp_icefd != -1)
6236 {
6237 xsmp_idx = nfd;
6238 fds[nfd].fd = xsmp_icefd;
6239 fds[nfd].events = POLLIN;
6240 nfd++;
6241 }
6242# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006243#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006244 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006245#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006246 if (interrupted != NULL)
6247 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006249 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006250
6251 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006252 if (result == 0 && interrupted != NULL && ret > 0)
6253 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006254
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006255# ifdef FEAT_MZSCHEME
6256 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006257 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006258 finished = FALSE;
6259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261# ifdef FEAT_XCLIPBOARD
6262 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6263 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006264 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006266 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 finished = FALSE;
6268 }
6269# endif
6270# ifdef FEAT_MOUSE_GPM
6271 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273# endif
6274# ifdef USE_XSMP
6275 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6276 {
6277 if (fds[xsmp_idx].revents & POLLIN)
6278 {
6279 busy = TRUE;
6280 xsmp_handle_requests();
6281 busy = FALSE;
6282 }
6283 else if (fds[xsmp_idx].revents & POLLHUP)
6284 {
6285 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006286 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 xsmp_close();
6288 }
6289 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006290 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 }
6292# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006293#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006294 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006295 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006296 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298
Bram Moolenaar0f873732019-12-05 20:28:46 +01006299#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300
6301 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006302 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006303 // These are static because they can take 8 Kbyte each and cause the
6304 // signal stack to run out with -O3.
6305 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006307 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006309# ifdef FEAT_MZSCHEME
6310 mzvim_check_threads();
6311 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6312 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006313 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006314 mzquantum_used = TRUE;
6315 }
6316# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006318 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006320 tv.tv_sec = towait / 1000;
6321 tv.tv_usec = (towait % 1000) * (1000000/1000);
6322 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006324 else
6325 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326
6327 /*
6328 * Select on ready for reading and exceptional condition (end of file).
6329 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006330select_eintr:
6331 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006332 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 FD_ZERO(&efds);
6334 FD_SET(fd, &rfds);
6335# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006336 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 FD_SET(fd, &efds);
6338# endif
6339 maxfd = fd;
6340
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006342 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 if (xterm_Shell != (Widget)0)
6344 {
6345 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6346 if (maxfd < ConnectionNumber(xterm_dpy))
6347 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006348
Bram Moolenaar0f873732019-12-05 20:28:46 +01006349 // An event may have already been read but not handled. In
6350 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006351 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 }
6353# endif
6354# ifdef FEAT_MOUSE_GPM
6355 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6356 {
6357 FD_SET(gpm_fd, &rfds);
6358 FD_SET(gpm_fd, &efds);
6359 if (maxfd < gpm_fd)
6360 maxfd = gpm_fd;
6361 }
6362# endif
6363# ifdef USE_XSMP
6364 if (xsmp_icefd != -1)
6365 {
6366 FD_SET(xsmp_icefd, &rfds);
6367 FD_SET(xsmp_icefd, &efds);
6368 if (maxfd < xsmp_icefd)
6369 maxfd = xsmp_icefd;
6370 }
6371# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006372# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006373 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006374# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006375 if (interrupted != NULL)
6376 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
Bram Moolenaar643b6142018-09-12 20:29:09 +02006378 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6379 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006380 result = ret > 0 && FD_ISSET(fd, &rfds);
6381 if (result)
6382 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006383 else if (interrupted != NULL && ret > 0)
6384 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006385
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006386# ifdef EINTR
6387 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006388 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006389 // Check whether window has been resized, EINTR may be caused by
6390 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006391 if (do_resize)
6392 handle_resize();
6393
Bram Moolenaar0f873732019-12-05 20:28:46 +01006394 // Interrupted by a signal, need to try again. We ignore msec
6395 // here, because we do want to check even after a timeout if
6396 // characters are available. Needed for reading output of an
6397 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006398 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006399 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006400# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006401# ifdef __TANDEM
6402 if (ret == -1 && errno == ENOTSUP)
6403 {
6404 FD_ZERO(&rfds);
6405 FD_ZERO(&efds);
6406 ret = 0;
6407 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006408# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006409# ifdef FEAT_MZSCHEME
6410 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006411 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006412 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413# endif
6414
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415# ifdef FEAT_XCLIPBOARD
6416 if (ret > 0 && xterm_Shell != (Widget)0
6417 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6418 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006419 xterm_update(); // Maybe we should hand out clipboard
6420 // continue looping when we only got the X event and the input
6421 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 if (--ret == 0 && !input_available())
6423 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006424 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 finished = FALSE;
6426 }
6427 }
6428# endif
6429# ifdef FEAT_MOUSE_GPM
6430 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6431 {
6432 if (FD_ISSET(gpm_fd, &efds))
6433 gpm_close();
6434 else if (FD_ISSET(gpm_fd, &rfds))
6435 *check_for_gpm = 1;
6436 }
6437# endif
6438# ifdef USE_XSMP
6439 if (ret > 0 && xsmp_icefd != -1)
6440 {
6441 if (FD_ISSET(xsmp_icefd, &efds))
6442 {
6443 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006444 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 xsmp_close();
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 else if (FD_ISSET(xsmp_icefd, &rfds))
6450 {
6451 busy = TRUE;
6452 xsmp_handle_requests();
6453 busy = FALSE;
6454 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006455 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 }
6457 }
6458# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006459#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006460 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006461 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006462 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464
Bram Moolenaar0f873732019-12-05 20:28:46 +01006465#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
6467#ifdef MAY_LOOP
6468 if (finished || msec == 0)
6469 break;
6470
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006471# ifdef FEAT_CLIENTSERVER
6472 if (server_waiting())
6473 break;
6474# endif
6475
Bram Moolenaar0f873732019-12-05 20:28:46 +01006476 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 if (msec > 0)
6478 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006479# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006480 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006481 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006483 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 msec = msec / 2;
6485# endif
6486 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006487 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 }
6489#endif
6490 }
6491
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006492 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493}
6494
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006496 * Expand a path into all matching files and/or directories. Handles "*",
6497 * "?", "[a-z]", "**", etc.
6498 * "path" has backslashes before chars that are not to be expanded.
6499 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 */
6501 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006502mch_expandpath(
6503 garray_T *gap,
6504 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006505 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
Bram Moolenaar02743632005-07-25 20:42:36 +00006507 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
6510/*
6511 * mch_expand_wildcards() - this code does wild-card pattern matching using
6512 * the shell
6513 *
6514 * return OK for success, FAIL for error (you may lose some memory) and put
6515 * an error message in *file.
6516 *
6517 * num_pat is number of input patterns
6518 * pat is array of pointers to input patterns
6519 * num_file is pointer to number of matched file names
6520 * file is pointer to array of pointers to matched file names
6521 */
6522
6523#ifndef SEEK_SET
6524# define SEEK_SET 0
6525#endif
6526#ifndef SEEK_END
6527# define SEEK_END 2
6528#endif
6529
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006530#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006531
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006533mch_expand_wildcards(
6534 int num_pat,
6535 char_u **pat,
6536 int *num_file,
6537 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006538 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539{
6540 int i;
6541 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006542 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 char_u *p;
6544 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545
Bram Moolenaarc7247912008-01-13 12:54:11 +00006546 /*
6547 * This is the non-OS/2 implementation (really Unix).
6548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 int j;
6550 char_u *tempname;
6551 char_u *command;
6552 FILE *fd;
6553 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006554#define STYLE_ECHO 0 // use "echo", the default
6555#define STYLE_GLOB 1 // use "glob", for csh
6556#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6557#define STYLE_PRINT 3 // use "print -N", for zsh
6558#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6559 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 int shell_style = STYLE_ECHO;
6561 int check_spaces;
6562 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006563 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006564 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006565 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
Bram Moolenaar0f873732019-12-05 20:28:46 +01006567 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 *file = NULL;
6569
6570 /*
6571 * If there are no wildcards, just copy the names to allocated memory.
6572 * Saves a lot of time, because we don't have to start a new shell.
6573 */
6574 if (!have_wildcard(num_pat, pat))
6575 return save_patterns(num_pat, pat, num_file, file);
6576
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006577# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006578 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006579 if (sandbox != 0 && check_secure())
6580 return FAIL;
6581# endif
6582
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 /*
6584 * Don't allow the use of backticks in secure and restricted mode.
6585 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006586 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 for (i = 0; i < num_pat; ++i)
6588 if (vim_strchr(pat[i], '`') != NULL
6589 && (check_restricted() || check_secure()))
6590 return FAIL;
6591
6592 /*
6593 * get a name for the temp file
6594 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006595 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006597 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 return FAIL;
6599 }
6600
6601 /*
6602 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006603 * file.
6604 * STYLE_BT: NL separated
6605 * If expanding `cmd` execute it directly.
6606 * STYLE_GLOB: NUL separated
6607 * If we use *csh, "glob" will work better than "echo".
6608 * STYLE_PRINT: NL or NUL separated
6609 * If we use *zsh, "print -N" will work better than "glob".
6610 * STYLE_VIMGLOB: NL separated
6611 * If we use *sh*, we define "vimglob()".
6612 * STYLE_ECHO: space separated.
6613 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 */
6615 if (num_pat == 1 && *pat[0] == '`'
6616 && (len = STRLEN(pat[0])) > 2
6617 && *(pat[0] + len - 1) == '`')
6618 shell_style = STYLE_BT;
6619 else if ((len = STRLEN(p_sh)) >= 3)
6620 {
6621 if (STRCMP(p_sh + len - 3, "csh") == 0)
6622 shell_style = STYLE_GLOB;
6623 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6624 shell_style = STYLE_PRINT;
6625 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006626 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6627 "sh") != NULL)
6628 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629
Bram Moolenaar0f873732019-12-05 20:28:46 +01006630 // Compute the length of the command. We need 2 extra bytes: for the
6631 // optional '&' and for the NUL.
6632 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006634 if (shell_style == STYLE_VIMGLOB)
6635 len += STRLEN(sh_vimglob_func);
6636
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006637 for (i = 0; i < num_pat; ++i)
6638 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006639 // Count the length of the patterns in the same way as they are put in
6640 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006641#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006642 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006643#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006644 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006645 for (j = 0; pat[i][j] != NUL; ++j)
6646 {
6647 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006648 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006649 ++len;
6650 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006651#endif
6652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 command = alloc(len);
6654 if (command == NULL)
6655 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006656 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 vim_free(tempname);
6658 return FAIL;
6659 }
6660
6661 /*
6662 * Build the shell command:
6663 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6664 * recognizes this).
6665 * - Add the shell command to print the expanded names.
6666 * - Add the temp file name.
6667 * - Add the file name patterns.
6668 */
6669 if (shell_style == STYLE_BT)
6670 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006671 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006672 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006673 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006675 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006676 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006678 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006680 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 *p = ' ';
6682 }
6683 STRCAT(command, ">");
6684 }
6685 else
6686 {
Christian Brabandt8b8d8292021-11-19 12:37:36 +00006687 STRCPY(command, "");
6688 if (shell_style == STYLE_GLOB)
6689 {
6690 // Assume the nonomatch option is valid only for csh like shells,
6691 // otherwise, this may set the positional parameters for the shell,
6692 // e.g. "$*".
6693 if (flags & EW_NOTFOUND)
6694 STRCAT(command, "set nonomatch; ");
6695 else
6696 STRCAT(command, "unset nonomatch; ");
6697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 if (shell_style == STYLE_GLOB)
6699 STRCAT(command, "glob >");
6700 else if (shell_style == STYLE_PRINT)
6701 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006702 else if (shell_style == STYLE_VIMGLOB)
6703 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 else
6705 STRCAT(command, "echo >");
6706 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006707
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006709
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 if (shell_style != STYLE_BT)
6711 for (i = 0; i < num_pat; ++i)
6712 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006713 // When using system() always add extra quotes, because the shell
6714 // is started twice. Otherwise put a backslash before special
6715 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716#ifdef USE_SYSTEM
6717 STRCAT(command, " \"");
6718 STRCAT(command, pat[i]);
6719 STRCAT(command, "\"");
6720#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006721 int intick = FALSE;
6722
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 p = command + STRLEN(command);
6724 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006725 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006726 {
6727 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006728 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006729 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6730 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006731 // Remove a backslash, take char literally. But keep
6732 // backslash inside backticks, before a special character
6733 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006734 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006735 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6736 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006737 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006738 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006739 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006740 else if (!intick
6741 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6742 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006743 // Put a backslash before a special character, but not
6744 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6745 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006746 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006747
Bram Moolenaar0f873732019-12-05 20:28:46 +01006748 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006749 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 *p = NUL;
6752#endif
6753 }
6754 if (flags & EW_SILENT)
6755 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006756 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006757 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
6759 /*
6760 * Using zsh -G: If a pattern has no matches, it is just deleted from
6761 * the argument list, otherwise zsh gives an error message and doesn't
6762 * expand any other pattern.
6763 */
6764 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006765 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766
6767 /*
6768 * If we use -f then shell variables set in .cshrc won't get expanded.
6769 * vi can do it, so we will too, but it is only necessary if there is a "$"
6770 * in one of the patterns, otherwise we can still use the fast option.
6771 */
6772 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006773 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774
6775 /*
6776 * execute the shell command
6777 */
6778 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6779
Bram Moolenaar0f873732019-12-05 20:28:46 +01006780 // When running in the background, give it some time to create the temp
6781 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006782 if (ampersand)
Bram Moolenaar0981c872020-08-23 14:28:37 +02006783 mch_delay(10L, MCH_DELAY_IGNOREINPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784
Bram Moolenaar0f873732019-12-05 20:28:46 +01006785 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 show_shell_mess = TRUE;
6787 vim_free(command);
6788
Bram Moolenaar0f873732019-12-05 20:28:46 +01006789 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {
6791 mch_remove(tempname);
6792 vim_free(tempname);
6793 /*
6794 * With interactive completion, the error message is not printed.
6795 * However with USE_SYSTEM, I don't know how to turn off error messages
6796 * from the shell, so screen may still get messed up -- webb.
6797 */
6798#ifndef USE_SYSTEM
6799 if (!(flags & EW_SILENT))
6800#endif
6801 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006802 redraw_later_clear(); // probably messed up screen
6803 msg_putchar('\n'); // clear bottom line quickly
6804 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805#ifdef USE_SYSTEM
6806 if (!(flags & EW_SILENT))
6807#endif
6808 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006809 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006810 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 }
6812 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006813 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6814 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (shell_style == STYLE_BT)
6816 return FAIL;
6817 goto notfound;
6818 }
6819
6820 /*
6821 * read the names from the file into memory
6822 */
6823 fd = fopen((char *)tempname, READBIN);
6824 if (fd == NULL)
6825 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006826 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 if (!(flags & EW_SILENT))
6828 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006829 msg(_(e_cannot_expand_wildcards));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006830 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 }
6832 vim_free(tempname);
6833 goto notfound;
6834 }
6835 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006836 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006838 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006839 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006840 buffer = NULL;
6841 else
6842 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 if (buffer == NULL)
6844 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006845 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 mch_remove(tempname);
6847 vim_free(tempname);
6848 fclose(fd);
6849 return FAIL;
6850 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006851 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 i = fread((char *)buffer, 1, len, fd);
6853 fclose(fd);
6854 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006855 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006857 // unexpected read error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006858 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 vim_free(tempname);
6860 vim_free(buffer);
6861 return FAIL;
6862 }
6863 vim_free(tempname);
6864
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006865# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006866 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006868 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6870 *p++ = buffer[i];
6871 len = p - buffer;
6872# endif
6873
6874
Bram Moolenaar0f873732019-12-05 20:28:46 +01006875 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 if (shell_style == STYLE_ECHO)
6877 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006878 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006880 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {
6882 while (*p != ' ' && *p != '\n')
6883 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006884 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006887 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006888 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006890 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006892 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 {
6894 while (*p != '\n' && *p != NUL)
6895 ++p;
6896 if (*p != NUL)
6897 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006898 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 }
6900 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006901 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 else
6903 {
6904 /*
6905 * Some versions of zsh use spaces instead of NULs to separate
6906 * results. Only do this when there is no NUL before the end of the
6907 * buffer, otherwise we would never be able to use file names with
6908 * embedded spaces when zsh does use NULs.
6909 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6910 * don't check for spaces again.
6911 */
6912 check_spaces = FALSE;
6913 if (shell_style == STYLE_PRINT && !did_find_nul)
6914 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006915 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006916 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006917 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 did_find_nul = TRUE;
6919 else
6920 check_spaces = TRUE;
6921 }
6922
6923 /*
6924 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6925 * already is one, for STYLE_GLOB it needs to be added.
6926 */
6927 if (len && buffer[len - 1] == NUL)
6928 --len;
6929 else
6930 buffer[len] = NUL;
6931 i = 0;
6932 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006933 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 {
6935 ++i;
6936 *p = NUL;
6937 }
6938 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006939 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 }
6941 if (i == 0)
6942 {
6943 /*
6944 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6945 * /bin/sh will happily expand it to nothing rather than returning an
6946 * error; and hey, it's good to check anyway -- webb.
6947 */
6948 vim_free(buffer);
6949 goto notfound;
6950 }
6951 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006952 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 if (*file == NULL)
6954 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006955 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 vim_free(buffer);
6957 return FAIL;
6958 }
6959
6960 /*
6961 * Isolate the individual file names.
6962 */
6963 p = buffer;
6964 for (i = 0; i < *num_file; ++i)
6965 {
6966 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006967 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006968 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6969 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006971 while (!(shell_style == STYLE_ECHO && *p == ' ')
6972 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006974 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 *p = NUL;
6976 else
6977 {
6978 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006979 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 }
6981 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006982 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006984 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006986 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 }
6988 }
6989
6990 /*
6991 * Move the file names to allocated memory.
6992 */
6993 for (j = 0, i = 0; i < *num_file; ++i)
6994 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006995 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6997 continue;
6998
Bram Moolenaar0f873732019-12-05 20:28:46 +01006999 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 dir = (mch_isdir((*file)[i]));
7001 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
7002 continue;
7003
Bram Moolenaar0f873732019-12-05 20:28:46 +01007004 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01007005 if (!dir && (flags & EW_EXEC)
7006 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00007007 continue;
7008
Bram Moolenaar964b3742019-05-24 18:54:09 +02007009 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 if (p)
7011 {
7012 STRCPY(p, (*file)[i]);
7013 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007014 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 (*file)[j++] = p;
7016 }
7017 }
7018 vim_free(buffer);
7019 *num_file = j;
7020
Bram Moolenaar0f873732019-12-05 20:28:46 +01007021 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007023 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 goto notfound;
7025 }
7026
7027 return OK;
7028
7029notfound:
7030 if (flags & EW_NOTFOUND)
7031 return save_patterns(num_pat, pat, num_file, file);
7032 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033}
7034
Bram Moolenaar0f873732019-12-05 20:28:46 +01007035#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007038save_patterns(
7039 int num_pat,
7040 char_u **pat,
7041 int *num_file,
7042 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043{
7044 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00007045 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007047 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 if (*file == NULL)
7049 return FAIL;
7050 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00007051 {
7052 s = vim_strsave(pat[i]);
7053 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007054 // Be compatible with expand_filename(): halve the number of
7055 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00007056 backslash_halve(s);
7057 (*file)[i] = s;
7058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 *num_file = num_pat;
7060 return OK;
7061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063/*
7064 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7065 * expand.
7066 */
7067 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007068mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007070 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 if (*p == '\\' && p[1] != NUL)
7073 ++p;
7074 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 if (vim_strchr((char_u *)
7076#ifdef VMS
7077 "*?%"
7078#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080#endif
7081 , *p) != NULL)
7082 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 }
7084 return FALSE;
7085}
7086
7087/*
7088 * Return TRUE if the string "p" contains a wildcard.
7089 * Don't recognize '~' at the end as a wildcard.
7090 */
7091 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007092mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007094 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 if (*p == '\\' && p[1] != NUL)
7097 ++p;
7098 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 if (vim_strchr((char_u *)
7100#ifdef VMS
7101 "*?%$"
7102#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104#endif
7105 , *p) != NULL
7106 || (*p == '~' && p[1] != NUL))
7107 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 }
7109 return FALSE;
7110}
7111
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007113have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114{
7115 int i;
7116
7117 for (i = 0; i < num; i++)
7118 if (mch_has_wildcard(file[i]))
7119 return 1;
7120 return 0;
7121}
7122
7123 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007124have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
7126 int i;
7127
7128 for (i = 0; i < num; i++)
7129 if (vim_strchr(file[i], '$') != NULL)
7130 return TRUE;
7131 return FALSE;
7132}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007134#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135/*
7136 * Scaled-down version of rename(), which is missing in Xenix.
7137 * This version can only move regular files and will fail if the
7138 * destination exists.
7139 */
7140 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007141mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
7143 struct stat st;
7144
Bram Moolenaar0f873732019-12-05 20:28:46 +01007145 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007147 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007149 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 return 0;
7151 return -1;
7152}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007153#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007155#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156/*
7157 * Initializes connection with gpm (if it isn't already opened)
7158 * Return 1 if succeeded (or connection already opened), 0 if failed
7159 */
7160 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007161gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007163 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
7165 if (!gpm_flag)
7166 {
7167 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7168 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007169 // Default handling for mouse move
7170 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 gpm_connect.maxMod = 0xffff;
7172 if (Gpm_Open(&gpm_connect, 0) > 0)
7173 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007174 // gpm library tries to handling TSTP causes
7175 // problems. Anyways, we close connection to Gpm whenever
7176 // we are going to suspend or starting an external process
7177 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007178# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007180# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007181 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 }
7183 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007184 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 return 0;
7186 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007187 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188}
7189
7190/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007191 * Returns TRUE if the GPM mouse is enabled.
7192 */
7193 int
7194gpm_enabled(void)
7195{
7196 return gpm_flag && gpm_fd >= 0;
7197}
7198
7199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 */
7202 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007203gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007205 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 Gpm_Close();
7207}
7208
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007209/*
7210 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007212 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 */
7214 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007215mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 int button;
7218 static Gpm_Event gpm_event;
7219 char_u string[6];
7220 int_u vim_modifiers;
7221 int row,col;
7222 unsigned char buttons_mask;
7223 unsigned char gpm_modifiers;
7224 static unsigned char old_buttons = 0;
7225
7226 Gpm_GetEvent(&gpm_event);
7227
7228#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007229 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 if (hold_gui_events)
7231 return 0;
7232#endif
7233
7234 row = gpm_event.y - 1;
7235 col = gpm_event.x - 1;
7236
Bram Moolenaar0f873732019-12-05 20:28:46 +01007237 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 string[1] = 'M';
7239 string[2] = 'G';
7240 switch (GPM_BARE_EVENTS(gpm_event.type))
7241 {
7242 case GPM_DRAG:
7243 string[3] = MOUSE_DRAG;
7244 break;
7245 case GPM_DOWN:
7246 buttons_mask = gpm_event.buttons & ~old_buttons;
7247 old_buttons = gpm_event.buttons;
7248 switch (buttons_mask)
7249 {
7250 case GPM_B_LEFT:
7251 button = MOUSE_LEFT;
7252 break;
7253 case GPM_B_MIDDLE:
7254 button = MOUSE_MIDDLE;
7255 break;
7256 case GPM_B_RIGHT:
7257 button = MOUSE_RIGHT;
7258 break;
7259 default:
7260 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007261 // Don't know what to do. Can more than one button be
7262 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 }
7264 string[3] = (char_u)(button | 0x20);
7265 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7266 break;
7267 case GPM_UP:
7268 string[3] = MOUSE_RELEASE;
7269 old_buttons &= ~gpm_event.buttons;
7270 break;
7271 default:
7272 return 0;
7273 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007274 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 gpm_modifiers = gpm_event.modifiers;
7276 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007277 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7278 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7279 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7281 vim_modifiers |= MOUSE_SHIFT;
7282
7283 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7284 vim_modifiers |= MOUSE_CTRL;
7285 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7286 vim_modifiers |= MOUSE_ALT;
7287 string[3] |= vim_modifiers;
7288 string[4] = (char_u)(col + ' ' + 1);
7289 string[5] = (char_u)(row + ' ' + 1);
7290 add_to_input_buf(string, 6);
7291 return 6;
7292}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007293#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007295#ifdef FEAT_SYSMOUSE
7296/*
7297 * Initialize connection with sysmouse.
7298 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7299 * output, any sysmouse output than will be processed via sig_sysmouse().
7300 * Return OK if succeeded, FAIL if failed.
7301 */
7302 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007303sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007304{
7305 struct mouse_info mouse;
7306
7307 mouse.operation = MOUSE_MODE;
7308 mouse.u.mode.mode = 0;
7309 mouse.u.mode.signal = SIGUSR2;
7310 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7311 {
7312 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7313 mouse.operation = MOUSE_SHOW;
7314 ioctl(1, CONS_MOUSECTL, &mouse);
7315 return OK;
7316 }
7317 return FAIL;
7318}
7319
7320/*
7321 * Stop processing SIGUSR2 signals, and also make sure that
7322 * virtual console do not send us any sysmouse related signal.
7323 */
7324 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007325sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007326{
7327 struct mouse_info mouse;
7328
7329 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7330 mouse.operation = MOUSE_MODE;
7331 mouse.u.mode.mode = 0;
7332 mouse.u.mode.signal = 0;
7333 ioctl(1, CONS_MOUSECTL, &mouse);
7334}
7335
7336/*
7337 * Gets info from sysmouse and adds special keys to input buf.
7338 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007339 static RETSIGTYPE
7340sig_sysmouse SIGDEFARG(sigarg)
7341{
7342 struct mouse_info mouse;
7343 struct video_info video;
7344 char_u string[6];
7345 int row, col;
7346 int button;
7347 int buttons;
7348 static int oldbuttons = 0;
7349
7350#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007351 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007352 if (hold_gui_events)
7353 return;
7354#endif
7355
7356 mouse.operation = MOUSE_GETINFO;
7357 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7358 && ioctl(1, FBIO_MODEINFO, &video) != -1
7359 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7360 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7361 {
7362 row = mouse.u.data.y / video.vi_cheight;
7363 col = mouse.u.data.x / video.vi_cwidth;
7364 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007365 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007366 string[1] = 'M';
7367 string[2] = 'S';
7368 if (oldbuttons == buttons && buttons != 0)
7369 {
7370 button = MOUSE_DRAG;
7371 }
7372 else
7373 {
7374 switch (buttons)
7375 {
7376 case 0:
7377 button = MOUSE_RELEASE;
7378 break;
7379 case 1:
7380 button = MOUSE_LEFT;
7381 break;
7382 case 2:
7383 button = MOUSE_MIDDLE;
7384 break;
7385 case 4:
7386 button = MOUSE_RIGHT;
7387 break;
7388 default:
7389 return;
7390 }
7391 oldbuttons = buttons;
7392 }
7393 string[3] = (char_u)(button);
7394 string[4] = (char_u)(col + ' ' + 1);
7395 string[5] = (char_u)(row + ' ' + 1);
7396 add_to_input_buf(string, 6);
7397 }
7398 return;
7399}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007400#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007401
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007403typedef char_u * (*STRPROCSTR)(char_u *);
7404typedef char_u * (*INTPROCSTR)(int);
7405typedef int (*STRPROCINT)(char_u *);
7406typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407
7408/*
7409 * Call a DLL routine which takes either a string or int param
7410 * and returns an allocated string.
7411 */
7412 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007413mch_libcall(
7414 char_u *libname,
7415 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007416 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007417 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007418 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007419 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
7421# if defined(USE_DLOPEN)
7422 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007423 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424# else
7425 shl_t hinstLib;
7426# endif
7427 STRPROCSTR ProcAdd;
7428 INTPROCSTR ProcAddI;
7429 char_u *retval_str = NULL;
7430 int retval_int = 0;
7431 int success = FALSE;
7432
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007433 /*
7434 * Get a handle to the DLL module.
7435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007437 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007438 (void)dlerror();
7439
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 hinstLib = dlopen((char *)libname, RTLD_LAZY
7441# ifdef RTLD_LOCAL
7442 | RTLD_LOCAL
7443# endif
7444 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007445 if (hinstLib == NULL)
7446 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007447 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007448 dlerr = (char *)dlerror();
7449 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007450 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452# else
7453 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7454# endif
7455
Bram Moolenaar0f873732019-12-05 20:28:46 +01007456 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 if (hinstLib != NULL)
7458 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007459# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 /*
7461 * Catch a crash when calling the library function. For example when
7462 * using a number where a string pointer is expected.
7463 */
7464 mch_startjmp();
7465 if (SETJMP(lc_jump_env) != 0)
7466 {
7467 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007468# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007469 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007470# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 mch_didjmp();
7472 }
7473 else
7474# endif
7475 {
7476 retval_str = NULL;
7477 retval_int = 0;
7478
7479 if (argstring != NULL)
7480 {
7481# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007482 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007483 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484# else
7485 if (shl_findsym(&hinstLib, (const char *)funcname,
7486 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7487 ProcAdd = NULL;
7488# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007489 if ((success = (ProcAdd != NULL
7490# if defined(USE_DLOPEN)
7491 && dlerr == NULL
7492# endif
7493 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 {
7495 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007496 retval_int = ((STRPROCINT)(void *)ProcAdd)(argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 else
7498 retval_str = (ProcAdd)(argstring);
7499 }
7500 }
7501 else
7502 {
7503# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007504 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007505 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506# else
7507 if (shl_findsym(&hinstLib, (const char *)funcname,
7508 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7509 ProcAddI = NULL;
7510# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007511 if ((success = (ProcAddI != NULL
7512# if defined(USE_DLOPEN)
7513 && dlerr == NULL
7514# endif
7515 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 {
7517 if (string_result == NULL)
Bram Moolenaara4224862020-09-13 22:00:12 +02007518 retval_int = ((INTPROCINT)(void *)ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 else
7520 retval_str = (ProcAddI)(argint);
7521 }
7522 }
7523
Bram Moolenaar0f873732019-12-05 20:28:46 +01007524 // Save the string before we free the library.
7525 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 if (string_result == NULL)
7527 *number_result = retval_int;
7528 else if (retval_str != NULL
7529 && retval_str != (char_u *)1
7530 && retval_str != (char_u *)-1)
7531 *string_result = vim_strsave(retval_str);
7532 }
7533
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007534# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 mch_endjmp();
7536# ifdef SIGHASARG
7537 if (lc_signal != 0)
7538 {
7539 int i;
7540
Bram Moolenaar0f873732019-12-05 20:28:46 +01007541 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 for (i = 0; signal_info[i].sig != -1; i++)
7543 if (lc_signal == signal_info[i].sig)
7544 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007545 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 }
7547# endif
7548# endif
7549
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007551 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007552 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007553 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007554
Bram Moolenaar0f873732019-12-05 20:28:46 +01007555 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 (void)dlclose(hinstLib);
7557# else
7558 (void)shl_unload(hinstLib);
7559# endif
7560 }
7561
7562 if (!success)
7563 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007564 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 return FAIL;
7566 }
7567
7568 return OK;
7569}
7570#endif
7571
7572#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007573static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574static int xterm_button;
7575
7576/*
7577 * Setup a dummy window for X selections in a terminal.
7578 */
7579 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007580setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581{
7582 int z = 0;
7583 char *strp = "";
7584 Widget AppShell;
7585
7586 if (!x_connect_to_server())
7587 return;
7588
7589 open_app_context();
7590 if (app_context != NULL && xterm_Shell == (Widget)0)
7591 {
7592 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007593# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007595# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007596# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007597 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598
7599 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007600 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601# endif
7602
Bram Moolenaar0f873732019-12-05 20:28:46 +01007603 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 oldhandler = XSetErrorHandler(x_error_check);
7605
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007606# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007607 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7609 mch_startjmp();
7610 if (SETJMP(lc_jump_env) != 0)
7611 {
7612 mch_didjmp();
7613 xterm_dpy = NULL;
7614 }
7615 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 {
7618 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7619 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007620 if (xterm_dpy != NULL)
7621 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007622# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007624# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 }
7626
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007627# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007628 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007630# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007631 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 (void)XSetErrorHandler(oldhandler);
7633
7634 if (xterm_dpy == NULL)
7635 {
7636 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007637 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 return;
7639 }
7640
Bram Moolenaar0f873732019-12-05 20:28:46 +01007641 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 (void)XSetIOErrorHandler(x_IOerror_handler);
7643
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007644# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007646 {
7647 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007648 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007649 verbose_leave();
7650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651# endif
7652
Bram Moolenaar0f873732019-12-05 20:28:46 +01007653 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7655 applicationShellWidgetClass, xterm_dpy,
7656 NULL);
7657 if (AppShell == (Widget)0)
7658 return;
7659 xterm_Shell = XtVaCreatePopupShell("VIM",
7660 topLevelShellWidgetClass, AppShell,
7661 XtNmappedWhenManaged, 0,
7662 XtNwidth, 1,
7663 XtNheight, 1,
7664 NULL);
7665 if (xterm_Shell == (Widget)0)
7666 return;
7667
7668 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007669 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 if (x11_display == NULL)
7671 x11_display = xterm_dpy;
7672
7673 XtRealizeWidget(xterm_Shell);
7674 XSync(xterm_dpy, False);
7675 xterm_update();
7676 }
7677 if (xterm_Shell != (Widget)0)
7678 {
7679 clip_init(TRUE);
7680 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7681 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007682 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 if (test_x11_window(xterm_dpy) == FAIL)
7684 x11_window = 0;
7685 if (x11_window != 0)
7686 xterm_trace = 0;
7687 }
7688}
7689
7690 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007691start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
7693 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7694 return;
7695 xterm_trace = 1;
7696 xterm_button = button;
7697 do_xterm_trace();
7698}
7699
7700
7701 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007702stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703{
7704 if (xterm_trace < 0)
7705 return;
7706 xterm_trace = 0;
7707}
7708
7709/*
7710 * Query the xterm pointer and generate mouse termcodes if necessary
7711 * return TRUE if dragging is active, else FALSE
7712 */
7713 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007714do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
7716 Window root, child;
7717 int root_x, root_y;
7718 int win_x, win_y;
7719 int row, col;
7720 int_u mask_return;
7721 char_u buf[50];
7722 char_u *strp;
7723 long got_hints;
7724 static char_u *mouse_code;
7725 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7726 static int prev_row = 0, prev_col = 0;
7727 static XSizeHints xterm_hints;
7728
7729 if (xterm_trace <= 0)
7730 return FALSE;
7731
7732 if (xterm_trace == 1)
7733 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007734 // Get the hints just before tracking starts. The font size might
7735 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007736 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7737 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 || xterm_hints.width_inc <= 1
7739 || xterm_hints.height_inc <= 1)
7740 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007741 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 return FALSE;
7743 }
7744
Bram Moolenaar0f873732019-12-05 20:28:46 +01007745 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 mouse_code = find_termcode(mouse_name);
7747 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007748 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 xterm_trace = 2;
7750
Bram Moolenaar0f873732019-12-05 20:28:46 +01007751 // Find the offset of the chars, there might be a scrollbar on the
7752 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7754 &win_x, &win_y, &mask_return);
7755 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7756 - (xterm_hints.height_inc / 2);
7757 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7758 xterm_hints.y = 2;
7759 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7760 - (xterm_hints.width_inc / 2);
7761 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7762 xterm_hints.x = 2;
7763 return TRUE;
7764 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007765 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 {
7767 xterm_trace = 0;
7768 return FALSE;
7769 }
7770
7771 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7772 &win_x, &win_y, &mask_return);
7773
7774 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7775 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7776 if (row == prev_row && col == prev_col)
7777 return TRUE;
7778
7779 STRCPY(buf, mouse_code);
7780 strp = buf + STRLEN(buf);
7781 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7782 *strp++ = (char_u)(col + ' ' + 1);
7783 *strp++ = (char_u)(row + ' ' + 1);
7784 *strp = 0;
7785 add_to_input_buf(buf, STRLEN(buf));
7786
7787 prev_row = row;
7788 prev_col = col;
7789 return TRUE;
7790}
7791
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007792# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793/*
7794 * Destroy the display, window and app_context. Required for GTK.
7795 */
7796 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007797clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798{
7799 if (xterm_Shell != (Widget)0)
7800 {
7801 XtDestroyWidget(xterm_Shell);
7802 xterm_Shell = (Widget)0;
7803 }
7804 if (xterm_dpy != NULL)
7805 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007806# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007807 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007809# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 if (x11_display == xterm_dpy)
7811 x11_display = NULL;
7812 xterm_dpy = NULL;
7813 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007814# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 if (app_context != (XtAppContext)NULL)
7816 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007817 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 XtDestroyApplicationContext(app_context);
7819 app_context = (XtAppContext)NULL;
7820 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007821# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822}
7823# endif
7824
7825/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007826 * Catch up with GUI or X events.
7827 */
7828 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007829clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007830{
7831# ifdef FEAT_GUI
7832 if (gui.in_use)
7833 gui_mch_update();
7834 else
7835# endif
7836 if (xterm_Shell != (Widget)0)
7837 xterm_update();
7838}
7839
7840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 * Catch up with any queued X events. This may put keyboard input into the
7842 * input buffer, call resize call-backs, trigger timers etc. If there is
7843 * nothing in the X event queue (& no timers pending), then we return
7844 * immediately.
7845 */
7846 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007847xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848{
7849 XEvent event;
7850
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007851 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007853 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007855 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007856 break;
7857
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007858 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007859 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007860 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007861 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007862#ifdef FEAT_CLIENTSERVER
7863 {
7864 XPropertyEvent *e = (XPropertyEvent *)&event;
7865
7866 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007868 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007871 XtDispatchEvent(&event);
7872 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007873 else
7874 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007875 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007876 XtAppProcessEvent(app_context, mask);
7877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 }
7879}
7880
7881 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007882clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883{
7884 if (xterm_Shell != (Widget)0)
7885 return clip_x11_own_selection(xterm_Shell, cbd);
7886 return FAIL;
7887}
7888
7889 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007890clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891{
7892 if (xterm_Shell != (Widget)0)
7893 clip_x11_lose_selection(xterm_Shell, cbd);
7894}
7895
7896 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007897clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
7899 if (xterm_Shell != (Widget)0)
7900 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7901}
7902
7903 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007904clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 clip_x11_set_selection(cbd);
7907}
7908#endif
7909
7910
7911#if defined(USE_XSMP) || defined(PROTO)
7912/*
7913 * Code for X Session Management Protocol.
7914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915
7916# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917/*
7918 * This is our chance to ask the user if they want to save,
7919 * or abort the logout
7920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007922xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923{
Bram Moolenaare1004402020-10-24 20:49:43 +02007924 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 int cancel_shutdown = False;
7926
Bram Moolenaare1004402020-10-24 20:49:43 +02007927 save_cmod_flags = cmdmod.cmod_flags;
7928 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007929 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007930 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 cancel_shutdown = True;
Bram Moolenaare1004402020-10-24 20:49:43 +02007932 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007933 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 out_flush();
7935
Bram Moolenaar0f873732019-12-05 20:28:46 +01007936 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 SmcInteractDone(smc_conn, cancel_shutdown);
7938
Bram Moolenaar0f873732019-12-05 20:28:46 +01007939 // Finish off
7940 // Only end save-yourself here if we're not cancelling shutdown;
7941 // we'll get a cancelled callback later in which we'll end it.
7942 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 if (!cancel_shutdown)
7944 {
7945 xsmp.save_yourself = False;
7946 SmcSaveYourselfDone(smc_conn, True);
7947 }
7948}
7949# endif
7950
7951/*
7952 * Callback that starts save-yourself.
7953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007955xsmp_handle_save_yourself(
7956 SmcConn smc_conn,
7957 SmPointer client_data UNUSED,
7958 int save_type UNUSED,
7959 Bool shutdown,
7960 int interact_style UNUSED,
7961 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007963 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 if (xsmp.save_yourself)
7965 SmcSaveYourselfDone(smc_conn, True);
7966 xsmp.save_yourself = True;
7967 xsmp.shutdown = shutdown;
7968
Bram Moolenaar0f873732019-12-05 20:28:46 +01007969 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007971 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972
7973 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007974 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975
7976# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007977 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007979 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 SmcInteractRequest(smc_conn, SmDialogError,
7981 xsmp_handle_interaction, client_data);
7982 else
7983# endif
7984 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007985 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 SmcSaveYourselfDone(smc_conn, True);
7987 xsmp.save_yourself = False;
7988 }
7989}
7990
7991
7992/*
7993 * Callback to warn us of imminent death.
7994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007996xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997{
7998 xsmp_close();
7999
Bram Moolenaar0f873732019-12-05 20:28:46 +01008000 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 getout_preserve_modified(0);
8002}
8003
8004
8005/*
8006 * Callback to tell us that save-yourself has completed.
8007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008009xsmp_save_complete(
8010 SmcConn smc_conn UNUSED,
8011 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012{
8013 xsmp.save_yourself = False;
8014}
8015
8016
8017/*
8018 * Callback to tell us that an instigated shutdown was cancelled
8019 * (maybe even by us)
8020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008022xsmp_shutdown_cancelled(
8023 SmcConn smc_conn,
8024 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025{
8026 if (xsmp.save_yourself)
8027 SmcSaveYourselfDone(smc_conn, True);
8028 xsmp.save_yourself = False;
8029 xsmp.shutdown = False;
8030}
8031
8032
8033/*
8034 * Callback to tell us that a new ICE connection has been established.
8035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008037xsmp_ice_connection(
8038 IceConn iceConn,
8039 IcePointer clientData UNUSED,
8040 Bool opening,
8041 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008043 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 if (opening)
8045 {
8046 xsmp_icefd = IceConnectionNumber(iceConn);
8047 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
8048 }
8049}
8050
8051
Bram Moolenaar0f873732019-12-05 20:28:46 +01008052// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008054xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055{
8056 Bool rep;
8057
8058 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
8059 == IceProcessMessagesIOError)
8060 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008061 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008063 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 xsmp_close();
8065 return FAIL;
8066 }
8067 else
8068 return OK;
8069}
8070
8071static int dummy;
8072
Bram Moolenaar0f873732019-12-05 20:28:46 +01008073// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 void
8075xsmp_init(void)
8076{
8077 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 SmcCallbacks smcallbacks;
8079#if 0
8080 SmPropValue smname;
8081 SmProp smnameprop;
8082 SmProp *smprops[1];
8083#endif
8084
8085 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008086 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087
8088 xsmp.save_yourself = xsmp.shutdown = False;
8089
Bram Moolenaar0f873732019-12-05 20:28:46 +01008090 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8092 smcallbacks.save_yourself.client_data = NULL;
8093 smcallbacks.die.callback = xsmp_die;
8094 smcallbacks.die.client_data = NULL;
8095 smcallbacks.save_complete.callback = xsmp_save_complete;
8096 smcallbacks.save_complete.client_data = NULL;
8097 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8098 smcallbacks.shutdown_cancelled.client_data = NULL;
8099
Bram Moolenaar0f873732019-12-05 20:28:46 +01008100 // Set up a watch on ICE connection creations. The "dummy" argument is
8101 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8103 {
8104 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008105 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 return;
8107 }
8108
Bram Moolenaar0f873732019-12-05 20:28:46 +01008109 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 xsmp.smcconn = SmcOpenConnection(
8111 NULL,
8112 NULL,
8113 SmProtoMajor,
8114 SmProtoMinor,
8115 SmcSaveYourselfProcMask | SmcDieProcMask
8116 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8117 &smcallbacks,
8118 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008119 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008120 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 errorstring);
8122 if (xsmp.smcconn == NULL)
8123 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008125 {
Bram Moolenaare1be1182020-10-24 13:30:51 +02008126 char errorreport[132];
8127
8128 // If the message is too long it might not be NUL terminated. Add
8129 // a NUL at the end to make sure we don't go over the end.
8130 errorstring[sizeof(errorstring) - 1] = NUL;
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008131 vim_snprintf(errorreport, sizeof(errorreport),
8132 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008133 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 return;
8136 }
8137 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8138
8139#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008140 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 smname.value = "vim";
8142 smname.length = 3;
8143 smnameprop.name = "SmProgram";
8144 smnameprop.type = "SmARRAY8";
8145 smnameprop.num_vals = 1;
8146 smnameprop.vals = &smname;
8147
8148 smprops[0] = &smnameprop;
8149 SmcSetProperties(xsmp.smcconn, 1, smprops);
8150#endif
8151}
8152
8153
Bram Moolenaar0f873732019-12-05 20:28:46 +01008154// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008156xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157{
8158 if (xsmp_icefd != -1)
8159 {
8160 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008161 if (xsmp.clientid != NULL)
8162 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008163 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 xsmp_icefd = -1;
8165 }
8166}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008167#endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168
8169
8170#ifdef EBCDIC
Bram Moolenaar0f873732019-12-05 20:28:46 +01008171// Translate character to its CTRL- value
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172char CtrlTable[] =
8173{
8174/* 00 - 5E */
8175 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8177 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8178 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8179 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8180 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8181/* ^ */ 0x1E,
8182/* - */ 0x1F,
8183/* 61 - 6C */
8184 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8185/* _ */ 0x1F,
8186/* 6E - 80 */
8187 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8188/* a */ 0x01,
8189/* b */ 0x02,
8190/* c */ 0x03,
8191/* d */ 0x37,
8192/* e */ 0x2D,
8193/* f */ 0x2E,
8194/* g */ 0x2F,
8195/* h */ 0x16,
8196/* i */ 0x05,
8197/* 8A - 90 */
8198 0, 0, 0, 0, 0, 0, 0,
8199/* j */ 0x15,
8200/* k */ 0x0B,
8201/* l */ 0x0C,
8202/* m */ 0x0D,
8203/* n */ 0x0E,
8204/* o */ 0x0F,
8205/* p */ 0x10,
8206/* q */ 0x11,
8207/* r */ 0x12,
8208/* 9A - A1 */
8209 0, 0, 0, 0, 0, 0, 0, 0,
8210/* s */ 0x13,
8211/* t */ 0x3C,
8212/* u */ 0x3D,
8213/* v */ 0x32,
8214/* w */ 0x26,
8215/* x */ 0x18,
8216/* y */ 0x19,
8217/* z */ 0x3F,
8218/* AA - AC */
8219 0, 0, 0,
8220/* [ */ 0x27,
8221/* AE - BC */
8222 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8223/* ] */ 0x1D,
8224/* BE - C0 */ 0, 0, 0,
8225/* A */ 0x01,
8226/* B */ 0x02,
8227/* C */ 0x03,
8228/* D */ 0x37,
8229/* E */ 0x2D,
8230/* F */ 0x2E,
8231/* G */ 0x2F,
8232/* H */ 0x16,
8233/* I */ 0x05,
8234/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8235/* J */ 0x15,
8236/* K */ 0x0B,
8237/* L */ 0x0C,
8238/* M */ 0x0D,
8239/* N */ 0x0E,
8240/* O */ 0x0F,
8241/* P */ 0x10,
8242/* Q */ 0x11,
8243/* R */ 0x12,
8244/* DA - DF */ 0, 0, 0, 0, 0, 0,
8245/* \ */ 0x1C,
8246/* E1 */ 0,
8247/* S */ 0x13,
8248/* T */ 0x3C,
8249/* U */ 0x3D,
8250/* V */ 0x32,
8251/* W */ 0x26,
8252/* X */ 0x18,
8253/* Y */ 0x19,
8254/* Z */ 0x3F,
8255/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8256 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8257};
8258
8259char MetaCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008260{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8262 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8263 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8264 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8265};
8266
8267
Bram Moolenaar0f873732019-12-05 20:28:46 +01008268// TODO: Use characters NOT numbers!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269char CtrlCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008270{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8272 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8273 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8274 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8275};
8276
8277
8278#endif