blob: 12fb33b7ae97609d30de27308186dc7901db2571 [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!!!
15 * Also for BeOS and Atari MiNT.
16 *
17 * A lot of this file was originally written by Juergen Weigert and later
18 * changed beyond recognition.
19 */
20
21/*
22 * Some systems have a prototype for select() that has (int *) instead of
23 * (fd_set *), which is wrong. This define removes that prototype. We define
24 * our own prototype below.
25 * Don't use it for the Mac, it causes a warning for precompiled headers.
26 * TODO: use a configure check for precompiled headers?
27 */
Bram Moolenaar311d9822007-02-27 15:48:28 +000028#if !defined(__APPLE__) && !defined(__TANDEM)
Bram Moolenaar071d4272004-06-13 20:20:40 +000029# define select select_declared_wrong
30#endif
31
32#include "vim.h"
33
Bram Moolenaar325b7a22004-07-05 15:58:32 +000034#ifdef FEAT_MZSCHEME
35# include "if_mzsch.h"
36#endif
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#include "os_unixx.h" /* unix includes for os_unix.c only */
39
40#ifdef USE_XSMP
41# include <X11/SM/SMlib.h>
42#endif
43
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000044#ifdef HAVE_SELINUX
45# include <selinux/selinux.h>
46static int selinux_enabled = -1;
47#endif
48
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020049#ifdef HAVE_SMACK
50# include <attr/xattr.h>
51# include <linux/xattr.h>
52# ifndef SMACK_LABEL_LEN
53# define SMACK_LABEL_LEN 1024
54# endif
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057/*
58 * Use this prototype for select, some include files have a wrong prototype
59 */
Bram Moolenaar311d9822007-02-27 15:48:28 +000060#ifndef __TANDEM
61# undef select
62# ifdef __BEOS__
63# define select beos_select
64# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
66
Bram Moolenaara2442432007-04-26 14:26:37 +000067#ifdef __CYGWIN__
68# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000069# include <cygwin/version.h>
70# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
71 * for cygwin_conv_path() */
Bram Moolenaar693e40c2013-02-26 14:56:42 +010072# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
73# define WIN32_LEAN_AND_MEAN
74# include <windows.h>
75# include "winclip.pro"
76# endif
Bram Moolenaara2442432007-04-26 14:26:37 +000077# endif
78#endif
79
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#if defined(HAVE_SELECT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010081extern int select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#endif
83
84#ifdef FEAT_MOUSE_GPM
85# include <gpm.h>
86/* <linux/keyboard.h> contains defines conflicting with "keymap.h",
87 * I just copied relevant defines here. A cleaner solution would be to put gpm
88 * code into separate file and include there linux/keyboard.h
89 */
90/* #include <linux/keyboard.h> */
91# define KG_SHIFT 0
92# define KG_CTRL 2
93# define KG_ALT 3
94# define KG_ALTGR 1
95# define KG_SHIFTL 4
96# define KG_SHIFTR 5
97# define KG_CTRLL 6
98# define KG_CTRLR 7
99# define KG_CAPSSHIFT 8
100
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100101static void gpm_close(void);
102static int gpm_open(void);
103static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000106#ifdef FEAT_SYSMOUSE
107# include <sys/consio.h>
108# include <sys/fbio.h>
109
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100110static int sysmouse_open(void);
111static void sysmouse_close(void);
Bram Moolenaard99df422016-01-29 23:20:40 +0100112static RETSIGTYPE sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000113#endif
114
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115/*
116 * end of autoconf section. To be extended...
117 */
118
119/* Are the following #ifdefs still required? And why? Is that for X11? */
120
121#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
122# ifdef SIGWINCH
123# undef SIGWINCH
124# endif
125# ifdef TIOCGWINSZ
126# undef TIOCGWINSZ
127# endif
128#endif
129
130#if defined(SIGWINDOW) && !defined(SIGWINCH) /* hpux 9.01 has it */
131# define SIGWINCH SIGWINDOW
132#endif
133
134#ifdef FEAT_X11
135# include <X11/Xlib.h>
136# include <X11/Xutil.h>
137# include <X11/Xatom.h>
138# ifdef FEAT_XCLIPBOARD
139# include <X11/Intrinsic.h>
140# include <X11/Shell.h>
141# include <X11/StringDefs.h>
142static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void clip_update(void);
144static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# endif
146
147# if defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE)
148Window x11_window = 0;
149# endif
150Display *x11_display = NULL;
151
152# ifdef FEAT_TITLE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static int get_x11_windis(void);
154static void set_x11_title(char_u *);
155static void set_x11_icon(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156# endif
157#endif
158
159#ifdef FEAT_TITLE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static int get_x11_title(int);
161static int get_x11_icon(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163static char_u *oldtitle = NULL;
164static int did_set_title = FALSE;
165static char_u *oldicon = NULL;
166static int did_set_icon = FALSE;
167#endif
168
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100169static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170
Bram Moolenaar205b8862011-09-07 15:04:31 +0200171#ifdef HAVE_UNION_WAIT
172typedef union wait waitstatus;
173#else
174typedef int waitstatus;
175#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static pid_t wait4pid(pid_t, waitstatus *);
Bram Moolenaar205b8862011-09-07 15:04:31 +0200177
Bram Moolenaarcda77642016-06-04 13:32:35 +0200178static int WaitForChar(long msec, int *interrupted);
179static int WaitForCharOrMouse(long msec, int *interrupted);
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100180#if defined(__BEOS__) || defined(VMS)
Bram Moolenaarcda77642016-06-04 13:32:35 +0200181int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200183static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#endif
185
186#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100187static int do_xterm_trace(void);
Bram Moolenaarcf851ce2005-06-16 21:52:47 +0000188# define XT_TRACE_DELAY 50 /* delay for xterm tracing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#endif
190
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100191static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100194static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#endif
196#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100197static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198#endif
199#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100200static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#endif
202#if defined(SIGALRM) && defined(FEAT_X11) \
203 && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
204# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100205static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000206/* volatile because it is used in signal handler sig_alarm(). */
207static volatile int sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100209static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100211static void catch_int_signal(void);
212static void set_signals(void);
213static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200214#ifdef HAVE_SIGPROCMASK
215# define SIGSET_DECL(set) sigset_t set;
216# define BLOCK_SIGNALS(set) block_signals(set)
217# define UNBLOCK_SIGNALS(set) unblock_signals(set)
218#else
219# define SIGSET_DECL(set)
220# define BLOCK_SIGNALS(set) do { /**/ } while (0)
221# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
222#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100223static int have_wildcard(int, char_u **);
224static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100226static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
228#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000229# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#endif
231
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000232/* volatile because it is used in signal handler sig_winch(). */
233static volatile int do_resize = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234static char_u *extra_shell_arg = NULL;
235static int show_shell_mess = TRUE;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000236/* volatile because it is used in signal handler deathtrap(). */
237static volatile int deadly_signal = 0; /* The signal we caught */
238/* volatile because it is used in signal handler deathtrap(). */
239static volatile int in_mch_delay = FALSE; /* sleeping in mch_delay() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100241#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
242static int dont_check_job_ended = 0;
243#endif
244
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245static int curr_tmode = TMODE_COOK; /* contains current terminal mode */
246
247#ifdef USE_XSMP
248typedef struct
249{
250 SmcConn smcconn; /* The SM connection ID */
251 IceConn iceconn; /* The ICE connection ID */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200252 char *clientid; /* The client ID for the current smc session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 Bool save_yourself; /* If we're in the middle of a save_yourself */
254 Bool shutdown; /* If we're in shutdown mode */
255} xsmp_config_T;
256
257static xsmp_config_T xsmp;
258#endif
259
260#ifdef SYS_SIGLIST_DECLARED
261/*
262 * I have seen
263 * extern char *_sys_siglist[NSIG];
264 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
265 * that describe the signals. That is nearly what we want here. But
266 * autoconf does only check for sys_siglist (without the underscore), I
267 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100268 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 */
270#endif
271
272static struct signalinfo
273{
274 int sig; /* Signal number, eg. SIGSEGV etc */
275 char *name; /* Signal name (not char_u!). */
276 char deadly; /* Catch as a deadly signal? */
277} signal_info[] =
278{
279#ifdef SIGHUP
280 {SIGHUP, "HUP", TRUE},
281#endif
282#ifdef SIGQUIT
283 {SIGQUIT, "QUIT", TRUE},
284#endif
285#ifdef SIGILL
286 {SIGILL, "ILL", TRUE},
287#endif
288#ifdef SIGTRAP
289 {SIGTRAP, "TRAP", TRUE},
290#endif
291#ifdef SIGABRT
292 {SIGABRT, "ABRT", TRUE},
293#endif
294#ifdef SIGEMT
295 {SIGEMT, "EMT", TRUE},
296#endif
297#ifdef SIGFPE
298 {SIGFPE, "FPE", TRUE},
299#endif
300#ifdef SIGBUS
301 {SIGBUS, "BUS", TRUE},
302#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100303#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
304 /* MzScheme uses SEGV in its garbage collector */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 {SIGSEGV, "SEGV", TRUE},
306#endif
307#ifdef SIGSYS
308 {SIGSYS, "SYS", TRUE},
309#endif
310#ifdef SIGALRM
311 {SIGALRM, "ALRM", FALSE}, /* Perl's alarm() can trigger it */
312#endif
313#ifdef SIGTERM
314 {SIGTERM, "TERM", TRUE},
315#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100316#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 {SIGVTALRM, "VTALRM", TRUE},
318#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000319#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
320 /* MzScheme uses SIGPROF for its own needs; On Linux with profiling
321 * this makes Vim exit. WE_ARE_PROFILING is defined in Makefile. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 {SIGPROF, "PROF", TRUE},
323#endif
324#ifdef SIGXCPU
325 {SIGXCPU, "XCPU", TRUE},
326#endif
327#ifdef SIGXFSZ
328 {SIGXFSZ, "XFSZ", TRUE},
329#endif
330#ifdef SIGUSR1
331 {SIGUSR1, "USR1", TRUE},
332#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000333#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
334 /* Used for sysmouse handling */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {SIGUSR2, "USR2", TRUE},
336#endif
337#ifdef SIGINT
338 {SIGINT, "INT", FALSE},
339#endif
340#ifdef SIGWINCH
341 {SIGWINCH, "WINCH", FALSE},
342#endif
343#ifdef SIGTSTP
344 {SIGTSTP, "TSTP", FALSE},
345#endif
346#ifdef SIGPIPE
347 {SIGPIPE, "PIPE", FALSE},
348#endif
349 {-1, "Unknown!", FALSE}
350};
351
Bram Moolenaar25724922009-07-14 15:38:41 +0000352 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100353mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000354{
355 if (p_verbose >= 5)
356 {
357 verbose_enter();
358 smsg((char_u *)"chdir(%s)", path);
359 verbose_leave();
360 }
361# ifdef VMS
362 return chdir(vms_fixfilename(path));
363# else
364 return chdir(path);
365# endif
366}
367
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000368/*
369 * Write s[len] to the screen.
370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100372mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000374 ignored = (int)write(1, (char *)s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 if (p_wd) /* Unix is too fast, slow down a bit more */
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100376 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377}
378
379/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000380 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 * Get a characters from the keyboard.
382 * Return the number of characters that are available.
383 * If wtime == 0 do not wait for characters.
384 * If wtime == n wait a short time for characters.
385 * If wtime == -1 wait forever for characters.
386 */
387 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100388mch_inchar(
389 char_u *buf,
390 int maxlen,
391 long wtime, /* don't use "time", MIPS cannot handle it */
392 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393{
394 int len;
Bram Moolenaarcda77642016-06-04 13:32:35 +0200395 int interrupted = FALSE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200396 int did_start_blocking = FALSE;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200397 long wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200398 long elapsed_time = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100399#ifdef ELAPSED_FUNC
400 ELAPSED_TYPE start_tv;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200401
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100402 ELAPSED_INIT(start_tv);
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200405 /* repeat until we got a character or waited long enough */
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200406 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200408 /* Check if window changed size while we were busy, perhaps the ":set
409 * columns=99" command was used. */
410 while (do_resize)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 handle_resize();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200412
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200413#ifdef MESSAGE_QUEUE
414 parse_queued_messages();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200415#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200416 if (wtime < 0 && did_start_blocking)
417 /* blocking and already waited for p_ut */
418 wait_time = -1;
419 else
420 {
421 if (wtime >= 0)
422 wait_time = wtime;
423 else
424 /* going to block after p_ut */
425 wait_time = p_ut;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100426#ifdef ELAPSED_FUNC
427 elapsed_time = ELAPSED_FUNC(start_tv);
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200428#endif
429 wait_time -= elapsed_time;
430 if (wait_time < 0)
431 {
432 if (wtime >= 0)
433 /* no character available within "wtime" */
434 return 0;
435
436 if (wtime < 0)
437 {
438 /* no character available within 'updatetime' */
439 did_start_blocking = TRUE;
440#ifdef FEAT_AUTOCMD
441 if (trigger_cursorhold() && maxlen >= 3
442 && !typebuf_changed(tb_change_cnt))
443 {
444 buf[0] = K_SPECIAL;
445 buf[1] = KS_EXTRA;
446 buf[2] = (int)KE_CURSORHOLD;
447 return 3;
448 }
449#endif
450 /*
451 * If there is no character available within 'updatetime'
452 * seconds flush all the swap files to disk.
453 * Also done when interrupted by SIGWINCH.
454 */
455 before_blocking();
456 continue;
457 }
458 }
459 }
460
461#ifdef FEAT_JOB_CHANNEL
462 /* Checking if a job ended requires polling. Do this every 100 msec. */
463 if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
464 wait_time = 100L;
465#endif
466
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200468 * We want to be interrupted by the winch signal
469 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 */
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200471 if (WaitForChar(wait_time, &interrupted))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200472 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200473 /* If input was put directly in typeahead buffer bail out here. */
474 if (typebuf_changed(tb_change_cnt))
475 return 0;
476
477 /*
478 * For some terminals we only get one character at a time.
479 * We want the get all available characters, so we could keep on
480 * trying until none is available
481 * For some other terminals this is quite slow, that's why we don't
482 * do it.
483 */
484 len = read_from_input_buf(buf, (long)maxlen);
485 if (len > 0)
486 return len;
487 continue;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200490 /* no character available */
491#if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H))
492 /* estimate the elapsed time */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100493 elapsed_time += wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200496 if (do_resize /* interrupted by SIGWINCH signal */
497#ifdef FEAT_CLIENTSERVER
498 || server_waiting()
499#endif
500#ifdef MESSAGE_QUEUE
501 || interrupted
502#endif
503 || wait_time > 0
504 || !did_start_blocking)
505 continue;
506
507 /* no character available or interrupted */
508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200510 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511}
512
513 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100514handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515{
516 do_resize = FALSE;
517 shell_resized();
518}
519
520/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200521 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 */
523 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100524mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
Bram Moolenaarcda77642016-06-04 13:32:35 +0200526 return WaitForChar(0L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527}
528
529#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
530# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000531# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532# endif
533# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
534# include <sys/sysctl.h>
535# endif
536# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
537# include <sys/sysinfo.h>
538# endif
539
540/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000541 * Return total amount of memory available in Kbyte.
542 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100545mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000548 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200550# ifdef HAVE_SYSCTL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 int mib[2], physmem;
552 size_t len;
553
554 /* BSD way of getting the amount of RAM available. */
555 mib[0] = CTL_HW;
556 mib[1] = HW_USERMEM;
557 len = sizeof(physmem);
558 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
559 mem = (long_u)physmem;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200560# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200562# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 if (mem == 0)
564 {
565 struct sysinfo sinfo;
566
567 /* Linux way of getting amount of RAM available */
568 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000569 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200570# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000571 /* avoid overflow as much as possible */
572 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
573 {
574 sinfo.mem_unit = sinfo.mem_unit >> 1;
575 --shiftright;
576 }
577 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200578# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200580# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200583# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200585# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (mem == 0)
587 {
588 long pagesize, pagecount;
589
590 /* Solaris way of getting amount of RAM available */
591 pagesize = sysconf(_SC_PAGESIZE);
592 pagecount = sysconf(_SC_PHYS_PAGES);
593 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000594 {
595 /* avoid overflow as much as possible */
596 while (shiftright > 0 && (pagesize & 1) == 0)
597 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000598 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000599 --shiftright;
600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200604# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
606 /* Return the minimum of the physical memory and the user limit, because
607 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200608# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 {
610 struct rlimit rlp;
611
612 if (getrlimit(RLIMIT_DATA, &rlp) == 0
613 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200614# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200616# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000617 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000619 {
620 mem = (long_u)rlp.rlim_cur;
621 shiftright = 10;
622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200624# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
626 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000627 return mem >> shiftright;
628 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629}
630#endif
631
632 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100633mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000636#ifdef FEAT_MZSCHEME
637 long total = msec; /* remember original value */
638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640 if (ignoreinput)
641 {
642 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000643 * here. But we don't want QUIT to kill us (CTRL-\ used in a
644 * shell may produce SIGQUIT). */
645 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 old_tmode = curr_tmode;
647 if (curr_tmode == TMODE_RAW)
648 settmode(TMODE_SLEEP);
649
650 /*
651 * Everybody sleeps in a different way...
652 * Prefer nanosleep(), some versions of usleep() can only sleep up to
653 * one second.
654 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000655#ifdef FEAT_MZSCHEME
656 do
657 {
658 /* if total is large enough, wait by portions in p_mzq */
659 if (total > p_mzq)
660 msec = p_mzq;
661 else
662 msec = total;
663 total -= msec;
664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665#ifdef HAVE_NANOSLEEP
666 {
667 struct timespec ts;
668
669 ts.tv_sec = msec / 1000;
670 ts.tv_nsec = (msec % 1000) * 1000000;
671 (void)nanosleep(&ts, NULL);
672 }
673#else
674# ifdef HAVE_USLEEP
675 while (msec >= 1000)
676 {
677 usleep((unsigned int)(999 * 1000));
678 msec -= 999;
679 }
680 usleep((unsigned int)(msec * 1000));
681# else
682# ifndef HAVE_SELECT
683 poll(NULL, 0, (int)msec);
684# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {
686 struct timeval tv;
687
688 tv.tv_sec = msec / 1000;
689 tv.tv_usec = (msec % 1000) * 1000;
690 /*
691 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
692 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
693 */
694 select(0, NULL, NULL, NULL, &tv);
695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696# endif /* HAVE_SELECT */
697# endif /* HAVE_NANOSLEEP */
698#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000699#ifdef FEAT_MZSCHEME
700 }
701 while (total > 0);
702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
704 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000705 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707 else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200708 WaitForChar(msec, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709}
710
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000711#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
713# define HAVE_CHECK_STACK_GROWTH
714/*
715 * Support for checking for an almost-out-of-stack-space situation.
716 */
717
718/*
719 * Return a pointer to an item on the stack. Used to find out if the stack
720 * grows up or down.
721 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100722static void check_stack_growth(char *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723static int stack_grows_downwards;
724
725/*
726 * Find out if the stack grows upwards or downwards.
727 * "p" points to a variable on the stack of the caller.
728 */
729 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100730check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731{
732 int i;
733
734 stack_grows_downwards = (p > (char *)&i);
735}
736#endif
737
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000738#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739static char *stack_limit = NULL;
740
741#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
742# include <pthread.h>
743# include <pthread_np.h>
744#endif
745
746/*
747 * Find out until how var the stack can grow without getting into trouble.
748 * Called when starting up and when switching to the signal stack in
749 * deathtrap().
750 */
751 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100752get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753{
754 struct rlimit rlp;
755 int i;
756 long lim;
757
758 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
759 * limit doesn't fit in a long (rlim_cur might be "long long"). */
760 if (getrlimit(RLIMIT_STACK, &rlp) == 0
761 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
762# ifdef RLIM_INFINITY
763 && rlp.rlim_cur != RLIM_INFINITY
764# endif
765 )
766 {
767 lim = (long)rlp.rlim_cur;
768#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
769 {
770 pthread_attr_t attr;
771 size_t size;
772
773 /* On FreeBSD the initial thread always has a fixed stack size, no
774 * matter what the limits are set to. Normally it's 1 Mbyte. */
775 pthread_attr_init(&attr);
776 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
777 {
778 pthread_attr_getstacksize(&attr, &size);
779 if (lim > (long)size)
780 lim = (long)size;
781 }
782 pthread_attr_destroy(&attr);
783 }
784#endif
785 if (stack_grows_downwards)
786 {
787 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
788 if (stack_limit >= (char *)&i)
789 /* overflow, set to 1/16 of current stack position */
790 stack_limit = (char *)((long)&i / 16L);
791 }
792 else
793 {
794 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
795 if (stack_limit <= (char *)&i)
796 stack_limit = NULL; /* overflow */
797 }
798 }
799}
800
801/*
802 * Return FAIL when running out of stack space.
803 * "p" must point to any variable local to the caller that's on the stack.
804 */
805 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100806mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807{
808 if (stack_limit != NULL)
809 {
810 if (stack_grows_downwards)
811 {
812 if (p < stack_limit)
813 return FAIL;
814 }
815 else if (p > stack_limit)
816 return FAIL;
817 }
818 return OK;
819}
820#endif
821
822#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
823/*
824 * Support for using the signal stack.
825 * This helps when we run out of stack space, which causes a SIGSEGV. The
826 * signal handler then must run on another stack, since the normal stack is
827 * completely full.
828 */
829
Bram Moolenaar39766a72013-11-03 00:41:00 +0100830#if defined(HAVE_AVAILABILITYMACROS_H)
Bram Moolenaar4cc95d12013-11-02 21:49:32 +0100831# include <AvailabilityMacros.h>
832#endif
833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#ifndef SIGSTKSZ
835# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
836#endif
837
838# ifdef HAVE_SIGALTSTACK
839static stack_t sigstk; /* for sigaltstack() */
840# else
841static struct sigstack sigstk; /* for sigstack() */
842# endif
843
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100844static void init_signal_stack(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845static char *signal_stack;
846
847 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100848init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849{
850 if (signal_stack != NULL)
851 {
852# ifdef HAVE_SIGALTSTACK
Bram Moolenaar1a3d0862007-08-30 09:47:38 +0000853# if defined(__APPLE__) && (!defined(MAC_OS_X_VERSION_MAX_ALLOWED) \
854 || MAC_OS_X_VERSION_MAX_ALLOWED <= 1040)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 /* missing prototype. Adding it to osdef?.h.in doesn't work, because
856 * "struct sigaltstack" needs to be declared. */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100857 extern int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858# endif
859
860# ifdef HAVE_SS_BASE
861 sigstk.ss_base = signal_stack;
862# else
863 sigstk.ss_sp = signal_stack;
864# endif
865 sigstk.ss_size = SIGSTKSZ;
866 sigstk.ss_flags = 0;
867 (void)sigaltstack(&sigstk, NULL);
868# else
869 sigstk.ss_sp = signal_stack;
870 if (stack_grows_downwards)
871 sigstk.ss_sp += SIGSTKSZ - 1;
872 sigstk.ss_onstack = 0;
873 (void)sigstack(&sigstk, NULL);
874# endif
875 }
876}
877#endif
878
879/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000880 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 * will barf when the second argument to signal() is ``wrong''.
882 * Let me try it with a few tricky defines from my own osdef.h (jw).
883 */
884#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 static RETSIGTYPE
886sig_winch SIGDEFARG(sigarg)
887{
888 /* this is not required on all systems, but it doesn't hurt anybody */
889 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
890 do_resize = TRUE;
891 SIGRETURN;
892}
893#endif
894
895#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 static RETSIGTYPE
897catch_sigint SIGDEFARG(sigarg)
898{
899 /* this is not required on all systems, but it doesn't hurt anybody */
900 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
901 got_int = TRUE;
902 SIGRETURN;
903}
904#endif
905
906#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 static RETSIGTYPE
908catch_sigpwr SIGDEFARG(sigarg)
909{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000910 /* this is not required on all systems, but it doesn't hurt anybody */
911 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 /*
913 * I'm not sure we get the SIGPWR signal when the system is really going
914 * down or when the batteries are almost empty. Just preserve the swap
915 * files and don't exit, that can't do any harm.
916 */
917 ml_sync_all(FALSE, FALSE);
918 SIGRETURN;
919}
920#endif
921
922#ifdef SET_SIG_ALARM
923/*
924 * signal function for alarm().
925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 static RETSIGTYPE
927sig_alarm SIGDEFARG(sigarg)
928{
929 /* doesn't do anything, just to break a system call */
930 sig_alarm_called = TRUE;
931 SIGRETURN;
932}
933#endif
934
Bram Moolenaar44ecf652005-03-07 23:09:59 +0000935#if (defined(HAVE_SETJMP_H) \
936 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
937 || defined(FEAT_LIBCALL))) \
938 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939/*
940 * A simplistic version of setjmp() that only allows one level of using.
941 * Don't call twice before calling mch_endjmp()!.
942 * Usage:
943 * mch_startjmp();
944 * if (SETJMP(lc_jump_env) != 0)
945 * {
946 * mch_didjmp();
947 * EMSG("crash!");
948 * }
949 * else
950 * {
951 * do_the_work;
952 * mch_endjmp();
953 * }
954 * Note: Can't move SETJMP() here, because a function calling setjmp() must
955 * not return before the saved environment is used.
956 * Returns OK for normal return, FAIL when the protected code caused a
957 * problem and LONGJMP() was used.
958 */
959 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100960mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
962#ifdef SIGHASARG
963 lc_signal = 0;
964#endif
965 lc_active = TRUE;
966}
967
968 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100969mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
971 lc_active = FALSE;
972}
973
974 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100975mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
977# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
978 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
979 * otherwise catching the signal only works once. */
980 init_signal_stack();
981# endif
982}
983#endif
984
985/*
986 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200987 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200989 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
990 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 */
992 static RETSIGTYPE
993deathtrap SIGDEFARG(sigarg)
994{
995 static int entered = 0; /* count the number of times we got here.
996 Note: when memory has been corrupted
997 this may get an arbitrary value! */
998#ifdef SIGHASARG
999 int i;
1000#endif
1001
1002#if defined(HAVE_SETJMP_H)
1003 /*
1004 * Catch a crash in protected code.
1005 * Restores the environment saved in lc_jump_env, which looks like
1006 * SETJMP() returns 1.
1007 */
1008 if (lc_active)
1009 {
1010# if defined(SIGHASARG)
1011 lc_signal = sigarg;
1012# endif
1013 lc_active = FALSE; /* don't jump again */
1014 LONGJMP(lc_jump_env, 1);
1015 /* NOTREACHED */
1016 }
1017#endif
1018
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001019#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001020# ifdef SIGQUIT
1021 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1022 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1023 * pressing CTRL-\, but we don't want Vim to exit then. */
1024 if (in_mch_delay && sigarg == SIGQUIT)
1025 SIGRETURN;
1026# endif
1027
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001028 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1029 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1030 * free(). Calling free() again may then cause a crash. */
1031 if (entered == 0
1032 && (0
1033# ifdef SIGHUP
1034 || sigarg == SIGHUP
1035# endif
1036# ifdef SIGQUIT
1037 || sigarg == SIGQUIT
1038# endif
1039# ifdef SIGTERM
1040 || sigarg == SIGTERM
1041# endif
1042# ifdef SIGPWR
1043 || sigarg == SIGPWR
1044# endif
1045# ifdef SIGUSR1
1046 || sigarg == SIGUSR1
1047# endif
1048# ifdef SIGUSR2
1049 || sigarg == SIGUSR2
1050# endif
1051 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001052 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001053 SIGRETURN;
1054#endif
1055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 /* Remember how often we have been called. */
1057 ++entered;
1058
Bram Moolenaare429e702016-06-10 19:49:14 +02001059#ifdef FEAT_AUTOCMD
1060 /* Executing autocommands is likely to use more stack space than we have
1061 * available in the signal stack. */
1062 block_autocmds();
1063#endif
1064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065#ifdef FEAT_EVAL
1066 /* Set the v:dying variable. */
1067 set_vim_var_nr(VV_DYING, (long)entered);
1068#endif
1069
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001070#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 /* Since we are now using the signal stack, need to reset the stack
1072 * limit. Otherwise using a regexp will fail. */
1073 get_stack_limit();
1074#endif
1075
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001076#if 0
1077 /* This is for opening gdb the moment Vim crashes.
1078 * You need to manually adjust the file name and Vim executable name.
1079 * Suggested by SungHyun Nam. */
1080 {
1081# define VI_GDB_FILE "/tmp/vimgdb"
1082# define VIM_NAME "/usr/bin/vim"
1083 FILE *fp = fopen(VI_GDB_FILE, "w");
1084 if (fp)
1085 {
1086 fprintf(fp,
1087 "file %s\n"
1088 "attach %d\n"
1089 "set height 1000\n"
1090 "bt full\n"
1091 , VIM_NAME, getpid());
1092 fclose(fp);
1093 system("xterm -e gdb -x "VI_GDB_FILE);
1094 unlink(VI_GDB_FILE);
1095 }
1096 }
1097#endif
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099#ifdef SIGHASARG
1100 /* try to find the name of this signal */
1101 for (i = 0; signal_info[i].sig != -1; i++)
1102 if (sigarg == signal_info[i].sig)
1103 break;
1104 deadly_signal = sigarg;
1105#endif
1106
1107 full_screen = FALSE; /* don't write message to the GUI, it might be
1108 * part of the problem... */
1109 /*
1110 * If something goes wrong after entering here, we may get here again.
1111 * When this happens, give a message and try to exit nicely (resetting the
1112 * terminal mode, etc.)
1113 * When this happens twice, just exit, don't even try to give a message,
1114 * stack may be corrupt or something weird.
1115 * When this still happens again (or memory was corrupted in such a way
1116 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1117 */
1118 if (entered >= 3)
1119 {
1120 reset_signals(); /* don't catch any signals anymore */
1121 may_core_dump();
1122 if (entered >= 4)
1123 _exit(8);
1124 exit(7);
1125 }
1126 if (entered == 2)
1127 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001128 /* No translation, it may call malloc(). */
1129 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 out_flush();
1131 getout(1);
1132 }
1133
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001134 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001136 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 signal_info[i].name);
1138#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001139 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001141
1142 /* Preserve files and exit. This sets the really_exiting flag to prevent
1143 * calling free(). */
1144 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
Bram Moolenaare429e702016-06-10 19:49:14 +02001146 /* NOTREACHED */
1147
Bram Moolenaar009b2592004-10-24 19:18:58 +00001148#ifdef NBDEBUG
1149 reset_signals();
1150 may_core_dump();
1151 abort();
1152#endif
1153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 SIGRETURN;
1155}
1156
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001157#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158/*
1159 * On Solaris with multi-threading, suspending might not work immediately.
1160 * Catch the SIGCONT signal, which will be used as an indication whether the
1161 * suspending has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001162 *
1163 * On Linux, signal is not always handled immediately either.
1164 * See https://bugs.launchpad.net/bugs/291373
1165 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001166 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001168static volatile int sigcont_received;
Bram Moolenaard99df422016-01-29 23:20:40 +01001169static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
1171/*
1172 * signal handler for SIGCONT
1173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 static RETSIGTYPE
1175sigcont_handler SIGDEFARG(sigarg)
1176{
1177 sigcont_received = TRUE;
1178 SIGRETURN;
1179}
1180#endif
1181
Bram Moolenaar62b42182010-09-21 22:09:37 +02001182# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001183static void loose_clipboard(void);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001184# ifdef USE_SYSTEM
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001185static void save_clipboard(void);
1186static void restore_clipboard(void);
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001187
1188static void *clip_star_save = NULL;
1189static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001190# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001191
1192/*
1193 * Called when Vim is going to sleep or execute a shell command.
1194 * We can't respond to requests for the X selections. Lose them, otherwise
1195 * other applications will hang. But first copy the text to cut buffer 0.
1196 */
1197 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001198loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001199{
1200 if (clip_star.owned || clip_plus.owned)
1201 {
1202 x11_export_final_selection();
1203 if (clip_star.owned)
1204 clip_lose_selection(&clip_star);
1205 if (clip_plus.owned)
1206 clip_lose_selection(&clip_plus);
1207 if (x11_display != NULL)
1208 XFlush(x11_display);
1209 }
1210}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001211
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001212# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001213/*
1214 * Save clipboard text to restore later.
1215 */
1216 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001217save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001218{
1219 if (clip_star.owned)
1220 clip_star_save = get_register('*', TRUE);
1221 if (clip_plus.owned)
1222 clip_plus_save = get_register('+', TRUE);
1223}
1224
1225/*
1226 * Restore clipboard text if no one own the X selection.
1227 */
1228 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001229restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001230{
1231 if (clip_star_save != NULL)
1232 {
1233 if (!clip_gen_owner_exists(&clip_star))
1234 put_register('*', clip_star_save);
1235 else
1236 free_register(clip_star_save);
1237 clip_star_save = NULL;
1238 }
1239 if (clip_plus_save != NULL)
1240 {
1241 if (!clip_gen_owner_exists(&clip_plus))
1242 put_register('+', clip_plus_save);
1243 else
1244 free_register(clip_plus_save);
1245 clip_plus_save = NULL;
1246 }
1247}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001248# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001249#endif
1250
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251/*
1252 * If the machine has job control, use it to suspend the program,
1253 * otherwise fake it by starting a new shell.
1254 */
1255 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001256mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257{
1258 /* BeOS does have SIGTSTP, but it doesn't work. */
1259#if defined(SIGTSTP) && !defined(__BEOS__)
1260 out_flush(); /* needed to make cursor visible on some systems */
1261 settmode(TMODE_COOK);
1262 out_flush(); /* needed to disable mouse on some systems */
1263
1264# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001265 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266# endif
1267
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001268# if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 sigcont_received = FALSE;
1270# endif
1271 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001272# if defined(_REENTRANT) && defined(SIGCONT)
1273 /*
1274 * Wait for the SIGCONT signal to be handled. It generally happens
1275 * immediately, but somehow not all the time. Do not call pause()
1276 * because there would be race condition which would hang Vim if
1277 * signal happened in between the test of sigcont_received and the
1278 * call to pause(). If signal is not yet received, call sleep(0)
1279 * to just yield CPU. Signal should then be received. If somehow
1280 * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting
1281 * further if signal is not received after 1+2+3+4 ms (not expected
1282 * to happen).
1283 */
1284 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001285 long wait_time;
1286 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001287 /* Loop is not entered most of the time */
Bram Moolenaar262735e2009-07-14 10:20:22 +00001288 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290# endif
1291
1292# ifdef FEAT_TITLE
1293 /*
1294 * Set oldtitle to NULL, so the current title is obtained again.
1295 */
1296 vim_free(oldtitle);
1297 oldtitle = NULL;
1298# endif
1299 settmode(TMODE_RAW);
1300 need_check_timestamps = TRUE;
1301 did_check_timestamps = FALSE;
1302#else
1303 suspend_shell();
1304#endif
1305}
1306
1307 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001308mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309{
1310 Columns = 80;
1311 Rows = 24;
1312
1313 out_flush();
1314 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001315
Bram Moolenaar56718732006-03-15 22:53:57 +00001316#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001317 mac_conv_init();
1318#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001319#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1320 win_clip_init();
1321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322}
1323
1324 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001325set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326{
1327#if defined(SIGWINCH)
1328 /*
1329 * WINDOW CHANGE signal is handled with sig_winch().
1330 */
1331 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1332#endif
1333
1334 /*
1335 * We want the STOP signal to work, to make mch_suspend() work.
1336 * For "rvim" the STOP signal is ignored.
1337 */
1338#ifdef SIGTSTP
1339 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1340#endif
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001341#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 signal(SIGCONT, sigcont_handler);
1343#endif
1344
1345 /*
1346 * We want to ignore breaking of PIPEs.
1347 */
1348#ifdef SIGPIPE
1349 signal(SIGPIPE, SIG_IGN);
1350#endif
1351
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001353 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354#endif
1355
1356 /*
1357 * Ignore alarm signals (Perl's alarm() generates it).
1358 */
1359#ifdef SIGALRM
1360 signal(SIGALRM, SIG_IGN);
1361#endif
1362
1363 /*
1364 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1365 * work will be lost.
1366 */
1367#ifdef SIGPWR
1368 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1369#endif
1370
1371 /*
1372 * Arrange for other signals to gracefully shutdown Vim.
1373 */
1374 catch_signals(deathtrap, SIG_ERR);
1375
1376#if defined(FEAT_GUI) && defined(SIGHUP)
1377 /*
1378 * When the GUI is running, ignore the hangup signal.
1379 */
1380 if (gui.in_use)
1381 signal(SIGHUP, SIG_IGN);
1382#endif
1383}
1384
Bram Moolenaardf177f62005-02-22 08:39:57 +00001385#if defined(SIGINT) || defined(PROTO)
1386/*
1387 * Catch CTRL-C (only works while in Cooked mode).
1388 */
1389 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001390catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001391{
1392 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1393}
1394#endif
1395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001397reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
1399 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001400#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 /* SIGCONT isn't in the list, because its default action is ignore */
1402 signal(SIGCONT, SIG_DFL);
1403#endif
1404}
1405
1406 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001407catch_signals(
1408 RETSIGTYPE (*func_deadly)(),
1409 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
1411 int i;
1412
1413 for (i = 0; signal_info[i].sig != -1; i++)
1414 if (signal_info[i].deadly)
1415 {
1416#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1417 struct sigaction sa;
1418
1419 /* Setup to use the alternate stack for the signal function. */
1420 sa.sa_handler = func_deadly;
1421 sigemptyset(&sa.sa_mask);
1422# if defined(__linux__) && defined(_REENTRANT)
1423 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1424 * thread handling in combination with using the alternate stack:
1425 * pthread library functions try to use the stack pointer to
1426 * identify the current thread, causing a SEGV signal, which
1427 * recursively calls deathtrap() and hangs. */
1428 sa.sa_flags = 0;
1429# else
1430 sa.sa_flags = SA_ONSTACK;
1431# endif
1432 sigaction(signal_info[i].sig, &sa, NULL);
1433#else
1434# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1435 struct sigvec sv;
1436
1437 /* Setup to use the alternate stack for the signal function. */
1438 sv.sv_handler = func_deadly;
1439 sv.sv_mask = 0;
1440 sv.sv_flags = SV_ONSTACK;
1441 sigvec(signal_info[i].sig, &sv, NULL);
1442# else
1443 signal(signal_info[i].sig, func_deadly);
1444# endif
1445#endif
1446 }
1447 else if (func_other != SIG_ERR)
1448 signal(signal_info[i].sig, func_other);
1449}
1450
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001451#ifdef HAVE_SIGPROCMASK
1452 static void
1453block_signals(sigset_t *set)
1454{
1455 sigset_t newset;
1456 int i;
1457
1458 sigemptyset(&newset);
1459
1460 for (i = 0; signal_info[i].sig != -1; i++)
1461 sigaddset(&newset, signal_info[i].sig);
1462
1463# if defined(_REENTRANT) && defined(SIGCONT)
1464 /* SIGCONT isn't in the list, because its default action is ignore */
1465 sigaddset(&newset, SIGCONT);
1466# endif
1467
1468 sigprocmask(SIG_BLOCK, &newset, set);
1469}
1470
1471 static void
1472unblock_signals(sigset_t *set)
1473{
1474 sigprocmask(SIG_SETMASK, set, NULL);
1475}
1476#endif
1477
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001479 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001480 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1481 * return TRUE
1482 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1483 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001484 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001485 * Returns TRUE when Vim should exit.
1486 */
1487 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001488vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001489{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001490 static int got_signal = 0;
1491 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001492
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001493 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001494 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001495 case SIGNAL_BLOCK: blocked = TRUE;
1496 break;
1497
1498 case SIGNAL_UNBLOCK: blocked = FALSE;
1499 if (got_signal != 0)
1500 {
1501 kill(getpid(), got_signal);
1502 got_signal = 0;
1503 }
1504 break;
1505
1506 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001507 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001508 got_signal = sig;
1509#ifdef SIGPWR
1510 if (sig != SIGPWR)
1511#endif
1512 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001513 break;
1514 }
1515 return FALSE;
1516}
1517
1518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 * Check_win checks whether we have an interactive stdout.
1520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001522mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (isatty(1))
1525 return OK;
1526 return FAIL;
1527}
1528
1529/*
1530 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1531 */
1532 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001533mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534{
1535 if (isatty(read_cmd_fd))
1536 return TRUE;
1537 return FALSE;
1538}
1539
1540#ifdef FEAT_X11
1541
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001542# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1544
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545/*
1546 * Give a message about the elapsed time for opening the X window.
1547 */
1548 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001549xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001551 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552}
1553# endif
1554#endif
1555
1556#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1557/*
1558 * A few functions shared by X11 title and clipboard code.
1559 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001560static int x_error_handler(Display *dpy, XErrorEvent *error_event);
1561static int x_error_check(Display *dpy, XErrorEvent *error_event);
1562static int x_connect_to_server(void);
1563static int test_x11_window(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564
1565static int got_x_error = FALSE;
1566
1567/*
1568 * X Error handler, otherwise X just exits! (very rude) -- webb
1569 */
1570 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001571x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001573 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 STRCAT(IObuff, _("\nVim: Got X error\n"));
1575
1576 /* We cannot print a message and continue, because no X calls are allowed
1577 * here (causes my system to hang). Silently continuing might be an
1578 * alternative... */
1579 preserve_exit(); /* preserve files and exit */
1580
1581 return 0; /* NOTREACHED */
1582}
1583
1584/*
1585 * Another X Error handler, just used to check for errors.
1586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001588x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 got_x_error = TRUE;
1591 return 0;
1592}
1593
1594#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1595# if defined(HAVE_SETJMP_H)
1596/*
1597 * An X IO Error handler, used to catch error while opening the display.
1598 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001599static int x_IOerror_check(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001602x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604 /* This function should not return, it causes exit(). Longjump instead. */
1605 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001606# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001607 return 0; /* avoid the compiler complains about missing return value */
1608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609}
1610# endif
1611
1612/*
1613 * An X IO Error handler, used to catch terminal errors.
1614 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001615static int x_IOerror_handler(Display *dpy);
1616static void may_restore_clipboard(void);
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001617static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001620x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
1622 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001623 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 x11_window = 0;
1625 x11_display = NULL;
1626 xterm_Shell = (Widget)0;
1627
1628 /* This function should not return, it causes exit(). Longjump instead. */
1629 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001630# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001631 return 0; /* avoid the compiler complains about missing return value */
1632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001634
1635/*
1636 * If the X11 connection was lost try to restore it.
1637 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001638 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001639 */
1640 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001641may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001642{
1643 if (xterm_dpy_was_reset)
1644 {
1645 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001646
1647# ifndef LESSTIF_VERSION
1648 /* This has been reported to avoid Vim getting stuck. */
1649 if (app_context != (XtAppContext)NULL)
1650 {
1651 XtDestroyApplicationContext(app_context);
1652 app_context = (XtAppContext)NULL;
1653 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1654 }
1655# endif
1656
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001657 setup_term_clip();
1658 get_x11_title(FALSE);
1659 }
1660}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661#endif
1662
1663/*
1664 * Return TRUE when connection to the X server is desired.
1665 */
1666 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001667x_connect_to_server(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669#if defined(FEAT_CLIENTSERVER)
1670 if (x_force_connect)
1671 return TRUE;
1672#endif
1673 if (x_no_connect)
1674 return FALSE;
1675
1676 /* Check for a match with "exclude:" from 'clipboard'. */
1677 if (clip_exclude_prog != NULL)
1678 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001679 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 return FALSE;
1681 }
1682 return TRUE;
1683}
1684
1685/*
1686 * Test if "dpy" and x11_window are valid by getting the window title.
1687 * I don't actually want it yet, so there may be a simpler call to use, but
1688 * this will cause the error handler x_error_check() to be called if anything
1689 * is wrong, such as the window pointer being invalid (as can happen when the
1690 * user changes his DISPLAY, but not his WINDOWID) -- webb
1691 */
1692 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001693test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694{
1695 int (*old_handler)();
1696 XTextProperty text_prop;
1697
1698 old_handler = XSetErrorHandler(x_error_check);
1699 got_x_error = FALSE;
1700 if (XGetWMName(dpy, x11_window, &text_prop))
1701 XFree((void *)text_prop.value);
1702 XSync(dpy, False);
1703 (void)XSetErrorHandler(old_handler);
1704
1705 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001706 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
1708 return (got_x_error ? FAIL : OK);
1709}
1710#endif
1711
1712#ifdef FEAT_TITLE
1713
1714#ifdef FEAT_X11
1715
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001716static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
1718/*
1719 * try to get x11 window and display
1720 *
1721 * return FAIL for failure, OK otherwise
1722 */
1723 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001724get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726 char *winid;
1727 static int result = -1;
1728#define XD_NONE 0 /* x11_display not set here */
1729#define XD_HERE 1 /* x11_display opened here */
1730#define XD_GUI 2 /* x11_display used from gui.dpy */
1731#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1732 static int x11_display_from = XD_NONE;
1733 static int did_set_error_handler = FALSE;
1734
1735 if (!did_set_error_handler)
1736 {
1737 /* X just exits if it finds an error otherwise! */
1738 (void)XSetErrorHandler(x_error_handler);
1739 did_set_error_handler = TRUE;
1740 }
1741
Bram Moolenaar9372a112005-12-06 19:59:18 +00001742#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (gui.in_use)
1744 {
1745 /*
1746 * If the X11 display was opened here before, for the window where Vim
1747 * was started, close that one now to avoid a memory leak.
1748 */
1749 if (x11_display_from == XD_HERE && x11_display != NULL)
1750 {
1751 XCloseDisplay(x11_display);
1752 x11_display_from = XD_NONE;
1753 }
1754 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1755 {
1756 x11_display_from = XD_GUI;
1757 return OK;
1758 }
1759 x11_display = NULL;
1760 return FAIL;
1761 }
1762 else if (x11_display_from == XD_GUI)
1763 {
1764 /* GUI must have stopped somehow, clear x11_display */
1765 x11_window = 0;
1766 x11_display = NULL;
1767 x11_display_from = XD_NONE;
1768 }
1769#endif
1770
1771 /* When started with the "-X" argument, don't try connecting. */
1772 if (!x_connect_to_server())
1773 return FAIL;
1774
1775 /*
1776 * If WINDOWID not set, should try another method to find out
1777 * what the current window number is. The only code I know for
1778 * this is very complicated.
1779 * We assume that zero is invalid for WINDOWID.
1780 */
1781 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1782 x11_window = (Window)atol(winid);
1783
1784#ifdef FEAT_XCLIPBOARD
1785 if (xterm_dpy != NULL && x11_window != 0)
1786 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001787 /* We may have checked it already, but Gnome terminal can move us to
1788 * another window, so we need to check every time. */
1789 if (x11_display_from != XD_XTERM)
1790 {
1791 /*
1792 * If the X11 display was opened here before, for the window where
1793 * Vim was started, close that one now to avoid a memory leak.
1794 */
1795 if (x11_display_from == XD_HERE && x11_display != NULL)
1796 XCloseDisplay(x11_display);
1797 x11_display = xterm_dpy;
1798 x11_display_from = XD_XTERM;
1799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (test_x11_window(x11_display) == FAIL)
1801 {
1802 /* probably bad $WINDOWID */
1803 x11_window = 0;
1804 x11_display = NULL;
1805 x11_display_from = XD_NONE;
1806 return FAIL;
1807 }
1808 return OK;
1809 }
1810#endif
1811
1812 if (x11_window == 0 || x11_display == NULL)
1813 result = -1;
1814
1815 if (result != -1) /* Have already been here and set this */
1816 return result; /* Don't do all these X calls again */
1817
1818 if (x11_window != 0 && x11_display == NULL)
1819 {
1820#ifdef SET_SIG_ALARM
1821 RETSIGTYPE (*sig_save)();
1822#endif
1823#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1824 struct timeval start_tv;
1825
1826 if (p_verbose > 0)
1827 gettimeofday(&start_tv, NULL);
1828#endif
1829
1830#ifdef SET_SIG_ALARM
1831 /*
1832 * Opening the Display may hang if the DISPLAY setting is wrong, or
1833 * the network connection is bad. Set an alarm timer to get out.
1834 */
1835 sig_alarm_called = FALSE;
1836 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1837 (RETSIGTYPE (*)())sig_alarm);
1838 alarm(2);
1839#endif
1840 x11_display = XOpenDisplay(NULL);
1841
1842#ifdef SET_SIG_ALARM
1843 alarm(0);
1844 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1845 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001846 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847#endif
1848 if (x11_display != NULL)
1849 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001850# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001852 {
1853 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001854 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001855 verbose_leave();
1856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857# endif
1858 if (test_x11_window(x11_display) == FAIL)
1859 {
1860 /* Maybe window id is bad */
1861 x11_window = 0;
1862 XCloseDisplay(x11_display);
1863 x11_display = NULL;
1864 }
1865 else
1866 x11_display_from = XD_HERE;
1867 }
1868 }
1869 if (x11_window == 0 || x11_display == NULL)
1870 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001871
1872# ifdef FEAT_EVAL
1873 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1874# endif
1875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 return (result = OK);
1877}
1878
1879/*
1880 * Determine original x11 Window Title
1881 */
1882 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001883get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001885 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886}
1887
1888/*
1889 * Determine original x11 Window icon
1890 */
1891 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001892get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
1894 int retval = FALSE;
1895
1896 retval = get_x11_thing(FALSE, test_only);
1897
1898 /* could not get old icon, use terminal name */
1899 if (oldicon == NULL && !test_only)
1900 {
1901 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001902 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001904 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906
1907 return retval;
1908}
1909
1910 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001911get_x11_thing(
1912 int get_title, /* get title string */
1913 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914{
1915 XTextProperty text_prop;
1916 int retval = FALSE;
1917 Status status;
1918
1919 if (get_x11_windis() == OK)
1920 {
1921 /* Get window/icon name if any */
1922 if (get_title)
1923 status = XGetWMName(x11_display, x11_window, &text_prop);
1924 else
1925 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1926
1927 /*
1928 * If terminal is xterm, then x11_window may be a child window of the
1929 * outer xterm window that actually contains the window/icon name, so
1930 * keep traversing up the tree until a window with a title/icon is
1931 * found.
1932 */
1933 /* Previously this was only done for xterm and alikes. I don't see a
1934 * reason why it would fail for other terminal emulators.
1935 * if (term_is_xterm) */
1936 {
1937 Window root;
1938 Window parent;
1939 Window win = x11_window;
1940 Window *children;
1941 unsigned int num_children;
1942
1943 while (!status || text_prop.value == NULL)
1944 {
1945 if (!XQueryTree(x11_display, win, &root, &parent, &children,
1946 &num_children))
1947 break;
1948 if (children)
1949 XFree((void *)children);
1950 if (parent == root || parent == 0)
1951 break;
1952
1953 win = parent;
1954 if (get_title)
1955 status = XGetWMName(x11_display, win, &text_prop);
1956 else
1957 status = XGetWMIconName(x11_display, win, &text_prop);
1958 }
1959 }
1960 if (status && text_prop.value != NULL)
1961 {
1962 retval = TRUE;
1963 if (!test_only)
1964 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001965#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
1966 if (text_prop.encoding == XA_STRING
1967# ifdef FEAT_MBYTE
1968 && !has_mbyte
1969# endif
1970 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
1972#endif
1973 if (get_title)
1974 oldtitle = vim_strsave((char_u *)text_prop.value);
1975 else
1976 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001977#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979 else
1980 {
1981 char **cl;
1982 Status transform_status;
1983 int n = 0;
1984
1985 transform_status = XmbTextPropertyToTextList(x11_display,
1986 &text_prop,
1987 &cl, &n);
1988 if (transform_status >= Success && n > 0 && cl[0])
1989 {
1990 if (get_title)
1991 oldtitle = vim_strsave((char_u *) cl[0]);
1992 else
1993 oldicon = vim_strsave((char_u *) cl[0]);
1994 XFreeStringList(cl);
1995 }
1996 else
1997 {
1998 if (get_title)
1999 oldtitle = vim_strsave((char_u *)text_prop.value);
2000 else
2001 oldicon = vim_strsave((char_u *)text_prop.value);
2002 }
2003 }
2004#endif
2005 }
2006 XFree((void *)text_prop.value);
2007 }
2008 }
2009 return retval;
2010}
2011
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002012/* Xutf8 functions are not avaialble on older systems. Note that on some
2013 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2014 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2015 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002017# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018# define USE_UTF8_STRING
2019# endif
2020#endif
2021
2022/*
2023 * Set x11 Window Title
2024 *
2025 * get_x11_windis() must be called before this and have returned OK
2026 */
2027 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002028set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029{
2030 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2031 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2032 * supported everywhere and STRING doesn't work for multi-byte titles.
2033 */
2034#ifdef USE_UTF8_STRING
2035 if (enc_utf8)
2036 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2037 NULL, NULL, 0, NULL, NULL, NULL);
2038 else
2039#endif
2040 {
2041#if XtSpecificationRelease >= 4
2042# ifdef FEAT_XFONTSET
2043 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2044 NULL, NULL, 0, NULL, NULL, NULL);
2045# else
2046 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002047 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002050 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 XSetWMProperties(x11_display, x11_window, &text_prop,
2052 NULL, NULL, 0, NULL, NULL, NULL);
2053# endif
2054#else
2055 XStoreName(x11_display, x11_window, (char *)title);
2056#endif
2057 }
2058 XFlush(x11_display);
2059}
2060
2061/*
2062 * Set x11 Window icon
2063 *
2064 * get_x11_windis() must be called before this and have returned OK
2065 */
2066 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002067set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068{
2069 /* See above for comments about using X*SetWMProperties(). */
2070#ifdef USE_UTF8_STRING
2071 if (enc_utf8)
2072 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2073 NULL, 0, NULL, NULL, NULL);
2074 else
2075#endif
2076 {
2077#if XtSpecificationRelease >= 4
2078# ifdef FEAT_XFONTSET
2079 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2080 NULL, 0, NULL, NULL, NULL);
2081# else
2082 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002083 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002085 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2087 NULL, 0, NULL, NULL, NULL);
2088# endif
2089#else
2090 XSetIconName(x11_display, x11_window, (char *)icon);
2091#endif
2092 }
2093 XFlush(x11_display);
2094}
2095
2096#else /* FEAT_X11 */
2097
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002099get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100{
2101 return FALSE;
2102}
2103
2104 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002105get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106{
2107 if (!test_only)
2108 {
2109 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002110 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002112 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
2114 return FALSE;
2115}
2116
2117#endif /* FEAT_X11 */
2118
2119 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002120mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121{
2122 return get_x11_title(TRUE);
2123}
2124
2125 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002126mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
2128 return get_x11_icon(TRUE);
2129}
2130
2131/*
2132 * Set the window title and icon.
2133 */
2134 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002135mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136{
2137 int type = 0;
2138 static int recursive = 0;
2139
2140 if (T_NAME == NULL) /* no terminal name (yet) */
2141 return;
2142 if (title == NULL && icon == NULL) /* nothing to do */
2143 return;
2144
2145 /* When one of the X11 functions causes a deadly signal, we get here again
2146 * recursively. Avoid hanging then (something is probably locked). */
2147 if (recursive)
2148 return;
2149 ++recursive;
2150
2151 /*
2152 * if the window ID and the display is known, we may use X11 calls
2153 */
2154#ifdef FEAT_X11
2155 if (get_x11_windis() == OK)
2156 type = 1;
2157#else
2158# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2159 if (gui.in_use)
2160 type = 1;
2161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162#endif
2163
2164 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002165 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 * than x11 calls, because the x11 calls don't always work
2167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 if ((type || *T_TS != NUL) && title != NULL)
2169 {
2170 if (oldtitle == NULL
2171#ifdef FEAT_GUI
2172 && !gui.in_use
2173#endif
2174 ) /* first call but not in GUI, save title */
2175 (void)get_x11_title(FALSE);
2176
2177 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2178 term_settitle(title);
2179#ifdef FEAT_X11
2180 else
2181# ifdef FEAT_GUI_GTK
2182 if (!gui.in_use) /* don't do this if GTK+ is running */
2183# endif
2184 set_x11_title(title); /* x11 */
2185#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002186#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2188 else
2189 gui_mch_settitle(title, icon);
2190#endif
2191 did_set_title = TRUE;
2192 }
2193
2194 if ((type || *T_CIS != NUL) && icon != NULL)
2195 {
2196 if (oldicon == NULL
2197#ifdef FEAT_GUI
2198 && !gui.in_use
2199#endif
2200 ) /* first call, save icon */
2201 get_x11_icon(FALSE);
2202
2203 if (*T_CIS != NUL)
2204 {
2205 out_str(T_CIS); /* set icon start */
2206 out_str_nf(icon);
2207 out_str(T_CIE); /* set icon end */
2208 out_flush();
2209 }
2210#ifdef FEAT_X11
2211 else
2212# ifdef FEAT_GUI_GTK
2213 if (!gui.in_use) /* don't do this if GTK+ is running */
2214# endif
2215 set_x11_icon(icon); /* x11 */
2216#endif
2217 did_set_icon = TRUE;
2218 }
2219 --recursive;
2220}
2221
2222/*
2223 * Restore the window/icon title.
2224 * "which" is one of:
2225 * 1 only restore title
2226 * 2 only restore icon
2227 * 3 restore title and icon
2228 */
2229 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002230mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231{
2232 /* only restore the title or icon when it has been set */
2233 mch_settitle(((which & 1) && did_set_title) ?
2234 (oldtitle ? oldtitle : p_titleold) : NULL,
2235 ((which & 2) && did_set_icon) ? oldicon : NULL);
2236}
2237
2238#endif /* FEAT_TITLE */
2239
2240/*
2241 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002242 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 */
2244 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002245vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246{
2247 if (name == NULL)
2248 return FALSE;
2249 return (STRNICMP(name, "xterm", 5) == 0
2250 || STRNICMP(name, "nxterm", 6) == 0
2251 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002252 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 || STRNICMP(name, "rxvt", 4) == 0
2254 || STRCMP(name, "builtin_xterm") == 0);
2255}
2256
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002257#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2258/*
2259 * Return TRUE if "name" appears to be that of a terminal
2260 * known to support the xterm-style mouse protocol.
2261 * Relies on term_is_xterm having been set to its correct value.
2262 */
2263 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002264use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002265{
2266 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002267 && (term_is_xterm
2268 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002269 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002270 || STRICMP(name, "st") == 0
2271 || STRNICMP(name, "st-", 3) == 0
2272 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002273}
2274#endif
2275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2277/*
2278 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2279 * Return 1 for "xterm".
2280 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002281 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002282 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 */
2284 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002285use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002287 if (ttym_flags == TTYM_SGR)
2288 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002289 if (ttym_flags == TTYM_URXVT)
2290 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (ttym_flags == TTYM_XTERM2)
2292 return 2;
2293 if (ttym_flags == TTYM_XTERM)
2294 return 1;
2295 return 0;
2296}
2297#endif
2298
2299 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002300vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301{
2302 if (name == NULL)
2303 return FALSE;
2304 return (STRNICMP(name, "iris-ansi", 9) == 0
2305 || STRCMP(name, "builtin_iris-ansi") == 0);
2306}
2307
2308 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002309vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
2311 if (name == NULL)
2312 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002313 /* catch VT100 - VT5xx */
2314 return ((STRNICMP(name, "vt", 2) == 0
2315 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 || STRCMP(name, "builtin_vt320") == 0);
2317}
2318
2319/*
2320 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2321 * This should include all windowed terminal emulators.
2322 */
2323 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002324vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
2326 if (name == NULL)
2327 return FALSE;
2328 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2329 return TRUE;
2330 return ( STRNICMP(name, "hpterm", 6) == 0
2331 || STRNICMP(name, "sun-cmd", 7) == 0
2332 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002333 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 || STRNICMP(name, "dtterm", 6) == 0);
2335}
2336
2337/*
2338 * Insert user name in s[len].
2339 * Return OK if a name found.
2340 */
2341 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002342mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
2344#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002345 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 return OK;
2347#else
2348 return mch_get_uname(getuid(), s, len);
2349#endif
2350}
2351
2352/*
2353 * Insert user name for "uid" in s[len].
2354 * Return OK if a name found.
2355 */
2356 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002357mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
2359#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2360 struct passwd *pw;
2361
2362 if ((pw = getpwuid(uid)) != NULL
2363 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2364 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002365 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 return OK;
2367 }
2368#endif
2369 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2370 return FAIL; /* a number is not a name */
2371}
2372
2373/*
2374 * Insert host name is s[len].
2375 */
2376
2377#ifdef HAVE_SYS_UTSNAME_H
2378 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002379mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
2381 struct utsname vutsname;
2382
2383 if (uname(&vutsname) < 0)
2384 *s = NUL;
2385 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002386 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387}
2388#else /* HAVE_SYS_UTSNAME_H */
2389
2390# ifdef HAVE_SYS_SYSTEMINFO_H
2391# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2392# endif
2393
2394 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002395mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
2397# ifdef VAXC
2398 vaxc$gethostname((char *)s, len);
2399# else
2400 gethostname((char *)s, len);
2401# endif
2402 s[len - 1] = NUL; /* make sure it's terminated */
2403}
2404#endif /* HAVE_SYS_UTSNAME_H */
2405
2406/*
2407 * return process ID
2408 */
2409 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002410mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
2412 return (long)getpid();
2413}
2414
2415#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002416static char *strerror(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417
2418 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002419strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421 extern int sys_nerr;
2422 extern char *sys_errlist[];
2423 static char er[20];
2424
2425 if (err > 0 && err < sys_nerr)
2426 return (sys_errlist[err]);
2427 sprintf(er, "Error %d", err);
2428 return er;
2429}
2430#endif
2431
2432/*
2433 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2434 * Return OK for success, FAIL for failure.
2435 */
2436 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002437mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439#if defined(USE_GETCWD)
2440 if (getcwd((char *)buf, len) == NULL)
2441 {
2442 STRCPY(buf, strerror(errno));
2443 return FAIL;
2444 }
2445 return OK;
2446#else
2447 return (getwd((char *)buf) != NULL ? OK : FAIL);
2448#endif
2449}
2450
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002452 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 *
2454 * return FAIL for failure, OK for success
2455 */
2456 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002457mch_FullName(
2458 char_u *fname,
2459 char_u *buf,
2460 int len,
2461 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002464#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 int fd = -1;
2466 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 char_u olddir[MAXPATHL];
2469 char_u *p;
2470 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002471#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002472 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2473 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002474#endif
2475
Bram Moolenaar38323e42007-03-06 19:22:53 +00002476#ifdef VMS
2477 fname = vms_fixfilename(fname);
2478#endif
2479
Bram Moolenaara2442432007-04-26 14:26:37 +00002480#ifdef __CYGWIN__
2481 /*
2482 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2483 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002484# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002485 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2486 * a forward slash. */
2487 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2488 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002489# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002490 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002491# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002492 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002493#endif
2494
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002495 /* Expand it if forced or not an absolute path.
2496 * Do not do it for "/file", the result is always "/". */
2497 if ((force || !mch_isFullName(fname))
2498 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 {
2500 /*
2501 * If the file name has a path, change to that directory for a moment,
2502 * and then do the getwd() (and get back to where we were).
2503 * This will get the correct path name with "../" things.
2504 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002505 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002507#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 /*
2509 * Use fchdir() if possible, it's said to be faster and more
2510 * reliable. But on SunOS 4 it might not work. Check this by
2511 * doing a fchdir() right now.
2512 */
2513 if (!dont_fchdir)
2514 {
2515 fd = open(".", O_RDONLY | O_EXTRA, 0);
2516 if (fd >= 0 && fchdir(fd) < 0)
2517 {
2518 close(fd);
2519 fd = -1;
2520 dont_fchdir = TRUE; /* don't try again */
2521 }
2522 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
2525 /* Only change directory when we are sure we can return to where
2526 * we are now. After doing "su" chdir(".") might not work. */
2527 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002528#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 (mch_dirname(olddir, MAXPATHL) == FAIL
2532 || mch_chdir((char *)olddir) != 0))
2533 {
2534 p = NULL; /* can't get current dir: don't chdir */
2535 retval = FAIL;
2536 }
2537 else
2538 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 /* The directory is copied into buf[], to be able to remove
2540 * the file name without changing it (could be a string in
2541 * read-only memory) */
2542 if (p - fname >= len)
2543 retval = FAIL;
2544 else
2545 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002546 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (mch_chdir((char *)buf))
2548 retval = FAIL;
2549 else
2550 fname = p + 1;
2551 *buf = NUL;
2552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
2554 }
2555 if (mch_dirname(buf, len) == FAIL)
2556 {
2557 retval = FAIL;
2558 *buf = NUL;
2559 }
2560 if (p != NULL)
2561 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002562#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (fd >= 0)
2564 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002565 if (p_verbose >= 5)
2566 {
2567 verbose_enter();
2568 MSG("fchdir() to previous dir");
2569 verbose_leave();
2570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 l = fchdir(fd);
2572 close(fd);
2573 }
2574 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 l = mch_chdir((char *)olddir);
2577 if (l != 0)
2578 EMSG(_(e_prev_dir));
2579 }
2580
2581 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002582 if (l >= len - 1)
2583 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002584#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002585 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002587 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002590
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002592 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 return FAIL;
2594
2595 /* Do not append ".", "/dir/." is equal to "/dir". */
2596 if (STRCMP(fname, ".") != 0)
2597 STRCAT(buf, fname);
2598
2599 return OK;
2600}
2601
2602/*
2603 * Return TRUE if "fname" does not depend on the current directory.
2604 */
2605 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002606mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002608#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 return ( fname[0] == '/' || fname[0] == '.' ||
2610 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2611 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2612 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002613#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615#endif
2616}
2617
Bram Moolenaar24552be2005-12-10 20:17:30 +00002618#if defined(USE_FNAME_CASE) || defined(PROTO)
2619/*
2620 * Set the case of the file name, if it already exists. This will cause the
2621 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002622 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002623 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002624 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002625fname_case(
2626 char_u *name,
2627 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002628{
2629 struct stat st;
2630 char_u *slash, *tail;
2631 DIR *dirp;
2632 struct dirent *dp;
2633
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002634 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002635 {
2636 /* Open the directory where the file is located. */
2637 slash = vim_strrchr(name, '/');
2638 if (slash == NULL)
2639 {
2640 dirp = opendir(".");
2641 tail = name;
2642 }
2643 else
2644 {
2645 *slash = NUL;
2646 dirp = opendir((char *)name);
2647 *slash = '/';
2648 tail = slash + 1;
2649 }
2650
2651 if (dirp != NULL)
2652 {
2653 while ((dp = readdir(dirp)) != NULL)
2654 {
2655 /* Only accept names that differ in case and are the same byte
2656 * length. TODO: accept different length name. */
2657 if (STRICMP(tail, dp->d_name) == 0
2658 && STRLEN(tail) == STRLEN(dp->d_name))
2659 {
2660 char_u newname[MAXPATHL + 1];
2661 struct stat st2;
2662
2663 /* Verify the inode is equal. */
2664 vim_strncpy(newname, name, MAXPATHL);
2665 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2666 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002667 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002668 && st.st_ino == st2.st_ino
2669 && st.st_dev == st2.st_dev)
2670 {
2671 STRCPY(tail, dp->d_name);
2672 break;
2673 }
2674 }
2675 }
2676
2677 closedir(dirp);
2678 }
2679 }
2680}
2681#endif
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683/*
2684 * Get file permissions for 'name'.
2685 * Returns -1 when it doesn't exist.
2686 */
2687 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002688mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
2690 struct stat statb;
2691
2692 /* Keep the #ifdef outside of stat(), it may be a macro. */
2693#ifdef VMS
2694 if (stat((char *)vms_fixfilename(name), &statb))
2695#else
2696 if (stat((char *)name, &statb))
2697#endif
2698 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002699#ifdef __INTERIX
2700 /* The top bit makes the value negative, which means the file doesn't
2701 * exist. Remove the bit, we don't use it. */
2702 return statb.st_mode & ~S_ADDACE;
2703#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706}
2707
2708/*
2709 * set file permission for 'name' to 'perm'
2710 *
2711 * return FAIL for failure, OK otherwise
2712 */
2713 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002714mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715{
2716 return (chmod((char *)
2717#ifdef VMS
2718 vms_fixfilename(name),
2719#else
2720 name,
2721#endif
2722 (mode_t)perm) == 0 ? OK : FAIL);
2723}
2724
2725#if defined(HAVE_ACL) || defined(PROTO)
2726# ifdef HAVE_SYS_ACL_H
2727# include <sys/acl.h>
2728# endif
2729# ifdef HAVE_SYS_ACCESS_H
2730# include <sys/access.h>
2731# endif
2732
2733# ifdef HAVE_SOLARIS_ACL
2734typedef struct vim_acl_solaris_T {
2735 int acl_cnt;
2736 aclent_t *acl_entry;
2737} vim_acl_solaris_T;
2738# endif
2739
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002740#if defined(HAVE_SELINUX) || defined(PROTO)
2741/*
2742 * Copy security info from "from_file" to "to_file".
2743 */
2744 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002745mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002746{
2747 if (from_file == NULL)
2748 return;
2749
2750 if (selinux_enabled == -1)
2751 selinux_enabled = is_selinux_enabled();
2752
2753 if (selinux_enabled > 0)
2754 {
2755 security_context_t from_context = NULL;
2756 security_context_t to_context = NULL;
2757
2758 if (getfilecon((char *)from_file, &from_context) < 0)
2759 {
2760 /* If the filesystem doesn't support extended attributes,
2761 the original had no special security context and the
2762 target cannot have one either. */
2763 if (errno == EOPNOTSUPP)
2764 return;
2765
2766 MSG_PUTS(_("\nCould not get security context for "));
2767 msg_outtrans(from_file);
2768 msg_putchar('\n');
2769 return;
2770 }
2771 if (getfilecon((char *)to_file, &to_context) < 0)
2772 {
2773 MSG_PUTS(_("\nCould not get security context for "));
2774 msg_outtrans(to_file);
2775 msg_putchar('\n');
2776 freecon (from_context);
2777 return ;
2778 }
2779 if (strcmp(from_context, to_context) != 0)
2780 {
2781 if (setfilecon((char *)to_file, from_context) < 0)
2782 {
2783 MSG_PUTS(_("\nCould not set security context for "));
2784 msg_outtrans(to_file);
2785 msg_putchar('\n');
2786 }
2787 }
2788 freecon(to_context);
2789 freecon(from_context);
2790 }
2791}
2792#endif /* HAVE_SELINUX */
2793
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002794#if defined(HAVE_SMACK) && !defined(PROTO)
2795/*
2796 * Copy security info from "from_file" to "to_file".
2797 */
2798 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002799mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002800{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002801 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002802 {
2803 XATTR_NAME_SMACK,
2804 XATTR_NAME_SMACKEXEC,
2805 XATTR_NAME_SMACKMMAP
2806 };
2807
2808 char buffer[SMACK_LABEL_LEN];
2809 const char *name;
2810 int index;
2811 int ret;
2812 ssize_t size;
2813
2814 if (from_file == NULL)
2815 return;
2816
2817 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2818 / sizeof(smack_copied_attributes)[0]) ; index++)
2819 {
2820 /* get the name of the attribute to copy */
2821 name = smack_copied_attributes[index];
2822
2823 /* get the value of the attribute in buffer */
2824 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2825 if (size >= 0)
2826 {
2827 /* copy the attribute value of buffer */
2828 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2829 if (ret < 0)
2830 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002831 vim_snprintf((char *)IObuff, IOSIZE,
2832 _("Could not set security context %s for %s"),
2833 name, to_file);
2834 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002835 msg_putchar('\n');
2836 }
2837 }
2838 else
2839 {
2840 /* what reason of not having the attribute value? */
2841 switch (errno)
2842 {
2843 case ENOTSUP:
2844 /* extended attributes aren't supported or enabled */
2845 /* should a message be echoed? not sure... */
2846 return; /* leave because it isn't usefull to continue */
2847
2848 case ERANGE:
2849 default:
2850 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002851 vim_snprintf((char *)IObuff, IOSIZE,
2852 _("Could not get security context %s for %s. Removing it!"),
2853 name, from_file);
2854 msg_puts(IObuff);
2855 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002856 /* FALLTHROUGH to remove the attribute */
2857
2858 case ENODATA:
2859 /* no attribute of this name */
2860 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002861 /* Silently ignore errors, apparently this happens when
2862 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002863 break;
2864 }
2865 }
2866 }
2867}
2868#endif /* HAVE_SMACK */
2869
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870/*
2871 * Return a pointer to the ACL of file "fname" in allocated memory.
2872 * Return NULL if the ACL is not available for whatever reason.
2873 */
2874 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002875mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876{
2877 vim_acl_T ret = NULL;
2878#ifdef HAVE_POSIX_ACL
2879 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2880#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002881#ifdef HAVE_SOLARIS_ZFS_ACL
2882 acl_t *aclent;
2883
2884 if (acl_get((char *)fname, 0, &aclent) < 0)
2885 return NULL;
2886 ret = (vim_acl_T)aclent;
2887#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888#ifdef HAVE_SOLARIS_ACL
2889 vim_acl_solaris_T *aclent;
2890
2891 aclent = malloc(sizeof(vim_acl_solaris_T));
2892 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2893 {
2894 free(aclent);
2895 return NULL;
2896 }
2897 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2898 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2899 {
2900 free(aclent->acl_entry);
2901 free(aclent);
2902 return NULL;
2903 }
2904 ret = (vim_acl_T)aclent;
2905#else
2906#if defined(HAVE_AIX_ACL)
2907 int aclsize;
2908 struct acl *aclent;
2909
2910 aclsize = sizeof(struct acl);
2911 aclent = malloc(aclsize);
2912 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2913 {
2914 if (errno == ENOSPC)
2915 {
2916 aclsize = aclent->acl_len;
2917 aclent = realloc(aclent, aclsize);
2918 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2919 {
2920 free(aclent);
2921 return NULL;
2922 }
2923 }
2924 else
2925 {
2926 free(aclent);
2927 return NULL;
2928 }
2929 }
2930 ret = (vim_acl_T)aclent;
2931#endif /* HAVE_AIX_ACL */
2932#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002933#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934#endif /* HAVE_POSIX_ACL */
2935 return ret;
2936}
2937
2938/*
2939 * Set the ACL of file "fname" to "acl" (unless it's NULL).
2940 */
2941 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002942mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
2944 if (aclent == NULL)
2945 return;
2946#ifdef HAVE_POSIX_ACL
2947 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
2948#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002949#ifdef HAVE_SOLARIS_ZFS_ACL
2950 acl_set((char *)fname, (acl_t *)aclent);
2951#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952#ifdef HAVE_SOLARIS_ACL
2953 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
2954 ((vim_acl_solaris_T *)aclent)->acl_entry);
2955#else
2956#ifdef HAVE_AIX_ACL
2957 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
2958#endif /* HAVE_AIX_ACL */
2959#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002960#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961#endif /* HAVE_POSIX_ACL */
2962}
2963
2964 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002965mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966{
2967 if (aclent == NULL)
2968 return;
2969#ifdef HAVE_POSIX_ACL
2970 acl_free((acl_t)aclent);
2971#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002972#ifdef HAVE_SOLARIS_ZFS_ACL
2973 acl_free((acl_t *)aclent);
2974#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975#ifdef HAVE_SOLARIS_ACL
2976 free(((vim_acl_solaris_T *)aclent)->acl_entry);
2977 free(aclent);
2978#else
2979#ifdef HAVE_AIX_ACL
2980 free(aclent);
2981#endif /* HAVE_AIX_ACL */
2982#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002983#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984#endif /* HAVE_POSIX_ACL */
2985}
2986#endif
2987
2988/*
2989 * Set hidden flag for "name".
2990 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002992mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993{
2994 /* can't hide a file */
2995}
2996
2997/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01002998 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 * return FALSE if "name" is not a directory
3000 * return FALSE for error
3001 */
3002 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003003mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 struct stat statb;
3006
3007 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3008 return FALSE;
3009 if (stat((char *)name, &statb))
3010 return FALSE;
3011#ifdef _POSIX_SOURCE
3012 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3013#else
3014 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3015#endif
3016}
3017
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003018/*
3019 * return TRUE if "name" is a directory, NOT a symlink to a directory
3020 * return FALSE if "name" is not a directory
3021 * return FALSE for error
3022 */
3023 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003024mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003025{
3026 struct stat statb;
3027
3028 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3029 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003030 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003031 return FALSE;
3032#ifdef _POSIX_SOURCE
3033 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3034#else
3035 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3036#endif
3037}
3038
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003039static int executable_file(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
3041/*
3042 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3043 */
3044 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003045executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
3047 struct stat st;
3048
3049 if (stat((char *)name, &st))
3050 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003051#ifdef VMS
3052 /* Like on Unix system file can have executable rights but not necessarily
3053 * be an executable, but on Unix is not a default for an ordianry file to
3054 * have an executable flag - on VMS it is in most cases.
3055 * Therefore, this check does not have any sense - let keep us to the
3056 * conventions instead:
3057 * *.COM and *.EXE files are the executables - the rest are not. This is
3058 * not ideal but better then it was.
3059 */
3060 int vms_executable = 0;
3061 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3062 {
3063 if (strstr(vms_tolower((char*)name),".exe") != NULL
3064 || strstr(vms_tolower((char*)name),".com")!= NULL)
3065 vms_executable = 1;
3066 }
3067 return vms_executable;
3068#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071}
3072
3073/*
3074 * Return 1 if "name" can be found in $PATH and executed, 0 if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003075 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 * Return -1 if unknown.
3077 */
3078 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003079mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 char_u *buf;
3082 char_u *p, *e;
3083 int retval;
3084
Bram Moolenaarb5971142015-03-21 17:32:19 +01003085 /* When "use_path" is false and if it's an absolute or relative path don't
3086 * need to use $PATH. */
3087 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3088 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003089 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003090 /* There must be a path separator, files in the current directory
3091 * can't be executed. */
3092 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003093 {
3094 if (path != NULL)
3095 {
3096 if (name[0] == '.')
3097 *path = FullName_save(name, TRUE);
3098 else
3099 *path = vim_strsave(name);
3100 }
3101 return TRUE;
3102 }
3103 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105
3106 p = (char_u *)getenv("PATH");
3107 if (p == NULL || *p == NUL)
3108 return -1;
3109 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3110 if (buf == NULL)
3111 return -1;
3112
3113 /*
3114 * Walk through all entries in $PATH to check if "name" exists there and
3115 * is an executable file.
3116 */
3117 for (;;)
3118 {
3119 e = (char_u *)strchr((char *)p, ':');
3120 if (e == NULL)
3121 e = p + STRLEN(p);
3122 if (e - p <= 1) /* empty entry means current dir */
3123 STRCPY(buf, "./");
3124 else
3125 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003126 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 add_pathsep(buf);
3128 }
3129 STRCAT(buf, name);
3130 retval = executable_file(buf);
3131 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003132 {
3133 if (path != NULL)
3134 {
3135 if (buf[0] == '.')
3136 *path = FullName_save(buf, TRUE);
3137 else
3138 *path = vim_strsave(buf);
3139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
3143 if (*e != ':')
3144 break;
3145 p = e + 1;
3146 }
3147
3148 vim_free(buf);
3149 return retval;
3150}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152/*
3153 * Check what "name" is:
3154 * NODE_NORMAL: file or directory (or doesn't exist)
3155 * NODE_WRITABLE: writable device, socket, fifo, etc.
3156 * NODE_OTHER: non-writable things
3157 */
3158 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003159mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160{
3161 struct stat st;
3162
3163 if (stat((char *)name, &st))
3164 return NODE_NORMAL;
3165 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3166 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3168 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 /* Everything else is writable? */
3170 return NODE_WRITABLE;
3171}
3172
3173 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003174mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175{
3176#ifdef HAVE_CHECK_STACK_GROWTH
3177 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 check_stack_growth((char *)&i);
3180
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003181# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 get_stack_limit();
3183# endif
3184
3185#endif
3186
3187 /*
3188 * Setup an alternative stack for signals. Helps to catch signals when
3189 * running out of stack space.
3190 * Use of sigaltstack() is preferred, it's more portable.
3191 * Ignore any errors.
3192 */
3193#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003194 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 init_signal_stack();
3196#endif
3197}
3198
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003199#if defined(EXITFREE) || defined(PROTO)
3200 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003201mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003202{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003203# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3204 if (clip_star.owned)
3205 clip_lose_selection(&clip_star);
3206 if (clip_plus.owned)
3207 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003208# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003209# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003210 if (xterm_Shell != (Widget)0)
3211 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003212# ifndef LESSTIF_VERSION
3213 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003214 if (xterm_dpy != NULL)
3215 XtCloseDisplay(xterm_dpy);
3216 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003217 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003218 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003219# ifdef FEAT_X11
3220 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3221# endif
3222 }
3223# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003224# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003225# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003226 if (x11_display != NULL
3227# ifdef FEAT_XCLIPBOARD
3228 && x11_display != xterm_dpy
3229# endif
3230 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003231 XCloseDisplay(x11_display);
3232# endif
3233# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
3234 vim_free(signal_stack);
3235 signal_stack = NULL;
3236# endif
3237# ifdef FEAT_TITLE
3238 vim_free(oldtitle);
3239 vim_free(oldicon);
3240# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003241}
3242#endif
3243
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003244static void exit_scroll(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
3246/*
3247 * Output a newline when exiting.
3248 * Make sure the newline goes to the same stream as the text.
3249 */
3250 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003251exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003253 if (silent_mode)
3254 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 if (newline_on_exit || msg_didout)
3256 {
3257 if (msg_use_printf())
3258 {
3259 if (info_message)
3260 mch_msg("\n");
3261 else
3262 mch_errmsg("\r\n");
3263 }
3264 else
3265 out_char('\n');
3266 }
3267 else
3268 {
3269 restore_cterm_colors(); /* get original colors back */
3270 msg_clr_eos_force(); /* clear the rest of the display */
3271 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3272 }
3273}
3274
3275 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003276mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
3278 exiting = TRUE;
3279
3280#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3281 x11_export_final_selection();
3282#endif
3283
3284#ifdef FEAT_GUI
3285 if (!gui.in_use)
3286#endif
3287 {
3288 settmode(TMODE_COOK);
3289#ifdef FEAT_TITLE
3290 mch_restore_title(3); /* restore xterm title and icon name */
3291#endif
3292 /*
3293 * When t_ti is not empty but it doesn't cause swapping terminal
3294 * pages, need to output a newline when msg_didout is set. But when
3295 * t_ti does swap pages it should not go to the shell page. Do this
3296 * before stoptermcap().
3297 */
3298 if (swapping_screen() && !newline_on_exit)
3299 exit_scroll();
3300
3301 /* Stop termcap: May need to check for T_CRV response, which
3302 * requires RAW mode. */
3303 stoptermcap();
3304
3305 /*
3306 * A newline is only required after a message in the alternate screen.
3307 * This is set to TRUE by wait_return().
3308 */
3309 if (!swapping_screen() || newline_on_exit)
3310 exit_scroll();
3311
3312 /* Cursor may have been switched off without calling starttermcap()
3313 * when doing "vim -u vimrc" and vimrc contains ":q". */
3314 if (full_screen)
3315 cursor_on();
3316 }
3317 out_flush();
3318 ml_close_all(TRUE); /* remove all memfiles */
3319 may_core_dump();
3320#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 gui_exit(r);
3323#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003324
Bram Moolenaar56718732006-03-15 22:53:57 +00003325#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003326 mac_conv_cleanup();
3327#endif
3328
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#ifdef __QNX__
3330 /* A core dump won't be created if the signal handler
3331 * doesn't return, so we can't call exit() */
3332 if (deadly_signal != 0)
3333 return;
3334#endif
3335
Bram Moolenaar009b2592004-10-24 19:18:58 +00003336#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003337 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003338#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003339
3340#ifdef EXITFREE
3341 free_all_mem();
3342#endif
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 exit(r);
3345}
3346
3347 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003348may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
3350 if (deadly_signal != 0)
3351 {
3352 signal(deadly_signal, SIG_DFL);
3353 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3354 }
3355}
3356
3357#ifndef VMS
3358
3359 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003360mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
3362 static int first = TRUE;
3363
3364 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3365#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3366 /*
3367 * for "new" tty systems
3368 */
3369# ifdef HAVE_TERMIOS_H
3370 static struct termios told;
3371 struct termios tnew;
3372# else
3373 static struct termio told;
3374 struct termio tnew;
3375# endif
3376
3377 if (first)
3378 {
3379 first = FALSE;
3380# if defined(HAVE_TERMIOS_H)
3381 tcgetattr(read_cmd_fd, &told);
3382# else
3383 ioctl(read_cmd_fd, TCGETA, &told);
3384# endif
3385 }
3386
3387 tnew = told;
3388 if (tmode == TMODE_RAW)
3389 {
3390 /*
3391 * ~ICRNL enables typing ^V^M
3392 */
3393 tnew.c_iflag &= ~ICRNL;
3394 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3395# if defined(IEXTEN) && !defined(__MINT__)
3396 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3397 /* but it breaks function keys on MINT */
3398# endif
3399 );
3400# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3401 tnew.c_oflag &= ~ONLCR;
3402# endif
3403 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3404 tnew.c_cc[VTIME] = 0; /* don't wait */
3405 }
3406 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003407 {
3408 /* Also reset ICANON here, otherwise on Solaris select() won't see
3409 * typeahead characters. */
3410 tnew.c_lflag &= ~(ICANON | ECHO);
3411 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3412 tnew.c_cc[VTIME] = 0; /* don't wait */
3413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
3415# if defined(HAVE_TERMIOS_H)
3416 {
3417 int n = 10;
3418
3419 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3420 * few times. */
3421 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3422 && errno == EINTR && n > 0)
3423 --n;
3424 }
3425# else
3426 ioctl(read_cmd_fd, TCSETA, &tnew);
3427# endif
3428
3429#else
3430
3431 /*
3432 * for "old" tty systems
3433 */
3434# ifndef TIOCSETN
3435# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3436# endif
3437 static struct sgttyb ttybold;
3438 struct sgttyb ttybnew;
3439
3440 if (first)
3441 {
3442 first = FALSE;
3443 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3444 }
3445
3446 ttybnew = ttybold;
3447 if (tmode == TMODE_RAW)
3448 {
3449 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3450 ttybnew.sg_flags |= RAW;
3451 }
3452 else if (tmode == TMODE_SLEEP)
3453 ttybnew.sg_flags &= ~(ECHO);
3454 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3455#endif
3456 curr_tmode = tmode;
3457}
3458
3459/*
3460 * Try to get the code for "t_kb" from the stty setting
3461 *
3462 * Even if termcap claims a backspace key, the user's setting *should*
3463 * prevail. stty knows more about reality than termcap does, and if
3464 * somebody's usual erase key is DEL (which, for most BSD users, it will
3465 * be), they're going to get really annoyed if their erase key starts
3466 * doing forward deletes for no reason. (Eric Fischer)
3467 */
3468 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003469get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470{
3471 char_u buf[2];
3472 char_u *p;
3473
3474 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3475#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3476 /* for "new" tty systems */
3477# ifdef HAVE_TERMIOS_H
3478 struct termios keys;
3479# else
3480 struct termio keys;
3481# endif
3482
3483# if defined(HAVE_TERMIOS_H)
3484 if (tcgetattr(read_cmd_fd, &keys) != -1)
3485# else
3486 if (ioctl(read_cmd_fd, TCGETA, &keys) != -1)
3487# endif
3488 {
3489 buf[0] = keys.c_cc[VERASE];
3490 intr_char = keys.c_cc[VINTR];
3491#else
3492 /* for "old" tty systems */
3493 struct sgttyb keys;
3494
3495 if (ioctl(read_cmd_fd, TIOCGETP, &keys) != -1)
3496 {
3497 buf[0] = keys.sg_erase;
3498 intr_char = keys.sg_kill;
3499#endif
3500 buf[1] = NUL;
3501 add_termcode((char_u *)"kb", buf, FALSE);
3502
3503 /*
3504 * If <BS> and <DEL> are now the same, redefine <DEL>.
3505 */
3506 p = find_termcode((char_u *)"kD");
3507 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3508 do_fixdel(NULL);
3509 }
3510#if 0
3511 } /* to keep cindent happy */
3512#endif
3513}
3514
3515#endif /* VMS */
3516
3517#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
3518/*
3519 * Set mouse clicks on or off.
3520 */
3521 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003522mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523{
3524 static int ison = FALSE;
3525 int xterm_mouse_vers;
3526
3527 if (on == ison) /* return quickly if nothing to do */
3528 return;
3529
3530 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003531
3532# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003533 if (ttym_flags == TTYM_URXVT)
3534 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003535 out_str_nf((char_u *)
3536 (on
3537 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3538 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
3539 ison = on;
3540 }
3541# endif
3542
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003543# ifdef FEAT_MOUSE_SGR
3544 if (ttym_flags == TTYM_SGR)
3545 {
3546 out_str_nf((char_u *)
3547 (on
3548 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3549 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
3550 ison = on;
3551 }
3552# endif
3553
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 if (xterm_mouse_vers > 0)
3555 {
3556 if (on) /* enable mouse events, use mouse tracking if available */
3557 out_str_nf((char_u *)
3558 (xterm_mouse_vers > 1
3559 ? IF_EB("\033[?1002h", ESC_STR "[?1002h")
3560 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3561 else /* disable mouse events, could probably always send the same */
3562 out_str_nf((char_u *)
3563 (xterm_mouse_vers > 1
3564 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3565 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
3566 ison = on;
3567 }
3568
3569# ifdef FEAT_MOUSE_DEC
3570 else if (ttym_flags == TTYM_DEC)
3571 {
3572 if (on) /* enable mouse events */
3573 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3574 else /* disable mouse events */
3575 out_str_nf((char_u *)"\033['z");
3576 ison = on;
3577 }
3578# endif
3579
3580# ifdef FEAT_MOUSE_GPM
3581 else
3582 {
3583 if (on)
3584 {
3585 if (gpm_open())
3586 ison = TRUE;
3587 }
3588 else
3589 {
3590 gpm_close();
3591 ison = FALSE;
3592 }
3593 }
3594# endif
3595
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003596# ifdef FEAT_SYSMOUSE
3597 else
3598 {
3599 if (on)
3600 {
3601 if (sysmouse_open() == OK)
3602 ison = TRUE;
3603 }
3604 else
3605 {
3606 sysmouse_close();
3607 ison = FALSE;
3608 }
3609 }
3610# endif
3611
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612# ifdef FEAT_MOUSE_JSB
3613 else
3614 {
3615 if (on)
3616 {
3617 /* D - Enable Mouse up/down messages
3618 * L - Enable Left Button Reporting
3619 * M - Enable Middle Button Reporting
3620 * R - Enable Right Button Reporting
3621 * K - Enable SHIFT and CTRL key Reporting
3622 * + - Enable Advanced messaging of mouse moves and up/down messages
3623 * Q - Quiet No Ack
3624 * # - Numeric value of mouse pointer required
3625 * 0 = Multiview 2000 cursor, used as standard
3626 * 1 = Windows Arrow
3627 * 2 = Windows I Beam
3628 * 3 = Windows Hour Glass
3629 * 4 = Windows Cross Hair
3630 * 5 = Windows UP Arrow
3631 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003632# ifdef JSBTERM_MOUSE_NONADVANCED
3633 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3635 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003636# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3638 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 ison = TRUE;
3641 }
3642 else
3643 {
3644 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3645 ESC_STR "[0~ZwQ" ESC_STR "\\"));
3646 ison = FALSE;
3647 }
3648 }
3649# endif
3650# ifdef FEAT_MOUSE_PTERM
3651 else
3652 {
3653 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3654 if (on)
3655 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3656 else
3657 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
3658 ison = on;
3659 }
3660# endif
3661}
3662
3663/*
3664 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3665 */
3666 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003667check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668{
3669# ifdef FEAT_MOUSE_XTERM
3670 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003671# ifdef FEAT_MOUSE_URXVT
3672 && use_xterm_mouse() != 3
3673# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674# ifdef FEAT_GUI
3675 && !gui.in_use
3676# endif
3677 )
3678 {
3679 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003680 ? IF_EB("\233M", CSI_STR "M")
3681 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 if (*p_mouse != NUL)
3683 {
3684 /* force mouse off and maybe on to send possibly new mouse
3685 * activation sequence to the xterm, with(out) drag tracing. */
3686 mch_setmouse(FALSE);
3687 setmouse();
3688 }
3689 }
3690 else
3691 del_mouse_termcode(KS_MOUSE);
3692# endif
3693
3694# ifdef FEAT_MOUSE_GPM
3695 if (!use_xterm_mouse()
3696# ifdef FEAT_GUI
3697 && !gui.in_use
3698# endif
3699 )
3700 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3701# endif
3702
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003703# ifdef FEAT_SYSMOUSE
3704 if (!use_xterm_mouse()
3705# ifdef FEAT_GUI
3706 && !gui.in_use
3707# endif
3708 )
3709 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3710# endif
3711
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003713 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 if (!use_xterm_mouse()
3715# ifdef FEAT_GUI
3716 && !gui.in_use
3717# endif
3718 )
3719 set_mouse_termcode(KS_JSBTERM_MOUSE,
3720 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3721 else
3722 del_mouse_termcode(KS_JSBTERM_MOUSE);
3723# endif
3724
3725# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003726 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 * define it in the GUI or when using an xterm. */
3728 if (!use_xterm_mouse()
3729# ifdef FEAT_GUI
3730 && !gui.in_use
3731# endif
3732 )
3733 set_mouse_termcode(KS_NETTERM_MOUSE,
3734 (char_u *)IF_EB("\033}", ESC_STR "}"));
3735 else
3736 del_mouse_termcode(KS_NETTERM_MOUSE);
3737# endif
3738
3739# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003740 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003741 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742# ifdef FEAT_GUI
3743 && !gui.in_use
3744# endif
3745 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003746 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3747 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 else
3749 del_mouse_termcode(KS_DEC_MOUSE);
3750# endif
3751# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003752 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003753 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754# ifdef FEAT_GUI
3755 && !gui.in_use
3756# endif
3757 )
3758 set_mouse_termcode(KS_PTERM_MOUSE,
3759 (char_u *) IF_EB("\033[", ESC_STR "["));
3760 else
3761 del_mouse_termcode(KS_PTERM_MOUSE);
3762# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003763# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003764 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003765 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003766# ifdef FEAT_GUI
3767 && !gui.in_use
3768# endif
3769 )
3770 {
3771 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3772 ? IF_EB("\233", CSI_STR)
3773 : IF_EB("\033[", ESC_STR "[")));
3774
3775 if (*p_mouse != NUL)
3776 {
3777 mch_setmouse(FALSE);
3778 setmouse();
3779 }
3780 }
3781 else
3782 del_mouse_termcode(KS_URXVT_MOUSE);
3783# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003784# ifdef FEAT_MOUSE_SGR
Bram Moolenaar24dc2302014-05-13 20:19:58 +02003785 /* There is no conflict with xterm mouse */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003786 if (use_xterm_mouse() == 4
3787# ifdef FEAT_GUI
3788 && !gui.in_use
3789# endif
3790 )
3791 {
3792 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3793 ? IF_EB("\233<", CSI_STR "<")
3794 : IF_EB("\033[<", ESC_STR "[<")));
3795
3796 if (*p_mouse != NUL)
3797 {
3798 mch_setmouse(FALSE);
3799 setmouse();
3800 }
3801 }
3802 else
3803 del_mouse_termcode(KS_SGR_MOUSE);
3804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805}
3806#endif
3807
3808/*
3809 * set screen mode, always fails.
3810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003812mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813{
3814 EMSG(_(e_screenmode));
3815 return FAIL;
3816}
3817
3818#ifndef VMS
3819
3820/*
3821 * Try to get the current window size:
3822 * 1. with an ioctl(), most accurate method
3823 * 2. from the environment variables LINES and COLUMNS
3824 * 3. from the termcap
3825 * 4. keep using the old values
3826 * Return OK when size could be determined, FAIL otherwise.
3827 */
3828 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003829mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
3831 long rows = 0;
3832 long columns = 0;
3833 char_u *p;
3834
3835 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 * 1. try using an ioctl. It is the most accurate method.
3837 *
3838 * Try using TIOCGWINSZ first, some systems that have it also define
3839 * TIOCGSIZE but don't have a struct ttysize.
3840 */
3841# ifdef TIOCGWINSZ
3842 {
3843 struct winsize ws;
3844 int fd = 1;
3845
3846 /* When stdout is not a tty, use stdin for the ioctl(). */
3847 if (!isatty(fd) && isatty(read_cmd_fd))
3848 fd = read_cmd_fd;
3849 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
3850 {
3851 columns = ws.ws_col;
3852 rows = ws.ws_row;
3853 }
3854 }
3855# else /* TIOCGWINSZ */
3856# ifdef TIOCGSIZE
3857 {
3858 struct ttysize ts;
3859 int fd = 1;
3860
3861 /* When stdout is not a tty, use stdin for the ioctl(). */
3862 if (!isatty(fd) && isatty(read_cmd_fd))
3863 fd = read_cmd_fd;
3864 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
3865 {
3866 columns = ts.ts_cols;
3867 rows = ts.ts_lines;
3868 }
3869 }
3870# endif /* TIOCGSIZE */
3871# endif /* TIOCGWINSZ */
3872
3873 /*
3874 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003875 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
3876 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003878 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
3880 if ((p = (char_u *)getenv("LINES")))
3881 rows = atoi((char *)p);
3882 if ((p = (char_u *)getenv("COLUMNS")))
3883 columns = atoi((char *)p);
3884 }
3885
3886#ifdef HAVE_TGETENT
3887 /*
3888 * 3. try reading "co" and "li" entries from termcap
3889 */
3890 if (columns == 0 || rows == 0)
3891 getlinecol(&columns, &rows);
3892#endif
3893
3894 /*
3895 * 4. If everything fails, use the old values
3896 */
3897 if (columns <= 0 || rows <= 0)
3898 return FAIL;
3899
3900 Rows = rows;
3901 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02003902 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 return OK;
3904}
3905
3906/*
3907 * Try to set the window size to Rows and Columns.
3908 */
3909 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003910mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911{
3912 if (*T_CWS)
3913 {
3914 /*
3915 * NOTE: if you get an error here that term_set_winsize() is
3916 * undefined, check the output of configure. It could probably not
3917 * find a ncurses, termcap or termlib library.
3918 */
3919 term_set_winsize((int)Rows, (int)Columns);
3920 out_flush();
3921 screen_start(); /* don't know where cursor is now */
3922 }
3923}
3924
3925#endif /* VMS */
3926
3927/*
3928 * Rows and/or Columns has changed.
3929 */
3930 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003931mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932{
3933 /* Nothing to do. */
3934}
3935
Bram Moolenaar205b8862011-09-07 15:04:31 +02003936/*
3937 * Wait for process "child" to end.
3938 * Return "child" if it exited properly, <= 0 on error.
3939 */
3940 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01003941wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02003942{
3943 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02003944 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02003945
3946 while (wait_pid != child)
3947 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02003948 /* When compiled with Python threads are probably used, in which case
3949 * wait() sometimes hangs for no obvious reason. Use waitpid()
3950 * instead and loop (like the GUI). Also needed for other interfaces,
3951 * they might call system(). */
3952# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02003953 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02003954# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02003955 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02003956# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02003957 if (wait_pid == 0)
3958 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02003959 /* Wait for 1 to 10 msec before trying again. */
3960 mch_delay(delay_msec, TRUE);
3961 if (++delay_msec > 10)
3962 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02003963 continue;
3964 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02003965 if (wait_pid <= 0
3966# ifdef ECHILD
3967 && errno == ECHILD
3968# endif
3969 )
3970 break;
3971 }
3972 return wait_pid;
3973}
3974
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003975#if defined(FEAT_JOB_CHANNEL) || !defined(USE_SYSTEM) || defined(PROTO)
Bram Moolenaar72801402016-02-09 11:37:50 +01003976/*
3977 * Parse "cmd" and put the white-separated parts in "argv".
3978 * "argv" is an allocated array with "argc" entries.
3979 * Returns FAIL when out of memory.
3980 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01003981 int
3982mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
3983{
3984 int i;
3985 char_u *p;
3986 int inquote;
3987
3988 /*
3989 * Do this loop twice:
3990 * 1: find number of arguments
3991 * 2: separate them and build argv[]
3992 */
3993 for (i = 0; i < 2; ++i)
3994 {
Bram Moolenaar6231cb82016-04-26 19:42:42 +02003995 p = skipwhite(cmd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003996 inquote = FALSE;
3997 *argc = 0;
3998 for (;;)
3999 {
4000 if (i == 1)
4001 (*argv)[*argc] = (char *)p;
4002 ++*argc;
4003 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
4004 {
4005 if (*p == '"')
4006 inquote = !inquote;
4007 ++p;
4008 }
4009 if (*p == NUL)
4010 break;
4011 if (i == 1)
4012 *p++ = NUL;
4013 p = skipwhite(p);
4014 }
4015 if (*argv == NULL)
4016 {
4017 if (use_shcf)
4018 {
4019 /* Account for possible multiple args in p_shcf. */
4020 p = p_shcf;
4021 for (;;)
4022 {
4023 p = skiptowhite(p);
4024 if (*p == NUL)
4025 break;
4026 ++*argc;
4027 p = skipwhite(p);
4028 }
4029 }
4030
4031 *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *)));
4032 if (*argv == NULL) /* out of memory */
4033 return FAIL;
4034 }
4035 }
4036 return OK;
4037}
4038#endif
4039
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004040#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004041 static void
4042set_child_environment(void)
4043{
4044# ifdef HAVE_SETENV
4045 char envbuf[50];
4046# else
4047 static char envbuf_Rows[20];
4048 static char envbuf_Columns[20];
4049# endif
4050
4051 /* Simulate to have a dumb terminal (for now) */
4052# ifdef HAVE_SETENV
4053 setenv("TERM", "dumb", 1);
4054 sprintf((char *)envbuf, "%ld", Rows);
4055 setenv("ROWS", (char *)envbuf, 1);
4056 sprintf((char *)envbuf, "%ld", Rows);
4057 setenv("LINES", (char *)envbuf, 1);
4058 sprintf((char *)envbuf, "%ld", Columns);
4059 setenv("COLUMNS", (char *)envbuf, 1);
4060# else
4061 /*
4062 * Putenv does not copy the string, it has to remain valid.
4063 * Use a static array to avoid losing allocated memory.
4064 */
4065 putenv("TERM=dumb");
4066 sprintf(envbuf_Rows, "ROWS=%ld", Rows);
4067 putenv(envbuf_Rows);
4068 sprintf(envbuf_Rows, "LINES=%ld", Rows);
4069 putenv(envbuf_Rows);
4070 sprintf(envbuf_Columns, "COLUMNS=%ld", Columns);
4071 putenv(envbuf_Columns);
4072# endif
4073}
4074#endif
4075
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004077mch_call_shell(
4078 char_u *cmd,
4079 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080{
4081#ifdef VMS
4082 char *ifn = NULL;
4083 char *ofn = NULL;
4084#endif
4085 int tmode = cur_tmode;
4086#ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004087 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004088 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
4090 out_flush();
4091
4092 if (options & SHELL_COOKED)
4093 settmode(TMODE_COOK); /* set to normal mode */
4094
Bram Moolenaar62b42182010-09-21 22:09:37 +02004095# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004096 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004097 loose_clipboard();
4098# endif
4099
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (cmd == NULL)
4101 x = system((char *)p_sh);
4102 else
4103 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004104# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 if (ofn = strchr((char *)cmd, '>'))
4106 *ofn++ = '\0';
4107 if (ifn = strchr((char *)cmd, '<'))
4108 {
4109 char *p;
4110
4111 *ifn++ = '\0';
4112 p = strchr(ifn,' '); /* chop off any trailing spaces */
4113 if (p)
4114 *p = '\0';
4115 }
4116 if (ofn)
4117 x = vms_sys((char *)cmd, ofn, ifn);
4118 else
4119 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004120# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 newcmd = lalloc(STRLEN(p_sh)
4122 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4123 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4124 if (newcmd == NULL)
4125 x = 0;
4126 else
4127 {
4128 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4129 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4130 (char *)p_shcf,
4131 (char *)cmd);
4132 x = system((char *)newcmd);
4133 vim_free(newcmd);
4134 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004135# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
4137# ifdef VMS
4138 x = vms_sys_status(x);
4139# endif
4140 if (emsg_silent)
4141 ;
4142 else if (x == 127)
4143 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 else if (x && !(options & SHELL_SILENT))
4145 {
4146 MSG_PUTS(_("\nshell returned "));
4147 msg_outnum((long)x);
4148 msg_putchar('\n');
4149 }
4150
4151 if (tmode == TMODE_RAW)
4152 settmode(TMODE_RAW); /* set to raw mode */
4153# ifdef FEAT_TITLE
4154 resettitle();
4155# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004156# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4157 restore_clipboard();
4158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 return x;
4160
4161#else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */
4162
Bram Moolenaardf177f62005-02-22 08:39:57 +00004163# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4164 127, some shells use that already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
Bram Moolenaar835dc632016-02-07 14:27:38 +01004166 char_u *newcmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004168 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 pid_t wait_pid = 0;
4170# ifdef HAVE_UNION_WAIT
4171 union wait status;
4172# else
4173 int status = -1;
4174# endif
4175 int retval = -1;
4176 char **argv = NULL;
4177 int argc;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004178 char_u *p_shcf_copy = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 int i;
4180 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004182# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 int pty_slave_fd = -1;
4184 char *tty_name;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004185# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004186 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 int fd_fromshell[2];
4188 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004189 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190
Bram Moolenaar62b42182010-09-21 22:09:37 +02004191 newcmd = vim_strsave(p_sh);
4192 if (newcmd == NULL) /* out of memory */
4193 goto error;
4194
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 out_flush();
4196 if (options & SHELL_COOKED)
4197 settmode(TMODE_COOK); /* set to normal mode */
4198
Bram Moolenaar835dc632016-02-07 14:27:38 +01004199 if (mch_parse_cmd(newcmd, TRUE, &argv, &argc) == FAIL)
4200 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004201
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 if (cmd != NULL)
4203 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004204 char_u *s;
4205
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 if (extra_shell_arg != NULL)
4207 argv[argc++] = (char *)extra_shell_arg;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004208
4209 /* Break 'shellcmdflag' into white separated parts. This doesn't
4210 * handle quoted strings, they are very unlikely to appear. */
4211 p_shcf_copy = alloc((unsigned)STRLEN(p_shcf) + 1);
4212 if (p_shcf_copy == NULL) /* out of memory */
4213 goto error;
4214 s = p_shcf_copy;
4215 p = p_shcf;
4216 while (*p != NUL)
4217 {
4218 argv[argc++] = (char *)s;
4219 while (*p && *p != ' ' && *p != TAB)
4220 *s++ = *p++;
4221 *s++ = NUL;
4222 p = skipwhite(p);
4223 }
4224
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 argv[argc++] = (char *)cmd;
4226 }
4227 argv[argc] = NULL;
4228
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004230 * For the GUI, when writing the output into the buffer and when reading
4231 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4232 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004234 if ((options & (SHELL_READ|SHELL_WRITE))
4235# ifdef FEAT_GUI
4236 || (gui.in_use && show_shell_mess)
4237# endif
4238 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004240# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 /*
4242 * Try to open a master pty.
4243 * If this works, open the slave pty.
4244 * If the slave can't be opened, close the master pty.
4245 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004246 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
4248 pty_master_fd = OpenPTY(&tty_name); /* open pty */
Bram Moolenaare70172e2011-08-04 19:36:52 +02004249 if (pty_master_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 {
Bram Moolenaare70172e2011-08-04 19:36:52 +02004251 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4252 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4253 * adding O_NOCTTY always works when defined. */
4254#ifdef O_NOCTTY
4255 pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4256#else
4257 pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4258#endif
4259 if (pty_slave_fd < 0)
4260 {
4261 close(pty_master_fd);
4262 pty_master_fd = -1;
4263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265 }
4266 /*
4267 * If not opening a pty or it didn't work, try using pipes.
4268 */
4269 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 {
4272 pipe_error = (pipe(fd_toshell) < 0);
4273 if (!pipe_error) /* pipe create OK */
4274 {
4275 pipe_error = (pipe(fd_fromshell) < 0);
4276 if (pipe_error) /* pipe create failed */
4277 {
4278 close(fd_toshell[0]);
4279 close(fd_toshell[1]);
4280 }
4281 }
4282 if (pipe_error)
4283 {
4284 MSG_PUTS(_("\nCannot create pipes\n"));
4285 out_flush();
4286 }
4287 }
4288 }
4289
4290 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004292 SIGSET_DECL(curset)
4293
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294# ifdef __BEOS__
4295 beos_cleanup_read_thread();
4296# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004297
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004298 BLOCK_SIGNALS(&curset);
4299 pid = fork(); /* maybe we should use vfork() */
4300 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004302 UNBLOCK_SIGNALS(&curset);
4303
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004305 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004307 || (gui.in_use && show_shell_mess)
4308# endif
4309 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004311# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 if (pty_master_fd >= 0) /* close the pseudo tty */
4313 {
4314 close(pty_master_fd);
4315 close(pty_slave_fd);
4316 }
4317 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 close(fd_toshell[0]);
4321 close(fd_toshell[1]);
4322 close(fd_fromshell[0]);
4323 close(fd_fromshell[1]);
4324 }
4325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 }
4327 else if (pid == 0) /* child */
4328 {
4329 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004330 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
4332 if (!show_shell_mess || (options & SHELL_EXPAND))
4333 {
4334 int fd;
4335
4336 /*
4337 * Don't want to show any message from the shell. Can't just
4338 * close stdout and stderr though, because some systems will
4339 * break if you try to write to them after that, so we must
4340 * use dup() to replace them with something else -- webb
4341 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4342 * waiting for input.
4343 */
4344 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4345 fclose(stdin);
4346 fclose(stdout);
4347 fclose(stderr);
4348
4349 /*
4350 * If any of these open()'s and dup()'s fail, we just continue
4351 * anyway. It's not fatal, and on most systems it will make
4352 * no difference at all. On a few it will cause the execvp()
4353 * to exit with a non-zero status even when the completion
4354 * could be done, which is nothing too serious. If the open()
4355 * or dup() failed we'd just do the same thing ourselves
4356 * anyway -- webb
4357 */
4358 if (fd >= 0)
4359 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004360 ignored = dup(fd); /* To replace stdin (fd 0) */
4361 ignored = dup(fd); /* To replace stdout (fd 1) */
4362 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
4364 /* Don't need this now that we've duplicated it */
4365 close(fd);
4366 }
4367 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004368 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004370 || gui.in_use
4371# endif
4372 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 {
4374
Bram Moolenaardf177f62005-02-22 08:39:57 +00004375# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004376 /* Create our own process group, so that the child and all its
4377 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004378 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004379 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004380 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004381 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004382# if defined(SIGHUP)
4383 /* When doing "!xterm&" and 'shell' is bash: the shell
4384 * will exit and send SIGHUP to all processes in its
4385 * group, killing the just started process. Ignore SIGHUP
4386 * to avoid that. (suggested by Simon Schubert)
4387 */
4388 signal(SIGHUP, SIG_IGN);
4389# endif
4390 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004391# endif
4392# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004393 if (pty_slave_fd >= 0)
4394 {
4395 /* push stream discipline modules */
4396 if (options & SHELL_COOKED)
4397 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004399 /* Try to become controlling tty (probably doesn't work,
4400 * unless run by root) */
4401 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004403 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004404# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004405 set_child_environment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406
Bram Moolenaara5792f52005-11-23 21:25:05 +00004407 /*
4408 * stderr is only redirected when using the GUI, so that a
4409 * program like gpg can still access the terminal to get a
4410 * passphrase using stderr.
4411 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004412# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 if (pty_master_fd >= 0)
4414 {
4415 close(pty_master_fd); /* close master side of pty */
4416
4417 /* set up stdin/stdout/stderr for the child */
4418 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004419 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004421 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004422 if (gui.in_use)
4423 {
4424 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004425 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
4428 close(pty_slave_fd); /* has been dupped, close it now */
4429 }
4430 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 {
4433 /* set up stdin for the child */
4434 close(fd_toshell[1]);
4435 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004436 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 close(fd_toshell[0]);
4438
4439 /* set up stdout for the child */
4440 close(fd_fromshell[0]);
4441 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004442 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 close(fd_fromshell[1]);
4444
Bram Moolenaara5792f52005-11-23 21:25:05 +00004445# ifdef FEAT_GUI
4446 if (gui.in_use)
4447 {
4448 /* set up stderr for the child */
4449 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004450 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004451 }
4452# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 }
4454 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004455
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 /*
4457 * There is no type cast for the argv, because the type may be
4458 * different on different machines. This may cause a warning
4459 * message with strict compilers, don't worry about it.
4460 * Call _exit() instead of exit() to avoid closing the connection
4461 * to the X server (esp. with GTK, which uses atexit()).
4462 */
4463 execvp(argv[0], argv);
4464 _exit(EXEC_FAILED); /* exec failed, return failure code */
4465 }
4466 else /* parent */
4467 {
4468 /*
4469 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004470 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 */
4472 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004473 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004474 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004475# ifdef FEAT_JOB_CHANNEL
4476 ++dont_check_job_ended;
4477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 /*
4479 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004480 * This is also used to pipe stdin/stdout to/from the external
4481 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004483 if ((options & (SHELL_READ|SHELL_WRITE))
4484# ifdef FEAT_GUI
4485 || (gui.in_use && show_shell_mess)
4486# endif
4487 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004489# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004491# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004493# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4495 int ta_len = 0; /* valid bytes in ta_buf[] */
4496 int len;
4497 int p_more_save;
4498 int old_State;
4499 int c;
4500 int toshell_fd;
4501 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004502 garray_T ga;
4503 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004504# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4505 struct timeval start_tv;
4506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507
Bram Moolenaardf177f62005-02-22 08:39:57 +00004508# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 if (pty_master_fd >= 0)
4510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 fromshell_fd = pty_master_fd;
4512 toshell_fd = dup(pty_master_fd);
4513 }
4514 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
4517 close(fd_toshell[0]);
4518 close(fd_fromshell[1]);
4519 toshell_fd = fd_toshell[1];
4520 fromshell_fd = fd_fromshell[0];
4521 }
4522
4523 /*
4524 * Write to the child if there are typed characters.
4525 * Read from the child if there are characters available.
4526 * Repeat the reading a few times if more characters are
4527 * available. Need to check for typed keys now and then, but
4528 * not too often (delays when no chars are available).
4529 * This loop is quit if no characters can be read from the pty
4530 * (WaitForChar detected special condition), or there are no
4531 * characters available and the child has exited.
4532 * Only check if the child has exited when there is no more
4533 * output. The child may exit before all the output has
4534 * been printed.
4535 *
4536 * Currently this busy loops!
4537 * This can probably dead-lock when the write blocks!
4538 */
4539 p_more_save = p_more;
4540 p_more = FALSE;
4541 old_State = State;
4542 State = EXTERNCMD; /* don't redraw at window resize */
4543
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004544 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004545 {
4546 /* Fork a process that will write the lines to the
4547 * external program. */
4548 if ((wpid = fork()) == -1)
4549 {
4550 MSG_PUTS(_("\nCannot fork\n"));
4551 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004552 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004553 {
4554 linenr_T lnum = curbuf->b_op_start.lnum;
4555 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004556 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004557 size_t l;
4558
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004559 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004560 for (;;)
4561 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004562 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004563 if (l == 0)
4564 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004565 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004566 /* NL -> NUL translation */
4567 len = write(toshell_fd, "", (size_t)1);
4568 else
4569 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004570 char_u *s = vim_strchr(lp + written, NL);
4571
Bram Moolenaar89d40322006-08-29 15:30:07 +00004572 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004573 s == NULL ? l
4574 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004575 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004576 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004577 {
4578 /* Finished a line, add a NL, unless this line
4579 * should not have one. */
4580 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004581 || (!curbuf->b_p_bin
4582 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004583 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004584 && (lnum !=
4585 curbuf->b_ml.ml_line_count
4586 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004587 ignored = write(toshell_fd, "\n",
4588 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004589 ++lnum;
4590 if (lnum > curbuf->b_op_end.lnum)
4591 {
4592 /* finished all the lines, close pipe */
4593 close(toshell_fd);
4594 toshell_fd = -1;
4595 break;
4596 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004597 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004598 written = 0;
4599 }
4600 else if (len > 0)
4601 written += len;
4602 }
4603 _exit(0);
4604 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004605 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004606 {
4607 close(toshell_fd);
4608 toshell_fd = -1;
4609 }
4610 }
4611
4612 if (options & SHELL_READ)
4613 ga_init2(&ga, 1, BUFLEN);
4614
4615 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004616# ifdef ELAPSED_FUNC
4617 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 for (;;)
4620 {
4621 /*
4622 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004623 * if there are any.
4624 * Don't do this if we are expanding wild cards (would eat
4625 * typeahead).
4626 * Don't do this when filtering and terminal is in cooked
4627 * mode, the shell command will handle the I/O. Avoids
4628 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004629 * Don't get characters when the child has already
4630 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004631 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004632 * while (noread_cnt > 4), avoids that ":r !ls" eats
4633 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 */
4635 len = 0;
4636 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004637 && ((options &
4638 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4639 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004640# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004641 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004642# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004643 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004644 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004645 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004647 if (ta_len == 0)
4648 {
4649 /* Get extra characters when we don't have any.
4650 * Reset the counter and timer. */
4651 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004652# ifdef ELAPSED_FUNC
4653 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004654# endif
4655 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4656 }
4657 if (ta_len > 0 || len > 0)
4658 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 /*
4660 * For pipes:
4661 * Check for CTRL-C: send interrupt signal to child.
4662 * Check for CTRL-D: EOF, close pipe to child.
4663 */
4664 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4665 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004666# ifdef SIGINT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 /*
4668 * Send SIGINT to the child's group or all
4669 * processes in our group.
4670 */
4671 if (ta_buf[ta_len] == Ctrl_C
4672 || ta_buf[ta_len] == intr_char)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004673 {
4674# ifdef HAVE_SETSID
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 kill(-pid, SIGINT);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004676# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 kill(0, SIGINT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678# endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00004679 if (wpid > 0)
4680 kill(wpid, SIGINT);
4681 }
4682# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 if (pty_master_fd < 0 && toshell_fd >= 0
4684 && ta_buf[ta_len] == Ctrl_D)
4685 {
4686 close(toshell_fd);
4687 toshell_fd = -1;
4688 }
4689 }
4690
4691 /* replace K_BS by <BS> and K_DEL by <DEL> */
4692 for (i = ta_len; i < ta_len + len; ++i)
4693 {
4694 if (ta_buf[i] == CSI && len - i > 2)
4695 {
4696 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4697 if (c == K_DEL || c == K_KDEL || c == K_BS)
4698 {
4699 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4700 (size_t)(len - i - 2));
4701 if (c == K_DEL || c == K_KDEL)
4702 ta_buf[i] = DEL;
4703 else
4704 ta_buf[i] = Ctrl_H;
4705 len -= 2;
4706 }
4707 }
4708 else if (ta_buf[i] == '\r')
4709 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00004710# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00004712 i += (*mb_ptr2len_len)(ta_buf + i,
4713 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 }
4716
4717 /*
4718 * For pipes: echo the typed characters.
4719 * For a pty this does not seem to work.
4720 */
4721 if (pty_master_fd < 0)
4722 {
4723 for (i = ta_len; i < ta_len + len; ++i)
4724 {
4725 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4726 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004727# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 else if (has_mbyte)
4729 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004730 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731
4732 msg_outtrans_len(ta_buf + i, l);
4733 i += l - 1;
4734 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004735# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 else
4737 msg_outtrans_len(ta_buf + i, 1);
4738 }
4739 windgoto(msg_row, msg_col);
4740 out_flush();
4741 }
4742
4743 ta_len += len;
4744
4745 /*
4746 * Write the characters to the child, unless EOF has
4747 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004748 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004749 * When writing buffer lines, drop the typed
4750 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004752 if (options & SHELL_WRITE)
4753 ta_len = 0;
4754 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
4756 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
4757 if (len > 0)
4758 {
4759 ta_len -= len;
4760 mch_memmove(ta_buf, ta_buf + len, ta_len);
4761 }
4762 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
4765
Bram Moolenaardf177f62005-02-22 08:39:57 +00004766 if (got_int)
4767 {
4768 /* CTRL-C sends a signal to the child, we ignore it
4769 * ourselves */
4770# ifdef HAVE_SETSID
4771 kill(-pid, SIGINT);
4772# else
4773 kill(0, SIGINT);
4774# endif
4775 if (wpid > 0)
4776 kill(wpid, SIGINT);
4777 got_int = FALSE;
4778 }
4779
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 /*
4781 * Check if the child has any characters to be printed.
4782 * Read them and write them to our window. Repeat this as
4783 * long as there is something to do, avoid the 10ms wait
4784 * for mch_inchar(), or sending typeahead characters to
4785 * the external process.
4786 * TODO: This should handle escape sequences, compatible
4787 * to some terminal (vt52?).
4788 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004789 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01004790 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004792 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00004793# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004795# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00004797# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 );
4799 if (len <= 0) /* end of file or error */
4800 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004801
4802 noread_cnt = 0;
4803 if (options & SHELL_READ)
4804 {
4805 /* Do NUL -> NL translation, append NL separated
4806 * lines to the current buffer. */
4807 for (i = 0; i < len; ++i)
4808 {
4809 if (buffer[i] == NL)
4810 append_ga_line(&ga);
4811 else if (buffer[i] == NUL)
4812 ga_append(&ga, NL);
4813 else
4814 ga_append(&ga, buffer[i]);
4815 }
4816 }
4817# ifdef FEAT_MBYTE
4818 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 {
4820 int l;
4821
Bram Moolenaardf177f62005-02-22 08:39:57 +00004822 len += buffer_off;
4823 buffer[len] = NUL;
4824
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 /* Check if the last character in buffer[] is
4826 * incomplete, keep these bytes for the next
4827 * round. */
4828 for (p = buffer; p < buffer + len; p += l)
4829 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004830 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 if (l == 0)
4832 l = 1; /* NUL byte? */
4833 else if (MB_BYTE2LEN(*p) != l)
4834 break;
4835 }
4836 if (p == buffer) /* no complete character */
4837 {
4838 /* avoid getting stuck at an illegal byte */
4839 if (len >= 12)
4840 ++p;
4841 else
4842 {
4843 buffer_off = len;
4844 continue;
4845 }
4846 }
4847 c = *p;
4848 *p = NUL;
4849 msg_puts(buffer);
4850 if (p < buffer + len)
4851 {
4852 *p = c;
4853 buffer_off = (buffer + len) - p;
4854 mch_memmove(buffer, p, buffer_off);
4855 continue;
4856 }
4857 buffer_off = 0;
4858 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004859# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
4862 buffer[len] = NUL;
4863 msg_puts(buffer);
4864 }
4865
4866 windgoto(msg_row, msg_col);
4867 cursor_on();
4868 out_flush();
4869 if (got_int)
4870 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004871
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004872# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004873 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004874 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004875 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004876
4877 /* Avoid that we keep looping here without
4878 * checking for a CTRL-C for a long time. Don't
4879 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004880 if (msec > 2000)
4881 {
4882 noread_cnt = 5;
4883 break;
4884 }
4885 }
4886# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004889 /* If we already detected the child has finished, continue
4890 * reading output for a short while. Some text may be
4891 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004892 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004893 {
4894 if (noread_cnt < 5)
4895 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004896 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004897 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004898
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 /*
4900 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004901 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004903# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004904 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004905# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004907# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
4909 || (wait_pid == pid && WIFEXITED(status)))
4910 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004911 /* Don't break the loop yet, try reading more
4912 * characters from "fromshell_fd" first. When using
4913 * pipes there might still be something to read and
4914 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004917 else
4918 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004919
Bram Moolenaar95a51352013-03-21 22:53:50 +01004920# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004921 /* Handle any X events, e.g. serving the clipboard. */
4922 clip_update();
4923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
4925finished:
4926 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004927 if (options & SHELL_READ)
4928 {
4929 if (ga.ga_len > 0)
4930 {
4931 append_ga_line(&ga);
4932 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004933 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004934 }
4935 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004936 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004937 ga_clear(&ga);
4938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 /*
4941 * Give all typeahead that wasn't used back to ui_inchar().
4942 */
4943 if (ta_len)
4944 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 State = old_State;
4946 if (toshell_fd >= 0)
4947 close(toshell_fd);
4948 close(fromshell_fd);
4949 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01004950# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004951 else
4952 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004953 long delay_msec = 1;
4954
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004955 /*
4956 * Similar to the loop above, but only handle X events, no
4957 * I/O.
4958 */
4959 for (;;)
4960 {
4961 if (got_int)
4962 {
4963 /* CTRL-C sends a signal to the child, we ignore it
4964 * ourselves */
4965# ifdef HAVE_SETSID
4966 kill(-pid, SIGINT);
4967# else
4968 kill(0, SIGINT);
4969# endif
4970 got_int = FALSE;
4971 }
4972# ifdef __NeXT__
4973 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
4974# else
4975 wait_pid = waitpid(pid, &status, WNOHANG);
4976# endif
4977 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
4978 || (wait_pid == pid && WIFEXITED(status)))
4979 {
4980 wait_pid = pid;
4981 break;
4982 }
4983
4984 /* Handle any X events, e.g. serving the clipboard. */
4985 clip_update();
4986
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004987 /* Wait for 1 to 10 msec. 1 is faster but gives the child
4988 * less time. */
4989 mch_delay(delay_msec, TRUE);
4990 if (++delay_msec > 10)
4991 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004992 }
4993 }
4994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
4996 /*
4997 * Wait until our child has exited.
4998 * Ignore wait() returning pids of other children and returning
4999 * because of some signal like SIGWINCH.
5000 * Don't wait if wait_pid was already set above, indicating the
5001 * child already exited.
5002 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005003 if (wait_pid != pid)
5004 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
Bram Moolenaar624891f2010-10-13 16:22:09 +02005006# ifdef FEAT_GUI
5007 /* Close slave side of pty. Only do this after the child has
5008 * exited, otherwise the child may hang when it tries to write on
5009 * the pty. */
5010 if (pty_master_fd >= 0)
5011 close(pty_slave_fd);
5012# endif
5013
Bram Moolenaardf177f62005-02-22 08:39:57 +00005014 /* Make sure the child that writes to the external program is
5015 * dead. */
5016 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005017 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005018 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005019 wait4pid(wpid, NULL);
5020 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005021
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005022# ifdef FEAT_JOB_CHANNEL
5023 --dont_check_job_ended;
5024# endif
5025
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 /*
5027 * Set to raw mode right now, otherwise a CTRL-C after
5028 * catch_signals() will kill Vim.
5029 */
5030 if (tmode == TMODE_RAW)
5031 settmode(TMODE_RAW);
5032 did_settmode = TRUE;
5033 set_signals();
5034
5035 if (WIFEXITED(status))
5036 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005037 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005039 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
5041 if (retval == EXEC_FAILED)
5042 {
5043 MSG_PUTS(_("\nCannot execute shell "));
5044 msg_outtrans(p_sh);
5045 msg_putchar('\n');
5046 }
5047 else if (!(options & SHELL_SILENT))
5048 {
5049 MSG_PUTS(_("\nshell returned "));
5050 msg_outnum((long)retval);
5051 msg_putchar('\n');
5052 }
5053 }
5054 }
5055 else
5056 MSG_PUTS(_("\nCommand terminated\n"));
5057 }
5058 }
5059 vim_free(argv);
Bram Moolenaarea35ef62011-08-04 22:59:28 +02005060 vim_free(p_shcf_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061
5062error:
5063 if (!did_settmode)
5064 if (tmode == TMODE_RAW)
5065 settmode(TMODE_RAW); /* set to raw mode */
5066# ifdef FEAT_TITLE
5067 resettitle();
5068# endif
5069 vim_free(newcmd);
5070
5071 return retval;
5072
5073#endif /* USE_SYSTEM */
5074}
5075
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005076#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005077 void
Bram Moolenaar802d5592016-03-05 22:05:27 +01005078mch_start_job(char **argv, job_T *job, jobopt_T *options UNUSED)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005079{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005080 pid_t pid;
5081 int fd_in[2]; /* for stdin */
5082 int fd_out[2]; /* for stdout */
5083 int fd_err[2]; /* for stderr */
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005084 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005085 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5086 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5087 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005088 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005089 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5090 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005091 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005092 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005093
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005094 if (use_out_for_err && use_null_for_out)
5095 use_null_for_err = TRUE;
5096
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005097 /* default is to fail */
5098 job->jv_status = JOB_FAILED;
5099 fd_in[0] = -1;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005100 fd_in[1] = -1;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005101 fd_out[0] = -1;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005102 fd_out[1] = -1;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005103 fd_err[0] = -1;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005104 fd_err[1] = -1;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005105
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005106 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005107 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005108 if (use_file_for_in)
5109 {
5110 char_u *fname = options->jo_io_name[PART_IN];
5111
5112 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5113 if (fd_in[0] < 0)
5114 {
5115 EMSG2(_(e_notopen), fname);
5116 goto failed;
5117 }
5118 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005119 else if (!use_null_for_in && pipe(fd_in) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005120 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005121
5122 if (use_file_for_out)
5123 {
5124 char_u *fname = options->jo_io_name[PART_OUT];
5125
5126 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5127 if (fd_out[1] < 0)
5128 {
5129 EMSG2(_(e_notopen), fname);
5130 goto failed;
5131 }
5132 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005133 else if (!use_null_for_out && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005134 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005135
5136 if (use_file_for_err)
5137 {
5138 char_u *fname = options->jo_io_name[PART_ERR];
5139
5140 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5141 if (fd_err[1] < 0)
5142 {
5143 EMSG2(_(e_notopen), fname);
5144 goto failed;
5145 }
5146 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005147 else if (!use_out_for_err && !use_null_for_err && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005148 goto failed;
5149
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005150 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5151 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005152 if (options->jo_set & JO_CHANNEL)
5153 {
5154 channel = options->jo_channel;
5155 if (channel != NULL)
5156 ++channel->ch_refcount;
5157 }
5158 else
5159 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005160 if (channel == NULL)
5161 goto failed;
5162 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005163
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005164 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005165 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005166 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005167 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005168 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005169 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005170 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005171 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005172 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005173 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005174 int null_fd = -1;
5175 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005176
Bram Moolenaar835dc632016-02-07 14:27:38 +01005177 /* child */
5178 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005179 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005180
5181# ifdef HAVE_SETSID
5182 /* Create our own process group, so that the child and all its
5183 * children can be kill()ed. Don't do this when using pipes,
5184 * because stdin is not a tty, we would lose /dev/tty. */
5185 (void)setsid();
5186# endif
5187
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005188 set_child_environment();
5189
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005190 if (use_null_for_in || use_null_for_out || use_null_for_err)
5191 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
5192
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005193 /* set up stdin for the child */
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005194 if (use_null_for_in && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005195 {
5196 close(0);
5197 ignored = dup(null_fd);
5198 }
5199 else
5200 {
5201 if (!use_file_for_in)
5202 close(fd_in[1]);
5203 close(0);
5204 ignored = dup(fd_in[0]);
5205 close(fd_in[0]);
5206 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005207
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005208 /* set up stderr for the child */
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005209 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005210 {
5211 close(2);
5212 ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005213 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005214 }
5215 else if (use_out_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005216 {
5217 close(2);
5218 ignored = dup(fd_out[1]);
5219 }
5220 else
5221 {
Bram Moolenaare98d1212016-03-08 15:37:41 +01005222 if (!use_file_for_err)
5223 close(fd_err[0]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005224 close(2);
5225 ignored = dup(fd_err[1]);
5226 close(fd_err[1]);
5227 }
5228
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005229 /* set up stdout for the child */
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005230 if (use_null_for_out && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005231 {
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005232 close(1);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005233 ignored = dup(null_fd);
5234 }
5235 else
5236 {
5237 if (!use_file_for_out)
5238 close(fd_out[0]);
5239 close(1);
5240 ignored = dup(fd_out[1]);
5241 close(fd_out[1]);
5242 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005243
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005244 if (null_fd >= 0)
5245 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005246
Bram Moolenaar835dc632016-02-07 14:27:38 +01005247 /* See above for type of argv. */
5248 execvp(argv[0], argv);
5249
Bram Moolenaar4694a172016-04-21 14:05:23 +02005250 if (stderr_works)
5251 perror("executing job failed");
Bram Moolenaarcda77642016-06-04 13:32:35 +02005252#ifdef EXITFREE
5253 /* calling free_all_mem() here causes problems. Ignore valgrind
5254 * reporting possibly leaked memory. */
5255#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005256 _exit(EXEC_FAILED); /* exec failed, return failure code */
5257 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005258
5259 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005260 UNBLOCK_SIGNALS(&curset);
5261
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005262 job->jv_pid = pid;
5263 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005264 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005265
5266 /* child stdin, stdout and stderr */
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005267 if (!use_file_for_in && fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005268 close(fd_in[0]);
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005269 if (!use_file_for_out && fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005270 close(fd_out[1]);
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005271 if (!use_out_for_err && !use_file_for_err && fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005272 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005273 if (channel != NULL)
5274 {
5275 channel_set_pipes(channel,
5276 use_file_for_in || use_null_for_in
5277 ? INVALID_FD : fd_in[1],
5278 use_file_for_out || use_null_for_out
5279 ? INVALID_FD : fd_out[0],
5280 use_out_for_err || use_file_for_err || use_null_for_err
Bram Moolenaare98d1212016-03-08 15:37:41 +01005281 ? INVALID_FD : fd_err[0]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005282 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005283 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005284
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005285 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005286 return;
5287
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005288failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005289 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005290 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005291 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005292 if (fd_in[1] >= 0)
5293 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005294 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005295 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005296 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005297 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005298 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005299 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005300 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005301 close(fd_err[1]);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005302}
5303
5304 char *
5305mch_job_status(job_T *job)
5306{
5307# ifdef HAVE_UNION_WAIT
5308 union wait status;
5309# else
5310 int status = -1;
5311# endif
5312 pid_t wait_pid = 0;
5313
5314# ifdef __NeXT__
5315 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5316# else
5317 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5318# endif
5319 if (wait_pid == -1)
5320 {
5321 /* process must have exited */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005322 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005323 }
5324 if (wait_pid == 0)
5325 return "run";
5326 if (WIFEXITED(status))
5327 {
5328 /* LINTED avoid "bitwise operation on signed value" */
5329 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005330 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005331 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005332 if (WIFSIGNALED(status))
5333 {
5334 job->jv_exitval = -1;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005335 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005336 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005337 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005338
5339return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005340 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005341 {
5342 ch_log(job->jv_channel, "Job ended");
5343 job->jv_status = JOB_ENDED;
5344 }
5345 return "dead";
5346}
5347
5348 job_T *
5349mch_detect_ended_job(job_T *job_list)
5350{
5351# ifdef HAVE_UNION_WAIT
5352 union wait status;
5353# else
5354 int status = -1;
5355# endif
5356 pid_t wait_pid = 0;
5357 job_T *job;
5358
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005359# ifndef USE_SYSTEM
5360 /* Do not do this when waiting for a shell command to finish, we would get
5361 * the exit value here (and discard it), the exit value obtained there
5362 * would then be wrong. */
5363 if (dont_check_job_ended > 0)
5364 return NULL;
5365# endif
5366
Bram Moolenaar97792de2016-10-15 18:36:49 +02005367# ifdef __NeXT__
5368 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5369# else
5370 wait_pid = waitpid(-1, &status, WNOHANG);
5371# endif
5372 if (wait_pid <= 0)
5373 /* no process ended */
5374 return NULL;
5375 for (job = job_list; job != NULL; job = job->jv_next)
5376 {
5377 if (job->jv_pid == wait_pid)
5378 {
5379 if (WIFEXITED(status))
5380 /* LINTED avoid "bitwise operation on signed value" */
5381 job->jv_exitval = WEXITSTATUS(status);
5382 else if (WIFSIGNALED(status))
5383 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005384 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005385 {
5386 ch_log(job->jv_channel, "Job ended");
5387 job->jv_status = JOB_ENDED;
5388 }
5389 return job;
5390 }
5391 }
5392 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005393}
5394
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005395/*
5396 * Send a (deadly) signal to "job".
5397 * Return FAIL if "how" is not a valid name.
5398 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005399 int
5400mch_stop_job(job_T *job, char_u *how)
5401{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005402 int sig = -1;
5403 pid_t job_pid;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005404
Bram Moolenaar923d9262016-02-25 20:56:01 +01005405 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005406 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005407 else if (STRCMP(how, "hup") == 0)
5408 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005409 else if (STRCMP(how, "quit") == 0)
5410 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005411 else if (STRCMP(how, "int") == 0)
5412 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005413 else if (STRCMP(how, "kill") == 0)
5414 sig = SIGKILL;
5415 else if (isdigit(*how))
5416 sig = atoi((char *)how);
5417 else
5418 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005419
Bram Moolenaar72801402016-02-09 11:37:50 +01005420 /* TODO: have an option to only kill the process, not the group? */
Bram Moolenaar76467df2016-02-12 19:30:26 +01005421 job_pid = job->jv_pid;
5422 if (job_pid == getpgid(job_pid))
5423 job_pid = -job_pid;
5424
5425 kill(job_pid, sig);
5426
Bram Moolenaar835dc632016-02-07 14:27:38 +01005427 return OK;
5428}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005429
5430/*
5431 * Clear the data related to "job".
5432 */
5433 void
5434mch_clear_job(job_T *job)
5435{
5436 /* call waitpid because child process may become zombie */
5437# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005438 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005439# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005440 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005441# endif
5442}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005443#endif
5444
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445/*
5446 * Check for CTRL-C typed by reading all available characters.
5447 * In cooked mode we should get SIGINT, no need to check.
5448 */
5449 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005450mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005452 if ((curr_tmode == TMODE_RAW || force)
5453 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 fill_input_buf(FALSE);
5455}
5456
5457/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005458 * Wait "msec" msec until a character is available from the mouse, keyboard,
5459 * from inbuf[].
5460 * "msec" == -1 will block forever.
5461 * Invokes timer callbacks when needed.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005462 * "interrupted" (if not NULL) is set to TRUE when no character is available
5463 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005464 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005465 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 */
5467 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02005468WaitForChar(long msec, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005470#ifdef FEAT_TIMERS
5471 long due_time;
5472 long remaining = msec;
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005473 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005474
5475 /* When waiting very briefly don't trigger timers. */
5476 if (msec >= 0 && msec < 10L)
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005477 return WaitForCharOrMouse(msec, NULL);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005478
5479 while (msec < 0 || remaining > 0)
5480 {
5481 /* Trigger timers and then get the time in msec until the next one is
5482 * due. Wait up to that time. */
5483 due_time = check_due_timer();
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005484 if (typebuf.tb_change_cnt != tb_change_cnt)
5485 {
5486 /* timer may have used feedkeys() */
5487 return FALSE;
5488 }
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005489 if (due_time <= 0 || (msec > 0 && due_time > remaining))
5490 due_time = remaining;
Bram Moolenaarcda77642016-06-04 13:32:35 +02005491 if (WaitForCharOrMouse(due_time, interrupted))
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005492 return TRUE;
Bram Moolenaarcda77642016-06-04 13:32:35 +02005493 if (interrupted != NULL && *interrupted)
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005494 /* Nothing available, but need to return so that side effects get
5495 * handled, such as handling a message on a channel. */
5496 return FALSE;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005497 if (msec > 0)
5498 remaining -= due_time;
5499 }
5500 return FALSE;
5501#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005502 return WaitForCharOrMouse(msec, interrupted);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005503#endif
5504}
5505
5506/*
5507 * Wait "msec" msec until a character is available from the mouse or keyboard
5508 * or from inbuf[].
5509 * "msec" == -1 will block forever.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005510 * "interrupted" (if not NULL) is set to TRUE when no character is available
5511 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005512 * When a GUI is being used, this will never get called -- webb
5513 */
5514 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02005515WaitForCharOrMouse(long msec, int *interrupted)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005516{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517#ifdef FEAT_MOUSE_GPM
5518 int gpm_process_wanted;
5519#endif
5520#ifdef FEAT_XCLIPBOARD
5521 int rest;
5522#endif
5523 int avail;
5524
5525 if (input_available()) /* something in inbuf[] */
5526 return 1;
5527
5528#if defined(FEAT_MOUSE_DEC)
5529 /* May need to query the mouse position. */
5530 if (WantQueryMouse)
5531 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005532 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5534 }
5535#endif
5536
5537 /*
5538 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5539 * events. This is a bit complicated, because they might both be defined.
5540 */
5541#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5542# ifdef FEAT_XCLIPBOARD
5543 rest = 0;
5544 if (do_xterm_trace())
5545 rest = msec;
5546# endif
5547 do
5548 {
5549# ifdef FEAT_XCLIPBOARD
5550 if (rest != 0)
5551 {
5552 msec = XT_TRACE_DELAY;
5553 if (rest >= 0 && rest < XT_TRACE_DELAY)
5554 msec = rest;
5555 if (rest >= 0)
5556 rest -= msec;
5557 }
5558# endif
5559# ifdef FEAT_MOUSE_GPM
5560 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005561 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02005562 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005564 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565# endif
5566 if (!avail)
5567 {
5568 if (input_available())
5569 return 1;
5570# ifdef FEAT_XCLIPBOARD
5571 if (rest == 0 || !do_xterm_trace())
5572# endif
5573 break;
5574 }
5575 }
5576 while (FALSE
5577# ifdef FEAT_MOUSE_GPM
5578 || (gpm_process_wanted && mch_gpm_process() == 0)
5579# endif
5580# ifdef FEAT_XCLIPBOARD
5581 || (!avail && rest != 0)
5582# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005583 )
5584 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
5586#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005587 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588#endif
5589 return avail;
5590}
5591
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01005592#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593/*
5594 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005595 * "msec" == 0 will check for characters once.
5596 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005599 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005600 * "interrupted" (if not NULL) is set to TRUE when no character is available
5601 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603#if defined(__BEOS__)
5604 int
5605#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005606 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02005608RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609{
5610 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005611 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005612#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 static int busy = FALSE;
5614
5615 /* May retry getting characters after an event was handled. */
5616# define MAY_LOOP
5617
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005618# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 /* Remember at what time we started, so that we know how much longer we
5620 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02005621 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005622 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02005624 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005625 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626# endif
5627
5628 /* Handle being called recursively. This may happen for the session
5629 * manager stuff, it may save the file, which does a breakcheck. */
5630 if (busy)
5631 return 0;
5632#endif
5633
5634#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005635 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636#endif
5637 {
5638#ifdef MAY_LOOP
5639 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005640# ifdef FEAT_MZSCHEME
5641 int mzquantum_used = FALSE;
5642# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643#endif
5644#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005645 /* each channel may use in, out and err */
5646 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 int nfd;
5648# ifdef FEAT_XCLIPBOARD
5649 int xterm_idx = -1;
5650# endif
5651# ifdef FEAT_MOUSE_GPM
5652 int gpm_idx = -1;
5653# endif
5654# ifdef USE_XSMP
5655 int xsmp_idx = -1;
5656# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005657 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005659# ifdef FEAT_MZSCHEME
5660 mzvim_check_threads();
5661 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5662 {
5663 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
5664 mzquantum_used = TRUE;
5665 }
5666# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 fds[0].fd = fd;
5668 fds[0].events = POLLIN;
5669 nfd = 1;
5670
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005672 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 if (xterm_Shell != (Widget)0)
5674 {
5675 xterm_idx = nfd;
5676 fds[nfd].fd = ConnectionNumber(xterm_dpy);
5677 fds[nfd].events = POLLIN;
5678 nfd++;
5679 }
5680# endif
5681# ifdef FEAT_MOUSE_GPM
5682 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5683 {
5684 gpm_idx = nfd;
5685 fds[nfd].fd = gpm_fd;
5686 fds[nfd].events = POLLIN;
5687 nfd++;
5688 }
5689# endif
5690# ifdef USE_XSMP
5691 if (xsmp_icefd != -1)
5692 {
5693 xsmp_idx = nfd;
5694 fds[nfd].fd = xsmp_icefd;
5695 fds[nfd].events = POLLIN;
5696 nfd++;
5697 }
5698# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005699#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01005700 nfd = channel_poll_setup(nfd, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005701#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02005702 if (interrupted != NULL)
5703 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005705 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005706
5707 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02005708 if (result == 0 && interrupted != NULL && ret > 0)
5709 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005710
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005711# ifdef FEAT_MZSCHEME
5712 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005713 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005714 finished = FALSE;
5715# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717# ifdef FEAT_XCLIPBOARD
5718 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
5719 {
5720 xterm_update(); /* Maybe we should hand out clipboard */
5721 if (--ret == 0 && !input_available())
5722 /* Try again */
5723 finished = FALSE;
5724 }
5725# endif
5726# ifdef FEAT_MOUSE_GPM
5727 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
5728 {
5729 *check_for_gpm = 1;
5730 }
5731# endif
5732# ifdef USE_XSMP
5733 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
5734 {
5735 if (fds[xsmp_idx].revents & POLLIN)
5736 {
5737 busy = TRUE;
5738 xsmp_handle_requests();
5739 busy = FALSE;
5740 }
5741 else if (fds[xsmp_idx].revents & POLLHUP)
5742 {
5743 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005744 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 xsmp_close();
5746 }
5747 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005748 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 }
5750# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005751#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01005752 if (ret > 0)
5753 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756#else /* HAVE_SELECT */
5757
5758 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005759 struct timeval *tvp;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005760 fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005762 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005764# ifdef FEAT_MZSCHEME
5765 mzvim_check_threads();
5766 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5767 {
5768 towait = p_mzq; /* don't wait longer than 'mzquantum' */
5769 mzquantum_used = TRUE;
5770 }
5771# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005773 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005775 tv.tv_sec = towait / 1000;
5776 tv.tv_usec = (towait % 1000) * (1000000/1000);
5777 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005779 else
5780 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
5782 /*
5783 * Select on ready for reading and exceptional condition (end of file).
5784 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005785select_eintr:
5786 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005787 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 FD_ZERO(&efds);
5789 FD_SET(fd, &rfds);
5790# if !defined(__QNX__) && !defined(__CYGWIN32__)
5791 /* For QNX select() always returns 1 if this is set. Why? */
5792 FD_SET(fd, &efds);
5793# endif
5794 maxfd = fd;
5795
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005797 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 if (xterm_Shell != (Widget)0)
5799 {
5800 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
5801 if (maxfd < ConnectionNumber(xterm_dpy))
5802 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005803
5804 /* An event may have already been read but not handled. In
5805 * particulary, XFlush may cause this. */
5806 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 }
5808# endif
5809# ifdef FEAT_MOUSE_GPM
5810 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5811 {
5812 FD_SET(gpm_fd, &rfds);
5813 FD_SET(gpm_fd, &efds);
5814 if (maxfd < gpm_fd)
5815 maxfd = gpm_fd;
5816 }
5817# endif
5818# ifdef USE_XSMP
5819 if (xsmp_icefd != -1)
5820 {
5821 FD_SET(xsmp_icefd, &rfds);
5822 FD_SET(xsmp_icefd, &efds);
5823 if (maxfd < xsmp_icefd)
5824 maxfd = xsmp_icefd;
5825 }
5826# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005827# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005828 maxfd = channel_select_setup(maxfd, &rfds, &wfds);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005829# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02005830 if (interrupted != NULL)
5831 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005833 ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005834 result = ret > 0 && FD_ISSET(fd, &rfds);
5835 if (result)
5836 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02005837 else if (interrupted != NULL && ret > 0)
5838 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005839
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005840# ifdef EINTR
5841 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005842 {
5843 /* Check whether window has been resized, EINTR may be caused by
5844 * SIGWINCH. */
5845 if (do_resize)
5846 handle_resize();
5847
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005848 /* Interrupted by a signal, need to try again. We ignore msec
5849 * here, because we do want to check even after a timeout if
5850 * characters are available. Needed for reading output of an
5851 * external command after the process has finished. */
5852 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005853 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005854# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00005855# ifdef __TANDEM
5856 if (ret == -1 && errno == ENOTSUP)
5857 {
5858 FD_ZERO(&rfds);
5859 FD_ZERO(&efds);
5860 ret = 0;
5861 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005862# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005863# ifdef FEAT_MZSCHEME
5864 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005865 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005866 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867# endif
5868
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869# ifdef FEAT_XCLIPBOARD
5870 if (ret > 0 && xterm_Shell != (Widget)0
5871 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
5872 {
5873 xterm_update(); /* Maybe we should hand out clipboard */
5874 /* continue looping when we only got the X event and the input
5875 * buffer is empty */
5876 if (--ret == 0 && !input_available())
5877 {
5878 /* Try again */
5879 finished = FALSE;
5880 }
5881 }
5882# endif
5883# ifdef FEAT_MOUSE_GPM
5884 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
5885 {
5886 if (FD_ISSET(gpm_fd, &efds))
5887 gpm_close();
5888 else if (FD_ISSET(gpm_fd, &rfds))
5889 *check_for_gpm = 1;
5890 }
5891# endif
5892# ifdef USE_XSMP
5893 if (ret > 0 && xsmp_icefd != -1)
5894 {
5895 if (FD_ISSET(xsmp_icefd, &efds))
5896 {
5897 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005898 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 xsmp_close();
5900 if (--ret == 0)
5901 finished = FALSE; /* keep going if event was only one */
5902 }
5903 else if (FD_ISSET(xsmp_icefd, &rfds))
5904 {
5905 busy = TRUE;
5906 xsmp_handle_requests();
5907 busy = FALSE;
5908 if (--ret == 0)
5909 finished = FALSE; /* keep going if event was only one */
5910 }
5911 }
5912# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005913#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01005914 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005915 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
5918#endif /* HAVE_SELECT */
5919
5920#ifdef MAY_LOOP
5921 if (finished || msec == 0)
5922 break;
5923
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005924# ifdef FEAT_CLIENTSERVER
5925 if (server_waiting())
5926 break;
5927# endif
5928
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 /* We're going to loop around again, find out for how long */
5930 if (msec > 0)
5931 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005932# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005934 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935# else
5936 /* Guess we got interrupted halfway. */
5937 msec = msec / 2;
5938# endif
5939 if (msec <= 0)
5940 break; /* waited long enough */
5941 }
5942#endif
5943 }
5944
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005945 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946}
5947
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949/*
Bram Moolenaar02743632005-07-25 20:42:36 +00005950 * Expand a path into all matching files and/or directories. Handles "*",
5951 * "?", "[a-z]", "**", etc.
5952 * "path" has backslashes before chars that are not to be expanded.
5953 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 */
5955 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005956mch_expandpath(
5957 garray_T *gap,
5958 char_u *path,
5959 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960{
Bram Moolenaar02743632005-07-25 20:42:36 +00005961 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962}
5963#endif
5964
5965/*
5966 * mch_expand_wildcards() - this code does wild-card pattern matching using
5967 * the shell
5968 *
5969 * return OK for success, FAIL for error (you may lose some memory) and put
5970 * an error message in *file.
5971 *
5972 * num_pat is number of input patterns
5973 * pat is array of pointers to input patterns
5974 * num_file is pointer to number of matched file names
5975 * file is pointer to array of pointers to matched file names
5976 */
5977
5978#ifndef SEEK_SET
5979# define SEEK_SET 0
5980#endif
5981#ifndef SEEK_END
5982# define SEEK_END 2
5983#endif
5984
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005985#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00005986
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005988mch_expand_wildcards(
5989 int num_pat,
5990 char_u **pat,
5991 int *num_file,
5992 char_u ***file,
5993 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 int i;
5996 size_t len;
5997 char_u *p;
5998 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
Bram Moolenaarc7247912008-01-13 12:54:11 +00006000 /*
6001 * This is the non-OS/2 implementation (really Unix).
6002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 int j;
6004 char_u *tempname;
6005 char_u *command;
6006 FILE *fd;
6007 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006008#define STYLE_ECHO 0 /* use "echo", the default */
6009#define STYLE_GLOB 1 /* use "glob", for csh */
6010#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6011#define STYLE_PRINT 3 /* use "print -N", for zsh */
6012#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6013 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 int shell_style = STYLE_ECHO;
6015 int check_spaces;
6016 static int did_find_nul = FALSE;
6017 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006018 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006019 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020
6021 *num_file = 0; /* default: no files found */
6022 *file = NULL;
6023
6024 /*
6025 * If there are no wildcards, just copy the names to allocated memory.
6026 * Saves a lot of time, because we don't have to start a new shell.
6027 */
6028 if (!have_wildcard(num_pat, pat))
6029 return save_patterns(num_pat, pat, num_file, file);
6030
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006031# ifdef HAVE_SANDBOX
6032 /* Don't allow any shell command in the sandbox. */
6033 if (sandbox != 0 && check_secure())
6034 return FAIL;
6035# endif
6036
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 /*
6038 * Don't allow the use of backticks in secure and restricted mode.
6039 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006040 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 for (i = 0; i < num_pat; ++i)
6042 if (vim_strchr(pat[i], '`') != NULL
6043 && (check_restricted() || check_secure()))
6044 return FAIL;
6045
6046 /*
6047 * get a name for the temp file
6048 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006049 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 {
6051 EMSG(_(e_notmp));
6052 return FAIL;
6053 }
6054
6055 /*
6056 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006057 * file.
6058 * STYLE_BT: NL separated
6059 * If expanding `cmd` execute it directly.
6060 * STYLE_GLOB: NUL separated
6061 * If we use *csh, "glob" will work better than "echo".
6062 * STYLE_PRINT: NL or NUL separated
6063 * If we use *zsh, "print -N" will work better than "glob".
6064 * STYLE_VIMGLOB: NL separated
6065 * If we use *sh*, we define "vimglob()".
6066 * STYLE_ECHO: space separated.
6067 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 */
6069 if (num_pat == 1 && *pat[0] == '`'
6070 && (len = STRLEN(pat[0])) > 2
6071 && *(pat[0] + len - 1) == '`')
6072 shell_style = STYLE_BT;
6073 else if ((len = STRLEN(p_sh)) >= 3)
6074 {
6075 if (STRCMP(p_sh + len - 3, "csh") == 0)
6076 shell_style = STYLE_GLOB;
6077 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6078 shell_style = STYLE_PRINT;
6079 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006080 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6081 "sh") != NULL)
6082 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083
Bram Moolenaarc7247912008-01-13 12:54:11 +00006084 /* Compute the length of the command. We need 2 extra bytes: for the
6085 * optional '&' and for the NUL.
6086 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006088 if (shell_style == STYLE_VIMGLOB)
6089 len += STRLEN(sh_vimglob_func);
6090
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006091 for (i = 0; i < num_pat; ++i)
6092 {
6093 /* Count the length of the patterns in the same way as they are put in
6094 * "command" below. */
6095#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006097#else
6098 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006099 for (j = 0; pat[i][j] != NUL; ++j)
6100 {
6101 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6102 ++len; /* may add a backslash */
6103 ++len;
6104 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006105#endif
6106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 command = alloc(len);
6108 if (command == NULL)
6109 {
6110 /* out of memory */
6111 vim_free(tempname);
6112 return FAIL;
6113 }
6114
6115 /*
6116 * Build the shell command:
6117 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6118 * recognizes this).
6119 * - Add the shell command to print the expanded names.
6120 * - Add the temp file name.
6121 * - Add the file name patterns.
6122 */
6123 if (shell_style == STYLE_BT)
6124 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006125 /* change `command; command& ` to (command; command ) */
6126 STRCPY(command, "(");
6127 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006129 *p-- = ')'; /* remove last backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 while (p > command && vim_iswhite(*p))
6131 --p;
6132 if (*p == '&') /* remove trailing '&' */
6133 {
6134 ampersent = TRUE;
6135 *p = ' ';
6136 }
6137 STRCAT(command, ">");
6138 }
6139 else
6140 {
6141 if (flags & EW_NOTFOUND)
6142 STRCPY(command, "set nonomatch; ");
6143 else
6144 STRCPY(command, "unset nonomatch; ");
6145 if (shell_style == STYLE_GLOB)
6146 STRCAT(command, "glob >");
6147 else if (shell_style == STYLE_PRINT)
6148 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006149 else if (shell_style == STYLE_VIMGLOB)
6150 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 else
6152 STRCAT(command, "echo >");
6153 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006154
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006156
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 if (shell_style != STYLE_BT)
6158 for (i = 0; i < num_pat; ++i)
6159 {
6160 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006161 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006162 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163#ifdef USE_SYSTEM
6164 STRCAT(command, " \"");
6165 STRCAT(command, pat[i]);
6166 STRCAT(command, "\"");
6167#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006168 int intick = FALSE;
6169
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 p = command + STRLEN(command);
6171 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006172 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006173 {
6174 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006175 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006176 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6177 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006178 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006179 * backslash inside backticks, before a special character
6180 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006181 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006182 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6183 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006184 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006185 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006186 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006187 else if (!intick
6188 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6189 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006190 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006191 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6192 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006193 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006194
6195 /* Copy one character. */
6196 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 *p = NUL;
6199#endif
6200 }
6201 if (flags & EW_SILENT)
6202 show_shell_mess = FALSE;
6203 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006204 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205
6206 /*
6207 * Using zsh -G: If a pattern has no matches, it is just deleted from
6208 * the argument list, otherwise zsh gives an error message and doesn't
6209 * expand any other pattern.
6210 */
6211 if (shell_style == STYLE_PRINT)
6212 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6213
6214 /*
6215 * If we use -f then shell variables set in .cshrc won't get expanded.
6216 * vi can do it, so we will too, but it is only necessary if there is a "$"
6217 * in one of the patterns, otherwise we can still use the fast option.
6218 */
6219 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6220 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6221
6222 /*
6223 * execute the shell command
6224 */
6225 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6226
6227 /* When running in the background, give it some time to create the temp
6228 * file, but don't wait for it to finish. */
6229 if (ampersent)
6230 mch_delay(10L, TRUE);
6231
6232 extra_shell_arg = NULL; /* cleanup */
6233 show_shell_mess = TRUE;
6234 vim_free(command);
6235
Bram Moolenaarc7247912008-01-13 12:54:11 +00006236 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
6238 mch_remove(tempname);
6239 vim_free(tempname);
6240 /*
6241 * With interactive completion, the error message is not printed.
6242 * However with USE_SYSTEM, I don't know how to turn off error messages
6243 * from the shell, so screen may still get messed up -- webb.
6244 */
6245#ifndef USE_SYSTEM
6246 if (!(flags & EW_SILENT))
6247#endif
6248 {
6249 redraw_later_clear(); /* probably messed up screen */
6250 msg_putchar('\n'); /* clear bottom line quickly */
6251 cmdline_row = Rows - 1; /* continue on last line */
6252#ifdef USE_SYSTEM
6253 if (!(flags & EW_SILENT))
6254#endif
6255 {
6256 MSG(_(e_wildexpand));
6257 msg_start(); /* don't overwrite this message */
6258 }
6259 }
6260 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6261 * EW_NOTFOUND is given */
6262 if (shell_style == STYLE_BT)
6263 return FAIL;
6264 goto notfound;
6265 }
6266
6267 /*
6268 * read the names from the file into memory
6269 */
6270 fd = fopen((char *)tempname, READBIN);
6271 if (fd == NULL)
6272 {
6273 /* Something went wrong, perhaps a file name with a special char. */
6274 if (!(flags & EW_SILENT))
6275 {
6276 MSG(_(e_wildexpand));
6277 msg_start(); /* don't overwrite this message */
6278 }
6279 vim_free(tempname);
6280 goto notfound;
6281 }
6282 fseek(fd, 0L, SEEK_END);
6283 len = ftell(fd); /* get size of temp file */
6284 fseek(fd, 0L, SEEK_SET);
6285 buffer = alloc(len + 1);
6286 if (buffer == NULL)
6287 {
6288 /* out of memory */
6289 mch_remove(tempname);
6290 vim_free(tempname);
6291 fclose(fd);
6292 return FAIL;
6293 }
6294 i = fread((char *)buffer, 1, len, fd);
6295 fclose(fd);
6296 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006297 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 {
6299 /* unexpected read error */
6300 EMSG2(_(e_notread), tempname);
6301 vim_free(tempname);
6302 vim_free(buffer);
6303 return FAIL;
6304 }
6305 vim_free(tempname);
6306
Bram Moolenaarc7247912008-01-13 12:54:11 +00006307# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6309 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006310 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6312 *p++ = buffer[i];
6313 len = p - buffer;
6314# endif
6315
6316
6317 /* file names are separated with Space */
6318 if (shell_style == STYLE_ECHO)
6319 {
6320 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6321 p = buffer;
6322 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6323 {
6324 while (*p != ' ' && *p != '\n')
6325 ++p;
6326 p = skipwhite(p); /* skip to next entry */
6327 }
6328 }
6329 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006330 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 {
6332 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6333 p = buffer;
6334 for (i = 0; *p != NUL; ++i) /* count number of entries */
6335 {
6336 while (*p != '\n' && *p != NUL)
6337 ++p;
6338 if (*p != NUL)
6339 ++p;
6340 p = skipwhite(p); /* skip leading white space */
6341 }
6342 }
6343 /* file names are separated with NUL */
6344 else
6345 {
6346 /*
6347 * Some versions of zsh use spaces instead of NULs to separate
6348 * results. Only do this when there is no NUL before the end of the
6349 * buffer, otherwise we would never be able to use file names with
6350 * embedded spaces when zsh does use NULs.
6351 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6352 * don't check for spaces again.
6353 */
6354 check_spaces = FALSE;
6355 if (shell_style == STYLE_PRINT && !did_find_nul)
6356 {
6357 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006358 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006359 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 did_find_nul = TRUE;
6361 else
6362 check_spaces = TRUE;
6363 }
6364
6365 /*
6366 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6367 * already is one, for STYLE_GLOB it needs to be added.
6368 */
6369 if (len && buffer[len - 1] == NUL)
6370 --len;
6371 else
6372 buffer[len] = NUL;
6373 i = 0;
6374 for (p = buffer; p < buffer + len; ++p)
6375 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6376 {
6377 ++i;
6378 *p = NUL;
6379 }
6380 if (len)
6381 ++i; /* count last entry */
6382 }
6383 if (i == 0)
6384 {
6385 /*
6386 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6387 * /bin/sh will happily expand it to nothing rather than returning an
6388 * error; and hey, it's good to check anyway -- webb.
6389 */
6390 vim_free(buffer);
6391 goto notfound;
6392 }
6393 *num_file = i;
6394 *file = (char_u **)alloc(sizeof(char_u *) * i);
6395 if (*file == NULL)
6396 {
6397 /* out of memory */
6398 vim_free(buffer);
6399 return FAIL;
6400 }
6401
6402 /*
6403 * Isolate the individual file names.
6404 */
6405 p = buffer;
6406 for (i = 0; i < *num_file; ++i)
6407 {
6408 (*file)[i] = p;
6409 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006410 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6411 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006413 while (!(shell_style == STYLE_ECHO && *p == ' ')
6414 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 ++p;
6416 if (p == buffer + len) /* last entry */
6417 *p = NUL;
6418 else
6419 {
6420 *p++ = NUL;
6421 p = skipwhite(p); /* skip to next entry */
6422 }
6423 }
6424 else /* NUL separates */
6425 {
6426 while (*p && p < buffer + len) /* skip entry */
6427 ++p;
6428 ++p; /* skip NUL */
6429 }
6430 }
6431
6432 /*
6433 * Move the file names to allocated memory.
6434 */
6435 for (j = 0, i = 0; i < *num_file; ++i)
6436 {
6437 /* Require the files to exist. Helps when using /bin/sh */
6438 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6439 continue;
6440
6441 /* check if this entry should be included */
6442 dir = (mch_isdir((*file)[i]));
6443 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6444 continue;
6445
Bram Moolenaara2031822006-03-07 22:29:51 +00006446 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006447 if (!dir && (flags & EW_EXEC)
6448 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006449 continue;
6450
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6452 if (p)
6453 {
6454 STRCPY(p, (*file)[i]);
6455 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006456 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 (*file)[j++] = p;
6458 }
6459 }
6460 vim_free(buffer);
6461 *num_file = j;
6462
6463 if (*num_file == 0) /* rejected all entries */
6464 {
6465 vim_free(*file);
6466 *file = NULL;
6467 goto notfound;
6468 }
6469
6470 return OK;
6471
6472notfound:
6473 if (flags & EW_NOTFOUND)
6474 return save_patterns(num_pat, pat, num_file, file);
6475 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476}
6477
6478#endif /* VMS */
6479
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006481save_patterns(
6482 int num_pat,
6483 char_u **pat,
6484 int *num_file,
6485 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486{
6487 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006488 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489
6490 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6491 if (*file == NULL)
6492 return FAIL;
6493 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006494 {
6495 s = vim_strsave(pat[i]);
6496 if (s != NULL)
6497 /* Be compatible with expand_filename(): halve the number of
6498 * backslashes. */
6499 backslash_halve(s);
6500 (*file)[i] = s;
6501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 *num_file = num_pat;
6503 return OK;
6504}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506/*
6507 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6508 * expand.
6509 */
6510 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006511mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006513 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 if (*p == '\\' && p[1] != NUL)
6516 ++p;
6517 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 if (vim_strchr((char_u *)
6519#ifdef VMS
6520 "*?%"
6521#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523#endif
6524 , *p) != NULL)
6525 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 }
6527 return FALSE;
6528}
6529
6530/*
6531 * Return TRUE if the string "p" contains a wildcard.
6532 * Don't recognize '~' at the end as a wildcard.
6533 */
6534 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006535mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006537 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (*p == '\\' && p[1] != NUL)
6540 ++p;
6541 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 if (vim_strchr((char_u *)
6543#ifdef VMS
6544 "*?%$"
6545#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547#endif
6548 , *p) != NULL
6549 || (*p == '~' && p[1] != NUL))
6550 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 }
6552 return FALSE;
6553}
6554
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006556have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557{
6558 int i;
6559
6560 for (i = 0; i < num; i++)
6561 if (mch_has_wildcard(file[i]))
6562 return 1;
6563 return 0;
6564}
6565
6566 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006567have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568{
6569 int i;
6570
6571 for (i = 0; i < num; i++)
6572 if (vim_strchr(file[i], '$') != NULL)
6573 return TRUE;
6574 return FALSE;
6575}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006577#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578/*
6579 * Scaled-down version of rename(), which is missing in Xenix.
6580 * This version can only move regular files and will fail if the
6581 * destination exists.
6582 */
6583 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006584mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585{
6586 struct stat st;
6587
6588 if (stat(dest, &st) >= 0) /* fail if destination exists */
6589 return -1;
6590 if (link(src, dest) != 0) /* link file to new name */
6591 return -1;
6592 if (mch_remove(src) == 0) /* delete link to old name */
6593 return 0;
6594 return -1;
6595}
6596#endif /* !HAVE_RENAME */
6597
6598#ifdef FEAT_MOUSE_GPM
6599/*
6600 * Initializes connection with gpm (if it isn't already opened)
6601 * Return 1 if succeeded (or connection already opened), 0 if failed
6602 */
6603 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006604gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
6606 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
6607
6608 if (!gpm_flag)
6609 {
6610 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
6611 gpm_connect.defaultMask = ~GPM_HARD;
6612 /* Default handling for mouse move*/
6613 gpm_connect.minMod = 0; /* Handle any modifier keys */
6614 gpm_connect.maxMod = 0xffff;
6615 if (Gpm_Open(&gpm_connect, 0) > 0)
6616 {
6617 /* gpm library tries to handling TSTP causes
6618 * problems. Anyways, we close connection to Gpm whenever
6619 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006620 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006622# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006624# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 return 1; /* succeed */
6626 }
6627 if (gpm_fd == -2)
6628 Gpm_Close(); /* We don't want to talk to xterm via gpm */
6629 return 0;
6630 }
6631 return 1; /* already open */
6632}
6633
6634/*
6635 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 */
6637 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006638gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
6640 if (gpm_flag && gpm_fd >= 0) /* if Open */
6641 Gpm_Close();
6642}
6643
6644/* Reads gpm event and adds special keys to input buf. Returns length of
6645 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02006646 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 */
6648 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006649mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650{
6651 int button;
6652 static Gpm_Event gpm_event;
6653 char_u string[6];
6654 int_u vim_modifiers;
6655 int row,col;
6656 unsigned char buttons_mask;
6657 unsigned char gpm_modifiers;
6658 static unsigned char old_buttons = 0;
6659
6660 Gpm_GetEvent(&gpm_event);
6661
6662#ifdef FEAT_GUI
6663 /* Don't put events in the input queue now. */
6664 if (hold_gui_events)
6665 return 0;
6666#endif
6667
6668 row = gpm_event.y - 1;
6669 col = gpm_event.x - 1;
6670
6671 string[0] = ESC; /* Our termcode */
6672 string[1] = 'M';
6673 string[2] = 'G';
6674 switch (GPM_BARE_EVENTS(gpm_event.type))
6675 {
6676 case GPM_DRAG:
6677 string[3] = MOUSE_DRAG;
6678 break;
6679 case GPM_DOWN:
6680 buttons_mask = gpm_event.buttons & ~old_buttons;
6681 old_buttons = gpm_event.buttons;
6682 switch (buttons_mask)
6683 {
6684 case GPM_B_LEFT:
6685 button = MOUSE_LEFT;
6686 break;
6687 case GPM_B_MIDDLE:
6688 button = MOUSE_MIDDLE;
6689 break;
6690 case GPM_B_RIGHT:
6691 button = MOUSE_RIGHT;
6692 break;
6693 default:
6694 return 0;
6695 /*Don't know what to do. Can more than one button be
6696 * reported in one event? */
6697 }
6698 string[3] = (char_u)(button | 0x20);
6699 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
6700 break;
6701 case GPM_UP:
6702 string[3] = MOUSE_RELEASE;
6703 old_buttons &= ~gpm_event.buttons;
6704 break;
6705 default:
6706 return 0;
6707 }
6708 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
6709 gpm_modifiers = gpm_event.modifiers;
6710 vim_modifiers = 0x0;
6711 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
6712 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
6713 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
6714 */
6715 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
6716 vim_modifiers |= MOUSE_SHIFT;
6717
6718 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
6719 vim_modifiers |= MOUSE_CTRL;
6720 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
6721 vim_modifiers |= MOUSE_ALT;
6722 string[3] |= vim_modifiers;
6723 string[4] = (char_u)(col + ' ' + 1);
6724 string[5] = (char_u)(row + ' ' + 1);
6725 add_to_input_buf(string, 6);
6726 return 6;
6727}
6728#endif /* FEAT_MOUSE_GPM */
6729
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006730#ifdef FEAT_SYSMOUSE
6731/*
6732 * Initialize connection with sysmouse.
6733 * Let virtual console inform us with SIGUSR2 for pending sysmouse
6734 * output, any sysmouse output than will be processed via sig_sysmouse().
6735 * Return OK if succeeded, FAIL if failed.
6736 */
6737 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006738sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006739{
6740 struct mouse_info mouse;
6741
6742 mouse.operation = MOUSE_MODE;
6743 mouse.u.mode.mode = 0;
6744 mouse.u.mode.signal = SIGUSR2;
6745 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
6746 {
6747 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
6748 mouse.operation = MOUSE_SHOW;
6749 ioctl(1, CONS_MOUSECTL, &mouse);
6750 return OK;
6751 }
6752 return FAIL;
6753}
6754
6755/*
6756 * Stop processing SIGUSR2 signals, and also make sure that
6757 * virtual console do not send us any sysmouse related signal.
6758 */
6759 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006760sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006761{
6762 struct mouse_info mouse;
6763
6764 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
6765 mouse.operation = MOUSE_MODE;
6766 mouse.u.mode.mode = 0;
6767 mouse.u.mode.signal = 0;
6768 ioctl(1, CONS_MOUSECTL, &mouse);
6769}
6770
6771/*
6772 * Gets info from sysmouse and adds special keys to input buf.
6773 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006774 static RETSIGTYPE
6775sig_sysmouse SIGDEFARG(sigarg)
6776{
6777 struct mouse_info mouse;
6778 struct video_info video;
6779 char_u string[6];
6780 int row, col;
6781 int button;
6782 int buttons;
6783 static int oldbuttons = 0;
6784
6785#ifdef FEAT_GUI
6786 /* Don't put events in the input queue now. */
6787 if (hold_gui_events)
6788 return;
6789#endif
6790
6791 mouse.operation = MOUSE_GETINFO;
6792 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
6793 && ioctl(1, FBIO_MODEINFO, &video) != -1
6794 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
6795 && video.vi_cheight > 0 && video.vi_cwidth > 0)
6796 {
6797 row = mouse.u.data.y / video.vi_cheight;
6798 col = mouse.u.data.x / video.vi_cwidth;
6799 buttons = mouse.u.data.buttons;
6800 string[0] = ESC; /* Our termcode */
6801 string[1] = 'M';
6802 string[2] = 'S';
6803 if (oldbuttons == buttons && buttons != 0)
6804 {
6805 button = MOUSE_DRAG;
6806 }
6807 else
6808 {
6809 switch (buttons)
6810 {
6811 case 0:
6812 button = MOUSE_RELEASE;
6813 break;
6814 case 1:
6815 button = MOUSE_LEFT;
6816 break;
6817 case 2:
6818 button = MOUSE_MIDDLE;
6819 break;
6820 case 4:
6821 button = MOUSE_RIGHT;
6822 break;
6823 default:
6824 return;
6825 }
6826 oldbuttons = buttons;
6827 }
6828 string[3] = (char_u)(button);
6829 string[4] = (char_u)(col + ' ' + 1);
6830 string[5] = (char_u)(row + ' ' + 1);
6831 add_to_input_buf(string, 6);
6832 }
6833 return;
6834}
6835#endif /* FEAT_SYSMOUSE */
6836
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01006838typedef char_u * (*STRPROCSTR)(char_u *);
6839typedef char_u * (*INTPROCSTR)(int);
6840typedef int (*STRPROCINT)(char_u *);
6841typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842
6843/*
6844 * Call a DLL routine which takes either a string or int param
6845 * and returns an allocated string.
6846 */
6847 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006848mch_libcall(
6849 char_u *libname,
6850 char_u *funcname,
6851 char_u *argstring, /* NULL when using a argint */
6852 int argint,
6853 char_u **string_result,/* NULL when using number_result */
6854 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855{
6856# if defined(USE_DLOPEN)
6857 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006858 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859# else
6860 shl_t hinstLib;
6861# endif
6862 STRPROCSTR ProcAdd;
6863 INTPROCSTR ProcAddI;
6864 char_u *retval_str = NULL;
6865 int retval_int = 0;
6866 int success = FALSE;
6867
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006868 /*
6869 * Get a handle to the DLL module.
6870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006872 /* First clear any error, it's not cleared by the dlopen() call. */
6873 (void)dlerror();
6874
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 hinstLib = dlopen((char *)libname, RTLD_LAZY
6876# ifdef RTLD_LOCAL
6877 | RTLD_LOCAL
6878# endif
6879 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006880 if (hinstLib == NULL)
6881 {
6882 /* "dlerr" must be used before dlclose() */
6883 dlerr = (char *)dlerror();
6884 if (dlerr != NULL)
6885 EMSG2(_("dlerror = \"%s\""), dlerr);
6886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887# else
6888 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
6889# endif
6890
6891 /* If the handle is valid, try to get the function address. */
6892 if (hinstLib != NULL)
6893 {
6894# ifdef HAVE_SETJMP_H
6895 /*
6896 * Catch a crash when calling the library function. For example when
6897 * using a number where a string pointer is expected.
6898 */
6899 mch_startjmp();
6900 if (SETJMP(lc_jump_env) != 0)
6901 {
6902 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00006903# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006904 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00006905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 mch_didjmp();
6907 }
6908 else
6909# endif
6910 {
6911 retval_str = NULL;
6912 retval_int = 0;
6913
6914 if (argstring != NULL)
6915 {
6916# if defined(USE_DLOPEN)
6917 ProcAdd = (STRPROCSTR)dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006918 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919# else
6920 if (shl_findsym(&hinstLib, (const char *)funcname,
6921 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
6922 ProcAdd = NULL;
6923# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006924 if ((success = (ProcAdd != NULL
6925# if defined(USE_DLOPEN)
6926 && dlerr == NULL
6927# endif
6928 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 {
6930 if (string_result == NULL)
6931 retval_int = ((STRPROCINT)ProcAdd)(argstring);
6932 else
6933 retval_str = (ProcAdd)(argstring);
6934 }
6935 }
6936 else
6937 {
6938# if defined(USE_DLOPEN)
6939 ProcAddI = (INTPROCSTR)dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006940 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941# else
6942 if (shl_findsym(&hinstLib, (const char *)funcname,
6943 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
6944 ProcAddI = NULL;
6945# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006946 if ((success = (ProcAddI != NULL
6947# if defined(USE_DLOPEN)
6948 && dlerr == NULL
6949# endif
6950 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {
6952 if (string_result == NULL)
6953 retval_int = ((INTPROCINT)ProcAddI)(argint);
6954 else
6955 retval_str = (ProcAddI)(argint);
6956 }
6957 }
6958
6959 /* Save the string before we free the library. */
6960 /* Assume that a "1" or "-1" result is an illegal pointer. */
6961 if (string_result == NULL)
6962 *number_result = retval_int;
6963 else if (retval_str != NULL
6964 && retval_str != (char_u *)1
6965 && retval_str != (char_u *)-1)
6966 *string_result = vim_strsave(retval_str);
6967 }
6968
6969# ifdef HAVE_SETJMP_H
6970 mch_endjmp();
6971# ifdef SIGHASARG
6972 if (lc_signal != 0)
6973 {
6974 int i;
6975
6976 /* try to find the name of this signal */
6977 for (i = 0; signal_info[i].sig != -1; i++)
6978 if (lc_signal == signal_info[i].sig)
6979 break;
6980 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
6981 }
6982# endif
6983# endif
6984
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006986 /* "dlerr" must be used before dlclose() */
6987 if (dlerr != NULL)
6988 EMSG2(_("dlerror = \"%s\""), dlerr);
6989
6990 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 (void)dlclose(hinstLib);
6992# else
6993 (void)shl_unload(hinstLib);
6994# endif
6995 }
6996
6997 if (!success)
6998 {
6999 EMSG2(_(e_libcall), funcname);
7000 return FAIL;
7001 }
7002
7003 return OK;
7004}
7005#endif
7006
7007#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7008static int xterm_trace = -1; /* default: disabled */
7009static int xterm_button;
7010
7011/*
7012 * Setup a dummy window for X selections in a terminal.
7013 */
7014 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007015setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016{
7017 int z = 0;
7018 char *strp = "";
7019 Widget AppShell;
7020
7021 if (!x_connect_to_server())
7022 return;
7023
7024 open_app_context();
7025 if (app_context != NULL && xterm_Shell == (Widget)0)
7026 {
7027 int (*oldhandler)();
7028#if defined(HAVE_SETJMP_H)
7029 int (*oldIOhandler)();
7030#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007031# ifdef ELAPSED_FUNC
7032 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033
7034 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007035 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036# endif
7037
7038 /* Ignore X errors while opening the display */
7039 oldhandler = XSetErrorHandler(x_error_check);
7040
7041#if defined(HAVE_SETJMP_H)
7042 /* Ignore X IO errors while opening the display */
7043 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7044 mch_startjmp();
7045 if (SETJMP(lc_jump_env) != 0)
7046 {
7047 mch_didjmp();
7048 xterm_dpy = NULL;
7049 }
7050 else
7051#endif
7052 {
7053 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7054 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
7055#if defined(HAVE_SETJMP_H)
7056 mch_endjmp();
7057#endif
7058 }
7059
7060#if defined(HAVE_SETJMP_H)
7061 /* Now handle X IO errors normally. */
7062 (void)XSetIOErrorHandler(oldIOhandler);
7063#endif
7064 /* Now handle X errors normally. */
7065 (void)XSetErrorHandler(oldhandler);
7066
7067 if (xterm_dpy == NULL)
7068 {
7069 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007070 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 return;
7072 }
7073
7074 /* Catch terminating error of the X server connection. */
7075 (void)XSetIOErrorHandler(x_IOerror_handler);
7076
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007077# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007079 {
7080 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007081 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007082 verbose_leave();
7083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084# endif
7085
7086 /* Create a Shell to make converters work. */
7087 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7088 applicationShellWidgetClass, xterm_dpy,
7089 NULL);
7090 if (AppShell == (Widget)0)
7091 return;
7092 xterm_Shell = XtVaCreatePopupShell("VIM",
7093 topLevelShellWidgetClass, AppShell,
7094 XtNmappedWhenManaged, 0,
7095 XtNwidth, 1,
7096 XtNheight, 1,
7097 NULL);
7098 if (xterm_Shell == (Widget)0)
7099 return;
7100
7101 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007102 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 if (x11_display == NULL)
7104 x11_display = xterm_dpy;
7105
7106 XtRealizeWidget(xterm_Shell);
7107 XSync(xterm_dpy, False);
7108 xterm_update();
7109 }
7110 if (xterm_Shell != (Widget)0)
7111 {
7112 clip_init(TRUE);
7113 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7114 x11_window = (Window)atol(strp);
7115 /* Check if $WINDOWID is valid. */
7116 if (test_x11_window(xterm_dpy) == FAIL)
7117 x11_window = 0;
7118 if (x11_window != 0)
7119 xterm_trace = 0;
7120 }
7121}
7122
7123 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007124start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
7126 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7127 return;
7128 xterm_trace = 1;
7129 xterm_button = button;
7130 do_xterm_trace();
7131}
7132
7133
7134 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007135stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136{
7137 if (xterm_trace < 0)
7138 return;
7139 xterm_trace = 0;
7140}
7141
7142/*
7143 * Query the xterm pointer and generate mouse termcodes if necessary
7144 * return TRUE if dragging is active, else FALSE
7145 */
7146 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007147do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148{
7149 Window root, child;
7150 int root_x, root_y;
7151 int win_x, win_y;
7152 int row, col;
7153 int_u mask_return;
7154 char_u buf[50];
7155 char_u *strp;
7156 long got_hints;
7157 static char_u *mouse_code;
7158 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7159 static int prev_row = 0, prev_col = 0;
7160 static XSizeHints xterm_hints;
7161
7162 if (xterm_trace <= 0)
7163 return FALSE;
7164
7165 if (xterm_trace == 1)
7166 {
7167 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007168 * have changed recently. */
7169 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7170 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 || xterm_hints.width_inc <= 1
7172 || xterm_hints.height_inc <= 1)
7173 {
7174 xterm_trace = -1; /* Not enough data -- disable tracing */
7175 return FALSE;
7176 }
7177
7178 /* Rely on the same mouse code for the duration of this */
7179 mouse_code = find_termcode(mouse_name);
7180 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007181 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 xterm_trace = 2;
7183
7184 /* Find the offset of the chars, there might be a scrollbar on the
7185 * left of the window and/or a menu on the top (eterm etc.) */
7186 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7187 &win_x, &win_y, &mask_return);
7188 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7189 - (xterm_hints.height_inc / 2);
7190 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7191 xterm_hints.y = 2;
7192 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7193 - (xterm_hints.width_inc / 2);
7194 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7195 xterm_hints.x = 2;
7196 return TRUE;
7197 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007198 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {
7200 xterm_trace = 0;
7201 return FALSE;
7202 }
7203
7204 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7205 &win_x, &win_y, &mask_return);
7206
7207 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7208 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7209 if (row == prev_row && col == prev_col)
7210 return TRUE;
7211
7212 STRCPY(buf, mouse_code);
7213 strp = buf + STRLEN(buf);
7214 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7215 *strp++ = (char_u)(col + ' ' + 1);
7216 *strp++ = (char_u)(row + ' ' + 1);
7217 *strp = 0;
7218 add_to_input_buf(buf, STRLEN(buf));
7219
7220 prev_row = row;
7221 prev_col = col;
7222 return TRUE;
7223}
7224
7225# if defined(FEAT_GUI) || defined(PROTO)
7226/*
7227 * Destroy the display, window and app_context. Required for GTK.
7228 */
7229 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007230clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231{
7232 if (xterm_Shell != (Widget)0)
7233 {
7234 XtDestroyWidget(xterm_Shell);
7235 xterm_Shell = (Widget)0;
7236 }
7237 if (xterm_dpy != NULL)
7238 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007239# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 /* Lesstif and Solaris crash here, lose some memory */
7241 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 if (x11_display == xterm_dpy)
7244 x11_display = NULL;
7245 xterm_dpy = NULL;
7246 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007247# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 if (app_context != (XtAppContext)NULL)
7249 {
7250 /* Lesstif and Solaris crash here, lose some memory */
7251 XtDestroyApplicationContext(app_context);
7252 app_context = (XtAppContext)NULL;
7253 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007254# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255}
7256# endif
7257
7258/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007259 * Catch up with GUI or X events.
7260 */
7261 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007262clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007263{
7264# ifdef FEAT_GUI
7265 if (gui.in_use)
7266 gui_mch_update();
7267 else
7268# endif
7269 if (xterm_Shell != (Widget)0)
7270 xterm_update();
7271}
7272
7273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 * Catch up with any queued X events. This may put keyboard input into the
7275 * input buffer, call resize call-backs, trigger timers etc. If there is
7276 * nothing in the X event queue (& no timers pending), then we return
7277 * immediately.
7278 */
7279 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007280xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281{
7282 XEvent event;
7283
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007284 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007286 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007288 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007289 break;
7290
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007291 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007292 {
7293 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007294 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007295#ifdef FEAT_CLIENTSERVER
7296 {
7297 XPropertyEvent *e = (XPropertyEvent *)&event;
7298
7299 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007301 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007304 XtDispatchEvent(&event);
7305 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007306 else
7307 {
7308 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007309 XtAppProcessEvent(app_context, mask);
7310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 }
7312}
7313
7314 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007315clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316{
7317 if (xterm_Shell != (Widget)0)
7318 return clip_x11_own_selection(xterm_Shell, cbd);
7319 return FAIL;
7320}
7321
7322 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007323clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
7325 if (xterm_Shell != (Widget)0)
7326 clip_x11_lose_selection(xterm_Shell, cbd);
7327}
7328
7329 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007330clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
7332 if (xterm_Shell != (Widget)0)
7333 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7334}
7335
7336 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007337clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338{
7339 clip_x11_set_selection(cbd);
7340}
7341#endif
7342
7343
7344#if defined(USE_XSMP) || defined(PROTO)
7345/*
7346 * Code for X Session Management Protocol.
7347 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007348static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7349static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7350static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7351static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7352static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
7354
7355# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007356static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357
7358/*
7359 * This is our chance to ask the user if they want to save,
7360 * or abort the logout
7361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007363xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
7365 cmdmod_T save_cmdmod;
7366 int cancel_shutdown = False;
7367
7368 save_cmdmod = cmdmod;
7369 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007370 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 /* Mustn't logout */
7372 cancel_shutdown = True;
7373 cmdmod = save_cmdmod;
7374 setcursor(); /* position cursor */
7375 out_flush();
7376
7377 /* Done interaction */
7378 SmcInteractDone(smc_conn, cancel_shutdown);
7379
7380 /* Finish off
7381 * Only end save-yourself here if we're not cancelling shutdown;
7382 * we'll get a cancelled callback later in which we'll end it.
7383 * Hopefully get around glitchy SMs (like GNOME-1)
7384 */
7385 if (!cancel_shutdown)
7386 {
7387 xsmp.save_yourself = False;
7388 SmcSaveYourselfDone(smc_conn, True);
7389 }
7390}
7391# endif
7392
7393/*
7394 * Callback that starts save-yourself.
7395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007397xsmp_handle_save_yourself(
7398 SmcConn smc_conn,
7399 SmPointer client_data UNUSED,
7400 int save_type UNUSED,
7401 Bool shutdown,
7402 int interact_style UNUSED,
7403 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404{
7405 /* Handle already being in saveyourself */
7406 if (xsmp.save_yourself)
7407 SmcSaveYourselfDone(smc_conn, True);
7408 xsmp.save_yourself = True;
7409 xsmp.shutdown = shutdown;
7410
7411 /* First up, preserve all files */
7412 out_flush();
7413 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7414
7415 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007416 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
7418# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7419 /* Now see if we can ask about unsaved files */
7420 if (shutdown && !fast && gui.in_use)
7421 /* Need to interact with user, but need SM's permission */
7422 SmcInteractRequest(smc_conn, SmDialogError,
7423 xsmp_handle_interaction, client_data);
7424 else
7425# endif
7426 {
7427 /* Can stop the cycle here */
7428 SmcSaveYourselfDone(smc_conn, True);
7429 xsmp.save_yourself = False;
7430 }
7431}
7432
7433
7434/*
7435 * Callback to warn us of imminent death.
7436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007438xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
7440 xsmp_close();
7441
7442 /* quit quickly leaving swapfiles for modified buffers behind */
7443 getout_preserve_modified(0);
7444}
7445
7446
7447/*
7448 * Callback to tell us that save-yourself has completed.
7449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007451xsmp_save_complete(
7452 SmcConn smc_conn UNUSED,
7453 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454{
7455 xsmp.save_yourself = False;
7456}
7457
7458
7459/*
7460 * Callback to tell us that an instigated shutdown was cancelled
7461 * (maybe even by us)
7462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007464xsmp_shutdown_cancelled(
7465 SmcConn smc_conn,
7466 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467{
7468 if (xsmp.save_yourself)
7469 SmcSaveYourselfDone(smc_conn, True);
7470 xsmp.save_yourself = False;
7471 xsmp.shutdown = False;
7472}
7473
7474
7475/*
7476 * Callback to tell us that a new ICE connection has been established.
7477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007479xsmp_ice_connection(
7480 IceConn iceConn,
7481 IcePointer clientData UNUSED,
7482 Bool opening,
7483 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
7485 /* Intercept creation of ICE connection fd */
7486 if (opening)
7487 {
7488 xsmp_icefd = IceConnectionNumber(iceConn);
7489 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7490 }
7491}
7492
7493
7494/* Handle any ICE processing that's required; return FAIL if SM lost */
7495 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007496xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
7498 Bool rep;
7499
7500 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7501 == IceProcessMessagesIOError)
7502 {
7503 /* Lost ICE */
7504 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007505 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 xsmp_close();
7507 return FAIL;
7508 }
7509 else
7510 return OK;
7511}
7512
7513static int dummy;
7514
7515/* Set up X Session Management Protocol */
7516 void
7517xsmp_init(void)
7518{
7519 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 SmcCallbacks smcallbacks;
7521#if 0
7522 SmPropValue smname;
7523 SmProp smnameprop;
7524 SmProp *smprops[1];
7525#endif
7526
7527 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007528 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529
7530 xsmp.save_yourself = xsmp.shutdown = False;
7531
7532 /* Set up SM callbacks - must have all, even if they're not used */
7533 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7534 smcallbacks.save_yourself.client_data = NULL;
7535 smcallbacks.die.callback = xsmp_die;
7536 smcallbacks.die.client_data = NULL;
7537 smcallbacks.save_complete.callback = xsmp_save_complete;
7538 smcallbacks.save_complete.client_data = NULL;
7539 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7540 smcallbacks.shutdown_cancelled.client_data = NULL;
7541
7542 /* Set up a watch on ICE connection creations. The "dummy" argument is
7543 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7544 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7545 {
7546 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007547 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 return;
7549 }
7550
7551 /* Create an SM connection */
7552 xsmp.smcconn = SmcOpenConnection(
7553 NULL,
7554 NULL,
7555 SmProtoMajor,
7556 SmProtoMinor,
7557 SmcSaveYourselfProcMask | SmcDieProcMask
7558 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
7559 &smcallbacks,
7560 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00007561 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 sizeof(errorstring),
7563 errorstring);
7564 if (xsmp.smcconn == NULL)
7565 {
7566 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00007567
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007569 {
7570 vim_snprintf(errorreport, sizeof(errorreport),
7571 _("XSMP SmcOpenConnection failed: %s"), errorstring);
7572 verb_msg((char_u *)errorreport);
7573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 return;
7575 }
7576 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
7577
7578#if 0
7579 /* ID ourselves */
7580 smname.value = "vim";
7581 smname.length = 3;
7582 smnameprop.name = "SmProgram";
7583 smnameprop.type = "SmARRAY8";
7584 smnameprop.num_vals = 1;
7585 smnameprop.vals = &smname;
7586
7587 smprops[0] = &smnameprop;
7588 SmcSetProperties(xsmp.smcconn, 1, smprops);
7589#endif
7590}
7591
7592
7593/* Shut down XSMP comms. */
7594 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007595xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597 if (xsmp_icefd != -1)
7598 {
7599 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00007600 if (xsmp.clientid != NULL)
7601 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00007602 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 xsmp_icefd = -1;
7604 }
7605}
7606#endif /* USE_XSMP */
7607
7608
7609#ifdef EBCDIC
7610/* Translate character to its CTRL- value */
7611char CtrlTable[] =
7612{
7613/* 00 - 5E */
7614 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7615 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7616 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7617 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7618 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7619 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7620/* ^ */ 0x1E,
7621/* - */ 0x1F,
7622/* 61 - 6C */
7623 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7624/* _ */ 0x1F,
7625/* 6E - 80 */
7626 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7627/* a */ 0x01,
7628/* b */ 0x02,
7629/* c */ 0x03,
7630/* d */ 0x37,
7631/* e */ 0x2D,
7632/* f */ 0x2E,
7633/* g */ 0x2F,
7634/* h */ 0x16,
7635/* i */ 0x05,
7636/* 8A - 90 */
7637 0, 0, 0, 0, 0, 0, 0,
7638/* j */ 0x15,
7639/* k */ 0x0B,
7640/* l */ 0x0C,
7641/* m */ 0x0D,
7642/* n */ 0x0E,
7643/* o */ 0x0F,
7644/* p */ 0x10,
7645/* q */ 0x11,
7646/* r */ 0x12,
7647/* 9A - A1 */
7648 0, 0, 0, 0, 0, 0, 0, 0,
7649/* s */ 0x13,
7650/* t */ 0x3C,
7651/* u */ 0x3D,
7652/* v */ 0x32,
7653/* w */ 0x26,
7654/* x */ 0x18,
7655/* y */ 0x19,
7656/* z */ 0x3F,
7657/* AA - AC */
7658 0, 0, 0,
7659/* [ */ 0x27,
7660/* AE - BC */
7661 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7662/* ] */ 0x1D,
7663/* BE - C0 */ 0, 0, 0,
7664/* A */ 0x01,
7665/* B */ 0x02,
7666/* C */ 0x03,
7667/* D */ 0x37,
7668/* E */ 0x2D,
7669/* F */ 0x2E,
7670/* G */ 0x2F,
7671/* H */ 0x16,
7672/* I */ 0x05,
7673/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
7674/* J */ 0x15,
7675/* K */ 0x0B,
7676/* L */ 0x0C,
7677/* M */ 0x0D,
7678/* N */ 0x0E,
7679/* O */ 0x0F,
7680/* P */ 0x10,
7681/* Q */ 0x11,
7682/* R */ 0x12,
7683/* DA - DF */ 0, 0, 0, 0, 0, 0,
7684/* \ */ 0x1C,
7685/* E1 */ 0,
7686/* S */ 0x13,
7687/* T */ 0x3C,
7688/* U */ 0x3D,
7689/* V */ 0x32,
7690/* W */ 0x26,
7691/* X */ 0x18,
7692/* Y */ 0x19,
7693/* Z */ 0x3F,
7694/* EA - FF*/ 0, 0, 0, 0, 0, 0,
7695 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7696};
7697
7698char MetaCharTable[]=
7699{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7700 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
7701 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
7702 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
7703 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
7704};
7705
7706
7707/* TODO: Use characters NOT numbers!!! */
7708char CtrlCharTable[]=
7709{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7710 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
7711 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
7712 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
7713 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
7714};
7715
7716
7717#endif