blob: d2e1c79bd9a962dbdf7be7eaab6d2c9349ac47a4 [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)
81extern int select __ARGS((int, fd_set *, fd_set *, fd_set *, struct timeval *));
82#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
101static void gpm_close __ARGS((void));
102static int gpm_open __ARGS((void));
103static int mch_gpm_process __ARGS((void));
104#endif
105
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000106#ifdef FEAT_SYSMOUSE
107# include <sys/consio.h>
108# include <sys/fbio.h>
109
110static int sysmouse_open __ARGS((void));
111static void sysmouse_close __ARGS((void));
112static RETSIGTYPE sig_sysmouse __ARGS(SIGPROTOARG);
113#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 Moolenaar090cfc12013-03-19 12:35:42 +0100143static void clip_update __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144static void xterm_update __ARGS((void));
145# 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
153static int get_x11_windis __ARGS((void));
154static void set_x11_title __ARGS((char_u *));
155static void set_x11_icon __ARGS((char_u *));
156# endif
157#endif
158
159#ifdef FEAT_TITLE
160static int get_x11_title __ARGS((int));
161static int get_x11_icon __ARGS((int));
162
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
169static void may_core_dump __ARGS((void));
170
Bram Moolenaar205b8862011-09-07 15:04:31 +0200171#ifdef HAVE_UNION_WAIT
172typedef union wait waitstatus;
173#else
174typedef int waitstatus;
175#endif
Bram Moolenaar9f118812011-09-08 23:24:14 +0200176static pid_t wait4pid __ARGS((pid_t, waitstatus *));
Bram Moolenaar205b8862011-09-07 15:04:31 +0200177
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178static int WaitForChar __ARGS((long));
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100179#if defined(__BEOS__) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180int RealWaitForChar __ARGS((int, long, int *));
181#else
182static int RealWaitForChar __ARGS((int, long, int *));
183#endif
184
185#ifdef FEAT_XCLIPBOARD
186static int do_xterm_trace __ARGS((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
190static void handle_resize __ARGS((void));
191
192#if defined(SIGWINCH)
193static RETSIGTYPE sig_winch __ARGS(SIGPROTOARG);
194#endif
195#if defined(SIGINT)
196static RETSIGTYPE catch_sigint __ARGS(SIGPROTOARG);
197#endif
198#if defined(SIGPWR)
199static RETSIGTYPE catch_sigpwr __ARGS(SIGPROTOARG);
200#endif
201#if defined(SIGALRM) && defined(FEAT_X11) \
202 && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
203# define SET_SIG_ALARM
204static RETSIGTYPE sig_alarm __ARGS(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
208static RETSIGTYPE deathtrap __ARGS(SIGPROTOARG);
209
Bram Moolenaardf177f62005-02-22 08:39:57 +0000210static void catch_int_signal __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211static void set_signals __ARGS((void));
212static void catch_signals __ARGS((RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)()));
213#ifndef __EMX__
214static int have_wildcard __ARGS((int, char_u **));
215static int have_dollars __ARGS((int, char_u **));
216#endif
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#ifndef __EMX__
219static int save_patterns __ARGS((int num_pat, char_u **pat, int *num_file, char_u ***file));
220#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 */
699static void check_stack_growth __ARGS((char *p));
700static 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
823static void init_signal_stack __ARGS((void));
824static 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. */
836 extern int sigaltstack __ARGS((const struct sigaltstack *ss, struct sigaltstack *oss));
837# 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 Moolenaar071d4272004-06-13 20:20:40 +00001140static RETSIGTYPE sigcont_handler __ARGS(SIGPROTOARG);
1141
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)
1154static void loose_clipboard __ARGS((void));
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001155# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001156static void save_clipboard __ARGS((void));
1157static void restore_clipboard __ARGS((void));
1158
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
1492static void xopen_message __ARGS((struct timeval *tvp));
1493
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 */
1516static int x_error_handler __ARGS((Display *dpy, XErrorEvent *error_event));
1517static int x_error_check __ARGS((Display *dpy, XErrorEvent *error_event));
1518static int x_connect_to_server __ARGS((void));
1519static int test_x11_window __ARGS((Display *dpy));
1520
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 */
1559static int x_IOerror_check __ARGS((Display *dpy));
1560
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 */
1576static int x_IOerror_handler __ARGS((Display *dpy));
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001577static void may_restore_clipboard __ARGS((void));
1578static 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
1679static int get_x11_thing __ARGS((int get_title, int test_only));
1680
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)
2396static char *strerror __ARGS((int));
2397
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 {
2825 MSG_PUTS(_("Could not set security context "));
2826 MSG_PUTS(name);
2827 MSG_PUTS(_(" for "));
2828 msg_outtrans(to_file);
2829 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 */
2845 MSG_PUTS(_("Could not get security context "));
2846 MSG_PUTS(name);
2847 MSG_PUTS(_(" for "));
2848 msg_outtrans(from_file);
2849 MSG_PUTS(_(". Removing it!\n"));
2850 /* 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 Moolenaar071d4272004-06-13 20:20:40 +00003040static int executable_file __ARGS((char_u *name));
3041
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 Moolenaar071d4272004-06-13 20:20:40 +00003250static void exit_scroll __ARGS((void));
3251
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 Moolenaar67c53842010-05-22 18:28:27 +02005192#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005193 int nb_fd = netbeans_filedesc();
Bram Moolenaar67c53842010-05-22 18:28:27 +02005194#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005195#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 static int busy = FALSE;
5197
5198 /* May retry getting characters after an event was handled. */
5199# define MAY_LOOP
5200
5201# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
5202 /* Remember at what time we started, so that we know how much longer we
5203 * should wait after being interrupted. */
5204# define USE_START_TV
5205 struct timeval start_tv;
5206
5207 if (msec > 0 && (
5208# ifdef FEAT_XCLIPBOARD
5209 xterm_Shell != (Widget)0
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005210# if defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 ||
5212# endif
5213# endif
5214# ifdef USE_XSMP
5215 xsmp_icefd != -1
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005216# ifdef FEAT_MZSCHEME
5217 ||
5218# endif
5219# endif
5220# ifdef FEAT_MZSCHEME
5221 (mzthreads_allowed() && p_mzq > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222# endif
5223 ))
5224 gettimeofday(&start_tv, NULL);
5225# endif
5226
5227 /* Handle being called recursively. This may happen for the session
5228 * manager stuff, it may save the file, which does a breakcheck. */
5229 if (busy)
5230 return 0;
5231#endif
5232
5233#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005234 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235#endif
5236 {
5237#ifdef MAY_LOOP
5238 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005239# ifdef FEAT_MZSCHEME
5240 int mzquantum_used = FALSE;
5241# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242#endif
5243#ifndef HAVE_SELECT
Bram Moolenaar67c53842010-05-22 18:28:27 +02005244 struct pollfd fds[6];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 int nfd;
5246# ifdef FEAT_XCLIPBOARD
5247 int xterm_idx = -1;
5248# endif
5249# ifdef FEAT_MOUSE_GPM
5250 int gpm_idx = -1;
5251# endif
5252# ifdef USE_XSMP
5253 int xsmp_idx = -1;
5254# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005255# ifdef FEAT_NETBEANS_INTG
5256 int nb_idx = -1;
5257# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005258 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005260# ifdef FEAT_MZSCHEME
5261 mzvim_check_threads();
5262 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5263 {
5264 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
5265 mzquantum_used = TRUE;
5266 }
5267# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 fds[0].fd = fd;
5269 fds[0].events = POLLIN;
5270 nfd = 1;
5271
5272# ifdef FEAT_SNIFF
5273# define SNIFF_IDX 1
5274 if (want_sniff_request)
5275 {
5276 fds[SNIFF_IDX].fd = fd_from_sniff;
5277 fds[SNIFF_IDX].events = POLLIN;
5278 nfd++;
5279 }
5280# endif
5281# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005282 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 if (xterm_Shell != (Widget)0)
5284 {
5285 xterm_idx = nfd;
5286 fds[nfd].fd = ConnectionNumber(xterm_dpy);
5287 fds[nfd].events = POLLIN;
5288 nfd++;
5289 }
5290# endif
5291# ifdef FEAT_MOUSE_GPM
5292 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5293 {
5294 gpm_idx = nfd;
5295 fds[nfd].fd = gpm_fd;
5296 fds[nfd].events = POLLIN;
5297 nfd++;
5298 }
5299# endif
5300# ifdef USE_XSMP
5301 if (xsmp_icefd != -1)
5302 {
5303 xsmp_idx = nfd;
5304 fds[nfd].fd = xsmp_icefd;
5305 fds[nfd].events = POLLIN;
5306 nfd++;
5307 }
5308# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005309#ifdef FEAT_NETBEANS_INTG
5310 if (nb_fd != -1)
5311 {
5312 nb_idx = nfd;
5313 fds[nfd].fd = nb_fd;
5314 fds[nfd].events = POLLIN;
5315 nfd++;
5316 }
5317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005319 ret = poll(fds, nfd, towait);
5320# ifdef FEAT_MZSCHEME
5321 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005322 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005323 finished = FALSE;
5324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325
5326# ifdef FEAT_SNIFF
5327 if (ret < 0)
5328 sniff_disconnect(1);
5329 else if (want_sniff_request)
5330 {
5331 if (fds[SNIFF_IDX].revents & POLLHUP)
5332 sniff_disconnect(1);
5333 if (fds[SNIFF_IDX].revents & POLLIN)
5334 sniff_request_waiting = 1;
5335 }
5336# endif
5337# ifdef FEAT_XCLIPBOARD
5338 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
5339 {
5340 xterm_update(); /* Maybe we should hand out clipboard */
5341 if (--ret == 0 && !input_available())
5342 /* Try again */
5343 finished = FALSE;
5344 }
5345# endif
5346# ifdef FEAT_MOUSE_GPM
5347 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
5348 {
5349 *check_for_gpm = 1;
5350 }
5351# endif
5352# ifdef USE_XSMP
5353 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
5354 {
5355 if (fds[xsmp_idx].revents & POLLIN)
5356 {
5357 busy = TRUE;
5358 xsmp_handle_requests();
5359 busy = FALSE;
5360 }
5361 else if (fds[xsmp_idx].revents & POLLHUP)
5362 {
5363 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005364 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 xsmp_close();
5366 }
5367 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005368 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 }
5370# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005371#ifdef FEAT_NETBEANS_INTG
5372 if (ret > 0 && nb_idx != -1 && fds[nb_idx].revents & POLLIN)
5373 {
5374 netbeans_read();
5375 --ret;
5376 }
5377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378
5379
5380#else /* HAVE_SELECT */
5381
5382 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005383 struct timeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 fd_set rfds, efds;
5385 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005386 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005388# ifdef FEAT_MZSCHEME
5389 mzvim_check_threads();
5390 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5391 {
5392 towait = p_mzq; /* don't wait longer than 'mzquantum' */
5393 mzquantum_used = TRUE;
5394 }
5395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396# ifdef __EMX__
5397 /* don't check for incoming chars if not in raw mode, because select()
5398 * always returns TRUE then (in some version of emx.dll) */
5399 if (curr_tmode != TMODE_RAW)
5400 return 0;
5401# endif
5402
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005403 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005405 tv.tv_sec = towait / 1000;
5406 tv.tv_usec = (towait % 1000) * (1000000/1000);
5407 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005409 else
5410 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
5412 /*
5413 * Select on ready for reading and exceptional condition (end of file).
5414 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005415select_eintr:
5416 FD_ZERO(&rfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 FD_ZERO(&efds);
5418 FD_SET(fd, &rfds);
5419# if !defined(__QNX__) && !defined(__CYGWIN32__)
5420 /* For QNX select() always returns 1 if this is set. Why? */
5421 FD_SET(fd, &efds);
5422# endif
5423 maxfd = fd;
5424
5425# ifdef FEAT_SNIFF
5426 if (want_sniff_request)
5427 {
5428 FD_SET(fd_from_sniff, &rfds);
5429 FD_SET(fd_from_sniff, &efds);
5430 if (maxfd < fd_from_sniff)
5431 maxfd = fd_from_sniff;
5432 }
5433# endif
5434# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005435 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 if (xterm_Shell != (Widget)0)
5437 {
5438 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
5439 if (maxfd < ConnectionNumber(xterm_dpy))
5440 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005441
5442 /* An event may have already been read but not handled. In
5443 * particulary, XFlush may cause this. */
5444 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 }
5446# endif
5447# ifdef FEAT_MOUSE_GPM
5448 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5449 {
5450 FD_SET(gpm_fd, &rfds);
5451 FD_SET(gpm_fd, &efds);
5452 if (maxfd < gpm_fd)
5453 maxfd = gpm_fd;
5454 }
5455# endif
5456# ifdef USE_XSMP
5457 if (xsmp_icefd != -1)
5458 {
5459 FD_SET(xsmp_icefd, &rfds);
5460 FD_SET(xsmp_icefd, &efds);
5461 if (maxfd < xsmp_icefd)
5462 maxfd = xsmp_icefd;
5463 }
5464# endif
Bram Moolenaardd82d692012-08-15 17:26:57 +02005465# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar67c53842010-05-22 18:28:27 +02005466 if (nb_fd != -1)
5467 {
5468 FD_SET(nb_fd, &rfds);
5469 if (maxfd < nb_fd)
5470 maxfd = nb_fd;
5471 }
Bram Moolenaardd82d692012-08-15 17:26:57 +02005472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005474 ret = select(maxfd + 1, &rfds, NULL, &efds, tvp);
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005475# ifdef EINTR
5476 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005477 {
5478 /* Check whether window has been resized, EINTR may be caused by
5479 * SIGWINCH. */
5480 if (do_resize)
5481 handle_resize();
5482
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005483 /* Interrupted by a signal, need to try again. We ignore msec
5484 * here, because we do want to check even after a timeout if
5485 * characters are available. Needed for reading output of an
5486 * external command after the process has finished. */
5487 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005488 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005489# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00005490# ifdef __TANDEM
5491 if (ret == -1 && errno == ENOTSUP)
5492 {
5493 FD_ZERO(&rfds);
5494 FD_ZERO(&efds);
5495 ret = 0;
5496 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005497# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005498# ifdef FEAT_MZSCHEME
5499 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005500 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005501 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502# endif
5503
5504# ifdef FEAT_SNIFF
5505 if (ret < 0 )
5506 sniff_disconnect(1);
5507 else if (ret > 0 && want_sniff_request)
5508 {
5509 if (FD_ISSET(fd_from_sniff, &efds))
5510 sniff_disconnect(1);
5511 if (FD_ISSET(fd_from_sniff, &rfds))
5512 sniff_request_waiting = 1;
5513 }
5514# endif
5515# ifdef FEAT_XCLIPBOARD
5516 if (ret > 0 && xterm_Shell != (Widget)0
5517 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
5518 {
5519 xterm_update(); /* Maybe we should hand out clipboard */
5520 /* continue looping when we only got the X event and the input
5521 * buffer is empty */
5522 if (--ret == 0 && !input_available())
5523 {
5524 /* Try again */
5525 finished = FALSE;
5526 }
5527 }
5528# endif
5529# ifdef FEAT_MOUSE_GPM
5530 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
5531 {
5532 if (FD_ISSET(gpm_fd, &efds))
5533 gpm_close();
5534 else if (FD_ISSET(gpm_fd, &rfds))
5535 *check_for_gpm = 1;
5536 }
5537# endif
5538# ifdef USE_XSMP
5539 if (ret > 0 && xsmp_icefd != -1)
5540 {
5541 if (FD_ISSET(xsmp_icefd, &efds))
5542 {
5543 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005544 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 xsmp_close();
5546 if (--ret == 0)
5547 finished = FALSE; /* keep going if event was only one */
5548 }
5549 else if (FD_ISSET(xsmp_icefd, &rfds))
5550 {
5551 busy = TRUE;
5552 xsmp_handle_requests();
5553 busy = FALSE;
5554 if (--ret == 0)
5555 finished = FALSE; /* keep going if event was only one */
5556 }
5557 }
5558# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005559#ifdef FEAT_NETBEANS_INTG
5560 if (ret > 0 && nb_fd != -1 && FD_ISSET(nb_fd, &rfds))
5561 {
5562 netbeans_read();
5563 --ret;
5564 }
5565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
5567#endif /* HAVE_SELECT */
5568
5569#ifdef MAY_LOOP
5570 if (finished || msec == 0)
5571 break;
5572
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005573# ifdef FEAT_CLIENTSERVER
5574 if (server_waiting())
5575 break;
5576# endif
5577
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 /* We're going to loop around again, find out for how long */
5579 if (msec > 0)
5580 {
5581# ifdef USE_START_TV
5582 struct timeval mtv;
5583
5584 /* Compute remaining wait time. */
5585 gettimeofday(&mtv, NULL);
5586 msec -= (mtv.tv_sec - start_tv.tv_sec) * 1000L
5587 + (mtv.tv_usec - start_tv.tv_usec) / 1000L;
5588# else
5589 /* Guess we got interrupted halfway. */
5590 msec = msec / 2;
5591# endif
5592 if (msec <= 0)
5593 break; /* waited long enough */
5594 }
5595#endif
5596 }
5597
5598 return (ret > 0);
5599}
5600
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602/*
Bram Moolenaar02743632005-07-25 20:42:36 +00005603 * Expand a path into all matching files and/or directories. Handles "*",
5604 * "?", "[a-z]", "**", etc.
5605 * "path" has backslashes before chars that are not to be expanded.
5606 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 */
5608 int
5609mch_expandpath(gap, path, flags)
5610 garray_T *gap;
5611 char_u *path;
5612 int flags; /* EW_* flags */
5613{
Bram Moolenaar02743632005-07-25 20:42:36 +00005614 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615}
5616#endif
5617
5618/*
5619 * mch_expand_wildcards() - this code does wild-card pattern matching using
5620 * the shell
5621 *
5622 * return OK for success, FAIL for error (you may lose some memory) and put
5623 * an error message in *file.
5624 *
5625 * num_pat is number of input patterns
5626 * pat is array of pointers to input patterns
5627 * num_file is pointer to number of matched file names
5628 * file is pointer to array of pointers to matched file names
5629 */
5630
5631#ifndef SEEK_SET
5632# define SEEK_SET 0
5633#endif
5634#ifndef SEEK_END
5635# define SEEK_END 2
5636#endif
5637
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005638#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00005639
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 int
5641mch_expand_wildcards(num_pat, pat, num_file, file, flags)
5642 int num_pat;
5643 char_u **pat;
5644 int *num_file;
5645 char_u ***file;
5646 int flags; /* EW_* flags */
5647{
5648 int i;
5649 size_t len;
5650 char_u *p;
5651 int dir;
5652#ifdef __EMX__
Bram Moolenaarc7247912008-01-13 12:54:11 +00005653 /*
5654 * This is the OS/2 implementation.
5655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656# define EXPL_ALLOC_INC 16
5657 char_u **expl_files;
5658 size_t files_alloced, files_free;
5659 char_u *buf;
5660 int has_wildcard;
5661
5662 *num_file = 0; /* default: no files found */
5663 files_alloced = EXPL_ALLOC_INC; /* how much space is allocated */
5664 files_free = EXPL_ALLOC_INC; /* how much space is not used */
5665 *file = (char_u **)alloc(sizeof(char_u **) * files_alloced);
5666 if (*file == NULL)
5667 return FAIL;
5668
5669 for (; num_pat > 0; num_pat--, pat++)
5670 {
5671 expl_files = NULL;
5672 if (vim_strchr(*pat, '$') || vim_strchr(*pat, '~'))
5673 /* expand environment var or home dir */
5674 buf = expand_env_save(*pat);
5675 else
5676 buf = vim_strsave(*pat);
5677 expl_files = NULL;
Bram Moolenaard8b02732005-01-14 21:48:43 +00005678 has_wildcard = mch_has_exp_wildcard(buf); /* (still) wildcards? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 if (has_wildcard) /* yes, so expand them */
5680 expl_files = (char_u **)_fnexplode(buf);
5681
5682 /*
5683 * return value of buf if no wildcards left,
5684 * OR if no match AND EW_NOTFOUND is set.
5685 */
5686 if ((!has_wildcard && ((flags & EW_NOTFOUND) || mch_getperm(buf) >= 0))
5687 || (expl_files == NULL && (flags & EW_NOTFOUND)))
5688 { /* simply save the current contents of *buf */
5689 expl_files = (char_u **)alloc(sizeof(char_u **) * 2);
5690 if (expl_files != NULL)
5691 {
5692 expl_files[0] = vim_strsave(buf);
5693 expl_files[1] = NULL;
5694 }
5695 }
5696 vim_free(buf);
5697
5698 /*
5699 * Count number of names resulting from expansion,
5700 * At the same time add a backslash to the end of names that happen to
5701 * be directories, and replace slashes with backslashes.
5702 */
5703 if (expl_files)
5704 {
5705 for (i = 0; (p = expl_files[i]) != NULL; i++)
5706 {
5707 dir = mch_isdir(p);
5708 /* If we don't want dirs and this is one, skip it */
5709 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
5710 continue;
5711
Bram Moolenaara2031822006-03-07 22:29:51 +00005712 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01005713 if (!dir && (flags & EW_EXEC)
5714 && !mch_can_exe(p, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00005715 continue;
5716
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 if (--files_free == 0)
5718 {
5719 /* need more room in table of pointers */
5720 files_alloced += EXPL_ALLOC_INC;
5721 *file = (char_u **)vim_realloc(*file,
5722 sizeof(char_u **) * files_alloced);
5723 if (*file == NULL)
5724 {
5725 EMSG(_(e_outofmem));
5726 *num_file = 0;
5727 return FAIL;
5728 }
5729 files_free = EXPL_ALLOC_INC;
5730 }
5731 slash_adjust(p);
5732 if (dir)
5733 {
5734 /* For a directory we add a '/', unless it's already
5735 * there. */
5736 len = STRLEN(p);
5737 if (((*file)[*num_file] = alloc(len + 2)) != NULL)
5738 {
5739 STRCPY((*file)[*num_file], p);
Bram Moolenaar654b5b52006-06-22 17:47:10 +00005740 if (!after_pathsep((*file)[*num_file],
5741 (*file)[*num_file] + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 {
5743 (*file)[*num_file][len] = psepc;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005744 (*file)[*num_file][len + 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
5746 }
5747 }
5748 else
5749 {
5750 (*file)[*num_file] = vim_strsave(p);
5751 }
5752
5753 /*
5754 * Error message already given by either alloc or vim_strsave.
5755 * Should return FAIL, but returning OK works also.
5756 */
5757 if ((*file)[*num_file] == NULL)
5758 break;
5759 (*num_file)++;
5760 }
5761 _fnexplodefree((char **)expl_files);
5762 }
5763 }
5764 return OK;
5765
5766#else /* __EMX__ */
Bram Moolenaarc7247912008-01-13 12:54:11 +00005767 /*
5768 * This is the non-OS/2 implementation (really Unix).
5769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 int j;
5771 char_u *tempname;
5772 char_u *command;
5773 FILE *fd;
5774 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005775#define STYLE_ECHO 0 /* use "echo", the default */
5776#define STYLE_GLOB 1 /* use "glob", for csh */
5777#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
5778#define STYLE_PRINT 3 /* use "print -N", for zsh */
5779#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
5780 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 int shell_style = STYLE_ECHO;
5782 int check_spaces;
5783 static int did_find_nul = FALSE;
5784 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005785 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005786 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
5788 *num_file = 0; /* default: no files found */
5789 *file = NULL;
5790
5791 /*
5792 * If there are no wildcards, just copy the names to allocated memory.
5793 * Saves a lot of time, because we don't have to start a new shell.
5794 */
5795 if (!have_wildcard(num_pat, pat))
5796 return save_patterns(num_pat, pat, num_file, file);
5797
Bram Moolenaar0e634da2005-07-20 21:57:28 +00005798# ifdef HAVE_SANDBOX
5799 /* Don't allow any shell command in the sandbox. */
5800 if (sandbox != 0 && check_secure())
5801 return FAIL;
5802# endif
5803
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 /*
5805 * Don't allow the use of backticks in secure and restricted mode.
5806 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00005807 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 for (i = 0; i < num_pat; ++i)
5809 if (vim_strchr(pat[i], '`') != NULL
5810 && (check_restricted() || check_secure()))
5811 return FAIL;
5812
5813 /*
5814 * get a name for the temp file
5815 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005816 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 {
5818 EMSG(_(e_notmp));
5819 return FAIL;
5820 }
5821
5822 /*
5823 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00005824 * file.
5825 * STYLE_BT: NL separated
5826 * If expanding `cmd` execute it directly.
5827 * STYLE_GLOB: NUL separated
5828 * If we use *csh, "glob" will work better than "echo".
5829 * STYLE_PRINT: NL or NUL separated
5830 * If we use *zsh, "print -N" will work better than "glob".
5831 * STYLE_VIMGLOB: NL separated
5832 * If we use *sh*, we define "vimglob()".
5833 * STYLE_ECHO: space separated.
5834 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 */
5836 if (num_pat == 1 && *pat[0] == '`'
5837 && (len = STRLEN(pat[0])) > 2
5838 && *(pat[0] + len - 1) == '`')
5839 shell_style = STYLE_BT;
5840 else if ((len = STRLEN(p_sh)) >= 3)
5841 {
5842 if (STRCMP(p_sh + len - 3, "csh") == 0)
5843 shell_style = STYLE_GLOB;
5844 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
5845 shell_style = STYLE_PRINT;
5846 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00005847 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
5848 "sh") != NULL)
5849 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
Bram Moolenaarc7247912008-01-13 12:54:11 +00005851 /* Compute the length of the command. We need 2 extra bytes: for the
5852 * optional '&' and for the NUL.
5853 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005855 if (shell_style == STYLE_VIMGLOB)
5856 len += STRLEN(sh_vimglob_func);
5857
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005858 for (i = 0; i < num_pat; ++i)
5859 {
5860 /* Count the length of the patterns in the same way as they are put in
5861 * "command" below. */
5862#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005864#else
5865 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00005866 for (j = 0; pat[i][j] != NUL; ++j)
5867 {
5868 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
5869 ++len; /* may add a backslash */
5870 ++len;
5871 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005872#endif
5873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 command = alloc(len);
5875 if (command == NULL)
5876 {
5877 /* out of memory */
5878 vim_free(tempname);
5879 return FAIL;
5880 }
5881
5882 /*
5883 * Build the shell command:
5884 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
5885 * recognizes this).
5886 * - Add the shell command to print the expanded names.
5887 * - Add the temp file name.
5888 * - Add the file name patterns.
5889 */
5890 if (shell_style == STYLE_BT)
5891 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00005892 /* change `command; command& ` to (command; command ) */
5893 STRCPY(command, "(");
5894 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005896 *p-- = ')'; /* remove last backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 while (p > command && vim_iswhite(*p))
5898 --p;
5899 if (*p == '&') /* remove trailing '&' */
5900 {
5901 ampersent = TRUE;
5902 *p = ' ';
5903 }
5904 STRCAT(command, ">");
5905 }
5906 else
5907 {
5908 if (flags & EW_NOTFOUND)
5909 STRCPY(command, "set nonomatch; ");
5910 else
5911 STRCPY(command, "unset nonomatch; ");
5912 if (shell_style == STYLE_GLOB)
5913 STRCAT(command, "glob >");
5914 else if (shell_style == STYLE_PRINT)
5915 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00005916 else if (shell_style == STYLE_VIMGLOB)
5917 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 else
5919 STRCAT(command, "echo >");
5920 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00005921
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00005923
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 if (shell_style != STYLE_BT)
5925 for (i = 0; i < num_pat; ++i)
5926 {
5927 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00005928 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005929 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930#ifdef USE_SYSTEM
5931 STRCAT(command, " \"");
5932 STRCAT(command, pat[i]);
5933 STRCAT(command, "\"");
5934#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00005935 int intick = FALSE;
5936
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 p = command + STRLEN(command);
5938 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00005939 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005940 {
5941 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00005942 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005943 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
5944 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005945 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00005946 * backslash inside backticks, before a special character
5947 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005948 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00005949 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
5950 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005951 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00005952 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005953 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02005954 else if (!intick
5955 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
5956 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00005957 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02005958 * when inside ``. And not for $var when EW_KEEPDOLLAR is
5959 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00005960 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00005961
5962 /* Copy one character. */
5963 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00005964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 *p = NUL;
5966#endif
5967 }
5968 if (flags & EW_SILENT)
5969 show_shell_mess = FALSE;
5970 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00005971 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972
5973 /*
5974 * Using zsh -G: If a pattern has no matches, it is just deleted from
5975 * the argument list, otherwise zsh gives an error message and doesn't
5976 * expand any other pattern.
5977 */
5978 if (shell_style == STYLE_PRINT)
5979 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
5980
5981 /*
5982 * If we use -f then shell variables set in .cshrc won't get expanded.
5983 * vi can do it, so we will too, but it is only necessary if there is a "$"
5984 * in one of the patterns, otherwise we can still use the fast option.
5985 */
5986 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
5987 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
5988
5989 /*
5990 * execute the shell command
5991 */
5992 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
5993
5994 /* When running in the background, give it some time to create the temp
5995 * file, but don't wait for it to finish. */
5996 if (ampersent)
5997 mch_delay(10L, TRUE);
5998
5999 extra_shell_arg = NULL; /* cleanup */
6000 show_shell_mess = TRUE;
6001 vim_free(command);
6002
Bram Moolenaarc7247912008-01-13 12:54:11 +00006003 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 {
6005 mch_remove(tempname);
6006 vim_free(tempname);
6007 /*
6008 * With interactive completion, the error message is not printed.
6009 * However with USE_SYSTEM, I don't know how to turn off error messages
6010 * from the shell, so screen may still get messed up -- webb.
6011 */
6012#ifndef USE_SYSTEM
6013 if (!(flags & EW_SILENT))
6014#endif
6015 {
6016 redraw_later_clear(); /* probably messed up screen */
6017 msg_putchar('\n'); /* clear bottom line quickly */
6018 cmdline_row = Rows - 1; /* continue on last line */
6019#ifdef USE_SYSTEM
6020 if (!(flags & EW_SILENT))
6021#endif
6022 {
6023 MSG(_(e_wildexpand));
6024 msg_start(); /* don't overwrite this message */
6025 }
6026 }
6027 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6028 * EW_NOTFOUND is given */
6029 if (shell_style == STYLE_BT)
6030 return FAIL;
6031 goto notfound;
6032 }
6033
6034 /*
6035 * read the names from the file into memory
6036 */
6037 fd = fopen((char *)tempname, READBIN);
6038 if (fd == NULL)
6039 {
6040 /* Something went wrong, perhaps a file name with a special char. */
6041 if (!(flags & EW_SILENT))
6042 {
6043 MSG(_(e_wildexpand));
6044 msg_start(); /* don't overwrite this message */
6045 }
6046 vim_free(tempname);
6047 goto notfound;
6048 }
6049 fseek(fd, 0L, SEEK_END);
6050 len = ftell(fd); /* get size of temp file */
6051 fseek(fd, 0L, SEEK_SET);
6052 buffer = alloc(len + 1);
6053 if (buffer == NULL)
6054 {
6055 /* out of memory */
6056 mch_remove(tempname);
6057 vim_free(tempname);
6058 fclose(fd);
6059 return FAIL;
6060 }
6061 i = fread((char *)buffer, 1, len, fd);
6062 fclose(fd);
6063 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006064 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 {
6066 /* unexpected read error */
6067 EMSG2(_(e_notread), tempname);
6068 vim_free(tempname);
6069 vim_free(buffer);
6070 return FAIL;
6071 }
6072 vim_free(tempname);
6073
Bram Moolenaarc7247912008-01-13 12:54:11 +00006074# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6076 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006077 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6079 *p++ = buffer[i];
6080 len = p - buffer;
6081# endif
6082
6083
6084 /* file names are separated with Space */
6085 if (shell_style == STYLE_ECHO)
6086 {
6087 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6088 p = buffer;
6089 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6090 {
6091 while (*p != ' ' && *p != '\n')
6092 ++p;
6093 p = skipwhite(p); /* skip to next entry */
6094 }
6095 }
6096 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006097 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 {
6099 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6100 p = buffer;
6101 for (i = 0; *p != NUL; ++i) /* count number of entries */
6102 {
6103 while (*p != '\n' && *p != NUL)
6104 ++p;
6105 if (*p != NUL)
6106 ++p;
6107 p = skipwhite(p); /* skip leading white space */
6108 }
6109 }
6110 /* file names are separated with NUL */
6111 else
6112 {
6113 /*
6114 * Some versions of zsh use spaces instead of NULs to separate
6115 * results. Only do this when there is no NUL before the end of the
6116 * buffer, otherwise we would never be able to use file names with
6117 * embedded spaces when zsh does use NULs.
6118 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6119 * don't check for spaces again.
6120 */
6121 check_spaces = FALSE;
6122 if (shell_style == STYLE_PRINT && !did_find_nul)
6123 {
6124 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006125 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006126 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 did_find_nul = TRUE;
6128 else
6129 check_spaces = TRUE;
6130 }
6131
6132 /*
6133 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6134 * already is one, for STYLE_GLOB it needs to be added.
6135 */
6136 if (len && buffer[len - 1] == NUL)
6137 --len;
6138 else
6139 buffer[len] = NUL;
6140 i = 0;
6141 for (p = buffer; p < buffer + len; ++p)
6142 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6143 {
6144 ++i;
6145 *p = NUL;
6146 }
6147 if (len)
6148 ++i; /* count last entry */
6149 }
6150 if (i == 0)
6151 {
6152 /*
6153 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6154 * /bin/sh will happily expand it to nothing rather than returning an
6155 * error; and hey, it's good to check anyway -- webb.
6156 */
6157 vim_free(buffer);
6158 goto notfound;
6159 }
6160 *num_file = i;
6161 *file = (char_u **)alloc(sizeof(char_u *) * i);
6162 if (*file == NULL)
6163 {
6164 /* out of memory */
6165 vim_free(buffer);
6166 return FAIL;
6167 }
6168
6169 /*
6170 * Isolate the individual file names.
6171 */
6172 p = buffer;
6173 for (i = 0; i < *num_file; ++i)
6174 {
6175 (*file)[i] = p;
6176 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006177 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6178 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006180 while (!(shell_style == STYLE_ECHO && *p == ' ')
6181 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 ++p;
6183 if (p == buffer + len) /* last entry */
6184 *p = NUL;
6185 else
6186 {
6187 *p++ = NUL;
6188 p = skipwhite(p); /* skip to next entry */
6189 }
6190 }
6191 else /* NUL separates */
6192 {
6193 while (*p && p < buffer + len) /* skip entry */
6194 ++p;
6195 ++p; /* skip NUL */
6196 }
6197 }
6198
6199 /*
6200 * Move the file names to allocated memory.
6201 */
6202 for (j = 0, i = 0; i < *num_file; ++i)
6203 {
6204 /* Require the files to exist. Helps when using /bin/sh */
6205 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6206 continue;
6207
6208 /* check if this entry should be included */
6209 dir = (mch_isdir((*file)[i]));
6210 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6211 continue;
6212
Bram Moolenaara2031822006-03-07 22:29:51 +00006213 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006214 if (!dir && (flags & EW_EXEC)
6215 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006216 continue;
6217
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6219 if (p)
6220 {
6221 STRCPY(p, (*file)[i]);
6222 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006223 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 (*file)[j++] = p;
6225 }
6226 }
6227 vim_free(buffer);
6228 *num_file = j;
6229
6230 if (*num_file == 0) /* rejected all entries */
6231 {
6232 vim_free(*file);
6233 *file = NULL;
6234 goto notfound;
6235 }
6236
6237 return OK;
6238
6239notfound:
6240 if (flags & EW_NOTFOUND)
6241 return save_patterns(num_pat, pat, num_file, file);
6242 return FAIL;
6243
6244#endif /* __EMX__ */
6245}
6246
6247#endif /* VMS */
6248
6249#ifndef __EMX__
6250 static int
6251save_patterns(num_pat, pat, num_file, file)
6252 int num_pat;
6253 char_u **pat;
6254 int *num_file;
6255 char_u ***file;
6256{
6257 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006258 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259
6260 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6261 if (*file == NULL)
6262 return FAIL;
6263 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006264 {
6265 s = vim_strsave(pat[i]);
6266 if (s != NULL)
6267 /* Be compatible with expand_filename(): halve the number of
6268 * backslashes. */
6269 backslash_halve(s);
6270 (*file)[i] = s;
6271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 *num_file = num_pat;
6273 return OK;
6274}
6275#endif
6276
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277/*
6278 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6279 * expand.
6280 */
6281 int
6282mch_has_exp_wildcard(p)
6283 char_u *p;
6284{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006285 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 if (*p == '\\' && p[1] != NUL)
6288 ++p;
6289 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 if (vim_strchr((char_u *)
6291#ifdef VMS
6292 "*?%"
6293#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295#endif
6296 , *p) != NULL)
6297 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 }
6299 return FALSE;
6300}
6301
6302/*
6303 * Return TRUE if the string "p" contains a wildcard.
6304 * Don't recognize '~' at the end as a wildcard.
6305 */
6306 int
6307mch_has_wildcard(p)
6308 char_u *p;
6309{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006310 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 if (*p == '\\' && p[1] != NUL)
6313 ++p;
6314 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 if (vim_strchr((char_u *)
6316#ifdef VMS
6317 "*?%$"
6318#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320#endif
6321 , *p) != NULL
6322 || (*p == '~' && p[1] != NUL))
6323 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 }
6325 return FALSE;
6326}
6327
6328#ifndef __EMX__
6329 static int
6330have_wildcard(num, file)
6331 int num;
6332 char_u **file;
6333{
6334 int i;
6335
6336 for (i = 0; i < num; i++)
6337 if (mch_has_wildcard(file[i]))
6338 return 1;
6339 return 0;
6340}
6341
6342 static int
6343have_dollars(num, file)
6344 int num;
6345 char_u **file;
6346{
6347 int i;
6348
6349 for (i = 0; i < num; i++)
6350 if (vim_strchr(file[i], '$') != NULL)
6351 return TRUE;
6352 return FALSE;
6353}
6354#endif /* ifndef __EMX__ */
6355
6356#ifndef HAVE_RENAME
6357/*
6358 * Scaled-down version of rename(), which is missing in Xenix.
6359 * This version can only move regular files and will fail if the
6360 * destination exists.
6361 */
6362 int
6363mch_rename(src, dest)
6364 const char *src, *dest;
6365{
6366 struct stat st;
6367
6368 if (stat(dest, &st) >= 0) /* fail if destination exists */
6369 return -1;
6370 if (link(src, dest) != 0) /* link file to new name */
6371 return -1;
6372 if (mch_remove(src) == 0) /* delete link to old name */
6373 return 0;
6374 return -1;
6375}
6376#endif /* !HAVE_RENAME */
6377
6378#ifdef FEAT_MOUSE_GPM
6379/*
6380 * Initializes connection with gpm (if it isn't already opened)
6381 * Return 1 if succeeded (or connection already opened), 0 if failed
6382 */
6383 static int
6384gpm_open()
6385{
6386 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
6387
6388 if (!gpm_flag)
6389 {
6390 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
6391 gpm_connect.defaultMask = ~GPM_HARD;
6392 /* Default handling for mouse move*/
6393 gpm_connect.minMod = 0; /* Handle any modifier keys */
6394 gpm_connect.maxMod = 0xffff;
6395 if (Gpm_Open(&gpm_connect, 0) > 0)
6396 {
6397 /* gpm library tries to handling TSTP causes
6398 * problems. Anyways, we close connection to Gpm whenever
6399 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006400 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006402# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 return 1; /* succeed */
6406 }
6407 if (gpm_fd == -2)
6408 Gpm_Close(); /* We don't want to talk to xterm via gpm */
6409 return 0;
6410 }
6411 return 1; /* already open */
6412}
6413
6414/*
6415 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 */
6417 static void
6418gpm_close()
6419{
6420 if (gpm_flag && gpm_fd >= 0) /* if Open */
6421 Gpm_Close();
6422}
6423
6424/* Reads gpm event and adds special keys to input buf. Returns length of
6425 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02006426 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 */
6428 static int
6429mch_gpm_process()
6430{
6431 int button;
6432 static Gpm_Event gpm_event;
6433 char_u string[6];
6434 int_u vim_modifiers;
6435 int row,col;
6436 unsigned char buttons_mask;
6437 unsigned char gpm_modifiers;
6438 static unsigned char old_buttons = 0;
6439
6440 Gpm_GetEvent(&gpm_event);
6441
6442#ifdef FEAT_GUI
6443 /* Don't put events in the input queue now. */
6444 if (hold_gui_events)
6445 return 0;
6446#endif
6447
6448 row = gpm_event.y - 1;
6449 col = gpm_event.x - 1;
6450
6451 string[0] = ESC; /* Our termcode */
6452 string[1] = 'M';
6453 string[2] = 'G';
6454 switch (GPM_BARE_EVENTS(gpm_event.type))
6455 {
6456 case GPM_DRAG:
6457 string[3] = MOUSE_DRAG;
6458 break;
6459 case GPM_DOWN:
6460 buttons_mask = gpm_event.buttons & ~old_buttons;
6461 old_buttons = gpm_event.buttons;
6462 switch (buttons_mask)
6463 {
6464 case GPM_B_LEFT:
6465 button = MOUSE_LEFT;
6466 break;
6467 case GPM_B_MIDDLE:
6468 button = MOUSE_MIDDLE;
6469 break;
6470 case GPM_B_RIGHT:
6471 button = MOUSE_RIGHT;
6472 break;
6473 default:
6474 return 0;
6475 /*Don't know what to do. Can more than one button be
6476 * reported in one event? */
6477 }
6478 string[3] = (char_u)(button | 0x20);
6479 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
6480 break;
6481 case GPM_UP:
6482 string[3] = MOUSE_RELEASE;
6483 old_buttons &= ~gpm_event.buttons;
6484 break;
6485 default:
6486 return 0;
6487 }
6488 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
6489 gpm_modifiers = gpm_event.modifiers;
6490 vim_modifiers = 0x0;
6491 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
6492 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
6493 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
6494 */
6495 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
6496 vim_modifiers |= MOUSE_SHIFT;
6497
6498 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
6499 vim_modifiers |= MOUSE_CTRL;
6500 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
6501 vim_modifiers |= MOUSE_ALT;
6502 string[3] |= vim_modifiers;
6503 string[4] = (char_u)(col + ' ' + 1);
6504 string[5] = (char_u)(row + ' ' + 1);
6505 add_to_input_buf(string, 6);
6506 return 6;
6507}
6508#endif /* FEAT_MOUSE_GPM */
6509
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006510#ifdef FEAT_SYSMOUSE
6511/*
6512 * Initialize connection with sysmouse.
6513 * Let virtual console inform us with SIGUSR2 for pending sysmouse
6514 * output, any sysmouse output than will be processed via sig_sysmouse().
6515 * Return OK if succeeded, FAIL if failed.
6516 */
6517 static int
6518sysmouse_open()
6519{
6520 struct mouse_info mouse;
6521
6522 mouse.operation = MOUSE_MODE;
6523 mouse.u.mode.mode = 0;
6524 mouse.u.mode.signal = SIGUSR2;
6525 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
6526 {
6527 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
6528 mouse.operation = MOUSE_SHOW;
6529 ioctl(1, CONS_MOUSECTL, &mouse);
6530 return OK;
6531 }
6532 return FAIL;
6533}
6534
6535/*
6536 * Stop processing SIGUSR2 signals, and also make sure that
6537 * virtual console do not send us any sysmouse related signal.
6538 */
6539 static void
6540sysmouse_close()
6541{
6542 struct mouse_info mouse;
6543
6544 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
6545 mouse.operation = MOUSE_MODE;
6546 mouse.u.mode.mode = 0;
6547 mouse.u.mode.signal = 0;
6548 ioctl(1, CONS_MOUSECTL, &mouse);
6549}
6550
6551/*
6552 * Gets info from sysmouse and adds special keys to input buf.
6553 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006554 static RETSIGTYPE
6555sig_sysmouse SIGDEFARG(sigarg)
6556{
6557 struct mouse_info mouse;
6558 struct video_info video;
6559 char_u string[6];
6560 int row, col;
6561 int button;
6562 int buttons;
6563 static int oldbuttons = 0;
6564
6565#ifdef FEAT_GUI
6566 /* Don't put events in the input queue now. */
6567 if (hold_gui_events)
6568 return;
6569#endif
6570
6571 mouse.operation = MOUSE_GETINFO;
6572 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
6573 && ioctl(1, FBIO_MODEINFO, &video) != -1
6574 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
6575 && video.vi_cheight > 0 && video.vi_cwidth > 0)
6576 {
6577 row = mouse.u.data.y / video.vi_cheight;
6578 col = mouse.u.data.x / video.vi_cwidth;
6579 buttons = mouse.u.data.buttons;
6580 string[0] = ESC; /* Our termcode */
6581 string[1] = 'M';
6582 string[2] = 'S';
6583 if (oldbuttons == buttons && buttons != 0)
6584 {
6585 button = MOUSE_DRAG;
6586 }
6587 else
6588 {
6589 switch (buttons)
6590 {
6591 case 0:
6592 button = MOUSE_RELEASE;
6593 break;
6594 case 1:
6595 button = MOUSE_LEFT;
6596 break;
6597 case 2:
6598 button = MOUSE_MIDDLE;
6599 break;
6600 case 4:
6601 button = MOUSE_RIGHT;
6602 break;
6603 default:
6604 return;
6605 }
6606 oldbuttons = buttons;
6607 }
6608 string[3] = (char_u)(button);
6609 string[4] = (char_u)(col + ' ' + 1);
6610 string[5] = (char_u)(row + ' ' + 1);
6611 add_to_input_buf(string, 6);
6612 }
6613 return;
6614}
6615#endif /* FEAT_SYSMOUSE */
6616
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617#if defined(FEAT_LIBCALL) || defined(PROTO)
6618typedef char_u * (*STRPROCSTR)__ARGS((char_u *));
6619typedef char_u * (*INTPROCSTR)__ARGS((int));
6620typedef int (*STRPROCINT)__ARGS((char_u *));
6621typedef int (*INTPROCINT)__ARGS((int));
6622
6623/*
6624 * Call a DLL routine which takes either a string or int param
6625 * and returns an allocated string.
6626 */
6627 int
6628mch_libcall(libname, funcname, argstring, argint, string_result, number_result)
6629 char_u *libname;
6630 char_u *funcname;
6631 char_u *argstring; /* NULL when using a argint */
6632 int argint;
6633 char_u **string_result;/* NULL when using number_result */
6634 int *number_result;
6635{
6636# if defined(USE_DLOPEN)
6637 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006638 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639# else
6640 shl_t hinstLib;
6641# endif
6642 STRPROCSTR ProcAdd;
6643 INTPROCSTR ProcAddI;
6644 char_u *retval_str = NULL;
6645 int retval_int = 0;
6646 int success = FALSE;
6647
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006648 /*
6649 * Get a handle to the DLL module.
6650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006652 /* First clear any error, it's not cleared by the dlopen() call. */
6653 (void)dlerror();
6654
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 hinstLib = dlopen((char *)libname, RTLD_LAZY
6656# ifdef RTLD_LOCAL
6657 | RTLD_LOCAL
6658# endif
6659 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006660 if (hinstLib == NULL)
6661 {
6662 /* "dlerr" must be used before dlclose() */
6663 dlerr = (char *)dlerror();
6664 if (dlerr != NULL)
6665 EMSG2(_("dlerror = \"%s\""), dlerr);
6666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667# else
6668 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
6669# endif
6670
6671 /* If the handle is valid, try to get the function address. */
6672 if (hinstLib != NULL)
6673 {
6674# ifdef HAVE_SETJMP_H
6675 /*
6676 * Catch a crash when calling the library function. For example when
6677 * using a number where a string pointer is expected.
6678 */
6679 mch_startjmp();
6680 if (SETJMP(lc_jump_env) != 0)
6681 {
6682 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00006683# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006684 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00006685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 mch_didjmp();
6687 }
6688 else
6689# endif
6690 {
6691 retval_str = NULL;
6692 retval_int = 0;
6693
6694 if (argstring != NULL)
6695 {
6696# if defined(USE_DLOPEN)
6697 ProcAdd = (STRPROCSTR)dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006698 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699# else
6700 if (shl_findsym(&hinstLib, (const char *)funcname,
6701 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
6702 ProcAdd = NULL;
6703# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006704 if ((success = (ProcAdd != NULL
6705# if defined(USE_DLOPEN)
6706 && dlerr == NULL
6707# endif
6708 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 {
6710 if (string_result == NULL)
6711 retval_int = ((STRPROCINT)ProcAdd)(argstring);
6712 else
6713 retval_str = (ProcAdd)(argstring);
6714 }
6715 }
6716 else
6717 {
6718# if defined(USE_DLOPEN)
6719 ProcAddI = (INTPROCSTR)dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006720 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721# else
6722 if (shl_findsym(&hinstLib, (const char *)funcname,
6723 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
6724 ProcAddI = NULL;
6725# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006726 if ((success = (ProcAddI != NULL
6727# if defined(USE_DLOPEN)
6728 && dlerr == NULL
6729# endif
6730 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 {
6732 if (string_result == NULL)
6733 retval_int = ((INTPROCINT)ProcAddI)(argint);
6734 else
6735 retval_str = (ProcAddI)(argint);
6736 }
6737 }
6738
6739 /* Save the string before we free the library. */
6740 /* Assume that a "1" or "-1" result is an illegal pointer. */
6741 if (string_result == NULL)
6742 *number_result = retval_int;
6743 else if (retval_str != NULL
6744 && retval_str != (char_u *)1
6745 && retval_str != (char_u *)-1)
6746 *string_result = vim_strsave(retval_str);
6747 }
6748
6749# ifdef HAVE_SETJMP_H
6750 mch_endjmp();
6751# ifdef SIGHASARG
6752 if (lc_signal != 0)
6753 {
6754 int i;
6755
6756 /* try to find the name of this signal */
6757 for (i = 0; signal_info[i].sig != -1; i++)
6758 if (lc_signal == signal_info[i].sig)
6759 break;
6760 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
6761 }
6762# endif
6763# endif
6764
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006766 /* "dlerr" must be used before dlclose() */
6767 if (dlerr != NULL)
6768 EMSG2(_("dlerror = \"%s\""), dlerr);
6769
6770 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 (void)dlclose(hinstLib);
6772# else
6773 (void)shl_unload(hinstLib);
6774# endif
6775 }
6776
6777 if (!success)
6778 {
6779 EMSG2(_(e_libcall), funcname);
6780 return FAIL;
6781 }
6782
6783 return OK;
6784}
6785#endif
6786
6787#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
6788static int xterm_trace = -1; /* default: disabled */
6789static int xterm_button;
6790
6791/*
6792 * Setup a dummy window for X selections in a terminal.
6793 */
6794 void
6795setup_term_clip()
6796{
6797 int z = 0;
6798 char *strp = "";
6799 Widget AppShell;
6800
6801 if (!x_connect_to_server())
6802 return;
6803
6804 open_app_context();
6805 if (app_context != NULL && xterm_Shell == (Widget)0)
6806 {
6807 int (*oldhandler)();
6808#if defined(HAVE_SETJMP_H)
6809 int (*oldIOhandler)();
6810#endif
6811# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
6812 struct timeval start_tv;
6813
6814 if (p_verbose > 0)
6815 gettimeofday(&start_tv, NULL);
6816# endif
6817
6818 /* Ignore X errors while opening the display */
6819 oldhandler = XSetErrorHandler(x_error_check);
6820
6821#if defined(HAVE_SETJMP_H)
6822 /* Ignore X IO errors while opening the display */
6823 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
6824 mch_startjmp();
6825 if (SETJMP(lc_jump_env) != 0)
6826 {
6827 mch_didjmp();
6828 xterm_dpy = NULL;
6829 }
6830 else
6831#endif
6832 {
6833 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
6834 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
6835#if defined(HAVE_SETJMP_H)
6836 mch_endjmp();
6837#endif
6838 }
6839
6840#if defined(HAVE_SETJMP_H)
6841 /* Now handle X IO errors normally. */
6842 (void)XSetIOErrorHandler(oldIOhandler);
6843#endif
6844 /* Now handle X errors normally. */
6845 (void)XSetErrorHandler(oldhandler);
6846
6847 if (xterm_dpy == NULL)
6848 {
6849 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006850 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 return;
6852 }
6853
6854 /* Catch terminating error of the X server connection. */
6855 (void)XSetIOErrorHandler(x_IOerror_handler);
6856
6857# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
6858 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006859 {
6860 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 xopen_message(&start_tv);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006862 verbose_leave();
6863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864# endif
6865
6866 /* Create a Shell to make converters work. */
6867 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
6868 applicationShellWidgetClass, xterm_dpy,
6869 NULL);
6870 if (AppShell == (Widget)0)
6871 return;
6872 xterm_Shell = XtVaCreatePopupShell("VIM",
6873 topLevelShellWidgetClass, AppShell,
6874 XtNmappedWhenManaged, 0,
6875 XtNwidth, 1,
6876 XtNheight, 1,
6877 NULL);
6878 if (xterm_Shell == (Widget)0)
6879 return;
6880
6881 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02006882 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 if (x11_display == NULL)
6884 x11_display = xterm_dpy;
6885
6886 XtRealizeWidget(xterm_Shell);
6887 XSync(xterm_dpy, False);
6888 xterm_update();
6889 }
6890 if (xterm_Shell != (Widget)0)
6891 {
6892 clip_init(TRUE);
6893 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
6894 x11_window = (Window)atol(strp);
6895 /* Check if $WINDOWID is valid. */
6896 if (test_x11_window(xterm_dpy) == FAIL)
6897 x11_window = 0;
6898 if (x11_window != 0)
6899 xterm_trace = 0;
6900 }
6901}
6902
6903 void
6904start_xterm_trace(button)
6905 int button;
6906{
6907 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
6908 return;
6909 xterm_trace = 1;
6910 xterm_button = button;
6911 do_xterm_trace();
6912}
6913
6914
6915 void
6916stop_xterm_trace()
6917{
6918 if (xterm_trace < 0)
6919 return;
6920 xterm_trace = 0;
6921}
6922
6923/*
6924 * Query the xterm pointer and generate mouse termcodes if necessary
6925 * return TRUE if dragging is active, else FALSE
6926 */
6927 static int
6928do_xterm_trace()
6929{
6930 Window root, child;
6931 int root_x, root_y;
6932 int win_x, win_y;
6933 int row, col;
6934 int_u mask_return;
6935 char_u buf[50];
6936 char_u *strp;
6937 long got_hints;
6938 static char_u *mouse_code;
6939 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
6940 static int prev_row = 0, prev_col = 0;
6941 static XSizeHints xterm_hints;
6942
6943 if (xterm_trace <= 0)
6944 return FALSE;
6945
6946 if (xterm_trace == 1)
6947 {
6948 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00006949 * have changed recently. */
6950 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
6951 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 || xterm_hints.width_inc <= 1
6953 || xterm_hints.height_inc <= 1)
6954 {
6955 xterm_trace = -1; /* Not enough data -- disable tracing */
6956 return FALSE;
6957 }
6958
6959 /* Rely on the same mouse code for the duration of this */
6960 mouse_code = find_termcode(mouse_name);
6961 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02006962 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 xterm_trace = 2;
6964
6965 /* Find the offset of the chars, there might be a scrollbar on the
6966 * left of the window and/or a menu on the top (eterm etc.) */
6967 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
6968 &win_x, &win_y, &mask_return);
6969 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
6970 - (xterm_hints.height_inc / 2);
6971 if (xterm_hints.y <= xterm_hints.height_inc / 2)
6972 xterm_hints.y = 2;
6973 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
6974 - (xterm_hints.width_inc / 2);
6975 if (xterm_hints.x <= xterm_hints.width_inc / 2)
6976 xterm_hints.x = 2;
6977 return TRUE;
6978 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006979 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 {
6981 xterm_trace = 0;
6982 return FALSE;
6983 }
6984
6985 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
6986 &win_x, &win_y, &mask_return);
6987
6988 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
6989 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
6990 if (row == prev_row && col == prev_col)
6991 return TRUE;
6992
6993 STRCPY(buf, mouse_code);
6994 strp = buf + STRLEN(buf);
6995 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
6996 *strp++ = (char_u)(col + ' ' + 1);
6997 *strp++ = (char_u)(row + ' ' + 1);
6998 *strp = 0;
6999 add_to_input_buf(buf, STRLEN(buf));
7000
7001 prev_row = row;
7002 prev_col = col;
7003 return TRUE;
7004}
7005
7006# if defined(FEAT_GUI) || defined(PROTO)
7007/*
7008 * Destroy the display, window and app_context. Required for GTK.
7009 */
7010 void
7011clear_xterm_clip()
7012{
7013 if (xterm_Shell != (Widget)0)
7014 {
7015 XtDestroyWidget(xterm_Shell);
7016 xterm_Shell = (Widget)0;
7017 }
7018 if (xterm_dpy != NULL)
7019 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007020# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 /* Lesstif and Solaris crash here, lose some memory */
7022 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 if (x11_display == xterm_dpy)
7025 x11_display = NULL;
7026 xterm_dpy = NULL;
7027 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007028# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 if (app_context != (XtAppContext)NULL)
7030 {
7031 /* Lesstif and Solaris crash here, lose some memory */
7032 XtDestroyApplicationContext(app_context);
7033 app_context = (XtAppContext)NULL;
7034 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036}
7037# endif
7038
7039/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007040 * Catch up with GUI or X events.
7041 */
7042 static void
7043clip_update()
7044{
7045# ifdef FEAT_GUI
7046 if (gui.in_use)
7047 gui_mch_update();
7048 else
7049# endif
7050 if (xterm_Shell != (Widget)0)
7051 xterm_update();
7052}
7053
7054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 * Catch up with any queued X events. This may put keyboard input into the
7056 * input buffer, call resize call-backs, trigger timers etc. If there is
7057 * nothing in the X event queue (& no timers pending), then we return
7058 * immediately.
7059 */
7060 static void
7061xterm_update()
7062{
7063 XEvent event;
7064
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007065 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007067 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007069 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007070 break;
7071
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007072 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007073 {
7074 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007075 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007076#ifdef FEAT_CLIENTSERVER
7077 {
7078 XPropertyEvent *e = (XPropertyEvent *)&event;
7079
7080 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007082 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007085 XtDispatchEvent(&event);
7086 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007087 else
7088 {
7089 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007090 XtAppProcessEvent(app_context, mask);
7091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 }
7093}
7094
7095 int
7096clip_xterm_own_selection(cbd)
7097 VimClipboard *cbd;
7098{
7099 if (xterm_Shell != (Widget)0)
7100 return clip_x11_own_selection(xterm_Shell, cbd);
7101 return FAIL;
7102}
7103
7104 void
7105clip_xterm_lose_selection(cbd)
7106 VimClipboard *cbd;
7107{
7108 if (xterm_Shell != (Widget)0)
7109 clip_x11_lose_selection(xterm_Shell, cbd);
7110}
7111
7112 void
7113clip_xterm_request_selection(cbd)
7114 VimClipboard *cbd;
7115{
7116 if (xterm_Shell != (Widget)0)
7117 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7118}
7119
7120 void
7121clip_xterm_set_selection(cbd)
7122 VimClipboard *cbd;
7123{
7124 clip_x11_set_selection(cbd);
7125}
7126#endif
7127
7128
7129#if defined(USE_XSMP) || defined(PROTO)
7130/*
7131 * Code for X Session Management Protocol.
7132 */
7133static void xsmp_handle_save_yourself __ARGS((SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast));
7134static void xsmp_die __ARGS((SmcConn smc_conn, SmPointer client_data));
7135static void xsmp_save_complete __ARGS((SmcConn smc_conn, SmPointer client_data));
7136static void xsmp_shutdown_cancelled __ARGS((SmcConn smc_conn, SmPointer client_data));
7137static void xsmp_ice_connection __ARGS((IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData));
7138
7139
7140# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7141static void xsmp_handle_interaction __ARGS((SmcConn smc_conn, SmPointer client_data));
7142
7143/*
7144 * This is our chance to ask the user if they want to save,
7145 * or abort the logout
7146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 static void
7148xsmp_handle_interaction(smc_conn, client_data)
7149 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007150 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
7152 cmdmod_T save_cmdmod;
7153 int cancel_shutdown = False;
7154
7155 save_cmdmod = cmdmod;
7156 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007157 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 /* Mustn't logout */
7159 cancel_shutdown = True;
7160 cmdmod = save_cmdmod;
7161 setcursor(); /* position cursor */
7162 out_flush();
7163
7164 /* Done interaction */
7165 SmcInteractDone(smc_conn, cancel_shutdown);
7166
7167 /* Finish off
7168 * Only end save-yourself here if we're not cancelling shutdown;
7169 * we'll get a cancelled callback later in which we'll end it.
7170 * Hopefully get around glitchy SMs (like GNOME-1)
7171 */
7172 if (!cancel_shutdown)
7173 {
7174 xsmp.save_yourself = False;
7175 SmcSaveYourselfDone(smc_conn, True);
7176 }
7177}
7178# endif
7179
7180/*
7181 * Callback that starts save-yourself.
7182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 static void
7184xsmp_handle_save_yourself(smc_conn, client_data, save_type,
7185 shutdown, interact_style, fast)
7186 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007187 SmPointer client_data UNUSED;
7188 int save_type UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 Bool shutdown;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007190 int interact_style UNUSED;
7191 Bool fast UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
7193 /* Handle already being in saveyourself */
7194 if (xsmp.save_yourself)
7195 SmcSaveYourselfDone(smc_conn, True);
7196 xsmp.save_yourself = True;
7197 xsmp.shutdown = shutdown;
7198
7199 /* First up, preserve all files */
7200 out_flush();
7201 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7202
7203 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007204 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205
7206# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7207 /* Now see if we can ask about unsaved files */
7208 if (shutdown && !fast && gui.in_use)
7209 /* Need to interact with user, but need SM's permission */
7210 SmcInteractRequest(smc_conn, SmDialogError,
7211 xsmp_handle_interaction, client_data);
7212 else
7213# endif
7214 {
7215 /* Can stop the cycle here */
7216 SmcSaveYourselfDone(smc_conn, True);
7217 xsmp.save_yourself = False;
7218 }
7219}
7220
7221
7222/*
7223 * Callback to warn us of imminent death.
7224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 static void
7226xsmp_die(smc_conn, client_data)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007227 SmcConn smc_conn UNUSED;
7228 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
7230 xsmp_close();
7231
7232 /* quit quickly leaving swapfiles for modified buffers behind */
7233 getout_preserve_modified(0);
7234}
7235
7236
7237/*
7238 * Callback to tell us that save-yourself has completed.
7239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 static void
7241xsmp_save_complete(smc_conn, client_data)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007242 SmcConn smc_conn UNUSED;
7243 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244{
7245 xsmp.save_yourself = False;
7246}
7247
7248
7249/*
7250 * Callback to tell us that an instigated shutdown was cancelled
7251 * (maybe even by us)
7252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 static void
7254xsmp_shutdown_cancelled(smc_conn, client_data)
7255 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007256 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257{
7258 if (xsmp.save_yourself)
7259 SmcSaveYourselfDone(smc_conn, True);
7260 xsmp.save_yourself = False;
7261 xsmp.shutdown = False;
7262}
7263
7264
7265/*
7266 * Callback to tell us that a new ICE connection has been established.
7267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 static void
7269xsmp_ice_connection(iceConn, clientData, opening, watchData)
7270 IceConn iceConn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007271 IcePointer clientData UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 Bool opening;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007273 IcePointer *watchData UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274{
7275 /* Intercept creation of ICE connection fd */
7276 if (opening)
7277 {
7278 xsmp_icefd = IceConnectionNumber(iceConn);
7279 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7280 }
7281}
7282
7283
7284/* Handle any ICE processing that's required; return FAIL if SM lost */
7285 int
7286xsmp_handle_requests()
7287{
7288 Bool rep;
7289
7290 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7291 == IceProcessMessagesIOError)
7292 {
7293 /* Lost ICE */
7294 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007295 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 xsmp_close();
7297 return FAIL;
7298 }
7299 else
7300 return OK;
7301}
7302
7303static int dummy;
7304
7305/* Set up X Session Management Protocol */
7306 void
7307xsmp_init(void)
7308{
7309 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 SmcCallbacks smcallbacks;
7311#if 0
7312 SmPropValue smname;
7313 SmProp smnameprop;
7314 SmProp *smprops[1];
7315#endif
7316
7317 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007318 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
7320 xsmp.save_yourself = xsmp.shutdown = False;
7321
7322 /* Set up SM callbacks - must have all, even if they're not used */
7323 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7324 smcallbacks.save_yourself.client_data = NULL;
7325 smcallbacks.die.callback = xsmp_die;
7326 smcallbacks.die.client_data = NULL;
7327 smcallbacks.save_complete.callback = xsmp_save_complete;
7328 smcallbacks.save_complete.client_data = NULL;
7329 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7330 smcallbacks.shutdown_cancelled.client_data = NULL;
7331
7332 /* Set up a watch on ICE connection creations. The "dummy" argument is
7333 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7334 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7335 {
7336 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007337 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 return;
7339 }
7340
7341 /* Create an SM connection */
7342 xsmp.smcconn = SmcOpenConnection(
7343 NULL,
7344 NULL,
7345 SmProtoMajor,
7346 SmProtoMinor,
7347 SmcSaveYourselfProcMask | SmcDieProcMask
7348 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
7349 &smcallbacks,
7350 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00007351 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 sizeof(errorstring),
7353 errorstring);
7354 if (xsmp.smcconn == NULL)
7355 {
7356 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00007357
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007359 {
7360 vim_snprintf(errorreport, sizeof(errorreport),
7361 _("XSMP SmcOpenConnection failed: %s"), errorstring);
7362 verb_msg((char_u *)errorreport);
7363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 return;
7365 }
7366 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
7367
7368#if 0
7369 /* ID ourselves */
7370 smname.value = "vim";
7371 smname.length = 3;
7372 smnameprop.name = "SmProgram";
7373 smnameprop.type = "SmARRAY8";
7374 smnameprop.num_vals = 1;
7375 smnameprop.vals = &smname;
7376
7377 smprops[0] = &smnameprop;
7378 SmcSetProperties(xsmp.smcconn, 1, smprops);
7379#endif
7380}
7381
7382
7383/* Shut down XSMP comms. */
7384 void
7385xsmp_close()
7386{
7387 if (xsmp_icefd != -1)
7388 {
7389 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00007390 if (xsmp.clientid != NULL)
7391 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00007392 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 xsmp_icefd = -1;
7394 }
7395}
7396#endif /* USE_XSMP */
7397
7398
7399#ifdef EBCDIC
7400/* Translate character to its CTRL- value */
7401char CtrlTable[] =
7402{
7403/* 00 - 5E */
7404 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7405 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7406 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7407 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7408 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7409 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7410/* ^ */ 0x1E,
7411/* - */ 0x1F,
7412/* 61 - 6C */
7413 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7414/* _ */ 0x1F,
7415/* 6E - 80 */
7416 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7417/* a */ 0x01,
7418/* b */ 0x02,
7419/* c */ 0x03,
7420/* d */ 0x37,
7421/* e */ 0x2D,
7422/* f */ 0x2E,
7423/* g */ 0x2F,
7424/* h */ 0x16,
7425/* i */ 0x05,
7426/* 8A - 90 */
7427 0, 0, 0, 0, 0, 0, 0,
7428/* j */ 0x15,
7429/* k */ 0x0B,
7430/* l */ 0x0C,
7431/* m */ 0x0D,
7432/* n */ 0x0E,
7433/* o */ 0x0F,
7434/* p */ 0x10,
7435/* q */ 0x11,
7436/* r */ 0x12,
7437/* 9A - A1 */
7438 0, 0, 0, 0, 0, 0, 0, 0,
7439/* s */ 0x13,
7440/* t */ 0x3C,
7441/* u */ 0x3D,
7442/* v */ 0x32,
7443/* w */ 0x26,
7444/* x */ 0x18,
7445/* y */ 0x19,
7446/* z */ 0x3F,
7447/* AA - AC */
7448 0, 0, 0,
7449/* [ */ 0x27,
7450/* AE - BC */
7451 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7452/* ] */ 0x1D,
7453/* BE - C0 */ 0, 0, 0,
7454/* A */ 0x01,
7455/* B */ 0x02,
7456/* C */ 0x03,
7457/* D */ 0x37,
7458/* E */ 0x2D,
7459/* F */ 0x2E,
7460/* G */ 0x2F,
7461/* H */ 0x16,
7462/* I */ 0x05,
7463/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
7464/* J */ 0x15,
7465/* K */ 0x0B,
7466/* L */ 0x0C,
7467/* M */ 0x0D,
7468/* N */ 0x0E,
7469/* O */ 0x0F,
7470/* P */ 0x10,
7471/* Q */ 0x11,
7472/* R */ 0x12,
7473/* DA - DF */ 0, 0, 0, 0, 0, 0,
7474/* \ */ 0x1C,
7475/* E1 */ 0,
7476/* S */ 0x13,
7477/* T */ 0x3C,
7478/* U */ 0x3D,
7479/* V */ 0x32,
7480/* W */ 0x26,
7481/* X */ 0x18,
7482/* Y */ 0x19,
7483/* Z */ 0x3F,
7484/* EA - FF*/ 0, 0, 0, 0, 0, 0,
7485 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7486};
7487
7488char MetaCharTable[]=
7489{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7490 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
7491 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
7492 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
7493 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
7494};
7495
7496
7497/* TODO: Use characters NOT numbers!!! */
7498char CtrlCharTable[]=
7499{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7500 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
7501 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
7502 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
7503 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
7504};
7505
7506
7507#endif