blob: 59aeddb362d8f948be77ef241a4b69c30b1d24c4 [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 Moolenaar4ffa0702013-12-11 17:12:37 +0100179#if defined(__BEOS__) || defined(VMS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100180int RealWaitForChar(int, long, int *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100182static int RealWaitForChar(int, long, int *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#endif
184
185#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100186static int do_xterm_trace(void);
Bram Moolenaarcf851ce2005-06-16 21:52:47 +0000187# define XT_TRACE_DELAY 50 /* delay for xterm tracing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#endif
189
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100190static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191
192#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100193static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194#endif
195#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100196static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#endif
198#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100199static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200#endif
201#if defined(SIGALRM) && defined(FEAT_X11) \
202 && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
203# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100204static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000205/* volatile because it is used in signal handler sig_alarm(). */
206static volatile int sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100208static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100210static void catch_int_signal(void);
211static void set_signals(void);
212static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#ifndef __EMX__
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100214static int have_wildcard(int, char_u **);
215static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#endif
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#ifndef __EMX__
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100219static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
221
222#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000223# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#endif
225
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000226/* volatile because it is used in signal handler sig_winch(). */
227static volatile int do_resize = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228#ifndef __EMX__
229static char_u *extra_shell_arg = NULL;
230static int show_shell_mess = TRUE;
231#endif
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000232/* volatile because it is used in signal handler deathtrap(). */
233static volatile int deadly_signal = 0; /* The signal we caught */
234/* volatile because it is used in signal handler deathtrap(). */
235static volatile int in_mch_delay = FALSE; /* sleeping in mch_delay() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236
237static int curr_tmode = TMODE_COOK; /* contains current terminal mode */
238
239#ifdef USE_XSMP
240typedef struct
241{
242 SmcConn smcconn; /* The SM connection ID */
243 IceConn iceconn; /* The ICE connection ID */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200244 char *clientid; /* The client ID for the current smc session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 Bool save_yourself; /* If we're in the middle of a save_yourself */
246 Bool shutdown; /* If we're in shutdown mode */
247} xsmp_config_T;
248
249static xsmp_config_T xsmp;
250#endif
251
252#ifdef SYS_SIGLIST_DECLARED
253/*
254 * I have seen
255 * extern char *_sys_siglist[NSIG];
256 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
257 * that describe the signals. That is nearly what we want here. But
258 * autoconf does only check for sys_siglist (without the underscore), I
259 * do not want to change everything today.... jw.
260 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.in
261 */
262#endif
263
264static struct signalinfo
265{
266 int sig; /* Signal number, eg. SIGSEGV etc */
267 char *name; /* Signal name (not char_u!). */
268 char deadly; /* Catch as a deadly signal? */
269} signal_info[] =
270{
271#ifdef SIGHUP
272 {SIGHUP, "HUP", TRUE},
273#endif
274#ifdef SIGQUIT
275 {SIGQUIT, "QUIT", TRUE},
276#endif
277#ifdef SIGILL
278 {SIGILL, "ILL", TRUE},
279#endif
280#ifdef SIGTRAP
281 {SIGTRAP, "TRAP", TRUE},
282#endif
283#ifdef SIGABRT
284 {SIGABRT, "ABRT", TRUE},
285#endif
286#ifdef SIGEMT
287 {SIGEMT, "EMT", TRUE},
288#endif
289#ifdef SIGFPE
290 {SIGFPE, "FPE", TRUE},
291#endif
292#ifdef SIGBUS
293 {SIGBUS, "BUS", TRUE},
294#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100295#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
296 /* MzScheme uses SEGV in its garbage collector */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 {SIGSEGV, "SEGV", TRUE},
298#endif
299#ifdef SIGSYS
300 {SIGSYS, "SYS", TRUE},
301#endif
302#ifdef SIGALRM
303 {SIGALRM, "ALRM", FALSE}, /* Perl's alarm() can trigger it */
304#endif
305#ifdef SIGTERM
306 {SIGTERM, "TERM", TRUE},
307#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100308#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 {SIGVTALRM, "VTALRM", TRUE},
310#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000311#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
312 /* MzScheme uses SIGPROF for its own needs; On Linux with profiling
313 * this makes Vim exit. WE_ARE_PROFILING is defined in Makefile. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 {SIGPROF, "PROF", TRUE},
315#endif
316#ifdef SIGXCPU
317 {SIGXCPU, "XCPU", TRUE},
318#endif
319#ifdef SIGXFSZ
320 {SIGXFSZ, "XFSZ", TRUE},
321#endif
322#ifdef SIGUSR1
323 {SIGUSR1, "USR1", TRUE},
324#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000325#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
326 /* Used for sysmouse handling */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 {SIGUSR2, "USR2", TRUE},
328#endif
329#ifdef SIGINT
330 {SIGINT, "INT", FALSE},
331#endif
332#ifdef SIGWINCH
333 {SIGWINCH, "WINCH", FALSE},
334#endif
335#ifdef SIGTSTP
336 {SIGTSTP, "TSTP", FALSE},
337#endif
338#ifdef SIGPIPE
339 {SIGPIPE, "PIPE", FALSE},
340#endif
341 {-1, "Unknown!", FALSE}
342};
343
Bram Moolenaar25724922009-07-14 15:38:41 +0000344 int
345mch_chdir(path)
346 char *path;
347{
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
365mch_write(s, len)
366 char_u *s;
367 int len;
368{
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000369 ignored = (int)write(1, (char *)s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 if (p_wd) /* Unix is too fast, slow down a bit more */
371 RealWaitForChar(read_cmd_fd, p_wd, NULL);
372}
373
374/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000375 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 * Get a characters from the keyboard.
377 * Return the number of characters that are available.
378 * If wtime == 0 do not wait for characters.
379 * If wtime == n wait a short time for characters.
380 * If wtime == -1 wait forever for characters.
381 */
382 int
383mch_inchar(buf, maxlen, wtime, tb_change_cnt)
384 char_u *buf;
385 int maxlen;
386 long wtime; /* don't use "time", MIPS cannot handle it */
387 int tb_change_cnt;
388{
389 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200391#ifdef MESSAGE_QUEUE
392 parse_queued_messages();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200393#endif
394
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 /* Check if window changed size while we were busy, perhaps the ":set
396 * columns=99" command was used. */
397 while (do_resize)
398 handle_resize();
399
400 if (wtime >= 0)
401 {
402 while (WaitForChar(wtime) == 0) /* no character available */
403 {
Bram Moolenaar5d8afeb2015-11-19 19:55:16 +0100404 if (do_resize)
405 handle_resize();
406#ifdef FEAT_CLIENTSERVER
407 else if (!server_waiting())
408#else
409 else
410#endif
411 /* return if not interrupted by resize or server */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 return 0;
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200413#ifdef MESSAGE_QUEUE
414 parse_queued_messages();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 }
417 }
418 else /* wtime == -1 */
419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 /*
421 * If there is no character available within 'updatetime' seconds
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000422 * flush all the swap files to disk.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 * Also done when interrupted by SIGWINCH.
424 */
425 if (WaitForChar(p_ut) == 0)
426 {
427#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +0000428 if (trigger_cursorhold() && maxlen >= 3
429 && !typebuf_changed(tb_change_cnt))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 {
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000431 buf[0] = K_SPECIAL;
432 buf[1] = KS_EXTRA;
433 buf[2] = (int)KE_CURSORHOLD;
434 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436#endif
Bram Moolenaard4098f52005-06-27 22:37:13 +0000437 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 }
439 }
440
441 for (;;) /* repeat until we got a character */
442 {
443 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 Moolenaar67c53842010-05-22 18:28:27 +0200448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200450 * We want to be interrupted by the winch signal
451 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200453 if (WaitForChar(-1L) == 0)
454 {
455 if (do_resize) /* interrupted by SIGWINCH signal */
456 handle_resize();
457 return 0;
458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459
460 /* If input was put directly in typeahead buffer bail out here. */
461 if (typebuf_changed(tb_change_cnt))
462 return 0;
463
464 /*
465 * For some terminals we only get one character at a time.
466 * We want the get all available characters, so we could keep on
467 * trying until none is available
468 * For some other terminals this is quite slow, that's why we don't do
469 * it.
470 */
471 len = read_from_input_buf(buf, (long)maxlen);
472 if (len > 0)
473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 return len;
475 }
476 }
477}
478
479 static void
480handle_resize()
481{
482 do_resize = FALSE;
483 shell_resized();
484}
485
486/*
487 * return non-zero if a character is available
488 */
489 int
490mch_char_avail()
491{
492 return WaitForChar(0L);
493}
494
495#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
496# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000497# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498# endif
499# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
500# include <sys/sysctl.h>
501# endif
502# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
503# include <sys/sysinfo.h>
504# endif
505
506/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000507 * Return total amount of memory available in Kbyte.
508 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 long_u
511mch_total_mem(special)
Bram Moolenaar78a15312009-05-15 19:33:18 +0000512 int special UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513{
514# ifdef __EMX__
Bram Moolenaar914572a2007-05-01 11:37:47 +0000515 return ulimit(3, 0L) >> 10; /* always 32MB? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516# else
517 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000518 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
520# ifdef HAVE_SYSCTL
521 int mib[2], physmem;
522 size_t len;
523
524 /* BSD way of getting the amount of RAM available. */
525 mib[0] = CTL_HW;
526 mib[1] = HW_USERMEM;
527 len = sizeof(physmem);
528 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
529 mem = (long_u)physmem;
530# endif
531
532# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
533 if (mem == 0)
534 {
535 struct sysinfo sinfo;
536
537 /* Linux way of getting amount of RAM available */
538 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000539 {
540# ifdef HAVE_SYSINFO_MEM_UNIT
541 /* avoid overflow as much as possible */
542 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
543 {
544 sinfo.mem_unit = sinfo.mem_unit >> 1;
545 --shiftright;
546 }
547 mem = sinfo.totalram * sinfo.mem_unit;
548# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 mem = sinfo.totalram;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000550# endif
551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
553# endif
554
555# ifdef HAVE_SYSCONF
556 if (mem == 0)
557 {
558 long pagesize, pagecount;
559
560 /* Solaris way of getting amount of RAM available */
561 pagesize = sysconf(_SC_PAGESIZE);
562 pagecount = sysconf(_SC_PHYS_PAGES);
563 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000564 {
565 /* avoid overflow as much as possible */
566 while (shiftright > 0 && (pagesize & 1) == 0)
567 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000568 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000569 --shiftright;
570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 }
574# endif
575
576 /* Return the minimum of the physical memory and the user limit, because
577 * using more than the user limit may cause Vim to be terminated. */
578# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
579 {
580 struct rlimit rlp;
581
582 if (getrlimit(RLIMIT_DATA, &rlp) == 0
583 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
584# ifdef RLIM_INFINITY
585 && rlp.rlim_cur != RLIM_INFINITY
586# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000587 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000589 {
590 mem = (long_u)rlp.rlim_cur;
591 shiftright = 10;
592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 }
594# endif
595
596 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000597 return mem >> shiftright;
598 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599# endif
600}
601#endif
602
603 void
604mch_delay(msec, ignoreinput)
605 long msec;
606 int ignoreinput;
607{
608 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000609#ifdef FEAT_MZSCHEME
610 long total = msec; /* remember original value */
611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613 if (ignoreinput)
614 {
615 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000616 * here. But we don't want QUIT to kill us (CTRL-\ used in a
617 * shell may produce SIGQUIT). */
618 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 old_tmode = curr_tmode;
620 if (curr_tmode == TMODE_RAW)
621 settmode(TMODE_SLEEP);
622
623 /*
624 * Everybody sleeps in a different way...
625 * Prefer nanosleep(), some versions of usleep() can only sleep up to
626 * one second.
627 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000628#ifdef FEAT_MZSCHEME
629 do
630 {
631 /* if total is large enough, wait by portions in p_mzq */
632 if (total > p_mzq)
633 msec = p_mzq;
634 else
635 msec = total;
636 total -= msec;
637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638#ifdef HAVE_NANOSLEEP
639 {
640 struct timespec ts;
641
642 ts.tv_sec = msec / 1000;
643 ts.tv_nsec = (msec % 1000) * 1000000;
644 (void)nanosleep(&ts, NULL);
645 }
646#else
647# ifdef HAVE_USLEEP
648 while (msec >= 1000)
649 {
650 usleep((unsigned int)(999 * 1000));
651 msec -= 999;
652 }
653 usleep((unsigned int)(msec * 1000));
654# else
655# ifndef HAVE_SELECT
656 poll(NULL, 0, (int)msec);
657# else
658# ifdef __EMX__
659 _sleep2(msec);
660# else
661 {
662 struct timeval tv;
663
664 tv.tv_sec = msec / 1000;
665 tv.tv_usec = (msec % 1000) * 1000;
666 /*
667 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
668 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
669 */
670 select(0, NULL, NULL, NULL, &tv);
671 }
672# endif /* __EMX__ */
673# endif /* HAVE_SELECT */
674# endif /* HAVE_NANOSLEEP */
675#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000676#ifdef FEAT_MZSCHEME
677 }
678 while (total > 0);
679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
681 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000682 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 }
684 else
685 WaitForChar(msec);
686}
687
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000688#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
690# define HAVE_CHECK_STACK_GROWTH
691/*
692 * Support for checking for an almost-out-of-stack-space situation.
693 */
694
695/*
696 * Return a pointer to an item on the stack. Used to find out if the stack
697 * grows up or down.
698 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100699static void check_stack_growth(char *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700static int stack_grows_downwards;
701
702/*
703 * Find out if the stack grows upwards or downwards.
704 * "p" points to a variable on the stack of the caller.
705 */
706 static void
707check_stack_growth(p)
708 char *p;
709{
710 int i;
711
712 stack_grows_downwards = (p > (char *)&i);
713}
714#endif
715
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000716#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717static char *stack_limit = NULL;
718
719#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
720# include <pthread.h>
721# include <pthread_np.h>
722#endif
723
724/*
725 * Find out until how var the stack can grow without getting into trouble.
726 * Called when starting up and when switching to the signal stack in
727 * deathtrap().
728 */
729 static void
730get_stack_limit()
731{
732 struct rlimit rlp;
733 int i;
734 long lim;
735
736 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
737 * limit doesn't fit in a long (rlim_cur might be "long long"). */
738 if (getrlimit(RLIMIT_STACK, &rlp) == 0
739 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
740# ifdef RLIM_INFINITY
741 && rlp.rlim_cur != RLIM_INFINITY
742# endif
743 )
744 {
745 lim = (long)rlp.rlim_cur;
746#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
747 {
748 pthread_attr_t attr;
749 size_t size;
750
751 /* On FreeBSD the initial thread always has a fixed stack size, no
752 * matter what the limits are set to. Normally it's 1 Mbyte. */
753 pthread_attr_init(&attr);
754 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
755 {
756 pthread_attr_getstacksize(&attr, &size);
757 if (lim > (long)size)
758 lim = (long)size;
759 }
760 pthread_attr_destroy(&attr);
761 }
762#endif
763 if (stack_grows_downwards)
764 {
765 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
766 if (stack_limit >= (char *)&i)
767 /* overflow, set to 1/16 of current stack position */
768 stack_limit = (char *)((long)&i / 16L);
769 }
770 else
771 {
772 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
773 if (stack_limit <= (char *)&i)
774 stack_limit = NULL; /* overflow */
775 }
776 }
777}
778
779/*
780 * Return FAIL when running out of stack space.
781 * "p" must point to any variable local to the caller that's on the stack.
782 */
783 int
784mch_stackcheck(p)
785 char *p;
786{
787 if (stack_limit != NULL)
788 {
789 if (stack_grows_downwards)
790 {
791 if (p < stack_limit)
792 return FAIL;
793 }
794 else if (p > stack_limit)
795 return FAIL;
796 }
797 return OK;
798}
799#endif
800
801#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
802/*
803 * Support for using the signal stack.
804 * This helps when we run out of stack space, which causes a SIGSEGV. The
805 * signal handler then must run on another stack, since the normal stack is
806 * completely full.
807 */
808
Bram Moolenaar39766a72013-11-03 00:41:00 +0100809#if defined(HAVE_AVAILABILITYMACROS_H)
Bram Moolenaar4cc95d12013-11-02 21:49:32 +0100810# include <AvailabilityMacros.h>
811#endif
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813#ifndef SIGSTKSZ
814# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
815#endif
816
817# ifdef HAVE_SIGALTSTACK
818static stack_t sigstk; /* for sigaltstack() */
819# else
820static struct sigstack sigstk; /* for sigstack() */
821# endif
822
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100823static void init_signal_stack(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824static char *signal_stack;
825
826 static void
827init_signal_stack()
828{
829 if (signal_stack != NULL)
830 {
831# ifdef HAVE_SIGALTSTACK
Bram Moolenaar1a3d0862007-08-30 09:47:38 +0000832# if defined(__APPLE__) && (!defined(MAC_OS_X_VERSION_MAX_ALLOWED) \
833 || MAC_OS_X_VERSION_MAX_ALLOWED <= 1040)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 /* missing prototype. Adding it to osdef?.h.in doesn't work, because
835 * "struct sigaltstack" needs to be declared. */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100836 extern int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837# endif
838
839# ifdef HAVE_SS_BASE
840 sigstk.ss_base = signal_stack;
841# else
842 sigstk.ss_sp = signal_stack;
843# endif
844 sigstk.ss_size = SIGSTKSZ;
845 sigstk.ss_flags = 0;
846 (void)sigaltstack(&sigstk, NULL);
847# else
848 sigstk.ss_sp = signal_stack;
849 if (stack_grows_downwards)
850 sigstk.ss_sp += SIGSTKSZ - 1;
851 sigstk.ss_onstack = 0;
852 (void)sigstack(&sigstk, NULL);
853# endif
854 }
855}
856#endif
857
858/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000859 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 * will barf when the second argument to signal() is ``wrong''.
861 * Let me try it with a few tricky defines from my own osdef.h (jw).
862 */
863#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 static RETSIGTYPE
865sig_winch SIGDEFARG(sigarg)
866{
867 /* this is not required on all systems, but it doesn't hurt anybody */
868 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
869 do_resize = TRUE;
870 SIGRETURN;
871}
872#endif
873
874#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 static RETSIGTYPE
876catch_sigint SIGDEFARG(sigarg)
877{
878 /* this is not required on all systems, but it doesn't hurt anybody */
879 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
880 got_int = TRUE;
881 SIGRETURN;
882}
883#endif
884
885#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 static RETSIGTYPE
887catch_sigpwr SIGDEFARG(sigarg)
888{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000889 /* this is not required on all systems, but it doesn't hurt anybody */
890 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 /*
892 * I'm not sure we get the SIGPWR signal when the system is really going
893 * down or when the batteries are almost empty. Just preserve the swap
894 * files and don't exit, that can't do any harm.
895 */
896 ml_sync_all(FALSE, FALSE);
897 SIGRETURN;
898}
899#endif
900
901#ifdef SET_SIG_ALARM
902/*
903 * signal function for alarm().
904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 static RETSIGTYPE
906sig_alarm SIGDEFARG(sigarg)
907{
908 /* doesn't do anything, just to break a system call */
909 sig_alarm_called = TRUE;
910 SIGRETURN;
911}
912#endif
913
Bram Moolenaar44ecf652005-03-07 23:09:59 +0000914#if (defined(HAVE_SETJMP_H) \
915 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
916 || defined(FEAT_LIBCALL))) \
917 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918/*
919 * A simplistic version of setjmp() that only allows one level of using.
920 * Don't call twice before calling mch_endjmp()!.
921 * Usage:
922 * mch_startjmp();
923 * if (SETJMP(lc_jump_env) != 0)
924 * {
925 * mch_didjmp();
926 * EMSG("crash!");
927 * }
928 * else
929 * {
930 * do_the_work;
931 * mch_endjmp();
932 * }
933 * Note: Can't move SETJMP() here, because a function calling setjmp() must
934 * not return before the saved environment is used.
935 * Returns OK for normal return, FAIL when the protected code caused a
936 * problem and LONGJMP() was used.
937 */
938 void
939mch_startjmp()
940{
941#ifdef SIGHASARG
942 lc_signal = 0;
943#endif
944 lc_active = TRUE;
945}
946
947 void
948mch_endjmp()
949{
950 lc_active = FALSE;
951}
952
953 void
954mch_didjmp()
955{
956# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
957 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
958 * otherwise catching the signal only works once. */
959 init_signal_stack();
960# endif
961}
962#endif
963
964/*
965 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200966 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200968 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
969 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 */
971 static RETSIGTYPE
972deathtrap SIGDEFARG(sigarg)
973{
974 static int entered = 0; /* count the number of times we got here.
975 Note: when memory has been corrupted
976 this may get an arbitrary value! */
977#ifdef SIGHASARG
978 int i;
979#endif
980
981#if defined(HAVE_SETJMP_H)
982 /*
983 * Catch a crash in protected code.
984 * Restores the environment saved in lc_jump_env, which looks like
985 * SETJMP() returns 1.
986 */
987 if (lc_active)
988 {
989# if defined(SIGHASARG)
990 lc_signal = sigarg;
991# endif
992 lc_active = FALSE; /* don't jump again */
993 LONGJMP(lc_jump_env, 1);
994 /* NOTREACHED */
995 }
996#endif
997
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000998#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000999# ifdef SIGQUIT
1000 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1001 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1002 * pressing CTRL-\, but we don't want Vim to exit then. */
1003 if (in_mch_delay && sigarg == SIGQUIT)
1004 SIGRETURN;
1005# endif
1006
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001007 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1008 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1009 * free(). Calling free() again may then cause a crash. */
1010 if (entered == 0
1011 && (0
1012# ifdef SIGHUP
1013 || sigarg == SIGHUP
1014# endif
1015# ifdef SIGQUIT
1016 || sigarg == SIGQUIT
1017# endif
1018# ifdef SIGTERM
1019 || sigarg == SIGTERM
1020# endif
1021# ifdef SIGPWR
1022 || sigarg == SIGPWR
1023# endif
1024# ifdef SIGUSR1
1025 || sigarg == SIGUSR1
1026# endif
1027# ifdef SIGUSR2
1028 || sigarg == SIGUSR2
1029# endif
1030 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001031 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001032 SIGRETURN;
1033#endif
1034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 /* Remember how often we have been called. */
1036 ++entered;
1037
1038#ifdef FEAT_EVAL
1039 /* Set the v:dying variable. */
1040 set_vim_var_nr(VV_DYING, (long)entered);
1041#endif
1042
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001043#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 /* Since we are now using the signal stack, need to reset the stack
1045 * limit. Otherwise using a regexp will fail. */
1046 get_stack_limit();
1047#endif
1048
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001049#if 0
1050 /* This is for opening gdb the moment Vim crashes.
1051 * You need to manually adjust the file name and Vim executable name.
1052 * Suggested by SungHyun Nam. */
1053 {
1054# define VI_GDB_FILE "/tmp/vimgdb"
1055# define VIM_NAME "/usr/bin/vim"
1056 FILE *fp = fopen(VI_GDB_FILE, "w");
1057 if (fp)
1058 {
1059 fprintf(fp,
1060 "file %s\n"
1061 "attach %d\n"
1062 "set height 1000\n"
1063 "bt full\n"
1064 , VIM_NAME, getpid());
1065 fclose(fp);
1066 system("xterm -e gdb -x "VI_GDB_FILE);
1067 unlink(VI_GDB_FILE);
1068 }
1069 }
1070#endif
1071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#ifdef SIGHASARG
1073 /* try to find the name of this signal */
1074 for (i = 0; signal_info[i].sig != -1; i++)
1075 if (sigarg == signal_info[i].sig)
1076 break;
1077 deadly_signal = sigarg;
1078#endif
1079
1080 full_screen = FALSE; /* don't write message to the GUI, it might be
1081 * part of the problem... */
1082 /*
1083 * If something goes wrong after entering here, we may get here again.
1084 * When this happens, give a message and try to exit nicely (resetting the
1085 * terminal mode, etc.)
1086 * When this happens twice, just exit, don't even try to give a message,
1087 * stack may be corrupt or something weird.
1088 * When this still happens again (or memory was corrupted in such a way
1089 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1090 */
1091 if (entered >= 3)
1092 {
1093 reset_signals(); /* don't catch any signals anymore */
1094 may_core_dump();
1095 if (entered >= 4)
1096 _exit(8);
1097 exit(7);
1098 }
1099 if (entered == 2)
1100 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001101 /* No translation, it may call malloc(). */
1102 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 out_flush();
1104 getout(1);
1105 }
1106
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001107 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001109 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 signal_info[i].name);
1111#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001112 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001114
1115 /* Preserve files and exit. This sets the really_exiting flag to prevent
1116 * calling free(). */
1117 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
Bram Moolenaar009b2592004-10-24 19:18:58 +00001119#ifdef NBDEBUG
1120 reset_signals();
1121 may_core_dump();
1122 abort();
1123#endif
1124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 SIGRETURN;
1126}
1127
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001128#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129/*
1130 * On Solaris with multi-threading, suspending might not work immediately.
1131 * Catch the SIGCONT signal, which will be used as an indication whether the
1132 * suspending has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001133 *
1134 * On Linux, signal is not always handled immediately either.
1135 * See https://bugs.launchpad.net/bugs/291373
1136 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001137 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001139static volatile int sigcont_received;
Bram Moolenaard99df422016-01-29 23:20:40 +01001140static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141
1142/*
1143 * signal handler for SIGCONT
1144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 static RETSIGTYPE
1146sigcont_handler SIGDEFARG(sigarg)
1147{
1148 sigcont_received = TRUE;
1149 SIGRETURN;
1150}
1151#endif
1152
Bram Moolenaar62b42182010-09-21 22:09:37 +02001153# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001154static void loose_clipboard(void);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001155# ifdef USE_SYSTEM
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001156static void save_clipboard(void);
1157static void restore_clipboard(void);
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001158
1159static void *clip_star_save = NULL;
1160static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001161# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001162
1163/*
1164 * Called when Vim is going to sleep or execute a shell command.
1165 * We can't respond to requests for the X selections. Lose them, otherwise
1166 * other applications will hang. But first copy the text to cut buffer 0.
1167 */
1168 static void
1169loose_clipboard()
1170{
1171 if (clip_star.owned || clip_plus.owned)
1172 {
1173 x11_export_final_selection();
1174 if (clip_star.owned)
1175 clip_lose_selection(&clip_star);
1176 if (clip_plus.owned)
1177 clip_lose_selection(&clip_plus);
1178 if (x11_display != NULL)
1179 XFlush(x11_display);
1180 }
1181}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001182
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001183# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001184/*
1185 * Save clipboard text to restore later.
1186 */
1187 static void
1188save_clipboard()
1189{
1190 if (clip_star.owned)
1191 clip_star_save = get_register('*', TRUE);
1192 if (clip_plus.owned)
1193 clip_plus_save = get_register('+', TRUE);
1194}
1195
1196/*
1197 * Restore clipboard text if no one own the X selection.
1198 */
1199 static void
1200restore_clipboard()
1201{
1202 if (clip_star_save != NULL)
1203 {
1204 if (!clip_gen_owner_exists(&clip_star))
1205 put_register('*', clip_star_save);
1206 else
1207 free_register(clip_star_save);
1208 clip_star_save = NULL;
1209 }
1210 if (clip_plus_save != NULL)
1211 {
1212 if (!clip_gen_owner_exists(&clip_plus))
1213 put_register('+', clip_plus_save);
1214 else
1215 free_register(clip_plus_save);
1216 clip_plus_save = NULL;
1217 }
1218}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001219# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001220#endif
1221
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222/*
1223 * If the machine has job control, use it to suspend the program,
1224 * otherwise fake it by starting a new shell.
1225 */
1226 void
1227mch_suspend()
1228{
1229 /* BeOS does have SIGTSTP, but it doesn't work. */
1230#if defined(SIGTSTP) && !defined(__BEOS__)
1231 out_flush(); /* needed to make cursor visible on some systems */
1232 settmode(TMODE_COOK);
1233 out_flush(); /* needed to disable mouse on some systems */
1234
1235# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001236 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237# endif
1238
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001239# if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 sigcont_received = FALSE;
1241# endif
1242 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001243# if defined(_REENTRANT) && defined(SIGCONT)
1244 /*
1245 * Wait for the SIGCONT signal to be handled. It generally happens
1246 * immediately, but somehow not all the time. Do not call pause()
1247 * because there would be race condition which would hang Vim if
1248 * signal happened in between the test of sigcont_received and the
1249 * call to pause(). If signal is not yet received, call sleep(0)
1250 * to just yield CPU. Signal should then be received. If somehow
1251 * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting
1252 * further if signal is not received after 1+2+3+4 ms (not expected
1253 * to happen).
1254 */
1255 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001256 long wait_time;
1257 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001258 /* Loop is not entered most of the time */
Bram Moolenaar262735e2009-07-14 10:20:22 +00001259 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261# endif
1262
1263# ifdef FEAT_TITLE
1264 /*
1265 * Set oldtitle to NULL, so the current title is obtained again.
1266 */
1267 vim_free(oldtitle);
1268 oldtitle = NULL;
1269# endif
1270 settmode(TMODE_RAW);
1271 need_check_timestamps = TRUE;
1272 did_check_timestamps = FALSE;
1273#else
1274 suspend_shell();
1275#endif
1276}
1277
1278 void
1279mch_init()
1280{
1281 Columns = 80;
1282 Rows = 24;
1283
1284 out_flush();
1285 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001286
Bram Moolenaar56718732006-03-15 22:53:57 +00001287#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001288 mac_conv_init();
1289#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001290#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1291 win_clip_init();
1292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293}
1294
1295 static void
1296set_signals()
1297{
1298#if defined(SIGWINCH)
1299 /*
1300 * WINDOW CHANGE signal is handled with sig_winch().
1301 */
1302 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1303#endif
1304
1305 /*
1306 * We want the STOP signal to work, to make mch_suspend() work.
1307 * For "rvim" the STOP signal is ignored.
1308 */
1309#ifdef SIGTSTP
1310 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1311#endif
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001312#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 signal(SIGCONT, sigcont_handler);
1314#endif
1315
1316 /*
1317 * We want to ignore breaking of PIPEs.
1318 */
1319#ifdef SIGPIPE
1320 signal(SIGPIPE, SIG_IGN);
1321#endif
1322
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001324 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325#endif
1326
1327 /*
1328 * Ignore alarm signals (Perl's alarm() generates it).
1329 */
1330#ifdef SIGALRM
1331 signal(SIGALRM, SIG_IGN);
1332#endif
1333
1334 /*
1335 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1336 * work will be lost.
1337 */
1338#ifdef SIGPWR
1339 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1340#endif
1341
1342 /*
1343 * Arrange for other signals to gracefully shutdown Vim.
1344 */
1345 catch_signals(deathtrap, SIG_ERR);
1346
1347#if defined(FEAT_GUI) && defined(SIGHUP)
1348 /*
1349 * When the GUI is running, ignore the hangup signal.
1350 */
1351 if (gui.in_use)
1352 signal(SIGHUP, SIG_IGN);
1353#endif
1354}
1355
Bram Moolenaardf177f62005-02-22 08:39:57 +00001356#if defined(SIGINT) || defined(PROTO)
1357/*
1358 * Catch CTRL-C (only works while in Cooked mode).
1359 */
1360 static void
1361catch_int_signal()
1362{
1363 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1364}
1365#endif
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 void
1368reset_signals()
1369{
1370 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001371#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 /* SIGCONT isn't in the list, because its default action is ignore */
1373 signal(SIGCONT, SIG_DFL);
1374#endif
1375}
1376
1377 static void
1378catch_signals(func_deadly, func_other)
1379 RETSIGTYPE (*func_deadly)();
1380 RETSIGTYPE (*func_other)();
1381{
1382 int i;
1383
1384 for (i = 0; signal_info[i].sig != -1; i++)
1385 if (signal_info[i].deadly)
1386 {
1387#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1388 struct sigaction sa;
1389
1390 /* Setup to use the alternate stack for the signal function. */
1391 sa.sa_handler = func_deadly;
1392 sigemptyset(&sa.sa_mask);
1393# if defined(__linux__) && defined(_REENTRANT)
1394 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1395 * thread handling in combination with using the alternate stack:
1396 * pthread library functions try to use the stack pointer to
1397 * identify the current thread, causing a SEGV signal, which
1398 * recursively calls deathtrap() and hangs. */
1399 sa.sa_flags = 0;
1400# else
1401 sa.sa_flags = SA_ONSTACK;
1402# endif
1403 sigaction(signal_info[i].sig, &sa, NULL);
1404#else
1405# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1406 struct sigvec sv;
1407
1408 /* Setup to use the alternate stack for the signal function. */
1409 sv.sv_handler = func_deadly;
1410 sv.sv_mask = 0;
1411 sv.sv_flags = SV_ONSTACK;
1412 sigvec(signal_info[i].sig, &sv, NULL);
1413# else
1414 signal(signal_info[i].sig, func_deadly);
1415# endif
1416#endif
1417 }
1418 else if (func_other != SIG_ERR)
1419 signal(signal_info[i].sig, func_other);
1420}
1421
1422/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001423 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001424 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1425 * return TRUE
1426 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1427 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001428 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001429 * Returns TRUE when Vim should exit.
1430 */
1431 int
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001432vim_handle_signal(sig)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001433 int sig;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001434{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001435 static int got_signal = 0;
1436 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001437
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001438 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001439 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001440 case SIGNAL_BLOCK: blocked = TRUE;
1441 break;
1442
1443 case SIGNAL_UNBLOCK: blocked = FALSE;
1444 if (got_signal != 0)
1445 {
1446 kill(getpid(), got_signal);
1447 got_signal = 0;
1448 }
1449 break;
1450
1451 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001452 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001453 got_signal = sig;
1454#ifdef SIGPWR
1455 if (sig != SIGPWR)
1456#endif
1457 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001458 break;
1459 }
1460 return FALSE;
1461}
1462
1463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 * Check_win checks whether we have an interactive stdout.
1465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 int
1467mch_check_win(argc, argv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001468 int argc UNUSED;
1469 char **argv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 if (isatty(1))
1472 return OK;
1473 return FAIL;
1474}
1475
1476/*
1477 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1478 */
1479 int
1480mch_input_isatty()
1481{
1482 if (isatty(read_cmd_fd))
1483 return TRUE;
1484 return FALSE;
1485}
1486
1487#ifdef FEAT_X11
1488
1489# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) \
1490 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1491
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001492static void xopen_message(struct timeval *tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
1494/*
1495 * Give a message about the elapsed time for opening the X window.
1496 */
1497 static void
1498xopen_message(tvp)
1499 struct timeval *tvp; /* must contain start time */
1500{
1501 struct timeval end_tv;
1502
1503 /* Compute elapsed time. */
1504 gettimeofday(&end_tv, NULL);
1505 smsg((char_u *)_("Opening the X display took %ld msec"),
1506 (end_tv.tv_sec - tvp->tv_sec) * 1000L
Bram Moolenaar051b7822005-05-19 21:00:46 +00001507 + (end_tv.tv_usec - tvp->tv_usec) / 1000L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508}
1509# endif
1510#endif
1511
1512#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1513/*
1514 * A few functions shared by X11 title and clipboard code.
1515 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001516static int x_error_handler(Display *dpy, XErrorEvent *error_event);
1517static int x_error_check(Display *dpy, XErrorEvent *error_event);
1518static int x_connect_to_server(void);
1519static int test_x11_window(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
1521static int got_x_error = FALSE;
1522
1523/*
1524 * X Error handler, otherwise X just exits! (very rude) -- webb
1525 */
1526 static int
1527x_error_handler(dpy, error_event)
1528 Display *dpy;
1529 XErrorEvent *error_event;
1530{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001531 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 STRCAT(IObuff, _("\nVim: Got X error\n"));
1533
1534 /* We cannot print a message and continue, because no X calls are allowed
1535 * here (causes my system to hang). Silently continuing might be an
1536 * alternative... */
1537 preserve_exit(); /* preserve files and exit */
1538
1539 return 0; /* NOTREACHED */
1540}
1541
1542/*
1543 * Another X Error handler, just used to check for errors.
1544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 static int
1546x_error_check(dpy, error_event)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001547 Display *dpy UNUSED;
1548 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
1562x_IOerror_check(dpy)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001563 Display *dpy UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565 /* This function should not return, it causes exit(). Longjump instead. */
1566 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001567# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001568 return 0; /* avoid the compiler complains about missing return value */
1569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570}
1571# endif
1572
1573/*
1574 * An X IO Error handler, used to catch terminal errors.
1575 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001576static int x_IOerror_handler(Display *dpy);
1577static void may_restore_clipboard(void);
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001578static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 static int
1581x_IOerror_handler(dpy)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001582 Display *dpy UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583{
1584 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001585 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 x11_window = 0;
1587 x11_display = NULL;
1588 xterm_Shell = (Widget)0;
1589
1590 /* This function should not return, it causes exit(). Longjump instead. */
1591 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001592# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001593 return 0; /* avoid the compiler complains about missing return value */
1594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001596
1597/*
1598 * If the X11 connection was lost try to restore it.
1599 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001600 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001601 */
1602 static void
1603may_restore_clipboard()
1604{
1605 if (xterm_dpy_was_reset)
1606 {
1607 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001608
1609# ifndef LESSTIF_VERSION
1610 /* This has been reported to avoid Vim getting stuck. */
1611 if (app_context != (XtAppContext)NULL)
1612 {
1613 XtDestroyApplicationContext(app_context);
1614 app_context = (XtAppContext)NULL;
1615 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1616 }
1617# endif
1618
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001619 setup_term_clip();
1620 get_x11_title(FALSE);
1621 }
1622}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623#endif
1624
1625/*
1626 * Return TRUE when connection to the X server is desired.
1627 */
1628 static int
1629x_connect_to_server()
1630{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631#if defined(FEAT_CLIENTSERVER)
1632 if (x_force_connect)
1633 return TRUE;
1634#endif
1635 if (x_no_connect)
1636 return FALSE;
1637
1638 /* Check for a match with "exclude:" from 'clipboard'. */
1639 if (clip_exclude_prog != NULL)
1640 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001641 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 return FALSE;
1643 }
1644 return TRUE;
1645}
1646
1647/*
1648 * Test if "dpy" and x11_window are valid by getting the window title.
1649 * I don't actually want it yet, so there may be a simpler call to use, but
1650 * this will cause the error handler x_error_check() to be called if anything
1651 * is wrong, such as the window pointer being invalid (as can happen when the
1652 * user changes his DISPLAY, but not his WINDOWID) -- webb
1653 */
1654 static int
1655test_x11_window(dpy)
1656 Display *dpy;
1657{
1658 int (*old_handler)();
1659 XTextProperty text_prop;
1660
1661 old_handler = XSetErrorHandler(x_error_check);
1662 got_x_error = FALSE;
1663 if (XGetWMName(dpy, x11_window, &text_prop))
1664 XFree((void *)text_prop.value);
1665 XSync(dpy, False);
1666 (void)XSetErrorHandler(old_handler);
1667
1668 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001669 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
1671 return (got_x_error ? FAIL : OK);
1672}
1673#endif
1674
1675#ifdef FEAT_TITLE
1676
1677#ifdef FEAT_X11
1678
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001679static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
1681/*
1682 * try to get x11 window and display
1683 *
1684 * return FAIL for failure, OK otherwise
1685 */
1686 static int
1687get_x11_windis()
1688{
1689 char *winid;
1690 static int result = -1;
1691#define XD_NONE 0 /* x11_display not set here */
1692#define XD_HERE 1 /* x11_display opened here */
1693#define XD_GUI 2 /* x11_display used from gui.dpy */
1694#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1695 static int x11_display_from = XD_NONE;
1696 static int did_set_error_handler = FALSE;
1697
1698 if (!did_set_error_handler)
1699 {
1700 /* X just exits if it finds an error otherwise! */
1701 (void)XSetErrorHandler(x_error_handler);
1702 did_set_error_handler = TRUE;
1703 }
1704
Bram Moolenaar9372a112005-12-06 19:59:18 +00001705#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if (gui.in_use)
1707 {
1708 /*
1709 * If the X11 display was opened here before, for the window where Vim
1710 * was started, close that one now to avoid a memory leak.
1711 */
1712 if (x11_display_from == XD_HERE && x11_display != NULL)
1713 {
1714 XCloseDisplay(x11_display);
1715 x11_display_from = XD_NONE;
1716 }
1717 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1718 {
1719 x11_display_from = XD_GUI;
1720 return OK;
1721 }
1722 x11_display = NULL;
1723 return FAIL;
1724 }
1725 else if (x11_display_from == XD_GUI)
1726 {
1727 /* GUI must have stopped somehow, clear x11_display */
1728 x11_window = 0;
1729 x11_display = NULL;
1730 x11_display_from = XD_NONE;
1731 }
1732#endif
1733
1734 /* When started with the "-X" argument, don't try connecting. */
1735 if (!x_connect_to_server())
1736 return FAIL;
1737
1738 /*
1739 * If WINDOWID not set, should try another method to find out
1740 * what the current window number is. The only code I know for
1741 * this is very complicated.
1742 * We assume that zero is invalid for WINDOWID.
1743 */
1744 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1745 x11_window = (Window)atol(winid);
1746
1747#ifdef FEAT_XCLIPBOARD
1748 if (xterm_dpy != NULL && x11_window != 0)
1749 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001750 /* We may have checked it already, but Gnome terminal can move us to
1751 * another window, so we need to check every time. */
1752 if (x11_display_from != XD_XTERM)
1753 {
1754 /*
1755 * If the X11 display was opened here before, for the window where
1756 * Vim was started, close that one now to avoid a memory leak.
1757 */
1758 if (x11_display_from == XD_HERE && x11_display != NULL)
1759 XCloseDisplay(x11_display);
1760 x11_display = xterm_dpy;
1761 x11_display_from = XD_XTERM;
1762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (test_x11_window(x11_display) == FAIL)
1764 {
1765 /* probably bad $WINDOWID */
1766 x11_window = 0;
1767 x11_display = NULL;
1768 x11_display_from = XD_NONE;
1769 return FAIL;
1770 }
1771 return OK;
1772 }
1773#endif
1774
1775 if (x11_window == 0 || x11_display == NULL)
1776 result = -1;
1777
1778 if (result != -1) /* Have already been here and set this */
1779 return result; /* Don't do all these X calls again */
1780
1781 if (x11_window != 0 && x11_display == NULL)
1782 {
1783#ifdef SET_SIG_ALARM
1784 RETSIGTYPE (*sig_save)();
1785#endif
1786#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1787 struct timeval start_tv;
1788
1789 if (p_verbose > 0)
1790 gettimeofday(&start_tv, NULL);
1791#endif
1792
1793#ifdef SET_SIG_ALARM
1794 /*
1795 * Opening the Display may hang if the DISPLAY setting is wrong, or
1796 * the network connection is bad. Set an alarm timer to get out.
1797 */
1798 sig_alarm_called = FALSE;
1799 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1800 (RETSIGTYPE (*)())sig_alarm);
1801 alarm(2);
1802#endif
1803 x11_display = XOpenDisplay(NULL);
1804
1805#ifdef SET_SIG_ALARM
1806 alarm(0);
1807 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1808 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001809 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810#endif
1811 if (x11_display != NULL)
1812 {
1813# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1814 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001815 {
1816 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 xopen_message(&start_tv);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001818 verbose_leave();
1819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820# endif
1821 if (test_x11_window(x11_display) == FAIL)
1822 {
1823 /* Maybe window id is bad */
1824 x11_window = 0;
1825 XCloseDisplay(x11_display);
1826 x11_display = NULL;
1827 }
1828 else
1829 x11_display_from = XD_HERE;
1830 }
1831 }
1832 if (x11_window == 0 || x11_display == NULL)
1833 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001834
1835# ifdef FEAT_EVAL
1836 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1837# endif
1838
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 return (result = OK);
1840}
1841
1842/*
1843 * Determine original x11 Window Title
1844 */
1845 static int
1846get_x11_title(test_only)
1847 int test_only;
1848{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001849 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850}
1851
1852/*
1853 * Determine original x11 Window icon
1854 */
1855 static int
1856get_x11_icon(test_only)
1857 int test_only;
1858{
1859 int retval = FALSE;
1860
1861 retval = get_x11_thing(FALSE, test_only);
1862
1863 /* could not get old icon, use terminal name */
1864 if (oldicon == NULL && !test_only)
1865 {
1866 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001867 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001869 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 }
1871
1872 return retval;
1873}
1874
1875 static int
1876get_x11_thing(get_title, test_only)
1877 int get_title; /* get title string */
1878 int test_only;
1879{
1880 XTextProperty text_prop;
1881 int retval = FALSE;
1882 Status status;
1883
1884 if (get_x11_windis() == OK)
1885 {
1886 /* Get window/icon name if any */
1887 if (get_title)
1888 status = XGetWMName(x11_display, x11_window, &text_prop);
1889 else
1890 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1891
1892 /*
1893 * If terminal is xterm, then x11_window may be a child window of the
1894 * outer xterm window that actually contains the window/icon name, so
1895 * keep traversing up the tree until a window with a title/icon is
1896 * found.
1897 */
1898 /* Previously this was only done for xterm and alikes. I don't see a
1899 * reason why it would fail for other terminal emulators.
1900 * if (term_is_xterm) */
1901 {
1902 Window root;
1903 Window parent;
1904 Window win = x11_window;
1905 Window *children;
1906 unsigned int num_children;
1907
1908 while (!status || text_prop.value == NULL)
1909 {
1910 if (!XQueryTree(x11_display, win, &root, &parent, &children,
1911 &num_children))
1912 break;
1913 if (children)
1914 XFree((void *)children);
1915 if (parent == root || parent == 0)
1916 break;
1917
1918 win = parent;
1919 if (get_title)
1920 status = XGetWMName(x11_display, win, &text_prop);
1921 else
1922 status = XGetWMIconName(x11_display, win, &text_prop);
1923 }
1924 }
1925 if (status && text_prop.value != NULL)
1926 {
1927 retval = TRUE;
1928 if (!test_only)
1929 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001930#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
1931 if (text_prop.encoding == XA_STRING
1932# ifdef FEAT_MBYTE
1933 && !has_mbyte
1934# endif
1935 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 {
1937#endif
1938 if (get_title)
1939 oldtitle = vim_strsave((char_u *)text_prop.value);
1940 else
1941 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001942#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 }
1944 else
1945 {
1946 char **cl;
1947 Status transform_status;
1948 int n = 0;
1949
1950 transform_status = XmbTextPropertyToTextList(x11_display,
1951 &text_prop,
1952 &cl, &n);
1953 if (transform_status >= Success && n > 0 && cl[0])
1954 {
1955 if (get_title)
1956 oldtitle = vim_strsave((char_u *) cl[0]);
1957 else
1958 oldicon = vim_strsave((char_u *) cl[0]);
1959 XFreeStringList(cl);
1960 }
1961 else
1962 {
1963 if (get_title)
1964 oldtitle = vim_strsave((char_u *)text_prop.value);
1965 else
1966 oldicon = vim_strsave((char_u *)text_prop.value);
1967 }
1968 }
1969#endif
1970 }
1971 XFree((void *)text_prop.value);
1972 }
1973 }
1974 return retval;
1975}
1976
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02001977/* Xutf8 functions are not avaialble on older systems. Note that on some
1978 * systems X_HAVE_UTF8_STRING may be defined in a header file but
1979 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
1980 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02001982# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983# define USE_UTF8_STRING
1984# endif
1985#endif
1986
1987/*
1988 * Set x11 Window Title
1989 *
1990 * get_x11_windis() must be called before this and have returned OK
1991 */
1992 static void
1993set_x11_title(title)
1994 char_u *title;
1995{
1996 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
1997 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
1998 * supported everywhere and STRING doesn't work for multi-byte titles.
1999 */
2000#ifdef USE_UTF8_STRING
2001 if (enc_utf8)
2002 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2003 NULL, NULL, 0, NULL, NULL, NULL);
2004 else
2005#endif
2006 {
2007#if XtSpecificationRelease >= 4
2008# ifdef FEAT_XFONTSET
2009 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2010 NULL, NULL, 0, NULL, NULL, NULL);
2011# else
2012 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002013 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014
2015 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002016 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 XSetWMProperties(x11_display, x11_window, &text_prop,
2018 NULL, NULL, 0, NULL, NULL, NULL);
2019# endif
2020#else
2021 XStoreName(x11_display, x11_window, (char *)title);
2022#endif
2023 }
2024 XFlush(x11_display);
2025}
2026
2027/*
2028 * Set x11 Window icon
2029 *
2030 * get_x11_windis() must be called before this and have returned OK
2031 */
2032 static void
2033set_x11_icon(icon)
2034 char_u *icon;
2035{
2036 /* See above for comments about using X*SetWMProperties(). */
2037#ifdef USE_UTF8_STRING
2038 if (enc_utf8)
2039 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2040 NULL, 0, NULL, NULL, NULL);
2041 else
2042#endif
2043 {
2044#if XtSpecificationRelease >= 4
2045# ifdef FEAT_XFONTSET
2046 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2047 NULL, 0, NULL, NULL, NULL);
2048# else
2049 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002050 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002052 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2054 NULL, 0, NULL, NULL, NULL);
2055# endif
2056#else
2057 XSetIconName(x11_display, x11_window, (char *)icon);
2058#endif
2059 }
2060 XFlush(x11_display);
2061}
2062
2063#else /* FEAT_X11 */
2064
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 static int
2066get_x11_title(test_only)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002067 int test_only UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068{
2069 return FALSE;
2070}
2071
2072 static int
2073get_x11_icon(test_only)
2074 int test_only;
2075{
2076 if (!test_only)
2077 {
2078 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002079 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002081 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 }
2083 return FALSE;
2084}
2085
2086#endif /* FEAT_X11 */
2087
2088 int
2089mch_can_restore_title()
2090{
2091 return get_x11_title(TRUE);
2092}
2093
2094 int
2095mch_can_restore_icon()
2096{
2097 return get_x11_icon(TRUE);
2098}
2099
2100/*
2101 * Set the window title and icon.
2102 */
2103 void
2104mch_settitle(title, icon)
2105 char_u *title;
2106 char_u *icon;
2107{
2108 int type = 0;
2109 static int recursive = 0;
2110
2111 if (T_NAME == NULL) /* no terminal name (yet) */
2112 return;
2113 if (title == NULL && icon == NULL) /* nothing to do */
2114 return;
2115
2116 /* When one of the X11 functions causes a deadly signal, we get here again
2117 * recursively. Avoid hanging then (something is probably locked). */
2118 if (recursive)
2119 return;
2120 ++recursive;
2121
2122 /*
2123 * if the window ID and the display is known, we may use X11 calls
2124 */
2125#ifdef FEAT_X11
2126 if (get_x11_windis() == OK)
2127 type = 1;
2128#else
2129# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2130 if (gui.in_use)
2131 type = 1;
2132# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133#endif
2134
2135 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002136 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 * than x11 calls, because the x11 calls don't always work
2138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if ((type || *T_TS != NUL) && title != NULL)
2140 {
2141 if (oldtitle == NULL
2142#ifdef FEAT_GUI
2143 && !gui.in_use
2144#endif
2145 ) /* first call but not in GUI, save title */
2146 (void)get_x11_title(FALSE);
2147
2148 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2149 term_settitle(title);
2150#ifdef FEAT_X11
2151 else
2152# ifdef FEAT_GUI_GTK
2153 if (!gui.in_use) /* don't do this if GTK+ is running */
2154# endif
2155 set_x11_title(title); /* x11 */
2156#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002157#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2159 else
2160 gui_mch_settitle(title, icon);
2161#endif
2162 did_set_title = TRUE;
2163 }
2164
2165 if ((type || *T_CIS != NUL) && icon != NULL)
2166 {
2167 if (oldicon == NULL
2168#ifdef FEAT_GUI
2169 && !gui.in_use
2170#endif
2171 ) /* first call, save icon */
2172 get_x11_icon(FALSE);
2173
2174 if (*T_CIS != NUL)
2175 {
2176 out_str(T_CIS); /* set icon start */
2177 out_str_nf(icon);
2178 out_str(T_CIE); /* set icon end */
2179 out_flush();
2180 }
2181#ifdef FEAT_X11
2182 else
2183# ifdef FEAT_GUI_GTK
2184 if (!gui.in_use) /* don't do this if GTK+ is running */
2185# endif
2186 set_x11_icon(icon); /* x11 */
2187#endif
2188 did_set_icon = TRUE;
2189 }
2190 --recursive;
2191}
2192
2193/*
2194 * Restore the window/icon title.
2195 * "which" is one of:
2196 * 1 only restore title
2197 * 2 only restore icon
2198 * 3 restore title and icon
2199 */
2200 void
2201mch_restore_title(which)
2202 int which;
2203{
2204 /* only restore the title or icon when it has been set */
2205 mch_settitle(((which & 1) && did_set_title) ?
2206 (oldtitle ? oldtitle : p_titleold) : NULL,
2207 ((which & 2) && did_set_icon) ? oldicon : NULL);
2208}
2209
2210#endif /* FEAT_TITLE */
2211
2212/*
2213 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002214 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 */
2216 int
2217vim_is_xterm(name)
2218 char_u *name;
2219{
2220 if (name == NULL)
2221 return FALSE;
2222 return (STRNICMP(name, "xterm", 5) == 0
2223 || STRNICMP(name, "nxterm", 6) == 0
2224 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002225 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 || STRNICMP(name, "rxvt", 4) == 0
2227 || STRCMP(name, "builtin_xterm") == 0);
2228}
2229
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002230#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2231/*
2232 * Return TRUE if "name" appears to be that of a terminal
2233 * known to support the xterm-style mouse protocol.
2234 * Relies on term_is_xterm having been set to its correct value.
2235 */
2236 int
2237use_xterm_like_mouse(name)
2238 char_u *name;
2239{
2240 return (name != NULL
2241 && (term_is_xterm || STRNICMP(name, "screen", 6) == 0));
2242}
2243#endif
2244
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2246/*
2247 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2248 * Return 1 for "xterm".
2249 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002250 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002251 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 */
2253 int
2254use_xterm_mouse()
2255{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002256 if (ttym_flags == TTYM_SGR)
2257 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002258 if (ttym_flags == TTYM_URXVT)
2259 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 if (ttym_flags == TTYM_XTERM2)
2261 return 2;
2262 if (ttym_flags == TTYM_XTERM)
2263 return 1;
2264 return 0;
2265}
2266#endif
2267
2268 int
2269vim_is_iris(name)
2270 char_u *name;
2271{
2272 if (name == NULL)
2273 return FALSE;
2274 return (STRNICMP(name, "iris-ansi", 9) == 0
2275 || STRCMP(name, "builtin_iris-ansi") == 0);
2276}
2277
2278 int
2279vim_is_vt300(name)
2280 char_u *name;
2281{
2282 if (name == NULL)
2283 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002284 /* catch VT100 - VT5xx */
2285 return ((STRNICMP(name, "vt", 2) == 0
2286 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 || STRCMP(name, "builtin_vt320") == 0);
2288}
2289
2290/*
2291 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2292 * This should include all windowed terminal emulators.
2293 */
2294 int
2295vim_is_fastterm(name)
2296 char_u *name;
2297{
2298 if (name == NULL)
2299 return FALSE;
2300 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2301 return TRUE;
2302 return ( STRNICMP(name, "hpterm", 6) == 0
2303 || STRNICMP(name, "sun-cmd", 7) == 0
2304 || STRNICMP(name, "screen", 6) == 0
2305 || STRNICMP(name, "dtterm", 6) == 0);
2306}
2307
2308/*
2309 * Insert user name in s[len].
2310 * Return OK if a name found.
2311 */
2312 int
2313mch_get_user_name(s, len)
2314 char_u *s;
2315 int len;
2316{
2317#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002318 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 return OK;
2320#else
2321 return mch_get_uname(getuid(), s, len);
2322#endif
2323}
2324
2325/*
2326 * Insert user name for "uid" in s[len].
2327 * Return OK if a name found.
2328 */
2329 int
2330mch_get_uname(uid, s, len)
2331 uid_t uid;
2332 char_u *s;
2333 int len;
2334{
2335#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2336 struct passwd *pw;
2337
2338 if ((pw = getpwuid(uid)) != NULL
2339 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2340 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002341 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 return OK;
2343 }
2344#endif
2345 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2346 return FAIL; /* a number is not a name */
2347}
2348
2349/*
2350 * Insert host name is s[len].
2351 */
2352
2353#ifdef HAVE_SYS_UTSNAME_H
2354 void
2355mch_get_host_name(s, len)
2356 char_u *s;
2357 int len;
2358{
2359 struct utsname vutsname;
2360
2361 if (uname(&vutsname) < 0)
2362 *s = NUL;
2363 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002364 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365}
2366#else /* HAVE_SYS_UTSNAME_H */
2367
2368# ifdef HAVE_SYS_SYSTEMINFO_H
2369# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2370# endif
2371
2372 void
2373mch_get_host_name(s, len)
2374 char_u *s;
2375 int len;
2376{
2377# ifdef VAXC
2378 vaxc$gethostname((char *)s, len);
2379# else
2380 gethostname((char *)s, len);
2381# endif
2382 s[len - 1] = NUL; /* make sure it's terminated */
2383}
2384#endif /* HAVE_SYS_UTSNAME_H */
2385
2386/*
2387 * return process ID
2388 */
2389 long
2390mch_get_pid()
2391{
2392 return (long)getpid();
2393}
2394
2395#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002396static char *strerror(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 static char *
2399strerror(err)
2400 int err;
2401{
2402 extern int sys_nerr;
2403 extern char *sys_errlist[];
2404 static char er[20];
2405
2406 if (err > 0 && err < sys_nerr)
2407 return (sys_errlist[err]);
2408 sprintf(er, "Error %d", err);
2409 return er;
2410}
2411#endif
2412
2413/*
2414 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2415 * Return OK for success, FAIL for failure.
2416 */
2417 int
2418mch_dirname(buf, len)
2419 char_u *buf;
2420 int len;
2421{
2422#if defined(USE_GETCWD)
2423 if (getcwd((char *)buf, len) == NULL)
2424 {
2425 STRCPY(buf, strerror(errno));
2426 return FAIL;
2427 }
2428 return OK;
2429#else
2430 return (getwd((char *)buf) != NULL ? OK : FAIL);
2431#endif
2432}
2433
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002435 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 *
2437 * return FAIL for failure, OK for success
2438 */
2439 int
2440mch_FullName(fname, buf, len, force)
2441 char_u *fname, *buf;
2442 int len;
2443 int force; /* also expand when already absolute path */
2444{
2445 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002446#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 int fd = -1;
2448 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 char_u olddir[MAXPATHL];
2451 char_u *p;
2452 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002453#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002454 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2455 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002456#endif
2457
Bram Moolenaar38323e42007-03-06 19:22:53 +00002458#ifdef VMS
2459 fname = vms_fixfilename(fname);
2460#endif
2461
Bram Moolenaara2442432007-04-26 14:26:37 +00002462#ifdef __CYGWIN__
2463 /*
2464 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2465 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002466# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002467 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2468 * a forward slash. */
2469 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2470 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002471# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002472 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002473# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002474 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002475#endif
2476
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002477 /* Expand it if forced or not an absolute path.
2478 * Do not do it for "/file", the result is always "/". */
2479 if ((force || !mch_isFullName(fname))
2480 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
2482 /*
2483 * If the file name has a path, change to that directory for a moment,
2484 * and then do the getwd() (and get back to where we were).
2485 * This will get the correct path name with "../" things.
2486 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002487 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002489#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 /*
2491 * Use fchdir() if possible, it's said to be faster and more
2492 * reliable. But on SunOS 4 it might not work. Check this by
2493 * doing a fchdir() right now.
2494 */
2495 if (!dont_fchdir)
2496 {
2497 fd = open(".", O_RDONLY | O_EXTRA, 0);
2498 if (fd >= 0 && fchdir(fd) < 0)
2499 {
2500 close(fd);
2501 fd = -1;
2502 dont_fchdir = TRUE; /* don't try again */
2503 }
2504 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506
2507 /* Only change directory when we are sure we can return to where
2508 * we are now. After doing "su" chdir(".") might not work. */
2509 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002510#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 (mch_dirname(olddir, MAXPATHL) == FAIL
2514 || mch_chdir((char *)olddir) != 0))
2515 {
2516 p = NULL; /* can't get current dir: don't chdir */
2517 retval = FAIL;
2518 }
2519 else
2520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 /* The directory is copied into buf[], to be able to remove
2522 * the file name without changing it (could be a string in
2523 * read-only memory) */
2524 if (p - fname >= len)
2525 retval = FAIL;
2526 else
2527 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002528 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 if (mch_chdir((char *)buf))
2530 retval = FAIL;
2531 else
2532 fname = p + 1;
2533 *buf = NUL;
2534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 }
2536 }
2537 if (mch_dirname(buf, len) == FAIL)
2538 {
2539 retval = FAIL;
2540 *buf = NUL;
2541 }
2542 if (p != NULL)
2543 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002544#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 if (fd >= 0)
2546 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002547 if (p_verbose >= 5)
2548 {
2549 verbose_enter();
2550 MSG("fchdir() to previous dir");
2551 verbose_leave();
2552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 l = fchdir(fd);
2554 close(fd);
2555 }
2556 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 l = mch_chdir((char *)olddir);
2559 if (l != 0)
2560 EMSG(_(e_prev_dir));
2561 }
2562
2563 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002564 if (l >= len - 1)
2565 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002566#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002567 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002569 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002572
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002574 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 return FAIL;
2576
2577 /* Do not append ".", "/dir/." is equal to "/dir". */
2578 if (STRCMP(fname, ".") != 0)
2579 STRCAT(buf, fname);
2580
2581 return OK;
2582}
2583
2584/*
2585 * Return TRUE if "fname" does not depend on the current directory.
2586 */
2587 int
2588mch_isFullName(fname)
2589 char_u *fname;
2590{
2591#ifdef __EMX__
2592 return _fnisabs(fname);
2593#else
2594# ifdef VMS
2595 return ( fname[0] == '/' || fname[0] == '.' ||
2596 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2597 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2598 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
2599# else
2600 return (*fname == '/' || *fname == '~');
2601# endif
2602#endif
2603}
2604
Bram Moolenaar24552be2005-12-10 20:17:30 +00002605#if defined(USE_FNAME_CASE) || defined(PROTO)
2606/*
2607 * Set the case of the file name, if it already exists. This will cause the
2608 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002609 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002610 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002611 void
2612fname_case(name, len)
2613 char_u *name;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002614 int len UNUSED; /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002615{
2616 struct stat st;
2617 char_u *slash, *tail;
2618 DIR *dirp;
2619 struct dirent *dp;
2620
2621 if (lstat((char *)name, &st) >= 0)
2622 {
2623 /* Open the directory where the file is located. */
2624 slash = vim_strrchr(name, '/');
2625 if (slash == NULL)
2626 {
2627 dirp = opendir(".");
2628 tail = name;
2629 }
2630 else
2631 {
2632 *slash = NUL;
2633 dirp = opendir((char *)name);
2634 *slash = '/';
2635 tail = slash + 1;
2636 }
2637
2638 if (dirp != NULL)
2639 {
2640 while ((dp = readdir(dirp)) != NULL)
2641 {
2642 /* Only accept names that differ in case and are the same byte
2643 * length. TODO: accept different length name. */
2644 if (STRICMP(tail, dp->d_name) == 0
2645 && STRLEN(tail) == STRLEN(dp->d_name))
2646 {
2647 char_u newname[MAXPATHL + 1];
2648 struct stat st2;
2649
2650 /* Verify the inode is equal. */
2651 vim_strncpy(newname, name, MAXPATHL);
2652 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2653 MAXPATHL - (tail - name));
2654 if (lstat((char *)newname, &st2) >= 0
2655 && st.st_ino == st2.st_ino
2656 && st.st_dev == st2.st_dev)
2657 {
2658 STRCPY(tail, dp->d_name);
2659 break;
2660 }
2661 }
2662 }
2663
2664 closedir(dirp);
2665 }
2666 }
2667}
2668#endif
2669
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670/*
2671 * Get file permissions for 'name'.
2672 * Returns -1 when it doesn't exist.
2673 */
2674 long
2675mch_getperm(name)
2676 char_u *name;
2677{
2678 struct stat statb;
2679
2680 /* Keep the #ifdef outside of stat(), it may be a macro. */
2681#ifdef VMS
2682 if (stat((char *)vms_fixfilename(name), &statb))
2683#else
2684 if (stat((char *)name, &statb))
2685#endif
2686 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002687#ifdef __INTERIX
2688 /* The top bit makes the value negative, which means the file doesn't
2689 * exist. Remove the bit, we don't use it. */
2690 return statb.st_mode & ~S_ADDACE;
2691#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694}
2695
2696/*
2697 * set file permission for 'name' to 'perm'
2698 *
2699 * return FAIL for failure, OK otherwise
2700 */
2701 int
2702mch_setperm(name, perm)
2703 char_u *name;
2704 long perm;
2705{
2706 return (chmod((char *)
2707#ifdef VMS
2708 vms_fixfilename(name),
2709#else
2710 name,
2711#endif
2712 (mode_t)perm) == 0 ? OK : FAIL);
2713}
2714
2715#if defined(HAVE_ACL) || defined(PROTO)
2716# ifdef HAVE_SYS_ACL_H
2717# include <sys/acl.h>
2718# endif
2719# ifdef HAVE_SYS_ACCESS_H
2720# include <sys/access.h>
2721# endif
2722
2723# ifdef HAVE_SOLARIS_ACL
2724typedef struct vim_acl_solaris_T {
2725 int acl_cnt;
2726 aclent_t *acl_entry;
2727} vim_acl_solaris_T;
2728# endif
2729
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002730#if defined(HAVE_SELINUX) || defined(PROTO)
2731/*
2732 * Copy security info from "from_file" to "to_file".
2733 */
2734 void
2735mch_copy_sec(from_file, to_file)
2736 char_u *from_file;
2737 char_u *to_file;
2738{
2739 if (from_file == NULL)
2740 return;
2741
2742 if (selinux_enabled == -1)
2743 selinux_enabled = is_selinux_enabled();
2744
2745 if (selinux_enabled > 0)
2746 {
2747 security_context_t from_context = NULL;
2748 security_context_t to_context = NULL;
2749
2750 if (getfilecon((char *)from_file, &from_context) < 0)
2751 {
2752 /* If the filesystem doesn't support extended attributes,
2753 the original had no special security context and the
2754 target cannot have one either. */
2755 if (errno == EOPNOTSUPP)
2756 return;
2757
2758 MSG_PUTS(_("\nCould not get security context for "));
2759 msg_outtrans(from_file);
2760 msg_putchar('\n');
2761 return;
2762 }
2763 if (getfilecon((char *)to_file, &to_context) < 0)
2764 {
2765 MSG_PUTS(_("\nCould not get security context for "));
2766 msg_outtrans(to_file);
2767 msg_putchar('\n');
2768 freecon (from_context);
2769 return ;
2770 }
2771 if (strcmp(from_context, to_context) != 0)
2772 {
2773 if (setfilecon((char *)to_file, from_context) < 0)
2774 {
2775 MSG_PUTS(_("\nCould not set security context for "));
2776 msg_outtrans(to_file);
2777 msg_putchar('\n');
2778 }
2779 }
2780 freecon(to_context);
2781 freecon(from_context);
2782 }
2783}
2784#endif /* HAVE_SELINUX */
2785
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002786#if defined(HAVE_SMACK) && !defined(PROTO)
2787/*
2788 * Copy security info from "from_file" to "to_file".
2789 */
2790 void
2791mch_copy_sec(from_file, to_file)
2792 char_u *from_file;
2793 char_u *to_file;
2794{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002795 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002796 {
2797 XATTR_NAME_SMACK,
2798 XATTR_NAME_SMACKEXEC,
2799 XATTR_NAME_SMACKMMAP
2800 };
2801
2802 char buffer[SMACK_LABEL_LEN];
2803 const char *name;
2804 int index;
2805 int ret;
2806 ssize_t size;
2807
2808 if (from_file == NULL)
2809 return;
2810
2811 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2812 / sizeof(smack_copied_attributes)[0]) ; index++)
2813 {
2814 /* get the name of the attribute to copy */
2815 name = smack_copied_attributes[index];
2816
2817 /* get the value of the attribute in buffer */
2818 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2819 if (size >= 0)
2820 {
2821 /* copy the attribute value of buffer */
2822 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2823 if (ret < 0)
2824 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002825 vim_snprintf((char *)IObuff, IOSIZE,
2826 _("Could not set security context %s for %s"),
2827 name, to_file);
2828 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002829 msg_putchar('\n');
2830 }
2831 }
2832 else
2833 {
2834 /* what reason of not having the attribute value? */
2835 switch (errno)
2836 {
2837 case ENOTSUP:
2838 /* extended attributes aren't supported or enabled */
2839 /* should a message be echoed? not sure... */
2840 return; /* leave because it isn't usefull to continue */
2841
2842 case ERANGE:
2843 default:
2844 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002845 vim_snprintf((char *)IObuff, IOSIZE,
2846 _("Could not get security context %s for %s. Removing it!"),
2847 name, from_file);
2848 msg_puts(IObuff);
2849 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002850 /* FALLTHROUGH to remove the attribute */
2851
2852 case ENODATA:
2853 /* no attribute of this name */
2854 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002855 /* Silently ignore errors, apparently this happens when
2856 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002857 break;
2858 }
2859 }
2860 }
2861}
2862#endif /* HAVE_SMACK */
2863
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864/*
2865 * Return a pointer to the ACL of file "fname" in allocated memory.
2866 * Return NULL if the ACL is not available for whatever reason.
2867 */
2868 vim_acl_T
2869mch_get_acl(fname)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002870 char_u *fname UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871{
2872 vim_acl_T ret = NULL;
2873#ifdef HAVE_POSIX_ACL
2874 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2875#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002876#ifdef HAVE_SOLARIS_ZFS_ACL
2877 acl_t *aclent;
2878
2879 if (acl_get((char *)fname, 0, &aclent) < 0)
2880 return NULL;
2881 ret = (vim_acl_T)aclent;
2882#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883#ifdef HAVE_SOLARIS_ACL
2884 vim_acl_solaris_T *aclent;
2885
2886 aclent = malloc(sizeof(vim_acl_solaris_T));
2887 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2888 {
2889 free(aclent);
2890 return NULL;
2891 }
2892 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2893 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2894 {
2895 free(aclent->acl_entry);
2896 free(aclent);
2897 return NULL;
2898 }
2899 ret = (vim_acl_T)aclent;
2900#else
2901#if defined(HAVE_AIX_ACL)
2902 int aclsize;
2903 struct acl *aclent;
2904
2905 aclsize = sizeof(struct acl);
2906 aclent = malloc(aclsize);
2907 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2908 {
2909 if (errno == ENOSPC)
2910 {
2911 aclsize = aclent->acl_len;
2912 aclent = realloc(aclent, aclsize);
2913 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2914 {
2915 free(aclent);
2916 return NULL;
2917 }
2918 }
2919 else
2920 {
2921 free(aclent);
2922 return NULL;
2923 }
2924 }
2925 ret = (vim_acl_T)aclent;
2926#endif /* HAVE_AIX_ACL */
2927#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002928#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929#endif /* HAVE_POSIX_ACL */
2930 return ret;
2931}
2932
2933/*
2934 * Set the ACL of file "fname" to "acl" (unless it's NULL).
2935 */
2936 void
2937mch_set_acl(fname, aclent)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002938 char_u *fname UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 vim_acl_T aclent;
2940{
2941 if (aclent == NULL)
2942 return;
2943#ifdef HAVE_POSIX_ACL
2944 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
2945#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002946#ifdef HAVE_SOLARIS_ZFS_ACL
2947 acl_set((char *)fname, (acl_t *)aclent);
2948#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949#ifdef HAVE_SOLARIS_ACL
2950 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
2951 ((vim_acl_solaris_T *)aclent)->acl_entry);
2952#else
2953#ifdef HAVE_AIX_ACL
2954 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
2955#endif /* HAVE_AIX_ACL */
2956#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002957#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958#endif /* HAVE_POSIX_ACL */
2959}
2960
2961 void
2962mch_free_acl(aclent)
2963 vim_acl_T aclent;
2964{
2965 if (aclent == NULL)
2966 return;
2967#ifdef HAVE_POSIX_ACL
2968 acl_free((acl_t)aclent);
2969#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002970#ifdef HAVE_SOLARIS_ZFS_ACL
2971 acl_free((acl_t *)aclent);
2972#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973#ifdef HAVE_SOLARIS_ACL
2974 free(((vim_acl_solaris_T *)aclent)->acl_entry);
2975 free(aclent);
2976#else
2977#ifdef HAVE_AIX_ACL
2978 free(aclent);
2979#endif /* HAVE_AIX_ACL */
2980#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002981#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982#endif /* HAVE_POSIX_ACL */
2983}
2984#endif
2985
2986/*
2987 * Set hidden flag for "name".
2988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 void
2990mch_hide(name)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002991 char_u *name UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 /* can't hide a file */
2994}
2995
2996/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01002997 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 * return FALSE if "name" is not a directory
2999 * return FALSE for error
3000 */
3001 int
3002mch_isdir(name)
3003 char_u *name;
3004{
3005 struct stat statb;
3006
3007 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3008 return FALSE;
3009 if (stat((char *)name, &statb))
3010 return FALSE;
3011#ifdef _POSIX_SOURCE
3012 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3013#else
3014 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3015#endif
3016}
3017
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003018/*
3019 * return TRUE if "name" is a directory, NOT a symlink to a directory
3020 * return FALSE if "name" is not a directory
3021 * return FALSE for error
3022 */
3023 int
3024mch_isrealdir(name)
3025 char_u *name;
3026{
3027 struct stat statb;
3028
3029 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3030 return FALSE;
3031 if (lstat((char *)name, &statb))
3032 return FALSE;
3033#ifdef _POSIX_SOURCE
3034 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3035#else
3036 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3037#endif
3038}
3039
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003040static int executable_file(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
3042/*
3043 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3044 */
3045 static int
3046executable_file(name)
3047 char_u *name;
3048{
3049 struct stat st;
3050
3051 if (stat((char *)name, &st))
3052 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003053#ifdef VMS
3054 /* Like on Unix system file can have executable rights but not necessarily
3055 * be an executable, but on Unix is not a default for an ordianry file to
3056 * have an executable flag - on VMS it is in most cases.
3057 * Therefore, this check does not have any sense - let keep us to the
3058 * conventions instead:
3059 * *.COM and *.EXE files are the executables - the rest are not. This is
3060 * not ideal but better then it was.
3061 */
3062 int vms_executable = 0;
3063 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3064 {
3065 if (strstr(vms_tolower((char*)name),".exe") != NULL
3066 || strstr(vms_tolower((char*)name),".com")!= NULL)
3067 vms_executable = 1;
3068 }
3069 return vms_executable;
3070#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073}
3074
3075/*
3076 * Return 1 if "name" can be found in $PATH and executed, 0 if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003077 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 * Return -1 if unknown.
3079 */
3080 int
Bram Moolenaarb5971142015-03-21 17:32:19 +01003081mch_can_exe(name, path, use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 char_u *name;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003083 char_u **path;
Bram Moolenaarb5971142015-03-21 17:32:19 +01003084 int use_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
3086 char_u *buf;
3087 char_u *p, *e;
3088 int retval;
3089
Bram Moolenaarb5971142015-03-21 17:32:19 +01003090 /* When "use_path" is false and if it's an absolute or relative path don't
3091 * need to use $PATH. */
3092 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3093 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003094 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003095 /* There must be a path separator, files in the current directory
3096 * can't be executed. */
3097 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003098 {
3099 if (path != NULL)
3100 {
3101 if (name[0] == '.')
3102 *path = FullName_save(name, TRUE);
3103 else
3104 *path = vim_strsave(name);
3105 }
3106 return TRUE;
3107 }
3108 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110
3111 p = (char_u *)getenv("PATH");
3112 if (p == NULL || *p == NUL)
3113 return -1;
3114 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3115 if (buf == NULL)
3116 return -1;
3117
3118 /*
3119 * Walk through all entries in $PATH to check if "name" exists there and
3120 * is an executable file.
3121 */
3122 for (;;)
3123 {
3124 e = (char_u *)strchr((char *)p, ':');
3125 if (e == NULL)
3126 e = p + STRLEN(p);
3127 if (e - p <= 1) /* empty entry means current dir */
3128 STRCPY(buf, "./");
3129 else
3130 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003131 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 add_pathsep(buf);
3133 }
3134 STRCAT(buf, name);
3135 retval = executable_file(buf);
3136 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003137 {
3138 if (path != NULL)
3139 {
3140 if (buf[0] == '.')
3141 *path = FullName_save(buf, TRUE);
3142 else
3143 *path = vim_strsave(buf);
3144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
3148 if (*e != ':')
3149 break;
3150 p = e + 1;
3151 }
3152
3153 vim_free(buf);
3154 return retval;
3155}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
3157/*
3158 * Check what "name" is:
3159 * NODE_NORMAL: file or directory (or doesn't exist)
3160 * NODE_WRITABLE: writable device, socket, fifo, etc.
3161 * NODE_OTHER: non-writable things
3162 */
3163 int
3164mch_nodetype(name)
3165 char_u *name;
3166{
3167 struct stat st;
3168
3169 if (stat((char *)name, &st))
3170 return NODE_NORMAL;
3171 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3172 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3174 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 /* Everything else is writable? */
3176 return NODE_WRITABLE;
3177}
3178
3179 void
3180mch_early_init()
3181{
3182#ifdef HAVE_CHECK_STACK_GROWTH
3183 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 check_stack_growth((char *)&i);
3186
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003187# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 get_stack_limit();
3189# endif
3190
3191#endif
3192
3193 /*
3194 * Setup an alternative stack for signals. Helps to catch signals when
3195 * running out of stack space.
3196 * Use of sigaltstack() is preferred, it's more portable.
3197 * Ignore any errors.
3198 */
3199#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003200 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 init_signal_stack();
3202#endif
3203}
3204
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003205#if defined(EXITFREE) || defined(PROTO)
3206 void
3207mch_free_mem()
3208{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003209# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3210 if (clip_star.owned)
3211 clip_lose_selection(&clip_star);
3212 if (clip_plus.owned)
3213 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003214# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003215# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003216 if (xterm_Shell != (Widget)0)
3217 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003218# ifndef LESSTIF_VERSION
3219 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003220 if (xterm_dpy != NULL)
3221 XtCloseDisplay(xterm_dpy);
3222 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003223 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003224 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003225# ifdef FEAT_X11
3226 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3227# endif
3228 }
3229# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003230# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003231# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003232 if (x11_display != NULL
3233# ifdef FEAT_XCLIPBOARD
3234 && x11_display != xterm_dpy
3235# endif
3236 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003237 XCloseDisplay(x11_display);
3238# endif
3239# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
3240 vim_free(signal_stack);
3241 signal_stack = NULL;
3242# endif
3243# ifdef FEAT_TITLE
3244 vim_free(oldtitle);
3245 vim_free(oldicon);
3246# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003247}
3248#endif
3249
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003250static void exit_scroll(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
3252/*
3253 * Output a newline when exiting.
3254 * Make sure the newline goes to the same stream as the text.
3255 */
3256 static void
3257exit_scroll()
3258{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003259 if (silent_mode)
3260 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 if (newline_on_exit || msg_didout)
3262 {
3263 if (msg_use_printf())
3264 {
3265 if (info_message)
3266 mch_msg("\n");
3267 else
3268 mch_errmsg("\r\n");
3269 }
3270 else
3271 out_char('\n');
3272 }
3273 else
3274 {
3275 restore_cterm_colors(); /* get original colors back */
3276 msg_clr_eos_force(); /* clear the rest of the display */
3277 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3278 }
3279}
3280
3281 void
3282mch_exit(r)
3283 int r;
3284{
3285 exiting = TRUE;
3286
3287#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3288 x11_export_final_selection();
3289#endif
3290
3291#ifdef FEAT_GUI
3292 if (!gui.in_use)
3293#endif
3294 {
3295 settmode(TMODE_COOK);
3296#ifdef FEAT_TITLE
3297 mch_restore_title(3); /* restore xterm title and icon name */
3298#endif
3299 /*
3300 * When t_ti is not empty but it doesn't cause swapping terminal
3301 * pages, need to output a newline when msg_didout is set. But when
3302 * t_ti does swap pages it should not go to the shell page. Do this
3303 * before stoptermcap().
3304 */
3305 if (swapping_screen() && !newline_on_exit)
3306 exit_scroll();
3307
3308 /* Stop termcap: May need to check for T_CRV response, which
3309 * requires RAW mode. */
3310 stoptermcap();
3311
3312 /*
3313 * A newline is only required after a message in the alternate screen.
3314 * This is set to TRUE by wait_return().
3315 */
3316 if (!swapping_screen() || newline_on_exit)
3317 exit_scroll();
3318
3319 /* Cursor may have been switched off without calling starttermcap()
3320 * when doing "vim -u vimrc" and vimrc contains ":q". */
3321 if (full_screen)
3322 cursor_on();
3323 }
3324 out_flush();
3325 ml_close_all(TRUE); /* remove all memfiles */
3326 may_core_dump();
3327#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 gui_exit(r);
3330#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003331
Bram Moolenaar56718732006-03-15 22:53:57 +00003332#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003333 mac_conv_cleanup();
3334#endif
3335
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336#ifdef __QNX__
3337 /* A core dump won't be created if the signal handler
3338 * doesn't return, so we can't call exit() */
3339 if (deadly_signal != 0)
3340 return;
3341#endif
3342
Bram Moolenaar009b2592004-10-24 19:18:58 +00003343#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003344 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003345#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003346
3347#ifdef EXITFREE
3348 free_all_mem();
3349#endif
3350
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 exit(r);
3352}
3353
3354 static void
3355may_core_dump()
3356{
3357 if (deadly_signal != 0)
3358 {
3359 signal(deadly_signal, SIG_DFL);
3360 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3361 }
3362}
3363
3364#ifndef VMS
3365
3366 void
3367mch_settmode(tmode)
3368 int tmode;
3369{
3370 static int first = TRUE;
3371
3372 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3373#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3374 /*
3375 * for "new" tty systems
3376 */
3377# ifdef HAVE_TERMIOS_H
3378 static struct termios told;
3379 struct termios tnew;
3380# else
3381 static struct termio told;
3382 struct termio tnew;
3383# endif
3384
3385 if (first)
3386 {
3387 first = FALSE;
3388# if defined(HAVE_TERMIOS_H)
3389 tcgetattr(read_cmd_fd, &told);
3390# else
3391 ioctl(read_cmd_fd, TCGETA, &told);
3392# endif
3393 }
3394
3395 tnew = told;
3396 if (tmode == TMODE_RAW)
3397 {
3398 /*
3399 * ~ICRNL enables typing ^V^M
3400 */
3401 tnew.c_iflag &= ~ICRNL;
3402 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3403# if defined(IEXTEN) && !defined(__MINT__)
3404 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3405 /* but it breaks function keys on MINT */
3406# endif
3407 );
3408# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3409 tnew.c_oflag &= ~ONLCR;
3410# endif
3411 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3412 tnew.c_cc[VTIME] = 0; /* don't wait */
3413 }
3414 else if (tmode == TMODE_SLEEP)
3415 tnew.c_lflag &= ~(ECHO);
3416
3417# if defined(HAVE_TERMIOS_H)
3418 {
3419 int n = 10;
3420
3421 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3422 * few times. */
3423 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3424 && errno == EINTR && n > 0)
3425 --n;
3426 }
3427# else
3428 ioctl(read_cmd_fd, TCSETA, &tnew);
3429# endif
3430
3431#else
3432
3433 /*
3434 * for "old" tty systems
3435 */
3436# ifndef TIOCSETN
3437# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3438# endif
3439 static struct sgttyb ttybold;
3440 struct sgttyb ttybnew;
3441
3442 if (first)
3443 {
3444 first = FALSE;
3445 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3446 }
3447
3448 ttybnew = ttybold;
3449 if (tmode == TMODE_RAW)
3450 {
3451 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3452 ttybnew.sg_flags |= RAW;
3453 }
3454 else if (tmode == TMODE_SLEEP)
3455 ttybnew.sg_flags &= ~(ECHO);
3456 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3457#endif
3458 curr_tmode = tmode;
3459}
3460
3461/*
3462 * Try to get the code for "t_kb" from the stty setting
3463 *
3464 * Even if termcap claims a backspace key, the user's setting *should*
3465 * prevail. stty knows more about reality than termcap does, and if
3466 * somebody's usual erase key is DEL (which, for most BSD users, it will
3467 * be), they're going to get really annoyed if their erase key starts
3468 * doing forward deletes for no reason. (Eric Fischer)
3469 */
3470 void
3471get_stty()
3472{
3473 char_u buf[2];
3474 char_u *p;
3475
3476 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3477#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3478 /* for "new" tty systems */
3479# ifdef HAVE_TERMIOS_H
3480 struct termios keys;
3481# else
3482 struct termio keys;
3483# endif
3484
3485# if defined(HAVE_TERMIOS_H)
3486 if (tcgetattr(read_cmd_fd, &keys) != -1)
3487# else
3488 if (ioctl(read_cmd_fd, TCGETA, &keys) != -1)
3489# endif
3490 {
3491 buf[0] = keys.c_cc[VERASE];
3492 intr_char = keys.c_cc[VINTR];
3493#else
3494 /* for "old" tty systems */
3495 struct sgttyb keys;
3496
3497 if (ioctl(read_cmd_fd, TIOCGETP, &keys) != -1)
3498 {
3499 buf[0] = keys.sg_erase;
3500 intr_char = keys.sg_kill;
3501#endif
3502 buf[1] = NUL;
3503 add_termcode((char_u *)"kb", buf, FALSE);
3504
3505 /*
3506 * If <BS> and <DEL> are now the same, redefine <DEL>.
3507 */
3508 p = find_termcode((char_u *)"kD");
3509 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3510 do_fixdel(NULL);
3511 }
3512#if 0
3513 } /* to keep cindent happy */
3514#endif
3515}
3516
3517#endif /* VMS */
3518
3519#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
3520/*
3521 * Set mouse clicks on or off.
3522 */
3523 void
3524mch_setmouse(on)
3525 int on;
3526{
3527 static int ison = FALSE;
3528 int xterm_mouse_vers;
3529
3530 if (on == ison) /* return quickly if nothing to do */
3531 return;
3532
3533 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003534
3535# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003536 if (ttym_flags == TTYM_URXVT)
3537 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003538 out_str_nf((char_u *)
3539 (on
3540 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3541 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
3542 ison = on;
3543 }
3544# endif
3545
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003546# ifdef FEAT_MOUSE_SGR
3547 if (ttym_flags == TTYM_SGR)
3548 {
3549 out_str_nf((char_u *)
3550 (on
3551 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3552 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
3553 ison = on;
3554 }
3555# endif
3556
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 if (xterm_mouse_vers > 0)
3558 {
3559 if (on) /* enable mouse events, use mouse tracking if available */
3560 out_str_nf((char_u *)
3561 (xterm_mouse_vers > 1
3562 ? IF_EB("\033[?1002h", ESC_STR "[?1002h")
3563 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3564 else /* disable mouse events, could probably always send the same */
3565 out_str_nf((char_u *)
3566 (xterm_mouse_vers > 1
3567 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3568 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
3569 ison = on;
3570 }
3571
3572# ifdef FEAT_MOUSE_DEC
3573 else if (ttym_flags == TTYM_DEC)
3574 {
3575 if (on) /* enable mouse events */
3576 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3577 else /* disable mouse events */
3578 out_str_nf((char_u *)"\033['z");
3579 ison = on;
3580 }
3581# endif
3582
3583# ifdef FEAT_MOUSE_GPM
3584 else
3585 {
3586 if (on)
3587 {
3588 if (gpm_open())
3589 ison = TRUE;
3590 }
3591 else
3592 {
3593 gpm_close();
3594 ison = FALSE;
3595 }
3596 }
3597# endif
3598
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003599# ifdef FEAT_SYSMOUSE
3600 else
3601 {
3602 if (on)
3603 {
3604 if (sysmouse_open() == OK)
3605 ison = TRUE;
3606 }
3607 else
3608 {
3609 sysmouse_close();
3610 ison = FALSE;
3611 }
3612 }
3613# endif
3614
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615# ifdef FEAT_MOUSE_JSB
3616 else
3617 {
3618 if (on)
3619 {
3620 /* D - Enable Mouse up/down messages
3621 * L - Enable Left Button Reporting
3622 * M - Enable Middle Button Reporting
3623 * R - Enable Right Button Reporting
3624 * K - Enable SHIFT and CTRL key Reporting
3625 * + - Enable Advanced messaging of mouse moves and up/down messages
3626 * Q - Quiet No Ack
3627 * # - Numeric value of mouse pointer required
3628 * 0 = Multiview 2000 cursor, used as standard
3629 * 1 = Windows Arrow
3630 * 2 = Windows I Beam
3631 * 3 = Windows Hour Glass
3632 * 4 = Windows Cross Hair
3633 * 5 = Windows UP Arrow
3634 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003635# ifdef JSBTERM_MOUSE_NONADVANCED
3636 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3638 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003639# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3641 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003642# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 ison = TRUE;
3644 }
3645 else
3646 {
3647 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3648 ESC_STR "[0~ZwQ" ESC_STR "\\"));
3649 ison = FALSE;
3650 }
3651 }
3652# endif
3653# ifdef FEAT_MOUSE_PTERM
3654 else
3655 {
3656 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3657 if (on)
3658 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3659 else
3660 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
3661 ison = on;
3662 }
3663# endif
3664}
3665
3666/*
3667 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3668 */
3669 void
3670check_mouse_termcode()
3671{
3672# ifdef FEAT_MOUSE_XTERM
3673 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003674# ifdef FEAT_MOUSE_URXVT
3675 && use_xterm_mouse() != 3
3676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677# ifdef FEAT_GUI
3678 && !gui.in_use
3679# endif
3680 )
3681 {
3682 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003683 ? IF_EB("\233M", CSI_STR "M")
3684 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 if (*p_mouse != NUL)
3686 {
3687 /* force mouse off and maybe on to send possibly new mouse
3688 * activation sequence to the xterm, with(out) drag tracing. */
3689 mch_setmouse(FALSE);
3690 setmouse();
3691 }
3692 }
3693 else
3694 del_mouse_termcode(KS_MOUSE);
3695# endif
3696
3697# ifdef FEAT_MOUSE_GPM
3698 if (!use_xterm_mouse()
3699# ifdef FEAT_GUI
3700 && !gui.in_use
3701# endif
3702 )
3703 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3704# endif
3705
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003706# ifdef FEAT_SYSMOUSE
3707 if (!use_xterm_mouse()
3708# ifdef FEAT_GUI
3709 && !gui.in_use
3710# endif
3711 )
3712 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3713# endif
3714
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003716 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 if (!use_xterm_mouse()
3718# ifdef FEAT_GUI
3719 && !gui.in_use
3720# endif
3721 )
3722 set_mouse_termcode(KS_JSBTERM_MOUSE,
3723 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3724 else
3725 del_mouse_termcode(KS_JSBTERM_MOUSE);
3726# endif
3727
3728# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003729 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 * define it in the GUI or when using an xterm. */
3731 if (!use_xterm_mouse()
3732# ifdef FEAT_GUI
3733 && !gui.in_use
3734# endif
3735 )
3736 set_mouse_termcode(KS_NETTERM_MOUSE,
3737 (char_u *)IF_EB("\033}", ESC_STR "}"));
3738 else
3739 del_mouse_termcode(KS_NETTERM_MOUSE);
3740# endif
3741
3742# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003743 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003744 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745# ifdef FEAT_GUI
3746 && !gui.in_use
3747# endif
3748 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003749 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3750 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 else
3752 del_mouse_termcode(KS_DEC_MOUSE);
3753# endif
3754# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003755 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003756 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757# ifdef FEAT_GUI
3758 && !gui.in_use
3759# endif
3760 )
3761 set_mouse_termcode(KS_PTERM_MOUSE,
3762 (char_u *) IF_EB("\033[", ESC_STR "["));
3763 else
3764 del_mouse_termcode(KS_PTERM_MOUSE);
3765# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003766# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003767 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003768 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003769# ifdef FEAT_GUI
3770 && !gui.in_use
3771# endif
3772 )
3773 {
3774 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3775 ? IF_EB("\233", CSI_STR)
3776 : IF_EB("\033[", ESC_STR "[")));
3777
3778 if (*p_mouse != NUL)
3779 {
3780 mch_setmouse(FALSE);
3781 setmouse();
3782 }
3783 }
3784 else
3785 del_mouse_termcode(KS_URXVT_MOUSE);
3786# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003787# ifdef FEAT_MOUSE_SGR
Bram Moolenaar24dc2302014-05-13 20:19:58 +02003788 /* There is no conflict with xterm mouse */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003789 if (use_xterm_mouse() == 4
3790# ifdef FEAT_GUI
3791 && !gui.in_use
3792# endif
3793 )
3794 {
3795 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3796 ? IF_EB("\233<", CSI_STR "<")
3797 : IF_EB("\033[<", ESC_STR "[<")));
3798
3799 if (*p_mouse != NUL)
3800 {
3801 mch_setmouse(FALSE);
3802 setmouse();
3803 }
3804 }
3805 else
3806 del_mouse_termcode(KS_SGR_MOUSE);
3807# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808}
3809#endif
3810
3811/*
3812 * set screen mode, always fails.
3813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 int
3815mch_screenmode(arg)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003816 char_u *arg UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817{
3818 EMSG(_(e_screenmode));
3819 return FAIL;
3820}
3821
3822#ifndef VMS
3823
3824/*
3825 * Try to get the current window size:
3826 * 1. with an ioctl(), most accurate method
3827 * 2. from the environment variables LINES and COLUMNS
3828 * 3. from the termcap
3829 * 4. keep using the old values
3830 * Return OK when size could be determined, FAIL otherwise.
3831 */
3832 int
3833mch_get_shellsize()
3834{
3835 long rows = 0;
3836 long columns = 0;
3837 char_u *p;
3838
3839 /*
3840 * For OS/2 use _scrsize().
3841 */
3842# ifdef __EMX__
3843 {
3844 int s[2];
3845
3846 _scrsize(s);
3847 columns = s[0];
3848 rows = s[1];
3849 }
3850# endif
3851
3852 /*
3853 * 1. try using an ioctl. It is the most accurate method.
3854 *
3855 * Try using TIOCGWINSZ first, some systems that have it also define
3856 * TIOCGSIZE but don't have a struct ttysize.
3857 */
3858# ifdef TIOCGWINSZ
3859 {
3860 struct winsize ws;
3861 int fd = 1;
3862
3863 /* When stdout is not a tty, use stdin for the ioctl(). */
3864 if (!isatty(fd) && isatty(read_cmd_fd))
3865 fd = read_cmd_fd;
3866 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
3867 {
3868 columns = ws.ws_col;
3869 rows = ws.ws_row;
3870 }
3871 }
3872# else /* TIOCGWINSZ */
3873# ifdef TIOCGSIZE
3874 {
3875 struct ttysize ts;
3876 int fd = 1;
3877
3878 /* When stdout is not a tty, use stdin for the ioctl(). */
3879 if (!isatty(fd) && isatty(read_cmd_fd))
3880 fd = read_cmd_fd;
3881 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
3882 {
3883 columns = ts.ts_cols;
3884 rows = ts.ts_lines;
3885 }
3886 }
3887# endif /* TIOCGSIZE */
3888# endif /* TIOCGWINSZ */
3889
3890 /*
3891 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003892 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
3893 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003895 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
3897 if ((p = (char_u *)getenv("LINES")))
3898 rows = atoi((char *)p);
3899 if ((p = (char_u *)getenv("COLUMNS")))
3900 columns = atoi((char *)p);
3901 }
3902
3903#ifdef HAVE_TGETENT
3904 /*
3905 * 3. try reading "co" and "li" entries from termcap
3906 */
3907 if (columns == 0 || rows == 0)
3908 getlinecol(&columns, &rows);
3909#endif
3910
3911 /*
3912 * 4. If everything fails, use the old values
3913 */
3914 if (columns <= 0 || rows <= 0)
3915 return FAIL;
3916
3917 Rows = rows;
3918 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02003919 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 return OK;
3921}
3922
3923/*
3924 * Try to set the window size to Rows and Columns.
3925 */
3926 void
3927mch_set_shellsize()
3928{
3929 if (*T_CWS)
3930 {
3931 /*
3932 * NOTE: if you get an error here that term_set_winsize() is
3933 * undefined, check the output of configure. It could probably not
3934 * find a ncurses, termcap or termlib library.
3935 */
3936 term_set_winsize((int)Rows, (int)Columns);
3937 out_flush();
3938 screen_start(); /* don't know where cursor is now */
3939 }
3940}
3941
3942#endif /* VMS */
3943
3944/*
3945 * Rows and/or Columns has changed.
3946 */
3947 void
3948mch_new_shellsize()
3949{
3950 /* Nothing to do. */
3951}
3952
Bram Moolenaar205b8862011-09-07 15:04:31 +02003953/*
3954 * Wait for process "child" to end.
3955 * Return "child" if it exited properly, <= 0 on error.
3956 */
3957 static pid_t
3958wait4pid(child, status)
3959 pid_t child;
3960 waitstatus *status;
3961{
3962 pid_t wait_pid = 0;
3963
3964 while (wait_pid != child)
3965 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02003966 /* When compiled with Python threads are probably used, in which case
3967 * wait() sometimes hangs for no obvious reason. Use waitpid()
3968 * instead and loop (like the GUI). Also needed for other interfaces,
3969 * they might call system(). */
3970# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02003971 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02003972# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02003973 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02003974# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02003975 if (wait_pid == 0)
3976 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003977 /* Wait for 10 msec before trying again. */
Bram Moolenaar205b8862011-09-07 15:04:31 +02003978 mch_delay(10L, TRUE);
3979 continue;
3980 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02003981 if (wait_pid <= 0
3982# ifdef ECHILD
3983 && errno == ECHILD
3984# endif
3985 )
3986 break;
3987 }
3988 return wait_pid;
3989}
3990
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 int
3992mch_call_shell(cmd, options)
3993 char_u *cmd;
3994 int options; /* SHELL_*, see vim.h */
3995{
3996#ifdef VMS
3997 char *ifn = NULL;
3998 char *ofn = NULL;
3999#endif
4000 int tmode = cur_tmode;
4001#ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */
4002 int x;
4003# ifndef __EMX__
4004 char_u *newcmd; /* only needed for unix */
4005# else
4006 /*
4007 * Set the preferred shell in the EMXSHELL environment variable (but
4008 * only if it is different from what is already in the environment).
4009 * Emx then takes care of whether to use "/c" or "-c" in an
4010 * intelligent way. Simply pass the whole thing to emx's system() call.
4011 * Emx also starts an interactive shell if system() is passed an empty
4012 * string.
4013 */
4014 char_u *p, *old;
4015
4016 if (((old = (char_u *)getenv("EMXSHELL")) == NULL) || STRCMP(old, p_sh))
4017 {
4018 /* should check HAVE_SETENV, but I know we don't have it. */
4019 p = alloc(10 + strlen(p_sh));
4020 if (p)
4021 {
4022 sprintf((char *)p, "EMXSHELL=%s", p_sh);
4023 putenv((char *)p); /* don't free the pointer! */
4024 }
4025 }
4026# endif
4027
4028 out_flush();
4029
4030 if (options & SHELL_COOKED)
4031 settmode(TMODE_COOK); /* set to normal mode */
4032
Bram Moolenaar62b42182010-09-21 22:09:37 +02004033# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004034 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004035 loose_clipboard();
4036# endif
4037
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038# ifdef __EMX__
4039 if (cmd == NULL)
4040 x = system(""); /* this starts an interactive shell in emx */
4041 else
4042 x = system((char *)cmd);
4043 /* system() returns -1 when error occurs in starting shell */
4044 if (x == -1 && !emsg_silent)
4045 {
4046 MSG_PUTS(_("\nCannot execute shell "));
4047 msg_outtrans(p_sh);
4048 msg_putchar('\n');
4049 }
4050# else /* not __EMX__ */
4051 if (cmd == NULL)
4052 x = system((char *)p_sh);
4053 else
4054 {
4055# ifdef VMS
4056 if (ofn = strchr((char *)cmd, '>'))
4057 *ofn++ = '\0';
4058 if (ifn = strchr((char *)cmd, '<'))
4059 {
4060 char *p;
4061
4062 *ifn++ = '\0';
4063 p = strchr(ifn,' '); /* chop off any trailing spaces */
4064 if (p)
4065 *p = '\0';
4066 }
4067 if (ofn)
4068 x = vms_sys((char *)cmd, ofn, ifn);
4069 else
4070 x = system((char *)cmd);
4071# else
4072 newcmd = lalloc(STRLEN(p_sh)
4073 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4074 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4075 if (newcmd == NULL)
4076 x = 0;
4077 else
4078 {
4079 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4080 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4081 (char *)p_shcf,
4082 (char *)cmd);
4083 x = system((char *)newcmd);
4084 vim_free(newcmd);
4085 }
4086# endif
4087 }
4088# ifdef VMS
4089 x = vms_sys_status(x);
4090# endif
4091 if (emsg_silent)
4092 ;
4093 else if (x == 127)
4094 MSG_PUTS(_("\nCannot execute shell sh\n"));
4095# endif /* __EMX__ */
4096 else if (x && !(options & SHELL_SILENT))
4097 {
4098 MSG_PUTS(_("\nshell returned "));
4099 msg_outnum((long)x);
4100 msg_putchar('\n');
4101 }
4102
4103 if (tmode == TMODE_RAW)
4104 settmode(TMODE_RAW); /* set to raw mode */
4105# ifdef FEAT_TITLE
4106 resettitle();
4107# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004108# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4109 restore_clipboard();
4110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 return x;
4112
4113#else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */
4114
Bram Moolenaardf177f62005-02-22 08:39:57 +00004115# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4116 127, some shells use that already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
4118 char_u *newcmd = NULL;
4119 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004120 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 pid_t wait_pid = 0;
4122# ifdef HAVE_UNION_WAIT
4123 union wait status;
4124# else
4125 int status = -1;
4126# endif
4127 int retval = -1;
4128 char **argv = NULL;
4129 int argc;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004130 char_u *p_shcf_copy = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 int i;
4132 char_u *p;
4133 int inquote;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004135# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 int pty_slave_fd = -1;
4137 char *tty_name;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004138# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004139 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 int fd_fromshell[2];
4141 int pipe_error = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004142# ifdef HAVE_SETENV
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 char envbuf[50];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004144# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 static char envbuf_Rows[20];
4146 static char envbuf_Columns[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004148 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149
Bram Moolenaar62b42182010-09-21 22:09:37 +02004150 newcmd = vim_strsave(p_sh);
4151 if (newcmd == NULL) /* out of memory */
4152 goto error;
4153
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 out_flush();
4155 if (options & SHELL_COOKED)
4156 settmode(TMODE_COOK); /* set to normal mode */
4157
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004158 /*
4159 * Do this loop twice:
4160 * 1: find number of arguments
4161 * 2: separate them and build argv[]
4162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 for (i = 0; i < 2; ++i)
4164 {
4165 p = newcmd;
4166 inquote = FALSE;
4167 argc = 0;
4168 for (;;)
4169 {
4170 if (i == 1)
4171 argv[argc] = (char *)p;
4172 ++argc;
4173 while (*p && (inquote || (*p != ' ' && *p != TAB)))
4174 {
4175 if (*p == '"')
4176 inquote = !inquote;
4177 ++p;
4178 }
4179 if (*p == NUL)
4180 break;
4181 if (i == 1)
4182 *p++ = NUL;
4183 p = skipwhite(p);
4184 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004185 if (argv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004187 /*
4188 * Account for possible multiple args in p_shcf.
4189 */
4190 p = p_shcf;
4191 for (;;)
4192 {
4193 p = skiptowhite(p);
4194 if (*p == NUL)
4195 break;
4196 ++argc;
4197 p = skipwhite(p);
4198 }
4199
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 argv = (char **)alloc((unsigned)((argc + 4) * sizeof(char *)));
4201 if (argv == NULL) /* out of memory */
4202 goto error;
4203 }
4204 }
4205 if (cmd != NULL)
4206 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004207 char_u *s;
4208
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 if (extra_shell_arg != NULL)
4210 argv[argc++] = (char *)extra_shell_arg;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004211
4212 /* Break 'shellcmdflag' into white separated parts. This doesn't
4213 * handle quoted strings, they are very unlikely to appear. */
4214 p_shcf_copy = alloc((unsigned)STRLEN(p_shcf) + 1);
4215 if (p_shcf_copy == NULL) /* out of memory */
4216 goto error;
4217 s = p_shcf_copy;
4218 p = p_shcf;
4219 while (*p != NUL)
4220 {
4221 argv[argc++] = (char *)s;
4222 while (*p && *p != ' ' && *p != TAB)
4223 *s++ = *p++;
4224 *s++ = NUL;
4225 p = skipwhite(p);
4226 }
4227
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 argv[argc++] = (char *)cmd;
4229 }
4230 argv[argc] = NULL;
4231
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004233 * For the GUI, when writing the output into the buffer and when reading
4234 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4235 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004237 if ((options & (SHELL_READ|SHELL_WRITE))
4238# ifdef FEAT_GUI
4239 || (gui.in_use && show_shell_mess)
4240# endif
4241 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004243# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 /*
4245 * Try to open a master pty.
4246 * If this works, open the slave pty.
4247 * If the slave can't be opened, close the master pty.
4248 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004249 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 {
4251 pty_master_fd = OpenPTY(&tty_name); /* open pty */
Bram Moolenaare70172e2011-08-04 19:36:52 +02004252 if (pty_master_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
Bram Moolenaare70172e2011-08-04 19:36:52 +02004254 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4255 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4256 * adding O_NOCTTY always works when defined. */
4257#ifdef O_NOCTTY
4258 pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4259#else
4260 pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4261#endif
4262 if (pty_slave_fd < 0)
4263 {
4264 close(pty_master_fd);
4265 pty_master_fd = -1;
4266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268 }
4269 /*
4270 * If not opening a pty or it didn't work, try using pipes.
4271 */
4272 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 {
4275 pipe_error = (pipe(fd_toshell) < 0);
4276 if (!pipe_error) /* pipe create OK */
4277 {
4278 pipe_error = (pipe(fd_fromshell) < 0);
4279 if (pipe_error) /* pipe create failed */
4280 {
4281 close(fd_toshell[0]);
4282 close(fd_toshell[1]);
4283 }
4284 }
4285 if (pipe_error)
4286 {
4287 MSG_PUTS(_("\nCannot create pipes\n"));
4288 out_flush();
4289 }
4290 }
4291 }
4292
4293 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 {
4295# ifdef __BEOS__
4296 beos_cleanup_read_thread();
4297# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004298
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 if ((pid = fork()) == -1) /* maybe we should use vfork() */
4300 {
4301 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004302 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004304 || (gui.in_use && show_shell_mess)
4305# endif
4306 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004308# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 if (pty_master_fd >= 0) /* close the pseudo tty */
4310 {
4311 close(pty_master_fd);
4312 close(pty_slave_fd);
4313 }
4314 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004315# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 {
4317 close(fd_toshell[0]);
4318 close(fd_toshell[1]);
4319 close(fd_fromshell[0]);
4320 close(fd_fromshell[1]);
4321 }
4322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324 else if (pid == 0) /* child */
4325 {
4326 reset_signals(); /* handle signals normally */
4327
4328 if (!show_shell_mess || (options & SHELL_EXPAND))
4329 {
4330 int fd;
4331
4332 /*
4333 * Don't want to show any message from the shell. Can't just
4334 * close stdout and stderr though, because some systems will
4335 * break if you try to write to them after that, so we must
4336 * use dup() to replace them with something else -- webb
4337 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4338 * waiting for input.
4339 */
4340 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4341 fclose(stdin);
4342 fclose(stdout);
4343 fclose(stderr);
4344
4345 /*
4346 * If any of these open()'s and dup()'s fail, we just continue
4347 * anyway. It's not fatal, and on most systems it will make
4348 * no difference at all. On a few it will cause the execvp()
4349 * to exit with a non-zero status even when the completion
4350 * could be done, which is nothing too serious. If the open()
4351 * or dup() failed we'd just do the same thing ourselves
4352 * anyway -- webb
4353 */
4354 if (fd >= 0)
4355 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004356 ignored = dup(fd); /* To replace stdin (fd 0) */
4357 ignored = dup(fd); /* To replace stdout (fd 1) */
4358 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
4360 /* Don't need this now that we've duplicated it */
4361 close(fd);
4362 }
4363 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004364 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004366 || gui.in_use
4367# endif
4368 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 {
4370
Bram Moolenaardf177f62005-02-22 08:39:57 +00004371# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004372 /* Create our own process group, so that the child and all its
4373 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004374 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004375 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004376 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004377 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004378# if defined(SIGHUP)
4379 /* When doing "!xterm&" and 'shell' is bash: the shell
4380 * will exit and send SIGHUP to all processes in its
4381 * group, killing the just started process. Ignore SIGHUP
4382 * to avoid that. (suggested by Simon Schubert)
4383 */
4384 signal(SIGHUP, SIG_IGN);
4385# endif
4386 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004387# endif
4388# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004389 if (pty_slave_fd >= 0)
4390 {
4391 /* push stream discipline modules */
4392 if (options & SHELL_COOKED)
4393 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004395 /* Try to become controlling tty (probably doesn't work,
4396 * unless run by root) */
4397 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004399 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 /* Simulate to have a dumb terminal (for now) */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004402# ifdef HAVE_SETENV
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 setenv("TERM", "dumb", 1);
4404 sprintf((char *)envbuf, "%ld", Rows);
4405 setenv("ROWS", (char *)envbuf, 1);
4406 sprintf((char *)envbuf, "%ld", Rows);
4407 setenv("LINES", (char *)envbuf, 1);
4408 sprintf((char *)envbuf, "%ld", Columns);
4409 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004410# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 /*
4412 * Putenv does not copy the string, it has to remain valid.
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004413 * Use a static array to avoid losing allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 */
4415 putenv("TERM=dumb");
4416 sprintf(envbuf_Rows, "ROWS=%ld", Rows);
4417 putenv(envbuf_Rows);
4418 sprintf(envbuf_Rows, "LINES=%ld", Rows);
4419 putenv(envbuf_Rows);
4420 sprintf(envbuf_Columns, "COLUMNS=%ld", Columns);
4421 putenv(envbuf_Columns);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
Bram Moolenaara5792f52005-11-23 21:25:05 +00004424 /*
4425 * stderr is only redirected when using the GUI, so that a
4426 * program like gpg can still access the terminal to get a
4427 * passphrase using stderr.
4428 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004429# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (pty_master_fd >= 0)
4431 {
4432 close(pty_master_fd); /* close master side of pty */
4433
4434 /* set up stdin/stdout/stderr for the child */
4435 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004436 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004438 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004439 if (gui.in_use)
4440 {
4441 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004442 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
4445 close(pty_slave_fd); /* has been dupped, close it now */
4446 }
4447 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 {
4450 /* set up stdin for the child */
4451 close(fd_toshell[1]);
4452 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004453 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 close(fd_toshell[0]);
4455
4456 /* set up stdout for the child */
4457 close(fd_fromshell[0]);
4458 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004459 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 close(fd_fromshell[1]);
4461
Bram Moolenaara5792f52005-11-23 21:25:05 +00004462# ifdef FEAT_GUI
4463 if (gui.in_use)
4464 {
4465 /* set up stderr for the child */
4466 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004467 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004468 }
4469# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004472
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 /*
4474 * There is no type cast for the argv, because the type may be
4475 * different on different machines. This may cause a warning
4476 * message with strict compilers, don't worry about it.
4477 * Call _exit() instead of exit() to avoid closing the connection
4478 * to the X server (esp. with GTK, which uses atexit()).
4479 */
4480 execvp(argv[0], argv);
4481 _exit(EXEC_FAILED); /* exec failed, return failure code */
4482 }
4483 else /* parent */
4484 {
4485 /*
4486 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004487 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 */
4489 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004490 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
4492 /*
4493 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004494 * This is also used to pipe stdin/stdout to/from the external
4495 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004497 if ((options & (SHELL_READ|SHELL_WRITE))
4498# ifdef FEAT_GUI
4499 || (gui.in_use && show_shell_mess)
4500# endif
4501 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004503# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004505# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4509 int ta_len = 0; /* valid bytes in ta_buf[] */
4510 int len;
4511 int p_more_save;
4512 int old_State;
4513 int c;
4514 int toshell_fd;
4515 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004516 garray_T ga;
4517 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004518# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4519 struct timeval start_tv;
4520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
Bram Moolenaardf177f62005-02-22 08:39:57 +00004522# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 if (pty_master_fd >= 0)
4524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 fromshell_fd = pty_master_fd;
4526 toshell_fd = dup(pty_master_fd);
4527 }
4528 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004529# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
4531 close(fd_toshell[0]);
4532 close(fd_fromshell[1]);
4533 toshell_fd = fd_toshell[1];
4534 fromshell_fd = fd_fromshell[0];
4535 }
4536
4537 /*
4538 * Write to the child if there are typed characters.
4539 * Read from the child if there are characters available.
4540 * Repeat the reading a few times if more characters are
4541 * available. Need to check for typed keys now and then, but
4542 * not too often (delays when no chars are available).
4543 * This loop is quit if no characters can be read from the pty
4544 * (WaitForChar detected special condition), or there are no
4545 * characters available and the child has exited.
4546 * Only check if the child has exited when there is no more
4547 * output. The child may exit before all the output has
4548 * been printed.
4549 *
4550 * Currently this busy loops!
4551 * This can probably dead-lock when the write blocks!
4552 */
4553 p_more_save = p_more;
4554 p_more = FALSE;
4555 old_State = State;
4556 State = EXTERNCMD; /* don't redraw at window resize */
4557
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004558 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004559 {
4560 /* Fork a process that will write the lines to the
4561 * external program. */
4562 if ((wpid = fork()) == -1)
4563 {
4564 MSG_PUTS(_("\nCannot fork\n"));
4565 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004566 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004567 {
4568 linenr_T lnum = curbuf->b_op_start.lnum;
4569 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004570 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004571 size_t l;
4572
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004573 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004574 for (;;)
4575 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004576 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004577 if (l == 0)
4578 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004579 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004580 /* NL -> NUL translation */
4581 len = write(toshell_fd, "", (size_t)1);
4582 else
4583 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004584 char_u *s = vim_strchr(lp + written, NL);
4585
Bram Moolenaar89d40322006-08-29 15:30:07 +00004586 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004587 s == NULL ? l
4588 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004589 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004590 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004591 {
4592 /* Finished a line, add a NL, unless this line
4593 * should not have one. */
4594 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004595 || (!curbuf->b_p_bin
4596 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004597 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004598 && (lnum !=
4599 curbuf->b_ml.ml_line_count
4600 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004601 ignored = write(toshell_fd, "\n",
4602 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004603 ++lnum;
4604 if (lnum > curbuf->b_op_end.lnum)
4605 {
4606 /* finished all the lines, close pipe */
4607 close(toshell_fd);
4608 toshell_fd = -1;
4609 break;
4610 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004611 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004612 written = 0;
4613 }
4614 else if (len > 0)
4615 written += len;
4616 }
4617 _exit(0);
4618 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004619 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004620 {
4621 close(toshell_fd);
4622 toshell_fd = -1;
4623 }
4624 }
4625
4626 if (options & SHELL_READ)
4627 ga_init2(&ga, 1, BUFLEN);
4628
4629 noread_cnt = 0;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004630# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4631 gettimeofday(&start_tv, NULL);
4632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 for (;;)
4634 {
4635 /*
4636 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004637 * if there are any.
4638 * Don't do this if we are expanding wild cards (would eat
4639 * typeahead).
4640 * Don't do this when filtering and terminal is in cooked
4641 * mode, the shell command will handle the I/O. Avoids
4642 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004643 * Don't get characters when the child has already
4644 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004645 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004646 * while (noread_cnt > 4), avoids that ":r !ls" eats
4647 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 */
4649 len = 0;
4650 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004651 && ((options &
4652 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4653 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004654# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004655 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004656# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004657 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004658 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004659 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004661 if (ta_len == 0)
4662 {
4663 /* Get extra characters when we don't have any.
4664 * Reset the counter and timer. */
4665 noread_cnt = 0;
4666# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4667 gettimeofday(&start_tv, NULL);
4668# endif
4669 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4670 }
4671 if (ta_len > 0 || len > 0)
4672 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 /*
4674 * For pipes:
4675 * Check for CTRL-C: send interrupt signal to child.
4676 * Check for CTRL-D: EOF, close pipe to child.
4677 */
4678 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4679 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004680# ifdef SIGINT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 /*
4682 * Send SIGINT to the child's group or all
4683 * processes in our group.
4684 */
4685 if (ta_buf[ta_len] == Ctrl_C
4686 || ta_buf[ta_len] == intr_char)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004687 {
4688# ifdef HAVE_SETSID
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 kill(-pid, SIGINT);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004690# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 kill(0, SIGINT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692# endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00004693 if (wpid > 0)
4694 kill(wpid, SIGINT);
4695 }
4696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 if (pty_master_fd < 0 && toshell_fd >= 0
4698 && ta_buf[ta_len] == Ctrl_D)
4699 {
4700 close(toshell_fd);
4701 toshell_fd = -1;
4702 }
4703 }
4704
4705 /* replace K_BS by <BS> and K_DEL by <DEL> */
4706 for (i = ta_len; i < ta_len + len; ++i)
4707 {
4708 if (ta_buf[i] == CSI && len - i > 2)
4709 {
4710 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4711 if (c == K_DEL || c == K_KDEL || c == K_BS)
4712 {
4713 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4714 (size_t)(len - i - 2));
4715 if (c == K_DEL || c == K_KDEL)
4716 ta_buf[i] = DEL;
4717 else
4718 ta_buf[i] = Ctrl_H;
4719 len -= 2;
4720 }
4721 }
4722 else if (ta_buf[i] == '\r')
4723 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00004724# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00004726 i += (*mb_ptr2len_len)(ta_buf + i,
4727 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004728# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 }
4730
4731 /*
4732 * For pipes: echo the typed characters.
4733 * For a pty this does not seem to work.
4734 */
4735 if (pty_master_fd < 0)
4736 {
4737 for (i = ta_len; i < ta_len + len; ++i)
4738 {
4739 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4740 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004741# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 else if (has_mbyte)
4743 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004744 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
4746 msg_outtrans_len(ta_buf + i, l);
4747 i += l - 1;
4748 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 else
4751 msg_outtrans_len(ta_buf + i, 1);
4752 }
4753 windgoto(msg_row, msg_col);
4754 out_flush();
4755 }
4756
4757 ta_len += len;
4758
4759 /*
4760 * Write the characters to the child, unless EOF has
4761 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004762 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004763 * When writing buffer lines, drop the typed
4764 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004766 if (options & SHELL_WRITE)
4767 ta_len = 0;
4768 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
4770 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
4771 if (len > 0)
4772 {
4773 ta_len -= len;
4774 mch_memmove(ta_buf, ta_buf + len, ta_len);
4775 }
4776 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 }
4779
Bram Moolenaardf177f62005-02-22 08:39:57 +00004780 if (got_int)
4781 {
4782 /* CTRL-C sends a signal to the child, we ignore it
4783 * ourselves */
4784# ifdef HAVE_SETSID
4785 kill(-pid, SIGINT);
4786# else
4787 kill(0, SIGINT);
4788# endif
4789 if (wpid > 0)
4790 kill(wpid, SIGINT);
4791 got_int = FALSE;
4792 }
4793
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 /*
4795 * Check if the child has any characters to be printed.
4796 * Read them and write them to our window. Repeat this as
4797 * long as there is something to do, avoid the 10ms wait
4798 * for mch_inchar(), or sending typeahead characters to
4799 * the external process.
4800 * TODO: This should handle escape sequences, compatible
4801 * to some terminal (vt52?).
4802 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004803 ++noread_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 while (RealWaitForChar(fromshell_fd, 10L, NULL))
4805 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004806 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00004807# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004809# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00004811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 );
4813 if (len <= 0) /* end of file or error */
4814 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004815
4816 noread_cnt = 0;
4817 if (options & SHELL_READ)
4818 {
4819 /* Do NUL -> NL translation, append NL separated
4820 * lines to the current buffer. */
4821 for (i = 0; i < len; ++i)
4822 {
4823 if (buffer[i] == NL)
4824 append_ga_line(&ga);
4825 else if (buffer[i] == NUL)
4826 ga_append(&ga, NL);
4827 else
4828 ga_append(&ga, buffer[i]);
4829 }
4830 }
4831# ifdef FEAT_MBYTE
4832 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 {
4834 int l;
4835
Bram Moolenaardf177f62005-02-22 08:39:57 +00004836 len += buffer_off;
4837 buffer[len] = NUL;
4838
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 /* Check if the last character in buffer[] is
4840 * incomplete, keep these bytes for the next
4841 * round. */
4842 for (p = buffer; p < buffer + len; p += l)
4843 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004844 l = mb_cptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 if (l == 0)
4846 l = 1; /* NUL byte? */
4847 else if (MB_BYTE2LEN(*p) != l)
4848 break;
4849 }
4850 if (p == buffer) /* no complete character */
4851 {
4852 /* avoid getting stuck at an illegal byte */
4853 if (len >= 12)
4854 ++p;
4855 else
4856 {
4857 buffer_off = len;
4858 continue;
4859 }
4860 }
4861 c = *p;
4862 *p = NUL;
4863 msg_puts(buffer);
4864 if (p < buffer + len)
4865 {
4866 *p = c;
4867 buffer_off = (buffer + len) - p;
4868 mch_memmove(buffer, p, buffer_off);
4869 continue;
4870 }
4871 buffer_off = 0;
4872 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004873# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
4876 buffer[len] = NUL;
4877 msg_puts(buffer);
4878 }
4879
4880 windgoto(msg_row, msg_col);
4881 cursor_on();
4882 out_flush();
4883 if (got_int)
4884 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004885
4886# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4887 {
4888 struct timeval now_tv;
4889 long msec;
4890
4891 /* Avoid that we keep looping here without
4892 * checking for a CTRL-C for a long time. Don't
4893 * break out too often to avoid losing typeahead. */
4894 gettimeofday(&now_tv, NULL);
4895 msec = (now_tv.tv_sec - start_tv.tv_sec) * 1000L
4896 + (now_tv.tv_usec - start_tv.tv_usec) / 1000L;
4897 if (msec > 2000)
4898 {
4899 noread_cnt = 5;
4900 break;
4901 }
4902 }
4903# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
4905
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004906 /* If we already detected the child has finished break the
4907 * loop now. */
4908 if (wait_pid == pid)
4909 break;
4910
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 /*
4912 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004913 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004915# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004916 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004917# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004919# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
4921 || (wait_pid == pid && WIFEXITED(status)))
4922 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004923 /* Don't break the loop yet, try reading more
4924 * characters from "fromshell_fd" first. When using
4925 * pipes there might still be something to read and
4926 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004929 else
4930 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004931
Bram Moolenaar95a51352013-03-21 22:53:50 +01004932# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004933 /* Handle any X events, e.g. serving the clipboard. */
4934 clip_update();
4935# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937finished:
4938 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004939 if (options & SHELL_READ)
4940 {
4941 if (ga.ga_len > 0)
4942 {
4943 append_ga_line(&ga);
4944 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004945 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004946 }
4947 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004948 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004949 ga_clear(&ga);
4950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 /*
4953 * Give all typeahead that wasn't used back to ui_inchar().
4954 */
4955 if (ta_len)
4956 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 State = old_State;
4958 if (toshell_fd >= 0)
4959 close(toshell_fd);
4960 close(fromshell_fd);
4961 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01004962# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004963 else
4964 {
4965 /*
4966 * Similar to the loop above, but only handle X events, no
4967 * I/O.
4968 */
4969 for (;;)
4970 {
4971 if (got_int)
4972 {
4973 /* CTRL-C sends a signal to the child, we ignore it
4974 * ourselves */
4975# ifdef HAVE_SETSID
4976 kill(-pid, SIGINT);
4977# else
4978 kill(0, SIGINT);
4979# endif
4980 got_int = FALSE;
4981 }
4982# ifdef __NeXT__
4983 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
4984# else
4985 wait_pid = waitpid(pid, &status, WNOHANG);
4986# endif
4987 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
4988 || (wait_pid == pid && WIFEXITED(status)))
4989 {
4990 wait_pid = pid;
4991 break;
4992 }
4993
4994 /* Handle any X events, e.g. serving the clipboard. */
4995 clip_update();
4996
4997 mch_delay(10L, TRUE);
4998 }
4999 }
5000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
5002 /*
5003 * Wait until our child has exited.
5004 * Ignore wait() returning pids of other children and returning
5005 * because of some signal like SIGWINCH.
5006 * Don't wait if wait_pid was already set above, indicating the
5007 * child already exited.
5008 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005009 if (wait_pid != pid)
5010 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011
Bram Moolenaar624891f2010-10-13 16:22:09 +02005012# ifdef FEAT_GUI
5013 /* Close slave side of pty. Only do this after the child has
5014 * exited, otherwise the child may hang when it tries to write on
5015 * the pty. */
5016 if (pty_master_fd >= 0)
5017 close(pty_slave_fd);
5018# endif
5019
Bram Moolenaardf177f62005-02-22 08:39:57 +00005020 /* Make sure the child that writes to the external program is
5021 * dead. */
5022 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005023 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005024 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005025 wait4pid(wpid, NULL);
5026 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005027
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 /*
5029 * Set to raw mode right now, otherwise a CTRL-C after
5030 * catch_signals() will kill Vim.
5031 */
5032 if (tmode == TMODE_RAW)
5033 settmode(TMODE_RAW);
5034 did_settmode = TRUE;
5035 set_signals();
5036
5037 if (WIFEXITED(status))
5038 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005039 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005041 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 {
5043 if (retval == EXEC_FAILED)
5044 {
5045 MSG_PUTS(_("\nCannot execute shell "));
5046 msg_outtrans(p_sh);
5047 msg_putchar('\n');
5048 }
5049 else if (!(options & SHELL_SILENT))
5050 {
5051 MSG_PUTS(_("\nshell returned "));
5052 msg_outnum((long)retval);
5053 msg_putchar('\n');
5054 }
5055 }
5056 }
5057 else
5058 MSG_PUTS(_("\nCommand terminated\n"));
5059 }
5060 }
5061 vim_free(argv);
Bram Moolenaarea35ef62011-08-04 22:59:28 +02005062 vim_free(p_shcf_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
5064error:
5065 if (!did_settmode)
5066 if (tmode == TMODE_RAW)
5067 settmode(TMODE_RAW); /* set to raw mode */
5068# ifdef FEAT_TITLE
5069 resettitle();
5070# endif
5071 vim_free(newcmd);
5072
5073 return retval;
5074
5075#endif /* USE_SYSTEM */
5076}
5077
5078/*
5079 * Check for CTRL-C typed by reading all available characters.
5080 * In cooked mode we should get SIGINT, no need to check.
5081 */
5082 void
5083mch_breakcheck()
5084{
5085 if (curr_tmode == TMODE_RAW && RealWaitForChar(read_cmd_fd, 0L, NULL))
5086 fill_input_buf(FALSE);
5087}
5088
5089/*
5090 * Wait "msec" msec until a character is available from the keyboard or from
5091 * inbuf[]. msec == -1 will block forever.
5092 * When a GUI is being used, this will never get called -- webb
5093 */
5094 static int
5095WaitForChar(msec)
5096 long msec;
5097{
5098#ifdef FEAT_MOUSE_GPM
5099 int gpm_process_wanted;
5100#endif
5101#ifdef FEAT_XCLIPBOARD
5102 int rest;
5103#endif
5104 int avail;
5105
5106 if (input_available()) /* something in inbuf[] */
5107 return 1;
5108
5109#if defined(FEAT_MOUSE_DEC)
5110 /* May need to query the mouse position. */
5111 if (WantQueryMouse)
5112 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005113 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5115 }
5116#endif
5117
5118 /*
5119 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5120 * events. This is a bit complicated, because they might both be defined.
5121 */
5122#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5123# ifdef FEAT_XCLIPBOARD
5124 rest = 0;
5125 if (do_xterm_trace())
5126 rest = msec;
5127# endif
5128 do
5129 {
5130# ifdef FEAT_XCLIPBOARD
5131 if (rest != 0)
5132 {
5133 msec = XT_TRACE_DELAY;
5134 if (rest >= 0 && rest < XT_TRACE_DELAY)
5135 msec = rest;
5136 if (rest >= 0)
5137 rest -= msec;
5138 }
5139# endif
5140# ifdef FEAT_MOUSE_GPM
5141 gpm_process_wanted = 0;
5142 avail = RealWaitForChar(read_cmd_fd, msec, &gpm_process_wanted);
5143# else
5144 avail = RealWaitForChar(read_cmd_fd, msec, NULL);
5145# endif
5146 if (!avail)
5147 {
5148 if (input_available())
5149 return 1;
5150# ifdef FEAT_XCLIPBOARD
5151 if (rest == 0 || !do_xterm_trace())
5152# endif
5153 break;
5154 }
5155 }
5156 while (FALSE
5157# ifdef FEAT_MOUSE_GPM
5158 || (gpm_process_wanted && mch_gpm_process() == 0)
5159# endif
5160# ifdef FEAT_XCLIPBOARD
5161 || (!avail && rest != 0)
5162# endif
5163 );
5164
5165#else
5166 avail = RealWaitForChar(read_cmd_fd, msec, NULL);
5167#endif
5168 return avail;
5169}
5170
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01005171#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172/*
5173 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005174 * "msec" == 0 will check for characters once.
5175 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 * When a GUI is being used, this will not be used for input -- webb
5177 * Returns also, when a request from Sniff is waiting -- toni.
5178 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005179 * Or when a clientserver message is on the queue.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181#if defined(__BEOS__)
5182 int
5183#else
5184 static int
5185#endif
5186RealWaitForChar(fd, msec, check_for_gpm)
5187 int fd;
5188 long msec;
Bram Moolenaar78a15312009-05-15 19:33:18 +00005189 int *check_for_gpm UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190{
5191 int ret;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005192#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 static int busy = FALSE;
5194
5195 /* May retry getting characters after an event was handled. */
5196# define MAY_LOOP
5197
5198# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
5199 /* Remember at what time we started, so that we know how much longer we
5200 * should wait after being interrupted. */
5201# define USE_START_TV
5202 struct timeval start_tv;
5203
5204 if (msec > 0 && (
5205# ifdef FEAT_XCLIPBOARD
5206 xterm_Shell != (Widget)0
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005207# if defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 ||
5209# endif
5210# endif
5211# ifdef USE_XSMP
5212 xsmp_icefd != -1
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005213# ifdef FEAT_MZSCHEME
5214 ||
5215# endif
5216# endif
5217# ifdef FEAT_MZSCHEME
5218 (mzthreads_allowed() && p_mzq > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219# endif
5220 ))
5221 gettimeofday(&start_tv, NULL);
5222# endif
5223
5224 /* Handle being called recursively. This may happen for the session
5225 * manager stuff, it may save the file, which does a breakcheck. */
5226 if (busy)
5227 return 0;
5228#endif
5229
5230#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005231 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232#endif
5233 {
5234#ifdef MAY_LOOP
5235 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005236# ifdef FEAT_MZSCHEME
5237 int mzquantum_used = FALSE;
5238# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239#endif
5240#ifndef HAVE_SELECT
Bram Moolenaare0874f82016-01-24 20:36:41 +01005241 struct pollfd fds[6 + MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 int nfd;
5243# ifdef FEAT_XCLIPBOARD
5244 int xterm_idx = -1;
5245# endif
5246# ifdef FEAT_MOUSE_GPM
5247 int gpm_idx = -1;
5248# endif
5249# ifdef USE_XSMP
5250 int xsmp_idx = -1;
5251# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005252 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005254# ifdef FEAT_MZSCHEME
5255 mzvim_check_threads();
5256 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5257 {
5258 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
5259 mzquantum_used = TRUE;
5260 }
5261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 fds[0].fd = fd;
5263 fds[0].events = POLLIN;
5264 nfd = 1;
5265
5266# ifdef FEAT_SNIFF
5267# define SNIFF_IDX 1
5268 if (want_sniff_request)
5269 {
5270 fds[SNIFF_IDX].fd = fd_from_sniff;
5271 fds[SNIFF_IDX].events = POLLIN;
5272 nfd++;
5273 }
5274# endif
5275# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005276 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 if (xterm_Shell != (Widget)0)
5278 {
5279 xterm_idx = nfd;
5280 fds[nfd].fd = ConnectionNumber(xterm_dpy);
5281 fds[nfd].events = POLLIN;
5282 nfd++;
5283 }
5284# endif
5285# ifdef FEAT_MOUSE_GPM
5286 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5287 {
5288 gpm_idx = nfd;
5289 fds[nfd].fd = gpm_fd;
5290 fds[nfd].events = POLLIN;
5291 nfd++;
5292 }
5293# endif
5294# ifdef USE_XSMP
5295 if (xsmp_icefd != -1)
5296 {
5297 xsmp_idx = nfd;
5298 fds[nfd].fd = xsmp_icefd;
5299 fds[nfd].events = POLLIN;
5300 nfd++;
5301 }
5302# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01005303#ifdef FEAT_CHANNEL
5304 nfd = channel_poll_setup(nfd, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005307 ret = poll(fds, nfd, towait);
5308# ifdef FEAT_MZSCHEME
5309 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005310 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005311 finished = FALSE;
5312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313
5314# ifdef FEAT_SNIFF
5315 if (ret < 0)
5316 sniff_disconnect(1);
5317 else if (want_sniff_request)
5318 {
5319 if (fds[SNIFF_IDX].revents & POLLHUP)
5320 sniff_disconnect(1);
5321 if (fds[SNIFF_IDX].revents & POLLIN)
5322 sniff_request_waiting = 1;
5323 }
5324# endif
5325# ifdef FEAT_XCLIPBOARD
5326 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
5327 {
5328 xterm_update(); /* Maybe we should hand out clipboard */
5329 if (--ret == 0 && !input_available())
5330 /* Try again */
5331 finished = FALSE;
5332 }
5333# endif
5334# ifdef FEAT_MOUSE_GPM
5335 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
5336 {
5337 *check_for_gpm = 1;
5338 }
5339# endif
5340# ifdef USE_XSMP
5341 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
5342 {
5343 if (fds[xsmp_idx].revents & POLLIN)
5344 {
5345 busy = TRUE;
5346 xsmp_handle_requests();
5347 busy = FALSE;
5348 }
5349 else if (fds[xsmp_idx].revents & POLLHUP)
5350 {
5351 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005352 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 xsmp_close();
5354 }
5355 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005356 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 }
5358# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01005359#ifdef FEAT_CHANNEL
5360 if (ret > 0)
5361 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363
5364
5365#else /* HAVE_SELECT */
5366
5367 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005368 struct timeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 fd_set rfds, efds;
5370 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005371 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005373# ifdef FEAT_MZSCHEME
5374 mzvim_check_threads();
5375 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5376 {
5377 towait = p_mzq; /* don't wait longer than 'mzquantum' */
5378 mzquantum_used = TRUE;
5379 }
5380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381# ifdef __EMX__
5382 /* don't check for incoming chars if not in raw mode, because select()
5383 * always returns TRUE then (in some version of emx.dll) */
5384 if (curr_tmode != TMODE_RAW)
5385 return 0;
5386# endif
5387
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005388 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005390 tv.tv_sec = towait / 1000;
5391 tv.tv_usec = (towait % 1000) * (1000000/1000);
5392 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005394 else
5395 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396
5397 /*
5398 * Select on ready for reading and exceptional condition (end of file).
5399 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005400select_eintr:
5401 FD_ZERO(&rfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 FD_ZERO(&efds);
5403 FD_SET(fd, &rfds);
5404# if !defined(__QNX__) && !defined(__CYGWIN32__)
5405 /* For QNX select() always returns 1 if this is set. Why? */
5406 FD_SET(fd, &efds);
5407# endif
5408 maxfd = fd;
5409
5410# ifdef FEAT_SNIFF
5411 if (want_sniff_request)
5412 {
5413 FD_SET(fd_from_sniff, &rfds);
5414 FD_SET(fd_from_sniff, &efds);
5415 if (maxfd < fd_from_sniff)
5416 maxfd = fd_from_sniff;
5417 }
5418# endif
5419# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005420 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 if (xterm_Shell != (Widget)0)
5422 {
5423 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
5424 if (maxfd < ConnectionNumber(xterm_dpy))
5425 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005426
5427 /* An event may have already been read but not handled. In
5428 * particulary, XFlush may cause this. */
5429 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
5431# endif
5432# ifdef FEAT_MOUSE_GPM
5433 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5434 {
5435 FD_SET(gpm_fd, &rfds);
5436 FD_SET(gpm_fd, &efds);
5437 if (maxfd < gpm_fd)
5438 maxfd = gpm_fd;
5439 }
5440# endif
5441# ifdef USE_XSMP
5442 if (xsmp_icefd != -1)
5443 {
5444 FD_SET(xsmp_icefd, &rfds);
5445 FD_SET(xsmp_icefd, &efds);
5446 if (maxfd < xsmp_icefd)
5447 maxfd = xsmp_icefd;
5448 }
5449# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01005450# ifdef FEAT_CHANNEL
5451 maxfd = channel_select_setup(maxfd, &rfds);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005452# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005454 ret = select(maxfd + 1, &rfds, NULL, &efds, tvp);
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005455# ifdef EINTR
5456 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005457 {
5458 /* Check whether window has been resized, EINTR may be caused by
5459 * SIGWINCH. */
5460 if (do_resize)
5461 handle_resize();
5462
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005463 /* Interrupted by a signal, need to try again. We ignore msec
5464 * here, because we do want to check even after a timeout if
5465 * characters are available. Needed for reading output of an
5466 * external command after the process has finished. */
5467 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005468 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005469# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00005470# ifdef __TANDEM
5471 if (ret == -1 && errno == ENOTSUP)
5472 {
5473 FD_ZERO(&rfds);
5474 FD_ZERO(&efds);
5475 ret = 0;
5476 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005477# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005478# ifdef FEAT_MZSCHEME
5479 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005480 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005481 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482# endif
5483
5484# ifdef FEAT_SNIFF
5485 if (ret < 0 )
5486 sniff_disconnect(1);
5487 else if (ret > 0 && want_sniff_request)
5488 {
5489 if (FD_ISSET(fd_from_sniff, &efds))
5490 sniff_disconnect(1);
5491 if (FD_ISSET(fd_from_sniff, &rfds))
5492 sniff_request_waiting = 1;
5493 }
5494# endif
5495# ifdef FEAT_XCLIPBOARD
5496 if (ret > 0 && xterm_Shell != (Widget)0
5497 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
5498 {
5499 xterm_update(); /* Maybe we should hand out clipboard */
5500 /* continue looping when we only got the X event and the input
5501 * buffer is empty */
5502 if (--ret == 0 && !input_available())
5503 {
5504 /* Try again */
5505 finished = FALSE;
5506 }
5507 }
5508# endif
5509# ifdef FEAT_MOUSE_GPM
5510 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
5511 {
5512 if (FD_ISSET(gpm_fd, &efds))
5513 gpm_close();
5514 else if (FD_ISSET(gpm_fd, &rfds))
5515 *check_for_gpm = 1;
5516 }
5517# endif
5518# ifdef USE_XSMP
5519 if (ret > 0 && xsmp_icefd != -1)
5520 {
5521 if (FD_ISSET(xsmp_icefd, &efds))
5522 {
5523 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005524 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 xsmp_close();
5526 if (--ret == 0)
5527 finished = FALSE; /* keep going if event was only one */
5528 }
5529 else if (FD_ISSET(xsmp_icefd, &rfds))
5530 {
5531 busy = TRUE;
5532 xsmp_handle_requests();
5533 busy = FALSE;
5534 if (--ret == 0)
5535 finished = FALSE; /* keep going if event was only one */
5536 }
5537 }
5538# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01005539#ifdef FEAT_CHANNEL
5540 if (ret > 0)
5541 ret = channel_select_check(ret, &rfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
5544#endif /* HAVE_SELECT */
5545
5546#ifdef MAY_LOOP
5547 if (finished || msec == 0)
5548 break;
5549
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005550# ifdef FEAT_CLIENTSERVER
5551 if (server_waiting())
5552 break;
5553# endif
5554
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 /* We're going to loop around again, find out for how long */
5556 if (msec > 0)
5557 {
5558# ifdef USE_START_TV
5559 struct timeval mtv;
5560
5561 /* Compute remaining wait time. */
5562 gettimeofday(&mtv, NULL);
5563 msec -= (mtv.tv_sec - start_tv.tv_sec) * 1000L
5564 + (mtv.tv_usec - start_tv.tv_usec) / 1000L;
5565# else
5566 /* Guess we got interrupted halfway. */
5567 msec = msec / 2;
5568# endif
5569 if (msec <= 0)
5570 break; /* waited long enough */
5571 }
5572#endif
5573 }
5574
5575 return (ret > 0);
5576}
5577
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579/*
Bram Moolenaar02743632005-07-25 20:42:36 +00005580 * Expand a path into all matching files and/or directories. Handles "*",
5581 * "?", "[a-z]", "**", etc.
5582 * "path" has backslashes before chars that are not to be expanded.
5583 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 */
5585 int
5586mch_expandpath(gap, path, flags)
5587 garray_T *gap;
5588 char_u *path;
5589 int flags; /* EW_* flags */
5590{
Bram Moolenaar02743632005-07-25 20:42:36 +00005591 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592}
5593#endif
5594
5595/*
5596 * mch_expand_wildcards() - this code does wild-card pattern matching using
5597 * the shell
5598 *
5599 * return OK for success, FAIL for error (you may lose some memory) and put
5600 * an error message in *file.
5601 *
5602 * num_pat is number of input patterns
5603 * pat is array of pointers to input patterns
5604 * num_file is pointer to number of matched file names
5605 * file is pointer to array of pointers to matched file names
5606 */
5607
5608#ifndef SEEK_SET
5609# define SEEK_SET 0
5610#endif
5611#ifndef SEEK_END
5612# define SEEK_END 2
5613#endif
5614
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005615#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00005616
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 int
5618mch_expand_wildcards(num_pat, pat, num_file, file, flags)
5619 int num_pat;
5620 char_u **pat;
5621 int *num_file;
5622 char_u ***file;
5623 int flags; /* EW_* flags */
5624{
5625 int i;
5626 size_t len;
5627 char_u *p;
5628 int dir;
5629#ifdef __EMX__
Bram Moolenaarc7247912008-01-13 12:54:11 +00005630 /*
5631 * This is the OS/2 implementation.
5632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633# define EXPL_ALLOC_INC 16
5634 char_u **expl_files;
5635 size_t files_alloced, files_free;
5636 char_u *buf;
5637 int has_wildcard;
5638
5639 *num_file = 0; /* default: no files found */
5640 files_alloced = EXPL_ALLOC_INC; /* how much space is allocated */
5641 files_free = EXPL_ALLOC_INC; /* how much space is not used */
5642 *file = (char_u **)alloc(sizeof(char_u **) * files_alloced);
5643 if (*file == NULL)
5644 return FAIL;
5645
5646 for (; num_pat > 0; num_pat--, pat++)
5647 {
5648 expl_files = NULL;
5649 if (vim_strchr(*pat, '$') || vim_strchr(*pat, '~'))
5650 /* expand environment var or home dir */
5651 buf = expand_env_save(*pat);
5652 else
5653 buf = vim_strsave(*pat);
5654 expl_files = NULL;
Bram Moolenaard8b02732005-01-14 21:48:43 +00005655 has_wildcard = mch_has_exp_wildcard(buf); /* (still) wildcards? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 if (has_wildcard) /* yes, so expand them */
5657 expl_files = (char_u **)_fnexplode(buf);
5658
5659 /*
5660 * return value of buf if no wildcards left,
5661 * OR if no match AND EW_NOTFOUND is set.
5662 */
5663 if ((!has_wildcard && ((flags & EW_NOTFOUND) || mch_getperm(buf) >= 0))
5664 || (expl_files == NULL && (flags & EW_NOTFOUND)))
5665 { /* simply save the current contents of *buf */
5666 expl_files = (char_u **)alloc(sizeof(char_u **) * 2);
5667 if (expl_files != NULL)
5668 {
5669 expl_files[0] = vim_strsave(buf);
5670 expl_files[1] = NULL;
5671 }
5672 }
5673 vim_free(buf);
5674
5675 /*
5676 * Count number of names resulting from expansion,
5677 * At the same time add a backslash to the end of names that happen to
5678 * be directories, and replace slashes with backslashes.
5679 */
5680 if (expl_files)
5681 {
5682 for (i = 0; (p = expl_files[i]) != NULL; i++)
5683 {
5684 dir = mch_isdir(p);
5685 /* If we don't want dirs and this is one, skip it */
5686 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
5687 continue;
5688
Bram Moolenaara2031822006-03-07 22:29:51 +00005689 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01005690 if (!dir && (flags & EW_EXEC)
5691 && !mch_can_exe(p, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00005692 continue;
5693
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 if (--files_free == 0)
5695 {
5696 /* need more room in table of pointers */
5697 files_alloced += EXPL_ALLOC_INC;
5698 *file = (char_u **)vim_realloc(*file,
5699 sizeof(char_u **) * files_alloced);
5700 if (*file == NULL)
5701 {
5702 EMSG(_(e_outofmem));
5703 *num_file = 0;
5704 return FAIL;
5705 }
5706 files_free = EXPL_ALLOC_INC;
5707 }
5708 slash_adjust(p);
5709 if (dir)
5710 {
5711 /* For a directory we add a '/', unless it's already
5712 * there. */
5713 len = STRLEN(p);
5714 if (((*file)[*num_file] = alloc(len + 2)) != NULL)
5715 {
5716 STRCPY((*file)[*num_file], p);
Bram Moolenaar654b5b52006-06-22 17:47:10 +00005717 if (!after_pathsep((*file)[*num_file],
5718 (*file)[*num_file] + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {
5720 (*file)[*num_file][len] = psepc;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005721 (*file)[*num_file][len + 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
5723 }
5724 }
5725 else
5726 {
5727 (*file)[*num_file] = vim_strsave(p);
5728 }
5729
5730 /*
5731 * Error message already given by either alloc or vim_strsave.
5732 * Should return FAIL, but returning OK works also.
5733 */
5734 if ((*file)[*num_file] == NULL)
5735 break;
5736 (*num_file)++;
5737 }
5738 _fnexplodefree((char **)expl_files);
5739 }
5740 }
5741 return OK;
5742
5743#else /* __EMX__ */
Bram Moolenaarc7247912008-01-13 12:54:11 +00005744 /*
5745 * This is the non-OS/2 implementation (really Unix).
5746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 int j;
5748 char_u *tempname;
5749 char_u *command;
5750 FILE *fd;
5751 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005752#define STYLE_ECHO 0 /* use "echo", the default */
5753#define STYLE_GLOB 1 /* use "glob", for csh */
5754#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
5755#define STYLE_PRINT 3 /* use "print -N", for zsh */
5756#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
5757 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 int shell_style = STYLE_ECHO;
5759 int check_spaces;
5760 static int did_find_nul = FALSE;
5761 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005762 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005763 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764
5765 *num_file = 0; /* default: no files found */
5766 *file = NULL;
5767
5768 /*
5769 * If there are no wildcards, just copy the names to allocated memory.
5770 * Saves a lot of time, because we don't have to start a new shell.
5771 */
5772 if (!have_wildcard(num_pat, pat))
5773 return save_patterns(num_pat, pat, num_file, file);
5774
Bram Moolenaar0e634da2005-07-20 21:57:28 +00005775# ifdef HAVE_SANDBOX
5776 /* Don't allow any shell command in the sandbox. */
5777 if (sandbox != 0 && check_secure())
5778 return FAIL;
5779# endif
5780
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 /*
5782 * Don't allow the use of backticks in secure and restricted mode.
5783 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00005784 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 for (i = 0; i < num_pat; ++i)
5786 if (vim_strchr(pat[i], '`') != NULL
5787 && (check_restricted() || check_secure()))
5788 return FAIL;
5789
5790 /*
5791 * get a name for the temp file
5792 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005793 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 {
5795 EMSG(_(e_notmp));
5796 return FAIL;
5797 }
5798
5799 /*
5800 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00005801 * file.
5802 * STYLE_BT: NL separated
5803 * If expanding `cmd` execute it directly.
5804 * STYLE_GLOB: NUL separated
5805 * If we use *csh, "glob" will work better than "echo".
5806 * STYLE_PRINT: NL or NUL separated
5807 * If we use *zsh, "print -N" will work better than "glob".
5808 * STYLE_VIMGLOB: NL separated
5809 * If we use *sh*, we define "vimglob()".
5810 * STYLE_ECHO: space separated.
5811 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 */
5813 if (num_pat == 1 && *pat[0] == '`'
5814 && (len = STRLEN(pat[0])) > 2
5815 && *(pat[0] + len - 1) == '`')
5816 shell_style = STYLE_BT;
5817 else if ((len = STRLEN(p_sh)) >= 3)
5818 {
5819 if (STRCMP(p_sh + len - 3, "csh") == 0)
5820 shell_style = STYLE_GLOB;
5821 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
5822 shell_style = STYLE_PRINT;
5823 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00005824 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
5825 "sh") != NULL)
5826 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827
Bram Moolenaarc7247912008-01-13 12:54:11 +00005828 /* Compute the length of the command. We need 2 extra bytes: for the
5829 * optional '&' and for the NUL.
5830 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005832 if (shell_style == STYLE_VIMGLOB)
5833 len += STRLEN(sh_vimglob_func);
5834
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005835 for (i = 0; i < num_pat; ++i)
5836 {
5837 /* Count the length of the patterns in the same way as they are put in
5838 * "command" below. */
5839#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005841#else
5842 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00005843 for (j = 0; pat[i][j] != NUL; ++j)
5844 {
5845 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
5846 ++len; /* may add a backslash */
5847 ++len;
5848 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005849#endif
5850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 command = alloc(len);
5852 if (command == NULL)
5853 {
5854 /* out of memory */
5855 vim_free(tempname);
5856 return FAIL;
5857 }
5858
5859 /*
5860 * Build the shell command:
5861 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
5862 * recognizes this).
5863 * - Add the shell command to print the expanded names.
5864 * - Add the temp file name.
5865 * - Add the file name patterns.
5866 */
5867 if (shell_style == STYLE_BT)
5868 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00005869 /* change `command; command& ` to (command; command ) */
5870 STRCPY(command, "(");
5871 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005873 *p-- = ')'; /* remove last backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 while (p > command && vim_iswhite(*p))
5875 --p;
5876 if (*p == '&') /* remove trailing '&' */
5877 {
5878 ampersent = TRUE;
5879 *p = ' ';
5880 }
5881 STRCAT(command, ">");
5882 }
5883 else
5884 {
5885 if (flags & EW_NOTFOUND)
5886 STRCPY(command, "set nonomatch; ");
5887 else
5888 STRCPY(command, "unset nonomatch; ");
5889 if (shell_style == STYLE_GLOB)
5890 STRCAT(command, "glob >");
5891 else if (shell_style == STYLE_PRINT)
5892 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00005893 else if (shell_style == STYLE_VIMGLOB)
5894 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 else
5896 STRCAT(command, "echo >");
5897 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00005898
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00005900
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 if (shell_style != STYLE_BT)
5902 for (i = 0; i < num_pat; ++i)
5903 {
5904 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00005905 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005906 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907#ifdef USE_SYSTEM
5908 STRCAT(command, " \"");
5909 STRCAT(command, pat[i]);
5910 STRCAT(command, "\"");
5911#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00005912 int intick = FALSE;
5913
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 p = command + STRLEN(command);
5915 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00005916 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005917 {
5918 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00005919 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005920 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
5921 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005922 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00005923 * backslash inside backticks, before a special character
5924 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005925 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00005926 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
5927 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005928 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00005929 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005930 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02005931 else if (!intick
5932 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
5933 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00005934 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02005935 * when inside ``. And not for $var when EW_KEEPDOLLAR is
5936 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00005937 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00005938
5939 /* Copy one character. */
5940 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00005941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 *p = NUL;
5943#endif
5944 }
5945 if (flags & EW_SILENT)
5946 show_shell_mess = FALSE;
5947 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00005948 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949
5950 /*
5951 * Using zsh -G: If a pattern has no matches, it is just deleted from
5952 * the argument list, otherwise zsh gives an error message and doesn't
5953 * expand any other pattern.
5954 */
5955 if (shell_style == STYLE_PRINT)
5956 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
5957
5958 /*
5959 * If we use -f then shell variables set in .cshrc won't get expanded.
5960 * vi can do it, so we will too, but it is only necessary if there is a "$"
5961 * in one of the patterns, otherwise we can still use the fast option.
5962 */
5963 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
5964 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
5965
5966 /*
5967 * execute the shell command
5968 */
5969 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
5970
5971 /* When running in the background, give it some time to create the temp
5972 * file, but don't wait for it to finish. */
5973 if (ampersent)
5974 mch_delay(10L, TRUE);
5975
5976 extra_shell_arg = NULL; /* cleanup */
5977 show_shell_mess = TRUE;
5978 vim_free(command);
5979
Bram Moolenaarc7247912008-01-13 12:54:11 +00005980 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 {
5982 mch_remove(tempname);
5983 vim_free(tempname);
5984 /*
5985 * With interactive completion, the error message is not printed.
5986 * However with USE_SYSTEM, I don't know how to turn off error messages
5987 * from the shell, so screen may still get messed up -- webb.
5988 */
5989#ifndef USE_SYSTEM
5990 if (!(flags & EW_SILENT))
5991#endif
5992 {
5993 redraw_later_clear(); /* probably messed up screen */
5994 msg_putchar('\n'); /* clear bottom line quickly */
5995 cmdline_row = Rows - 1; /* continue on last line */
5996#ifdef USE_SYSTEM
5997 if (!(flags & EW_SILENT))
5998#endif
5999 {
6000 MSG(_(e_wildexpand));
6001 msg_start(); /* don't overwrite this message */
6002 }
6003 }
6004 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6005 * EW_NOTFOUND is given */
6006 if (shell_style == STYLE_BT)
6007 return FAIL;
6008 goto notfound;
6009 }
6010
6011 /*
6012 * read the names from the file into memory
6013 */
6014 fd = fopen((char *)tempname, READBIN);
6015 if (fd == NULL)
6016 {
6017 /* Something went wrong, perhaps a file name with a special char. */
6018 if (!(flags & EW_SILENT))
6019 {
6020 MSG(_(e_wildexpand));
6021 msg_start(); /* don't overwrite this message */
6022 }
6023 vim_free(tempname);
6024 goto notfound;
6025 }
6026 fseek(fd, 0L, SEEK_END);
6027 len = ftell(fd); /* get size of temp file */
6028 fseek(fd, 0L, SEEK_SET);
6029 buffer = alloc(len + 1);
6030 if (buffer == NULL)
6031 {
6032 /* out of memory */
6033 mch_remove(tempname);
6034 vim_free(tempname);
6035 fclose(fd);
6036 return FAIL;
6037 }
6038 i = fread((char *)buffer, 1, len, fd);
6039 fclose(fd);
6040 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006041 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 {
6043 /* unexpected read error */
6044 EMSG2(_(e_notread), tempname);
6045 vim_free(tempname);
6046 vim_free(buffer);
6047 return FAIL;
6048 }
6049 vim_free(tempname);
6050
Bram Moolenaarc7247912008-01-13 12:54:11 +00006051# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6053 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006054 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6056 *p++ = buffer[i];
6057 len = p - buffer;
6058# endif
6059
6060
6061 /* file names are separated with Space */
6062 if (shell_style == STYLE_ECHO)
6063 {
6064 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6065 p = buffer;
6066 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6067 {
6068 while (*p != ' ' && *p != '\n')
6069 ++p;
6070 p = skipwhite(p); /* skip to next entry */
6071 }
6072 }
6073 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006074 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 {
6076 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6077 p = buffer;
6078 for (i = 0; *p != NUL; ++i) /* count number of entries */
6079 {
6080 while (*p != '\n' && *p != NUL)
6081 ++p;
6082 if (*p != NUL)
6083 ++p;
6084 p = skipwhite(p); /* skip leading white space */
6085 }
6086 }
6087 /* file names are separated with NUL */
6088 else
6089 {
6090 /*
6091 * Some versions of zsh use spaces instead of NULs to separate
6092 * results. Only do this when there is no NUL before the end of the
6093 * buffer, otherwise we would never be able to use file names with
6094 * embedded spaces when zsh does use NULs.
6095 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6096 * don't check for spaces again.
6097 */
6098 check_spaces = FALSE;
6099 if (shell_style == STYLE_PRINT && !did_find_nul)
6100 {
6101 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006102 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006103 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 did_find_nul = TRUE;
6105 else
6106 check_spaces = TRUE;
6107 }
6108
6109 /*
6110 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6111 * already is one, for STYLE_GLOB it needs to be added.
6112 */
6113 if (len && buffer[len - 1] == NUL)
6114 --len;
6115 else
6116 buffer[len] = NUL;
6117 i = 0;
6118 for (p = buffer; p < buffer + len; ++p)
6119 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6120 {
6121 ++i;
6122 *p = NUL;
6123 }
6124 if (len)
6125 ++i; /* count last entry */
6126 }
6127 if (i == 0)
6128 {
6129 /*
6130 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6131 * /bin/sh will happily expand it to nothing rather than returning an
6132 * error; and hey, it's good to check anyway -- webb.
6133 */
6134 vim_free(buffer);
6135 goto notfound;
6136 }
6137 *num_file = i;
6138 *file = (char_u **)alloc(sizeof(char_u *) * i);
6139 if (*file == NULL)
6140 {
6141 /* out of memory */
6142 vim_free(buffer);
6143 return FAIL;
6144 }
6145
6146 /*
6147 * Isolate the individual file names.
6148 */
6149 p = buffer;
6150 for (i = 0; i < *num_file; ++i)
6151 {
6152 (*file)[i] = p;
6153 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006154 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6155 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006157 while (!(shell_style == STYLE_ECHO && *p == ' ')
6158 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 ++p;
6160 if (p == buffer + len) /* last entry */
6161 *p = NUL;
6162 else
6163 {
6164 *p++ = NUL;
6165 p = skipwhite(p); /* skip to next entry */
6166 }
6167 }
6168 else /* NUL separates */
6169 {
6170 while (*p && p < buffer + len) /* skip entry */
6171 ++p;
6172 ++p; /* skip NUL */
6173 }
6174 }
6175
6176 /*
6177 * Move the file names to allocated memory.
6178 */
6179 for (j = 0, i = 0; i < *num_file; ++i)
6180 {
6181 /* Require the files to exist. Helps when using /bin/sh */
6182 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6183 continue;
6184
6185 /* check if this entry should be included */
6186 dir = (mch_isdir((*file)[i]));
6187 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6188 continue;
6189
Bram Moolenaara2031822006-03-07 22:29:51 +00006190 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006191 if (!dir && (flags & EW_EXEC)
6192 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006193 continue;
6194
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6196 if (p)
6197 {
6198 STRCPY(p, (*file)[i]);
6199 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006200 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 (*file)[j++] = p;
6202 }
6203 }
6204 vim_free(buffer);
6205 *num_file = j;
6206
6207 if (*num_file == 0) /* rejected all entries */
6208 {
6209 vim_free(*file);
6210 *file = NULL;
6211 goto notfound;
6212 }
6213
6214 return OK;
6215
6216notfound:
6217 if (flags & EW_NOTFOUND)
6218 return save_patterns(num_pat, pat, num_file, file);
6219 return FAIL;
6220
6221#endif /* __EMX__ */
6222}
6223
6224#endif /* VMS */
6225
6226#ifndef __EMX__
6227 static int
6228save_patterns(num_pat, pat, num_file, file)
6229 int num_pat;
6230 char_u **pat;
6231 int *num_file;
6232 char_u ***file;
6233{
6234 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006235 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236
6237 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6238 if (*file == NULL)
6239 return FAIL;
6240 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006241 {
6242 s = vim_strsave(pat[i]);
6243 if (s != NULL)
6244 /* Be compatible with expand_filename(): halve the number of
6245 * backslashes. */
6246 backslash_halve(s);
6247 (*file)[i] = s;
6248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 *num_file = num_pat;
6250 return OK;
6251}
6252#endif
6253
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254/*
6255 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6256 * expand.
6257 */
6258 int
6259mch_has_exp_wildcard(p)
6260 char_u *p;
6261{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006262 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 if (*p == '\\' && p[1] != NUL)
6265 ++p;
6266 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 if (vim_strchr((char_u *)
6268#ifdef VMS
6269 "*?%"
6270#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272#endif
6273 , *p) != NULL)
6274 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 }
6276 return FALSE;
6277}
6278
6279/*
6280 * Return TRUE if the string "p" contains a wildcard.
6281 * Don't recognize '~' at the end as a wildcard.
6282 */
6283 int
6284mch_has_wildcard(p)
6285 char_u *p;
6286{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006287 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 if (*p == '\\' && p[1] != NUL)
6290 ++p;
6291 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 if (vim_strchr((char_u *)
6293#ifdef VMS
6294 "*?%$"
6295#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297#endif
6298 , *p) != NULL
6299 || (*p == '~' && p[1] != NUL))
6300 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 }
6302 return FALSE;
6303}
6304
6305#ifndef __EMX__
6306 static int
6307have_wildcard(num, file)
6308 int num;
6309 char_u **file;
6310{
6311 int i;
6312
6313 for (i = 0; i < num; i++)
6314 if (mch_has_wildcard(file[i]))
6315 return 1;
6316 return 0;
6317}
6318
6319 static int
6320have_dollars(num, file)
6321 int num;
6322 char_u **file;
6323{
6324 int i;
6325
6326 for (i = 0; i < num; i++)
6327 if (vim_strchr(file[i], '$') != NULL)
6328 return TRUE;
6329 return FALSE;
6330}
6331#endif /* ifndef __EMX__ */
6332
6333#ifndef HAVE_RENAME
6334/*
6335 * Scaled-down version of rename(), which is missing in Xenix.
6336 * This version can only move regular files and will fail if the
6337 * destination exists.
6338 */
6339 int
6340mch_rename(src, dest)
6341 const char *src, *dest;
6342{
6343 struct stat st;
6344
6345 if (stat(dest, &st) >= 0) /* fail if destination exists */
6346 return -1;
6347 if (link(src, dest) != 0) /* link file to new name */
6348 return -1;
6349 if (mch_remove(src) == 0) /* delete link to old name */
6350 return 0;
6351 return -1;
6352}
6353#endif /* !HAVE_RENAME */
6354
6355#ifdef FEAT_MOUSE_GPM
6356/*
6357 * Initializes connection with gpm (if it isn't already opened)
6358 * Return 1 if succeeded (or connection already opened), 0 if failed
6359 */
6360 static int
6361gpm_open()
6362{
6363 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
6364
6365 if (!gpm_flag)
6366 {
6367 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
6368 gpm_connect.defaultMask = ~GPM_HARD;
6369 /* Default handling for mouse move*/
6370 gpm_connect.minMod = 0; /* Handle any modifier keys */
6371 gpm_connect.maxMod = 0xffff;
6372 if (Gpm_Open(&gpm_connect, 0) > 0)
6373 {
6374 /* gpm library tries to handling TSTP causes
6375 * problems. Anyways, we close connection to Gpm whenever
6376 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006377 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006379# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 return 1; /* succeed */
6383 }
6384 if (gpm_fd == -2)
6385 Gpm_Close(); /* We don't want to talk to xterm via gpm */
6386 return 0;
6387 }
6388 return 1; /* already open */
6389}
6390
6391/*
6392 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 */
6394 static void
6395gpm_close()
6396{
6397 if (gpm_flag && gpm_fd >= 0) /* if Open */
6398 Gpm_Close();
6399}
6400
6401/* Reads gpm event and adds special keys to input buf. Returns length of
6402 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02006403 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 */
6405 static int
6406mch_gpm_process()
6407{
6408 int button;
6409 static Gpm_Event gpm_event;
6410 char_u string[6];
6411 int_u vim_modifiers;
6412 int row,col;
6413 unsigned char buttons_mask;
6414 unsigned char gpm_modifiers;
6415 static unsigned char old_buttons = 0;
6416
6417 Gpm_GetEvent(&gpm_event);
6418
6419#ifdef FEAT_GUI
6420 /* Don't put events in the input queue now. */
6421 if (hold_gui_events)
6422 return 0;
6423#endif
6424
6425 row = gpm_event.y - 1;
6426 col = gpm_event.x - 1;
6427
6428 string[0] = ESC; /* Our termcode */
6429 string[1] = 'M';
6430 string[2] = 'G';
6431 switch (GPM_BARE_EVENTS(gpm_event.type))
6432 {
6433 case GPM_DRAG:
6434 string[3] = MOUSE_DRAG;
6435 break;
6436 case GPM_DOWN:
6437 buttons_mask = gpm_event.buttons & ~old_buttons;
6438 old_buttons = gpm_event.buttons;
6439 switch (buttons_mask)
6440 {
6441 case GPM_B_LEFT:
6442 button = MOUSE_LEFT;
6443 break;
6444 case GPM_B_MIDDLE:
6445 button = MOUSE_MIDDLE;
6446 break;
6447 case GPM_B_RIGHT:
6448 button = MOUSE_RIGHT;
6449 break;
6450 default:
6451 return 0;
6452 /*Don't know what to do. Can more than one button be
6453 * reported in one event? */
6454 }
6455 string[3] = (char_u)(button | 0x20);
6456 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
6457 break;
6458 case GPM_UP:
6459 string[3] = MOUSE_RELEASE;
6460 old_buttons &= ~gpm_event.buttons;
6461 break;
6462 default:
6463 return 0;
6464 }
6465 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
6466 gpm_modifiers = gpm_event.modifiers;
6467 vim_modifiers = 0x0;
6468 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
6469 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
6470 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
6471 */
6472 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
6473 vim_modifiers |= MOUSE_SHIFT;
6474
6475 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
6476 vim_modifiers |= MOUSE_CTRL;
6477 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
6478 vim_modifiers |= MOUSE_ALT;
6479 string[3] |= vim_modifiers;
6480 string[4] = (char_u)(col + ' ' + 1);
6481 string[5] = (char_u)(row + ' ' + 1);
6482 add_to_input_buf(string, 6);
6483 return 6;
6484}
6485#endif /* FEAT_MOUSE_GPM */
6486
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006487#ifdef FEAT_SYSMOUSE
6488/*
6489 * Initialize connection with sysmouse.
6490 * Let virtual console inform us with SIGUSR2 for pending sysmouse
6491 * output, any sysmouse output than will be processed via sig_sysmouse().
6492 * Return OK if succeeded, FAIL if failed.
6493 */
6494 static int
6495sysmouse_open()
6496{
6497 struct mouse_info mouse;
6498
6499 mouse.operation = MOUSE_MODE;
6500 mouse.u.mode.mode = 0;
6501 mouse.u.mode.signal = SIGUSR2;
6502 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
6503 {
6504 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
6505 mouse.operation = MOUSE_SHOW;
6506 ioctl(1, CONS_MOUSECTL, &mouse);
6507 return OK;
6508 }
6509 return FAIL;
6510}
6511
6512/*
6513 * Stop processing SIGUSR2 signals, and also make sure that
6514 * virtual console do not send us any sysmouse related signal.
6515 */
6516 static void
6517sysmouse_close()
6518{
6519 struct mouse_info mouse;
6520
6521 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
6522 mouse.operation = MOUSE_MODE;
6523 mouse.u.mode.mode = 0;
6524 mouse.u.mode.signal = 0;
6525 ioctl(1, CONS_MOUSECTL, &mouse);
6526}
6527
6528/*
6529 * Gets info from sysmouse and adds special keys to input buf.
6530 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006531 static RETSIGTYPE
6532sig_sysmouse SIGDEFARG(sigarg)
6533{
6534 struct mouse_info mouse;
6535 struct video_info video;
6536 char_u string[6];
6537 int row, col;
6538 int button;
6539 int buttons;
6540 static int oldbuttons = 0;
6541
6542#ifdef FEAT_GUI
6543 /* Don't put events in the input queue now. */
6544 if (hold_gui_events)
6545 return;
6546#endif
6547
6548 mouse.operation = MOUSE_GETINFO;
6549 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
6550 && ioctl(1, FBIO_MODEINFO, &video) != -1
6551 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
6552 && video.vi_cheight > 0 && video.vi_cwidth > 0)
6553 {
6554 row = mouse.u.data.y / video.vi_cheight;
6555 col = mouse.u.data.x / video.vi_cwidth;
6556 buttons = mouse.u.data.buttons;
6557 string[0] = ESC; /* Our termcode */
6558 string[1] = 'M';
6559 string[2] = 'S';
6560 if (oldbuttons == buttons && buttons != 0)
6561 {
6562 button = MOUSE_DRAG;
6563 }
6564 else
6565 {
6566 switch (buttons)
6567 {
6568 case 0:
6569 button = MOUSE_RELEASE;
6570 break;
6571 case 1:
6572 button = MOUSE_LEFT;
6573 break;
6574 case 2:
6575 button = MOUSE_MIDDLE;
6576 break;
6577 case 4:
6578 button = MOUSE_RIGHT;
6579 break;
6580 default:
6581 return;
6582 }
6583 oldbuttons = buttons;
6584 }
6585 string[3] = (char_u)(button);
6586 string[4] = (char_u)(col + ' ' + 1);
6587 string[5] = (char_u)(row + ' ' + 1);
6588 add_to_input_buf(string, 6);
6589 }
6590 return;
6591}
6592#endif /* FEAT_SYSMOUSE */
6593
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01006595typedef char_u * (*STRPROCSTR)(char_u *);
6596typedef char_u * (*INTPROCSTR)(int);
6597typedef int (*STRPROCINT)(char_u *);
6598typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599
6600/*
6601 * Call a DLL routine which takes either a string or int param
6602 * and returns an allocated string.
6603 */
6604 int
6605mch_libcall(libname, funcname, argstring, argint, string_result, number_result)
6606 char_u *libname;
6607 char_u *funcname;
6608 char_u *argstring; /* NULL when using a argint */
6609 int argint;
6610 char_u **string_result;/* NULL when using number_result */
6611 int *number_result;
6612{
6613# if defined(USE_DLOPEN)
6614 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006615 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616# else
6617 shl_t hinstLib;
6618# endif
6619 STRPROCSTR ProcAdd;
6620 INTPROCSTR ProcAddI;
6621 char_u *retval_str = NULL;
6622 int retval_int = 0;
6623 int success = FALSE;
6624
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006625 /*
6626 * Get a handle to the DLL module.
6627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006629 /* First clear any error, it's not cleared by the dlopen() call. */
6630 (void)dlerror();
6631
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 hinstLib = dlopen((char *)libname, RTLD_LAZY
6633# ifdef RTLD_LOCAL
6634 | RTLD_LOCAL
6635# endif
6636 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006637 if (hinstLib == NULL)
6638 {
6639 /* "dlerr" must be used before dlclose() */
6640 dlerr = (char *)dlerror();
6641 if (dlerr != NULL)
6642 EMSG2(_("dlerror = \"%s\""), dlerr);
6643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644# else
6645 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
6646# endif
6647
6648 /* If the handle is valid, try to get the function address. */
6649 if (hinstLib != NULL)
6650 {
6651# ifdef HAVE_SETJMP_H
6652 /*
6653 * Catch a crash when calling the library function. For example when
6654 * using a number where a string pointer is expected.
6655 */
6656 mch_startjmp();
6657 if (SETJMP(lc_jump_env) != 0)
6658 {
6659 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00006660# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006661 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00006662# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 mch_didjmp();
6664 }
6665 else
6666# endif
6667 {
6668 retval_str = NULL;
6669 retval_int = 0;
6670
6671 if (argstring != NULL)
6672 {
6673# if defined(USE_DLOPEN)
6674 ProcAdd = (STRPROCSTR)dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006675 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676# else
6677 if (shl_findsym(&hinstLib, (const char *)funcname,
6678 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
6679 ProcAdd = NULL;
6680# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006681 if ((success = (ProcAdd != NULL
6682# if defined(USE_DLOPEN)
6683 && dlerr == NULL
6684# endif
6685 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
6687 if (string_result == NULL)
6688 retval_int = ((STRPROCINT)ProcAdd)(argstring);
6689 else
6690 retval_str = (ProcAdd)(argstring);
6691 }
6692 }
6693 else
6694 {
6695# if defined(USE_DLOPEN)
6696 ProcAddI = (INTPROCSTR)dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006697 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698# else
6699 if (shl_findsym(&hinstLib, (const char *)funcname,
6700 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
6701 ProcAddI = NULL;
6702# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006703 if ((success = (ProcAddI != NULL
6704# if defined(USE_DLOPEN)
6705 && dlerr == NULL
6706# endif
6707 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 {
6709 if (string_result == NULL)
6710 retval_int = ((INTPROCINT)ProcAddI)(argint);
6711 else
6712 retval_str = (ProcAddI)(argint);
6713 }
6714 }
6715
6716 /* Save the string before we free the library. */
6717 /* Assume that a "1" or "-1" result is an illegal pointer. */
6718 if (string_result == NULL)
6719 *number_result = retval_int;
6720 else if (retval_str != NULL
6721 && retval_str != (char_u *)1
6722 && retval_str != (char_u *)-1)
6723 *string_result = vim_strsave(retval_str);
6724 }
6725
6726# ifdef HAVE_SETJMP_H
6727 mch_endjmp();
6728# ifdef SIGHASARG
6729 if (lc_signal != 0)
6730 {
6731 int i;
6732
6733 /* try to find the name of this signal */
6734 for (i = 0; signal_info[i].sig != -1; i++)
6735 if (lc_signal == signal_info[i].sig)
6736 break;
6737 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
6738 }
6739# endif
6740# endif
6741
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006743 /* "dlerr" must be used before dlclose() */
6744 if (dlerr != NULL)
6745 EMSG2(_("dlerror = \"%s\""), dlerr);
6746
6747 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 (void)dlclose(hinstLib);
6749# else
6750 (void)shl_unload(hinstLib);
6751# endif
6752 }
6753
6754 if (!success)
6755 {
6756 EMSG2(_(e_libcall), funcname);
6757 return FAIL;
6758 }
6759
6760 return OK;
6761}
6762#endif
6763
6764#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
6765static int xterm_trace = -1; /* default: disabled */
6766static int xterm_button;
6767
6768/*
6769 * Setup a dummy window for X selections in a terminal.
6770 */
6771 void
6772setup_term_clip()
6773{
6774 int z = 0;
6775 char *strp = "";
6776 Widget AppShell;
6777
6778 if (!x_connect_to_server())
6779 return;
6780
6781 open_app_context();
6782 if (app_context != NULL && xterm_Shell == (Widget)0)
6783 {
6784 int (*oldhandler)();
6785#if defined(HAVE_SETJMP_H)
6786 int (*oldIOhandler)();
6787#endif
6788# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
6789 struct timeval start_tv;
6790
6791 if (p_verbose > 0)
6792 gettimeofday(&start_tv, NULL);
6793# endif
6794
6795 /* Ignore X errors while opening the display */
6796 oldhandler = XSetErrorHandler(x_error_check);
6797
6798#if defined(HAVE_SETJMP_H)
6799 /* Ignore X IO errors while opening the display */
6800 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
6801 mch_startjmp();
6802 if (SETJMP(lc_jump_env) != 0)
6803 {
6804 mch_didjmp();
6805 xterm_dpy = NULL;
6806 }
6807 else
6808#endif
6809 {
6810 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
6811 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
6812#if defined(HAVE_SETJMP_H)
6813 mch_endjmp();
6814#endif
6815 }
6816
6817#if defined(HAVE_SETJMP_H)
6818 /* Now handle X IO errors normally. */
6819 (void)XSetIOErrorHandler(oldIOhandler);
6820#endif
6821 /* Now handle X errors normally. */
6822 (void)XSetErrorHandler(oldhandler);
6823
6824 if (xterm_dpy == NULL)
6825 {
6826 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006827 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 return;
6829 }
6830
6831 /* Catch terminating error of the X server connection. */
6832 (void)XSetIOErrorHandler(x_IOerror_handler);
6833
6834# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
6835 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006836 {
6837 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 xopen_message(&start_tv);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006839 verbose_leave();
6840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841# endif
6842
6843 /* Create a Shell to make converters work. */
6844 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
6845 applicationShellWidgetClass, xterm_dpy,
6846 NULL);
6847 if (AppShell == (Widget)0)
6848 return;
6849 xterm_Shell = XtVaCreatePopupShell("VIM",
6850 topLevelShellWidgetClass, AppShell,
6851 XtNmappedWhenManaged, 0,
6852 XtNwidth, 1,
6853 XtNheight, 1,
6854 NULL);
6855 if (xterm_Shell == (Widget)0)
6856 return;
6857
6858 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02006859 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 if (x11_display == NULL)
6861 x11_display = xterm_dpy;
6862
6863 XtRealizeWidget(xterm_Shell);
6864 XSync(xterm_dpy, False);
6865 xterm_update();
6866 }
6867 if (xterm_Shell != (Widget)0)
6868 {
6869 clip_init(TRUE);
6870 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
6871 x11_window = (Window)atol(strp);
6872 /* Check if $WINDOWID is valid. */
6873 if (test_x11_window(xterm_dpy) == FAIL)
6874 x11_window = 0;
6875 if (x11_window != 0)
6876 xterm_trace = 0;
6877 }
6878}
6879
6880 void
6881start_xterm_trace(button)
6882 int button;
6883{
6884 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
6885 return;
6886 xterm_trace = 1;
6887 xterm_button = button;
6888 do_xterm_trace();
6889}
6890
6891
6892 void
6893stop_xterm_trace()
6894{
6895 if (xterm_trace < 0)
6896 return;
6897 xterm_trace = 0;
6898}
6899
6900/*
6901 * Query the xterm pointer and generate mouse termcodes if necessary
6902 * return TRUE if dragging is active, else FALSE
6903 */
6904 static int
6905do_xterm_trace()
6906{
6907 Window root, child;
6908 int root_x, root_y;
6909 int win_x, win_y;
6910 int row, col;
6911 int_u mask_return;
6912 char_u buf[50];
6913 char_u *strp;
6914 long got_hints;
6915 static char_u *mouse_code;
6916 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
6917 static int prev_row = 0, prev_col = 0;
6918 static XSizeHints xterm_hints;
6919
6920 if (xterm_trace <= 0)
6921 return FALSE;
6922
6923 if (xterm_trace == 1)
6924 {
6925 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00006926 * have changed recently. */
6927 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
6928 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 || xterm_hints.width_inc <= 1
6930 || xterm_hints.height_inc <= 1)
6931 {
6932 xterm_trace = -1; /* Not enough data -- disable tracing */
6933 return FALSE;
6934 }
6935
6936 /* Rely on the same mouse code for the duration of this */
6937 mouse_code = find_termcode(mouse_name);
6938 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02006939 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 xterm_trace = 2;
6941
6942 /* Find the offset of the chars, there might be a scrollbar on the
6943 * left of the window and/or a menu on the top (eterm etc.) */
6944 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
6945 &win_x, &win_y, &mask_return);
6946 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
6947 - (xterm_hints.height_inc / 2);
6948 if (xterm_hints.y <= xterm_hints.height_inc / 2)
6949 xterm_hints.y = 2;
6950 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
6951 - (xterm_hints.width_inc / 2);
6952 if (xterm_hints.x <= xterm_hints.width_inc / 2)
6953 xterm_hints.x = 2;
6954 return TRUE;
6955 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006956 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {
6958 xterm_trace = 0;
6959 return FALSE;
6960 }
6961
6962 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
6963 &win_x, &win_y, &mask_return);
6964
6965 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
6966 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
6967 if (row == prev_row && col == prev_col)
6968 return TRUE;
6969
6970 STRCPY(buf, mouse_code);
6971 strp = buf + STRLEN(buf);
6972 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
6973 *strp++ = (char_u)(col + ' ' + 1);
6974 *strp++ = (char_u)(row + ' ' + 1);
6975 *strp = 0;
6976 add_to_input_buf(buf, STRLEN(buf));
6977
6978 prev_row = row;
6979 prev_col = col;
6980 return TRUE;
6981}
6982
6983# if defined(FEAT_GUI) || defined(PROTO)
6984/*
6985 * Destroy the display, window and app_context. Required for GTK.
6986 */
6987 void
6988clear_xterm_clip()
6989{
6990 if (xterm_Shell != (Widget)0)
6991 {
6992 XtDestroyWidget(xterm_Shell);
6993 xterm_Shell = (Widget)0;
6994 }
6995 if (xterm_dpy != NULL)
6996 {
Bram Moolenaare8208012008-06-20 09:59:25 +00006997# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 /* Lesstif and Solaris crash here, lose some memory */
6999 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 if (x11_display == xterm_dpy)
7002 x11_display = NULL;
7003 xterm_dpy = NULL;
7004 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007005# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 if (app_context != (XtAppContext)NULL)
7007 {
7008 /* Lesstif and Solaris crash here, lose some memory */
7009 XtDestroyApplicationContext(app_context);
7010 app_context = (XtAppContext)NULL;
7011 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013}
7014# endif
7015
7016/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007017 * Catch up with GUI or X events.
7018 */
7019 static void
7020clip_update()
7021{
7022# ifdef FEAT_GUI
7023 if (gui.in_use)
7024 gui_mch_update();
7025 else
7026# endif
7027 if (xterm_Shell != (Widget)0)
7028 xterm_update();
7029}
7030
7031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 * Catch up with any queued X events. This may put keyboard input into the
7033 * input buffer, call resize call-backs, trigger timers etc. If there is
7034 * nothing in the X event queue (& no timers pending), then we return
7035 * immediately.
7036 */
7037 static void
7038xterm_update()
7039{
7040 XEvent event;
7041
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007042 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007044 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007046 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007047 break;
7048
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007049 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007050 {
7051 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007052 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007053#ifdef FEAT_CLIENTSERVER
7054 {
7055 XPropertyEvent *e = (XPropertyEvent *)&event;
7056
7057 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007059 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007062 XtDispatchEvent(&event);
7063 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007064 else
7065 {
7066 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007067 XtAppProcessEvent(app_context, mask);
7068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 }
7070}
7071
7072 int
7073clip_xterm_own_selection(cbd)
7074 VimClipboard *cbd;
7075{
7076 if (xterm_Shell != (Widget)0)
7077 return clip_x11_own_selection(xterm_Shell, cbd);
7078 return FAIL;
7079}
7080
7081 void
7082clip_xterm_lose_selection(cbd)
7083 VimClipboard *cbd;
7084{
7085 if (xterm_Shell != (Widget)0)
7086 clip_x11_lose_selection(xterm_Shell, cbd);
7087}
7088
7089 void
7090clip_xterm_request_selection(cbd)
7091 VimClipboard *cbd;
7092{
7093 if (xterm_Shell != (Widget)0)
7094 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7095}
7096
7097 void
7098clip_xterm_set_selection(cbd)
7099 VimClipboard *cbd;
7100{
7101 clip_x11_set_selection(cbd);
7102}
7103#endif
7104
7105
7106#if defined(USE_XSMP) || defined(PROTO)
7107/*
7108 * Code for X Session Management Protocol.
7109 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007110static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7111static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7112static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7113static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7114static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
7116
7117# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007118static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119
7120/*
7121 * This is our chance to ask the user if they want to save,
7122 * or abort the logout
7123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 static void
7125xsmp_handle_interaction(smc_conn, client_data)
7126 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007127 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 cmdmod_T save_cmdmod;
7130 int cancel_shutdown = False;
7131
7132 save_cmdmod = cmdmod;
7133 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007134 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 /* Mustn't logout */
7136 cancel_shutdown = True;
7137 cmdmod = save_cmdmod;
7138 setcursor(); /* position cursor */
7139 out_flush();
7140
7141 /* Done interaction */
7142 SmcInteractDone(smc_conn, cancel_shutdown);
7143
7144 /* Finish off
7145 * Only end save-yourself here if we're not cancelling shutdown;
7146 * we'll get a cancelled callback later in which we'll end it.
7147 * Hopefully get around glitchy SMs (like GNOME-1)
7148 */
7149 if (!cancel_shutdown)
7150 {
7151 xsmp.save_yourself = False;
7152 SmcSaveYourselfDone(smc_conn, True);
7153 }
7154}
7155# endif
7156
7157/*
7158 * Callback that starts save-yourself.
7159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 static void
7161xsmp_handle_save_yourself(smc_conn, client_data, save_type,
7162 shutdown, interact_style, fast)
7163 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007164 SmPointer client_data UNUSED;
7165 int save_type UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 Bool shutdown;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007167 int interact_style UNUSED;
7168 Bool fast UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169{
7170 /* Handle already being in saveyourself */
7171 if (xsmp.save_yourself)
7172 SmcSaveYourselfDone(smc_conn, True);
7173 xsmp.save_yourself = True;
7174 xsmp.shutdown = shutdown;
7175
7176 /* First up, preserve all files */
7177 out_flush();
7178 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7179
7180 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007181 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182
7183# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7184 /* Now see if we can ask about unsaved files */
7185 if (shutdown && !fast && gui.in_use)
7186 /* Need to interact with user, but need SM's permission */
7187 SmcInteractRequest(smc_conn, SmDialogError,
7188 xsmp_handle_interaction, client_data);
7189 else
7190# endif
7191 {
7192 /* Can stop the cycle here */
7193 SmcSaveYourselfDone(smc_conn, True);
7194 xsmp.save_yourself = False;
7195 }
7196}
7197
7198
7199/*
7200 * Callback to warn us of imminent death.
7201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 static void
7203xsmp_die(smc_conn, client_data)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007204 SmcConn smc_conn UNUSED;
7205 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206{
7207 xsmp_close();
7208
7209 /* quit quickly leaving swapfiles for modified buffers behind */
7210 getout_preserve_modified(0);
7211}
7212
7213
7214/*
7215 * Callback to tell us that save-yourself has completed.
7216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 static void
7218xsmp_save_complete(smc_conn, client_data)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007219 SmcConn smc_conn UNUSED;
7220 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
7222 xsmp.save_yourself = False;
7223}
7224
7225
7226/*
7227 * Callback to tell us that an instigated shutdown was cancelled
7228 * (maybe even by us)
7229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 static void
7231xsmp_shutdown_cancelled(smc_conn, client_data)
7232 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007233 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234{
7235 if (xsmp.save_yourself)
7236 SmcSaveYourselfDone(smc_conn, True);
7237 xsmp.save_yourself = False;
7238 xsmp.shutdown = False;
7239}
7240
7241
7242/*
7243 * Callback to tell us that a new ICE connection has been established.
7244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 static void
7246xsmp_ice_connection(iceConn, clientData, opening, watchData)
7247 IceConn iceConn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007248 IcePointer clientData UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 Bool opening;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007250 IcePointer *watchData UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
7252 /* Intercept creation of ICE connection fd */
7253 if (opening)
7254 {
7255 xsmp_icefd = IceConnectionNumber(iceConn);
7256 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7257 }
7258}
7259
7260
7261/* Handle any ICE processing that's required; return FAIL if SM lost */
7262 int
7263xsmp_handle_requests()
7264{
7265 Bool rep;
7266
7267 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7268 == IceProcessMessagesIOError)
7269 {
7270 /* Lost ICE */
7271 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007272 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 xsmp_close();
7274 return FAIL;
7275 }
7276 else
7277 return OK;
7278}
7279
7280static int dummy;
7281
7282/* Set up X Session Management Protocol */
7283 void
7284xsmp_init(void)
7285{
7286 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 SmcCallbacks smcallbacks;
7288#if 0
7289 SmPropValue smname;
7290 SmProp smnameprop;
7291 SmProp *smprops[1];
7292#endif
7293
7294 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007295 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
7297 xsmp.save_yourself = xsmp.shutdown = False;
7298
7299 /* Set up SM callbacks - must have all, even if they're not used */
7300 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7301 smcallbacks.save_yourself.client_data = NULL;
7302 smcallbacks.die.callback = xsmp_die;
7303 smcallbacks.die.client_data = NULL;
7304 smcallbacks.save_complete.callback = xsmp_save_complete;
7305 smcallbacks.save_complete.client_data = NULL;
7306 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7307 smcallbacks.shutdown_cancelled.client_data = NULL;
7308
7309 /* Set up a watch on ICE connection creations. The "dummy" argument is
7310 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7311 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7312 {
7313 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007314 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 return;
7316 }
7317
7318 /* Create an SM connection */
7319 xsmp.smcconn = SmcOpenConnection(
7320 NULL,
7321 NULL,
7322 SmProtoMajor,
7323 SmProtoMinor,
7324 SmcSaveYourselfProcMask | SmcDieProcMask
7325 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
7326 &smcallbacks,
7327 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00007328 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 sizeof(errorstring),
7330 errorstring);
7331 if (xsmp.smcconn == NULL)
7332 {
7333 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00007334
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007336 {
7337 vim_snprintf(errorreport, sizeof(errorreport),
7338 _("XSMP SmcOpenConnection failed: %s"), errorstring);
7339 verb_msg((char_u *)errorreport);
7340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 return;
7342 }
7343 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
7344
7345#if 0
7346 /* ID ourselves */
7347 smname.value = "vim";
7348 smname.length = 3;
7349 smnameprop.name = "SmProgram";
7350 smnameprop.type = "SmARRAY8";
7351 smnameprop.num_vals = 1;
7352 smnameprop.vals = &smname;
7353
7354 smprops[0] = &smnameprop;
7355 SmcSetProperties(xsmp.smcconn, 1, smprops);
7356#endif
7357}
7358
7359
7360/* Shut down XSMP comms. */
7361 void
7362xsmp_close()
7363{
7364 if (xsmp_icefd != -1)
7365 {
7366 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00007367 if (xsmp.clientid != NULL)
7368 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00007369 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 xsmp_icefd = -1;
7371 }
7372}
7373#endif /* USE_XSMP */
7374
7375
7376#ifdef EBCDIC
7377/* Translate character to its CTRL- value */
7378char CtrlTable[] =
7379{
7380/* 00 - 5E */
7381 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7382 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7383 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7384 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7385 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7386 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7387/* ^ */ 0x1E,
7388/* - */ 0x1F,
7389/* 61 - 6C */
7390 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7391/* _ */ 0x1F,
7392/* 6E - 80 */
7393 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7394/* a */ 0x01,
7395/* b */ 0x02,
7396/* c */ 0x03,
7397/* d */ 0x37,
7398/* e */ 0x2D,
7399/* f */ 0x2E,
7400/* g */ 0x2F,
7401/* h */ 0x16,
7402/* i */ 0x05,
7403/* 8A - 90 */
7404 0, 0, 0, 0, 0, 0, 0,
7405/* j */ 0x15,
7406/* k */ 0x0B,
7407/* l */ 0x0C,
7408/* m */ 0x0D,
7409/* n */ 0x0E,
7410/* o */ 0x0F,
7411/* p */ 0x10,
7412/* q */ 0x11,
7413/* r */ 0x12,
7414/* 9A - A1 */
7415 0, 0, 0, 0, 0, 0, 0, 0,
7416/* s */ 0x13,
7417/* t */ 0x3C,
7418/* u */ 0x3D,
7419/* v */ 0x32,
7420/* w */ 0x26,
7421/* x */ 0x18,
7422/* y */ 0x19,
7423/* z */ 0x3F,
7424/* AA - AC */
7425 0, 0, 0,
7426/* [ */ 0x27,
7427/* AE - BC */
7428 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7429/* ] */ 0x1D,
7430/* BE - C0 */ 0, 0, 0,
7431/* A */ 0x01,
7432/* B */ 0x02,
7433/* C */ 0x03,
7434/* D */ 0x37,
7435/* E */ 0x2D,
7436/* F */ 0x2E,
7437/* G */ 0x2F,
7438/* H */ 0x16,
7439/* I */ 0x05,
7440/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
7441/* J */ 0x15,
7442/* K */ 0x0B,
7443/* L */ 0x0C,
7444/* M */ 0x0D,
7445/* N */ 0x0E,
7446/* O */ 0x0F,
7447/* P */ 0x10,
7448/* Q */ 0x11,
7449/* R */ 0x12,
7450/* DA - DF */ 0, 0, 0, 0, 0, 0,
7451/* \ */ 0x1C,
7452/* E1 */ 0,
7453/* S */ 0x13,
7454/* T */ 0x3C,
7455/* U */ 0x3D,
7456/* V */ 0x32,
7457/* W */ 0x26,
7458/* X */ 0x18,
7459/* Y */ 0x19,
7460/* Z */ 0x3F,
7461/* EA - FF*/ 0, 0, 0, 0, 0, 0,
7462 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7463};
7464
7465char MetaCharTable[]=
7466{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7467 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
7468 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
7469 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
7470 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
7471};
7472
7473
7474/* TODO: Use characters NOT numbers!!! */
7475char CtrlCharTable[]=
7476{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7477 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
7478 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
7479 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
7480 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
7481};
7482
7483
7484#endif