blob: 36196369cb429eb3131db86fae25f34e6e86af58 [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/*
2997 * return TRUE if "name" is a directory
2998 * 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 Moolenaar071d4272004-06-13 20:20:40 +00003018static int executable_file __ARGS((char_u *name));
3019
3020/*
3021 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3022 */
3023 static int
3024executable_file(name)
3025 char_u *name;
3026{
3027 struct stat st;
3028
3029 if (stat((char *)name, &st))
3030 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003031#ifdef VMS
3032 /* Like on Unix system file can have executable rights but not necessarily
3033 * be an executable, but on Unix is not a default for an ordianry file to
3034 * have an executable flag - on VMS it is in most cases.
3035 * Therefore, this check does not have any sense - let keep us to the
3036 * conventions instead:
3037 * *.COM and *.EXE files are the executables - the rest are not. This is
3038 * not ideal but better then it was.
3039 */
3040 int vms_executable = 0;
3041 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3042 {
3043 if (strstr(vms_tolower((char*)name),".exe") != NULL
3044 || strstr(vms_tolower((char*)name),".com")!= NULL)
3045 vms_executable = 1;
3046 }
3047 return vms_executable;
3048#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051}
3052
3053/*
3054 * Return 1 if "name" can be found in $PATH and executed, 0 if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003055 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 * Return -1 if unknown.
3057 */
3058 int
Bram Moolenaarb5971142015-03-21 17:32:19 +01003059mch_can_exe(name, path, use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 char_u *name;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003061 char_u **path;
Bram Moolenaarb5971142015-03-21 17:32:19 +01003062 int use_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 char_u *buf;
3065 char_u *p, *e;
3066 int retval;
3067
Bram Moolenaarb5971142015-03-21 17:32:19 +01003068 /* When "use_path" is false and if it's an absolute or relative path don't
3069 * need to use $PATH. */
3070 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3071 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003072 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003073 /* There must be a path separator, files in the current directory
3074 * can't be executed. */
3075 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003076 {
3077 if (path != NULL)
3078 {
3079 if (name[0] == '.')
3080 *path = FullName_save(name, TRUE);
3081 else
3082 *path = vim_strsave(name);
3083 }
3084 return TRUE;
3085 }
3086 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
3089 p = (char_u *)getenv("PATH");
3090 if (p == NULL || *p == NUL)
3091 return -1;
3092 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3093 if (buf == NULL)
3094 return -1;
3095
3096 /*
3097 * Walk through all entries in $PATH to check if "name" exists there and
3098 * is an executable file.
3099 */
3100 for (;;)
3101 {
3102 e = (char_u *)strchr((char *)p, ':');
3103 if (e == NULL)
3104 e = p + STRLEN(p);
3105 if (e - p <= 1) /* empty entry means current dir */
3106 STRCPY(buf, "./");
3107 else
3108 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003109 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 add_pathsep(buf);
3111 }
3112 STRCAT(buf, name);
3113 retval = executable_file(buf);
3114 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003115 {
3116 if (path != NULL)
3117 {
3118 if (buf[0] == '.')
3119 *path = FullName_save(buf, TRUE);
3120 else
3121 *path = vim_strsave(buf);
3122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125
3126 if (*e != ':')
3127 break;
3128 p = e + 1;
3129 }
3130
3131 vim_free(buf);
3132 return retval;
3133}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
3135/*
3136 * Check what "name" is:
3137 * NODE_NORMAL: file or directory (or doesn't exist)
3138 * NODE_WRITABLE: writable device, socket, fifo, etc.
3139 * NODE_OTHER: non-writable things
3140 */
3141 int
3142mch_nodetype(name)
3143 char_u *name;
3144{
3145 struct stat st;
3146
3147 if (stat((char *)name, &st))
3148 return NODE_NORMAL;
3149 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3150 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3152 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 /* Everything else is writable? */
3154 return NODE_WRITABLE;
3155}
3156
3157 void
3158mch_early_init()
3159{
3160#ifdef HAVE_CHECK_STACK_GROWTH
3161 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 check_stack_growth((char *)&i);
3164
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003165# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 get_stack_limit();
3167# endif
3168
3169#endif
3170
3171 /*
3172 * Setup an alternative stack for signals. Helps to catch signals when
3173 * running out of stack space.
3174 * Use of sigaltstack() is preferred, it's more portable.
3175 * Ignore any errors.
3176 */
3177#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003178 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 init_signal_stack();
3180#endif
3181}
3182
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003183#if defined(EXITFREE) || defined(PROTO)
3184 void
3185mch_free_mem()
3186{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003187# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3188 if (clip_star.owned)
3189 clip_lose_selection(&clip_star);
3190 if (clip_plus.owned)
3191 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003192# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003193# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003194 if (xterm_Shell != (Widget)0)
3195 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003196# ifndef LESSTIF_VERSION
3197 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003198 if (xterm_dpy != NULL)
3199 XtCloseDisplay(xterm_dpy);
3200 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003201 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003202 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003203# ifdef FEAT_X11
3204 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3205# endif
3206 }
3207# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003208# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003209# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003210 if (x11_display != NULL
3211# ifdef FEAT_XCLIPBOARD
3212 && x11_display != xterm_dpy
3213# endif
3214 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003215 XCloseDisplay(x11_display);
3216# endif
3217# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
3218 vim_free(signal_stack);
3219 signal_stack = NULL;
3220# endif
3221# ifdef FEAT_TITLE
3222 vim_free(oldtitle);
3223 vim_free(oldicon);
3224# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003225}
3226#endif
3227
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228static void exit_scroll __ARGS((void));
3229
3230/*
3231 * Output a newline when exiting.
3232 * Make sure the newline goes to the same stream as the text.
3233 */
3234 static void
3235exit_scroll()
3236{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003237 if (silent_mode)
3238 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (newline_on_exit || msg_didout)
3240 {
3241 if (msg_use_printf())
3242 {
3243 if (info_message)
3244 mch_msg("\n");
3245 else
3246 mch_errmsg("\r\n");
3247 }
3248 else
3249 out_char('\n');
3250 }
3251 else
3252 {
3253 restore_cterm_colors(); /* get original colors back */
3254 msg_clr_eos_force(); /* clear the rest of the display */
3255 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3256 }
3257}
3258
3259 void
3260mch_exit(r)
3261 int r;
3262{
3263 exiting = TRUE;
3264
3265#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3266 x11_export_final_selection();
3267#endif
3268
3269#ifdef FEAT_GUI
3270 if (!gui.in_use)
3271#endif
3272 {
3273 settmode(TMODE_COOK);
3274#ifdef FEAT_TITLE
3275 mch_restore_title(3); /* restore xterm title and icon name */
3276#endif
3277 /*
3278 * When t_ti is not empty but it doesn't cause swapping terminal
3279 * pages, need to output a newline when msg_didout is set. But when
3280 * t_ti does swap pages it should not go to the shell page. Do this
3281 * before stoptermcap().
3282 */
3283 if (swapping_screen() && !newline_on_exit)
3284 exit_scroll();
3285
3286 /* Stop termcap: May need to check for T_CRV response, which
3287 * requires RAW mode. */
3288 stoptermcap();
3289
3290 /*
3291 * A newline is only required after a message in the alternate screen.
3292 * This is set to TRUE by wait_return().
3293 */
3294 if (!swapping_screen() || newline_on_exit)
3295 exit_scroll();
3296
3297 /* Cursor may have been switched off without calling starttermcap()
3298 * when doing "vim -u vimrc" and vimrc contains ":q". */
3299 if (full_screen)
3300 cursor_on();
3301 }
3302 out_flush();
3303 ml_close_all(TRUE); /* remove all memfiles */
3304 may_core_dump();
3305#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 gui_exit(r);
3308#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003309
Bram Moolenaar56718732006-03-15 22:53:57 +00003310#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003311 mac_conv_cleanup();
3312#endif
3313
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#ifdef __QNX__
3315 /* A core dump won't be created if the signal handler
3316 * doesn't return, so we can't call exit() */
3317 if (deadly_signal != 0)
3318 return;
3319#endif
3320
Bram Moolenaar009b2592004-10-24 19:18:58 +00003321#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003322 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003323#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003324
3325#ifdef EXITFREE
3326 free_all_mem();
3327#endif
3328
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 exit(r);
3330}
3331
3332 static void
3333may_core_dump()
3334{
3335 if (deadly_signal != 0)
3336 {
3337 signal(deadly_signal, SIG_DFL);
3338 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3339 }
3340}
3341
3342#ifndef VMS
3343
3344 void
3345mch_settmode(tmode)
3346 int tmode;
3347{
3348 static int first = TRUE;
3349
3350 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3351#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3352 /*
3353 * for "new" tty systems
3354 */
3355# ifdef HAVE_TERMIOS_H
3356 static struct termios told;
3357 struct termios tnew;
3358# else
3359 static struct termio told;
3360 struct termio tnew;
3361# endif
3362
3363 if (first)
3364 {
3365 first = FALSE;
3366# if defined(HAVE_TERMIOS_H)
3367 tcgetattr(read_cmd_fd, &told);
3368# else
3369 ioctl(read_cmd_fd, TCGETA, &told);
3370# endif
3371 }
3372
3373 tnew = told;
3374 if (tmode == TMODE_RAW)
3375 {
3376 /*
3377 * ~ICRNL enables typing ^V^M
3378 */
3379 tnew.c_iflag &= ~ICRNL;
3380 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3381# if defined(IEXTEN) && !defined(__MINT__)
3382 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3383 /* but it breaks function keys on MINT */
3384# endif
3385 );
3386# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3387 tnew.c_oflag &= ~ONLCR;
3388# endif
3389 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3390 tnew.c_cc[VTIME] = 0; /* don't wait */
3391 }
3392 else if (tmode == TMODE_SLEEP)
3393 tnew.c_lflag &= ~(ECHO);
3394
3395# if defined(HAVE_TERMIOS_H)
3396 {
3397 int n = 10;
3398
3399 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3400 * few times. */
3401 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3402 && errno == EINTR && n > 0)
3403 --n;
3404 }
3405# else
3406 ioctl(read_cmd_fd, TCSETA, &tnew);
3407# endif
3408
3409#else
3410
3411 /*
3412 * for "old" tty systems
3413 */
3414# ifndef TIOCSETN
3415# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3416# endif
3417 static struct sgttyb ttybold;
3418 struct sgttyb ttybnew;
3419
3420 if (first)
3421 {
3422 first = FALSE;
3423 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3424 }
3425
3426 ttybnew = ttybold;
3427 if (tmode == TMODE_RAW)
3428 {
3429 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3430 ttybnew.sg_flags |= RAW;
3431 }
3432 else if (tmode == TMODE_SLEEP)
3433 ttybnew.sg_flags &= ~(ECHO);
3434 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3435#endif
3436 curr_tmode = tmode;
3437}
3438
3439/*
3440 * Try to get the code for "t_kb" from the stty setting
3441 *
3442 * Even if termcap claims a backspace key, the user's setting *should*
3443 * prevail. stty knows more about reality than termcap does, and if
3444 * somebody's usual erase key is DEL (which, for most BSD users, it will
3445 * be), they're going to get really annoyed if their erase key starts
3446 * doing forward deletes for no reason. (Eric Fischer)
3447 */
3448 void
3449get_stty()
3450{
3451 char_u buf[2];
3452 char_u *p;
3453
3454 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3455#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3456 /* for "new" tty systems */
3457# ifdef HAVE_TERMIOS_H
3458 struct termios keys;
3459# else
3460 struct termio keys;
3461# endif
3462
3463# if defined(HAVE_TERMIOS_H)
3464 if (tcgetattr(read_cmd_fd, &keys) != -1)
3465# else
3466 if (ioctl(read_cmd_fd, TCGETA, &keys) != -1)
3467# endif
3468 {
3469 buf[0] = keys.c_cc[VERASE];
3470 intr_char = keys.c_cc[VINTR];
3471#else
3472 /* for "old" tty systems */
3473 struct sgttyb keys;
3474
3475 if (ioctl(read_cmd_fd, TIOCGETP, &keys) != -1)
3476 {
3477 buf[0] = keys.sg_erase;
3478 intr_char = keys.sg_kill;
3479#endif
3480 buf[1] = NUL;
3481 add_termcode((char_u *)"kb", buf, FALSE);
3482
3483 /*
3484 * If <BS> and <DEL> are now the same, redefine <DEL>.
3485 */
3486 p = find_termcode((char_u *)"kD");
3487 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3488 do_fixdel(NULL);
3489 }
3490#if 0
3491 } /* to keep cindent happy */
3492#endif
3493}
3494
3495#endif /* VMS */
3496
3497#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
3498/*
3499 * Set mouse clicks on or off.
3500 */
3501 void
3502mch_setmouse(on)
3503 int on;
3504{
3505 static int ison = FALSE;
3506 int xterm_mouse_vers;
3507
3508 if (on == ison) /* return quickly if nothing to do */
3509 return;
3510
3511 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003512
3513# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003514 if (ttym_flags == TTYM_URXVT)
3515 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003516 out_str_nf((char_u *)
3517 (on
3518 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3519 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
3520 ison = on;
3521 }
3522# endif
3523
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003524# ifdef FEAT_MOUSE_SGR
3525 if (ttym_flags == TTYM_SGR)
3526 {
3527 out_str_nf((char_u *)
3528 (on
3529 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3530 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
3531 ison = on;
3532 }
3533# endif
3534
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (xterm_mouse_vers > 0)
3536 {
3537 if (on) /* enable mouse events, use mouse tracking if available */
3538 out_str_nf((char_u *)
3539 (xterm_mouse_vers > 1
3540 ? IF_EB("\033[?1002h", ESC_STR "[?1002h")
3541 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3542 else /* disable mouse events, could probably always send the same */
3543 out_str_nf((char_u *)
3544 (xterm_mouse_vers > 1
3545 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3546 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
3547 ison = on;
3548 }
3549
3550# ifdef FEAT_MOUSE_DEC
3551 else if (ttym_flags == TTYM_DEC)
3552 {
3553 if (on) /* enable mouse events */
3554 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3555 else /* disable mouse events */
3556 out_str_nf((char_u *)"\033['z");
3557 ison = on;
3558 }
3559# endif
3560
3561# ifdef FEAT_MOUSE_GPM
3562 else
3563 {
3564 if (on)
3565 {
3566 if (gpm_open())
3567 ison = TRUE;
3568 }
3569 else
3570 {
3571 gpm_close();
3572 ison = FALSE;
3573 }
3574 }
3575# endif
3576
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003577# ifdef FEAT_SYSMOUSE
3578 else
3579 {
3580 if (on)
3581 {
3582 if (sysmouse_open() == OK)
3583 ison = TRUE;
3584 }
3585 else
3586 {
3587 sysmouse_close();
3588 ison = FALSE;
3589 }
3590 }
3591# endif
3592
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593# ifdef FEAT_MOUSE_JSB
3594 else
3595 {
3596 if (on)
3597 {
3598 /* D - Enable Mouse up/down messages
3599 * L - Enable Left Button Reporting
3600 * M - Enable Middle Button Reporting
3601 * R - Enable Right Button Reporting
3602 * K - Enable SHIFT and CTRL key Reporting
3603 * + - Enable Advanced messaging of mouse moves and up/down messages
3604 * Q - Quiet No Ack
3605 * # - Numeric value of mouse pointer required
3606 * 0 = Multiview 2000 cursor, used as standard
3607 * 1 = Windows Arrow
3608 * 2 = Windows I Beam
3609 * 3 = Windows Hour Glass
3610 * 4 = Windows Cross Hair
3611 * 5 = Windows UP Arrow
3612 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003613# ifdef JSBTERM_MOUSE_NONADVANCED
3614 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3616 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003617# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3619 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003620# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 ison = TRUE;
3622 }
3623 else
3624 {
3625 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3626 ESC_STR "[0~ZwQ" ESC_STR "\\"));
3627 ison = FALSE;
3628 }
3629 }
3630# endif
3631# ifdef FEAT_MOUSE_PTERM
3632 else
3633 {
3634 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3635 if (on)
3636 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3637 else
3638 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
3639 ison = on;
3640 }
3641# endif
3642}
3643
3644/*
3645 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3646 */
3647 void
3648check_mouse_termcode()
3649{
3650# ifdef FEAT_MOUSE_XTERM
3651 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003652# ifdef FEAT_MOUSE_URXVT
3653 && use_xterm_mouse() != 3
3654# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655# ifdef FEAT_GUI
3656 && !gui.in_use
3657# endif
3658 )
3659 {
3660 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003661 ? IF_EB("\233M", CSI_STR "M")
3662 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if (*p_mouse != NUL)
3664 {
3665 /* force mouse off and maybe on to send possibly new mouse
3666 * activation sequence to the xterm, with(out) drag tracing. */
3667 mch_setmouse(FALSE);
3668 setmouse();
3669 }
3670 }
3671 else
3672 del_mouse_termcode(KS_MOUSE);
3673# endif
3674
3675# ifdef FEAT_MOUSE_GPM
3676 if (!use_xterm_mouse()
3677# ifdef FEAT_GUI
3678 && !gui.in_use
3679# endif
3680 )
3681 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3682# endif
3683
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003684# ifdef FEAT_SYSMOUSE
3685 if (!use_xterm_mouse()
3686# ifdef FEAT_GUI
3687 && !gui.in_use
3688# endif
3689 )
3690 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3691# endif
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003694 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 if (!use_xterm_mouse()
3696# ifdef FEAT_GUI
3697 && !gui.in_use
3698# endif
3699 )
3700 set_mouse_termcode(KS_JSBTERM_MOUSE,
3701 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3702 else
3703 del_mouse_termcode(KS_JSBTERM_MOUSE);
3704# endif
3705
3706# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003707 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 * define it in the GUI or when using an xterm. */
3709 if (!use_xterm_mouse()
3710# ifdef FEAT_GUI
3711 && !gui.in_use
3712# endif
3713 )
3714 set_mouse_termcode(KS_NETTERM_MOUSE,
3715 (char_u *)IF_EB("\033}", ESC_STR "}"));
3716 else
3717 del_mouse_termcode(KS_NETTERM_MOUSE);
3718# endif
3719
3720# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003721 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003722 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723# ifdef FEAT_GUI
3724 && !gui.in_use
3725# endif
3726 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003727 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3728 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 else
3730 del_mouse_termcode(KS_DEC_MOUSE);
3731# endif
3732# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003733 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003734 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735# ifdef FEAT_GUI
3736 && !gui.in_use
3737# endif
3738 )
3739 set_mouse_termcode(KS_PTERM_MOUSE,
3740 (char_u *) IF_EB("\033[", ESC_STR "["));
3741 else
3742 del_mouse_termcode(KS_PTERM_MOUSE);
3743# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003744# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003745 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003746 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003747# ifdef FEAT_GUI
3748 && !gui.in_use
3749# endif
3750 )
3751 {
3752 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3753 ? IF_EB("\233", CSI_STR)
3754 : IF_EB("\033[", ESC_STR "[")));
3755
3756 if (*p_mouse != NUL)
3757 {
3758 mch_setmouse(FALSE);
3759 setmouse();
3760 }
3761 }
3762 else
3763 del_mouse_termcode(KS_URXVT_MOUSE);
3764# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003765# ifdef FEAT_MOUSE_SGR
Bram Moolenaar24dc2302014-05-13 20:19:58 +02003766 /* There is no conflict with xterm mouse */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003767 if (use_xterm_mouse() == 4
3768# ifdef FEAT_GUI
3769 && !gui.in_use
3770# endif
3771 )
3772 {
3773 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3774 ? IF_EB("\233<", CSI_STR "<")
3775 : IF_EB("\033[<", ESC_STR "[<")));
3776
3777 if (*p_mouse != NUL)
3778 {
3779 mch_setmouse(FALSE);
3780 setmouse();
3781 }
3782 }
3783 else
3784 del_mouse_termcode(KS_SGR_MOUSE);
3785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786}
3787#endif
3788
3789/*
3790 * set screen mode, always fails.
3791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 int
3793mch_screenmode(arg)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003794 char_u *arg UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795{
3796 EMSG(_(e_screenmode));
3797 return FAIL;
3798}
3799
3800#ifndef VMS
3801
3802/*
3803 * Try to get the current window size:
3804 * 1. with an ioctl(), most accurate method
3805 * 2. from the environment variables LINES and COLUMNS
3806 * 3. from the termcap
3807 * 4. keep using the old values
3808 * Return OK when size could be determined, FAIL otherwise.
3809 */
3810 int
3811mch_get_shellsize()
3812{
3813 long rows = 0;
3814 long columns = 0;
3815 char_u *p;
3816
3817 /*
3818 * For OS/2 use _scrsize().
3819 */
3820# ifdef __EMX__
3821 {
3822 int s[2];
3823
3824 _scrsize(s);
3825 columns = s[0];
3826 rows = s[1];
3827 }
3828# endif
3829
3830 /*
3831 * 1. try using an ioctl. It is the most accurate method.
3832 *
3833 * Try using TIOCGWINSZ first, some systems that have it also define
3834 * TIOCGSIZE but don't have a struct ttysize.
3835 */
3836# ifdef TIOCGWINSZ
3837 {
3838 struct winsize ws;
3839 int fd = 1;
3840
3841 /* When stdout is not a tty, use stdin for the ioctl(). */
3842 if (!isatty(fd) && isatty(read_cmd_fd))
3843 fd = read_cmd_fd;
3844 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
3845 {
3846 columns = ws.ws_col;
3847 rows = ws.ws_row;
3848 }
3849 }
3850# else /* TIOCGWINSZ */
3851# ifdef TIOCGSIZE
3852 {
3853 struct ttysize ts;
3854 int fd = 1;
3855
3856 /* When stdout is not a tty, use stdin for the ioctl(). */
3857 if (!isatty(fd) && isatty(read_cmd_fd))
3858 fd = read_cmd_fd;
3859 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
3860 {
3861 columns = ts.ts_cols;
3862 rows = ts.ts_lines;
3863 }
3864 }
3865# endif /* TIOCGSIZE */
3866# endif /* TIOCGWINSZ */
3867
3868 /*
3869 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003870 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
3871 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003873 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
3875 if ((p = (char_u *)getenv("LINES")))
3876 rows = atoi((char *)p);
3877 if ((p = (char_u *)getenv("COLUMNS")))
3878 columns = atoi((char *)p);
3879 }
3880
3881#ifdef HAVE_TGETENT
3882 /*
3883 * 3. try reading "co" and "li" entries from termcap
3884 */
3885 if (columns == 0 || rows == 0)
3886 getlinecol(&columns, &rows);
3887#endif
3888
3889 /*
3890 * 4. If everything fails, use the old values
3891 */
3892 if (columns <= 0 || rows <= 0)
3893 return FAIL;
3894
3895 Rows = rows;
3896 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02003897 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 return OK;
3899}
3900
3901/*
3902 * Try to set the window size to Rows and Columns.
3903 */
3904 void
3905mch_set_shellsize()
3906{
3907 if (*T_CWS)
3908 {
3909 /*
3910 * NOTE: if you get an error here that term_set_winsize() is
3911 * undefined, check the output of configure. It could probably not
3912 * find a ncurses, termcap or termlib library.
3913 */
3914 term_set_winsize((int)Rows, (int)Columns);
3915 out_flush();
3916 screen_start(); /* don't know where cursor is now */
3917 }
3918}
3919
3920#endif /* VMS */
3921
3922/*
3923 * Rows and/or Columns has changed.
3924 */
3925 void
3926mch_new_shellsize()
3927{
3928 /* Nothing to do. */
3929}
3930
Bram Moolenaar205b8862011-09-07 15:04:31 +02003931/*
3932 * Wait for process "child" to end.
3933 * Return "child" if it exited properly, <= 0 on error.
3934 */
3935 static pid_t
3936wait4pid(child, status)
3937 pid_t child;
3938 waitstatus *status;
3939{
3940 pid_t wait_pid = 0;
3941
3942 while (wait_pid != child)
3943 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02003944 /* When compiled with Python threads are probably used, in which case
3945 * wait() sometimes hangs for no obvious reason. Use waitpid()
3946 * instead and loop (like the GUI). Also needed for other interfaces,
3947 * they might call system(). */
3948# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02003949 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02003950# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02003951 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02003952# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02003953 if (wait_pid == 0)
3954 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003955 /* Wait for 10 msec before trying again. */
Bram Moolenaar205b8862011-09-07 15:04:31 +02003956 mch_delay(10L, TRUE);
3957 continue;
3958 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02003959 if (wait_pid <= 0
3960# ifdef ECHILD
3961 && errno == ECHILD
3962# endif
3963 )
3964 break;
3965 }
3966 return wait_pid;
3967}
3968
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 int
3970mch_call_shell(cmd, options)
3971 char_u *cmd;
3972 int options; /* SHELL_*, see vim.h */
3973{
3974#ifdef VMS
3975 char *ifn = NULL;
3976 char *ofn = NULL;
3977#endif
3978 int tmode = cur_tmode;
3979#ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */
3980 int x;
3981# ifndef __EMX__
3982 char_u *newcmd; /* only needed for unix */
3983# else
3984 /*
3985 * Set the preferred shell in the EMXSHELL environment variable (but
3986 * only if it is different from what is already in the environment).
3987 * Emx then takes care of whether to use "/c" or "-c" in an
3988 * intelligent way. Simply pass the whole thing to emx's system() call.
3989 * Emx also starts an interactive shell if system() is passed an empty
3990 * string.
3991 */
3992 char_u *p, *old;
3993
3994 if (((old = (char_u *)getenv("EMXSHELL")) == NULL) || STRCMP(old, p_sh))
3995 {
3996 /* should check HAVE_SETENV, but I know we don't have it. */
3997 p = alloc(10 + strlen(p_sh));
3998 if (p)
3999 {
4000 sprintf((char *)p, "EMXSHELL=%s", p_sh);
4001 putenv((char *)p); /* don't free the pointer! */
4002 }
4003 }
4004# endif
4005
4006 out_flush();
4007
4008 if (options & SHELL_COOKED)
4009 settmode(TMODE_COOK); /* set to normal mode */
4010
Bram Moolenaar62b42182010-09-21 22:09:37 +02004011# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004012 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004013 loose_clipboard();
4014# endif
4015
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016# ifdef __EMX__
4017 if (cmd == NULL)
4018 x = system(""); /* this starts an interactive shell in emx */
4019 else
4020 x = system((char *)cmd);
4021 /* system() returns -1 when error occurs in starting shell */
4022 if (x == -1 && !emsg_silent)
4023 {
4024 MSG_PUTS(_("\nCannot execute shell "));
4025 msg_outtrans(p_sh);
4026 msg_putchar('\n');
4027 }
4028# else /* not __EMX__ */
4029 if (cmd == NULL)
4030 x = system((char *)p_sh);
4031 else
4032 {
4033# ifdef VMS
4034 if (ofn = strchr((char *)cmd, '>'))
4035 *ofn++ = '\0';
4036 if (ifn = strchr((char *)cmd, '<'))
4037 {
4038 char *p;
4039
4040 *ifn++ = '\0';
4041 p = strchr(ifn,' '); /* chop off any trailing spaces */
4042 if (p)
4043 *p = '\0';
4044 }
4045 if (ofn)
4046 x = vms_sys((char *)cmd, ofn, ifn);
4047 else
4048 x = system((char *)cmd);
4049# else
4050 newcmd = lalloc(STRLEN(p_sh)
4051 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4052 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4053 if (newcmd == NULL)
4054 x = 0;
4055 else
4056 {
4057 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4058 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4059 (char *)p_shcf,
4060 (char *)cmd);
4061 x = system((char *)newcmd);
4062 vim_free(newcmd);
4063 }
4064# endif
4065 }
4066# ifdef VMS
4067 x = vms_sys_status(x);
4068# endif
4069 if (emsg_silent)
4070 ;
4071 else if (x == 127)
4072 MSG_PUTS(_("\nCannot execute shell sh\n"));
4073# endif /* __EMX__ */
4074 else if (x && !(options & SHELL_SILENT))
4075 {
4076 MSG_PUTS(_("\nshell returned "));
4077 msg_outnum((long)x);
4078 msg_putchar('\n');
4079 }
4080
4081 if (tmode == TMODE_RAW)
4082 settmode(TMODE_RAW); /* set to raw mode */
4083# ifdef FEAT_TITLE
4084 resettitle();
4085# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004086# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4087 restore_clipboard();
4088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 return x;
4090
4091#else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */
4092
Bram Moolenaardf177f62005-02-22 08:39:57 +00004093# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4094 127, some shells use that already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
4096 char_u *newcmd = NULL;
4097 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004098 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 pid_t wait_pid = 0;
4100# ifdef HAVE_UNION_WAIT
4101 union wait status;
4102# else
4103 int status = -1;
4104# endif
4105 int retval = -1;
4106 char **argv = NULL;
4107 int argc;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004108 char_u *p_shcf_copy = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 int i;
4110 char_u *p;
4111 int inquote;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004113# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 int pty_slave_fd = -1;
4115 char *tty_name;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004116# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004117 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 int fd_fromshell[2];
4119 int pipe_error = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004120# ifdef HAVE_SETENV
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 char envbuf[50];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004122# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 static char envbuf_Rows[20];
4124 static char envbuf_Columns[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004126 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127
Bram Moolenaar62b42182010-09-21 22:09:37 +02004128 newcmd = vim_strsave(p_sh);
4129 if (newcmd == NULL) /* out of memory */
4130 goto error;
4131
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 out_flush();
4133 if (options & SHELL_COOKED)
4134 settmode(TMODE_COOK); /* set to normal mode */
4135
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004136 /*
4137 * Do this loop twice:
4138 * 1: find number of arguments
4139 * 2: separate them and build argv[]
4140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 for (i = 0; i < 2; ++i)
4142 {
4143 p = newcmd;
4144 inquote = FALSE;
4145 argc = 0;
4146 for (;;)
4147 {
4148 if (i == 1)
4149 argv[argc] = (char *)p;
4150 ++argc;
4151 while (*p && (inquote || (*p != ' ' && *p != TAB)))
4152 {
4153 if (*p == '"')
4154 inquote = !inquote;
4155 ++p;
4156 }
4157 if (*p == NUL)
4158 break;
4159 if (i == 1)
4160 *p++ = NUL;
4161 p = skipwhite(p);
4162 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004163 if (argv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004165 /*
4166 * Account for possible multiple args in p_shcf.
4167 */
4168 p = p_shcf;
4169 for (;;)
4170 {
4171 p = skiptowhite(p);
4172 if (*p == NUL)
4173 break;
4174 ++argc;
4175 p = skipwhite(p);
4176 }
4177
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 argv = (char **)alloc((unsigned)((argc + 4) * sizeof(char *)));
4179 if (argv == NULL) /* out of memory */
4180 goto error;
4181 }
4182 }
4183 if (cmd != NULL)
4184 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004185 char_u *s;
4186
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 if (extra_shell_arg != NULL)
4188 argv[argc++] = (char *)extra_shell_arg;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004189
4190 /* Break 'shellcmdflag' into white separated parts. This doesn't
4191 * handle quoted strings, they are very unlikely to appear. */
4192 p_shcf_copy = alloc((unsigned)STRLEN(p_shcf) + 1);
4193 if (p_shcf_copy == NULL) /* out of memory */
4194 goto error;
4195 s = p_shcf_copy;
4196 p = p_shcf;
4197 while (*p != NUL)
4198 {
4199 argv[argc++] = (char *)s;
4200 while (*p && *p != ' ' && *p != TAB)
4201 *s++ = *p++;
4202 *s++ = NUL;
4203 p = skipwhite(p);
4204 }
4205
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 argv[argc++] = (char *)cmd;
4207 }
4208 argv[argc] = NULL;
4209
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004211 * For the GUI, when writing the output into the buffer and when reading
4212 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4213 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004215 if ((options & (SHELL_READ|SHELL_WRITE))
4216# ifdef FEAT_GUI
4217 || (gui.in_use && show_shell_mess)
4218# endif
4219 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004221# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 /*
4223 * Try to open a master pty.
4224 * If this works, open the slave pty.
4225 * If the slave can't be opened, close the master pty.
4226 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004227 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
4229 pty_master_fd = OpenPTY(&tty_name); /* open pty */
Bram Moolenaare70172e2011-08-04 19:36:52 +02004230 if (pty_master_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 {
Bram Moolenaare70172e2011-08-04 19:36:52 +02004232 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4233 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4234 * adding O_NOCTTY always works when defined. */
4235#ifdef O_NOCTTY
4236 pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4237#else
4238 pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4239#endif
4240 if (pty_slave_fd < 0)
4241 {
4242 close(pty_master_fd);
4243 pty_master_fd = -1;
4244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
4246 }
4247 /*
4248 * If not opening a pty or it didn't work, try using pipes.
4249 */
4250 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004251# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
4253 pipe_error = (pipe(fd_toshell) < 0);
4254 if (!pipe_error) /* pipe create OK */
4255 {
4256 pipe_error = (pipe(fd_fromshell) < 0);
4257 if (pipe_error) /* pipe create failed */
4258 {
4259 close(fd_toshell[0]);
4260 close(fd_toshell[1]);
4261 }
4262 }
4263 if (pipe_error)
4264 {
4265 MSG_PUTS(_("\nCannot create pipes\n"));
4266 out_flush();
4267 }
4268 }
4269 }
4270
4271 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 {
4273# ifdef __BEOS__
4274 beos_cleanup_read_thread();
4275# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004276
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 if ((pid = fork()) == -1) /* maybe we should use vfork() */
4278 {
4279 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004280 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004282 || (gui.in_use && show_shell_mess)
4283# endif
4284 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004286# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 if (pty_master_fd >= 0) /* close the pseudo tty */
4288 {
4289 close(pty_master_fd);
4290 close(pty_slave_fd);
4291 }
4292 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004293# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 {
4295 close(fd_toshell[0]);
4296 close(fd_toshell[1]);
4297 close(fd_fromshell[0]);
4298 close(fd_fromshell[1]);
4299 }
4300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 else if (pid == 0) /* child */
4303 {
4304 reset_signals(); /* handle signals normally */
4305
4306 if (!show_shell_mess || (options & SHELL_EXPAND))
4307 {
4308 int fd;
4309
4310 /*
4311 * Don't want to show any message from the shell. Can't just
4312 * close stdout and stderr though, because some systems will
4313 * break if you try to write to them after that, so we must
4314 * use dup() to replace them with something else -- webb
4315 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4316 * waiting for input.
4317 */
4318 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4319 fclose(stdin);
4320 fclose(stdout);
4321 fclose(stderr);
4322
4323 /*
4324 * If any of these open()'s and dup()'s fail, we just continue
4325 * anyway. It's not fatal, and on most systems it will make
4326 * no difference at all. On a few it will cause the execvp()
4327 * to exit with a non-zero status even when the completion
4328 * could be done, which is nothing too serious. If the open()
4329 * or dup() failed we'd just do the same thing ourselves
4330 * anyway -- webb
4331 */
4332 if (fd >= 0)
4333 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004334 ignored = dup(fd); /* To replace stdin (fd 0) */
4335 ignored = dup(fd); /* To replace stdout (fd 1) */
4336 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
4338 /* Don't need this now that we've duplicated it */
4339 close(fd);
4340 }
4341 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004342 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004344 || gui.in_use
4345# endif
4346 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 {
4348
Bram Moolenaardf177f62005-02-22 08:39:57 +00004349# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004350 /* Create our own process group, so that the child and all its
4351 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004352 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004353 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004354 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004355 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004356# if defined(SIGHUP)
4357 /* When doing "!xterm&" and 'shell' is bash: the shell
4358 * will exit and send SIGHUP to all processes in its
4359 * group, killing the just started process. Ignore SIGHUP
4360 * to avoid that. (suggested by Simon Schubert)
4361 */
4362 signal(SIGHUP, SIG_IGN);
4363# endif
4364 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004365# endif
4366# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004367 if (pty_slave_fd >= 0)
4368 {
4369 /* push stream discipline modules */
4370 if (options & SHELL_COOKED)
4371 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004373 /* Try to become controlling tty (probably doesn't work,
4374 * unless run by root) */
4375 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004377 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 /* Simulate to have a dumb terminal (for now) */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004380# ifdef HAVE_SETENV
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 setenv("TERM", "dumb", 1);
4382 sprintf((char *)envbuf, "%ld", Rows);
4383 setenv("ROWS", (char *)envbuf, 1);
4384 sprintf((char *)envbuf, "%ld", Rows);
4385 setenv("LINES", (char *)envbuf, 1);
4386 sprintf((char *)envbuf, "%ld", Columns);
4387 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004388# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 /*
4390 * Putenv does not copy the string, it has to remain valid.
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004391 * Use a static array to avoid losing allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 */
4393 putenv("TERM=dumb");
4394 sprintf(envbuf_Rows, "ROWS=%ld", Rows);
4395 putenv(envbuf_Rows);
4396 sprintf(envbuf_Rows, "LINES=%ld", Rows);
4397 putenv(envbuf_Rows);
4398 sprintf(envbuf_Columns, "COLUMNS=%ld", Columns);
4399 putenv(envbuf_Columns);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
Bram Moolenaara5792f52005-11-23 21:25:05 +00004402 /*
4403 * stderr is only redirected when using the GUI, so that a
4404 * program like gpg can still access the terminal to get a
4405 * passphrase using stderr.
4406 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004407# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 if (pty_master_fd >= 0)
4409 {
4410 close(pty_master_fd); /* close master side of pty */
4411
4412 /* set up stdin/stdout/stderr for the child */
4413 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004414 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004416 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004417 if (gui.in_use)
4418 {
4419 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004420 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422
4423 close(pty_slave_fd); /* has been dupped, close it now */
4424 }
4425 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004426# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 {
4428 /* set up stdin for the child */
4429 close(fd_toshell[1]);
4430 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004431 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 close(fd_toshell[0]);
4433
4434 /* set up stdout for the child */
4435 close(fd_fromshell[0]);
4436 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004437 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 close(fd_fromshell[1]);
4439
Bram Moolenaara5792f52005-11-23 21:25:05 +00004440# ifdef FEAT_GUI
4441 if (gui.in_use)
4442 {
4443 /* set up stderr for the child */
4444 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004445 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004446 }
4447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004450
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 /*
4452 * There is no type cast for the argv, because the type may be
4453 * different on different machines. This may cause a warning
4454 * message with strict compilers, don't worry about it.
4455 * Call _exit() instead of exit() to avoid closing the connection
4456 * to the X server (esp. with GTK, which uses atexit()).
4457 */
4458 execvp(argv[0], argv);
4459 _exit(EXEC_FAILED); /* exec failed, return failure code */
4460 }
4461 else /* parent */
4462 {
4463 /*
4464 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004465 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 */
4467 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004468 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
4470 /*
4471 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004472 * This is also used to pipe stdin/stdout to/from the external
4473 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004475 if ((options & (SHELL_READ|SHELL_WRITE))
4476# ifdef FEAT_GUI
4477 || (gui.in_use && show_shell_mess)
4478# endif
4479 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004481# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004483# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4487 int ta_len = 0; /* valid bytes in ta_buf[] */
4488 int len;
4489 int p_more_save;
4490 int old_State;
4491 int c;
4492 int toshell_fd;
4493 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004494 garray_T ga;
4495 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004496# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4497 struct timeval start_tv;
4498# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
Bram Moolenaardf177f62005-02-22 08:39:57 +00004500# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (pty_master_fd >= 0)
4502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 fromshell_fd = pty_master_fd;
4504 toshell_fd = dup(pty_master_fd);
4505 }
4506 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 {
4509 close(fd_toshell[0]);
4510 close(fd_fromshell[1]);
4511 toshell_fd = fd_toshell[1];
4512 fromshell_fd = fd_fromshell[0];
4513 }
4514
4515 /*
4516 * Write to the child if there are typed characters.
4517 * Read from the child if there are characters available.
4518 * Repeat the reading a few times if more characters are
4519 * available. Need to check for typed keys now and then, but
4520 * not too often (delays when no chars are available).
4521 * This loop is quit if no characters can be read from the pty
4522 * (WaitForChar detected special condition), or there are no
4523 * characters available and the child has exited.
4524 * Only check if the child has exited when there is no more
4525 * output. The child may exit before all the output has
4526 * been printed.
4527 *
4528 * Currently this busy loops!
4529 * This can probably dead-lock when the write blocks!
4530 */
4531 p_more_save = p_more;
4532 p_more = FALSE;
4533 old_State = State;
4534 State = EXTERNCMD; /* don't redraw at window resize */
4535
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004536 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004537 {
4538 /* Fork a process that will write the lines to the
4539 * external program. */
4540 if ((wpid = fork()) == -1)
4541 {
4542 MSG_PUTS(_("\nCannot fork\n"));
4543 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004544 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004545 {
4546 linenr_T lnum = curbuf->b_op_start.lnum;
4547 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004548 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004549 size_t l;
4550
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004551 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004552 for (;;)
4553 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004554 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004555 if (l == 0)
4556 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004557 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004558 /* NL -> NUL translation */
4559 len = write(toshell_fd, "", (size_t)1);
4560 else
4561 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004562 char_u *s = vim_strchr(lp + written, NL);
4563
Bram Moolenaar89d40322006-08-29 15:30:07 +00004564 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004565 s == NULL ? l
4566 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004567 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004568 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004569 {
4570 /* Finished a line, add a NL, unless this line
4571 * should not have one. */
4572 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004573 || (!curbuf->b_p_bin
4574 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004575 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004576 && (lnum !=
4577 curbuf->b_ml.ml_line_count
4578 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004579 ignored = write(toshell_fd, "\n",
4580 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004581 ++lnum;
4582 if (lnum > curbuf->b_op_end.lnum)
4583 {
4584 /* finished all the lines, close pipe */
4585 close(toshell_fd);
4586 toshell_fd = -1;
4587 break;
4588 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004589 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004590 written = 0;
4591 }
4592 else if (len > 0)
4593 written += len;
4594 }
4595 _exit(0);
4596 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004597 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004598 {
4599 close(toshell_fd);
4600 toshell_fd = -1;
4601 }
4602 }
4603
4604 if (options & SHELL_READ)
4605 ga_init2(&ga, 1, BUFLEN);
4606
4607 noread_cnt = 0;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004608# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4609 gettimeofday(&start_tv, NULL);
4610# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 for (;;)
4612 {
4613 /*
4614 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004615 * if there are any.
4616 * Don't do this if we are expanding wild cards (would eat
4617 * typeahead).
4618 * Don't do this when filtering and terminal is in cooked
4619 * mode, the shell command will handle the I/O. Avoids
4620 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004621 * Don't get characters when the child has already
4622 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004623 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004624 * while (noread_cnt > 4), avoids that ":r !ls" eats
4625 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 */
4627 len = 0;
4628 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004629 && ((options &
4630 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4631 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004632# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004633 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004634# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004635 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004636 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004637 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004639 if (ta_len == 0)
4640 {
4641 /* Get extra characters when we don't have any.
4642 * Reset the counter and timer. */
4643 noread_cnt = 0;
4644# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4645 gettimeofday(&start_tv, NULL);
4646# endif
4647 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4648 }
4649 if (ta_len > 0 || len > 0)
4650 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 /*
4652 * For pipes:
4653 * Check for CTRL-C: send interrupt signal to child.
4654 * Check for CTRL-D: EOF, close pipe to child.
4655 */
4656 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4657 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004658# ifdef SIGINT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 /*
4660 * Send SIGINT to the child's group or all
4661 * processes in our group.
4662 */
4663 if (ta_buf[ta_len] == Ctrl_C
4664 || ta_buf[ta_len] == intr_char)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004665 {
4666# ifdef HAVE_SETSID
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 kill(-pid, SIGINT);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004668# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 kill(0, SIGINT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670# endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00004671 if (wpid > 0)
4672 kill(wpid, SIGINT);
4673 }
4674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (pty_master_fd < 0 && toshell_fd >= 0
4676 && ta_buf[ta_len] == Ctrl_D)
4677 {
4678 close(toshell_fd);
4679 toshell_fd = -1;
4680 }
4681 }
4682
4683 /* replace K_BS by <BS> and K_DEL by <DEL> */
4684 for (i = ta_len; i < ta_len + len; ++i)
4685 {
4686 if (ta_buf[i] == CSI && len - i > 2)
4687 {
4688 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4689 if (c == K_DEL || c == K_KDEL || c == K_BS)
4690 {
4691 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4692 (size_t)(len - i - 2));
4693 if (c == K_DEL || c == K_KDEL)
4694 ta_buf[i] = DEL;
4695 else
4696 ta_buf[i] = Ctrl_H;
4697 len -= 2;
4698 }
4699 }
4700 else if (ta_buf[i] == '\r')
4701 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00004702# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00004704 i += (*mb_ptr2len_len)(ta_buf + i,
4705 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004706# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 }
4708
4709 /*
4710 * For pipes: echo the typed characters.
4711 * For a pty this does not seem to work.
4712 */
4713 if (pty_master_fd < 0)
4714 {
4715 for (i = ta_len; i < ta_len + len; ++i)
4716 {
4717 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4718 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004719# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 else if (has_mbyte)
4721 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004722 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723
4724 msg_outtrans_len(ta_buf + i, l);
4725 i += l - 1;
4726 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 else
4729 msg_outtrans_len(ta_buf + i, 1);
4730 }
4731 windgoto(msg_row, msg_col);
4732 out_flush();
4733 }
4734
4735 ta_len += len;
4736
4737 /*
4738 * Write the characters to the child, unless EOF has
4739 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004740 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004741 * When writing buffer lines, drop the typed
4742 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004744 if (options & SHELL_WRITE)
4745 ta_len = 0;
4746 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
4748 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
4749 if (len > 0)
4750 {
4751 ta_len -= len;
4752 mch_memmove(ta_buf, ta_buf + len, ta_len);
4753 }
4754 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757
Bram Moolenaardf177f62005-02-22 08:39:57 +00004758 if (got_int)
4759 {
4760 /* CTRL-C sends a signal to the child, we ignore it
4761 * ourselves */
4762# ifdef HAVE_SETSID
4763 kill(-pid, SIGINT);
4764# else
4765 kill(0, SIGINT);
4766# endif
4767 if (wpid > 0)
4768 kill(wpid, SIGINT);
4769 got_int = FALSE;
4770 }
4771
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 /*
4773 * Check if the child has any characters to be printed.
4774 * Read them and write them to our window. Repeat this as
4775 * long as there is something to do, avoid the 10ms wait
4776 * for mch_inchar(), or sending typeahead characters to
4777 * the external process.
4778 * TODO: This should handle escape sequences, compatible
4779 * to some terminal (vt52?).
4780 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004781 ++noread_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 while (RealWaitForChar(fromshell_fd, 10L, NULL))
4783 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004784 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00004785# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004787# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00004789# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 );
4791 if (len <= 0) /* end of file or error */
4792 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004793
4794 noread_cnt = 0;
4795 if (options & SHELL_READ)
4796 {
4797 /* Do NUL -> NL translation, append NL separated
4798 * lines to the current buffer. */
4799 for (i = 0; i < len; ++i)
4800 {
4801 if (buffer[i] == NL)
4802 append_ga_line(&ga);
4803 else if (buffer[i] == NUL)
4804 ga_append(&ga, NL);
4805 else
4806 ga_append(&ga, buffer[i]);
4807 }
4808 }
4809# ifdef FEAT_MBYTE
4810 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 {
4812 int l;
4813
Bram Moolenaardf177f62005-02-22 08:39:57 +00004814 len += buffer_off;
4815 buffer[len] = NUL;
4816
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 /* Check if the last character in buffer[] is
4818 * incomplete, keep these bytes for the next
4819 * round. */
4820 for (p = buffer; p < buffer + len; p += l)
4821 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004822 l = mb_cptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 if (l == 0)
4824 l = 1; /* NUL byte? */
4825 else if (MB_BYTE2LEN(*p) != l)
4826 break;
4827 }
4828 if (p == buffer) /* no complete character */
4829 {
4830 /* avoid getting stuck at an illegal byte */
4831 if (len >= 12)
4832 ++p;
4833 else
4834 {
4835 buffer_off = len;
4836 continue;
4837 }
4838 }
4839 c = *p;
4840 *p = NUL;
4841 msg_puts(buffer);
4842 if (p < buffer + len)
4843 {
4844 *p = c;
4845 buffer_off = (buffer + len) - p;
4846 mch_memmove(buffer, p, buffer_off);
4847 continue;
4848 }
4849 buffer_off = 0;
4850 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004851# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 {
4854 buffer[len] = NUL;
4855 msg_puts(buffer);
4856 }
4857
4858 windgoto(msg_row, msg_col);
4859 cursor_on();
4860 out_flush();
4861 if (got_int)
4862 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004863
4864# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4865 {
4866 struct timeval now_tv;
4867 long msec;
4868
4869 /* Avoid that we keep looping here without
4870 * checking for a CTRL-C for a long time. Don't
4871 * break out too often to avoid losing typeahead. */
4872 gettimeofday(&now_tv, NULL);
4873 msec = (now_tv.tv_sec - start_tv.tv_sec) * 1000L
4874 + (now_tv.tv_usec - start_tv.tv_usec) / 1000L;
4875 if (msec > 2000)
4876 {
4877 noread_cnt = 5;
4878 break;
4879 }
4880 }
4881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004884 /* If we already detected the child has finished break the
4885 * loop now. */
4886 if (wait_pid == pid)
4887 break;
4888
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 /*
4890 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004891 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004893# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004894 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004895# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004897# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
4899 || (wait_pid == pid && WIFEXITED(status)))
4900 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004901 /* Don't break the loop yet, try reading more
4902 * characters from "fromshell_fd" first. When using
4903 * pipes there might still be something to read and
4904 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004907 else
4908 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004909
Bram Moolenaar95a51352013-03-21 22:53:50 +01004910# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004911 /* Handle any X events, e.g. serving the clipboard. */
4912 clip_update();
4913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915finished:
4916 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004917 if (options & SHELL_READ)
4918 {
4919 if (ga.ga_len > 0)
4920 {
4921 append_ga_line(&ga);
4922 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004923 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004924 }
4925 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004926 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004927 ga_clear(&ga);
4928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 /*
4931 * Give all typeahead that wasn't used back to ui_inchar().
4932 */
4933 if (ta_len)
4934 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 State = old_State;
4936 if (toshell_fd >= 0)
4937 close(toshell_fd);
4938 close(fromshell_fd);
4939 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01004940# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004941 else
4942 {
4943 /*
4944 * Similar to the loop above, but only handle X events, no
4945 * I/O.
4946 */
4947 for (;;)
4948 {
4949 if (got_int)
4950 {
4951 /* CTRL-C sends a signal to the child, we ignore it
4952 * ourselves */
4953# ifdef HAVE_SETSID
4954 kill(-pid, SIGINT);
4955# else
4956 kill(0, SIGINT);
4957# endif
4958 got_int = FALSE;
4959 }
4960# ifdef __NeXT__
4961 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
4962# else
4963 wait_pid = waitpid(pid, &status, WNOHANG);
4964# endif
4965 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
4966 || (wait_pid == pid && WIFEXITED(status)))
4967 {
4968 wait_pid = pid;
4969 break;
4970 }
4971
4972 /* Handle any X events, e.g. serving the clipboard. */
4973 clip_update();
4974
4975 mch_delay(10L, TRUE);
4976 }
4977 }
4978# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979
4980 /*
4981 * Wait until our child has exited.
4982 * Ignore wait() returning pids of other children and returning
4983 * because of some signal like SIGWINCH.
4984 * Don't wait if wait_pid was already set above, indicating the
4985 * child already exited.
4986 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02004987 if (wait_pid != pid)
4988 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989
Bram Moolenaar624891f2010-10-13 16:22:09 +02004990# ifdef FEAT_GUI
4991 /* Close slave side of pty. Only do this after the child has
4992 * exited, otherwise the child may hang when it tries to write on
4993 * the pty. */
4994 if (pty_master_fd >= 0)
4995 close(pty_slave_fd);
4996# endif
4997
Bram Moolenaardf177f62005-02-22 08:39:57 +00004998 /* Make sure the child that writes to the external program is
4999 * dead. */
5000 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005001 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005002 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005003 wait4pid(wpid, NULL);
5004 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005005
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 /*
5007 * Set to raw mode right now, otherwise a CTRL-C after
5008 * catch_signals() will kill Vim.
5009 */
5010 if (tmode == TMODE_RAW)
5011 settmode(TMODE_RAW);
5012 did_settmode = TRUE;
5013 set_signals();
5014
5015 if (WIFEXITED(status))
5016 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005017 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005019 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 {
5021 if (retval == EXEC_FAILED)
5022 {
5023 MSG_PUTS(_("\nCannot execute shell "));
5024 msg_outtrans(p_sh);
5025 msg_putchar('\n');
5026 }
5027 else if (!(options & SHELL_SILENT))
5028 {
5029 MSG_PUTS(_("\nshell returned "));
5030 msg_outnum((long)retval);
5031 msg_putchar('\n');
5032 }
5033 }
5034 }
5035 else
5036 MSG_PUTS(_("\nCommand terminated\n"));
5037 }
5038 }
5039 vim_free(argv);
Bram Moolenaarea35ef62011-08-04 22:59:28 +02005040 vim_free(p_shcf_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
5042error:
5043 if (!did_settmode)
5044 if (tmode == TMODE_RAW)
5045 settmode(TMODE_RAW); /* set to raw mode */
5046# ifdef FEAT_TITLE
5047 resettitle();
5048# endif
5049 vim_free(newcmd);
5050
5051 return retval;
5052
5053#endif /* USE_SYSTEM */
5054}
5055
5056/*
5057 * Check for CTRL-C typed by reading all available characters.
5058 * In cooked mode we should get SIGINT, no need to check.
5059 */
5060 void
5061mch_breakcheck()
5062{
5063 if (curr_tmode == TMODE_RAW && RealWaitForChar(read_cmd_fd, 0L, NULL))
5064 fill_input_buf(FALSE);
5065}
5066
5067/*
5068 * Wait "msec" msec until a character is available from the keyboard or from
5069 * inbuf[]. msec == -1 will block forever.
5070 * When a GUI is being used, this will never get called -- webb
5071 */
5072 static int
5073WaitForChar(msec)
5074 long msec;
5075{
5076#ifdef FEAT_MOUSE_GPM
5077 int gpm_process_wanted;
5078#endif
5079#ifdef FEAT_XCLIPBOARD
5080 int rest;
5081#endif
5082 int avail;
5083
5084 if (input_available()) /* something in inbuf[] */
5085 return 1;
5086
5087#if defined(FEAT_MOUSE_DEC)
5088 /* May need to query the mouse position. */
5089 if (WantQueryMouse)
5090 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005091 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5093 }
5094#endif
5095
5096 /*
5097 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5098 * events. This is a bit complicated, because they might both be defined.
5099 */
5100#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5101# ifdef FEAT_XCLIPBOARD
5102 rest = 0;
5103 if (do_xterm_trace())
5104 rest = msec;
5105# endif
5106 do
5107 {
5108# ifdef FEAT_XCLIPBOARD
5109 if (rest != 0)
5110 {
5111 msec = XT_TRACE_DELAY;
5112 if (rest >= 0 && rest < XT_TRACE_DELAY)
5113 msec = rest;
5114 if (rest >= 0)
5115 rest -= msec;
5116 }
5117# endif
5118# ifdef FEAT_MOUSE_GPM
5119 gpm_process_wanted = 0;
5120 avail = RealWaitForChar(read_cmd_fd, msec, &gpm_process_wanted);
5121# else
5122 avail = RealWaitForChar(read_cmd_fd, msec, NULL);
5123# endif
5124 if (!avail)
5125 {
5126 if (input_available())
5127 return 1;
5128# ifdef FEAT_XCLIPBOARD
5129 if (rest == 0 || !do_xterm_trace())
5130# endif
5131 break;
5132 }
5133 }
5134 while (FALSE
5135# ifdef FEAT_MOUSE_GPM
5136 || (gpm_process_wanted && mch_gpm_process() == 0)
5137# endif
5138# ifdef FEAT_XCLIPBOARD
5139 || (!avail && rest != 0)
5140# endif
5141 );
5142
5143#else
5144 avail = RealWaitForChar(read_cmd_fd, msec, NULL);
5145#endif
5146 return avail;
5147}
5148
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01005149#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150/*
5151 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005152 * "msec" == 0 will check for characters once.
5153 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 * When a GUI is being used, this will not be used for input -- webb
5155 * Returns also, when a request from Sniff is waiting -- toni.
5156 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005157 * Or when a clientserver message is on the queue.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159#if defined(__BEOS__)
5160 int
5161#else
5162 static int
5163#endif
5164RealWaitForChar(fd, msec, check_for_gpm)
5165 int fd;
5166 long msec;
Bram Moolenaar78a15312009-05-15 19:33:18 +00005167 int *check_for_gpm UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168{
5169 int ret;
Bram Moolenaar67c53842010-05-22 18:28:27 +02005170#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005171 int nb_fd = netbeans_filedesc();
Bram Moolenaar67c53842010-05-22 18:28:27 +02005172#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005173#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 static int busy = FALSE;
5175
5176 /* May retry getting characters after an event was handled. */
5177# define MAY_LOOP
5178
5179# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
5180 /* Remember at what time we started, so that we know how much longer we
5181 * should wait after being interrupted. */
5182# define USE_START_TV
5183 struct timeval start_tv;
5184
5185 if (msec > 0 && (
5186# ifdef FEAT_XCLIPBOARD
5187 xterm_Shell != (Widget)0
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005188# if defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 ||
5190# endif
5191# endif
5192# ifdef USE_XSMP
5193 xsmp_icefd != -1
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005194# ifdef FEAT_MZSCHEME
5195 ||
5196# endif
5197# endif
5198# ifdef FEAT_MZSCHEME
5199 (mzthreads_allowed() && p_mzq > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200# endif
5201 ))
5202 gettimeofday(&start_tv, NULL);
5203# endif
5204
5205 /* Handle being called recursively. This may happen for the session
5206 * manager stuff, it may save the file, which does a breakcheck. */
5207 if (busy)
5208 return 0;
5209#endif
5210
5211#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005212 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213#endif
5214 {
5215#ifdef MAY_LOOP
5216 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005217# ifdef FEAT_MZSCHEME
5218 int mzquantum_used = FALSE;
5219# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220#endif
5221#ifndef HAVE_SELECT
Bram Moolenaar67c53842010-05-22 18:28:27 +02005222 struct pollfd fds[6];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 int nfd;
5224# ifdef FEAT_XCLIPBOARD
5225 int xterm_idx = -1;
5226# endif
5227# ifdef FEAT_MOUSE_GPM
5228 int gpm_idx = -1;
5229# endif
5230# ifdef USE_XSMP
5231 int xsmp_idx = -1;
5232# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005233# ifdef FEAT_NETBEANS_INTG
5234 int nb_idx = -1;
5235# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005236 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005238# ifdef FEAT_MZSCHEME
5239 mzvim_check_threads();
5240 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5241 {
5242 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
5243 mzquantum_used = TRUE;
5244 }
5245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 fds[0].fd = fd;
5247 fds[0].events = POLLIN;
5248 nfd = 1;
5249
5250# ifdef FEAT_SNIFF
5251# define SNIFF_IDX 1
5252 if (want_sniff_request)
5253 {
5254 fds[SNIFF_IDX].fd = fd_from_sniff;
5255 fds[SNIFF_IDX].events = POLLIN;
5256 nfd++;
5257 }
5258# endif
5259# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005260 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 if (xterm_Shell != (Widget)0)
5262 {
5263 xterm_idx = nfd;
5264 fds[nfd].fd = ConnectionNumber(xterm_dpy);
5265 fds[nfd].events = POLLIN;
5266 nfd++;
5267 }
5268# endif
5269# ifdef FEAT_MOUSE_GPM
5270 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5271 {
5272 gpm_idx = nfd;
5273 fds[nfd].fd = gpm_fd;
5274 fds[nfd].events = POLLIN;
5275 nfd++;
5276 }
5277# endif
5278# ifdef USE_XSMP
5279 if (xsmp_icefd != -1)
5280 {
5281 xsmp_idx = nfd;
5282 fds[nfd].fd = xsmp_icefd;
5283 fds[nfd].events = POLLIN;
5284 nfd++;
5285 }
5286# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005287#ifdef FEAT_NETBEANS_INTG
5288 if (nb_fd != -1)
5289 {
5290 nb_idx = nfd;
5291 fds[nfd].fd = nb_fd;
5292 fds[nfd].events = POLLIN;
5293 nfd++;
5294 }
5295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005297 ret = poll(fds, nfd, towait);
5298# ifdef FEAT_MZSCHEME
5299 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005300 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005301 finished = FALSE;
5302# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
5304# ifdef FEAT_SNIFF
5305 if (ret < 0)
5306 sniff_disconnect(1);
5307 else if (want_sniff_request)
5308 {
5309 if (fds[SNIFF_IDX].revents & POLLHUP)
5310 sniff_disconnect(1);
5311 if (fds[SNIFF_IDX].revents & POLLIN)
5312 sniff_request_waiting = 1;
5313 }
5314# endif
5315# ifdef FEAT_XCLIPBOARD
5316 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
5317 {
5318 xterm_update(); /* Maybe we should hand out clipboard */
5319 if (--ret == 0 && !input_available())
5320 /* Try again */
5321 finished = FALSE;
5322 }
5323# endif
5324# ifdef FEAT_MOUSE_GPM
5325 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
5326 {
5327 *check_for_gpm = 1;
5328 }
5329# endif
5330# ifdef USE_XSMP
5331 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
5332 {
5333 if (fds[xsmp_idx].revents & POLLIN)
5334 {
5335 busy = TRUE;
5336 xsmp_handle_requests();
5337 busy = FALSE;
5338 }
5339 else if (fds[xsmp_idx].revents & POLLHUP)
5340 {
5341 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005342 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 xsmp_close();
5344 }
5345 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005346 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
5348# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005349#ifdef FEAT_NETBEANS_INTG
5350 if (ret > 0 && nb_idx != -1 && fds[nb_idx].revents & POLLIN)
5351 {
5352 netbeans_read();
5353 --ret;
5354 }
5355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
5357
5358#else /* HAVE_SELECT */
5359
5360 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005361 struct timeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 fd_set rfds, efds;
5363 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005364 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005366# ifdef FEAT_MZSCHEME
5367 mzvim_check_threads();
5368 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5369 {
5370 towait = p_mzq; /* don't wait longer than 'mzquantum' */
5371 mzquantum_used = TRUE;
5372 }
5373# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374# ifdef __EMX__
5375 /* don't check for incoming chars if not in raw mode, because select()
5376 * always returns TRUE then (in some version of emx.dll) */
5377 if (curr_tmode != TMODE_RAW)
5378 return 0;
5379# endif
5380
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005381 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005383 tv.tv_sec = towait / 1000;
5384 tv.tv_usec = (towait % 1000) * (1000000/1000);
5385 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005387 else
5388 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389
5390 /*
5391 * Select on ready for reading and exceptional condition (end of file).
5392 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005393select_eintr:
5394 FD_ZERO(&rfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 FD_ZERO(&efds);
5396 FD_SET(fd, &rfds);
5397# if !defined(__QNX__) && !defined(__CYGWIN32__)
5398 /* For QNX select() always returns 1 if this is set. Why? */
5399 FD_SET(fd, &efds);
5400# endif
5401 maxfd = fd;
5402
5403# ifdef FEAT_SNIFF
5404 if (want_sniff_request)
5405 {
5406 FD_SET(fd_from_sniff, &rfds);
5407 FD_SET(fd_from_sniff, &efds);
5408 if (maxfd < fd_from_sniff)
5409 maxfd = fd_from_sniff;
5410 }
5411# endif
5412# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005413 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 if (xterm_Shell != (Widget)0)
5415 {
5416 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
5417 if (maxfd < ConnectionNumber(xterm_dpy))
5418 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005419
5420 /* An event may have already been read but not handled. In
5421 * particulary, XFlush may cause this. */
5422 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 }
5424# endif
5425# ifdef FEAT_MOUSE_GPM
5426 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5427 {
5428 FD_SET(gpm_fd, &rfds);
5429 FD_SET(gpm_fd, &efds);
5430 if (maxfd < gpm_fd)
5431 maxfd = gpm_fd;
5432 }
5433# endif
5434# ifdef USE_XSMP
5435 if (xsmp_icefd != -1)
5436 {
5437 FD_SET(xsmp_icefd, &rfds);
5438 FD_SET(xsmp_icefd, &efds);
5439 if (maxfd < xsmp_icefd)
5440 maxfd = xsmp_icefd;
5441 }
5442# endif
Bram Moolenaardd82d692012-08-15 17:26:57 +02005443# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar67c53842010-05-22 18:28:27 +02005444 if (nb_fd != -1)
5445 {
5446 FD_SET(nb_fd, &rfds);
5447 if (maxfd < nb_fd)
5448 maxfd = nb_fd;
5449 }
Bram Moolenaardd82d692012-08-15 17:26:57 +02005450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005452 ret = select(maxfd + 1, &rfds, NULL, &efds, tvp);
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005453# ifdef EINTR
5454 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005455 {
5456 /* Check whether window has been resized, EINTR may be caused by
5457 * SIGWINCH. */
5458 if (do_resize)
5459 handle_resize();
5460
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005461 /* Interrupted by a signal, need to try again. We ignore msec
5462 * here, because we do want to check even after a timeout if
5463 * characters are available. Needed for reading output of an
5464 * external command after the process has finished. */
5465 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005466 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005467# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00005468# ifdef __TANDEM
5469 if (ret == -1 && errno == ENOTSUP)
5470 {
5471 FD_ZERO(&rfds);
5472 FD_ZERO(&efds);
5473 ret = 0;
5474 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005475# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005476# ifdef FEAT_MZSCHEME
5477 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005478 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005479 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480# endif
5481
5482# ifdef FEAT_SNIFF
5483 if (ret < 0 )
5484 sniff_disconnect(1);
5485 else if (ret > 0 && want_sniff_request)
5486 {
5487 if (FD_ISSET(fd_from_sniff, &efds))
5488 sniff_disconnect(1);
5489 if (FD_ISSET(fd_from_sniff, &rfds))
5490 sniff_request_waiting = 1;
5491 }
5492# endif
5493# ifdef FEAT_XCLIPBOARD
5494 if (ret > 0 && xterm_Shell != (Widget)0
5495 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
5496 {
5497 xterm_update(); /* Maybe we should hand out clipboard */
5498 /* continue looping when we only got the X event and the input
5499 * buffer is empty */
5500 if (--ret == 0 && !input_available())
5501 {
5502 /* Try again */
5503 finished = FALSE;
5504 }
5505 }
5506# endif
5507# ifdef FEAT_MOUSE_GPM
5508 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
5509 {
5510 if (FD_ISSET(gpm_fd, &efds))
5511 gpm_close();
5512 else if (FD_ISSET(gpm_fd, &rfds))
5513 *check_for_gpm = 1;
5514 }
5515# endif
5516# ifdef USE_XSMP
5517 if (ret > 0 && xsmp_icefd != -1)
5518 {
5519 if (FD_ISSET(xsmp_icefd, &efds))
5520 {
5521 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005522 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 xsmp_close();
5524 if (--ret == 0)
5525 finished = FALSE; /* keep going if event was only one */
5526 }
5527 else if (FD_ISSET(xsmp_icefd, &rfds))
5528 {
5529 busy = TRUE;
5530 xsmp_handle_requests();
5531 busy = FALSE;
5532 if (--ret == 0)
5533 finished = FALSE; /* keep going if event was only one */
5534 }
5535 }
5536# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005537#ifdef FEAT_NETBEANS_INTG
5538 if (ret > 0 && nb_fd != -1 && FD_ISSET(nb_fd, &rfds))
5539 {
5540 netbeans_read();
5541 --ret;
5542 }
5543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544
5545#endif /* HAVE_SELECT */
5546
5547#ifdef MAY_LOOP
5548 if (finished || msec == 0)
5549 break;
5550
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005551# ifdef FEAT_CLIENTSERVER
5552 if (server_waiting())
5553 break;
5554# endif
5555
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 /* We're going to loop around again, find out for how long */
5557 if (msec > 0)
5558 {
5559# ifdef USE_START_TV
5560 struct timeval mtv;
5561
5562 /* Compute remaining wait time. */
5563 gettimeofday(&mtv, NULL);
5564 msec -= (mtv.tv_sec - start_tv.tv_sec) * 1000L
5565 + (mtv.tv_usec - start_tv.tv_usec) / 1000L;
5566# else
5567 /* Guess we got interrupted halfway. */
5568 msec = msec / 2;
5569# endif
5570 if (msec <= 0)
5571 break; /* waited long enough */
5572 }
5573#endif
5574 }
5575
5576 return (ret > 0);
5577}
5578
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580/*
Bram Moolenaar02743632005-07-25 20:42:36 +00005581 * Expand a path into all matching files and/or directories. Handles "*",
5582 * "?", "[a-z]", "**", etc.
5583 * "path" has backslashes before chars that are not to be expanded.
5584 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 */
5586 int
5587mch_expandpath(gap, path, flags)
5588 garray_T *gap;
5589 char_u *path;
5590 int flags; /* EW_* flags */
5591{
Bram Moolenaar02743632005-07-25 20:42:36 +00005592 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593}
5594#endif
5595
5596/*
5597 * mch_expand_wildcards() - this code does wild-card pattern matching using
5598 * the shell
5599 *
5600 * return OK for success, FAIL for error (you may lose some memory) and put
5601 * an error message in *file.
5602 *
5603 * num_pat is number of input patterns
5604 * pat is array of pointers to input patterns
5605 * num_file is pointer to number of matched file names
5606 * file is pointer to array of pointers to matched file names
5607 */
5608
5609#ifndef SEEK_SET
5610# define SEEK_SET 0
5611#endif
5612#ifndef SEEK_END
5613# define SEEK_END 2
5614#endif
5615
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005616#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00005617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 int
5619mch_expand_wildcards(num_pat, pat, num_file, file, flags)
5620 int num_pat;
5621 char_u **pat;
5622 int *num_file;
5623 char_u ***file;
5624 int flags; /* EW_* flags */
5625{
5626 int i;
5627 size_t len;
5628 char_u *p;
5629 int dir;
5630#ifdef __EMX__
Bram Moolenaarc7247912008-01-13 12:54:11 +00005631 /*
5632 * This is the OS/2 implementation.
5633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634# define EXPL_ALLOC_INC 16
5635 char_u **expl_files;
5636 size_t files_alloced, files_free;
5637 char_u *buf;
5638 int has_wildcard;
5639
5640 *num_file = 0; /* default: no files found */
5641 files_alloced = EXPL_ALLOC_INC; /* how much space is allocated */
5642 files_free = EXPL_ALLOC_INC; /* how much space is not used */
5643 *file = (char_u **)alloc(sizeof(char_u **) * files_alloced);
5644 if (*file == NULL)
5645 return FAIL;
5646
5647 for (; num_pat > 0; num_pat--, pat++)
5648 {
5649 expl_files = NULL;
5650 if (vim_strchr(*pat, '$') || vim_strchr(*pat, '~'))
5651 /* expand environment var or home dir */
5652 buf = expand_env_save(*pat);
5653 else
5654 buf = vim_strsave(*pat);
5655 expl_files = NULL;
Bram Moolenaard8b02732005-01-14 21:48:43 +00005656 has_wildcard = mch_has_exp_wildcard(buf); /* (still) wildcards? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 if (has_wildcard) /* yes, so expand them */
5658 expl_files = (char_u **)_fnexplode(buf);
5659
5660 /*
5661 * return value of buf if no wildcards left,
5662 * OR if no match AND EW_NOTFOUND is set.
5663 */
5664 if ((!has_wildcard && ((flags & EW_NOTFOUND) || mch_getperm(buf) >= 0))
5665 || (expl_files == NULL && (flags & EW_NOTFOUND)))
5666 { /* simply save the current contents of *buf */
5667 expl_files = (char_u **)alloc(sizeof(char_u **) * 2);
5668 if (expl_files != NULL)
5669 {
5670 expl_files[0] = vim_strsave(buf);
5671 expl_files[1] = NULL;
5672 }
5673 }
5674 vim_free(buf);
5675
5676 /*
5677 * Count number of names resulting from expansion,
5678 * At the same time add a backslash to the end of names that happen to
5679 * be directories, and replace slashes with backslashes.
5680 */
5681 if (expl_files)
5682 {
5683 for (i = 0; (p = expl_files[i]) != NULL; i++)
5684 {
5685 dir = mch_isdir(p);
5686 /* If we don't want dirs and this is one, skip it */
5687 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
5688 continue;
5689
Bram Moolenaara2031822006-03-07 22:29:51 +00005690 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01005691 if (!dir && (flags & EW_EXEC)
5692 && !mch_can_exe(p, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00005693 continue;
5694
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 if (--files_free == 0)
5696 {
5697 /* need more room in table of pointers */
5698 files_alloced += EXPL_ALLOC_INC;
5699 *file = (char_u **)vim_realloc(*file,
5700 sizeof(char_u **) * files_alloced);
5701 if (*file == NULL)
5702 {
5703 EMSG(_(e_outofmem));
5704 *num_file = 0;
5705 return FAIL;
5706 }
5707 files_free = EXPL_ALLOC_INC;
5708 }
5709 slash_adjust(p);
5710 if (dir)
5711 {
5712 /* For a directory we add a '/', unless it's already
5713 * there. */
5714 len = STRLEN(p);
5715 if (((*file)[*num_file] = alloc(len + 2)) != NULL)
5716 {
5717 STRCPY((*file)[*num_file], p);
Bram Moolenaar654b5b52006-06-22 17:47:10 +00005718 if (!after_pathsep((*file)[*num_file],
5719 (*file)[*num_file] + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 (*file)[*num_file][len] = psepc;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005722 (*file)[*num_file][len + 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
5724 }
5725 }
5726 else
5727 {
5728 (*file)[*num_file] = vim_strsave(p);
5729 }
5730
5731 /*
5732 * Error message already given by either alloc or vim_strsave.
5733 * Should return FAIL, but returning OK works also.
5734 */
5735 if ((*file)[*num_file] == NULL)
5736 break;
5737 (*num_file)++;
5738 }
5739 _fnexplodefree((char **)expl_files);
5740 }
5741 }
5742 return OK;
5743
5744#else /* __EMX__ */
Bram Moolenaarc7247912008-01-13 12:54:11 +00005745 /*
5746 * This is the non-OS/2 implementation (really Unix).
5747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 int j;
5749 char_u *tempname;
5750 char_u *command;
5751 FILE *fd;
5752 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005753#define STYLE_ECHO 0 /* use "echo", the default */
5754#define STYLE_GLOB 1 /* use "glob", for csh */
5755#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
5756#define STYLE_PRINT 3 /* use "print -N", for zsh */
5757#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
5758 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 int shell_style = STYLE_ECHO;
5760 int check_spaces;
5761 static int did_find_nul = FALSE;
5762 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005763 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005764 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
5766 *num_file = 0; /* default: no files found */
5767 *file = NULL;
5768
5769 /*
5770 * If there are no wildcards, just copy the names to allocated memory.
5771 * Saves a lot of time, because we don't have to start a new shell.
5772 */
5773 if (!have_wildcard(num_pat, pat))
5774 return save_patterns(num_pat, pat, num_file, file);
5775
Bram Moolenaar0e634da2005-07-20 21:57:28 +00005776# ifdef HAVE_SANDBOX
5777 /* Don't allow any shell command in the sandbox. */
5778 if (sandbox != 0 && check_secure())
5779 return FAIL;
5780# endif
5781
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 /*
5783 * Don't allow the use of backticks in secure and restricted mode.
5784 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00005785 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 for (i = 0; i < num_pat; ++i)
5787 if (vim_strchr(pat[i], '`') != NULL
5788 && (check_restricted() || check_secure()))
5789 return FAIL;
5790
5791 /*
5792 * get a name for the temp file
5793 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005794 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 {
5796 EMSG(_(e_notmp));
5797 return FAIL;
5798 }
5799
5800 /*
5801 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00005802 * file.
5803 * STYLE_BT: NL separated
5804 * If expanding `cmd` execute it directly.
5805 * STYLE_GLOB: NUL separated
5806 * If we use *csh, "glob" will work better than "echo".
5807 * STYLE_PRINT: NL or NUL separated
5808 * If we use *zsh, "print -N" will work better than "glob".
5809 * STYLE_VIMGLOB: NL separated
5810 * If we use *sh*, we define "vimglob()".
5811 * STYLE_ECHO: space separated.
5812 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 */
5814 if (num_pat == 1 && *pat[0] == '`'
5815 && (len = STRLEN(pat[0])) > 2
5816 && *(pat[0] + len - 1) == '`')
5817 shell_style = STYLE_BT;
5818 else if ((len = STRLEN(p_sh)) >= 3)
5819 {
5820 if (STRCMP(p_sh + len - 3, "csh") == 0)
5821 shell_style = STYLE_GLOB;
5822 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
5823 shell_style = STYLE_PRINT;
5824 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00005825 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
5826 "sh") != NULL)
5827 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828
Bram Moolenaarc7247912008-01-13 12:54:11 +00005829 /* Compute the length of the command. We need 2 extra bytes: for the
5830 * optional '&' and for the NUL.
5831 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00005833 if (shell_style == STYLE_VIMGLOB)
5834 len += STRLEN(sh_vimglob_func);
5835
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005836 for (i = 0; i < num_pat; ++i)
5837 {
5838 /* Count the length of the patterns in the same way as they are put in
5839 * "command" below. */
5840#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005842#else
5843 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00005844 for (j = 0; pat[i][j] != NUL; ++j)
5845 {
5846 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
5847 ++len; /* may add a backslash */
5848 ++len;
5849 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00005850#endif
5851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 command = alloc(len);
5853 if (command == NULL)
5854 {
5855 /* out of memory */
5856 vim_free(tempname);
5857 return FAIL;
5858 }
5859
5860 /*
5861 * Build the shell command:
5862 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
5863 * recognizes this).
5864 * - Add the shell command to print the expanded names.
5865 * - Add the temp file name.
5866 * - Add the file name patterns.
5867 */
5868 if (shell_style == STYLE_BT)
5869 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00005870 /* change `command; command& ` to (command; command ) */
5871 STRCPY(command, "(");
5872 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005874 *p-- = ')'; /* remove last backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 while (p > command && vim_iswhite(*p))
5876 --p;
5877 if (*p == '&') /* remove trailing '&' */
5878 {
5879 ampersent = TRUE;
5880 *p = ' ';
5881 }
5882 STRCAT(command, ">");
5883 }
5884 else
5885 {
5886 if (flags & EW_NOTFOUND)
5887 STRCPY(command, "set nonomatch; ");
5888 else
5889 STRCPY(command, "unset nonomatch; ");
5890 if (shell_style == STYLE_GLOB)
5891 STRCAT(command, "glob >");
5892 else if (shell_style == STYLE_PRINT)
5893 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00005894 else if (shell_style == STYLE_VIMGLOB)
5895 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 else
5897 STRCAT(command, "echo >");
5898 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00005899
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00005901
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 if (shell_style != STYLE_BT)
5903 for (i = 0; i < num_pat; ++i)
5904 {
5905 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00005906 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005907 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908#ifdef USE_SYSTEM
5909 STRCAT(command, " \"");
5910 STRCAT(command, pat[i]);
5911 STRCAT(command, "\"");
5912#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00005913 int intick = FALSE;
5914
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 p = command + STRLEN(command);
5916 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00005917 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005918 {
5919 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00005920 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005921 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
5922 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005923 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00005924 * backslash inside backticks, before a special character
5925 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005926 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00005927 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
5928 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005929 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00005930 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00005931 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02005932 else if (!intick
5933 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
5934 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00005935 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02005936 * when inside ``. And not for $var when EW_KEEPDOLLAR is
5937 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00005938 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00005939
5940 /* Copy one character. */
5941 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00005942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 *p = NUL;
5944#endif
5945 }
5946 if (flags & EW_SILENT)
5947 show_shell_mess = FALSE;
5948 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00005949 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950
5951 /*
5952 * Using zsh -G: If a pattern has no matches, it is just deleted from
5953 * the argument list, otherwise zsh gives an error message and doesn't
5954 * expand any other pattern.
5955 */
5956 if (shell_style == STYLE_PRINT)
5957 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
5958
5959 /*
5960 * If we use -f then shell variables set in .cshrc won't get expanded.
5961 * vi can do it, so we will too, but it is only necessary if there is a "$"
5962 * in one of the patterns, otherwise we can still use the fast option.
5963 */
5964 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
5965 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
5966
5967 /*
5968 * execute the shell command
5969 */
5970 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
5971
5972 /* When running in the background, give it some time to create the temp
5973 * file, but don't wait for it to finish. */
5974 if (ampersent)
5975 mch_delay(10L, TRUE);
5976
5977 extra_shell_arg = NULL; /* cleanup */
5978 show_shell_mess = TRUE;
5979 vim_free(command);
5980
Bram Moolenaarc7247912008-01-13 12:54:11 +00005981 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 {
5983 mch_remove(tempname);
5984 vim_free(tempname);
5985 /*
5986 * With interactive completion, the error message is not printed.
5987 * However with USE_SYSTEM, I don't know how to turn off error messages
5988 * from the shell, so screen may still get messed up -- webb.
5989 */
5990#ifndef USE_SYSTEM
5991 if (!(flags & EW_SILENT))
5992#endif
5993 {
5994 redraw_later_clear(); /* probably messed up screen */
5995 msg_putchar('\n'); /* clear bottom line quickly */
5996 cmdline_row = Rows - 1; /* continue on last line */
5997#ifdef USE_SYSTEM
5998 if (!(flags & EW_SILENT))
5999#endif
6000 {
6001 MSG(_(e_wildexpand));
6002 msg_start(); /* don't overwrite this message */
6003 }
6004 }
6005 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6006 * EW_NOTFOUND is given */
6007 if (shell_style == STYLE_BT)
6008 return FAIL;
6009 goto notfound;
6010 }
6011
6012 /*
6013 * read the names from the file into memory
6014 */
6015 fd = fopen((char *)tempname, READBIN);
6016 if (fd == NULL)
6017 {
6018 /* Something went wrong, perhaps a file name with a special char. */
6019 if (!(flags & EW_SILENT))
6020 {
6021 MSG(_(e_wildexpand));
6022 msg_start(); /* don't overwrite this message */
6023 }
6024 vim_free(tempname);
6025 goto notfound;
6026 }
6027 fseek(fd, 0L, SEEK_END);
6028 len = ftell(fd); /* get size of temp file */
6029 fseek(fd, 0L, SEEK_SET);
6030 buffer = alloc(len + 1);
6031 if (buffer == NULL)
6032 {
6033 /* out of memory */
6034 mch_remove(tempname);
6035 vim_free(tempname);
6036 fclose(fd);
6037 return FAIL;
6038 }
6039 i = fread((char *)buffer, 1, len, fd);
6040 fclose(fd);
6041 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006042 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 {
6044 /* unexpected read error */
6045 EMSG2(_(e_notread), tempname);
6046 vim_free(tempname);
6047 vim_free(buffer);
6048 return FAIL;
6049 }
6050 vim_free(tempname);
6051
Bram Moolenaarc7247912008-01-13 12:54:11 +00006052# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6054 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006055 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6057 *p++ = buffer[i];
6058 len = p - buffer;
6059# endif
6060
6061
6062 /* file names are separated with Space */
6063 if (shell_style == STYLE_ECHO)
6064 {
6065 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6066 p = buffer;
6067 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6068 {
6069 while (*p != ' ' && *p != '\n')
6070 ++p;
6071 p = skipwhite(p); /* skip to next entry */
6072 }
6073 }
6074 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006075 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 {
6077 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6078 p = buffer;
6079 for (i = 0; *p != NUL; ++i) /* count number of entries */
6080 {
6081 while (*p != '\n' && *p != NUL)
6082 ++p;
6083 if (*p != NUL)
6084 ++p;
6085 p = skipwhite(p); /* skip leading white space */
6086 }
6087 }
6088 /* file names are separated with NUL */
6089 else
6090 {
6091 /*
6092 * Some versions of zsh use spaces instead of NULs to separate
6093 * results. Only do this when there is no NUL before the end of the
6094 * buffer, otherwise we would never be able to use file names with
6095 * embedded spaces when zsh does use NULs.
6096 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6097 * don't check for spaces again.
6098 */
6099 check_spaces = FALSE;
6100 if (shell_style == STYLE_PRINT && !did_find_nul)
6101 {
6102 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006103 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006104 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 did_find_nul = TRUE;
6106 else
6107 check_spaces = TRUE;
6108 }
6109
6110 /*
6111 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6112 * already is one, for STYLE_GLOB it needs to be added.
6113 */
6114 if (len && buffer[len - 1] == NUL)
6115 --len;
6116 else
6117 buffer[len] = NUL;
6118 i = 0;
6119 for (p = buffer; p < buffer + len; ++p)
6120 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6121 {
6122 ++i;
6123 *p = NUL;
6124 }
6125 if (len)
6126 ++i; /* count last entry */
6127 }
6128 if (i == 0)
6129 {
6130 /*
6131 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6132 * /bin/sh will happily expand it to nothing rather than returning an
6133 * error; and hey, it's good to check anyway -- webb.
6134 */
6135 vim_free(buffer);
6136 goto notfound;
6137 }
6138 *num_file = i;
6139 *file = (char_u **)alloc(sizeof(char_u *) * i);
6140 if (*file == NULL)
6141 {
6142 /* out of memory */
6143 vim_free(buffer);
6144 return FAIL;
6145 }
6146
6147 /*
6148 * Isolate the individual file names.
6149 */
6150 p = buffer;
6151 for (i = 0; i < *num_file; ++i)
6152 {
6153 (*file)[i] = p;
6154 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006155 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6156 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006158 while (!(shell_style == STYLE_ECHO && *p == ' ')
6159 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 ++p;
6161 if (p == buffer + len) /* last entry */
6162 *p = NUL;
6163 else
6164 {
6165 *p++ = NUL;
6166 p = skipwhite(p); /* skip to next entry */
6167 }
6168 }
6169 else /* NUL separates */
6170 {
6171 while (*p && p < buffer + len) /* skip entry */
6172 ++p;
6173 ++p; /* skip NUL */
6174 }
6175 }
6176
6177 /*
6178 * Move the file names to allocated memory.
6179 */
6180 for (j = 0, i = 0; i < *num_file; ++i)
6181 {
6182 /* Require the files to exist. Helps when using /bin/sh */
6183 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6184 continue;
6185
6186 /* check if this entry should be included */
6187 dir = (mch_isdir((*file)[i]));
6188 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6189 continue;
6190
Bram Moolenaara2031822006-03-07 22:29:51 +00006191 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006192 if (!dir && (flags & EW_EXEC)
6193 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006194 continue;
6195
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6197 if (p)
6198 {
6199 STRCPY(p, (*file)[i]);
6200 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006201 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 (*file)[j++] = p;
6203 }
6204 }
6205 vim_free(buffer);
6206 *num_file = j;
6207
6208 if (*num_file == 0) /* rejected all entries */
6209 {
6210 vim_free(*file);
6211 *file = NULL;
6212 goto notfound;
6213 }
6214
6215 return OK;
6216
6217notfound:
6218 if (flags & EW_NOTFOUND)
6219 return save_patterns(num_pat, pat, num_file, file);
6220 return FAIL;
6221
6222#endif /* __EMX__ */
6223}
6224
6225#endif /* VMS */
6226
6227#ifndef __EMX__
6228 static int
6229save_patterns(num_pat, pat, num_file, file)
6230 int num_pat;
6231 char_u **pat;
6232 int *num_file;
6233 char_u ***file;
6234{
6235 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006236 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237
6238 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6239 if (*file == NULL)
6240 return FAIL;
6241 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006242 {
6243 s = vim_strsave(pat[i]);
6244 if (s != NULL)
6245 /* Be compatible with expand_filename(): halve the number of
6246 * backslashes. */
6247 backslash_halve(s);
6248 (*file)[i] = s;
6249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 *num_file = num_pat;
6251 return OK;
6252}
6253#endif
6254
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255/*
6256 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6257 * expand.
6258 */
6259 int
6260mch_has_exp_wildcard(p)
6261 char_u *p;
6262{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006263 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 if (*p == '\\' && p[1] != NUL)
6266 ++p;
6267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 if (vim_strchr((char_u *)
6269#ifdef VMS
6270 "*?%"
6271#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273#endif
6274 , *p) != NULL)
6275 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 }
6277 return FALSE;
6278}
6279
6280/*
6281 * Return TRUE if the string "p" contains a wildcard.
6282 * Don't recognize '~' at the end as a wildcard.
6283 */
6284 int
6285mch_has_wildcard(p)
6286 char_u *p;
6287{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006288 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 if (*p == '\\' && p[1] != NUL)
6291 ++p;
6292 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 if (vim_strchr((char_u *)
6294#ifdef VMS
6295 "*?%$"
6296#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298#endif
6299 , *p) != NULL
6300 || (*p == '~' && p[1] != NUL))
6301 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 }
6303 return FALSE;
6304}
6305
6306#ifndef __EMX__
6307 static int
6308have_wildcard(num, file)
6309 int num;
6310 char_u **file;
6311{
6312 int i;
6313
6314 for (i = 0; i < num; i++)
6315 if (mch_has_wildcard(file[i]))
6316 return 1;
6317 return 0;
6318}
6319
6320 static int
6321have_dollars(num, file)
6322 int num;
6323 char_u **file;
6324{
6325 int i;
6326
6327 for (i = 0; i < num; i++)
6328 if (vim_strchr(file[i], '$') != NULL)
6329 return TRUE;
6330 return FALSE;
6331}
6332#endif /* ifndef __EMX__ */
6333
6334#ifndef HAVE_RENAME
6335/*
6336 * Scaled-down version of rename(), which is missing in Xenix.
6337 * This version can only move regular files and will fail if the
6338 * destination exists.
6339 */
6340 int
6341mch_rename(src, dest)
6342 const char *src, *dest;
6343{
6344 struct stat st;
6345
6346 if (stat(dest, &st) >= 0) /* fail if destination exists */
6347 return -1;
6348 if (link(src, dest) != 0) /* link file to new name */
6349 return -1;
6350 if (mch_remove(src) == 0) /* delete link to old name */
6351 return 0;
6352 return -1;
6353}
6354#endif /* !HAVE_RENAME */
6355
6356#ifdef FEAT_MOUSE_GPM
6357/*
6358 * Initializes connection with gpm (if it isn't already opened)
6359 * Return 1 if succeeded (or connection already opened), 0 if failed
6360 */
6361 static int
6362gpm_open()
6363{
6364 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
6365
6366 if (!gpm_flag)
6367 {
6368 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
6369 gpm_connect.defaultMask = ~GPM_HARD;
6370 /* Default handling for mouse move*/
6371 gpm_connect.minMod = 0; /* Handle any modifier keys */
6372 gpm_connect.maxMod = 0xffff;
6373 if (Gpm_Open(&gpm_connect, 0) > 0)
6374 {
6375 /* gpm library tries to handling TSTP causes
6376 * problems. Anyways, we close connection to Gpm whenever
6377 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006378 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006380# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 return 1; /* succeed */
6384 }
6385 if (gpm_fd == -2)
6386 Gpm_Close(); /* We don't want to talk to xterm via gpm */
6387 return 0;
6388 }
6389 return 1; /* already open */
6390}
6391
6392/*
6393 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 */
6395 static void
6396gpm_close()
6397{
6398 if (gpm_flag && gpm_fd >= 0) /* if Open */
6399 Gpm_Close();
6400}
6401
6402/* Reads gpm event and adds special keys to input buf. Returns length of
6403 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02006404 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 */
6406 static int
6407mch_gpm_process()
6408{
6409 int button;
6410 static Gpm_Event gpm_event;
6411 char_u string[6];
6412 int_u vim_modifiers;
6413 int row,col;
6414 unsigned char buttons_mask;
6415 unsigned char gpm_modifiers;
6416 static unsigned char old_buttons = 0;
6417
6418 Gpm_GetEvent(&gpm_event);
6419
6420#ifdef FEAT_GUI
6421 /* Don't put events in the input queue now. */
6422 if (hold_gui_events)
6423 return 0;
6424#endif
6425
6426 row = gpm_event.y - 1;
6427 col = gpm_event.x - 1;
6428
6429 string[0] = ESC; /* Our termcode */
6430 string[1] = 'M';
6431 string[2] = 'G';
6432 switch (GPM_BARE_EVENTS(gpm_event.type))
6433 {
6434 case GPM_DRAG:
6435 string[3] = MOUSE_DRAG;
6436 break;
6437 case GPM_DOWN:
6438 buttons_mask = gpm_event.buttons & ~old_buttons;
6439 old_buttons = gpm_event.buttons;
6440 switch (buttons_mask)
6441 {
6442 case GPM_B_LEFT:
6443 button = MOUSE_LEFT;
6444 break;
6445 case GPM_B_MIDDLE:
6446 button = MOUSE_MIDDLE;
6447 break;
6448 case GPM_B_RIGHT:
6449 button = MOUSE_RIGHT;
6450 break;
6451 default:
6452 return 0;
6453 /*Don't know what to do. Can more than one button be
6454 * reported in one event? */
6455 }
6456 string[3] = (char_u)(button | 0x20);
6457 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
6458 break;
6459 case GPM_UP:
6460 string[3] = MOUSE_RELEASE;
6461 old_buttons &= ~gpm_event.buttons;
6462 break;
6463 default:
6464 return 0;
6465 }
6466 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
6467 gpm_modifiers = gpm_event.modifiers;
6468 vim_modifiers = 0x0;
6469 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
6470 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
6471 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
6472 */
6473 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
6474 vim_modifiers |= MOUSE_SHIFT;
6475
6476 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
6477 vim_modifiers |= MOUSE_CTRL;
6478 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
6479 vim_modifiers |= MOUSE_ALT;
6480 string[3] |= vim_modifiers;
6481 string[4] = (char_u)(col + ' ' + 1);
6482 string[5] = (char_u)(row + ' ' + 1);
6483 add_to_input_buf(string, 6);
6484 return 6;
6485}
6486#endif /* FEAT_MOUSE_GPM */
6487
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006488#ifdef FEAT_SYSMOUSE
6489/*
6490 * Initialize connection with sysmouse.
6491 * Let virtual console inform us with SIGUSR2 for pending sysmouse
6492 * output, any sysmouse output than will be processed via sig_sysmouse().
6493 * Return OK if succeeded, FAIL if failed.
6494 */
6495 static int
6496sysmouse_open()
6497{
6498 struct mouse_info mouse;
6499
6500 mouse.operation = MOUSE_MODE;
6501 mouse.u.mode.mode = 0;
6502 mouse.u.mode.signal = SIGUSR2;
6503 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
6504 {
6505 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
6506 mouse.operation = MOUSE_SHOW;
6507 ioctl(1, CONS_MOUSECTL, &mouse);
6508 return OK;
6509 }
6510 return FAIL;
6511}
6512
6513/*
6514 * Stop processing SIGUSR2 signals, and also make sure that
6515 * virtual console do not send us any sysmouse related signal.
6516 */
6517 static void
6518sysmouse_close()
6519{
6520 struct mouse_info mouse;
6521
6522 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
6523 mouse.operation = MOUSE_MODE;
6524 mouse.u.mode.mode = 0;
6525 mouse.u.mode.signal = 0;
6526 ioctl(1, CONS_MOUSECTL, &mouse);
6527}
6528
6529/*
6530 * Gets info from sysmouse and adds special keys to input buf.
6531 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006532 static RETSIGTYPE
6533sig_sysmouse SIGDEFARG(sigarg)
6534{
6535 struct mouse_info mouse;
6536 struct video_info video;
6537 char_u string[6];
6538 int row, col;
6539 int button;
6540 int buttons;
6541 static int oldbuttons = 0;
6542
6543#ifdef FEAT_GUI
6544 /* Don't put events in the input queue now. */
6545 if (hold_gui_events)
6546 return;
6547#endif
6548
6549 mouse.operation = MOUSE_GETINFO;
6550 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
6551 && ioctl(1, FBIO_MODEINFO, &video) != -1
6552 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
6553 && video.vi_cheight > 0 && video.vi_cwidth > 0)
6554 {
6555 row = mouse.u.data.y / video.vi_cheight;
6556 col = mouse.u.data.x / video.vi_cwidth;
6557 buttons = mouse.u.data.buttons;
6558 string[0] = ESC; /* Our termcode */
6559 string[1] = 'M';
6560 string[2] = 'S';
6561 if (oldbuttons == buttons && buttons != 0)
6562 {
6563 button = MOUSE_DRAG;
6564 }
6565 else
6566 {
6567 switch (buttons)
6568 {
6569 case 0:
6570 button = MOUSE_RELEASE;
6571 break;
6572 case 1:
6573 button = MOUSE_LEFT;
6574 break;
6575 case 2:
6576 button = MOUSE_MIDDLE;
6577 break;
6578 case 4:
6579 button = MOUSE_RIGHT;
6580 break;
6581 default:
6582 return;
6583 }
6584 oldbuttons = buttons;
6585 }
6586 string[3] = (char_u)(button);
6587 string[4] = (char_u)(col + ' ' + 1);
6588 string[5] = (char_u)(row + ' ' + 1);
6589 add_to_input_buf(string, 6);
6590 }
6591 return;
6592}
6593#endif /* FEAT_SYSMOUSE */
6594
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595#if defined(FEAT_LIBCALL) || defined(PROTO)
6596typedef char_u * (*STRPROCSTR)__ARGS((char_u *));
6597typedef char_u * (*INTPROCSTR)__ARGS((int));
6598typedef int (*STRPROCINT)__ARGS((char_u *));
6599typedef int (*INTPROCINT)__ARGS((int));
6600
6601/*
6602 * Call a DLL routine which takes either a string or int param
6603 * and returns an allocated string.
6604 */
6605 int
6606mch_libcall(libname, funcname, argstring, argint, string_result, number_result)
6607 char_u *libname;
6608 char_u *funcname;
6609 char_u *argstring; /* NULL when using a argint */
6610 int argint;
6611 char_u **string_result;/* NULL when using number_result */
6612 int *number_result;
6613{
6614# if defined(USE_DLOPEN)
6615 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006616 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617# else
6618 shl_t hinstLib;
6619# endif
6620 STRPROCSTR ProcAdd;
6621 INTPROCSTR ProcAddI;
6622 char_u *retval_str = NULL;
6623 int retval_int = 0;
6624 int success = FALSE;
6625
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006626 /*
6627 * Get a handle to the DLL module.
6628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006630 /* First clear any error, it's not cleared by the dlopen() call. */
6631 (void)dlerror();
6632
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 hinstLib = dlopen((char *)libname, RTLD_LAZY
6634# ifdef RTLD_LOCAL
6635 | RTLD_LOCAL
6636# endif
6637 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006638 if (hinstLib == NULL)
6639 {
6640 /* "dlerr" must be used before dlclose() */
6641 dlerr = (char *)dlerror();
6642 if (dlerr != NULL)
6643 EMSG2(_("dlerror = \"%s\""), dlerr);
6644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645# else
6646 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
6647# endif
6648
6649 /* If the handle is valid, try to get the function address. */
6650 if (hinstLib != NULL)
6651 {
6652# ifdef HAVE_SETJMP_H
6653 /*
6654 * Catch a crash when calling the library function. For example when
6655 * using a number where a string pointer is expected.
6656 */
6657 mch_startjmp();
6658 if (SETJMP(lc_jump_env) != 0)
6659 {
6660 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00006661# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006662 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00006663# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 mch_didjmp();
6665 }
6666 else
6667# endif
6668 {
6669 retval_str = NULL;
6670 retval_int = 0;
6671
6672 if (argstring != NULL)
6673 {
6674# if defined(USE_DLOPEN)
6675 ProcAdd = (STRPROCSTR)dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006676 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677# else
6678 if (shl_findsym(&hinstLib, (const char *)funcname,
6679 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
6680 ProcAdd = NULL;
6681# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006682 if ((success = (ProcAdd != NULL
6683# if defined(USE_DLOPEN)
6684 && dlerr == NULL
6685# endif
6686 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 {
6688 if (string_result == NULL)
6689 retval_int = ((STRPROCINT)ProcAdd)(argstring);
6690 else
6691 retval_str = (ProcAdd)(argstring);
6692 }
6693 }
6694 else
6695 {
6696# if defined(USE_DLOPEN)
6697 ProcAddI = (INTPROCSTR)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 *)&ProcAddI) < 0)
6702 ProcAddI = NULL;
6703# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006704 if ((success = (ProcAddI != 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 = ((INTPROCINT)ProcAddI)(argint);
6712 else
6713 retval_str = (ProcAddI)(argint);
6714 }
6715 }
6716
6717 /* Save the string before we free the library. */
6718 /* Assume that a "1" or "-1" result is an illegal pointer. */
6719 if (string_result == NULL)
6720 *number_result = retval_int;
6721 else if (retval_str != NULL
6722 && retval_str != (char_u *)1
6723 && retval_str != (char_u *)-1)
6724 *string_result = vim_strsave(retval_str);
6725 }
6726
6727# ifdef HAVE_SETJMP_H
6728 mch_endjmp();
6729# ifdef SIGHASARG
6730 if (lc_signal != 0)
6731 {
6732 int i;
6733
6734 /* try to find the name of this signal */
6735 for (i = 0; signal_info[i].sig != -1; i++)
6736 if (lc_signal == signal_info[i].sig)
6737 break;
6738 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
6739 }
6740# endif
6741# endif
6742
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006744 /* "dlerr" must be used before dlclose() */
6745 if (dlerr != NULL)
6746 EMSG2(_("dlerror = \"%s\""), dlerr);
6747
6748 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 (void)dlclose(hinstLib);
6750# else
6751 (void)shl_unload(hinstLib);
6752# endif
6753 }
6754
6755 if (!success)
6756 {
6757 EMSG2(_(e_libcall), funcname);
6758 return FAIL;
6759 }
6760
6761 return OK;
6762}
6763#endif
6764
6765#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
6766static int xterm_trace = -1; /* default: disabled */
6767static int xterm_button;
6768
6769/*
6770 * Setup a dummy window for X selections in a terminal.
6771 */
6772 void
6773setup_term_clip()
6774{
6775 int z = 0;
6776 char *strp = "";
6777 Widget AppShell;
6778
6779 if (!x_connect_to_server())
6780 return;
6781
6782 open_app_context();
6783 if (app_context != NULL && xterm_Shell == (Widget)0)
6784 {
6785 int (*oldhandler)();
6786#if defined(HAVE_SETJMP_H)
6787 int (*oldIOhandler)();
6788#endif
6789# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
6790 struct timeval start_tv;
6791
6792 if (p_verbose > 0)
6793 gettimeofday(&start_tv, NULL);
6794# endif
6795
6796 /* Ignore X errors while opening the display */
6797 oldhandler = XSetErrorHandler(x_error_check);
6798
6799#if defined(HAVE_SETJMP_H)
6800 /* Ignore X IO errors while opening the display */
6801 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
6802 mch_startjmp();
6803 if (SETJMP(lc_jump_env) != 0)
6804 {
6805 mch_didjmp();
6806 xterm_dpy = NULL;
6807 }
6808 else
6809#endif
6810 {
6811 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
6812 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
6813#if defined(HAVE_SETJMP_H)
6814 mch_endjmp();
6815#endif
6816 }
6817
6818#if defined(HAVE_SETJMP_H)
6819 /* Now handle X IO errors normally. */
6820 (void)XSetIOErrorHandler(oldIOhandler);
6821#endif
6822 /* Now handle X errors normally. */
6823 (void)XSetErrorHandler(oldhandler);
6824
6825 if (xterm_dpy == NULL)
6826 {
6827 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006828 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 return;
6830 }
6831
6832 /* Catch terminating error of the X server connection. */
6833 (void)XSetIOErrorHandler(x_IOerror_handler);
6834
6835# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
6836 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006837 {
6838 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 xopen_message(&start_tv);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006840 verbose_leave();
6841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842# endif
6843
6844 /* Create a Shell to make converters work. */
6845 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
6846 applicationShellWidgetClass, xterm_dpy,
6847 NULL);
6848 if (AppShell == (Widget)0)
6849 return;
6850 xterm_Shell = XtVaCreatePopupShell("VIM",
6851 topLevelShellWidgetClass, AppShell,
6852 XtNmappedWhenManaged, 0,
6853 XtNwidth, 1,
6854 XtNheight, 1,
6855 NULL);
6856 if (xterm_Shell == (Widget)0)
6857 return;
6858
6859 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02006860 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 if (x11_display == NULL)
6862 x11_display = xterm_dpy;
6863
6864 XtRealizeWidget(xterm_Shell);
6865 XSync(xterm_dpy, False);
6866 xterm_update();
6867 }
6868 if (xterm_Shell != (Widget)0)
6869 {
6870 clip_init(TRUE);
6871 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
6872 x11_window = (Window)atol(strp);
6873 /* Check if $WINDOWID is valid. */
6874 if (test_x11_window(xterm_dpy) == FAIL)
6875 x11_window = 0;
6876 if (x11_window != 0)
6877 xterm_trace = 0;
6878 }
6879}
6880
6881 void
6882start_xterm_trace(button)
6883 int button;
6884{
6885 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
6886 return;
6887 xterm_trace = 1;
6888 xterm_button = button;
6889 do_xterm_trace();
6890}
6891
6892
6893 void
6894stop_xterm_trace()
6895{
6896 if (xterm_trace < 0)
6897 return;
6898 xterm_trace = 0;
6899}
6900
6901/*
6902 * Query the xterm pointer and generate mouse termcodes if necessary
6903 * return TRUE if dragging is active, else FALSE
6904 */
6905 static int
6906do_xterm_trace()
6907{
6908 Window root, child;
6909 int root_x, root_y;
6910 int win_x, win_y;
6911 int row, col;
6912 int_u mask_return;
6913 char_u buf[50];
6914 char_u *strp;
6915 long got_hints;
6916 static char_u *mouse_code;
6917 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
6918 static int prev_row = 0, prev_col = 0;
6919 static XSizeHints xterm_hints;
6920
6921 if (xterm_trace <= 0)
6922 return FALSE;
6923
6924 if (xterm_trace == 1)
6925 {
6926 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00006927 * have changed recently. */
6928 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
6929 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 || xterm_hints.width_inc <= 1
6931 || xterm_hints.height_inc <= 1)
6932 {
6933 xterm_trace = -1; /* Not enough data -- disable tracing */
6934 return FALSE;
6935 }
6936
6937 /* Rely on the same mouse code for the duration of this */
6938 mouse_code = find_termcode(mouse_name);
6939 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02006940 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 xterm_trace = 2;
6942
6943 /* Find the offset of the chars, there might be a scrollbar on the
6944 * left of the window and/or a menu on the top (eterm etc.) */
6945 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
6946 &win_x, &win_y, &mask_return);
6947 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
6948 - (xterm_hints.height_inc / 2);
6949 if (xterm_hints.y <= xterm_hints.height_inc / 2)
6950 xterm_hints.y = 2;
6951 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
6952 - (xterm_hints.width_inc / 2);
6953 if (xterm_hints.x <= xterm_hints.width_inc / 2)
6954 xterm_hints.x = 2;
6955 return TRUE;
6956 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006957 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 {
6959 xterm_trace = 0;
6960 return FALSE;
6961 }
6962
6963 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
6964 &win_x, &win_y, &mask_return);
6965
6966 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
6967 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
6968 if (row == prev_row && col == prev_col)
6969 return TRUE;
6970
6971 STRCPY(buf, mouse_code);
6972 strp = buf + STRLEN(buf);
6973 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
6974 *strp++ = (char_u)(col + ' ' + 1);
6975 *strp++ = (char_u)(row + ' ' + 1);
6976 *strp = 0;
6977 add_to_input_buf(buf, STRLEN(buf));
6978
6979 prev_row = row;
6980 prev_col = col;
6981 return TRUE;
6982}
6983
6984# if defined(FEAT_GUI) || defined(PROTO)
6985/*
6986 * Destroy the display, window and app_context. Required for GTK.
6987 */
6988 void
6989clear_xterm_clip()
6990{
6991 if (xterm_Shell != (Widget)0)
6992 {
6993 XtDestroyWidget(xterm_Shell);
6994 xterm_Shell = (Widget)0;
6995 }
6996 if (xterm_dpy != NULL)
6997 {
Bram Moolenaare8208012008-06-20 09:59:25 +00006998# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 /* Lesstif and Solaris crash here, lose some memory */
7000 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007001# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 if (x11_display == xterm_dpy)
7003 x11_display = NULL;
7004 xterm_dpy = NULL;
7005 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007006# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 if (app_context != (XtAppContext)NULL)
7008 {
7009 /* Lesstif and Solaris crash here, lose some memory */
7010 XtDestroyApplicationContext(app_context);
7011 app_context = (XtAppContext)NULL;
7012 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007013# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014}
7015# endif
7016
7017/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007018 * Catch up with GUI or X events.
7019 */
7020 static void
7021clip_update()
7022{
7023# ifdef FEAT_GUI
7024 if (gui.in_use)
7025 gui_mch_update();
7026 else
7027# endif
7028 if (xterm_Shell != (Widget)0)
7029 xterm_update();
7030}
7031
7032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 * Catch up with any queued X events. This may put keyboard input into the
7034 * input buffer, call resize call-backs, trigger timers etc. If there is
7035 * nothing in the X event queue (& no timers pending), then we return
7036 * immediately.
7037 */
7038 static void
7039xterm_update()
7040{
7041 XEvent event;
7042
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007043 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007045 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007047 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007048 break;
7049
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007050 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007051 {
7052 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007053 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007054#ifdef FEAT_CLIENTSERVER
7055 {
7056 XPropertyEvent *e = (XPropertyEvent *)&event;
7057
7058 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007060 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007063 XtDispatchEvent(&event);
7064 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007065 else
7066 {
7067 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007068 XtAppProcessEvent(app_context, mask);
7069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 }
7071}
7072
7073 int
7074clip_xterm_own_selection(cbd)
7075 VimClipboard *cbd;
7076{
7077 if (xterm_Shell != (Widget)0)
7078 return clip_x11_own_selection(xterm_Shell, cbd);
7079 return FAIL;
7080}
7081
7082 void
7083clip_xterm_lose_selection(cbd)
7084 VimClipboard *cbd;
7085{
7086 if (xterm_Shell != (Widget)0)
7087 clip_x11_lose_selection(xterm_Shell, cbd);
7088}
7089
7090 void
7091clip_xterm_request_selection(cbd)
7092 VimClipboard *cbd;
7093{
7094 if (xterm_Shell != (Widget)0)
7095 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7096}
7097
7098 void
7099clip_xterm_set_selection(cbd)
7100 VimClipboard *cbd;
7101{
7102 clip_x11_set_selection(cbd);
7103}
7104#endif
7105
7106
7107#if defined(USE_XSMP) || defined(PROTO)
7108/*
7109 * Code for X Session Management Protocol.
7110 */
7111static void xsmp_handle_save_yourself __ARGS((SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast));
7112static void xsmp_die __ARGS((SmcConn smc_conn, SmPointer client_data));
7113static void xsmp_save_complete __ARGS((SmcConn smc_conn, SmPointer client_data));
7114static void xsmp_shutdown_cancelled __ARGS((SmcConn smc_conn, SmPointer client_data));
7115static void xsmp_ice_connection __ARGS((IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData));
7116
7117
7118# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7119static void xsmp_handle_interaction __ARGS((SmcConn smc_conn, SmPointer client_data));
7120
7121/*
7122 * This is our chance to ask the user if they want to save,
7123 * or abort the logout
7124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 static void
7126xsmp_handle_interaction(smc_conn, client_data)
7127 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007128 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129{
7130 cmdmod_T save_cmdmod;
7131 int cancel_shutdown = False;
7132
7133 save_cmdmod = cmdmod;
7134 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007135 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 /* Mustn't logout */
7137 cancel_shutdown = True;
7138 cmdmod = save_cmdmod;
7139 setcursor(); /* position cursor */
7140 out_flush();
7141
7142 /* Done interaction */
7143 SmcInteractDone(smc_conn, cancel_shutdown);
7144
7145 /* Finish off
7146 * Only end save-yourself here if we're not cancelling shutdown;
7147 * we'll get a cancelled callback later in which we'll end it.
7148 * Hopefully get around glitchy SMs (like GNOME-1)
7149 */
7150 if (!cancel_shutdown)
7151 {
7152 xsmp.save_yourself = False;
7153 SmcSaveYourselfDone(smc_conn, True);
7154 }
7155}
7156# endif
7157
7158/*
7159 * Callback that starts save-yourself.
7160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 static void
7162xsmp_handle_save_yourself(smc_conn, client_data, save_type,
7163 shutdown, interact_style, fast)
7164 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007165 SmPointer client_data UNUSED;
7166 int save_type UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 Bool shutdown;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007168 int interact_style UNUSED;
7169 Bool fast UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 /* Handle already being in saveyourself */
7172 if (xsmp.save_yourself)
7173 SmcSaveYourselfDone(smc_conn, True);
7174 xsmp.save_yourself = True;
7175 xsmp.shutdown = shutdown;
7176
7177 /* First up, preserve all files */
7178 out_flush();
7179 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7180
7181 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007182 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
7184# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7185 /* Now see if we can ask about unsaved files */
7186 if (shutdown && !fast && gui.in_use)
7187 /* Need to interact with user, but need SM's permission */
7188 SmcInteractRequest(smc_conn, SmDialogError,
7189 xsmp_handle_interaction, client_data);
7190 else
7191# endif
7192 {
7193 /* Can stop the cycle here */
7194 SmcSaveYourselfDone(smc_conn, True);
7195 xsmp.save_yourself = False;
7196 }
7197}
7198
7199
7200/*
7201 * Callback to warn us of imminent death.
7202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 static void
7204xsmp_die(smc_conn, client_data)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007205 SmcConn smc_conn UNUSED;
7206 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207{
7208 xsmp_close();
7209
7210 /* quit quickly leaving swapfiles for modified buffers behind */
7211 getout_preserve_modified(0);
7212}
7213
7214
7215/*
7216 * Callback to tell us that save-yourself has completed.
7217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 static void
7219xsmp_save_complete(smc_conn, client_data)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007220 SmcConn smc_conn UNUSED;
7221 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
7223 xsmp.save_yourself = False;
7224}
7225
7226
7227/*
7228 * Callback to tell us that an instigated shutdown was cancelled
7229 * (maybe even by us)
7230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 static void
7232xsmp_shutdown_cancelled(smc_conn, client_data)
7233 SmcConn smc_conn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007234 SmPointer client_data UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235{
7236 if (xsmp.save_yourself)
7237 SmcSaveYourselfDone(smc_conn, True);
7238 xsmp.save_yourself = False;
7239 xsmp.shutdown = False;
7240}
7241
7242
7243/*
7244 * Callback to tell us that a new ICE connection has been established.
7245 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 static void
7247xsmp_ice_connection(iceConn, clientData, opening, watchData)
7248 IceConn iceConn;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007249 IcePointer clientData UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 Bool opening;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007251 IcePointer *watchData UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252{
7253 /* Intercept creation of ICE connection fd */
7254 if (opening)
7255 {
7256 xsmp_icefd = IceConnectionNumber(iceConn);
7257 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7258 }
7259}
7260
7261
7262/* Handle any ICE processing that's required; return FAIL if SM lost */
7263 int
7264xsmp_handle_requests()
7265{
7266 Bool rep;
7267
7268 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7269 == IceProcessMessagesIOError)
7270 {
7271 /* Lost ICE */
7272 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007273 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 xsmp_close();
7275 return FAIL;
7276 }
7277 else
7278 return OK;
7279}
7280
7281static int dummy;
7282
7283/* Set up X Session Management Protocol */
7284 void
7285xsmp_init(void)
7286{
7287 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 SmcCallbacks smcallbacks;
7289#if 0
7290 SmPropValue smname;
7291 SmProp smnameprop;
7292 SmProp *smprops[1];
7293#endif
7294
7295 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007296 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297
7298 xsmp.save_yourself = xsmp.shutdown = False;
7299
7300 /* Set up SM callbacks - must have all, even if they're not used */
7301 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7302 smcallbacks.save_yourself.client_data = NULL;
7303 smcallbacks.die.callback = xsmp_die;
7304 smcallbacks.die.client_data = NULL;
7305 smcallbacks.save_complete.callback = xsmp_save_complete;
7306 smcallbacks.save_complete.client_data = NULL;
7307 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7308 smcallbacks.shutdown_cancelled.client_data = NULL;
7309
7310 /* Set up a watch on ICE connection creations. The "dummy" argument is
7311 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7312 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7313 {
7314 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007315 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 return;
7317 }
7318
7319 /* Create an SM connection */
7320 xsmp.smcconn = SmcOpenConnection(
7321 NULL,
7322 NULL,
7323 SmProtoMajor,
7324 SmProtoMinor,
7325 SmcSaveYourselfProcMask | SmcDieProcMask
7326 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
7327 &smcallbacks,
7328 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00007329 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 sizeof(errorstring),
7331 errorstring);
7332 if (xsmp.smcconn == NULL)
7333 {
7334 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00007335
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007337 {
7338 vim_snprintf(errorreport, sizeof(errorreport),
7339 _("XSMP SmcOpenConnection failed: %s"), errorstring);
7340 verb_msg((char_u *)errorreport);
7341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 return;
7343 }
7344 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
7345
7346#if 0
7347 /* ID ourselves */
7348 smname.value = "vim";
7349 smname.length = 3;
7350 smnameprop.name = "SmProgram";
7351 smnameprop.type = "SmARRAY8";
7352 smnameprop.num_vals = 1;
7353 smnameprop.vals = &smname;
7354
7355 smprops[0] = &smnameprop;
7356 SmcSetProperties(xsmp.smcconn, 1, smprops);
7357#endif
7358}
7359
7360
7361/* Shut down XSMP comms. */
7362 void
7363xsmp_close()
7364{
7365 if (xsmp_icefd != -1)
7366 {
7367 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00007368 if (xsmp.clientid != NULL)
7369 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00007370 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 xsmp_icefd = -1;
7372 }
7373}
7374#endif /* USE_XSMP */
7375
7376
7377#ifdef EBCDIC
7378/* Translate character to its CTRL- value */
7379char CtrlTable[] =
7380{
7381/* 00 - 5E */
7382 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7383 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7384 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7385 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7386 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7387 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7388/* ^ */ 0x1E,
7389/* - */ 0x1F,
7390/* 61 - 6C */
7391 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7392/* _ */ 0x1F,
7393/* 6E - 80 */
7394 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7395/* a */ 0x01,
7396/* b */ 0x02,
7397/* c */ 0x03,
7398/* d */ 0x37,
7399/* e */ 0x2D,
7400/* f */ 0x2E,
7401/* g */ 0x2F,
7402/* h */ 0x16,
7403/* i */ 0x05,
7404/* 8A - 90 */
7405 0, 0, 0, 0, 0, 0, 0,
7406/* j */ 0x15,
7407/* k */ 0x0B,
7408/* l */ 0x0C,
7409/* m */ 0x0D,
7410/* n */ 0x0E,
7411/* o */ 0x0F,
7412/* p */ 0x10,
7413/* q */ 0x11,
7414/* r */ 0x12,
7415/* 9A - A1 */
7416 0, 0, 0, 0, 0, 0, 0, 0,
7417/* s */ 0x13,
7418/* t */ 0x3C,
7419/* u */ 0x3D,
7420/* v */ 0x32,
7421/* w */ 0x26,
7422/* x */ 0x18,
7423/* y */ 0x19,
7424/* z */ 0x3F,
7425/* AA - AC */
7426 0, 0, 0,
7427/* [ */ 0x27,
7428/* AE - BC */
7429 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7430/* ] */ 0x1D,
7431/* BE - C0 */ 0, 0, 0,
7432/* A */ 0x01,
7433/* B */ 0x02,
7434/* C */ 0x03,
7435/* D */ 0x37,
7436/* E */ 0x2D,
7437/* F */ 0x2E,
7438/* G */ 0x2F,
7439/* H */ 0x16,
7440/* I */ 0x05,
7441/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
7442/* J */ 0x15,
7443/* K */ 0x0B,
7444/* L */ 0x0C,
7445/* M */ 0x0D,
7446/* N */ 0x0E,
7447/* O */ 0x0F,
7448/* P */ 0x10,
7449/* Q */ 0x11,
7450/* R */ 0x12,
7451/* DA - DF */ 0, 0, 0, 0, 0, 0,
7452/* \ */ 0x1C,
7453/* E1 */ 0,
7454/* S */ 0x13,
7455/* T */ 0x3C,
7456/* U */ 0x3D,
7457/* V */ 0x32,
7458/* W */ 0x26,
7459/* X */ 0x18,
7460/* Y */ 0x19,
7461/* Z */ 0x3F,
7462/* EA - FF*/ 0, 0, 0, 0, 0, 0,
7463 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7464};
7465
7466char MetaCharTable[]=
7467{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7468 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
7469 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
7470 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
7471 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
7472};
7473
7474
7475/* TODO: Use characters NOT numbers!!! */
7476char CtrlCharTable[]=
7477{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7478 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
7479 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
7480 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
7481 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
7482};
7483
7484
7485#endif