blob: b4808b5fe8a69c5cbbb3dc21a524555c3a3e849c [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100178static int WaitForChar(long);
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100179static int WaitForCharOrMouse(long, int *break_loop);
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100180#if defined(__BEOS__) || defined(VMS)
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100181int RealWaitForChar(int, long, int *, int *break_loop);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#else
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100183static int RealWaitForChar(int, long, int *, int *break_loop);
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 Moolenaar071d4272004-06-13 20:20:40 +0000214#ifndef __EMX__
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100215static int have_wildcard(int, char_u **);
216static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217#endif
218
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219#ifndef __EMX__
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100220static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#endif
222
223#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000224# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#endif
226
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000227/* volatile because it is used in signal handler sig_winch(). */
228static volatile int do_resize = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#ifndef __EMX__
230static char_u *extra_shell_arg = NULL;
231static int show_shell_mess = TRUE;
232#endif
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000233/* volatile because it is used in signal handler deathtrap(). */
234static volatile int deadly_signal = 0; /* The signal we caught */
235/* volatile because it is used in signal handler deathtrap(). */
236static volatile int in_mch_delay = FALSE; /* sleeping in mch_delay() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237
238static int curr_tmode = TMODE_COOK; /* contains current terminal mode */
239
240#ifdef USE_XSMP
241typedef struct
242{
243 SmcConn smcconn; /* The SM connection ID */
244 IceConn iceconn; /* The ICE connection ID */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200245 char *clientid; /* The client ID for the current smc session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 Bool save_yourself; /* If we're in the middle of a save_yourself */
247 Bool shutdown; /* If we're in shutdown mode */
248} xsmp_config_T;
249
250static xsmp_config_T xsmp;
251#endif
252
253#ifdef SYS_SIGLIST_DECLARED
254/*
255 * I have seen
256 * extern char *_sys_siglist[NSIG];
257 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
258 * that describe the signals. That is nearly what we want here. But
259 * autoconf does only check for sys_siglist (without the underscore), I
260 * do not want to change everything today.... jw.
261 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.in
262 */
263#endif
264
265static struct signalinfo
266{
267 int sig; /* Signal number, eg. SIGSEGV etc */
268 char *name; /* Signal name (not char_u!). */
269 char deadly; /* Catch as a deadly signal? */
270} signal_info[] =
271{
272#ifdef SIGHUP
273 {SIGHUP, "HUP", TRUE},
274#endif
275#ifdef SIGQUIT
276 {SIGQUIT, "QUIT", TRUE},
277#endif
278#ifdef SIGILL
279 {SIGILL, "ILL", TRUE},
280#endif
281#ifdef SIGTRAP
282 {SIGTRAP, "TRAP", TRUE},
283#endif
284#ifdef SIGABRT
285 {SIGABRT, "ABRT", TRUE},
286#endif
287#ifdef SIGEMT
288 {SIGEMT, "EMT", TRUE},
289#endif
290#ifdef SIGFPE
291 {SIGFPE, "FPE", TRUE},
292#endif
293#ifdef SIGBUS
294 {SIGBUS, "BUS", TRUE},
295#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100296#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
297 /* MzScheme uses SEGV in its garbage collector */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 {SIGSEGV, "SEGV", TRUE},
299#endif
300#ifdef SIGSYS
301 {SIGSYS, "SYS", TRUE},
302#endif
303#ifdef SIGALRM
304 {SIGALRM, "ALRM", FALSE}, /* Perl's alarm() can trigger it */
305#endif
306#ifdef SIGTERM
307 {SIGTERM, "TERM", TRUE},
308#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100309#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 {SIGVTALRM, "VTALRM", TRUE},
311#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000312#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
313 /* MzScheme uses SIGPROF for its own needs; On Linux with profiling
314 * this makes Vim exit. WE_ARE_PROFILING is defined in Makefile. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 {SIGPROF, "PROF", TRUE},
316#endif
317#ifdef SIGXCPU
318 {SIGXCPU, "XCPU", TRUE},
319#endif
320#ifdef SIGXFSZ
321 {SIGXFSZ, "XFSZ", TRUE},
322#endif
323#ifdef SIGUSR1
324 {SIGUSR1, "USR1", TRUE},
325#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000326#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
327 /* Used for sysmouse handling */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 {SIGUSR2, "USR2", TRUE},
329#endif
330#ifdef SIGINT
331 {SIGINT, "INT", FALSE},
332#endif
333#ifdef SIGWINCH
334 {SIGWINCH, "WINCH", FALSE},
335#endif
336#ifdef SIGTSTP
337 {SIGTSTP, "TSTP", FALSE},
338#endif
339#ifdef SIGPIPE
340 {SIGPIPE, "PIPE", FALSE},
341#endif
342 {-1, "Unknown!", FALSE}
343};
344
Bram Moolenaar25724922009-07-14 15:38:41 +0000345 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100346mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000347{
348 if (p_verbose >= 5)
349 {
350 verbose_enter();
351 smsg((char_u *)"chdir(%s)", path);
352 verbose_leave();
353 }
354# ifdef VMS
355 return chdir(vms_fixfilename(path));
356# else
357 return chdir(path);
358# endif
359}
360
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000361/*
362 * Write s[len] to the screen.
363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100365mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366{
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000367 ignored = (int)write(1, (char *)s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 if (p_wd) /* Unix is too fast, slow down a bit more */
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100369 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370}
371
372/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000373 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 * Get a characters from the keyboard.
375 * Return the number of characters that are available.
376 * If wtime == 0 do not wait for characters.
377 * If wtime == n wait a short time for characters.
378 * If wtime == -1 wait forever for characters.
379 */
380 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100381mch_inchar(
382 char_u *buf,
383 int maxlen,
384 long wtime, /* don't use "time", MIPS cannot handle it */
385 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386{
387 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200389#ifdef MESSAGE_QUEUE
390 parse_queued_messages();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200391#endif
392
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 /* Check if window changed size while we were busy, perhaps the ":set
394 * columns=99" command was used. */
395 while (do_resize)
396 handle_resize();
397
398 if (wtime >= 0)
399 {
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200400 while (!WaitForChar(wtime)) /* no character available */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 {
Bram Moolenaar5d8afeb2015-11-19 19:55:16 +0100402 if (do_resize)
403 handle_resize();
404#ifdef FEAT_CLIENTSERVER
405 else if (!server_waiting())
406#else
407 else
408#endif
409 /* return if not interrupted by resize or server */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 return 0;
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200411#ifdef MESSAGE_QUEUE
412 parse_queued_messages();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 }
415 }
416 else /* wtime == -1 */
417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 /*
419 * If there is no character available within 'updatetime' seconds
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000420 * flush all the swap files to disk.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 * Also done when interrupted by SIGWINCH.
422 */
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200423 if (!WaitForChar(p_ut))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 {
425#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +0000426 if (trigger_cursorhold() && maxlen >= 3
427 && !typebuf_changed(tb_change_cnt))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 {
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000429 buf[0] = K_SPECIAL;
430 buf[1] = KS_EXTRA;
431 buf[2] = (int)KE_CURSORHOLD;
432 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434#endif
Bram Moolenaard4098f52005-06-27 22:37:13 +0000435 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 }
437 }
438
439 for (;;) /* repeat until we got a character */
440 {
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +0200441 long wtime_now = -1L;
442
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 while (do_resize) /* window changed size */
444 handle_resize();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200445
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200446#ifdef MESSAGE_QUEUE
447 parse_queued_messages();
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +0200448
449# ifdef FEAT_JOB_CHANNEL
450 if (has_pending_job())
451 {
452 /* Don't wait longer than a few seconds, checking for a finished
453 * job requires polling. */
454 if (p_ut > 9000L)
455 wtime_now = 1000L;
456 else
457 wtime_now = 10000L - p_ut;
458 }
459# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200462 * We want to be interrupted by the winch signal
463 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 */
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +0200465 if (!WaitForChar(wtime_now))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200466 {
467 if (do_resize) /* interrupted by SIGWINCH signal */
468 handle_resize();
469 return 0;
470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471
472 /* If input was put directly in typeahead buffer bail out here. */
473 if (typebuf_changed(tb_change_cnt))
474 return 0;
475
476 /*
477 * For some terminals we only get one character at a time.
478 * We want the get all available characters, so we could keep on
479 * trying until none is available
480 * For some other terminals this is quite slow, that's why we don't do
481 * it.
482 */
483 len = read_from_input_buf(buf, (long)maxlen);
484 if (len > 0)
485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 return len;
487 }
488 }
489}
490
491 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100492handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 do_resize = FALSE;
495 shell_resized();
496}
497
498/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200499 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 */
501 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100502mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503{
504 return WaitForChar(0L);
505}
506
507#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
508# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000509# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510# endif
511# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
512# include <sys/sysctl.h>
513# endif
514# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
515# include <sys/sysinfo.h>
516# endif
517
518/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000519 * Return total amount of memory available in Kbyte.
520 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100523mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525# ifdef __EMX__
Bram Moolenaar914572a2007-05-01 11:37:47 +0000526 return ulimit(3, 0L) >> 10; /* always 32MB? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527# else
528 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000529 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530
531# ifdef HAVE_SYSCTL
532 int mib[2], physmem;
533 size_t len;
534
535 /* BSD way of getting the amount of RAM available. */
536 mib[0] = CTL_HW;
537 mib[1] = HW_USERMEM;
538 len = sizeof(physmem);
539 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
540 mem = (long_u)physmem;
541# endif
542
543# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
544 if (mem == 0)
545 {
546 struct sysinfo sinfo;
547
548 /* Linux way of getting amount of RAM available */
549 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000550 {
551# ifdef HAVE_SYSINFO_MEM_UNIT
552 /* avoid overflow as much as possible */
553 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
554 {
555 sinfo.mem_unit = sinfo.mem_unit >> 1;
556 --shiftright;
557 }
558 mem = sinfo.totalram * sinfo.mem_unit;
559# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 mem = sinfo.totalram;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000561# endif
562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 }
564# endif
565
566# ifdef HAVE_SYSCONF
567 if (mem == 0)
568 {
569 long pagesize, pagecount;
570
571 /* Solaris way of getting amount of RAM available */
572 pagesize = sysconf(_SC_PAGESIZE);
573 pagecount = sysconf(_SC_PHYS_PAGES);
574 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000575 {
576 /* avoid overflow as much as possible */
577 while (shiftright > 0 && (pagesize & 1) == 0)
578 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000579 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000580 --shiftright;
581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 }
585# endif
586
587 /* Return the minimum of the physical memory and the user limit, because
588 * using more than the user limit may cause Vim to be terminated. */
589# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
590 {
591 struct rlimit rlp;
592
593 if (getrlimit(RLIMIT_DATA, &rlp) == 0
594 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
595# ifdef RLIM_INFINITY
596 && rlp.rlim_cur != RLIM_INFINITY
597# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000598 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000600 {
601 mem = (long_u)rlp.rlim_cur;
602 shiftright = 10;
603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 }
605# endif
606
607 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000608 return mem >> shiftright;
609 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610# endif
611}
612#endif
613
614 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100615mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616{
617 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000618#ifdef FEAT_MZSCHEME
619 long total = msec; /* remember original value */
620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
622 if (ignoreinput)
623 {
624 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000625 * here. But we don't want QUIT to kill us (CTRL-\ used in a
626 * shell may produce SIGQUIT). */
627 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 old_tmode = curr_tmode;
629 if (curr_tmode == TMODE_RAW)
630 settmode(TMODE_SLEEP);
631
632 /*
633 * Everybody sleeps in a different way...
634 * Prefer nanosleep(), some versions of usleep() can only sleep up to
635 * one second.
636 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000637#ifdef FEAT_MZSCHEME
638 do
639 {
640 /* if total is large enough, wait by portions in p_mzq */
641 if (total > p_mzq)
642 msec = p_mzq;
643 else
644 msec = total;
645 total -= msec;
646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647#ifdef HAVE_NANOSLEEP
648 {
649 struct timespec ts;
650
651 ts.tv_sec = msec / 1000;
652 ts.tv_nsec = (msec % 1000) * 1000000;
653 (void)nanosleep(&ts, NULL);
654 }
655#else
656# ifdef HAVE_USLEEP
657 while (msec >= 1000)
658 {
659 usleep((unsigned int)(999 * 1000));
660 msec -= 999;
661 }
662 usleep((unsigned int)(msec * 1000));
663# else
664# ifndef HAVE_SELECT
665 poll(NULL, 0, (int)msec);
666# else
667# ifdef __EMX__
668 _sleep2(msec);
669# else
670 {
671 struct timeval tv;
672
673 tv.tv_sec = msec / 1000;
674 tv.tv_usec = (msec % 1000) * 1000;
675 /*
676 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
677 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
678 */
679 select(0, NULL, NULL, NULL, &tv);
680 }
681# endif /* __EMX__ */
682# endif /* HAVE_SELECT */
683# endif /* HAVE_NANOSLEEP */
684#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000685#ifdef FEAT_MZSCHEME
686 }
687 while (total > 0);
688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689
690 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000691 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 }
693 else
694 WaitForChar(msec);
695}
696
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000697#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
699# define HAVE_CHECK_STACK_GROWTH
700/*
701 * Support for checking for an almost-out-of-stack-space situation.
702 */
703
704/*
705 * Return a pointer to an item on the stack. Used to find out if the stack
706 * grows up or down.
707 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100708static void check_stack_growth(char *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709static int stack_grows_downwards;
710
711/*
712 * Find out if the stack grows upwards or downwards.
713 * "p" points to a variable on the stack of the caller.
714 */
715 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100716check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717{
718 int i;
719
720 stack_grows_downwards = (p > (char *)&i);
721}
722#endif
723
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000724#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725static char *stack_limit = NULL;
726
727#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
728# include <pthread.h>
729# include <pthread_np.h>
730#endif
731
732/*
733 * Find out until how var the stack can grow without getting into trouble.
734 * Called when starting up and when switching to the signal stack in
735 * deathtrap().
736 */
737 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100738get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739{
740 struct rlimit rlp;
741 int i;
742 long lim;
743
744 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
745 * limit doesn't fit in a long (rlim_cur might be "long long"). */
746 if (getrlimit(RLIMIT_STACK, &rlp) == 0
747 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
748# ifdef RLIM_INFINITY
749 && rlp.rlim_cur != RLIM_INFINITY
750# endif
751 )
752 {
753 lim = (long)rlp.rlim_cur;
754#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
755 {
756 pthread_attr_t attr;
757 size_t size;
758
759 /* On FreeBSD the initial thread always has a fixed stack size, no
760 * matter what the limits are set to. Normally it's 1 Mbyte. */
761 pthread_attr_init(&attr);
762 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
763 {
764 pthread_attr_getstacksize(&attr, &size);
765 if (lim > (long)size)
766 lim = (long)size;
767 }
768 pthread_attr_destroy(&attr);
769 }
770#endif
771 if (stack_grows_downwards)
772 {
773 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
774 if (stack_limit >= (char *)&i)
775 /* overflow, set to 1/16 of current stack position */
776 stack_limit = (char *)((long)&i / 16L);
777 }
778 else
779 {
780 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
781 if (stack_limit <= (char *)&i)
782 stack_limit = NULL; /* overflow */
783 }
784 }
785}
786
787/*
788 * Return FAIL when running out of stack space.
789 * "p" must point to any variable local to the caller that's on the stack.
790 */
791 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100792mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794 if (stack_limit != NULL)
795 {
796 if (stack_grows_downwards)
797 {
798 if (p < stack_limit)
799 return FAIL;
800 }
801 else if (p > stack_limit)
802 return FAIL;
803 }
804 return OK;
805}
806#endif
807
808#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
809/*
810 * Support for using the signal stack.
811 * This helps when we run out of stack space, which causes a SIGSEGV. The
812 * signal handler then must run on another stack, since the normal stack is
813 * completely full.
814 */
815
Bram Moolenaar39766a72013-11-03 00:41:00 +0100816#if defined(HAVE_AVAILABILITYMACROS_H)
Bram Moolenaar4cc95d12013-11-02 21:49:32 +0100817# include <AvailabilityMacros.h>
818#endif
819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820#ifndef SIGSTKSZ
821# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
822#endif
823
824# ifdef HAVE_SIGALTSTACK
825static stack_t sigstk; /* for sigaltstack() */
826# else
827static struct sigstack sigstk; /* for sigstack() */
828# endif
829
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100830static void init_signal_stack(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831static char *signal_stack;
832
833 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100834init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
836 if (signal_stack != NULL)
837 {
838# ifdef HAVE_SIGALTSTACK
Bram Moolenaar1a3d0862007-08-30 09:47:38 +0000839# if defined(__APPLE__) && (!defined(MAC_OS_X_VERSION_MAX_ALLOWED) \
840 || MAC_OS_X_VERSION_MAX_ALLOWED <= 1040)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 /* missing prototype. Adding it to osdef?.h.in doesn't work, because
842 * "struct sigaltstack" needs to be declared. */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100843 extern int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844# endif
845
846# ifdef HAVE_SS_BASE
847 sigstk.ss_base = signal_stack;
848# else
849 sigstk.ss_sp = signal_stack;
850# endif
851 sigstk.ss_size = SIGSTKSZ;
852 sigstk.ss_flags = 0;
853 (void)sigaltstack(&sigstk, NULL);
854# else
855 sigstk.ss_sp = signal_stack;
856 if (stack_grows_downwards)
857 sigstk.ss_sp += SIGSTKSZ - 1;
858 sigstk.ss_onstack = 0;
859 (void)sigstack(&sigstk, NULL);
860# endif
861 }
862}
863#endif
864
865/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000866 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 * will barf when the second argument to signal() is ``wrong''.
868 * Let me try it with a few tricky defines from my own osdef.h (jw).
869 */
870#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 static RETSIGTYPE
872sig_winch SIGDEFARG(sigarg)
873{
874 /* this is not required on all systems, but it doesn't hurt anybody */
875 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
876 do_resize = TRUE;
877 SIGRETURN;
878}
879#endif
880
881#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 static RETSIGTYPE
883catch_sigint SIGDEFARG(sigarg)
884{
885 /* this is not required on all systems, but it doesn't hurt anybody */
886 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
887 got_int = TRUE;
888 SIGRETURN;
889}
890#endif
891
892#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 static RETSIGTYPE
894catch_sigpwr SIGDEFARG(sigarg)
895{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000896 /* this is not required on all systems, but it doesn't hurt anybody */
897 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 /*
899 * I'm not sure we get the SIGPWR signal when the system is really going
900 * down or when the batteries are almost empty. Just preserve the swap
901 * files and don't exit, that can't do any harm.
902 */
903 ml_sync_all(FALSE, FALSE);
904 SIGRETURN;
905}
906#endif
907
908#ifdef SET_SIG_ALARM
909/*
910 * signal function for alarm().
911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 static RETSIGTYPE
913sig_alarm SIGDEFARG(sigarg)
914{
915 /* doesn't do anything, just to break a system call */
916 sig_alarm_called = TRUE;
917 SIGRETURN;
918}
919#endif
920
Bram Moolenaar44ecf652005-03-07 23:09:59 +0000921#if (defined(HAVE_SETJMP_H) \
922 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
923 || defined(FEAT_LIBCALL))) \
924 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925/*
926 * A simplistic version of setjmp() that only allows one level of using.
927 * Don't call twice before calling mch_endjmp()!.
928 * Usage:
929 * mch_startjmp();
930 * if (SETJMP(lc_jump_env) != 0)
931 * {
932 * mch_didjmp();
933 * EMSG("crash!");
934 * }
935 * else
936 * {
937 * do_the_work;
938 * mch_endjmp();
939 * }
940 * Note: Can't move SETJMP() here, because a function calling setjmp() must
941 * not return before the saved environment is used.
942 * Returns OK for normal return, FAIL when the protected code caused a
943 * problem and LONGJMP() was used.
944 */
945 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100946mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
948#ifdef SIGHASARG
949 lc_signal = 0;
950#endif
951 lc_active = TRUE;
952}
953
954 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100955mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
957 lc_active = FALSE;
958}
959
960 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100961mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962{
963# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
964 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
965 * otherwise catching the signal only works once. */
966 init_signal_stack();
967# endif
968}
969#endif
970
971/*
972 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200973 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200975 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
976 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 */
978 static RETSIGTYPE
979deathtrap SIGDEFARG(sigarg)
980{
981 static int entered = 0; /* count the number of times we got here.
982 Note: when memory has been corrupted
983 this may get an arbitrary value! */
984#ifdef SIGHASARG
985 int i;
986#endif
987
988#if defined(HAVE_SETJMP_H)
989 /*
990 * Catch a crash in protected code.
991 * Restores the environment saved in lc_jump_env, which looks like
992 * SETJMP() returns 1.
993 */
994 if (lc_active)
995 {
996# if defined(SIGHASARG)
997 lc_signal = sigarg;
998# endif
999 lc_active = FALSE; /* don't jump again */
1000 LONGJMP(lc_jump_env, 1);
1001 /* NOTREACHED */
1002 }
1003#endif
1004
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001005#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001006# ifdef SIGQUIT
1007 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1008 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1009 * pressing CTRL-\, but we don't want Vim to exit then. */
1010 if (in_mch_delay && sigarg == SIGQUIT)
1011 SIGRETURN;
1012# endif
1013
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001014 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1015 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1016 * free(). Calling free() again may then cause a crash. */
1017 if (entered == 0
1018 && (0
1019# ifdef SIGHUP
1020 || sigarg == SIGHUP
1021# endif
1022# ifdef SIGQUIT
1023 || sigarg == SIGQUIT
1024# endif
1025# ifdef SIGTERM
1026 || sigarg == SIGTERM
1027# endif
1028# ifdef SIGPWR
1029 || sigarg == SIGPWR
1030# endif
1031# ifdef SIGUSR1
1032 || sigarg == SIGUSR1
1033# endif
1034# ifdef SIGUSR2
1035 || sigarg == SIGUSR2
1036# endif
1037 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001038 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001039 SIGRETURN;
1040#endif
1041
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 /* Remember how often we have been called. */
1043 ++entered;
1044
1045#ifdef FEAT_EVAL
1046 /* Set the v:dying variable. */
1047 set_vim_var_nr(VV_DYING, (long)entered);
1048#endif
1049
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001050#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 /* Since we are now using the signal stack, need to reset the stack
1052 * limit. Otherwise using a regexp will fail. */
1053 get_stack_limit();
1054#endif
1055
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001056#if 0
1057 /* This is for opening gdb the moment Vim crashes.
1058 * You need to manually adjust the file name and Vim executable name.
1059 * Suggested by SungHyun Nam. */
1060 {
1061# define VI_GDB_FILE "/tmp/vimgdb"
1062# define VIM_NAME "/usr/bin/vim"
1063 FILE *fp = fopen(VI_GDB_FILE, "w");
1064 if (fp)
1065 {
1066 fprintf(fp,
1067 "file %s\n"
1068 "attach %d\n"
1069 "set height 1000\n"
1070 "bt full\n"
1071 , VIM_NAME, getpid());
1072 fclose(fp);
1073 system("xterm -e gdb -x "VI_GDB_FILE);
1074 unlink(VI_GDB_FILE);
1075 }
1076 }
1077#endif
1078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079#ifdef SIGHASARG
1080 /* try to find the name of this signal */
1081 for (i = 0; signal_info[i].sig != -1; i++)
1082 if (sigarg == signal_info[i].sig)
1083 break;
1084 deadly_signal = sigarg;
1085#endif
1086
1087 full_screen = FALSE; /* don't write message to the GUI, it might be
1088 * part of the problem... */
1089 /*
1090 * If something goes wrong after entering here, we may get here again.
1091 * When this happens, give a message and try to exit nicely (resetting the
1092 * terminal mode, etc.)
1093 * When this happens twice, just exit, don't even try to give a message,
1094 * stack may be corrupt or something weird.
1095 * When this still happens again (or memory was corrupted in such a way
1096 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1097 */
1098 if (entered >= 3)
1099 {
1100 reset_signals(); /* don't catch any signals anymore */
1101 may_core_dump();
1102 if (entered >= 4)
1103 _exit(8);
1104 exit(7);
1105 }
1106 if (entered == 2)
1107 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001108 /* No translation, it may call malloc(). */
1109 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 out_flush();
1111 getout(1);
1112 }
1113
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001114 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001116 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 signal_info[i].name);
1118#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001119 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001121
1122 /* Preserve files and exit. This sets the really_exiting flag to prevent
1123 * calling free(). */
1124 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
Bram Moolenaar009b2592004-10-24 19:18:58 +00001126#ifdef NBDEBUG
1127 reset_signals();
1128 may_core_dump();
1129 abort();
1130#endif
1131
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 SIGRETURN;
1133}
1134
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001135#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136/*
1137 * On Solaris with multi-threading, suspending might not work immediately.
1138 * Catch the SIGCONT signal, which will be used as an indication whether the
1139 * suspending has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001140 *
1141 * On Linux, signal is not always handled immediately either.
1142 * See https://bugs.launchpad.net/bugs/291373
1143 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001144 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001146static volatile int sigcont_received;
Bram Moolenaard99df422016-01-29 23:20:40 +01001147static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148
1149/*
1150 * signal handler for SIGCONT
1151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 static RETSIGTYPE
1153sigcont_handler SIGDEFARG(sigarg)
1154{
1155 sigcont_received = TRUE;
1156 SIGRETURN;
1157}
1158#endif
1159
Bram Moolenaar62b42182010-09-21 22:09:37 +02001160# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001161static void loose_clipboard(void);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001162# ifdef USE_SYSTEM
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001163static void save_clipboard(void);
1164static void restore_clipboard(void);
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001165
1166static void *clip_star_save = NULL;
1167static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001168# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001169
1170/*
1171 * Called when Vim is going to sleep or execute a shell command.
1172 * We can't respond to requests for the X selections. Lose them, otherwise
1173 * other applications will hang. But first copy the text to cut buffer 0.
1174 */
1175 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001176loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001177{
1178 if (clip_star.owned || clip_plus.owned)
1179 {
1180 x11_export_final_selection();
1181 if (clip_star.owned)
1182 clip_lose_selection(&clip_star);
1183 if (clip_plus.owned)
1184 clip_lose_selection(&clip_plus);
1185 if (x11_display != NULL)
1186 XFlush(x11_display);
1187 }
1188}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001189
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001190# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001191/*
1192 * Save clipboard text to restore later.
1193 */
1194 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001195save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001196{
1197 if (clip_star.owned)
1198 clip_star_save = get_register('*', TRUE);
1199 if (clip_plus.owned)
1200 clip_plus_save = get_register('+', TRUE);
1201}
1202
1203/*
1204 * Restore clipboard text if no one own the X selection.
1205 */
1206 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001207restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001208{
1209 if (clip_star_save != NULL)
1210 {
1211 if (!clip_gen_owner_exists(&clip_star))
1212 put_register('*', clip_star_save);
1213 else
1214 free_register(clip_star_save);
1215 clip_star_save = NULL;
1216 }
1217 if (clip_plus_save != NULL)
1218 {
1219 if (!clip_gen_owner_exists(&clip_plus))
1220 put_register('+', clip_plus_save);
1221 else
1222 free_register(clip_plus_save);
1223 clip_plus_save = NULL;
1224 }
1225}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001226# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001227#endif
1228
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229/*
1230 * If the machine has job control, use it to suspend the program,
1231 * otherwise fake it by starting a new shell.
1232 */
1233 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001234mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235{
1236 /* BeOS does have SIGTSTP, but it doesn't work. */
1237#if defined(SIGTSTP) && !defined(__BEOS__)
1238 out_flush(); /* needed to make cursor visible on some systems */
1239 settmode(TMODE_COOK);
1240 out_flush(); /* needed to disable mouse on some systems */
1241
1242# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001243 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244# endif
1245
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001246# if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 sigcont_received = FALSE;
1248# endif
1249 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001250# if defined(_REENTRANT) && defined(SIGCONT)
1251 /*
1252 * Wait for the SIGCONT signal to be handled. It generally happens
1253 * immediately, but somehow not all the time. Do not call pause()
1254 * because there would be race condition which would hang Vim if
1255 * signal happened in between the test of sigcont_received and the
1256 * call to pause(). If signal is not yet received, call sleep(0)
1257 * to just yield CPU. Signal should then be received. If somehow
1258 * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting
1259 * further if signal is not received after 1+2+3+4 ms (not expected
1260 * to happen).
1261 */
1262 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001263 long wait_time;
1264 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001265 /* Loop is not entered most of the time */
Bram Moolenaar262735e2009-07-14 10:20:22 +00001266 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268# endif
1269
1270# ifdef FEAT_TITLE
1271 /*
1272 * Set oldtitle to NULL, so the current title is obtained again.
1273 */
1274 vim_free(oldtitle);
1275 oldtitle = NULL;
1276# endif
1277 settmode(TMODE_RAW);
1278 need_check_timestamps = TRUE;
1279 did_check_timestamps = FALSE;
1280#else
1281 suspend_shell();
1282#endif
1283}
1284
1285 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001286mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287{
1288 Columns = 80;
1289 Rows = 24;
1290
1291 out_flush();
1292 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001293
Bram Moolenaar56718732006-03-15 22:53:57 +00001294#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001295 mac_conv_init();
1296#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001297#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1298 win_clip_init();
1299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300}
1301
1302 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001303set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304{
1305#if defined(SIGWINCH)
1306 /*
1307 * WINDOW CHANGE signal is handled with sig_winch().
1308 */
1309 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1310#endif
1311
1312 /*
1313 * We want the STOP signal to work, to make mch_suspend() work.
1314 * For "rvim" the STOP signal is ignored.
1315 */
1316#ifdef SIGTSTP
1317 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1318#endif
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001319#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 signal(SIGCONT, sigcont_handler);
1321#endif
1322
1323 /*
1324 * We want to ignore breaking of PIPEs.
1325 */
1326#ifdef SIGPIPE
1327 signal(SIGPIPE, SIG_IGN);
1328#endif
1329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001331 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332#endif
1333
1334 /*
1335 * Ignore alarm signals (Perl's alarm() generates it).
1336 */
1337#ifdef SIGALRM
1338 signal(SIGALRM, SIG_IGN);
1339#endif
1340
1341 /*
1342 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1343 * work will be lost.
1344 */
1345#ifdef SIGPWR
1346 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1347#endif
1348
1349 /*
1350 * Arrange for other signals to gracefully shutdown Vim.
1351 */
1352 catch_signals(deathtrap, SIG_ERR);
1353
1354#if defined(FEAT_GUI) && defined(SIGHUP)
1355 /*
1356 * When the GUI is running, ignore the hangup signal.
1357 */
1358 if (gui.in_use)
1359 signal(SIGHUP, SIG_IGN);
1360#endif
1361}
1362
Bram Moolenaardf177f62005-02-22 08:39:57 +00001363#if defined(SIGINT) || defined(PROTO)
1364/*
1365 * Catch CTRL-C (only works while in Cooked mode).
1366 */
1367 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001368catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001369{
1370 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1371}
1372#endif
1373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001375reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
1377 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001378#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 /* SIGCONT isn't in the list, because its default action is ignore */
1380 signal(SIGCONT, SIG_DFL);
1381#endif
1382}
1383
1384 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001385catch_signals(
1386 RETSIGTYPE (*func_deadly)(),
1387 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 int i;
1390
1391 for (i = 0; signal_info[i].sig != -1; i++)
1392 if (signal_info[i].deadly)
1393 {
1394#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1395 struct sigaction sa;
1396
1397 /* Setup to use the alternate stack for the signal function. */
1398 sa.sa_handler = func_deadly;
1399 sigemptyset(&sa.sa_mask);
1400# if defined(__linux__) && defined(_REENTRANT)
1401 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1402 * thread handling in combination with using the alternate stack:
1403 * pthread library functions try to use the stack pointer to
1404 * identify the current thread, causing a SEGV signal, which
1405 * recursively calls deathtrap() and hangs. */
1406 sa.sa_flags = 0;
1407# else
1408 sa.sa_flags = SA_ONSTACK;
1409# endif
1410 sigaction(signal_info[i].sig, &sa, NULL);
1411#else
1412# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1413 struct sigvec sv;
1414
1415 /* Setup to use the alternate stack for the signal function. */
1416 sv.sv_handler = func_deadly;
1417 sv.sv_mask = 0;
1418 sv.sv_flags = SV_ONSTACK;
1419 sigvec(signal_info[i].sig, &sv, NULL);
1420# else
1421 signal(signal_info[i].sig, func_deadly);
1422# endif
1423#endif
1424 }
1425 else if (func_other != SIG_ERR)
1426 signal(signal_info[i].sig, func_other);
1427}
1428
1429/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001430 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001431 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1432 * return TRUE
1433 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1434 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001435 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001436 * Returns TRUE when Vim should exit.
1437 */
1438 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001439vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001440{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001441 static int got_signal = 0;
1442 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001443
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001444 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001445 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001446 case SIGNAL_BLOCK: blocked = TRUE;
1447 break;
1448
1449 case SIGNAL_UNBLOCK: blocked = FALSE;
1450 if (got_signal != 0)
1451 {
1452 kill(getpid(), got_signal);
1453 got_signal = 0;
1454 }
1455 break;
1456
1457 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001458 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001459 got_signal = sig;
1460#ifdef SIGPWR
1461 if (sig != SIGPWR)
1462#endif
1463 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001464 break;
1465 }
1466 return FALSE;
1467}
1468
1469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 * Check_win checks whether we have an interactive stdout.
1471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001473mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if (isatty(1))
1476 return OK;
1477 return FAIL;
1478}
1479
1480/*
1481 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1482 */
1483 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001484mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485{
1486 if (isatty(read_cmd_fd))
1487 return TRUE;
1488 return FALSE;
1489}
1490
1491#ifdef FEAT_X11
1492
1493# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) \
1494 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1495
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001496static void xopen_message(struct timeval *tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
1498/*
1499 * Give a message about the elapsed time for opening the X window.
1500 */
1501 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001502xopen_message(
1503 struct timeval *tvp) /* must contain start time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 struct timeval end_tv;
1506
1507 /* Compute elapsed time. */
1508 gettimeofday(&end_tv, NULL);
1509 smsg((char_u *)_("Opening the X display took %ld msec"),
1510 (end_tv.tv_sec - tvp->tv_sec) * 1000L
Bram Moolenaar051b7822005-05-19 21:00:46 +00001511 + (end_tv.tv_usec - tvp->tv_usec) / 1000L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512}
1513# endif
1514#endif
1515
1516#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1517/*
1518 * A few functions shared by X11 title and clipboard code.
1519 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001520static int x_error_handler(Display *dpy, XErrorEvent *error_event);
1521static int x_error_check(Display *dpy, XErrorEvent *error_event);
1522static int x_connect_to_server(void);
1523static int test_x11_window(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
1525static int got_x_error = FALSE;
1526
1527/*
1528 * X Error handler, otherwise X just exits! (very rude) -- webb
1529 */
1530 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001531x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001533 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 STRCAT(IObuff, _("\nVim: Got X error\n"));
1535
1536 /* We cannot print a message and continue, because no X calls are allowed
1537 * here (causes my system to hang). Silently continuing might be an
1538 * alternative... */
1539 preserve_exit(); /* preserve files and exit */
1540
1541 return 0; /* NOTREACHED */
1542}
1543
1544/*
1545 * Another X Error handler, just used to check for errors.
1546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001548x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
1550 got_x_error = TRUE;
1551 return 0;
1552}
1553
1554#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1555# if defined(HAVE_SETJMP_H)
1556/*
1557 * An X IO Error handler, used to catch error while opening the display.
1558 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001559static int x_IOerror_check(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001562x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
1564 /* This function should not return, it causes exit(). Longjump instead. */
1565 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001566# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001567 return 0; /* avoid the compiler complains about missing return value */
1568# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569}
1570# endif
1571
1572/*
1573 * An X IO Error handler, used to catch terminal errors.
1574 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001575static int x_IOerror_handler(Display *dpy);
1576static void may_restore_clipboard(void);
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001577static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001580x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001583 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 x11_window = 0;
1585 x11_display = NULL;
1586 xterm_Shell = (Widget)0;
1587
1588 /* This function should not return, it causes exit(). Longjump instead. */
1589 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001590# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001591 return 0; /* avoid the compiler complains about missing return value */
1592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001594
1595/*
1596 * If the X11 connection was lost try to restore it.
1597 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001598 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001599 */
1600 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001601may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001602{
1603 if (xterm_dpy_was_reset)
1604 {
1605 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001606
1607# ifndef LESSTIF_VERSION
1608 /* This has been reported to avoid Vim getting stuck. */
1609 if (app_context != (XtAppContext)NULL)
1610 {
1611 XtDestroyApplicationContext(app_context);
1612 app_context = (XtAppContext)NULL;
1613 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1614 }
1615# endif
1616
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001617 setup_term_clip();
1618 get_x11_title(FALSE);
1619 }
1620}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621#endif
1622
1623/*
1624 * Return TRUE when connection to the X server is desired.
1625 */
1626 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001627x_connect_to_server(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629#if defined(FEAT_CLIENTSERVER)
1630 if (x_force_connect)
1631 return TRUE;
1632#endif
1633 if (x_no_connect)
1634 return FALSE;
1635
1636 /* Check for a match with "exclude:" from 'clipboard'. */
1637 if (clip_exclude_prog != NULL)
1638 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001639 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 return FALSE;
1641 }
1642 return TRUE;
1643}
1644
1645/*
1646 * Test if "dpy" and x11_window are valid by getting the window title.
1647 * I don't actually want it yet, so there may be a simpler call to use, but
1648 * this will cause the error handler x_error_check() to be called if anything
1649 * is wrong, such as the window pointer being invalid (as can happen when the
1650 * user changes his DISPLAY, but not his WINDOWID) -- webb
1651 */
1652 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001653test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654{
1655 int (*old_handler)();
1656 XTextProperty text_prop;
1657
1658 old_handler = XSetErrorHandler(x_error_check);
1659 got_x_error = FALSE;
1660 if (XGetWMName(dpy, x11_window, &text_prop))
1661 XFree((void *)text_prop.value);
1662 XSync(dpy, False);
1663 (void)XSetErrorHandler(old_handler);
1664
1665 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001666 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
1668 return (got_x_error ? FAIL : OK);
1669}
1670#endif
1671
1672#ifdef FEAT_TITLE
1673
1674#ifdef FEAT_X11
1675
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001676static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678/*
1679 * try to get x11 window and display
1680 *
1681 * return FAIL for failure, OK otherwise
1682 */
1683 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001684get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685{
1686 char *winid;
1687 static int result = -1;
1688#define XD_NONE 0 /* x11_display not set here */
1689#define XD_HERE 1 /* x11_display opened here */
1690#define XD_GUI 2 /* x11_display used from gui.dpy */
1691#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1692 static int x11_display_from = XD_NONE;
1693 static int did_set_error_handler = FALSE;
1694
1695 if (!did_set_error_handler)
1696 {
1697 /* X just exits if it finds an error otherwise! */
1698 (void)XSetErrorHandler(x_error_handler);
1699 did_set_error_handler = TRUE;
1700 }
1701
Bram Moolenaar9372a112005-12-06 19:59:18 +00001702#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 if (gui.in_use)
1704 {
1705 /*
1706 * If the X11 display was opened here before, for the window where Vim
1707 * was started, close that one now to avoid a memory leak.
1708 */
1709 if (x11_display_from == XD_HERE && x11_display != NULL)
1710 {
1711 XCloseDisplay(x11_display);
1712 x11_display_from = XD_NONE;
1713 }
1714 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1715 {
1716 x11_display_from = XD_GUI;
1717 return OK;
1718 }
1719 x11_display = NULL;
1720 return FAIL;
1721 }
1722 else if (x11_display_from == XD_GUI)
1723 {
1724 /* GUI must have stopped somehow, clear x11_display */
1725 x11_window = 0;
1726 x11_display = NULL;
1727 x11_display_from = XD_NONE;
1728 }
1729#endif
1730
1731 /* When started with the "-X" argument, don't try connecting. */
1732 if (!x_connect_to_server())
1733 return FAIL;
1734
1735 /*
1736 * If WINDOWID not set, should try another method to find out
1737 * what the current window number is. The only code I know for
1738 * this is very complicated.
1739 * We assume that zero is invalid for WINDOWID.
1740 */
1741 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1742 x11_window = (Window)atol(winid);
1743
1744#ifdef FEAT_XCLIPBOARD
1745 if (xterm_dpy != NULL && x11_window != 0)
1746 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001747 /* We may have checked it already, but Gnome terminal can move us to
1748 * another window, so we need to check every time. */
1749 if (x11_display_from != XD_XTERM)
1750 {
1751 /*
1752 * If the X11 display was opened here before, for the window where
1753 * Vim was started, close that one now to avoid a memory leak.
1754 */
1755 if (x11_display_from == XD_HERE && x11_display != NULL)
1756 XCloseDisplay(x11_display);
1757 x11_display = xterm_dpy;
1758 x11_display_from = XD_XTERM;
1759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 if (test_x11_window(x11_display) == FAIL)
1761 {
1762 /* probably bad $WINDOWID */
1763 x11_window = 0;
1764 x11_display = NULL;
1765 x11_display_from = XD_NONE;
1766 return FAIL;
1767 }
1768 return OK;
1769 }
1770#endif
1771
1772 if (x11_window == 0 || x11_display == NULL)
1773 result = -1;
1774
1775 if (result != -1) /* Have already been here and set this */
1776 return result; /* Don't do all these X calls again */
1777
1778 if (x11_window != 0 && x11_display == NULL)
1779 {
1780#ifdef SET_SIG_ALARM
1781 RETSIGTYPE (*sig_save)();
1782#endif
1783#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1784 struct timeval start_tv;
1785
1786 if (p_verbose > 0)
1787 gettimeofday(&start_tv, NULL);
1788#endif
1789
1790#ifdef SET_SIG_ALARM
1791 /*
1792 * Opening the Display may hang if the DISPLAY setting is wrong, or
1793 * the network connection is bad. Set an alarm timer to get out.
1794 */
1795 sig_alarm_called = FALSE;
1796 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1797 (RETSIGTYPE (*)())sig_alarm);
1798 alarm(2);
1799#endif
1800 x11_display = XOpenDisplay(NULL);
1801
1802#ifdef SET_SIG_ALARM
1803 alarm(0);
1804 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1805 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001806 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#endif
1808 if (x11_display != NULL)
1809 {
1810# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1811 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001812 {
1813 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 xopen_message(&start_tv);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001815 verbose_leave();
1816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817# endif
1818 if (test_x11_window(x11_display) == FAIL)
1819 {
1820 /* Maybe window id is bad */
1821 x11_window = 0;
1822 XCloseDisplay(x11_display);
1823 x11_display = NULL;
1824 }
1825 else
1826 x11_display_from = XD_HERE;
1827 }
1828 }
1829 if (x11_window == 0 || x11_display == NULL)
1830 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001831
1832# ifdef FEAT_EVAL
1833 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1834# endif
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 return (result = OK);
1837}
1838
1839/*
1840 * Determine original x11 Window Title
1841 */
1842 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001843get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001845 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846}
1847
1848/*
1849 * Determine original x11 Window icon
1850 */
1851 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001852get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853{
1854 int retval = FALSE;
1855
1856 retval = get_x11_thing(FALSE, test_only);
1857
1858 /* could not get old icon, use terminal name */
1859 if (oldicon == NULL && !test_only)
1860 {
1861 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001862 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001864 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 }
1866
1867 return retval;
1868}
1869
1870 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001871get_x11_thing(
1872 int get_title, /* get title string */
1873 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874{
1875 XTextProperty text_prop;
1876 int retval = FALSE;
1877 Status status;
1878
1879 if (get_x11_windis() == OK)
1880 {
1881 /* Get window/icon name if any */
1882 if (get_title)
1883 status = XGetWMName(x11_display, x11_window, &text_prop);
1884 else
1885 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1886
1887 /*
1888 * If terminal is xterm, then x11_window may be a child window of the
1889 * outer xterm window that actually contains the window/icon name, so
1890 * keep traversing up the tree until a window with a title/icon is
1891 * found.
1892 */
1893 /* Previously this was only done for xterm and alikes. I don't see a
1894 * reason why it would fail for other terminal emulators.
1895 * if (term_is_xterm) */
1896 {
1897 Window root;
1898 Window parent;
1899 Window win = x11_window;
1900 Window *children;
1901 unsigned int num_children;
1902
1903 while (!status || text_prop.value == NULL)
1904 {
1905 if (!XQueryTree(x11_display, win, &root, &parent, &children,
1906 &num_children))
1907 break;
1908 if (children)
1909 XFree((void *)children);
1910 if (parent == root || parent == 0)
1911 break;
1912
1913 win = parent;
1914 if (get_title)
1915 status = XGetWMName(x11_display, win, &text_prop);
1916 else
1917 status = XGetWMIconName(x11_display, win, &text_prop);
1918 }
1919 }
1920 if (status && text_prop.value != NULL)
1921 {
1922 retval = TRUE;
1923 if (!test_only)
1924 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001925#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
1926 if (text_prop.encoding == XA_STRING
1927# ifdef FEAT_MBYTE
1928 && !has_mbyte
1929# endif
1930 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
1932#endif
1933 if (get_title)
1934 oldtitle = vim_strsave((char_u *)text_prop.value);
1935 else
1936 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001937#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939 else
1940 {
1941 char **cl;
1942 Status transform_status;
1943 int n = 0;
1944
1945 transform_status = XmbTextPropertyToTextList(x11_display,
1946 &text_prop,
1947 &cl, &n);
1948 if (transform_status >= Success && n > 0 && cl[0])
1949 {
1950 if (get_title)
1951 oldtitle = vim_strsave((char_u *) cl[0]);
1952 else
1953 oldicon = vim_strsave((char_u *) cl[0]);
1954 XFreeStringList(cl);
1955 }
1956 else
1957 {
1958 if (get_title)
1959 oldtitle = vim_strsave((char_u *)text_prop.value);
1960 else
1961 oldicon = vim_strsave((char_u *)text_prop.value);
1962 }
1963 }
1964#endif
1965 }
1966 XFree((void *)text_prop.value);
1967 }
1968 }
1969 return retval;
1970}
1971
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02001972/* Xutf8 functions are not avaialble on older systems. Note that on some
1973 * systems X_HAVE_UTF8_STRING may be defined in a header file but
1974 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
1975 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02001977# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978# define USE_UTF8_STRING
1979# endif
1980#endif
1981
1982/*
1983 * Set x11 Window Title
1984 *
1985 * get_x11_windis() must be called before this and have returned OK
1986 */
1987 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001988set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
1990 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
1991 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
1992 * supported everywhere and STRING doesn't work for multi-byte titles.
1993 */
1994#ifdef USE_UTF8_STRING
1995 if (enc_utf8)
1996 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
1997 NULL, NULL, 0, NULL, NULL, NULL);
1998 else
1999#endif
2000 {
2001#if XtSpecificationRelease >= 4
2002# ifdef FEAT_XFONTSET
2003 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2004 NULL, NULL, 0, NULL, NULL, NULL);
2005# else
2006 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002007 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008
2009 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002010 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 XSetWMProperties(x11_display, x11_window, &text_prop,
2012 NULL, NULL, 0, NULL, NULL, NULL);
2013# endif
2014#else
2015 XStoreName(x11_display, x11_window, (char *)title);
2016#endif
2017 }
2018 XFlush(x11_display);
2019}
2020
2021/*
2022 * Set x11 Window icon
2023 *
2024 * get_x11_windis() must be called before this and have returned OK
2025 */
2026 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002027set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
2029 /* See above for comments about using X*SetWMProperties(). */
2030#ifdef USE_UTF8_STRING
2031 if (enc_utf8)
2032 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2033 NULL, 0, NULL, NULL, NULL);
2034 else
2035#endif
2036 {
2037#if XtSpecificationRelease >= 4
2038# ifdef FEAT_XFONTSET
2039 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2040 NULL, 0, NULL, NULL, NULL);
2041# else
2042 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002043 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002045 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2047 NULL, 0, NULL, NULL, NULL);
2048# endif
2049#else
2050 XSetIconName(x11_display, x11_window, (char *)icon);
2051#endif
2052 }
2053 XFlush(x11_display);
2054}
2055
2056#else /* FEAT_X11 */
2057
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002059get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060{
2061 return FALSE;
2062}
2063
2064 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002065get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066{
2067 if (!test_only)
2068 {
2069 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002070 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002072 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 }
2074 return FALSE;
2075}
2076
2077#endif /* FEAT_X11 */
2078
2079 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002080mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081{
2082 return get_x11_title(TRUE);
2083}
2084
2085 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002086mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087{
2088 return get_x11_icon(TRUE);
2089}
2090
2091/*
2092 * Set the window title and icon.
2093 */
2094 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002095mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096{
2097 int type = 0;
2098 static int recursive = 0;
2099
2100 if (T_NAME == NULL) /* no terminal name (yet) */
2101 return;
2102 if (title == NULL && icon == NULL) /* nothing to do */
2103 return;
2104
2105 /* When one of the X11 functions causes a deadly signal, we get here again
2106 * recursively. Avoid hanging then (something is probably locked). */
2107 if (recursive)
2108 return;
2109 ++recursive;
2110
2111 /*
2112 * if the window ID and the display is known, we may use X11 calls
2113 */
2114#ifdef FEAT_X11
2115 if (get_x11_windis() == OK)
2116 type = 1;
2117#else
2118# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2119 if (gui.in_use)
2120 type = 1;
2121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122#endif
2123
2124 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002125 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 * than x11 calls, because the x11 calls don't always work
2127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 if ((type || *T_TS != NUL) && title != NULL)
2129 {
2130 if (oldtitle == NULL
2131#ifdef FEAT_GUI
2132 && !gui.in_use
2133#endif
2134 ) /* first call but not in GUI, save title */
2135 (void)get_x11_title(FALSE);
2136
2137 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2138 term_settitle(title);
2139#ifdef FEAT_X11
2140 else
2141# ifdef FEAT_GUI_GTK
2142 if (!gui.in_use) /* don't do this if GTK+ is running */
2143# endif
2144 set_x11_title(title); /* x11 */
2145#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002146#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2148 else
2149 gui_mch_settitle(title, icon);
2150#endif
2151 did_set_title = TRUE;
2152 }
2153
2154 if ((type || *T_CIS != NUL) && icon != NULL)
2155 {
2156 if (oldicon == NULL
2157#ifdef FEAT_GUI
2158 && !gui.in_use
2159#endif
2160 ) /* first call, save icon */
2161 get_x11_icon(FALSE);
2162
2163 if (*T_CIS != NUL)
2164 {
2165 out_str(T_CIS); /* set icon start */
2166 out_str_nf(icon);
2167 out_str(T_CIE); /* set icon end */
2168 out_flush();
2169 }
2170#ifdef FEAT_X11
2171 else
2172# ifdef FEAT_GUI_GTK
2173 if (!gui.in_use) /* don't do this if GTK+ is running */
2174# endif
2175 set_x11_icon(icon); /* x11 */
2176#endif
2177 did_set_icon = TRUE;
2178 }
2179 --recursive;
2180}
2181
2182/*
2183 * Restore the window/icon title.
2184 * "which" is one of:
2185 * 1 only restore title
2186 * 2 only restore icon
2187 * 3 restore title and icon
2188 */
2189 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002190mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191{
2192 /* only restore the title or icon when it has been set */
2193 mch_settitle(((which & 1) && did_set_title) ?
2194 (oldtitle ? oldtitle : p_titleold) : NULL,
2195 ((which & 2) && did_set_icon) ? oldicon : NULL);
2196}
2197
2198#endif /* FEAT_TITLE */
2199
2200/*
2201 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002202 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 */
2204 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002205vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206{
2207 if (name == NULL)
2208 return FALSE;
2209 return (STRNICMP(name, "xterm", 5) == 0
2210 || STRNICMP(name, "nxterm", 6) == 0
2211 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002212 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 || STRNICMP(name, "rxvt", 4) == 0
2214 || STRCMP(name, "builtin_xterm") == 0);
2215}
2216
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002217#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2218/*
2219 * Return TRUE if "name" appears to be that of a terminal
2220 * known to support the xterm-style mouse protocol.
2221 * Relies on term_is_xterm having been set to its correct value.
2222 */
2223 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002224use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002225{
2226 return (name != NULL
2227 && (term_is_xterm || STRNICMP(name, "screen", 6) == 0));
2228}
2229#endif
2230
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2232/*
2233 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2234 * Return 1 for "xterm".
2235 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002236 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002237 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 */
2239 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002240use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002242 if (ttym_flags == TTYM_SGR)
2243 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002244 if (ttym_flags == TTYM_URXVT)
2245 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 if (ttym_flags == TTYM_XTERM2)
2247 return 2;
2248 if (ttym_flags == TTYM_XTERM)
2249 return 1;
2250 return 0;
2251}
2252#endif
2253
2254 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002255vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256{
2257 if (name == NULL)
2258 return FALSE;
2259 return (STRNICMP(name, "iris-ansi", 9) == 0
2260 || STRCMP(name, "builtin_iris-ansi") == 0);
2261}
2262
2263 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002264vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
2266 if (name == NULL)
2267 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002268 /* catch VT100 - VT5xx */
2269 return ((STRNICMP(name, "vt", 2) == 0
2270 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 || STRCMP(name, "builtin_vt320") == 0);
2272}
2273
2274/*
2275 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2276 * This should include all windowed terminal emulators.
2277 */
2278 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002279vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280{
2281 if (name == NULL)
2282 return FALSE;
2283 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2284 return TRUE;
2285 return ( STRNICMP(name, "hpterm", 6) == 0
2286 || STRNICMP(name, "sun-cmd", 7) == 0
2287 || STRNICMP(name, "screen", 6) == 0
2288 || STRNICMP(name, "dtterm", 6) == 0);
2289}
2290
2291/*
2292 * Insert user name in s[len].
2293 * Return OK if a name found.
2294 */
2295 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002296mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297{
2298#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002299 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 return OK;
2301#else
2302 return mch_get_uname(getuid(), s, len);
2303#endif
2304}
2305
2306/*
2307 * Insert user name for "uid" in s[len].
2308 * Return OK if a name found.
2309 */
2310 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002311mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312{
2313#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2314 struct passwd *pw;
2315
2316 if ((pw = getpwuid(uid)) != NULL
2317 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2318 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002319 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 return OK;
2321 }
2322#endif
2323 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2324 return FAIL; /* a number is not a name */
2325}
2326
2327/*
2328 * Insert host name is s[len].
2329 */
2330
2331#ifdef HAVE_SYS_UTSNAME_H
2332 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002333mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
2335 struct utsname vutsname;
2336
2337 if (uname(&vutsname) < 0)
2338 *s = NUL;
2339 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002340 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341}
2342#else /* HAVE_SYS_UTSNAME_H */
2343
2344# ifdef HAVE_SYS_SYSTEMINFO_H
2345# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2346# endif
2347
2348 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002349mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350{
2351# ifdef VAXC
2352 vaxc$gethostname((char *)s, len);
2353# else
2354 gethostname((char *)s, len);
2355# endif
2356 s[len - 1] = NUL; /* make sure it's terminated */
2357}
2358#endif /* HAVE_SYS_UTSNAME_H */
2359
2360/*
2361 * return process ID
2362 */
2363 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002364mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365{
2366 return (long)getpid();
2367}
2368
2369#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002370static char *strerror(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
2372 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002373strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374{
2375 extern int sys_nerr;
2376 extern char *sys_errlist[];
2377 static char er[20];
2378
2379 if (err > 0 && err < sys_nerr)
2380 return (sys_errlist[err]);
2381 sprintf(er, "Error %d", err);
2382 return er;
2383}
2384#endif
2385
2386/*
2387 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2388 * Return OK for success, FAIL for failure.
2389 */
2390 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002391mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392{
2393#if defined(USE_GETCWD)
2394 if (getcwd((char *)buf, len) == NULL)
2395 {
2396 STRCPY(buf, strerror(errno));
2397 return FAIL;
2398 }
2399 return OK;
2400#else
2401 return (getwd((char *)buf) != NULL ? OK : FAIL);
2402#endif
2403}
2404
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002406 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 *
2408 * return FAIL for failure, OK for success
2409 */
2410 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002411mch_FullName(
2412 char_u *fname,
2413 char_u *buf,
2414 int len,
2415 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416{
2417 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002418#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 int fd = -1;
2420 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 char_u olddir[MAXPATHL];
2423 char_u *p;
2424 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002425#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002426 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2427 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002428#endif
2429
Bram Moolenaar38323e42007-03-06 19:22:53 +00002430#ifdef VMS
2431 fname = vms_fixfilename(fname);
2432#endif
2433
Bram Moolenaara2442432007-04-26 14:26:37 +00002434#ifdef __CYGWIN__
2435 /*
2436 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2437 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002438# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002439 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2440 * a forward slash. */
2441 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2442 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002443# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002444 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002445# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002446 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002447#endif
2448
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002449 /* Expand it if forced or not an absolute path.
2450 * Do not do it for "/file", the result is always "/". */
2451 if ((force || !mch_isFullName(fname))
2452 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 {
2454 /*
2455 * If the file name has a path, change to that directory for a moment,
2456 * and then do the getwd() (and get back to where we were).
2457 * This will get the correct path name with "../" things.
2458 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002459 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002461#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 /*
2463 * Use fchdir() if possible, it's said to be faster and more
2464 * reliable. But on SunOS 4 it might not work. Check this by
2465 * doing a fchdir() right now.
2466 */
2467 if (!dont_fchdir)
2468 {
2469 fd = open(".", O_RDONLY | O_EXTRA, 0);
2470 if (fd >= 0 && fchdir(fd) < 0)
2471 {
2472 close(fd);
2473 fd = -1;
2474 dont_fchdir = TRUE; /* don't try again */
2475 }
2476 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
2479 /* Only change directory when we are sure we can return to where
2480 * we are now. After doing "su" chdir(".") might not work. */
2481 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002482#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 (mch_dirname(olddir, MAXPATHL) == FAIL
2486 || mch_chdir((char *)olddir) != 0))
2487 {
2488 p = NULL; /* can't get current dir: don't chdir */
2489 retval = FAIL;
2490 }
2491 else
2492 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 /* The directory is copied into buf[], to be able to remove
2494 * the file name without changing it (could be a string in
2495 * read-only memory) */
2496 if (p - fname >= len)
2497 retval = FAIL;
2498 else
2499 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002500 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 if (mch_chdir((char *)buf))
2502 retval = FAIL;
2503 else
2504 fname = p + 1;
2505 *buf = NUL;
2506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 }
2508 }
2509 if (mch_dirname(buf, len) == FAIL)
2510 {
2511 retval = FAIL;
2512 *buf = NUL;
2513 }
2514 if (p != NULL)
2515 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002516#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 if (fd >= 0)
2518 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002519 if (p_verbose >= 5)
2520 {
2521 verbose_enter();
2522 MSG("fchdir() to previous dir");
2523 verbose_leave();
2524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 l = fchdir(fd);
2526 close(fd);
2527 }
2528 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 l = mch_chdir((char *)olddir);
2531 if (l != 0)
2532 EMSG(_(e_prev_dir));
2533 }
2534
2535 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002536 if (l >= len - 1)
2537 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002538#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002539 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002541 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002544
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002546 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 return FAIL;
2548
2549 /* Do not append ".", "/dir/." is equal to "/dir". */
2550 if (STRCMP(fname, ".") != 0)
2551 STRCAT(buf, fname);
2552
2553 return OK;
2554}
2555
2556/*
2557 * Return TRUE if "fname" does not depend on the current directory.
2558 */
2559 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002560mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561{
2562#ifdef __EMX__
2563 return _fnisabs(fname);
2564#else
2565# ifdef VMS
2566 return ( fname[0] == '/' || fname[0] == '.' ||
2567 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2568 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2569 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
2570# else
2571 return (*fname == '/' || *fname == '~');
2572# endif
2573#endif
2574}
2575
Bram Moolenaar24552be2005-12-10 20:17:30 +00002576#if defined(USE_FNAME_CASE) || defined(PROTO)
2577/*
2578 * Set the case of the file name, if it already exists. This will cause the
2579 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002580 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002581 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002582 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002583fname_case(
2584 char_u *name,
2585 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002586{
2587 struct stat st;
2588 char_u *slash, *tail;
2589 DIR *dirp;
2590 struct dirent *dp;
2591
2592 if (lstat((char *)name, &st) >= 0)
2593 {
2594 /* Open the directory where the file is located. */
2595 slash = vim_strrchr(name, '/');
2596 if (slash == NULL)
2597 {
2598 dirp = opendir(".");
2599 tail = name;
2600 }
2601 else
2602 {
2603 *slash = NUL;
2604 dirp = opendir((char *)name);
2605 *slash = '/';
2606 tail = slash + 1;
2607 }
2608
2609 if (dirp != NULL)
2610 {
2611 while ((dp = readdir(dirp)) != NULL)
2612 {
2613 /* Only accept names that differ in case and are the same byte
2614 * length. TODO: accept different length name. */
2615 if (STRICMP(tail, dp->d_name) == 0
2616 && STRLEN(tail) == STRLEN(dp->d_name))
2617 {
2618 char_u newname[MAXPATHL + 1];
2619 struct stat st2;
2620
2621 /* Verify the inode is equal. */
2622 vim_strncpy(newname, name, MAXPATHL);
2623 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2624 MAXPATHL - (tail - name));
2625 if (lstat((char *)newname, &st2) >= 0
2626 && st.st_ino == st2.st_ino
2627 && st.st_dev == st2.st_dev)
2628 {
2629 STRCPY(tail, dp->d_name);
2630 break;
2631 }
2632 }
2633 }
2634
2635 closedir(dirp);
2636 }
2637 }
2638}
2639#endif
2640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641/*
2642 * Get file permissions for 'name'.
2643 * Returns -1 when it doesn't exist.
2644 */
2645 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002646mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647{
2648 struct stat statb;
2649
2650 /* Keep the #ifdef outside of stat(), it may be a macro. */
2651#ifdef VMS
2652 if (stat((char *)vms_fixfilename(name), &statb))
2653#else
2654 if (stat((char *)name, &statb))
2655#endif
2656 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002657#ifdef __INTERIX
2658 /* The top bit makes the value negative, which means the file doesn't
2659 * exist. Remove the bit, we don't use it. */
2660 return statb.st_mode & ~S_ADDACE;
2661#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664}
2665
2666/*
2667 * set file permission for 'name' to 'perm'
2668 *
2669 * return FAIL for failure, OK otherwise
2670 */
2671 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002672mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 return (chmod((char *)
2675#ifdef VMS
2676 vms_fixfilename(name),
2677#else
2678 name,
2679#endif
2680 (mode_t)perm) == 0 ? OK : FAIL);
2681}
2682
2683#if defined(HAVE_ACL) || defined(PROTO)
2684# ifdef HAVE_SYS_ACL_H
2685# include <sys/acl.h>
2686# endif
2687# ifdef HAVE_SYS_ACCESS_H
2688# include <sys/access.h>
2689# endif
2690
2691# ifdef HAVE_SOLARIS_ACL
2692typedef struct vim_acl_solaris_T {
2693 int acl_cnt;
2694 aclent_t *acl_entry;
2695} vim_acl_solaris_T;
2696# endif
2697
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002698#if defined(HAVE_SELINUX) || defined(PROTO)
2699/*
2700 * Copy security info from "from_file" to "to_file".
2701 */
2702 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002703mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002704{
2705 if (from_file == NULL)
2706 return;
2707
2708 if (selinux_enabled == -1)
2709 selinux_enabled = is_selinux_enabled();
2710
2711 if (selinux_enabled > 0)
2712 {
2713 security_context_t from_context = NULL;
2714 security_context_t to_context = NULL;
2715
2716 if (getfilecon((char *)from_file, &from_context) < 0)
2717 {
2718 /* If the filesystem doesn't support extended attributes,
2719 the original had no special security context and the
2720 target cannot have one either. */
2721 if (errno == EOPNOTSUPP)
2722 return;
2723
2724 MSG_PUTS(_("\nCould not get security context for "));
2725 msg_outtrans(from_file);
2726 msg_putchar('\n');
2727 return;
2728 }
2729 if (getfilecon((char *)to_file, &to_context) < 0)
2730 {
2731 MSG_PUTS(_("\nCould not get security context for "));
2732 msg_outtrans(to_file);
2733 msg_putchar('\n');
2734 freecon (from_context);
2735 return ;
2736 }
2737 if (strcmp(from_context, to_context) != 0)
2738 {
2739 if (setfilecon((char *)to_file, from_context) < 0)
2740 {
2741 MSG_PUTS(_("\nCould not set security context for "));
2742 msg_outtrans(to_file);
2743 msg_putchar('\n');
2744 }
2745 }
2746 freecon(to_context);
2747 freecon(from_context);
2748 }
2749}
2750#endif /* HAVE_SELINUX */
2751
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002752#if defined(HAVE_SMACK) && !defined(PROTO)
2753/*
2754 * Copy security info from "from_file" to "to_file".
2755 */
2756 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002757mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002758{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002759 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002760 {
2761 XATTR_NAME_SMACK,
2762 XATTR_NAME_SMACKEXEC,
2763 XATTR_NAME_SMACKMMAP
2764 };
2765
2766 char buffer[SMACK_LABEL_LEN];
2767 const char *name;
2768 int index;
2769 int ret;
2770 ssize_t size;
2771
2772 if (from_file == NULL)
2773 return;
2774
2775 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2776 / sizeof(smack_copied_attributes)[0]) ; index++)
2777 {
2778 /* get the name of the attribute to copy */
2779 name = smack_copied_attributes[index];
2780
2781 /* get the value of the attribute in buffer */
2782 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2783 if (size >= 0)
2784 {
2785 /* copy the attribute value of buffer */
2786 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2787 if (ret < 0)
2788 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002789 vim_snprintf((char *)IObuff, IOSIZE,
2790 _("Could not set security context %s for %s"),
2791 name, to_file);
2792 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002793 msg_putchar('\n');
2794 }
2795 }
2796 else
2797 {
2798 /* what reason of not having the attribute value? */
2799 switch (errno)
2800 {
2801 case ENOTSUP:
2802 /* extended attributes aren't supported or enabled */
2803 /* should a message be echoed? not sure... */
2804 return; /* leave because it isn't usefull to continue */
2805
2806 case ERANGE:
2807 default:
2808 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002809 vim_snprintf((char *)IObuff, IOSIZE,
2810 _("Could not get security context %s for %s. Removing it!"),
2811 name, from_file);
2812 msg_puts(IObuff);
2813 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002814 /* FALLTHROUGH to remove the attribute */
2815
2816 case ENODATA:
2817 /* no attribute of this name */
2818 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002819 /* Silently ignore errors, apparently this happens when
2820 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002821 break;
2822 }
2823 }
2824 }
2825}
2826#endif /* HAVE_SMACK */
2827
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828/*
2829 * Return a pointer to the ACL of file "fname" in allocated memory.
2830 * Return NULL if the ACL is not available for whatever reason.
2831 */
2832 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002833mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
2835 vim_acl_T ret = NULL;
2836#ifdef HAVE_POSIX_ACL
2837 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2838#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002839#ifdef HAVE_SOLARIS_ZFS_ACL
2840 acl_t *aclent;
2841
2842 if (acl_get((char *)fname, 0, &aclent) < 0)
2843 return NULL;
2844 ret = (vim_acl_T)aclent;
2845#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846#ifdef HAVE_SOLARIS_ACL
2847 vim_acl_solaris_T *aclent;
2848
2849 aclent = malloc(sizeof(vim_acl_solaris_T));
2850 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2851 {
2852 free(aclent);
2853 return NULL;
2854 }
2855 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2856 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2857 {
2858 free(aclent->acl_entry);
2859 free(aclent);
2860 return NULL;
2861 }
2862 ret = (vim_acl_T)aclent;
2863#else
2864#if defined(HAVE_AIX_ACL)
2865 int aclsize;
2866 struct acl *aclent;
2867
2868 aclsize = sizeof(struct acl);
2869 aclent = malloc(aclsize);
2870 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2871 {
2872 if (errno == ENOSPC)
2873 {
2874 aclsize = aclent->acl_len;
2875 aclent = realloc(aclent, aclsize);
2876 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2877 {
2878 free(aclent);
2879 return NULL;
2880 }
2881 }
2882 else
2883 {
2884 free(aclent);
2885 return NULL;
2886 }
2887 }
2888 ret = (vim_acl_T)aclent;
2889#endif /* HAVE_AIX_ACL */
2890#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002891#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892#endif /* HAVE_POSIX_ACL */
2893 return ret;
2894}
2895
2896/*
2897 * Set the ACL of file "fname" to "acl" (unless it's NULL).
2898 */
2899 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002900mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
2902 if (aclent == NULL)
2903 return;
2904#ifdef HAVE_POSIX_ACL
2905 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
2906#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002907#ifdef HAVE_SOLARIS_ZFS_ACL
2908 acl_set((char *)fname, (acl_t *)aclent);
2909#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910#ifdef HAVE_SOLARIS_ACL
2911 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
2912 ((vim_acl_solaris_T *)aclent)->acl_entry);
2913#else
2914#ifdef HAVE_AIX_ACL
2915 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
2916#endif /* HAVE_AIX_ACL */
2917#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002918#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919#endif /* HAVE_POSIX_ACL */
2920}
2921
2922 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002923mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924{
2925 if (aclent == NULL)
2926 return;
2927#ifdef HAVE_POSIX_ACL
2928 acl_free((acl_t)aclent);
2929#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002930#ifdef HAVE_SOLARIS_ZFS_ACL
2931 acl_free((acl_t *)aclent);
2932#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933#ifdef HAVE_SOLARIS_ACL
2934 free(((vim_acl_solaris_T *)aclent)->acl_entry);
2935 free(aclent);
2936#else
2937#ifdef HAVE_AIX_ACL
2938 free(aclent);
2939#endif /* HAVE_AIX_ACL */
2940#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002941#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942#endif /* HAVE_POSIX_ACL */
2943}
2944#endif
2945
2946/*
2947 * Set hidden flag for "name".
2948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002950mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951{
2952 /* can't hide a file */
2953}
2954
2955/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01002956 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 * return FALSE if "name" is not a directory
2958 * return FALSE for error
2959 */
2960 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002961mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962{
2963 struct stat statb;
2964
2965 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
2966 return FALSE;
2967 if (stat((char *)name, &statb))
2968 return FALSE;
2969#ifdef _POSIX_SOURCE
2970 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
2971#else
2972 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
2973#endif
2974}
2975
Bram Moolenaar43a34f92016-01-17 15:56:34 +01002976/*
2977 * return TRUE if "name" is a directory, NOT a symlink to a directory
2978 * return FALSE if "name" is not a directory
2979 * return FALSE for error
2980 */
2981 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002982mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01002983{
2984 struct stat statb;
2985
2986 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
2987 return FALSE;
2988 if (lstat((char *)name, &statb))
2989 return FALSE;
2990#ifdef _POSIX_SOURCE
2991 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
2992#else
2993 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
2994#endif
2995}
2996
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002997static int executable_file(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998
2999/*
3000 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3001 */
3002 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003003executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 struct stat st;
3006
3007 if (stat((char *)name, &st))
3008 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003009#ifdef VMS
3010 /* Like on Unix system file can have executable rights but not necessarily
3011 * be an executable, but on Unix is not a default for an ordianry file to
3012 * have an executable flag - on VMS it is in most cases.
3013 * Therefore, this check does not have any sense - let keep us to the
3014 * conventions instead:
3015 * *.COM and *.EXE files are the executables - the rest are not. This is
3016 * not ideal but better then it was.
3017 */
3018 int vms_executable = 0;
3019 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3020 {
3021 if (strstr(vms_tolower((char*)name),".exe") != NULL
3022 || strstr(vms_tolower((char*)name),".com")!= NULL)
3023 vms_executable = 1;
3024 }
3025 return vms_executable;
3026#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029}
3030
3031/*
3032 * Return 1 if "name" can be found in $PATH and executed, 0 if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003033 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 * Return -1 if unknown.
3035 */
3036 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003037mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038{
3039 char_u *buf;
3040 char_u *p, *e;
3041 int retval;
3042
Bram Moolenaarb5971142015-03-21 17:32:19 +01003043 /* When "use_path" is false and if it's an absolute or relative path don't
3044 * need to use $PATH. */
3045 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3046 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003047 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003048 /* There must be a path separator, files in the current directory
3049 * can't be executed. */
3050 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003051 {
3052 if (path != NULL)
3053 {
3054 if (name[0] == '.')
3055 *path = FullName_save(name, TRUE);
3056 else
3057 *path = vim_strsave(name);
3058 }
3059 return TRUE;
3060 }
3061 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
3064 p = (char_u *)getenv("PATH");
3065 if (p == NULL || *p == NUL)
3066 return -1;
3067 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3068 if (buf == NULL)
3069 return -1;
3070
3071 /*
3072 * Walk through all entries in $PATH to check if "name" exists there and
3073 * is an executable file.
3074 */
3075 for (;;)
3076 {
3077 e = (char_u *)strchr((char *)p, ':');
3078 if (e == NULL)
3079 e = p + STRLEN(p);
3080 if (e - p <= 1) /* empty entry means current dir */
3081 STRCPY(buf, "./");
3082 else
3083 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003084 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 add_pathsep(buf);
3086 }
3087 STRCAT(buf, name);
3088 retval = executable_file(buf);
3089 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003090 {
3091 if (path != NULL)
3092 {
3093 if (buf[0] == '.')
3094 *path = FullName_save(buf, TRUE);
3095 else
3096 *path = vim_strsave(buf);
3097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
3101 if (*e != ':')
3102 break;
3103 p = e + 1;
3104 }
3105
3106 vim_free(buf);
3107 return retval;
3108}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
3110/*
3111 * Check what "name" is:
3112 * NODE_NORMAL: file or directory (or doesn't exist)
3113 * NODE_WRITABLE: writable device, socket, fifo, etc.
3114 * NODE_OTHER: non-writable things
3115 */
3116 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003117mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
3119 struct stat st;
3120
3121 if (stat((char *)name, &st))
3122 return NODE_NORMAL;
3123 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3124 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3126 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 /* Everything else is writable? */
3128 return NODE_WRITABLE;
3129}
3130
3131 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003132mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134#ifdef HAVE_CHECK_STACK_GROWTH
3135 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 check_stack_growth((char *)&i);
3138
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003139# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 get_stack_limit();
3141# endif
3142
3143#endif
3144
3145 /*
3146 * Setup an alternative stack for signals. Helps to catch signals when
3147 * running out of stack space.
3148 * Use of sigaltstack() is preferred, it's more portable.
3149 * Ignore any errors.
3150 */
3151#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003152 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 init_signal_stack();
3154#endif
3155}
3156
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003157#if defined(EXITFREE) || defined(PROTO)
3158 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003159mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003160{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003161# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3162 if (clip_star.owned)
3163 clip_lose_selection(&clip_star);
3164 if (clip_plus.owned)
3165 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003166# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003167# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003168 if (xterm_Shell != (Widget)0)
3169 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003170# ifndef LESSTIF_VERSION
3171 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003172 if (xterm_dpy != NULL)
3173 XtCloseDisplay(xterm_dpy);
3174 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003175 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003176 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003177# ifdef FEAT_X11
3178 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3179# endif
3180 }
3181# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003182# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003183# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003184 if (x11_display != NULL
3185# ifdef FEAT_XCLIPBOARD
3186 && x11_display != xterm_dpy
3187# endif
3188 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003189 XCloseDisplay(x11_display);
3190# endif
3191# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
3192 vim_free(signal_stack);
3193 signal_stack = NULL;
3194# endif
3195# ifdef FEAT_TITLE
3196 vim_free(oldtitle);
3197 vim_free(oldicon);
3198# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003199}
3200#endif
3201
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003202static void exit_scroll(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204/*
3205 * Output a newline when exiting.
3206 * Make sure the newline goes to the same stream as the text.
3207 */
3208 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003209exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003211 if (silent_mode)
3212 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 if (newline_on_exit || msg_didout)
3214 {
3215 if (msg_use_printf())
3216 {
3217 if (info_message)
3218 mch_msg("\n");
3219 else
3220 mch_errmsg("\r\n");
3221 }
3222 else
3223 out_char('\n');
3224 }
3225 else
3226 {
3227 restore_cterm_colors(); /* get original colors back */
3228 msg_clr_eos_force(); /* clear the rest of the display */
3229 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3230 }
3231}
3232
3233 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003234mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235{
3236 exiting = TRUE;
3237
3238#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3239 x11_export_final_selection();
3240#endif
3241
3242#ifdef FEAT_GUI
3243 if (!gui.in_use)
3244#endif
3245 {
3246 settmode(TMODE_COOK);
3247#ifdef FEAT_TITLE
3248 mch_restore_title(3); /* restore xterm title and icon name */
3249#endif
3250 /*
3251 * When t_ti is not empty but it doesn't cause swapping terminal
3252 * pages, need to output a newline when msg_didout is set. But when
3253 * t_ti does swap pages it should not go to the shell page. Do this
3254 * before stoptermcap().
3255 */
3256 if (swapping_screen() && !newline_on_exit)
3257 exit_scroll();
3258
3259 /* Stop termcap: May need to check for T_CRV response, which
3260 * requires RAW mode. */
3261 stoptermcap();
3262
3263 /*
3264 * A newline is only required after a message in the alternate screen.
3265 * This is set to TRUE by wait_return().
3266 */
3267 if (!swapping_screen() || newline_on_exit)
3268 exit_scroll();
3269
3270 /* Cursor may have been switched off without calling starttermcap()
3271 * when doing "vim -u vimrc" and vimrc contains ":q". */
3272 if (full_screen)
3273 cursor_on();
3274 }
3275 out_flush();
3276 ml_close_all(TRUE); /* remove all memfiles */
3277 may_core_dump();
3278#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 gui_exit(r);
3281#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003282
Bram Moolenaar56718732006-03-15 22:53:57 +00003283#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003284 mac_conv_cleanup();
3285#endif
3286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287#ifdef __QNX__
3288 /* A core dump won't be created if the signal handler
3289 * doesn't return, so we can't call exit() */
3290 if (deadly_signal != 0)
3291 return;
3292#endif
3293
Bram Moolenaar009b2592004-10-24 19:18:58 +00003294#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003295 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003296#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003297
3298#ifdef EXITFREE
3299 free_all_mem();
3300#endif
3301
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 exit(r);
3303}
3304
3305 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003306may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307{
3308 if (deadly_signal != 0)
3309 {
3310 signal(deadly_signal, SIG_DFL);
3311 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3312 }
3313}
3314
3315#ifndef VMS
3316
3317 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003318mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 static int first = TRUE;
3321
3322 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3323#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3324 /*
3325 * for "new" tty systems
3326 */
3327# ifdef HAVE_TERMIOS_H
3328 static struct termios told;
3329 struct termios tnew;
3330# else
3331 static struct termio told;
3332 struct termio tnew;
3333# endif
3334
3335 if (first)
3336 {
3337 first = FALSE;
3338# if defined(HAVE_TERMIOS_H)
3339 tcgetattr(read_cmd_fd, &told);
3340# else
3341 ioctl(read_cmd_fd, TCGETA, &told);
3342# endif
3343 }
3344
3345 tnew = told;
3346 if (tmode == TMODE_RAW)
3347 {
3348 /*
3349 * ~ICRNL enables typing ^V^M
3350 */
3351 tnew.c_iflag &= ~ICRNL;
3352 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3353# if defined(IEXTEN) && !defined(__MINT__)
3354 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3355 /* but it breaks function keys on MINT */
3356# endif
3357 );
3358# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3359 tnew.c_oflag &= ~ONLCR;
3360# endif
3361 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3362 tnew.c_cc[VTIME] = 0; /* don't wait */
3363 }
3364 else if (tmode == TMODE_SLEEP)
3365 tnew.c_lflag &= ~(ECHO);
3366
3367# if defined(HAVE_TERMIOS_H)
3368 {
3369 int n = 10;
3370
3371 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3372 * few times. */
3373 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3374 && errno == EINTR && n > 0)
3375 --n;
3376 }
3377# else
3378 ioctl(read_cmd_fd, TCSETA, &tnew);
3379# endif
3380
3381#else
3382
3383 /*
3384 * for "old" tty systems
3385 */
3386# ifndef TIOCSETN
3387# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3388# endif
3389 static struct sgttyb ttybold;
3390 struct sgttyb ttybnew;
3391
3392 if (first)
3393 {
3394 first = FALSE;
3395 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3396 }
3397
3398 ttybnew = ttybold;
3399 if (tmode == TMODE_RAW)
3400 {
3401 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3402 ttybnew.sg_flags |= RAW;
3403 }
3404 else if (tmode == TMODE_SLEEP)
3405 ttybnew.sg_flags &= ~(ECHO);
3406 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3407#endif
3408 curr_tmode = tmode;
3409}
3410
3411/*
3412 * Try to get the code for "t_kb" from the stty setting
3413 *
3414 * Even if termcap claims a backspace key, the user's setting *should*
3415 * prevail. stty knows more about reality than termcap does, and if
3416 * somebody's usual erase key is DEL (which, for most BSD users, it will
3417 * be), they're going to get really annoyed if their erase key starts
3418 * doing forward deletes for no reason. (Eric Fischer)
3419 */
3420 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003421get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
3423 char_u buf[2];
3424 char_u *p;
3425
3426 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3427#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3428 /* for "new" tty systems */
3429# ifdef HAVE_TERMIOS_H
3430 struct termios keys;
3431# else
3432 struct termio keys;
3433# endif
3434
3435# if defined(HAVE_TERMIOS_H)
3436 if (tcgetattr(read_cmd_fd, &keys) != -1)
3437# else
3438 if (ioctl(read_cmd_fd, TCGETA, &keys) != -1)
3439# endif
3440 {
3441 buf[0] = keys.c_cc[VERASE];
3442 intr_char = keys.c_cc[VINTR];
3443#else
3444 /* for "old" tty systems */
3445 struct sgttyb keys;
3446
3447 if (ioctl(read_cmd_fd, TIOCGETP, &keys) != -1)
3448 {
3449 buf[0] = keys.sg_erase;
3450 intr_char = keys.sg_kill;
3451#endif
3452 buf[1] = NUL;
3453 add_termcode((char_u *)"kb", buf, FALSE);
3454
3455 /*
3456 * If <BS> and <DEL> are now the same, redefine <DEL>.
3457 */
3458 p = find_termcode((char_u *)"kD");
3459 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3460 do_fixdel(NULL);
3461 }
3462#if 0
3463 } /* to keep cindent happy */
3464#endif
3465}
3466
3467#endif /* VMS */
3468
3469#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
3470/*
3471 * Set mouse clicks on or off.
3472 */
3473 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003474mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475{
3476 static int ison = FALSE;
3477 int xterm_mouse_vers;
3478
3479 if (on == ison) /* return quickly if nothing to do */
3480 return;
3481
3482 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003483
3484# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003485 if (ttym_flags == TTYM_URXVT)
3486 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003487 out_str_nf((char_u *)
3488 (on
3489 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3490 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
3491 ison = on;
3492 }
3493# endif
3494
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003495# ifdef FEAT_MOUSE_SGR
3496 if (ttym_flags == TTYM_SGR)
3497 {
3498 out_str_nf((char_u *)
3499 (on
3500 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3501 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
3502 ison = on;
3503 }
3504# endif
3505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (xterm_mouse_vers > 0)
3507 {
3508 if (on) /* enable mouse events, use mouse tracking if available */
3509 out_str_nf((char_u *)
3510 (xterm_mouse_vers > 1
3511 ? IF_EB("\033[?1002h", ESC_STR "[?1002h")
3512 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3513 else /* disable mouse events, could probably always send the same */
3514 out_str_nf((char_u *)
3515 (xterm_mouse_vers > 1
3516 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3517 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
3518 ison = on;
3519 }
3520
3521# ifdef FEAT_MOUSE_DEC
3522 else if (ttym_flags == TTYM_DEC)
3523 {
3524 if (on) /* enable mouse events */
3525 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3526 else /* disable mouse events */
3527 out_str_nf((char_u *)"\033['z");
3528 ison = on;
3529 }
3530# endif
3531
3532# ifdef FEAT_MOUSE_GPM
3533 else
3534 {
3535 if (on)
3536 {
3537 if (gpm_open())
3538 ison = TRUE;
3539 }
3540 else
3541 {
3542 gpm_close();
3543 ison = FALSE;
3544 }
3545 }
3546# endif
3547
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003548# ifdef FEAT_SYSMOUSE
3549 else
3550 {
3551 if (on)
3552 {
3553 if (sysmouse_open() == OK)
3554 ison = TRUE;
3555 }
3556 else
3557 {
3558 sysmouse_close();
3559 ison = FALSE;
3560 }
3561 }
3562# endif
3563
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564# ifdef FEAT_MOUSE_JSB
3565 else
3566 {
3567 if (on)
3568 {
3569 /* D - Enable Mouse up/down messages
3570 * L - Enable Left Button Reporting
3571 * M - Enable Middle Button Reporting
3572 * R - Enable Right Button Reporting
3573 * K - Enable SHIFT and CTRL key Reporting
3574 * + - Enable Advanced messaging of mouse moves and up/down messages
3575 * Q - Quiet No Ack
3576 * # - Numeric value of mouse pointer required
3577 * 0 = Multiview 2000 cursor, used as standard
3578 * 1 = Windows Arrow
3579 * 2 = Windows I Beam
3580 * 3 = Windows Hour Glass
3581 * 4 = Windows Cross Hair
3582 * 5 = Windows UP Arrow
3583 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003584# ifdef JSBTERM_MOUSE_NONADVANCED
3585 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3587 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003588# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3590 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 ison = TRUE;
3593 }
3594 else
3595 {
3596 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3597 ESC_STR "[0~ZwQ" ESC_STR "\\"));
3598 ison = FALSE;
3599 }
3600 }
3601# endif
3602# ifdef FEAT_MOUSE_PTERM
3603 else
3604 {
3605 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3606 if (on)
3607 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3608 else
3609 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
3610 ison = on;
3611 }
3612# endif
3613}
3614
3615/*
3616 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3617 */
3618 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003619check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
3621# ifdef FEAT_MOUSE_XTERM
3622 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003623# ifdef FEAT_MOUSE_URXVT
3624 && use_xterm_mouse() != 3
3625# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626# ifdef FEAT_GUI
3627 && !gui.in_use
3628# endif
3629 )
3630 {
3631 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003632 ? IF_EB("\233M", CSI_STR "M")
3633 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 if (*p_mouse != NUL)
3635 {
3636 /* force mouse off and maybe on to send possibly new mouse
3637 * activation sequence to the xterm, with(out) drag tracing. */
3638 mch_setmouse(FALSE);
3639 setmouse();
3640 }
3641 }
3642 else
3643 del_mouse_termcode(KS_MOUSE);
3644# endif
3645
3646# ifdef FEAT_MOUSE_GPM
3647 if (!use_xterm_mouse()
3648# ifdef FEAT_GUI
3649 && !gui.in_use
3650# endif
3651 )
3652 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3653# endif
3654
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003655# ifdef FEAT_SYSMOUSE
3656 if (!use_xterm_mouse()
3657# ifdef FEAT_GUI
3658 && !gui.in_use
3659# endif
3660 )
3661 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3662# endif
3663
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003665 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 if (!use_xterm_mouse()
3667# ifdef FEAT_GUI
3668 && !gui.in_use
3669# endif
3670 )
3671 set_mouse_termcode(KS_JSBTERM_MOUSE,
3672 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3673 else
3674 del_mouse_termcode(KS_JSBTERM_MOUSE);
3675# endif
3676
3677# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003678 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 * define it in the GUI or when using an xterm. */
3680 if (!use_xterm_mouse()
3681# ifdef FEAT_GUI
3682 && !gui.in_use
3683# endif
3684 )
3685 set_mouse_termcode(KS_NETTERM_MOUSE,
3686 (char_u *)IF_EB("\033}", ESC_STR "}"));
3687 else
3688 del_mouse_termcode(KS_NETTERM_MOUSE);
3689# endif
3690
3691# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003692 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003693 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694# ifdef FEAT_GUI
3695 && !gui.in_use
3696# endif
3697 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003698 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3699 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 else
3701 del_mouse_termcode(KS_DEC_MOUSE);
3702# endif
3703# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003704 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003705 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706# ifdef FEAT_GUI
3707 && !gui.in_use
3708# endif
3709 )
3710 set_mouse_termcode(KS_PTERM_MOUSE,
3711 (char_u *) IF_EB("\033[", ESC_STR "["));
3712 else
3713 del_mouse_termcode(KS_PTERM_MOUSE);
3714# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003715# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003716 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003717 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003718# ifdef FEAT_GUI
3719 && !gui.in_use
3720# endif
3721 )
3722 {
3723 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3724 ? IF_EB("\233", CSI_STR)
3725 : IF_EB("\033[", ESC_STR "[")));
3726
3727 if (*p_mouse != NUL)
3728 {
3729 mch_setmouse(FALSE);
3730 setmouse();
3731 }
3732 }
3733 else
3734 del_mouse_termcode(KS_URXVT_MOUSE);
3735# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003736# ifdef FEAT_MOUSE_SGR
Bram Moolenaar24dc2302014-05-13 20:19:58 +02003737 /* There is no conflict with xterm mouse */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003738 if (use_xterm_mouse() == 4
3739# ifdef FEAT_GUI
3740 && !gui.in_use
3741# endif
3742 )
3743 {
3744 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3745 ? IF_EB("\233<", CSI_STR "<")
3746 : IF_EB("\033[<", ESC_STR "[<")));
3747
3748 if (*p_mouse != NUL)
3749 {
3750 mch_setmouse(FALSE);
3751 setmouse();
3752 }
3753 }
3754 else
3755 del_mouse_termcode(KS_SGR_MOUSE);
3756# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757}
3758#endif
3759
3760/*
3761 * set screen mode, always fails.
3762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003764mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765{
3766 EMSG(_(e_screenmode));
3767 return FAIL;
3768}
3769
3770#ifndef VMS
3771
3772/*
3773 * Try to get the current window size:
3774 * 1. with an ioctl(), most accurate method
3775 * 2. from the environment variables LINES and COLUMNS
3776 * 3. from the termcap
3777 * 4. keep using the old values
3778 * Return OK when size could be determined, FAIL otherwise.
3779 */
3780 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003781mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
3783 long rows = 0;
3784 long columns = 0;
3785 char_u *p;
3786
3787 /*
3788 * For OS/2 use _scrsize().
3789 */
3790# ifdef __EMX__
3791 {
3792 int s[2];
3793
3794 _scrsize(s);
3795 columns = s[0];
3796 rows = s[1];
3797 }
3798# endif
3799
3800 /*
3801 * 1. try using an ioctl. It is the most accurate method.
3802 *
3803 * Try using TIOCGWINSZ first, some systems that have it also define
3804 * TIOCGSIZE but don't have a struct ttysize.
3805 */
3806# ifdef TIOCGWINSZ
3807 {
3808 struct winsize ws;
3809 int fd = 1;
3810
3811 /* When stdout is not a tty, use stdin for the ioctl(). */
3812 if (!isatty(fd) && isatty(read_cmd_fd))
3813 fd = read_cmd_fd;
3814 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
3815 {
3816 columns = ws.ws_col;
3817 rows = ws.ws_row;
3818 }
3819 }
3820# else /* TIOCGWINSZ */
3821# ifdef TIOCGSIZE
3822 {
3823 struct ttysize ts;
3824 int fd = 1;
3825
3826 /* When stdout is not a tty, use stdin for the ioctl(). */
3827 if (!isatty(fd) && isatty(read_cmd_fd))
3828 fd = read_cmd_fd;
3829 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
3830 {
3831 columns = ts.ts_cols;
3832 rows = ts.ts_lines;
3833 }
3834 }
3835# endif /* TIOCGSIZE */
3836# endif /* TIOCGWINSZ */
3837
3838 /*
3839 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003840 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
3841 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003843 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 {
3845 if ((p = (char_u *)getenv("LINES")))
3846 rows = atoi((char *)p);
3847 if ((p = (char_u *)getenv("COLUMNS")))
3848 columns = atoi((char *)p);
3849 }
3850
3851#ifdef HAVE_TGETENT
3852 /*
3853 * 3. try reading "co" and "li" entries from termcap
3854 */
3855 if (columns == 0 || rows == 0)
3856 getlinecol(&columns, &rows);
3857#endif
3858
3859 /*
3860 * 4. If everything fails, use the old values
3861 */
3862 if (columns <= 0 || rows <= 0)
3863 return FAIL;
3864
3865 Rows = rows;
3866 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02003867 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 return OK;
3869}
3870
3871/*
3872 * Try to set the window size to Rows and Columns.
3873 */
3874 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003875mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876{
3877 if (*T_CWS)
3878 {
3879 /*
3880 * NOTE: if you get an error here that term_set_winsize() is
3881 * undefined, check the output of configure. It could probably not
3882 * find a ncurses, termcap or termlib library.
3883 */
3884 term_set_winsize((int)Rows, (int)Columns);
3885 out_flush();
3886 screen_start(); /* don't know where cursor is now */
3887 }
3888}
3889
3890#endif /* VMS */
3891
3892/*
3893 * Rows and/or Columns has changed.
3894 */
3895 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003896mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
3898 /* Nothing to do. */
3899}
3900
Bram Moolenaar205b8862011-09-07 15:04:31 +02003901/*
3902 * Wait for process "child" to end.
3903 * Return "child" if it exited properly, <= 0 on error.
3904 */
3905 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01003906wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02003907{
3908 pid_t wait_pid = 0;
3909
3910 while (wait_pid != child)
3911 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02003912 /* When compiled with Python threads are probably used, in which case
3913 * wait() sometimes hangs for no obvious reason. Use waitpid()
3914 * instead and loop (like the GUI). Also needed for other interfaces,
3915 * they might call system(). */
3916# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02003917 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02003918# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02003919 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02003920# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02003921 if (wait_pid == 0)
3922 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003923 /* Wait for 10 msec before trying again. */
Bram Moolenaar205b8862011-09-07 15:04:31 +02003924 mch_delay(10L, TRUE);
3925 continue;
3926 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02003927 if (wait_pid <= 0
3928# ifdef ECHILD
3929 && errno == ECHILD
3930# endif
3931 )
3932 break;
3933 }
3934 return wait_pid;
3935}
3936
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003937#if defined(FEAT_JOB_CHANNEL) || !defined(USE_SYSTEM) || defined(PROTO)
Bram Moolenaar72801402016-02-09 11:37:50 +01003938/*
3939 * Parse "cmd" and put the white-separated parts in "argv".
3940 * "argv" is an allocated array with "argc" entries.
3941 * Returns FAIL when out of memory.
3942 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01003943 int
3944mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
3945{
3946 int i;
3947 char_u *p;
3948 int inquote;
3949
3950 /*
3951 * Do this loop twice:
3952 * 1: find number of arguments
3953 * 2: separate them and build argv[]
3954 */
3955 for (i = 0; i < 2; ++i)
3956 {
Bram Moolenaar6231cb82016-04-26 19:42:42 +02003957 p = skipwhite(cmd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003958 inquote = FALSE;
3959 *argc = 0;
3960 for (;;)
3961 {
3962 if (i == 1)
3963 (*argv)[*argc] = (char *)p;
3964 ++*argc;
3965 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
3966 {
3967 if (*p == '"')
3968 inquote = !inquote;
3969 ++p;
3970 }
3971 if (*p == NUL)
3972 break;
3973 if (i == 1)
3974 *p++ = NUL;
3975 p = skipwhite(p);
3976 }
3977 if (*argv == NULL)
3978 {
3979 if (use_shcf)
3980 {
3981 /* Account for possible multiple args in p_shcf. */
3982 p = p_shcf;
3983 for (;;)
3984 {
3985 p = skiptowhite(p);
3986 if (*p == NUL)
3987 break;
3988 ++*argc;
3989 p = skipwhite(p);
3990 }
3991 }
3992
3993 *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *)));
3994 if (*argv == NULL) /* out of memory */
3995 return FAIL;
3996 }
3997 }
3998 return OK;
3999}
4000#endif
4001
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004002#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004003 static void
4004set_child_environment(void)
4005{
4006# ifdef HAVE_SETENV
4007 char envbuf[50];
4008# else
4009 static char envbuf_Rows[20];
4010 static char envbuf_Columns[20];
4011# endif
4012
4013 /* Simulate to have a dumb terminal (for now) */
4014# ifdef HAVE_SETENV
4015 setenv("TERM", "dumb", 1);
4016 sprintf((char *)envbuf, "%ld", Rows);
4017 setenv("ROWS", (char *)envbuf, 1);
4018 sprintf((char *)envbuf, "%ld", Rows);
4019 setenv("LINES", (char *)envbuf, 1);
4020 sprintf((char *)envbuf, "%ld", Columns);
4021 setenv("COLUMNS", (char *)envbuf, 1);
4022# else
4023 /*
4024 * Putenv does not copy the string, it has to remain valid.
4025 * Use a static array to avoid losing allocated memory.
4026 */
4027 putenv("TERM=dumb");
4028 sprintf(envbuf_Rows, "ROWS=%ld", Rows);
4029 putenv(envbuf_Rows);
4030 sprintf(envbuf_Rows, "LINES=%ld", Rows);
4031 putenv(envbuf_Rows);
4032 sprintf(envbuf_Columns, "COLUMNS=%ld", Columns);
4033 putenv(envbuf_Columns);
4034# endif
4035}
4036#endif
4037
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004039mch_call_shell(
4040 char_u *cmd,
4041 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042{
4043#ifdef VMS
4044 char *ifn = NULL;
4045 char *ofn = NULL;
4046#endif
4047 int tmode = cur_tmode;
4048#ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */
4049 int x;
4050# ifndef __EMX__
4051 char_u *newcmd; /* only needed for unix */
4052# else
4053 /*
4054 * Set the preferred shell in the EMXSHELL environment variable (but
4055 * only if it is different from what is already in the environment).
4056 * Emx then takes care of whether to use "/c" or "-c" in an
4057 * intelligent way. Simply pass the whole thing to emx's system() call.
4058 * Emx also starts an interactive shell if system() is passed an empty
4059 * string.
4060 */
4061 char_u *p, *old;
4062
4063 if (((old = (char_u *)getenv("EMXSHELL")) == NULL) || STRCMP(old, p_sh))
4064 {
4065 /* should check HAVE_SETENV, but I know we don't have it. */
4066 p = alloc(10 + strlen(p_sh));
4067 if (p)
4068 {
4069 sprintf((char *)p, "EMXSHELL=%s", p_sh);
4070 putenv((char *)p); /* don't free the pointer! */
4071 }
4072 }
4073# endif
4074
4075 out_flush();
4076
4077 if (options & SHELL_COOKED)
4078 settmode(TMODE_COOK); /* set to normal mode */
4079
Bram Moolenaar62b42182010-09-21 22:09:37 +02004080# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004081 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004082 loose_clipboard();
4083# endif
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085# ifdef __EMX__
4086 if (cmd == NULL)
4087 x = system(""); /* this starts an interactive shell in emx */
4088 else
4089 x = system((char *)cmd);
4090 /* system() returns -1 when error occurs in starting shell */
4091 if (x == -1 && !emsg_silent)
4092 {
4093 MSG_PUTS(_("\nCannot execute shell "));
4094 msg_outtrans(p_sh);
4095 msg_putchar('\n');
4096 }
4097# else /* not __EMX__ */
4098 if (cmd == NULL)
4099 x = system((char *)p_sh);
4100 else
4101 {
4102# ifdef VMS
4103 if (ofn = strchr((char *)cmd, '>'))
4104 *ofn++ = '\0';
4105 if (ifn = strchr((char *)cmd, '<'))
4106 {
4107 char *p;
4108
4109 *ifn++ = '\0';
4110 p = strchr(ifn,' '); /* chop off any trailing spaces */
4111 if (p)
4112 *p = '\0';
4113 }
4114 if (ofn)
4115 x = vms_sys((char *)cmd, ofn, ifn);
4116 else
4117 x = system((char *)cmd);
4118# else
4119 newcmd = lalloc(STRLEN(p_sh)
4120 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4121 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4122 if (newcmd == NULL)
4123 x = 0;
4124 else
4125 {
4126 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4127 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4128 (char *)p_shcf,
4129 (char *)cmd);
4130 x = system((char *)newcmd);
4131 vim_free(newcmd);
4132 }
4133# endif
4134 }
4135# ifdef VMS
4136 x = vms_sys_status(x);
4137# endif
4138 if (emsg_silent)
4139 ;
4140 else if (x == 127)
4141 MSG_PUTS(_("\nCannot execute shell sh\n"));
4142# endif /* __EMX__ */
4143 else if (x && !(options & SHELL_SILENT))
4144 {
4145 MSG_PUTS(_("\nshell returned "));
4146 msg_outnum((long)x);
4147 msg_putchar('\n');
4148 }
4149
4150 if (tmode == TMODE_RAW)
4151 settmode(TMODE_RAW); /* set to raw mode */
4152# ifdef FEAT_TITLE
4153 resettitle();
4154# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004155# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4156 restore_clipboard();
4157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return x;
4159
4160#else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */
4161
Bram Moolenaardf177f62005-02-22 08:39:57 +00004162# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4163 127, some shells use that already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164
Bram Moolenaar835dc632016-02-07 14:27:38 +01004165 char_u *newcmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004167 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 pid_t wait_pid = 0;
4169# ifdef HAVE_UNION_WAIT
4170 union wait status;
4171# else
4172 int status = -1;
4173# endif
4174 int retval = -1;
4175 char **argv = NULL;
4176 int argc;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004177 char_u *p_shcf_copy = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 int i;
4179 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004181# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 int pty_slave_fd = -1;
4183 char *tty_name;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004184# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004185 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 int fd_fromshell[2];
4187 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004188 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189
Bram Moolenaar62b42182010-09-21 22:09:37 +02004190 newcmd = vim_strsave(p_sh);
4191 if (newcmd == NULL) /* out of memory */
4192 goto error;
4193
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 out_flush();
4195 if (options & SHELL_COOKED)
4196 settmode(TMODE_COOK); /* set to normal mode */
4197
Bram Moolenaar835dc632016-02-07 14:27:38 +01004198 if (mch_parse_cmd(newcmd, TRUE, &argv, &argc) == FAIL)
4199 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004200
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 if (cmd != NULL)
4202 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004203 char_u *s;
4204
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 if (extra_shell_arg != NULL)
4206 argv[argc++] = (char *)extra_shell_arg;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004207
4208 /* Break 'shellcmdflag' into white separated parts. This doesn't
4209 * handle quoted strings, they are very unlikely to appear. */
4210 p_shcf_copy = alloc((unsigned)STRLEN(p_shcf) + 1);
4211 if (p_shcf_copy == NULL) /* out of memory */
4212 goto error;
4213 s = p_shcf_copy;
4214 p = p_shcf;
4215 while (*p != NUL)
4216 {
4217 argv[argc++] = (char *)s;
4218 while (*p && *p != ' ' && *p != TAB)
4219 *s++ = *p++;
4220 *s++ = NUL;
4221 p = skipwhite(p);
4222 }
4223
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 argv[argc++] = (char *)cmd;
4225 }
4226 argv[argc] = NULL;
4227
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004229 * For the GUI, when writing the output into the buffer and when reading
4230 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4231 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004233 if ((options & (SHELL_READ|SHELL_WRITE))
4234# ifdef FEAT_GUI
4235 || (gui.in_use && show_shell_mess)
4236# endif
4237 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004239# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 /*
4241 * Try to open a master pty.
4242 * If this works, open the slave pty.
4243 * If the slave can't be opened, close the master pty.
4244 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004245 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 {
4247 pty_master_fd = OpenPTY(&tty_name); /* open pty */
Bram Moolenaare70172e2011-08-04 19:36:52 +02004248 if (pty_master_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
Bram Moolenaare70172e2011-08-04 19:36:52 +02004250 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4251 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4252 * adding O_NOCTTY always works when defined. */
4253#ifdef O_NOCTTY
4254 pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4255#else
4256 pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4257#endif
4258 if (pty_slave_fd < 0)
4259 {
4260 close(pty_master_fd);
4261 pty_master_fd = -1;
4262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264 }
4265 /*
4266 * If not opening a pty or it didn't work, try using pipes.
4267 */
4268 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004269# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {
4271 pipe_error = (pipe(fd_toshell) < 0);
4272 if (!pipe_error) /* pipe create OK */
4273 {
4274 pipe_error = (pipe(fd_fromshell) < 0);
4275 if (pipe_error) /* pipe create failed */
4276 {
4277 close(fd_toshell[0]);
4278 close(fd_toshell[1]);
4279 }
4280 }
4281 if (pipe_error)
4282 {
4283 MSG_PUTS(_("\nCannot create pipes\n"));
4284 out_flush();
4285 }
4286 }
4287 }
4288
4289 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 {
4291# ifdef __BEOS__
4292 beos_cleanup_read_thread();
4293# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004294
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 if ((pid = fork()) == -1) /* maybe we should use vfork() */
4296 {
4297 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004298 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004300 || (gui.in_use && show_shell_mess)
4301# endif
4302 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004304# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 if (pty_master_fd >= 0) /* close the pseudo tty */
4306 {
4307 close(pty_master_fd);
4308 close(pty_slave_fd);
4309 }
4310 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
4313 close(fd_toshell[0]);
4314 close(fd_toshell[1]);
4315 close(fd_fromshell[0]);
4316 close(fd_fromshell[1]);
4317 }
4318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 }
4320 else if (pid == 0) /* child */
4321 {
4322 reset_signals(); /* handle signals normally */
4323
4324 if (!show_shell_mess || (options & SHELL_EXPAND))
4325 {
4326 int fd;
4327
4328 /*
4329 * Don't want to show any message from the shell. Can't just
4330 * close stdout and stderr though, because some systems will
4331 * break if you try to write to them after that, so we must
4332 * use dup() to replace them with something else -- webb
4333 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4334 * waiting for input.
4335 */
4336 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4337 fclose(stdin);
4338 fclose(stdout);
4339 fclose(stderr);
4340
4341 /*
4342 * If any of these open()'s and dup()'s fail, we just continue
4343 * anyway. It's not fatal, and on most systems it will make
4344 * no difference at all. On a few it will cause the execvp()
4345 * to exit with a non-zero status even when the completion
4346 * could be done, which is nothing too serious. If the open()
4347 * or dup() failed we'd just do the same thing ourselves
4348 * anyway -- webb
4349 */
4350 if (fd >= 0)
4351 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004352 ignored = dup(fd); /* To replace stdin (fd 0) */
4353 ignored = dup(fd); /* To replace stdout (fd 1) */
4354 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
4356 /* Don't need this now that we've duplicated it */
4357 close(fd);
4358 }
4359 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004360 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004362 || gui.in_use
4363# endif
4364 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 {
4366
Bram Moolenaardf177f62005-02-22 08:39:57 +00004367# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004368 /* Create our own process group, so that the child and all its
4369 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004370 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004371 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004372 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004373 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004374# if defined(SIGHUP)
4375 /* When doing "!xterm&" and 'shell' is bash: the shell
4376 * will exit and send SIGHUP to all processes in its
4377 * group, killing the just started process. Ignore SIGHUP
4378 * to avoid that. (suggested by Simon Schubert)
4379 */
4380 signal(SIGHUP, SIG_IGN);
4381# endif
4382 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004383# endif
4384# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004385 if (pty_slave_fd >= 0)
4386 {
4387 /* push stream discipline modules */
4388 if (options & SHELL_COOKED)
4389 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004391 /* Try to become controlling tty (probably doesn't work,
4392 * unless run by root) */
4393 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004395 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004396# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004397 set_child_environment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
Bram Moolenaara5792f52005-11-23 21:25:05 +00004399 /*
4400 * stderr is only redirected when using the GUI, so that a
4401 * program like gpg can still access the terminal to get a
4402 * passphrase using stderr.
4403 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004404# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 if (pty_master_fd >= 0)
4406 {
4407 close(pty_master_fd); /* close master side of pty */
4408
4409 /* set up stdin/stdout/stderr for the child */
4410 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004411 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004413 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004414 if (gui.in_use)
4415 {
4416 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004417 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
4420 close(pty_slave_fd); /* has been dupped, close it now */
4421 }
4422 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
4425 /* set up stdin for the child */
4426 close(fd_toshell[1]);
4427 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004428 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 close(fd_toshell[0]);
4430
4431 /* set up stdout for the child */
4432 close(fd_fromshell[0]);
4433 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004434 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 close(fd_fromshell[1]);
4436
Bram Moolenaara5792f52005-11-23 21:25:05 +00004437# ifdef FEAT_GUI
4438 if (gui.in_use)
4439 {
4440 /* set up stderr for the child */
4441 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004442 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004443 }
4444# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004447
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 /*
4449 * There is no type cast for the argv, because the type may be
4450 * different on different machines. This may cause a warning
4451 * message with strict compilers, don't worry about it.
4452 * Call _exit() instead of exit() to avoid closing the connection
4453 * to the X server (esp. with GTK, which uses atexit()).
4454 */
4455 execvp(argv[0], argv);
4456 _exit(EXEC_FAILED); /* exec failed, return failure code */
4457 }
4458 else /* parent */
4459 {
4460 /*
4461 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004462 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 */
4464 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004465 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467 /*
4468 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004469 * This is also used to pipe stdin/stdout to/from the external
4470 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004472 if ((options & (SHELL_READ|SHELL_WRITE))
4473# ifdef FEAT_GUI
4474 || (gui.in_use && show_shell_mess)
4475# endif
4476 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004478# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004480# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4484 int ta_len = 0; /* valid bytes in ta_buf[] */
4485 int len;
4486 int p_more_save;
4487 int old_State;
4488 int c;
4489 int toshell_fd;
4490 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004491 garray_T ga;
4492 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004493# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4494 struct timeval start_tv;
4495# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
Bram Moolenaardf177f62005-02-22 08:39:57 +00004497# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 if (pty_master_fd >= 0)
4499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 fromshell_fd = pty_master_fd;
4501 toshell_fd = dup(pty_master_fd);
4502 }
4503 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
4506 close(fd_toshell[0]);
4507 close(fd_fromshell[1]);
4508 toshell_fd = fd_toshell[1];
4509 fromshell_fd = fd_fromshell[0];
4510 }
4511
4512 /*
4513 * Write to the child if there are typed characters.
4514 * Read from the child if there are characters available.
4515 * Repeat the reading a few times if more characters are
4516 * available. Need to check for typed keys now and then, but
4517 * not too often (delays when no chars are available).
4518 * This loop is quit if no characters can be read from the pty
4519 * (WaitForChar detected special condition), or there are no
4520 * characters available and the child has exited.
4521 * Only check if the child has exited when there is no more
4522 * output. The child may exit before all the output has
4523 * been printed.
4524 *
4525 * Currently this busy loops!
4526 * This can probably dead-lock when the write blocks!
4527 */
4528 p_more_save = p_more;
4529 p_more = FALSE;
4530 old_State = State;
4531 State = EXTERNCMD; /* don't redraw at window resize */
4532
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004533 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004534 {
4535 /* Fork a process that will write the lines to the
4536 * external program. */
4537 if ((wpid = fork()) == -1)
4538 {
4539 MSG_PUTS(_("\nCannot fork\n"));
4540 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004541 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004542 {
4543 linenr_T lnum = curbuf->b_op_start.lnum;
4544 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004545 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004546 size_t l;
4547
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004548 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004549 for (;;)
4550 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004551 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004552 if (l == 0)
4553 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004554 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004555 /* NL -> NUL translation */
4556 len = write(toshell_fd, "", (size_t)1);
4557 else
4558 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004559 char_u *s = vim_strchr(lp + written, NL);
4560
Bram Moolenaar89d40322006-08-29 15:30:07 +00004561 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004562 s == NULL ? l
4563 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004564 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004565 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004566 {
4567 /* Finished a line, add a NL, unless this line
4568 * should not have one. */
4569 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004570 || (!curbuf->b_p_bin
4571 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004572 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004573 && (lnum !=
4574 curbuf->b_ml.ml_line_count
4575 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004576 ignored = write(toshell_fd, "\n",
4577 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004578 ++lnum;
4579 if (lnum > curbuf->b_op_end.lnum)
4580 {
4581 /* finished all the lines, close pipe */
4582 close(toshell_fd);
4583 toshell_fd = -1;
4584 break;
4585 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004586 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004587 written = 0;
4588 }
4589 else if (len > 0)
4590 written += len;
4591 }
4592 _exit(0);
4593 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004594 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004595 {
4596 close(toshell_fd);
4597 toshell_fd = -1;
4598 }
4599 }
4600
4601 if (options & SHELL_READ)
4602 ga_init2(&ga, 1, BUFLEN);
4603
4604 noread_cnt = 0;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004605# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4606 gettimeofday(&start_tv, NULL);
4607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 for (;;)
4609 {
4610 /*
4611 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004612 * if there are any.
4613 * Don't do this if we are expanding wild cards (would eat
4614 * typeahead).
4615 * Don't do this when filtering and terminal is in cooked
4616 * mode, the shell command will handle the I/O. Avoids
4617 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004618 * Don't get characters when the child has already
4619 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004620 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004621 * while (noread_cnt > 4), avoids that ":r !ls" eats
4622 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 */
4624 len = 0;
4625 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004626 && ((options &
4627 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4628 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004629# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004630 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004631# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004632 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004633 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004634 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004636 if (ta_len == 0)
4637 {
4638 /* Get extra characters when we don't have any.
4639 * Reset the counter and timer. */
4640 noread_cnt = 0;
4641# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4642 gettimeofday(&start_tv, NULL);
4643# endif
4644 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4645 }
4646 if (ta_len > 0 || len > 0)
4647 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 /*
4649 * For pipes:
4650 * Check for CTRL-C: send interrupt signal to child.
4651 * Check for CTRL-D: EOF, close pipe to child.
4652 */
4653 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4654 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004655# ifdef SIGINT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 /*
4657 * Send SIGINT to the child's group or all
4658 * processes in our group.
4659 */
4660 if (ta_buf[ta_len] == Ctrl_C
4661 || ta_buf[ta_len] == intr_char)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004662 {
4663# ifdef HAVE_SETSID
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 kill(-pid, SIGINT);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004665# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 kill(0, SIGINT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667# endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00004668 if (wpid > 0)
4669 kill(wpid, SIGINT);
4670 }
4671# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (pty_master_fd < 0 && toshell_fd >= 0
4673 && ta_buf[ta_len] == Ctrl_D)
4674 {
4675 close(toshell_fd);
4676 toshell_fd = -1;
4677 }
4678 }
4679
4680 /* replace K_BS by <BS> and K_DEL by <DEL> */
4681 for (i = ta_len; i < ta_len + len; ++i)
4682 {
4683 if (ta_buf[i] == CSI && len - i > 2)
4684 {
4685 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4686 if (c == K_DEL || c == K_KDEL || c == K_BS)
4687 {
4688 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4689 (size_t)(len - i - 2));
4690 if (c == K_DEL || c == K_KDEL)
4691 ta_buf[i] = DEL;
4692 else
4693 ta_buf[i] = Ctrl_H;
4694 len -= 2;
4695 }
4696 }
4697 else if (ta_buf[i] == '\r')
4698 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00004699# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00004701 i += (*mb_ptr2len_len)(ta_buf + i,
4702 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705
4706 /*
4707 * For pipes: echo the typed characters.
4708 * For a pty this does not seem to work.
4709 */
4710 if (pty_master_fd < 0)
4711 {
4712 for (i = ta_len; i < ta_len + len; ++i)
4713 {
4714 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4715 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004716# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 else if (has_mbyte)
4718 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004719 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720
4721 msg_outtrans_len(ta_buf + i, l);
4722 i += l - 1;
4723 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004724# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 else
4726 msg_outtrans_len(ta_buf + i, 1);
4727 }
4728 windgoto(msg_row, msg_col);
4729 out_flush();
4730 }
4731
4732 ta_len += len;
4733
4734 /*
4735 * Write the characters to the child, unless EOF has
4736 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004737 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004738 * When writing buffer lines, drop the typed
4739 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004741 if (options & SHELL_WRITE)
4742 ta_len = 0;
4743 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
4745 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
4746 if (len > 0)
4747 {
4748 ta_len -= len;
4749 mch_memmove(ta_buf, ta_buf + len, ta_len);
4750 }
4751 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
4754
Bram Moolenaardf177f62005-02-22 08:39:57 +00004755 if (got_int)
4756 {
4757 /* CTRL-C sends a signal to the child, we ignore it
4758 * ourselves */
4759# ifdef HAVE_SETSID
4760 kill(-pid, SIGINT);
4761# else
4762 kill(0, SIGINT);
4763# endif
4764 if (wpid > 0)
4765 kill(wpid, SIGINT);
4766 got_int = FALSE;
4767 }
4768
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 /*
4770 * Check if the child has any characters to be printed.
4771 * Read them and write them to our window. Repeat this as
4772 * long as there is something to do, avoid the 10ms wait
4773 * for mch_inchar(), or sending typeahead characters to
4774 * the external process.
4775 * TODO: This should handle escape sequences, compatible
4776 * to some terminal (vt52?).
4777 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004778 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01004779 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004781 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00004782# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004784# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00004786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 );
4788 if (len <= 0) /* end of file or error */
4789 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004790
4791 noread_cnt = 0;
4792 if (options & SHELL_READ)
4793 {
4794 /* Do NUL -> NL translation, append NL separated
4795 * lines to the current buffer. */
4796 for (i = 0; i < len; ++i)
4797 {
4798 if (buffer[i] == NL)
4799 append_ga_line(&ga);
4800 else if (buffer[i] == NUL)
4801 ga_append(&ga, NL);
4802 else
4803 ga_append(&ga, buffer[i]);
4804 }
4805 }
4806# ifdef FEAT_MBYTE
4807 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 {
4809 int l;
4810
Bram Moolenaardf177f62005-02-22 08:39:57 +00004811 len += buffer_off;
4812 buffer[len] = NUL;
4813
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 /* Check if the last character in buffer[] is
4815 * incomplete, keep these bytes for the next
4816 * round. */
4817 for (p = buffer; p < buffer + len; p += l)
4818 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004819 l = mb_cptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 if (l == 0)
4821 l = 1; /* NUL byte? */
4822 else if (MB_BYTE2LEN(*p) != l)
4823 break;
4824 }
4825 if (p == buffer) /* no complete character */
4826 {
4827 /* avoid getting stuck at an illegal byte */
4828 if (len >= 12)
4829 ++p;
4830 else
4831 {
4832 buffer_off = len;
4833 continue;
4834 }
4835 }
4836 c = *p;
4837 *p = NUL;
4838 msg_puts(buffer);
4839 if (p < buffer + len)
4840 {
4841 *p = c;
4842 buffer_off = (buffer + len) - p;
4843 mch_memmove(buffer, p, buffer_off);
4844 continue;
4845 }
4846 buffer_off = 0;
4847 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004848# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
4851 buffer[len] = NUL;
4852 msg_puts(buffer);
4853 }
4854
4855 windgoto(msg_row, msg_col);
4856 cursor_on();
4857 out_flush();
4858 if (got_int)
4859 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004860
4861# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004862 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004863 {
4864 struct timeval now_tv;
4865 long msec;
4866
4867 /* Avoid that we keep looping here without
4868 * checking for a CTRL-C for a long time. Don't
4869 * break out too often to avoid losing typeahead. */
4870 gettimeofday(&now_tv, NULL);
4871 msec = (now_tv.tv_sec - start_tv.tv_sec) * 1000L
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004872 + (now_tv.tv_usec - start_tv.tv_usec) / 1000L;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004873 if (msec > 2000)
4874 {
4875 noread_cnt = 5;
4876 break;
4877 }
4878 }
4879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004882 /* If we already detected the child has finished, continue
4883 * reading output for a short while. Some text may be
4884 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004885 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004886 {
4887 if (noread_cnt < 5)
4888 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004889 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004890 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004891
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 /*
4893 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004894 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004896# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004897 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004898# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004900# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
4902 || (wait_pid == pid && WIFEXITED(status)))
4903 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004904 /* Don't break the loop yet, try reading more
4905 * characters from "fromshell_fd" first. When using
4906 * pipes there might still be something to read and
4907 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004910 else
4911 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004912
Bram Moolenaar95a51352013-03-21 22:53:50 +01004913# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004914 /* Handle any X events, e.g. serving the clipboard. */
4915 clip_update();
4916# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918finished:
4919 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004920 if (options & SHELL_READ)
4921 {
4922 if (ga.ga_len > 0)
4923 {
4924 append_ga_line(&ga);
4925 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004926 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004927 }
4928 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004929 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004930 ga_clear(&ga);
4931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 /*
4934 * Give all typeahead that wasn't used back to ui_inchar().
4935 */
4936 if (ta_len)
4937 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 State = old_State;
4939 if (toshell_fd >= 0)
4940 close(toshell_fd);
4941 close(fromshell_fd);
4942 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01004943# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004944 else
4945 {
4946 /*
4947 * Similar to the loop above, but only handle X events, no
4948 * I/O.
4949 */
4950 for (;;)
4951 {
4952 if (got_int)
4953 {
4954 /* CTRL-C sends a signal to the child, we ignore it
4955 * ourselves */
4956# ifdef HAVE_SETSID
4957 kill(-pid, SIGINT);
4958# else
4959 kill(0, SIGINT);
4960# endif
4961 got_int = FALSE;
4962 }
4963# ifdef __NeXT__
4964 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
4965# else
4966 wait_pid = waitpid(pid, &status, WNOHANG);
4967# endif
4968 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
4969 || (wait_pid == pid && WIFEXITED(status)))
4970 {
4971 wait_pid = pid;
4972 break;
4973 }
4974
4975 /* Handle any X events, e.g. serving the clipboard. */
4976 clip_update();
4977
4978 mch_delay(10L, TRUE);
4979 }
4980 }
4981# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982
4983 /*
4984 * Wait until our child has exited.
4985 * Ignore wait() returning pids of other children and returning
4986 * because of some signal like SIGWINCH.
4987 * Don't wait if wait_pid was already set above, indicating the
4988 * child already exited.
4989 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02004990 if (wait_pid != pid)
4991 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
Bram Moolenaar624891f2010-10-13 16:22:09 +02004993# ifdef FEAT_GUI
4994 /* Close slave side of pty. Only do this after the child has
4995 * exited, otherwise the child may hang when it tries to write on
4996 * the pty. */
4997 if (pty_master_fd >= 0)
4998 close(pty_slave_fd);
4999# endif
5000
Bram Moolenaardf177f62005-02-22 08:39:57 +00005001 /* Make sure the child that writes to the external program is
5002 * dead. */
5003 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005004 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005005 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005006 wait4pid(wpid, NULL);
5007 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005008
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 /*
5010 * Set to raw mode right now, otherwise a CTRL-C after
5011 * catch_signals() will kill Vim.
5012 */
5013 if (tmode == TMODE_RAW)
5014 settmode(TMODE_RAW);
5015 did_settmode = TRUE;
5016 set_signals();
5017
5018 if (WIFEXITED(status))
5019 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005020 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005022 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
5024 if (retval == EXEC_FAILED)
5025 {
5026 MSG_PUTS(_("\nCannot execute shell "));
5027 msg_outtrans(p_sh);
5028 msg_putchar('\n');
5029 }
5030 else if (!(options & SHELL_SILENT))
5031 {
5032 MSG_PUTS(_("\nshell returned "));
5033 msg_outnum((long)retval);
5034 msg_putchar('\n');
5035 }
5036 }
5037 }
5038 else
5039 MSG_PUTS(_("\nCommand terminated\n"));
5040 }
5041 }
5042 vim_free(argv);
Bram Moolenaarea35ef62011-08-04 22:59:28 +02005043 vim_free(p_shcf_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044
5045error:
5046 if (!did_settmode)
5047 if (tmode == TMODE_RAW)
5048 settmode(TMODE_RAW); /* set to raw mode */
5049# ifdef FEAT_TITLE
5050 resettitle();
5051# endif
5052 vim_free(newcmd);
5053
5054 return retval;
5055
5056#endif /* USE_SYSTEM */
5057}
5058
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005059#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005060 void
Bram Moolenaar802d5592016-03-05 22:05:27 +01005061mch_start_job(char **argv, job_T *job, jobopt_T *options UNUSED)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005062{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005063 pid_t pid;
5064 int fd_in[2]; /* for stdin */
5065 int fd_out[2]; /* for stdout */
5066 int fd_err[2]; /* for stderr */
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005067 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005068 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5069 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5070 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005071 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005072 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5073 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005074 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005075
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005076 if (use_out_for_err && use_null_for_out)
5077 use_null_for_err = TRUE;
5078
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005079 /* default is to fail */
5080 job->jv_status = JOB_FAILED;
5081 fd_in[0] = -1;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005082 fd_in[1] = -1;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005083 fd_out[0] = -1;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005084 fd_out[1] = -1;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005085 fd_err[0] = -1;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005086 fd_err[1] = -1;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005087
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005088 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005089 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005090 if (use_file_for_in)
5091 {
5092 char_u *fname = options->jo_io_name[PART_IN];
5093
5094 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5095 if (fd_in[0] < 0)
5096 {
5097 EMSG2(_(e_notopen), fname);
5098 goto failed;
5099 }
5100 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005101 else if (!use_null_for_in && pipe(fd_in) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005102 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005103
5104 if (use_file_for_out)
5105 {
5106 char_u *fname = options->jo_io_name[PART_OUT];
5107
5108 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5109 if (fd_out[1] < 0)
5110 {
5111 EMSG2(_(e_notopen), fname);
5112 goto failed;
5113 }
5114 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005115 else if (!use_null_for_out && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005116 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005117
5118 if (use_file_for_err)
5119 {
5120 char_u *fname = options->jo_io_name[PART_ERR];
5121
5122 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5123 if (fd_err[1] < 0)
5124 {
5125 EMSG2(_(e_notopen), fname);
5126 goto failed;
5127 }
5128 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005129 else if (!use_out_for_err && !use_null_for_err && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005130 goto failed;
5131
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005132 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5133 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005134 if (options->jo_set & JO_CHANNEL)
5135 {
5136 channel = options->jo_channel;
5137 if (channel != NULL)
5138 ++channel->ch_refcount;
5139 }
5140 else
5141 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005142 if (channel == NULL)
5143 goto failed;
5144 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005145
5146 pid = fork(); /* maybe we should use vfork() */
5147 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005148 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005149 /* failed to fork */
5150 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005151 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005152
5153 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005154 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005155 int null_fd = -1;
5156 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005157
Bram Moolenaar835dc632016-02-07 14:27:38 +01005158 /* child */
5159 reset_signals(); /* handle signals normally */
5160
5161# ifdef HAVE_SETSID
5162 /* Create our own process group, so that the child and all its
5163 * children can be kill()ed. Don't do this when using pipes,
5164 * because stdin is not a tty, we would lose /dev/tty. */
5165 (void)setsid();
5166# endif
5167
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005168 set_child_environment();
5169
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005170 if (use_null_for_in || use_null_for_out || use_null_for_err)
5171 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
5172
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005173 /* set up stdin for the child */
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005174 if (use_null_for_in && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005175 {
5176 close(0);
5177 ignored = dup(null_fd);
5178 }
5179 else
5180 {
5181 if (!use_file_for_in)
5182 close(fd_in[1]);
5183 close(0);
5184 ignored = dup(fd_in[0]);
5185 close(fd_in[0]);
5186 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005187
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005188 /* set up stderr for the child */
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005189 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005190 {
5191 close(2);
5192 ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005193 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005194 }
5195 else if (use_out_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005196 {
5197 close(2);
5198 ignored = dup(fd_out[1]);
5199 }
5200 else
5201 {
Bram Moolenaare98d1212016-03-08 15:37:41 +01005202 if (!use_file_for_err)
5203 close(fd_err[0]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005204 close(2);
5205 ignored = dup(fd_err[1]);
5206 close(fd_err[1]);
5207 }
5208
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005209 /* set up stdout for the child */
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005210 if (use_null_for_out && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005211 {
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005212 close(1);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005213 ignored = dup(null_fd);
5214 }
5215 else
5216 {
5217 if (!use_file_for_out)
5218 close(fd_out[0]);
5219 close(1);
5220 ignored = dup(fd_out[1]);
5221 close(fd_out[1]);
5222 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005223
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005224 if (null_fd >= 0)
5225 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005226
Bram Moolenaar835dc632016-02-07 14:27:38 +01005227 /* See above for type of argv. */
5228 execvp(argv[0], argv);
5229
Bram Moolenaar4694a172016-04-21 14:05:23 +02005230 if (stderr_works)
5231 perror("executing job failed");
Bram Moolenaar835dc632016-02-07 14:27:38 +01005232 _exit(EXEC_FAILED); /* exec failed, return failure code */
5233 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005234
5235 /* parent */
5236 job->jv_pid = pid;
5237 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005238 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005239
5240 /* child stdin, stdout and stderr */
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005241 if (!use_file_for_in && fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005242 close(fd_in[0]);
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005243 if (!use_file_for_out && fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005244 close(fd_out[1]);
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005245 if (!use_out_for_err && !use_file_for_err && fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005246 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005247 if (channel != NULL)
5248 {
5249 channel_set_pipes(channel,
5250 use_file_for_in || use_null_for_in
5251 ? INVALID_FD : fd_in[1],
5252 use_file_for_out || use_null_for_out
5253 ? INVALID_FD : fd_out[0],
5254 use_out_for_err || use_file_for_err || use_null_for_err
Bram Moolenaare98d1212016-03-08 15:37:41 +01005255 ? INVALID_FD : fd_err[0]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005256 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005257 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005258
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005259 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005260 return;
5261
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005262failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005263 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005264 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005265 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005266 if (fd_in[1] >= 0)
5267 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005268 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005269 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005270 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005271 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005272 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005273 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005274 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005275 close(fd_err[1]);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005276}
5277
5278 char *
5279mch_job_status(job_T *job)
5280{
5281# ifdef HAVE_UNION_WAIT
5282 union wait status;
5283# else
5284 int status = -1;
5285# endif
5286 pid_t wait_pid = 0;
5287
5288# ifdef __NeXT__
5289 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5290# else
5291 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5292# endif
5293 if (wait_pid == -1)
5294 {
5295 /* process must have exited */
5296 job->jv_status = JOB_ENDED;
5297 return "dead";
5298 }
5299 if (wait_pid == 0)
5300 return "run";
5301 if (WIFEXITED(status))
5302 {
5303 /* LINTED avoid "bitwise operation on signed value" */
5304 job->jv_exitval = WEXITSTATUS(status);
5305 job->jv_status = JOB_ENDED;
5306 return "dead";
5307 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005308 if (WIFSIGNALED(status))
5309 {
5310 job->jv_exitval = -1;
5311 job->jv_status = JOB_ENDED;
5312 return "dead";
5313 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005314 return "run";
5315}
5316
5317 int
5318mch_stop_job(job_T *job, char_u *how)
5319{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005320 int sig = -1;
5321 pid_t job_pid;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005322
Bram Moolenaar923d9262016-02-25 20:56:01 +01005323 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005324 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005325 else if (STRCMP(how, "hup") == 0)
5326 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005327 else if (STRCMP(how, "quit") == 0)
5328 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005329 else if (STRCMP(how, "int") == 0)
5330 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005331 else if (STRCMP(how, "kill") == 0)
5332 sig = SIGKILL;
5333 else if (isdigit(*how))
5334 sig = atoi((char *)how);
5335 else
5336 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005337
Bram Moolenaar72801402016-02-09 11:37:50 +01005338 /* TODO: have an option to only kill the process, not the group? */
Bram Moolenaar76467df2016-02-12 19:30:26 +01005339 job_pid = job->jv_pid;
5340 if (job_pid == getpgid(job_pid))
5341 job_pid = -job_pid;
5342
5343 kill(job_pid, sig);
5344
Bram Moolenaar835dc632016-02-07 14:27:38 +01005345 return OK;
5346}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005347
5348/*
5349 * Clear the data related to "job".
5350 */
5351 void
5352mch_clear_job(job_T *job)
5353{
5354 /* call waitpid because child process may become zombie */
5355# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005356 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005357# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005358 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005359# endif
5360}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005361#endif
5362
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363/*
5364 * Check for CTRL-C typed by reading all available characters.
5365 * In cooked mode we should get SIGINT, no need to check.
5366 */
5367 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005368mch_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005370 if (curr_tmode == TMODE_RAW && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 fill_input_buf(FALSE);
5372}
5373
5374/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005375 * Wait "msec" msec until a character is available from the mouse, keyboard,
5376 * from inbuf[].
5377 * "msec" == -1 will block forever.
5378 * Invokes timer callbacks when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005380 * Returns TRUE when a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 */
5382 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005383WaitForChar(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005385#ifdef FEAT_TIMERS
5386 long due_time;
5387 long remaining = msec;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005388 int break_loop = FALSE;
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005389 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005390
5391 /* When waiting very briefly don't trigger timers. */
5392 if (msec >= 0 && msec < 10L)
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005393 return WaitForCharOrMouse(msec, NULL);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005394
5395 while (msec < 0 || remaining > 0)
5396 {
5397 /* Trigger timers and then get the time in msec until the next one is
5398 * due. Wait up to that time. */
5399 due_time = check_due_timer();
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005400 if (typebuf.tb_change_cnt != tb_change_cnt)
5401 {
5402 /* timer may have used feedkeys() */
5403 return FALSE;
5404 }
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005405 if (due_time <= 0 || (msec > 0 && due_time > remaining))
5406 due_time = remaining;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005407 if (WaitForCharOrMouse(due_time, &break_loop))
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005408 return TRUE;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005409 if (break_loop)
5410 /* Nothing available, but need to return so that side effects get
5411 * handled, such as handling a message on a channel. */
5412 return FALSE;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005413 if (msec > 0)
5414 remaining -= due_time;
5415 }
5416 return FALSE;
5417#else
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005418 return WaitForCharOrMouse(msec, NULL);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005419#endif
5420}
5421
5422/*
5423 * Wait "msec" msec until a character is available from the mouse or keyboard
5424 * or from inbuf[].
5425 * "msec" == -1 will block forever.
5426 * When a GUI is being used, this will never get called -- webb
5427 */
5428 static int
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005429WaitForCharOrMouse(long msec, int *break_loop)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005430{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431#ifdef FEAT_MOUSE_GPM
5432 int gpm_process_wanted;
5433#endif
5434#ifdef FEAT_XCLIPBOARD
5435 int rest;
5436#endif
5437 int avail;
5438
5439 if (input_available()) /* something in inbuf[] */
5440 return 1;
5441
5442#if defined(FEAT_MOUSE_DEC)
5443 /* May need to query the mouse position. */
5444 if (WantQueryMouse)
5445 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005446 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5448 }
5449#endif
5450
5451 /*
5452 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5453 * events. This is a bit complicated, because they might both be defined.
5454 */
5455#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5456# ifdef FEAT_XCLIPBOARD
5457 rest = 0;
5458 if (do_xterm_trace())
5459 rest = msec;
5460# endif
5461 do
5462 {
5463# ifdef FEAT_XCLIPBOARD
5464 if (rest != 0)
5465 {
5466 msec = XT_TRACE_DELAY;
5467 if (rest >= 0 && rest < XT_TRACE_DELAY)
5468 msec = rest;
5469 if (rest >= 0)
5470 rest -= msec;
5471 }
5472# endif
5473# ifdef FEAT_MOUSE_GPM
5474 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005475 avail = RealWaitForChar(read_cmd_fd, msec,
5476 &gpm_process_wanted, break_loop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477# else
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005478 avail = RealWaitForChar(read_cmd_fd, msec, NULL, break_loop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479# endif
5480 if (!avail)
5481 {
5482 if (input_available())
5483 return 1;
5484# ifdef FEAT_XCLIPBOARD
5485 if (rest == 0 || !do_xterm_trace())
5486# endif
5487 break;
5488 }
5489 }
5490 while (FALSE
5491# ifdef FEAT_MOUSE_GPM
5492 || (gpm_process_wanted && mch_gpm_process() == 0)
5493# endif
5494# ifdef FEAT_XCLIPBOARD
5495 || (!avail && rest != 0)
5496# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005497 )
5498 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499
5500#else
Bram Moolenaare1581302016-03-26 21:04:48 +01005501 avail = RealWaitForChar(read_cmd_fd, msec, NULL, break_loop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502#endif
5503 return avail;
5504}
5505
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01005506#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507/*
5508 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005509 * "msec" == 0 will check for characters once.
5510 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005513 * Or when a clientserver message is on the queue.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515#if defined(__BEOS__)
5516 int
5517#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005518 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519#endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005520RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *break_loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
5522 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005523 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005524#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 static int busy = FALSE;
5526
5527 /* May retry getting characters after an event was handled. */
5528# define MAY_LOOP
5529
5530# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
5531 /* Remember at what time we started, so that we know how much longer we
5532 * should wait after being interrupted. */
5533# define USE_START_TV
5534 struct timeval start_tv;
5535
5536 if (msec > 0 && (
5537# ifdef FEAT_XCLIPBOARD
5538 xterm_Shell != (Widget)0
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005539# if defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 ||
5541# endif
5542# endif
5543# ifdef USE_XSMP
5544 xsmp_icefd != -1
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005545# ifdef FEAT_MZSCHEME
5546 ||
5547# endif
5548# endif
5549# ifdef FEAT_MZSCHEME
5550 (mzthreads_allowed() && p_mzq > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551# endif
5552 ))
5553 gettimeofday(&start_tv, NULL);
5554# endif
5555
5556 /* Handle being called recursively. This may happen for the session
5557 * manager stuff, it may save the file, which does a breakcheck. */
5558 if (busy)
5559 return 0;
5560#endif
5561
5562#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005563 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564#endif
5565 {
5566#ifdef MAY_LOOP
5567 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005568# ifdef FEAT_MZSCHEME
5569 int mzquantum_used = FALSE;
5570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571#endif
5572#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005573 /* each channel may use in, out and err */
5574 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 int nfd;
5576# ifdef FEAT_XCLIPBOARD
5577 int xterm_idx = -1;
5578# endif
5579# ifdef FEAT_MOUSE_GPM
5580 int gpm_idx = -1;
5581# endif
5582# ifdef USE_XSMP
5583 int xsmp_idx = -1;
5584# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005585 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005587# ifdef FEAT_MZSCHEME
5588 mzvim_check_threads();
5589 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5590 {
5591 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
5592 mzquantum_used = TRUE;
5593 }
5594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 fds[0].fd = fd;
5596 fds[0].events = POLLIN;
5597 nfd = 1;
5598
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005600 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 if (xterm_Shell != (Widget)0)
5602 {
5603 xterm_idx = nfd;
5604 fds[nfd].fd = ConnectionNumber(xterm_dpy);
5605 fds[nfd].events = POLLIN;
5606 nfd++;
5607 }
5608# endif
5609# ifdef FEAT_MOUSE_GPM
5610 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5611 {
5612 gpm_idx = nfd;
5613 fds[nfd].fd = gpm_fd;
5614 fds[nfd].events = POLLIN;
5615 nfd++;
5616 }
5617# endif
5618# ifdef USE_XSMP
5619 if (xsmp_icefd != -1)
5620 {
5621 xsmp_idx = nfd;
5622 fds[nfd].fd = xsmp_icefd;
5623 fds[nfd].events = POLLIN;
5624 nfd++;
5625 }
5626# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005627#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01005628 nfd = channel_poll_setup(nfd, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005631 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005632
5633 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005634 if (break_loop != NULL && ret > 0)
5635 *break_loop = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005636
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005637# ifdef FEAT_MZSCHEME
5638 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005639 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005640 finished = FALSE;
5641# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643# ifdef FEAT_XCLIPBOARD
5644 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
5645 {
5646 xterm_update(); /* Maybe we should hand out clipboard */
5647 if (--ret == 0 && !input_available())
5648 /* Try again */
5649 finished = FALSE;
5650 }
5651# endif
5652# ifdef FEAT_MOUSE_GPM
5653 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
5654 {
5655 *check_for_gpm = 1;
5656 }
5657# endif
5658# ifdef USE_XSMP
5659 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
5660 {
5661 if (fds[xsmp_idx].revents & POLLIN)
5662 {
5663 busy = TRUE;
5664 xsmp_handle_requests();
5665 busy = FALSE;
5666 }
5667 else if (fds[xsmp_idx].revents & POLLHUP)
5668 {
5669 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005670 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 xsmp_close();
5672 }
5673 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005674 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 }
5676# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005677#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01005678 if (ret > 0)
5679 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681
5682
5683#else /* HAVE_SELECT */
5684
5685 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005686 struct timeval *tvp;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005687 fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005689 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005691# ifdef FEAT_MZSCHEME
5692 mzvim_check_threads();
5693 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5694 {
5695 towait = p_mzq; /* don't wait longer than 'mzquantum' */
5696 mzquantum_used = TRUE;
5697 }
5698# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699# ifdef __EMX__
5700 /* don't check for incoming chars if not in raw mode, because select()
5701 * always returns TRUE then (in some version of emx.dll) */
5702 if (curr_tmode != TMODE_RAW)
5703 return 0;
5704# endif
5705
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005706 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005708 tv.tv_sec = towait / 1000;
5709 tv.tv_usec = (towait % 1000) * (1000000/1000);
5710 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005712 else
5713 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
5715 /*
5716 * Select on ready for reading and exceptional condition (end of file).
5717 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005718select_eintr:
5719 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005720 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 FD_ZERO(&efds);
5722 FD_SET(fd, &rfds);
5723# if !defined(__QNX__) && !defined(__CYGWIN32__)
5724 /* For QNX select() always returns 1 if this is set. Why? */
5725 FD_SET(fd, &efds);
5726# endif
5727 maxfd = fd;
5728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005730 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 if (xterm_Shell != (Widget)0)
5732 {
5733 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
5734 if (maxfd < ConnectionNumber(xterm_dpy))
5735 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005736
5737 /* An event may have already been read but not handled. In
5738 * particulary, XFlush may cause this. */
5739 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 }
5741# endif
5742# ifdef FEAT_MOUSE_GPM
5743 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5744 {
5745 FD_SET(gpm_fd, &rfds);
5746 FD_SET(gpm_fd, &efds);
5747 if (maxfd < gpm_fd)
5748 maxfd = gpm_fd;
5749 }
5750# endif
5751# ifdef USE_XSMP
5752 if (xsmp_icefd != -1)
5753 {
5754 FD_SET(xsmp_icefd, &rfds);
5755 FD_SET(xsmp_icefd, &efds);
5756 if (maxfd < xsmp_icefd)
5757 maxfd = xsmp_icefd;
5758 }
5759# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005760# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005761 maxfd = channel_select_setup(maxfd, &rfds, &wfds);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005762# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005764 ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005765 result = ret > 0 && FD_ISSET(fd, &rfds);
5766 if (result)
5767 --ret;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005768 if (break_loop != NULL && ret > 0)
5769 *break_loop = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005770
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005771# ifdef EINTR
5772 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005773 {
5774 /* Check whether window has been resized, EINTR may be caused by
5775 * SIGWINCH. */
5776 if (do_resize)
5777 handle_resize();
5778
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005779 /* Interrupted by a signal, need to try again. We ignore msec
5780 * here, because we do want to check even after a timeout if
5781 * characters are available. Needed for reading output of an
5782 * external command after the process has finished. */
5783 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005784 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005785# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00005786# ifdef __TANDEM
5787 if (ret == -1 && errno == ENOTSUP)
5788 {
5789 FD_ZERO(&rfds);
5790 FD_ZERO(&efds);
5791 ret = 0;
5792 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005793# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005794# ifdef FEAT_MZSCHEME
5795 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005796 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005797 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798# endif
5799
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800# ifdef FEAT_XCLIPBOARD
5801 if (ret > 0 && xterm_Shell != (Widget)0
5802 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
5803 {
5804 xterm_update(); /* Maybe we should hand out clipboard */
5805 /* continue looping when we only got the X event and the input
5806 * buffer is empty */
5807 if (--ret == 0 && !input_available())
5808 {
5809 /* Try again */
5810 finished = FALSE;
5811 }
5812 }
5813# endif
5814# ifdef FEAT_MOUSE_GPM
5815 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
5816 {
5817 if (FD_ISSET(gpm_fd, &efds))
5818 gpm_close();
5819 else if (FD_ISSET(gpm_fd, &rfds))
5820 *check_for_gpm = 1;
5821 }
5822# endif
5823# ifdef USE_XSMP
5824 if (ret > 0 && xsmp_icefd != -1)
5825 {
5826 if (FD_ISSET(xsmp_icefd, &efds))
5827 {
5828 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005829 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 xsmp_close();
5831 if (--ret == 0)
5832 finished = FALSE; /* keep going if event was only one */
5833 }
5834 else if (FD_ISSET(xsmp_icefd, &rfds))
5835 {
5836 busy = TRUE;
5837 xsmp_handle_requests();
5838 busy = FALSE;
5839 if (--ret == 0)
5840 finished = FALSE; /* keep going if event was only one */
5841 }
5842 }
5843# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005844#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01005845 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005846 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
5849#endif /* HAVE_SELECT */
5850
5851#ifdef MAY_LOOP
5852 if (finished || msec == 0)
5853 break;
5854
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005855# ifdef FEAT_CLIENTSERVER
5856 if (server_waiting())
5857 break;
5858# endif
5859
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 /* We're going to loop around again, find out for how long */
5861 if (msec > 0)
5862 {
5863# ifdef USE_START_TV
5864 struct timeval mtv;
5865
5866 /* Compute remaining wait time. */
5867 gettimeofday(&mtv, NULL);
5868 msec -= (mtv.tv_sec - start_tv.tv_sec) * 1000L
5869 + (mtv.tv_usec - start_tv.tv_usec) / 1000L;
5870# else
5871 /* Guess we got interrupted halfway. */
5872 msec = msec / 2;
5873# endif
5874 if (msec <= 0)
5875 break; /* waited long enough */
5876 }
5877#endif
5878 }
5879
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005880 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881}
5882
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884/*
Bram Moolenaar02743632005-07-25 20:42:36 +00005885 * Expand a path into all matching files and/or directories. Handles "*",
5886 * "?", "[a-z]", "**", etc.
5887 * "path" has backslashes before chars that are not to be expanded.
5888 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 */
5890 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005891mch_expandpath(
5892 garray_T *gap,
5893 char_u *path,
5894 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895{
Bram Moolenaar02743632005-07-25 20:42:36 +00005896 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897}
5898#endif
5899
5900/*
5901 * mch_expand_wildcards() - this code does wild-card pattern matching using
5902 * the shell
5903 *
5904 * return OK for success, FAIL for error (you may lose some memory) and put
5905 * an error message in *file.
5906 *
5907 * num_pat is number of input patterns
5908 * pat is array of pointers to input patterns
5909 * num_file is pointer to number of matched file names
5910 * file is pointer to array of pointers to matched file names
5911 */
5912
5913#ifndef SEEK_SET
5914# define SEEK_SET 0
5915#endif
5916#ifndef SEEK_END
5917# define SEEK_END 2
5918#endif
5919
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005920#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00005921
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005923mch_expand_wildcards(
5924 int num_pat,
5925 char_u **pat,
5926 int *num_file,
5927 char_u ***file,
5928 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929{
5930 int i;
5931 size_t len;
5932 char_u *p;
5933 int dir;
5934#ifdef __EMX__
Bram Moolenaarc7247912008-01-13 12:54:11 +00005935 /*
5936 * This is the OS/2 implementation.
5937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938# define EXPL_ALLOC_INC 16
5939 char_u **expl_files;
5940 size_t files_alloced, files_free;
5941 char_u *buf;
5942 int has_wildcard;
5943
5944 *num_file = 0; /* default: no files found */
5945 files_alloced = EXPL_ALLOC_INC; /* how much space is allocated */
5946 files_free = EXPL_ALLOC_INC; /* how much space is not used */
5947 *file = (char_u **)alloc(sizeof(char_u **) * files_alloced);
5948 if (*file == NULL)
5949 return FAIL;
5950
5951 for (; num_pat > 0; num_pat--, pat++)
5952 {
5953 expl_files = NULL;
5954 if (vim_strchr(*pat, '$') || vim_strchr(*pat, '~'))
5955 /* expand environment var or home dir */
5956 buf = expand_env_save(*pat);
5957 else
5958 buf = vim_strsave(*pat);
5959 expl_files = NULL;
Bram Moolenaard8b02732005-01-14 21:48:43 +00005960 has_wildcard = mch_has_exp_wildcard(buf); /* (still) wildcards? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 if (has_wildcard) /* yes, so expand them */
5962 expl_files = (char_u **)_fnexplode(buf);
5963
5964 /*
5965 * return value of buf if no wildcards left,
5966 * OR if no match AND EW_NOTFOUND is set.
5967 */
5968 if ((!has_wildcard && ((flags & EW_NOTFOUND) || mch_getperm(buf) >= 0))
5969 || (expl_files == NULL && (flags & EW_NOTFOUND)))
5970 { /* simply save the current contents of *buf */
5971 expl_files = (char_u **)alloc(sizeof(char_u **) * 2);
5972 if (expl_files != NULL)
5973 {
5974 expl_files[0] = vim_strsave(buf);
5975 expl_files[1] = NULL;
5976 }
5977 }
5978 vim_free(buf);
5979
5980 /*
5981 * Count number of names resulting from expansion,
5982 * At the same time add a backslash to the end of names that happen to
5983 * be directories, and replace slashes with backslashes.
5984 */
5985 if (expl_files)
5986 {
5987 for (i = 0; (p = expl_files[i]) != NULL; i++)
5988 {
5989 dir = mch_isdir(p);
5990 /* If we don't want dirs and this is one, skip it */
5991 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
5992 continue;
5993
Bram Moolenaara2031822006-03-07 22:29:51 +00005994 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01005995 if (!dir && (flags & EW_EXEC)
5996 && !mch_can_exe(p, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00005997 continue;
5998
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 if (--files_free == 0)
6000 {
6001 /* need more room in table of pointers */
6002 files_alloced += EXPL_ALLOC_INC;
6003 *file = (char_u **)vim_realloc(*file,
6004 sizeof(char_u **) * files_alloced);
6005 if (*file == NULL)
6006 {
6007 EMSG(_(e_outofmem));
6008 *num_file = 0;
6009 return FAIL;
6010 }
6011 files_free = EXPL_ALLOC_INC;
6012 }
6013 slash_adjust(p);
6014 if (dir)
6015 {
6016 /* For a directory we add a '/', unless it's already
6017 * there. */
6018 len = STRLEN(p);
6019 if (((*file)[*num_file] = alloc(len + 2)) != NULL)
6020 {
6021 STRCPY((*file)[*num_file], p);
Bram Moolenaar654b5b52006-06-22 17:47:10 +00006022 if (!after_pathsep((*file)[*num_file],
6023 (*file)[*num_file] + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 {
6025 (*file)[*num_file][len] = psepc;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006026 (*file)[*num_file][len + 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 }
6028 }
6029 }
6030 else
6031 {
6032 (*file)[*num_file] = vim_strsave(p);
6033 }
6034
6035 /*
6036 * Error message already given by either alloc or vim_strsave.
6037 * Should return FAIL, but returning OK works also.
6038 */
6039 if ((*file)[*num_file] == NULL)
6040 break;
6041 (*num_file)++;
6042 }
6043 _fnexplodefree((char **)expl_files);
6044 }
6045 }
6046 return OK;
6047
6048#else /* __EMX__ */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006049 /*
6050 * This is the non-OS/2 implementation (really Unix).
6051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 int j;
6053 char_u *tempname;
6054 char_u *command;
6055 FILE *fd;
6056 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006057#define STYLE_ECHO 0 /* use "echo", the default */
6058#define STYLE_GLOB 1 /* use "glob", for csh */
6059#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6060#define STYLE_PRINT 3 /* use "print -N", for zsh */
6061#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6062 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 int shell_style = STYLE_ECHO;
6064 int check_spaces;
6065 static int did_find_nul = FALSE;
6066 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006067 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006068 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069
6070 *num_file = 0; /* default: no files found */
6071 *file = NULL;
6072
6073 /*
6074 * If there are no wildcards, just copy the names to allocated memory.
6075 * Saves a lot of time, because we don't have to start a new shell.
6076 */
6077 if (!have_wildcard(num_pat, pat))
6078 return save_patterns(num_pat, pat, num_file, file);
6079
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006080# ifdef HAVE_SANDBOX
6081 /* Don't allow any shell command in the sandbox. */
6082 if (sandbox != 0 && check_secure())
6083 return FAIL;
6084# endif
6085
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 /*
6087 * Don't allow the use of backticks in secure and restricted mode.
6088 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006089 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 for (i = 0; i < num_pat; ++i)
6091 if (vim_strchr(pat[i], '`') != NULL
6092 && (check_restricted() || check_secure()))
6093 return FAIL;
6094
6095 /*
6096 * get a name for the temp file
6097 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006098 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 {
6100 EMSG(_(e_notmp));
6101 return FAIL;
6102 }
6103
6104 /*
6105 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006106 * file.
6107 * STYLE_BT: NL separated
6108 * If expanding `cmd` execute it directly.
6109 * STYLE_GLOB: NUL separated
6110 * If we use *csh, "glob" will work better than "echo".
6111 * STYLE_PRINT: NL or NUL separated
6112 * If we use *zsh, "print -N" will work better than "glob".
6113 * STYLE_VIMGLOB: NL separated
6114 * If we use *sh*, we define "vimglob()".
6115 * STYLE_ECHO: space separated.
6116 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 */
6118 if (num_pat == 1 && *pat[0] == '`'
6119 && (len = STRLEN(pat[0])) > 2
6120 && *(pat[0] + len - 1) == '`')
6121 shell_style = STYLE_BT;
6122 else if ((len = STRLEN(p_sh)) >= 3)
6123 {
6124 if (STRCMP(p_sh + len - 3, "csh") == 0)
6125 shell_style = STYLE_GLOB;
6126 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6127 shell_style = STYLE_PRINT;
6128 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006129 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6130 "sh") != NULL)
6131 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
Bram Moolenaarc7247912008-01-13 12:54:11 +00006133 /* Compute the length of the command. We need 2 extra bytes: for the
6134 * optional '&' and for the NUL.
6135 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006137 if (shell_style == STYLE_VIMGLOB)
6138 len += STRLEN(sh_vimglob_func);
6139
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006140 for (i = 0; i < num_pat; ++i)
6141 {
6142 /* Count the length of the patterns in the same way as they are put in
6143 * "command" below. */
6144#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006146#else
6147 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006148 for (j = 0; pat[i][j] != NUL; ++j)
6149 {
6150 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6151 ++len; /* may add a backslash */
6152 ++len;
6153 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006154#endif
6155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 command = alloc(len);
6157 if (command == NULL)
6158 {
6159 /* out of memory */
6160 vim_free(tempname);
6161 return FAIL;
6162 }
6163
6164 /*
6165 * Build the shell command:
6166 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6167 * recognizes this).
6168 * - Add the shell command to print the expanded names.
6169 * - Add the temp file name.
6170 * - Add the file name patterns.
6171 */
6172 if (shell_style == STYLE_BT)
6173 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006174 /* change `command; command& ` to (command; command ) */
6175 STRCPY(command, "(");
6176 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006178 *p-- = ')'; /* remove last backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 while (p > command && vim_iswhite(*p))
6180 --p;
6181 if (*p == '&') /* remove trailing '&' */
6182 {
6183 ampersent = TRUE;
6184 *p = ' ';
6185 }
6186 STRCAT(command, ">");
6187 }
6188 else
6189 {
6190 if (flags & EW_NOTFOUND)
6191 STRCPY(command, "set nonomatch; ");
6192 else
6193 STRCPY(command, "unset nonomatch; ");
6194 if (shell_style == STYLE_GLOB)
6195 STRCAT(command, "glob >");
6196 else if (shell_style == STYLE_PRINT)
6197 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006198 else if (shell_style == STYLE_VIMGLOB)
6199 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 else
6201 STRCAT(command, "echo >");
6202 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006203
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006205
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 if (shell_style != STYLE_BT)
6207 for (i = 0; i < num_pat; ++i)
6208 {
6209 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006210 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006211 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212#ifdef USE_SYSTEM
6213 STRCAT(command, " \"");
6214 STRCAT(command, pat[i]);
6215 STRCAT(command, "\"");
6216#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006217 int intick = FALSE;
6218
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 p = command + STRLEN(command);
6220 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006221 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006222 {
6223 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006224 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006225 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6226 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006227 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006228 * backslash inside backticks, before a special character
6229 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006230 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006231 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6232 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006233 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006234 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006235 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006236 else if (!intick
6237 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6238 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006239 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006240 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6241 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006242 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006243
6244 /* Copy one character. */
6245 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 *p = NUL;
6248#endif
6249 }
6250 if (flags & EW_SILENT)
6251 show_shell_mess = FALSE;
6252 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006253 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254
6255 /*
6256 * Using zsh -G: If a pattern has no matches, it is just deleted from
6257 * the argument list, otherwise zsh gives an error message and doesn't
6258 * expand any other pattern.
6259 */
6260 if (shell_style == STYLE_PRINT)
6261 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6262
6263 /*
6264 * If we use -f then shell variables set in .cshrc won't get expanded.
6265 * vi can do it, so we will too, but it is only necessary if there is a "$"
6266 * in one of the patterns, otherwise we can still use the fast option.
6267 */
6268 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6269 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6270
6271 /*
6272 * execute the shell command
6273 */
6274 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6275
6276 /* When running in the background, give it some time to create the temp
6277 * file, but don't wait for it to finish. */
6278 if (ampersent)
6279 mch_delay(10L, TRUE);
6280
6281 extra_shell_arg = NULL; /* cleanup */
6282 show_shell_mess = TRUE;
6283 vim_free(command);
6284
Bram Moolenaarc7247912008-01-13 12:54:11 +00006285 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 {
6287 mch_remove(tempname);
6288 vim_free(tempname);
6289 /*
6290 * With interactive completion, the error message is not printed.
6291 * However with USE_SYSTEM, I don't know how to turn off error messages
6292 * from the shell, so screen may still get messed up -- webb.
6293 */
6294#ifndef USE_SYSTEM
6295 if (!(flags & EW_SILENT))
6296#endif
6297 {
6298 redraw_later_clear(); /* probably messed up screen */
6299 msg_putchar('\n'); /* clear bottom line quickly */
6300 cmdline_row = Rows - 1; /* continue on last line */
6301#ifdef USE_SYSTEM
6302 if (!(flags & EW_SILENT))
6303#endif
6304 {
6305 MSG(_(e_wildexpand));
6306 msg_start(); /* don't overwrite this message */
6307 }
6308 }
6309 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6310 * EW_NOTFOUND is given */
6311 if (shell_style == STYLE_BT)
6312 return FAIL;
6313 goto notfound;
6314 }
6315
6316 /*
6317 * read the names from the file into memory
6318 */
6319 fd = fopen((char *)tempname, READBIN);
6320 if (fd == NULL)
6321 {
6322 /* Something went wrong, perhaps a file name with a special char. */
6323 if (!(flags & EW_SILENT))
6324 {
6325 MSG(_(e_wildexpand));
6326 msg_start(); /* don't overwrite this message */
6327 }
6328 vim_free(tempname);
6329 goto notfound;
6330 }
6331 fseek(fd, 0L, SEEK_END);
6332 len = ftell(fd); /* get size of temp file */
6333 fseek(fd, 0L, SEEK_SET);
6334 buffer = alloc(len + 1);
6335 if (buffer == NULL)
6336 {
6337 /* out of memory */
6338 mch_remove(tempname);
6339 vim_free(tempname);
6340 fclose(fd);
6341 return FAIL;
6342 }
6343 i = fread((char *)buffer, 1, len, fd);
6344 fclose(fd);
6345 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006346 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 {
6348 /* unexpected read error */
6349 EMSG2(_(e_notread), tempname);
6350 vim_free(tempname);
6351 vim_free(buffer);
6352 return FAIL;
6353 }
6354 vim_free(tempname);
6355
Bram Moolenaarc7247912008-01-13 12:54:11 +00006356# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6358 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006359 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6361 *p++ = buffer[i];
6362 len = p - buffer;
6363# endif
6364
6365
6366 /* file names are separated with Space */
6367 if (shell_style == STYLE_ECHO)
6368 {
6369 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6370 p = buffer;
6371 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6372 {
6373 while (*p != ' ' && *p != '\n')
6374 ++p;
6375 p = skipwhite(p); /* skip to next entry */
6376 }
6377 }
6378 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006379 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 {
6381 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6382 p = buffer;
6383 for (i = 0; *p != NUL; ++i) /* count number of entries */
6384 {
6385 while (*p != '\n' && *p != NUL)
6386 ++p;
6387 if (*p != NUL)
6388 ++p;
6389 p = skipwhite(p); /* skip leading white space */
6390 }
6391 }
6392 /* file names are separated with NUL */
6393 else
6394 {
6395 /*
6396 * Some versions of zsh use spaces instead of NULs to separate
6397 * results. Only do this when there is no NUL before the end of the
6398 * buffer, otherwise we would never be able to use file names with
6399 * embedded spaces when zsh does use NULs.
6400 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6401 * don't check for spaces again.
6402 */
6403 check_spaces = FALSE;
6404 if (shell_style == STYLE_PRINT && !did_find_nul)
6405 {
6406 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006407 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006408 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 did_find_nul = TRUE;
6410 else
6411 check_spaces = TRUE;
6412 }
6413
6414 /*
6415 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6416 * already is one, for STYLE_GLOB it needs to be added.
6417 */
6418 if (len && buffer[len - 1] == NUL)
6419 --len;
6420 else
6421 buffer[len] = NUL;
6422 i = 0;
6423 for (p = buffer; p < buffer + len; ++p)
6424 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6425 {
6426 ++i;
6427 *p = NUL;
6428 }
6429 if (len)
6430 ++i; /* count last entry */
6431 }
6432 if (i == 0)
6433 {
6434 /*
6435 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6436 * /bin/sh will happily expand it to nothing rather than returning an
6437 * error; and hey, it's good to check anyway -- webb.
6438 */
6439 vim_free(buffer);
6440 goto notfound;
6441 }
6442 *num_file = i;
6443 *file = (char_u **)alloc(sizeof(char_u *) * i);
6444 if (*file == NULL)
6445 {
6446 /* out of memory */
6447 vim_free(buffer);
6448 return FAIL;
6449 }
6450
6451 /*
6452 * Isolate the individual file names.
6453 */
6454 p = buffer;
6455 for (i = 0; i < *num_file; ++i)
6456 {
6457 (*file)[i] = p;
6458 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006459 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6460 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006462 while (!(shell_style == STYLE_ECHO && *p == ' ')
6463 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 ++p;
6465 if (p == buffer + len) /* last entry */
6466 *p = NUL;
6467 else
6468 {
6469 *p++ = NUL;
6470 p = skipwhite(p); /* skip to next entry */
6471 }
6472 }
6473 else /* NUL separates */
6474 {
6475 while (*p && p < buffer + len) /* skip entry */
6476 ++p;
6477 ++p; /* skip NUL */
6478 }
6479 }
6480
6481 /*
6482 * Move the file names to allocated memory.
6483 */
6484 for (j = 0, i = 0; i < *num_file; ++i)
6485 {
6486 /* Require the files to exist. Helps when using /bin/sh */
6487 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6488 continue;
6489
6490 /* check if this entry should be included */
6491 dir = (mch_isdir((*file)[i]));
6492 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6493 continue;
6494
Bram Moolenaara2031822006-03-07 22:29:51 +00006495 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006496 if (!dir && (flags & EW_EXEC)
6497 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006498 continue;
6499
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6501 if (p)
6502 {
6503 STRCPY(p, (*file)[i]);
6504 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006505 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 (*file)[j++] = p;
6507 }
6508 }
6509 vim_free(buffer);
6510 *num_file = j;
6511
6512 if (*num_file == 0) /* rejected all entries */
6513 {
6514 vim_free(*file);
6515 *file = NULL;
6516 goto notfound;
6517 }
6518
6519 return OK;
6520
6521notfound:
6522 if (flags & EW_NOTFOUND)
6523 return save_patterns(num_pat, pat, num_file, file);
6524 return FAIL;
6525
6526#endif /* __EMX__ */
6527}
6528
6529#endif /* VMS */
6530
6531#ifndef __EMX__
6532 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006533save_patterns(
6534 int num_pat,
6535 char_u **pat,
6536 int *num_file,
6537 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538{
6539 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006540 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541
6542 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6543 if (*file == NULL)
6544 return FAIL;
6545 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006546 {
6547 s = vim_strsave(pat[i]);
6548 if (s != NULL)
6549 /* Be compatible with expand_filename(): halve the number of
6550 * backslashes. */
6551 backslash_halve(s);
6552 (*file)[i] = s;
6553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 *num_file = num_pat;
6555 return OK;
6556}
6557#endif
6558
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559/*
6560 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6561 * expand.
6562 */
6563 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006564mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006566 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 if (*p == '\\' && p[1] != NUL)
6569 ++p;
6570 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 if (vim_strchr((char_u *)
6572#ifdef VMS
6573 "*?%"
6574#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576#endif
6577 , *p) != NULL)
6578 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 }
6580 return FALSE;
6581}
6582
6583/*
6584 * Return TRUE if the string "p" contains a wildcard.
6585 * Don't recognize '~' at the end as a wildcard.
6586 */
6587 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006588mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006590 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 if (*p == '\\' && p[1] != NUL)
6593 ++p;
6594 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 if (vim_strchr((char_u *)
6596#ifdef VMS
6597 "*?%$"
6598#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600#endif
6601 , *p) != NULL
6602 || (*p == '~' && p[1] != NUL))
6603 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 }
6605 return FALSE;
6606}
6607
6608#ifndef __EMX__
6609 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006610have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611{
6612 int i;
6613
6614 for (i = 0; i < num; i++)
6615 if (mch_has_wildcard(file[i]))
6616 return 1;
6617 return 0;
6618}
6619
6620 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006621have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622{
6623 int i;
6624
6625 for (i = 0; i < num; i++)
6626 if (vim_strchr(file[i], '$') != NULL)
6627 return TRUE;
6628 return FALSE;
6629}
6630#endif /* ifndef __EMX__ */
6631
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006632#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633/*
6634 * Scaled-down version of rename(), which is missing in Xenix.
6635 * This version can only move regular files and will fail if the
6636 * destination exists.
6637 */
6638 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006639mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640{
6641 struct stat st;
6642
6643 if (stat(dest, &st) >= 0) /* fail if destination exists */
6644 return -1;
6645 if (link(src, dest) != 0) /* link file to new name */
6646 return -1;
6647 if (mch_remove(src) == 0) /* delete link to old name */
6648 return 0;
6649 return -1;
6650}
6651#endif /* !HAVE_RENAME */
6652
6653#ifdef FEAT_MOUSE_GPM
6654/*
6655 * Initializes connection with gpm (if it isn't already opened)
6656 * Return 1 if succeeded (or connection already opened), 0 if failed
6657 */
6658 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006659gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660{
6661 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
6662
6663 if (!gpm_flag)
6664 {
6665 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
6666 gpm_connect.defaultMask = ~GPM_HARD;
6667 /* Default handling for mouse move*/
6668 gpm_connect.minMod = 0; /* Handle any modifier keys */
6669 gpm_connect.maxMod = 0xffff;
6670 if (Gpm_Open(&gpm_connect, 0) > 0)
6671 {
6672 /* gpm library tries to handling TSTP causes
6673 * problems. Anyways, we close connection to Gpm whenever
6674 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006675 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006677# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006679# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 return 1; /* succeed */
6681 }
6682 if (gpm_fd == -2)
6683 Gpm_Close(); /* We don't want to talk to xterm via gpm */
6684 return 0;
6685 }
6686 return 1; /* already open */
6687}
6688
6689/*
6690 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 */
6692 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006693gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
6695 if (gpm_flag && gpm_fd >= 0) /* if Open */
6696 Gpm_Close();
6697}
6698
6699/* Reads gpm event and adds special keys to input buf. Returns length of
6700 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02006701 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 */
6703 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006704mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
6706 int button;
6707 static Gpm_Event gpm_event;
6708 char_u string[6];
6709 int_u vim_modifiers;
6710 int row,col;
6711 unsigned char buttons_mask;
6712 unsigned char gpm_modifiers;
6713 static unsigned char old_buttons = 0;
6714
6715 Gpm_GetEvent(&gpm_event);
6716
6717#ifdef FEAT_GUI
6718 /* Don't put events in the input queue now. */
6719 if (hold_gui_events)
6720 return 0;
6721#endif
6722
6723 row = gpm_event.y - 1;
6724 col = gpm_event.x - 1;
6725
6726 string[0] = ESC; /* Our termcode */
6727 string[1] = 'M';
6728 string[2] = 'G';
6729 switch (GPM_BARE_EVENTS(gpm_event.type))
6730 {
6731 case GPM_DRAG:
6732 string[3] = MOUSE_DRAG;
6733 break;
6734 case GPM_DOWN:
6735 buttons_mask = gpm_event.buttons & ~old_buttons;
6736 old_buttons = gpm_event.buttons;
6737 switch (buttons_mask)
6738 {
6739 case GPM_B_LEFT:
6740 button = MOUSE_LEFT;
6741 break;
6742 case GPM_B_MIDDLE:
6743 button = MOUSE_MIDDLE;
6744 break;
6745 case GPM_B_RIGHT:
6746 button = MOUSE_RIGHT;
6747 break;
6748 default:
6749 return 0;
6750 /*Don't know what to do. Can more than one button be
6751 * reported in one event? */
6752 }
6753 string[3] = (char_u)(button | 0x20);
6754 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
6755 break;
6756 case GPM_UP:
6757 string[3] = MOUSE_RELEASE;
6758 old_buttons &= ~gpm_event.buttons;
6759 break;
6760 default:
6761 return 0;
6762 }
6763 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
6764 gpm_modifiers = gpm_event.modifiers;
6765 vim_modifiers = 0x0;
6766 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
6767 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
6768 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
6769 */
6770 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
6771 vim_modifiers |= MOUSE_SHIFT;
6772
6773 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
6774 vim_modifiers |= MOUSE_CTRL;
6775 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
6776 vim_modifiers |= MOUSE_ALT;
6777 string[3] |= vim_modifiers;
6778 string[4] = (char_u)(col + ' ' + 1);
6779 string[5] = (char_u)(row + ' ' + 1);
6780 add_to_input_buf(string, 6);
6781 return 6;
6782}
6783#endif /* FEAT_MOUSE_GPM */
6784
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006785#ifdef FEAT_SYSMOUSE
6786/*
6787 * Initialize connection with sysmouse.
6788 * Let virtual console inform us with SIGUSR2 for pending sysmouse
6789 * output, any sysmouse output than will be processed via sig_sysmouse().
6790 * Return OK if succeeded, FAIL if failed.
6791 */
6792 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006793sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006794{
6795 struct mouse_info mouse;
6796
6797 mouse.operation = MOUSE_MODE;
6798 mouse.u.mode.mode = 0;
6799 mouse.u.mode.signal = SIGUSR2;
6800 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
6801 {
6802 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
6803 mouse.operation = MOUSE_SHOW;
6804 ioctl(1, CONS_MOUSECTL, &mouse);
6805 return OK;
6806 }
6807 return FAIL;
6808}
6809
6810/*
6811 * Stop processing SIGUSR2 signals, and also make sure that
6812 * virtual console do not send us any sysmouse related signal.
6813 */
6814 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006815sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006816{
6817 struct mouse_info mouse;
6818
6819 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
6820 mouse.operation = MOUSE_MODE;
6821 mouse.u.mode.mode = 0;
6822 mouse.u.mode.signal = 0;
6823 ioctl(1, CONS_MOUSECTL, &mouse);
6824}
6825
6826/*
6827 * Gets info from sysmouse and adds special keys to input buf.
6828 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006829 static RETSIGTYPE
6830sig_sysmouse SIGDEFARG(sigarg)
6831{
6832 struct mouse_info mouse;
6833 struct video_info video;
6834 char_u string[6];
6835 int row, col;
6836 int button;
6837 int buttons;
6838 static int oldbuttons = 0;
6839
6840#ifdef FEAT_GUI
6841 /* Don't put events in the input queue now. */
6842 if (hold_gui_events)
6843 return;
6844#endif
6845
6846 mouse.operation = MOUSE_GETINFO;
6847 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
6848 && ioctl(1, FBIO_MODEINFO, &video) != -1
6849 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
6850 && video.vi_cheight > 0 && video.vi_cwidth > 0)
6851 {
6852 row = mouse.u.data.y / video.vi_cheight;
6853 col = mouse.u.data.x / video.vi_cwidth;
6854 buttons = mouse.u.data.buttons;
6855 string[0] = ESC; /* Our termcode */
6856 string[1] = 'M';
6857 string[2] = 'S';
6858 if (oldbuttons == buttons && buttons != 0)
6859 {
6860 button = MOUSE_DRAG;
6861 }
6862 else
6863 {
6864 switch (buttons)
6865 {
6866 case 0:
6867 button = MOUSE_RELEASE;
6868 break;
6869 case 1:
6870 button = MOUSE_LEFT;
6871 break;
6872 case 2:
6873 button = MOUSE_MIDDLE;
6874 break;
6875 case 4:
6876 button = MOUSE_RIGHT;
6877 break;
6878 default:
6879 return;
6880 }
6881 oldbuttons = buttons;
6882 }
6883 string[3] = (char_u)(button);
6884 string[4] = (char_u)(col + ' ' + 1);
6885 string[5] = (char_u)(row + ' ' + 1);
6886 add_to_input_buf(string, 6);
6887 }
6888 return;
6889}
6890#endif /* FEAT_SYSMOUSE */
6891
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01006893typedef char_u * (*STRPROCSTR)(char_u *);
6894typedef char_u * (*INTPROCSTR)(int);
6895typedef int (*STRPROCINT)(char_u *);
6896typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
6898/*
6899 * Call a DLL routine which takes either a string or int param
6900 * and returns an allocated string.
6901 */
6902 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006903mch_libcall(
6904 char_u *libname,
6905 char_u *funcname,
6906 char_u *argstring, /* NULL when using a argint */
6907 int argint,
6908 char_u **string_result,/* NULL when using number_result */
6909 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910{
6911# if defined(USE_DLOPEN)
6912 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006913 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914# else
6915 shl_t hinstLib;
6916# endif
6917 STRPROCSTR ProcAdd;
6918 INTPROCSTR ProcAddI;
6919 char_u *retval_str = NULL;
6920 int retval_int = 0;
6921 int success = FALSE;
6922
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006923 /*
6924 * Get a handle to the DLL module.
6925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006927 /* First clear any error, it's not cleared by the dlopen() call. */
6928 (void)dlerror();
6929
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 hinstLib = dlopen((char *)libname, RTLD_LAZY
6931# ifdef RTLD_LOCAL
6932 | RTLD_LOCAL
6933# endif
6934 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006935 if (hinstLib == NULL)
6936 {
6937 /* "dlerr" must be used before dlclose() */
6938 dlerr = (char *)dlerror();
6939 if (dlerr != NULL)
6940 EMSG2(_("dlerror = \"%s\""), dlerr);
6941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942# else
6943 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
6944# endif
6945
6946 /* If the handle is valid, try to get the function address. */
6947 if (hinstLib != NULL)
6948 {
6949# ifdef HAVE_SETJMP_H
6950 /*
6951 * Catch a crash when calling the library function. For example when
6952 * using a number where a string pointer is expected.
6953 */
6954 mch_startjmp();
6955 if (SETJMP(lc_jump_env) != 0)
6956 {
6957 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00006958# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006959 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00006960# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 mch_didjmp();
6962 }
6963 else
6964# endif
6965 {
6966 retval_str = NULL;
6967 retval_int = 0;
6968
6969 if (argstring != NULL)
6970 {
6971# if defined(USE_DLOPEN)
6972 ProcAdd = (STRPROCSTR)dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006973 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974# else
6975 if (shl_findsym(&hinstLib, (const char *)funcname,
6976 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
6977 ProcAdd = NULL;
6978# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006979 if ((success = (ProcAdd != NULL
6980# if defined(USE_DLOPEN)
6981 && dlerr == NULL
6982# endif
6983 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 {
6985 if (string_result == NULL)
6986 retval_int = ((STRPROCINT)ProcAdd)(argstring);
6987 else
6988 retval_str = (ProcAdd)(argstring);
6989 }
6990 }
6991 else
6992 {
6993# if defined(USE_DLOPEN)
6994 ProcAddI = (INTPROCSTR)dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006995 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996# else
6997 if (shl_findsym(&hinstLib, (const char *)funcname,
6998 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
6999 ProcAddI = NULL;
7000# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007001 if ((success = (ProcAddI != NULL
7002# if defined(USE_DLOPEN)
7003 && dlerr == NULL
7004# endif
7005 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 {
7007 if (string_result == NULL)
7008 retval_int = ((INTPROCINT)ProcAddI)(argint);
7009 else
7010 retval_str = (ProcAddI)(argint);
7011 }
7012 }
7013
7014 /* Save the string before we free the library. */
7015 /* Assume that a "1" or "-1" result is an illegal pointer. */
7016 if (string_result == NULL)
7017 *number_result = retval_int;
7018 else if (retval_str != NULL
7019 && retval_str != (char_u *)1
7020 && retval_str != (char_u *)-1)
7021 *string_result = vim_strsave(retval_str);
7022 }
7023
7024# ifdef HAVE_SETJMP_H
7025 mch_endjmp();
7026# ifdef SIGHASARG
7027 if (lc_signal != 0)
7028 {
7029 int i;
7030
7031 /* try to find the name of this signal */
7032 for (i = 0; signal_info[i].sig != -1; i++)
7033 if (lc_signal == signal_info[i].sig)
7034 break;
7035 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7036 }
7037# endif
7038# endif
7039
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007041 /* "dlerr" must be used before dlclose() */
7042 if (dlerr != NULL)
7043 EMSG2(_("dlerror = \"%s\""), dlerr);
7044
7045 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 (void)dlclose(hinstLib);
7047# else
7048 (void)shl_unload(hinstLib);
7049# endif
7050 }
7051
7052 if (!success)
7053 {
7054 EMSG2(_(e_libcall), funcname);
7055 return FAIL;
7056 }
7057
7058 return OK;
7059}
7060#endif
7061
7062#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7063static int xterm_trace = -1; /* default: disabled */
7064static int xterm_button;
7065
7066/*
7067 * Setup a dummy window for X selections in a terminal.
7068 */
7069 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007070setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071{
7072 int z = 0;
7073 char *strp = "";
7074 Widget AppShell;
7075
7076 if (!x_connect_to_server())
7077 return;
7078
7079 open_app_context();
7080 if (app_context != NULL && xterm_Shell == (Widget)0)
7081 {
7082 int (*oldhandler)();
7083#if defined(HAVE_SETJMP_H)
7084 int (*oldIOhandler)();
7085#endif
7086# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
7087 struct timeval start_tv;
7088
7089 if (p_verbose > 0)
7090 gettimeofday(&start_tv, NULL);
7091# endif
7092
7093 /* Ignore X errors while opening the display */
7094 oldhandler = XSetErrorHandler(x_error_check);
7095
7096#if defined(HAVE_SETJMP_H)
7097 /* Ignore X IO errors while opening the display */
7098 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7099 mch_startjmp();
7100 if (SETJMP(lc_jump_env) != 0)
7101 {
7102 mch_didjmp();
7103 xterm_dpy = NULL;
7104 }
7105 else
7106#endif
7107 {
7108 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7109 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
7110#if defined(HAVE_SETJMP_H)
7111 mch_endjmp();
7112#endif
7113 }
7114
7115#if defined(HAVE_SETJMP_H)
7116 /* Now handle X IO errors normally. */
7117 (void)XSetIOErrorHandler(oldIOhandler);
7118#endif
7119 /* Now handle X errors normally. */
7120 (void)XSetErrorHandler(oldhandler);
7121
7122 if (xterm_dpy == NULL)
7123 {
7124 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007125 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 return;
7127 }
7128
7129 /* Catch terminating error of the X server connection. */
7130 (void)XSetIOErrorHandler(x_IOerror_handler);
7131
7132# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
7133 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007134 {
7135 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 xopen_message(&start_tv);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007137 verbose_leave();
7138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139# endif
7140
7141 /* Create a Shell to make converters work. */
7142 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7143 applicationShellWidgetClass, xterm_dpy,
7144 NULL);
7145 if (AppShell == (Widget)0)
7146 return;
7147 xterm_Shell = XtVaCreatePopupShell("VIM",
7148 topLevelShellWidgetClass, AppShell,
7149 XtNmappedWhenManaged, 0,
7150 XtNwidth, 1,
7151 XtNheight, 1,
7152 NULL);
7153 if (xterm_Shell == (Widget)0)
7154 return;
7155
7156 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007157 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 if (x11_display == NULL)
7159 x11_display = xterm_dpy;
7160
7161 XtRealizeWidget(xterm_Shell);
7162 XSync(xterm_dpy, False);
7163 xterm_update();
7164 }
7165 if (xterm_Shell != (Widget)0)
7166 {
7167 clip_init(TRUE);
7168 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7169 x11_window = (Window)atol(strp);
7170 /* Check if $WINDOWID is valid. */
7171 if (test_x11_window(xterm_dpy) == FAIL)
7172 x11_window = 0;
7173 if (x11_window != 0)
7174 xterm_trace = 0;
7175 }
7176}
7177
7178 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007179start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180{
7181 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7182 return;
7183 xterm_trace = 1;
7184 xterm_button = button;
7185 do_xterm_trace();
7186}
7187
7188
7189 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007190stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191{
7192 if (xterm_trace < 0)
7193 return;
7194 xterm_trace = 0;
7195}
7196
7197/*
7198 * Query the xterm pointer and generate mouse termcodes if necessary
7199 * return TRUE if dragging is active, else FALSE
7200 */
7201 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007202do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203{
7204 Window root, child;
7205 int root_x, root_y;
7206 int win_x, win_y;
7207 int row, col;
7208 int_u mask_return;
7209 char_u buf[50];
7210 char_u *strp;
7211 long got_hints;
7212 static char_u *mouse_code;
7213 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7214 static int prev_row = 0, prev_col = 0;
7215 static XSizeHints xterm_hints;
7216
7217 if (xterm_trace <= 0)
7218 return FALSE;
7219
7220 if (xterm_trace == 1)
7221 {
7222 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007223 * have changed recently. */
7224 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7225 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 || xterm_hints.width_inc <= 1
7227 || xterm_hints.height_inc <= 1)
7228 {
7229 xterm_trace = -1; /* Not enough data -- disable tracing */
7230 return FALSE;
7231 }
7232
7233 /* Rely on the same mouse code for the duration of this */
7234 mouse_code = find_termcode(mouse_name);
7235 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007236 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 xterm_trace = 2;
7238
7239 /* Find the offset of the chars, there might be a scrollbar on the
7240 * left of the window and/or a menu on the top (eterm etc.) */
7241 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7242 &win_x, &win_y, &mask_return);
7243 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7244 - (xterm_hints.height_inc / 2);
7245 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7246 xterm_hints.y = 2;
7247 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7248 - (xterm_hints.width_inc / 2);
7249 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7250 xterm_hints.x = 2;
7251 return TRUE;
7252 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007253 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 {
7255 xterm_trace = 0;
7256 return FALSE;
7257 }
7258
7259 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7260 &win_x, &win_y, &mask_return);
7261
7262 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7263 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7264 if (row == prev_row && col == prev_col)
7265 return TRUE;
7266
7267 STRCPY(buf, mouse_code);
7268 strp = buf + STRLEN(buf);
7269 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7270 *strp++ = (char_u)(col + ' ' + 1);
7271 *strp++ = (char_u)(row + ' ' + 1);
7272 *strp = 0;
7273 add_to_input_buf(buf, STRLEN(buf));
7274
7275 prev_row = row;
7276 prev_col = col;
7277 return TRUE;
7278}
7279
7280# if defined(FEAT_GUI) || defined(PROTO)
7281/*
7282 * Destroy the display, window and app_context. Required for GTK.
7283 */
7284 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007285clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286{
7287 if (xterm_Shell != (Widget)0)
7288 {
7289 XtDestroyWidget(xterm_Shell);
7290 xterm_Shell = (Widget)0;
7291 }
7292 if (xterm_dpy != NULL)
7293 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007294# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 /* Lesstif and Solaris crash here, lose some memory */
7296 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 if (x11_display == xterm_dpy)
7299 x11_display = NULL;
7300 xterm_dpy = NULL;
7301 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007302# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 if (app_context != (XtAppContext)NULL)
7304 {
7305 /* Lesstif and Solaris crash here, lose some memory */
7306 XtDestroyApplicationContext(app_context);
7307 app_context = (XtAppContext)NULL;
7308 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007309# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310}
7311# endif
7312
7313/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007314 * Catch up with GUI or X events.
7315 */
7316 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007317clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007318{
7319# ifdef FEAT_GUI
7320 if (gui.in_use)
7321 gui_mch_update();
7322 else
7323# endif
7324 if (xterm_Shell != (Widget)0)
7325 xterm_update();
7326}
7327
7328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 * Catch up with any queued X events. This may put keyboard input into the
7330 * input buffer, call resize call-backs, trigger timers etc. If there is
7331 * nothing in the X event queue (& no timers pending), then we return
7332 * immediately.
7333 */
7334 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007335xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
7337 XEvent event;
7338
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007339 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007341 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007343 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007344 break;
7345
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007346 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007347 {
7348 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007349 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007350#ifdef FEAT_CLIENTSERVER
7351 {
7352 XPropertyEvent *e = (XPropertyEvent *)&event;
7353
7354 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007356 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007359 XtDispatchEvent(&event);
7360 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007361 else
7362 {
7363 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007364 XtAppProcessEvent(app_context, mask);
7365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 }
7367}
7368
7369 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007370clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371{
7372 if (xterm_Shell != (Widget)0)
7373 return clip_x11_own_selection(xterm_Shell, cbd);
7374 return FAIL;
7375}
7376
7377 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007378clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379{
7380 if (xterm_Shell != (Widget)0)
7381 clip_x11_lose_selection(xterm_Shell, cbd);
7382}
7383
7384 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007385clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386{
7387 if (xterm_Shell != (Widget)0)
7388 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7389}
7390
7391 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007392clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
7394 clip_x11_set_selection(cbd);
7395}
7396#endif
7397
7398
7399#if defined(USE_XSMP) || defined(PROTO)
7400/*
7401 * Code for X Session Management Protocol.
7402 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007403static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7404static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7405static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7406static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7407static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408
7409
7410# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007411static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412
7413/*
7414 * This is our chance to ask the user if they want to save,
7415 * or abort the logout
7416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007418xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419{
7420 cmdmod_T save_cmdmod;
7421 int cancel_shutdown = False;
7422
7423 save_cmdmod = cmdmod;
7424 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007425 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 /* Mustn't logout */
7427 cancel_shutdown = True;
7428 cmdmod = save_cmdmod;
7429 setcursor(); /* position cursor */
7430 out_flush();
7431
7432 /* Done interaction */
7433 SmcInteractDone(smc_conn, cancel_shutdown);
7434
7435 /* Finish off
7436 * Only end save-yourself here if we're not cancelling shutdown;
7437 * we'll get a cancelled callback later in which we'll end it.
7438 * Hopefully get around glitchy SMs (like GNOME-1)
7439 */
7440 if (!cancel_shutdown)
7441 {
7442 xsmp.save_yourself = False;
7443 SmcSaveYourselfDone(smc_conn, True);
7444 }
7445}
7446# endif
7447
7448/*
7449 * Callback that starts save-yourself.
7450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007452xsmp_handle_save_yourself(
7453 SmcConn smc_conn,
7454 SmPointer client_data UNUSED,
7455 int save_type UNUSED,
7456 Bool shutdown,
7457 int interact_style UNUSED,
7458 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459{
7460 /* Handle already being in saveyourself */
7461 if (xsmp.save_yourself)
7462 SmcSaveYourselfDone(smc_conn, True);
7463 xsmp.save_yourself = True;
7464 xsmp.shutdown = shutdown;
7465
7466 /* First up, preserve all files */
7467 out_flush();
7468 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7469
7470 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007471 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472
7473# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7474 /* Now see if we can ask about unsaved files */
7475 if (shutdown && !fast && gui.in_use)
7476 /* Need to interact with user, but need SM's permission */
7477 SmcInteractRequest(smc_conn, SmDialogError,
7478 xsmp_handle_interaction, client_data);
7479 else
7480# endif
7481 {
7482 /* Can stop the cycle here */
7483 SmcSaveYourselfDone(smc_conn, True);
7484 xsmp.save_yourself = False;
7485 }
7486}
7487
7488
7489/*
7490 * Callback to warn us of imminent death.
7491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007493xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494{
7495 xsmp_close();
7496
7497 /* quit quickly leaving swapfiles for modified buffers behind */
7498 getout_preserve_modified(0);
7499}
7500
7501
7502/*
7503 * Callback to tell us that save-yourself has completed.
7504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007506xsmp_save_complete(
7507 SmcConn smc_conn UNUSED,
7508 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
7510 xsmp.save_yourself = False;
7511}
7512
7513
7514/*
7515 * Callback to tell us that an instigated shutdown was cancelled
7516 * (maybe even by us)
7517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007519xsmp_shutdown_cancelled(
7520 SmcConn smc_conn,
7521 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
7523 if (xsmp.save_yourself)
7524 SmcSaveYourselfDone(smc_conn, True);
7525 xsmp.save_yourself = False;
7526 xsmp.shutdown = False;
7527}
7528
7529
7530/*
7531 * Callback to tell us that a new ICE connection has been established.
7532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007534xsmp_ice_connection(
7535 IceConn iceConn,
7536 IcePointer clientData UNUSED,
7537 Bool opening,
7538 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539{
7540 /* Intercept creation of ICE connection fd */
7541 if (opening)
7542 {
7543 xsmp_icefd = IceConnectionNumber(iceConn);
7544 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7545 }
7546}
7547
7548
7549/* Handle any ICE processing that's required; return FAIL if SM lost */
7550 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007551xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552{
7553 Bool rep;
7554
7555 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7556 == IceProcessMessagesIOError)
7557 {
7558 /* Lost ICE */
7559 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007560 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 xsmp_close();
7562 return FAIL;
7563 }
7564 else
7565 return OK;
7566}
7567
7568static int dummy;
7569
7570/* Set up X Session Management Protocol */
7571 void
7572xsmp_init(void)
7573{
7574 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 SmcCallbacks smcallbacks;
7576#if 0
7577 SmPropValue smname;
7578 SmProp smnameprop;
7579 SmProp *smprops[1];
7580#endif
7581
7582 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007583 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584
7585 xsmp.save_yourself = xsmp.shutdown = False;
7586
7587 /* Set up SM callbacks - must have all, even if they're not used */
7588 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7589 smcallbacks.save_yourself.client_data = NULL;
7590 smcallbacks.die.callback = xsmp_die;
7591 smcallbacks.die.client_data = NULL;
7592 smcallbacks.save_complete.callback = xsmp_save_complete;
7593 smcallbacks.save_complete.client_data = NULL;
7594 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7595 smcallbacks.shutdown_cancelled.client_data = NULL;
7596
7597 /* Set up a watch on ICE connection creations. The "dummy" argument is
7598 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7599 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7600 {
7601 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007602 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 return;
7604 }
7605
7606 /* Create an SM connection */
7607 xsmp.smcconn = SmcOpenConnection(
7608 NULL,
7609 NULL,
7610 SmProtoMajor,
7611 SmProtoMinor,
7612 SmcSaveYourselfProcMask | SmcDieProcMask
7613 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
7614 &smcallbacks,
7615 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00007616 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 sizeof(errorstring),
7618 errorstring);
7619 if (xsmp.smcconn == NULL)
7620 {
7621 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00007622
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007624 {
7625 vim_snprintf(errorreport, sizeof(errorreport),
7626 _("XSMP SmcOpenConnection failed: %s"), errorstring);
7627 verb_msg((char_u *)errorreport);
7628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 return;
7630 }
7631 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
7632
7633#if 0
7634 /* ID ourselves */
7635 smname.value = "vim";
7636 smname.length = 3;
7637 smnameprop.name = "SmProgram";
7638 smnameprop.type = "SmARRAY8";
7639 smnameprop.num_vals = 1;
7640 smnameprop.vals = &smname;
7641
7642 smprops[0] = &smnameprop;
7643 SmcSetProperties(xsmp.smcconn, 1, smprops);
7644#endif
7645}
7646
7647
7648/* Shut down XSMP comms. */
7649 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007650xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651{
7652 if (xsmp_icefd != -1)
7653 {
7654 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00007655 if (xsmp.clientid != NULL)
7656 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00007657 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 xsmp_icefd = -1;
7659 }
7660}
7661#endif /* USE_XSMP */
7662
7663
7664#ifdef EBCDIC
7665/* Translate character to its CTRL- value */
7666char CtrlTable[] =
7667{
7668/* 00 - 5E */
7669 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7670 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7671 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7672 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7673 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7674 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7675/* ^ */ 0x1E,
7676/* - */ 0x1F,
7677/* 61 - 6C */
7678 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7679/* _ */ 0x1F,
7680/* 6E - 80 */
7681 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7682/* a */ 0x01,
7683/* b */ 0x02,
7684/* c */ 0x03,
7685/* d */ 0x37,
7686/* e */ 0x2D,
7687/* f */ 0x2E,
7688/* g */ 0x2F,
7689/* h */ 0x16,
7690/* i */ 0x05,
7691/* 8A - 90 */
7692 0, 0, 0, 0, 0, 0, 0,
7693/* j */ 0x15,
7694/* k */ 0x0B,
7695/* l */ 0x0C,
7696/* m */ 0x0D,
7697/* n */ 0x0E,
7698/* o */ 0x0F,
7699/* p */ 0x10,
7700/* q */ 0x11,
7701/* r */ 0x12,
7702/* 9A - A1 */
7703 0, 0, 0, 0, 0, 0, 0, 0,
7704/* s */ 0x13,
7705/* t */ 0x3C,
7706/* u */ 0x3D,
7707/* v */ 0x32,
7708/* w */ 0x26,
7709/* x */ 0x18,
7710/* y */ 0x19,
7711/* z */ 0x3F,
7712/* AA - AC */
7713 0, 0, 0,
7714/* [ */ 0x27,
7715/* AE - BC */
7716 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7717/* ] */ 0x1D,
7718/* BE - C0 */ 0, 0, 0,
7719/* A */ 0x01,
7720/* B */ 0x02,
7721/* C */ 0x03,
7722/* D */ 0x37,
7723/* E */ 0x2D,
7724/* F */ 0x2E,
7725/* G */ 0x2F,
7726/* H */ 0x16,
7727/* I */ 0x05,
7728/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
7729/* J */ 0x15,
7730/* K */ 0x0B,
7731/* L */ 0x0C,
7732/* M */ 0x0D,
7733/* N */ 0x0E,
7734/* O */ 0x0F,
7735/* P */ 0x10,
7736/* Q */ 0x11,
7737/* R */ 0x12,
7738/* DA - DF */ 0, 0, 0, 0, 0, 0,
7739/* \ */ 0x1C,
7740/* E1 */ 0,
7741/* S */ 0x13,
7742/* T */ 0x3C,
7743/* U */ 0x3D,
7744/* V */ 0x32,
7745/* W */ 0x26,
7746/* X */ 0x18,
7747/* Y */ 0x19,
7748/* Z */ 0x3F,
7749/* EA - FF*/ 0, 0, 0, 0, 0, 0,
7750 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7751};
7752
7753char MetaCharTable[]=
7754{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7755 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
7756 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
7757 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
7758 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
7759};
7760
7761
7762/* TODO: Use characters NOT numbers!!! */
7763char CtrlCharTable[]=
7764{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7765 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
7766 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
7767 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
7768 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
7769};
7770
7771
7772#endif